Apply XeLaTeX CMaps to document text and embedded fonts

This commit is contained in:
gloamxun committed 2026-11-28 21:40:17 +08:00
1 parent af6466cd98
commit 7d460e0fa1
20 files changed
+747 -65

No files matched your search

@@ -134,6 +134,7 @@ pub(super) fn resolve(
xdv_descriptor: None,
xdv_widths: BTreeMap::new(),
xdv_matrix: Default::default(),
xdv_unicode: None,
resources,
program_index: Some(program_index),
})
@@ -141,6 +141,7 @@ pub(super) fn resolve(
xdv_descriptor: None,
xdv_widths: BTreeMap::new(),
xdv_matrix: Default::default(),
xdv_unicode: None,
resources,
program_index: None,
})
@@ -32,6 +32,8 @@ pub(super) struct MapEntry {
pub transform: Type1Transform,
#[serde(default, skip_serializing_if = "XdvFontMatrix::is_identity")]
xdv_matrix: XdvFontMatrix,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) xdv_to_unicode: Option<String>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)]
@@ -94,6 +96,7 @@ impl MapEntry {
transform: Type1Transform::default(),
subset: false,
xdv_matrix: XdvFontMatrix::default(),
xdv_to_unicode: None,
}
}
@@ -377,6 +380,11 @@ fn parse_xdv_map_line(
while let Some(option) = words.next() {
match option {
"-r" => {}, // Obsolete remapping option in the reference driver.
"-u" => {
let name = words.next().filter(|value| !value.starts_with('-'))
.ok_or_else(|| format!("missing XDV map ToUnicode name for {name}"))?;
entry.xdv_to_unicode = Some(name.into());
}
"-s" | "-e" => {
let number = words.next().and_then(|word| word.parse::<f64>().ok())
.filter(|value| value.is_finite())
@@ -402,6 +410,9 @@ fn parse_xdv_map_line(
pub struct PdfFontBindings {
fonts: BTreeMap<FontId, Option<Arc<MapEntry>>>,
pub(super) default_map: Option<ResourceVersion>,
// The PDF device looks up explicit ToUnicode overrides when the font closes.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub(super) xdv_unicode: BTreeMap<String, Arc<MapEntry>>,
}
impl PdfFontBindings {
@@ -842,6 +853,17 @@ impl JobFontMap {
Arc::make_mut(&mut self.tables.postscript).remove(&key);
}
let entry = Arc::new(entry);
if self.xdv_loaded.is_some()
&& let Some(bindings) = &mut self.bindings
&& (entry.xdv_to_unicode.is_some() || bindings.xdv_unicode.contains_key(&name))
{
let unicode = &mut Arc::make_mut(bindings).xdv_unicode;
if mode != MapMode::Delete && entry.xdv_to_unicode.is_some() {
unicode.insert(name.clone(), entry.clone());
} else {
unicode.remove(&name);
}
}
if mode == MapMode::Delete {
Arc::make_mut(&mut self.tables.entries).remove(&name);
} else {
@@ -950,5 +972,6 @@ pub(super) fn dedicated_space_map(font: &NativeFont) -> MapEntry {
transform: Type1Transform::default(),
subset: false,
xdv_matrix: XdvFontMatrix::default(),
xdv_to_unicode: None,
}
}
@@ -226,6 +226,7 @@ pub(super) fn resolve(
xdv_descriptor: None,
xdv_widths: BTreeMap::new(),
xdv_matrix: Default::default(),
xdv_unicode: None,
resources,
program_index: Some(program_index),
})