From 6b74a173129f07f8acc829b70048acbae7e4e042 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 18:32:33 +0800 Subject: [PATCH] Carry paragraph font faces through the display list --- crates/texrra-tex-core/src/lib.rs | 29 +++++++++++++++++++++++-- crates/texrra-tex-core/src/paragraph.rs | 20 ++++++++++++++++- crates/texrra-tex-core/src/render.rs | 8 ++++++- docs/TEX_CORE.md | 2 +- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 00936dd..3fccd88 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -6,8 +6,8 @@ pub mod render; pub use cmr10::Cmr10Metrics; pub use paragraph::{ - HorizontalBox, HorizontalItem, Paragraph, ParagraphStyle, layout_items, layout_paragraph, - layout_paragraph_with_font, text_items, text_items_with_font, + HorizontalBox, HorizontalItem, Paragraph, ParagraphFont, ParagraphStyle, layout_items, + layout_paragraph, layout_paragraph_with_font, text_items, text_items_with_font, }; pub use render::{DisplayItem, DisplayList}; @@ -407,6 +407,15 @@ impl Unit { Self::paragraph_with_style(id, source, ParagraphStyle::default()) } + /// Build a paragraph unit with an explicit font face. + pub fn paragraph_with_face(id: UnitId, source: impl Into, font: ParagraphFont) -> Self { + let mut unit = Self::paragraph_with_style(id, source, ParagraphStyle::default()); + if let Some(VerticalItem::Paragraph(paragraph)) = unit.items.first_mut() { + paragraph.font = font; + } + unit + } + pub fn paragraph_with_style( id: UnitId, source: impl Into, @@ -1599,6 +1608,22 @@ mod tests { assert!(text.trim_end().ends_with("%%EOF")); } + #[test] + fn bold_paragraph_paints_with_a_bold_face() { + let document = Document::new( + 2_000_000, + vec![Unit::paragraph_with_face(1, "bold", ParagraphFont::Bold)], + ); + let layout = Engine::layout(&document); + let list = layout.pages()[0].display_list(); + assert!(list.items.iter().any(|item| matches!( + item, + crate::render::DisplayItem::Text { + font: crate::render::FontFace::Bold, + .. + } + ))); + } #[test] fn pdf_writer_uses_a_bold_font_resource() { let mut list = crate::render::DisplayList::default(); diff --git a/crates/texrra-tex-core/src/paragraph.rs b/crates/texrra-tex-core/src/paragraph.rs index 2c726bc..857b710 100755 --- a/crates/texrra-tex-core/src/paragraph.rs +++ b/crates/texrra-tex-core/src/paragraph.rs @@ -57,11 +57,27 @@ impl Default for ParagraphStyle { } } -#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum ParagraphFont { + Roman, + Bold, + Italic, +} + +impl Default for ParagraphFont { + fn default() -> Self { + Self::Roman + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub struct Paragraph { pub lines: Vec>, pub height: i64, pub width: i64, + #[serde(default)] + pub font: ParagraphFont, } impl Paragraph { @@ -118,6 +134,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra lines: Vec::new(), height: 0, width: 0, + font: ParagraphFont::Roman, }; } let mut breakpoints = vec![0_usize]; @@ -286,6 +303,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra height: lines.len() as i64 * style.line_height, width: max_width, lines, + font: ParagraphFont::Roman, } } diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 2c6668c..2a0472d 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -7,6 +7,7 @@ use crate::cmr10::Cmr10Metrics; use crate::math::{self, MathAtom}; +use crate::paragraph::ParagraphFont; use crate::{FloatPlacement, Halign, HorizontalItem, Page, VerticalItem}; /// One absolute drawing primitive in scaled points. @@ -344,6 +345,11 @@ impl Page { y += *height + *depth; } VerticalItem::Paragraph(paragraph) => { + let face = match paragraph.font { + ParagraphFont::Roman => FontFace::Roman, + ParagraphFont::Bold => FontFace::Bold, + ParagraphFont::Italic => FontFace::Italic, + }; let lines = paragraph.lines.len().max(1) as i64; let line_height = (paragraph.height / lines).max(1); let size = ((line_height as f64) * 0.7) as i64; @@ -355,7 +361,7 @@ impl Page { HorizontalItem::Box(box_) => { if let Some(character) = box_.character { list.push(DisplayItem::Text { - font: FontFace::Roman, + font: face, x, y: baseline, size, diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 7d75022..55c4e8f 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -228,7 +228,7 @@ 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 | | 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 | -| M5 NFSS/TFM | partial | CMR10 widths, per-face Base-14 PDF resources, and SVG font-family/weight/style for Roman/Bold/Italic/Mono; TFM loading and font embedding absent | +| M5 NFSS/TFM | partial | CMR10 widths, per-face Base-14 PDF resources, and SVG font-family/weight/style for Roman/Bold/Italic/Mono; paragraph units carry a ParagraphFont face; TFM loading and font embedding absent | | 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 |