Use per-face Computer Modern TFMs for paragraph layout
This commit is contained in:
1 parent
7cb5b42429
commit
521c87d952
9 files changed
+136
-26
No files matched your search
@@ -1,61 +1,142 @@
|
||||
//! Computer Modern Roman 10pt metrics backed by the real `cmr10.tfm`.
|
||||
//! Computer Modern 10pt metrics backed by the real TFM files.
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use crate::paragraph::ParagraphFont;
|
||||
use crate::tfm::TfmMetrics;
|
||||
|
||||
/// Metrics for CMR10, parsed from the TFM file embedded in the crate.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct Cmr10Metrics {}
|
||||
/// One Computer Modern text face at 10pt.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum Cmr10Metrics {
|
||||
#[default]
|
||||
Roman,
|
||||
Bold,
|
||||
Italic,
|
||||
BoldItalic,
|
||||
Mono,
|
||||
Sans,
|
||||
}
|
||||
|
||||
static CMR10_TFM: &[u8] = include_bytes!("../assets/cmr10.tfm");
|
||||
static CMR10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMBX10_TFM: &[u8] = include_bytes!("../assets/cmbx10.tfm");
|
||||
static CMTI10_TFM: &[u8] = include_bytes!("../assets/cmti10.tfm");
|
||||
static CMBXTI10_TFM: &[u8] = include_bytes!("../assets/cmbxti10.tfm");
|
||||
static CMTT10_TFM: &[u8] = include_bytes!("../assets/cmtt10.tfm");
|
||||
static CMSS10_TFM: &[u8] = include_bytes!("../assets/cmss10.tfm");
|
||||
|
||||
fn metrics() -> &'static TfmMetrics {
|
||||
CMR10_METRICS
|
||||
.get_or_init(|| TfmMetrics::parse(CMR10_TFM).expect("embedded cmr10.tfm must be valid"))
|
||||
static CMR10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMBX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMTI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMBXTI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMTT10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMSS10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
|
||||
fn parse(bytes: &[u8]) -> TfmMetrics {
|
||||
TfmMetrics::parse(bytes).expect("embedded TFM must be valid")
|
||||
}
|
||||
|
||||
fn parse_roman() -> TfmMetrics {
|
||||
parse(CMR10_TFM)
|
||||
}
|
||||
|
||||
fn parse_bold() -> TfmMetrics {
|
||||
parse(CMBX10_TFM)
|
||||
}
|
||||
|
||||
fn parse_italic() -> TfmMetrics {
|
||||
parse(CMTI10_TFM)
|
||||
}
|
||||
|
||||
fn parse_bold_italic() -> TfmMetrics {
|
||||
parse(CMBXTI10_TFM)
|
||||
}
|
||||
|
||||
fn parse_mono() -> TfmMetrics {
|
||||
parse(CMTT10_TFM)
|
||||
}
|
||||
|
||||
fn parse_sans() -> TfmMetrics {
|
||||
parse(CMSS10_TFM)
|
||||
}
|
||||
|
||||
fn metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics {
|
||||
match font {
|
||||
Cmr10Metrics::Roman => CMR10_METRICS.get_or_init(parse_roman),
|
||||
Cmr10Metrics::Bold => CMBX10_METRICS.get_or_init(parse_bold),
|
||||
Cmr10Metrics::Italic => CMTI10_METRICS.get_or_init(parse_italic),
|
||||
Cmr10Metrics::BoldItalic => CMBXTI10_METRICS.get_or_init(parse_bold_italic),
|
||||
Cmr10Metrics::Mono => CMTT10_METRICS.get_or_init(parse_mono),
|
||||
Cmr10Metrics::Sans => CMSS10_METRICS.get_or_init(parse_sans),
|
||||
}
|
||||
}
|
||||
|
||||
impl Cmr10Metrics {
|
||||
pub const fn new() -> Self {
|
||||
Self {}
|
||||
Self::Roman
|
||||
}
|
||||
|
||||
/// Select the Computer Modern face that corresponds to a paragraph face.
|
||||
pub const fn for_paragraph_font(font: ParagraphFont) -> Self {
|
||||
match font {
|
||||
ParagraphFont::Roman => Self::Roman,
|
||||
ParagraphFont::Bold => Self::Bold,
|
||||
ParagraphFont::Italic => Self::Italic,
|
||||
ParagraphFont::Mono => Self::Mono,
|
||||
ParagraphFont::Sans => Self::Sans,
|
||||
}
|
||||
}
|
||||
|
||||
/// Select the face by PDF slot: Roman, bold, italic, bold-italic, mono, sans.
|
||||
pub const fn for_pdf_slot(slot: usize) -> Self {
|
||||
match slot {
|
||||
1 => Self::Bold,
|
||||
2 => Self::Italic,
|
||||
3 => Self::BoldItalic,
|
||||
4 => Self::Mono,
|
||||
5 => Self::Sans,
|
||||
_ => Self::Roman,
|
||||
}
|
||||
}
|
||||
|
||||
fn metrics(self) -> &'static TfmMetrics {
|
||||
metrics_for(self)
|
||||
}
|
||||
|
||||
pub fn width(&self, character: char) -> i64 {
|
||||
let width = metrics().width(character);
|
||||
let width = self.metrics().width(character);
|
||||
if width > 0 { width } else { 327_680 }
|
||||
}
|
||||
|
||||
pub fn height(&self, character: char) -> i64 {
|
||||
metrics().height(character)
|
||||
self.metrics().height(character)
|
||||
}
|
||||
|
||||
pub fn depth(&self, character: char) -> i64 {
|
||||
metrics().depth(character)
|
||||
self.metrics().depth(character)
|
||||
}
|
||||
|
||||
pub fn italic(&self, character: char) -> i64 {
|
||||
metrics().italic(character)
|
||||
self.metrics().italic(character)
|
||||
}
|
||||
|
||||
pub fn ligature(&self, left: char, right: char) -> Option<(char, char)> {
|
||||
metrics().ligature(left, right)
|
||||
self.metrics().ligature(left, right)
|
||||
}
|
||||
|
||||
pub fn kern(&self, left: char, right: char) -> i64 {
|
||||
metrics().kern(left, right)
|
||||
self.metrics().kern(left, right)
|
||||
}
|
||||
|
||||
pub fn space(&self) -> i64 {
|
||||
metrics().space()
|
||||
self.metrics().space()
|
||||
}
|
||||
|
||||
pub fn stretch(&self) -> i64 {
|
||||
metrics().stretch()
|
||||
self.metrics().stretch()
|
||||
}
|
||||
|
||||
pub fn shrink(&self) -> i64 {
|
||||
metrics().shrink()
|
||||
self.metrics().shrink()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,4 +155,15 @@ mod tests {
|
||||
assert!(font.height('A') > 0);
|
||||
assert!(font.depth('g') > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bold_and_italic_tfms_differ_from_roman() {
|
||||
let roman = Cmr10Metrics::Roman;
|
||||
let bold = Cmr10Metrics::Bold;
|
||||
let italic = Cmr10Metrics::Italic;
|
||||
assert_ne!(bold.width('m'), roman.width('m'));
|
||||
assert_ne!(italic.width('m'), roman.width('m'));
|
||||
assert!(bold.space() > 0);
|
||||
assert!(italic.height('A') > 0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user