diff --git a/crates/svg/src/glyph.rs b/crates/svg/src/glyph.rs index 22697f58a3..076083f156 100644 --- a/crates/svg/src/glyph.rs +++ b/crates/svg/src/glyph.rs @@ -2,6 +2,7 @@ //! it, made as the importer makes art of any SVG file. The text crate asks for it through the //! reader the app registers ([`vectorcraft_text::set_svg_glyph_reader`]). +use std::collections::{HashMap, HashSet}; use std::ops::Range; use std::sync::Arc; use std::sync::atomic::AtomicU64; @@ -20,6 +21,8 @@ const MAX_SVG_GLYPH_BYTES: u64 = 1 << 20; /// embedded content and scripts (and images of SVG documents, in [`glyph`]). const LEFT_OUT: [&str; 3] = ["text", "foreignObject", "script"]; +const XLINK: &str = "http://www.w3.org/1999/xlink"; + /// Glyph `gid`'s art from its SVG `document` (possibly gzipped), in a font of `upem` units per em: /// its element `glyph` (or the whole document when that is its root), in font units (y down), /// the root's `viewBox` (or else its size) fitted into the em square by its larger side, centred. @@ -28,6 +31,11 @@ const LEFT_OUT: [&str; 3] = ["text", "foreignObject", "script"]; /// ([`own_art`]). With the art, whether it uses the text's colours. `None` when the document doesn't read, is too large or has /// no such element. pub fn glyph_art(document: &[u8], gid: u32, upem: f64, fill: Color, stroke: Option) -> Option<(Node, bool)> { + glyph_art_with(document, gid, upem, fill, stroke, true) +} + +/// [`glyph_art`], the importer reading only what the glyph refers to when `reduce` ([`glyph`]). +fn glyph_art_with(document: &[u8], gid: u32, upem: f64, fill: Color, stroke: Option, reduce: bool) -> Option<(Node, bool)> { // The SVGZ budget left is the glyph's cap. let unpacked = AtomicU64::new(crate::MAX_SVGZ.saturating_sub(MAX_SVG_GLYPH_BYTES)); let text = crate::text_within(document, &unpacked).ok()?; @@ -39,7 +47,7 @@ pub fn glyph_art(document: &[u8], gid: u32, upem: f64, fill: Color, stroke: Opti let [r, g, b, _] = c.to_rgba8(1.0); format!("#{r:02x}{g:02x}{b:02x}") }; - let Glyph { svg, whole, frame } = glyph(&text, &id, &hex(fill))?; + let Glyph { svg, whole, frame } = glyph(&text, &id, &hex(fill), reduce)?; let coloured = ["currentColor", "currentcolor", "context-fill", "context-stroke"].iter().any(|k| svg.contains(k)); // usvg resolves `context-fill` and `context-stroke` only for markers. let svg = svg.replace("context-fill", &hex(fill)).replace("context-stroke", &stroke.map_or_else(|| "none".into(), hex)); @@ -86,9 +94,10 @@ struct Glyph { /// `svg` with only what glyph `id` draws, as if the root `use`d it: the rest of the document (a /// font may keep all its glyphs in one) only defines what it refers to, and what its parents apply /// doesn't reach it. Without the [`LEFT_OUT`] elements and event attributes, the root without a -/// size (the em square is its frame) and in the text's colour `color`. `None` when it doesn't read -/// or has no element `id`. -fn glyph(svg: &str, id: &str, color: &str) -> Option { +/// size (the em square is its frame) and in the text's color `color`. With `reduce`, the `defs` +/// hold only what the glyph refers to ([`referred`]), when that can be followed; else the whole +/// rest of the document. `None` when it doesn't read or has no element `id`. +fn glyph(svg: &str, id: &str, color: &str, reduce: bool) -> Option { // Before the XML reader: it nests by recursion and expands entities without a total limit. if crate::import::xml_too_deep(svg) || crate::entities::expansion_too_large(svg) { return None; @@ -105,8 +114,7 @@ fn glyph(svg: &str, id: &str, color: &str) -> Option { // data URL not typed as a raster goes too: the importer reads an untyped one (`text/plain`) as // SVG, SVGZ as well, when its bytes aren't a raster. let svg_image = |n: &roxmltree::Node| { - let href = n.attribute("href").or_else(|| n.attribute(("http://www.w3.org/1999/xlink", "href"))); - let data = href.map(|h| h.trim_start()).filter(|h| h.get(..5).is_some_and(|v| v.eq_ignore_ascii_case("data:"))); + let data = link(*n).map(|h| h.value()).map(|h| h.trim_start()).filter(|h| h.get(..5).is_some_and(|v| v.eq_ignore_ascii_case("data:"))); let raster = |h: &str| { ["data:image/png", "data:image/jpeg", "data:image/jpg", "data:image/gif", "data:image/webp"] .iter() @@ -114,7 +122,8 @@ fn glyph(svg: &str, id: &str, color: &str) -> Option { }; n.tag_name().name() == "image" && data.is_some_and(|h| !raster(h)) }; - for n in root.descendants().filter(|n| n.is_element() && (LEFT_OUT.contains(&n.tag_name().name()) || svg_image(n))) { + let cut = |n: &roxmltree::Node| n.is_element() && (LEFT_OUT.contains(&n.tag_name().name()) || svg_image(n)); + for n in root.descendants().filter(cut) { edits.push((n.range(), String::new())); } for a in root.attributes().filter(|a| matches!(a.name(), "width" | "height" | "color")) { @@ -128,10 +137,18 @@ fn glyph(svg: &str, id: &str, color: &str) -> Option { let start = root.range().start + 1; let name = start + svg.get(start..)?.find(|c: char| c.is_whitespace() || c == '>' || c == '/')?; edits.push((name..name, format!(" color=\"{color}\""))); - // The namespaces the glyph's parents declare, which it leaves: the root declares them too. + // The namespaces the glyph's parents declare, which it leaves: the root declares them too, or the + // glyph itself, where the root gives the prefix (or the default) another one. + let own = target.range().start + 1; + let own = own + svg.get(own..)?.find(|c: char| c.is_whitespace() || c == '>' || c == '/')?; for ns in target.namespaces() { - if let Some(prefix) = ns.name().filter(|p| root.lookup_namespace_uri(Some(p)).is_none()) { - edits.push((name..name, format!(" xmlns:{prefix}=\"{}\"", crate::xml_escape(ns.uri())))); + let uri = crate::xml_escape(ns.uri()); + let at_root = root.lookup_namespace_uri(ns.name()); + match ns.name() { + Some(prefix) if at_root.is_none() => edits.push((name..name, format!(" xmlns:{prefix}=\"{uri}\""))), + _ if whole || at_root == Some(ns.uri()) || declares(svg, target, ns.name())? => {} + Some(prefix) => edits.push((own..own, format!(" xmlns:{prefix}=\"{uri}\""))), + None => edits.push((own..own, format!(" xmlns=\"{uri}\""))), } } edits.sort_by_key(|(r, _)| (r.start, r.end)); @@ -141,11 +158,14 @@ fn glyph(svg: &str, id: &str, color: &str) -> Option { // The root's content goes in `defs`, and the glyph after it, on its own. let content = root.first_child()?.range().start; let close = svg.get(..root.range().end)?.rfind(" pieces.into_iter().map(|r| edited(svg, r, &edits)).collect::>()?, + None => [edited(svg, content..at.start, &edits)?, edited(svg, at.end..close, &edits)?].concat(), + }; [ edited(svg, 0..content, &edits)?, "".into(), - edited(svg, content..at.start, &edits)?, - edited(svg, at.end..close, &edits)?, + defs, "".into(), edited(svg, at.clone(), &edits)?, edited(svg, close..svg.len(), &edits)?, @@ -164,12 +184,265 @@ fn glyph(svg: &str, id: &str, color: &str) -> Option { Some(Glyph { svg: out, whole, frame: view_box.or_else(size) }) } +/// Whether the start tag of `element` in `svg` declares namespace `prefix` (`None`: the default +/// one). `None` when the tag doesn't read. +fn declares(svg: &str, element: roxmltree::Node, prefix: Option<&str>) -> Option { + let want = prefix.map_or_else(|| "xmlns".to_string(), |p| format!("xmlns:{p}")); + let tag = svg.get(element.range().start + 1..)?; + let mut rest = tag.trim_start_matches(|c: char| !(c.is_whitespace() || c == '>' || c == '/')); + loop { + rest = rest.trim_start(); + if rest.is_empty() || rest.starts_with(['>', '/']) { + return Some(false); + } + let eq = rest.find('=')?; + let value = rest.get(eq + 1..)?.trim_start(); + let quote = value.chars().next().filter(|q| matches!(q, '"' | '\''))?; + let close = 1 + value.get(1..)?.find(quote)?; + if rest.get(..eq)?.trim_end() == want { + return Some(true); + } + rest = value.get(close + 1..)?; + } +} + +/// The byte ranges of `svg` (whose XML is `xml`) that the `defs` of glyph element `target` need, in +/// order: the elements it refers to with `url(#id)` (in an attribute, a `style` or a sheet) or +/// `href="#id"`, those they refer to in turn, and every ` + + {other}"## + ), + // Clip paths, masks, filters, markers and patterns, a cycle, the glyph using itself, a + // non-ASCII id. + format!( + r##" + + + + + + + {other}"## + ), + // A rule on the glyph's own parent, which it leaves either way; a quoted, spaced URL to + // an id with punctuation in it. + format!( + r##" + + {other}"## + ), + // A pattern whose art takes its paint from the group holding it. + format!( + r##" + + {other}"## + ), + ]; + for doc in &docs { + let (reduced, whole) = (rewrite(doc, 1, true), rewrite(doc, 1, false)); + assert!(reduced.len() < whole.len() && !reduced.contains("#123456"), "reduced: {reduced}"); + assert_eq!(art(doc, 1, true), art(doc, 1, false), "{reduced}"); + } + } + + /// Whatever the reduction can't follow keeps the whole document in the `defs`. + #[test] + fn what_cant_be_followed_keeps_the_whole_document() { + let glyph = |defs: &str, art: &str| { + format!( + r##"{defs}{art}"## + ) + }; + let gradient = r##""##; + let path = |fill: &str| format!(r##""##); + let docs = [ + glyph("", &path("#000")), + glyph("", &path("#000")), + glyph("", &path("#000")), + glyph("", &path("#000")), + glyph(r"", &path("#000")), + glyph("", &path("#000")), + glyph(gradient, &path(r"url(#\67)")), + glyph(gradient, r#""#), + glyph(gradient, &path("url(#%67)")), + glyph(gradient, &path("url('#g)")), + glyph(gradient, &path("url(#g) url(#g x)")), + glyph(gradient, &path("url(#nothing)")), + glyph(gradient, &path("url(other.svg#g)")), + glyph(gradient, r##""##), + glyph(&format!("{gradient}{gradient}"), &path("url(#g)")), + glyph(&format!(r#"{gradient}"#), &path("url(#g)")), + glyph(&format!("{gradient}"), &path("url(#g)")), + r##""## + .to_string(), + r##"]>"## + .to_string(), + ]; + for doc in &docs { + assert_eq!(rewrite(doc, 1, true), rewrite(doc, 1, false), "{doc}"); + } + } + + #[test] + fn edits_apply_inside_the_range_asked_for() { + let edits = vec![(0..1, "A".to_string()), (2..2, "B".into()), (3..5, String::new()), (4..6, "C".into())]; + // An edit overlapping one already made is passed by; one not inside the range is not made. + assert_eq!(edited("0123456", 0..7, &edits).as_deref(), Some("A1B256")); + assert_eq!(edited("0123456", 2..5, &edits).as_deref(), Some("B2")); + assert_eq!(edited("0123456", 4..7, &edits).as_deref(), Some("C6")); + assert_eq!(edited("0123456", 1..2, &edits).as_deref(), Some("1B")); + } + + /// Of an element's `href` and `xlink:href`, only the plain one is followed, as the importer + /// follows it: the other's element stays out, and one that isn't there doesn't keep the whole + /// document. + #[test] + fn only_the_link_the_importer_follows_is_kept() { + let doc = |xlink: &str| { + format!( + r##" + "## + ) + }; + for xlink in ["#nothing", "other.svg#b"] { + let doc = doc(xlink); + let reduced = rewrite(&doc, 1, true); + assert!(reduced.len() < rewrite(&doc, 1, false).len() && !reduced.contains("#123456"), "{reduced}"); + assert_eq!(art(&doc, 1, true), art(&doc, 1, false)); + } + let doc = doc("#b").replace(r##""##, ""); + let reduced = rewrite(&doc, 1, true); + assert!(reduced.contains(r#"id="a""#) && !reduced.contains(r#"id="b""#), "{reduced}"); + assert_eq!(art(&doc, 1, true), art(&doc, 1, false)); + } + + /// A glyph inside an element that gives a prefix (or the default namespace) another namespace + /// than the root does keeps its own after it leaves that element. + #[test] + fn a_glyph_keeps_its_namespaces_when_the_root_gives_them_others() { + let flat = r##" + "##; + let nested = r##" + "##; + // The glyph declaring the prefix itself keeps it as it is. + let declared = r##" + "##; + for reduce in [true, false] { + assert_eq!(art(nested, 1, reduce), art(flat, 1, reduce), "{}", rewrite(nested, 1, reduce)); + assert_eq!(art(declared, 1, reduce), art(flat, 1, reduce), "{}", rewrite(declared, 1, reduce)); + } + assert_eq!(rewrite(declared, 1, true).matches("xmlns:v=").count(), 2); + // A path in another default namespace is no SVG path, out of that element too. + let other = r##" + "##; + let only = r##""##; + for reduce in [true, false] { + assert_eq!(art(other, 1, reduce), art(only, 1, reduce), "{}", rewrite(other, 1, reduce)); + } + } + + /// Many references to the same elements keep each of them once. + #[test] + fn repeated_references_keep_each_element_once() { + let uses: String = (0..2000).map(|i| format!(r##""##)).collect(); + let doc = format!( + r##" + + {uses}"## + ); + let reduced = rewrite(&doc, 1, true); + for id in ["g", "a", "b"] { + assert_eq!(reduced.matches(&format!(r#"id="{id}""#)).count(), 1, "{id}"); + } + assert!(!reduced.contains("#123456")); + } + + /// An empty document, or one without the glyph, has no art; one of exactly the cap reads, one a + /// byte larger doesn't. + #[test] + fn empty_and_capped_documents() { + assert!(glyph_art(b"", 1, 1000.0, Color::BLACK, None).is_none()); + assert!(glyph_art(br#""#, 1, 1000.0, Color::BLACK, None).is_none()); + let doc = shared(20); + let at = doc.len() - "".len(); + let padded = |len: u64| { + let pad = "x".repeat(len as usize - doc.len() - "".len()); + format!("{}{}", &doc[..at], &doc[at..]) + }; + let (full, over) = (padded(MAX_SVG_GLYPH_BYTES), padded(MAX_SVG_GLYPH_BYTES + 1)); + assert_eq!(full.len() as u64, MAX_SVG_GLYPH_BYTES); + assert_eq!(art(&full, 3, true), art(&doc, 3, true)); + assert!(glyph_art(over.as_bytes(), 3, 1000.0, Color::BLACK, None).is_none()); + } + + /// This thread's CPU time in seconds where the system reports it, else the time since a first + /// call. + fn cpu_seconds() -> f64 { + static START: std::sync::OnceLock = std::sync::OnceLock::new(); + let schedstat = std::fs::read_to_string("/proc/thread-self/schedstat").ok(); + match schedstat.as_deref().and_then(|s| s.split_whitespace().next()?.parse::().ok()) { + Some(ns) => ns / 1e9, + None => START.get_or_init(std::time::Instant::now).elapsed().as_secs_f64(), + } + } + + /// The importer's time for 50 distinct glyphs of shared documents, read whole and reduced, the + /// two alternating in one process: `cargo test -p vectorcraft-svg --lib glyph::tests::timing -- + /// --ignored --nocapture`. + #[test] + #[ignore = "timing, run by hand"] + fn timing() { + for n in [100, 500, 2000] { + let doc = shared(n); + let gids: Vec = (0..50).map(|i| (i * n / 50) as u32).collect(); + let run = |reduce: bool| { + let t = cpu_seconds(); + for g in &gids { + assert!(glyph_art_with(doc.as_bytes(), *g, 1000.0, Color::BLACK, None, reduce).is_some()); + } + cpu_seconds() - t + }; + run(false); + run(true); + let (mut whole, mut reduced) = (vec![], vec![]); + for round in 0..9 { + if round % 2 == 0 { + whole.push(run(false)); + reduced.push(run(true)); + } else { + reduced.push(run(true)); + whole.push(run(false)); + } + } + let median = |v: &mut Vec| { + v.sort_by(f64::total_cmp); + v[v.len() / 2] + }; + let ms = |v: &[f64]| v.iter().map(|s| format!("{:.1}", s * 1e3)).collect::>().join(" "); + println!("{n} glyphs, {} bytes: whole {} ms, reduced {} ms", doc.len(), ms(&whole), ms(&reduced)); + println!("{n} glyphs: median whole {:.1} ms, reduced {:.1} ms", median(&mut whole) * 1e3, median(&mut reduced) * 1e3); + } + } +} diff --git a/crates/svg/tests/it/svg_glyphs.rs b/crates/svg/tests/it/svg_glyphs.rs index 20e5342412..3e4ba8b697 100644 --- a/crates/svg/tests/it/svg_glyphs.rs +++ b/crates/svg/tests/it/svg_glyphs.rs @@ -241,13 +241,10 @@ fn an_images_plain_href_decides_whether_it_is_an_svg_image() { let svg = vectorcraft_svg::base64_encode( br##""##, ); - let image = |href: &str, xlink: &str| { - format!( - r##""##, - if href == "png" { &png } else { &svg }, - if xlink == "png" { &png } else { &svg } - ) - }; + let data = |kind: &str| format!("data:image/{kind};base64,{}", if kind == "png" { &png } else { &svg }); + let image = |href: &str, xlink: &str| format!(r##""##, data(href), data(xlink)); + // The same, `xlink:href` written first. + let xlink_first = |href: &str, xlink: &str| format!(r##""##, data(xlink), data(href)); let images = |inner: String| { let doc = format!( r##"{inner}"## @@ -258,6 +255,38 @@ fn an_images_plain_href_decides_whether_it_is_an_svg_image() { }; assert_eq!(images(image("png", "svg+xml")), 1, "the PNG stays"); assert_eq!(images(image("svg+xml", "png")), 0, "the document goes"); + assert_eq!(images(xlink_first("png", "svg+xml")), 1, "the PNG stays, its link written last"); + assert_eq!(images(xlink_first("svg+xml", "png")), 0, "the document goes, its link written last"); +} + +/// A glyph draws what it refers to wherever its document defines it: here a sheet's `url()` to a +/// gradient inside another glyph's element, which takes its stops from one in `defs`. The other +/// glyph's own art is not drawn. +#[test] +fn a_glyph_draws_what_it_refers_to_from_anywhere_in_its_document() { + let doc = r##" + + + + "##; + let fills = fills(&art(doc, 5).unwrap()); + let stops = fills.iter().find_map(|p| match p { + Paint::Gradient(g) => Some(g.gradient.stops.iter().map(|s| s.color).collect::>()), + _ => None, + }); + assert_eq!(stops, Some(vec![Color::rgb8(255, 0, 0), Color::rgb8(0, 0, 255)]), "{fills:?}"); + assert!(!fills.contains(&Paint::solid(Color::rgb8(0, 255, 0))), "not the other glyph's art: {fills:?}"); +} + +/// A glyph in colors of its own doesn't use the text's because another glyph of its document does. +#[test] +fn another_glyphs_current_color_does_not_color_a_glyph() { + let doc = r##" + + + "##; + assert!(!glyph_art(doc.as_bytes(), 1, 1000.0, BLUE, None).unwrap().1); + assert!(glyph_art(doc.as_bytes(), 2, 1000.0, BLUE, None).unwrap().1); } /// A glyph's `` draws its art but keeps no link: outlined text must not export a diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 0782bacab9..3f46017fe5 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -718,6 +718,7 @@ since 2026-10-01. - **Colour emoji with bitmap glyphs:** `sbix` (Apple Color Emoji) and `CBDT` (Noto Color Emoji) glyphs draw as their largest strike's PNG, placed with the type and unpainted by its fill; PDF writes them in Type 3 glyphs or as images, SVG as images with outlined text, EPS and legacy `.ai` as images, and Create Outlines makes embedded images. Synthetic test fonts in `crates/text/src/test_fonts.rs`; tests `crates/text/src/tests/bitmaps.rs`, `crates/render/src/tests/bitmapglyphs.rs`, `crates/svg/tests/it/colour_glyphs.rs`, `pdf::tests::colour_bitmap_glyphs_export_as_images`, the `eps` `colour_bitmap_glyphs_*` tests and `tests_pathops::create_outlines_turns_bitmap_glyphs_into_images`. COLR and OpenType SVG fonts remain. - **COLR colour fonts:** v0 and v1 glyphs draw as vector layers (solid colours, linear and radial gradients, including ones that repeat or reflect, clips, blend modes, the foreground colour from the run's fill) on the canvas, as paths in SVG, EPS, legacy `.ai` and PDF with outlined text, as Type 3 text in PDF, and as editable groups with Create Outlines; sweep gradients, Porter-Duff compositing and radial gradients that repeat or reflect and start at a circle draw the glyph as an image. Measured by `tests_colr`, `render::tests_colrglyphs`, `pdf::tests::colr_glyphs_export_as_vector_art`, `svg/tests/it/colour_glyphs.rs`, `eps` `colr_glyphs_*`, `tests_pathops::create_outlines_turns_colr_glyphs_into_layer_groups`. Scores unchanged (estimated). - **OpenType SVG colour fonts:** each glyph's SVG document is read by the SVG importer (registered by the engine) into vector art in the run's colours, then drawn, exported and outlined as COLR glyphs are; PDF real text writes them as text that draws nothing and draws their art where they sit. Measured by `tests_svg_glyphs`, `svg/tests/it/svg_glyphs.rs`, `tests_colourfonts::create_outlines_turns_svg_glyphs_into_groups`, `tests_colourfonts::pdf_real_text_draws_svg_glyphs_as_art_and_keeps_their_characters`. Scores unchanged (estimated). +- **OpenType SVG glyphs from a shared document:** when one SVG document holds many glyphs, the importer reads only the glyph's element and what it refers to (by `href` or `url(#id)`, through other elements, the elements holding them and every `