Preserve the XDV text line when switching OpenType fonts

This commit is contained in:
gloamxun committed 2026-11-28 06:36:03 +08:00
1 parent b22e127e93
commit aae23d837e
3 files changed
+150 -18

No files matched your search

+63 -18
View File
@@ -60,6 +60,26 @@ struct TextRun {
}
impl TextRun {
fn select_font(
&mut self,
output: &mut PageContent,
font: FontId,
size: i64,
bp_per_unit: f64,
embolden: f64,
) {
let size_bp = size as f64 * bp_per_unit;
output.push_str(&format!("/U{} {size_bp:.4} Tf ", font.index()));
if embolden > 0. {
output.push_str(&format!("2 Tr {embolden:.6} w "));
} else if self.emboldened {
output.push_str("0 Tr ");
}
self.font = font;
self.size = size;
self.emboldened = embolden > 0.;
}
fn place_line(
&mut self,
output: &mut PageContent,
@@ -86,6 +106,23 @@ fn rounded_coordinate(value: i64, bp_per_unit: f64) -> (f64, i64) {
(rounded, ((exact - rounded) / bp_per_unit).round() as i64)
}
fn place_absolute(
output: &mut PageContent,
[a, b, c, d]: [f64; 4],
position: [i64; 2],
origin: [i64; 2],
bp_per_unit: f64,
) -> [i64; 2] {
let [tx, ty] = std::array::from_fn(|axis| {
let (delta, error) = rounded_coordinate(position[axis] - origin[axis], bp_per_unit);
(rounded_coordinate(origin[axis], bp_per_unit).0 + delta, error)
});
output.push_str(&format!(
"{a} {b} {c} {d} {:.3} {:.3} Tm [<", tx.0, ty.0,
));
[tx.1, ty.1]
}
#[derive(Default)]
pub(super) struct UsedFonts<'a>(BTreeMap<FontId, (&'a OpenTypeFont, BTreeMap<u16, &'a str>)>);
@@ -575,8 +612,25 @@ impl EmbeddedFont {
if let Some(run) = state.run.as_mut() {
let delh = (x - run.start[0] - run.offset).abs();
let delv = (y - run.start[1]).abs();
if override_text || run.font != font.id || run.size != size {
if override_text {
state.finish(output);
} else if run.font != font.id || run.size != size {
if xdv {
// Selecting a font ends the string but retains the text
// line matrix. Recompute Tm only when its shape changes.
output.push_str(">] TJ ");
run.select_font(output, font.id, size, bp_per_unit, embolden);
if relative && state.matrix == (device, vertical) {
run.place_line(output, [x, y], device, vertical, bp_per_unit);
} else {
place_absolute(output, [a, b, c, d], [x, y], page_origin, bp_per_unit);
run.start = [x, y];
run.offset = 0;
}
repositioned = true;
} else {
state.finish(output);
}
} else if relative {
if delv > (0.001 / bp_per_unit).round() as i64 || delh > (3. * horizontal_em) as i64 {
// Td moves in text space. Precompensate for the shear and
@@ -599,18 +653,15 @@ impl EmbeddedFont {
let start_relative = first && (xdv || emboldened || font_colors) && relative && state.matrix == (device, vertical);
state.matrix = (device, vertical);
let run = state.run.get_or_insert_with(|| {
let size_bp = size as f64 * bp_per_unit;
output.push_str(&format!("BT /U{} {size_bp:.4} Tf ", font.id.index()));
if emboldened {
output.push_str(&format!("2 Tr {embolden:.6} w "));
}
output.push_str("BT ");
let mut run = TextRun {
font: font.id,
size,
start: [x, y],
offset: 0,
emboldened,
emboldened: false,
};
run.select_font(output, font.id, size, bp_per_unit, embolden);
if start_relative {
// At a new text object the XDV device reuses the font
// matrix at the page origin, then positions the first line.
@@ -622,25 +673,19 @@ impl EmbeddedFont {
run.place_line(output, [x, y], device, vertical, bp_per_unit);
} else {
let origin = if xdv { page_origin } else { [0, 0] };
let [tx, ty] = std::array::from_fn(|axis| {
let value = [x, y][axis];
let (delta, error) = rounded_coordinate(value - origin[axis], bp_per_unit);
(rounded_coordinate(origin[axis], bp_per_unit).0 + delta, error)
});
output.push_str(&format!(
"{a} {b} {c} {d} {:.3} {:.3} Tm [<", tx.0, ty.0,
));
let [dx, dy] =
place_absolute(output, [a, b, c, d], [x, y], origin, bp_per_unit);
if xdv || !relative {
run.start = [x - tx.1, y - ty.1];
run.start = [x - dx, y - dy];
}
if !xdv && !relative {
run.offset = tx.1;
run.offset = dx;
}
}
run
});
// Move in PDF's thousandth-em text units, tracking widths in TeX sp.
let kernel = if horizontal_em == 0. || (relative && (first || repositioned)) {
let kernel = if horizontal_em == 0. || repositioned || (relative && first) {
0
} else {
((run.start[0] + run.offset - x) as f64 * 1000. / horizontal_em) as i64