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())
}
-16
View File
@@ -389,24 +389,8 @@ export function bookToEpub(book: Book): EpubFile[] {
return files;
}
function toBase64(bytes: Uint8Array): string {
let binary = "";
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
return btoa(binary);
}
async function fontFile(url: string, path: string): Promise<EpubFile> {
const response = await fetch(url);
const buffer = await response.arrayBuffer();
return { path, data: toBase64(new Uint8Array(buffer)), encoding: "base64" };
}
export async function buildEpub(book: Book): Promise<Uint8Array> {
const files = bookToEpub(book);
files.push(
await fontFile("/fonts/Literata-VF.ttf", "OEBPS/fonts/Literata-VF.ttf"),
await fontFile("/fonts/Literata-Italic-VF.ttf", "OEBPS/fonts/Literata-Italic-VF.ttf"),
);
const buf = await invoke<ArrayBuffer>("package_epub", { files });
return new Uint8Array(buf);
}