Scale paragraphs by their declared base font size

This commit is contained in:
gloamxun committed 2026-10-24 13:04:19 +08:00
1 parent aa4d86d735
commit 9876895a84
8 files changed
+249 -17

No files matched your search

+91 -6
View File
@@ -11,9 +11,7 @@ use std::sync::{Mutex, OnceLock};
use texrra_tex_core::cmr10::Cmr10Metrics;
use texrra_tex_core::math::{AlignedRow, MathAtom, parse_math, split_math_lines};
use texrra_tex_core::pdf::{
PageOrigin, display_lists_to_pdf_with_cm_type1, display_lists_to_pdf_with_paper,
};
use texrra_tex_core::pdf::{PageOrigin, display_lists_to_pdf_with_cm_type1};
use texrra_tex_core::{Document, Engine, ParagraphFont, ParagraphStyle, Unit};
const SCALE: i64 = 65_536;
@@ -654,6 +652,7 @@ struct SourceParagraph {
equation_rows: Vec<EquationRow>,
/// Whether the rows are centred (`gather`) rather than column-aligned.
align_centered: bool,
font_size: Option<i64>,
}
/// One row of an `align`/`gather` environment.
@@ -730,6 +729,7 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
font_size: None,
});
pending = (0, 0, 0);
pending_break = false;
@@ -887,6 +887,7 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
font_size: None,
}
})
.collect();
@@ -916,6 +917,7 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
font_size: None,
}];
}
}
@@ -954,6 +956,7 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
font_size: None,
}
})
.collect();
@@ -1030,7 +1033,56 @@ fn nfss_declaration<'a>(text: &'a str, command: &str) -> Option<&'a str> {
Some(rest)
}
/// TeX size commands with the article-class 10pt point size values.
const SIZE_COMMANDS: [(&str, f64); 10] = [
("\\tiny", 5.0),
("\\scriptsize", 7.0),
("\\footnotesize", 8.0),
("\\small", 9.0),
("\\normalsize", 10.0),
("\\large", 12.0),
("\\Large", 14.4),
("\\LARGE", 17.28),
("\\huge", 20.74),
("\\Huge", 24.88),
];
/// Scan a size declaration such as `\large` or
/// `\fontsize{14pt}{18pt}\selectfont`, returning the size and the rest.
fn font_size_command(rest: &str) -> Option<(i64, &str)> {
for (command, points) in SIZE_COMMANDS {
if let Some(after) = nfss_declaration(rest, command) {
return Some(((points * 65_536.0).round() as i64, after));
}
}
let after = rest.strip_prefix("\\fontsize")?;
if after
.chars()
.next()
.is_some_and(|character| character.is_ascii_alphabetic())
{
return None;
}
let after = after.trim_start();
let (size_text, length) = braced_group(after)?;
let size = parse_dimension(size_text.trim())?;
let after = after[length..].trim_start();
let (_, length) = braced_group(after)?;
let mut after = after[length..].trim_start();
if let Some(rest) = after.strip_prefix("\\selectfont") {
after = rest;
}
Some((size, after))
}
fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)> {
nfss_font_with_size(raw, base).map(|(font, _, body)| (font, body))
}
fn nfss_font_with_size(
raw: &str,
base: ParagraphFont,
) -> Option<(ParagraphFont, Option<i64>, String)> {
let (mut family, mut bold, mut italic) = match base {
ParagraphFont::Roman => (Family::Roman, false, false),
ParagraphFont::Bold => (Family::Roman, true, false),
@@ -1047,10 +1099,14 @@ fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)>
};
let mut small_caps = base == ParagraphFont::SmallCaps;
let mut slanted = matches!(base, ParagraphFont::Slanted | ParagraphFont::BoldSlanted);
let mut font_size: Option<i64> = None;
let mut text = raw.trim_start();
let mut consumed = false;
loop {
if let Some((name, rest)) = nfss_braced(text, "\\fontfamily") {
if let Some((size, rest)) = font_size_command(text) {
font_size = Some(size);
text = rest;
} else if let Some((name, rest)) = nfss_braced(text, "\\fontfamily") {
family = nfss_family_name(&name);
text = rest;
} else if let Some((name, rest)) = nfss_braced(text, "\\fontseries") {
@@ -1202,7 +1258,7 @@ fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)>
}
}
};
Some((font, text.to_string()))
Some((font, font_size, text.to_string()))
}
/// Split a paragraph containing inline style commands into styled runs.
@@ -1389,13 +1445,15 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
equation_tag: None,
equation_rows: rows,
align_centered: centered,
font_size: None,
};
}
}
if let Some((font, body)) = nfss_font(raw, ParagraphFont::Roman) {
if let Some((font, font_size, body)) = nfss_font_with_size(raw, ParagraphFont::Roman) {
return SourceParagraph {
text: body,
font,
font_size,
line_height: 12,
math: false,
left_skip: 0,
@@ -1431,6 +1489,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
font_size: None,
};
}
if let Some(inner) = raw
@@ -1453,6 +1512,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
font_size: None,
};
}
if let Some(math) = raw
@@ -1479,6 +1539,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
font_size: None,
};
}
let candidates = [
@@ -1506,6 +1567,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
font_size: None,
};
}
}
@@ -1525,6 +1587,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
font_size: None,
}
}
@@ -1646,6 +1709,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
} else {
paragraph.line_height * SCALE
},
font_size: paragraph.font_size.unwrap_or(655_360),
char_width: 10 * SCALE,
space_width: 4 * SCALE,
space_stretch: 6 * SCALE,
@@ -1764,6 +1828,26 @@ mod tests {
);
}
#[test]
fn size_commands_set_the_paragraph_font_size() {
let (body, geometry) = geometry_and_body("\\large Big text");
let paragraphs = paragraphs(&body).unwrap();
let document = build_document(&paragraphs, &geometry);
let unit = &document.units[0];
assert_eq!(unit.paragraph_style.as_ref().unwrap().font_size, 12 * SCALE);
assert_eq!(unit.source, "Big text");
}
#[test]
fn fontsize_sets_the_exact_paragraph_size() {
let (body, geometry) = geometry_and_body("\\fontsize{20pt}{24pt}\\selectfont Huge text");
let paragraphs = paragraphs(&body).unwrap();
let document = build_document(&paragraphs, &geometry);
let unit = &document.units[0];
assert_eq!(unit.paragraph_style.as_ref().unwrap().font_size, 20 * SCALE);
assert_eq!(unit.source, "Huge text");
}
#[test]
fn engine_template_keeps_paragraph_columns() {
assert_eq!(engine_template("{|l|c|p{3cm}|}"), "lcp{3cm}");
@@ -2522,6 +2606,7 @@ mod inline_tests {
assert_eq!(runs, vec![(ParagraphFont::Bold, "a b c".to_string())]);
}
#[test]
fn styled_runs_paint_with_their_face() {
let document = Document::new(
550 * 65_536,