Apply CMR10 kern pairs in route-D paragraphs
This commit is contained in:
1 parent
a09398811f
commit
867bace1c9
4 files changed
+99
-5
No files matched your search
@@ -19,6 +19,8 @@ pub struct CharInfo {
|
||||
pub depth_index: u8,
|
||||
pub italic_index: u8,
|
||||
pub tag: u8,
|
||||
/// Start offset into the lig/kern instruction table (tag 1).
|
||||
pub lig_kern_start: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -32,6 +34,7 @@ pub struct TfmMetrics {
|
||||
depths: Vec<i32>,
|
||||
italics: Vec<i32>,
|
||||
params: Vec<i32>,
|
||||
kerns: std::collections::HashMap<(char, char), i64>,
|
||||
}
|
||||
|
||||
impl TfmMetrics {
|
||||
@@ -98,8 +101,9 @@ impl TfmMetrics {
|
||||
width_index: bytes[offset],
|
||||
height_index: bytes[offset + 1] >> 4,
|
||||
depth_index: bytes[offset + 1] & 0x0f,
|
||||
italic_index: bytes[offset + 2],
|
||||
tag: bytes[offset + 3],
|
||||
italic_index: bytes[offset + 2] >> 2,
|
||||
tag: bytes[offset + 2] & 0x03,
|
||||
lig_kern_start: usize::from(bytes[offset + 3]),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -131,6 +135,16 @@ impl TfmMetrics {
|
||||
let depths = table(depth_offset, nd, "depth")?;
|
||||
let italics = table(italic_offset, ni, "italic")?;
|
||||
let params = table(params_offset, np, "parameter")?;
|
||||
let kern_table = table(kern_offset, nk, "kern")?;
|
||||
let kerns = parse_kerns(
|
||||
bytes,
|
||||
lig_kern_offset,
|
||||
usize::from(nl),
|
||||
&kern_table,
|
||||
&char_info,
|
||||
u32::from(bc),
|
||||
design_size_pt,
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
design_size_pt,
|
||||
@@ -142,6 +156,7 @@ impl TfmMetrics {
|
||||
depths,
|
||||
italics,
|
||||
params,
|
||||
kerns,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -158,6 +173,11 @@ impl TfmMetrics {
|
||||
(f64::from(raw) * self.design_size_pt / 16.0).round() as i64
|
||||
}
|
||||
|
||||
/// Kerning between two characters, in scaled points.
|
||||
pub fn kern(&self, left: char, right: char) -> i64 {
|
||||
self.kerns.get(&(left, right)).copied().unwrap_or(0)
|
||||
}
|
||||
|
||||
fn index_for(&self, character: char) -> Option<usize> {
|
||||
let code = u32::from(character);
|
||||
if code < u32::from(self.bc) || code > u32::from(self.ec) {
|
||||
@@ -230,6 +250,45 @@ impl TfmMetrics {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_kerns(
|
||||
bytes: &[u8],
|
||||
lig_kern_offset: usize,
|
||||
instruction_count: usize,
|
||||
kern_table: &[i32],
|
||||
char_info: &[CharInfo],
|
||||
bc: u32,
|
||||
design_size_pt: f64,
|
||||
) -> std::collections::HashMap<(char, char), i64> {
|
||||
let mut kerns = std::collections::HashMap::new();
|
||||
for (index, info) in char_info.iter().enumerate() {
|
||||
if info.tag != 1 {
|
||||
continue;
|
||||
}
|
||||
let left = char::from_u32(bc + index as u32).unwrap_or('?');
|
||||
let mut cursor = info.lig_kern_start;
|
||||
while cursor < instruction_count {
|
||||
let offset = lig_kern_offset + cursor * 4;
|
||||
let Some(entry) = bytes.get(offset..offset + 4) else {
|
||||
break;
|
||||
};
|
||||
let (skip, next, op, remainder) = (entry[0], entry[1], entry[2], entry[3]);
|
||||
if op >= 128 {
|
||||
let kern_index = usize::from(op - 128) * 256 + usize::from(remainder);
|
||||
if let Some(raw) = kern_table.get(kern_index) {
|
||||
let right = char::from_u32(u32::from(next)).unwrap_or('?');
|
||||
let value = (f64::from(*raw) * design_size_pt / 16.0).round() as i64;
|
||||
kerns.insert((left, right), value);
|
||||
}
|
||||
}
|
||||
if skip >= 128 {
|
||||
break;
|
||||
}
|
||||
cursor += usize::from(skip) + 1;
|
||||
}
|
||||
}
|
||||
kerns
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in new issue
Block a user