Select XeTeX OpenType vertical metrics from the font tables

This commit is contained in:
gloamxun committed 2026-11-25 21:52:56 +08:00
1 parent c97e0e7279
commit 2b82172bfb
3 files changed
+73 -3

No files matched your search

+29 -3
View File
@@ -228,9 +228,7 @@ impl OpenTypeFont {
};
FontSize::At(size).validate()?;
let scale = |value| scale_units(value, size, face.units_per_em());
let hhea = face.tables().hhea;
let ascent = scale(i64::from(hhea.ascender));
let descent = -scale(i64::from(hhea.descender));
let [ascent, descent] = sized_face_metrics(&face, size);
let xheight = |reported: Option<i16>, letter: char, fallback: i64| {
reported.map_or_else(
|| {
@@ -488,6 +486,34 @@ fn points_to_sp(points: f32) -> i64 {
(f64::from(points) * 65536. + 0.5) as i64
}
fn sized_face_metrics(face: &ttf_parser::Face<'_>, size: i64) -> [i64; 2] {
let hhea = face.tables().hhea;
let horizontal = (hhea.ascender, hhea.descender);
let (ascent, descent) = match face.raw_face().table(Tag::from_bytes(b"OS/2")) {
// FreeType checks the stored bit, including in versions before the
// OpenType specification assigned USE_TYPO_METRICS to it.
Some(table) if table.len() >= 78 && table[..2] != [0xff, 0xff] => {
let field = |offset| i16::from_be_bytes([table[offset], table[offset + 1]]);
let typographic = (field(68), field(70));
if field(62) & 128 != 0 {
typographic
} else if horizontal != (0, 0) {
horizontal
} else if typographic != (0, 0) {
typographic
} else {
(field(74), field(76).wrapping_neg())
}
}
_ => horizontal,
};
let units = face.units_per_em();
[
scale_units(i64::from(ascent), size, units),
-scale_units(i64::from(descent), size, units),
]
}
pub(crate) fn scale_units(value: i64, size: i64, units: u16) -> i64 {
let points = (value as f32 * (size as f32 / 65536.)) / f32::from(units);
// XeTeX converts the float point position by adding 0.5 and truncating.