Add dotted leaders to generated contents entries

This commit is contained in:
gloamxun committed 2026-10-19 14:20:30 +08:00
1 parent 1b0ce27ed3
commit 6d4aa37189
8 files changed
+79 -18

No files matched your search

+1 -1
View File
@@ -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 | — |
+3
View File
@@ -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 {
@@ -427,6 +427,9 @@ fn contents_kind(block: &Block) -> Option<ContentsKind> {
fn contents_line_runs(title: &[Run], target: usize) -> Vec<Run> {
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,
+2 -7
View File
@@ -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.
+4 -4
View File
@@ -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.
+24 -5
View File
@@ -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));
+41
View File
@@ -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,
}
}
+1 -1
View File
@@ -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</text>"), "missing contents heading");
assert!(svg.contains(">Alpha</text>"), "missing first entry");
assert!(svg.contains(">Beta</text>"), "missing second entry");
assert!(svg.contains(">1</text>"), "missing first page number");
assert!(svg.contains(">2</text>"), "missing second page number");
assert!(svg.contains(". . ."), "missing dotted leader: {svg}");
}
#[test]