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

+1 -4
View File
@@ -812,9 +812,6 @@ fn page_content(
if let Some(dedicated) = list.pdf_space_plan.as_ref().and_then(|plan| plan.dedicated.as_ref()) if let Some(dedicated) = list.pdf_space_plan.as_ref().and_then(|plan| plan.dedicated.as_ref())
&& *operation == crate::pdf_device::text_spaces::SpaceOperation::Explicit && *operation == crate::pdf_device::text_spaces::SpaceOperation::Explicit
{ {
if tex_text.is_none() {
content.move_origin(page_origin, list.units, digits);
}
native_text::NativeText::explicit_space( native_text::NativeText::explicit_space(
&mut tex_text, &mut content, *x, *y, height, digits, dedicated, &mut tex_text, &mut content, *x, *y, height, digits, dedicated,
); );
@@ -898,7 +895,7 @@ fn page_content(
} = item } = item
{ {
assert!(paint.is_some(), "native glyph program must be resolved"); assert!(paint.is_some(), "native glyph program must be resolved");
if tex_text.is_none() { if xdv && tex_text.is_none() {
content.move_origin(page_origin, list.units, digits); content.move_origin(page_origin, list.units, digits);
} }
let _ = native_text::NativeText::glyph( let _ = native_text::NativeText::glyph(
+37 -8
View File
@@ -7,7 +7,7 @@ use crate::{
PdfGraphics, PdfLiteralMode, divide_scaled, print_decimal, print_sp, PdfGraphics, PdfLiteralMode, divide_scaled, print_decimal, print_sp,
text_spaces::{ResolvedSpaceFont, SpaceOperation, SpacePlan, SpaceUse}, text_spaces::{ResolvedSpaceFont, SpaceOperation, SpacePlan, SpaceUse},
}, },
render::{DisplayItem, DisplayList}, render::{DisplayItem, DisplayList, DisplayUnits},
}; };
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{BTreeMap, BTreeSet};
@@ -57,9 +57,12 @@ impl NativeText {
let vertical = composite.is_some_and(|font| font.vertical.is_some()); let vertical = composite.is_some_and(|font| font.vertical.is_some());
let first = text.is_none(); let first = text.is_none();
let state = text.get_or_insert_with(|| { 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"); out.push_str("BT\n");
if let Some(origin) = xdv_origin.filter(|_| !vertical && paint.xdv_matrix.is_identity()) { if let Some(origin) = xdv_origin.filter(|_| !vertical && paint.xdv_matrix.is_identity()) {
let [h, v] = out.origin.position();
out.push_str(&format!( out.push_str(&format!(
"1 0 0 1 {} {} Tm\n", "1 0 0 1 {} {} Tm\n",
print_sp(origin - h, digits), print_sp(origin - h, digits),
@@ -73,8 +76,10 @@ impl NativeText {
xdv: xdv_origin.is_some(), xdv: xdv_origin.is_some(),
vertical_cid: false, vertical_cid: false,
xdv_matrix: XdvFontMatrix::default(), xdv_matrix: XdvFontMatrix::default(),
v: xdv_origin.unwrap_or(height), // The page-origin move can leave scaled-point rounding
start_h: xdv_origin.unwrap_or(0), // residue even when the emitted translations cancel.
v: xdv_origin.unwrap_or(height - v),
start_h: xdv_origin.unwrap_or(h),
delta_h: 0, delta_h: 0,
cursor_correction: 0.0, cursor_correction: 0.0,
opened: false, opened: false,
@@ -138,8 +143,10 @@ impl NativeText {
state.start_h = x; state.start_h = x;
state.v = y; state.v = y;
} else if !state.xdv && (font.expansion != 0 || state.expansion != 0) { } else if !state.xdv && (font.expansion != 0 || state.expansion != 0) {
let (h, h_out) = divide_scaled(x, 6_578_176, digits + 2); let [origin_h, origin_v] = out.origin.position();
let (v, v_out) = divide_scaled(height - y, 6_578_176, digits + 2); 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 { if !out.measuring {
out.push_str(&format!( out.push_str(&format!(
"{} 0 0 1 {} {} Tm\n", "{} 0 0 1 {} {} Tm\n",
@@ -148,8 +155,8 @@ impl NativeText {
print_decimal(v, digits), print_decimal(v, digits),
)); ));
} }
state.start_h = h_out; state.start_h = origin_h + h_out;
state.v = height - v_out; state.v = height - origin_v - v_out;
state.expansion = font.expansion; state.expansion = font.expansion;
} else { } else {
let (h, h_out, v, v_out) = if state.xdv { 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); 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), _ => NativeText::finish(&mut text, &mut out),
} }
} }
+27
View File
@@ -18,6 +18,33 @@
| W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;原样 12 页 NeurIPS 的中部编辑与恢复已验证前后缀复用、仅更新一页,且与独立全量一致。全部副作用/交叉编辑、旧来源与正式性能门禁仍待闭合 | | W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;原样 12 页 NeurIPS 的中部编辑与恢复已验证前后缀复用、仅更新一页,且与独立全量一致。全部副作用/交叉编辑、旧来源与正式性能门禁仍待闭合 |
| W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 | | W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 |
## 2026-11-29 PDF 文字游标沿用舍入后的原点
图形指令回到页原点时,打印的平移可以抵消,内部仍会留下 sp 级余量。
原生 PDF 文字以舍入后的设备原点开启文字段,普通文字及伸缩字体的
矩阵复位都使用同一位置。空格探测沿用相同的图形原点,字形导出与
空格预判以同一文字游标计算。真实参考用例同时核对横向、纵向和
伸缩字体,三组字形坐标全部一致。
[pdfTeX 1.40.29 的原点计算](https://github.com/TeX-Live/texlive-source/blob/5aa90aa0e1c8e743b85ed8a94ea77531edadefd5/texk/web2c/pdftexdir/pdftex.web#L16012-L16028)
OUP 原版诊断记录的首个偏差字形落点为 (16476456, 32080643) sp,
和原生原始盒一致。独立诊断版的首轮 PDF 字形与其未加诊断的参考
逐项相同。修正后从九份原样交付、同版格式和空辅助文件开始,原生
首轮 32,262 个字形已逐项对齐。三轮 aux 字节均与参考一致。
收敛后的 10 页、32,469 个字形及坐标、6,109 个词框和 144 DPI
灰度图全部通过。此前第 1、7 页共 761 个坐标差异均消失。新旧
原生原始布局清单逐项相同,26 项定向回归通过。
证据在 target/p3-oup-text-positions/。保留从独立目录直接
复制辅助文件时的所有权检查失败,以及重新从空辅助文件产生的
三轮结果。SHA256SUMS 指纹为
1d6d130274a7e33b033fe768869f8500158a75ce541da0dd58b288ce7521775e。
随后独立用例确认图形原点阈值还有一个差异。三位小数时,第一次
只在横向移动 33 sp,参考保持原点,当前原生先发出 0.001 bp
平移。第二次的两轴位移最终相同。下一入口是对照同版本驱动
的最小移动阈值及双轴判定,分别验证 pdfTeX 与 XDV 的路径。
[原样 OUP 出版记录](PUBLISHER_TEMPLATE_MATRIX.md#2026-11-29-oup-原样双栏阶段对照)
## 2026-11-29 OUP 双栏分页与字形差异 ## 2026-11-29 OUP 双栏分页与字形差异
原样 OUP Contemporary Large 覆盖跨栏表格、脚注、双栏浮动及实际 原样 OUP Contemporary Large 覆盖跨栏表格、脚注、双栏浮动及实际
+7
View File
@@ -1388,6 +1388,13 @@ target/p3-oup-journals/。SHA256SUMS 指纹为
2db0efddfeeab2cad82a65db569d77463937b7566b6e0c08de39caf2ccb156da。 2db0efddfeeab2cad82a65db569d77463937b7566b6e0c08de39caf2ccb156da。
[OUP 官方包](https://ctan.org/pkg/oup-authoring-template) [OUP 官方包](https://ctan.org/pkg/oup-authoring-template)
随后修正图形原点切换后的 PDF 文字定位。用相同原样交付和空辅助
文件再次执行三轮,逐轮 aux 与冻结参考逐字节一致。第一轮 32,262
个字形、收敛后 32,469 个字形及坐标均逐项一致。最终 10 页的
6,109 个词及词框和 144 DPI 灰度图也完全一致。保留上轮第 1、7
页的 761 个位置差异,以及同源三轮实际作业和结果指纹。
[文字游标与差异追踪](P2_M1_PROGRESS.md#2026-11-29-pdf-文字游标沿用舍入后的原点)
单纯生成 PDF 或 DVI 只说明编译路径可达。完整验收要在参考环境闭合后比较页数、 单纯生成 PDF 或 DVI 只说明编译路径可达。完整验收要在参考环境闭合后比较页数、
双栏位置及跨栏材料、实际字体/字形、图表浮动、引用收敛、词框及墨迹;测试源 双栏位置及跨栏材料、实际字体/字形、图表浮动、引用收敛、词框及墨迹;测试源
与容差都不得为通过而替换。类 Typst 的编辑延迟另用同批原样项目分规模测量, 与容差都不得为通过而替换。类 Typst 的编辑延迟另用同批原样项目分规模测量,
+48
View File
@@ -240,6 +240,54 @@ fn saved_pdf_position_is_observed_in_shipout_order() {
fs::remove_dir_all(work).unwrap(); fs::remove_dir_all(work).unwrap();
} }
#[test]
fn native_pdf_text_uses_the_rounded_origin_after_positioned_literals() {
let work = directory();
let source = work.join("origin.tex");
fs::write(
&source,
r"\catcode123=1 \catcode125=2 \catcode35=6 \catcode37=14
\pdfoutput=1 \pdfdecimaldigits=3 \pdfpagewidth=200pt \pdfpageheight=200pt
\pdfhorigin=0sp \pdfvorigin=0sp
\font\body=cmr10 \body
\def\place#1#2#3{\hbox to0sp{\kern#1sp\lower#2sp\hbox{#3}\hss}}
\shipout\hbox{%
\place{66}{13107134}{\pdfliteral{q Q}}%
\place{330}{13106870}{\pdfliteral{q Q}}%
\place{33}{13107134}{A}}
\end",
)
.unwrap();
let result = native_fonts::command()
.args(["native", "--job", "--initex", "--etex"])
.arg(&source)
.arg("origin.pdf")
.current_dir(&work)
.output()
.unwrap();
assert!(
result.status.success(),
"{}",
String::from_utf8_lossy(&result.stderr)
);
let pdf = lopdf::Document::load(work.join("origin.pdf")).unwrap();
let contents = pdf.get_page_content(pdf.get_pages()[&1]).unwrap();
let stream = lopdf::content::Content::decode(&contents).unwrap();
let mut moves = stream.operations.iter().filter(|op| op.operator == "Td");
let start = moves.next().expect("first glyph position");
// The two literals leave 1 sp of residue on each axis after moving
// back. pdfTeX 1.40.29 emits 0 0 Td for the first glyph.
assert!(matches!(
start.operands.as_slice(),
[x, y] if [x, y].iter().all(|axis| matches!(
axis,
lopdf::Object::Integer(0) | lopdf::Object::Real(0.0)
))
), "{start:?}");
assert!(moves.next().is_none());
fs::remove_dir_all(work).unwrap();
}
#[test] #[test]
fn shipped_pdf_spaces_take_a_real_font_slot_in_output_order() { fn shipped_pdf_spaces_take_a_real_font_slot_in_output_order() {
let work = directory(); let work = directory();