diff --git a/crates/core/src/document/mod.rs b/crates/core/src/document/mod.rs index 1ad16d6c..47071fd3 100644 --- a/crates/core/src/document/mod.rs +++ b/crates/core/src/document/mod.rs @@ -240,7 +240,10 @@ pub fn open>(path: P) -> Option> { }) }, _ => { - PdfOpener::new().and_then(|o| { + PdfOpener::new().and_then(|mut o| { + if matches!(k.as_ref(), "mobi" | "fb2" | "xps" | "txt") { + o.load_user_stylesheet(); + } o.open(path) .map(|d| Box::new(d) as Box) }) diff --git a/crates/core/src/document/pdf.rs b/crates/core/src/document/pdf.rs index 911bac5b..90798c3e 100644 --- a/crates/core/src/document/pdf.rs +++ b/crates/core/src/document/pdf.rs @@ -1,15 +1,14 @@ use super::mupdf_sys::*; +use std::fs; use std::ptr; use std::slice; use std::char; use std::rc::Rc; use std::path::Path; -use std::io::Read; -use std::fs::File; +use std::io::ErrorKind; use std::ffi::{CString, CStr}; use std::os::unix::ffi::OsStrExt; -use anyhow::Error; use super::{Document, Location, TextLocation, BoundedText, TocEntry}; use super::{chapter, chapter_relative}; use crate::metadata::TextAlign; @@ -17,6 +16,8 @@ use crate::geom::{Boundary, CycleDir}; use crate::unit::pt_to_px; use crate::framebuffer::Pixmap; +const USER_STYLESHEET: &str = "css/html-user.css"; + impl Into for FzRect { fn into(self) -> Boundary { Boundary { @@ -92,16 +93,12 @@ impl PdfOpener { } } - pub fn set_user_css>(&mut self, path: P) -> Result<(), Error> { - let mut file = File::open(path)?; - let mut buf = Vec::new(); - file.read_to_end(&mut buf)?; - let buf = CString::new(buf)?; - unsafe { - // The CSS will only be parsed when an HTML or EPUB document is opened - fz_set_user_css((self.0).0, buf.as_ptr()); + pub fn load_user_stylesheet(&mut self) { + if let Ok(content) = fs::read_to_string(USER_STYLESHEET) + .and_then(|s| CString::new(s).map_err(Into::into)) + .map_err(|e| if e.kind() != ErrorKind::NotFound { eprintln!("{:#}", e) }) { + unsafe { fz_set_user_css((self.0).0, content.as_ptr()) } } - Ok(()) } }