Parse cmr10 TFM metrics for route-D math
This commit is contained in:
1 parent
84e2e9cadb
commit
f909e8199c
4 files changed
+314
-37
No files matched your search
@@ -1,12 +1,19 @@
|
||||
// Computer Modern Roman 10pt character widths, extracted from cmr10.tfm
|
||||
// with tftopl. Values are scaled points at 10pt design size.
|
||||
#[derive(Clone, Debug)]
|
||||
//! Computer Modern Roman 10pt metrics backed by the real `cmr10.tfm`.
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use crate::tfm::TfmMetrics;
|
||||
|
||||
/// Metrics for CMR10, parsed from the TFM file embedded in the crate.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct Cmr10Metrics {}
|
||||
|
||||
impl Default for Cmr10Metrics {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
static CMR10_TFM: &[u8] = include_bytes!("../assets/cmr10.tfm");
|
||||
static CMR10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
|
||||
fn metrics() -> &'static TfmMetrics {
|
||||
CMR10_METRICS
|
||||
.get_or_init(|| TfmMetrics::parse(CMR10_TFM).expect("embedded cmr10.tfm must be valid"))
|
||||
}
|
||||
|
||||
impl Cmr10Metrics {
|
||||
@@ -15,43 +22,35 @@ impl Cmr10Metrics {
|
||||
}
|
||||
|
||||
pub fn width(&self, character: char) -> i64 {
|
||||
self.widths()
|
||||
.get(character as usize)
|
||||
.copied()
|
||||
.unwrap_or(327_680)
|
||||
let width = metrics().width(character);
|
||||
if width > 0 { width } else { 327_680 }
|
||||
}
|
||||
|
||||
pub fn height(&self, character: char) -> i64 {
|
||||
metrics().height(character)
|
||||
}
|
||||
|
||||
pub fn depth(&self, character: char) -> i64 {
|
||||
metrics().depth(character)
|
||||
}
|
||||
|
||||
pub fn italic(&self, character: char) -> i64 {
|
||||
metrics().italic(character)
|
||||
}
|
||||
|
||||
pub fn space(&self) -> i64 {
|
||||
218_454
|
||||
metrics().space()
|
||||
}
|
||||
|
||||
pub fn stretch(&self) -> i64 {
|
||||
109_227
|
||||
metrics().stretch()
|
||||
}
|
||||
|
||||
pub fn shrink(&self) -> i64 {
|
||||
72_818
|
||||
}
|
||||
|
||||
fn widths(&self) -> &'static [i64; 128] {
|
||||
&CMR10_WIDTHS
|
||||
metrics().shrink()
|
||||
}
|
||||
}
|
||||
|
||||
const CMR10_WIDTHS: [i64; 128] = [
|
||||
409601, 546135, 509727, 455112, 436908, 491521, 473317, 509727, 473317, 509727, 473317, 382295,
|
||||
364090, 364090, 546135, 546135, 182045, 200250, 327681, 327681, 327681, 327681, 327681, 491521,
|
||||
291272, 327682, 473317, 509727, 327681, 591647, 664464, 509727, 182045, 182045, 327681, 546135,
|
||||
327681, 546135, 509727, 182045, 254863, 254863, 327681, 509727, 182045, 218454, 182045, 327681,
|
||||
327681, 327681, 327681, 327681, 327681, 327681, 327681, 327681, 327681, 327681, 182045, 182045,
|
||||
182045, 509727, 309477, 309477, 509727, 491521, 464215, 473317, 500624, 446010, 427807, 514277,
|
||||
491521, 236658, 336783, 509727, 409601, 600748, 491521, 509727, 446010, 509727, 482419, 364090,
|
||||
473317, 491521, 491521, 673567, 491521, 491521, 400499, 182045, 327681, 182045, 327681, 182045,
|
||||
182045, 327681, 364090, 291272, 364090, 291272, 200250, 327681, 364090, 182045, 200250, 345887,
|
||||
182045, 546135, 364090, 327681, 364090, 345885, 256684, 258503, 254863, 364090, 345887, 473317,
|
||||
345887, 345887, 291272, 327681, 655362, 327681, 327681, 327681,
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -64,5 +63,7 @@ mod tests {
|
||||
assert!((font.space() - 218_454).abs() <= 2);
|
||||
assert!(font.stretch() > 0);
|
||||
assert!(font.shrink() > 0);
|
||||
assert!(font.height('A') > 0);
|
||||
assert!(font.depth('g') > 0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user