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>,
@@ -35,6 +35,55 @@ fn messages(result: &Execution) -> Vec<&str> {
.collect()
}
#[test]
fn opentype_design_and_scaled_requests_share_actual_sized_instances() {
use sha2::{Digest, Sha256};
use std::{fs, path::PathBuf, process::Command};
use texrra_tex_core::resources::{LoadedResource, ResourceKind, ResourceVersion};
struct PhysicalFont(LoadedResource);
impl SourceProvider for PhysicalFont {
fn read(&mut self, name: &str, _: &str) -> Result<SourceFile, String> {
Err(format!("unexpected input {name}"))
}
fn resource(&mut self, kind: ResourceKind, name: &str, _: &str) -> Result<LoadedResource, String> {
assert_eq!((kind, name), (ResourceKind::OpenType, "lmroman10-regular.otf"));
Ok(LoadedResource { version: self.0.version.clone(), data: self.0.data.clone() })
}
}
let font = Command::new("kpsewhich").arg("lmroman10-regular.otf").output().unwrap();
assert!(font.status.success(), "missing reference OpenType resource");
let path = PathBuf::from(String::from_utf8(font.stdout).unwrap().trim());
let data = fs::read(&path).unwrap();
let mut provider = PhysicalFont(LoadedResource {
version: ResourceVersion {
path,
sha256: format!("{:x}", Sha256::digest(&data)),
bytes: data.len() as u64,
},
data,
});
let result = Executor::new(
State::initex().with_etex().with_input_profile(InputProfile::XeTeX),
Limits::default(),
)
.run(
source(r#"\catcode123=1 \catcode125=2
\font\design="[lmroman10-regular.otf]"
\font\same="[lmroman10-regular.otf]" scaled1000
\font\exact="[lmroman10-regular.otf]" at657818sp
\font\scaled="[lmroman10-regular.otf]" scaled737
\font\explicit="[lmroman10-regular.otf]" at10pt
\message{\number\fontdimen6\design/\number\fontdimen6\scaled/\number\fontdimen6\explicit}
\message{\ifx\design\same SAME\fi/\ifx\design\exact SAME\fi/\ifx\design\explicit SAME\else DIFFERENT\fi}
\end"#),
&mut provider,
)
.unwrap();
assert_eq!(messages(&result), ["657818/484811/655360", "SAME/SAME/DIFFERENT"]);
}
#[test]
fn xetex_primitives_follow_each_profiles_language_and_device() {
let tex = r"\catcode123=1 \catcode125=2