Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(text): keep tiny EngineData reals parseable (#979) (#1085)

Minghang Chamling925bdfe335b7
diff --git a/crates/text/src/engine_data.rs b/crates/text/src/engine_data.rs
index c74986a0b..d5731f663 100644
--- a/crates/text/src/engine_data.rs
+++ b/crates/text/src/engine_data.rs
@@ -319,6 +319,9 @@ fn format_real(r: f64) -> String {
while s.ends_with('0') {
s.pop();
}
+ if matches!(s.as_str(), "0." | "-0.") {
+ return "0.0".into();
+ }
if let Some(rest) = s.strip_prefix("0.") {
s = format!(".{rest}");
} else if let Some(rest) = s.strip_prefix("-0.") {
@@ -361,6 +364,26 @@ mod tests {
assert_eq!(format_real(12.125), "12.125");
}
+ #[test]
+ fn tiny_reals_round_trip_as_numbers() {
+ for value in [0.000004, -0.000004] {
+ assert_eq!(format_real(value), "0.0");
+ let source = Value::Dict(vec![(
+ "EngineDict".into(),
+ Value::Dict(vec![("StyleRun".into(), Value::Array(vec![Value::Dict(vec![("BaselineShift".into(), Value::Real(value))])]))]),
+ )]);
+ let encoded = write(&source);
+ let parsed = parse(&encoded).expect("formatted tiny real should parse");
+ let baseline_shift = parsed
+ .path(&["EngineDict", "StyleRun"])
+ .and_then(Value::as_array)
+ .and_then(|runs| runs.first())
+ .and_then(|run| run.get("BaselineShift"))
+ .and_then(Value::as_f64);
+ assert_eq!(baseline_shift, Some(0.0));
+ }
+ }
+
#[test]
fn malformed_input_never_panics() {
for cut in 0..SAMPLE.len() {