ebook-rs 0.16.1

Pure Rust multi-format eBook engine (EPUB 2/3, MOBI, AZW3, KFX, FB2, LIT, CBZ, PDF, ODT, DOCX, RTF, TXT, MD) featuring Mozilla UniFFI, Readium CFI/LCP, SpeechSynthesis TTS sync, CJK vertical/RTL reflow, EPUB3 optimizer, AI RAG BM25 chunking, zero-copy search, Zstd caching, Python/WASM bindings, and native MCP server.
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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use crate::archive::EpubArchive;
use crate::book::Book;
use crate::deobfuscate::FontDeobfuscator;
use crate::error::EbookError;
use crate::layout::RenditionLayout;
use crate::metadata::{Metadata, PageProgressionDirection, SpineItem};
use crate::nav::NavPoint;
use crate::opf::OpfPackage;
use crate::section::Section;
use ahash::AHashMap;

/// Rich Text Format (.rtf) document parser engine.
pub struct RtfBook;

#[derive(Debug, Clone, PartialEq, Eq, Default)]
struct RtfState {
    bold: bool,
    italic: bool,
    underline: bool,
    strike: bool,
    is_destination: bool,
    dest_name: String,
}

impl RtfBook {
    /// Parse Rich Text Format (.rtf) byte slice into a unified `Book` instance.
    pub fn parse(bytes: &[u8], title_fallback: &str) -> Result<Book, EbookError> {
        let text = match std::str::from_utf8(bytes) {
            Ok(s) => s.to_string(),
            Err(_) => String::from_utf8_lossy(bytes).to_string(),
        };

        if !text.starts_with("{\\rtf") && !text.contains("{\\rtf") {
            return Err(EbookError::InvalidFormat(
                "Not a valid RTF document (missing {\\rtf header)".to_string(),
            ));
        }

        let mut archive = EpubArchive::empty();
        let mut title = title_fallback.to_string();
        let mut creators = Vec::new();
        let mut description = None;

        let mut sections: Vec<Section> = Vec::new();
        let mut spine: Vec<SpineItem> = Vec::new();
        let mut toc: Vec<NavPoint> = Vec::new();

        let mut current_html = String::new();
        let mut current_text = String::new();
        let mut cur_run = String::new();
        let mut section_index = 0;
        let mut img_counter = 0;

        let chars: Vec<char> = text.chars().collect();
        let len = chars.len();
        let mut i = 0;

        let mut state_stack: Vec<RtfState> = vec![RtfState::default()];
        let mut cur_state = RtfState::default();

        let mut dest_buffer = String::new();
        let mut in_pict = false;
        let mut pict_hex = String::new();
        let mut pict_type = "png";

        let mut in_table = false;
        let mut table_cells: Vec<String> = Vec::new();

        let flush_run =
            |html: &mut String, text: &mut String, run: &mut String, state: &RtfState| {
                if run.is_empty() {
                    return;
                }
                let mut formatted = xml_escape(run);
                if state.strike {
                    formatted = format!("<s>{}</s>", formatted);
                }
                if state.underline {
                    formatted = format!("<u>{}</u>", formatted);
                }
                if state.italic {
                    formatted = format!("<em>{}</em>", formatted);
                }
                if state.bold {
                    formatted = format!("<strong>{}</strong>", formatted);
                }
                html.push_str(&formatted);
                text.push_str(run);
                run.clear();
            };

        let flush_section = |sec_idx: usize,
                             cur_html: &str,
                             cur_txt: &str,
                             sections: &mut Vec<Section>,
                             spine: &mut Vec<SpineItem>,
                             toc: &mut Vec<NavPoint>| {
            if cur_txt.trim().is_empty() && !sections.is_empty() {
                return;
            }
            let href = format!("section_{}.html", sec_idx);
            let full_html = format!("<div class=\"rtf-section\">\n{}\n</div>", cur_html);
            let char_count = cur_txt.chars().count();
            let plain_text_lower = cur_txt.to_lowercase();

            sections.push(Section {
                index: sec_idx,
                idref: format!("sec_{}", sec_idx),
                href: href.clone(),
                full_path: href.clone(),
                raw_html: full_html.clone(),
                processed_html: full_html,
                plain_text: cur_txt.to_string(),
                plain_text_lower,
                char_count,
                viewport_width: None,
                viewport_height: None,
            });

            spine.push(SpineItem {
                idref: format!("sec_{}", sec_idx),
                linear: true,
                properties: Vec::new(),
                index: sec_idx,
                href: href.clone(),
                media_type: "application/xhtml+xml".to_string(),
            });

            toc.push(NavPoint {
                id: format!("nav_{}", toc.len() + 1),
                label: format!("Section {}", sec_idx + 1),
                href: href.clone(),
                full_path: href,
                subitems: Vec::new(),
            });
        };

        while i < len {
            match chars[i] {
                '{' => {
                    flush_run(
                        &mut current_html,
                        &mut current_text,
                        &mut cur_run,
                        &cur_state,
                    );
                    state_stack.push(cur_state.clone());
                    cur_state.is_destination = false;
                    cur_state.dest_name.clear();
                    i += 1;
                }
                '}' => {
                    flush_run(
                        &mut current_html,
                        &mut current_text,
                        &mut cur_run,
                        &cur_state,
                    );
                    if in_pict {
                        if !pict_hex.is_empty() {
                            if let Ok(bin) = hex_to_bytes(&pict_hex) {
                                img_counter += 1;
                                let img_name = format!("images/img_{}.{}", img_counter, pict_type);
                                archive.insert(&img_name, bin);
                                current_html.push_str(&format!(
                                    "<p><img src=\"{}\" alt=\"image\" /></p>\n",
                                    img_name
                                ));
                            }
                        }
                        in_pict = false;
                        pict_hex.clear();
                    }

                    if cur_state.is_destination {
                        let dest = cur_state.dest_name.as_str();
                        let buf_clean = dest_buffer.trim().to_string();
                        if !buf_clean.is_empty() {
                            match dest {
                                "title" => title = buf_clean,
                                "author" => creators.push(buf_clean),
                                "doccomm" | "subject" => description = Some(buf_clean),
                                _ => {}
                            }
                        }
                        dest_buffer.clear();
                    }

                    if let Some(prev) = state_stack.pop() {
                        cur_state = prev;
                    }
                    i += 1;
                }
                '\\' => {
                    i += 1;
                    if i >= len {
                        break;
                    }

                    // Special escaped characters
                    match chars[i] {
                        '{' | '}' | '\\' => {
                            let c = chars[i];
                            if cur_state.is_destination {
                                dest_buffer.push(c);
                            } else if in_pict {
                                pict_hex.push(c);
                            } else {
                                cur_run.push(c);
                            }
                            i += 1;
                            continue;
                        }
                        '\'' => {
                            // Hex escape \'XX
                            if i + 2 < len {
                                let hex_str: String = chars[i + 1..=i + 2].iter().collect();
                                if let Ok(byte) = u8::from_str_radix(&hex_str, 16) {
                                    let ch = byte as char;
                                    if cur_state.is_destination {
                                        dest_buffer.push(ch);
                                    } else {
                                        cur_run.push(ch);
                                    }
                                }
                                i += 3;
                                continue;
                            }
                        }
                        '*' => {
                            // Ignorable destination marker
                            cur_state.is_destination = true;
                            i += 1;
                            continue;
                        }
                        _ => {}
                    }

                    // Read control word [a-zA-Z]+
                    let mut word = String::new();
                    while i < len && chars[i].is_ascii_alphabetic() {
                        word.push(chars[i]);
                        i += 1;
                    }

                    // Read optional numeric parameter
                    let mut is_neg = false;
                    if i < len && chars[i] == '-' {
                        is_neg = true;
                        i += 1;
                    }
                    let mut param_str = String::new();
                    while i < len && chars[i].is_ascii_digit() {
                        param_str.push(chars[i]);
                        i += 1;
                    }
                    let param: Option<i32> = if !param_str.is_empty() {
                        let p = param_str.parse::<i32>().unwrap_or(0);
                        Some(if is_neg { -p } else { p })
                    } else {
                        None
                    };

                    // Optional trailing space delimiter after control word
                    if i < len && chars[i] == ' ' {
                        i += 1;
                    }

                    match word.as_str() {
                        "b" => {
                            let next_bold = param != Some(0);
                            if next_bold != cur_state.bold {
                                flush_run(
                                    &mut current_html,
                                    &mut current_text,
                                    &mut cur_run,
                                    &cur_state,
                                );
                                cur_state.bold = next_bold;
                            }
                        }
                        "i" => {
                            let next_italic = param != Some(0);
                            if next_italic != cur_state.italic {
                                flush_run(
                                    &mut current_html,
                                    &mut current_text,
                                    &mut cur_run,
                                    &cur_state,
                                );
                                cur_state.italic = next_italic;
                            }
                        }
                        "ul" => {
                            let next_ul = param != Some(0);
                            if next_ul != cur_state.underline {
                                flush_run(
                                    &mut current_html,
                                    &mut current_text,
                                    &mut cur_run,
                                    &cur_state,
                                );
                                cur_state.underline = next_ul;
                            }
                        }
                        "ulnone" => {
                            if cur_state.underline {
                                flush_run(
                                    &mut current_html,
                                    &mut current_text,
                                    &mut cur_run,
                                    &cur_state,
                                );
                                cur_state.underline = false;
                            }
                        }
                        "strike" => {
                            let next_strike = param != Some(0);
                            if next_strike != cur_state.strike {
                                flush_run(
                                    &mut current_html,
                                    &mut current_text,
                                    &mut cur_run,
                                    &cur_state,
                                );
                                cur_state.strike = next_strike;
                            }
                        }
                        "par" => {
                            flush_run(
                                &mut current_html,
                                &mut current_text,
                                &mut cur_run,
                                &cur_state,
                            );
                            if !cur_state.is_destination && !in_pict {
                                current_html.push_str("<br/>\n");
                                current_text.push('\n');
                            }
                        }
                        "line" => {
                            flush_run(
                                &mut current_html,
                                &mut current_text,
                                &mut cur_run,
                                &cur_state,
                            );
                            if !cur_state.is_destination && !in_pict {
                                current_html.push_str("<br/>");
                                current_text.push('\n');
                            }
                        }
                        "page" => {
                            flush_run(
                                &mut current_html,
                                &mut current_text,
                                &mut cur_run,
                                &cur_state,
                            );
                            if !cur_state.is_destination && !in_pict && !current_text.is_empty() {
                                flush_section(
                                    section_index,
                                    &current_html,
                                    &current_text,
                                    &mut sections,
                                    &mut spine,
                                    &mut toc,
                                );
                                section_index += 1;
                                current_html.clear();
                                current_text.clear();
                            }
                        }
                        "tab" => {
                            flush_run(
                                &mut current_html,
                                &mut current_text,
                                &mut cur_run,
                                &cur_state,
                            );
                            if !cur_state.is_destination && !in_pict {
                                current_html.push_str("&emsp;");
                                current_text.push('\t');
                            }
                        }
                        "u" => {
                            if let Some(code) = param {
                                let unsigned_code = if code < 0 {
                                    (code + 65536) as u32
                                } else {
                                    code as u32
                                };
                                if let Some(ch) = char::from_u32(unsigned_code) {
                                    if cur_state.is_destination {
                                        dest_buffer.push(ch);
                                    } else {
                                        cur_run.push(ch);
                                    }
                                }
                                // Skip optional fallback char
                                if i < len && chars[i] == '?' {
                                    i += 1;
                                }
                            }
                        }
                        "info" | "title" | "author" | "subject" | "doccomm" | "keywords"
                        | "fonttbl" | "colortbl" | "stylesheet" => {
                            flush_run(
                                &mut current_html,
                                &mut current_text,
                                &mut cur_run,
                                &cur_state,
                            );
                            cur_state.is_destination = true;
                            cur_state.dest_name = word;
                        }
                        "pict" => {
                            flush_run(
                                &mut current_html,
                                &mut current_text,
                                &mut cur_run,
                                &cur_state,
                            );
                            in_pict = true;
                            pict_hex.clear();
                        }
                        "pngblip" => pict_type = "png",
                        "jpegblip" => pict_type = "jpg",
                        "trowd" => {
                            flush_run(
                                &mut current_html,
                                &mut current_text,
                                &mut cur_run,
                                &cur_state,
                            );
                            in_table = true;
                            table_cells.clear();
                        }
                        "cell" => {
                            flush_run(
                                &mut current_html,
                                &mut current_text,
                                &mut cur_run,
                                &cur_state,
                            );
                        }
                        "row" => {
                            flush_run(
                                &mut current_html,
                                &mut current_text,
                                &mut cur_run,
                                &cur_state,
                            );
                            if in_table {
                                current_html.push_str("<tr>\n");
                                for cell in &table_cells {
                                    current_html.push_str(&format!("  <td>{}</td>\n", cell));
                                }
                                current_html.push_str("</tr>\n");
                                table_cells.clear();
                            }
                        }
                        _ => {}
                    }
                }
                c => {
                    if in_pict {
                        if c.is_ascii_hexdigit() {
                            pict_hex.push(c);
                        }
                    } else if cur_state.is_destination {
                        dest_buffer.push(c);
                    } else if c != '\r' && c != '\n' {
                        cur_run.push(c);
                    }
                    i += 1;
                }
            }
        }

        // Flush remaining run and trailing section
        flush_run(
            &mut current_html,
            &mut current_text,
            &mut cur_run,
            &cur_state,
        );
        flush_section(
            section_index,
            &current_html,
            &current_text,
            &mut sections,
            &mut spine,
            &mut toc,
        );

        let metadata = Metadata {
            title,
            creators,
            publishers: Vec::new(),
            languages: vec!["en".to_string()],
            rights: None,
            description,
            identifier: None,
            pub_date: None,
            modified_date: None,
            subjects: vec!["Document".to_string()],
            cover_id: None,
            cover_href: None,
            direction: PageProgressionDirection::Ltr,
            meta_properties: AHashMap::new(),
            accessibility: Default::default(),
        };

        let opf = OpfPackage {
            version: "3.0".to_string(),
            opf_path: "content.opf".to_string(),
            opf_dir: "".to_string(),
            metadata,
            manifest: AHashMap::new(),
            spine,
            guide: Vec::new(),
            toc_item_id: None,
            nav_item_id: None,
        };

        let mut book = Book {
            archive,
            opf,
            layout: RenditionLayout::default(),
            toc,
            landmarks: Vec::new(),
            page_list: Vec::new(),
            sections,
            locations: crate::locations::Locations::default(),
            annotations: crate::annotations::AnnotationManager::default(),
            before_display_hooks: Vec::new(),
            font_deobfuscator: FontDeobfuscator::parse_encryption_xml(""),
            media_overlays: AHashMap::new(),
            render_cache: parking_lot::Mutex::new(AHashMap::new()),
        };

        book.generate_locations(1000);
        Ok(book)
    }
}

fn hex_to_bytes(hex: &str) -> Result<Vec<u8>, String> {
    let clean: String = hex.chars().filter(|c| c.is_ascii_hexdigit()).collect();
    if !clean.len().is_multiple_of(2) {
        return Err("Invalid hex length".to_string());
    }
    let mut bytes = Vec::with_capacity(clean.len() / 2);
    let chars: Vec<char> = clean.chars().collect();
    for chunk in chars.chunks(2) {
        let pair: String = chunk.iter().collect();
        let b = u8::from_str_radix(&pair, 16).map_err(|e| e.to_string())?;
        bytes.push(b);
    }
    Ok(bytes)
}

fn xml_escape(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}