Expose stored left and right margin kerns from native boxes

This commit is contained in:
gloamxun committed 2026-11-21 02:13:39 +08:00
1 parent 432c1d2ba7
commit d46d6d79a8
9 files changed
+104 -31

No files matched your search

@@ -896,6 +896,22 @@ impl Executor {
let number = self.pdf_page_reference(files)?; let number = self.pdf_page_reference(files)?;
self.print_tokens(&number.to_string(), token)?; self.print_tokens(&number.to_string(), token)?;
} }
Leftmarginkern | Rightmarginkern => {
let register = self.register_index(files)?;
self.reads.insert(Dependency::BoxRegister(register));
let Some(NativeBox { content: BoxContent::Horizontal(nodes), .. }) =
self.state.box_register(register)
else {
return self.fail("margin kern requires a non-void horizontal box");
};
let width = paragraph::stored_margin_kern(nodes, primitive == Rightmarginkern);
let value = if width == 0 {
"0pt".to_owned()
} else {
format!("{}pt", registers::print_scaled(width))
};
self.print_tokens(&value, token)?;
}
Pdfcolorstackinit => { Pdfcolorstackinit => {
let stack = self.init_pdf_color_stack(files)?; let stack = self.init_pdf_color_stack(files)?;
self.print_tokens(&stack.to_string(), token)?; self.print_tokens(&stack.to_string(), token)?;
@@ -8,8 +8,8 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak}, sync::{Arc, Mutex, OnceLock, Weak},
}; };
const SCHEMA: u32 = 38; const SCHEMA: u32 = 39;
const COMPATIBLE_SCHEMAS: [u32; 10] = [27, 28, 31, 32, 33, 34, 35, 36, 37, SCHEMA]; const COMPATIBLE_SCHEMAS: [u32; 11] = [27, 28, 31, 32, 33, 34, 35, 36, 37, 38, SCHEMA];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION")); const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
#[derive(serde::Serialize, serde::Deserialize)] #[derive(serde::Serialize, serde::Deserialize)]
@@ -6,6 +6,7 @@ use std::collections::BTreeMap;
mod break_cache; mod break_cache;
mod expansion; mod expansion;
mod protrusion; mod protrusion;
pub(super) use protrusion::stored_margin_kern;
use break_cache::BreakNode; use break_cache::BreakNode;
pub use break_cache::{LineBreakCache, LineBreakWork}; pub use break_cache::{LineBreakCache, LineBreakWork};
@@ -43,32 +43,7 @@ impl Edge {
}) if boxed.width == 0 && boxed.height == 0 && boxed.depth == 0 => Self::Skip, }) if boxed.width == 0 && boxed.height == 0 && boxed.depth == 0 => Self::Skip,
_ => Self::Solid, _ => Self::Solid,
}, },
TextNode::Language(_) _ if skippable(node) => Self::Skip,
| TextNode::Mark(_)
| TextNode::Adjustment(_)
| TextNode::Insertion(_)
| TextNode::Penalty(_)
| TextNode::Indent(0)
| TextNode::AccentKern(0)
| TextNode::MathBoundary { width: 0, .. }
| TextNode::Kern {
explicit: false, ..
}
| TextNode::Kern { width: 0, .. } => Self::Skip,
TextNode::Output(node) if node.dimensions().is_none() => Self::Skip,
TextNode::Glue(glue)
| TextNode::NamedGlue(glue, _)
| TextNode::Leaders(LeaderGlue { glue, .. })
if glue.canonical_zero =>
{
Self::Skip
}
TextNode::Discretionary {
pre_break,
post_break,
replacement_count: 0,
..
} if pre_break.is_empty() && post_break.is_empty() => Self::Skip,
_ => Self::character(node), _ => Self::character(node),
} }
} }
@@ -107,6 +82,58 @@ impl Edge {
} }
} }
fn skippable(node: &TextNode) -> bool {
match node {
TextNode::Box(boxed) => matches!(
boxed.native_box.as_deref(),
Some(NativeBox { content: BoxContent::Horizontal(nodes), .. })
if nodes.is_empty() && boxed.width == 0 && boxed.height == 0 && boxed.depth == 0
),
TextNode::Language(_)
| TextNode::Mark(_)
| TextNode::Adjustment(_)
| TextNode::Insertion(_)
| TextNode::Penalty(_)
| TextNode::Indent(0)
| TextNode::AccentKern(0)
| TextNode::MathBoundary { width: 0, .. }
| TextNode::Kern { explicit: false, .. }
| TextNode::Kern { width: 0, .. } => true,
TextNode::Output(node) => node.dimensions().is_none(),
TextNode::Glue(glue)
| TextNode::NamedGlue(glue, _)
| TextNode::Leaders(LeaderGlue { glue, .. }) => glue.canonical_zero,
TextNode::Discretionary {
pre_break,
post_break,
replacement_count: 0,
..
} => pre_break.is_empty() && post_break.is_empty(),
_ => false,
}
}
pub(in crate::execution) fn stored_margin_kern(nodes: &[TextNode], right: bool) -> i32 {
let side_skip = if right { Parameter::Rightskip } else { Parameter::Leftskip };
let non_skippable = |node: &&TextNode| {
!matches!(node, TextNode::NamedGlue(_, parameter) if *parameter == side_skip)
&& !skippable(node)
};
// The query walks only the stored hlist. It does not descend into a nested
// box or recalculate protrusion using the current font codes.
let edge = if right {
nodes.iter().rev().find(non_skippable)
} else {
nodes.iter().find(non_skippable)
};
match edge {
Some(TextNode::MarginKern { width, right: actual_side, .. }) if *actual_side == right => {
*width
}
_ => 0,
}
}
pub(super) fn margin_kern( pub(super) fn margin_kern(
state: &State, state: &State,
nodes: &[TextNode], nodes: &[TextNode],
@@ -329,6 +329,8 @@ pub enum Primitive {
Pdfpageref, Pdfpageref,
Partokenname, Partokenname,
Pdfliteral, Pdfliteral,
Leftmarginkern,
Rightmarginkern,
} }
impl Primitive { impl Primitive {
@@ -384,6 +386,8 @@ impl Primitive {
| Pdfuniformdeviate | Pdfuniformdeviate
| Pdfnormaldeviate | Pdfnormaldeviate
| Pdfpageref | Pdfpageref
| Leftmarginkern
| Rightmarginkern
| Pdfcolorstackinit | Pdfcolorstackinit
| Csname | Csname
| Unless | Unless
@@ -972,6 +976,8 @@ impl Default for State {
("pdfpageref", Pdfpageref), ("pdfpageref", Pdfpageref),
("partokenname", Partokenname), ("partokenname", Partokenname),
("pdfliteral", Pdfliteral), ("pdfliteral", Pdfliteral),
("leftmarginkern", Leftmarginkern),
("rightmarginkern", Rightmarginkern),
("pdfcolorstack", Pdfcolorstack), ("pdfcolorstack", Pdfcolorstack),
("pdfsave", Pdfsave), ("pdfsave", Pdfsave),
("pdfstartlink", Pdfstartlink), ("pdfstartlink", Pdfstartlink),
@@ -246,7 +246,7 @@ fn genuine_fraction_primitives_and_retained_boxes_survive_format_roundtrip() {
let envelope: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); let envelope: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
let payload: serde_json::Value = let payload: serde_json::Value =
serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap(); serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap();
assert_eq!(payload["schema"], 38); assert_eq!(payload["schema"], 39);
let state = State::initex().with_etex(); let state = State::initex().with_etex();
let loaded = FormatImage::decode(&bytes) let loaded = FormatImage::decode(&bytes)
.unwrap() .unwrap()
@@ -334,7 +334,7 @@ fn old_schema_and_missing_unicode_state_are_rejected_even_with_valid_checksums()
let envelope: serde_json::Value = serde_json::from_slice(&encoded).unwrap(); let envelope: serde_json::Value = serde_json::from_slice(&encoded).unwrap();
let original: serde_json::Value = let original: serde_json::Value =
serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap(); serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap();
assert_eq!(original["schema"], 38); assert_eq!(original["schema"], 39);
for (schema, expected) in [ for (schema, expected) in [
(1, "incompatible native format"), (1, "incompatible native format"),
(2, "incompatible native format"), (2, "incompatible native format"),
@@ -164,7 +164,7 @@ fn new_format_owns_vcenter_and_boxed_math_without_reinjecting_old_meanings() {
let envelope: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); let envelope: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
let payload: serde_json::Value = let payload: serde_json::Value =
serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap(); serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap();
assert_eq!(payload["schema"], 38); assert_eq!(payload["schema"], 39);
let executor = FormatImage::decode(&bytes) let executor = FormatImage::decode(&bytes)
.unwrap() .unwrap()
.new_job( .new_job(
+23
View File
@@ -1126,6 +1126,29 @@ schema28 更新。schema 1至26和29仍拒绝,27与28缺少必需 Unicode
[原样模板记录](PUBLISHER_TEMPLATE_MATRIX.md#aps-revtex-原样双栏样稿) [原样模板记录](PUBLISHER_TEMPLATE_MATRIX.md#aps-revtex-原样双栏样稿)
[pdfTeX 的重音布局](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/pdftexdir/pdftex.web#L28084-L28143) [pdfTeX 的重音布局](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/pdftexdir/pdftex.web#L28084-L28143)
### 实际横盒的左右边缘 kern
`\leftmarginkern` 和 `\rightmarginkern` 从指定横盒自己的列表读出带符号
的边缘节点,跳过同侧 skip 和 pdfTeX 边缘扫描允许跳过的节点。查询只读,
保持盒可继续复制和发页。嵌套横盒有独立边缘,查询不会下降到内盒。
空横盒返回 `0pt`,空寄存器或垂直盒触发诊断。schema39 登记两个原语。
[pdfTeX 的边缘 kern 查询](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/pdftexdir/pdftex.web#L11371-L11399)
同一未改动输入有5条查询记录,包含非零左右 skip、−0.75 pt 和
−1.25 pt 的实际悬挂、嵌套及空盒、改变字体代码后查询原盒,以及后续
尺寸赋值,均与 pdfTeX 逐字节一致。用查询结果绘制的一页144 DPI 灰度图
逐像素相同。垂直盒被两端拒绝。
编辑第二页从右边缘改读左边缘时,复用946步并保持已经装好的横盒,
只替换第二页。PDF、SVG 和来源映射与独立零复用一致。来源映射只
归一化测试目录及会话修订号。
`target/p3-margin-kern-query-native` 的回执:
- `receipt.json` 为 `9751ac64739fa0cb19b227128eb9710f7ddf3dea19e2fb8212304e3da276037d`。
- `checkpoint/receipt.json` 为 `1462c599a6c1a9e658d52b16c6ed29dbd5652e6467e471e1f86b45c98def3399`。
- 已有悬挂与发页对照通过,`protrusion-regression.log` 为 `2d690085cf12141a0d5aadf771ce5efbc3911417355faaaa4b9e7599118ceb74`。
### 原生 PDF 字面与实际设备原点 ### 原生 PDF 字面与实际设备原点
`\pdfliteral` 保留实际列表节点和八位 PDF 字节。普通节点在扫描时展开, `\pdfliteral` 保留实际列表节点和八位 PDF 字节。普通节点在扫描时展开,