mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-02 19:17:03 +00:00
fix(epub): embed fonts in the Rust packager instead of fetching in the webview
This commit is contained in:
1 parent
cb506af374
commit
f8ddde5e91
2 files changed
+14
-22
No files matched your search
+14
-6
@@ -5,6 +5,9 @@ use std::io::Write;
|
|||||||
use zip::write::SimpleFileOptions;
|
use zip::write::SimpleFileOptions;
|
||||||
use zip::{CompressionMethod, ZipWriter};
|
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)]
|
#[derive(Deserialize)]
|
||||||
pub struct EpubFile {
|
pub struct EpubFile {
|
||||||
path: String,
|
path: String,
|
||||||
@@ -17,10 +20,8 @@ fn build(files: &[EpubFile]) -> Result<Vec<u8>, String> {
|
|||||||
let mut zip = ZipWriter::new(buffer);
|
let mut zip = ZipWriter::new(buffer);
|
||||||
|
|
||||||
let stored = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);
|
let stored = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);
|
||||||
zip.start_file("mimetype", stored)
|
zip.start_file("mimetype", stored).map_err(|e| e.to_string())?;
|
||||||
.map_err(|e| e.to_string())?;
|
zip.write_all(b"application/epub+zip").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);
|
let deflated = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);
|
||||||
for file in files {
|
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))?,
|
.map_err(|e| format!("failed to decode \"{}\": {e}", file.path))?,
|
||||||
_ => file.data.as_bytes().to_vec(),
|
_ => file.data.as_bytes().to_vec(),
|
||||||
};
|
};
|
||||||
zip.start_file(&file.path, deflated)
|
zip.start_file(&file.path, deflated).map_err(|e| e.to_string())?;
|
||||||
.map_err(|e| e.to_string())?;
|
|
||||||
zip.write_all(&bytes).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())
|
Ok(zip.finish().map_err(|e| e.to_string())?.into_inner())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -389,24 +389,8 @@ export function bookToEpub(book: Book): EpubFile[] {
|
|||||||
return files;
|
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> {
|
export async function buildEpub(book: Book): Promise<Uint8Array> {
|
||||||
const files = bookToEpub(book);
|
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 });
|
const buf = await invoke<ArrayBuffer>("package_epub", { files });
|
||||||
return new Uint8Array(buf);
|
return new Uint8Array(buf);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user