From cc8bfd60dff791350aea48e2cf1da2fcd1e15b45 Mon Sep 17 00:00:00 2001 From: KenM76 <172507185+KenM76@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:49:17 -0500 Subject: [PATCH] fix(measure): keep a decimal comma on single-unit scales; one page walk per listing A single-unit decimal scale was read as plain, which dropped its number formats and with them a decimal mark other than the point, so angles (and lengths) were labelled with a point. Such a scale now keeps its formats. A single-unit scale with a decimal point reads as before. list looked each angle without /Measure up through scale_at, which walked the page tree again per angle. It now hands the page it is already on to the same viewport lookup; scale_at and scale_at_or keep their signatures and behaviour. Co-Authored-By: Claude Opus 5.5 --- crates/measure/src/lib.rs | 30 ++++++++++++++++++++++++------ crates/measure/src/tests.rs | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/crates/measure/src/lib.rs b/crates/measure/src/lib.rs index 55b35e0c8b..9beb16e061 100644 --- a/crates/measure/src/lib.rs +++ b/crates/measure/src/lib.rs @@ -86,7 +86,7 @@ pub struct Scale { /// or whose values show as fractions or cut off to whole units (ISO 32000-2 §12.9.2): /// readings are labelled with them, and a measurement added with this scale writes them /// back. `unit`, `area_unit` and the factors are their first units'. `None` for single-unit - /// decimal scales, which `unit`, `area_unit` and `precision` describe. + /// decimal scales with a decimal point, which `unit`, `area_unit` and `precision` describe. #[serde(skip_serializing_if = "Option::is_none")] pub formats: Option, } @@ -260,7 +260,11 @@ impl Scale { } let areas = format(b"A")?; let (area_unit, area_factor) = first(&areas)?; - let plain = [&xs, &distances, &areas].into_iter().chain(ys.as_ref()).all(|a| number_format::plain_precision(a).is_some()); + // A decimal mark other than the point is kept with the formats, so readings and angles show it. + let plain = [&xs, &distances, &areas] + .into_iter() + .chain(ys.as_ref()) + .all(|a| number_format::plain_precision(a).is_some() && a.iter().all(|f| f.decimal == ".")); // The decimals a distance shows: of its only unit, or of its last one when compound. let precision = number_format::plain_precision(&distances) .or_else(|| { @@ -443,6 +447,8 @@ fn nums(v: impl IntoIterator) -> Object { Object::Array(v.into_iter().map(Object::Real).collect()) } fn page(doc: &Document, index: usize) -> Result { + #[cfg(test)] + PAGE_LOOKUPS.with(|n| n.set(n.get() + 1)); pdfcraft_model::pages(doc).get(index).cloned().ok_or_else(|| invalid("measurement page does not exist")) } fn annot(doc: &Document, page_index: usize, index: usize) -> Result<(ObjRef, Dict)> { @@ -571,7 +577,14 @@ fn scale_of(doc: &Document, d: &Dict, scales: &mut Scales) -> Result { } } -fn read_measurement(doc: &Document, d: &Dict, kind: Kind, page: usize, index: usize, scales: &mut Scales) -> Result { +fn read_measurement( + doc: &Document, + d: &Dict, + kind: Kind, + (page, p): (usize, &pdfcraft_model::Page), + index: usize, + scales: &mut Scales, +) -> Result { // An angle saved without `/Measure` takes its decimal mark from the scale in force at its first // point when it is read, which a viewport added since can change. let saved = if kind == Kind::Angle && !d.contains(b"Measure") { None } else { Some(scale_of(doc, d, scales)?) }; @@ -590,7 +603,7 @@ fn read_measurement(doc: &Document, d: &Dict, kind: Kind, page: usize, index: us validate_geometry(kind, &points)?; let scale = match saved { Some(scale) => scale, - None => points.first().and_then(|at| scale_at(doc, page, *at).ok()).unwrap_or_default(), + None => points.first().and_then(|at| scale_on(doc, p, *at, 1.0 / 72.0, "in").ok()).unwrap_or_default(), }; let reading = reading(kind, &points, &scale)?; Ok(Measurement { @@ -666,7 +679,7 @@ pub fn list(doc: &Document) -> Listing { out.truncated = true; return out; } - match read_measurement(doc, d, kind, page, index, &mut scales) { + match read_measurement(doc, d, kind, (page, p), index, &mut scales) { Ok(m) => { kept = kept.saturating_add(retained(&m)); if kept > MAX_LISTED_BYTES { @@ -688,6 +701,8 @@ const MAX_VIEWPORTS: usize = 1024; thread_local! { /// Numbers read from viewport `/BBox` arrays. pub(crate) static VIEWPORT_NUMBERS_READ: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// Page trees walked to find one page. + pub(crate) static PAGE_LOOKUPS: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// Last containing viewport wins, using the first point of the measurement (ISO 32000-2 §12.9). @@ -699,7 +714,10 @@ pub fn scale_at(doc: &Document, page_index: usize, at: Point) -> Result { /// are `per_point` in a PDF point (25.4 / 72 for millimetres). pub fn scale_at_or(doc: &Document, page_index: usize, at: Point, per_point: f64, unit: &str) -> Result { check_points(&[at])?; - let p = page(doc, page_index)?; + scale_on(doc, &page(doc, page_index)?, at, per_point, unit) +} +/// [`scale_at_or`] on a page already found, so a listing walks the page tree once. +fn scale_on(doc: &Document, p: &pdfcraft_model::Page, at: Point, per_point: f64, unit: &str) -> Result { if let Some(o) = p.dict.get(b"VP") { let o = doc.resolve(o); let arr = o.as_array().ok_or_else(|| invalid("invalid page viewports"))?; diff --git a/crates/measure/src/tests.rs b/crates/measure/src/tests.rs index 2b1321dc5d..a94a5b82fe 100644 --- a/crates/measure/src/tests.rs +++ b/crates/measure/src/tests.rs @@ -820,8 +820,7 @@ fn an_angle_saved_without_a_scale_keeps_its_points() { #[test] fn an_angle_saved_without_a_scale_takes_the_decimal_mark_of_the_viewport_at_its_first_point() { - // A two-unit format keeps its separators (a single-unit decimal scale is read for its - // precision alone), so the viewport's comma reaches the angle. + // A two-unit format keeps its separators, so the viewport's comma reaches the angle. let m_cm = "[<< /U (m) /C 0.000352778 /RD (,) >> << /U (cm) /C 100 /D 10 /RD (,) >>]"; let measure = format!("<< /Type /Measure /Subtype /RL /R (1:1) /X {m_cm} /D {m_cm} /A [<< /U (sq m) /C 1 /RD (,) >>] >>"); let viewport = format!("/VP [<< /Type /Viewport /BBox [0 0 300 300] /Measure {measure} >>]"); @@ -833,3 +832,34 @@ fn an_angle_saved_without_a_scale_takes_the_decimal_mark_of_the_viewport_at_its_ let outside = fixture("", "/Annots [5 0 R]", &[angle]); assert_eq!(list(&outside).measurements[0].reading.label, "90.0\u{b0}"); } + +#[test] +fn a_single_unit_scale_with_a_decimal_comma_labels_angles_and_lengths_with_it() { + let x = "[<< /U (m) /C 0.000352778 /D 100 /RD (,) >>]"; + let measure = + format!("<< /Type /Measure /Subtype /RL /R (1:1) /X {x} /D [<< /U (m) /C 1 /D 100 /RD (,) >>] /A [<< /U (sq m) /C 1 /RD (,) >>] >>"); + let viewport = format!("/VP [<< /Type /Viewport /BBox [0 0 300 300] /Measure {measure} >>]"); + let angle = "<< /Type /Annot /Subtype /PolyLine /Rect [90 90 210 210] /PCMeasureKind /Angle /Vertices [200 100 100 100 100 200] >>"; + let d = fixture("", &format!("/Annots [5 0 R] {viewport}"), &[angle]); + let listing = list(&d); + let [m] = &listing.measurements[..] else { panic!("{listing:?}") }; + assert_eq!(m.reading.label, "90,0\u{b0}"); + let scale = scale_at(&d, 0, [100.0, 100.0]).unwrap(); + let line = reading(Kind::Distance, &[[0.0, 0.0], [720.0, 0.0]], &scale).unwrap(); + assert!(line.label.starts_with("0,25"), "{line:?}"); +} + +#[test] +fn listing_angles_without_a_scale_walks_the_page_tree_once() { + let angle = |n: u32| { + format!("<< /Type /Annot /Subtype /PolyLine /Rect [90 90 210 210] /PCMeasureKind /Angle /NM (a{n}) /Vertices [200 100 100 100 100 200] >>") + }; + let angles = [angle(1), angle(2), angle(3)]; + let d = fixture("", "/Annots [5 0 R 6 0 R 7 0 R]", &angles.iter().map(String::as_str).collect::>()); + PAGE_LOOKUPS.with(|c| c.set(0)); + let listing = list(&d); + assert_eq!(listing.measurements.len(), 3, "{listing:?}"); + assert_eq!(PAGE_LOOKUPS.with(std::cell::Cell::get), 0); + // The public lookup still finds its page. + assert_eq!(scale_at(&d, 0, [100.0, 100.0]).unwrap().unit, "in"); +}