pub mod line;
pub mod math;
pub mod style;
use crate::common::geom::Rect;
use serde::Serialize;
pub use line::{FRACTION_TERM, Line, LineGlyph, RAISED_FRACTION_TERM, 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],
#[serde(skip)]
pub math: Option<Box<MathNode>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub note: Option<Vec<Span>>,
}
impl Span {
pub fn plain(text: impl Into<String>) -> Self {
Self::styled(text, 0)
}
pub fn styled(text: impl Into<String>, style: u8) -> Self {
Self {
text: text.into(),
style,
href: None,
color: [0; 3],
math: None,
note: None,
}
}
}
#[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,
}
pub(crate) const HEAVY_RULE: u8 = 2;
pub(crate) const THIN_RULE: u8 = 1;
impl Table {
pub(crate) fn ruled(
rows: Vec<Vec<TableCell>>,
n_cols: usize,
header_rows: usize,
col_align: Vec<&'static str>,
) -> Self {
let mut rule_above = vec![vec![0u8; n_cols]; rows.len()];
if let Some(first) = rule_above.first_mut() {
first.fill(HEAVY_RULE);
}
if let Some(under_header) = rule_above.get_mut(header_rows).filter(|_| header_rows > 0) {
under_header.fill(THIN_RULE);
}
Self {
n_cols,
header_rows,
col_align,
rule_above,
rule_bottom: vec![HEAVY_RULE; n_cols],
rule_left: vec![vec![false; n_cols]; rows.len()],
rule_right: vec![false; rows.len()],
rows,
geom: TableGeom::default(),
}
}
}
#[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,
#[serde(skip_serializing_if = "Option::is_none")]
pub embedded: Option<usize>,
}
impl ImageSpec {
pub fn region(page: usize, rect: Rect, transparent: bool) -> Self {
Self {
page,
rect,
transparent,
embedded: None,
}
}
}
#[derive(Debug, Clone)]
pub struct Picture {
pub data: Vec<u8>,
pub format: PictureFormat,
pub(crate) png_size: Option<(u32, u32)>,
pub(crate) outline_text: bool,
}
impl Picture {
pub(crate) fn bitmap(data: Vec<u8>, format: PictureFormat) -> Self {
Self {
data,
format,
png_size: None,
outline_text: false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PictureFormat {
Png,
Jpeg,
Svg,
}
impl PictureFormat {
pub fn mime(self) -> &'static str {
match self {
Self::Png => "image/png",
Self::Jpeg => "image/jpeg",
Self::Svg => "image/svg+xml",
}
}
pub fn extension(self) -> &'static str {
match self {
Self::Png => "png",
Self::Jpeg => "jpg",
Self::Svg => "svg",
}
}
}
#[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>,
level: u8,
},
ListContinuation,
Equation {
image: Option<ImageSpec>,
number: Option<String>,
},
Figure {
kind: FloatKind,
image: Option<ImageSpec>,
label: Option<String>,
},
Footnote {
marker: String,
},
Reference {
label: Option<String>,
},
Code,
ContentsEntry {
level: u8,
page: String,
},
}
#[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 caption_above: bool,
#[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 new(id: usize, kind: BlockKind) -> Self {
Self {
id,
kind,
page: 0,
bbox: Rect::default(),
spans: Vec::new(),
rows: Vec::new(),
anchor: None,
loc: Loc::default(),
lines: Vec::new(),
line_texts: Vec::new(),
hlines: Vec::new(),
table: None,
caption_above: false,
inner_lines: Vec::new(),
inner_spans: Vec::new(),
eq_runs: Vec::new(),
math: None,
}
}
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>,
#[serde(skip_serializing_if = "Option::is_none")]
pub source_format: 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>,
#[serde(skip)]
pub pictures: Vec<Picture>,
}
#[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]
}