diff options
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/graphics/image.rs | 27 | ||||
| -rw-r--r-- | src/library/text/par.rs | 23 |
2 files changed, 19 insertions, 31 deletions
diff --git a/src/library/graphics/image.rs b/src/library/graphics/image.rs index feb37ae6..784afa01 100644 --- a/src/library/graphics/image.rs +++ b/src/library/graphics/image.rs @@ -1,10 +1,12 @@ -use crate::image::ImageId; +use std::ffi::OsStr; + +use crate::image::Image; use crate::library::prelude::*; use crate::library::text::TextNode; /// Show a raster or vector graphic. #[derive(Debug, Hash)] -pub struct ImageNode(pub ImageId); +pub struct ImageNode(pub Image); #[node] impl ImageNode { @@ -16,12 +18,20 @@ impl ImageNode { args.expect::<Spanned<EcoString>>("path to image file")?; let full = vm.locate(&path).at(span)?; - let id = vm.ctx.images.load(&full).at(span)?; + let ext = full.extension().and_then(OsStr::to_str).unwrap_or_default(); + let image = vm + .ctx + .loader + .load(&full) + .and_then(|buffer| Image::new(buffer, ext)) + .map_err(|err| failed_to_load("image", &full, err)) + .at(span)?; + let width = args.named("width")?; let height = args.named("height")?; Ok(Content::inline( - ImageNode(id).pack().sized(Spec::new(width, height)), + ImageNode(image).pack().sized(Spec::new(width, height)), )) } } @@ -29,13 +39,12 @@ impl ImageNode { impl Layout for ImageNode { fn layout( &self, - ctx: &mut Context, + _: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Frame>> { - let img = ctx.images.get(self.0); - let pxw = img.width() as f64; - let pxh = img.height() as f64; + let pxw = self.0.width() as f64; + let pxh = self.0.height() as f64; let px_ratio = pxw / pxh; // Find out whether the image is wider or taller than the target size. @@ -71,7 +80,7 @@ impl Layout for ImageNode { // 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.push(Point::zero(), Element::Image(self.0.clone(), fitted)); frame.resize(target, Align::CENTER_HORIZON); // Create a clipping group if only part of the image should be visible. diff --git a/src/library/text/par.rs b/src/library/text/par.rs index 6c5c9765..168aca26 100644 --- a/src/library/text/par.rs +++ b/src/library/text/par.rs @@ -25,8 +25,6 @@ pub enum ParChild { Spacing(Spacing), /// An arbitrary inline-level node. Node(LayoutNode), - /// A pin identified by index. - Pin(usize), } #[node] @@ -101,7 +99,6 @@ impl Debug for ParChild { Self::Quote { double } => write!(f, "Quote({double})"), Self::Spacing(kind) => write!(f, "{:?}", kind), Self::Node(node) => node.fmt(f), - Self::Pin(idx) => write!(f, "Pin({idx})"), } } } @@ -197,7 +194,6 @@ type Range = std::ops::Range<usize>; // paragraph's full text. const SPACING_REPLACE: char = ' '; // Space const NODE_REPLACE: char = '\u{FFFC}'; // Object Replacement Character -const PIN_REPLACE: char = '\u{200D}'; // Zero Width Joiner /// A paragraph representation in which children are already layouted and text /// is already preshaped. @@ -278,8 +274,6 @@ enum Segment<'a> { Spacing(Spacing), /// An arbitrary inline-level layout node. Node(&'a LayoutNode), - /// A pin identified by index. - Pin(usize), } impl Segment<'_> { @@ -289,7 +283,6 @@ impl Segment<'_> { Self::Text(len) => len, Self::Spacing(_) => SPACING_REPLACE.len_utf8(), Self::Node(_) => NODE_REPLACE.len_utf8(), - Self::Pin(_) => PIN_REPLACE.len_utf8(), } } } @@ -307,8 +300,6 @@ enum Item<'a> { Frame(Frame), /// A repeating node that fills the remaining space. Repeat(&'a RepeatNode, StyleChain<'a>), - /// A pin identified by index. - Pin(usize), } impl<'a> Item<'a> { @@ -326,7 +317,6 @@ impl<'a> Item<'a> { Self::Text(shaped) => shaped.text.len(), Self::Absolute(_) | Self::Fractional(_) => SPACING_REPLACE.len_utf8(), Self::Frame(_) | Self::Repeat(_, _) => NODE_REPLACE.len_utf8(), - Self::Pin(_) => PIN_REPLACE.len_utf8(), } } @@ -336,7 +326,7 @@ impl<'a> Item<'a> { Self::Text(shaped) => shaped.width, Self::Absolute(v) => *v, Self::Frame(frame) => frame.width(), - Self::Fractional(_) | Self::Repeat(_, _) | Self::Pin(_) => Length::zero(), + Self::Fractional(_) | Self::Repeat(_, _) => Length::zero(), } } } @@ -467,7 +457,6 @@ fn collect<'a>( ParChild::Quote { .. } => Some('"'), ParChild::Spacing(_) => Some(SPACING_REPLACE), ParChild::Node(_) => Some(NODE_REPLACE), - ParChild::Pin(_) => Some(PIN_REPLACE), }); full.push_str(quoter.quote("es, double, peeked)); @@ -484,10 +473,6 @@ fn collect<'a>( full.push(NODE_REPLACE); Segment::Node(node) } - &ParChild::Pin(idx) => { - full.push(PIN_REPLACE); - Segment::Pin(idx) - } }; if let Some(last) = full.chars().last() { @@ -556,7 +541,6 @@ fn prepare<'a>( items.push(Item::Frame(frame)); } } - Segment::Pin(idx) => items.push(Item::Pin(idx)), } cursor = end; @@ -1187,11 +1171,6 @@ fn commit( } offset = before + fill; } - Item::Pin(idx) => { - let mut frame = Frame::new(Size::zero()); - frame.push(Point::zero(), Element::Pin(*idx)); - push(&mut offset, frame); - } } } |
