diff --git a/crates/texrra-tex-core/src/execution/engine.rs b/crates/texrra-tex-core/src/execution/engine.rs index 4a87ae3..0032662 100644 --- a/crates/texrra-tex-core/src/execution/engine.rs +++ b/crates/texrra-tex-core/src/execution/engine.rs @@ -1103,6 +1103,10 @@ impl Executor { } Pdftexrevision => self.print_tokens(self.state.pdftex_revision(), token)?, XeTeXrevision => self.print_tokens(Self::XETEX_REVISION, token)?, + XeTeXglyphname => { + let name = self.xetex_glyph_name(files)?; + self.print_tokens(&name, token)?; + } Etexrevision => self.print_tokens(".6", token)?, Csname => { let name = self.csname(files)?; @@ -1285,7 +1289,7 @@ impl Executor { RegisterValue::Dimension(self.paragraph_shape_dimension(p, files)?).text(false) } Meaning::Primitive( - p @ (Fontdimen | Fontcharwd | Fontcharht | Fontchardp | Fontcharic), + p @ (Fontdimen | Fontcharwd | Fontcharht | Fontchardp | Fontcharic | XeTeXglyphbounds), ) => RegisterValue::Dimension(self.font_dimension(p, files)?).text(false), _ => self.internal_integer_from(next, files)?.to_string(), }; @@ -2337,7 +2341,8 @@ impl Executor { | Primitive::Fontcharwd | Primitive::Fontcharht | Primitive::Fontchardp - | Primitive::Fontcharic), + | Primitive::Fontcharic + | Primitive::XeTeXglyphbounds), ) => self.font_dimension(p, files), Meaning::Primitive(p) if p.page_dimension().is_some() => self.page_dimension_value(p), Meaning::Primitive(Primitive::Interactionmode) => Ok(self.interaction_mode_value()), @@ -3255,6 +3260,8 @@ impl Executor { Primitive::XeTeXpdffile => "XeTeXpdffile".into(), Primitive::XeTeXpicfile => "XeTeXpicfile".into(), Primitive::XeTeXglyph => "XeTeXglyph".into(), + Primitive::XeTeXglyphbounds => "XeTeXglyphbounds".into(), + Primitive::XeTeXglyphname => "XeTeXglyphname".into(), Primitive::XeTeXinputencoding => "XeTeXinputencoding".into(), Primitive::XeTeXdefaultencoding => "XeTeXdefaultencoding".into(), Primitive::XeTeXcharclass => "XeTeXcharclass".into(), diff --git a/crates/texrra-tex-core/src/execution/engine/fonts.rs b/crates/texrra-tex-core/src/execution/engine/fonts.rs index b7e88e7..33fc22d 100644 --- a/crates/texrra-tex-core/src/execution/engine/fonts.rs +++ b/crates/texrra-tex-core/src/execution/engine/fonts.rs @@ -68,6 +68,9 @@ impl Executor { .fontdimen(font, n) .map_err(|e| self.error(e, self.current)); } + if p == Primitive::XeTeXglyphbounds { + return self.xetex_glyph_bounds(files); + } let font = self.font_identifier(files)?; let slot = self.integer(files)?; let instance = self.state.fonts.font(font).map_err(|e| self.error(e, self.current))?; @@ -98,6 +101,40 @@ impl Executor { _ => unreachable!(), } as i32) } + fn xetex_glyph_bounds(&mut self, files: &mut impl SourceProvider) -> Result { + self.reads.insert(Dependency::TextFont); + self.reads.insert(Dependency::FontResources); + let font = self.state.fonts.font(self.state.text_font()) + .map_err(|error| self.error(error, self.current))? + .opentype() + .cloned() + .ok_or_else(|| self.error(r"\XeTeXglyphbounds requires a native font", self.current))?; + let edge = self.integer(files)?; + if !(1..=4).contains(&edge) { + return self.fail(format!(r"\XeTeXglyphbounds requires an edge index from 1 to 4 (got {edge})")); + } + // XeTeX passes the operand to its 16-bit native glyph interface. + let glyph = self.integer(files)? as u16; + let edges = font.glyph_bounds(glyph) + .map_err(|error| self.error(error, self.current))?; + i32::try_from(edges[edge as usize - 1]) + .map_err(|_| self.error("XeTeX glyph bound exceeds the dimension range", self.current)) + } + + pub(super) fn xetex_glyph_name( + &mut self, files: &mut impl SourceProvider, + ) -> Result { + let id = self.font_identifier(files)?; + let font = self.state.fonts.font(id).map_err(|error| self.error(error, self.current))? + .opentype() + .cloned() + .ok_or_else(|| self.error(r"\XeTeXglyphname requires a native font", self.current))?; + let glyph = self.integer(files)? as u16; + let face = font.face().map_err(|error| self.error(error, self.current))?; + let name = face.glyph_name(rustybuzz::ttf_parser::GlyphId(glyph)); + Ok(name.unwrap_or_default().to_owned()) + } + pub(super) fn protrusion_target( &mut self, files: &mut impl SourceProvider, diff --git a/crates/texrra-tex-core/src/execution/engine/quantities.rs b/crates/texrra-tex-core/src/execution/engine/quantities.rs index 3f999f6..d6a53e0 100644 --- a/crates/texrra-tex-core/src/execution/engine/quantities.rs +++ b/crates/texrra-tex-core/src/execution/engine/quantities.rs @@ -141,7 +141,8 @@ impl Executor { | Primitive::Fontcharwd | Primitive::Fontcharht | Primitive::Fontchardp - | Primitive::Fontcharic), + | Primitive::Fontcharic + | Primitive::XeTeXglyphbounds), ) => Ok(Some(Target::Value(self.font_dimension(p, files)?))), Meaning::Primitive( p @ (Primitive::Dimexpr | Primitive::Glueexpr | Primitive::Muexpr), diff --git a/crates/texrra-tex-core/src/execution/format_image.rs b/crates/texrra-tex-core/src/execution/format_image.rs index b469ca8..4a88c0c 100644 --- a/crates/texrra-tex-core/src/execution/format_image.rs +++ b/crates/texrra-tex-core/src/execution/format_image.rs @@ -8,9 +8,9 @@ use std::{ sync::{Arc, Mutex, OnceLock, Weak}, }; -const SCHEMA: u32 = 73; -const COMPATIBLE_SCHEMAS: [u32; 40] = [ - 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, SCHEMA, +const SCHEMA: u32 = 74; +const COMPATIBLE_SCHEMAS: [u32; 41] = [ + 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, SCHEMA, ]; const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION")); diff --git a/crates/texrra-tex-core/src/execution/state.rs b/crates/texrra-tex-core/src/execution/state.rs index 7503c8a..373f95b 100644 --- a/crates/texrra-tex-core/src/execution/state.rs +++ b/crates/texrra-tex-core/src/execution/state.rs @@ -390,6 +390,8 @@ pub enum Primitive { XeTeXcharclass, XeTeXinterchartoks, XeTeXglyph, + XeTeXglyphbounds, + XeTeXglyphname, } impl Primitive { @@ -472,6 +474,7 @@ impl Primitive { | Pdfcreationdate | Pdftexrevision | XeTeXrevision + | XeTeXglyphname | Pdfstrcmp | Pdfmatch | Pdflastmatch @@ -1732,6 +1735,8 @@ impl State { ("XeTeXpdffile", Primitive::XeTeXpdffile), ("XeTeXpicfile", Primitive::XeTeXpicfile), ("XeTeXglyph", Primitive::XeTeXglyph), + ("XeTeXglyphbounds", Primitive::XeTeXglyphbounds), + ("XeTeXglyphname", Primitive::XeTeXglyphname), ("XeTeXpdfpagecount", Primitive::XeTeXpdfpagecount), ("XeTeXinputencoding", Primitive::XeTeXinputencoding), ("XeTeXdefaultencoding", Primitive::XeTeXdefaultencoding), diff --git a/crates/texrra-tex-core/src/opentype.rs b/crates/texrra-tex-core/src/opentype.rs index 6035bcd..04d0594 100644 --- a/crates/texrra-tex-core/src/opentype.rs +++ b/crates/texrra-tex-core/src/opentype.rs @@ -441,6 +441,22 @@ impl OpenTypeFont { Ok(OpenTypeRun::single_glyph(self, &face, GlyphId(glyph), "", source, use_glyph_metrics)) } + pub(crate) fn glyph_bounds(&self, glyph: u16) -> Result<[i64; 4], String> { + let face = self.face()?; + let glyph = GlyphId(glyph); + let [left, bottom, right, top] = glyph_control_bounds(&face, glyph); + let points = |units| units_to_points(units, self.size, face.units_per_em()); + let advance = points(f32::from(face.glyph_hor_advance(glyph).unwrap_or(0))); + let extend = self.request.horizontal_scale(); + let bearing_scale = if extend == 0. { 1. } else { extend }; + Ok([ + points_to_sp(points(left) * bearing_scale), + points_to_sp(points(top)), + points_to_sp((advance - points(right)) * bearing_scale), + points_to_sp(-points(bottom)), + ]) + } + pub(crate) fn glyph_metrics( &self, character: char, diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index 32d3f01..0f10753 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -2175,6 +2175,33 @@ PDF,恢复实际 PFB 并反转斜切时复用 1277 步,只替换第二页。 全量编译一致。incremental-receipt.json 的 SHA-256 为 bbc0991b0316e6d76aa58649ec873525f5544ab530d93fe491fc6e16bd00a27d。 +### XeTeX 字形边距与名称查询 + +`\XeTeXglyphbounds` 读取当前字体的真实轮廓控制框和字形前进量, +四个选择依次返回左边距、高度、右边距、深度。正负边距进入 TeX +尺寸和条件扫描,按参考处理字宽缩放。`\XeTeXglyphname` +从指定字体读取原始字形名称,并以普通字符展开。两个查询对较宽的 +整数操作数采用 XeTeX 的 16 位字形编号转换。 +[XeTeX 的字形查询与设备度量](https://github.com/TeX-Live/texlive-source/blob/279984fab6eaed8d17e02882f2668ab5caac0f27/texk/web2c/xetexdir/XeTeX_ext.c#L1863-L1892) + +原语、尺寸表达式、宏展开和真实 fontspec 对照共 4 页、 +6 个字形、2 条规则。查询值、原始坐标、提取文本及 +144 DPI RGB 光栅一致,另核对空格字形、零横向缩放和有符号编号。 +由原样 xelatex.ini 转储 schema74 后实际加载,fontspec-xetex.sty +和两种字体的读取指纹与参考相同。旧 schema71 原样 ACL 复编的 +PDF、aux、out、bbl 与前版一致。12 项 XeTeX 定向回归通过。 + +三次跨文件修改字形编号各复用 66 步、重放两次调用,只更新 +第二页。实际字形位置由尺寸查询计算,字体内的名称写入 aux。 +分别以该次编辑之前的生成文件初态独立全量编译,PDF、SVG、 +来源映射和实际 aux 文件均一致。 +回执 `target/p3-xetex-glyph-queries/receipt.json` 的 SHA-256 为 +`f539cf9c95a5a75d2544daf09c311a20d1569cf39f19f15ff8d8df65595cb686`。 + +下一入口为 `\XeTeXfirstfontchar` 和 `\XeTeXlastfontchar`。 +`target/p3-xetex-font-char-range` 已保存 CFF、TrueType、TFM 和 +nullfont 的原样查询及参考出页。 + ### XeTeX 显式字形节点 `\XeTeXglyph` 在 XeTeX 扩展模式读入真实 16 位字形编号, diff --git a/docs/PUBLISHER_TEMPLATE_MATRIX.md b/docs/PUBLISHER_TEMPLATE_MATRIX.md index 7304ff1..51634ae 100644 --- a/docs/PUBLISHER_TEMPLATE_MATRIX.md +++ b/docs/PUBLISHER_TEMPLATE_MATRIX.md @@ -758,6 +758,16 @@ XDV 链接注释接入后,沿原样 ACL 的第二轮辅助文件和 BibTeX `target/p3-xdv-annotation/receipt.json`。 [链接注释阶段记录](P2_M1_PROGRESS.md#xdv-链接注释与实际矩形) +#### XeTeX 字形边距与名称复编 + +同一 schema71、真实字体及已收敛的辅助文件原样复编 ACL。 +PDF、aux、out、bbl 与前版逐字节一致。另从原样 xelatex.ini +转储 schema74,执行真实 fontspec 和两种实际字体的边距与名称 +查询。查询驱动的两页输出与参考一致。原样 ACL 继续沿用既有整页门禁。 +回执 `target/p3-xetex-glyph-queries/receipt.json` 的 SHA-256 为 +`f539cf9c95a5a75d2544daf09c311a20d1569cf39f19f15ff8d8df65595cb686`。 +[字形查询阶段记录](P2_M1_PROGRESS.md#xetex-字形边距与名称查询) + #### XeTeX 显式字形复编 同一 schema71、真实字体和已收敛的辅助文件原样复编 ACL。