Skip to content

Commit 1b9d4fa

Browse files
committed
fix: don't error when no font is provided
fixes #25
1 parent 771a11f commit 1b9d4fa

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

crates/c2pdf/src/bin/c2pdf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ fn main() {
108108
Mm(args.margin_right),
109109
);
110110
let mut doc = PdfDocument::new(&args.name);
111-
let (font_bytes, used_bundled) = load_font(args.font);
112-
if used_bundled {
111+
let (font_bytes, font_loaded) = load_font(args.font);
112+
if let c2pdf::font_loader::FontLoaded::FailProvided = font_loaded {
113113
error!("Unable to load provided font")
114114
}
115115
let font_bytes = &*font_bytes;

crates/c2pdf/src/lib/font_loader.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,30 @@ fn bundled_font_bytes() -> Arc<Vec<u8>> {
4141
fn is_path(s: &str) -> bool {
4242
PathBuf::from(s).extension().is_some() || s.len() > 31 || s.starts_with('.')
4343
}
44+
/// Details on how the requested font was loaded
45+
pub enum FontLoaded {
46+
/// Successfully loaded provided font
47+
SuccessProvided,
48+
/// Failed loading provided font
49+
FailProvided,
50+
/// No font provided, so using font embedded in binary
51+
NoneProvided,
52+
}
4453
/// Loads a given font - falling back to the bundled font if loading from the system, or from the given path fails
45-
pub fn load_font(name_or_path: Option<String>) -> (Arc<Vec<u8>>, bool) {
46-
let data = name_or_path.and_then(|name_or_path| {
47-
if is_path(&name_or_path) {
48-
load_font_path(name_or_path)
54+
pub fn load_font(name_or_path: Option<String>) -> (Arc<Vec<u8>>, FontLoaded) {
55+
if let Some(name_or_path) = name_or_path {
56+
if let Ok(data) = {
57+
if is_path(&name_or_path) {
58+
load_font_path(name_or_path)
59+
} else {
60+
load_font_system(name_or_path)
61+
}
62+
} {
63+
(data, FontLoaded::SuccessProvided)
4964
} else {
50-
load_font_system(name_or_path)
65+
(bundled_font_bytes(), FontLoaded::FailProvided)
5166
}
52-
.ok()
53-
});
54-
55-
match data {
56-
Some(d) => (d, false),
57-
None => (bundled_font_bytes(), true),
67+
} else {
68+
(bundled_font_bytes(), FontLoaded::NoneProvided)
5869
}
5970
}

0 commit comments

Comments
 (0)