diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-05-28 12:44:44 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-05-28 12:46:43 +0200 |
| commit | 0bfee5b7772338fd39bbf708d3e31ea7bcec859b (patch) | |
| tree | 5f76c7d0529d6c089e8e3383356692dfce09cffb /src/library/image.rs | |
| parent | eabf28f08187bd9a10bbadbbaf9617e2bc1949aa (diff) | |
Refactored loading and cache architecture
Diffstat (limited to 'src/library/image.rs')
| -rw-r--r-- | src/library/image.rs | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/src/library/image.rs b/src/library/image.rs index b73c26a9..cd6a97d1 100644 --- a/src/library/image.rs +++ b/src/library/image.rs @@ -1,7 +1,7 @@ use ::image::GenericImageView; use super::*; -use crate::env::ImageId; +use crate::image::ImageId; use crate::layout::{AnyNode, Element, Frame, Layout, LayoutContext, Regions}; /// `image`: An image. @@ -18,21 +18,26 @@ pub fn image(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { let width = args.eat_named(ctx, "width"); let height = args.eat_named(ctx, "height"); + let mut node = None; + if let Some(path) = &path { + if let Some(id) = ctx.cache.image.load(ctx.loader, &path.v) { + let img = ctx.cache.image.get(id); + let dimensions = img.buf.dimensions(); + node = Some(ImageNode { id, dimensions, width, height }); + } else { + ctx.diag(error!(path.span, "failed to load image")); + } + } + Value::template("image", move |ctx| { - if let Some(path) = &path { - if let Some(id) = ctx.env.load_image(&path.v) { - let img = ctx.env.image(id); - let dimensions = img.buf.dimensions(); - ctx.push(ImageNode { id, dimensions, width, height }); - } else { - ctx.diag(error!(path.span, "failed to load image")); - } + if let Some(node) = node { + ctx.push(node); } }) } /// An image node. -#[derive(Debug, Clone, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, Hash)] struct ImageNode { /// The id of the image file. id: ImageId, |
