Support scoped XeTeX OpenType colors in PDF and SVG

This commit is contained in:
gloamxun committed 2026-11-26 11:27:13 +08:00
1 parent 5a4271364a
commit a0922d0105
10 files changed
+183 -16

No files matched your search

+21 -4
View File
@@ -62,6 +62,8 @@ pub enum DisplayItem {
},
/// Word-level PDF replacement text. None closes the current word.
OpenTypeActualText(Option<String>),
/// Word-local font color and alpha. None restores the surrounding paint.
OpenTypeColor(Option<[u8; 4]>),
/// A native math glyph. `face` preserves the optical master independently
/// of the painted size; `slot` preserves CMEX next-larger selection.
MathGlyph {
@@ -435,7 +437,7 @@ impl DisplayList {
}
DisplayItem::Fill(_) | DisplayItem::Background(_) | DisplayItem::PdfColor(_) => {}
DisplayItem::Special(_) | DisplayItem::PdfImageObject(_)
| DisplayItem::OpenTypeActualText(_) => {}
| DisplayItem::OpenTypeActualText(_) | DisplayItem::OpenTypeColor(_) => {}
}
}
}
@@ -518,6 +520,7 @@ impl DisplayList {
let mut opentype_faces = std::collections::BTreeMap::new();
let mut fill = [0u8, 0, 0];
let mut rule_fill = [0u8; 3];
let mut opentype_color: Option<[u8; 4]> = None;
let mut stroke = [0u8; 3];
let mut transparency = SvgTransparency::default();
let mut graphics = Vec::new();
@@ -587,17 +590,30 @@ impl DisplayList {
write!(out, " matrix({horizontal:.9} 0 {shear:.9} -{scale:.9} 0 0)").unwrap();
}
}
let (fill, stroke, alpha) = match opentype_color {
Some([r, g, b, a]) => ([r, g, b], [r, g, b], Some(crate::opentype::color_alpha(a))),
None => (fill, stroke, None),
};
write!(
out, "\" fill=\"#{:02x}{:02x}{:02x}\"{fill_opacity}",
fill[0], fill[1], fill[2],
out, "\" fill=\"#{:02x}{:02x}{:02x}\"", fill[0], fill[1], fill[2],
).unwrap();
match alpha {
Some(alpha) if alpha < 1. => write!(out, " fill-opacity=\"{alpha}\"").unwrap(),
None => out.push_str(fill_opacity),
_ => {}
}
if embolden > 0. {
let stroke_width = embolden / self.units.bp(65536);
write!(
out,
" stroke=\"#{:02x}{:02x}{:02x}\" stroke-width=\"{stroke_width:.9}\" stroke-miterlimit=\"10\"{stroke_opacity}",
" stroke=\"#{:02x}{:02x}{:02x}\" stroke-width=\"{stroke_width:.9}\" stroke-miterlimit=\"10\"",
stroke[0], stroke[1], stroke[2],
).unwrap();
match alpha {
Some(alpha) if alpha < 1. => write!(out, " stroke-opacity=\"{alpha}\"").unwrap(),
None => out.push_str(stroke_opacity),
_ => {}
}
}
out.push_str("/></g>\n");
}
@@ -751,6 +767,7 @@ impl DisplayList {
}
DisplayItem::Link { .. } | DisplayItem::PdfTextSpace { .. }
| DisplayItem::OpenTypeActualText(_) => {}
DisplayItem::OpenTypeColor(color) => opentype_color = *color,
DisplayItem::Special(content) => {
out.push_str(&format!(
"<!-- special: {} -->