Render vertical and CFF legacy XeLaTeX composite fonts

This commit is contained in:
gloamxun committed 2026-11-28 22:17:46 +08:00
1 parent bbeeca0317
commit ff52a9f66b
14 files changed
+877 -292

No files matched your search

+57 -6
View File
@@ -1,6 +1,7 @@
//! Resolve an eight-bit TeX font mapped to a name-keyed OpenType CFF face.
use super::{
MappedOutline, NativeFont, NativeFontPaint, NativeFontProgram,
composite::{self, Codes, CompositeFont},
map::{MapEntry, OutlineFiles},
};
use crate::{execution::SourceProvider, resources::ResourceKind};
@@ -11,12 +12,18 @@ use std::{collections::{BTreeMap, BTreeSet}, sync::Arc};
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct OpenTypeCffProgram {
pub(crate) name: String,
data: Arc<[u8]>,
pub(crate) data: Arc<[u8]>,
#[serde(default)]
pub(crate) face_index: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) composite: Option<CompositeFont>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub(crate) composite_glyphs: Vec<u16>,
}
impl OpenTypeCffProgram {
pub(crate) fn face(&self) -> Result<Face<'_>, String> {
Face::parse(&self.data, 0)
Face::parse(&self.data, self.face_index)
.map_err(|error| format!("invalid OpenType font {}: {error}", self.name))
}
}
@@ -57,6 +64,43 @@ pub(super) fn resolve(
font: &NativeFont,
slots: BTreeSet<u8>,
) -> Result<NativeFontPaint, String> {
if let Some(encoding) = &outline.xdv_encoding
&& let Some(codes) = Codes::load(encoding, outline, files)?
{
let mut raw_outline = outline.clone();
raw_outline.encoding = None;
let MappedOutline { data, mut resources, program_index, .. } =
raw_outline.read_outline(files, outlines, ResourceKind::OpenType, file)?;
let face = Face::parse(&data, outline.face_index)
.map_err(|error| format!("invalid OpenType font {file}: {error}"))?;
if face.tables().cff.is_none() {
return Err(format!("OpenType font has no supported CFF1 table: {file}"));
}
let composite::Glyphs { mapping, glyphs, names, glyph_paths } =
composite::resolve(files, outline, &data, &face, slots, encoding, codes, &mut resources)?;
let name = face.names().into_iter()
.filter(|name| name.name_id == rustybuzz::ttf_parser::name_id::POST_SCRIPT_NAME)
.find_map(|name| name.to_string())
.unwrap_or_else(|| outline.postscript_name().unwrap_or(&font.name).to_owned());
let bbox = scaled_bbox(&face);
return Ok(NativeFontPaint {
program: NativeFontProgram::OpenTypeCff(OpenTypeCffProgram {
name, data, face_index: outline.face_index, composite: Some(mapping), composite_glyphs: glyphs,
}),
encoding: names,
glyph_paths,
bbox,
stem_v: 0,
subset: true,
descriptor_flags: None,
xdv_descriptor: None,
xdv_widths: BTreeMap::new(),
xdv_matrix: Default::default(),
xdv_unicode: None,
resources,
program_index: Some(program_index),
});
}
if outline.subset {
return Err(format!("OTF fonts must be included entirely: {file}"));
}
@@ -67,7 +111,7 @@ pub(super) fn resolve(
program_index,
} = outline.read_outline(files, outlines, ResourceKind::OpenType, file)?;
let face =
Face::parse(&data, 0).map_err(|error| format!("invalid OpenType font {file}: {error}"))?;
Face::parse(&data, outline.face_index).map_err(|error| format!("invalid OpenType font {file}: {error}"))?;
let table = face
.raw_face()
.table(Tag::from_bytes(b"CFF "))
@@ -116,12 +160,13 @@ pub(super) fn resolve(
})
.collect()
});
let bbox = face.global_bounding_box();
let bbox = [bbox.x_min, bbox.y_min, bbox.x_max, bbox.y_max]
.map(|value| 1000 * i32::from(value) / i32::from(face.units_per_em()));
let bbox = scaled_bbox(&face);
let program = OpenTypeCffProgram {
name: outline.postscript_name().unwrap_or(&font.name).to_owned(),
data,
face_index: outline.face_index,
composite: None,
composite_glyphs: Vec::new(),
};
Ok(NativeFontPaint {
program: NativeFontProgram::OpenTypeCff(program),
@@ -139,3 +184,9 @@ pub(super) fn resolve(
program_index: Some(program_index),
})
}
fn scaled_bbox(face: &Face<'_>) -> [i32; 4] {
let bbox = face.global_bounding_box();
[bbox.x_min, bbox.y_min, bbox.x_max, bbox.y_max]
.map(|value| 1000 * i32::from(value) / i32::from(face.units_per_em()))
}