From 4ff24a0d6d4cec5cb8affec063b631e18b0a9d2d Mon Sep 17 00:00:00 2001 From: Taha-Mahmoodi <85902429+Taha-Mahmoodi@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:29:49 +0430 Subject: [PATCH] PDF: honor embedded-font preferences in synchronous opens MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Compose the canonical font choice before Text As, retaining explicit overrides and avoiding an extra parameter clone. - Port the real eight-case synchronous/background font-binding regression to the current Craft runtime. - Build on the contributors’ PDF handling merged in #4569; preserve worker/profile/clipboard and glyph round-trip behavior. Fixes #9286 --- crates/ui-egui/src/dialogs/import_pdf.rs | 8 ++- .../ui-egui/src/dialogs/tests_import_pdf.rs | 2 +- crates/ui-egui/src/io/open.rs | 7 +- crates/ui-egui/src/opening/tests.rs | 69 +++++++++++++++++++ docs/development.md | 4 ++ 5 files changed, 83 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/dialogs/import_pdf.rs b/crates/ui-egui/src/dialogs/import_pdf.rs index e28bdbb021..17f1ea744f 100644 --- a/crates/ui-egui/src/dialogs/import_pdf.rs +++ b/crates/ui-egui/src/dialogs/import_pdf.rs @@ -618,14 +618,16 @@ pub(crate) fn preferred_text_as(app: &VectorcraftApp) -> TextAs { /// or `.ait` file opens as it says: Outlines would open it from its PDF content, without its /// editing data. So does an open with `linkPages`, which refuses Outlines: a linked page shows the /// file's own text. -pub(crate) fn with_preferred_text_as(app: &VectorcraftApp, name: &str, bytes: &[u8], p: &Value) -> Value { +pub(crate) fn with_preferred_text_as(app: &VectorcraftApp, name: &str, bytes: &[u8], mut p: Value) -> Value { let given = p.get("textAs").is_some_and(|v| !v.is_null()); let linked = p.get("linkPages").and_then(Value::as_bool) == Some(true); let options = p.is_object() || p.is_null(); if given || linked || !options || preferred_text_as(app) == TextAs::Text || fileio::detect(name, bytes).map(|f| f.id) != Some("pdf") { - return p.clone(); + return p; + } + if !p.is_object() { + p = json!({}); } - let mut p = if p.is_object() { p.clone() } else { json!({}) }; p["textAs"] = json!(TextAs::Outlines.id()); p } diff --git a/crates/ui-egui/src/dialogs/tests_import_pdf.rs b/crates/ui-egui/src/dialogs/tests_import_pdf.rs index 30a1aa9d65..49b5ec44b7 100644 --- a/crates/ui-egui/src/dialogs/tests_import_pdf.rs +++ b/crates/ui-egui/src/dialogs/tests_import_pdf.rs @@ -430,7 +430,7 @@ fn a_pdf_opened_without_the_dialog_takes_the_pdf_text_preference() { io::open_document(&mut app, "one.pdf", &one, None, &json!({"textAs": "text"})).unwrap(); opened(&mut app); assert_eq!(type_objects(&app), 1, "an open that gives textAs keeps it"); - let p = import_pdf::with_preferred_text_as(&app, "one.ai", &one, &serde_json::Value::Null); + let p = import_pdf::with_preferred_text_as(&app, "one.ai", &one, serde_json::Value::Null); assert!(p.is_null(), "a .ai file opens as it says: {p}"); // In the dialog too, a .ai file starts from Editable, and its choice is not kept for PDFs. assert!(import_pdf::offer(&mut app, "two.ai", &two_pages(false, None), None, None)); diff --git a/crates/ui-egui/src/io/open.rs b/crates/ui-egui/src/io/open.rs index 639f33ac21..5ab17f9a69 100644 --- a/crates/ui-egui/src/io/open.rs +++ b/crates/ui-egui/src/io/open.rs @@ -143,9 +143,10 @@ pub fn opens_as(name: &str) -> Option { /// Open a document through the engine loader with the `document.open` options in `p` → /// `document.open`'s result, or `{dialog}` when Edit › Color Settings asks about its profile first /// ([`dialogs::profile_mismatch::open_or_ask`]): the document then opens, and goes on the recent -/// files, once the question is answered. A PDF opens with the `pdfTextAs` preference unless `p` -/// gives `textAs` ([`dialogs::import_pdf::with_preferred_text_as`]). +/// files, once the question is answered. PDF font and Text As preferences supply missing options; +/// explicit `embeddedFonts` and `textAs` choices keep their precedence. pub fn open_document(app: &mut VectorcraftApp, name: &str, bytes: &[u8], path: Option, p: &Value) -> Result { + let p = app.session.prefs.with_pdf_font_choice(p); let p = &dialogs::import_pdf::with_preferred_text_as(app, name, bytes, p); let update_links = app.session.prefs.update_links == "automatically"; let low_res_proxy_eps = app.session.prefs.low_res_proxy_eps; @@ -609,7 +610,7 @@ fn open_file_with(app: &mut VectorcraftApp, path: &str, background: bool, p: &Va note_recent(app, path); return Ok(Value::Null); } - let params = crate::dialogs::import_pdf::with_preferred_text_as(app, path, &bytes, &Value::Null); + let params = crate::dialogs::import_pdf::with_preferred_text_as(app, path, &bytes, Value::Null); return Ok(crate::opening::start_with(app, path, bytes, params)); } let r = open_bytes_with(app, path, &bytes, Some(path.to_string()), p)?; diff --git a/crates/ui-egui/src/opening/tests.rs b/crates/ui-egui/src/opening/tests.rs index 2466381a3d..551cac14e8 100644 --- a/crates/ui-egui/src/opening/tests.rs +++ b/crates/ui-egui/src/opening/tests.rs @@ -4,6 +4,75 @@ use super::*; use crate::Services; use crate::tests::picks::{cancel_dialog, close_shown, draw_dialogs, open_dialogs, show_question}; +/// #9286: both real open consumers apply the same preference/explicit-option precedence and bind +/// the carried program to rendered text, rather than merely storing the requested option. +#[test] +fn synchronous_and_background_pdf_open_honor_embedded_font_preferences_and_overrides() { + use std::fmt::Write as _; + use vectorcraft_doc::{FILE_FONT_MIME, NodeKind}; + use vectorcraft_testkit::pdf::{PdfPage, first_extra, pdf_with}; + use vectorcraft_text::{FileFontInfo, FileGlyph, build_file_font}; + + let mut glyph = FileGlyph { ch: 'x', outline: Default::default(), advance: 600.0 }; + glyph.outline.move_to((0.0, 0.0)); + glyph.outline.line_to((500.0, 0.0)); + glyph.outline.line_to((500.0, 700.0)); + glyph.outline.line_to((0.0, 700.0)); + glyph.outline.close_path(); + let info = FileFontInfo { family: "VectorCraftUIOpenPreference".into(), style: "Regular".into(), ..Default::default() }; + let font_bytes = build_file_font(&info, &[glyph]).unwrap(); + let mut hex = String::with_capacity(font_bytes.len() * 2); + for b in font_bytes { + write!(hex, "{b:02x}").unwrap(); + } + let n = first_extra(1); + let page = PdfPage { resources: format!("/Font << /F1 {n} 0 R >>"), ..PdfPage::new(100.0, 100.0, "BT /F1 24 Tf 10 40 Td (x) Tj ET") }; + let font = format!( + "<< /Type /Font /Subtype /TrueType /BaseFont /ABCDEF+VectorCraftUIOpenPreference /FirstChar 120 /LastChar 120 /Widths [600] /Encoding /WinAnsiEncoding /FontDescriptor {} 0 R >>", + n + 1 + ); + let descriptor = format!( + "<< /Type /FontDescriptor /FontName /ABCDEF+VectorCraftUIOpenPreference /Flags 34 /FontBBox [0 -200 500 900] /ItalicAngle 0 /Ascent 900 /Descent -200 /CapHeight 700 /StemV 80 /FontFile2 {} 0 R >>", + n + 2 + ); + let program = format!("<< /Length {} /Filter /ASCIIHexDecode >>\nstream\n{hex}>\nendstream", hex.len() + 1); + let bytes = pdf_with(&[page], &[&font, &descriptor, &program], None); + for background in [false, true] { + for (preference, explicit, expected) in [(false, None, false), (true, None, true), (false, Some(true), true), (true, Some(false), false)] { + let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Services::default())); + app.session.prefs.pdf_import_embedded_fonts = preference; + let mut params = json!({"textAs": "text"}); + if let Some(value) = explicit { + params["embeddedFonts"] = json!(value); + } + if background { + start_with(&mut app, "embedded-preference.pdf", bytes.clone(), params); + open_ready(&mut app); + } else { + crate::io::open_document(&mut app, "embedded-preference.pdf", &bytes, None, ¶ms).unwrap(); + } + let doc = &app.session.doc().unwrap().doc; + let db = vectorcraft_text::FontDb::global(); + let mut text_count = 0; + let mut carries_font = false; + doc.walk(|node| { + if let NodeKind::Text(text) = &node.kind { + assert_eq!(text.plain_text(), "x", "imported text content"); + text_count += 1; + carries_font |= vectorcraft_text::layout(db, text).glyphs.iter().any(|glyph| db.is_file_font(glyph.font_id)); + } + }); + assert_eq!(text_count, 1, "text survives: background={background}, preference={preference}, explicit={explicit:?}"); + assert_eq!(carries_font, expected, "rendered font: background={background}, preference={preference}, explicit={explicit:?}"); + assert_eq!( + doc.images.values().any(|blob| blob.mime == FILE_FONT_MIME), + expected, + "owned font program: background={background}, preference={preference}, explicit={explicit:?}" + ); + } + } +} + #[test] fn long_operation_busy_while_a_file_opens_or_a_background_save_runs() { let mut app = VectorcraftApp::new(Session::new(), Services::default()); diff --git a/docs/development.md b/docs/development.md index 404dc10f33..8ec8db5fab 100644 --- a/docs/development.md +++ b/docs/development.md @@ -352,6 +352,10 @@ The app decides which cursors exist; whoever draws them works from the list. For doesn't run. `cargo xtask ci` runs the same two as its `test` and `doctests` steps. - The test build leaves out the examples (`--lib --bins --tests`); clippy (`--all-targets`) still checks them. +- PDF open consumers compose the embedded-font preference before Text As without cloning the + composed options again; explicit options win in both synchronous and background opens. + `opening::tests::synchronous_and_background_pdf_open_honor_embedded_font_preferences_and_overrides` + exercises both paths with a generated embedded font and checks rendered font ownership (#9286). - Each crate has one integration-test binary: its test files are modules of `tests/it/main.rs`. Run one file's tests with `cargo nextest run -p vectorcraft-svg --test it roundtrip::` (or `cargo test` with the same arguments). A new test file gets a `mod` line in `main.rs`.