Add per-face PDF font resources

This commit is contained in:
gloamxun committed 2026-10-20 18:27:14 +08:00
1 parent 03025de783
commit d40595c7e5
5 files changed
+91 -9

No files matched your search

@@ -16,6 +16,7 @@ fn main() {
y: 100 * 65_536, y: 100 * 65_536,
size: 12 * 65_536, size: 12 * 65_536,
text: "route-D XObject".to_string(), text: "route-D XObject".to_string(),
font: texrra_tex_core::render::FontFace::Roman,
}); });
list.push(DisplayItem::Rule { list.push(DisplayItem::Rule {
x: 72 * 65_536, x: 72 * 65_536,
+17
View File
@@ -1598,6 +1598,23 @@ mod tests {
assert!(text.contains("Tj")); assert!(text.contains("Tj"));
assert!(text.trim_end().ends_with("%%EOF")); assert!(text.trim_end().ends_with("%%EOF"));
} }
#[test]
fn pdf_writer_uses_a_bold_font_resource() {
let mut list = crate::render::DisplayList::default();
list.push(crate::render::DisplayItem::Text {
x: 10,
y: 20,
size: 10 * 65_536,
text: "bold".to_owned(),
font: crate::render::FontFace::Bold,
});
let pdf = crate::pdf::display_list_to_pdf(&list, 400 * 65_536);
let text = String::from_utf8_lossy(&pdf);
assert!(text.contains("/BaseFont /Times-Bold"));
assert!(text.contains("/F2 4 0 R"));
assert!(text.contains("BT /F2"));
}
#[test] #[test]
fn math_display_list_paints_symbols_and_fraction_rule() { fn math_display_list_paints_symbols_and_fraction_rule() {
let atom = crate::math::parse_math("x + \\frac{a}{b}"); let atom = crate::math::parse_math("x + \\frac{a}{b}");
+48 -6
View File
@@ -5,7 +5,7 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use crate::render::{DisplayItem, DisplayList}; use crate::render::{DisplayItem, DisplayList, FontFace};
fn escape(text: &str) -> String { fn escape(text: &str) -> String {
let mut out = String::new(); let mut out = String::new();
@@ -25,13 +25,40 @@ fn pt(value: i64) -> String {
format!("{:.3}", value as f64 / 65_536.0) format!("{:.3}", value as f64 / 65_536.0)
} }
fn font_base(font: FontFace) -> &'static str {
match font {
FontFace::Roman => "Times-Roman",
FontFace::Bold => "Times-Bold",
FontFace::Italic => "Times-Italic",
FontFace::BoldItalic => "Times-BoldItalic",
FontFace::Mono => "Courier",
}
}
fn font_resource(font: FontFace) -> &'static str {
match font {
FontFace::Roman => "F1",
FontFace::Bold => "F2",
FontFace::Italic => "F3",
FontFace::BoldItalic => "F4",
FontFace::Mono => "F5",
}
}
fn page_content(list: &DisplayList, height: i64, image_names: &BTreeMap<usize, String>) -> String { fn page_content(list: &DisplayList, height: i64, image_names: &BTreeMap<usize, String>) -> String {
let mut content = String::from("q\n0 0 0 rg\n"); let mut content = String::from("q\n0 0 0 rg\n");
for item in &list.items { for item in &list.items {
match item { match item {
DisplayItem::Text { x, y, size, text } => { DisplayItem::Text {
x,
y,
size,
text,
font,
} => {
content.push_str(&format!( content.push_str(&format!(
"BT /F1 {} Tf 1 0 0 1 {} {} Tm ({}) Tj ET\n", "BT /{} {} Tf 1 0 0 1 {} {} Tm ({}) Tj ET\n",
font_resource(*font),
pt(*size), pt(*size),
pt(*x), pt(*x),
pt(height - *y), pt(height - *y),
@@ -93,7 +120,21 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
let mut objects: Vec<Vec<u8>> = Vec::new(); let mut objects: Vec<Vec<u8>> = Vec::new();
objects.push(b"<< /Type /Catalog /Pages 2 0 R >>".to_vec()); objects.push(b"<< /Type /Catalog /Pages 2 0 R >>".to_vec());
objects.push(Vec::new()); // pages object filled below objects.push(Vec::new()); // pages object filled below
objects.push(b"<< /Type /Font /Subtype /Type1 /BaseFont /Times-Roman >>".to_vec()); for font in [
FontFace::Roman,
FontFace::Bold,
FontFace::Italic,
FontFace::BoldItalic,
FontFace::Mono,
] {
objects.push(
format!(
"<< /Type /Font /Subtype /Type1 /BaseFont /{} >>",
font_base(font)
)
.into_bytes(),
);
}
let mut kids = Vec::new(); let mut kids = Vec::new();
for list in pages { for list in pages {
@@ -113,10 +154,11 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
.map(|(_, slot)| format!("/Im{slot} {} 0 R", image_base + *slot)) .map(|(_, slot)| format!("/Im{slot} {} 0 R", image_base + *slot))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(" "); .join(" ");
let fonts = "/F1 3 0 R /F2 4 0 R /F3 5 0 R /F4 6 0 R /F5 7 0 R";
let resources = if used.is_empty() { let resources = if used.is_empty() {
"<< /Font << /F1 3 0 R >> >>".to_string() format!("<< /Font << {fonts} >> >>")
} else { } else {
format!("<< /Font << /F1 3 0 R >> /XObject << {xobjects} >> >>") format!("<< /Font << {fonts} >> /XObject << {xobjects} >> >>")
}; };
let links = list let links = list
.items .items
+23 -1
View File
@@ -10,6 +10,15 @@ use crate::math::{self, MathAtom};
use crate::{FloatPlacement, Halign, HorizontalItem, Page, VerticalItem}; use crate::{FloatPlacement, Halign, HorizontalItem, Page, VerticalItem};
/// One absolute drawing primitive in scaled points. /// One absolute drawing primitive in scaled points.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FontFace {
Roman,
Bold,
Italic,
BoldItalic,
Mono,
}
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum DisplayItem { pub enum DisplayItem {
Text { Text {
@@ -17,6 +26,7 @@ pub enum DisplayItem {
y: i64, y: i64,
size: i64, size: i64,
text: String, text: String,
font: FontFace,
}, },
Rule { Rule {
x: i64, x: i64,
@@ -112,7 +122,9 @@ impl DisplayList {
); );
for item in &self.items { for item in &self.items {
match item { match item {
DisplayItem::Text { x, y, size, text } => { DisplayItem::Text {
x, y, size, text, ..
} => {
let x = *x as f64 / 65_536.0; let x = *x as f64 / 65_536.0;
let y = *y as f64 / 65_536.0; let y = *y as f64 / 65_536.0;
let size = *size as f64 / 65_536.0; let size = *size as f64 / 65_536.0;
@@ -213,6 +225,7 @@ impl Halign {
for (index, cell) in row.cells.iter().enumerate() { for (index, cell) in row.cells.iter().enumerate() {
if let Some((x, _)) = self.cell_box(row_index, index) { if let Some((x, _)) = self.cell_box(row_index, index) {
list.push(DisplayItem::Text { list.push(DisplayItem::Text {
font: FontFace::Roman,
x, x,
y: baseline, y: baseline,
size: self.line_height * 5 / 6, size: self.line_height * 5 / 6,
@@ -323,6 +336,7 @@ impl Page {
HorizontalItem::Box(box_) => { HorizontalItem::Box(box_) => {
if let Some(character) = box_.character { if let Some(character) = box_.character {
list.push(DisplayItem::Text { list.push(DisplayItem::Text {
font: FontFace::Roman,
x, x,
y: baseline, y: baseline,
size, size,
@@ -435,6 +449,7 @@ impl Page {
MathAtom::Symbol(character) => { MathAtom::Symbol(character) => {
if !character.is_whitespace() { if !character.is_whitespace() {
list.push(DisplayItem::Text { list.push(DisplayItem::Text {
font: FontFace::Roman,
x, x,
y: baseline, y: baseline,
size, size,
@@ -446,6 +461,7 @@ impl Page {
let mut cursor = x; let mut cursor = x;
for character in text.chars() { for character in text.chars() {
list.push(DisplayItem::Text { list.push(DisplayItem::Text {
font: FontFace::Roman,
x: cursor, x: cursor,
y: baseline, y: baseline,
size, size,
@@ -520,6 +536,7 @@ impl Page {
MathAtom::Sqrt { radicand, .. } => { MathAtom::Sqrt { radicand, .. } => {
let box_ = math::layout_math(radicand, font); let box_ = math::layout_math(radicand, font);
list.push(DisplayItem::Text { list.push(DisplayItem::Text {
font: FontFace::Roman,
x, x,
y: baseline, y: baseline,
size, size,
@@ -537,6 +554,7 @@ impl Page {
let body_box = math::layout_math(body, font); let body_box = math::layout_math(body, font);
if *left != '.' { if *left != '.' {
list.push(DisplayItem::Text { list.push(DisplayItem::Text {
font: FontFace::Roman,
x, x,
y: baseline, y: baseline,
size, size,
@@ -546,6 +564,7 @@ impl Page {
Self::paint_math(body, font, x + font.width(*left), baseline, size, list); Self::paint_math(body, font, x + font.width(*left), baseline, size, list);
if *right != '.' { if *right != '.' {
list.push(DisplayItem::Text { list.push(DisplayItem::Text {
font: FontFace::Roman,
x: x + font.width(*left) + body_box.width, x: x + font.width(*left) + body_box.width,
y: baseline, y: baseline,
size, size,
@@ -557,6 +576,7 @@ impl Page {
let base_box = math::layout_math(base, font); let base_box = math::layout_math(base, font);
Self::paint_math(base, font, x, baseline, size, list); Self::paint_math(base, font, x, baseline, size, list);
list.push(DisplayItem::Text { list.push(DisplayItem::Text {
font: FontFace::Roman,
x: x + base_box.width / 2, x: x + base_box.width / 2,
y: baseline - base_box.height - 65_536, y: baseline - base_box.height - 65_536,
size, size,
@@ -654,6 +674,7 @@ impl Page {
MathAtom::BigOperator { name } => { MathAtom::BigOperator { name } => {
for (index, character) in name.chars().enumerate() { for (index, character) in name.chars().enumerate() {
list.push(DisplayItem::Text { list.push(DisplayItem::Text {
font: FontFace::Roman,
x: x + index as i64 * font.width(character), x: x + index as i64 * font.width(character),
y: baseline, y: baseline,
size, size,
@@ -663,6 +684,7 @@ impl Page {
} }
MathAtom::SizedDelimiter { delimiter, .. } => { MathAtom::SizedDelimiter { delimiter, .. } => {
list.push(DisplayItem::Text { list.push(DisplayItem::Text {
font: FontFace::Roman,
x, x,
y: baseline, y: baseline,
size, size,
+2 -2
View File
@@ -228,8 +228,8 @@ for a later overlaid slash.
| M2 vertical/page builder | partial | vsize, topskip, insertions, marks, output hook, top/bottom floats, rules, vskip, penalties; interpreted `\output` and float pages absent | | M2 vertical/page builder | partial | vsize, topskip, insertions, marks, output hook, top/bottom floats, rules, vskip, penalties; interpreted `\output` and float pages absent |
| 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 | CMR10 width table only | | M5 NFSS/TFM | partial | CMR10 widths and per-face Base-14 resources (Roman/Bold/Italic/BoldItalic/Mono); TFM loading and font embedding absent |
| M6 specials/XObject | not started | | | M6 specials/XObject | partial | raw specials, RGB image XObjects, URI link annotations; class/package execution absent |
| M7 incremental integration | partial | edit replay + page hashes, not wired into v1 preview | | M7 incremental integration | partial | edit replay + page hashes, not wired into v1 preview |
v1 remains the production renderer; the route-D core is additive and tested independently. v1 remains the production renderer; the route-D core is additive and tested independently.