Normalize native XeTeX physical input before tokenization
This commit is contained in:
1 parent
048478c346
commit
76df2d6832
10 files changed
+214
-63
No files matched your search
@@ -2860,11 +2860,9 @@ impl Executor {
|
||||
let chars: Vec<xe_encoding::Character> = if let Some(encoding) = &encoding {
|
||||
let (resolved, bom) = encoding.resolve(&line);
|
||||
let (length, _) = resolved.framing().line_boundary(&line[bom..]);
|
||||
let (mut chars, warnings) = resolved.decode(&line[bom..bom + length], base + bom)
|
||||
let (chars, warnings) = resolved
|
||||
.decode_line(&line[bom..bom + length], base + bom, &self.state, &mut self.reads)
|
||||
.map_err(|e| self.error(e, Span { source, start: base + bom, end }))?;
|
||||
while chars.last().is_some_and(|c| matches!(c.value, ' ' | '\r' | '\n')) {
|
||||
chars.pop();
|
||||
}
|
||||
if warnings > 0 {
|
||||
self.observe_xe_utf8_warnings(
|
||||
vec![(self.input_position.map_or(1, |(_, line)| line), warnings)],
|
||||
|
||||
@@ -8,9 +8,9 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 76;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 43] = [
|
||||
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, 74, 75, SCHEMA,
|
||||
const SCHEMA: u32 = 77;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 44] = [
|
||||
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, 74, 75, 76, SCHEMA,
|
||||
];
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
|
||||
@@ -112,6 +112,39 @@ mod tests {
|
||||
);
|
||||
assert!(mouth.next(&state, &mut reads).unwrap().is_none());
|
||||
}
|
||||
#[test]
|
||||
fn xe_normalization_preserves_reordered_bytes_and_control_word_extent() {
|
||||
use super::super::{InputProfile, Parameter, ParameterValue, state::Key};
|
||||
|
||||
let mut state = State::default().with_input_profile(InputProfile::XeTeX);
|
||||
state.assign_integer(Key::Endlinechar, -1, false);
|
||||
for mark in ['\u{0300}', '\u{0315}'] {
|
||||
state.assign_catcode(mark, CatCode::Letter, false);
|
||||
}
|
||||
let mut mouth = Mouth::new(0, "é\n\\a\u{0315}\u{0300}!\na\u{0315}\u{0300}é".into(), InputProfile::XeTeX);
|
||||
let mut reads = BTreeSet::new();
|
||||
let first = mouth.next(&state, &mut reads).unwrap().unwrap();
|
||||
assert!(matches!(first.kind, TokenKind::Character('é', _)));
|
||||
assert_eq!((first.span.start, first.span.end), (0, 2));
|
||||
state.assign_parameter(Parameter::XeTeXinputnormalization, ParameterValue::Integer(2), false);
|
||||
let name = mouth.next(&state, &mut reads).unwrap().unwrap();
|
||||
assert_eq!(name.kind, TokenKind::Command(CommandName::Control("a\u{0300}\u{0315}".into())));
|
||||
assert_eq!((name.span.start, name.span.end), (3, 9));
|
||||
for (value, range) in [
|
||||
('!', (9, 10)),
|
||||
('a', (11, 12)),
|
||||
('\u{0300}', (14, 16)),
|
||||
('\u{0315}', (12, 14)),
|
||||
('e', (16, 18)),
|
||||
('\u{0301}', (16, 18)),
|
||||
] {
|
||||
let token = mouth.next(&state, &mut reads).unwrap().unwrap();
|
||||
assert!(matches!(token.kind, TokenKind::Character(c, _) if c == value));
|
||||
assert_eq!((token.span.start, token.span.end), range);
|
||||
}
|
||||
assert!(reads.contains(&Dependency::Parameter(Parameter::XeTeXinputnormalization)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disabled_endlinechar_does_not_merge_control_names_across_lines() {
|
||||
let mut mouth = Mouth::new(
|
||||
@@ -369,8 +402,15 @@ impl Mouth {
|
||||
|
||||
pub(super) fn xe_location(&self, offset: usize) -> Option<(usize, usize)> {
|
||||
let xe = self.xe.as_ref()?;
|
||||
(xe.line_start <= offset && offset <= self.offset && self.line_number > 0)
|
||||
.then(|| (self.line_number, 1 + self.line.partition_point(|c| c.end <= offset && c.start < offset)))
|
||||
(xe.line_start <= offset && offset <= self.offset && self.line_number > 0).then(|| {
|
||||
// Canonical ordering can move a mark ahead of an earlier byte range.
|
||||
let preceding = self
|
||||
.line
|
||||
.iter()
|
||||
.filter(|c| c.end <= offset && c.start < offset)
|
||||
.count();
|
||||
(self.line_number, 1 + preceding)
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn take_utf8_warnings(&mut self) -> Vec<(usize, usize)> {
|
||||
@@ -518,12 +558,10 @@ impl Mouth {
|
||||
self.line_number += 1;
|
||||
self.line.clear();
|
||||
self.cursor = 0;
|
||||
let (mut chars, invalid_utf8) = xe.encoding.decode(&self.data[start..content_end], start)
|
||||
let (characters, invalid_utf8) = xe.encoding
|
||||
.decode_line(&self.data[start..content_end], start, state, reads)
|
||||
.map_err(|message| (Span { source: self.source, start, end: content_end }, message))?;
|
||||
while chars.last().is_some_and(|c| matches!(c.value, ' ' | '\r' | '\n')) {
|
||||
chars.pop();
|
||||
}
|
||||
self.line = chars;
|
||||
self.line = characters;
|
||||
if invalid_utf8 > 0 {
|
||||
self.invalid_utf8.push((self.line_number, invalid_utf8));
|
||||
}
|
||||
@@ -607,15 +645,11 @@ impl Mouth {
|
||||
let Some((value, extra)) = self.expanded_superscript(c.value, self.cursor + 1) else {
|
||||
return Some(c);
|
||||
};
|
||||
let end = self.line[self.cursor + extra].end;
|
||||
self.line.splice(
|
||||
self.cursor..=self.cursor + extra,
|
||||
[Character {
|
||||
value,
|
||||
start: c.start,
|
||||
end,
|
||||
}],
|
||||
);
|
||||
let mut replacement = Character { value, ..c };
|
||||
for &part in &self.line[self.cursor + 1..=self.cursor + extra] {
|
||||
replacement.cover(part);
|
||||
}
|
||||
self.line.splice(self.cursor..=self.cursor + extra, [replacement]);
|
||||
}
|
||||
}
|
||||
/// get_next's ordinary ^^ conversion changes cur_chr and advances loc.
|
||||
@@ -629,8 +663,10 @@ impl Mouth {
|
||||
break;
|
||||
};
|
||||
c.value = value;
|
||||
for &part in &self.line[self.cursor..self.cursor + extra] {
|
||||
c.cover(part);
|
||||
}
|
||||
self.cursor += extra;
|
||||
c.end = self.line[self.cursor - 1].end;
|
||||
}
|
||||
c
|
||||
}
|
||||
@@ -688,7 +724,8 @@ impl Mouth {
|
||||
let mut name = String::new();
|
||||
if let Some(next) = self.peek(state, reads) {
|
||||
self.cursor += 1;
|
||||
span.end = next.end;
|
||||
span.start = span.start.min(next.start);
|
||||
span.end = span.end.max(next.end);
|
||||
name.push(next.value);
|
||||
let code = Self::catcode(state, reads, next.value);
|
||||
if code == CatCode::Letter {
|
||||
@@ -697,7 +734,8 @@ impl Mouth {
|
||||
break;
|
||||
}
|
||||
name.push(next.value);
|
||||
span.end = next.end;
|
||||
span.start = span.start.min(next.start);
|
||||
span.end = span.end.max(next.end);
|
||||
self.cursor += 1;
|
||||
}
|
||||
self.state = LineState::Skip;
|
||||
|
||||
@@ -130,6 +130,7 @@ pub enum Parameter {
|
||||
XeTeXinterwordspaceshaping,
|
||||
XeTeXlinebreakskip,
|
||||
XeTeXlinebreakpenalty,
|
||||
XeTeXinputnormalization,
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum ParameterValue {
|
||||
@@ -162,7 +163,7 @@ impl Parameter {
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) const XETEX: [Self; 10] = [
|
||||
pub(super) const XETEX: [Self; 11] = [
|
||||
Self::XeTeXuseglyphmetrics,
|
||||
Self::XeTeXdashbreakstate,
|
||||
Self::Suppressfontnotfounderror,
|
||||
@@ -173,6 +174,7 @@ impl Parameter {
|
||||
Self::XeTeXinterwordspaceshaping,
|
||||
Self::XeTeXlinebreakskip,
|
||||
Self::XeTeXlinebreakpenalty,
|
||||
Self::XeTeXinputnormalization,
|
||||
];
|
||||
|
||||
pub const ALL: &'static [Self] = &[
|
||||
@@ -301,6 +303,7 @@ impl Parameter {
|
||||
Self::XeTeXinterwordspaceshaping,
|
||||
Self::XeTeXlinebreakskip,
|
||||
Self::XeTeXlinebreakpenalty,
|
||||
Self::XeTeXinputnormalization,
|
||||
];
|
||||
pub fn name(self) -> String {
|
||||
match self {
|
||||
@@ -314,6 +317,7 @@ impl Parameter {
|
||||
Self::XeTeXinterwordspaceshaping => "XeTeXinterwordspaceshaping".into(),
|
||||
Self::XeTeXlinebreakskip => "XeTeXlinebreakskip".into(),
|
||||
Self::XeTeXlinebreakpenalty => "XeTeXlinebreakpenalty".into(),
|
||||
Self::XeTeXinputnormalization => "XeTeXinputnormalization".into(),
|
||||
_ => format!("{self:?}").to_ascii_lowercase(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
//! XeTeX physical input encodings. Decoded units retain their source bytes.
|
||||
use std::sync::Arc;
|
||||
use std::{collections::BTreeSet, sync::Arc};
|
||||
|
||||
use super::{Dependency, Parameter, ParameterValue, State};
|
||||
|
||||
pub(super) mod icu;
|
||||
|
||||
@@ -68,6 +70,13 @@ pub(super) struct Character {
|
||||
pub end: usize,
|
||||
}
|
||||
|
||||
impl Character {
|
||||
pub fn cover(&mut self, other: Self) {
|
||||
self.start = self.start.min(other.start);
|
||||
self.end = self.end.max(other.end);
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoding {
|
||||
pub fn parse(name: &str) -> Result<Option<Self>, String> {
|
||||
let builtin = match name.to_ascii_lowercase().as_str() {
|
||||
@@ -106,7 +115,29 @@ impl Encoding {
|
||||
(encoding, bom)
|
||||
}
|
||||
|
||||
pub fn decode(&self, data: &[u8], base: usize) -> Result<(Vec<Character>, usize), String> {
|
||||
/// Decode a complete physical line before TeX looks at any of its catcodes.
|
||||
pub fn decode_line(
|
||||
&self,
|
||||
data: &[u8],
|
||||
base: usize,
|
||||
state: &State,
|
||||
reads: &mut BTreeSet<Dependency>,
|
||||
) -> Result<(Vec<Character>, usize), String> {
|
||||
let (mut characters, warnings) = self.decode(data, base)?;
|
||||
let parameter = Parameter::XeTeXinputnormalization;
|
||||
reads.insert(Dependency::Parameter(parameter));
|
||||
match state.parameter(parameter) {
|
||||
ParameterValue::Integer(1) => characters = normalize(characters, true),
|
||||
ParameterValue::Integer(2) => characters = normalize(characters, false),
|
||||
_ => {}
|
||||
}
|
||||
while characters.last().is_some_and(|c| matches!(c.value, ' ' | '\r' | '\n')) {
|
||||
characters.pop();
|
||||
}
|
||||
Ok((characters, warnings))
|
||||
}
|
||||
|
||||
fn decode(&self, data: &[u8], base: usize) -> Result<(Vec<Character>, usize), String> {
|
||||
let mut invalid_utf8 = 0;
|
||||
let chars = match self {
|
||||
Self::Auto => unreachable!("resolve the encoding at file open"),
|
||||
@@ -174,6 +205,22 @@ impl Encoding {
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize(characters: Vec<Character>, composed: bool) -> Vec<Character> {
|
||||
use unicode_normalization::char::decompose_canonical;
|
||||
|
||||
if characters.iter().all(|character| character.value.is_ascii()) {
|
||||
return characters;
|
||||
}
|
||||
let mut decomposed = Vec::with_capacity(characters.len());
|
||||
for character in characters {
|
||||
decompose_canonical(character.value, |value| decomposed.push((value, character)));
|
||||
}
|
||||
crate::normalization::order_and_compose(decomposed, composed, Character::cover)
|
||||
.into_iter()
|
||||
.map(|(value, source)| Character { value, ..source })
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn decode_utf8_character(input: &[u8]) -> (char, usize, bool) {
|
||||
let first = input[0];
|
||||
let (mut value, continuation) = match first {
|
||||
|
||||
@@ -483,9 +483,7 @@ fn push(output: &mut Vec<Entry>, value: u32, source: &PaintSource) -> Result<(),
|
||||
}
|
||||
|
||||
fn normalize(input: Vec<Entry>, composed: bool) -> Result<Vec<Entry>, &'static str> {
|
||||
use unicode_normalization::char::{
|
||||
canonical_combining_class as ccc, compose, decompose_canonical,
|
||||
};
|
||||
use unicode_normalization::char::decompose_canonical;
|
||||
|
||||
let mut decomposed = Vec::with_capacity(input.len());
|
||||
for entry in input {
|
||||
@@ -498,35 +496,15 @@ fn normalize(input: Vec<Entry>, composed: bool) -> Result<Vec<Entry>, &'static s
|
||||
return Err("mapped OpenType word capacity exceeded");
|
||||
}
|
||||
}
|
||||
let mut marks = 0;
|
||||
for i in 0..decomposed.len() {
|
||||
if ccc(decomposed[i].0) == 0 {
|
||||
decomposed[marks..i].sort_by_key(|(c, _)| ccc(*c));
|
||||
marks = i + 1;
|
||||
}
|
||||
}
|
||||
decomposed[marks..].sort_by_key(|(c, _)| ccc(*c));
|
||||
let mut output: Vec<Entry> = Vec::with_capacity(decomposed.len());
|
||||
let mut starter: Option<usize> = None;
|
||||
let mut last_class = 0;
|
||||
for (character, source) in decomposed {
|
||||
let class = ccc(character);
|
||||
if composed
|
||||
&& (last_class == 0 || last_class < class)
|
||||
&& let Some(index) = starter
|
||||
&& let Some(joined) = compose(char::from_u32(output[index].value).unwrap(), character)
|
||||
{
|
||||
output[index].value = joined.into();
|
||||
output[index].source.merge(&source);
|
||||
output[index].source =
|
||||
std::mem::take(&mut output[index].source).derived(Derivation::FontMapping);
|
||||
continue;
|
||||
}
|
||||
if class == 0 {
|
||||
starter = Some(output.len());
|
||||
}
|
||||
last_class = class;
|
||||
push(&mut output, character.into(), &source)?;
|
||||
}
|
||||
Ok(output)
|
||||
let normalized = crate::normalization::order_and_compose(decomposed, composed, |starter, mark| {
|
||||
starter.merge(&mark);
|
||||
*starter = std::mem::take(starter).derived(Derivation::FontMapping);
|
||||
});
|
||||
Ok(normalized
|
||||
.into_iter()
|
||||
.map(|(character, source)| Entry {
|
||||
value: character.into(),
|
||||
source,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
@@ -5,6 +5,7 @@ pub mod math;
|
||||
pub mod math_engine_tex;
|
||||
pub mod math_font;
|
||||
mod font_mapping;
|
||||
mod normalization;
|
||||
pub mod opentype;
|
||||
pub mod native_font;
|
||||
pub mod output;
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
use unicode_normalization::char::{canonical_combining_class, compose};
|
||||
|
||||
/// Canonically order decomposed characters and optionally recompose them.
|
||||
/// Each payload follows its own character across reordering.
|
||||
pub(crate) fn order_and_compose<T>(
|
||||
mut decomposed: Vec<(char, T)>,
|
||||
composed: bool,
|
||||
mut merge: impl FnMut(&mut T, T),
|
||||
) -> Vec<(char, T)> {
|
||||
let mut marks = 0;
|
||||
for i in 0..decomposed.len() {
|
||||
if canonical_combining_class(decomposed[i].0) == 0 {
|
||||
decomposed[marks..i].sort_by_key(|(c, _)| canonical_combining_class(*c));
|
||||
marks = i + 1;
|
||||
}
|
||||
}
|
||||
decomposed[marks..].sort_by_key(|(c, _)| canonical_combining_class(*c));
|
||||
if !composed {
|
||||
return decomposed;
|
||||
}
|
||||
|
||||
let mut output: Vec<(char, T)> = Vec::with_capacity(decomposed.len());
|
||||
let mut starter: Option<usize> = None;
|
||||
let mut last_class = 0;
|
||||
for (character, payload) in decomposed {
|
||||
let class = canonical_combining_class(character);
|
||||
if (last_class == 0 || last_class < class)
|
||||
&& let Some(index) = starter
|
||||
&& let Some(joined) = compose(output[index].0, character)
|
||||
{
|
||||
output[index].0 = joined;
|
||||
merge(&mut output[index].1, payload);
|
||||
continue;
|
||||
}
|
||||
if class == 0 {
|
||||
starter = Some(output.len());
|
||||
}
|
||||
last_class = class;
|
||||
output.push((character, payload));
|
||||
}
|
||||
output
|
||||
}
|
||||
@@ -2175,6 +2175,40 @@ PDF,恢复实际 PFB 并反转斜切时复用 1277 步,只替换第二页。
|
||||
全量编译一致。incremental-receipt.json 的 SHA-256 为
|
||||
bbc0991b0316e6d76aa58649ec873525f5544ab530d93fe491fc6e16bd00a27d。
|
||||
|
||||
### XeTeX 物理输入行的 Unicode 规范化
|
||||
|
||||
`\XeTeXinputnormalization` 接入有作用域的整数状态。物理输入行先按
|
||||
正在读取的字节编码解码,值 1 执行 NFC,值 2 执行 NFD,其他值
|
||||
保留解码结果。随后去除行尾空格并分词。已载入的行保持加载时的
|
||||
状态。`\input`、`\read`、`\readline` 共用实际解码与规范化,
|
||||
`\scantokens` 的内存文本保留原字符。规范化使用已采用的 Unicode 17
|
||||
数据,与字体映射共用规范排序与合成。分解字符与重排的标记保留各自的
|
||||
原始字节跨度,合成字符覆盖参与合成的范围,诊断和控制序列扫描处理
|
||||
重排后的偏移。
|
||||
[参考行读取](https://github.com/TeX-Live/texlive-source/blob/279984fab6eaed8d17e02882f2668ab5caac0f27/texk/web2c/xetexdir/XeTeX_ext.c#L275-L434)
|
||||
|
||||
Unicode 17 的 20,034 条官方数据覆盖 200,340 次规范化转换。原样
|
||||
XeTeX 对照覆盖同一行赋值、分组、UTF-8 子文件、UTF-16 流、ISO-8859-1、
|
||||
内存文件、控制序列名及跨格式重载。真实 xelatex.ini 转储并加载
|
||||
schema77。原语与真实 fontspec 的 3 页、40 个字形、85
|
||||
项诊断、提取文本和 144 DPI 光栅与参考一致。11 项定向扫描回归
|
||||
通过,既有真实 TECkit 字体映射的 600 DPI 光栅仍与参考一致。原样 ACL
|
||||
使用旧 schema71 与已收敛辅助文件复编,PDF、aux、out、bbl 与前版逐字节一致。
|
||||
本次格式转储与定向构建并行,耗时记录在回执中,性能仍按固定环境验收。
|
||||
[Unicode 17 规范化测试数据](https://www.unicode.org/Public/17.0.0/ucd/NormalizationTest.txt)
|
||||
|
||||
三次跨文件模式编辑各复用 86 步,只更新第二页。渲染和来源命中
|
||||
保留 NFC 的合并范围及 NFD 的原始分离范围。PDF、SVG、来源映射
|
||||
与真实 aux 均与对应生成初态的独立全量一致。无效模式赋值保留
|
||||
上一版实际 aux,随后恢复编译并通过同一对照。
|
||||
回执 `target/p3-xetex-input-normalization/receipt.json` 的 SHA-256 为 `1274d38b084b15d01560745ae44821d8952a311d74d5f793e3c7f42f9dd27794`。
|
||||
|
||||
下一入口为 Graphite 字体造型与断行。`target/p3-xetex-graphite` 保存 SIL
|
||||
Padauk 5.100 原样字体、许可、三页原版 XDV/PDF 和当前原生诊断。
|
||||
同一字体的 Graphite/OpenType 类型查询分别为 3 和 2,词宽分别为
|
||||
6312192 和 6275328 sp,`G` locale 进入实际字体断行。
|
||||
[SIL Padauk 5.100](https://github.com/silnrsi/font-padauk/releases/tag/v5.100)
|
||||
|
||||
### XeTeX Unicode 段落断行
|
||||
|
||||
`\XeTeXlinebreaklocale` 决定实际 OpenType 词的 ICU 行断点。XeTeX
|
||||
|
||||
@@ -758,6 +758,15 @@ XDV 链接注释接入后,沿原样 ACL 的第二轮辅助文件和 BibTeX
|
||||
`target/p3-xdv-annotation/receipt.json`。
|
||||
[链接注释阶段记录](P2_M1_PROGRESS.md#xdv-链接注释与实际矩形)
|
||||
|
||||
#### XeTeX Unicode 物理输入规范化复编
|
||||
|
||||
真实 xelatex.ini 转储后加载 schema77,以未改动 fontspec 读取实际
|
||||
Latin Modern 字体。NFC 规范化后的控制序列名称、盒度量、字形坐标
|
||||
与光栅均与 XeTeX 一致。原样 ACL 以旧 schema71 和已收敛生成初态
|
||||
复编,PDF、aux、out、bbl 与前版逐字节一致,整页门禁保持。
|
||||
回执 `target/p3-xetex-input-normalization/receipt.json` 的 SHA-256 为 `1274d38b084b15d01560745ae44821d8952a311d74d5f793e3c7f42f9dd27794`。
|
||||
[输入规范化阶段记录](P2_M1_PROGRESS.md#xetex-物理输入行的-unicode-规范化)
|
||||
|
||||
#### XeTeX Unicode 段落断行复编
|
||||
|
||||
以同一 schema71、真实字体和已收敛辅助文件复编原样 ACL,PDF、
|
||||
|
||||
Reference in new issue
Block a user