Carry paragraph font faces through the display list

This commit is contained in:
gloamxun committed 2026-10-20 18:32:33 +08:00
1 parent 96c88d2787
commit 6b74a17312
4 files changed
+54 -5

No files matched your search

+27 -2
View File
@@ -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<String>, 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<String>,
@@ -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();
+19 -1
View File
@@ -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<Vec<HorizontalItem>>,
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,
}
}
+7 -1
View File
@@ -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,
+1 -1
View File
@@ -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 |