Add float placement and page headers/footers
This commit is contained in:
1 parent
1da46c61d6
commit
ed1da06685
16 files changed
+974
-90
No files matched your search
@@ -1,4 +1,5 @@
|
||||
pub(super) use texrra_core::document::{
|
||||
Block, ColumnAlignment, DocumentModel, EquationNumber, ListBlock, RuleBox, Run,
|
||||
Block, ColumnAlignment, DocumentModel, EquationNumber, ListBlock, PAGE_NUMBER_MARKER,
|
||||
PageDecoration, PageDecorationPosition, PageStyleKind, RuleBox, Run,
|
||||
};
|
||||
pub(super) use texrra_core::math_expr::*;
|
||||
@@ -350,21 +350,15 @@ const PRELUDE: &str = r#"
|
||||
\def~{ }
|
||||
\def\AtBeginDocument#1{#1}
|
||||
\def\newgeometry#1{\texrrageometry{#1}}
|
||||
\def\fancyhf{}
|
||||
\def\fancyheadoffset#1{}
|
||||
\def\fancyhead#1{}
|
||||
\def\fancyfoot#1{}
|
||||
\def\lhead#1{}
|
||||
\def\chead#1{}
|
||||
\def\rhead#1{}
|
||||
\def\lfoot#1{}
|
||||
\def\cfoot#1{}
|
||||
\def\rfoot#1{}
|
||||
\def\lhead#1{\fancyhead[L]{#1}}
|
||||
\def\chead#1{\fancyhead[C]{#1}}
|
||||
\def\rhead#1{\fancyhead[R]{#1}}
|
||||
\def\lfoot#1{\fancyfoot[L]{#1}}
|
||||
\def\cfoot#1{\fancyfoot[C]{#1}}
|
||||
\def\rfoot#1{\fancyfoot[R]{#1}}
|
||||
\def\headrulewidth{0.4pt}
|
||||
\def\footrulewidth{0pt}
|
||||
\def\pagestyle#1{}
|
||||
\def\thispagestyle#1{}
|
||||
\def\thepage{1}
|
||||
\def\hypersetup#1{}
|
||||
\def\microtypesetup#1{}
|
||||
\def\urlstyle#1{}
|
||||
|
||||
+48
-15
@@ -22,7 +22,8 @@ use super::engine::glue::TeXGlue;
|
||||
use super::page_builder::{self, GlueOrder, PageAnchor, VerticalNode};
|
||||
use super::{
|
||||
Block, ColumnAlignment, DocumentModel, EquationNumber, ListBlock, MathExpr,
|
||||
MathLayoutEnvironment, PDF_POINTS_PER_INCH, RuleBox, Run,
|
||||
MathLayoutEnvironment, PAGE_NUMBER_MARKER, PDF_POINTS_PER_INCH, PageDecoration,
|
||||
PageDecorationPosition, PageStyleKind, RuleBox, Run,
|
||||
box_font::{BoxFont, TeXHyphenator, scaled_points},
|
||||
is_cjk, math_font, math_layout, parse_dimension, prohibits_cjk_line_break, text_layout,
|
||||
};
|
||||
@@ -76,6 +77,7 @@ enum PageBlockKey {
|
||||
path: PathBuf,
|
||||
options: String,
|
||||
caption: Option<Vec<Run>>,
|
||||
placement: String,
|
||||
fingerprint: u64,
|
||||
},
|
||||
Bibliography(Vec<(String, Vec<Run>)>),
|
||||
@@ -108,6 +110,7 @@ struct PageLayoutConfig {
|
||||
text_font_fingerprint: u64,
|
||||
math_font_fingerprint: u64,
|
||||
math_layout_font_fingerprint: u64,
|
||||
page_style_fingerprint: u64,
|
||||
}
|
||||
|
||||
struct PageLayoutSnapshot {
|
||||
@@ -490,6 +493,8 @@ impl PositionedCentered {
|
||||
|
||||
struct PositionedPages {
|
||||
pages: Vec<PositionedPage>,
|
||||
content_margins: Margins,
|
||||
left_margin_mm: f64,
|
||||
next_page: usize,
|
||||
cjk_family: Option<FontFamily<fonts::Font>>,
|
||||
mono_family: Option<FontFamily<fonts::Font>>,
|
||||
@@ -499,12 +504,16 @@ struct PositionedPages {
|
||||
impl PositionedPages {
|
||||
fn new(
|
||||
pages: Vec<PositionedPage>,
|
||||
content_margins: Margins,
|
||||
left_margin_mm: f64,
|
||||
cjk_family: Option<FontFamily<fonts::Font>>,
|
||||
mono_family: Option<FontFamily<fonts::Font>>,
|
||||
sans_family: Option<FontFamily<fonts::Font>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
pages,
|
||||
content_margins,
|
||||
left_margin_mm,
|
||||
next_page: 0,
|
||||
cjk_family,
|
||||
mono_family,
|
||||
@@ -526,6 +535,9 @@ impl Element for PositionedPages {
|
||||
let page_index = self.next_page;
|
||||
self.next_page += 1;
|
||||
let page = &mut self.pages[page_index];
|
||||
let mut body_area = area.clone();
|
||||
body_area.add_margins(self.content_margins);
|
||||
let body_height = f64::from(body_area.size().height);
|
||||
|
||||
let mut used_height = 0.0_f64;
|
||||
for item in &mut page.items {
|
||||
@@ -540,8 +552,20 @@ impl Element for PositionedPages {
|
||||
let (result, y) = match item {
|
||||
PositionedPageItem::Line(planned_line) => {
|
||||
let y = scaled_units_to_mm(planned_line.y);
|
||||
let mut line_area = area.clone();
|
||||
line_area.add_offset(Position::new(scaled_units_to_mm(planned_line.x), y));
|
||||
let is_decoration = planned_line.anchor.block == usize::MAX;
|
||||
let line_area = if is_decoration {
|
||||
let mut decoration_area = area.clone();
|
||||
decoration_area.add_offset(Position::new(
|
||||
self.left_margin_mm + scaled_units_to_mm(planned_line.x),
|
||||
y,
|
||||
));
|
||||
decoration_area
|
||||
} else {
|
||||
let mut content_area = body_area.clone();
|
||||
content_area
|
||||
.add_offset(Position::new(scaled_units_to_mm(planned_line.x), y));
|
||||
content_area
|
||||
};
|
||||
let result = match &mut planned_line.content {
|
||||
PositionedLineContent::Text { line, font_size } => {
|
||||
let text_line = text_layout::TextLine::new(
|
||||
@@ -568,13 +592,13 @@ impl Element for PositionedPages {
|
||||
element, style, y, ..
|
||||
} => {
|
||||
let y = scaled_units_to_mm(*y);
|
||||
let mut math_area = area.clone();
|
||||
let mut math_area = body_area.clone();
|
||||
math_area.add_offset(Position::new(0.0, y));
|
||||
(element.render(context, math_area, *style)?, y)
|
||||
}
|
||||
PositionedPageItem::Table { element, y, .. } => {
|
||||
let y = scaled_units_to_mm(*y);
|
||||
let mut table_area = area.clone();
|
||||
let mut table_area = body_area.clone();
|
||||
table_area.add_offset(Position::new(0.0, y));
|
||||
(
|
||||
element.render(
|
||||
@@ -590,7 +614,7 @@ impl Element for PositionedPages {
|
||||
}
|
||||
PositionedPageItem::Image { element, y, .. } => {
|
||||
let y = scaled_units_to_mm(*y);
|
||||
let mut image_area = area.clone();
|
||||
let mut image_area = body_area.clone();
|
||||
image_area.add_offset(Position::new(0.0, y));
|
||||
(
|
||||
element.render(
|
||||
@@ -606,7 +630,7 @@ impl Element for PositionedPages {
|
||||
}
|
||||
PositionedPageItem::FramedBox { element, y, .. } => {
|
||||
let y = scaled_units_to_mm(*y);
|
||||
let mut box_area = area.clone();
|
||||
let mut box_area = body_area.clone();
|
||||
box_area.add_offset(Position::new(0.0, y));
|
||||
(
|
||||
element.render(
|
||||
@@ -622,7 +646,7 @@ impl Element for PositionedPages {
|
||||
}
|
||||
PositionedPageItem::Centered { element, y, .. } => {
|
||||
let y = scaled_units_to_mm(*y);
|
||||
let mut centered_area = area.clone();
|
||||
let mut centered_area = body_area.clone();
|
||||
centered_area.add_offset(Position::new(0.0, y));
|
||||
(
|
||||
element.render(
|
||||
@@ -642,7 +666,12 @@ impl Element for PositionedPages {
|
||||
format!(
|
||||
"Rust page layout placed a {kind} outside its page frame (page {}, y {y:.2}mm, area {:.2}mm)",
|
||||
page_index + 1,
|
||||
f64::from(area.size().height),
|
||||
if matches!(item, PositionedPageItem::Line(line) if line.anchor.block == usize::MAX)
|
||||
{
|
||||
f64::from(area.size().height)
|
||||
} else {
|
||||
body_height
|
||||
},
|
||||
),
|
||||
genpdf::error::ErrorKind::PageSizeExceeded,
|
||||
));
|
||||
@@ -2066,12 +2095,7 @@ impl RenderResources {
|
||||
model.page.height_mm as f32,
|
||||
));
|
||||
let mut decorator = genpdf::SimplePageDecorator::new();
|
||||
decorator.set_margins(Margins::trbl(
|
||||
model.margins.top,
|
||||
model.margins.right,
|
||||
model.margins.bottom,
|
||||
model.margins.left,
|
||||
));
|
||||
decorator.set_margins(Margins::trbl(0.0, 0.0, 0.0, 0.0));
|
||||
document.set_page_decorator(decorator);
|
||||
|
||||
let mono_family = inputs
|
||||
@@ -2249,6 +2273,13 @@ pub(super) fn render_pdf(
|
||||
} = resources;
|
||||
document.push(PositionedPages::new(
|
||||
pages,
|
||||
Margins::trbl(
|
||||
model.margins.top,
|
||||
model.margins.right,
|
||||
model.margins.bottom,
|
||||
model.margins.left,
|
||||
),
|
||||
model.margins.left,
|
||||
cjk_family,
|
||||
mono_family,
|
||||
sans_family,
|
||||
@@ -2474,6 +2505,7 @@ pub(super) fn render_pdf(
|
||||
caption,
|
||||
alignments,
|
||||
rules,
|
||||
..
|
||||
} => {
|
||||
if let Some(caption) = caption {
|
||||
document.push(
|
||||
@@ -2505,6 +2537,7 @@ pub(super) fn render_pdf(
|
||||
path,
|
||||
options,
|
||||
caption,
|
||||
..
|
||||
} => {
|
||||
let image_path = PathBuf::from(&path);
|
||||
let image_path = if image_path.is_absolute() {
|
||||
|
||||
+275
-21
@@ -176,6 +176,7 @@ fn page_block_keys(model: &DocumentModel, source_dir: &Path) -> Option<Vec<PageB
|
||||
caption,
|
||||
alignments,
|
||||
rules,
|
||||
..
|
||||
} => Some(PageBlockKey::Table {
|
||||
rows: rows
|
||||
.iter()
|
||||
@@ -196,6 +197,7 @@ fn page_block_keys(model: &DocumentModel, source_dir: &Path) -> Option<Vec<PageB
|
||||
path,
|
||||
options,
|
||||
caption,
|
||||
placement,
|
||||
} => {
|
||||
let image_path = resolved_image_path(source_dir, path);
|
||||
let bytes = fs::read(&image_path).ok()?;
|
||||
@@ -205,6 +207,7 @@ fn page_block_keys(model: &DocumentModel, source_dir: &Path) -> Option<Vec<PageB
|
||||
path: image_path,
|
||||
options: options.clone(),
|
||||
caption: caption.as_deref().map(normalize_page_runs),
|
||||
placement: placement.clone(),
|
||||
fingerprint: hasher.finish(),
|
||||
})
|
||||
}
|
||||
@@ -1335,6 +1338,7 @@ pub(super) fn build_page_break_plan(
|
||||
caption,
|
||||
alignments,
|
||||
rules,
|
||||
..
|
||||
} => {
|
||||
pre_display_size = None;
|
||||
let mut line_index = 0;
|
||||
@@ -1408,6 +1412,7 @@ pub(super) fn build_page_break_plan(
|
||||
path,
|
||||
options,
|
||||
caption,
|
||||
..
|
||||
} => {
|
||||
pre_display_size = None;
|
||||
let Some(figure) = layout_figure(
|
||||
@@ -1540,6 +1545,12 @@ 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,
|
||||
page_style_fingerprint: {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
model.page_style.hash(&mut hasher);
|
||||
model.page_style_overrides.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
},
|
||||
};
|
||||
let blocks = page_block_keys(model, source_dir)?;
|
||||
let breaks = if model.show_title {
|
||||
@@ -1934,6 +1945,7 @@ pub(super) fn build_positioned_pages(
|
||||
let page_goal = scaled_points(content_height_mm * PDF_POINTS_PER_INCH / 25.4).0 as i64;
|
||||
let mut pages = vec![PositionedPage::default()];
|
||||
let mut block_end_pages: HashMap<usize, usize> = HashMap::new();
|
||||
let mut block_start_pages: HashMap<usize, usize> = HashMap::new();
|
||||
let mut cursor = 0_i64;
|
||||
let mut has_previous_block = false;
|
||||
let mut pending_gap = 0_i64;
|
||||
@@ -1998,6 +2010,9 @@ pub(super) fn build_positioned_pages(
|
||||
}
|
||||
|
||||
for (block_index, block) in model.blocks.iter().enumerate() {
|
||||
block_start_pages
|
||||
.entry(block_index)
|
||||
.or_insert_with(|| pages.len().saturating_sub(1));
|
||||
match block {
|
||||
Block::Paragraph(runs) | Block::Heading { runs, .. } => {
|
||||
let (font_size, is_heading) = match block {
|
||||
@@ -2420,6 +2435,7 @@ pub(super) fn build_positioned_pages(
|
||||
caption,
|
||||
alignments,
|
||||
rules,
|
||||
..
|
||||
} => {
|
||||
pre_display_size = None;
|
||||
let mut line_index = 0;
|
||||
@@ -2562,6 +2578,7 @@ pub(super) fn build_positioned_pages(
|
||||
path,
|
||||
options,
|
||||
caption,
|
||||
placement,
|
||||
} => {
|
||||
pre_display_size = None;
|
||||
let Some(figure) = layout_figure(
|
||||
@@ -2591,28 +2608,50 @@ pub(super) fn build_positioned_pages(
|
||||
line: 0,
|
||||
};
|
||||
let block_gap = if has_previous_block { pending_gap } else { 0 };
|
||||
position_box(
|
||||
&mut pages,
|
||||
&mut cursor,
|
||||
page_breaks,
|
||||
anchor,
|
||||
figure_height,
|
||||
figure_height,
|
||||
block_gap,
|
||||
page_goal,
|
||||
top_skip,
|
||||
max_depth,
|
||||
);
|
||||
pages
|
||||
.last_mut()
|
||||
.expect("the page list starts with one page")
|
||||
.items
|
||||
.push(PositionedPageItem::Image {
|
||||
let bottom_float = placement.contains('b')
|
||||
&& !placement.contains('h')
|
||||
&& !placement.contains('t')
|
||||
&& cursor > 0
|
||||
&& cursor
|
||||
.saturating_add(block_gap)
|
||||
.saturating_add(figure_height)
|
||||
<= page_goal;
|
||||
if bottom_float {
|
||||
let y = page_goal.saturating_sub(figure_height);
|
||||
pages
|
||||
.last_mut()
|
||||
.expect("the page list starts with one page")
|
||||
.items
|
||||
.push(PositionedPageItem::Image {
|
||||
anchor,
|
||||
element: figure,
|
||||
y,
|
||||
});
|
||||
cursor = page_goal;
|
||||
} else {
|
||||
position_box(
|
||||
&mut pages,
|
||||
&mut cursor,
|
||||
page_breaks,
|
||||
anchor,
|
||||
element: figure,
|
||||
y: cursor,
|
||||
});
|
||||
cursor = cursor.saturating_add(figure_height);
|
||||
figure_height,
|
||||
figure_height,
|
||||
block_gap,
|
||||
page_goal,
|
||||
top_skip,
|
||||
max_depth,
|
||||
);
|
||||
pages
|
||||
.last_mut()
|
||||
.expect("the page list starts with one page")
|
||||
.items
|
||||
.push(PositionedPageItem::Image {
|
||||
anchor,
|
||||
element: figure,
|
||||
y: cursor,
|
||||
});
|
||||
cursor = cursor.saturating_add(figure_height);
|
||||
}
|
||||
pending_gap = paragraph_gap + (normal_line_height as f64 * 0.15).round() as i64;
|
||||
has_previous_block = true;
|
||||
}
|
||||
@@ -2750,6 +2789,10 @@ pub(super) fn build_positioned_pages(
|
||||
}
|
||||
Block::Penalty(_) => {}
|
||||
Block::PageBreak => {
|
||||
if pages.last().is_some_and(|page| !page.items.is_empty()) {
|
||||
pages.push(PositionedPage::default());
|
||||
cursor = 0;
|
||||
}
|
||||
pending_gap = 0;
|
||||
has_previous_block = false;
|
||||
pre_display_size = None;
|
||||
@@ -2773,9 +2816,220 @@ pub(super) fn build_positioned_pages(
|
||||
cache,
|
||||
);
|
||||
pages.retain(|page| !page.items.is_empty());
|
||||
append_page_decorations(
|
||||
model,
|
||||
&mut pages,
|
||||
&block_start_pages,
|
||||
content_width_mm,
|
||||
metrics,
|
||||
font_cache,
|
||||
cjk_family,
|
||||
mono_family,
|
||||
sans_family,
|
||||
math_metrics,
|
||||
math_layout,
|
||||
cache,
|
||||
);
|
||||
Some(pages)
|
||||
}
|
||||
|
||||
fn decoration_slot_runs(decoration: &PageDecoration, position: PageDecorationPosition) -> &[Run] {
|
||||
match position {
|
||||
PageDecorationPosition::Left => &decoration.left,
|
||||
PageDecorationPosition::Center => &decoration.center,
|
||||
PageDecorationPosition::Right => &decoration.right,
|
||||
}
|
||||
}
|
||||
|
||||
fn resolved_page_number_runs(runs: &[Run], page_number: usize) -> Vec<Run> {
|
||||
let number = page_number.to_string();
|
||||
runs.iter()
|
||||
.map(|run| {
|
||||
let mut run = run.clone();
|
||||
if run.text.contains(PAGE_NUMBER_MARKER) {
|
||||
run.text = run.text.replace(PAGE_NUMBER_MARKER, &number);
|
||||
}
|
||||
run
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn effective_page_style_kind(
|
||||
model: &DocumentModel,
|
||||
page_index: usize,
|
||||
page_count: usize,
|
||||
block_start_pages: &HashMap<usize, usize>,
|
||||
) -> PageStyleKind {
|
||||
let mut kind = model.page_style.kind;
|
||||
for override_ in &model.page_style_overrides {
|
||||
let page = block_start_pages
|
||||
.get(&override_.anchor)
|
||||
.copied()
|
||||
.unwrap_or_else(|| page_count.saturating_sub(1));
|
||||
if page == page_index {
|
||||
kind = override_.kind;
|
||||
}
|
||||
}
|
||||
kind
|
||||
}
|
||||
|
||||
fn append_page_decorations(
|
||||
model: &DocumentModel,
|
||||
pages: &mut [PositionedPage],
|
||||
block_start_pages: &HashMap<usize, usize>,
|
||||
content_width_mm: f64,
|
||||
metrics: &BoxFontMetrics,
|
||||
font_cache: &fonts::FontCache,
|
||||
cjk_family: Option<FontFamily<fonts::Font>>,
|
||||
mono_family: Option<FontFamily<fonts::Font>>,
|
||||
sans_family: Option<FontFamily<fonts::Font>>,
|
||||
math_metrics: &math_font::MathFontMetrics,
|
||||
math_layout: &MathLayoutEnvironment,
|
||||
cache: &mut ParagraphLayoutCache,
|
||||
) {
|
||||
let page_count = pages.len();
|
||||
for (index, page) in pages.iter_mut().enumerate() {
|
||||
let page_number = index + 1;
|
||||
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(
|
||||
page,
|
||||
&runs,
|
||||
page_number,
|
||||
false,
|
||||
PageDecorationPosition::Center,
|
||||
content_width_mm,
|
||||
model.page.height_mm,
|
||||
metrics,
|
||||
font_cache,
|
||||
cjk_family,
|
||||
mono_family,
|
||||
sans_family,
|
||||
math_metrics,
|
||||
math_layout,
|
||||
cache,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
for is_header in [false, true] {
|
||||
let decoration = if is_header {
|
||||
&model.page_style.header
|
||||
} else {
|
||||
&model.page_style.footer
|
||||
};
|
||||
for position in [
|
||||
PageDecorationPosition::Left,
|
||||
PageDecorationPosition::Center,
|
||||
PageDecorationPosition::Right,
|
||||
] {
|
||||
let runs = decoration_slot_runs(decoration, position);
|
||||
if runs.is_empty() {
|
||||
continue;
|
||||
}
|
||||
append_decoration(
|
||||
page,
|
||||
runs,
|
||||
page_number,
|
||||
is_header,
|
||||
position,
|
||||
content_width_mm,
|
||||
model.page.height_mm,
|
||||
metrics,
|
||||
font_cache,
|
||||
cjk_family,
|
||||
mono_family,
|
||||
sans_family,
|
||||
math_metrics,
|
||||
math_layout,
|
||||
cache,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn append_decoration(
|
||||
page: &mut PositionedPage,
|
||||
runs: &[Run],
|
||||
page_number: usize,
|
||||
is_header: bool,
|
||||
position: PageDecorationPosition,
|
||||
content_width_mm: f64,
|
||||
page_height_mm: f64,
|
||||
metrics: &BoxFontMetrics,
|
||||
font_cache: &fonts::FontCache,
|
||||
cjk_family: Option<FontFamily<fonts::Font>>,
|
||||
mono_family: Option<FontFamily<fonts::Font>>,
|
||||
sans_family: Option<FontFamily<fonts::Font>>,
|
||||
math_metrics: &math_font::MathFontMetrics,
|
||||
math_layout: &MathLayoutEnvironment,
|
||||
cache: &mut ParagraphLayoutCache,
|
||||
) {
|
||||
let runs = resolved_page_number_runs(runs, page_number);
|
||||
let lines = measure_positioned_lines(
|
||||
&runs,
|
||||
9,
|
||||
content_width_mm,
|
||||
metrics,
|
||||
font_cache,
|
||||
cjk_family,
|
||||
mono_family,
|
||||
sans_family,
|
||||
math_metrics,
|
||||
math_layout,
|
||||
cache,
|
||||
);
|
||||
if lines.is_empty() {
|
||||
return;
|
||||
}
|
||||
let body_width = millimeters_to_scaled(content_width_mm);
|
||||
let page_height = millimeters_to_scaled(page_height_mm);
|
||||
let top_skip = i64::from(scaled_points(ARTICLE_11PT_TOPSKIP_PT).0);
|
||||
let total_height: i64 = lines.iter().map(|line| line.height).sum();
|
||||
let mut y = if is_header {
|
||||
(top_skip / 2).max(0)
|
||||
} else {
|
||||
(page_height - millimeters_to_scaled(12.0) - total_height).max(0)
|
||||
};
|
||||
for (line_index, line) in lines.into_iter().enumerate() {
|
||||
let width = match &line.content {
|
||||
PositionedLineContent::Text { line, font_size } => {
|
||||
let text_line = text_layout::TextLine::new(
|
||||
line,
|
||||
cjk_family,
|
||||
mono_family,
|
||||
sans_family,
|
||||
*font_size,
|
||||
);
|
||||
millimeters_to_scaled(text_line.natural_width_mm(font_cache))
|
||||
}
|
||||
PositionedLineContent::InlineMath(_) => 0,
|
||||
};
|
||||
let x = match position {
|
||||
PageDecorationPosition::Left => 0,
|
||||
PageDecorationPosition::Center => (body_width - width) / 2,
|
||||
PageDecorationPosition::Right => body_width - width,
|
||||
};
|
||||
page.items.push(PositionedPageItem::Line(PositionedLine {
|
||||
anchor: PageAnchor {
|
||||
block: usize::MAX,
|
||||
line: page_number.saturating_mul(1024).saturating_add(line_index),
|
||||
},
|
||||
content: line.content,
|
||||
y,
|
||||
x,
|
||||
centered: false,
|
||||
}));
|
||||
y = y.saturating_add(line.height);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn append_vertical_text(
|
||||
nodes: &mut Vec<VerticalNode>,
|
||||
|
||||
@@ -1687,6 +1687,138 @@ fn page_break_primitive(
|
||||
execute_layout_primitive(LayoutPrimitive::PageBreak, token, input)
|
||||
}
|
||||
|
||||
fn page_style_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
let content = read_unexpanded_argument(input, token, "expects a page style name")?;
|
||||
let name = simple_token_text(strip_braced_tokens(&content)).to_ascii_lowercase();
|
||||
let kind = match name.trim() {
|
||||
"empty" => texrra_core::document::PageStyleKind::Empty,
|
||||
"fancy" => texrra_core::document::PageStyleKind::Fancy,
|
||||
_ => texrra_core::document::PageStyleKind::Plain,
|
||||
};
|
||||
write_event(input, token, FrontendEventKind::PageStyle(kind));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn this_page_style_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
let content = read_unexpanded_argument(input, token, "expects a page style name")?;
|
||||
let name = simple_token_text(strip_braced_tokens(&content)).to_ascii_lowercase();
|
||||
let kind = match name.trim() {
|
||||
"empty" => texrra_core::document::PageStyleKind::Empty,
|
||||
"fancy" => texrra_core::document::PageStyleKind::Fancy,
|
||||
_ => texrra_core::document::PageStyleKind::Plain,
|
||||
};
|
||||
write_event(input, token, FrontendEventKind::ThisPageStyle(kind));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fancy_decoration_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
header: bool,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
let position = read_optional_bracketed_argument(input, token, "decoration position")?
|
||||
.map(|tokens| simple_token_text(&tokens))
|
||||
.and_then(|text| text.chars().find(|character| !character.is_whitespace()));
|
||||
let position = match position {
|
||||
Some('L') | Some('l') => texrra_core::document::PageDecorationPosition::Left,
|
||||
Some('R') | Some('r') => texrra_core::document::PageDecorationPosition::Right,
|
||||
_ => texrra_core::document::PageDecorationPosition::Center,
|
||||
};
|
||||
let content = read_unexpanded_argument(input, token, "expects decoration content")?;
|
||||
let inner = strip_braced_tokens(&content).to_vec();
|
||||
if inner.is_empty() {
|
||||
write_event(
|
||||
input,
|
||||
token,
|
||||
FrontendEventKind::PageDecorationClear { header },
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
write_event(
|
||||
input,
|
||||
token,
|
||||
FrontendEventKind::PageDecorationStart { header, position },
|
||||
);
|
||||
let trace_key = token.trace_key();
|
||||
let mut expansion = inner;
|
||||
expansion.push(control_sequence_token(
|
||||
input,
|
||||
"texrrapagedecorationend",
|
||||
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>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
fancy_decoration_primitive(token, input, true)
|
||||
}
|
||||
|
||||
fn fancy_foot_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
fancy_decoration_primitive(token, input, false)
|
||||
}
|
||||
|
||||
fn fancy_hf_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
write_event(
|
||||
input,
|
||||
token,
|
||||
FrontendEventKind::PageDecorationClear { header: true },
|
||||
);
|
||||
write_event(
|
||||
input,
|
||||
token,
|
||||
FrontendEventKind::PageDecorationClear { header: false },
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn page_decoration_end_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
write_event(input, token, FrontendEventKind::PageDecorationEnd);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn page_number_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
write_event(input, token, FrontendEventKind::PageNumber);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn simple_token_text(tokens: &[token::Token]) -> String {
|
||||
let mut text = String::new();
|
||||
for token in tokens {
|
||||
match token.value() {
|
||||
token::Value::Letter(character)
|
||||
| token::Value::Other(character)
|
||||
| token::Value::Space(character) => text.push(character),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
fn scan_engine_dimension(
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<common::Scaled> {
|
||||
@@ -6572,6 +6704,34 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
|
||||
texlang::command::BuiltIn::new_execution(page_break_primitive)
|
||||
.with_tag(layout_primitive_tag(LayoutPrimitive::PageBreak)),
|
||||
);
|
||||
built_ins.insert(
|
||||
"pagestyle",
|
||||
texlang::command::BuiltIn::new_execution(page_style_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"thispagestyle",
|
||||
texlang::command::BuiltIn::new_execution(this_page_style_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"fancyhead",
|
||||
texlang::command::BuiltIn::new_execution(fancy_head_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"fancyfoot",
|
||||
texlang::command::BuiltIn::new_execution(fancy_foot_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"fancyhf",
|
||||
texlang::command::BuiltIn::new_execution(fancy_hf_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"texrrapagedecorationend",
|
||||
texlang::command::BuiltIn::new_execution(page_decoration_end_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"thepage",
|
||||
texlang::command::BuiltIn::new_execution(page_number_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"kern",
|
||||
texlang::command::BuiltIn::new_execution(kern_primitive)
|
||||
|
||||
@@ -284,6 +284,28 @@ impl TextLine {
|
||||
self
|
||||
}
|
||||
|
||||
/// Natural (unjustified) width of the line in millimetres, for centering
|
||||
/// and right-aligning decorations whose box may be wider than the text.
|
||||
pub(super) fn natural_width_mm(&self, fonts: &fonts::FontCache) -> f64 {
|
||||
let natural_space = Style::new()
|
||||
.with_font_size(self.font_size)
|
||||
.str_width(fonts, " ");
|
||||
let mut x = Mm::from(0.0);
|
||||
for item in &self.items {
|
||||
match item {
|
||||
TextLineItem::Text(span) => {
|
||||
x += span.style.str_width(fonts, &span.text);
|
||||
}
|
||||
TextLineItem::Glue(_) => x += natural_space,
|
||||
TextLineItem::Kern(width) => x += *width,
|
||||
TextLineItem::Rule { width, .. } => x += *width,
|
||||
TextLineItem::Frame { width, .. } => x += *width,
|
||||
}
|
||||
}
|
||||
let width: printpdf::Mm = x.into();
|
||||
width.0
|
||||
}
|
||||
|
||||
pub(super) fn layout_spans(&self, fonts: &fonts::FontCache) -> TextLineLayout {
|
||||
let mut spans = Vec::with_capacity(self.items.len());
|
||||
let mut rules = Vec::new();
|
||||
|
||||
Reference in new issue
Block a user