Show the current heading in headings-style headers
The headings page style only painted the page number. Document now records its heading units (unit index and title) during layout, and a page_headings helper carries the latest heading from page to page through the page sources. The running-head pass uses that list in headings style: the numbered heading title is uppercased at the left of the header and the page number stays right-aligned, with the first page still plain. The headings test gains a section and checks "1 INTRODUCTION" sits at the left of the header baseline.
This commit is contained in:
1 parent
ae810867f7
commit
9da0f136a4
3 files changed
+55
-2
No files matched your search
+50
-1
@@ -7246,12 +7246,14 @@ pub fn pdf_bytes_with_images(
|
||||
geometry.suppress_first_page_number,
|
||||
);
|
||||
}
|
||||
let headings = page_headings(&document, &engine);
|
||||
stamp_running_heads(
|
||||
&mut pages,
|
||||
&geometry,
|
||||
document.page_goal,
|
||||
document.text_width,
|
||||
document.paper_height,
|
||||
&headings,
|
||||
);
|
||||
let metadata = PdfMetadata {
|
||||
title: geometry
|
||||
@@ -13011,6 +13013,15 @@ fn build_document_with_labels(
|
||||
}
|
||||
}
|
||||
let mut document = Document::new(700 * SCALE, units);
|
||||
for (index, paragraph) in paragraphs.iter().enumerate() {
|
||||
if paragraph.heading_level.is_some()
|
||||
&& let Some(unit) = unit_for_paragraph.get(index)
|
||||
{
|
||||
document
|
||||
.headings
|
||||
.push((*unit as u64, scrub_title_metadata(¶graph.text)));
|
||||
}
|
||||
}
|
||||
if footnote_count > 0 {
|
||||
document.insertion_classes.push(InsertionClass {
|
||||
number: 0,
|
||||
@@ -13088,12 +13099,33 @@ fn page_number_text(index: usize, style: &str, start: u32) -> String {
|
||||
/// Draw fancyhdr's running header/footer fields on every page. Positions are
|
||||
/// content-relative: the header sits above the first baseline and the footer
|
||||
/// below the page goal, so the PDF writer's page origin shifts both correctly.
|
||||
/// Latest heading title in force on each page, derived from the page sources.
|
||||
fn page_headings(document: &Document, engine: &Engine) -> Vec<Option<String>> {
|
||||
let by_unit: std::collections::HashMap<u64, &str> = document
|
||||
.headings
|
||||
.iter()
|
||||
.map(|(unit, title)| (*unit, title.as_str()))
|
||||
.collect();
|
||||
let mut current: Option<String> = None;
|
||||
let mut result = Vec::new();
|
||||
for page in engine.pages() {
|
||||
for (unit, _) in &page.sources {
|
||||
if let Some(title) = by_unit.get(unit) {
|
||||
current = Some((*title).to_string());
|
||||
}
|
||||
}
|
||||
result.push(current.clone());
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn stamp_running_heads(
|
||||
lists: &mut [DisplayList],
|
||||
geometry: &PageGeometry,
|
||||
page_goal: i64,
|
||||
text_width: i64,
|
||||
paper_height: i64,
|
||||
headings: &[Option<String>],
|
||||
) {
|
||||
if (!geometry.fancy_pagestyle && !geometry.headings_pagestyle) || geometry.suppress_page_numbers
|
||||
{
|
||||
@@ -13108,6 +13140,15 @@ fn stamp_running_heads(
|
||||
// LaTeX's headings style uses plain on the first page.
|
||||
continue;
|
||||
}
|
||||
if let Some(Some(title)) = headings.get(index) {
|
||||
list.push(DisplayItem::Text {
|
||||
x: 0,
|
||||
y: baseline,
|
||||
size: 9 * SCALE,
|
||||
text: title.to_uppercase(),
|
||||
font: FontFace::Roman,
|
||||
});
|
||||
}
|
||||
let number = page_number_text(index, style, geometry.page_start);
|
||||
let width = text_width_roman(&number);
|
||||
list.push(DisplayItem::Text {
|
||||
@@ -13542,12 +13583,14 @@ fn render_json(
|
||||
geometry.suppress_first_page_number,
|
||||
);
|
||||
}
|
||||
let headings = page_headings(document, engine);
|
||||
stamp_running_heads(
|
||||
&mut lists,
|
||||
geometry,
|
||||
document.page_goal,
|
||||
document.text_width,
|
||||
document.paper_height,
|
||||
&headings,
|
||||
);
|
||||
for (index, (page, mut list)) in engine.pages().iter().zip(lists).enumerate() {
|
||||
list.place(document.h_offset, document.v_offset);
|
||||
@@ -16746,7 +16789,7 @@ A definition.
|
||||
|
||||
#[test]
|
||||
fn headings_pagestyle_puts_the_number_in_the_header() {
|
||||
let source = "\\documentclass{article}\n\\pagestyle{headings}\n\\begin{document}\n\nFirst.\n\n\\newpage\n\nSecond.\n\n\\end{document}\n";
|
||||
let source = "\\documentclass{article}\n\\pagestyle{headings}\n\\begin{document}\n\n\\section{Introduction}\n\nFirst.\n\n\\newpage\n\nSecond.\n\n\\end{document}\n";
|
||||
let json = preview_json(source).expect("preview");
|
||||
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
|
||||
assert_eq!(
|
||||
@@ -16757,6 +16800,12 @@ A definition.
|
||||
);
|
||||
assert_eq!(value["pageCount"].as_u64(), Some(2));
|
||||
let texts = svg_texts(&json);
|
||||
let title = texts
|
||||
.iter()
|
||||
.find(|(text, _, _)| text == "1 INTRODUCTION")
|
||||
.expect("header heading");
|
||||
assert!(title.1 < 100.0, "title should be left-aligned");
|
||||
assert!(title.2 < 0.0, "title should sit in the header");
|
||||
let number = texts
|
||||
.iter()
|
||||
.find(|(text, _, _)| text == "2")
|
||||
|
||||
Reference in new issue
Block a user