Registry / RepositoryTypeScriptRustPython
Rollpie
ReadmeFiles
Versions
Info
Download
0.4.01085.8 KB2026-10-160.3.21087.6 KB2026-10-160.3.11087.5 KB2026-10-160.3.0117.8 KB2026-10-160.2.1106.1 KB2026-10-150.2.0105.5 KB2026-10-15
Version
0.3.1
Copyright
Rollpie, Irohabook
Publisher
math
Published
2026-10-16
Size
1087.5 KB
Downloads
24
Checksum
656aa128e98955e746555e14f8366cdca6c6761d61e1b74fa77a4797476f12dd
Dependencies
rust/[email protected]
rust/[email protected]

lib.rs

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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! hongo - a library that typesets LaTeX documents into PDF
//!
//! Reading (token, parse), numbering (number), typesetting (layout, line, page), and writing
//! (pdf, stream, embed, svg) are kept apart. Math is passed to suki, and chemical structures to lina.
//!
//! It does not depend on outside crates. It reads fonts and writes PDF with its own code.
//!
//! ```no_run
//! use std::path::Path;
//!
//! let library = hongo::font::open(
//!     Path::new("font/latinmodern-math.otf"),
//!     None, None, None, None, None,
//!     Some(Path::new("font/ipaexm.ttf")),
//! ).unwrap();
//! let style = hongo::style::Style::default();
//! let source = std::fs::read_to_string("doc.tex").unwrap();
//! let made = hongo::render(&source, &library, &style, Path::new(".")).unwrap();
//! std::fs::write("doc.pdf", made.data).unwrap();
//! ```
//!
//! Typesetting takes 2 passes. The first pass decides which page each heading goes on, and the
//! second puts those numbers into the table of contents and `\pageref`.

pub mod chem;
pub mod draw;
pub mod embed;
pub mod error;
pub mod font;
pub mod image;
pub mod layout;
pub mod line;
pub mod math;
pub mod node;
pub mod number;
pub mod page;
pub mod parse;
pub mod pdf;
pub mod stream;
pub mod style;
pub mod svg;
pub mod text;
pub mod token;
#[cfg(target_arch = "wasm32")]
pub mod web;

pub use error::Error;
pub use font::Library;
pub use style::Style;
use std::collections::BTreeSet;
use std::path::Path;


/// The maximum depth for following `\input`
const INCLUDE_LIMIT: usize = 16;


/// The typeset result
#[derive(Debug, Clone, PartialEq)]
pub struct Made {
    /// The PDF itself
    pub data: Vec<u8>,
    pub page: usize,
    /// Characters that were not in any typeface
    pub missing: BTreeSet<char>,
}


/// The typeset pages, before they are written as a PDF
#[derive(Debug, Clone, PartialEq)]
pub struct Paged {
    pub page: Vec<draw::Page>,
    pub picture: Vec<image::Picture>,
    pub destination: Vec<pdf::Destination>,
    pub title: String,
    /// Characters that were not in any typeface
    pub missing: BTreeSet<char>,
}


/// Turns a LaTeX document into a PDF
pub fn render(source: &str, library: &Library, style: &Style, base: &Path) -> Result<Made, Error> {
    let paged = typeset(source, library, style, base)?;
    let data = pdf::write(&paged.page, library, &paged.picture, &paged.title, &paged.destination);

    Ok(Made {data, page: paged.page.len(), missing: paged.missing})
}


/// Typesets a LaTeX document into pages
pub fn typeset(
    source: &str,
    library: &Library,
    style: &Style,
    base: &Path,
) -> Result<Paged, Error> {
    let joined = include(source, base, 0)?;
    let characters: Vec<char> = joined.chars().collect();
    let mut document = parse::parse(&characters)
        .map_err(|reason| Error::new(error::describe(&characters, &reason), reason.position))?;
    let mut shaped = style.clone();
    style::apply(&mut shaped, &document.option);

    let mut numbering = number::assign(&mut document);
    let (block, _, _) = layout::build(&document, library, &shaped, &numbering, base)?;
    page::paginate(&block, library, &shaped, &mut numbering);

    let (block, picture, missing) = layout::build(&document, library, &shaped, &numbering, base)?;
    let (page, destination) = page::paginate(&block, library, &shaped, &mut numbering);
    let title = node::plain(&document.title).trim().to_string();

    Ok(Paged {page, picture, destination, title, missing})
}


/// Replaces `\input{…}` and `\include{…}` with the contents of the file
fn include(source: &str, base: &Path, depth: usize) -> Result<String, Error> {
    if depth >= INCLUDE_LIMIT {
        return Err(Error::new("\\input が深すぎます", 0));
    }

    if !source.contains("\\input") && !source.contains("\\include{") {
        return Ok(source.to_string());
    }

    let raw: Vec<char> = source.chars().collect();
    let mut out = String::with_capacity(source.len());
    let mut at = 0;

    while at < raw.len() {
        let end = untouched_end(&raw, at);

        if end > at {
            out.extend(&raw[at..end]);
            at = end;

            continue;
        }

        if raw[at] != '\\' {
            out.push(raw[at]);
            at += 1;

            continue;
        }

        let found = ["\\input", "\\include"]
            .iter()
            .find(|name| starts_with(&raw, at, name));
        let name = match found {
            Some(name) => name,
            None => {
                let after = (at + 2).min(raw.len());
                out.extend(&raw[at..after]);
                at = after;

                continue;
            }
        };
        let (path, next) = match brace_after(&raw, at + name.chars().count()) {
            Some(pair) => pair,
            None => {
                out.push(raw[at]);
                at += 1;

                continue;
            }
        };
        let full = with_suffix(base, &path);
        let body = std::fs::read_to_string(&full).map_err(|reason| {
            Error::new(format!("読めません: {}: {reason}", full.display()), at)
        })?;
        out.push_str(&include(&body, base, depth + 1)?);
        at = next;
    }

    Ok(out)
}


/// The file name with `.tex` added
fn with_suffix(base: &Path, path: &str) -> std::path::PathBuf {
    let direct = base.join(path);

    if direct.is_file() {
        return direct;
    }

    base.join(format!("{path}.tex"))
}


/// Whether the sequence starts at that position
fn starts_with(source: &[char], at: usize, needle: &str) -> bool {
    let want: Vec<char> = needle.chars().collect();

    if at + want.len() > source.len() || source[at..at + want.len()] != want[..] {
        return false;
    }

    // Do not mistake \inputmore for \input
    !source
        .get(at + want.len())
        .is_some_and(|character| character.is_alphabetic())
}


/// The contents of the `{…}` that follows any white space, and the position after it
fn brace_after(source: &[char], at: usize) -> Option<(String, usize)> {
    let mut start = at;

    while start < source.len() && source[start].is_whitespace() {
        start += 1;
    }

    if source.get(start) != Some(&'{') {
        return None;
    }

    let mut end = start + 1;

    while end < source.len() && source[end] != '}' {
        end += 1;
    }

    if end >= source.len() {
        return None;
    }

    Some((source[start + 1..end].iter().collect(), end + 1))
}


/// The end of the comment, `\verb`, or `verbatim` that starts at that position, or that position
fn untouched_end(source: &[char], at: usize) -> usize {
    if source[at] == '%' {
        let mut end = at;

        while end < source.len() && source[end] != '\n' {
            end += 1;
        }

        return end;
    }

    if source[at] != '\\' {
        return at;
    }

    if starts_with(source, at, "\\verb") {
        let mut end = at + "\\verb".chars().count();

        if source.get(end) == Some(&'*') {
            end += 1;
        }

        let mark = match source.get(end) {
            Some(mark) => *mark,
            None => return source.len(),
        };
        end += 1;

        while end < source.len() && source[end] != mark {
            end += 1;
        }

        return (end + 1).min(source.len());
    }

    let open: Vec<char> = "\\begin{verbatim}".chars().collect();

    if !source[at..].starts_with(&open) {
        return at;
    }

    let close: Vec<char> = "\\end{verbatim}".chars().collect();
    let mut end = at + open.len();

    while end < source.len() && !source[end..].starts_with(&close) {
        end += 1;
    }

    (end + close.len()).min(source.len())
}


#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;
    use std::sync::OnceLock;

    /// The typefaces for the tests. They are loaded only once
    fn library() -> &'static Library {
        static ONCE: OnceLock<Library> = OnceLock::new();

        ONCE.get_or_init(|| {
            font::open(
                Path::new("font/latinmodern-math.otf"),
                None,
                None,
                None,
                None,
                None,
                japanese().as_deref(),
            )
            .expect("試験用の書体")
        })
    }

    /// The typeface for Japanese text on this machine. If there is none, the tests for Japanese
    /// text are skipped
    fn japanese() -> Option<PathBuf> {
        let path = PathBuf::from("/Library/Fonts/Arial Unicode.ttf");

        if path.is_file() {Some(path)} else {None}
    }

    fn make(body: &str) -> Made {
        let source =
            format!("\\documentclass{{article}}\n\\begin{{document}}\n{body}\n\\end{{document}}");

        render(&source, library(), &Style::default(), Path::new("test")).expect(body)
    }

    fn text_of(made: &Made) -> String {
        String::from_utf8_lossy(&made.data).to_string()
    }

    #[test]
    fn a_document_becomes_a_pdf() {
        let made = make("こんにちは。 Hello.");
        assert!(made.data.starts_with(b"%PDF-1.7"));
        assert!(made.data.ends_with(b"%%EOF\n"));
        assert_eq!(made.page, 1);

        let body = text_of(&made);
        assert!(body.contains("/Type /Catalog"));
        assert!(body.contains("/Type /Pages"));
        assert!(body.contains("/Subtype /Type0"));
        assert!(body.contains("/Encoding /Identity-H"));
        assert!(body.contains("startxref"));
    }

    #[test]
    fn a_long_document_runs_over_more_than_one_page() {
        let one = "This is a paragraph that fills a good deal of a line, and there are many. ";
        let made = make(&one.repeat(200));
        assert!(made.page > 1, "{}", made.page);
    }

    #[test]
    fn math_and_chemistry_come_out() {
        let made = make(
            "文中に $e^{i\\pi}+1=0$ があります。\n\n\
\\[ x = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a} \\]\n\n\
\\chemfig{H_3C-CH_2-OH}",
        );
        assert_eq!(made.page, 1);
        assert!(made.data.len() > 10_000);
    }

    #[test]
    fn headings_reach_the_outline() {
        let made = make("\\section{はじめに}\\subsection{その中}本文。");
        let body = text_of(&made);
        assert!(body.contains("/Type /Outlines"));
        assert!(body.contains("/PageMode /UseOutlines"));
    }

    #[test]
    fn a_picture_is_carried_over_as_it_is() {
        let made = make("\\includegraphics[width=3cm]{color.png}");
        let body = text_of(&made);
        assert!(body.contains("/Subtype /Image"));
        assert!(body.contains("/Filter /FlateDecode"));
        assert!(body.contains("/Predictor 15"));
    }

    #[test]
    fn an_unknown_command_says_where_it_is() {
        let source = "\\documentclass{article}\\begin{document}\\nosuchthing\\end{document}";
        let found = render(source, library(), &Style::default(), Path::new("test")).unwrap_err();
        assert!(found.message.contains("知らない命令"), "{}", found.message);
        assert!(found.message.contains("行"), "{}", found.message);
    }

    #[test]
    fn a_missing_picture_says_so() {
        let source =
            "\\documentclass{article}\\begin{document}\\includegraphics{nope}\\end{document}";
        let found = render(source, library(), &Style::default(), Path::new("test")).unwrap_err();
        assert!(found.message.contains("画像が見つかりません"), "{}", found.message);
    }

    #[test]
    fn japanese_needs_no_missing_letters() {
        if japanese().is_none() {
            return;
        }

        let made = make("日本語の文章を組みます。ひらがな、カタカナ、漢字。");
        assert!(made.missing.is_empty(), "{:?}", made.missing);
    }

    #[test]
    fn ruby_does_not_change_the_line_spacing() {
        if japanese().is_none() {
            return;
        }

        let plain = make("漢字の上に何も載せない行です。\n\n同じ行を続けます。");
        let ruby = make("\\ruby{漢字}{かんじ}の上に何も載せない行です。\n\n同じ行を続けます。");
        assert_eq!(plain.page, ruby.page);
        assert!(ruby.missing.is_empty(), "{:?}", ruby.missing);
    }

    #[test]
    fn a_table_and_a_list_come_out() {
        let made = make(
            "\\begin{itemize}\\item 1 つ目\\item 2 つ目\\end{itemize}\n\n\
\\begin{tabular}{|l|r|}\\hline a & 1 \\\\ b & 22 \\\\ \\hline\\end{tabular}",
        );
        assert_eq!(made.page, 1);
        assert!(text_of(&made).contains(" re f"));
    }

    #[test]
    fn a_defined_command_is_opened_up() {
        let made = make("\\newcommand{\\hi}[1]{こんにちは、#1さん}\\hi{本郷}");
        assert_eq!(made.page, 1);
        assert!(made.missing.is_empty() || japanese().is_none());
    }

    #[test]
    fn a_page_reference_finds_the_page_it_points_at() {
        let source = format!(
            "\\documentclass{{article}}\\begin{{document}}\\tableofcontents\n\n\
\\section{{A}}\\label{{a}}\n\n{}\n\n\\newpage\\section{{B}}\\label{{b}}\n\n\
\\ref{{b}} は \\pageref{{b}} ページ。\\end{{document}}",
            "text ".repeat(400)
        );
        let characters: Vec<char> = source.chars().collect();
        let mut document = parse::parse(&characters).unwrap();
        let mut numbering = number::assign(&mut document);
        let style = Style::default();
        let base = Path::new("test");
        let (block, _, _) =
            layout::build(&document, library(), &style, &numbering, base).unwrap();
        let (leaf, destination) = page::paginate(&block, library(), &style, &mut numbering);
        assert_eq!(leaf.len(), 2);
        assert_eq!(numbering.page.get("a"), Some(&1));
        assert_eq!(numbering.page.get("b"), Some(&2));
        assert_eq!(numbering.page.get("contents:1"), Some(&2));
        // The table of contents itself is also a destination
        assert_eq!(destination.len(), 3);
        assert_eq!(destination[0].title, "目次");
        assert_eq!(destination[2].title, "2 B");
    }

    #[test]
    fn a_page_becomes_svg() {
        let source = "\\documentclass{article}\\begin{document}Hello $x^2$\\end{document}";
        let paged = typeset(source, library(), &Style::default(), Path::new("test")).unwrap();
        let mut path = svg::GlyphPath::new();
        let drawn = svg::render(&paged.page[0], library(), "p0", &mut path);
        assert!(drawn.starts_with("<svg"));
        assert!(drawn.ends_with("</svg>"));
        assert!(drawn.contains("<path id=\"p0-"));
        assert!(drawn.contains("<use href=\"#p0-"));
    }

    #[test]
    fn a_float_too_large_for_the_top_still_comes_out() {
        let made = make(
            "Text.\n\n\\begin{figure}\\rule{1cm}{500pt}\\caption{A large figure}\\end{figure}",
        );
        assert_eq!(made.page, 2);
    }

    #[test]
    fn an_input_inside_a_comment_is_left_alone() {
        let source = "前 % \\input{nothing}\n後";
        assert_eq!(include(source, Path::new("test"), 0).unwrap(), source);
    }

    #[test]
    fn an_input_inside_verbatim_text_is_left_alone() {
        let source =
            "\\verb|\\input{nothing}|\n\\begin{verbatim}\n\\input{nothing}\n\\end{verbatim}";
        assert_eq!(include(source, Path::new("test"), 0).unwrap(), source);
    }

    #[test]
    fn an_input_after_an_escaped_percent_is_read() {
        assert!(include("100\\% \\input{nothing}", Path::new("test"), 0).is_err());
    }
}