Use OpenType design sizes in native XeTeX

This commit is contained in:
gloamxun committed 2026-11-28 12:43:18 +08:00
1 parent b472f66d04
commit 115e1f0fe0
4 files changed
+136 -12

No files matched your search

+11 -12
View File
@@ -308,6 +308,14 @@ impl Hash for OpenTypeFont {
}
}
fn requested_font_size(design_size: i64, size: FontSize) -> i64 {
match size {
FontSize::At(size) => size,
FontSize::Design => design_size,
FontSize::Scaled(ratio) => design_size * i64::from(ratio) / 1000,
}
}
impl OpenTypeFont {
pub(crate) fn new(
id: FontId,
@@ -328,13 +336,8 @@ impl OpenTypeFont {
.filter(|name| name.name_id == ttf_parser::name_id::POST_SCRIPT_NAME)
.find_map(|name| name.to_string())
.ok_or("OpenType face has no PostScript name")?;
// Native file fonts use 10pt unless they declare an optical design size.
let design_size = 10 * 65_536;
let size = match size {
FontSize::At(size) => size,
FontSize::Design => design_size,
FontSize::Scaled(ratio) => design_size * i64::from(ratio) / 1000,
};
let design_size = font_info::design_size(&face);
let size = requested_font_size(design_size, size);
FontSize::At(size).validate()?;
let letter_space = request.letter_space(size);
if !letter_space.is_finite() || letter_space.abs() > 1_073_741_823. {
@@ -399,11 +402,7 @@ impl OpenTypeFont {
}
pub(crate) fn requested_size(&self, size: FontSize) -> i64 {
match size {
FontSize::At(size) => size,
FontSize::Design => self.design_size,
FontSize::Scaled(ratio) => self.design_size * i64::from(ratio) / 1000,
}
requested_font_size(self.design_size, size)
}
pub(crate) fn face(&self) -> Result<ttf_parser::Face<'_>, String> {
@@ -1,5 +1,46 @@
//! XeTeX font inspection from the selected OpenType face.
use super::{Tag, scale_units, ttf_parser};
use read_fonts::{
FontData, FontRead,
tables::{gpos::Gpos, layout::FeatureParams},
};
pub(super) fn design_size(face: &ttf_parser::Face<'_>) -> i64 {
let optical_size = face
.raw_face()
.table(Tag::from_bytes(b"GPOS"))
.and_then(optical_design_decipoints);
optical_size.map_or(10 * 65_536, |size| {
// The GPOS size feature uses tenths of a PostScript point.
// XeTeX converts to TeX points and rounds to a scaled point.
(i64::from(size) * 7_227 * 65_536 + 36_000) / 72_000
})
}
fn optical_design_decipoints(gpos: &[u8]) -> Option<u16> {
let features = Gpos::read(FontData::new(gpos)).ok()?.feature_list().ok()?;
features
.feature_records()
.iter()
.filter(|record| record.feature_tag() == read_fonts::types::Tag::new(b"size"))
.find_map(|record| {
let feature = record.feature(features.offset_data()).ok()?;
if feature.lookup_list_indices().len() != usize::from(feature.lookup_index_count()) {
return None;
}
let FeatureParams::Size(params) = feature.feature_params()?.ok()? else {
return None;
};
let size = params.design_size();
let family = params.identifier();
let name = params.name_entry();
let start = params.range_start();
let end = params.range_end();
let design_only = [family, name, start, end] == [0; 4];
let valid_range = (start..=end).contains(&size) && (256..=32767).contains(&name);
(size != 0 && (design_only || valid_range)).then_some(size)
})
}
pub(crate) struct LayoutTags<'a> {
tables: &'a ttf_parser::FaceTables<'a>,