Read XeTeX font character ranges from active charmaps
This commit is contained in:
1 parent
fc6d704edf
commit
4e16531a62
9 files changed
+4672
-8
No files matched your search
@@ -196,12 +196,19 @@ impl Executor {
|
||||
}
|
||||
|
||||
let id = self.font_identifier(files)?;
|
||||
let font = self.state.fonts.font(id).map_err(|error| self.error(error, self.current))?
|
||||
.opentype().cloned();
|
||||
let instance = self.state.fonts.font(id).map_err(|error| self.error(error, self.current))?;
|
||||
let font = instance.opentype();
|
||||
if matches!(command, XeTeXfirstfontchar | XeTeXlastfontchar) {
|
||||
let range = match font {
|
||||
Some(font) => font.character_range().map_err(|error| self.error(error, self.current))?,
|
||||
None => instance.metrics().map_or([1, 0], crate::tfm::TfmMetrics::character_range),
|
||||
};
|
||||
return Ok(range[usize::from(command == XeTeXlastfontchar)] as i32);
|
||||
}
|
||||
if command == XeTeXfonttype {
|
||||
return Ok(if font.is_some() { 2 } else { 0 });
|
||||
}
|
||||
let Some(font) = font else {
|
||||
let Some(font) = font.cloned() else {
|
||||
return match command {
|
||||
XeTeXcountglyphs | XeTeXOTcountscripts => Ok(0),
|
||||
_ => self.fail("XeTeX OpenType layout query requires a native OpenType font"),
|
||||
|
||||
@@ -8,9 +8,9 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
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 SCHEMA: u32 = 75;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 42] = [
|
||||
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, SCHEMA,
|
||||
];
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
|
||||
@@ -392,14 +392,18 @@ pub enum Primitive {
|
||||
XeTeXglyph,
|
||||
XeTeXglyphbounds,
|
||||
XeTeXglyphname,
|
||||
XeTeXfirstfontchar,
|
||||
XeTeXlastfontchar,
|
||||
}
|
||||
|
||||
impl Primitive {
|
||||
const XETEX_FONT_QUERIES: [Self; 10] = [
|
||||
const XETEX_FONT_QUERIES: [Self; 12] = [
|
||||
Self::XeTeXfonttype,
|
||||
Self::XeTeXcountglyphs,
|
||||
Self::XeTeXcharglyph,
|
||||
Self::XeTeXglyphindex,
|
||||
Self::XeTeXfirstfontchar,
|
||||
Self::XeTeXlastfontchar,
|
||||
Self::XeTeXOTcountscripts,
|
||||
Self::XeTeXOTscripttag,
|
||||
Self::XeTeXOTcountlanguages,
|
||||
@@ -414,6 +418,8 @@ impl Primitive {
|
||||
Self::XeTeXcountglyphs => "XeTeXcountglyphs",
|
||||
Self::XeTeXcharglyph => "XeTeXcharglyph",
|
||||
Self::XeTeXglyphindex => "XeTeXglyphindex",
|
||||
Self::XeTeXfirstfontchar => "XeTeXfirstfontchar",
|
||||
Self::XeTeXlastfontchar => "XeTeXlastfontchar",
|
||||
Self::XeTeXOTcountscripts => "XeTeXOTcountscripts",
|
||||
Self::XeTeXOTscripttag => "XeTeXOTscripttag",
|
||||
Self::XeTeXOTcountlanguages => "XeTeXOTcountlanguages",
|
||||
|
||||
@@ -13,10 +13,11 @@ use rustybuzz::{
|
||||
use std::{
|
||||
hash::{Hash, Hasher},
|
||||
path::Path,
|
||||
sync::Arc,
|
||||
sync::{Arc, OnceLock},
|
||||
};
|
||||
use unicode_bidi::BidiInfo;
|
||||
|
||||
mod character_range;
|
||||
mod font_info;
|
||||
mod tags;
|
||||
pub(crate) use font_info::LayoutTags;
|
||||
@@ -264,6 +265,8 @@ pub struct OpenTypeFont {
|
||||
request: Request,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
mapping: Option<Arc<FontMapping>>,
|
||||
#[serde(skip)]
|
||||
character_range: OnceLock<Result<[u32; 2], String>>,
|
||||
}
|
||||
|
||||
impl PartialEq for OpenTypeFont {
|
||||
@@ -358,6 +361,7 @@ impl OpenTypeFont {
|
||||
descent,
|
||||
request,
|
||||
mapping,
|
||||
character_range: OnceLock::new(),
|
||||
});
|
||||
// The unmapped space can itself undergo OpenType substitution.
|
||||
// XeTeX adds tracking to its glue even for a single shaped glyph.
|
||||
@@ -389,6 +393,12 @@ impl OpenTypeFont {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn character_range(&self) -> Result<[u32; 2], String> {
|
||||
self.character_range
|
||||
.get_or_init(|| character_range::of(&self.face()?))
|
||||
.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn mapping(&self) -> Option<&Arc<FontMapping>> {
|
||||
self.mapping.as_ref()
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,155 @@
|
||||
//! Character bounds of the Unicode charmap selected by XeTeX's FreeType face.
|
||||
use read_fonts::{
|
||||
FontData, FontRead,
|
||||
tables::cmap::{Cmap, CmapSubtable, PlatformId, SequentialMapGroup},
|
||||
};
|
||||
use rustybuzz::ttf_parser::{Face, GlyphId, Tag};
|
||||
use std::{collections::BTreeMap, sync::OnceLock};
|
||||
|
||||
pub(super) fn of(face: &Face<'_>) -> Result<[u32; 2], String> {
|
||||
let cmap = face.raw_face().table(Tag::from_bytes(b"cmap"))
|
||||
.and_then(|data| Cmap::read(FontData::new(data)).ok());
|
||||
let mut has_symbol = false;
|
||||
let mut has_cff_unicode = false;
|
||||
let selected = cmap.iter().flat_map(|cmap| {
|
||||
cmap.encoding_records().iter().enumerate().filter_map(|(index, record)| {
|
||||
let table = record.subtable(cmap.offset_data()).ok()?;
|
||||
(!matches!(table, CmapSubtable::Format15(_))).then_some((index, record, table))
|
||||
})
|
||||
}).filter_map(|(index, record, table)| {
|
||||
let encoding = (record.platform_id(), record.encoding_id());
|
||||
has_cff_unicode |= matches!(encoding, (PlatformId::Unicode, _) | (PlatformId::Windows, 1));
|
||||
let priority = match encoding {
|
||||
(PlatformId::Windows, 10) | (PlatformId::Unicode, 4) => 2,
|
||||
(PlatformId::Unicode, 6) if matches!(table, CmapSubtable::Format13(_)) => 2,
|
||||
(PlatformId::Unicode | PlatformId::ISO, _) | (PlatformId::Windows, 1) => 1,
|
||||
(PlatformId::Windows, 0) => {
|
||||
has_symbol = true;
|
||||
return None;
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
Some((priority, index, table))
|
||||
}).max_by_key(|(priority, index, _)| (*priority, *index));
|
||||
let glyph_count = face.number_of_glyphs();
|
||||
// The CFF driver can append a Unicode charmap derived from PostScript
|
||||
// names. A pre-existing full-repertoire map still has priority.
|
||||
if !has_cff_unicode
|
||||
&& selected.as_ref().is_none_or(|(priority, _, _)| *priority < 2)
|
||||
&& let Some(cff) = face.tables().cff
|
||||
&& let Some(bounds) = synthesized(glyph_count, |glyph| cff.glyph_name(glyph))
|
||||
{
|
||||
return Ok(bounds);
|
||||
}
|
||||
let Some((_, index, table)) = selected else {
|
||||
return Ok(if has_symbol {
|
||||
[0, 0]
|
||||
} else {
|
||||
synthesized(glyph_count, |glyph| face.glyph_name(glyph)).unwrap_or([0, 0])
|
||||
});
|
||||
};
|
||||
|
||||
let glyph_count = u32::from(glyph_count);
|
||||
let mut bounds = [u32::MAX, 0];
|
||||
let mut include = |first: u32, last: u32| {
|
||||
bounds[0] = bounds[0].min(first);
|
||||
bounds[1] = bounds[1].max(last);
|
||||
};
|
||||
// Reduce grouped mappings by their endpoints. A constant group can
|
||||
// represent a wide range without requiring a walk over every code.
|
||||
match table {
|
||||
CmapSubtable::Format8(table) => sequential(table.groups(), glyph_count, &mut include),
|
||||
CmapSubtable::Format12(table) => sequential(table.groups(), glyph_count, &mut include),
|
||||
CmapSubtable::Format13(table) => {
|
||||
for group in table.groups() {
|
||||
let first = group.start_char_code();
|
||||
let last = group.end_char_code();
|
||||
if first <= last && (1..glyph_count).contains(&group.glyph_id()) {
|
||||
include(first, last);
|
||||
}
|
||||
}
|
||||
}
|
||||
CmapSubtable::Format2(_) => {
|
||||
let subtable = face.tables().cmap
|
||||
.and_then(|cmap| cmap.subtables.get(index as u16))
|
||||
.ok_or("cannot read the selected OpenType format 2 charmap")?;
|
||||
subtable.codepoints(|code| {
|
||||
if subtable.glyph_index(code).is_some_and(|glyph| (1..glyph_count).contains(&u32::from(glyph.0))) {
|
||||
include(code, code);
|
||||
}
|
||||
});
|
||||
}
|
||||
_ => {
|
||||
for (code, glyph) in table.iter() {
|
||||
if (1..glyph_count).contains(&glyph.to_u32()) {
|
||||
include(code, code);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(if bounds[0] > bounds[1] { [0, 0] } else { bounds })
|
||||
}
|
||||
|
||||
fn synthesized<'a>(glyph_count: u16, name: impl Fn(GlyphId) -> Option<&'a str>) -> Option<[u32; 2]> {
|
||||
// Additional Unicode representations used by FreeType's glyph-name cmap.
|
||||
const ALIASES: [(&str, u32); 10] = [
|
||||
("Delta", 0x0394), ("Omega", 0x03A9), ("fraction", 0x2215),
|
||||
("hyphen", 0x00AD), ("macron", 0x02C9), ("mu", 0x03BC),
|
||||
("periodcentered", 0x2219), ("space", 0x00A0),
|
||||
("Tcommaaccent", 0x021A), ("tcommaaccent", 0x021B),
|
||||
];
|
||||
let mut first = u32::MAX;
|
||||
let mut last = 0;
|
||||
for glyph in (1..glyph_count).filter_map(|id| name(GlyphId(id))) {
|
||||
let primary = name_code(glyph);
|
||||
let alias = ALIASES.iter().find(|(label, _)| *label == glyph).map(|(_, code)| *code);
|
||||
for code in [primary, alias].into_iter().flatten().filter(|code| *code != 0) {
|
||||
first = first.min(code);
|
||||
last = last.max(code);
|
||||
}
|
||||
}
|
||||
(first <= last).then_some([first, last])
|
||||
}
|
||||
|
||||
fn name_code(name: &str) -> Option<u32> {
|
||||
let base = name.split('.').next()?;
|
||||
let encoded = if let Some(hex) = base.strip_prefix("uni") {
|
||||
(hex.len() == 4).then_some(hex)
|
||||
} else {
|
||||
base.strip_prefix('u').filter(|hex| (4..=6).contains(&hex.len()))
|
||||
};
|
||||
if let Some(hex) = encoded
|
||||
&& hex.bytes().all(|byte| byte.is_ascii_digit() || (b'A'..=b'F').contains(&byte))
|
||||
{
|
||||
return u32::from_str_radix(hex, 16).ok();
|
||||
}
|
||||
|
||||
static GLYPH_LIST: OnceLock<BTreeMap<&str, u32>> = OnceLock::new();
|
||||
let dictionary = GLYPH_LIST.get_or_init(|| {
|
||||
include_str!("adobe_glyphs.txt").lines().filter_map(|line| {
|
||||
let (name, value) = line.split_once(';')?;
|
||||
Some((name, u32::from_str_radix(value, 16).ok()?))
|
||||
}).collect()
|
||||
});
|
||||
dictionary.get(base).copied()
|
||||
}
|
||||
|
||||
fn sequential(
|
||||
groups: &[SequentialMapGroup],
|
||||
glyph_count: u32,
|
||||
include: &mut impl FnMut(u32, u32),
|
||||
) {
|
||||
for group in groups {
|
||||
let first_code = group.start_char_code();
|
||||
let last_code = group.end_char_code();
|
||||
let first_glyph = group.start_glyph_id();
|
||||
if first_code > last_code || first_glyph >= glyph_count {
|
||||
continue;
|
||||
}
|
||||
let first_offset = u32::from(first_glyph == 0);
|
||||
let last_offset = (last_code - first_code).min(glyph_count - 1 - first_glyph);
|
||||
if first_offset <= last_offset {
|
||||
include(first_code + first_offset, first_code + last_offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -370,6 +370,14 @@ impl TfmMetrics {
|
||||
pub fn design_size_pt(&self) -> f64 {
|
||||
self.design_size as f64 / 65536.0
|
||||
}
|
||||
pub(crate) fn character_range(&self) -> [u32; 2] {
|
||||
// TeX normalizes the empty range with bc = 256 on load.
|
||||
if self.bc == 256 {
|
||||
[1, 0]
|
||||
} else {
|
||||
[u32::from(self.bc), u32::from(self.ec)]
|
||||
}
|
||||
}
|
||||
pub fn char_info(&self, c: char) -> Option<CharInfo> {
|
||||
let c = c as u32;
|
||||
if c < u32::from(self.bc) || c > u32::from(self.ec) {
|
||||
|
||||
@@ -2175,6 +2175,36 @@ PDF,恢复实际 PFB 并反转斜切时复用 1277 步,只替换第二页。
|
||||
全量编译一致。incremental-receipt.json 的 SHA-256 为
|
||||
bbc0991b0316e6d76aa58649ec873525f5544ab530d93fe491fc6e16bd00a27d。
|
||||
|
||||
### XeTeX 字体首尾字符
|
||||
|
||||
`\XeTeXfirstfontchar` 和 `\XeTeXlastfontchar` 接入真实字体的整数扫描。
|
||||
OpenType 查询按 FreeType 的顺序选择 Unicode 映射,过滤零字形和
|
||||
超出字体字形表的映射。分组表按有效端点归约,结果随实际字体实例缓存。
|
||||
缺少 Unicode 表时依据字体内的 PostScript 名称与 Adobe Glyph List
|
||||
合成范围。两份 Adobe 原表的 4401 条单字符映射与 FreeType 2.14.3
|
||||
使用的数据逐项相同,随数据保留原许可。TFM 读取实际边界,nullfont
|
||||
返回 1 和 0。
|
||||
[FreeType 的映射选择](https://github.com/freetype/freetype/blob/VER-2-14-3/src/base/ftobjs.c#L1372-L1446)
|
||||
[Adobe Glyph List](https://github.com/adobe-type-tools/agl-aglfn/tree/4036a9ca80a62f64f9de4f7321a9a045ad0ecfd6)
|
||||
|
||||
由原样 xelatex.ini 转储并加载 schema75。原版 CFF、TrueType、TFM、
|
||||
nullfont、派生映射以及真实 fontspec 的结果一致,共 4 页、
|
||||
4 个字形和 3 条规则。查询控制的出页、原始字体和坐标、
|
||||
文本与 144 DPI RGB 光栅均核对通过。原样 ACL 以原 schema71 复编,
|
||||
PDF、aux、out、bbl 与前版逐字节一致。12 项定向回归通过。
|
||||
|
||||
辅助文件已经生成后,三次同路径替换字体文件各复用 66 步、
|
||||
重放两次调用,只更新第二页。实际字形位置和 aux 由新字体范围
|
||||
决定。以每次编辑之前的生成初态独立全量编译,PDF、SVG、来源映射
|
||||
和实际 aux 一致。首次由无到有的 aux 触发既有存在性守卫,实际全量
|
||||
输出与独立全量一致,保留失效记录。
|
||||
回执 `target/p3-xetex-font-char-range/receipt.json` 的 SHA-256 为
|
||||
`282052a94a208051ceaad86ff1f6f10a7851991c7844927592ffdbe3e6c21e26`。
|
||||
|
||||
下一入口为 `\XeTeXlinebreaklocale` 及其胶和罚值。
|
||||
`target/p3-xetex-line-break-locale` 保留同一中文字体和源的
|
||||
默认断行与 zh locale 两页原版输出、盒度量及原生首处诊断。
|
||||
|
||||
### XeTeX 字形边距与名称查询
|
||||
|
||||
`\XeTeXglyphbounds` 读取当前字体的真实轮廓控制框和字形前进量,
|
||||
|
||||
@@ -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 转储 schema75,
|
||||
真实 fontspec 显式读取 CFF 和 TrueType 范围,查询参与条件和实际发页。
|
||||
两页的字形位置、规则、文本和光栅与参考一致。ACL 整页门禁保持。
|
||||
回执 `target/p3-xetex-font-char-range/receipt.json` 的 SHA-256 为
|
||||
`282052a94a208051ceaad86ff1f6f10a7851991c7844927592ffdbe3e6c21e26`。
|
||||
[字体范围阶段记录](P2_M1_PROGRESS.md#xetex-字体首尾字符)
|
||||
|
||||
#### XeTeX 字形边距与名称复编
|
||||
|
||||
同一 schema71、真实字体及已收敛的辅助文件原样复编 ACL。
|
||||
|
||||
Reference in new issue
Block a user