pub mod line;
pub mod math;
pub mod style;
use crate::common::geom::Rect;
use serde::Serialize;
pub use line::{FRACTION_TERM, Line, LineGlyph, RotatedText};
pub use math::MathNode;
pub use style::*;
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct Span {
pub text: String,
pub style: u8,
#[serde(skip_serializing_if = "Option::is_none")]
pub href: Option<String>,
#[serde(skip_serializing_if = "is_black")]
pub color: [u8; 3],
}
impl Span {
pub fn plain(text: impl Into<String>) -> Self {
Self {
text: text.into(),
style: 0,
href: None,
color: [0; 3],
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct TableCell {
pub col: usize,
pub colspan: usize,
pub rowspan: usize,
pub spans: Vec<Span>,
}
#[derive(Debug, Clone, Serialize)]
pub struct Table {
pub n_cols: usize,
pub header_rows: usize,
pub col_align: Vec<&'static str>,
pub rows: Vec<Vec<TableCell>>,
pub rule_above: Vec<Vec<u8>>,
pub rule_bottom: Vec<u8>,
pub rule_left: Vec<Vec<bool>>,
pub rule_right: Vec<bool>,
#[serde(skip)]
pub geom: TableGeom,
}
#[derive(Debug, Clone, Default)]
pub struct TableGeom {
pub bbox: Rect,
pub col_bounds: Vec<f32>,
pub col_text: Vec<(f32, f32)>,
pub row_bounds: Vec<f32>,
pub row_baselines: Vec<f32>,
pub size: f32,
}
#[derive(Debug, Clone, Default)]
pub struct HLine {
pub spans: Vec<Span>,
pub soft_hyphen: bool,
}
#[derive(Debug, Clone)]
pub struct EqRun {
pub text: String,
pub x: f32,
pub y: f32,
pub width: f32,
pub size: f32,
pub style: u8,
pub color: [u8; 3],
pub ink: Option<Rect>,
}
#[derive(Debug, Clone)]
pub struct RotatedRun {
pub text: String,
pub x: f32,
pub y: f32,
pub angle: f32,
pub size: f32,
pub length: f32,
pub style: u8,
pub color: [u8; 3],
pub origins: Vec<(f32, f32)>,
}
#[derive(Debug, Clone)]
pub struct PLine {
pub line: Line,
pub spans: Vec<Span>,
pub kind: &'static str,
}
#[derive(Debug, Clone, Copy, Serialize)]
pub struct ImageSpec {
pub page: usize,
pub rect: Rect,
pub transparent: bool,
}
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum BlockKind {
Title,
Authors,
Note,
Heading {
level: u8,
},
Paragraph {
in_abstract: bool,
},
ListItem {
marker: Option<String>,
},
ListContinuation,
Equation {
image: ImageSpec,
number: Option<String>,
},
Figure {
kind: FloatKind,
image: Option<ImageSpec>,
label: Option<String>,
},
Footnote {
marker: String,
},
Reference {
label: Option<String>,
},
Code,
}
#[derive(Debug, Clone, Serialize)]
pub struct Block {
pub id: usize,
#[serde(flatten)]
pub kind: BlockKind,
pub page: usize,
pub bbox: Rect,
pub spans: Vec<Span>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub rows: Vec<Vec<Span>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub anchor: Option<String>,
#[serde(skip)]
pub loc: Loc,
#[serde(skip)]
pub lines: Vec<Line>,
#[serde(skip)]
pub line_texts: Vec<String>,
#[serde(skip)]
pub hlines: Vec<HLine>,
#[serde(skip_serializing_if = "Option::is_none")]
pub table: Option<Table>,
#[serde(skip)]
pub inner_lines: Vec<Line>,
#[serde(skip)]
pub inner_spans: Vec<Vec<Span>>,
#[serde(skip)]
pub eq_runs: Vec<EqRun>,
#[serde(skip)]
pub math: Option<MathNode>,
}
impl Block {
pub fn text(&self) -> String {
self.spans.iter().map(|span| span.text.as_str()).collect()
}
}
#[derive(Debug, Clone, Copy, Serialize)]
pub struct PageInfo {
pub width: f32,
pub height: f32,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct Meta {
pub title: Option<String>,
pub arxiv_id: Option<String>,
pub arxiv_stamp: Option<String>,
pub source: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct Document {
pub meta: Meta,
pub pages: Vec<PageInfo>,
pub body_size: f32,
pub leading: f32,
pub body_font: String,
pub math_font: String,
pub blocks: Vec<Block>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Loc {
pub page: usize,
pub zone: usize,
pub zone_cols: usize,
pub col: usize,
pub leaf: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum FloatKind {
Figure,
Table,
Algorithm,
Graphic,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Align {
Left,
Center,
Right,
}
fn is_black(color: &[u8; 3]) -> bool {
*color == [0; 3]
}