From 2ffccc3e90d93f18aa4e0ad8aac6a80e0f5b7aff Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 06:07:34 -0800 Subject: [PATCH 1/2] SVG export: feet, yards, meters, Q and Ha documents keep their printed size Since #8610, a document in millimeters, centimeters, inches or picas is exported with that unit on the root width and height. A document in feet, feet and inches, yards, meters, Q or Ha still got unitless point values, which SVG readers take as CSS pixels at 96 per inch, so the art opened at 75% of its size. These units have no SVG unit identifier, so the export now writes feet and yards in inches, meters in centimeters, and Q and Ha in millimeters. The viewBox stays in points, and VectorCraft reads the file back at the same size. --- crates/doc/src/units.rs | 12 ++++++++++++ crates/svg/src/export.rs | 6 +++--- crates/svg/tests/it/import_units.rs | 25 +++++++++++++++++++++++++ docs/gaps.md | 2 +- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/crates/doc/src/units.rs b/crates/doc/src/units.rs index 728c8aa816..f65fa1fd10 100644 --- a/crates/doc/src/units.rs +++ b/crates/doc/src/units.rs @@ -96,6 +96,18 @@ impl Unit { _ => None, } } + /// The SVG length unit an export writes this unit's sizes in: the unit itself when SVG has it, + /// else an SVG unit of the same kind (inches for feet and yards, centimeters for meters, + /// millimeters for Q and Ha). `None` for points and pixels. + pub fn svg_unit(self) -> Option { + match self { + Unit::Millimeters | Unit::Centimeters | Unit::Inches | Unit::Picas => Some(self), + Unit::FeetInches | Unit::Feet | Unit::Yards => Some(Unit::Inches), + Unit::Meters => Some(Unit::Centimeters), + Unit::Q | Unit::Ha => Some(Unit::Millimeters), + Unit::Points | Unit::Pixels => None, + } + } pub const fn label(self) -> &'static str { match self { Unit::Points => "Points", diff --git a/crates/svg/src/export.rs b/crates/svg/src/export.rs index 84b2efa32b..e0f5540b7d 100644 --- a/crates/svg/src/export.rs +++ b/crates/svg/src/export.rs @@ -195,9 +195,9 @@ fn write(doc: &Document, opts: &ExportOptions, native: Option<&[u8]>, id_prefix: root = root.attr("version", "1.2").attr("baseProfile", "tiny"); } if !opts.responsive { - if let Some(suffix) = doc.units.svg_suffix() { - let w_val = physical_num(doc.units.from_pt(rect.width())); - let h_val = physical_num(doc.units.from_pt(rect.height())); + if let Some((unit, suffix)) = doc.units.svg_unit().and_then(|u| Some((u, u.svg_suffix()?))) { + let w_val = physical_num(unit.from_pt(rect.width())); + let h_val = physical_num(unit.from_pt(rect.height())); root = root.attr("width", format_args!("{w_val}{suffix}")).attr("height", format_args!("{h_val}{suffix}")); } else { root = root.attr("width", &ww).attr("height", &hh); diff --git a/crates/svg/tests/it/import_units.rs b/crates/svg/tests/it/import_units.rs index 0140741aeb..91138d8a93 100644 --- a/crates/svg/tests/it/import_units.rs +++ b/crates/svg/tests/it/import_units.rs @@ -192,3 +192,28 @@ fn export_physical_units_writes_unit_suffixes() { let back = import(&s).unwrap(); assert_eq!(back.units, Unit::Millimeters); } + +/// The `width` of `svg`'s root as a length in points, read by the SVG and CSS absolute units +/// (96 px per inch). `None` when it has no unit an SVG reader takes as physical. +fn root_width_in_points(svg: &str) -> Option { + let start = svg.find("().ok().map(|v| v * points_per_unit) +} + +#[test] +fn every_physical_document_unit_exports_its_printed_size() { + // 100 inches by 50: feet, yards, meters, Q and Ha have no SVG unit of their own. + for unit in [Unit::Feet, Unit::FeetInches, Unit::Yards, Unit::Meters, Unit::Q, Unit::Ha, Unit::Inches, Unit::Millimeters] { + let mut d = Document::new(7200.0, 3600.0); + d.units = unit; + let s = export(&d, &ExportOptions::default()); + let width = root_width_in_points(&s).unwrap_or_else(|| panic!("{unit:?}: no physical width in {s}")); + assert!((width - 7200.0).abs() < 0.01, "{unit:?}: {width} pt in {s}"); + let back = import(&s).unwrap(); + assert!(close_rect(back.artboards[0].rect, d.artboards[0].rect), "{unit:?}: {:?}", back.artboards[0].rect); + } +} diff --git a/docs/gaps.md b/docs/gaps.md index b502f9e146..0dcd5ac37b 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -442,7 +442,7 @@ macOS cursor delivery (#2292) is narrowed to a native host adapter using the exi such stubs into one vertex (within 4× the sweep's tolerance up and down its frame and 1/16 of the 0.01 pt precision along it, bounded by each cluster's extent so a run of tiny level segments keeps its length); regression tests in `crates/pathops/src/planar.rs`. Estimate unchanged. -- **Done (#864):** SVG export writes physical unit suffixes on the root `` for documents with physical units (`mm`, `cm`, `in`, `pc`), so external vector tools (Inkscape, LightBurn) and browsers render drawings at their intended physical scale rather than scaling to 75% under point-based user-unit assumptions; unitless user coordinates in `viewBox` and path geometry remain in 1:1 internal points per SVG 1.1 coords §7.10, preserving document units on round trips (`export_physical_units_writes_unit_suffixes`, `svg_round_trip_preserves_document_units`). General document unit preferences across restarts remain tracked under #864. +- **Done (#864):** SVG export writes physical unit suffixes on the root `` for documents with physical units (`mm`, `cm`, `in`, `pc`; documents in feet and yards are written in `in`, in meters in `cm`, and in Q or Ha in `mm`, since SVG has no unit for them: `every_physical_document_unit_exports_its_printed_size`), so external vector tools (Inkscape, LightBurn) and browsers render drawings at their intended physical scale rather than scaling to 75% under point-based user-unit assumptions; unitless user coordinates in `viewBox` and path geometry remain in 1:1 internal points per SVG 1.1 coords §7.10, preserving document units on round trips (`export_physical_units_writes_unit_suffixes`, `svg_round_trip_preserves_document_units`). General document unit preferences across restarts remain tracked under #864. - **Missing:** general document unit preference on restart (#864; SVG export physical units done). Shape Builder regions (#937, closed), PDF text placement (#722, above), the bezier drag preview (#834, #3065, #3729) and deleting an interior anchor of an open path (above) are done. From f49734f5ec92d66f932f2e084f989314d650b8b2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 08:57:54 -0800 Subject: [PATCH 2/2] Docs: SVG round trips keep mm, cm, in and pc; other units keep their printed size --- docs/gaps.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/gaps.md b/docs/gaps.md index 0dcd5ac37b..0c9937458b 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -442,7 +442,7 @@ macOS cursor delivery (#2292) is narrowed to a native host adapter using the exi such stubs into one vertex (within 4× the sweep's tolerance up and down its frame and 1/16 of the 0.01 pt precision along it, bounded by each cluster's extent so a run of tiny level segments keeps its length); regression tests in `crates/pathops/src/planar.rs`. Estimate unchanged. -- **Done (#864):** SVG export writes physical unit suffixes on the root `` for documents with physical units (`mm`, `cm`, `in`, `pc`; documents in feet and yards are written in `in`, in meters in `cm`, and in Q or Ha in `mm`, since SVG has no unit for them: `every_physical_document_unit_exports_its_printed_size`), so external vector tools (Inkscape, LightBurn) and browsers render drawings at their intended physical scale rather than scaling to 75% under point-based user-unit assumptions; unitless user coordinates in `viewBox` and path geometry remain in 1:1 internal points per SVG 1.1 coords §7.10, preserving document units on round trips (`export_physical_units_writes_unit_suffixes`, `svg_round_trip_preserves_document_units`). General document unit preferences across restarts remain tracked under #864. +- **Done (#864):** SVG export writes physical unit suffixes on the root `` for documents with physical units (`mm`, `cm`, `in`, `pc`; documents in feet and yards are written in `in`, in meters in `cm`, and in Q or Ha in `mm`, since SVG has no unit for them: `every_physical_document_unit_exports_its_printed_size`), so external vector tools (Inkscape, LightBurn) and browsers render drawings at their intended physical scale rather than scaling to 75% under point-based user-unit assumptions; unitless user coordinates in `viewBox` and path geometry remain in 1:1 internal points per SVG 1.1 coords §7.10. A round trip keeps `mm`, `cm`, `in` and `pc` documents in their unit (`export_physical_units_writes_unit_suffixes`, `svg_round_trip_preserves_document_units`); documents in feet, yards, meters, Q or Ha keep their printed size and open again in the SVG unit they were written in. General document unit preferences across restarts remain tracked under #864. - **Missing:** general document unit preference on restart (#864; SVG export physical units done). Shape Builder regions (#937, closed), PDF text placement (#722, above), the bezier drag preview (#834, #3065, #3729) and deleting an interior anchor of an open path (above) are done.