Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

G9: restore document ruler units from .ai editing data

nrq6993-a11yf557a1b6f3c6
diff --git a/crates/eps/src/import/ai/read.rs b/crates/eps/src/import/ai/read.rs
index 357b88fe6..45c89569b 100644
--- a/crates/eps/src/import/ai/read.rs
+++ b/crates/eps/src/import/ai/read.rs
@@ -53,7 +53,7 @@ use vectorcraft_color::{BlendMode, Paint};
use vectorcraft_doc::cmyk::Inks;
use vectorcraft_doc::{
Appearance, AppearanceItem, Artboard, ColorMode, Dash, Document, FillLayer, ImageBlob, ImageObject, Knockout, LayerColor, LineCap, LineJoin,
- LinkInfo, Node, NodeId, NodeKind, OpacityMask, PatternDef, StrokeLayer,
+ LinkInfo, Node, NodeId, NodeKind, OpacityMask, PatternDef, StrokeLayer, Unit,
};
use vectorcraft_geom::{FillRule, PathData};
@@ -80,6 +80,19 @@ const MAX_PIXELS: u64 = 1 << 25;
const MAX_HEX: usize = (MAX_PIXELS as usize) * 10;
/// Largest side of the document and its artboards (points).
const MAX_SIDE: f64 = 1e6;
+
+/// `%AI5_RulerUnits` → [`Unit`] (Illustrator 7 spec table 3; 4 and 5 are centimeters and pixels in later files).
+fn ruler_unit(v: u8) -> Option<Unit> {
+ Some(match v {
+ 0 => Unit::Inches,
+ 1 => Unit::Millimeters,
+ 2 => Unit::Points,
+ 3 => Unit::Picas,
+ 4 => Unit::Centimeters,
+ 5 => Unit::Pixels,
+ _ => return None,
+ })
+}
/// Art space (y up) → a pattern's space in the document's terms (y down).
const FLIP: Affine = Affine::new([1.0, 0.0, 0.0, -1.0, 0.0, 0.0]);
/// Most patterns read (others can't fill art).
@@ -507,6 +520,8 @@ struct Reader<'a> {
/// (see [`Reader::ensure_space`]); 1 (no scaling) when the file doesn't declare one, `None`
/// when the declared factor is not applied ([`read`]).
large_canvas: Option<f64>,
+ /// `%AI5_RulerUnits` from the header (Illustrator 7 spec table 3, plus centimeters and pixels).
+ ruler_units: Option<u8>,
/// This reader reads a pattern's definition (see [`Reader::define_pattern`]).
pattern: Option<PatternHead>,
/// The tile (y down) of each pattern read, by name.
@@ -643,6 +658,7 @@ impl<'a> Reader<'a> {
art_size: None,
template_center: None,
large_canvas: None,
+ ruler_units: None,
pattern: None,
pattern_tiles: HashMap::new(),
text: None,
@@ -773,6 +789,11 @@ impl<'a> Reader<'a> {
self.cropmarks = Some(r);
}
}
+ } else if self.frames.is_empty()
+ && let Some(v) = line.strip_prefix("AI5_RulerUnits:")
+ && let Ok(n) = v.trim().parse::<u8>()
+ {
+ self.ruler_units = Some(n);
} else if let Some(v) = line.strip_prefix("AI5_ArtSize:") {
self.art_size = two(v);
} else if let Some(v) = line.strip_prefix("AI3_TemplateBox:") {
@@ -2281,6 +2302,9 @@ impl<'a> Reader<'a> {
let (swatches, groups) = vectorcraft_color::default_swatches(ColorMode::Cmyk.model());
(doc.swatches, doc.swatch_groups) = (swatches.into(), groups.into());
}
+ if let Some(u) = self.ruler_units.and_then(ruler_unit) {
+ doc.units = u;
+ }
for n in &self.spots {
if doc.swatch(&n.name).is_none() {
doc.swatches.push(Swatch { name: n.name.clone(), paint: Paint::solid(n.color), global: true, spot: n.spot });
diff --git a/crates/eps/src/import/ai/tests.rs b/crates/eps/src/import/ai/tests.rs
index 16dceec45..b6c8c9e5c 100644
--- a/crates/eps/src/import/ai/tests.rs
+++ b/crates/eps/src/import/ai/tests.rs
@@ -5,7 +5,7 @@ use std::sync::Arc;
use kurbo::{Point, Rect};
use vectorcraft_color::gradient::GradientKind;
use vectorcraft_color::{BlendMode, Color, Paint};
-use vectorcraft_doc::{AppearanceItem, ColorMode, Document, Knockout, LayerColor, LineCap, LineJoin, Node, NodeKind};
+use vectorcraft_doc::{AppearanceItem, ColorMode, Document, Knockout, LayerColor, LineCap, LineJoin, Node, NodeKind, Unit};
use super::*;
@@ -33,6 +33,18 @@ fn read_ok(body: &str) -> Structure {
}
}
+#[test]
+fn ruler_units_from_the_header_set_the_documents_units() {
+ let body = layer("");
+ let with_mm = format!(
+ "%!PS-Adobe-3.0\n%%Creator: test\n%AI5_FileFormat 14.0\n%AI5_RulerUnits: 1\n%AI3_Cropmarks: 0 0 200 100\n%%EndComments\n%%BeginProlog\n%%EndProlog\n%%BeginSetup\n%%EndSetup\n{body}\n%%PageTrailer\ngsave annotatepage grestore showpage\n%%Trailer\n%%EOF\n"
+ );
+ assert_eq!(read_ok(&with_mm).doc.units, Unit::Millimeters);
+ assert_eq!(read_ok(&stream(&body)).doc.units, Unit::Points, "default when the comment is absent");
+ let with_in = with_mm.replace("AI5_RulerUnits: 1", "AI5_RulerUnits: 0");
+ assert_eq!(read(with_in.as_bytes()).unwrap().doc.units, Unit::Inches);
+}
+
/// The children of the first layer.
fn art(doc: &Document) -> Vec<Arc<Node>> {
doc.layers.first().and_then(|l| l.children()).cloned().unwrap_or_default()
diff --git a/crates/eps/src/import/tests_native.rs b/crates/eps/src/import/tests_native.rs
index df3b4c9cc..c0cd26c31 100644
--- a/crates/eps/src/import/tests_native.rs
+++ b/crates/eps/src/import/tests_native.rs
@@ -2,7 +2,7 @@
//! carries (see `native`). The files here are small ones written by hand for the tests, with the
//! operators seen in files their users own.
-use vectorcraft_doc::{Document, LayerColor, Node, NodeKind};
+use vectorcraft_doc::{Document, LayerColor, Node, NodeKind, Unit};
use crate::import::import;
@@ -73,6 +73,16 @@ fn names(d: &Document) -> Vec<String> {
d.layers.iter().map(|l| l.name.clone().unwrap_or_default()).collect()
}
+#[test]
+fn native_import_keeps_ruler_units_from_editing_data() {
+ let art = layer("Art", true, &square(10, 10));
+ let native = format!(
+ "%!PS-Adobe-3.0 \n%%BoundingBox: 0 0 100 100\n%%HiResBoundingBox: 0 0 100 100\n%AI3_Cropmarks: 0 0 100 100\n%AI5_RulerUnits: 1\n%AI5_NumLayers: 1\n{art}%%Trailer\n"
+ );
+ let r = import(&eps(&page_square(10, 10), &native)).unwrap();
+ assert_eq!(r.document.units, Unit::Millimeters);
+}
+
fn kinds(l: &Node) -> Vec<&'static str> {
l.children()
.into_iter()