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

@@ -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"),