Shape native XeTeX Graphite fonts with actual feature settings

This commit is contained in:
gloamxun committed 2026-11-26 13:27:16 +08:00
1 parent 76df2d6832
commit eb0afccc03
9 files changed
+744 -39

No files matched your search

@@ -1107,6 +1107,9 @@ impl Executor {
let name = self.xetex_glyph_name(files)?;
self.print_tokens(&name, token)?;
}
p @ (XeTeXfeaturename | XeTeXselectorname) => {
self.scan_xetex_graphite_label(p, files)?;
}
Etexrevision => self.print_tokens(".6", token)?,
Csname => {
let name = self.csname(files)?;
@@ -3267,6 +3270,8 @@ impl Executor {
Primitive::XeTeXglyph => "XeTeXglyph".into(),
Primitive::XeTeXglyphbounds => "XeTeXglyphbounds".into(),
Primitive::XeTeXglyphname => "XeTeXglyphname".into(),
Primitive::XeTeXfeaturename => "XeTeXfeaturename".into(),
Primitive::XeTeXselectorname => "XeTeXselectorname".into(),
Primitive::XeTeXinputencoding => "XeTeXinputencoding".into(),
Primitive::XeTeXdefaultencoding => "XeTeXdefaultencoding".into(),
Primitive::XeTeXlinebreaklocale => "XeTeXlinebreaklocale".into(),
@@ -167,6 +167,27 @@ impl Executor {
Ok((id, slot))
}
pub(super) fn scan_xetex_graphite_label(
&mut self, command: Primitive, files: &mut impl SourceProvider,
) -> Result<(), Error> {
let id = self.font_identifier(files)?;
let instance = self.state.fonts.font(id).map_err(|error| self.error(error, self.current))?;
let graphite = match instance.opentype() {
Some(font) => font.graphite_features().map_err(|error| self.error(error, self.current))?.is_some(),
None => false,
};
if !graphite {
return self.fail("XeTeX feature label requires a Graphite font");
}
self.integer(files)?;
if command == Primitive::XeTeXselectorname {
self.integer(files)?;
}
// XeTeX passes its current convert code to grprintfontname. The
// legacy switch handles codes 8 and 9, so these queries emit no tokens.
Ok(())
}
pub(super) fn xetex_font_query(
&mut self,
command: Primitive,
@@ -205,10 +226,55 @@ impl Executor {
};
return Ok(range[usize::from(command == XeTeXlastfontchar)] as i32);
}
let font = font.cloned();
let graphite = match &font {
Some(font) => font.graphite_features().map_err(|error| self.error(error, self.current))?,
None => None,
};
if command == XeTeXfonttype {
return Ok(if font.is_some() { 2 } else { 0 });
return Ok(if graphite.is_some() { 3 } else if font.is_some() { 2 } else { 0 });
}
let Some(font) = font.cloned() else {
if command == XeTeXcountfeatures {
return Ok(graphite.map_or(0, |catalog| catalog.len() as i32));
}
if matches!(command, XeTeXfeaturecode | XeTeXfindfeaturebyname | XeTeXisexclusivefeature
| XeTeXcountselectors | XeTeXselectorcode | XeTeXfindselectorbyname | XeTeXisdefaultselector)
{
let catalog = graphite.ok_or_else(|| self.error("XeTeX feature query requires a Graphite font", self.current))?;
if command == XeTeXfindfeaturebyname {
let name = self.filename(files)?;
return Ok(catalog.iter().find(|feature| feature.name.starts_with(&name)).map_or(-1, |feature| feature.id as i32));
}
let first = self.integer(files)?;
if command == XeTeXfeaturecode {
return Ok(catalog.get(first as usize).map_or(0, |feature| feature.id as i32));
}
if command == XeTeXisexclusivefeature {
return Ok(1);
}
let feature = catalog.iter().find(|feature| feature.id == first as u32);
if command == XeTeXcountselectors {
return Ok(feature.map_or(0, |feature| feature.values.len() as i32));
}
if command == XeTeXfindselectorbyname {
let name = self.filename(files)?;
return Ok(feature.and_then(|feature| feature.values.iter().find(|(_, label)| label.starts_with(&name)))
.map_or(-1, |(value, _)| i32::from(*value as i16)));
}
let second = self.integer(files)?;
if command == XeTeXisdefaultselector {
return Ok(i32::from(feature.is_some_and(|feature| i32::from(feature.default) == second)));
}
return Ok(feature.and_then(|feature| feature.values.get(second as usize))
.map_or(0, |(value, _)| i32::from(*value as i16)));
}
if graphite.is_some() && command != XeTeXcountglyphs {
if command == XeTeXOTcountscripts {
return Ok(0);
}
return self.fail("XeTeX OpenType layout query requires an OpenType layout font");
}
let Some(font) = font else {
return match command {
XeTeXcountglyphs | XeTeXOTcountscripts => Ok(0),
_ => self.fail("XeTeX OpenType layout query requires a native OpenType font"),
@@ -762,7 +762,18 @@ impl Executor {
self.state.fonts.load_tfm(files, &file, &from, size, name.clone(), defaults, false)
};
match font {
Ok(font) => self.define_traced(name, Meaning::Font(font), global, files),
Ok(font) => {
let instance = self.state.fonts.font(font).map_err(|error| self.error(error, self.current))?;
if let Some(native) = instance.opentype() {
for option in native.ignored_graphite_options().map_err(|error| self.error(error, self.current))? {
files.observe_diagnostic(
WriteDestination::Log,
&format!("\nUnknown feature `{option}' in font `{file}'.\n"),
).map_err(|error| self.error(error, self.current))?;
}
}
self.define_traced(name, Meaning::Font(font), global, files)
}
Err(FontLoadError::Rejected(_) | FontLoadError::Missing(_)) if suppress => Ok(()),
Err(error) => Err(self.error(error.message(), self.current)),
}
@@ -8,9 +8,9 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
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 SCHEMA: u32 = 78;
const COMPATIBLE_SCHEMAS: [u32; 45] = [
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, 77, SCHEMA,
];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
+31 -1
View File
@@ -395,10 +395,20 @@ pub enum Primitive {
XeTeXfirstfontchar,
XeTeXlastfontchar,
XeTeXlinebreaklocale,
XeTeXcountfeatures,
XeTeXfeaturecode,
XeTeXfindfeaturebyname,
XeTeXisexclusivefeature,
XeTeXcountselectors,
XeTeXselectorcode,
XeTeXfindselectorbyname,
XeTeXisdefaultselector,
XeTeXfeaturename,
XeTeXselectorname,
}
impl Primitive {
const XETEX_FONT_QUERIES: [Self; 12] = [
const XETEX_FONT_QUERIES: [Self; 20] = [
Self::XeTeXfonttype,
Self::XeTeXcountglyphs,
Self::XeTeXcharglyph,
@@ -411,6 +421,14 @@ impl Primitive {
Self::XeTeXOTlanguagetag,
Self::XeTeXOTcountfeatures,
Self::XeTeXOTfeaturetag,
Self::XeTeXcountfeatures,
Self::XeTeXfeaturecode,
Self::XeTeXfindfeaturebyname,
Self::XeTeXisexclusivefeature,
Self::XeTeXcountselectors,
Self::XeTeXselectorcode,
Self::XeTeXfindselectorbyname,
Self::XeTeXisdefaultselector,
];
pub(super) fn xetex_font_query_name(self) -> Option<&'static str> {
@@ -427,6 +445,14 @@ impl Primitive {
Self::XeTeXOTlanguagetag => "XeTeXOTlanguagetag",
Self::XeTeXOTcountfeatures => "XeTeXOTcountfeatures",
Self::XeTeXOTfeaturetag => "XeTeXOTfeaturetag",
Self::XeTeXcountfeatures => "XeTeXcountfeatures",
Self::XeTeXfeaturecode => "XeTeXfeaturecode",
Self::XeTeXfindfeaturebyname => "XeTeXfindfeaturebyname",
Self::XeTeXisexclusivefeature => "XeTeXisexclusivefeature",
Self::XeTeXcountselectors => "XeTeXcountselectors",
Self::XeTeXselectorcode => "XeTeXselectorcode",
Self::XeTeXfindselectorbyname => "XeTeXfindselectorbyname",
Self::XeTeXisdefaultselector => "XeTeXisdefaultselector",
_ => return None,
})
}
@@ -482,6 +508,8 @@ impl Primitive {
| Pdftexrevision
| XeTeXrevision
| XeTeXglyphname
| XeTeXfeaturename
| XeTeXselectorname
| Pdfstrcmp
| Pdfmatch
| Pdflastmatch
@@ -1747,6 +1775,8 @@ impl State {
("XeTeXglyph", Primitive::XeTeXglyph),
("XeTeXglyphbounds", Primitive::XeTeXglyphbounds),
("XeTeXglyphname", Primitive::XeTeXglyphname),
("XeTeXfeaturename", Primitive::XeTeXfeaturename),
("XeTeXselectorname", Primitive::XeTeXselectorname),
("XeTeXpdfpagecount", Primitive::XeTeXpdfpagecount),
("XeTeXinputencoding", Primitive::XeTeXinputencoding),
("XeTeXdefaultencoding", Primitive::XeTeXdefaultencoding),