fix(epub): embed fonts in the Rust packager instead of fetching in the webview

This commit is contained in:
pj committed 2026-06-20 14:30:07 -04:00
1 parent cb506af374
commit f8ddde5e91
2 files changed
+14 -22

No files matched your search

+14 -6
View File
@@ -5,6 +5,9 @@ use std::io::Write;
use zip::write::SimpleFileOptions;
use zip::{CompressionMethod, ZipWriter};
static LITERATA: &[u8] = include_bytes!("../../public/fonts/Literata-VF.ttf");
static LITERATA_ITALIC: &[u8] = include_bytes!("../../public/fonts/Literata-Italic-VF.ttf");
#[derive(Deserialize)]
pub struct EpubFile {
path: String,
@@ -17,10 +20,8 @@ fn build(files: &[EpubFile]) -> Result<Vec<u8>, String> {
let mut zip = ZipWriter::new(buffer);
let stored = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);
zip.start_file("mimetype", stored)
.map_err(|e| e.to_string())?;
zip.write_all(b"application/epub+zip")
.map_err(|e| e.to_string())?;
zip.start_file("mimetype", stored).map_err(|e| e.to_string())?;
zip.write_all(b"application/epub+zip").map_err(|e| e.to_string())?;
let deflated = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);
for file in files {
@@ -33,11 +34,18 @@ fn build(files: &[EpubFile]) -> Result<Vec<u8>, String> {
.map_err(|e| format!("failed to decode \"{}\": {e}", file.path))?,
_ => file.data.as_bytes().to_vec(),
};
zip.start_file(&file.path, deflated)
.map_err(|e| e.to_string())?;
zip.start_file(&file.path, deflated).map_err(|e| e.to_string())?;
zip.write_all(&bytes).map_err(|e| e.to_string())?;
}
for (path, bytes) in [
("OEBPS/fonts/Literata-VF.ttf", LITERATA),
("OEBPS/fonts/Literata-Italic-VF.ttf", LITERATA_ITALIC),
] {
zip.start_file(path, deflated).map_err(|e| e.to_string())?;
zip.write_all(bytes).map_err(|e| e.to_string())?;
}
Ok(zip.finish().map_err(|e| e.to_string())?.into_inner())
}