-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show image loading errors to the user (#8)
* Move image loading to its own file * Move image fetching to own file * Show image loading errors to the user * Fix typo * Fix a couple more typos
- Loading branch information
Showing
4 changed files
with
169 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#[cfg(not(feature = "fetch"))] | ||
pub fn get_image_data(uri: &str, on_done: impl 'static + Send + FnOnce(Result<Vec<u8>, String>)) { | ||
get_image_data_from_file(uri, on_done) | ||
} | ||
|
||
#[cfg(feature = "fetch")] | ||
pub fn get_image_data(uri: &str, on_done: impl 'static + Send + FnOnce(Result<Vec<u8>, String>)) { | ||
let url = url::Url::parse(uri); | ||
if url.is_ok() { | ||
let uri = uri.to_owned(); | ||
ehttp::fetch(ehttp::Request::get(&uri), move |result| match result { | ||
Ok(response) => { | ||
on_done(Ok(response.bytes)); | ||
} | ||
Err(err) => { | ||
on_done(Err(err)); | ||
} | ||
}); | ||
} else { | ||
get_image_data_from_file(uri, on_done) | ||
} | ||
} | ||
|
||
fn get_image_data_from_file( | ||
path: &str, | ||
on_done: impl 'static + Send + FnOnce(Result<Vec<u8>, String>), | ||
) { | ||
on_done(std::fs::read(path).map_err(|err| err.to_string())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use egui::ColorImage; | ||
|
||
pub fn load_image(url: &str, data: &[u8]) -> Result<ColorImage, String> { | ||
if url.ends_with(".svg") { | ||
try_render_svg(data) | ||
} else { | ||
try_load_image(data).map_err(|err| err.to_string()) | ||
} | ||
} | ||
|
||
fn try_load_image(data: &[u8]) -> image::ImageResult<ColorImage> { | ||
let image = image::load_from_memory(data)?; | ||
let image_buffer = image.to_rgba8(); | ||
let size = [image.width() as usize, image.height() as usize]; | ||
let pixels = image_buffer.as_flat_samples(); | ||
|
||
Ok(ColorImage::from_rgba_unmultiplied(size, pixels.as_slice())) | ||
} | ||
|
||
#[cfg(not(feature = "svg"))] | ||
fn try_render_svg(_data: &[u8]) -> Result<ColorImage, String> { | ||
Err("SVG support not enabled".to_owned()) | ||
} | ||
|
||
#[cfg(feature = "svg")] | ||
fn try_render_svg(data: &[u8]) -> Result<ColorImage, String> { | ||
use resvg::tiny_skia; | ||
use usvg::{TreeParsing, TreeTextToPath}; | ||
|
||
let tree = { | ||
let options = usvg::Options::default(); | ||
let mut fontdb = usvg::fontdb::Database::new(); | ||
fontdb.load_system_fonts(); | ||
|
||
let mut tree = usvg::Tree::from_data(data, &options).map_err(|err| err.to_string())?; | ||
tree.convert_text(&fontdb); | ||
resvg::Tree::from_usvg(&tree) | ||
}; | ||
|
||
let size = tree.size.to_int_size(); | ||
|
||
let (w, h) = (size.width(), size.height()); | ||
let mut pixmap = tiny_skia::Pixmap::new(w, h) | ||
.ok_or_else(|| format!("Failed to create {w}x{h} SVG image"))?; | ||
tree.render(tiny_skia::Transform::default(), &mut pixmap.as_mut()); | ||
|
||
Ok(ColorImage::from_rgba_unmultiplied( | ||
[pixmap.width() as usize, pixmap.height() as usize], | ||
&pixmap.take(), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters