From 4586d961e97aa4ad0035edc3e719849a5c133da7 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Wed, 2 Dec 2026 04:41:41 +0800 Subject: [PATCH] Share exact repeated native glyph outlines in SVG previews --- crates/texrra-tex-core/src/render.rs | 101 +++++++++++++++++++++++++-- src/native_preview.rs | 2 +- 2 files changed, 97 insertions(+), 6 deletions(-) diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 52ff60d..22386ba 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -176,6 +176,49 @@ struct NativeSvgGlyph<'a> { scalable: bool, } +#[derive(Default)] +struct NativeSvgOutlines<'a> { + ids: std::collections::HashMap<&'a str, Option>, + definitions: Vec<&'a str>, +} + +impl<'a> NativeSvgOutlines<'a> { + fn observe(&mut self, path: &'a str) { + // Even two instances of a long outline pay for the definition and + // references. Keep short and one-off paths inline. + if path.len() < 128 { + return; + } + use std::collections::hash_map::Entry; + match self.ids.entry(path) { + Entry::Vacant(entry) => { + entry.insert(None); + } + Entry::Occupied(mut entry) if entry.get().is_none() => { + entry.insert(Some(self.definitions.len())); + self.definitions.push(path); + } + Entry::Occupied(_) => {} + } + } + + fn id(&self, path: &str) -> Option { + self.ids.get(path).copied().flatten() + } + + fn write_definitions(&self, out: &mut String) { + if self.definitions.is_empty() { + return; + } + use std::fmt::Write as _; + out.push_str("\n"); + for (id, path) in self.definitions.iter().enumerate() { + writeln!(out, "").unwrap(); + } + out.push_str("\n"); + } +} + impl DisplayItem { fn native_svg_glyph(&self) -> Option, String>> { let Self::NativeGlyph { @@ -573,12 +616,23 @@ impl DisplayList { .expect("SVG display list cannot be rendered") } pub fn try_to_svg(&self) -> Result { + self.render_svg(false) + } + + /// Render a page preview with shared definitions for repeated native outlines. + #[doc(hidden)] + pub fn try_to_preview_svg(&self) -> Result { + self.render_svg(true) + } + + fn render_svg(&self, share_native_outlines: bool) -> Result { if !(1..=32768).contains(&self.magnification) { return Err("invalid display-list magnification".into()); } if self.has_embedded_pdf() { return Err("this page requires a PDF renderer".into()); } + let mut outlines = share_native_outlines.then(NativeSvgOutlines::default); for item in &self.items { if let DisplayItem::PdfColor { program, .. } = item && program @@ -589,12 +643,16 @@ impl DisplayList { return Err("unresolved PDF graphics state in SVG page".into()); } if let Some(glyph) = item.native_svg_glyph() { - glyph?; + let glyph = glyph?; + if let Some(outlines) = &mut outlines { + outlines.observe(glyph.path); + } } } - self.svg_with_resolved_fonts() + self.svg_with_resolved_fonts(outlines.as_ref()) } - fn svg_with_resolved_fonts(&self) -> Result { + + fn svg_with_resolved_fonts(&self, outlines: Option<&NativeSvgOutlines<'_>>) -> Result { let width = self.width as f64 / 65_536.0; let height = self.height as f64 / 65_536.0; let mag = f64::from(self.magnification) / 1000.0 @@ -641,6 +699,9 @@ impl DisplayList { } out.push_str("\n"); } + if let Some(outlines) = outlines { + outlines.write_definitions(&mut out); + } let mut opentype_paths = std::collections::BTreeMap::new(); let mut opentype_faces = std::collections::BTreeMap::new(); let mut fill = [0u8, 0, 0]; @@ -697,9 +758,14 @@ impl DisplayList { use std::fmt::Write as _; write!( out, - "", + escape_attribute(font), escape_attribute(glyph), escape_attribute(&label), ).unwrap(); + if let Some(id) = outlines.and_then(|outlines| outlines.id(path)) { + write!(out, "\n\n\n\n" + )); + } + #[test] fn relocates_raw_shipout_origins_without_changing_paint_or_virtual_packets() { use crate::math_engine_tex::{MathItem, PositionedItem}; diff --git a/src/native_preview.rs b/src/native_preview.rs index 9898cb5..30d9be5 100644 --- a/src/native_preview.rs +++ b/src/native_preview.rs @@ -248,7 +248,7 @@ impl PreviewState { }); pdf.render_page(index, page.pdf_user_unit(), prior) } else { - page.try_to_svg().map(|svg| CachedPage { + page.try_to_preview_svg().map(|svg| CachedPage { svg: Arc::from(svg), pdf_page: None, same_as_previous: false,