Shape and render explicit OpenType fonts in native Unicode jobs

This commit is contained in:
gloamxun committed 2026-11-21 10:35:03 +08:00
1 parent 3caf1eced2
commit 70822cbd94
33 files changed
+1255 -80

No files matched your search

+38 -4
View File
@@ -51,6 +51,15 @@ pub enum DisplayItem {
font: std::sync::Arc<crate::native_font::NativeFont>,
paint: Option<std::sync::Arc<crate::native_font::NativeFontPaint>>,
},
OpenTypeGlyph {
x: i64,
y: i64,
size: i64,
advance: i64,
font: std::sync::Arc<crate::opentype::OpenTypeFont>,
glyph: u16,
text: String,
},
/// A native math glyph. `face` preserves the optical master independently
/// of the painted size; `slot` preserves CMEX next-larger selection.
MathGlyph {
@@ -393,7 +402,8 @@ impl DisplayList {
match item {
DisplayItem::Text { x, y, .. }
| DisplayItem::MathGlyph { x, y, .. }
| DisplayItem::NativeGlyph { x, y, .. } => {
| DisplayItem::NativeGlyph { x, y, .. }
| DisplayItem::OpenTypeGlyph { x, y, .. } => {
*x += dx;
*y += dy;
}
@@ -448,9 +458,9 @@ impl DisplayList {
glyph?;
}
}
Ok(self.svg_with_resolved_fonts())
self.svg_with_resolved_fonts()
}
fn svg_with_resolved_fonts(&self) -> String {
fn svg_with_resolved_fonts(&self) -> Result<String, String> {
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
@@ -491,6 +501,8 @@ impl DisplayList {
}
out.push_str("</defs>\n");
}
let mut opentype_paths = std::collections::BTreeMap::new();
let mut opentype_faces = std::collections::BTreeMap::new();
let mut fill = [0u8, 0, 0];
let mut rule_fill = [0u8; 3];
let mut stroke = [0u8; 3];
@@ -530,6 +542,23 @@ impl DisplayList {
};
out.push_str(&format!("<g data-font=\"{}\" data-glyph=\"{}\" data-slot=\"{slot}\" aria-label=\"{}\"><path d=\"{}\" transform=\"translate({x:.5} {y:.5}) scale({horizontal_scale:.8} -{scale:.8})\" fill=\"#{:02x}{:02x}{:02x}\"{fill_opacity}/></g>\n", escape(font), escape(glyph), escape(&label), path, fill[0],fill[1],fill[2]));
}
DisplayItem::OpenTypeGlyph { x, y, size, font, glyph, text, .. } => {
let face = match opentype_faces.entry(font.id) {
std::collections::btree_map::Entry::Vacant(entry) => entry.insert(font.face()?),
std::collections::btree_map::Entry::Occupied(entry) => entry.into_mut(),
};
let path = opentype_paths.entry((font.id, *glyph))
.or_insert_with(|| crate::opentype::outline(face, *glyph));
let x = *x as f64 / 65536.;
let y = *y as f64 / 65536.;
let scale = *size as f64 / (65536. * f64::from(face.units_per_em()));
if !path.is_empty() {
out.push_str(&format!(
"<g data-font=\\\"{}\\\" data-glyph=\\\"{glyph}\\\" aria-label=\\\"{}\\\"><path d=\\\"{path}\\\" transform=\\\"translate({x:.5} {y:.5}) scale({scale:.9} -{scale:.9})\\\" fill=\\\"#{:02x}{:02x}{:02x}\\\"{fill_opacity}/></g>\n",
escape(&font.name), escape(text), fill[0], fill[1], fill[2]
));
}
}
DisplayItem::MathGlyph {
x,
y,
@@ -795,7 +824,7 @@ impl DisplayList {
out.push_str("</g>\n");
}
out.push_str("</svg>\n");
out
Ok(out)
}
}
@@ -1560,7 +1589,12 @@ impl Page {
for position in &geometry.items {
let x = x + scaled(position.x);
let y = baseline + scaled(position.y);
if let MathItem::OpenType(run) = &position.item {
run.paint(x, y, scaled, |item, _| list.push(item));
continue;
}
list.push(match &position.item {
MathItem::OpenType(_) => unreachable!("OpenType run handled before scalar paint"),
MathItem::NativeGlyph {
font,
slot,