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.
This commit is contained in:
dotCooCoo committed 2026-11-25 06:07:34 -08:00
1 parent 2a5dbc479f
commit 2ffccc3e90
4 files changed
+41 -4

No files matched your search

+12
View File
@@ -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<Unit> {
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",
+3 -3
View File
@@ -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);
+25
View File
@@ -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<f64> {
let start = svg.find("<svg")?;
let w = svg[start..].split("width=\"").nth(1)?.split('"').next()?;
let (points_per_unit, number) = [("mm", 72.0 / 25.4), ("cm", 72.0 / 2.54), ("in", 72.0), ("pc", 12.0), ("pt", 1.0)]
.into_iter()
.find_map(|(suffix, pt)| w.strip_suffix(suffix).map(|v| (pt, v)))?;
number.parse::<f64>().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);
}
}
+1 -1
View File
@@ -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 `<svg width="..." height="...">` 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 `<svg width="..." height="...">` 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.