Implement native letterspaced fonts and virtual glyph placement

This commit is contained in:
gloamxun committed 2026-11-21 05:48:44 +08:00
1 parent 6675ab639a
commit f66a5a7e20
13 files changed
+337 -35

No files matched your search

+58 -3
View File
@@ -63,6 +63,9 @@ pub struct TfmMetrics {
ec: u16,
char_info: Vec<CharInfo>,
widths: Vec<i32>,
// The virtual font changes the already scaled widths by a uniform amount.
#[serde(default, skip_serializing_if = "Option::is_none")]
letter_spacing: Option<LetterspaceMetrics>,
heights: Vec<i32>,
depths: Vec<i32>,
italics: Vec<i32>,
@@ -73,6 +76,12 @@ pub struct TfmMetrics {
boundary_char: Option<u8>,
boundary_label: Option<usize>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
struct LetterspaceMetrics {
width_delta: i64,
quad: i64,
}
impl TfmMetrics {
pub fn parse(bytes: &[u8]) -> Result<Self, String> {
Self::parse_at(bytes, FontSize::Design)
@@ -186,6 +195,7 @@ impl TfmMetrics {
ec: ec as u16,
char_info,
widths,
letter_spacing: None,
heights,
depths,
italics,
@@ -304,6 +314,11 @@ impl TfmMetrics {
if a != 0 && a != 255 {
return Err("TFM metric must be between -16 and 16".into());
}
let (z, alpha, beta) = Self::fix_word_scale(size);
let n = ((i64::from(d) * z / 256 + i64::from(c) * z) / 256 + i64::from(b) * z) / beta;
Ok(if a == 0 { n } else { n - alpha })
}
fn fix_word_scale(size: i64) -> (i64, i64, i64) {
let (mut z, mut alpha) = (size, 16i64);
while z >= 1 << 23 {
z /= 2;
@@ -311,9 +326,25 @@ impl TfmMetrics {
}
let beta = 256 / alpha;
let alpha = alpha * z;
let n = ((i64::from(d) * z / 256 + i64::from(c) * z) / 256 + i64::from(b) * z) / beta;
Ok(if a == 0 { n } else { n - alpha })
(z, alpha, beta)
}
/// Match the VF packet encoder before decoding with TeX's fixed-point scale.
pub(crate) fn encode_vf_move(movement: i64, size: i64) -> Result<i32, String> {
FontSize::At(size).validate()?;
let (z, alpha, beta) = Self::fix_word_scale(size);
let (prefix, magnitude) = if movement < 0 {
(0xff00_0000, alpha + movement)
} else {
(0, movement)
};
let fraction = magnitude * beta * 65_536 / z;
if !(0..1 << 24).contains(&fraction) {
return Err("letterspaced font movement is outside VF fixed-point range".into());
}
Ok((prefix | fraction as u32) as i32)
}
pub fn at_size(&self, requested: FontSize) -> Result<Self, String> {
let mut font = self.clone();
font.size = Self::resolve_size(self.design_size, requested)?;
@@ -355,7 +386,28 @@ impl TfmMetrics {
})
}
pub fn width(&self, c: char) -> i64 {
self.dimension(c, &self.widths, |i| i.width_index)
let Some(info) = self.char_info(c).filter(|info| info.width_index != 0) else {
return 0;
};
self.scaled(self.widths[usize::from(info.width_index)])
.expect("validated TFM metric")
+ self.letter_spacing.as_ref().map_or(0, |metrics| metrics.width_delta)
}
pub(crate) fn set_letter_spacing(
&mut self,
width_delta: i64,
quad: i64,
no_ligatures: bool,
) {
self.letter_spacing = Some(LetterspaceMetrics { width_delta, quad });
if no_ligatures {
for info in &mut self.char_info {
if info.width_index != 0 && info.tag == 1 {
info.tag = 0;
}
}
}
}
pub fn height(&self, c: char) -> i64 {
self.dimension(c, &self.heights, |i| i.height_index)
@@ -371,6 +423,9 @@ impl TfmMetrics {
}
/// Zero-based fontdimen. Slant is 16.16 and independent of the font size.
pub fn param(&self, index: usize) -> i64 {
if index == 5 && let Some(metrics) = &self.letter_spacing {
return metrics.quad;
}
self.params.get(index).map_or(0, |raw| {
if index == 0 {
i64::from(*raw).div_euclid(16)