Allocate native automatic font expansion instances

This commit is contained in:
gloamxun committed 2026-11-21 08:27:32 +08:00
1 parent 2d036b807e
commit 9b80a782cf
11 files changed
+288 -107

No files matched your search

@@ -67,12 +67,13 @@ impl Limits {
struct Character<'a> {
font: &'a Arc<NativeFont>,
id: FontId,
slot: u8,
width: i64,
}
fn character(node: &TextNode) -> Option<Character<'_>> {
let TextNode::Glyph { glyph, slot, .. } = node else {
let TextNode::Glyph { font: id, glyph, slot, .. } = node else {
return None;
};
let geometry = glyph.math_geometry.as_ref()?;
@@ -81,6 +82,7 @@ fn character(node: &TextNode) -> Option<Character<'_>> {
};
Some(Character {
font,
id: *id,
slot: *slot,
width: glyph.width,
})
@@ -89,7 +91,7 @@ fn character(node: &TextNode) -> Option<Character<'_>> {
fn pair(nodes: &[TextNode], at: usize) -> Option<(Character<'_>, Character<'_>)> {
let left = character(nodes.get(at.checked_sub(1)?)?)?;
let right = character(nodes.get(at + 1)?)?;
(left.font.id == right.font.id && left.font.expansion == 0 && right.font.expansion == 0)
(left.id == right.id && left.font.expansion == 0 && right.font.expansion == 0)
.then_some((left, right))
}
@@ -110,13 +112,10 @@ fn capacity(
if amount == 0 {
return Ok(width);
}
let Some(external) = state.fonts.external_limit(font.id, amount)? else {
return Ok(pdf_device::magnify(width, 1000 + amount));
};
let left = char::from(slot);
let variant = state.fonts.expansion_limit(font.id, amount)?;
Ok(match right {
Some(right) => external.kern(left, char::from(right)),
None => external.width(left),
Some(right) => variant.horizontal(variant.metrics().kern(char::from(slot), char::from(right))),
None => variant.advance(slot),
})
};
let code = state.fonts.expansion_code(font.id, slot);
@@ -264,7 +263,7 @@ pub(super) enum MarginVariation {
impl MarginVariation {
pub(super) fn project(state: &State, font: FontId, slot: u8) -> Result<Option<Self>, String> {
if !state.fonts.has_external_expansion(font) {
if state.fonts.expansion(font).is_none() {
return Ok(None);
}
let face = state.fonts.glyph_font(font)?;
@@ -279,7 +278,7 @@ impl MarginVariation {
if amount == 0 {
return Ok(Some(original));
}
let Some(variant) = state.fonts.external_variant(font, amount) else {
let Some(variant) = state.fonts.expansion_variant(font, amount) else {
return Ok(None);
};
let quad = i64::from(state.fonts.font(variant)?.quad());
@@ -303,14 +302,13 @@ impl MarginVariation {
match *self {
Self::Measured(capacity) => Ok(capacity),
Self::Pending { amounts, code, original } => {
let base = state.fonts.glyph_font(font)?.clone();
let defaults = state.font_defaults();
let mut selected = |amount| -> Result<i64, String> {
if amount == 0 {
return Ok(original);
}
let face = state.fonts.expansion_face(&base, amount, defaults, files)?;
let quad = i64::from(state.fonts.font(face.id)?.quad());
let (selected, _) = state.fonts.expansion_face(font, amount, defaults, files)?;
let quad = i64::from(state.fonts.font(selected)?.quad());
Ok(pdf_device::magnify(quad, code))
};
let capacity = Capacity {
@@ -325,18 +323,18 @@ impl MarginVariation {
}
#[derive(Default)]
struct PackingFonts(BTreeMap<(FontId, i32), Arc<NativeFont>>);
struct PackingFonts(BTreeMap<(FontId, i32), (FontId, Arc<NativeFont>)>);
impl PackingFonts {
fn select(
&mut self,
state: &mut State,
font: &Arc<NativeFont>,
font: FontId,
amount: i32,
files: &mut impl SourceProvider,
) -> Result<Arc<NativeFont>, String> {
) -> Result<(FontId, Arc<NativeFont>), String> {
use std::collections::btree_map::Entry;
let selected = match self.0.entry((font.id, amount)) {
let selected = match self.0.entry((font, amount)) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
let defaults = state.font_defaults();
@@ -346,7 +344,7 @@ impl PackingFonts {
Ok(selected.clone())
}
fn external_margin(
fn margin(
&mut self,
state: &mut State,
font: FontId,
@@ -355,7 +353,7 @@ impl PackingFonts {
ratio: i32,
files: &mut impl SourceProvider,
) -> Result<Option<(i32, FontId)>, String> {
if !state.fonts.has_external_expansion(font) {
if state.fonts.expansion(font).is_none() {
return Ok(None);
}
let base = state.fonts.glyph_font(font)?.clone();
@@ -363,10 +361,10 @@ impl PackingFonts {
if amount == 0 {
return Ok(None);
}
let face = self.select(state, &base, amount, files)?;
let (selected, _) = self.select(state, font, amount, files)?;
let code = state.fonts.protrusion_code(font, slot, right);
let quad = i64::from(state.fonts.font(face.id)?.quad());
Ok(Some((-pdf_device::magnify(quad, code) as i32, face.id)))
let quad = i64::from(state.fonts.font(selected)?.quad());
Ok(Some((-pdf_device::magnify(quad, code) as i32, selected)))
}
}
@@ -380,7 +378,7 @@ pub(super) fn pack(
let shortfall = target - natural;
let mut fonts = PackingFonts::default();
let mut capacity = Capacity::default();
if state.fonts.has_external_expansions() {
if state.fonts.has_expansion() {
// Measuring the actual margin assigns its extreme variants before
// glue setting decides whether to perform any substitution.
for node in material.iter() {
@@ -388,7 +386,7 @@ pub(super) fn pack(
continue;
};
for (ratio, limit) in [(1000, &mut capacity.stretch), (-1000, &mut capacity.shrink)] {
if let Some((expanded, _)) = fonts.external_margin(state, font, slot, right, ratio, files)? {
if let Some((expanded, _)) = fonts.margin(state, font, slot, right, ratio, files)? {
*limit += i64::from(expanded) - i64::from(width);
}
}
@@ -410,7 +408,7 @@ pub(super) fn pack(
pdf_device::divide_scaled(shortfall.clamp(-available, available), available, 3).0 as i32;
for i in 0..material.len() {
if let TextNode::MarginKern { ref mut width, right, ref mut font, slot } = material[i] {
if let Some((expanded, selected)) = fonts.external_margin(state, *font, slot, right, ratio, files)? {
if let Some((expanded, selected)) = fonts.margin(state, *font, slot, right, ratio, files)? {
*width = expanded;
*font = selected;
}
@@ -423,16 +421,13 @@ pub(super) fn pack(
if amount == 0 {
continue;
}
let face = fonts.select(state, glyph.font, amount, files)?;
let (selected, paint) = fonts.select(state, glyph.id, amount, files)?;
let character = char::from(glyph.slot);
let external = face.expansion == 0;
if external && !face.has_slot(glyph.slot) {
return Err(format!("expanded font {} has no TFM slot {}", face.name, glyph.slot));
}
// The external left font has already changed when pdfTeX checks
// the pair for substitution. The two font identities differ, so
// the original kern remains even though the limits affect capacity.
if !external
let metrics = state.fonts.character(selected, glyph.slot)?
.ok_or_else(|| format!("expanded font has no TFM slot {}", glyph.slot))?;
// Reuse the original pair for synthesized kern scaling. A loaded
// TFM can affect capacity without replacing the original pair kern.
if state.fonts.glyph_font(selected)?.expansion != 0
&& let Some(TextNode::Kern { width, explicit: false }) = material.get(i + 1)
&& let Some((left, right)) = pair(material, i + 1)
{
@@ -447,16 +442,11 @@ pub(super) fn pack(
let TextNode::Glyph { font, glyph, .. } = &mut material[i] else {
unreachable!()
};
*font = face.id;
if external {
glyph.width = face.metrics().width(character);
glyph.height = face.metrics().height(character);
glyph.depth = face.metrics().depth(character);
glyph.italic = face.metrics().italic(character);
} else {
glyph.width = pdf_device::magnify(glyph.width, 1000 + amount);
glyph.italic = pdf_device::magnify(glyph.italic, 1000 + amount);
}
*font = selected;
glyph.width = metrics.width;
glyph.height = metrics.height;
glyph.depth = metrics.depth;
glyph.italic = metrics.italic;
let geometry = glyph.math_geometry.as_mut().expect("native character geometry");
geometry.metrics.width = glyph.width;
geometry.metrics.height = glyph.height;
@@ -464,7 +454,7 @@ pub(super) fn pack(
let MathItem::NativeGlyph { font, .. } = &mut geometry.items[0].item else {
unreachable!()
};
*font = face;
*font = paint;
}
Ok(())
}