Embed every Computer Modern face as Type1 outlines
This commit is contained in:
1 parent
5f5fc393a4
commit
f7849f39c2
8 files changed
+59
-32
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.
@@ -245,10 +245,24 @@ enum FontBackend {
|
|||||||
ComputerModern,
|
ComputerModern,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cmr10_type1() -> Option<&'static Type1Font> {
|
/// Knuth's Computer Modern Type1 programs, in `FontFace` order.
|
||||||
static FONT: OnceLock<Option<Type1Font>> = OnceLock::new();
|
fn type1_bytes(slot: usize) -> Option<&'static [u8]> {
|
||||||
FONT.get_or_init(|| Type1Font::from_pfb(include_bytes!("../../../assets/fonts/cmr10.pfb")))
|
static PFBS: [Option<&'static [u8]>; 6] = [
|
||||||
.as_ref()
|
Some(include_bytes!("../../../assets/fonts/cmr10.pfb")),
|
||||||
|
Some(include_bytes!("../../../assets/fonts/cmbx10.pfb")),
|
||||||
|
Some(include_bytes!("../../../assets/fonts/cmti10.pfb")),
|
||||||
|
Some(include_bytes!("../../../assets/fonts/cmbxti10.pfb")),
|
||||||
|
Some(include_bytes!("../../../assets/fonts/cmtt10.pfb")),
|
||||||
|
Some(include_bytes!("../../../assets/fonts/cmss10.pfb")),
|
||||||
|
];
|
||||||
|
PFBS[slot]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn type1_fonts() -> &'static [Option<Type1Font>; 6] {
|
||||||
|
static FONTS: OnceLock<[Option<Type1Font>; 6]> = OnceLock::new();
|
||||||
|
FONTS.get_or_init(|| {
|
||||||
|
[0usize, 1, 2, 3, 4, 5].map(|slot| type1_bytes(slot).and_then(Type1Font::from_pfb))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TeX page origin offsets (`\hoffset`, `\voffset`) in scaled points.
|
/// TeX page origin offsets (`\hoffset`, `\voffset`) in scaled points.
|
||||||
@@ -263,7 +277,7 @@ fn page_content(
|
|||||||
height: i64,
|
height: i64,
|
||||||
image_names: &BTreeMap<usize, String>,
|
image_names: &BTreeMap<usize, String>,
|
||||||
faces: &'static [PdfFace; 6],
|
faces: &'static [PdfFace; 6],
|
||||||
cm_codes: Option<&BTreeMap<char, u8>>,
|
cm_codes: &[Option<&BTreeMap<char, u8>>],
|
||||||
origin: PageOrigin,
|
origin: PageOrigin,
|
||||||
) -> String {
|
) -> String {
|
||||||
let mut content = String::from("q\n0 0 0 rg\n");
|
let mut content = String::from("q\n0 0 0 rg\n");
|
||||||
@@ -283,8 +297,11 @@ fn page_content(
|
|||||||
text,
|
text,
|
||||||
font,
|
font,
|
||||||
} => {
|
} => {
|
||||||
let operand = match cm_codes {
|
let operand = match cm_codes
|
||||||
Some(codes) if matches!(*font, FontFace::Roman) => {
|
.get(*font as usize)
|
||||||
|
.and_then(|entry| entry.as_ref())
|
||||||
|
{
|
||||||
|
Some(codes) => {
|
||||||
let mut literal = String::from("(");
|
let mut literal = String::from("(");
|
||||||
for character in text.chars() {
|
for character in text.chars() {
|
||||||
if let Some(code) = codes.get(&character) {
|
if let Some(code) = codes.get(&character) {
|
||||||
@@ -445,23 +462,26 @@ fn write_pdf(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let cm_codes: Option<(&Type1Font, BTreeMap<char, u8>)> = if backend
|
let cm_faces: Vec<Option<(&'static Type1Font, BTreeMap<char, u8>)>> = (0..6)
|
||||||
== FontBackend::ComputerModern
|
.map(|slot| {
|
||||||
&& used_faces[0]
|
if backend != FontBackend::ComputerModern || !used_faces[slot] {
|
||||||
{
|
|
||||||
cmr10_type1().and_then(|font| {
|
|
||||||
if !font.covers(used_chars[0].iter().copied()) {
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let codes = used_chars[0]
|
let font = type1_fonts()[slot].as_ref()?;
|
||||||
|
if !font.covers(used_chars[slot].iter().copied()) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let codes = used_chars[slot]
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|character| font.code_for(*character).map(|code| (*character, code)))
|
.filter_map(|character| font.code_for(*character).map(|code| (*character, code)))
|
||||||
.collect();
|
.collect();
|
||||||
Some((font, codes))
|
Some((font, codes))
|
||||||
})
|
})
|
||||||
} else {
|
.collect();
|
||||||
None
|
let cm_views: Vec<Option<&BTreeMap<char, u8>>> = cm_faces
|
||||||
};
|
.iter()
|
||||||
|
.map(|entry| entry.as_ref().map(|(_, codes)| codes))
|
||||||
|
.collect();
|
||||||
let minimal = pages.iter().all(|page| {
|
let minimal = pages.iter().all(|page| {
|
||||||
page.items.iter().all(|item| match item {
|
page.items.iter().all(|item| match item {
|
||||||
DisplayItem::Text { text, .. } => text.chars().all(minimal_text_char),
|
DisplayItem::Text { text, .. } => text.chars().all(minimal_text_char),
|
||||||
@@ -474,9 +494,7 @@ fn write_pdf(
|
|||||||
if !used_faces[slot] {
|
if !used_faces[slot] {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if slot == 0
|
if let Some((font, codes)) = &cm_faces[slot] {
|
||||||
&& let Some((font, codes)) = &cm_codes
|
|
||||||
{
|
|
||||||
let font_id = objects.len() + 1;
|
let font_id = objects.len() + 1;
|
||||||
let descriptor_id = font_id + 1;
|
let descriptor_id = font_id + 1;
|
||||||
let file_id = font_id + 2;
|
let file_id = font_id + 2;
|
||||||
@@ -656,14 +674,7 @@ fn write_pdf(
|
|||||||
for (index, slot) in &used {
|
for (index, slot) in &used {
|
||||||
names.insert(*index, format!("Im{slot}"));
|
names.insert(*index, format!("Im{slot}"));
|
||||||
}
|
}
|
||||||
let content = page_content(
|
let content = page_content(list, height, &names, faces, &cm_views, origin);
|
||||||
list,
|
|
||||||
height,
|
|
||||||
&names,
|
|
||||||
faces,
|
|
||||||
cm_codes.as_ref().map(|(_, codes)| codes),
|
|
||||||
origin,
|
|
||||||
);
|
|
||||||
objects.push(
|
objects.push(
|
||||||
format!(
|
format!(
|
||||||
"<< /Length {} >>\nstream\n{}endstream",
|
"<< /Length {} >>\nstream\n{}endstream",
|
||||||
@@ -800,6 +811,22 @@ mod type1_pdf_tests {
|
|||||||
assert!(!text.contains("/Subtype /Type0"), "{text}");
|
assert!(!text.contains("/Subtype /Type0"), "{text}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bold_text_embeds_cmbx10() {
|
||||||
|
let mut list = hello_list();
|
||||||
|
list.items[0] = DisplayItem::Text {
|
||||||
|
x: 0,
|
||||||
|
y: 800_000,
|
||||||
|
size: 655_360,
|
||||||
|
text: "Bold".to_string(),
|
||||||
|
font: FontFace::Bold,
|
||||||
|
};
|
||||||
|
let pdf = write(&[list]);
|
||||||
|
let text = String::from_utf8_lossy(&pdf);
|
||||||
|
assert!(text.contains("/BaseFont /CMBX10"), "{text}");
|
||||||
|
assert!(!text.contains("/BaseFont /CMR10"), "{text}");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn type1_backend_falls_back_for_uncovered_characters() {
|
fn type1_backend_falls_back_for_uncovered_characters() {
|
||||||
let mut list = hello_list();
|
let mut list = hello_list();
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
|
|||||||
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
||||||
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
|
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
|
||||||
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
|
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
|
||||||
PDF opt-in with `?engine=route-d`). 532 workspace tests are green and no oracle
|
PDF opt-in with `?engine=route-d`). 533 workspace tests are green and no oracle
|
||||||
fixture is ignored.
|
fixture is ignored.
|
||||||
|
|
||||||
Status after G1 (P0-P2): the paragraph model reproduces TeX's passes,
|
Status after G1 (P0-P2): the paragraph model reproduces TeX's passes,
|
||||||
|
|||||||
+3
-3
@@ -331,7 +331,7 @@ for a later overlaid slash.
|
|||||||
| M2 vertical/page builder | partial | vsize, topskip, insertions, marks, Rust output hook, top/bottom floats, float-page/text fractions, rules, vskip, penalties; `page::PageBuilder` scores breakpoints with page badness and penalties and models `\topskip`/`\maxdepth`; `Engine::layout_paged` builds pages through it for documents made of box/glue/penalty/mark vertical material (a unit may span pages), while in-text floats flow inline through the page builder; paged layouts replay edits from the edited page (insertions, bottom floats and interpreted `\output` force a full rebuild), and insertions are placed through the builder too; `\output` interpreted for a `\shipout`/`\ifdim`/`\ifnum` register subset over page globals and `\outputpenalty`/`\pageno`/`\deadcycles` |
|
| M2 vertical/page builder | partial | vsize, topskip, insertions, marks, Rust output hook, top/bottom floats, float-page/text fractions, rules, vskip, penalties; `page::PageBuilder` scores breakpoints with page badness and penalties and models `\topskip`/`\maxdepth`; `Engine::layout_paged` builds pages through it for documents made of box/glue/penalty/mark vertical material (a unit may span pages), while in-text floats flow inline through the page builder; paged layouts replay edits from the edited page (insertions, bottom floats and interpreted `\output` force a full rebuild), and insertions are placed through the builder too; `\output` interpreted for a `\shipout`/`\ifdim`/`\ifnum` register subset over page globals and `\outputpenalty`/`\pageno`/`\deadcycles` |
|
||||||
| M3 halign | partial | natural columns, spans, omit, hline rows, templates, noalign skip, rules; no PDF painter |
|
| M3 halign | partial | natural columns, spans, omit, hline rows, templates, noalign skip, rules; no PDF painter |
|
||||||
| M4 math | partial | atoms, scripts, fractions, roots, delimiters, accents, operators, classes, braces, boxed, negated; no math painter/shrink wrapping |
|
| M4 math | partial | atoms, scripts, fractions, roots, delimiters, accents, operators, classes, braces, boxed, negated; no math painter/shrink wrapping |
|
||||||
| M5 NFSS/TFM | partial | runtime `cmr10.tfm` widths/heights/depths/kern pairs and ff/fi/fl/ffi/ffl ligatures; text faces embed Liberation Serif/Mono as Type0 CID fonts with Identity-H, ToUnicode and width arrays; used faces embed per-document TrueType glyf subsets that keep the original glyph IDs, include composite components, and carry a matching ToUnicode CMap; ASCII documents use the compact tier; no Base-14 dependency; SVG carries family/weight/style; route-D PDFs embed Knuth's `cmr10.pfb` as a `/Subtype /Type1` font with `/FontFile` (`Length1/2/3`), `/Encoding /Differences` and TFM `/Widths`, falling back to Liberation for characters the program does not encode |
|
| M5 NFSS/TFM | partial | runtime `cmr10.tfm` widths/heights/depths/kern pairs and ff/fi/fl/ffi/ffl ligatures; text faces embed Liberation Serif/Mono as Type0 CID fonts with Identity-H, ToUnicode and width arrays; used faces embed per-document TrueType glyf subsets that keep the original glyph IDs, include composite components, and carry a matching ToUnicode CMap; ASCII documents use the compact tier; no Base-14 dependency; SVG carries family/weight/style; route-D PDFs embed Knuth's Computer Modern Type1 programs (cmr10/cmbx10/cmti10/cmbxti10/cmtt10/cmss10) as `/Subtype /Type1` fonts with `/FontFile` (`Length1/2/3`), `/Encoding /Differences` and TFM `/Widths`, falling back per face to Liberation when the program does not encode every character |
|
||||||
| M6 specials/XObject | partial | raw specials, RGB image XObjects, URI link annotations; class/package execution absent |
|
| M6 specials/XObject | partial | raw specials, RGB image XObjects, URI link annotations; class/package execution absent |
|
||||||
| M7 incremental integration | partial | route-D preview/PDF/edit endpoints plus opt-in `?engine=route-d` project preview, content-based page hashes and section/emphasis/display-math/itemize/enumerate/description/quote/center/flushright mapping; editor toggle in `texrra-web`; inline math pending |
|
| M7 incremental integration | partial | route-D preview/PDF/edit endpoints plus opt-in `?engine=route-d` project preview, content-based page hashes and section/emphasis/display-math/itemize/enumerate/description/quote/center/flushright mapping; editor toggle in `texrra-web`; inline math pending |
|
||||||
|
|
||||||
@@ -353,7 +353,7 @@ M3 painter: `Page::display_list()` and `Halign::display_list()` produce absolute
|
|||||||
|
|
||||||
PDF writer: `crates/texrra-tex-core/src/pdf.rs` writes multi-page PDF 1.4 with five embedded Liberation Type0/CID faces (Identity-H + ToUnicode + `/W` arrays), filled rectangles, diagonal rules, raw-RGB `/XObject` images, `/Annots` URI links and arbitrary `/special` operators. Ligature substitution and line-end italic correction are implemented; full NFSS family switching remains open.
|
PDF writer: `crates/texrra-tex-core/src/pdf.rs` writes multi-page PDF 1.4 with five embedded Liberation Type0/CID faces (Identity-H + ToUnicode + `/W` arrays), filled rectangles, diagonal rules, raw-RGB `/XObject` images, `/Annots` URI links and arbitrary `/special` operators. Ligature substitution and line-end italic correction are implemented; full NFSS family switching remains open.
|
||||||
|
|
||||||
M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end and math-script italic corrections and monospace/sans paragraph faces are applied; paragraph-level NFSS declarations (`\rmfamily`, `\sffamily`, `\ttfamily`, `\bfseries`, `\itshape`, `\texttt`, `\textsf`) select those faces and the `\fontfamily`/`\fontseries`/`\fontshape`/`\fontencoding`/`\selectfont` command forms plus legacy `\rm`/`\sf`/`\tt`/`\bf`/`\it`/`\sl`/`\up`/`\normalfont` declarations are parsed; inline `\textbf`/`\emph`/`\textit`/`\texttt`/`\textsf` groups and `{\bfseries ...}` declaration groups (including nesting) split a paragraph into styled runs, each shaped by its own TFM and painted with its own face; nested faces compose relatively (`\emph` inside bold yields bold italic via cmbxti10, `\emph` inside italic returns upright); styled runs are stored on the paragraph unit, so incremental edits rebuild them through `Engine::edit_runs` instead of flattening the paragraph back to one face; true family/series/shape switching remains open; route-D PDFs now embed Knuth's `cmr10.pfb` Type1 program as a simple font (PFB `Length1/2/3`, `/Encoding /Differences` from the program's built-in encoding, `/Widths` in 1000-unit text space from CMR10 advances) whenever the program covers every Roman character on the page, and fall back to the Liberation Type0 subset otherwise; `pdffonts` reports `Type 1 CMR10` and `pdftotext` still extracts the text. `Unit::paragraph` now builds its item stream with `layout_paragraph_with_font`, so CMR10 advances, kerns and ligatures drive paragraph widths; each glyph box also carries the TFM height/depth, and the painter emits 10pt glyphs and the Type0 `/W` array reports CMR10 advances, and ligature boxes paint their component letters, so `pdftotext` keeps word boundaries instead of inserting spaces between letters.
|
M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end and math-script italic corrections and monospace/sans paragraph faces are applied; paragraph-level NFSS declarations (`\rmfamily`, `\sffamily`, `\ttfamily`, `\bfseries`, `\itshape`, `\texttt`, `\textsf`) select those faces and the `\fontfamily`/`\fontseries`/`\fontshape`/`\fontencoding`/`\selectfont` command forms plus legacy `\rm`/`\sf`/`\tt`/`\bf`/`\it`/`\sl`/`\up`/`\normalfont` declarations are parsed; inline `\textbf`/`\emph`/`\textit`/`\texttt`/`\textsf` groups and `{\bfseries ...}` declaration groups (including nesting) split a paragraph into styled runs, each shaped by its own TFM and painted with its own face; nested faces compose relatively (`\emph` inside bold yields bold italic via cmbxti10, `\emph` inside italic returns upright); styled runs are stored on the paragraph unit, so incremental edits rebuild them through `Engine::edit_runs` instead of flattening the paragraph back to one face; true family/series/shape switching remains open; route-D PDFs now embed all six Knuth Computer Modern Type1 programs (cmr10, cmbx10, cmti10, cmbxti10, cmtt10, cmss10) as simple fonts (PFB `Length1/2/3`, `/Encoding /Differences` from each program's built-in encoding, `/Widths` in 1000-unit text space from that face's TFM advances) whenever the program covers every character of that face on the page, and fall back per face to the Liberation Type0 subset otherwise; `pdffonts` reports `Type 1` faces (CMR10, CMBX10, ...) and `pdftotext` still extracts the text. `Unit::paragraph` now builds its item stream with `layout_paragraph_with_font`, so CMR10 advances, kerns and ligatures drive paragraph widths; each glyph box also carries the TFM height/depth, and the painter emits 10pt glyphs and the Type0 `/W` array reports CMR10 advances, and ligature boxes paint their component letters, so `pdftotext` keeps word boundaries instead of inserting spaces between letters.
|
||||||
|
|
||||||
M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments.
|
M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments.
|
||||||
|
|
||||||
@@ -367,7 +367,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal
|
|||||||
|
|
||||||
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.
|
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 347 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 532 tests (353 route-D unit, 33 engine binary unit, 8 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
|
The crate currently has 347 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 533 tests (354 route-D unit, 33 engine binary unit, 8 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
|
||||||
|
|
||||||
### M0 incremental demo
|
### M0 incremental demo
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user