78 lines
1.9 KiB
Rust
78 lines
1.9 KiB
Rust
//! 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 {}
|
|
|
|
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 {
|
|
pub const fn new() -> Self {
|
|
Self {}
|
|
}
|
|
|
|
pub fn width(&self, character: char) -> i64 {
|
|
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 ligature(&self, left: char, right: char) -> Option<(char, char)> {
|
|
metrics().ligature(left, right)
|
|
}
|
|
|
|
pub fn kern(&self, left: char, right: char) -> i64 {
|
|
metrics().kern(left, right)
|
|
}
|
|
|
|
pub fn space(&self) -> i64 {
|
|
metrics().space()
|
|
}
|
|
|
|
pub fn stretch(&self) -> i64 {
|
|
metrics().stretch()
|
|
}
|
|
|
|
pub fn shrink(&self) -> i64 {
|
|
metrics().shrink()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn reads_cmr10_widths() {
|
|
let font = Cmr10Metrics::new();
|
|
assert!(font.width('A') > 450_000);
|
|
assert!((font.width('a') - 327_681).abs() <= 2);
|
|
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);
|
|
}
|
|
}
|