diff --git a/README.md b/README.md index e4eb8f9..9120f4d 100644 --- a/README.md +++ b/README.md @@ -488,7 +488,7 @@ advisory breaks, starred `\vspace`/`\hspace`, `center`, `\hrule`, `\fbox`, | Fonts | serif/sans/mono faces, bold/italic/mono/sans declarations, group scoping, true scaled small caps | — | | Paragraphs | Knuth-Plass breaking, TeX hyphenation, justification, `\centering`/`\raggedright`/`\raggedleft`, `\parindent` (17pt default), `\hfill`/`\hfil` | — | | Notes/floats | footnotes with page-bottom insertion area, title/author `\thanks`, figure/table `[htbp]` placement (top/bottom/page/here approximations) | deferred float queue, `\textfraction` | -| Contents | `\tableofcontents`, `\listoffigures`, `\listoftables` generated from numbered headings and captions, page numbers resolved after pagination | dotted leaders, per-chapter book/report contents | +| Contents | `\tableofcontents`, `\listoffigures`, `\listoftables` generated from numbered headings and captions, dotted leaders, page numbers resolved after pagination | per-chapter book/report contents | | Pages | A4/Letter/... page sizes, margins/geometry, automatic pagination, `\newpage`, `\pagebreak`, plain/fancy page styles, `\thispagestyle`, fancyhdr L/C/R headers and footers | — | | 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 cache, page-hash diffing, SVG refresh | — | diff --git a/crates/texrra-core/src/document.rs b/crates/texrra-core/src/document.rs index 735e863..7d6cdbb 100644 --- a/crates/texrra-core/src/document.rs +++ b/crates/texrra-core/src/document.rs @@ -167,6 +167,9 @@ pub struct PageStyleOverride { /// Marker character used by `\thepage` inside header/footer runs; the page /// renderer substitutes the concrete page number before measuring and painting. +/// Private-use marker for a dotted leader in generated contents lines. +pub const CONTENTS_LEADER_MARKER: char = '\u{F6000}'; + pub const PAGE_NUMBER_MARKER: char = '\u{F5000}'; pub fn page_number_marker_text() -> String { diff --git a/crates/texrra-core/src/document_parser.rs b/crates/texrra-core/src/document_parser.rs index 63ee9f4..9c89c76 100644 --- a/crates/texrra-core/src/document_parser.rs +++ b/crates/texrra-core/src/document_parser.rs @@ -427,6 +427,9 @@ fn contents_kind(block: &Block) -> Option { fn contents_line_runs(title: &[Run], target: usize) -> Vec { let mut runs = title.to_vec(); trim_run_end(&mut runs); + runs.push(Run::plain( + crate::document::CONTENTS_LEADER_MARKER.to_string(), + )); let mut filler = Run::plain(String::new()); filler.glue = Some(TeXGlue { width: common::Scaled::ZERO, diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 70e9cf2..41a17a0 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -9,7 +9,7 @@ family and true scaled small caps; inline box geometry (`\fbox`, `\makebox`, footnotes/`\thanks`; figure/table `[htbp]` placement approximations; page styles (plain/fancy, `\thispagestyle`, fancyhdr headers/footers, `\thepage`); geometry-aware `\textwidth`/`\textheight` named length registers; table of -contents and figure/table lists; native PDF rectangles; unified pagination; +contents and figure/table lists with dotted leaders; native PDF rectangles; unified pagination; `\centering`/`\raggedright`/`\raggedleft` declarations and environments (`build_positioned_pages` is the only page builder and no separate break planner remains). @@ -26,12 +26,7 @@ planner remains). needs a vertical-material capture with its own natural width, baseline, and nested line/box children in the positioned renderer. -## 3. Contents leaders (remaining) - -- TOC/list entries use infinite glue for right-aligned page numbers. Dotted - leaders require a glue item that repeats a character across its stretch. - -## 4. Custom title blocks (remaining) +## 3. Custom title blocks (remaining) - Let a document-level `\renewcommand{\maketitle}` contribute a real title block once the engine's title model can run box material. diff --git a/docs/STATUS.md b/docs/STATUS.md index 2d1f44d..6d0406f 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -35,7 +35,8 @@ the source changes; the SVG preview is refreshed per page. superscript markers, and title/author `\thanks`. - Contents: `\tableofcontents`, `\listoffigures`, and `\listoftables` generate headings plus entries from numbered sections and captioned - figures/tables; page numbers are resolved after pagination. + figures/tables, with dotted leaders and page numbers resolved after + pagination. - 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). @@ -62,9 +63,8 @@ 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. -- Contents: entries use infinite glue instead of dotted leaders and only - numbered headings plus captions are listed; per-chapter contents in - book/report are not distinguished. +- Contents: only numbered headings plus captions are listed; per-chapter + contents in book/report are not distinguished. - Miscellaneous: a document-level `\renewcommand{\maketitle}` is ignored in favor of the engine title block. diff --git a/src/rust_typeset/pdf.rs b/src/rust_typeset/pdf.rs index f70659b..ac5d8a8 100644 --- a/src/rust_typeset/pdf.rs +++ b/src/rust_typeset/pdf.rs @@ -1228,6 +1228,18 @@ fn boxworks_line_break( previous_font_id = None; } for character in run.text.chars() { + if character == texrra_core::document::CONTENTS_LEADER_MARKER { + horizontal.push( + ds::Char { + char: character, + font: null_font_id, + } + .into(), + ); + previous_character = None; + previous_font_id = None; + continue; + } if inline_markers.contains_key(&character) { horizontal.push( ds::Char { @@ -1423,9 +1435,14 @@ fn boxworks_line_break( } } let mut line_items = Vec::new(); + let mut leader_pending = false; for item in &line_box.list { match item { ds::Horizontal::Char(character) => { + if character.char == texrra_core::document::CONTENTS_LEADER_MARKER { + leader_pending = true; + continue; + } if let Some(marker) = inline_markers.get(&character.char) { match marker { InlineMarker::RaiseStart(raise) => { @@ -1472,11 +1489,13 @@ fn boxworks_line_break( } } ds::Horizontal::Glue(glue) => { - line_items.push(text_layout::LineItem::Glue(paragraph_glue_width( - &glue.value, - line_box.glue_order, - ratio, - ))); + let width = paragraph_glue_width(&glue.value, line_box.glue_order, ratio); + if leader_pending { + leader_pending = false; + line_items.push(text_layout::LineItem::Leader(width)); + } else { + line_items.push(text_layout::LineItem::Glue(width)); + } } ds::Horizontal::Kern(kern) => { line_items.push(text_layout::LineItem::Kern(kern.width)); diff --git a/src/rust_typeset/text_layout.rs b/src/rust_typeset/text_layout.rs index 77dd931..6d1c5b7 100644 --- a/src/rust_typeset/text_layout.rs +++ b/src/rust_typeset/text_layout.rs @@ -42,6 +42,7 @@ pub(super) enum LineItem { }, Kern(Scaled), Glue(Scaled), + Leader(Scaled), } struct TextSpan { @@ -105,6 +106,7 @@ enum TextLineItem { }, Kern(Mm), Glue(Mm), + Leader(Mm), } pub(super) struct PositionedRule { @@ -245,6 +247,18 @@ impl TextLine { ); items.push(TextLineItem::Glue(scaled_to_mm(*width))); } + LineItem::Leader(width) => { + flush_text( + &mut items, + &mut text, + &mut text_style, + cjk_family, + monospace_family, + sans_family, + font_size, + ); + items.push(TextLineItem::Leader(scaled_to_mm(*width))); + } LineItem::Kern(width) => { flush_text( &mut items, @@ -311,6 +325,7 @@ impl TextLine { } TextLineItem::Glue(_) if index < last_content => x += natural_space, TextLineItem::Glue(_) => {} + TextLineItem::Leader(width) => x += *width, TextLineItem::Kern(width) => x += *width, TextLineItem::Rule { width, .. } => x += *width, TextLineItem::Frame { width, .. } => x += *width, @@ -393,6 +408,32 @@ impl TextLine { natural_space }; } + TextLineItem::Leader(width) => { + let style = Style::new().with_font_size(self.font_size); + let dot = style.str_width(fonts, "."); + let space = style.str_width(fonts, " "); + let unit = f64::from(dot + space); + let width_mm = f64::from(*width); + let count = ((width_mm / unit).floor() as usize).max(1); + let mut leader = String::new(); + for index in 0..count { + if index > 0 { + leader.push(' '); + } + leader.push('.'); + } + let text_width = style.str_width(fonts, &leader); + spans.push(PositionedSpan { + text: leader, + style, + x, + y_offset: Mm::from(0.0), + }); + x += text_width; + if text_width < *width { + x += *width - text_width; + } + } TextLineItem::Kern(width) => x += *width, } } diff --git a/tests/typeset.rs b/tests/typeset.rs index 77dec85..ba4661c 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -804,12 +804,12 @@ Beta paragraph. assert_eq!(value["status"], "ok", "{value}"); assert_eq!(pages(&value).len(), 2, "{value}"); let svg = page_svg(&value, 0); - eprintln!("{svg}"); assert!(svg.contains(">Contents"), "missing contents heading"); assert!(svg.contains(">Alpha"), "missing first entry"); assert!(svg.contains(">Beta"), "missing second entry"); assert!(svg.contains(">1"), "missing first page number"); assert!(svg.contains(">2"), "missing second page number"); + assert!(svg.contains(". . ."), "missing dotted leader: {svg}"); } #[test]