mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
1344 lines
71 KiB
Rust
1344 lines
71 KiB
Rust
//! Native placement must keep the composite fonts its editable art uses, without changing the destination's meanings.
|
|
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
|
|
|
use std::path::Path;
|
|
use std::sync::{Mutex, MutexGuard};
|
|
|
|
use serde_json::{Value, json};
|
|
use vectorcraft_doc::{CompositeFont, Document, Node, NodeId, NodeKind};
|
|
use vectorcraft_engine::Session;
|
|
use vectorcraft_testkit::TempDir;
|
|
use vectorcraft_text::FontDb;
|
|
|
|
struct FontContext {
|
|
fonts: Vec<CompositeFont>,
|
|
_lock: MutexGuard<'static, ()>,
|
|
}
|
|
|
|
impl FontContext {
|
|
fn hold() -> Self {
|
|
static LOCK: Mutex<()> = Mutex::new(());
|
|
let lock = LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
|
let db = FontDb::global();
|
|
assert!(db.face("Source Sans 3", "Regular").is_some(), "CONTROL: the bundled test face is available");
|
|
Self { fonts: registered_fonts(), _lock: lock }
|
|
}
|
|
}
|
|
|
|
impl Drop for FontContext {
|
|
fn drop(&mut self) {
|
|
let db = FontDb::global();
|
|
for name in db.composite_font_names() {
|
|
if CompositeFont::named(&self.fonts, &name).is_none() {
|
|
db.remove_composite_font(&name);
|
|
}
|
|
}
|
|
for font in &self.fonts {
|
|
db.set_composite_font(font);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn registered_fonts() -> Vec<CompositeFont> {
|
|
let db = FontDb::global();
|
|
db.composite_font_names().iter().map(|name| (*db.composite_font(name).unwrap()).clone()).collect()
|
|
}
|
|
|
|
fn document(session: &Session) -> &Document {
|
|
&session.doc().expect("CONTROL: an active document").doc
|
|
}
|
|
|
|
fn destination(fonts: &[CompositeFont]) -> Session {
|
|
let mut session = Session::new();
|
|
session.add_document(Document::new(100.0, 80.0), None);
|
|
session.execute("type.compositeFonts.set", &json!({"fonts": fonts})).unwrap();
|
|
session
|
|
}
|
|
|
|
fn font_document(font: &CompositeFont) -> Document {
|
|
let unused = CompositeFont::new(format!("{} Unused", font.name), "Source Sans 3", "Regular");
|
|
let mut session = destination(&[font.clone(), unused]);
|
|
session.execute("text.create", &json!({"x": 4, "y": 16, "text": "Ab", "font": font.name})).unwrap();
|
|
let doc = document(&session).clone();
|
|
// Leave the fixture's name cold through an ordinary public edit, not a registry injection.
|
|
session.execute("type.compositeFonts.set", &json!({"fonts": []})).unwrap();
|
|
doc
|
|
}
|
|
|
|
fn write_native(path: &Path, source: &Document) -> Document {
|
|
let options = vectorcraft_format::SaveOptions { include_linked: true, precision: None, ..Default::default() };
|
|
let bytes = vectorcraft_format::save_with(source, &options).unwrap();
|
|
std::fs::write(path, &bytes).unwrap();
|
|
let loaded = vectorcraft_format::load(&bytes).unwrap();
|
|
assert_eq!(loaded.composite_fonts, source.composite_fonts, "CONTROL: native fixture keeps its definitions");
|
|
loaded
|
|
}
|
|
|
|
fn place(session: &mut Session, path: &Path, linked: bool) -> NodeId {
|
|
let reply = session.execute("file.place", &json!({"path": path, "link": linked, "at": [30, 25]})).unwrap();
|
|
assert_eq!(reply["linked"], linked, "CONTROL: the intended public placement route");
|
|
let id = NodeId(reply["ids"][0].as_u64().unwrap());
|
|
if linked {
|
|
session.execute("links.embed", &json!({"ids": [id.0]})).unwrap();
|
|
}
|
|
id
|
|
}
|
|
|
|
fn placed_family(session: &Session, id: NodeId) -> String {
|
|
let root = document(session).node(id).unwrap();
|
|
let mut families = Vec::new();
|
|
root.walk(&mut |node| {
|
|
if let NodeKind::Text(text) = &node.kind {
|
|
families.extend(text.runs.iter().filter(|run| run.text == "Ab").map(|run| run.style.font_family.clone()));
|
|
}
|
|
});
|
|
assert_eq!(families.len(), 1, "CONTROL: the one source lettering run remains editable");
|
|
families.pop().unwrap()
|
|
}
|
|
|
|
fn kept_text(session: &mut Session, font: &CompositeFont) -> (NodeId, Node) {
|
|
let reply = session.execute("text.create", &json!({"x": 2, "y": 12, "text": "Destination", "font": font.name})).unwrap();
|
|
let id = NodeId(reply["id"].as_u64().unwrap());
|
|
(id, document(session).node(id).unwrap().clone())
|
|
}
|
|
|
|
#[test]
|
|
fn native_composite_meanings_survive_editable_and_embedded_placement() {
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-composite");
|
|
let path = dir.join("lettering.vectorcraft");
|
|
let incoming = CompositeFont::new("NativeAdoptionLettering", "Source Sans 3", "Regular");
|
|
write_native(&path, &font_document(&incoming));
|
|
for linked in [false, true] {
|
|
// No definition, an exact-name equal definition, and an exact-name conflicting definition.
|
|
for state in 0..3 {
|
|
let mut existing = incoming.clone();
|
|
if state == 2 {
|
|
existing.parts.roman.size = 125.0;
|
|
}
|
|
let mut fonts = Vec::new();
|
|
if state == 1 {
|
|
fonts.push(CompositeFont { name: "NativeAdoptionEquivalentOtherName".into(), ..incoming.clone() });
|
|
}
|
|
if state != 0 {
|
|
fonts.push(existing.clone());
|
|
}
|
|
let mut session = destination(&fonts);
|
|
let kept = (state != 0).then(|| kept_text(&mut session, &existing));
|
|
let id = place(&mut session, &path, linked);
|
|
let name = placed_family(&session, id);
|
|
let imported = CompositeFont::named(&document(&session).composite_fonts, &name)
|
|
.expect("REGRESSION: editable native art must retain the meaning of its named composite font");
|
|
assert_eq!((&imported.parts, &imported.custom), (&incoming.parts, &incoming.custom));
|
|
assert_eq!(CompositeFont::same_name(&name, &incoming.name), state != 2);
|
|
assert_eq!(document(&session).composite_fonts.len(), if state == 0 { 1 } else { 2 });
|
|
if let Some((id, art)) = kept {
|
|
assert_eq!(document(&session).node(id), Some(&art));
|
|
assert_eq!(CompositeFont::named(&document(&session).composite_fonts, &existing.name), Some(&existing));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn native_composite_alias_is_reused_without_changing_destination_art() {
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-composite-repeat");
|
|
let path = dir.join("lettering.vectorcraft");
|
|
let incoming = CompositeFont::new("NativeAdoptionRepeated", "Source Sans 3", "Regular");
|
|
write_native(&path, &font_document(&incoming));
|
|
for linked in [false, true] {
|
|
let mut existing = incoming.clone();
|
|
existing.parts.roman.size = 125.0;
|
|
let mut session = destination(std::slice::from_ref(&existing));
|
|
let (kept_id, kept_art) = kept_text(&mut session, &existing);
|
|
let first = place(&mut session, &path, linked);
|
|
let alias = placed_family(&session, first);
|
|
assert!(!CompositeFont::same_name(&alias, &incoming.name), "a real conflict requires an alias");
|
|
let first_definition = CompositeFont::named(&document(&session).composite_fonts, &alias).unwrap().clone();
|
|
let count = document(&session).composite_fonts.len();
|
|
let second = place(&mut session, &path, linked);
|
|
assert_eq!(placed_family(&session, second), alias, "repeated art reuses the same meaning, without assuming alias spelling");
|
|
assert_eq!(document(&session).composite_fonts.len(), count);
|
|
assert_eq!(CompositeFont::named(&document(&session).composite_fonts, &alias), Some(&first_definition));
|
|
assert_eq!(document(&session).node(kept_id), Some(&kept_art));
|
|
assert_eq!(CompositeFont::named(&document(&session).composite_fonts, &existing.name), Some(&existing));
|
|
}
|
|
}
|
|
|
|
fn snapshot(session: &Session) -> Value {
|
|
let state = session.doc().unwrap();
|
|
json!({"document": &*state.doc, "selection": state.selection,
|
|
"undo": state.history.undo.len(), "redo": state.history.redo.len(),
|
|
"journal": session.journal.iter().cloned().collect::<Vec<_>>(),
|
|
"journalFirst": session.journal.first_kept(), "journalHeld": session.journal.held()})
|
|
}
|
|
|
|
fn plain_text(session: &Session, id: NodeId) -> String {
|
|
let NodeKind::Text(text) = &document(session).node(id).unwrap().kind else {
|
|
panic!("CONTROL: the live text object remains");
|
|
};
|
|
text.plain_text()
|
|
}
|
|
|
|
fn select_type_range(session: &mut Session, id: NodeId) -> (vectorcraft_engine::ViewInfo, Value) {
|
|
use vectorcraft_tools::{Mods, PointerEvent, PointerKind, ToolKey};
|
|
let view = vectorcraft_engine::ViewInfo::default();
|
|
session.select_tool("type", view).unwrap();
|
|
let bounds = document(session).bounds_of(&[id], false).unwrap();
|
|
for kind in [PointerKind::Down, PointerKind::Up] {
|
|
session.pointer(&PointerEvent::new(kind, bounds.x1 - 1.0, bounds.center().y), view).unwrap();
|
|
}
|
|
session.tool_key(ToolKey::Home, Mods::default(), view).unwrap();
|
|
for _ in 0..2 {
|
|
session.tool_key(ToolKey::Right, Mods { shift: true, ..Mods::default() }, view).unwrap();
|
|
}
|
|
let options = session.tool_options();
|
|
assert_eq!(options["editing"], id.0);
|
|
assert_eq!((options["start"].clone(), options["end"].clone()), (json!(0), json!(2)));
|
|
assert_eq!(options["typing"], false, "CONTROL: a real selected range, with no pending typing");
|
|
assert!(!session.in_interaction());
|
|
(view, options)
|
|
}
|
|
|
|
#[test]
|
|
fn native_composite_limit_errors_are_atomic_for_place_embed_and_async_replace() {
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-composite-limit");
|
|
let path = dir.join("one-more-font.vectorcraft");
|
|
let incoming = CompositeFont::new("NativeAdoptionOneMore", "Source Sans 3", "Regular");
|
|
write_native(&path, &font_document(&incoming));
|
|
let full: Vec<_> = (0..vectorcraft_doc::text::COMPOSITE_FONTS_MAX)
|
|
.map(|i| CompositeFont::new(format!("NativeAdoptionHeld{i}"), "Source Sans 3", "Regular"))
|
|
.collect();
|
|
for linked in [false, true] {
|
|
let mut session = destination(&full);
|
|
let (command, params) = if linked {
|
|
let placed = session.execute("file.place", &json!({"path": path, "link": true})).unwrap();
|
|
assert_eq!(placed["linked"], true);
|
|
("links.embed", json!({"ids": placed["ids"]}))
|
|
} else {
|
|
("file.place", json!({"path": path, "link": false}))
|
|
};
|
|
let before = snapshot(&session);
|
|
let registry = registered_fonts();
|
|
let error = session.execute(command, ¶ms).expect_err("REGRESSION: the prospective document exceeds its real composite-font cap");
|
|
assert!(error.to_string().contains("256") && error.to_string().contains("composite"), "the intended cap error: {error}");
|
|
assert_eq!(snapshot(&session), before, "failed adoption preserves art, resources, selection, history and journal");
|
|
assert_eq!(registered_fonts(), registry);
|
|
}
|
|
// The public async handoff must not leave its provisional reselection behind on this new failure.
|
|
let mut session = destination(&full);
|
|
let a = session.execute("shape.rectangle", &json!({"x": 1, "y": 1, "width": 10, "height": 10})).unwrap()["id"].clone();
|
|
let b = NodeId(
|
|
session.execute("text.create", &json!({"x": 40, "y": 50, "text": "Bravo", "font": "Source Sans 3", "size": 20})).unwrap()["id"]
|
|
.as_u64()
|
|
.unwrap(),
|
|
);
|
|
session.execute("select.set", &json!({"ids": [a]})).unwrap();
|
|
let read = session.prepare_place(&json!({"path": path, "link": false, "replace": true})).unwrap().read().unwrap();
|
|
let (view, options) = select_type_range(&mut session, b);
|
|
let before = snapshot(&session);
|
|
let error = session.place_read(read).expect_err("CONTROL: prepared bytes reach the same prospective-font cap");
|
|
assert!(error.to_string().contains("256") && error.to_string().contains("composite"));
|
|
assert_eq!(snapshot(&session), before, "failed completion restores the later selection and original replay journal");
|
|
assert_eq!(session.tool_id(), "type");
|
|
assert_eq!(session.tool_options(), options, "provisional reselection must not end the actual Type range/caret");
|
|
assert!(session.tool_wants_text());
|
|
session.tool_text("Z", view).unwrap();
|
|
assert_eq!(plain_text(&session, b), "Zavo", "next typing still replaces the selected Br range");
|
|
session.tool_text("!", view).unwrap();
|
|
assert_eq!(plain_text(&session, b), "Z!avo", "continued typing uses the retained new caret");
|
|
session.keep_typing().unwrap();
|
|
assert_eq!(plain_text(&session, b), "Z!avo");
|
|
}
|
|
|
|
#[test]
|
|
fn native_composite_import_is_independent_of_unrelated_lent_font_names() {
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-composite-loan");
|
|
let path = dir.join("lettering.vectorcraft");
|
|
let part = "NativeAdoptionMissingPhysicalFace";
|
|
let incoming = CompositeFont::new("NativeAdoptionWithMissingFace", part, "Regular");
|
|
write_native(&path, &font_document(&incoming));
|
|
let mut session = destination(&[]);
|
|
let foreign = CompositeFont::new(part, "Source Sans 3", "Regular");
|
|
let db = FontDb::global();
|
|
let _loan = db.lend_document_composite_fonts(std::slice::from_ref(&foreign));
|
|
assert_eq!(*db.composite_font(part).unwrap(), foreign, "CONTROL: another drawn document lends that name on this thread");
|
|
let id = place(&mut session, &path, false);
|
|
let name = placed_family(&session, id);
|
|
let imported = CompositeFont::named(&document(&session).composite_fonts, &name).unwrap();
|
|
assert_eq!((&imported.parts, &imported.custom), (&incoming.parts, &incoming.custom));
|
|
assert!(CompositeFont::named(&document(&session).composite_fonts, part).is_none(), "the unrelated loan is not adopted");
|
|
assert_eq!(*db.composite_font(part).unwrap(), foreign, "the caller's loan remains intact");
|
|
}
|
|
|
|
fn outline_paths(session: &Session, reply: &Value) -> Vec<vectorcraft_geom::PathData> {
|
|
let mut paths = Vec::new();
|
|
for id in reply["ids"].as_array().unwrap() {
|
|
document(session).node(NodeId(id.as_u64().unwrap())).unwrap().walk(&mut |node| {
|
|
if let NodeKind::Path { path, .. } = &node.kind {
|
|
paths.push(path.clone());
|
|
}
|
|
});
|
|
}
|
|
assert!(!paths.is_empty(), "CONTROL: real source lettering produced outlines");
|
|
paths
|
|
}
|
|
|
|
#[test]
|
|
fn native_composite_fonts_reach_later_batch_outline_steps() {
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-composite-batch");
|
|
let path = dir.join("scaled-lettering.vectorcraft");
|
|
let mut incoming = CompositeFont::new("NativeAdoptionBatch", "Source Sans 3", "Regular");
|
|
incoming.parts.roman.size = 125.0;
|
|
let source = write_native(&path, &font_document(&incoming));
|
|
let mut text = None;
|
|
source.layers[0].walk(&mut |node| {
|
|
if let NodeKind::Text(value) = &node.kind {
|
|
text = Some((**value).clone());
|
|
}
|
|
});
|
|
let text = text.unwrap();
|
|
let db = FontDb::global();
|
|
let intended = {
|
|
let _loan = db.lend_document_composite_fonts(std::slice::from_ref(&incoming));
|
|
vectorcraft_text::layout(db, &text)
|
|
};
|
|
let fallback = {
|
|
let _loan = db.lend_document_composite_fonts(&[]);
|
|
vectorcraft_text::layout(db, &text)
|
|
};
|
|
assert!((intended.bounds.width() - fallback.bounds.width()).abs() > 0.1, "CONTROL: this source distinguishes intended and fallback glyphs");
|
|
let mut old = incoming.clone();
|
|
old.parts.roman.size = 100.0;
|
|
let mut batch = destination(std::slice::from_ref(&old));
|
|
let (kept_id, kept_art) = kept_text(&mut batch, &old);
|
|
let reply = batch
|
|
.execute(
|
|
"command.batch",
|
|
&json!({"commands": [
|
|
{"command": "file.place", "params": {"path": path, "link": false, "at": [30,25]}},
|
|
{"command": "type.createOutlines", "params": {}}
|
|
]}),
|
|
)
|
|
.unwrap();
|
|
let actual = outline_paths(&batch, &reply["results"][1]);
|
|
let imported = document(&batch)
|
|
.composite_fonts
|
|
.iter()
|
|
.find(|font| font.parts == incoming.parts && font.custom == incoming.custom)
|
|
.expect("CONTROL: this batch adopted the distinct 125% meaning");
|
|
assert!(!CompositeFont::same_name(&imported.name, &old.name));
|
|
assert_eq!(document(&batch).node(kept_id), Some(&kept_art));
|
|
assert_eq!(CompositeFont::named(&document(&batch).composite_fonts, &old.name), Some(&old));
|
|
// Run the ordinary control only after the cold batch, so it cannot prewarm the imported alias.
|
|
let mut ordinary = destination(std::slice::from_ref(&old));
|
|
place(&mut ordinary, &path, false);
|
|
let reply = ordinary.execute("type.createOutlines", &json!({})).unwrap();
|
|
assert_eq!(actual, outline_paths(&ordinary, &reply), "batch layout must see the imported meaning before top-level synchronization");
|
|
}
|
|
|
|
fn menu_has(reply: &Value, name: &str) -> bool {
|
|
reply["families"].as_array().unwrap().iter().any(|family| family.as_str().is_some_and(|f| f.eq_ignore_ascii_case(name)))
|
|
}
|
|
|
|
#[test]
|
|
fn native_composite_preview_fonts_follow_cancel_replacement_commit_and_undo() {
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-composite-preview");
|
|
let path = dir.join("lettering.vectorcraft");
|
|
let incoming = CompositeFont::new("NativeAdoptionPreview", "Source Sans 3", "Regular");
|
|
write_native(&path, &font_document(&incoming));
|
|
let held = CompositeFont::new("NativeAdoptionPreviewHeld", "Source Sans 3", "Regular");
|
|
for boundary in 0..3 {
|
|
let mut session = destination(std::slice::from_ref(&held));
|
|
let before = document(&session).clone();
|
|
let registry = registered_fonts();
|
|
assert!(!menu_has(&session.execute("text.fontList", &json!({})).unwrap(), &incoming.name));
|
|
session.begin_interaction("Preview native lettering").unwrap();
|
|
session
|
|
.preview(
|
|
"command.batch",
|
|
&json!({"commands": [
|
|
{"command": "file.place", "params": {"path": path, "link": false}}
|
|
]}),
|
|
)
|
|
.unwrap();
|
|
assert_eq!(CompositeFont::named(&document(&session).composite_fonts, &incoming.name), Some(&incoming));
|
|
assert_eq!(*FontDb::global().composite_font(&incoming.name).unwrap(), incoming);
|
|
match boundary {
|
|
0 => {
|
|
session.cancel_interaction().unwrap();
|
|
assert_eq!(registered_fonts(), registry, "cancel restores the registry before another command can hide a leak");
|
|
}
|
|
1 => {
|
|
let next = session.preview("text.fontList", &json!({})).unwrap();
|
|
assert!(!menu_has(&next, &incoming.name), "the first consumer after replacing a preview must not see its old font");
|
|
assert_eq!(registered_fonts(), registry);
|
|
session.cancel_interaction().unwrap();
|
|
}
|
|
_ => {
|
|
session.commit_interaction().unwrap();
|
|
assert_eq!(*FontDb::global().composite_font(&incoming.name).unwrap(), incoming, "commit keeps the imported meaning");
|
|
session.execute("edit.undo", &json!({})).unwrap();
|
|
assert_eq!(registered_fonts(), registry, "Undo restores the pre-preview font meanings");
|
|
}
|
|
}
|
|
assert_eq!(document(&session), &before);
|
|
assert_eq!(CompositeFont::named(&document(&session).composite_fonts, &held.name), Some(&held));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn native_composite_alias_forced_symbol_copy_keeps_all_symbol_options() {
|
|
use vectorcraft_engine::cmd::clipboard::Clipboard;
|
|
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-composite-symbol");
|
|
let path = dir.join("symbol-lettering.vectorcraft");
|
|
let incoming = CompositeFont::new("NativeAdoptionSymbolFont", "Source Sans 3", "Regular");
|
|
let mut builder = Session::new();
|
|
builder.add_document(font_document(&incoming), None);
|
|
let mut text_id = None;
|
|
document(&builder).layers[0].walk(&mut |node| {
|
|
if matches!(&node.kind, NodeKind::Text(_)) {
|
|
assert!(text_id.replace(node.id).is_none(), "CONTROL: exactly one source lettering object");
|
|
}
|
|
});
|
|
let symbol = "NativeAdoptionLetteringSymbol";
|
|
builder
|
|
.execute(
|
|
"symbol.new",
|
|
&json!({
|
|
"name": symbol, "ids": [text_id.unwrap().0], "registration": 0, "exportType": "graphic",
|
|
"nineSlice": true, "nineSliceGuides": [0.2, 0.3, 0.25, 0.15], "alignToPixelGrid": true
|
|
}),
|
|
)
|
|
.unwrap();
|
|
let source = write_native(&path, document(&builder));
|
|
let options = Clipboard::from_document(&source).symbol_options.get(symbol).unwrap().clone();
|
|
assert!(options.size.is_some(), "CONTROL: the real symbol constructor recorded its natural size");
|
|
assert_eq!(options.registration, vectorcraft_doc::SymbolRegistration(0));
|
|
assert_eq!(options.export_type, vectorcraft_doc::SymbolExportType::Graphic);
|
|
assert_eq!(options.nine_slice, vectorcraft_doc::SymbolNineSlice::new(0.2, 0.3, 0.25, 0.15));
|
|
assert!(options.pixel_aligned);
|
|
|
|
for linked in [false, true] {
|
|
let mut target = source.clone();
|
|
let index = target.composite_fonts.iter().position(|font| CompositeFont::same_name(&font.name, &incoming.name)).unwrap();
|
|
target.composite_fonts[index].parts.roman.size = 125.0;
|
|
let held_font = target.composite_fonts[index].clone();
|
|
assert_eq!(target.symbols, source.symbols, "CONTROL: identical symbol names and art before only the font meaning changes");
|
|
let mut session = Session::new();
|
|
session.add_document(target, None);
|
|
let before = document(&session).clone();
|
|
let held_options = Clipboard::from_document(&before).symbol_options.get(symbol).unwrap().clone();
|
|
assert_eq!(held_options, options, "CONTROL: options also match; the font alias alone must force the new copy");
|
|
|
|
let id = place(&mut session, &path, linked);
|
|
let mut used_symbols = Vec::new();
|
|
document(&session).node(id).unwrap().walk(&mut |node| {
|
|
if let NodeKind::SymbolInstance { symbol, .. } = &node.kind {
|
|
used_symbols.push(symbol.clone());
|
|
}
|
|
});
|
|
assert_eq!(used_symbols.len(), 1);
|
|
let copied_name = &used_symbols[0];
|
|
assert_ne!(copied_name.as_str(), symbol, "the import must not reuse art whose same family name has a different meaning here");
|
|
let copied = document(&session).symbols.iter().find(|definition| &definition.name == copied_name).unwrap();
|
|
let mut families = Vec::new();
|
|
copied.art.walk(&mut |node| {
|
|
if let NodeKind::Text(text) = &node.kind {
|
|
families.extend(text.runs.iter().filter(|run| run.text == "Ab").map(|run| run.style.font_family.clone()));
|
|
}
|
|
});
|
|
assert_eq!(families.len(), 1);
|
|
assert!(!CompositeFont::same_name(&families[0], &incoming.name));
|
|
let adopted = CompositeFont::named(&document(&session).composite_fonts, &families[0]).unwrap();
|
|
assert_eq!((&adopted.parts, &adopted.custom), (&incoming.parts, &incoming.custom));
|
|
let after = Clipboard::from_document(document(&session));
|
|
assert_eq!(after.symbol_options.get(copied_name), Some(&options), "the alias-forced copy preserves all five option fields");
|
|
assert_eq!(after.symbol_options.get(symbol), Some(&held_options));
|
|
assert_eq!(CompositeFont::named(&document(&session).composite_fonts, &incoming.name), Some(&held_font));
|
|
assert_eq!(
|
|
document(&session).symbols.iter().find(|definition| definition.name == symbol),
|
|
before.symbols.iter().find(|definition| definition.name == symbol)
|
|
);
|
|
for layer in &before.layers {
|
|
for child in layer.children().unwrap() {
|
|
assert_eq!(document(&session).node(child.id), Some(child.as_ref()), "existing destination art remains exact");
|
|
}
|
|
}
|
|
assert_eq!(document(&session).symbols.len(), before.symbols.len() + 1);
|
|
let first_symbols = document(&session).symbols.clone();
|
|
let first_fonts = document(&session).composite_fonts.clone();
|
|
let first_resources = native_resource_counts(document(&session));
|
|
let repeated = place(&mut session, &path, linked);
|
|
let repeated_name = only_symbol_name(document(&session).node(repeated).unwrap());
|
|
assert_eq!(&repeated_name, copied_name, "repeating the import reuses the actual first symbol alias");
|
|
assert_eq!(symbol_font_family(document(&session), &repeated_name), families[0]);
|
|
assert_eq!(native_resource_counts(document(&session)), first_resources, "repetition adds art, not more resources");
|
|
assert_eq!(document(&session).symbols, first_symbols);
|
|
assert_eq!(document(&session).composite_fonts, first_fonts);
|
|
assert_original_native_art(document(&session), &before);
|
|
}
|
|
}
|
|
|
|
fn only_symbol_name(root: &Node) -> String {
|
|
let mut names = Vec::new();
|
|
root.walk(&mut |node| {
|
|
if let NodeKind::SymbolInstance { symbol, .. } = &node.kind {
|
|
names.push(symbol.clone());
|
|
}
|
|
});
|
|
assert_eq!(names.len(), 1, "CONTROL: one instance at this level of the native symbol chain");
|
|
names.pop().unwrap()
|
|
}
|
|
|
|
fn symbol_font_family(doc: &Document, symbol: &str) -> String {
|
|
let art = &doc.symbols.iter().find(|definition| definition.name == symbol).unwrap().art;
|
|
let mut names = Vec::new();
|
|
art.walk(&mut |node| {
|
|
if let NodeKind::Text(text) = &node.kind {
|
|
names.extend(text.runs.iter().filter(|run| run.text == "Ab").map(|run| run.style.font_family.clone()));
|
|
}
|
|
});
|
|
assert_eq!(names.len(), 1, "CONTROL: the inner symbol retains editable source lettering");
|
|
names.pop().unwrap()
|
|
}
|
|
|
|
fn native_resource_counts(doc: &Document) -> Value {
|
|
json!({"fonts": doc.composite_fonts.len(), "symbols": doc.symbols.len(), "patterns": doc.patterns.len(),
|
|
"images": doc.images.len(), "graphicStyles": doc.graphic_styles.len(), "charStyles": doc.char_styles.len(),
|
|
"paraStyles": doc.para_styles.len(), "swatches": doc.swatches.len(), "groups": doc.swatch_groups.len(),
|
|
"graphDesigns": doc.graph_designs.len(), "brushes": vectorcraft_brush::library(doc).len()})
|
|
}
|
|
|
|
fn assert_original_native_art(doc: &Document, before: &Document) {
|
|
for symbol in &before.symbols {
|
|
assert_eq!(doc.symbols.iter().find(|definition| definition.name == symbol.name), Some(symbol));
|
|
}
|
|
for font in &before.composite_fonts {
|
|
assert_eq!(CompositeFont::named(&doc.composite_fonts, &font.name), Some(font));
|
|
}
|
|
for pattern in &before.patterns {
|
|
assert_eq!(doc.pattern(&pattern.name), Some(pattern), "the original destination pattern meaning is unchanged");
|
|
}
|
|
for brush in vectorcraft_brush::library(before) {
|
|
assert_eq!(vectorcraft_brush::find(doc, &brush.name), Some(brush), "the original stored brush meaning is unchanged");
|
|
}
|
|
for layer in &before.layers {
|
|
for child in layer.children().unwrap() {
|
|
assert_eq!(doc.node(child.id), Some(child.as_ref()), "the pre-existing destination art is unchanged");
|
|
}
|
|
}
|
|
}
|
|
|
|
fn pattern_in_symbol(doc: &Document, symbol: &str) -> String {
|
|
let art = &doc.symbols.iter().find(|definition| definition.name == symbol).unwrap().art;
|
|
let mut names = Vec::new();
|
|
art.walk(&mut |node| {
|
|
if let vectorcraft_color::Paint::Pattern { pattern, .. } = node.appearance.fill_paint() {
|
|
names.push(pattern.clone());
|
|
}
|
|
});
|
|
assert_eq!(names.len(), 1, "CONTROL: one pattern dependency in the inner symbol");
|
|
names.pop().unwrap()
|
|
}
|
|
|
|
fn pattern_font_family(doc: &Document, pattern: &str) -> String {
|
|
let mut names = Vec::new();
|
|
for root in &doc.pattern(pattern).unwrap().art {
|
|
root.walk(&mut |node| {
|
|
if let NodeKind::Text(text) = &node.kind {
|
|
names.extend(text.runs.iter().filter(|run| run.text == "Ab").map(|run| run.style.font_family.clone()));
|
|
}
|
|
});
|
|
}
|
|
assert_eq!(names.len(), 1, "CONTROL: the pattern holds the one editable source lettering run");
|
|
names.pop().unwrap()
|
|
}
|
|
|
|
fn brush_in_symbol(doc: &Document, symbol: &str) -> String {
|
|
let art = &doc.symbols.iter().find(|definition| definition.name == symbol).unwrap().art;
|
|
let mut names = Vec::new();
|
|
art.walk(&mut |node| {
|
|
if let Some(brush) = node.appearance.stroke().and_then(|stroke| stroke.brush.as_ref()) {
|
|
names.push(brush.clone());
|
|
}
|
|
});
|
|
assert_eq!(names.len(), 1, "CONTROL: one brush dependency in the inner symbol");
|
|
names.pop().unwrap()
|
|
}
|
|
|
|
fn brush_font_family(doc: &Document, name: &str) -> String {
|
|
let brush = vectorcraft_brush::find(doc, name).unwrap();
|
|
let vectorcraft_brush::BrushKind::Art(brush) = &brush.kind else {
|
|
panic!("CONTROL: the imported resource remains an art brush");
|
|
};
|
|
assert_eq!(brush.width, 137.0, "the brush retains the non-default width chosen by its creator");
|
|
let mut names = Vec::new();
|
|
brush.art.walk(&mut |node| {
|
|
if let NodeKind::Text(text) = &node.kind {
|
|
names.extend(text.runs.iter().filter(|run| run.text == "Ab").map(|run| run.style.font_family.clone()));
|
|
}
|
|
});
|
|
assert_eq!(names.len(), 1, "CONTROL: the brush library, not document art, holds the editable source lettering");
|
|
names.pop().unwrap()
|
|
}
|
|
|
|
const NESTED_INNER: &str = "NativeAdoptionInner";
|
|
const NESTED_OUTER: &str = "NativeAdoptionOuter";
|
|
const NESTED_PATTERN: &str = "NativeAdoptionPatternText";
|
|
const NESTED_BRUSH: &str = "NativeAdoptionBrushText";
|
|
|
|
// Build the same nested native artifact for each direct, pattern-backed or brush-backed case.
|
|
fn nested_native_source(path: &std::path::Path, incoming: &CompositeFont, dependency: usize) -> (Session, Document) {
|
|
let inner = NESTED_INNER;
|
|
let outer = NESTED_OUTER;
|
|
let mut builder = Session::new();
|
|
builder.add_document(font_document(incoming), None);
|
|
let mut text_id = None;
|
|
document(&builder).layers[0].walk(&mut |node| {
|
|
if matches!(&node.kind, NodeKind::Text(_)) {
|
|
assert!(text_id.replace(node.id).is_none());
|
|
}
|
|
});
|
|
let pattern_name = NESTED_PATTERN;
|
|
let brush_name = NESTED_BRUSH;
|
|
if dependency != 0 {
|
|
if dependency == 1 {
|
|
builder.execute("object.pattern.make", &json!({"ids":[text_id.unwrap().0],"name":pattern_name,"edit":false})).unwrap();
|
|
} else {
|
|
builder
|
|
.execute(
|
|
"brush.new",
|
|
&json!({"type":"art","name":brush_name,"ids":[text_id.unwrap().0],
|
|
"params":{"width":137.0}}),
|
|
)
|
|
.unwrap();
|
|
}
|
|
let mut doc = document(&builder).clone();
|
|
doc.remove(text_id.unwrap()).unwrap();
|
|
let id = doc.alloc_id();
|
|
let appearance = if dependency == 1 {
|
|
vectorcraft_doc::Appearance::basic(vectorcraft_doc::pattern::pattern_paint(pattern_name), vectorcraft_color::Paint::None, 0.0)
|
|
} else {
|
|
let mut appearance = vectorcraft_doc::Appearance::basic(
|
|
vectorcraft_color::Paint::None,
|
|
vectorcraft_color::Paint::solid(vectorcraft_color::Color::BLACK),
|
|
1.0,
|
|
);
|
|
appearance.stroke_mut().unwrap().brush = Some(brush_name.into());
|
|
appearance
|
|
};
|
|
let rectangle = Node::path(id, vectorcraft_geom::shapes::rectangle(vectorcraft_geom::Rect::new(0.0, 0.0, 20.0, 20.0)), appearance);
|
|
std::sync::Arc::make_mut(&mut doc.layers[0]).children_mut().unwrap().push(std::sync::Arc::new(rectangle));
|
|
doc.fix_next_id();
|
|
builder = Session::new();
|
|
builder.add_document(doc, None);
|
|
text_id = Some(id);
|
|
}
|
|
let made = builder
|
|
.execute(
|
|
"symbol.new",
|
|
&json!({
|
|
"name": inner, "ids": [text_id.unwrap().0], "registration": 0, "exportType": "graphic",
|
|
"nineSlice": true, "nineSliceGuides": [0.2,0.3,0.25,0.15], "alignToPixelGrid": true
|
|
}),
|
|
)
|
|
.unwrap();
|
|
builder
|
|
.execute(
|
|
"symbol.new",
|
|
&json!({
|
|
"name": outer, "ids": [made["id"]], "registration": 8, "exportType": "graphic",
|
|
"nineSlice": true, "nineSliceGuides": [0.1,0.2,0.3,0.25], "alignToPixelGrid": true
|
|
}),
|
|
)
|
|
.unwrap();
|
|
let source = write_native(path, document(&builder));
|
|
(builder, source)
|
|
}
|
|
|
|
// Check one complete inline or embedded import and its repeat against the same source artifact.
|
|
fn assert_nested_alias_roundtrip(
|
|
source: &Document,
|
|
source_options: &std::collections::BTreeMap<String, vectorcraft_engine::cmd::clipboard::SymbolOptions>,
|
|
path: &Path,
|
|
incoming: &CompositeFont,
|
|
dependency: usize,
|
|
linked: bool,
|
|
) {
|
|
use vectorcraft_engine::cmd::clipboard::Clipboard;
|
|
|
|
let inner = NESTED_INNER;
|
|
let outer = NESTED_OUTER;
|
|
let pattern_name = NESTED_PATTERN;
|
|
let brush_name = NESTED_BRUSH;
|
|
let mut target = source.clone();
|
|
let font = target.composite_fonts.iter_mut().find(|font| CompositeFont::same_name(&font.name, &incoming.name)).unwrap();
|
|
font.parts.roman.size = 125.0;
|
|
assert_eq!(target.symbols, source.symbols, "CONTROL: only the meaning of the existing font differs");
|
|
let mut session = Session::new();
|
|
session.add_document(target, None);
|
|
let before = document(&session).clone();
|
|
let id = place(&mut session, path, linked);
|
|
let copied_outer = only_symbol_name(document(&session).node(id).unwrap());
|
|
assert_ne!(copied_outer, outer);
|
|
let outer_art = &document(&session).symbols.iter().find(|definition| definition.name == copied_outer).unwrap().art;
|
|
let copied_inner = only_symbol_name(outer_art);
|
|
assert_ne!(copied_inner, inner, "the child follows its aliased composite-font meaning");
|
|
let copied_pattern = (dependency == 1).then(|| pattern_in_symbol(document(&session), &copied_inner));
|
|
let copied_brush = (dependency == 2).then(|| brush_in_symbol(document(&session), &copied_inner));
|
|
let family = if let Some(pattern) = &copied_pattern {
|
|
assert_ne!(pattern.as_str(), pattern_name, "the pattern also gets a new meaning under the font alias");
|
|
pattern_font_family(document(&session), pattern)
|
|
} else if let Some(brush) = &copied_brush {
|
|
assert_ne!(brush.as_str(), brush_name, "the stored brush gets its own aliased font meaning");
|
|
brush_font_family(document(&session), brush)
|
|
} else {
|
|
symbol_font_family(document(&session), &copied_inner)
|
|
};
|
|
assert!(!CompositeFont::same_name(&family, &incoming.name));
|
|
let definition = CompositeFont::named(&document(&session).composite_fonts, &family).unwrap();
|
|
assert_eq!((&definition.parts, &definition.custom), (&incoming.parts, &incoming.custom));
|
|
let imported_options = Clipboard::from_document(document(&session)).symbol_options;
|
|
assert_eq!(imported_options.get(&copied_inner), source_options.get(inner));
|
|
assert_eq!(imported_options.get(&copied_outer), source_options.get(outer), "indirect parent copies keep all five option fields");
|
|
assert_eq!(imported_options.get(inner), source_options.get(inner));
|
|
assert_eq!(imported_options.get(outer), source_options.get(outer));
|
|
assert_original_native_art(document(&session), &before);
|
|
assert_eq!(document(&session).symbols.len(), before.symbols.len() + 2);
|
|
|
|
let first_symbols = document(&session).symbols.clone();
|
|
let first_fonts = document(&session).composite_fonts.clone();
|
|
let first_resources = native_resource_counts(document(&session));
|
|
let first_brushes = vectorcraft_brush::library(document(&session));
|
|
let repeated = place(&mut session, path, linked);
|
|
let repeated_outer = only_symbol_name(document(&session).node(repeated).unwrap());
|
|
let repeated_art = &document(&session).symbols.iter().find(|definition| definition.name == repeated_outer).unwrap().art;
|
|
assert_eq!(repeated_outer, copied_outer);
|
|
assert_eq!(only_symbol_name(repeated_art), copied_inner, "repeat also reuses the actual mapped child name");
|
|
if let Some(pattern) = &copied_pattern {
|
|
assert_eq!(pattern_in_symbol(document(&session), &copied_inner), *pattern);
|
|
assert_eq!(pattern_font_family(document(&session), pattern), family);
|
|
} else if let Some(brush) = &copied_brush {
|
|
assert_eq!(brush_in_symbol(document(&session), &copied_inner), *brush);
|
|
assert_eq!(brush_font_family(document(&session), brush), family);
|
|
} else {
|
|
assert_eq!(symbol_font_family(document(&session), &copied_inner), family);
|
|
}
|
|
assert_eq!(native_resource_counts(document(&session)), first_resources);
|
|
assert_eq!(document(&session).symbols, first_symbols);
|
|
assert_eq!(document(&session).composite_fonts, first_fonts);
|
|
assert_eq!(vectorcraft_brush::library(document(&session)), first_brushes);
|
|
assert_original_native_art(document(&session), &before);
|
|
}
|
|
|
|
#[test]
|
|
fn native_composite_nested_alias_copies_keep_outer_and_inner_options_and_reuse() {
|
|
use vectorcraft_engine::cmd::clipboard::Clipboard;
|
|
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-composite-nested");
|
|
let incoming = CompositeFont::new("NativeAdoptionNestedFont", "Source Sans 3", "Regular");
|
|
for dependency in 0..3 {
|
|
let path = dir.join(format!("nested-lettering-{dependency}.vectorcraft"));
|
|
let inner = NESTED_INNER;
|
|
let outer = NESTED_OUTER;
|
|
let (_builder, source) = nested_native_source(&path, &incoming, dependency);
|
|
let source_options = Clipboard::from_document(&source).symbol_options;
|
|
for (name, registration, guides) in [
|
|
(inner, 0, vectorcraft_doc::SymbolNineSlice::new(0.2, 0.3, 0.25, 0.15)),
|
|
(outer, 8, vectorcraft_doc::SymbolNineSlice::new(0.1, 0.2, 0.3, 0.25)),
|
|
] {
|
|
let options = source_options.get(name).unwrap();
|
|
assert!(options.size.is_some());
|
|
assert_eq!(options.registration, vectorcraft_doc::SymbolRegistration(registration));
|
|
assert_eq!(options.export_type, vectorcraft_doc::SymbolExportType::Graphic);
|
|
assert_eq!(options.nine_slice, guides);
|
|
assert!(options.pixel_aligned);
|
|
}
|
|
for linked in [false, true] {
|
|
assert_nested_alias_roundtrip(&source, &source_options, &path, &incoming, dependency, linked);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn native_composite_async_replace_is_independent_of_an_unrelated_live_tool_drag() {
|
|
use vectorcraft_geom::Rect;
|
|
use vectorcraft_tools::{Mods, PointerEvent, PointerKind, ToolKey};
|
|
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-composite-drag");
|
|
let path = dir.join("replacement.vectorcraft");
|
|
let incoming = CompositeFont::new("NativeAdoptionDrag", "Source Sans 3", "Regular");
|
|
write_native(&path, &font_document(&incoming));
|
|
let view = vectorcraft_engine::ViewInfo { smart_guides: false, ..Default::default() };
|
|
for cancel in [true, false] {
|
|
let mut session = Session::new();
|
|
session.prefs.real_time_drawing = true;
|
|
session.add_document(Document::new(400.0, 400.0), None);
|
|
let a = session.execute("shape.rectangle", &json!({"x":10,"y":10,"width":20,"height":20})).unwrap();
|
|
let a = NodeId(a["id"].as_u64().unwrap());
|
|
let b = session.execute("shape.rectangle", &json!({"x":100,"y":100,"width":40,"height":40})).unwrap();
|
|
let b = NodeId(b["id"].as_u64().unwrap());
|
|
let old_a = document(&session).node(a).unwrap().clone();
|
|
let old_b = document(&session).node(b).unwrap().clone();
|
|
session.execute("select.set", &json!({"ids":[a.0]})).unwrap();
|
|
let read = session.prepare_place(&json!({"path":path,"link":false,"replace":true})).unwrap().read().unwrap();
|
|
session.execute("select.set", &json!({"ids":[b.0]})).unwrap();
|
|
session.select_tool("selection", view).unwrap();
|
|
let steps = session.doc().unwrap().history.undo.len();
|
|
session.pointer(&PointerEvent::new(PointerKind::Down, 120.0, 120.0), view).unwrap();
|
|
session.pointer(&PointerEvent::new(PointerKind::Drag, 140.0, 130.0), view).unwrap();
|
|
assert!(session.in_interaction(), "CONTROL: actual selection-tool pointer actions began a drag");
|
|
assert_eq!(session.tool_id(), "selection");
|
|
assert_eq!(session.doc().unwrap().selection.objects, vec![b]);
|
|
assert_eq!(document(&session).bounds_of(&[b], false), Some(Rect::new(120.0, 110.0, 160.0, 150.0)));
|
|
assert_eq!(session.doc().unwrap().history.undo.len(), steps, "CONTROL: this is still an uncommitted tool preview");
|
|
|
|
let reply = session.place_read(read).expect("CONTROL: the native replacement succeeds while a different object is dragged");
|
|
let replacement = NodeId(reply["ids"][0].as_u64().unwrap());
|
|
let published = document(&session).node(replacement).unwrap().clone();
|
|
let completed_steps = session.doc().unwrap().history.undo.len();
|
|
assert_ne!(document(&session).node(a), Some(&old_a));
|
|
assert!(session.in_interaction(), "the unrelated tool drag remains live after external completion");
|
|
session.pointer(&PointerEvent::new(PointerKind::Drag, 170.0, 150.0), view).unwrap();
|
|
if cancel {
|
|
session.tool_key(ToolKey::Escape, Mods::default(), view).unwrap();
|
|
}
|
|
session.pointer(&PointerEvent::new(PointerKind::Up, 170.0, 150.0), view).unwrap();
|
|
assert!(!session.in_interaction());
|
|
assert_eq!(document(&session).node(replacement), Some(&published), "cancel/release of B must keep the published replacement of A");
|
|
assert_eq!(completed_steps, steps + 1, "the completion has its own undo step before the drag ends");
|
|
if cancel {
|
|
assert_eq!(document(&session).node(b), Some(&old_b));
|
|
assert_eq!(session.doc().unwrap().history.undo.len(), steps + 1);
|
|
} else {
|
|
assert_eq!(document(&session).bounds_of(&[b], false), Some(Rect::new(150.0, 130.0, 190.0, 170.0)));
|
|
assert_eq!(session.doc().unwrap().history.undo.len(), steps + 2, "commit creates a separate drag step");
|
|
session.execute("edit.undo", &json!({})).unwrap();
|
|
assert_eq!(document(&session).node(b), Some(&old_b));
|
|
assert_eq!(document(&session).node(replacement), Some(&published), "first Undo removes only the drag");
|
|
}
|
|
session.execute("edit.undo", &json!({})).unwrap();
|
|
assert_eq!(document(&session).node(a), Some(&old_a), "the next Undo independently restores the replaced target");
|
|
assert_ne!(document(&session).node(replacement), Some(&published), "Undo removes the replacement meaning even if an id is reused");
|
|
assert_eq!(document(&session).node(b), Some(&old_b));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn native_composite_mapped_symbol_art_shares_only_matching_options() {
|
|
use vectorcraft_engine::cmd::clipboard::Clipboard;
|
|
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-composite-mapped-options");
|
|
let path = dir.join("mapped-options.vectorcraft");
|
|
let incoming = CompositeFont::new("NativeAdoptionMappedOptions", "Source Sans 3", "Regular");
|
|
assert_native_source_font_geometry(&dir.join("source-geometry.vectorcraft"), &incoming);
|
|
let mut builder = Session::new();
|
|
builder.add_document(font_document(&incoming), None);
|
|
let children = document(&builder).layers[0].children().unwrap();
|
|
assert_eq!(children.len(), 1, "CONTROL: the one source lettering object");
|
|
let text_id = children[0].id;
|
|
let symbol = "NativeAdoptionMappedOptionsSymbol";
|
|
builder
|
|
.execute(
|
|
"symbol.new",
|
|
&json!({
|
|
"name": symbol, "ids": [text_id.0], "registration": 0, "exportType": "graphic",
|
|
"nineSlice": true, "nineSliceGuides": [0.2, 0.3, 0.25, 0.15], "alignToPixelGrid": true
|
|
}),
|
|
)
|
|
.unwrap();
|
|
let source = write_native(&path, document(&builder));
|
|
let source_options = Clipboard::from_document(&source).symbol_options.get(symbol).unwrap().clone();
|
|
for linked in [false, true] {
|
|
let mut target = source.clone();
|
|
let font = target.composite_fonts.iter_mut().find(|font| CompositeFont::same_name(&font.name, &incoming.name)).unwrap();
|
|
font.parts.roman.size = 125.0;
|
|
let mut seed = Session::new();
|
|
seed.add_document(target, None);
|
|
let first = place(&mut seed, &path, linked);
|
|
let alias_symbol = only_symbol_name(document(&seed).node(first).unwrap());
|
|
let alias_family = symbol_font_family(document(&seed), &alias_symbol);
|
|
assert_ne!(alias_symbol, symbol);
|
|
assert!(!CompositeFont::same_name(&alias_family, &incoming.name));
|
|
for matching_options in [false, true] {
|
|
let mut target = document(&seed).clone();
|
|
let definition = target.symbols.iter_mut().find(|definition| definition.name == symbol).unwrap();
|
|
// The original-name art now already uses the equivalent font selected by the import.
|
|
let NodeKind::Text(text) = &mut std::sync::Arc::make_mut(&mut definition.art).kind else {
|
|
panic!("CONTROL: the one source lettering object stays text in its symbol");
|
|
};
|
|
for run in &mut text.runs {
|
|
if CompositeFont::same_name(&run.style.font_family, &incoming.name) {
|
|
run.style.font_family = alias_family.clone();
|
|
}
|
|
}
|
|
assert_eq!(symbol_font_family(&target, symbol), alias_family);
|
|
if !matching_options {
|
|
// Public document storage keeps natural size separately from the symbol's art.
|
|
target.unknown.get_mut(vectorcraft_doc::SYMBOL_SIZES).unwrap().as_object_mut().unwrap().remove(symbol);
|
|
target.set_symbol_registration(symbol, vectorcraft_doc::SymbolRegistration(8));
|
|
target.set_symbol_export_type(symbol, vectorcraft_doc::SymbolExportType::default());
|
|
target.set_symbol_nine_slice(symbol, None);
|
|
target.set_symbol_pixel_aligned(symbol, false);
|
|
}
|
|
let held_options = Clipboard::from_document(&target).symbol_options.get(symbol).unwrap().clone();
|
|
if matching_options {
|
|
assert_eq!(held_options, source_options, "CONTROL: equal art and options may still share the original name");
|
|
} else {
|
|
assert_ne!(held_options.size, source_options.size);
|
|
assert_ne!(held_options.registration, source_options.registration);
|
|
assert_ne!(held_options.export_type, source_options.export_type);
|
|
assert_ne!(held_options.nine_slice, source_options.nine_slice);
|
|
assert_ne!(held_options.pixel_aligned, source_options.pixel_aligned);
|
|
}
|
|
let mut session = Session::new();
|
|
session.add_document(target, None);
|
|
let before = document(&session).clone();
|
|
let resources = native_resource_counts(&before);
|
|
let id = place(&mut session, &path, linked);
|
|
let imported = only_symbol_name(document(&session).node(id).unwrap());
|
|
let expected = if matching_options { symbol } else { alias_symbol.as_str() };
|
|
assert_eq!(imported, expected, "matching art must not capture different incoming symbol options");
|
|
assert_eq!(symbol_font_family(document(&session), &imported), alias_family);
|
|
let options = Clipboard::from_document(document(&session)).symbol_options;
|
|
assert_eq!(options.get(&imported), Some(&source_options));
|
|
assert_eq!(options.get(symbol), Some(&held_options));
|
|
assert_eq!(native_resource_counts(document(&session)), resources);
|
|
assert_original_native_art(document(&session), &before);
|
|
let repeated = place(&mut session, &path, linked);
|
|
assert_eq!(only_symbol_name(document(&session).node(repeated).unwrap()), imported);
|
|
assert_eq!(native_resource_counts(document(&session)), resources);
|
|
assert_original_native_art(document(&session), &before);
|
|
}
|
|
}
|
|
}
|
|
|
|
// The native reader must measure its own composite meanings before cropping, not the front document's.
|
|
fn assert_native_source_font_geometry(path: &Path, incoming: &CompositeFont) {
|
|
write_native(path, &font_document(incoming));
|
|
for linked in [false, true] {
|
|
let mut ordinary_placement = None;
|
|
let mut ordinary_destination_width = None;
|
|
for scale in [100.0, 125.0] {
|
|
let mut existing = incoming.clone();
|
|
existing.parts.roman.size = scale;
|
|
let mut session = destination(std::slice::from_ref(&existing));
|
|
let (kept_id, kept_art) = kept_text(&mut session, &existing);
|
|
let kept_width = kept_art.geometric_bounds().unwrap().width();
|
|
if let Some(width) = ordinary_destination_width {
|
|
assert!(kept_width > width, "CONTROL: the destination really measures this same font family at the larger size");
|
|
} else {
|
|
ordinary_destination_width = Some(kept_width);
|
|
}
|
|
let reply = session.execute("file.place", &json!({"path": path, "link": linked, "crop": "bounding", "at": [30, 25]})).unwrap();
|
|
assert_eq!(reply["linked"], linked);
|
|
let id = NodeId(reply["ids"][0].as_u64().unwrap());
|
|
if linked {
|
|
session.execute("links.embed", &json!({"ids": [id.0]})).unwrap();
|
|
}
|
|
let doc = document(&session);
|
|
let family = placed_family(&session, id);
|
|
let font = CompositeFont::named(&doc.composite_fonts, &family).unwrap();
|
|
assert_eq!((&font.parts, &font.custom), (&incoming.parts, &incoming.custom));
|
|
assert_eq!(CompositeFont::same_name(&family, &incoming.name), scale == 100.0);
|
|
let bounds = doc.node(id).unwrap().geometric_bounds_with(&doc.symbol_boxes()).unwrap();
|
|
assert!(bounds.width() > 0.0 && bounds.height() > 0.0);
|
|
if let Some(expected) = ordinary_placement {
|
|
assert_eq!(bounds, expected, "bounding placement keeps source geometry under a conflicting destination font meaning");
|
|
} else {
|
|
ordinary_placement = Some(bounds);
|
|
}
|
|
assert_eq!(doc.node(kept_id), Some(&kept_art), "source measurement does not alter the existing destination artwork");
|
|
assert_eq!(CompositeFont::named(&doc.composite_fonts, &existing.name), Some(&existing));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn native_symbol_options_reuse_without_composite_fonts() {
|
|
use vectorcraft_engine::cmd::clipboard::Clipboard;
|
|
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-no-font-options");
|
|
let path = dir.join("plain-symbol.vectorcraft");
|
|
let mut builder = Session::new();
|
|
builder.add_document(Document::new(100.0, 80.0), None);
|
|
let text = builder.execute("text.create", &json!({"x": 4, "y": 16, "text": "Ab", "font": "Source Sans 3"})).unwrap();
|
|
let symbol = "NativeNoFontOptionsSymbol";
|
|
builder
|
|
.execute(
|
|
"symbol.new",
|
|
&json!({"name": symbol, "ids": [text["id"]], "registration": 0, "exportType": "graphic",
|
|
"nineSlice": true, "nineSliceGuides": [0.2, 0.3, 0.25, 0.15], "alignToPixelGrid": true}),
|
|
)
|
|
.unwrap();
|
|
let source = write_native(&path, document(&builder));
|
|
assert!(source.composite_fonts.is_empty(), "CONTROL: no font-family alias can enable resource finalization");
|
|
let source_options = Clipboard::from_document(&source).symbol_options.get(symbol).unwrap().clone();
|
|
for linked in [false, true] {
|
|
for matching_options in [false, true] {
|
|
let mut target = source.clone();
|
|
if !matching_options {
|
|
target.unknown.get_mut(vectorcraft_doc::SYMBOL_SIZES).unwrap().as_object_mut().unwrap().remove(symbol);
|
|
target.set_symbol_registration(symbol, vectorcraft_doc::SymbolRegistration(8));
|
|
target.set_symbol_export_type(symbol, vectorcraft_doc::SymbolExportType::default());
|
|
target.set_symbol_nine_slice(symbol, None);
|
|
target.set_symbol_pixel_aligned(symbol, false);
|
|
}
|
|
let held_options = Clipboard::from_document(&target).symbol_options.get(symbol).unwrap().clone();
|
|
if matching_options {
|
|
assert_eq!(held_options, source_options);
|
|
} else {
|
|
assert_ne!(held_options.size, source_options.size);
|
|
assert_ne!(held_options.registration, source_options.registration);
|
|
assert_ne!(held_options.export_type, source_options.export_type);
|
|
assert_ne!(held_options.nine_slice, source_options.nine_slice);
|
|
assert_ne!(held_options.pixel_aligned, source_options.pixel_aligned);
|
|
}
|
|
let mut session = Session::new();
|
|
session.add_document(target, None);
|
|
let before = document(&session).clone();
|
|
let first = place(&mut session, &path, linked);
|
|
let name = only_symbol_name(document(&session).node(first).unwrap());
|
|
assert_eq!(name == symbol, matching_options, "equal options share; different options retain a separate definition");
|
|
let options = Clipboard::from_document(document(&session)).symbol_options;
|
|
assert_eq!(options.get(&name), Some(&source_options));
|
|
assert_eq!(options.get(symbol), Some(&held_options));
|
|
assert_eq!(document(&session).symbols.len(), before.symbols.len() + usize::from(!matching_options));
|
|
assert!(document(&session).composite_fonts.is_empty());
|
|
assert_original_native_art(document(&session), &before);
|
|
let resources = native_resource_counts(document(&session));
|
|
let second = place(&mut session, &path, linked);
|
|
assert_eq!(only_symbol_name(document(&session).node(second).unwrap()), name, "repeat reuses the actual first alias");
|
|
assert_eq!(native_resource_counts(document(&session)), resources);
|
|
assert_original_native_art(document(&session), &before);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn native_source_without_composites_ignores_front_font_meaning() {
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-empty-source-fonts");
|
|
let path = dir.join("undefined-family.vectorcraft");
|
|
let mut font = CompositeFont::new("NativeEmptySourceFamily", "Source Sans 3", "Regular");
|
|
let mut source = font_document(&font);
|
|
source.composite_fonts = Vec::new().into();
|
|
let source = write_native(&path, &source);
|
|
assert!(source.composite_fonts.is_empty());
|
|
let params = json!({"path": path, "crop": "bounding"});
|
|
let mut control = destination(&[]);
|
|
let expected = control.execute("file.place.info", ¶ms).unwrap();
|
|
assert!(expected["width"].as_f64().unwrap() > 0.0 && expected["height"].as_f64().unwrap() > 0.0);
|
|
font.parts.roman.size = 125.0;
|
|
let mut front = destination(std::slice::from_ref(&font));
|
|
let (kept, art) = kept_text(&mut front, &font);
|
|
let before = document(&front).clone();
|
|
let registered = FontDb::global().composite_font(&font.name).unwrap();
|
|
assert_eq!(&*registered, &font, "CONTROL: the front document really declares the conflicting meaning");
|
|
let actual = front.execute("file.place.info", ¶ms).unwrap();
|
|
assert_eq!(actual["width"], expected["width"], "bounding reads must not borrow a family absent from the source");
|
|
assert_eq!(actual["height"], expected["height"]);
|
|
assert_eq!(actual["warnings"], expected["warnings"]);
|
|
assert_eq!(document(&front), &before, "a source-size query does not edit the front document");
|
|
assert_eq!(document(&front).node(kept), Some(&art));
|
|
assert_eq!(&*FontDb::global().composite_font(&font.name).unwrap(), &font, "source measurement restores the front meaning");
|
|
// The file's undefined family is not given a new alias or post-adoption policy by this query test.
|
|
}
|
|
|
|
fn native_mapping_source(path: &Path, incoming: &CompositeFont, location: usize, classic: bool, mixed: bool) -> (Session, Document) {
|
|
use vectorcraft_color::{Color, Paint};
|
|
use vectorcraft_doc::{Appearance, Effect};
|
|
use vectorcraft_geom::{Affine, Rect};
|
|
|
|
let (builder, mut source) = nested_native_source(path, incoming, 0);
|
|
let outer = only_symbol_name(&source.layers[0]);
|
|
let outer_art = &source.symbols.iter().find(|definition| definition.name == outer).unwrap().art;
|
|
let symbol = only_symbol_name(outer_art);
|
|
std::sync::Arc::make_mut(&mut source.layers[0]).children_mut().unwrap().clear();
|
|
let entry = if classic { json!({"surface": 1, "symbol": symbol}) } else { json!({"symbol": symbol}) };
|
|
let rejected = if classic { json!({"surface": 2, "symbol": symbol, "scaleX": 0}) } else { json!({"symbol": symbol, "scale": 0}) };
|
|
if classic {
|
|
vectorcraft_render::effects::SurfaceMap::from_value(&entry).unwrap();
|
|
assert!(vectorcraft_render::effects::SurfaceMap::from_value(&rejected).is_err());
|
|
} else {
|
|
vectorcraft_render::effects::MappedGraphic::from_value(&entry).unwrap();
|
|
assert!(vectorcraft_render::effects::MappedGraphic::from_value(&rejected).is_err());
|
|
}
|
|
let effect = Effect {
|
|
id: if classic { vectorcraft_render::effects::CLASSIC_EXTRUDE } else { vectorcraft_render::effects::ROTATE }.into(),
|
|
params: if classic {
|
|
json!({"mapArt": [entry, rejected, {"surface": 1, "symbol": symbol}], "graphics": [{"symbol": symbol}]})
|
|
} else {
|
|
json!({"graphics": [entry, rejected], "mapArt": [{"surface": 1, "symbol": symbol}]})
|
|
},
|
|
visible: true,
|
|
};
|
|
let mut carrier = Node::path(
|
|
source.alloc_id(),
|
|
vectorcraft_geom::shapes::rectangle(Rect::new(2.0, 2.0, 22.0, 22.0)),
|
|
Appearance::basic(Paint::solid(Color::BLACK), Paint::solid(Color::BLACK), 1.0),
|
|
);
|
|
match location {
|
|
0 => carrier.appearance.effects.push(effect),
|
|
1 => carrier.appearance.fill_mut().unwrap().effects.push(effect),
|
|
_ => carrier.appearance.stroke_mut().unwrap().effects.push(effect),
|
|
}
|
|
assert_eq!(vectorcraft_render::effects::appearance_graphic_symbols(&carrier.appearance), vec![symbol.clone()]);
|
|
let mut added = vec![std::sync::Arc::new(carrier)];
|
|
if mixed {
|
|
let xf = Affine::translate((40.0, 40.0)) * Affine::scale(0.1);
|
|
added.push(std::sync::Arc::new(Node::new(source.alloc_id(), NodeKind::SymbolInstance { symbol, xf })));
|
|
}
|
|
std::sync::Arc::make_mut(&mut source.layers[0]).children_mut().unwrap().extend(added);
|
|
source.fix_next_id();
|
|
(builder, source)
|
|
}
|
|
|
|
fn one_mapping_effect(root: &Node) -> vectorcraft_doc::Effect {
|
|
let mut effects = Vec::new();
|
|
root.walk(&mut |node| {
|
|
effects.extend(node.appearance.effects.iter().cloned());
|
|
for item in &node.appearance.items {
|
|
effects.extend(item.effects().iter().cloned());
|
|
}
|
|
});
|
|
assert_eq!(effects.len(), 1, "CONTROL: one own/fill/stroke mapping effect, not rendered output");
|
|
effects.pop().unwrap()
|
|
}
|
|
|
|
fn assert_native_mapping_roundtrip(source: &Document, path: &Path, incoming: &CompositeFont, classic: bool, mixed: bool, linked: bool) {
|
|
use vectorcraft_engine::cmd::clipboard::Clipboard;
|
|
|
|
let key = if classic { "mapArt" } else { "graphics" };
|
|
let original_effect = one_mapping_effect(&source.layers[0]);
|
|
let original = original_effect.params[key][0]["symbol"].as_str().unwrap();
|
|
let source_options = Clipboard::from_document(source).symbol_options.get(original).unwrap().clone();
|
|
let mut target = source.clone();
|
|
target.composite_fonts.iter_mut().find(|font| CompositeFont::same_name(&font.name, &incoming.name)).unwrap().parts.roman.size = 125.0;
|
|
let mut session = Session::new();
|
|
session.add_document(target, None);
|
|
let before = document(&session).clone();
|
|
let first = place(&mut session, path, linked);
|
|
let effect = one_mapping_effect(document(&session).node(first).unwrap());
|
|
let mapped = effect.params[key][0]["symbol"].as_str().unwrap();
|
|
assert_ne!(mapped, original, "the accepted mapping must follow incoming art, not the conflicting destination symbol");
|
|
let family = symbol_font_family(document(&session), mapped);
|
|
assert!(!CompositeFont::same_name(&family, &incoming.name));
|
|
let font = CompositeFont::named(&document(&session).composite_fonts, &family).unwrap();
|
|
assert_eq!((&font.parts, &font.custom), (&incoming.parts, &incoming.custom));
|
|
assert_eq!(Clipboard::from_document(document(&session)).symbol_options.get(mapped), Some(&source_options));
|
|
let mut expected = original_effect.clone();
|
|
expected.params[key][0]["symbol"] = json!(mapped);
|
|
assert_eq!(effect, expected, "rejected records, duplicate surfaces and inactive keys retain their original fields");
|
|
if mixed {
|
|
assert_eq!(only_symbol_name(document(&session).node(first).unwrap()), mapped, "ordinary and mapped users follow the same copy");
|
|
}
|
|
assert_original_native_art(document(&session), &before);
|
|
let resources = native_resource_counts(document(&session));
|
|
let repeated = place(&mut session, path, linked);
|
|
assert_eq!(one_mapping_effect(document(&session).node(repeated).unwrap()), effect);
|
|
assert_eq!(native_resource_counts(document(&session)), resources);
|
|
assert_original_native_art(document(&session), &before);
|
|
}
|
|
|
|
#[test]
|
|
fn native_mapped_effect_symbols_keep_incoming_font_and_options() {
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-mapped-fonts");
|
|
let incoming = CompositeFont::new("NativeMappedFont", "Source Sans 3", "Regular");
|
|
// Three distinct storage seams, rather than an effect/backend matrix.
|
|
for (location, classic, mixed) in [(0, false, false), (1, true, false), (2, true, true)] {
|
|
let path = dir.join(format!("mapped-{location}.vectorcraft"));
|
|
let (_builder, source) = native_mapping_source(&path, &incoming, location, classic, mixed);
|
|
let source = write_native(&path, &source);
|
|
for linked in [false, true] {
|
|
assert_native_mapping_roundtrip(&source, &path, &incoming, classic, mixed, linked);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn one_inline_art(root: &Node) -> vectorcraft_doc::InlineArt {
|
|
let mut found = Vec::new();
|
|
root.walk(&mut |node| {
|
|
if let NodeKind::Text(text) = &node.kind {
|
|
found.extend(text.runs.iter().filter_map(|run| run.inline.clone()));
|
|
}
|
|
});
|
|
assert_eq!(found.len(), 1, "CONTROL: one inline-only symbol reference");
|
|
found.pop().unwrap()
|
|
}
|
|
|
|
fn assert_inline_native_roundtrip(source: &Document, path: &Path, incoming: &CompositeFont, linked: bool) {
|
|
let original = one_inline_art(&source.layers[0]);
|
|
let source_size = source.unknown.get(vectorcraft_doc::SYMBOL_SIZES).and_then(|sizes| sizes.get(&original.symbol));
|
|
let mut target = source.clone();
|
|
target.composite_fonts.iter_mut().find(|font| CompositeFont::same_name(&font.name, &incoming.name)).unwrap().parts.roman.size = 125.0;
|
|
let mut session = Session::new();
|
|
session.add_document(target, None);
|
|
let before = document(&session).clone();
|
|
let id = place(&mut session, path, linked);
|
|
let imported = one_inline_art(document(&session).node(id).unwrap());
|
|
assert_ne!(imported.symbol, original.symbol, "inline art must not resolve to the destination's different font meaning");
|
|
assert_eq!((imported.scale, imported.baseline_shift), (original.scale, original.baseline_shift));
|
|
let family = symbol_font_family(document(&session), &imported.symbol);
|
|
assert!(!CompositeFont::same_name(&family, &incoming.name));
|
|
let font = CompositeFont::named(&document(&session).composite_fonts, &family).unwrap();
|
|
assert_eq!((&font.parts, &font.custom), (&incoming.parts, &incoming.custom));
|
|
let imported_doc = document(&session);
|
|
assert_eq!(imported_doc.unknown.get(vectorcraft_doc::SYMBOL_SIZES).and_then(|sizes| sizes.get(&imported.symbol)), source_size);
|
|
assert_eq!(imported_doc.symbol_registration(&imported.symbol), source.symbol_registration(&original.symbol));
|
|
assert_eq!(imported_doc.symbol_export_type(&imported.symbol), source.symbol_export_type(&original.symbol));
|
|
assert_eq!(imported_doc.symbol_nine_slice(&imported.symbol), source.symbol_nine_slice(&original.symbol));
|
|
assert_eq!(imported_doc.symbol_pixel_aligned(&imported.symbol), source.symbol_pixel_aligned(&original.symbol));
|
|
assert_original_native_art(document(&session), &before);
|
|
let resources = native_resource_counts(document(&session));
|
|
let second = place(&mut session, path, linked);
|
|
let repeated = one_inline_art(document(&session).node(second).unwrap());
|
|
assert_eq!(repeated.symbol, imported.symbol);
|
|
assert_eq!((repeated.scale, repeated.baseline_shift), (original.scale, original.baseline_shift));
|
|
assert_eq!(native_resource_counts(document(&session)), resources);
|
|
assert_original_native_art(document(&session), &before);
|
|
}
|
|
|
|
#[test]
|
|
fn native_inline_symbol_keeps_incoming_font_and_options() {
|
|
use vectorcraft_doc::{InlineArt, TextRun};
|
|
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-inline-font");
|
|
let path = dir.join("inline-lettering.vectorcraft");
|
|
let incoming = CompositeFont::new("NativeInlineFont", "Source Sans 3", "Regular");
|
|
let (mut builder, source) = nested_native_source(&path, &incoming, 0);
|
|
let outer = only_symbol_name(&source.layers[0]);
|
|
let outer_art = &source.symbols.iter().find(|definition| definition.name == outer).unwrap().art;
|
|
let inner = only_symbol_name(outer_art);
|
|
let ids = document(&builder).layers[0].children().unwrap().iter().map(|node| node.id.0).collect::<Vec<_>>();
|
|
builder.execute("edit.clear", &json!({"ids": ids})).unwrap();
|
|
let made = builder.execute("text.create", &json!({"x": 10, "y": 30, "text": "X", "font": "Source Sans 3"})).unwrap();
|
|
let mut source = document(&builder).clone();
|
|
let NodeKind::Text(text) = &mut source.node_mut(NodeId(made["id"].as_u64().unwrap())).unwrap().kind else {
|
|
panic!("CONTROL: public text.create produces the inline holder");
|
|
};
|
|
let style = text.runs[0].style.clone();
|
|
assert!(!CompositeFont::same_name(&style.font_family, &incoming.name));
|
|
let mut inline = InlineArt::new(inner);
|
|
inline.scale = 1.25;
|
|
inline.baseline_shift = 2.0;
|
|
text.runs = vec![TextRun::inline(inline, style)];
|
|
let source = write_native(&path, &source);
|
|
let mut ordinary = false;
|
|
source.layers[0].walk(&mut |node| ordinary |= matches!(node.kind, NodeKind::SymbolInstance { .. }));
|
|
assert!(!ordinary, "CONTROL: no ordinary instance seeds the inline symbol dependency");
|
|
for linked in [false, true] {
|
|
assert_inline_native_roundtrip(&source, &path, &incoming, linked);
|
|
}
|
|
}
|
|
|
|
fn one_pattern_override(root: &Node) -> vectorcraft_doc::SymbolOverride {
|
|
let mut found = Vec::new();
|
|
root.walk(&mut |node| found.extend(node.extras().symbol_overrides.iter().cloned()));
|
|
assert_eq!(found.len(), 1, "CONTROL: one stored dynamic override");
|
|
found.pop().unwrap()
|
|
}
|
|
|
|
fn override_pattern(override_: &vectorcraft_doc::SymbolOverride) -> String {
|
|
use vectorcraft_color::Paint;
|
|
|
|
let Some(Paint::Pattern { pattern: fill, .. }) = &override_.fill else { panic!("CONTROL: a pattern fill override") };
|
|
let Some(Paint::Pattern { pattern: stroke, .. }) = &override_.stroke else { panic!("CONTROL: a pattern stroke override") };
|
|
assert_eq!(fill, stroke);
|
|
fill.clone()
|
|
}
|
|
|
|
fn assert_override_native_roundtrip(source: &Document, path: &Path, incoming: &CompositeFont, linked: bool) {
|
|
use vectorcraft_color::Paint;
|
|
use vectorcraft_engine::cmd::clipboard::Clipboard;
|
|
|
|
let original = one_pattern_override(&source.layers[0]);
|
|
let original_pattern = override_pattern(&original);
|
|
let symbol = only_symbol_name(&source.layers[0]);
|
|
let options = Clipboard::from_document(source).symbol_options.get(&symbol).unwrap().clone();
|
|
let mut target = source.clone();
|
|
target.composite_fonts.iter_mut().find(|font| CompositeFont::same_name(&font.name, &incoming.name)).unwrap().parts.roman.size = 125.0;
|
|
let mut session = Session::new();
|
|
session.add_document(target, None);
|
|
let before = document(&session).clone();
|
|
let first = place(&mut session, path, linked);
|
|
let imported = one_pattern_override(document(&session).node(first).unwrap());
|
|
let pattern = override_pattern(&imported);
|
|
assert_ne!(pattern, original_pattern, "override-only pattern art follows the incoming composite meaning");
|
|
let family = pattern_font_family(document(&session), &pattern);
|
|
assert!(!CompositeFont::same_name(&family, &incoming.name));
|
|
let font = CompositeFont::named(&document(&session).composite_fonts, &family).unwrap();
|
|
assert_eq!((&font.parts, &font.custom), (&incoming.parts, &incoming.custom));
|
|
let mut expected = original.clone();
|
|
for paint in [&mut expected.fill, &mut expected.stroke].into_iter().flatten() {
|
|
let Paint::Pattern { pattern: name, .. } = paint else { panic!("CONTROL: both override paints are patterns") };
|
|
*name = pattern.clone();
|
|
}
|
|
assert_eq!(imported, expected, "override paths, transforms, opacity and blend retain their source values");
|
|
let used_symbol = only_symbol_name(document(&session).node(first).unwrap());
|
|
assert_eq!(used_symbol, symbol, "plain base symbol still shares its unchanged art and options");
|
|
assert_eq!(Clipboard::from_document(document(&session)).symbol_options.get(&used_symbol), Some(&options));
|
|
assert_original_native_art(document(&session), &before);
|
|
let resources = native_resource_counts(document(&session));
|
|
let repeated = place(&mut session, path, linked);
|
|
assert_eq!(one_pattern_override(document(&session).node(repeated).unwrap()), imported);
|
|
assert_eq!(native_resource_counts(document(&session)), resources);
|
|
assert_original_native_art(document(&session), &before);
|
|
}
|
|
|
|
#[test]
|
|
fn native_symbol_override_patterns_keep_incoming_font_meaning() {
|
|
use vectorcraft_color::{Color, Paint};
|
|
use vectorcraft_doc::{Appearance, SymbolOverride};
|
|
use vectorcraft_geom::Affine;
|
|
|
|
let _context = FontContext::hold();
|
|
let dir = TempDir::new("native-override-font");
|
|
let path = dir.join("override-lettering.vectorcraft");
|
|
let incoming = CompositeFont::new("NativeOverrideFont", "Source Sans 3", "Regular");
|
|
let (_builder, mut source) = nested_native_source(&path, &incoming, 1);
|
|
let outer = only_symbol_name(&source.layers[0]);
|
|
let outer_art = &source.symbols.iter().find(|definition| definition.name == outer).unwrap().art;
|
|
let inner = only_symbol_name(outer_art);
|
|
let pattern = pattern_in_symbol(&source, &inner);
|
|
let base = source.symbols.iter_mut().find(|definition| definition.name == inner).unwrap();
|
|
base.dynamic = true;
|
|
std::sync::Arc::make_mut(&mut base.art).appearance = Appearance::basic(Paint::solid(Color::BLACK), Paint::solid(Color::BLACK), 1.0).into();
|
|
std::sync::Arc::make_mut(&mut source.layers[0]).children_mut().unwrap().clear();
|
|
let mut instance =
|
|
Node::new(source.alloc_id(), NodeKind::SymbolInstance { symbol: inner.clone(), xf: Affine::translate((40.0, 40.0)) * Affine::scale(0.1) });
|
|
instance.appearance = Appearance::basic(Paint::None, Paint::None, 0.0).into();
|
|
instance.extras_mut().symbol_overrides.push(SymbolOverride {
|
|
path: vec![],
|
|
fill: Some(vectorcraft_doc::pattern::pattern_paint(&pattern)),
|
|
stroke: Some(Paint::Pattern { pattern, xf: Affine::scale(2.0) }),
|
|
opacity: Some(0.65),
|
|
blend: None,
|
|
});
|
|
let base = &source.symbols.iter().find(|definition| definition.name == inner).unwrap().art;
|
|
let effective = vectorcraft_doc::symbol_overrides::instance_art(base, &instance);
|
|
let override_ = &instance.extras().symbol_overrides[0];
|
|
assert_eq!(Some(effective.appearance.fill_paint()), override_.fill);
|
|
assert_eq!(Some(effective.appearance.stroke().unwrap().paint.clone()), override_.stroke);
|
|
std::sync::Arc::make_mut(&mut source.layers[0]).children_mut().unwrap().push(std::sync::Arc::new(instance));
|
|
source.fix_next_id();
|
|
let source = write_native(&path, &source);
|
|
for linked in [false, true] {
|
|
assert_override_native_roundtrip(&source, &path, &incoming, linked);
|
|
}
|
|
}
|