Query actual XeTeX glyph bounds and names in native jobs
This commit is contained in:
1 parent
b88283f493
commit
fc6d704edf
8 files changed
+109
-6
No files matched your search
@@ -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(),
|
||||
|
||||
@@ -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<i32, Error> {
|
||||
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<String, Error> {
|
||||
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,
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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"));
|
||||
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user