ruvector-scipix 2.0.4

Rust OCR engine for scientific documents - extract LaTeX, MathML from math equations, research papers, and technical diagrams with ONNX GPU acceleration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! LaTeX output formatter with styling and package management

use super::LineData;

/// LaTeX document formatter
#[derive(Clone)]
pub struct LaTeXFormatter {
    packages: Vec<String>,
    document_class: String,
    preamble: String,
    numbered_equations: bool,
    custom_delimiters: Option<(String, String)>,
}

impl LaTeXFormatter {
    pub fn new() -> Self {
        Self {
            packages: vec!["amsmath".to_string(), "amssymb".to_string()],
            document_class: "article".to_string(),
            preamble: String::new(),
            numbered_equations: false,
            custom_delimiters: None,
        }
    }

    pub fn with_packages(mut self, packages: Vec<String>) -> Self {
        self.packages = packages;
        self
    }

    pub fn add_package(mut self, package: String) -> Self {
        if !self.packages.contains(&package) {
            self.packages.push(package);
        }
        self
    }

    pub fn document_class(mut self, class: String) -> Self {
        self.document_class = class;
        self
    }

    pub fn preamble(mut self, preamble: String) -> Self {
        self.preamble = preamble;
        self
    }

    pub fn numbered_equations(mut self, numbered: bool) -> Self {
        self.numbered_equations = numbered;
        self
    }

    pub fn custom_delimiters(mut self, start: String, end: String) -> Self {
        self.custom_delimiters = Some((start, end));
        self
    }

    /// Format plain LaTeX content
    pub fn format(&self, latex: &str) -> String {
        // Clean up LaTeX if needed
        let cleaned = self.clean_latex(latex);

        // Apply custom delimiters if specified
        if let Some((start, end)) = &self.custom_delimiters {
            format!("{}{}{}", start, cleaned, end)
        } else {
            cleaned
        }
    }

    /// Format line data to LaTeX
    pub fn format_lines(&self, lines: &[LineData]) -> String {
        let mut output = String::new();
        let mut in_align = false;

        for line in lines {
            match line.line_type.as_str() {
                "text" => {
                    if in_align {
                        output.push_str("\\end{align*}\n\n");
                        in_align = false;
                    }
                    output.push_str(&self.escape_text(&line.text));
                    output.push_str("\n\n");
                }
                "math" | "equation" => {
                    let latex = line.latex.as_ref().unwrap_or(&line.text);

                    if self.numbered_equations {
                        output.push_str("\\begin{equation}\n");
                        output.push_str(latex.trim());
                        output.push_str("\n\\end{equation}\n\n");
                    } else {
                        output.push_str("\\[\n");
                        output.push_str(latex.trim());
                        output.push_str("\n\\]\n\n");
                    }
                }
                "inline_math" => {
                    let latex = line.latex.as_ref().unwrap_or(&line.text);
                    output.push_str(&format!("${}$", latex.trim()));
                }
                "align" => {
                    if !in_align {
                        output.push_str("\\begin{align*}\n");
                        in_align = true;
                    }
                    let latex = line.latex.as_ref().unwrap_or(&line.text);
                    output.push_str(latex.trim());
                    output.push_str(" \\\\\n");
                }
                "table" => {
                    output.push_str(&self.format_table(&line.text));
                    output.push_str("\n\n");
                }
                _ => {
                    output.push_str(&line.text);
                    output.push_str("\n");
                }
            }
        }

        if in_align {
            output.push_str("\\end{align*}\n");
        }

        output.trim().to_string()
    }

    /// Format complete LaTeX document
    pub fn format_document(&self, content: &str) -> String {
        let mut doc = String::new();

        // Document class
        doc.push_str(&format!("\\documentclass{{{}}}\n\n", self.document_class));

        // Packages
        for package in &self.packages {
            doc.push_str(&format!("\\usepackage{{{}}}\n", package));
        }
        doc.push_str("\n");

        // Custom preamble
        if !self.preamble.is_empty() {
            doc.push_str(&self.preamble);
            doc.push_str("\n\n");
        }

        // Begin document
        doc.push_str("\\begin{document}\n\n");

        // Content
        doc.push_str(content);
        doc.push_str("\n\n");

        // End document
        doc.push_str("\\end{document}\n");

        doc
    }

    /// Clean and normalize LaTeX
    fn clean_latex(&self, latex: &str) -> String {
        let mut cleaned = latex.to_string();

        // Remove excessive whitespace
        while cleaned.contains("  ") {
            cleaned = cleaned.replace("  ", " ");
        }

        // Normalize line breaks
        cleaned = cleaned.replace("\r\n", "\n");

        // Ensure proper spacing around operators
        for op in &["=", "+", "-", r"\times", r"\div"] {
            let spaced = format!(" {} ", op);
            cleaned = cleaned.replace(op, &spaced);
        }

        // Remove duplicate spaces again
        while cleaned.contains("  ") {
            cleaned = cleaned.replace("  ", " ");
        }

        cleaned.trim().to_string()
    }

    /// Escape special LaTeX characters in text
    fn escape_text(&self, text: &str) -> String {
        text.replace('\\', r"\\")
            .replace('{', r"\{")
            .replace('}', r"\}")
            .replace('$', r"\$")
            .replace('%', r"\%")
            .replace('_', r"\_")
            .replace('&', r"\&")
            .replace('#', r"\#")
            .replace('^', r"\^")
            .replace('~', r"\~")
    }

    /// Format table to LaTeX tabular environment
    fn format_table(&self, table: &str) -> String {
        let rows: Vec<&str> = table.lines().collect();
        if rows.is_empty() {
            return String::new();
        }

        // Determine number of columns from first row
        let num_cols = rows[0].split('|').filter(|s| !s.is_empty()).count();
        let col_spec = "c".repeat(num_cols);

        let mut output = format!("\\begin{{tabular}}{{{}}}\n", col_spec);
        output.push_str("\\hline\n");

        for (i, row) in rows.iter().enumerate() {
            let cells: Vec<&str> = row
                .split('|')
                .map(|s| s.trim())
                .filter(|s| !s.is_empty())
                .collect();

            output.push_str(&cells.join(" & "));
            output.push_str(" \\\\\n");

            if i == 0 {
                output.push_str("\\hline\n");
            }
        }

        output.push_str("\\hline\n");
        output.push_str("\\end{tabular}");

        output
    }

    /// Convert inline LaTeX to display math
    pub fn inline_to_display(&self, latex: &str) -> String {
        if self.numbered_equations {
            format!("\\begin{{equation}}\n{}\n\\end{{equation}}", latex.trim())
        } else {
            format!("\\[\n{}\n\\]", latex.trim())
        }
    }

    /// Add equation label
    pub fn add_label(&self, latex: &str, label: &str) -> String {
        format!("{}\n\\label{{{}}}", latex.trim(), label)
    }
}

impl Default for LaTeXFormatter {
    fn default() -> Self {
        Self::new()
    }
}

/// Styled LaTeX formatter with predefined templates
#[allow(dead_code)]
pub struct StyledLaTeXFormatter {
    base: LaTeXFormatter,
    style: LaTeXStyle,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LaTeXStyle {
    Article,
    Report,
    Book,
    Beamer,
    Minimal,
}

impl StyledLaTeXFormatter {
    pub fn new(style: LaTeXStyle) -> Self {
        let base = match style {
            LaTeXStyle::Article => LaTeXFormatter::new()
                .document_class("article".to_string())
                .with_packages(vec![
                    "amsmath".to_string(),
                    "amssymb".to_string(),
                    "graphicx".to_string(),
                    "hyperref".to_string(),
                ]),
            LaTeXStyle::Report => LaTeXFormatter::new()
                .document_class("report".to_string())
                .with_packages(vec![
                    "amsmath".to_string(),
                    "amssymb".to_string(),
                    "graphicx".to_string(),
                    "hyperref".to_string(),
                    "geometry".to_string(),
                ]),
            LaTeXStyle::Book => LaTeXFormatter::new()
                .document_class("book".to_string())
                .with_packages(vec![
                    "amsmath".to_string(),
                    "amssymb".to_string(),
                    "graphicx".to_string(),
                    "hyperref".to_string(),
                    "geometry".to_string(),
                    "fancyhdr".to_string(),
                ]),
            LaTeXStyle::Beamer => LaTeXFormatter::new()
                .document_class("beamer".to_string())
                .with_packages(vec![
                    "amsmath".to_string(),
                    "amssymb".to_string(),
                    "graphicx".to_string(),
                ]),
            LaTeXStyle::Minimal => LaTeXFormatter::new()
                .document_class("article".to_string())
                .with_packages(vec!["amsmath".to_string()]),
        };

        Self { base, style }
    }

    pub fn format_document(
        &self,
        content: &str,
        title: Option<&str>,
        author: Option<&str>,
    ) -> String {
        let mut preamble = String::new();

        if let Some(t) = title {
            preamble.push_str(&format!("\\title{{{}}}\n", t));
        }
        if let Some(a) = author {
            preamble.push_str(&format!("\\author{{{}}}\n", a));
        }
        if title.is_some() || author.is_some() {
            preamble.push_str("\\date{\\today}\n");
        }

        let formatter = self.base.clone().preamble(preamble);
        let mut doc = formatter.format_document(content);

        // Add maketitle after \begin{document} if we have title/author
        if title.is_some() || author.is_some() {
            doc = doc.replace(
                "\\begin{document}\n\n",
                "\\begin{document}\n\n\\maketitle\n\n",
            );
        }

        doc
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::output::BoundingBox;

    #[test]
    fn test_format_simple() {
        let formatter = LaTeXFormatter::new();
        let result = formatter.format("E = mc^2");
        assert!(result.contains("mc^2"));
    }

    #[test]
    fn test_format_document() {
        let formatter = LaTeXFormatter::new();
        let doc = formatter.format_document("E = mc^2");

        assert!(doc.contains(r"\documentclass{article}"));
        assert!(doc.contains(r"\usepackage{amsmath}"));
        assert!(doc.contains(r"\begin{document}"));
        assert!(doc.contains("mc^2"));
        assert!(doc.contains(r"\end{document}"));
    }

    #[test]
    fn test_escape_text() {
        let formatter = LaTeXFormatter::new();
        let result = formatter.escape_text("Price: $100 & 50%");
        assert!(result.contains(r"\$100"));
        assert!(result.contains(r"\&"));
        assert!(result.contains(r"\%"));
    }

    #[test]
    fn test_inline_to_display() {
        let formatter = LaTeXFormatter::new();
        let result = formatter.inline_to_display("x^2 + y^2 = r^2");
        assert!(result.contains(r"\["));
        assert!(result.contains(r"\]"));
    }

    #[test]
    fn test_styled_formatter() {
        let formatter = StyledLaTeXFormatter::new(LaTeXStyle::Article);
        let doc = formatter.format_document("Content", Some("My Title"), Some("Author Name"));

        assert!(doc.contains(r"\title{My Title}"));
        assert!(doc.contains(r"\author{Author Name}"));
        assert!(doc.contains(r"\maketitle"));
    }

    #[test]
    fn test_format_lines() {
        let formatter = LaTeXFormatter::new();
        let lines = vec![
            LineData {
                line_type: "text".to_string(),
                text: "Introduction".to_string(),
                latex: None,
                bbox: BoundingBox::new(0.0, 0.0, 100.0, 20.0),
                confidence: 0.95,
                words: None,
            },
            LineData {
                line_type: "equation".to_string(),
                text: "E = mc^2".to_string(),
                latex: Some(r"E = mc^2".to_string()),
                bbox: BoundingBox::new(0.0, 25.0, 100.0, 30.0),
                confidence: 0.98,
                words: None,
            },
        ];

        let result = formatter.format_lines(&lines);
        assert!(result.contains("Introduction"));
        assert!(result.contains(r"\[") || result.contains(r"\begin{equation}"));
        assert!(result.contains("mc^2"));
    }
}