Render legacy XeLaTeX composite TrueType fonts

This commit is contained in:
gloamxun committed 2026-11-28 21:59:30 +08:00
1 parent 7d460e0fa1
commit bbeeca0317
11 files changed
+687 -55

No files matched your search

+61 -4
View File
@@ -21,6 +21,7 @@ pub(super) struct NativeText {
v: i64,
start_h: i64,
delta_h: i64,
cursor_correction: f64,
opened: bool,
}
impl NativeText {
@@ -38,7 +39,7 @@ impl NativeText {
digits: u8,
space: Option<SpaceUse>,
dedicated: Option<&ResolvedSpaceFont>,
) {
) -> Option<[f64; 2]> {
let first = text.is_none();
let state = text.get_or_insert_with(|| {
out.push_str("BT\n");
@@ -59,6 +60,7 @@ impl NativeText {
v: xdv_origin.unwrap_or(height),
start_h: xdv_origin.unwrap_or(0),
delta_h: 0,
cursor_correction: 0.0,
opened: false,
}
});
@@ -139,22 +141,49 @@ impl NativeText {
state.v -= v_out;
}
state.delta_h = 0;
state.cursor_correction = 0.0;
shift = 0;
}
if !state.opened {
out.push(b'[');
state.opened = true;
}
let cid_advance = out.track_legacy_cids.then(|| {
paint.legacy_cid_font().map(|program| f64::from(program.advance(slot)))
}).flatten();
let viewer_em = || {
// Tf uses 4 decimal places in bp. Convert the selected em back
// to display coordinates before applying Tz and Tm.
let (number, _) = divide_scaled(font.size_sp(), 6_578_176, 6);
let sp_per_bp = 65_536.0 * 803.0 / 800.0;
number as f64 * sp_per_bp / 10_000.0
* f64::from(1000 + font.expansion) / 1000.0
* paint.xdv_matrix.coefficients()[0]
};
if shift != 0 {
if cid_advance.is_some() {
state.cursor_correction += shift as f64 * viewer_em() / 1000.0 - shift_out as f64;
}
if !out.measuring {
out.push_str(&format!(" {} ", -shift));
}
state.delta_h += shift_out;
}
if !out.measuring {
out.push_str(&format!("<{slot:02X}>"));
if let Some(composite) = paint.legacy_cid_font() {
out.push_str(&format!("<{:04X}>", composite.cid(slot)));
} else {
out.push_str(&format!("<{slot:02X}>"));
}
}
state.delta_h += state.char_advance(font, paint, slot, state.effective_size);
let advance = state.char_advance(font, paint, slot, state.effective_size);
let position = cid_advance.map(|width| {
let x = (state.start_h + state.delta_h) as f64 + state.cursor_correction;
state.cursor_correction += width * viewer_em() / 1000.0 - advance as f64;
[x, state.v as f64]
});
state.delta_h += advance;
position
}
fn select_font(&mut self, out: &mut PageContent, font: &NativeFont, resource: FontId) {
@@ -260,7 +289,7 @@ impl NativeText {
digits: u8,
dedicated: &ResolvedSpaceFont,
) {
Self::glyph(
let _ = Self::glyph(
text,
out,
x,
@@ -301,6 +330,34 @@ impl NativeText {
}
}
/// Reuse the output device's selection, position and TJ logic when a CID
/// font's PDF advances differ from the TFM used for TeX layout.
pub(crate) fn legacy_cid_positions(page: &DisplayList) -> BTreeMap<usize, [f64; 2]> {
let mut positions = BTreeMap::new();
let Some(pdf) = &page.pdf else { return positions };
if pdf.driver != crate::pdf_device::PdfDriver::Xdv {
return positions;
}
let digits = pdf.file.decimal_digits;
let origin = crate::pdf_device::xdv_page_origin(page.units, page.height, page.magnification);
let mut output = PageContent { track_legacy_cids: true, ..PageContent::measuring() };
output.move_origin(origin, page.units, digits);
let mut run = None;
for (index, item) in page.items.iter().enumerate() {
if let DisplayItem::NativeGlyph { x, y, font, slot, paint: Some(paint), .. } = item {
if let Some(position) = NativeText::glyph(
&mut run, &mut output, *x, *y, font, font.id, paint, *slot,
page.height, Some(origin[0]), digits, None, None,
) {
positions.insert(index, position);
}
} else {
NativeText::finish(&mut run, &mut output);
}
}
positions
}
#[derive(Clone, Default)]
pub(crate) struct FontHistory {
used: BTreeSet<FontId>,