Use live font choices for pdfTeX packed kerns

This commit is contained in:
gloamxun committed 2026-11-26 14:33:49 +08:00
1 parent 9e13cca481
commit 02d4c3116a
3 files changed
+89 -53

No files matched your search

@@ -95,38 +95,45 @@ fn pair(nodes: &[TextNode], at: usize) -> Option<(Character<'_>, Character<'_>)>
.then_some((left, right))
}
fn measure(
state: &State,
font: &NativeFont,
slot: u8,
width: i64,
right: Option<u8>,
) -> Result<Capacity, String> {
let Some(config) = state.fonts.expansion(font.id) else {
return Ok(Capacity::default());
};
if font.expansion != 0 {
return Ok(Capacity::default());
}
let limit_width = |amount| -> Result<i64, String> {
if amount == 0 {
return Ok(width);
}
let variant = state.fonts.expansion_limit(font.id, amount)?;
Ok(match right {
Some(right) => variant.metrics().kern(char::from(slot), char::from(right)),
None => variant.advance(slot),
})
};
let code = state.fonts.expansion_code(font.id, slot);
let scale = |value: i64| {
pdf_device::magnify(if right.is_some() { value } else { value.max(0) }, code)
};
Ok(Capacity {
stretch: scale(limit_width(config.stretch)? - width),
shrink: scale(width - limit_width(-config.shrink)?),
})
}
fn capacity(
state: &State,
nodes: &[TextNode],
at: usize,
limits: Option<&mut Limits>,
) -> Result<Capacity, String> {
let measure = |font: &NativeFont, slot: u8, width, right: Option<u8>| {
let Some(config) = state.fonts.expansion(font.id) else {
return Ok(Capacity::default());
};
if font.expansion != 0 {
return Ok(Capacity::default());
}
let limit_width = |amount| -> Result<i64, String> {
if amount == 0 {
return Ok(width);
}
let variant = state.fonts.expansion_limit(font.id, amount)?;
Ok(match right {
Some(right) => variant.metrics().kern(char::from(slot), char::from(right)),
None => variant.advance(slot),
})
};
let code = state.fonts.expansion_code(font.id, slot);
let scale = |value: i64| {
pdf_device::magnify(if right.is_some() { value } else { value.max(0) }, code)
};
Ok(Capacity {
stretch: scale(limit_width(config.stretch)? - width),
shrink: scale(width - limit_width(-config.shrink)?),
})
};
if let Some(glyph) = character(&nodes[at]) {
if glyph.font.expansion == 0
&& let Some(limits) = limits
@@ -134,7 +141,7 @@ fn capacity(
{
limits.include(config)?;
}
return measure(glyph.font, glyph.slot, glyph.width, None);
return measure(state, glyph.font, glyph.slot, glyph.width, None);
}
if let TextNode::Kern {
width,
@@ -142,15 +149,7 @@ fn capacity(
} = nodes[at]
&& let Some((left, right)) = pair(nodes, at)
{
let kern = left
.font
.metrics()
.kern(char::from(left.slot), char::from(right.slot));
// A boundary kern and a TFM kern between two characters have the same
// node type. Only use the font program's actual kerning pair here.
if kern == i64::from(width) {
return measure(left.font, left.slot, kern, Some(right.slot));
}
return measure(state, left.font, left.slot, i64::from(width), Some(right.slot));
}
Ok(Capacity::default())
}
@@ -420,6 +419,21 @@ pub(super) fn pack(
}
continue;
}
if let TextNode::Kern { width, explicit: false } = material[i] {
// The left glyph has already been visited, while the right one
// still carries its original font. Check those live font choices.
if let Some((left, right)) = pair(material, i) {
let capacity = measure(state, left.font, left.slot, i64::from(width), Some(right.slot))?;
let variation = if ratio < 0 { capacity.shrink } else { capacity.stretch };
if variation != 0 && ratio != 0 {
let kern = left.font.metrics().kern(char::from(left.slot), char::from(right.slot));
let width = i32::try_from(kern)
.map_err(|_| "native font kern exceeds the scaled point range")?;
material[i] = TextNode::Kern { width, explicit: false };
}
}
continue;
}
let Some(glyph) = character(&material[i]) else {
continue;
};
@@ -428,26 +442,9 @@ pub(super) fn pack(
continue;
}
let selected = fonts.select(state, glyph.id, amount, files)?;
let character = char::from(glyph.slot);
let metrics = state.fonts.character(selected, glyph.slot)?
.ok_or_else(|| format!("expanded font has no TFM slot {}", glyph.slot))?;
// Reuse the original pair for synthesized kern scaling. A loaded
// TFM can affect capacity without replacing the original pair kern.
let face = state.fonts.glyph_font(selected)?;
if state.fonts.shares_font_parameters(selected)
&& !state.fonts.has_external_expansion(glyph.id)
&& let Some(TextNode::Kern { width, explicit: false }) = material.get(i + 1)
&& let Some((left, right)) = pair(material, i + 1)
{
let next = char::from(right.slot);
let original = left.font.metrics().kern(character, next);
if original == i64::from(*width)
&& let TextNode::Kern { width, .. } = &mut material[i + 1]
{
*width = i32::try_from(face.metrics().kern(character, next))
.map_err(|_| "expanded native font kern exceeds the scaled point range")?;
}
}
let TextNode::Glyph { font, glyph, .. } = &mut material[i] else {
unreachable!()
};
+29
View File
@@ -2175,6 +2175,35 @@ PDF,恢复实际 PFB 并反转斜切时复用 1277 步,只替换第二页。
全量编译一致。incremental-receipt.json 的 SHA-256 为
bbc0991b0316e6d76aa58649ec873525f5544ab530d93fe491fc6e16bd00a27d。
### pdfTeX 装盒中的字偶距与原样 arXiv 页面
计算伸缩容量时读取正常 kern 节点的实际宽度,并用同字体相邻字形的
TFM 极限度量。装盒替换字体时按节点顺序处理。到达 kern 时,左字形
已处理,右字形仍带其当前字体。只有此时两者的字体身份相同且所选
方向有非零字偶距差,才按当前 TFM 重取宽度。
[参考 kern 容量](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L20746-L20805)
[参考装盒节点顺序](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L20996-L21015)
在 CMR 中复验普通 `AV` 的 +50‰ 用例,参考字偶距为 −1.11113 pt,
候选从 −1.16669 pt 修正到 −1.11113 pt。另用实际 TFM 边界和
预载的伸缩字体构造左字形替换后与右字形同字体的情况,候选把原始
−1.11113 pt 重取为 TFM 的 −2.0 pt,与参考一致。两页小样的实际
kern 列表、词框与光栅通过,原差异分别为 159 和 124 个像素。
保留第二个用例初期字体变体链拒绝、量值为零的证据。
同一六项原样 arXiv 文件、冻结 schema78 与参考资源再经实际 BibTeX
和三轮 LaTeX。三轮 `aux`、实际 `bbl` 和最终 `out` 逐字节一致。
三页的 6918 个实际 PDF 字形身份、顺序、位置、字号和伸缩与参考
一致。1221 个提取词同序且词框完全一致,144 DPI 差异从 4772
降为 0。9 条书签、18 条链接的目标与矩形、另测的命名目标均一致。
实际 PFB、VF、编码和字体映射与冻结参考记录的资源相同。
三次跨文件更改正负伸缩,各复用 99 步,只更新第二页。在重新选择
伸缩字体后核对真实 `AV` 字偶距、来源命中、PDF、SVG、来源图与
`aux`,均与同生成初态的独立全量一致。
回执 `target/p3-arxiv-expansion-kern-order/receipt.json` 的 SHA-256
为 `3c50176d3a9f1ee6123271226c58f7940eb7129b3837738b39672dbca4353a25`。
### pdfTeX 字宽伸缩的两次量化
字形置换先按行伸缩比、`\efcode` 和字体上限计算最接近的整数,
+10
View File
@@ -476,6 +476,16 @@ schema37 优化版使用冻结输入耗时197.5秒生成原样格式,LaTeX 自
- `target/p3-margin-kern-query-native/format/receipt.json` 为 `a9484ec1c83d7f1f420028003f5f422a0e45b5bd7c06f62eb4c98252dc3cdf6d`。
- `target/p3-margin-kern-query-native/arxiv/receipt.json` 为 `85b2ee927455fae5f72d4aeb8373ffd27d6452d21c8c8fceadbd4a3be3a0f9a2`。
### 原样 arXiv 版面及链接复验
同一六项原样输入、冻结参考及 schema78 完成真实 BibTeX 和三轮
LaTeX,无未定义引用。逐轮 aux 与实际 bbl、最终 out 逐字节一致。
装盒按当前字体决定字偶距后,三页的 6918 个原始 PDF 字形及坐标、
1221 个词的顺序和词框、144 DPI 灰度光栅均与参考一致。9 条书签、
18 条链接和命名目标对照通过。核对真实的 14 份 PFB、7 份 VF、
2 份编码及字体映射。原项目的整体验收结果随同版矩阵统一复验。
[阶段记录](P2_M1_PROGRESS.md#pdftex-装盒中的字偶距与原样-arxiv-页面)
### 原样 arXiv 字宽伸缩量化复验
保持六份原样输入、冻结参考格式、字体映射和 schema78。原生和