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.2.0
Copyright
Rollpie, Irohabook
Publisher
math
Published
2026-10-15
Size
105.5 KB
Downloads
24
Checksum
f438a48f6f2fc61d892f930eb4fb56fb5a8e302e733a8c3a6283d0ff7350cd08
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
//! hongo - LaTeX の文書を PDF に組み上げるライブラリ
//!
//! 読み取り(token, parse)と、番号付け(number)と、組み上げ(layout, line, page)と、
//! 書き出し(pdf, stream, embed)を分けている。数式は suki、化学式は lina に渡す。
//!
//! 外の crate には依存しない。フォントの読み取りも PDF の書き出しも自前で行う。
//!
//! ```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();
//! ```
//!
//! 組むのに 2 回通す。1 回目でどの見出しがどのページに来るかが決まり、
//! 2 回目でその番号を目次と `\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 text;
pub mod token;

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


/// `\input` をたどる深さの上限
const INCLUDE_LIMIT: usize = 16;


/// 組み上がったもの
#[derive(Debug, Clone, PartialEq)]
pub struct Made {
    /// PDF そのもの
    pub data: Vec<u8>,
    pub page: usize,
    /// どの書体にもなかった字
    pub missing: BTreeSet<char>,
}


/// LaTeX の文書を PDF にする
pub fn render(source: &str, library: &Library, style: &Style, base: &Path) -> Result<Made, 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);
    let data = pdf::write(&page, library, &picture, title.trim(), &destination);

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


/// `\input{…}` と `\include{…}` を、そのファイルの中身に置き換える
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 skip = verbatim_ranges(&raw);
    let mut out = String::with_capacity(source.len());
    let mut at = 0;

    while at < raw.len() {
        if skip.iter().any(|(start, end)| at >= *start && at < *end) {
            out.push(raw[at]);
            at += 1;

            continue;
        }

        let found = ["\\input", "\\include"]
            .iter()
            .find(|name| starts_with(&raw, at, name));

        match found {
            Some(name) => {
                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;
            }
            None => {
                out.push(raw[at]);
                at += 1;
            }
        }
    }

    Ok(out)
}


/// `.tex` を足したファイル名
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"))
}


/// その位置から並びが始まっているか
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;
    }

    // \input と \inputmore を取り違えない
    !source
        .get(at + want.len())
        .is_some_and(|character| character.is_alphabetic())
}


/// 空白を飛ばした先の `{…}` の中身と、その次の位置
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))
}


/// そのまま出す部分の範囲。ここでは `\input` を開かない
fn verbatim_ranges(source: &[char]) -> Vec<(usize, usize)> {
    let open: Vec<char> = "\\begin{verbatim}".chars().collect();
    let close: Vec<char> = "\\end{verbatim}".chars().collect();
    let mut found = Vec::new();
    let mut at = 0;

    while at < source.len() {
        if source[at..].len() >= open.len() && source[at..at + open.len()] == open[..] {
            let mut end = at + open.len();

            while end + close.len() <= source.len() {
                if source[end..end + close.len()] == close[..] {
                    break;
                }

                end += 1;
            }

            found.push((at, (end + close.len()).min(source.len())));
            at = end + close.len();

            continue;
        }

        at += 1;
    }

    found
}


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

    /// 試験に使う書体。読み込みは 1 度だけ
    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("試験用の書体")
        })
    }

    /// この機械にある和文の書体。なければ和文の試験は飛ばす
    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));
        // 目次そのものも飛び先になる
        assert_eq!(destination.len(), 3);
        assert_eq!(destination[0].title, "目次");
        assert_eq!(destination[2].title, "2 B");
    }
}