Use external expanded font metrics in native paragraph layout
This commit is contained in:
1 parent
4ad66c120d
commit
12d9eb6a54
9 files changed
+326
-116
No files matched your search
@@ -1,6 +1,6 @@
|
||||
//! pdfTeX expansion uses each live font's stretch and shrink limits. Paragraph
|
||||
//! search budgets the quantization error; packing applies a font to each glyph.
|
||||
use super::{State, TextNode, metrics};
|
||||
use super::{FontId, SourceProvider, State, TextNode, metrics};
|
||||
use crate::{math_engine_tex::MathItem, native_font::NativeFont, pdf_device};
|
||||
use std::{collections::BTreeMap, ops::AddAssign, sync::Arc};
|
||||
|
||||
@@ -94,21 +94,34 @@ fn capacity(
|
||||
at: usize,
|
||||
limits: Option<&mut Limits>,
|
||||
) -> Result<Capacity, String> {
|
||||
let measure = |font: &NativeFont, slot, width, kern| {
|
||||
let measure = |font: &NativeFont, slot: u8, width, right: Option<u8>| {
|
||||
let Some(config) = state.fonts.expansion(font.id) else {
|
||||
return Capacity::default();
|
||||
return Ok(Capacity::default());
|
||||
};
|
||||
if font.expansion != 0 {
|
||||
return Capacity::default();
|
||||
return Ok(Capacity::default());
|
||||
}
|
||||
let limit_width = |amount| -> Result<i64, String> {
|
||||
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);
|
||||
Ok(match right {
|
||||
Some(right) => external.kern(left, char::from(right)),
|
||||
None => external.width(left),
|
||||
})
|
||||
};
|
||||
let code = state.fonts.expansion_code(font.id, slot);
|
||||
let scale = |value| pdf_device::magnify(value, code);
|
||||
let stretch = pdf_device::magnify(width, 1000 + config.stretch) - width;
|
||||
let shrink = width - pdf_device::magnify(width, 1000 - config.shrink);
|
||||
Capacity {
|
||||
stretch: scale(if kern { stretch } else { stretch.max(0) }),
|
||||
shrink: scale(if kern { shrink } else { shrink.max(0) }),
|
||||
}
|
||||
let scale = |value: i64| {
|
||||
pdf_device::magnify(if right.is_some() { value } else { value.max(0) }, code)
|
||||
};
|
||||
Ok(Capacity {
|
||||
stretch: scale(limit_width(config.stretch)? - width),
|
||||
shrink: scale(width - limit_width(-config.shrink)?),
|
||||
})
|
||||
};
|
||||
if let Some(glyph) = character(&nodes[at]) {
|
||||
if glyph.font.expansion == 0
|
||||
@@ -117,7 +130,7 @@ fn capacity(
|
||||
{
|
||||
limits.include(config)?;
|
||||
}
|
||||
return Ok(measure(glyph.font, glyph.slot, glyph.width, false));
|
||||
return measure(glyph.font, glyph.slot, glyph.width, None);
|
||||
}
|
||||
if let TextNode::Kern {
|
||||
width,
|
||||
@@ -132,7 +145,7 @@ fn capacity(
|
||||
// A boundary kern and a TFM kern between two characters have the same
|
||||
// node type. Only use the font program's actual kerning pair here.
|
||||
if kern == i64::from(width) {
|
||||
return Ok(measure(left.font, left.slot, kern, true));
|
||||
return measure(left.font, left.slot, kern, Some(right.slot));
|
||||
}
|
||||
}
|
||||
Ok(Capacity::default())
|
||||
@@ -213,11 +226,11 @@ impl Search {
|
||||
}
|
||||
}
|
||||
|
||||
fn glyph_ratio(state: &State, glyph: &Character<'_>, line_ratio: i32) -> i32 {
|
||||
let Some(config) = state.fonts.expansion(glyph.font.id) else {
|
||||
fn slot_ratio(state: &State, font: &NativeFont, slot: u8, line_ratio: i32) -> i32 {
|
||||
let Some(config) = state.fonts.expansion(font.id) else {
|
||||
return 0;
|
||||
};
|
||||
if glyph.font.expansion != 0 {
|
||||
if font.expansion != 0 {
|
||||
return 0;
|
||||
}
|
||||
let max = if line_ratio < 0 {
|
||||
@@ -225,19 +238,81 @@ fn glyph_ratio(state: &State, glyph: &Character<'_>, line_ratio: i32) -> i32 {
|
||||
} else {
|
||||
config.stretch
|
||||
};
|
||||
let code = state.fonts.expansion_code(glyph.font.id, glyph.slot);
|
||||
let code = state.fonts.expansion_code(font.id, slot);
|
||||
let magnitude = i64::from(line_ratio.abs()) * i64::from(code) * i64::from(max) / 1_000_000;
|
||||
let step = i64::from(config.step);
|
||||
(magnitude + step / 2).div_euclid(step) as i32 * config.step * line_ratio.signum()
|
||||
}
|
||||
|
||||
pub(super) fn pack(state: &State, material: &mut [TextNode], target: i64) -> Result<(), String> {
|
||||
#[derive(Default)]
|
||||
struct PackingFonts(BTreeMap<(FontId, i32), Arc<NativeFont>>);
|
||||
|
||||
impl PackingFonts {
|
||||
fn select(
|
||||
&mut self,
|
||||
state: &mut State,
|
||||
font: &Arc<NativeFont>,
|
||||
amount: i32,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<Arc<NativeFont>, String> {
|
||||
use std::collections::btree_map::Entry;
|
||||
let selected = match self.0.entry((font.id, amount)) {
|
||||
Entry::Occupied(entry) => entry.into_mut(),
|
||||
Entry::Vacant(entry) => {
|
||||
let defaults = state.font_defaults();
|
||||
entry.insert(state.fonts.expansion_face(font, amount, defaults, files)?)
|
||||
}
|
||||
};
|
||||
Ok(selected.clone())
|
||||
}
|
||||
|
||||
fn external_margin(
|
||||
&mut self,
|
||||
state: &mut State,
|
||||
font: FontId,
|
||||
slot: u8,
|
||||
right: bool,
|
||||
ratio: i32,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<Option<(i32, FontId)>, String> {
|
||||
if !state.fonts.has_external_expansion(font) {
|
||||
return Ok(None);
|
||||
}
|
||||
let base = state.fonts.glyph_font(font)?.clone();
|
||||
let amount = slot_ratio(state, &base, slot, ratio);
|
||||
if amount == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
let face = self.select(state, &base, 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)))
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn pack(
|
||||
state: &mut State,
|
||||
material: &mut [TextNode],
|
||||
target: i64,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), String> {
|
||||
let (natural, stretch, _) = metrics(material, Default::default(), Default::default());
|
||||
let shortfall = target - natural;
|
||||
if shortfall == 0 || (shortfall > 0 && stretch[1..].iter().any(|width| *width != 0)) {
|
||||
return Ok(());
|
||||
}
|
||||
let capacity = sum(state, material, None)?;
|
||||
let mut fonts = PackingFonts::default();
|
||||
let mut capacity = sum(state, material, None)?;
|
||||
for node in material.iter() {
|
||||
let TextNode::MarginKern { width, right, font, slot } = *node else {
|
||||
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)? {
|
||||
*limit += i64::from(expanded) - i64::from(width);
|
||||
}
|
||||
}
|
||||
}
|
||||
let available = if shortfall < 0 {
|
||||
capacity.shrink
|
||||
} else {
|
||||
@@ -248,54 +323,63 @@ pub(super) fn pack(state: &State, material: &mut [TextNode], target: i64) -> Res
|
||||
}
|
||||
let ratio =
|
||||
pdf_device::divide_scaled(shortfall.clamp(-available, available), available, 3).0 as i32;
|
||||
let mut fonts = BTreeMap::new();
|
||||
for i in 0..material.len() {
|
||||
if let TextNode::Kern {
|
||||
width,
|
||||
explicit: false,
|
||||
} = material[i]
|
||||
&& let Some((left, right)) = pair(material, i)
|
||||
&& let kern = left
|
||||
.font
|
||||
.metrics()
|
||||
.kern(char::from(left.slot), char::from(right.slot))
|
||||
&& kern == i64::from(width)
|
||||
{
|
||||
let amount = glyph_ratio(state, &left, ratio);
|
||||
if let TextNode::Kern { width, .. } = &mut material[i] {
|
||||
*width = i32::try_from(pdf_device::magnify(kern, 1000 + amount))
|
||||
.map_err(|_| "expanded native font kern exceeds the scaled point range")?;
|
||||
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)? {
|
||||
*width = expanded;
|
||||
*font = selected;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
for node in material {
|
||||
let Some(glyph) = character(node) else {
|
||||
let Some(glyph) = character(&material[i]) else {
|
||||
continue;
|
||||
};
|
||||
let amount = glyph_ratio(state, &glyph, ratio);
|
||||
let amount = slot_ratio(state, glyph.font, glyph.slot, ratio);
|
||||
if amount == 0 {
|
||||
continue;
|
||||
}
|
||||
let face = fonts.entry((glyph.font.id, amount)).or_insert_with(|| {
|
||||
Arc::new(NativeFont {
|
||||
expansion: amount,
|
||||
..glyph.font.as_ref().clone()
|
||||
})
|
||||
});
|
||||
let TextNode::Glyph { glyph, .. } = node else {
|
||||
let face = fonts.select(state, glyph.font, 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 Some(TextNode::Kern { width, explicit: false }) = material.get(i + 1)
|
||||
&& let Some((left, right)) = pair(material, i + 1)
|
||||
{
|
||||
let original = left.font.metrics().kern(character, char::from(right.slot));
|
||||
if original == i64::from(*width)
|
||||
&& let TextNode::Kern { width, .. } = &mut material[i + 1]
|
||||
{
|
||||
*width = i32::try_from(pdf_device::magnify(original, 1000 + amount))
|
||||
.map_err(|_| "expanded native font kern exceeds the scaled point range")?;
|
||||
}
|
||||
}
|
||||
let TextNode::Glyph { font, glyph, .. } = &mut material[i] else {
|
||||
unreachable!()
|
||||
};
|
||||
glyph.width = pdf_device::magnify(glyph.width, 1000 + amount);
|
||||
glyph.italic = pdf_device::magnify(glyph.italic, 1000 + amount);
|
||||
let geometry = glyph
|
||||
.math_geometry
|
||||
.as_mut()
|
||||
.expect("native character geometry");
|
||||
*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);
|
||||
}
|
||||
let geometry = glyph.math_geometry.as_mut().expect("native character geometry");
|
||||
geometry.metrics.width = glyph.width;
|
||||
geometry.metrics.height = glyph.height;
|
||||
geometry.metrics.depth = glyph.depth;
|
||||
let MathItem::NativeGlyph { font, .. } = &mut geometry.items[0].item else {
|
||||
unreachable!()
|
||||
};
|
||||
*font = face.clone();
|
||||
*font = face;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user