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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -468,8 +468,8 @@ impl Unit {
|
||||
font: ParagraphFont,
|
||||
) -> Self {
|
||||
let source = source.into();
|
||||
let mut paragraph =
|
||||
layout_paragraph_with_font(&source, &style, &crate::cmr10::Cmr10Metrics::new());
|
||||
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(font);
|
||||
let mut paragraph = layout_paragraph_with_font(&source, &style, &metrics);
|
||||
paragraph.font = font;
|
||||
Self {
|
||||
id,
|
||||
@@ -1558,6 +1558,20 @@ mod tests {
|
||||
assert_eq!(engine.checkpoints()[1].unit_index, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bold_paragraphs_use_cmbx10_advances() {
|
||||
fn width(unit: &Unit) -> i64 {
|
||||
match unit.items.first() {
|
||||
Some(VerticalItem::Paragraph(paragraph)) => paragraph.width,
|
||||
_ => panic!("expected paragraph"),
|
||||
}
|
||||
}
|
||||
let text = "mmmmmmmmmm iiiiiiiiii";
|
||||
let roman = Unit::paragraph_with_face(1, text, ParagraphFont::Roman);
|
||||
let bold = Unit::paragraph_with_face(2, text, ParagraphFont::Bold);
|
||||
assert_ne!(width(&roman), width(&bold));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn editing_a_paragraph_preserves_its_font_face() {
|
||||
let document = Document::new(
|
||||
|
||||
@@ -176,9 +176,13 @@ fn tounicode_cmap(face: &'static PdfFace, characters: &BTreeSet<char>) -> Vec<u8
|
||||
map.into_bytes()
|
||||
}
|
||||
|
||||
fn cid_widths(face: &'static PdfFace, used: &std::collections::BTreeSet<char>) -> String {
|
||||
fn cid_widths(
|
||||
face: &'static PdfFace,
|
||||
slot: usize,
|
||||
used: &std::collections::BTreeSet<char>,
|
||||
) -> String {
|
||||
let font = face_font(face);
|
||||
let metrics = Cmr10Metrics::new();
|
||||
let metrics = Cmr10Metrics::for_pdf_slot(slot);
|
||||
let mut entries = Vec::new();
|
||||
let mut characters = used.clone();
|
||||
characters.extend((0x20u8..=0x7e).map(char::from));
|
||||
@@ -365,7 +369,7 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
|
||||
objects.push(
|
||||
format!(
|
||||
"<< /Type /Font /Subtype /CIDFontType2 /BaseFont /{base} /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /FontDescriptor {descriptor_id} 0 R /DW 500 /W {} >>",
|
||||
cid_widths(face, &used_chars[slot])
|
||||
cid_widths(face, slot, &used_chars[slot])
|
||||
)
|
||||
.into_bytes(),
|
||||
);
|
||||
@@ -558,7 +562,7 @@ mod tests {
|
||||
let id = face_font(face).glyph(accent).id().0;
|
||||
let mut used = BTreeSet::new();
|
||||
used.insert(accent);
|
||||
let widths = cid_widths(face, &used);
|
||||
let widths = cid_widths(face, 0, &used);
|
||||
assert!(widths.contains(&format!("{id} [500]")), "{widths}");
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -270,9 +270,9 @@ M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row hei
|
||||
|
||||
Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits.
|
||||
|
||||
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans for both display-list painting and PDF face selection. `Engine::edit` preserves the face when reflowing an edited paragraph.
|
||||
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph.
|
||||
|
||||
The crate currently has 166 unit tests plus five route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 263 tests (166 route-D unit, 5 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units). v1 remains the live server path.
|
||||
The crate currently has 168 unit tests plus five route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 265 tests (168 route-D unit, 5 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units). v1 remains the live server path.
|
||||
|
||||
### M0 incremental demo
|
||||
|
||||
|
||||
Reference in new issue
Block a user