prensa 0.2.0

Optimize EPUBs for e-ink readers
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
//! Optimize EPUBs for e-ink readers.
//!
//! The engine works on any reader/writer pair, so it does not care whether the
//! book comes from a file, memory or (later) a browser. Front ends such as the
//! `prensa` CLI handle paths, atomic writes and printing.
//!
//! ```no_run
//! use std::fs::File;
//!
//! let input = File::open("book.epub")?;
//! let output = File::create("book-optimized.epub")?;
//! let report = prensa::optimize(input, output, &prensa::Options::default())?;
//! println!("{} image(s) optimized", report.images_changed);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

mod error;
mod fonts;
mod images;
mod inline;
mod options;
mod passes;
mod refs;
mod report;

pub use error::{Error, Source};
pub use options::{Device, InvalidOptions, Options, OptionsBuilder, Rotation};
pub use report::{Progress, Report, Warning};

use error::from_zip;
use images::Optimized;
use passes::{Action, Book, Fallback, Outcome, decide};
use std::collections::{HashMap, HashSet};
use std::io::{Read, Seek, Write};
use zip::write::SimpleFileOptions;
use zip::{CompressionMethod, ZipArchive, ZipWriter};

/// Largest entry read into memory to be rewritten (CSS, OPF, images).
///
/// Real books stay far below this; a bigger entry is a zip bomb or a broken
/// archive, and reading it would exhaust memory. Small in tests so the limit
/// can be hit without writing hundreds of megabytes.
const MAX_ENTRY_SIZE: u64 = if cfg!(test) {
    64 * 1024
} else {
    128 * 1024 * 1024
};

/// Read entry `i` fully, or `None` if it is larger than [`MAX_ENTRY_SIZE`].
///
/// The declared size is checked first, but it comes from the archive and can
/// lie, so the read itself is capped too.
fn read_entry<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    i: usize,
) -> Result<Option<Vec<u8>>, Error> {
    let entry = archive.by_index(i).map_err(from_zip)?;
    if entry.size() > MAX_ENTRY_SIZE {
        return Ok(None);
    }
    let mut bytes = Vec::new();
    entry.take(MAX_ENTRY_SIZE + 1).read_to_end(&mut bytes)?;
    Ok((bytes.len() as u64 <= MAX_ENTRY_SIZE).then_some(bytes))
}

fn too_large() -> String {
    format!(
        "entry is larger than {} MiB",
        MAX_ENTRY_SIZE / (1024 * 1024)
    )
}

/// Copy entry `i` as stored in the input, without decompressing and
/// recompressing it.
fn copy_raw<R, W>(
    archive: &mut ZipArchive<R>,
    writer: &mut ZipWriter<W>,
    i: usize,
) -> Result<(), Error>
where
    R: Read + Seek,
    W: Write + Seek,
{
    let entry = archive.by_index_raw(i).map_err(from_zip)?;
    writer.raw_copy_file(entry).map_err(from_zip)
}

fn malformed(entry: &str, source: Source) -> Error {
    Error::Malformed {
        entry: entry.to_owned(),
        source,
    }
}

/// What became of an image: rewritten (`Some`), kept because rewriting
/// gained nothing (`None`), or not readable as an image (`Err`, a warning).
type ImageResult = anyhow::Result<Option<Optimized>>;

/// Read image entry `i` and run the images pass on it.
fn optimize_image<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    i: usize,
    kind: images::Kind,
    opts: &Options,
) -> Result<ImageResult, Error> {
    Ok(match read_entry(archive, i)? {
        Some(bytes) => images::optimize(kind, &bytes, opts),
        None => Err(anyhow::anyhow!(too_large())),
    })
}

/// A warning for an entry copied unchanged.
fn copied_unchanged(entry: &str, reason: impl std::fmt::Display) -> Warning {
    Warning {
        entry: entry.to_owned(),
        // `{reason}`, not `{reason:#}`: `image` errors already include their
        // cause in the message, so the chain would repeat it. Some decoders
        // end it with a newline; a warning is one line.
        message: format!("{}; copied unchanged", reason.to_string().trim_end()),
    }
}

/// Read an EPUB from `input` and write an optimized copy to `output`.
///
/// Both are taken by value; pass `&mut file` to keep using it afterwards
/// (`&mut T` is `Read`/`Write` + `Seek` whenever `T` is).
///
/// The output follows the EPUB container (OCF) rules: `mimetype` is the first
/// entry and is stored uncompressed. On error, `output` may hold a partial
/// file; callers writing to disk should write to a temporary file and rename it.
pub fn optimize<R, W>(input: R, output: W, opts: &Options) -> Result<Report, Error>
where
    R: Read + Seek,
    W: Write + Seek,
{
    optimize_with_progress(input, output, opts, |_| {})
}

/// Like [`optimize`], calling `on_progress` before each entry is processed.
///
/// On success it is called exactly once per entry (directories included), in
/// archive order, with `done` equal to the entry's index. Nothing is called
/// after the last entry; treat the return of this function as completion.
///
/// The library never prints; front ends use this to draw a progress bar.
/// Images take most of the time, so progress is not linear in entries.
pub fn optimize_with_progress<R, W, F>(
    input: R,
    output: W,
    opts: &Options,
    mut on_progress: F,
) -> Result<Report, Error>
where
    R: Read + Seek,
    W: Write + Seek,
    F: FnMut(Progress<'_>),
{
    let mut archive = ZipArchive::new(input).map_err(from_zip)?;
    let mut writer = ZipWriter::new(output);

    let stored = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);
    let deflated = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);
    writer.start_file("mimetype", stored).map_err(from_zip)?;
    writer.write_all(b"application/epub+zip")?;

    // Name and size of every entry. Sizes in the report are as stored in the
    // EPUB (compressed), so they add up with the file size on disk.
    let entries = (0..archive.len())
        .map(|i| {
            let entry = archive.by_index_raw(i).map_err(from_zip)?;
            let name = entry.name().map_err(from_zip)?.into_owned();
            Ok((name, entry.compressed_size()))
        })
        .collect::<Result<Vec<(String, u64)>, Error>>()?;

    // Fonts declared in the manifest, so a font without a font extension is
    // removed together with its manifest item and `encryption.xml` entry.
    let mut book = Book::default();
    for (i, (name, _)) in entries.iter().enumerate() {
        if !name.to_ascii_lowercase().ends_with(".opf") {
            continue;
        }
        let bytes =
            read_entry(&mut archive, i)?.ok_or_else(|| malformed(name, too_large().into()))?;
        let opf = std::str::from_utf8(&bytes).map_err(|e| malformed(name, Box::new(e)))?;
        book.fonts
            .add_manifest(opf, name)
            .map_err(|e| malformed(name, e.into()))?;
    }

    let mut report = Report::default();

    // What happens to each image must be known before anything is written:
    // chapters that refer to an image can come earlier in the archive, and a
    // renamed image (WebP, BMP, GIF) or a turned one changes them. Images that
    // may change format are converted here; for JPEG and PNG, which keep their
    // name, the header is enough to know whether they will be turned.
    let mut taken: HashSet<String> = entries.iter().map(|(n, _)| n.to_lowercase()).collect();
    let mut converted: HashMap<usize, ImageResult> = HashMap::new();
    for (i, (name, _)) in entries.iter().enumerate() {
        let Some(kind) = images::kind_of(name) else {
            continue;
        };
        if !kind.may_change_format() {
            if opts.rotate().is_some()
                && let Some(bytes) = read_entry(&mut archive, i)?
                && let Some((w, h)) = images::dimensions(kind, &bytes)
                && images::rotation_for(w, h, opts).is_some()
            {
                book.rotated.insert(name.clone());
            }
            continue;
        }
        let result = optimize_image(&mut archive, i, kind, opts)?;
        if let Ok(Some(image)) = &result {
            let format = image.format;
            book.renames
                .add(name, format.extension(), format.media_type(), &mut taken);
            if image.rotated {
                book.rotated.insert(name.clone());
            }
        }
        converted.insert(i, result);
    }

    // Images embedded in chapters as `data:` URIs become entries of their own,
    // listed in the manifest (which may come before the chapters).
    for (i, (name, _)) in entries.iter().enumerate() {
        if !passes::is_document(name) {
            continue;
        }
        let Some(bytes) = read_entry(&mut archive, i)? else {
            continue;
        };
        // Chapters that are not UTF-8 or not XML are reported by their
        // rewrite, if they have anything to rewrite.
        let Ok(text) = std::str::from_utf8(&bytes) else {
            continue;
        };
        if !text.contains("data:image/") {
            continue;
        }
        let Ok(uris) = refs::data_uris(text) else {
            continue;
        };
        book.inline
            .extract(name, uris, opts, &mut taken, &mut report.warnings);
    }

    // Files that survive the passes, used to drop directory entries left empty
    // (e.g. `fonts/` after removing the fonts inside it).
    let kept_files: Vec<&str> = entries
        .iter()
        .map(|(name, _)| name.as_str())
        .filter(|n| {
            !n.ends_with('/') && !matches!(decide(n, &book), Action::Skip | Action::RemoveFont)
        })
        .collect();

    let total = entries.len();
    for (i, (name, size)) in entries.iter().enumerate() {
        on_progress(Progress {
            done: i,
            total,
            entry: name,
        });

        if name.ends_with('/') {
            if kept_files.iter().any(|f| f.starts_with(name.as_str())) {
                copy_raw(&mut archive, &mut writer, i)?;
            }
            continue;
        }

        match decide(name, &book) {
            Action::Skip => {}
            Action::RemoveFont => {
                report.fonts_removed += 1;
                report.font_bytes += size;
            }
            Action::CopyRaw => copy_raw(&mut archive, &mut writer, i)?,
            Action::Rewrite(transform, fallback) => {
                let bytes = read_entry(&mut archive, i)?
                    .ok_or_else(|| malformed(name, too_large().into()))?;
                let outcome = match String::from_utf8(bytes) {
                    Ok(text) => transform(&text, name, &book).map_err(Source::from),
                    Err(e) => Err(Box::new(e.utf8_error()) as Source),
                };
                match (outcome, fallback) {
                    (Ok(Outcome::Keep), _) => copy_raw(&mut archive, &mut writer, i)?,
                    (Ok(Outcome::Replace(new_text)), _) => {
                        writer
                            .start_file(name.as_str(), deflated)
                            .map_err(from_zip)?;
                        writer.write_all(new_text.as_bytes())?;
                    }
                    (Ok(Outcome::Drop), _) => {}
                    (Err(_), Fallback::Copy) => copy_raw(&mut archive, &mut writer, i)?,
                    (Err(e), Fallback::CopyWithWarning) => {
                        report.warnings.push(copied_unchanged(
                            name,
                            format_args!("{e}; image references in it not updated"),
                        ));
                        copy_raw(&mut archive, &mut writer, i)?;
                    }
                    // Broken XML aborts everything: no output beats an inconsistent EPUB
                    // (font removed but still listed in the manifest).
                    (Err(e), Fallback::Reject) => return Err(malformed(name, e)),
                }
            }
            Action::Image(kind) => {
                report.image_bytes_before += size;
                let result = match converted.remove(&i) {
                    Some(result) => result,
                    None => optimize_image(&mut archive, i, kind, opts)?,
                };
                match result {
                    Ok(Some(image)) => {
                        report.images_changed += 1;
                        report.image_bytes_after += image.bytes.len() as u64;
                        let new_name = book.renames.new_path(name).unwrap_or(name);
                        if new_name != name {
                            report.images_converted += 1;
                        }
                        if image.rotated {
                            report.images_rotated += 1;
                        }
                        // JPEG/PNG are already compressed: deflating them again only costs CPU.
                        writer.start_file(new_name, stored).map_err(from_zip)?;
                        writer.write_all(&image.bytes)?;
                        continue;
                    }
                    // Rewriting gained nothing.
                    Ok(None) => report.images_kept += 1,
                    // An image that fails to decode does not fail the book: it stays as it was.
                    Err(e) => {
                        report.images_failed += 1;
                        report.warnings.push(copied_unchanged(name, e));
                    }
                }
                report.image_bytes_after += size;
                copy_raw(&mut archive, &mut writer, i)?;
            }
        }
    }

    for file in &book.inline.files {
        report.images_extracted += 1;
        if file.changed {
            report.images_changed += 1;
        } else {
            report.images_kept += 1;
        }
        if file.rotated {
            report.images_rotated += 1;
        }
        // Inside the chapter the image was base64 text, compressed with it;
        // its decoded size is the closest to an image entry's size.
        report.image_bytes_before += file.original_len as u64;
        report.image_bytes_after += file.bytes.len() as u64;
        writer
            .start_file(file.path.as_str(), stored)
            .map_err(from_zip)?;
        writer.write_all(&file.bytes)?;
    }

    writer.finish().map_err(from_zip)?;
    Ok(report)
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;
    use quick_xml::Reader;
    use quick_xml::events::Event;
    use std::io::Cursor;

    fn escape(text: &str) -> String {
        text.replace('&', "&amp;")
            .replace('<', "&lt;")
            .replace('>', "&gt;")
    }

    /// Text a reader would show: XHTML text nodes and entity references, in order.
    fn visible_text(xhtml: &str) -> String {
        let mut reader = Reader::from_str(xhtml);
        let mut out = String::new();
        loop {
            match reader.read_event().expect("valid XHTML") {
                Event::Eof => break,
                Event::Text(t) => out.push_str(&t.xml10_content()),
                Event::GeneralRef(r) => out.push_str(&format!("&{};", r.xml10_content())),
                _ => {}
            }
        }
        out
    }

    /// Element structure: start, end and empty tags by name, in order.
    /// Attributes are left out on purpose: prensa may rewrite image
    /// attributes (`src`, `width`), never the tree. Reading positions in sync
    /// services (KOReader's XPath) depend on this tree and on the text.
    fn tags(xhtml: &str) -> Vec<String> {
        let mut reader = Reader::from_str(xhtml);
        let mut out = Vec::new();
        loop {
            let tag = match reader.read_event().expect("valid XHTML") {
                Event::Eof => break,
                Event::Start(e) => format!("<{}>", e.name().as_ref()),
                Event::Empty(e) => format!("<{}/>", e.name().as_ref()),
                Event::End(e) => format!("</{}>", e.name().as_ref()),
                _ => continue,
            };
            out.push(tag);
        }
        out
    }

    /// Build a minimal EPUB in memory: chapters, optional embedded font + @font-face,
    /// optional obfuscation entry. Every chapter shows `images/figure.webp`,
    /// also used as a CSS background; the file exists only if `figure` is given.
    fn build_epub(
        chapters: &[Vec<String>],
        font: Option<&[u8]>,
        figure: Option<&[u8]>,
    ) -> anyhow::Result<Vec<u8>> {
        let mut zip = ZipWriter::new(Cursor::new(Vec::new()));
        let stored = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);
        zip.start_file("mimetype", stored)?;
        zip.write_all(b"application/epub+zip")?;
        zip.start_file("META-INF/container.xml", stored)?;
        zip.write_all(br#"<?xml version="1.0"?><container><rootfiles><rootfile full-path="OEBPS/content.opf"/></rootfiles></container>"#)?;

        let mut manifest =
            String::from(r#"<item href="style.css" id="css" media-type="text/css"/>"#);
        let mut css = String::from("p { margin: 0 }\nh1 { background: url(images/figure.webp) }\n");
        if let Some(bytes) = figure {
            manifest
                .push_str(r#"<item href="images/figure.webp" id="fig" media-type="image/webp"/>"#);
            zip.start_file("OEBPS/images/figure.webp", stored)?;
            zip.write_all(bytes)?;
        }
        if let Some(bytes) = font {
            manifest.push_str(r#"<item href="fonts/f.ttf" id="font" media-type="font/ttf"/>"#);
            css.push_str("@font-face { font-family: \"F\"; src: url(fonts/f.ttf); }\n");
            zip.start_file("OEBPS/fonts/f.ttf", stored)?;
            zip.write_all(bytes)?;
            zip.start_file("META-INF/encryption.xml", stored)?;
            zip.write_all(br#"<encryption><EncryptedData><CipherReference URI="OEBPS/fonts/f.ttf"/></EncryptedData></encryption>"#)?;
        }
        for (i, paragraphs) in chapters.iter().enumerate() {
            manifest.push_str(&format!(
                r#"<item href="ch{i}.xhtml" id="ch{i}" media-type="application/xhtml+xml"/>"#
            ));
            let body: String = paragraphs
                .iter()
                .map(|p| format!("<p>{}</p>", escape(p)))
                .collect();
            zip.start_file(format!("OEBPS/ch{i}.xhtml"), stored)?;
            zip.write_all(format!(r#"<?xml version="1.0"?><html xmlns="http://www.w3.org/1999/xhtml"><body><div><h1>Chapter {i}</h1>{body}<img src="images/figure.webp" alt=""/></div></body></html>"#).as_bytes())?;
        }
        zip.start_file("OEBPS/style.css", stored)?;
        zip.write_all(css.as_bytes())?;
        zip.start_file("OEBPS/content.opf", stored)?;
        zip.write_all(format!(r#"<?xml version="1.0"?><package version="3.0"><manifest>{manifest}</manifest></package>"#).as_bytes())?;
        Ok(zip.finish()?.into_inner())
    }

    fn read_all(epub: &[u8]) -> Vec<(String, Vec<u8>)> {
        let mut archive = ZipArchive::new(Cursor::new(epub)).unwrap();
        (0..archive.len())
            .map(|i| {
                let mut entry = archive.by_index(i).unwrap();
                let mut bytes = Vec::new();
                entry.read_to_end(&mut bytes).unwrap();
                (entry.name().unwrap().into_owned(), bytes)
            })
            .collect()
    }

    /// `f` applied to every chapter, in archive order.
    fn per_chapter<T>(entries: &[(String, Vec<u8>)], f: fn(&str) -> T) -> Vec<T> {
        entries
            .iter()
            .filter(|(name, _)| name.ends_with(".xhtml"))
            .map(|(_, bytes)| f(std::str::from_utf8(bytes).unwrap()))
            .collect()
    }

    /// Zip with one deflated entry per `(name, bytes)`, in order.
    fn zip_of(entries: &[(&str, &[u8])]) -> Vec<u8> {
        let mut zip = ZipWriter::new(Cursor::new(Vec::new()));
        for (name, bytes) in entries {
            zip.start_file(*name, SimpleFileOptions::default()).unwrap();
            zip.write_all(bytes).unwrap();
        }
        zip.finish().unwrap().into_inner()
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(64))]

        /// The core promise: whatever the book contains, the text a reader sees
        /// and the element structure around it are identical before and after,
        /// and the output is a valid EPUB container.
        #[test]
        fn visible_text_and_structure_are_unchanged(
            chapters in prop::collection::vec(prop::collection::vec("\\PC{0,60}", 0..5), 1..4),
            font in prop::option::of(prop::collection::vec(any::<u8>(), 1..64)),
            has_figure in any::<bool>(),
        ) {
            let figure = has_figure.then(|| webp(8, 8));
            let input = build_epub(&chapters, font.as_deref(), figure.as_deref()).unwrap();
            let mut output = Cursor::new(Vec::new());

            let report = optimize(Cursor::new(&input), &mut output, &Options::default()).unwrap();

            let before = read_all(&input);
            let after = read_all(output.get_ref());
            prop_assert_eq!(per_chapter(&before, visible_text), per_chapter(&after, visible_text));
            prop_assert_eq!(per_chapter(&before, tags), per_chapter(&after, tags));

            // Container rules and the fonts pass, checked on every case.
            prop_assert_eq!(after[0].0.as_str(), "mimetype");
            prop_assert!(after.iter().all(|(name, _)| !fonts::is_font(name)));
            prop_assert!(after.iter().all(|(name, _)| name != "META-INF/encryption.xml"));
            let opf = &after.iter().find(|(n, _)| n.ends_with(".opf")).unwrap().1;
            prop_assert!(!String::from_utf8_lossy(opf).contains("f.ttf"));
            prop_assert_eq!(report.fonts_removed, usize::from(font.is_some()));

            // The images pass: a WebP figure becomes PNG (it is lossless) and
            // every reference follows it.
            if has_figure {
                prop_assert_eq!(report.images_converted, 1);
                prop_assert!(after.iter().all(|(name, _)| !name.ends_with(".webp")));
                prop_assert!(after.iter().any(|(name, _)| name == "OEBPS/images/figure.png"));
                for (name, bytes) in &after {
                    let text = String::from_utf8_lossy(bytes);
                    if name.ends_with(".xhtml") || name.ends_with(".css") || name.ends_with(".opf") {
                        prop_assert!(!text.contains("figure.webp"), "{}", name);
                        prop_assert!(text.contains("images/figure.png"), "{}", name);
                    }
                }
                prop_assert!(String::from_utf8_lossy(opf).contains(r#"media-type="image/png""#));
            }
        }
    }

    /// A lossless WebP (the only kind the `image` crate encodes).
    fn webp(w: u32, h: u32) -> Vec<u8> {
        let img =
            image::RgbImage::from_fn(w, h, |x, y| image::Rgb([x as u8 * 20, y as u8 * 20, 90]));
        let mut out = Vec::new();
        image::DynamicImage::ImageRgb8(img)
            .write_to(&mut Cursor::new(&mut out), image::ImageFormat::WebP)
            .unwrap();
        out
    }

    #[test]
    fn structure_ignores_attributes_but_not_elements() {
        let original = r#"<p>A. B.<img src="a.png" width="900"/></p>"#;
        let new_attributes = r#"<p>A. B.<img src="a.jpg"/></p>"#;
        // Same text, one paragraph split in two: what a "split long
        // paragraphs" pass would do, and what this check exists to catch.
        let split = r#"<p>A. </p><p>B.<img src="a.png" width="900"/></p>"#;

        assert_eq!(tags(original), tags(new_attributes));
        assert_eq!(visible_text(original), visible_text(split));
        assert_ne!(tags(original), tags(split));
    }

    #[test]
    fn reports_progress_for_every_entry() {
        let input = build_epub(&[vec!["a".into()], vec!["b".into()]], None, None).unwrap();
        let mut seen = Vec::new();
        let mut output = Cursor::new(Vec::new());
        optimize_with_progress(Cursor::new(&input), &mut output, &Options::default(), |p| {
            seen.push((p.done, p.total, p.entry.to_owned()));
        })
        .unwrap();

        let names: Vec<String> = read_all(&input).into_iter().map(|(n, _)| n).collect();
        let expected: Vec<_> = names
            .iter()
            .enumerate()
            .map(|(i, n)| (i, names.len(), n.clone()))
            .collect();
        assert_eq!(seen, expected);
    }

    #[test]
    fn sizes_are_as_stored_in_the_epub() {
        // A deflated font that compresses well: stored size << real size.
        let input = zip_of(&[("f.ttf", &[0; 10_000])]);
        let stored = ZipArchive::new(Cursor::new(&input))
            .unwrap()
            .by_index_raw(0)
            .unwrap()
            .compressed_size();

        let mut output = Cursor::new(Vec::new());
        let report = optimize(Cursor::new(&input), &mut output, &Options::default()).unwrap();
        assert!(stored < 10_000);
        assert_eq!(report.font_bytes, stored);
    }

    #[test]
    fn rejects_non_zip_input() {
        let mut output = Cursor::new(Vec::new());
        let err = optimize(Cursor::new(b"not a zip"), &mut output, &Options::default());
        assert!(matches!(err, Err(Error::InvalidZip(_))));
    }

    fn utf16(text: &str) -> Vec<u8> {
        [0xFF, 0xFE]
            .into_iter()
            .chain(text.encode_utf16().flat_map(u16::to_le_bytes))
            .collect()
    }

    #[test]
    fn rejects_opf_and_encryption_that_are_not_utf8() {
        let opf = utf16(r#"<package><manifest><item href="f.ttf" id="f"/></manifest></package>"#);
        let enc = utf16(
            r#"<encryption><EncryptedData><CipherReference URI="OEBPS/f.ttf"/></EncryptedData></encryption>"#,
        );
        for (name, bytes) in [
            ("OEBPS/content.opf", &opf),
            ("META-INF/encryption.xml", &enc),
        ] {
            let input = zip_of(&[(name, bytes), ("OEBPS/f.ttf", b"font")]);
            let mut output = Cursor::new(Vec::new());
            match optimize(Cursor::new(input), &mut output, &Options::default()) {
                Err(Error::Malformed { entry, .. }) => assert_eq!(entry, name),
                other => panic!("{name}: expected Malformed, got {other:?}"),
            }
        }
    }

    #[test]
    fn unchanged_text_entries_are_copied_as_stored() {
        // No @font-face, no font items: nothing to rewrite. Stored (not
        // deflated) in the input, so a rewrite would show up as deflated.
        let css: &[u8] = b"p { margin: 0 }";
        let opf: &[u8] =
            br#"<package><manifest><item href="ch.xhtml" id="c"/></manifest></package>"#;
        let mut zip = ZipWriter::new(Cursor::new(Vec::new()));
        let stored = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);
        for (name, bytes) in [("OEBPS/style.css", css), ("OEBPS/content.opf", opf)] {
            zip.start_file(name, stored).unwrap();
            zip.write_all(bytes).unwrap();
        }
        let input = zip.finish().unwrap().into_inner();

        let mut output = Cursor::new(Vec::new());
        optimize(Cursor::new(input), &mut output, &Options::default()).unwrap();

        let mut archive = ZipArchive::new(Cursor::new(output.into_inner())).unwrap();
        for (name, bytes) in [("OEBPS/style.css", css), ("OEBPS/content.opf", opf)] {
            let mut entry = archive.by_name(name).unwrap();
            assert_eq!(entry.compression(), CompressionMethod::Stored, "{name}");
            let mut out = Vec::new();
            entry.read_to_end(&mut out).unwrap();
            assert_eq!(out, bytes, "{name}");
        }
    }

    #[test]
    fn copies_css_that_is_not_utf8() {
        // Latin-1 `é` in a comment: invalid UTF-8.
        let css = b"/* caf\xe9 */ @font-face { src: url(f.ttf) }";
        let input = zip_of(&[("OEBPS/style.css", css)]);
        let mut output = Cursor::new(Vec::new());
        optimize(Cursor::new(input), &mut output, &Options::default()).unwrap();
        let after = read_all(output.get_ref());
        let (_, bytes) = after.iter().find(|(n, _)| n == "OEBPS/style.css").unwrap();
        assert_eq!(bytes, css);
    }

    #[test]
    fn read_errors_are_io_errors() {
        /// A reader whose disk fails: not a malformed book.
        struct FailingReader;
        impl Read for FailingReader {
            fn read(&mut self, _: &mut [u8]) -> std::io::Result<usize> {
                Err(std::io::Error::other("disk failed"))
            }
        }
        impl Seek for FailingReader {
            fn seek(&mut self, _: std::io::SeekFrom) -> std::io::Result<u64> {
                Ok(100)
            }
        }

        let mut output = Cursor::new(Vec::new());
        let err = optimize(FailingReader, &mut output, &Options::default());
        assert!(matches!(err, Err(Error::Io(_))), "got {err:?}");
    }

    /// Zip with a single entry of `size` zero bytes.
    fn zip_with_zeros(name: &str, size: u64) -> Vec<u8> {
        zip_of(&[(name, &vec![0; size as usize])])
    }

    #[test]
    fn oversized_text_entry_fails_the_book() {
        let input = zip_with_zeros("OEBPS/style.css", MAX_ENTRY_SIZE + 1);
        let mut output = Cursor::new(Vec::new());
        match optimize(Cursor::new(input), &mut output, &Options::default()) {
            Err(Error::Malformed { entry, source }) => {
                assert_eq!(entry, "OEBPS/style.css");
                assert!(source.to_string().contains("larger than"), "{source}");
            }
            other => panic!("expected Malformed, got {other:?}"),
        }
    }

    #[test]
    fn oversized_image_is_copied_unchanged() {
        let input = zip_with_zeros("OEBPS/a.png", MAX_ENTRY_SIZE + 1);
        let mut output = Cursor::new(Vec::new());
        let report = optimize(Cursor::new(&input), &mut output, &Options::default()).unwrap();
        let [warning] = report.warnings.as_slice() else {
            panic!("expected one warning, got {:?}", report.warnings);
        };
        assert_eq!(warning.entry, "OEBPS/a.png");
        assert!(warning.message.contains("larger than"), "{warning}");
        assert_eq!(report.images_failed, 1);
        let after = read_all(output.get_ref());
        assert_eq!(after[1].0, "OEBPS/a.png");
        assert_eq!(after[1].1.len() as u64, MAX_ENTRY_SIZE + 1);
    }

    #[test]
    fn entry_at_the_limit_is_read() {
        let input = zip_with_zeros("OEBPS/style.css", MAX_ENTRY_SIZE);
        let mut archive = ZipArchive::new(Cursor::new(input)).unwrap();
        let bytes = read_entry(&mut archive, 0)
            .unwrap()
            .expect("within the limit");
        assert_eq!(bytes.len() as u64, MAX_ENTRY_SIZE);
    }

    #[test]
    fn removes_fonts_declared_only_by_media_type() {
        let files: [(&str, &[u8]); 4] = [
            (
                "OEBPS/content.opf",
                br#"<package><manifest><item href="fonts/f0001" id="f" media-type="font/ttf"/><item href="ch.xhtml" id="c" media-type="application/xhtml+xml"/></manifest></package>"#,
            ),
            ("OEBPS/fonts/f0001", b"font bytes"),
            ("OEBPS/ch.xhtml", b"<html/>"),
            (
                "META-INF/encryption.xml",
                br#"<encryption><EncryptedData><CipherReference URI="OEBPS/fonts/f0001"/></EncryptedData></encryption>"#,
            ),
        ];
        let input = zip_of(&files);

        let mut output = Cursor::new(Vec::new());
        let report = optimize(Cursor::new(input), &mut output, &Options::default()).unwrap();
        let names: Vec<String> = read_all(output.get_ref())
            .into_iter()
            .map(|(n, _)| n)
            .collect();
        assert_eq!(names, ["mimetype", "OEBPS/content.opf", "OEBPS/ch.xhtml"]);
        assert_eq!(report.fonts_removed, 1);
    }

    #[test]
    fn undecodable_image_is_a_warning_naming_the_entry() {
        let input = zip_of(&[("OEBPS/a.png", b"not a png")]);

        let mut output = Cursor::new(Vec::new());
        let report = optimize(Cursor::new(input), &mut output, &Options::default()).unwrap();
        let [warning] = report.warnings.as_slice() else {
            panic!("expected one warning, got {:?}", report.warnings);
        };
        assert_eq!(warning.entry, "OEBPS/a.png");
        assert!(warning.message.ends_with("; copied unchanged"), "{warning}");
        assert!(warning.to_string().starts_with("OEBPS/a.png: "));
        assert_eq!(
            warning.message.matches("Invalid PNG signature").count(),
            1,
            "{warning}"
        );
        assert_eq!(report.images_failed, 1);
        assert_eq!(report.images_kept, 0);
    }

    #[test]
    fn truncated_jpeg_warning_is_one_line() {
        let input = zip_of(&[("OEBPS/a.jpg", b"\xff\xd8\xff\xe0\x00\x10JFIF\x00")]);
        let mut output = Cursor::new(Vec::new());
        let report = optimize(Cursor::new(input), &mut output, &Options::default()).unwrap();
        let [warning] = report.warnings.as_slice() else {
            panic!("expected one warning, got {:?}", report.warnings);
        };
        assert!(!warning.message.contains('\n'), "{warning:?}");
        assert!(
            warning.message.ends_with("; copied unchanged"),
            "{warning:?}"
        );
    }

    #[test]
    fn broken_opf_names_the_entry() {
        let input = zip_of(&[("OEBPS/content.opf", b"<package><manifest></package>")]);

        let mut output = Cursor::new(Vec::new());
        match optimize(Cursor::new(input), &mut output, &Options::default()) {
            Err(Error::Malformed { entry, .. }) => assert_eq!(entry, "OEBPS/content.opf"),
            other => panic!("expected Malformed, got {other:?}"),
        }
    }

    #[test]
    fn converted_image_does_not_overwrite_an_existing_file() {
        // `fig.webp` is lossless, so it becomes PNG, but `fig.png` exists.
        let mut png = Vec::new();
        image::DynamicImage::ImageLuma8(image::GrayImage::new(2, 2))
            .write_to(&mut Cursor::new(&mut png), image::ImageFormat::Png)
            .unwrap();
        let figure = webp(4, 4);
        let input = zip_of(&[
            (
                "OEBPS/content.opf",
                br#"<package><manifest><item href="fig.png" id="a" media-type="image/png"/><item href="fig.webp" id="b" media-type="image/webp"/></manifest></package>"#,
            ),
            ("OEBPS/fig.png", &png),
            ("OEBPS/fig.webp", &figure),
            (
                "OEBPS/ch.xhtml",
                br#"<html><body><img src="fig.png"/><img src="fig.webp"/></body></html>"#,
            ),
        ]);

        let mut output = Cursor::new(Vec::new());
        let report = optimize(Cursor::new(input), &mut output, &Options::default()).unwrap();
        let after = read_all(output.get_ref());
        let get = |n: &str| {
            after
                .iter()
                .find(|(name, _)| name == n)
                .map(|(_, b)| String::from_utf8_lossy(b).into_owned())
        };

        assert_eq!(report.images_converted, 1);
        assert!(get("OEBPS/fig.png").is_some());
        assert!(get("OEBPS/fig-1.png").is_some());
        assert!(get("OEBPS/fig.webp").is_none());
        assert_eq!(
            get("OEBPS/ch.xhtml").unwrap(),
            r#"<html><body><img src="fig.png"/><img src="fig-1.png"/></body></html>"#
        );
        let opf = get("OEBPS/content.opf").unwrap();
        assert!(
            opf.contains(r#"<item href="fig.png" id="a" media-type="image/png"/>"#),
            "{opf}"
        );
        assert!(
            opf.contains(r#"<item href="fig-1.png" id="b" media-type="image/png"/>"#),
            "{opf}"
        );
    }

    #[test]
    fn undecodable_webp_keeps_its_name_and_references() {
        let xhtml: &[u8] = br#"<html><body><img src="a.webp"/></body></html>"#;
        let input = zip_of(&[("OEBPS/a.webp", b"not a webp"), ("OEBPS/ch.xhtml", xhtml)]);
        let mut output = Cursor::new(Vec::new());
        let report = optimize(Cursor::new(input), &mut output, &Options::default()).unwrap();
        let after = read_all(output.get_ref());
        assert_eq!(report.images_failed, 1);
        assert_eq!(report.images_converted, 0);
        assert!(after.iter().any(|(n, _)| n == "OEBPS/a.webp"));
        assert!(
            after
                .iter()
                .any(|(n, b)| n == "OEBPS/ch.xhtml" && b == xhtml)
        );
    }

    #[test]
    fn broken_chapter_is_copied_with_a_warning() {
        // An unterminated tag: not even attributes can be read.
        let broken: &[u8] = br#"<html><body><p><img src="a.webp"</p></body></html>"#;
        let figure = webp(4, 4);
        let input = zip_of(&[("OEBPS/a.webp", &figure), ("OEBPS/ch.xhtml", broken)]);
        let mut output = Cursor::new(Vec::new());
        let report = optimize(Cursor::new(input), &mut output, &Options::default()).unwrap();
        let after = read_all(output.get_ref());

        assert!(
            after
                .iter()
                .any(|(n, b)| n == "OEBPS/ch.xhtml" && b == broken)
        );
        let [warning] = report.warnings.as_slice() else {
            panic!("expected one warning, got {:?}", report.warnings);
        };
        assert_eq!(warning.entry, "OEBPS/ch.xhtml");
        assert!(
            warning
                .message
                .ends_with("image references in it not updated; copied unchanged"),
            "{warning}"
        );
    }

    #[test]
    fn wide_image_is_turned_and_loses_its_sizes() {
        let mut png = Vec::new();
        image::DynamicImage::ImageLuma8(image::GrayImage::new(2000, 800))
            .write_to(&mut Cursor::new(&mut png), image::ImageFormat::Png)
            .unwrap();
        let chapter = br#"<html><body><p>Figure 1</p><img src="../img/wide.png" width="2000" height="800" alt="Figure 1"/></body></html>"#;
        let input = zip_of(&[
            ("OEBPS/img/wide.png", &png),
            ("OEBPS/text/ch.xhtml", chapter),
        ]);

        let cw = Options::builder()
            .rotate(Rotation::Clockwise)
            .build()
            .unwrap();
        let mut output = Cursor::new(Vec::new());
        let report = optimize(Cursor::new(&input), &mut output, &cw).unwrap();
        let after = read_all(output.get_ref());
        let get = |n: &str| &after.iter().find(|(name, _)| name == n).unwrap().1;

        assert_eq!(report.images_rotated, 1);
        let img = image::load_from_memory(get("OEBPS/img/wide.png")).unwrap();
        assert!(img.height() > img.width(), "turned to portrait");
        assert_eq!(
            String::from_utf8_lossy(get("OEBPS/text/ch.xhtml")),
            r#"<html><body><p>Figure 1</p><img src="../img/wide.png" alt="Figure 1"/></body></html>"#
        );

        // Off (the default): the chapter stays as it was.
        let mut output = Cursor::new(Vec::new());
        let report = optimize(Cursor::new(&input), &mut output, &Options::default()).unwrap();
        assert_eq!(report.images_rotated, 0);
        let after = read_all(output.get_ref());
        assert!(
            after
                .iter()
                .any(|(n, b)| n == "OEBPS/text/ch.xhtml" && b == chapter)
        );
    }

    #[test]
    fn embedded_image_moves_to_its_own_entry() {
        use base64::Engine;
        let mut png = Vec::new();
        image::DynamicImage::ImageLuma8(image::GrayImage::new(900, 300))
            .write_to(&mut Cursor::new(&mut png), image::ImageFormat::Png)
            .unwrap();
        let uri = format!(
            "data:image/png;base64,{}",
            base64::engine::general_purpose::STANDARD.encode(&png)
        );
        let chapter = format!(
            r#"<html><body><p>A</p><img src="{uri}" alt="a"/><p>B</p><img src="{uri}"/></body></html>"#
        );
        let other = format!(r#"<html><body><img src="{uri}"/></body></html>"#);
        let opf: &[u8] = br#"<package><manifest><item id="c" href="text/ch.xhtml" media-type="application/xhtml+xml"/></manifest></package>"#;
        let input = zip_of(&[
            ("OEBPS/content.opf", opf),
            ("OEBPS/text/ch.xhtml", chapter.as_bytes()),
            ("OEBPS/other/end.xhtml", other.as_bytes()),
        ]);

        let mut output = Cursor::new(Vec::new());
        let report = optimize(Cursor::new(&input), &mut output, &Options::default()).unwrap();
        let after = read_all(output.get_ref());
        let text = |n: &str| {
            String::from_utf8_lossy(&after.iter().find(|(name, _)| name == n).unwrap().1)
                .into_owned()
        };

        assert_eq!(report.images_extracted, 1);
        assert_eq!(report.images_changed, 1);
        let new_chapter = text("OEBPS/text/ch.xhtml");
        assert_eq!(
            new_chapter,
            r#"<html><body><p>A</p><img src="ch-image-1.png" alt="a"/><p>B</p><img src="ch-image-1.png"/></body></html>"#
        );
        assert_eq!(visible_text(&chapter), visible_text(&new_chapter));
        assert_eq!(tags(&chapter), tags(&new_chapter));
        // The same image in another chapter: the same file.
        assert_eq!(
            text("OEBPS/other/end.xhtml"),
            r#"<html><body><img src="../text/ch-image-1.png"/></body></html>"#
        );
        let image = &after
            .iter()
            .find(|(n, _)| n == "OEBPS/text/ch-image-1.png")
            .unwrap()
            .1;
        assert_eq!(image::load_from_memory(image).unwrap().width(), 440);
        assert!(text("OEBPS/content.opf").contains(
            r#"<item id="prensa-ch-image-1-png" href="text/ch-image-1.png" media-type="image/png"/></manifest>"#
        ));
    }
}