Use live font choices for pdfTeX packed kerns
This commit is contained in:
1 parent
9e13cca481
commit
02d4c3116a
3 files changed
+89
-53
No files matched your search
@@ -95,38 +95,45 @@ fn pair(nodes: &[TextNode], at: usize) -> Option<(Character<'_>, Character<'_>)>
|
||||
.then_some((left, right))
|
||||
}
|
||||
|
||||
fn measure(
|
||||
state: &State,
|
||||
font: &NativeFont,
|
||||
slot: u8,
|
||||
width: i64,
|
||||
right: Option<u8>,
|
||||
) -> Result<Capacity, String> {
|
||||
let Some(config) = state.fonts.expansion(font.id) else {
|
||||
return Ok(Capacity::default());
|
||||
};
|
||||
if font.expansion != 0 {
|
||||
return Ok(Capacity::default());
|
||||
}
|
||||
let limit_width = |amount| -> Result<i64, String> {
|
||||
if amount == 0 {
|
||||
return Ok(width);
|
||||
}
|
||||
let variant = state.fonts.expansion_limit(font.id, amount)?;
|
||||
Ok(match right {
|
||||
Some(right) => variant.metrics().kern(char::from(slot), char::from(right)),
|
||||
None => variant.advance(slot),
|
||||
})
|
||||
};
|
||||
let code = state.fonts.expansion_code(font.id, slot);
|
||||
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)?),
|
||||
})
|
||||
}
|
||||
|
||||
fn capacity(
|
||||
state: &State,
|
||||
nodes: &[TextNode],
|
||||
at: usize,
|
||||
limits: Option<&mut Limits>,
|
||||
) -> Result<Capacity, String> {
|
||||
let measure = |font: &NativeFont, slot: u8, width, right: Option<u8>| {
|
||||
let Some(config) = state.fonts.expansion(font.id) else {
|
||||
return Ok(Capacity::default());
|
||||
};
|
||||
if font.expansion != 0 {
|
||||
return Ok(Capacity::default());
|
||||
}
|
||||
let limit_width = |amount| -> Result<i64, String> {
|
||||
if amount == 0 {
|
||||
return Ok(width);
|
||||
}
|
||||
let variant = state.fonts.expansion_limit(font.id, amount)?;
|
||||
Ok(match right {
|
||||
Some(right) => variant.metrics().kern(char::from(slot), char::from(right)),
|
||||
None => variant.advance(slot),
|
||||
})
|
||||
};
|
||||
let code = state.fonts.expansion_code(font.id, slot);
|
||||
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
|
||||
&& let Some(limits) = limits
|
||||
@@ -134,7 +141,7 @@ fn capacity(
|
||||
{
|
||||
limits.include(config)?;
|
||||
}
|
||||
return measure(glyph.font, glyph.slot, glyph.width, None);
|
||||
return measure(state, glyph.font, glyph.slot, glyph.width, None);
|
||||
}
|
||||
if let TextNode::Kern {
|
||||
width,
|
||||
@@ -142,15 +149,7 @@ fn capacity(
|
||||
} = nodes[at]
|
||||
&& let Some((left, right)) = pair(nodes, at)
|
||||
{
|
||||
let kern = left
|
||||
.font
|
||||
.metrics()
|
||||
.kern(char::from(left.slot), char::from(right.slot));
|
||||
// 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 measure(left.font, left.slot, kern, Some(right.slot));
|
||||
}
|
||||
return measure(state, left.font, left.slot, i64::from(width), Some(right.slot));
|
||||
}
|
||||
Ok(Capacity::default())
|
||||
}
|
||||
@@ -420,6 +419,21 @@ pub(super) fn pack(
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let TextNode::Kern { width, explicit: false } = material[i] {
|
||||
// The left glyph has already been visited, while the right one
|
||||
// still carries its original font. Check those live font choices.
|
||||
if let Some((left, right)) = pair(material, i) {
|
||||
let capacity = measure(state, left.font, left.slot, i64::from(width), Some(right.slot))?;
|
||||
let variation = if ratio < 0 { capacity.shrink } else { capacity.stretch };
|
||||
if variation != 0 && ratio != 0 {
|
||||
let kern = left.font.metrics().kern(char::from(left.slot), char::from(right.slot));
|
||||
let width = i32::try_from(kern)
|
||||
.map_err(|_| "native font kern exceeds the scaled point range")?;
|
||||
material[i] = TextNode::Kern { width, explicit: false };
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let Some(glyph) = character(&material[i]) else {
|
||||
continue;
|
||||
};
|
||||
@@ -428,26 +442,9 @@ pub(super) fn pack(
|
||||
continue;
|
||||
}
|
||||
let selected = fonts.select(state, glyph.id, amount, files)?;
|
||||
let character = char::from(glyph.slot);
|
||||
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.
|
||||
let face = state.fonts.glyph_font(selected)?;
|
||||
if state.fonts.shares_font_parameters(selected)
|
||||
&& !state.fonts.has_external_expansion(glyph.id)
|
||||
&& let Some(TextNode::Kern { width, explicit: false }) = material.get(i + 1)
|
||||
&& let Some((left, right)) = pair(material, i + 1)
|
||||
{
|
||||
let next = char::from(right.slot);
|
||||
let original = left.font.metrics().kern(character, next);
|
||||
if original == i64::from(*width)
|
||||
&& let TextNode::Kern { width, .. } = &mut material[i + 1]
|
||||
{
|
||||
*width = i32::try_from(face.metrics().kern(character, next))
|
||||
.map_err(|_| "expanded native font kern exceeds the scaled point range")?;
|
||||
}
|
||||
}
|
||||
let TextNode::Glyph { font, glyph, .. } = &mut material[i] else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user