mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 14:06:09 +00:00
* SVG export: preserve document unit in viewBox and width/height (#864) When exporting SVG documents with physical units (mm, cm, in, pc), emit the physical unit suffix on the root <svg width=... height=...> attributes while keeping viewBox numbers and inner coordinates in 1:1 points per SVG 1.1 coords §7.10. This avoids the 75% scaling mismatch when opening drawings in external tools such as Inkscape or LightBurn, and preserves the document unit setting across save/open round trips. Unitless documents (points, pixels) continue to emit unitless dimensions. Tested via export_physical_units_writes_unit_suffixes in vectorcraft-svg and full round-trip svg_round_trip_preserves_document_units in vectorcraft-engine. Ran cargo xtask ci (fmt, clippy, tests, xtask gates). * SVG export: preserve nonzero width and viewBox on extreme small dimensions
This commit is contained in:
@@ -81,6 +81,21 @@ impl Unit {
|
|||||||
Unit::Ha => "H",
|
Unit::Ha => "H",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Whether this unit is a physical measurement unit (not points or pixels).
|
||||||
|
pub fn is_physical(self) -> bool {
|
||||||
|
self.svg_suffix().is_some() || matches!(self, Unit::FeetInches | Unit::Meters | Unit::Yards | Unit::Feet | Unit::Q | Unit::Ha)
|
||||||
|
}
|
||||||
|
/// The standard SVG length unit suffix, if this unit is a physical unit supported in SVG
|
||||||
|
/// (`mm`, `cm`, `in`, `pc`).
|
||||||
|
pub fn svg_suffix(self) -> Option<&'static str> {
|
||||||
|
match self {
|
||||||
|
Unit::Millimeters => Some("mm"),
|
||||||
|
Unit::Centimeters => Some("cm"),
|
||||||
|
Unit::Inches => Some("in"),
|
||||||
|
Unit::Picas => Some("pc"),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
pub const fn label(self) -> &'static str {
|
pub const fn label(self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
Unit::Points => "Points",
|
Unit::Points => "Points",
|
||||||
|
|||||||
@@ -437,3 +437,48 @@ fn svg_darkness_uses_rendered_shadow_in_non_srgb_working_space() {
|
|||||||
assert!(a.iter().zip(b).all(|(a, b)| a.abs_diff(b) <= 8), "at {x}: {a:?} vs {b:?}");
|
assert!(a.iter().zip(b).all(|(a, b)| a.abs_diff(b) <= 8), "at {x}: {a:?} vs {b:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// #864 / G11: SVG export preserves the document's physical units on the root `width`/`height` so
|
||||||
|
/// that the unit setting survives a save/open round-trip. The engine-level test covers the full
|
||||||
|
/// stack (file.new → document.setUnits → document.serialize → document.open).
|
||||||
|
#[test]
|
||||||
|
fn svg_round_trip_preserves_document_units() {
|
||||||
|
// (unit name the engine accepts, expected unit after re-import)
|
||||||
|
let cases: &[(&str, vectorcraft_doc::Unit)] = &[
|
||||||
|
("millimeters", vectorcraft_doc::Unit::Millimeters),
|
||||||
|
("centimeters", vectorcraft_doc::Unit::Centimeters),
|
||||||
|
("inches", vectorcraft_doc::Unit::Inches),
|
||||||
|
("picas", vectorcraft_doc::Unit::Picas),
|
||||||
|
// Points and pixels both serialise as bare user-unit numbers, so both import as Pixels.
|
||||||
|
("pixels", vectorcraft_doc::Unit::Pixels),
|
||||||
|
];
|
||||||
|
for (name, expected) in cases {
|
||||||
|
let mut s = Session::new();
|
||||||
|
s.execute("file.new", &json!({"width": 200, "height": 100, "artboards": 1})).unwrap();
|
||||||
|
s.execute("document.setUnits", &json!({"units": name})).unwrap();
|
||||||
|
let r = s.execute("document.serialize", &json!({"format": "svg"})).unwrap();
|
||||||
|
let svg_text = r["text"].as_str().unwrap();
|
||||||
|
match *expected {
|
||||||
|
vectorcraft_doc::Unit::Millimeters => assert!(svg_text.contains("mm\""), "mm suffix missing: {svg_text}"),
|
||||||
|
vectorcraft_doc::Unit::Centimeters => assert!(svg_text.contains("cm\""), "cm suffix missing: {svg_text}"),
|
||||||
|
vectorcraft_doc::Unit::Inches => assert!(svg_text.contains("in\""), "in suffix missing: {svg_text}"),
|
||||||
|
vectorcraft_doc::Unit::Picas => assert!(svg_text.contains("pc\""), "pc suffix missing: {svg_text}"),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
let b64 = vectorcraft_format::base64_encode(svg_text.as_bytes());
|
||||||
|
s.execute("document.open", &json!({"name": "test.svg", "dataBase64": b64})).unwrap();
|
||||||
|
let doc_state = s.doc().unwrap();
|
||||||
|
assert_eq!(doc_state.doc.units, *expected, "unit mismatch after round-trip for '{name}'");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Points serialise without suffix and re-open as Pixels (equivalent in user units).
|
||||||
|
let mut s = Session::new();
|
||||||
|
s.execute("file.new", &json!({"width": 200, "height": 100, "artboards": 1})).unwrap();
|
||||||
|
s.execute("document.setUnits", &json!({"units": "points"})).unwrap();
|
||||||
|
let r = s.execute("document.serialize", &json!({"format": "svg"})).unwrap();
|
||||||
|
let svg_text = r["text"].as_str().unwrap();
|
||||||
|
assert!(svg_text.contains("width=\"200\" height=\"100\""), "{svg_text}");
|
||||||
|
let b64 = vectorcraft_format::base64_encode(svg_text.as_bytes());
|
||||||
|
s.execute("document.open", &json!({"name": "test.svg", "dataBase64": b64})).unwrap();
|
||||||
|
assert_eq!(s.doc().unwrap().doc.units, vectorcraft_doc::Unit::Pixels);
|
||||||
|
}
|
||||||
|
|||||||
@@ -78,6 +78,18 @@ fn update_editing_hash(svg: &mut String) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Format physical dimensions (inches, mm, cm) with enough precision and significant digits
|
||||||
|
/// that small nonzero sizes do not round to zero or distort relative aspect ratio.
|
||||||
|
fn physical_num(v: f64) -> String {
|
||||||
|
if !v.is_finite() || v <= 0.0 {
|
||||||
|
return "0".into();
|
||||||
|
}
|
||||||
|
let leading_zeros = (-v.log10().floor() as i32).max(0);
|
||||||
|
let decimals = (leading_zeros + 3).clamp(3, 14) as u8;
|
||||||
|
let s = fmt_num(v, decimals);
|
||||||
|
if s == "0" && v > 0.0 { format!("{v:.16}").trim_end_matches('0').trim_end_matches('.').to_string() } else { s }
|
||||||
|
}
|
||||||
|
|
||||||
/// `doc` as SVG with `opts`, the native file `native` embedded for Preserve Editing, its generated
|
/// `doc` as SVG with `opts`, the native file `native` embedded for Preserve Editing, its generated
|
||||||
/// ids starting with `id_prefix`.
|
/// ids starting with `id_prefix`.
|
||||||
fn write(doc: &Document, opts: &ExportOptions, native: Option<&[u8]>, id_prefix: &str) -> Output {
|
fn write(doc: &Document, opts: &ExportOptions, native: Option<&[u8]>, id_prefix: &str) -> Output {
|
||||||
@@ -183,10 +195,17 @@ fn write(doc: &Document, opts: &ExportOptions, native: Option<&[u8]>, id_prefix:
|
|||||||
root = root.attr("version", "1.2").attr("baseProfile", "tiny");
|
root = root.attr("version", "1.2").attr("baseProfile", "tiny");
|
||||||
}
|
}
|
||||||
if !opts.responsive {
|
if !opts.responsive {
|
||||||
// Unitless width/height stay in this one branch so #7105 (physical unit suffixes) can rebase here.
|
if let Some(suffix) = doc.units.svg_suffix() {
|
||||||
root = root.attr("width", &ww).attr("height", &hh);
|
let w_val = physical_num(doc.units.from_pt(rect.width()));
|
||||||
|
let h_val = physical_num(doc.units.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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
root.attr("viewBox", format_args!("0 0 {ww} {hh}")).open();
|
let vb_w = if rect.width() > 0.0 && ww == "0" { physical_num(rect.width()) } else { ww.clone() };
|
||||||
|
let vb_h = if rect.height() > 0.0 && hh == "0" { physical_num(rect.height()) } else { hh.clone() };
|
||||||
|
root.attr("viewBox", format_args!("0 0 {vb_w} {vb_h}")).open();
|
||||||
let mut out = svg.as_str().to_string();
|
let mut out = svg.as_str().to_string();
|
||||||
out.push_str(nl);
|
out.push_str(nl);
|
||||||
let css = opts.styling == Styling::InternalCss && !w.classes.is_empty();
|
let css = opts.styling == Styling::InternalCss && !w.classes.is_empty();
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
use vectorcraft_doc::{Document, Node, NodeKind, Unit};
|
use vectorcraft_doc::{Document, Node, NodeKind, Unit};
|
||||||
use vectorcraft_geom::Rect;
|
use vectorcraft_geom::Rect;
|
||||||
use vectorcraft_svg::import;
|
use vectorcraft_svg::{ExportOptions, export, import};
|
||||||
|
|
||||||
fn art(d: &Document) -> Vec<&Node> {
|
fn art(d: &Document) -> Vec<&Node> {
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
@@ -130,3 +130,65 @@ fn absolute_lengths_in_a_pixel_document_keep_their_size() {
|
|||||||
assert!(close_rect(n.geometric_bounds().unwrap(), Rect::new(0.0, 0.0, 72.0, 720.0 / 25.4)), "{:?}", n.geometric_bounds());
|
assert!(close_rect(n.geometric_bounds().unwrap(), Rect::new(0.0, 0.0, 72.0, 720.0 / 25.4)), "{:?}", n.geometric_bounds());
|
||||||
assert!(close(n.appearance.stroke_width(), 2.0));
|
assert!(close(n.appearance.stroke_width(), 2.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn export_physical_units_writes_unit_suffixes() {
|
||||||
|
// Millimeters (#864 / G11: avoid 75% scale in external tools like LightBurn, LaserGRBL, Inkscape)
|
||||||
|
let mut d = Document::new(50.0 * 72.0 / 25.4, 30.0 * 72.0 / 25.4);
|
||||||
|
d.units = Unit::Millimeters;
|
||||||
|
let s = export(&d, &ExportOptions::default());
|
||||||
|
assert!(s.contains("width=\"50mm\" height=\"30mm\""), "{s}");
|
||||||
|
assert!(s.contains("viewBox=\"0 0 141.732 85.039\""), "{s}");
|
||||||
|
|
||||||
|
// Inches
|
||||||
|
let mut d = Document::new(8.5 * 72.0, 11.0 * 72.0);
|
||||||
|
d.units = Unit::Inches;
|
||||||
|
let s = export(&d, &ExportOptions::default());
|
||||||
|
assert!(s.contains("width=\"8.5in\" height=\"11in\""), "{s}");
|
||||||
|
assert!(s.contains("viewBox=\"0 0 612 792\""), "{s}");
|
||||||
|
|
||||||
|
// Centimeters
|
||||||
|
let mut d = Document::new(5.0 * 72.0 / 2.54, 3.0 * 72.0 / 2.54);
|
||||||
|
d.units = Unit::Centimeters;
|
||||||
|
let s = export(&d, &ExportOptions::default());
|
||||||
|
assert!(s.contains("width=\"5cm\" height=\"3cm\""), "{s}");
|
||||||
|
|
||||||
|
// Picas
|
||||||
|
let mut d = Document::new(10.0 * 12.0, 20.0 * 12.0);
|
||||||
|
d.units = Unit::Picas;
|
||||||
|
let s = export(&d, &ExportOptions::default());
|
||||||
|
assert!(s.contains("width=\"10pc\" height=\"20pc\""), "{s}");
|
||||||
|
|
||||||
|
// Points and pixels remain unitless
|
||||||
|
let mut d = Document::new(100.0, 200.0);
|
||||||
|
d.units = Unit::Points;
|
||||||
|
let s = export(&d, &ExportOptions::default());
|
||||||
|
assert!(s.contains("width=\"100\" height=\"200\""), "{s}");
|
||||||
|
|
||||||
|
d.units = Unit::Pixels;
|
||||||
|
let s = export(&d, &ExportOptions::default());
|
||||||
|
assert!(s.contains("width=\"100\" height=\"200\""), "{s}");
|
||||||
|
|
||||||
|
// Responsive export omits width and height even with physical units
|
||||||
|
d.units = Unit::Millimeters;
|
||||||
|
let s = export(&d, &ExportOptions { responsive: true, ..Default::default() });
|
||||||
|
assert!(!s.contains("width="), "{s}");
|
||||||
|
assert!(!s.contains("height="), "{s}");
|
||||||
|
|
||||||
|
// Non-zero small dimensions do not round to zero
|
||||||
|
let mut d = Document::new(0.001 * 72.0 / 25.4, 0.002 * 72.0 / 25.4);
|
||||||
|
d.units = Unit::Millimeters;
|
||||||
|
let s = export(&d, &ExportOptions::default());
|
||||||
|
assert!(s.contains("width=\"0.001mm\" height=\"0.002mm\""), "{s}");
|
||||||
|
|
||||||
|
// Extreme small dimension below 8 decimal places preserves non-zero width and viewBox
|
||||||
|
let mut d = Document::new(1e-9 * 72.0 / 25.4, 1e-9 * 72.0 / 25.4);
|
||||||
|
d.units = Unit::Millimeters;
|
||||||
|
let s = export(&d, &ExportOptions::default());
|
||||||
|
assert!(!s.contains("width=\"0mm\""), "{s}");
|
||||||
|
assert!(!s.contains("viewBox=\"0 0 0 0\""), "{s}");
|
||||||
|
|
||||||
|
// Re-importing preserved physical units
|
||||||
|
let back = import(&s).unwrap();
|
||||||
|
assert_eq!(back.units, Unit::Millimeters);
|
||||||
|
}
|
||||||
|
|||||||
+6
-4
File diff suppressed because one or more lines are too long
+1
-1
@@ -147,7 +147,7 @@ Gujarati interface follow-up (2026-11-14): the catalog and zero/one plural rule
|
|||||||
Selection, smart guides and snapping first. 60–90 h, parallel by tool.
|
Selection, smart guides and snapping first. 60–90 h, parallel by tool.
|
||||||
2. **`.ai` files from real users** (G9): layer structure (#951). 10–20 h.
|
2. **`.ai` files from real users** (G9): layer structure (#951). 10–20 h.
|
||||||
3. **Launch and stability** (G10): the Windows and Linux start-up reports. 20–35 h.
|
3. **Launch and stability** (G10): the Windows and Linux start-up reports. 20–35 h.
|
||||||
4. **Core-tool bugs** (G11): SVG units reverting to points (#864) and a Transform effect in a fill's or stroke's own effects (#885). Shape Builder regions (#937), Asset Export ghost borders (#983 / #1037) and the stalling bezier drag preview (#834) are fixed. 8–15 h.
|
4. **Core-tool bugs** (G11): SVG units reverting to points (#864; SVG export physical units fixed) and a Transform effect in a fill's or stroke's own effects (#885). Shape Builder regions (#937), Asset Export ghost borders (#983 / #1037) and the stalling bezier drag preview (#834) are fixed. 6–13 h.
|
||||||
5. **Then:** advanced type (G4) and the Illustrator 2026 additions (G13), then 3D (G3). The Effect Gallery's filter thumbnails and in-dialog preview are in (G2).
|
5. **Then:** advanced type (G4) and the Illustrator 2026 additions (G13), then 3D (G3). The Effect Gallery's filter thumbnails and in-dialog preview are in (G2).
|
||||||
|
|
||||||
## Next
|
## Next
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user