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

@@ -429,6 +429,7 @@ pub(crate) struct ResourceOutput {
link_references: BTreeMap<u32, Option<(usize, usize)>>,
structure_references: BTreeSet<u32>,
page_entries: Vec<Dictionary>,
font_opacities: Vec<BTreeMap<u8, String>>,
page_attributes: Vec<Dictionary>,
page_tree_attributes: Dictionary,
font_entries: BTreeMap<FontId, Dictionary>,
@@ -439,6 +440,7 @@ pub(crate) struct ResourceOutput {
pub(crate) struct RemappedResources {
pub pages: Vec<Vec<u8>>,
pub font_opacities: Vec<BTreeMap<u8, String>>,
pub page_attributes: Vec<Vec<u8>>,
pub page_tree_attributes: Vec<u8>,
pub fonts: BTreeMap<FontId, Vec<u8>>,
@@ -461,18 +463,31 @@ impl ResourceOutput {
) -> Result<Self, String> {
let empty = PdfObjects::default();
let objects = objects.unwrap_or(&empty);
let page_entries = pages
let (page_entries, font_opacities): (Vec<_>, Vec<_>) = pages
.iter()
.map(|page| {
let entries = parse_entries(&page.pdf_resources.entries)?;
let mut entries = parse_entries(&page.pdf_resources.entries)?;
if entries.has(b"Font") || entries.has(b"XObject") {
return Err(
"additional PDF resources overlap generated Font or XObject entries".into(),
);
}
Ok(entries)
let alphas = page
.items
.iter()
.filter_map(|item| match item {
crate::render::DisplayItem::OpenTypeColor(Some([_, _, _, alpha])) => {
Some(*alpha)
}
_ => None,
})
.collect();
let names = add_font_opacities(&mut entries, alphas, objects)?;
Ok((entries, names))
})
.collect::<Result<Vec<_>, String>>()?;
.collect::<Result<Vec<_>, String>>()?
.into_iter()
.unzip();
let font_entries = fonts
.into_iter()
.map(|(font, entries)| Ok((font, parse_entries(entries)?)))
@@ -563,6 +578,7 @@ impl ResourceOutput {
link_references,
structure_references,
page_entries,
font_opacities,
page_attributes,
page_tree_attributes,
font_entries,
@@ -628,17 +644,29 @@ impl ResourceOutput {
(number, id)
})
.collect();
let encode = |dictionary: Dictionary| {
let encode_dictionary = |dictionary: Dictionary, precise: bool| {
if dictionary.is_empty() {
return Vec::new();
}
let mut object = Object::Dictionary(dictionary);
remap(&mut object, &ids);
let bytes = encode_value(object).expect("valid PDF resource dictionary in memory");
let bytes = if precise {
super::xdv_object::encode_device_value(object)
} else {
encode_value(object)
}
.expect("valid PDF resource dictionary in memory");
bytes[2..bytes.len() - 2].to_vec()
};
let encode = |dictionary| encode_dictionary(dictionary, false);
RemappedResources {
pages: self.page_entries.into_iter().map(&encode).collect(),
pages: self
.page_entries
.into_iter()
.zip(&self.font_opacities)
.map(|(dictionary, opacities)| encode_dictionary(dictionary, !opacities.is_empty()))
.collect(),
font_opacities: self.font_opacities,
page_attributes: self.page_attributes.into_iter().map(&encode).collect(),
page_tree_attributes: encode(self.page_tree_attributes),
fonts: self.font_entries.into_iter()
@@ -656,6 +684,36 @@ impl ResourceOutput {
}
}
fn add_font_opacities(
entries: &mut Dictionary,
alphas: BTreeSet<u8>,
objects: &PdfObjects,
) -> Result<BTreeMap<u8, String>, String> {
let mut names = BTreeMap::new();
if alphas.is_empty() {
return Ok(names);
}
let mut states = match entries.get(b"ExtGState") {
Ok(value) => resolve_dictionary(value.clone(), objects)?,
Err(_) => Dictionary::new(),
};
for alpha in alphas {
let mut name = format!("TFontAlpha{alpha}");
while states.has(name.as_bytes()) {
name.push('_');
}
let opacity = crate::opentype::color_alpha(alpha);
let mut definition = Dictionary::new();
definition.set("Type", Object::Name(b"ExtGState".to_vec()));
definition.set("ca", Object::Real(opacity));
definition.set("CA", Object::Real(opacity));
states.set(name.as_bytes().to_vec(), Object::Dictionary(definition));
names.insert(alpha, name);
}
entries.set("ExtGState", Object::Dictionary(states));
Ok(names)
}
fn references(object: &Object, pending: &mut BTreeSet<u32>) -> Result<(), String> {
match object {
Object::Reference((number, 0)) if *number > 0 => {