diff --git a/README.md b/README.md index b035fdd..79266bd 100644 --- a/README.md +++ b/README.md @@ -304,12 +304,14 @@ rule reserves space without painting ink. `\fbox{...}` is an inline framed box: the content is measured as a single unbreakable unit, wrapped in 3pt padding and a 0.4pt frame, and painted in both the PDF and the SVG preview. -Known gaps for full document classes: base font-size class options -(`10pt`/`11pt`/`12pt`) are parsed but the engine still uses 11pt; -custom `\maketitle` implementations are ignored (the engine title layout is -used), `\parbox`/`\minipage` are block-level rather than inline, `\vbox` and -`\vtop` map to a full-width block box rather than true nested VBox material, and -the abstract prints as body text without an "Abstract" heading. +Base font-size class options `10pt`, `11pt`, and `12pt` now set the body +size and propagate through headings, title material, lists, tables, block +boxes, footnotes, and the default paragraph indent. +Known gaps for full document classes: custom `\maketitle` implementations +are ignored (the engine title layout is used), `\parbox`/`\minipage` are +block-level rather than inline, `\vbox` and `\vtop` map to a full-width block +box rather than true nested VBox material, and the abstract prints as body text +without an "Abstract" heading. `\footnote{...}` captures its content, emits a superscript marker, reserves space on the page where the paragraph ends, and paints the notes with a @@ -480,7 +482,7 @@ advisory breaks, starred `\vspace`/`\hspace`, `center`, `\hrule`, `\fbox`, | Paragraphs | Knuth-Plass breaking, TeX hyphenation, justification, centering, `\parindent` (17pt default), `\hfill`/`\hfil` | `\raggedright`/`\raggedleft` | | Notes/floats | footnotes with page-bottom insertion area, title/author `\thanks`, figure/table `[htbp]` placement (top/bottom/page/here approximations) | deferred float queue, `\textfraction` | | Pages | A4/Letter/... page sizes, margins/geometry, automatic pagination, `\newpage`, `\pagebreak`, plain/fancy page styles, `\thispagestyle`, fancyhdr L/C/R headers and footers | unified planner/renderer | -| Classes/packages | built-in + local classes, option protocol, local package options | base font-size options (10/11/12pt) | +| Classes/packages | built-in + local classes, option protocol, local package options, base font sizes 10/11/12pt | class-specific section formatting | | Live preview | incremental paragraph/page caches, page-hash diffing, SVG refresh | — | The compatibility layer also covers spacing macros, text symbols/letters, style diff --git a/crates/texrra-core/src/document.rs b/crates/texrra-core/src/document.rs index 11e5868..261e07c 100644 --- a/crates/texrra-core/src/document.rs +++ b/crates/texrra-core/src/document.rs @@ -68,6 +68,8 @@ pub struct DocumentModel { pub date: String, pub page: PageSize, pub parindent: common::Scaled, + /// Base font size selected by the document class (10, 11, or 12pt). + pub base_font_size: u8, pub margins: PageMargins, pub show_title: bool, pub blocks: Vec, @@ -88,6 +90,7 @@ impl Default for DocumentModel { page: PageSize::default(), // article's default \parindent for 10/11pt text. parindent: common::Scaled(17 * 65_536), + base_font_size: 11, margins: PageMargins::default(), show_title: false, blocks: Vec::new(), diff --git a/crates/texrra-core/src/document_parser.rs b/crates/texrra-core/src/document_parser.rs index 443c924..1cbcb64 100644 --- a/crates/texrra-core/src/document_parser.rs +++ b/crates/texrra-core/src/document_parser.rs @@ -853,6 +853,7 @@ impl Parser { self.finish_metadata(*field); } expanded::FrontendEventKind::Geometry(settings) => { + apply_class_font_size(&mut self.document, settings); apply_geometry(&mut self.document, settings); } expanded::FrontendEventKind::TextStyleStart(style) => self.begin_style(*style), @@ -2074,6 +2075,21 @@ fn apply_geometry(document: &mut DocumentModel, settings: &str) { apply_geometry_settings(&mut document.page, &mut document.margins, settings); } +/// Apply article-class base font size options (10pt, 11pt, 12pt). +fn apply_class_font_size(document: &mut DocumentModel, settings: &str) { + for setting in settings.split(',').map(str::trim) { + let size = match setting { + "10pt" => 10, + "11pt" => 11, + "12pt" => 12, + _ => continue, + }; + document.base_font_size = size; + let indent = if size == 11 { 17 } else { 15 }; + document.parindent = common::Scaled(indent * 65_536); + } +} + /// Apply comma-separated `geometry` settings to a page size and margins. /// /// This is shared by the document parser and by the frontend's live length diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index f2852d8..986cb3e 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -17,10 +17,13 @@ and geometry-aware `\textwidth`/`\textheight` named length registers. ordered list of placed items, then use it for both page-break decisions and final painting. This is the prerequisite for a true deferred float queue. -## 2. Base font size +## 2. Base font size (implemented) -- Thread the `10pt`/`11pt`/`12pt` class option through the parser, line metrics, - headings, and the PDF so the base size changes consistently. +- `DocumentModel.base_font_size` is set by the class option (default 11) + and threaded through both the page planner and the positioned renderer: + body lines, headings (`article_heading_font_size`), title material, lists, + tables, block boxes, footnotes, and the default `\parindent` (15pt for + 10/12pt, 17pt for 11pt). ## 3. Table of contents and lists diff --git a/docs/STATUS.md b/docs/STATUS.md index 2af3fe0..9038ad5 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -54,10 +54,12 @@ See `docs/ROADMAP.md` for concrete implementation plans. - Notes: footnotes inside captions, floats, or arbitrary boxes are not collected; the insertion area currently follows the page where the paragraph ends rather than the exact reference line. -- Miscellaneous: `\tableofcontents`/list commands are no-ops; base font-size - class options (10/11/12pt) are parsed but unused; rules are painted with 1x1 - images in PDF; a document-level `\renewcommand{\maketitle}` is ignored in - favor of the engine title block. +- Class sizes: 10pt/11pt/12pt set the body size and propagate through + headings, title material, lists, tables, block boxes, footnotes, and the + default paragraph indent (10/12pt use 15pt; 11pt uses 17pt). +- Miscellaneous: \tableofcontents/list commands are no-ops; rules are + painted with 1x1 images in PDF; a document-level \renewcommand{\maketitle} + is ignored in favor of the engine title block. ## Verification diff --git a/src/rust_typeset/pdf.rs b/src/rust_typeset/pdf.rs index 8471187..ede8edd 100644 --- a/src/rust_typeset/pdf.rs +++ b/src/rust_typeset/pdf.rs @@ -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; } diff --git a/src/rust_typeset/pdf/layout.rs b/src/rust_typeset/pdf/layout.rs index 386342e..9e1fcf4 100644 --- a/src/rust_typeset/pdf/layout.rs +++ b/src/rust_typeset/pdf/layout.rs @@ -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 { 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, + font_size: u8, ) -> Option { 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>, sans_family: Option>, 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 { - 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> { - 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, diff --git a/tests/typeset.rs b/tests/typeset.rs index 58ea4a5..8409ff5 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -759,3 +759,32 @@ fn letter_paper_updates_text_width_length() { "letter textwidth is wrong: {width}" ); } + +#[test] +fn document_class_font_size_scales_body_and_headings() { + for (option, body_size, heading_size) in [ + ("10pt", "3.5278", "4.9389"), + ("11pt", "3.8806", "4.9389"), + ("12pt", "4.2333", "5.9972"), + ] { + let source = format!( + r"\documentclass[{option}]{{article}} +\begin{{document}} +\section{{Heading}} +Body text. +\end{{document}} +" + ); + let value = fresh_preview(&source, "fontsize.tex"); + assert_eq!(value["status"], "ok", "{option}: {}", value["message"]); + let svg = page_svg(&value, 0); + assert!( + svg.contains(&format!("font-size=\"{body_size}\" font-weight=\"400\"")), + "{option}: body size {body_size} missing" + ); + assert!( + svg.contains(&format!("font-size=\"{heading_size}\" font-weight=\"700\"")), + "{option}: heading size {heading_size} missing" + ); + } +}