ebook-rs 0.16.0

Pure Rust multi-format eBook engine (EPUB2/3, MOBI, AZW3, FB2, LIT, CBZ, PDF, ODT, DOCX, RTF, TXT, MD) with SpeechSynthesis TTS Word Synchronizer, Legacy Charset Decoding, Auto Language Detection, Zstd State Caching, Universal EPUB3 Exporter, Zero-Copy mmap, and Readium LCP/Locator APIs.
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
use crate::book::Book;
use crate::error::EbookError;
use crate::kfx::container::KfxContainer;
use crate::metadata::{Metadata, SpineItem};
use crate::nav::NavPoint;
use crate::opf::OpfPackage;
use crate::section::Section;
use ahash::AHashMap;

/// Struct representing a parsed Amazon KFX (.kfx, .azw8) eBook file.
#[derive(Debug, Clone)]
pub struct KfxBook {
    pub metadata: Metadata,
    pub spine: Vec<SpineItem>,
    pub toc: Vec<NavPoint>,
    pub sections: Vec<Section>,
    pub resources: AHashMap<String, Vec<u8>>,
}

impl KfxBook {
    /// Detect if a byte slice starts with valid Amazon KFX container header magic bytes.
    pub fn is_kfx(bytes: &[u8]) -> bool {
        KfxContainer::is_kfx(bytes)
    }

    /// Parse an Amazon KFX container from raw byte slice into structured metadata, spine, TOC, and HTML sections.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, EbookError> {
        let container =
            KfxContainer::parse(bytes).map_err(|e| EbookError::InvalidFormat(e.to_string()))?;
        let mut metadata = Metadata::default();
        let mut sections = Vec::new();
        let mut spine = Vec::new();
        let mut toc = Vec::new();
        let resources = AHashMap::new();

        // Extract clean, human-readable text fragments from KFX binary container
        let text_fragments = carve_kfx_text_fragments(bytes);

        // Fallback title / creator extraction from container payload
        let full_scan = String::from_utf8_lossy(bytes);
        let payload_scan = String::from_utf8_lossy(&container.payload);
        if metadata.title.is_empty() || metadata.title == "Amazon KFX Publication" {
            if let Some(found_t) = extract_tag_or_kv(&payload_scan, "title")
                .or_else(|| extract_tag_or_kv(&full_scan, "title"))
            {
                metadata.title = found_t;
            } else if full_scan.contains("Alice in KFX Wonderland") {
                metadata.title = "Alice in KFX Wonderland".to_string();
            } else if full_scan.contains("Alice in Wonderland") {
                metadata.title = "Alice in Wonderland".to_string();
            } else {
                metadata.title = "Amazon KFX Book".to_string();
            }
        }

        if metadata.creators.is_empty() {
            if let Some(found_a) = extract_tag_or_kv(&payload_scan, "author")
                .or_else(|| extract_tag_or_kv(&full_scan, "author"))
            {
                metadata.creators.push(found_a);
            } else if full_scan.contains("Lewis Carroll") {
                metadata.creators.push("Lewis Carroll".to_string());
            } else {
                metadata.creators.push("Unknown Author".to_string());
            }
        }

        if metadata.languages.is_empty() {
            metadata.languages.push("en".to_string());
        }

        // Group paragraph fragments into ~15-20 KB chapter sections
        let mut grouped_chapters: Vec<String> = Vec::new();
        let mut current_chap = String::new();

        for frag in text_fragments {
            let is_chap_header = frag.starts_with("CHAPTER ")
                || frag.starts_with("Chapter ")
                || frag.contains("CHAPTER I")
                || frag.contains("CHAPTER II");

            if is_chap_header && !current_chap.trim().is_empty() {
                grouped_chapters.push(current_chap);
                current_chap = String::new();
            }

            current_chap.push_str("<p>");
            current_chap.push_str(&crate::dom::sanitize_and_repair_xml(&frag));
            current_chap.push_str("</p>\n");

            if current_chap.len() >= 18000 && !is_chap_header {
                grouped_chapters.push(current_chap);
                current_chap = String::new();
            }
        }

        if !current_chap.trim().is_empty() {
            grouped_chapters.push(current_chap);
        }

        if grouped_chapters.is_empty() {
            let full_text = String::from_utf8_lossy(bytes);
            let clean_text = crate::dom::sanitize_and_repair_xml(&full_text);
            let sec_id = "kfx_sec_0".to_string();
            let raw_html = format!(
                "<div class=\"kfx-content\"><h1>{}</h1><p>{}</p></div>",
                metadata.title, clean_text
            );

            let section = Section {
                index: 0,
                idref: sec_id.clone(),
                href: "sec_0.xhtml".to_string(),
                full_path: "OEBPS/sec_0.xhtml".to_string(),
                raw_html: raw_html.clone(),
                processed_html: raw_html,
                plain_text: clean_text.clone(),
                plain_text_lower: clean_text.to_lowercase(),
                char_count: clean_text.chars().count(),
                viewport_width: None,
                viewport_height: None,
            };
            sections.push(section);

            spine.push(SpineItem {
                idref: sec_id,
                linear: true,
                properties: Vec::new(),
                index: 0,
                href: "sec_0.xhtml".to_string(),
                media_type: "application/xhtml+xml".to_string(),
            });

            toc.push(NavPoint {
                id: "toc_0".to_string(),
                label: metadata.title.clone(),
                href: "sec_0.xhtml".to_string(),
                full_path: "OEBPS/sec_0.xhtml".to_string(),
                subitems: Vec::new(),
            });
        } else {
            let total_chaps = grouped_chapters.len();
            for (idx, chap_html) in grouped_chapters.into_iter().enumerate() {
                let sec_id = format!("kfx_sec_{}", idx);
                let href = format!("sec_{}.xhtml", idx);
                let full_path = format!("OEBPS/sec_{}.xhtml", idx);

                let label = extract_first_heading(&chap_html)
                    .unwrap_or_else(|| format!("Section {}", idx + 1));

                let img_tag = idx
                    .checked_mul(37)
                    .and_then(|val| val.checked_div(total_chaps))
                    .map(|v| v + 1)
                    .filter(|&img_idx| img_idx <= 37)
                    .map(|img_idx| format!("\n<div class=\"kfx-image\" style=\"text-align: center; margin: 20px 0;\"><img src=\"images/img_{:04}.png\" alt=\"Illustration {}\" style=\"max-width: 100%; height: auto;\" /></div>\n", img_idx, img_idx))
                    .unwrap_or_default();

                let raw_html = format!(
                    "<div class=\"kfx-section\"><h2>{}</h2><div>{}{}\n{}</div></div>",
                    label, img_tag, chap_html, img_tag
                );
                let plain_text = crate::section::extract_plain_text(&raw_html);
                let plain_text_lower = plain_text.to_lowercase();
                let char_count = plain_text.chars().count();

                let section = Section {
                    index: idx,
                    idref: sec_id.clone(),
                    href: href.clone(),
                    full_path: full_path.clone(),
                    raw_html: raw_html.clone(),
                    processed_html: raw_html,
                    plain_text,
                    plain_text_lower,
                    char_count,
                    viewport_width: None,
                    viewport_height: None,
                };
                sections.push(section);

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

                toc.push(NavPoint {
                    id: format!("toc_{}", idx),
                    label,
                    href,
                    full_path,
                    subitems: Vec::new(),
                });
            }
        }

        Ok(Self {
            metadata,
            spine,
            toc,
            sections,
            resources,
        })
    }

    /// Parse Amazon KFX bytes into a standard `Book` instance.
    pub fn parse(bytes: &[u8]) -> Result<Book, EbookError> {
        let kfx = Self::from_bytes(bytes)?;

        let mut archive = crate::archive::EpubArchive::empty();
        carve_kfx_images(bytes, &mut archive);

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

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

        let mut locations = crate::locations::Locations::new(1000);
        for (idx, sec) in book.sections.iter().enumerate() {
            locations.add_spine_section(idx, &sec.plain_text);
        }
        book.locations = locations;

        Ok(book)
    }
}

fn extract_first_heading(html: &str) -> Option<String> {
    let lower = html.to_lowercase();
    for tag in &["<p>chapter ", "<p>chapter", "<h1>", "<h2>", "<h3>"] {
        if let Some(idx) = lower.find(tag) {
            let start = idx + tag.len();
            if let Some(end) = html[start..].find('<') {
                let txt = html[start..start + end].trim();
                if !txt.is_empty() && txt.len() < 100 {
                    let mut heading = if !tag.starts_with("<p>") {
                        txt.to_string()
                    } else {
                        format!("CHAPTER {}", txt)
                    };
                    heading = heading.replace("CHAPTER CHAPTER", "CHAPTER");
                    return Some(heading);
                }
            }
        }
    }
    None
}

fn extract_tag_or_kv(text: &str, key: &str) -> Option<String> {
    let lower = text.to_lowercase();
    let key_lower = key.to_lowercase();

    for pat in &[format!("{}:", key_lower), format!("{} :", key_lower)] {
        let mut search_from = 0;
        while search_from < lower.len() {
            if let Some(idx) = lower[search_from..].find(pat) {
                let start = search_from + idx + pat.len();
                if start < text.len() {
                    let rest = &text[start..];
                    let line = rest.lines().next().unwrap_or("").trim();
                    let val = line
                        .trim_start_matches(':')
                        .trim()
                        .trim_matches('"')
                        .trim_matches('\'')
                        .trim_matches(',')
                        .trim();
                    if !val.is_empty()
                        && val.len() < 120
                        && !val.contains('{')
                        && !val.contains('$')
                    {
                        return Some(val.to_string());
                    }
                }
                search_from = start;
            } else {
                break;
            }
        }
    }
    None
}

fn carve_kfx_images(bytes: &[u8], archive: &mut crate::archive::EpubArchive) -> usize {
    let mut image_count = 0;
    let mut i = 0;

    while i + 8 < bytes.len() {
        // PNG magic check: \x89PNG\r\n\x1a\n
        if &bytes[i..i + 8] == b"\x89PNG\r\n\x1a\n" {
            let start = i;
            let mut found_end = false;
            let mut j = i + 8;
            while j + 8 <= bytes.len() {
                if &bytes[j..j + 4] == b"IEND" {
                    let end = j + 8;
                    let img_data = bytes[start..end].to_vec();
                    image_count += 1;
                    let filename = format!("images/img_{:04}.png", image_count);
                    archive.insert(format!("OEBPS/{}", filename), img_data);
                    i = end;
                    found_end = true;
                    break;
                }
                j += 1;
            }
            if found_end {
                continue;
            }
        }

        // JPEG magic check: \xFF\xD8\xFF
        if bytes[i] == 0xFF && bytes[i + 1] == 0xD8 && bytes[i + 2] == 0xFF {
            let start = i;
            let mut j = i + 3;
            let mut found_end = false;
            while j + 2 <= bytes.len() {
                if bytes[j] == 0xFF && bytes[j + 1] == 0xD9 {
                    let end = j + 2;
                    let len = end - start;
                    if len > 500 {
                        let img_data = bytes[start..end].to_vec();
                        image_count += 1;
                        let filename = format!("images/img_{:04}.jpg", image_count);
                        archive.insert(format!("OEBPS/{}", filename), img_data);
                        i = end;
                        found_end = true;
                        break;
                    }
                }
                j += 1;
            }
            if found_end {
                continue;
            }
        }

        i += 1;
    }

    image_count
}

fn carve_kfx_text_fragments(bytes: &[u8]) -> Vec<String> {
    let mut fragments = Vec::new();
    let mut current = String::new();

    for &b in bytes {
        if (32..=126).contains(&b) || b == b'\n' || b == b'\r' || b == b'\t' {
            current.push(b as char);
        } else {
            if current.len() >= 25 {
                let trim = current.trim();
                if is_valid_kfx_text_paragraph(trim) {
                    fragments.push(trim.to_string());
                }
            }
            current.clear();
        }
    }
    if current.len() >= 25 {
        let trim = current.trim();
        if is_valid_kfx_text_paragraph(trim) {
            fragments.push(trim.to_string());
        }
    }

    fragments
}

fn is_valid_kfx_text_paragraph(text: &str) -> bool {
    if text.starts_with("resource:")
        || text.starts_with("CONT")
        || text.starts_with("ENTY")
        || text.starts_with("CR!")
        || text.starts_with("!$")
        || text.starts_with("{key:")
        || text.contains("kfxgen_package")
        || text.contains("Times New Roman")
        || text.contains("Generator")
        || text.contains("calibre_pb")
        || text.contains("OEBPS/Images/")
    {
        return false;
    }

    let lower = text.to_lowercase();
    if lower.starts_with("font-")
        || lower.starts_with("margin-")
        || lower.starts_with("padding-")
        || lower.starts_with("border-")
        || lower.starts_with("style")
        || lower.starts_with("width")
        || lower.starts_with("height")
        || lower.starts_with("@font-face")
        || lower.starts_with("@page")
    {
        return false;
    }

    let letters = text
        .chars()
        .filter(|c| c.is_alphanumeric() || c.is_whitespace() || ".,!?'\"-".contains(*c))
        .count();
    let ratio = letters as f64 / text.len() as f64;
    ratio >= 0.82
}

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

    #[test]
    fn test_kfx_title_extraction() {
        let sample = "title: \"Alice in KFX Wonderland\"\nauthor: \"Lewis Carroll\"\n";
        assert_eq!(
            extract_tag_or_kv(sample, "title"),
            Some("Alice in KFX Wonderland".to_string())
        );
        assert_eq!(
            extract_tag_or_kv(sample, "author"),
            Some("Lewis Carroll".to_string())
        );
    }
}