summaryrefslogtreecommitdiff
path: root/src/library/image.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-02-28 15:50:48 +0100
committerLaurenz <laurmaedje@gmail.com>2022-02-28 23:54:34 +0100
commit3ca5b238238e1128aa7bbfbd5db9e632045d8600 (patch)
tree2471f4b340a15695b7f4d518c0b39fabaea676c4 /src/library/image.rs
parentb63c21c91d99a1554a019dc275f955d3e6a34271 (diff)
Reorganize library
Diffstat (limited to 'src/library/image.rs')
-rw-r--r--src/library/image.rs118
1 files changed, 0 insertions, 118 deletions
diff --git a/src/library/image.rs b/src/library/image.rs
deleted file mode 100644
index c1220734..00000000
--- a/src/library/image.rs
+++ /dev/null
@@ -1,118 +0,0 @@
-//! Raster and vector graphics.
-
-use super::prelude::*;
-use super::TextNode;
-use crate::diag::Error;
-use crate::image::ImageId;
-
-/// Show a raster or vector graphic.
-#[derive(Debug, Hash)]
-pub struct ImageNode(pub ImageId);
-
-#[class]
-impl ImageNode {
- /// How the image should adjust itself to a given area.
- pub const FIT: ImageFit = ImageFit::Cover;
-
- fn construct(ctx: &mut Context, args: &mut Args) -> TypResult<Template> {
- let path = args.expect::<Spanned<EcoString>>("path to image file")?;
- let full = ctx.resolve(&path.v);
- let id = ctx.images.load(&full).map_err(|err| {
- Error::boxed(path.span, match err.kind() {
- std::io::ErrorKind::NotFound => "file not found".into(),
- _ => format!("failed to load image ({})", err),
- })
- })?;
-
- let width = args.named("width")?;
- let height = args.named("height")?;
-
- Ok(Template::inline(
- ImageNode(id).pack().sized(Spec::new(width, height)),
- ))
- }
-}
-
-impl Layout for ImageNode {
- fn layout(
- &self,
- ctx: &mut Context,
- regions: &Regions,
- styles: StyleChain,
- ) -> TypResult<Vec<Arc<Frame>>> {
- let img = ctx.images.get(self.0);
- let pxw = img.width() as f64;
- let pxh = img.height() as f64;
- let px_ratio = pxw / pxh;
-
- // Find out whether the image is wider or taller than the target size.
- let &Regions { first, expand, .. } = regions;
- let region_ratio = first.x / first.y;
- let wide = px_ratio > region_ratio;
-
- // The space into which the image will be placed according to its fit.
- let target = if expand.x && expand.y {
- first
- } else if expand.x || (!expand.y && wide && first.x.is_finite()) {
- Size::new(first.x, first.y.min(first.x.safe_div(px_ratio)))
- } else if first.y.is_finite() {
- Size::new(first.x.min(first.y * px_ratio), first.y)
- } else {
- Size::new(Length::pt(pxw), Length::pt(pxh))
- };
-
- // Compute the actual size of the fitted image.
- let fit = styles.get(Self::FIT);
- let fitted = match fit {
- ImageFit::Cover | ImageFit::Contain => {
- if wide == (fit == ImageFit::Contain) {
- Size::new(target.x, target.x / px_ratio)
- } else {
- Size::new(target.y * px_ratio, target.y)
- }
- }
- ImageFit::Stretch => target,
- };
-
- // First, place the image in a frame of exactly its size and then resize
- // the frame to the target size, center aligning the image in the
- // process.
- let mut frame = Frame::new(fitted);
- frame.push(Point::zero(), Element::Image(self.0, fitted));
- frame.resize(target, Align::CENTER_HORIZON);
-
- // Create a clipping group if only part of the image should be visible.
- if fit == ImageFit::Cover && !target.fits(fitted) {
- frame.clip();
- }
-
- // Apply link if it exists.
- if let Some(url) = styles.get_ref(TextNode::LINK) {
- frame.link(url);
- }
-
- Ok(vec![Arc::new(frame)])
- }
-}
-
-/// How an image should adjust itself to a given area.
-#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
-pub enum ImageFit {
- /// The image should completely cover the area.
- Cover,
- /// The image should be fully contained in the area.
- Contain,
- /// The image should be stretched so that it exactly fills the area.
- Stretch,
-}
-
-castable! {
- ImageFit,
- Expected: "string",
- Value::Str(string) => match string.as_str() {
- "cover" => Self::Cover,
- "contain" => Self::Contain,
- "stretch" => Self::Stretch,
- _ => Err(r#"expected "cover", "contain" or "stretch""#)?,
- },
-}