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
@@ -75,6 +75,8 @@ pub struct DocumentModel {
|
||||
pub labels: HashMap<String, String>,
|
||||
pub citations: HashMap<String, usize>,
|
||||
pub footnotes: Vec<Footnote>,
|
||||
pub page_style: PageStyle,
|
||||
pub page_style_overrides: Vec<PageStyleOverride>,
|
||||
}
|
||||
|
||||
impl Default for DocumentModel {
|
||||
@@ -93,10 +95,81 @@ impl Default for DocumentModel {
|
||||
labels: HashMap::new(),
|
||||
citations: HashMap::new(),
|
||||
footnotes: Vec::new(),
|
||||
page_style: PageStyle::default(),
|
||||
page_style_overrides: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Page style selected by `\pagestyle`.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub enum PageStyleKind {
|
||||
Empty,
|
||||
/// LaTeX's default: a centered page number in the footer.
|
||||
#[default]
|
||||
Plain,
|
||||
/// fancyhdr-style header/footer declared with `\fancyhead`/`\fancyfoot`.
|
||||
Fancy,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub enum PageDecorationPosition {
|
||||
#[default]
|
||||
Center,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
/// Header or footer runs for one edge of the page.
|
||||
#[derive(Clone, Default, PartialEq, Eq, Hash)]
|
||||
pub struct PageDecoration {
|
||||
pub left: Vec<Run>,
|
||||
pub center: Vec<Run>,
|
||||
pub right: Vec<Run>,
|
||||
}
|
||||
|
||||
impl PageDecoration {
|
||||
pub fn slot_mut(&mut self, position: PageDecorationPosition) -> &mut Vec<Run> {
|
||||
match position {
|
||||
PageDecorationPosition::Left => &mut self.left,
|
||||
PageDecorationPosition::Center => &mut self.center,
|
||||
PageDecorationPosition::Right => &mut self.right,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.left.is_empty() && self.center.is_empty() && self.right.is_empty()
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.left.clear();
|
||||
self.center.clear();
|
||||
self.right.clear();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, PartialEq, Eq, Hash)]
|
||||
pub struct PageStyle {
|
||||
pub kind: PageStyleKind,
|
||||
pub header: PageDecoration,
|
||||
pub footer: PageDecoration,
|
||||
}
|
||||
|
||||
/// A `\thispagestyle` override, anchored at the block it was issued before.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct PageStyleOverride {
|
||||
pub anchor: usize,
|
||||
pub kind: PageStyleKind,
|
||||
}
|
||||
|
||||
/// Marker character used by `\thepage` inside header/footer runs; the page
|
||||
/// renderer substitutes the concrete page number before measuring and painting.
|
||||
pub const PAGE_NUMBER_MARKER: char = '\u{F5000}';
|
||||
|
||||
pub fn page_number_marker_text() -> String {
|
||||
PAGE_NUMBER_MARKER.to_string()
|
||||
}
|
||||
|
||||
pub struct ParsedDocument(pub DocumentModel);
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -137,11 +210,15 @@ pub enum Block {
|
||||
caption: Option<Vec<Run>>,
|
||||
alignments: Vec<ColumnAlignment>,
|
||||
rules: Vec<usize>,
|
||||
/// Float placement hints from `[htbp]`.
|
||||
placement: String,
|
||||
},
|
||||
Image {
|
||||
path: String,
|
||||
options: String,
|
||||
caption: Option<Vec<Run>>,
|
||||
/// Float placement hints from `[htbp]`.
|
||||
placement: String,
|
||||
},
|
||||
Bibliography(Vec<(String, Vec<Run>)>),
|
||||
/// A box such as `\fbox{...}` (framed) or `\parbox{width}{...}`.
|
||||
|
||||
@@ -4,8 +4,8 @@ use crate::{
|
||||
TeXGlue,
|
||||
document::{
|
||||
Block, ColumnAlignment, DocumentModel, EquationNumber, InlineBox, InlineBoxAlign,
|
||||
ListBlock, ListItem, PageMargins, PageSize, ParsedDocument, RuleBox, Run,
|
||||
UnresolvedReference,
|
||||
ListBlock, ListItem, PageMargins, PageSize, PageStyleOverride, ParsedDocument, RuleBox,
|
||||
Run, UnresolvedReference,
|
||||
},
|
||||
expanded,
|
||||
math_expr::*,
|
||||
@@ -300,7 +300,7 @@ fn consume_environment_preamble(
|
||||
expanded: &expanded::ExpandedTokenStream,
|
||||
start_index: usize,
|
||||
environment: &str,
|
||||
) -> usize {
|
||||
) -> (usize, Option<String>) {
|
||||
use expanded::{ExpandedItemKind, ExpandedTokenValue, FrontendEventKind as Event};
|
||||
|
||||
let mut argument_start = start_index;
|
||||
@@ -320,7 +320,7 @@ fn consume_environment_preamble(
|
||||
let Some(ExpandedItemKind::Event(Event::GroupStart)) =
|
||||
expanded.items.get(argument_start).map(|item| &item.kind)
|
||||
else {
|
||||
return start_index;
|
||||
return (start_index, None);
|
||||
};
|
||||
let mut depth = 0usize;
|
||||
for (index, item) in expanded.items.iter().enumerate().skip(argument_start) {
|
||||
@@ -329,13 +329,13 @@ fn consume_environment_preamble(
|
||||
ExpandedItemKind::Event(Event::GroupEnd) => {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
return index + 1;
|
||||
return (index + 1, None);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
return start_index;
|
||||
return (start_index, None);
|
||||
}
|
||||
|
||||
if !matches!(environment, "figure" | "table")
|
||||
@@ -348,11 +348,12 @@ fn consume_environment_preamble(
|
||||
})
|
||||
!= Some('[')
|
||||
{
|
||||
return start_index;
|
||||
return (start_index, None);
|
||||
}
|
||||
|
||||
let mut bracket_depth = 0usize;
|
||||
let mut group_depth = 0usize;
|
||||
let mut placement = String::new();
|
||||
for (index, item) in expanded.items.iter().enumerate().skip(argument_start) {
|
||||
match &item.kind {
|
||||
ExpandedItemKind::Event(Event::GroupStart) => group_depth += 1,
|
||||
@@ -364,15 +365,16 @@ fn consume_environment_preamble(
|
||||
Some(']') => {
|
||||
bracket_depth = bracket_depth.saturating_sub(1);
|
||||
if bracket_depth == 0 {
|
||||
return index + 1;
|
||||
return (index + 1, Some(placement));
|
||||
}
|
||||
}
|
||||
Some(character) if bracket_depth == 1 => placement.push(character),
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
start_index
|
||||
(start_index, None)
|
||||
}
|
||||
|
||||
struct MinipageBuilder {
|
||||
@@ -422,6 +424,19 @@ struct Parser {
|
||||
minipage: Option<MinipageBuilder>,
|
||||
footnote_capture: Option<Vec<Run>>,
|
||||
footnote_counter: u32,
|
||||
page_decoration_capture: Option<(
|
||||
bool,
|
||||
crate::document::PageDecorationPosition,
|
||||
Vec<Run>,
|
||||
usize,
|
||||
bool,
|
||||
bool,
|
||||
bool,
|
||||
bool,
|
||||
bool,
|
||||
)>,
|
||||
float_placement: String,
|
||||
table_placement: String,
|
||||
centered: Option<Vec<Run>>,
|
||||
current_source: Option<SourceLocation>,
|
||||
paragraph_source: Option<SourceLocation>,
|
||||
@@ -465,6 +480,9 @@ impl Parser {
|
||||
minipage: None,
|
||||
footnote_capture: None,
|
||||
footnote_counter: 0,
|
||||
page_decoration_capture: None,
|
||||
float_placement: String::new(),
|
||||
table_placement: String::new(),
|
||||
centered: None,
|
||||
current_source: None,
|
||||
paragraph_source: None,
|
||||
@@ -553,8 +571,16 @@ impl Parser {
|
||||
name: environment, ..
|
||||
} = kind
|
||||
{
|
||||
item_index =
|
||||
let (next_index, placement) =
|
||||
consume_environment_preamble(expanded, item_index, environment);
|
||||
item_index = next_index;
|
||||
if let Some(placement) = placement {
|
||||
match environment.as_str() {
|
||||
"figure" => self.float_placement = placement,
|
||||
"table" => self.table_placement = placement,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if paragraph_break {
|
||||
@@ -835,6 +861,83 @@ impl Parser {
|
||||
self.begin_style(*style);
|
||||
self.style_declarations.push((self.group_depth, *style));
|
||||
}
|
||||
expanded::FrontendEventKind::PageStyle(kind) => {
|
||||
self.document.page_style.kind = *kind;
|
||||
}
|
||||
expanded::FrontendEventKind::ThisPageStyle(kind) => {
|
||||
self.document.page_style_overrides.push(PageStyleOverride {
|
||||
anchor: self.document.blocks.len(),
|
||||
kind: *kind,
|
||||
});
|
||||
}
|
||||
expanded::FrontendEventKind::PageDecorationStart { header, position } => {
|
||||
self.page_decoration_capture = Some((
|
||||
*header,
|
||||
*position,
|
||||
Vec::new(),
|
||||
self.group_depth,
|
||||
self.bold,
|
||||
self.italic,
|
||||
self.monospace,
|
||||
self.sans,
|
||||
self.small_caps,
|
||||
));
|
||||
self.group_depth += 1;
|
||||
}
|
||||
expanded::FrontendEventKind::PageDecorationEnd => {
|
||||
if let Some((
|
||||
header,
|
||||
position,
|
||||
mut runs,
|
||||
depth,
|
||||
bold,
|
||||
italic,
|
||||
monospace,
|
||||
sans,
|
||||
small_caps,
|
||||
)) = self.page_decoration_capture.take()
|
||||
{
|
||||
while self
|
||||
.style_declarations
|
||||
.last()
|
||||
.is_some_and(|(declaration_depth, _)| *declaration_depth > depth)
|
||||
{
|
||||
if let Some((_, style)) = self.style_declarations.pop() {
|
||||
self.end_style(style);
|
||||
}
|
||||
}
|
||||
self.group_depth = depth;
|
||||
self.bold = bold;
|
||||
self.italic = italic;
|
||||
self.monospace = monospace;
|
||||
self.sans = sans;
|
||||
self.small_caps = small_caps;
|
||||
trim_run_end(&mut runs);
|
||||
let decoration = if header {
|
||||
&mut self.document.page_style.header
|
||||
} else {
|
||||
&mut self.document.page_style.footer
|
||||
};
|
||||
*decoration.slot_mut(position) = runs;
|
||||
}
|
||||
}
|
||||
expanded::FrontendEventKind::PageDecorationClear { header } => {
|
||||
let decoration = if *header {
|
||||
&mut self.document.page_style.header
|
||||
} else {
|
||||
&mut self.document.page_style.footer
|
||||
};
|
||||
decoration.clear();
|
||||
}
|
||||
expanded::FrontendEventKind::PageNumber => {
|
||||
let mut run = Run::plain(crate::document::page_number_marker_text());
|
||||
run.bold = self.bold;
|
||||
run.italic = self.italic;
|
||||
run.monospace = self.monospace;
|
||||
run.sans = self.sans;
|
||||
run.small_caps = self.small_caps;
|
||||
self.target_runs().push(run);
|
||||
}
|
||||
expanded::FrontendEventKind::GroupStart => self.group_depth += 1,
|
||||
expanded::FrontendEventKind::GroupEnd => {
|
||||
let depth = self.group_depth;
|
||||
@@ -1342,6 +1445,13 @@ impl Parser {
|
||||
}
|
||||
|
||||
fn target_runs(&mut self) -> &mut Vec<Run> {
|
||||
if self.page_decoration_capture.is_some() {
|
||||
return &mut self
|
||||
.page_decoration_capture
|
||||
.as_mut()
|
||||
.expect("page decoration capture was checked")
|
||||
.2;
|
||||
}
|
||||
if self.footnote_capture.is_some() {
|
||||
return self
|
||||
.footnote_capture
|
||||
@@ -1357,6 +1467,7 @@ impl Parser {
|
||||
&& self.centered.is_none()
|
||||
&& self.framed_box.is_none()
|
||||
&& self.metadata_capture.is_none()
|
||||
&& self.page_decoration_capture.is_none()
|
||||
&& self.caption_capture.is_none()
|
||||
&& self.heading.is_none()
|
||||
&& self.table.is_none()
|
||||
@@ -1546,6 +1657,7 @@ impl Parser {
|
||||
(false, "tabular") => self.finish_table(),
|
||||
(true, "figure") => {
|
||||
self.flush_paragraph();
|
||||
self.float_placement = String::new();
|
||||
self.figure_active = true;
|
||||
self.figure_image = None;
|
||||
self.figure_number += 1;
|
||||
@@ -1553,11 +1665,18 @@ impl Parser {
|
||||
}
|
||||
(false, "figure") => {
|
||||
self.flush_paragraph();
|
||||
if self.figure_active
|
||||
&& !self.float_placement.is_empty()
|
||||
&& self.float_placement.contains('p')
|
||||
{
|
||||
self.push_block(Block::PageBreak);
|
||||
}
|
||||
self.figure_active = false;
|
||||
self.figure_image = None;
|
||||
}
|
||||
(true, "table") => {
|
||||
self.flush_paragraph();
|
||||
self.table_placement = String::new();
|
||||
self.table_active = true;
|
||||
self.current_table_block = None;
|
||||
self.pending_table_caption = None;
|
||||
@@ -1620,11 +1739,19 @@ impl Parser {
|
||||
self.figure_number += 1;
|
||||
}
|
||||
self.current_reference = self.figure_number.to_string();
|
||||
if self.figure_active
|
||||
&& !self.float_placement.is_empty()
|
||||
&& !self.float_placement.contains('h')
|
||||
&& (self.float_placement.contains('t') || self.float_placement.contains('p'))
|
||||
{
|
||||
self.push_block(Block::PageBreak);
|
||||
}
|
||||
let index = self.document.blocks.len();
|
||||
self.push_block(Block::Image {
|
||||
path,
|
||||
options,
|
||||
caption: None,
|
||||
placement: self.float_placement.clone(),
|
||||
});
|
||||
if self.figure_active {
|
||||
self.figure_image = Some(index);
|
||||
@@ -1695,6 +1822,12 @@ impl Parser {
|
||||
if let Some(table) = self.table.take()
|
||||
&& !table.rows.is_empty()
|
||||
{
|
||||
if !self.table_placement.is_empty()
|
||||
&& !self.table_placement.contains('h')
|
||||
&& (self.table_placement.contains('t') || self.table_placement.contains('p'))
|
||||
{
|
||||
self.push_block(Block::PageBreak);
|
||||
}
|
||||
let index = self.document.blocks.len();
|
||||
let caption = table.caption.or_else(|| self.pending_table_caption.take());
|
||||
self.push_block(Block::Table {
|
||||
@@ -1702,10 +1835,14 @@ impl Parser {
|
||||
caption,
|
||||
alignments: table.alignments,
|
||||
rules: table.rules,
|
||||
placement: self.table_placement.clone(),
|
||||
});
|
||||
if self.table_active {
|
||||
self.current_table_block = Some(index);
|
||||
}
|
||||
if !self.table_placement.is_empty() && self.table_placement.contains('p') {
|
||||
self.push_block(Block::PageBreak);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -234,6 +234,17 @@ pub enum FrontendEventKind {
|
||||
Reference(String),
|
||||
Citation(String),
|
||||
BibliographyItem(String),
|
||||
PageStyle(crate::document::PageStyleKind),
|
||||
ThisPageStyle(crate::document::PageStyleKind),
|
||||
PageDecorationStart {
|
||||
header: bool,
|
||||
position: crate::document::PageDecorationPosition,
|
||||
},
|
||||
PageDecorationEnd,
|
||||
PageDecorationClear {
|
||||
header: bool,
|
||||
},
|
||||
PageNumber,
|
||||
GroupStart,
|
||||
GroupEnd,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user