diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-06-26 13:06:37 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-06-26 13:06:37 +0200 |
| commit | 285c2f617b74e182be69decea46bbd0afdb0f604 (patch) | |
| tree | 41bdb5d19bc80c165df6e55e829051f0812f7c3d /src/layout | |
| parent | 63cf36149635156013f0324b660bf4d362beb87f (diff) | |
Cleanse library
- Remove doc-comments for Typst functions from library
- Reduce number of library source files
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/image.rs | 65 | ||||
| -rw-r--r-- | src/layout/mod.rs | 2 |
2 files changed, 67 insertions, 0 deletions
diff --git a/src/layout/image.rs b/src/layout/image.rs new file mode 100644 index 00000000..9ba8cd82 --- /dev/null +++ b/src/layout/image.rs @@ -0,0 +1,65 @@ +use super::*; +use crate::image::ImageId; + +use ::image::GenericImageView; + +/// An image node. +#[derive(Debug, Copy, Clone, PartialEq, Hash)] +pub struct ImageNode { + /// The id of the image file. + pub id: ImageId, + /// The fixed width, if any. + pub width: Option<Linear>, + /// The fixed height, if any. + pub height: Option<Linear>, +} + +impl Layout for ImageNode { + fn layout( + &self, + ctx: &mut LayoutContext, + regions: &Regions, + ) -> Vec<Constrained<Rc<Frame>>> { + let Regions { current, base, .. } = regions; + let mut constraints = Constraints::new(regions.expand); + constraints.set_base_using_linears(Spec::new(self.width, self.height), regions); + + let width = self.width.map(|w| w.resolve(base.width)); + let height = self.height.map(|w| w.resolve(base.height)); + + let dimensions = ctx.cache.image.get(self.id).buf.dimensions(); + let pixel_width = dimensions.0 as f64; + let pixel_height = dimensions.1 as f64; + let pixel_ratio = pixel_width / pixel_height; + + let size = match (width, height) { + (Some(width), Some(height)) => Size::new(width, height), + (Some(width), None) => Size::new(width, width / pixel_ratio), + (None, Some(height)) => Size::new(height * pixel_ratio, height), + (None, None) => { + constraints.exact = current.to_spec().map(Some); + + let ratio = current.width / current.height; + if ratio < pixel_ratio && current.width.is_finite() { + Size::new(current.width, current.width / pixel_ratio) + } else if current.height.is_finite() { + // TODO: Fix issue with line spacing. + Size::new(current.height * pixel_ratio, current.height) + } else { + // Totally unbounded region, we have to make up something. + Size::new(Length::pt(pixel_width), Length::pt(pixel_height)) + } + } + }; + + let mut frame = Frame::new(size, size.height); + frame.push(Point::zero(), Element::Image(self.id, size)); + vec![frame.constrain(constraints)] + } +} + +impl From<ImageNode> for AnyNode { + fn from(image: ImageNode) -> Self { + Self::new(image) + } +} diff --git a/src/layout/mod.rs b/src/layout/mod.rs index f1ae3e2a..10c30f41 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -4,12 +4,14 @@ mod background; mod fixed; mod frame; mod grid; +mod image; mod incremental; mod pad; mod par; mod shaping; mod stack; +pub use self::image::*; pub use background::*; pub use fixed::*; pub use frame::*; |
