mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 02:28:24 +00:00
Artboards: an artboard's guide label colors its own guides
Each artboard can carry a guide label: one of the layer colors or a custom RGB. Guides that belong to an artboard draw in its label color, and guides without an artboard, or on an artboard without a label, keep the guide color from the preferences. Selected guides still draw in the selection color. - Artboard Options has a Guide Label row (None, the layer colors with swatches, or Custom with a color swatch). - artboard.new and artboard.setProps take guideLabel: null, a layer color's name, or #rrggbb. Any other value is an error. The inspect summary lists guideLabel when one is set. - The native format saves the label and skips it when unset; a preset index outside the layer colors reads as no label. - artboard.setProps no longer adds an undo step when nothing changes. Docs: docs/mcp.md, docs/target-app-parity.md.
This commit is contained in:
@@ -88,6 +88,7 @@ impl Builder {
|
||||
show_video_safe_areas: false,
|
||||
video_ruler_pixel_aspect_ratio: 1.0,
|
||||
color: None,
|
||||
guide_label: None,
|
||||
})
|
||||
.collect();
|
||||
if self.doc.artboards.is_empty() {
|
||||
|
||||
@@ -373,6 +373,7 @@ pub(crate) fn with_single_artboard(mut d: Document, rect: Rect, name: &str) -> D
|
||||
show_video_safe_areas: false,
|
||||
video_ruler_pixel_aspect_ratio: 1.0,
|
||||
color: None,
|
||||
guide_label: None,
|
||||
}];
|
||||
d
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"New Artboard",
|
||||
["Window", "Artboards"],
|
||||
None,
|
||||
"{x?, y?, width?, height?, name?, color?: color (its background color, as artboard.setProps takes it, #rrggbbaa and #rgba refused; default: none), showCenterMark?: bool, showCrossHairs?: bool, showVideoSafeAreas?: bool (as artboard.setProps takes them; default false), videoRulerPixelAspectRatio?: number 0.1–10 (as artboard.setProps takes it; default 1), side?: \"left\"|\"right\"|\"top\"|\"bottom\" (the size of artboard `beside`, 20 pt from that side of it; refused with x, y, width or height), beside?: n (the artboard `side` places it by; default the last)} (default place: right of the last)",
|
||||
"{x?, y?, width?, height?, name?, color?: color (its background color, as artboard.setProps takes it, #rrggbbaa and #rgba refused; default: none), guideLabel?: name | \"#rrggbb\" | null (its guide label, as artboard.setProps takes it; default: none), showCenterMark?: bool, showCrossHairs?: bool, showVideoSafeAreas?: bool (as artboard.setProps takes them; default false), videoRulerPixelAspectRatio?: number 0.1–10 (as artboard.setProps takes it; default 1), side?: \"left\"|\"right\"|\"top\"|\"bottom\" (the size of artboard `beside`, 20 pt from that side of it; refused with x, y, width or height), beside?: n (the artboard `side` places it by; default the last)} (default place: right of the last)",
|
||||
has_doc,
|
||||
artboard_new
|
||||
),
|
||||
@@ -155,7 +155,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Artboard Options…",
|
||||
["Window", "Artboards"],
|
||||
None,
|
||||
"{index | artboards: [{index, name?, color?, locked?, showCenterMark?, showCrossHairs?, showVideoSafeAreas?, videoRulerPixelAspectRatio?, x?, y?, width?, height?}…] (several artboards in one undo step, each with its own fields; the art inside each before any of them changes goes with it, and an object inside several goes with the first listed that moves or scales its art), name?, color?: color (\"#rrggbb\", {c,m,y,k}, … as the paint commands take, #rrggbbaa and #rgba refused: the background color the canvas draws the artboard's paper in, multiplied with the Simulate Colored Paper color while that is on; exports and print leave it out) | null (no background color), locked?: bool (the Artboards panel's lock column: a locked artboard keeps its rectangle (a new x, y, width or height is an error unless locked is false), can't be moved, deleted or cut, and Rearrange and Fit to Artwork Bounds leave it as it is; the art on it (each object of a layer or sublayer whose bounds with strokes overlap it, a group as one object) acts as locked art until it is unlocked: clicks, marquees, select.set, select.all and the tools pass over it (art drawn or pasted onto it stays selected and editable until a selection leaves it out or it is moved off the locked artboards), it is deselected, and when another artboard moves or scales it stays unless lockedAndHidden), showCenterMark?: bool (Artboard Options' Show Center Mark: the canvas draws a mark at the artboard's center), showCrossHairs?: bool (Show Cross Hairs: the canvas draws a line from the middle of each side through the center), showVideoSafeAreas?: bool (Show Video Safe Areas: the canvas draws the action-safe area 3.5% and the title-safe area 5% in from each edge, as EBU R 95 sets them; the three are screen-only, as the artboard's edges are: exports and print leave them out), videoRulerPixelAspectRatio?: number 0.1–10 (Video Ruler Pixel Aspect Ratio: the width of one video pixel over its height, which View › Rulers › Show Video Rulers counts the artboard in; default 1, square pixels), x?, y?, width?, height?, moveArt?: bool (default false; a position-only change moves the contained art with its artboard), scaleArt?: bool (Scale Artwork with Artboard: resized, the artboard takes the art fully inside it (locked and hidden art only with lockedAndHidden?: bool, default prefs moveLockedWithArtboard) and its guides from its old rectangle onto the new one, each side by its own ratio; strokes/corners/patterns as object.scale), strokes?: bool, corners?: bool, patterns?: bool (the three as object.scale, for art that moves or scales with the artboard), lockedAndHidden?: bool (locked and hidden art moves along; default: the preference; recorded in the journal so a replay matches)} → null, or with scaleArt {scaled: the art scaled}",
|
||||
"{index | artboards: [{index, name?, color?, guideLabel?, locked?, showCenterMark?, showCrossHairs?, showVideoSafeAreas?, videoRulerPixelAspectRatio?, x?, y?, width?, height?}…] (several artboards in one undo step, each with its own fields; the art inside each before any of them changes goes with it, and an object inside several goes with the first listed that moves or scales its art), name?, color?: color (\"#rrggbb\", {c,m,y,k}, … as the paint commands take, #rrggbbaa and #rgba refused: the background color the canvas draws the artboard's paper in, multiplied with the Simulate Colored Paper color while that is on; exports and print leave it out) | null (no background color), guideLabel?: name | \"#rrggbb\" | null (the guide label: the canvas draws the artboard's own guides (guide.add with artboard) in this color instead of the guide color preference, selected ones still in the selection color; a layer color's name spelled exactly as Layer Options lists it (\"Light Blue\", \"Red\", …) or #rrggbb; null for none, which new artboards and older files have; screen-only, so exports and print leave it out; it can change on a locked artboard; an invalid value anywhere in the list changes nothing), locked?: bool (the Artboards panel's lock column: a locked artboard keeps its rectangle (a new x, y, width or height is an error unless locked is false), can't be moved, deleted or cut, and Rearrange and Fit to Artwork Bounds leave it as it is; the art on it (each object of a layer or sublayer whose bounds with strokes overlap it, a group as one object) acts as locked art until it is unlocked: clicks, marquees, select.set, select.all and the tools pass over it (art drawn or pasted onto it stays selected and editable until a selection leaves it out or it is moved off the locked artboards), it is deselected, and when another artboard moves or scales it stays unless lockedAndHidden), showCenterMark?: bool (Artboard Options' Show Center Mark: the canvas draws a mark at the artboard's center), showCrossHairs?: bool (Show Cross Hairs: the canvas draws a line from the middle of each side through the center), showVideoSafeAreas?: bool (Show Video Safe Areas: the canvas draws the action-safe area 3.5% and the title-safe area 5% in from each edge, as EBU R 95 sets them; the three are screen-only, as the artboard's edges are: exports and print leave them out), videoRulerPixelAspectRatio?: number 0.1–10 (Video Ruler Pixel Aspect Ratio: the width of one video pixel over its height, which View › Rulers › Show Video Rulers counts the artboard in; default 1, square pixels), x?, y?, width?, height?, moveArt?: bool (default false; a position-only change moves the contained art with its artboard), scaleArt?: bool (Scale Artwork with Artboard: resized, the artboard takes the art fully inside it (locked and hidden art only with lockedAndHidden?: bool, default prefs moveLockedWithArtboard) and its guides from its old rectangle onto the new one, each side by its own ratio; strokes/corners/patterns as object.scale), strokes?: bool, corners?: bool, patterns?: bool (the three as object.scale, for art that moves or scales with the artboard), lockedAndHidden?: bool (locked and hidden art moves along; default: the preference; recorded in the journal so a replay matches)} → null, or with scaleArt {scaled: the art scaled}; no undo step when nothing changes",
|
||||
has_doc,
|
||||
artboard_set
|
||||
),
|
||||
@@ -859,6 +859,22 @@ fn artboard_color(p: &Value, cmd: &str) -> Result<Option<Option<vectorcraft_colo
|
||||
}
|
||||
}
|
||||
|
||||
/// An artboard command's `guideLabel` param, the color of its own guides: None when absent,
|
||||
/// `Some(None)` for null (no label), else `Some(Some(label))` for a preset's name exactly as
|
||||
/// [`LAYER_COLORS`] spells it or `#rrggbb` ([`LayerColor::from_label_text`]). Any other value,
|
||||
/// such as a preset's index, a name in other letter case or `#rrggbbaa`, is an error.
|
||||
fn artboard_guide_label(p: &Value, cmd: &str) -> Result<Option<Option<LayerColor>>> {
|
||||
match p.get("guideLabel") {
|
||||
None => Ok(None),
|
||||
Some(Value::Null) => Ok(Some(None)),
|
||||
Some(v) => v
|
||||
.as_str()
|
||||
.and_then(LayerColor::from_label_text)
|
||||
.map(|c| Some(Some(c)))
|
||||
.ok_or_else(|| bad(cmd, "`guideLabel` is null, a layer color's name or #rrggbb")),
|
||||
}
|
||||
}
|
||||
|
||||
/// An artboard command's text param `key`: None when absent or null, else the string. Any other
|
||||
/// value, such as 5, is an error: "`{key}` must be a string".
|
||||
fn artboard_str<'a>(p: &'a Value, key: &str, cmd: &str) -> Result<Option<&'a str>> {
|
||||
@@ -898,6 +914,7 @@ fn artboard_new(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let last = d0.artboards.last().map(|a| a.rect).unwrap_or(Rect::new(0.0, 0.0, 612.0, 792.0));
|
||||
let def = Rect::new(last.x1 + 20.0, last.y0, last.x1 + 20.0 + last.width(), last.y1);
|
||||
let color = artboard_color(p, "artboard.new")?.flatten();
|
||||
let guide_label = artboard_guide_label(p, "artboard.new")?.flatten();
|
||||
check_switches(p, &["showCenterMark", "showCrossHairs", "showVideoSafeAreas"], "artboard.new")?;
|
||||
check_numbers(p, &["x", "y", "width", "height"], "artboard.new")?;
|
||||
let r = match artboard_side(p, "artboard.new")? {
|
||||
@@ -932,6 +949,7 @@ fn artboard_new(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
show_video_safe_areas,
|
||||
video_ruler_pixel_aspect_ratio,
|
||||
color,
|
||||
guide_label,
|
||||
});
|
||||
Ok(d.artboards.len() - 1)
|
||||
})?;
|
||||
@@ -1001,6 +1019,7 @@ struct ArtboardEdit<'a> {
|
||||
now: Rect,
|
||||
name: Option<&'a str>,
|
||||
color: Option<Option<vectorcraft_color::Color>>,
|
||||
guide_label: Option<Option<LayerColor>>,
|
||||
lock: Option<bool>,
|
||||
center_mark: Option<bool>,
|
||||
cross_hairs: Option<bool>,
|
||||
@@ -1013,6 +1032,23 @@ struct ArtboardEdit<'a> {
|
||||
art: Vec<NodeId>,
|
||||
}
|
||||
|
||||
impl ArtboardEdit<'_> {
|
||||
/// Does the edit change artboard `a` (the one at its index) or the art and guides on it?
|
||||
fn changes(&self, a: &Artboard) -> bool {
|
||||
let name = self.name.map(|n| if n.is_empty() { format!("Artboard {}", self.index + 1) } else { n.to_string() });
|
||||
self.now != a.rect
|
||||
|| self.transform.is_some()
|
||||
|| name.is_some_and(|n| n != a.name)
|
||||
|| self.color.is_some_and(|c| c != a.color)
|
||||
|| self.guide_label.is_some_and(|c| c != a.guide_label)
|
||||
|| self.lock.is_some_and(|l| l != a.locked)
|
||||
|| self.center_mark.is_some_and(|m| m != a.show_center_mark)
|
||||
|| self.cross_hairs.is_some_and(|c| c != a.show_cross_hairs)
|
||||
|| self.safe_areas.is_some_and(|v| v != a.show_video_safe_areas)
|
||||
|| self.pixel_aspect.is_some_and(|r| r != a.video_ruler_pixel_aspect_ratio)
|
||||
}
|
||||
}
|
||||
|
||||
fn artboard_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// One artboard (`index` and its fields), or several (`artboards`, each with its own fields).
|
||||
let entries: Vec<&Value> = match p.get("artboards") {
|
||||
@@ -1044,6 +1080,8 @@ fn artboard_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let was = d.artboards.get(index).map(|a| a.rect).ok_or_else(|| EngineError::Other("no such artboard".into()))?;
|
||||
// The background color: a color sets it, null clears it, absent leaves it as it is.
|
||||
let color = artboard_color(q, "artboard.setProps")?;
|
||||
// The guide label likewise; a locked artboard's can change, as its name can.
|
||||
let guide_label = artboard_guide_label(q, "artboard.setProps")?;
|
||||
let now = rect_from(q, was);
|
||||
let lock = q.get("locked").and_then(Value::as_bool);
|
||||
let (center_mark, cross_hairs) = (q.get("showCenterMark").and_then(Value::as_bool), q.get("showCrossHairs").and_then(Value::as_bool));
|
||||
@@ -1070,6 +1108,7 @@ fn artboard_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
now,
|
||||
name: artboard_str(q, "name", "artboard.setProps")?.map(str::trim),
|
||||
color,
|
||||
guide_label,
|
||||
lock,
|
||||
center_mark,
|
||||
cross_hairs,
|
||||
@@ -1082,6 +1121,10 @@ fn artboard_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
art,
|
||||
});
|
||||
}
|
||||
// Nothing to change: no undo step.
|
||||
if !edits.iter().any(|e| d.artboards.get(e.index).is_none_or(|a| e.changes(a))) {
|
||||
return if scale_art { Ok(json!({ "scaled": [] })) } else { ok() };
|
||||
}
|
||||
// Noted in the journal when art goes along, so a replay transforms the same objects whatever
|
||||
// the preference is then.
|
||||
if edits.iter().any(|e| e.transform.is_some()) {
|
||||
@@ -1102,6 +1145,9 @@ fn artboard_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Some(c) = e.color {
|
||||
a.color = c;
|
||||
}
|
||||
if let Some(c) = e.guide_label {
|
||||
a.guide_label = c;
|
||||
}
|
||||
if let Some(l) = e.lock {
|
||||
a.locked = l;
|
||||
}
|
||||
|
||||
@@ -1319,6 +1319,7 @@ fn convert_to_artboards(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
show_video_safe_areas: false,
|
||||
video_ruler_pixel_aspect_ratio: 1.0,
|
||||
color: None,
|
||||
guide_label: None,
|
||||
});
|
||||
converted.push(id);
|
||||
}
|
||||
|
||||
@@ -344,6 +344,7 @@ impl DocSettings {
|
||||
show_video_safe_areas: false,
|
||||
video_ruler_pixel_aspect_ratio: 1.0,
|
||||
color: None,
|
||||
guide_label: None,
|
||||
})
|
||||
.collect();
|
||||
d
|
||||
|
||||
@@ -133,7 +133,8 @@ pub fn document(s: &Session) -> Value {
|
||||
document_opts(s, SummaryOpts::default())
|
||||
}
|
||||
|
||||
/// An artboard's name and rectangle, its background color (`#rrggbb`) when it has one,
|
||||
/// An artboard's name and rectangle, its background color (`#rrggbb`) when it has one, its guide
|
||||
/// label (a layer color's name or `#rrggbb`) when it has one,
|
||||
/// `locked: true` when it is locked, `showCenterMark: true`, `showCrossHairs: true` and
|
||||
/// `showVideoSafeAreas: true` when it shows them, and `videoRulerPixelAspectRatio` when it isn't 1.
|
||||
fn artboard_summary(a: &vectorcraft_doc::Artboard) -> Value {
|
||||
@@ -141,6 +142,9 @@ fn artboard_summary(a: &vectorcraft_doc::Artboard) -> Value {
|
||||
if let Some(c) = a.color {
|
||||
v["color"] = json!(c.to_hex());
|
||||
}
|
||||
if let Some(c) = a.guide_label {
|
||||
v["guideLabel"] = json!(c.label_text());
|
||||
}
|
||||
for (key, on) in [
|
||||
("locked", a.locked),
|
||||
("showCenterMark", a.show_center_mark),
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use serde_json::json;
|
||||
use vectorcraft_color::{Color, GradientKind, Paint};
|
||||
use vectorcraft_doc::{AppearanceItem, NodeKind};
|
||||
use vectorcraft_doc::{AppearanceItem, LayerColor, NodeKind};
|
||||
|
||||
use crate::*;
|
||||
|
||||
@@ -1387,6 +1387,128 @@ fn artboard_video_display_options_are_set_in_one_undo_step() {
|
||||
assert_eq!(video(&s, 1), (false, 10.0), "10 is in range");
|
||||
}
|
||||
|
||||
/// Artboard Options' guide label: `artboard.setProps {guideLabel}` takes a layer color's name or
|
||||
/// `#rrggbb` and null clears it, each change one undo step, also on a locked artboard (whose
|
||||
/// rectangle stays locked) and for several artboards in one list; a value equal to the artboard's
|
||||
/// adds no undo step. `artboard.new` takes it, a duplicate and an undone delete keep it, and
|
||||
/// `inspect_document` reports it. A bad value anywhere in a list changes nothing.
|
||||
#[test]
|
||||
fn artboard_guide_labels_are_set_and_cleared_in_one_undo_step() {
|
||||
let mut s = session();
|
||||
s.execute("artboard.new", &json!({})).unwrap();
|
||||
let label = |s: &Session, i: usize| s.doc().unwrap().doc.artboards[i].guide_label;
|
||||
let undo = |s: &Session| s.doc().unwrap().history.undo.len();
|
||||
assert_eq!((label(&s, 0), label(&s, 1)), (None, None), "no label by default");
|
||||
let u0 = undo(&s);
|
||||
s.execute("artboard.setProps", &json!({"index": 1, "guideLabel": "Red"})).unwrap();
|
||||
assert_eq!((label(&s, 0), label(&s, 1)), (None, Some(LayerColor::Preset(1))), "only that artboard");
|
||||
s.execute("artboard.setProps", &json!({"index": 1, "guideLabel": "#12aBcd"})).unwrap();
|
||||
assert_eq!(label(&s, 1), Some(LayerColor::Custom([0x12, 0xab, 0xcd])));
|
||||
s.execute("artboard.setProps", &json!({"index": 1, "guideLabel": null})).unwrap();
|
||||
assert_eq!(label(&s, 1), None, "null clears it");
|
||||
assert_eq!(undo(&s), u0 + 3, "one undo step each");
|
||||
for was in [Some(LayerColor::Custom([0x12, 0xab, 0xcd])), Some(LayerColor::Preset(1)), None] {
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(label(&s, 1), was);
|
||||
}
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
// The same label again, or a list that changes nothing: no undo step.
|
||||
let u = undo(&s);
|
||||
s.execute("artboard.setProps", &json!({"index": 1, "guideLabel": "Red"})).unwrap();
|
||||
s.execute("artboard.setProps", &json!({"artboards": [{"index": 0, "guideLabel": null}, {"index": 1, "guideLabel": "Red"}]})).unwrap();
|
||||
assert_eq!(undo(&s), u, "nothing changed, no undo step");
|
||||
// A locked artboard takes a label; its rectangle stays locked.
|
||||
s.execute("artboard.setProps", &json!({"index": 0, "locked": true})).unwrap();
|
||||
let rect0 = s.doc().unwrap().doc.artboards[0].rect;
|
||||
s.execute("artboard.setProps", &json!({"index": 0, "guideLabel": "Gold"})).unwrap();
|
||||
assert_eq!(label(&s, 0), Some(LayerColor::Preset(15)));
|
||||
assert!(s.execute("artboard.setProps", &json!({"index": 0, "x": 999, "guideLabel": "Teal"})).is_err());
|
||||
assert_eq!((s.doc().unwrap().doc.artboards[0].rect, label(&s, 0)), (rect0, Some(LayerColor::Preset(15))));
|
||||
let u = undo(&s);
|
||||
s.execute("artboard.setProps", &json!({"artboards": [{"index": 0, "guideLabel": "Teal"}, {"index": 1, "guideLabel": "#000000"}]})).unwrap();
|
||||
assert_eq!((label(&s, 0), label(&s, 1)), (Some(LayerColor::Preset(11)), Some(LayerColor::Custom([0, 0, 0]))), "each its own");
|
||||
assert_eq!(undo(&s), u + 1, "in one undo step");
|
||||
let doc = crate::inspect::document(&s);
|
||||
assert_eq!((doc["artboards"][0]["guideLabel"].clone(), doc["artboards"][1]["guideLabel"].clone()), (json!("Teal"), json!("#000000")));
|
||||
s.execute("artboard.setProps", &json!({"index": 1, "guideLabel": null})).unwrap();
|
||||
assert!(crate::inspect::document(&s)["artboards"][1].get("guideLabel").is_none(), "no label isn't reported");
|
||||
let copy = s.execute("artboard.duplicate", &json!({"index": 0})).unwrap()["index"].as_u64().unwrap() as usize;
|
||||
assert_eq!(label(&s, copy), Some(LayerColor::Preset(11)), "a duplicate keeps it");
|
||||
let made = s.execute("artboard.new", &json!({"guideLabel": "Violet"})).unwrap()["index"].as_u64().unwrap() as usize;
|
||||
assert_eq!(label(&s, made), Some(LayerColor::Preset(14)), "a new artboard takes it");
|
||||
let plain = s.execute("artboard.new", &json!({})).unwrap()["index"].as_u64().unwrap() as usize;
|
||||
assert_eq!(label(&s, plain), None);
|
||||
s.execute("artboard.delete", &json!({"index": made})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(label(&s, made), Some(LayerColor::Preset(14)), "undoing a delete brings it back");
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let bads = [
|
||||
json!("red"),
|
||||
json!(" Red"),
|
||||
json!("#12345"),
|
||||
json!("#1234567"),
|
||||
json!("#12345678"),
|
||||
json!("#fff"),
|
||||
json!("#gg0000"),
|
||||
json!(1),
|
||||
json!([1, 2, 3]),
|
||||
json!({"Preset": 1}),
|
||||
json!(true),
|
||||
];
|
||||
for bad in bads {
|
||||
assert!(s.execute("artboard.setProps", &json!({"index": 1, "guideLabel": bad})).is_err(), "{bad}");
|
||||
assert!(s.execute("artboard.new", &json!({"guideLabel": bad})).is_err(), "{bad}");
|
||||
assert!(
|
||||
s.execute("artboard.setProps", &json!({"artboards": [{"index": 0, "guideLabel": "Red"}, {"index": 1, "guideLabel": bad}]})).is_err(),
|
||||
"{bad}"
|
||||
);
|
||||
}
|
||||
assert!(s.execute("artboard.setProps", &json!({"artboards": [{"index": 1, "guideLabel": "Red"}, {"index": 1, "guideLabel": "Red"}]})).is_err());
|
||||
assert!(s.execute("artboard.setProps", &json!({"artboards": [{"index": 1, "guideLabel": "Red"}, {"index": 99, "guideLabel": "Red"}]})).is_err());
|
||||
assert_eq!(*s.doc().unwrap().doc, *before, "a bad value changes nothing");
|
||||
}
|
||||
|
||||
/// The native format keeps the guide label, as `{"Preset": n}` or `{"Custom": [r, g, b]}`. An
|
||||
/// artboard without one is written as before labels could be set, and a preset outside the layer
|
||||
/// colors in a file reads as no label. Exports leave the label out.
|
||||
#[test]
|
||||
fn artboard_guide_labels_round_trip_through_the_native_format() {
|
||||
let mut s = session();
|
||||
rect(&mut s);
|
||||
s.execute("artboard.new", &json!({})).unwrap();
|
||||
let save = |s: &Session| vectorcraft_format::save(&s.doc().unwrap().doc, false);
|
||||
let before = save(&s);
|
||||
let plain = (*s.doc().unwrap().doc).clone();
|
||||
s.execute("artboard.setProps", &json!({"artboards": [{"index": 0, "guideLabel": "Green"}, {"index": 1, "guideLabel": "#102030"}]})).unwrap();
|
||||
let bytes = save(&s);
|
||||
let saved: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
|
||||
let ab = |i: usize| saved["document"]["artboards"][i]["guide_label"].clone();
|
||||
assert_eq!((ab(0), ab(1)), (json!({"Preset": 2}), json!({"Custom": [16, 32, 48]})));
|
||||
let back = vectorcraft_format::load(&bytes).unwrap();
|
||||
assert_eq!(back.artboards, s.doc().unwrap().doc.artboards);
|
||||
// Screen-only: SVG and PDF exports are the same as without the labels.
|
||||
let on = (*s.doc().unwrap().doc).clone();
|
||||
assert_eq!(vectorcraft_svg::export(&on, &Default::default()), vectorcraft_svg::export(&plain, &Default::default()));
|
||||
let options = vectorcraft_pdf::PdfOptions { created: Some(0), ..Default::default() };
|
||||
assert_eq!(crate::export_pdf(&on, &options).unwrap(), crate::export_pdf(&plain, &options).unwrap());
|
||||
s.execute("artboard.setProps", &json!({"artboards": [{"index": 0, "guideLabel": null}, {"index": 1, "guideLabel": null}]})).unwrap();
|
||||
assert_eq!(save(&s), before, "without labels, the file is as it was");
|
||||
// A file's labels as another writer might put them: a preset past the layer colors reads as
|
||||
// none, and a label that isn't a layer color at all is refused.
|
||||
let mut edited = saved.clone();
|
||||
edited["document"]["artboards"][0]["guide_label"] = json!({"Preset": 27});
|
||||
edited["document"]["artboards"][1]["guide_label"] = json!({"Preset": 26});
|
||||
let back = vectorcraft_format::load(&serde_json::to_vec(&edited).unwrap()).unwrap();
|
||||
assert_eq!((back.artboards[0].guide_label, back.artboards[1].guide_label), (None, Some(LayerColor::Preset(26))));
|
||||
for bad in [json!({"Preset": 256}), json!({"Custom": [1, 2]}), json!("Red"), json!({"Custom": [1, 2, 300]})] {
|
||||
edited["document"]["artboards"][0]["guide_label"] = bad.clone();
|
||||
assert!(vectorcraft_format::load(&serde_json::to_vec(&edited).unwrap()).is_err(), "{bad}");
|
||||
}
|
||||
edited["document"]["artboards"][0]["guide_label"] = json!(null);
|
||||
let back = vectorcraft_format::load(&serde_json::to_vec(&edited).unwrap()).unwrap();
|
||||
assert_eq!(back.artboards[0].guide_label, None);
|
||||
}
|
||||
|
||||
/// The native format keeps Show Video Safe Areas and the Video Ruler Pixel Aspect Ratio. A
|
||||
/// document whose artboards hide the safe areas and have square pixels is written byte for byte as
|
||||
/// before they could be set, a file written without the two keys opens with the safe areas off
|
||||
|
||||
Reference in New Issue
Block a user