Use the rounded PDF graphics origin when starting text

This commit is contained in:
gloamxun committed 2026-11-29 10:15:19 +08:00
1 parent 893b46baae
commit ecb0f60960
5 files changed
+120 -12

No files matched your search

+37 -8
View File
@@ -7,7 +7,7 @@ use crate::{
PdfGraphics, PdfLiteralMode, divide_scaled, print_decimal, print_sp,
text_spaces::{ResolvedSpaceFont, SpaceOperation, SpacePlan, SpaceUse},
},
render::{DisplayItem, DisplayList},
render::{DisplayItem, DisplayList, DisplayUnits},
};
use std::collections::{BTreeMap, BTreeSet};
@@ -57,9 +57,12 @@ impl NativeText {
let vertical = composite.is_some_and(|font| font.vertical.is_some());
let first = text.is_none();
let state = text.get_or_insert_with(|| {
if xdv_origin.is_none() {
out.move_origin([0, 0], DisplayUnits::TeX, digits);
}
let [h, v] = out.origin.position();
out.push_str("BT\n");
if let Some(origin) = xdv_origin.filter(|_| !vertical && paint.xdv_matrix.is_identity()) {
let [h, v] = out.origin.position();
out.push_str(&format!(
"1 0 0 1 {} {} Tm\n",
print_sp(origin - h, digits),
@@ -73,8 +76,10 @@ impl NativeText {
xdv: xdv_origin.is_some(),
vertical_cid: false,
xdv_matrix: XdvFontMatrix::default(),
v: xdv_origin.unwrap_or(height),
start_h: xdv_origin.unwrap_or(0),
// The page-origin move can leave scaled-point rounding
// residue even when the emitted translations cancel.
v: xdv_origin.unwrap_or(height - v),
start_h: xdv_origin.unwrap_or(h),
delta_h: 0,
cursor_correction: 0.0,
opened: false,
@@ -138,8 +143,10 @@ impl NativeText {
state.start_h = x;
state.v = y;
} else if !state.xdv && (font.expansion != 0 || state.expansion != 0) {
let (h, h_out) = divide_scaled(x, 6_578_176, digits + 2);
let (v, v_out) = divide_scaled(height - y, 6_578_176, digits + 2);
let [origin_h, origin_v] = out.origin.position();
let (h, h_out) = divide_scaled(x - origin_h, HUNDRED_BP_IN_SP, digits + 2);
let (v, v_out) =
divide_scaled(height - y - origin_v, HUNDRED_BP_IN_SP, digits + 2);
if !out.measuring {
out.push_str(&format!(
"{} 0 0 1 {} {} Tm\n",
@@ -148,8 +155,8 @@ impl NativeText {
print_decimal(v, digits),
));
}
state.start_h = h_out;
state.v = height - v_out;
state.start_h = origin_h + h_out;
state.v = height - origin_v - v_out;
state.expansion = font.expansion;
} else {
let (h, h_out, v, v_out) = if state.xdv {
@@ -563,6 +570,28 @@ pub(crate) fn plan_spaces(
} => {
NativeText::break_string(&mut text, &mut out);
}
DisplayItem::PdfGraphics {
x,
y,
operation:
PdfGraphics::Literal {
mode: PdfLiteralMode::Origin,
..
}
| PdfGraphics::Save
| PdfGraphics::Restore { .. }
| PdfGraphics::Matrix(_)
| PdfGraphics::SaveMatrix(_),
}
| DisplayItem::PdfColor {
x,
y,
mode: PdfLiteralMode::Origin,
..
} => {
NativeText::finish(&mut text, &mut out);
out.move_origin([*x, page.height - *y], page.units, digits);
}
_ => NativeText::finish(&mut text, &mut out),
}
}