use crate::pdf::core::ContentStream;
use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum Align {
Left,
Center,
Right,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Row {
pub cells: Vec<String>,
pub is_separator: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Table {
pub columns: Vec<Align>,
pub rows: Vec<Row>,
}
impl Table {
pub fn parse(column_spec: &str, content: &str) -> Self {
let columns = parse_column_spec(column_spec);
let mut rows = Vec::new();
for line in content.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
if line.starts_with("\\toprule")
|| line.starts_with("\\midrule")
|| line.starts_with("\\bottomrule")
|| line.starts_with("\\hline")
{
rows.push(Row {
cells: vec![],
is_separator: true,
});
continue;
}
if line.starts_with("\\") && !line.contains('&') {
continue;
}
if line.contains('&') {
let cells: Vec<String> = line
.split('&')
.map(|s| {
s.trim()
.trim_end_matches("\\\\")
.trim()
.replace("\\$", "$")
.to_string()
})
.collect();
rows.push(Row {
cells,
is_separator: false,
});
}
}
Table { columns, rows }
}
}
pub fn render_table(
table: &Table,
stream: &mut ContentStream,
left_margin: f32,
y_position: f32,
line_height: f32,
content_width: f32,
) -> f32 {
let mut current_y = y_position - 5.0;
if table.columns.is_empty() || table.rows.is_empty() {
return current_y - 10.0;
}
let col_width = content_width / table.columns.len() as f32;
for row in &table.rows {
if row.is_separator {
let rule_y = current_y + line_height * 0.4;
stream.move_to(left_margin, rule_y);
stream.line_to(left_margin + content_width, rule_y);
stream.stroke();
current_y -= line_height * 0.5;
continue;
}
for (i, cell) in row.cells.iter().enumerate() {
if i >= table.columns.len() {
break;
}
if cell.is_empty() {
continue;
}
let x_pos = left_margin + (i as f32 * col_width);
let align = table.columns.get(i).copied().unwrap_or(Align::Left);
let cell_width = estimate_text_width(cell);
let x_offset = match align {
Align::Left => 0.0,
Align::Center => (col_width - cell_width) / 2.0,
Align::Right => col_width - cell_width,
};
stream.begin_text();
stream.set_font("F1", 10.0);
stream.set_position(x_pos + x_offset, current_y);
stream.show_text(cell);
stream.end_text();
}
current_y -= line_height;
}
current_y - 10.0
}
fn parse_column_spec(spec: &str) -> Vec<Align> {
let mut result = Vec::new();
let mut chars = spec.trim().trim_start_matches('{').trim_end_matches('}').chars();
while let Some(ch) = chars.next() {
match ch {
'l' => result.push(Align::Left),
'c' => result.push(Align::Center),
'r' => result.push(Align::Right),
'p' => {
let mut depth = 0;
for c in chars.by_ref() {
if c == '{' {
depth += 1;
} else if c == '}' {
depth -= 1;
if depth == 0 {
break;
}
}
}
result.push(Align::Left);
}
'|' | ' ' | '\t' => {} '@' => {
for c in chars.by_ref() {
if c == '}' {
break;
}
}
}
_ => {}
}
}
result
}
fn estimate_text_width(text: &str) -> f32 {
text.chars().count() as f32 * 5.0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_column_spec() {
assert_eq!(
parse_column_spec("|l|c|r|"),
vec![Align::Left, Align::Center, Align::Right]
);
assert_eq!(
parse_column_spec("{l c r}"),
vec![Align::Left, Align::Center, Align::Right]
);
assert_eq!(parse_column_spec("cc"), vec![Align::Center, Align::Center]);
assert_eq!(parse_column_spec("p{3cm}r"), vec![Align::Left, Align::Right]);
}
#[test]
fn test_table_parse_basic() {
let spec = "lcr";
let content = "A & B & C \\\\\n\\hline\n1 & 2 & 3 \\\\";
let table = Table::parse(spec, content);
assert_eq!(table.columns, vec![Align::Left, Align::Center, Align::Right]);
assert_eq!(table.rows.len(), 3);
assert!(table.rows[1].is_separator);
assert_eq!(table.rows[0].cells, vec!["A", "B", "C"]);
assert_eq!(table.rows[2].cells, vec!["1", "2", "3"]);
}
#[test]
fn test_table_parse_with_dollar_escape() {
let spec = "cc";
let content = "$x$ & $y$ \\\\";
let table = Table::parse(spec, content);
assert_eq!(table.rows[0].cells, vec!["$x$", "$y$"]);
}
}