Expand fancy headers at document start and add header rules

This commit is contained in:
gloamxun committed 2026-10-19 15:28:54 +08:00
1 parent 4991bdfcfc
commit 3406f84e86
5 files changed
+157 -6

No files matched your search

+46 -4
View File
@@ -2844,13 +2844,14 @@ fn append_page_decorations(
let page_count = pages.len();
for (index, page) in pages.iter_mut().enumerate() {
let page_number = index + 1;
let mut header_bottom: Option<i64> = None;
let kind = effective_page_style_kind(model, index, page_count, block_start_pages);
if kind == PageStyleKind::Empty {
continue;
}
if kind == PageStyleKind::Plain {
let runs = [Run::plain(page_number.to_string())];
append_decoration(
let _ = append_decoration(
page,
&runs,
page_number,
@@ -2884,7 +2885,7 @@ fn append_page_decorations(
if runs.is_empty() {
continue;
}
append_decoration(
let end_y = append_decoration(
page,
runs,
page_number,
@@ -2901,8 +2902,20 @@ fn append_page_decorations(
math_layout,
cache,
);
if is_header && let Some(end_y) = end_y {
header_bottom =
Some(header_bottom.map_or(end_y, |current: i64| current.max(end_y)));
}
}
}
if let Some(bottom) = header_bottom {
append_header_rule(
page,
page_number,
bottom.saturating_add(i64::from(scaled_points(2.0).0)),
content_width_mm,
);
}
}
}
@@ -2923,7 +2936,7 @@ fn append_decoration(
math_metrics: &math_font::MathFontMetrics,
math_layout: &MathLayoutEnvironment,
cache: &mut ParagraphLayoutCache,
) {
) -> Option<i64> {
let runs = resolved_page_number_runs(runs, page_number);
let lines = measure_positioned_lines(
&runs,
@@ -2939,7 +2952,7 @@ fn append_decoration(
cache,
);
if lines.is_empty() {
return;
return None;
}
let body_width = millimeters_to_scaled(content_width_mm);
let content_height = millimeters_to_scaled(content_height_mm);
@@ -2981,6 +2994,35 @@ fn append_decoration(
}));
y = y.saturating_add(line.height);
}
is_header.then_some(y)
}
fn append_header_rule(page: &mut PositionedPage, page_number: usize, y: i64, width_mm: f64) {
let rule_height = i64::from(scaled_points(0.4).0).max(1);
let width = millimeters_to_scaled(width_mm);
page.items.push(PositionedPageItem::Line(PositionedLine {
anchor: PageAnchor {
block: usize::MAX,
line: page_number.saturating_mul(1024).saturating_add(99),
},
content: PositionedLineContent::Text {
line: text_layout::LineBox {
width: common::Scaled(width as i32),
items: vec![text_layout::LineItem::Rule {
width: common::Scaled(width as i32),
raise: common::Scaled::ZERO,
height: common::Scaled(rule_height as i32),
y_offset: common::Scaled::ZERO,
}],
pre_display_size: None,
},
font_size: 9,
},
y,
x: 0,
centered: false,
ragged: false,
}));
}
fn display_math_style() -> Style {
+76
View File
@@ -523,6 +523,20 @@ struct CompilerState {
geometry_page: texrra_core::document::PageSize,
#[serde(skip)]
geometry_margins: texrra_core::document::PageMargins,
/// Header/footer definitions are stored as token lists until
/// `\begin{document}` so that later `\renewcommand`s (for example
/// `\shorttitle` in journal styles) are visible when they expand.
#[serde(skip)]
pending_page_decorations: Vec<PendingPageDecoration>,
#[serde(skip)]
document_started: bool,
}
struct PendingPageDecoration {
token: token::Token,
header: bool,
position: texrra_core::document::PageDecorationPosition,
inner: Vec<token::Token>,
}
struct TexlangInputTrace {
@@ -1812,6 +1826,18 @@ fn fancy_decoration_primitive(
);
return Ok(());
}
if !input.state().document_started {
input
.state_mut()
.pending_page_decorations
.push(PendingPageDecoration {
token,
header,
position,
inner,
});
return Ok(());
}
write_event(
input,
token,
@@ -1831,6 +1857,44 @@ fn fancy_decoration_primitive(
Ok(())
}
fn flush_pending_page_decorations(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
let Some(pending) = (!input.state().pending_page_decorations.is_empty())
.then(|| input.state_mut().pending_page_decorations.remove(0))
else {
return Ok(());
};
write_event(
input,
pending.token,
FrontendEventKind::PageDecorationStart {
header: pending.header,
position: pending.position,
},
);
let trace_key = pending.token.trace_key();
let mut expansion = pending.inner;
expansion.push(control_sequence_token(
input,
"texrrapagedecorationend",
trace_key,
));
if !input.state().pending_page_decorations.is_empty() {
expansion.push(control_sequence_token(
input,
"texrraflushpagedecorations",
token.trace_key(),
));
}
input
.as_mut()
.expansions_mut()
.extend(expansion.into_iter().rev());
Ok(())
}
fn fancy_head_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
@@ -5801,6 +5865,14 @@ fn begin_environment_primitive(
.filter_map(|token| token.char())
.collect::<String>();
let name = name.trim().to_string();
if name == "document" && !input.state().document_started {
input.state_mut().document_started = true;
if !input.state().pending_page_decorations.is_empty() {
let trace_key = token.trace_key();
let flush = control_sequence_token(input, "texrraflushpagedecorations", trace_key);
input.as_mut().expansions_mut().push(flush);
}
}
let argument = if matches!(
name.as_str(),
"array" | "tabular" | "alignedat" | "alignat" | "alignat*"
@@ -6941,6 +7013,10 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
"texrrapagedecorationend",
texlang::command::BuiltIn::new_execution(page_decoration_end_primitive),
);
built_ins.insert(
"texrraflushpagedecorations",
texlang::command::BuiltIn::new_execution(flush_pending_page_decorations),
);
built_ins.insert(
"thepage",
texlang::command::BuiltIn::new_execution(page_number_primitive),