From d0a5157b2f1ae256aa950c982c1d664458c41348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Sat, 16 Feb 2019 03:43:59 +0100 Subject: [PATCH] Added ImageSource and ImageReloadError stub API --- azul/src/app_resources.rs | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/azul/src/app_resources.rs b/azul/src/app_resources.rs index 73b2689c1..d6b679266 100644 --- a/azul/src/app_resources.rs +++ b/azul/src/app_resources.rs @@ -1,6 +1,9 @@ use std::{ + fmt, rc::Rc, cell::RefCell, + path::PathBuf, + io::Error as IoError, collections::hash_map::Entry::*, }; use webrender::api::{FontKey, FontInstanceKey}; @@ -23,6 +26,63 @@ use { pub type CssImageId = String; +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +pub enum ImageSource { + /// The image is embedded inside the binary file + Embedded(&'static [u8]), + File(PathBuf), +} + +#[derive(Debug)] +pub enum ImageReloadError { + Io(IoError, PathBuf), +} + +impl Clone for ImageReloadError { + fn clone(&self) -> Self { + use self::ImageReloadError::*; + match self { + Io(err, path) => Io(IoError::new(err.kind(), "Io Error"), path.clone()), + } + } +} + +impl fmt::Display for ImageReloadError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::ImageReloadError::*; + match self { + Io(err, path_buf) => + write!( + f, "Could not load \"{}\" - IO error: {}", + path_buf.as_path().to_string_lossy(), err + ), + } + } +} + +impl ImageSource { + + /// Creates an image source from a `&static [u8]`. + pub fn new_from_static(bytes: &'static [u8]) -> Self { + ImageSource::Embedded(bytes) + } + + /// Creates an image source from a file + pub fn new_from_file>(file_path: I) -> Self { + ImageSource::File(file_path.into()) + } + + /// Returns the bytes of the font + pub(crate) fn get_bytes(&self) -> Result, ImageReloadError> { + use std::fs; + use self::ImageSource::*; + match self { + Embedded(bytes) => Ok(bytes.to_vec()), + File(file_path) => fs::read(file_path).map_err(|e| ImageReloadError::Io(e, file_path.clone())), + } + } +} + /// Stores the resources for the application, souch as fonts, images and cached /// texts, also clipboard strings ///