summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/export/pdf.rs17
-rw-r--r--src/geom/length.rs8
-rw-r--r--src/layout/graphics.rs62
-rw-r--r--src/layout/mod.rs15
-rw-r--r--src/library/graphics.rs42
-rw-r--r--src/library/mod.rs27
6 files changed, 155 insertions, 16 deletions
diff --git a/src/export/pdf.rs b/src/export/pdf.rs
index 763cea8a..cb05dee0 100644
--- a/src/export/pdf.rs
+++ b/src/export/pdf.rs
@@ -148,6 +148,10 @@ impl<'a> PdfExporter<'a> {
text = text.tm(1.0, 0.0, 0.0, 1.0, x as f32, y as f32);
text = text.tj(&shaped.encode_glyphs_be());
}
+
+ LayoutElement::Image(_image) => {
+ // TODO: Write image.
+ }
}
}
@@ -280,12 +284,13 @@ fn remap_fonts(layouts: &[BoxLayout]) -> (HashMap<FaceId, usize>, Vec<FaceId>) {
// each text element to find out which face is uses.
for layout in layouts {
for (_, element) in &layout.elements {
- let LayoutElement::Text(shaped) = element;
- to_pdf.entry(shaped.face).or_insert_with(|| {
- let next_id = to_layout.len();
- to_layout.push(shaped.face);
- next_id
- });
+ if let LayoutElement::Text(shaped) = element {
+ to_pdf.entry(shaped.face).or_insert_with(|| {
+ let next_id = to_layout.len();
+ to_layout.push(shaped.face);
+ next_id
+ });
+ }
}
}
diff --git a/src/geom/length.rs b/src/geom/length.rs
index 60ccce2b..061510a1 100644
--- a/src/geom/length.rs
+++ b/src/geom/length.rs
@@ -142,6 +142,14 @@ impl Div<f64> for Length {
}
}
+impl Div for Length {
+ type Output = f64;
+
+ fn div(self, other: Self) -> f64 {
+ self.raw / other.raw
+ }
+}
+
assign_impl!(Length += Length);
assign_impl!(Length -= Length);
assign_impl!(Length *= f64);
diff --git a/src/layout/graphics.rs b/src/layout/graphics.rs
new file mode 100644
index 00000000..1fa05605
--- /dev/null
+++ b/src/layout/graphics.rs
@@ -0,0 +1,62 @@
+use std::fmt::{self, Debug, Formatter};
+
+use super::*;
+
+/// An image node.
+#[derive(Clone, PartialEq)]
+pub struct Image {
+ /// The image.
+ pub buf: RgbaImage,
+ /// The fixed width, if any.
+ pub width: Option<Linear>,
+ /// The fixed height, if any.
+ pub height: Option<Linear>,
+ /// How to align this image node in its parent.
+ pub align: BoxAlign,
+}
+
+impl Layout for Image {
+ fn layout(&self, _: &mut LayoutContext, areas: &Areas) -> Layouted {
+ let Area { rem, full } = areas.current;
+ let (pixel_width, pixel_height) = self.buf.dimensions();
+ let pixel_ratio = (pixel_width as f64) / (pixel_height as f64);
+
+ let width = self.width.map(|w| w.resolve(full.width));
+ let height = self.height.map(|w| w.resolve(full.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) => {
+ let ratio = rem.width / rem.height;
+ if ratio < pixel_ratio {
+ Size::new(rem.width, rem.width / pixel_ratio)
+ } else {
+ // TODO: Fix issue with line spacing.
+ Size::new(rem.height * pixel_ratio, rem.height)
+ }
+ }
+ };
+
+ let mut boxed = BoxLayout::new(size);
+ boxed.push(
+ Point::ZERO,
+ LayoutElement::Image(ImageElement { buf: self.buf.clone(), size }),
+ );
+
+ Layouted::Layout(boxed, self.align)
+ }
+}
+
+impl Debug for Image {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ f.pad("Image")
+ }
+}
+
+impl From<Image> for LayoutNode {
+ fn from(image: Image) -> Self {
+ Self::dynamic(image)
+ }
+}
diff --git a/src/layout/mod.rs b/src/layout/mod.rs
index 4dd6184f..5586a1fd 100644
--- a/src/layout/mod.rs
+++ b/src/layout/mod.rs
@@ -2,6 +2,7 @@
mod document;
mod fixed;
+mod graphics;
mod node;
mod pad;
mod par;
@@ -9,12 +10,15 @@ mod spacing;
mod stack;
mod text;
+use image::RgbaImage;
+
use crate::font::SharedFontLoader;
use crate::geom::*;
use crate::shaping::Shaped;
pub use document::*;
pub use fixed::*;
+pub use graphics::*;
pub use node::*;
pub use pad::*;
pub use par::*;
@@ -179,4 +183,15 @@ impl BoxLayout {
pub enum LayoutElement {
/// Shaped text.
Text(Shaped),
+ /// An image.
+ Image(ImageElement),
+}
+
+/// An image.
+#[derive(Debug, Clone, PartialEq)]
+pub struct ImageElement {
+ /// The image.
+ pub buf: RgbaImage,
+ /// The document size of the image.
+ pub size: Size,
}
diff --git a/src/library/graphics.rs b/src/library/graphics.rs
new file mode 100644
index 00000000..779d78b5
--- /dev/null
+++ b/src/library/graphics.rs
@@ -0,0 +1,42 @@
+use std::fs::File;
+use std::io::BufReader;
+
+use image::io::Reader;
+
+use crate::layout::Image;
+use crate::prelude::*;
+
+/// `image`: Include an image.
+///
+/// # Positional arguments
+/// - The path to the image (string)
+pub fn image(mut args: Args, ctx: &mut EvalContext) -> Value {
+ let path = args.need::<_, Spanned<String>>(ctx, 0, "path");
+ let width = args.get::<_, Linear>(ctx, "width");
+ let height = args.get::<_, Linear>(ctx, "height");
+
+ if let Some(path) = path {
+ if let Ok(file) = File::open(path.v) {
+ match Reader::new(BufReader::new(file))
+ .with_guessed_format()
+ .map_err(|err| err.into())
+ .and_then(|reader| reader.decode())
+ .map(|img| img.into_rgba8())
+ {
+ Ok(buf) => {
+ ctx.push(Image {
+ buf,
+ width,
+ height,
+ align: ctx.state.align,
+ });
+ }
+ Err(err) => ctx.diag(error!(path.span, "invalid image: {}", err)),
+ }
+ } else {
+ ctx.diag(error!(path.span, "failed to open image file"));
+ }
+ }
+
+ Value::None
+}
diff --git a/src/library/mod.rs b/src/library/mod.rs
index af23d050..e59201dc 100644
--- a/src/library/mod.rs
+++ b/src/library/mod.rs
@@ -4,6 +4,7 @@ mod align;
mod boxed;
mod color;
mod font;
+mod graphics;
mod page;
mod spacing;
@@ -11,29 +12,35 @@ pub use align::*;
pub use boxed::*;
pub use color::*;
pub use font::*;
+pub use graphics::*;
pub use page::*;
pub use spacing::*;
use crate::eval::{Scope, ValueFunc};
macro_rules! std {
- ($($name:literal => $func:expr),* $(,)?) => {
+ ($($func:expr $(=> $name:expr)?),* $(,)?) => {
/// Create a scope with all standard library functions.
pub fn _std() -> Scope {
let mut std = Scope::new();
- $(std.set($name, ValueFunc::new($func));)*
+ $(
+ let _name = stringify!($func);
+ $(let _name = $name;)?
+ std.set(_name, ValueFunc::new($func));
+ )*
std
}
};
}
std! {
- "align" => align,
- "box" => boxed,
- "font" => font,
- "h" => h,
- "page" => page,
- "pagebreak" => pagebreak,
- "rgb" => rgb,
- "v" => v,
+ align,
+ boxed => "box",
+ font,
+ h,
+ image,
+ page,
+ pagebreak,
+ rgb,
+ v,
}