Share exact repeated native glyph outlines in SVG previews
This commit is contained in:
1 parent
9f556d6a5f
commit
4586d961e9
2 files changed
+97
-6
No files matched your search
@@ -176,6 +176,49 @@ struct NativeSvgGlyph<'a> {
|
|||||||
scalable: bool,
|
scalable: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct NativeSvgOutlines<'a> {
|
||||||
|
ids: std::collections::HashMap<&'a str, Option<usize>>,
|
||||||
|
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<usize> {
|
||||||
|
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("<defs>\n");
|
||||||
|
for (id, path) in self.definitions.iter().enumerate() {
|
||||||
|
writeln!(out, "<path id=\"native-outline-{id}\" d=\"{path}\"/>").unwrap();
|
||||||
|
}
|
||||||
|
out.push_str("</defs>\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl DisplayItem {
|
impl DisplayItem {
|
||||||
fn native_svg_glyph(&self) -> Option<Result<NativeSvgGlyph<'_>, String>> {
|
fn native_svg_glyph(&self) -> Option<Result<NativeSvgGlyph<'_>, String>> {
|
||||||
let Self::NativeGlyph {
|
let Self::NativeGlyph {
|
||||||
@@ -573,12 +616,23 @@ impl DisplayList {
|
|||||||
.expect("SVG display list cannot be rendered")
|
.expect("SVG display list cannot be rendered")
|
||||||
}
|
}
|
||||||
pub fn try_to_svg(&self) -> Result<String, String> {
|
pub fn try_to_svg(&self) -> Result<String, String> {
|
||||||
|
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<String, String> {
|
||||||
|
self.render_svg(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_svg(&self, share_native_outlines: bool) -> Result<String, String> {
|
||||||
if !(1..=32768).contains(&self.magnification) {
|
if !(1..=32768).contains(&self.magnification) {
|
||||||
return Err("invalid display-list magnification".into());
|
return Err("invalid display-list magnification".into());
|
||||||
}
|
}
|
||||||
if self.has_embedded_pdf() {
|
if self.has_embedded_pdf() {
|
||||||
return Err("this page requires a PDF renderer".into());
|
return Err("this page requires a PDF renderer".into());
|
||||||
}
|
}
|
||||||
|
let mut outlines = share_native_outlines.then(NativeSvgOutlines::default);
|
||||||
for item in &self.items {
|
for item in &self.items {
|
||||||
if let DisplayItem::PdfColor { program, .. } = item
|
if let DisplayItem::PdfColor { program, .. } = item
|
||||||
&& program
|
&& program
|
||||||
@@ -589,12 +643,16 @@ impl DisplayList {
|
|||||||
return Err("unresolved PDF graphics state in SVG page".into());
|
return Err("unresolved PDF graphics state in SVG page".into());
|
||||||
}
|
}
|
||||||
if let Some(glyph) = item.native_svg_glyph() {
|
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<String, String> {
|
|
||||||
|
fn svg_with_resolved_fonts(&self, outlines: Option<&NativeSvgOutlines<'_>>) -> Result<String, String> {
|
||||||
let width = self.width as f64 / 65_536.0;
|
let width = self.width as f64 / 65_536.0;
|
||||||
let height = self.height as f64 / 65_536.0;
|
let height = self.height as f64 / 65_536.0;
|
||||||
let mag = f64::from(self.magnification) / 1000.0
|
let mag = f64::from(self.magnification) / 1000.0
|
||||||
@@ -641,6 +699,9 @@ impl DisplayList {
|
|||||||
}
|
}
|
||||||
out.push_str("</defs>\n");
|
out.push_str("</defs>\n");
|
||||||
}
|
}
|
||||||
|
if let Some(outlines) = outlines {
|
||||||
|
outlines.write_definitions(&mut out);
|
||||||
|
}
|
||||||
let mut opentype_paths = std::collections::BTreeMap::new();
|
let mut opentype_paths = std::collections::BTreeMap::new();
|
||||||
let mut opentype_faces = std::collections::BTreeMap::new();
|
let mut opentype_faces = std::collections::BTreeMap::new();
|
||||||
let mut fill = [0u8, 0, 0];
|
let mut fill = [0u8, 0, 0];
|
||||||
@@ -697,9 +758,14 @@ impl DisplayList {
|
|||||||
use std::fmt::Write as _;
|
use std::fmt::Write as _;
|
||||||
write!(
|
write!(
|
||||||
out,
|
out,
|
||||||
"<g data-font=\"{}\" data-glyph=\"{}\" data-slot=\"{slot}\" aria-label=\"{}\"><path d=\"{}\" transform=\"",
|
"<g data-font=\"{}\" data-glyph=\"{}\" data-slot=\"{slot}\" aria-label=\"{}\">",
|
||||||
escape_attribute(font), escape_attribute(glyph), escape_attribute(&label), path,
|
escape_attribute(font), escape_attribute(glyph), escape_attribute(&label),
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
if let Some(id) = outlines.and_then(|outlines| outlines.id(path)) {
|
||||||
|
write!(out, "<use href=\"#native-outline-{id}\" transform=\"").unwrap();
|
||||||
|
} else {
|
||||||
|
write!(out, "<path d=\"{path}\" transform=\"").unwrap();
|
||||||
|
}
|
||||||
if let Some([vx2, vy]) = vertical_origin {
|
if let Some([vx2, vy]) = vertical_origin {
|
||||||
let vx = f64::from(vx2) / 2.0;
|
let vx = f64::from(vx2) / 2.0;
|
||||||
let vy = f64::from(vy);
|
let vy = f64::from(vy);
|
||||||
@@ -2580,6 +2646,31 @@ impl Page {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn preview_outline_definitions_share_only_long_repeated_path_text() {
|
||||||
|
let first = "M0 0L1 1Z".repeat(16);
|
||||||
|
let second = "M0 0L2 2Z".repeat(16);
|
||||||
|
let single = "M0 0L3 3Z".repeat(16);
|
||||||
|
let same_second = second.clone();
|
||||||
|
let short = "M0 0L4 4Z";
|
||||||
|
let mut outlines = NativeSvgOutlines::default();
|
||||||
|
for path in [&first, &single, &second, &same_second, &first, &first] {
|
||||||
|
outlines.observe(path);
|
||||||
|
}
|
||||||
|
outlines.observe(short);
|
||||||
|
outlines.observe(short);
|
||||||
|
|
||||||
|
assert_eq!(outlines.id(&first), Some(1));
|
||||||
|
assert_eq!(outlines.id(&second), Some(0));
|
||||||
|
assert_eq!(outlines.id(&single), None);
|
||||||
|
assert_eq!(outlines.id(short), None);
|
||||||
|
let mut definitions = String::new();
|
||||||
|
outlines.write_definitions(&mut definitions);
|
||||||
|
assert_eq!(definitions, format!(
|
||||||
|
"<defs>\n<path id=\"native-outline-0\" d=\"{second}\"/>\n<path id=\"native-outline-1\" d=\"{first}\"/>\n</defs>\n"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn relocates_raw_shipout_origins_without_changing_paint_or_virtual_packets() {
|
fn relocates_raw_shipout_origins_without_changing_paint_or_virtual_packets() {
|
||||||
use crate::math_engine_tex::{MathItem, PositionedItem};
|
use crate::math_engine_tex::{MathItem, PositionedItem};
|
||||||
|
|||||||
@@ -248,7 +248,7 @@ impl PreviewState {
|
|||||||
});
|
});
|
||||||
pdf.render_page(index, page.pdf_user_unit(), prior)
|
pdf.render_page(index, page.pdf_user_unit(), prior)
|
||||||
} else {
|
} else {
|
||||||
page.try_to_svg().map(|svg| CachedPage {
|
page.try_to_preview_svg().map(|svg| CachedPage {
|
||||||
svg: Arc::from(svg),
|
svg: Arc::from(svg),
|
||||||
pdf_page: None,
|
pdf_page: None,
|
||||||
same_as_previous: false,
|
same_as_previous: false,
|
||||||
|
|||||||
Reference in new issue
Block a user