Add 10/11/12pt base font size options

This commit is contained in:
gloamxun committed 2026-10-19 13:52:24 +08:00
1 parent 269433abdb
commit 477e2124c8
8 files changed
+180 -69

No files matched your search

+20 -16
View File
@@ -37,11 +37,13 @@ use self::layout::{
// The local font renderer takes integer point sizes, so `\Large` is rounded
// from the article class's 14.4pt setting to 14pt.
fn article_heading_font_size(level: u8) -> u8 {
match level {
1 => 14,
2 => 12,
_ => 11,
fn article_heading_font_size(level: u8, base: u8) -> u8 {
match (base, level) {
(12, 1) => 17,
(_, 1) => 14,
(12, 2) => 14,
(_, 2) => 12,
_ => base,
}
}
@@ -111,6 +113,7 @@ struct PageLayoutConfig {
math_font_fingerprint: u64,
math_layout_font_fingerprint: u64,
page_style_fingerprint: u64,
base_font_size: u8,
}
struct PageLayoutSnapshot {
@@ -2305,6 +2308,7 @@ pub(super) fn render_pdf(
} = resources;
let page_break_plan = page_layout.plan;
let base_font_size = model.base_font_size;
if model.show_title {
if !model.title.is_empty() {
let mut title = Run::plain(model.title.clone());
@@ -2314,7 +2318,7 @@ pub(super) fn render_pdf(
&[title],
cjk_family,
mono_family,
20,
base_font_size + 9,
&math_metrics,
&math_layout,
)
@@ -2327,12 +2331,12 @@ pub(super) fn render_pdf(
&[Run::plain(model.author.clone())],
cjk_family,
mono_family,
12,
base_font_size + 1,
&math_metrics,
&math_layout,
)
.aligned(Alignment::Center),
Style::new().with_font_size(12),
Style::new().with_font_size(base_font_size + 1),
));
}
if !model.date.is_empty() {
@@ -2341,12 +2345,12 @@ pub(super) fn render_pdf(
&[Run::plain(model.date.clone())],
cjk_family,
mono_family,
11,
base_font_size,
&math_metrics,
&math_layout,
)
.aligned(Alignment::Center),
Style::new().with_font_size(11),
Style::new().with_font_size(base_font_size),
));
}
document.push(elements::Break::new(1.0));
@@ -2376,18 +2380,18 @@ pub(super) fn render_pdf(
&runs,
cjk_family,
mono_family,
11,
base_font_size,
&math_metrics,
&math_layout,
),
Style::new().with_font_size(11),
Style::new().with_font_size(base_font_size),
));
} else {
let lines = cached_paragraph_lines(
&runs,
content_width,
&box_metrics,
11,
base_font_size,
paragraph_layout_cache,
);
for (line_index, line) in lines.into_iter().enumerate() {
@@ -2407,9 +2411,9 @@ pub(super) fn render_pdf(
cjk_family,
mono_family,
sans_family,
11,
base_font_size,
),
Style::new().with_font_size(11),
Style::new().with_font_size(base_font_size),
));
}
}
@@ -2420,7 +2424,7 @@ pub(super) fn render_pdf(
}
}
Block::Heading { level, mut runs } => {
let size = article_heading_font_size(level);
let size = article_heading_font_size(level, base_font_size);
for run in &mut runs {
run.bold = true;
}
+91 -39
View File
@@ -466,6 +466,7 @@ fn append_list_layout_tokens(
math_metrics: &math_font::MathFontMetrics,
math_layout: &MathLayoutEnvironment,
cache: &mut ParagraphLayoutCache,
font_size: u8,
) -> Option<()> {
let topsep = latex_nested_list_topsep(depth);
tokens.push(ListLayoutToken::Gap {
@@ -477,7 +478,8 @@ fn append_list_layout_tokens(
outer_topsep: is_outer,
outer_bottomsep: false,
});
let (labels, local_indent_mm, label_x_mm) = list_layout_metrics(list, depth, font_cache, 11);
let (labels, local_indent_mm, label_x_mm) =
list_layout_metrics(list, depth, font_cache, font_size);
let text_x_mm = parent_indent_mm + local_indent_mm;
let item_width_mm = content_width_mm - text_x_mm;
if !list.items.is_empty() && item_width_mm <= 0.0 {
@@ -487,7 +489,7 @@ fn append_list_layout_tokens(
for (item_index, item) in list.items.iter().enumerate() {
let mut lines = measure_positioned_lines(
&item.runs,
11,
font_size,
item_width_mm.max(1.0),
metrics,
font_cache,
@@ -499,8 +501,13 @@ fn append_list_layout_tokens(
cache,
);
if lines.is_empty() {
let height =
font_line_height_scaled(font_cache, cjk_family, mono_family, sans_family, 11);
let height = font_line_height_scaled(
font_cache,
cjk_family,
mono_family,
sans_family,
font_size,
);
lines.push(MeasuredLine {
line_index: 0,
content: PositionedLineContent::Text {
@@ -509,7 +516,7 @@ fn append_list_layout_tokens(
items: Vec::new(),
pre_display_size: None,
},
font_size: 11,
font_size,
},
height,
ascent: height,
@@ -550,6 +557,7 @@ fn append_list_layout_tokens(
math_metrics,
math_layout,
cache,
font_size,
)?;
}
for glue in &item.vertical_glue_after {
@@ -602,6 +610,7 @@ fn layout_table(
math_metrics: &math_font::MathFontMetrics,
math_layout: &MathLayoutEnvironment,
cache: &mut ParagraphLayoutCache,
font_size: u8,
) -> Option<PositionedTable> {
let column_count = rows.iter().map(Vec::len).max().unwrap_or(0);
if column_count == 0 {
@@ -612,8 +621,14 @@ fn layout_table(
return None;
}
let empty_row_height =
font_line_height_scaled(font_cache, cjk_family, mono_family, sans_family, 10);
let cell_font_size = font_size.saturating_sub(1).max(1);
let empty_row_height = font_line_height_scaled(
font_cache,
cjk_family,
mono_family,
sans_family,
cell_font_size,
);
let mut positioned_rows = Vec::with_capacity(rows.len());
let mut total_height = 0_i64;
for row in rows {
@@ -623,7 +638,7 @@ fn layout_table(
let runs = row.get(column).map(Vec::as_slice).unwrap_or_default();
let lines = measure_positioned_lines(
runs,
10,
cell_font_size,
cell_width_mm,
metrics,
font_cache,
@@ -802,6 +817,7 @@ fn measure_framed_box(
cache: &mut ParagraphLayoutCache,
frame: bool,
width: Option<common::Scaled>,
font_size: u8,
) -> Option<MeasuredFramedBox> {
let padding_mm = if frame {
FRAMED_BOX_PADDING_PT * 25.4 / PDF_POINTS_PER_INCH
@@ -819,7 +835,7 @@ fn measure_framed_box(
if let Some(width) = width {
let box_width_mm = scaled_units_to_mm(i64::from(width.0));
let inner_mm = (box_width_mm - 2.0 * inset_mm).max(5.0);
let lines = cached_paragraph_lines(runs, inner_mm, metrics, 11, cache);
let lines = cached_paragraph_lines(runs, inner_mm, metrics, font_size, cache);
let mut content_height = 0_i64;
for line in &lines {
let (ascent, depth) = text_line_metrics_scaled(
@@ -828,11 +844,11 @@ fn measure_framed_box(
cjk_family,
mono_family,
sans_family,
11,
font_size,
);
content_height = content_height.saturating_add(ascent).saturating_add(depth);
}
let content_height = content_height.max(baseline_skip_scaled(11));
let content_height = content_height.max(baseline_skip_scaled(font_size));
return Some(MeasuredFramedBox {
lines,
width: millimeters_to_scaled(box_width_mm),
@@ -841,12 +857,18 @@ fn measure_framed_box(
frame,
});
}
let (line, ascent, descent) =
layout_framed_runs(runs, font_cache, cjk_family, mono_family, sans_family, 11);
let (line, ascent, descent) = layout_framed_runs(
runs,
font_cache,
cjk_family,
mono_family,
sans_family,
font_size,
);
let content_mm = f64::from(scaled_units_to_mm(i64::from(line.width.0))).min(max_content_mm);
let content_height =
i64::from(scaled_points(f64::from(ascent + descent) * PDF_POINTS_PER_INCH / 25.4).0)
.max(baseline_skip_scaled(11));
.max(baseline_skip_scaled(font_size));
Some(MeasuredFramedBox {
lines: vec![line],
width: millimeters_to_scaled(content_mm + 2.0 * inset_mm),
@@ -979,16 +1001,23 @@ fn measure_centered(
mono_family: Option<FontFamily<fonts::Font>>,
sans_family: Option<FontFamily<fonts::Font>>,
cache: &mut ParagraphLayoutCache,
font_size: u8,
) -> MeasuredCentered {
let lines = cached_paragraph_lines(runs, content_width_mm, metrics, 11, cache);
let lines = cached_paragraph_lines(runs, content_width_mm, metrics, font_size, cache);
let mut height = 0_i64;
for line in &lines {
let (ascent, depth) =
text_line_metrics_scaled(font_cache, line, cjk_family, mono_family, sans_family, 11);
let (ascent, depth) = text_line_metrics_scaled(
font_cache,
line,
cjk_family,
mono_family,
sans_family,
font_size,
);
height = height.saturating_add(ascent).saturating_add(depth);
}
if height == 0 {
height = baseline_skip_scaled(11);
height = baseline_skip_scaled(font_size);
}
MeasuredCentered { lines, height }
}
@@ -1008,8 +1037,14 @@ pub(super) fn build_page_break_plan(
paragraph_cache: &mut ParagraphLayoutCache,
page_layout_cache: &PageLayoutCache,
) -> Option<PageBreakPlan> {
let normal_line_height =
font_line_height_scaled(font_cache, cjk_family, mono_family, sans_family, 11);
let base_font_size = model.base_font_size;
let normal_line_height = font_line_height_scaled(
font_cache,
cjk_family,
mono_family,
sans_family,
base_font_size,
);
let top_skip = i64::from(scaled_points(ARTICLE_11PT_TOPSKIP_PT).0);
let max_depth = i64::from(scaled_points(ARTICLE_11PT_MAX_DEPTH_PT).0);
let page_goal = scaled_points(content_height_mm * PDF_POINTS_PER_INCH / 25.4).0 as i64;
@@ -1017,9 +1052,9 @@ pub(super) fn build_page_break_plan(
let mut pre_display_size = None;
if model.show_title {
for (text, size, bold) in [
(&model.title, 20, true),
(&model.author, 12, false),
(&model.date, 11, false),
(&model.title, base_font_size + 9, true),
(&model.author, base_font_size + 1, false),
(&model.date, base_font_size, false),
] {
if text.is_empty() {
continue;
@@ -1054,7 +1089,7 @@ pub(super) fn build_page_break_plan(
&mut nodes,
runs,
block_index,
11,
base_font_size,
content_width_mm,
font_cache,
cjk_family,
@@ -1068,7 +1103,7 @@ pub(super) fn build_page_break_plan(
&mut nodes,
runs,
Some(block_index),
11,
base_font_size,
content_width_mm,
metrics,
font_cache,
@@ -1111,6 +1146,7 @@ pub(super) fn build_page_break_plan(
paragraph_cache,
*frame,
*width,
base_font_size,
) else {
continue;
};
@@ -1140,6 +1176,7 @@ pub(super) fn build_page_break_plan(
mono_family,
sans_family,
paragraph_cache,
base_font_size,
);
nodes.push(VerticalNode::fixed_glue(
(normal_line_height as f64 * 0.35).round() as i64,
@@ -1181,7 +1218,7 @@ pub(super) fn build_page_break_plan(
for run in &mut runs {
run.bold = true;
}
let size = super::article_heading_font_size(*level);
let size = super::article_heading_font_size(*level, base_font_size);
pre_display_size = if runs.iter().any(|run| run.math.is_some()) {
append_vertical_inline_math(
&mut nodes,
@@ -1264,6 +1301,7 @@ pub(super) fn build_page_break_plan(
math_metrics,
math_layout,
paragraph_cache,
base_font_size,
)?;
let mut list_pre_display_size = None;
for token in tokens {
@@ -1387,6 +1425,7 @@ pub(super) fn build_page_break_plan(
math_metrics,
math_layout,
paragraph_cache,
base_font_size,
) {
for row in table.rows {
if row.height > page_goal {
@@ -1545,6 +1584,7 @@ pub(super) fn build_page_break_plan(
text_font_fingerprint: metrics.fingerprint,
math_font_fingerprint: math_metrics.fingerprint,
math_layout_font_fingerprint: math_layout.font_fingerprint,
base_font_size: model.base_font_size,
page_style_fingerprint: {
let mut hasher = DefaultHasher::new();
model.page_style.hash(&mut hasher);
@@ -1627,6 +1667,7 @@ fn footnote_reserve(
if numbers.is_empty() {
return 0;
}
let footnote_font_size = ((f32::from(model.base_font_size) * 0.82).round() as u8).max(1);
let width_mm = (content_width_mm - 8.0).max(20.0);
let mut height = 0_i64;
for number in numbers {
@@ -1635,7 +1676,7 @@ fn footnote_reserve(
};
for line in measure_positioned_lines(
&runs,
9,
footnote_font_size,
width_mm,
metrics,
font_cache,
@@ -1810,6 +1851,7 @@ fn append_page_footnotes(
math_layout: &MathLayoutEnvironment,
cache: &mut ParagraphLayoutCache,
) {
let footnote_font_size = ((f32::from(model.base_font_size) * 0.82).round() as u8).max(1);
let mut referenced = Vec::new();
for block in block_end_pages.keys() {
for number in footnote_numbers_for_block(model, *block) {
@@ -1858,7 +1900,7 @@ fn append_page_footnotes(
};
note_lines.extend(measure_positioned_lines(
&runs,
9,
footnote_font_size,
width_mm,
metrics,
font_cache,
@@ -1897,7 +1939,7 @@ fn append_page_footnotes(
},
content: PositionedLineContent::Text {
line: rule_line,
font_size: 9,
font_size: footnote_font_size,
},
y,
x: 0,
@@ -1936,8 +1978,14 @@ pub(super) fn build_positioned_pages(
math_layout: &MathLayoutEnvironment,
cache: &mut ParagraphLayoutCache,
) -> Option<Vec<PositionedPage>> {
let normal_line_height =
font_line_height_scaled(font_cache, cjk_family, mono_family, sans_family, 11);
let base_font_size = model.base_font_size;
let normal_line_height = font_line_height_scaled(
font_cache,
cjk_family,
mono_family,
sans_family,
base_font_size,
);
let top_skip = i64::from(scaled_points(ARTICLE_11PT_TOPSKIP_PT).0);
let max_depth = i64::from(scaled_points(ARTICLE_11PT_MAX_DEPTH_PT).0);
let paragraph_gap = (normal_line_height as f64 * 0.35).round() as i64;
@@ -1953,9 +2001,9 @@ pub(super) fn build_positioned_pages(
if model.show_title {
for (text, font_size, bold) in [
(&model.title, 20, true),
(&model.author, 12, false),
(&model.date, 11, false),
(&model.title, base_font_size + 9, true),
(&model.author, base_font_size + 1, false),
(&model.date, base_font_size, false),
] {
if text.is_empty() {
continue;
@@ -2016,9 +2064,9 @@ pub(super) fn build_positioned_pages(
match block {
Block::Paragraph(runs) | Block::Heading { runs, .. } => {
let (font_size, is_heading) = match block {
Block::Paragraph(_) => (11, false),
Block::Paragraph(_) => (base_font_size, false),
Block::Heading { level, .. } => {
let size = super::article_heading_font_size(*level);
let size = super::article_heading_font_size(*level, base_font_size);
(size, true)
}
_ => unreachable!("the outer match selected a text block"),
@@ -2119,6 +2167,7 @@ pub(super) fn build_positioned_pages(
cache,
*frame,
*width,
base_font_size,
) else {
continue;
};
@@ -2168,6 +2217,7 @@ pub(super) fn build_positioned_pages(
mono_family,
sans_family,
cache,
base_font_size,
);
let anchor = PageAnchor {
block: block_index,
@@ -2240,7 +2290,7 @@ pub(super) fn build_positioned_pages(
anchor,
content: PositionedLineContent::Text {
line,
font_size: 11,
font_size: base_font_size,
},
y: cursor,
x: 0,
@@ -2339,6 +2389,7 @@ pub(super) fn build_positioned_pages(
math_metrics,
math_layout,
cache,
base_font_size,
)?;
let mut list_pre_display_size = None;
for token in tokens {
@@ -2390,7 +2441,7 @@ pub(super) fn build_positioned_pages(
&label_runs,
content_width_mm,
metrics,
11,
base_font_size,
cache,
)
.into_iter()
@@ -2404,7 +2455,7 @@ pub(super) fn build_positioned_pages(
anchor,
content: PositionedLineContent::Text {
line: label_line,
font_size: 11,
font_size: base_font_size,
},
y: cursor,
x: millimeters_to_scaled(label_x_mm),
@@ -2505,6 +2556,7 @@ pub(super) fn build_positioned_pages(
math_metrics,
math_layout,
cache,
base_font_size,
) {
let PositionedTable {
rows: table_rows,