Support multicolumn spans and partial table rules
This commit is contained in:
1 parent
ca5cb0b8af
commit
d4e52dc225
13 files changed
+698
-187
No files matched your search
@@ -244,7 +244,9 @@ pub enum Block {
|
||||
rows: Vec<Vec<Vec<Run>>>,
|
||||
caption: Option<Vec<Run>>,
|
||||
alignments: Vec<ColumnAlignment>,
|
||||
rules: Vec<usize>,
|
||||
rules: Vec<TableRule>,
|
||||
/// Per-cell span and alignment metadata, parallel to `rows`.
|
||||
cell_styles: Vec<Vec<TableCellStyle>>,
|
||||
/// Float placement hints from `[htbp]`.
|
||||
placement: String,
|
||||
},
|
||||
@@ -281,13 +283,43 @@ pub enum Block {
|
||||
PageBreak,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ColumnAlignment {
|
||||
Left,
|
||||
Center,
|
||||
Right,
|
||||
}
|
||||
|
||||
/// Per-cell table metadata: a multicolumn span and an optional local alignment.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub struct TableCellStyle {
|
||||
pub colspan: usize,
|
||||
pub alignment: Option<ColumnAlignment>,
|
||||
}
|
||||
|
||||
/// A horizontal table rule. `end_column` is exclusive; `usize::MAX` means the
|
||||
/// rule spans every column.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct TableRule {
|
||||
pub row: usize,
|
||||
pub start_column: usize,
|
||||
pub end_column: usize,
|
||||
pub trim_left: bool,
|
||||
pub trim_right: bool,
|
||||
}
|
||||
|
||||
impl TableRule {
|
||||
pub fn full(row: usize) -> Self {
|
||||
Self {
|
||||
row,
|
||||
start_column: 0,
|
||||
end_column: usize::MAX,
|
||||
trim_left: false,
|
||||
trim_right: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct ListBlock {
|
||||
pub ordered: bool,
|
||||
|
||||
@@ -5,7 +5,8 @@ use crate::{
|
||||
document::{
|
||||
Block, ColumnAlignment, DocumentModel, EquationNumber, InlineBox, InlineBoxAlign,
|
||||
ListBlock, ListItem, PageMargins, PageSize, PageStyleKind, PageStyleOverride,
|
||||
ParagraphAlignment, ParsedDocument, RuleBox, Run, TitleStyle, UnresolvedReference,
|
||||
ParagraphAlignment, ParsedDocument, RuleBox, Run, TableCellStyle, TableRule, TitleStyle,
|
||||
UnresolvedReference,
|
||||
},
|
||||
expanded,
|
||||
math_expr::*,
|
||||
@@ -47,7 +48,10 @@ struct TableBuilder {
|
||||
cell: Vec<Run>,
|
||||
caption: Option<Vec<Run>>,
|
||||
alignments: Vec<ColumnAlignment>,
|
||||
rules: Vec<usize>,
|
||||
rules: Vec<TableRule>,
|
||||
cell_styles: Vec<Vec<TableCellStyle>>,
|
||||
row_styles: Vec<TableCellStyle>,
|
||||
pending_cell_style: Option<TableCellStyle>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -1115,9 +1119,33 @@ impl Parser {
|
||||
}
|
||||
expanded::FrontendEventKind::ListItem => self.start_list_item(),
|
||||
expanded::FrontendEventKind::TableCell => self.finish_table_cell(),
|
||||
expanded::FrontendEventKind::TableCellSpan { colspan, alignment } => {
|
||||
if let Some(table) = &mut self.table {
|
||||
table.pending_cell_style = Some(TableCellStyle {
|
||||
colspan: (*colspan).max(1),
|
||||
alignment: *alignment,
|
||||
});
|
||||
}
|
||||
}
|
||||
expanded::FrontendEventKind::TableRule => {
|
||||
if let Some(table) = &mut self.table {
|
||||
table.rules.push(table.rows.len());
|
||||
table.rules.push(TableRule::full(table.rows.len()));
|
||||
}
|
||||
}
|
||||
expanded::FrontendEventKind::TableRuleRange {
|
||||
start_column,
|
||||
end_column,
|
||||
trim_left,
|
||||
trim_right,
|
||||
} => {
|
||||
if let Some(table) = &mut self.table {
|
||||
table.rules.push(TableRule {
|
||||
row: table.rows.len(),
|
||||
start_column: *start_column,
|
||||
end_column: *end_column,
|
||||
trim_left: *trim_left,
|
||||
trim_right: *trim_right,
|
||||
});
|
||||
}
|
||||
}
|
||||
expanded::FrontendEventKind::DisplayMath {
|
||||
@@ -2141,6 +2169,8 @@ impl Parser {
|
||||
fn finish_table_cell(&mut self) {
|
||||
if let Some(table) = &mut self.table {
|
||||
trim_run_end(&mut table.cell);
|
||||
let style = table.pending_cell_style.take().unwrap_or_default();
|
||||
table.row_styles.push(style);
|
||||
table.row.push(std::mem::take(&mut table.cell));
|
||||
}
|
||||
}
|
||||
@@ -2151,6 +2181,9 @@ impl Parser {
|
||||
&& !table.row.is_empty()
|
||||
{
|
||||
table.rows.push(std::mem::take(&mut table.row));
|
||||
table
|
||||
.cell_styles
|
||||
.push(std::mem::take(&mut table.row_styles));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2172,6 +2205,7 @@ impl Parser {
|
||||
caption,
|
||||
alignments: table.alignments,
|
||||
rules: table.rules,
|
||||
cell_styles: table.cell_styles,
|
||||
placement: self.table_placement.clone(),
|
||||
});
|
||||
if self.table_active {
|
||||
|
||||
@@ -2,6 +2,7 @@ use std::{ops::Range, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
MathClass, MathStyleCommand, TeXGlue,
|
||||
document::ColumnAlignment,
|
||||
math_code::UnicodeMathCode,
|
||||
math_expr::{
|
||||
MathAccent, MathDelimiterGlyph, MathDelimiterSettings, MathExpr, MathFontFamilies,
|
||||
@@ -219,7 +220,17 @@ pub enum FrontendEventKind {
|
||||
RowBreak,
|
||||
ListItem,
|
||||
TableCell,
|
||||
TableCellSpan {
|
||||
colspan: usize,
|
||||
alignment: Option<ColumnAlignment>,
|
||||
},
|
||||
TableRule,
|
||||
TableRuleRange {
|
||||
start_column: usize,
|
||||
end_column: usize,
|
||||
trim_left: bool,
|
||||
trim_right: bool,
|
||||
},
|
||||
HeadingStart {
|
||||
level: u8,
|
||||
numbered: bool,
|
||||
|
||||
Reference in new issue
Block a user