diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-10-28 16:43:38 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-10-28 16:43:38 +0200 |
| commit | 95e9134a3c7d7a14d8c8928413fdffc665671895 (patch) | |
| tree | 822b5f6c2d23aba33cfe4d199405e493e37c3d70 /src/export | |
| parent | 66030ae5d73d85a0463562230b87f8ec7554c746 (diff) | |
Refactor `geom` module
Diffstat (limited to 'src/export')
| -rw-r--r-- | src/export/pdf/mod.rs | 10 | ||||
| -rw-r--r-- | src/export/pdf/outline.rs | 6 | ||||
| -rw-r--r-- | src/export/pdf/page.rs | 34 | ||||
| -rw-r--r-- | src/export/render.rs | 19 |
4 files changed, 32 insertions, 37 deletions
diff --git a/src/export/pdf/mod.rs b/src/export/pdf/mod.rs index 2f6f255d..5f47043e 100644 --- a/src/export/pdf/mod.rs +++ b/src/export/pdf/mod.rs @@ -16,7 +16,7 @@ use self::outline::{Heading, HeadingNode}; use self::page::Page; use crate::font::Font; use crate::frame::{Frame, Lang}; -use crate::geom::{Dir, Em, Length}; +use crate::geom::{Abs, Dir, Em}; use crate::image::Image; /// Export a collection of frames into a PDF file. @@ -182,13 +182,13 @@ where } } -/// Additional methods for [`Length`]. -trait LengthExt { - /// Convert an em length to a number of points. +/// Additional methods for [`Abs`]. +trait AbsExt { + /// Convert an to a number of points. fn to_f32(self) -> f32; } -impl LengthExt for Length { +impl AbsExt for Abs { fn to_f32(self) -> f32 { self.to_pt() as f32 } diff --git a/src/export/pdf/outline.rs b/src/export/pdf/outline.rs index f5530f86..73dd9e96 100644 --- a/src/export/pdf/outline.rs +++ b/src/export/pdf/outline.rs @@ -1,7 +1,7 @@ use pdf_writer::{Finish, Ref, TextStr}; -use super::{LengthExt, PdfContext, RefExt}; -use crate::geom::{Length, Point}; +use super::{AbsExt, PdfContext, RefExt}; +use crate::geom::{Abs, Point}; use crate::util::EcoString; /// A heading that can later be linked in the outline panel. @@ -77,7 +77,7 @@ pub fn write_outline_item( outline.title(TextStr(&node.heading.content)); outline.dest_direct().page(node.heading.page).xyz( node.heading.position.x.to_f32(), - (node.heading.position.y + Length::pt(3.0)).to_f32(), + (node.heading.position.y + Abs::pt(3.0)).to_f32(), None, ); diff --git a/src/export/pdf/page.rs b/src/export/pdf/page.rs index 1b1bec92..7fbf7833 100644 --- a/src/export/pdf/page.rs +++ b/src/export/pdf/page.rs @@ -3,12 +3,12 @@ use pdf_writer::writers::ColorSpace; use pdf_writer::{Content, Filter, Finish, Name, Rect, Ref, Str}; use super::{ - deflate, EmExt, Heading, HeadingNode, LengthExt, PdfContext, RefExt, D65_GRAY, SRGB, + deflate, AbsExt, EmExt, Heading, HeadingNode, PdfContext, RefExt, D65_GRAY, SRGB, }; use crate::font::Font; use crate::frame::{Destination, Element, Frame, Group, Role, Text}; use crate::geom::{ - self, Color, Em, Geometry, Length, Numeric, Paint, Point, Ratio, Shape, Size, Stroke, + self, Abs, Color, Em, Geometry, Numeric, Paint, Point, Ratio, Shape, Size, Stroke, Transform, }; use crate::image::Image; @@ -45,7 +45,7 @@ pub fn construct_page(ctx: &mut PdfContext, frame: &Frame) { ky: Ratio::zero(), kx: Ratio::zero(), sy: Ratio::new(-1.0), - tx: Length::zero(), + tx: Abs::zero(), ty: size.y, }); @@ -169,7 +169,7 @@ struct PageContext<'a> { #[derive(Debug, Default, Clone)] struct State { transform: Transform, - font: Option<(Font, Length)>, + font: Option<(Font, Abs)>, fill: Option<Paint>, fill_space: Option<Name<'static>>, stroke: Option<Stroke>, @@ -200,7 +200,7 @@ impl<'a> PageContext<'a> { ]); } - fn set_font(&mut self, font: &Font, size: Length) { + fn set_font(&mut self, font: &Font, size: Abs) { if self.state.font.as_ref().map(|(f, s)| (f, *s)) != Some((font, size)) { self.parent.font_map.insert(font.clone()); let name = format_eco!("F{}", self.parent.font_map.map(font.clone())); @@ -402,6 +402,12 @@ fn write_shape(ctx: &mut PageContext, x: f32, y: f32, shape: &Shape) { } match shape.geometry { + Geometry::Line(target) => { + let dx = target.x.to_f32(); + let dy = target.y.to_f32(); + ctx.content.move_to(x, y); + ctx.content.line_to(x + dx, y + dy); + } Geometry::Rect(size) => { let w = size.x.to_f32(); let h = size.y.to_f32(); @@ -409,16 +415,6 @@ fn write_shape(ctx: &mut PageContext, x: f32, y: f32, shape: &Shape) { ctx.content.rect(x, y, w, h); } } - Geometry::Ellipse(size) => { - let approx = geom::Path::ellipse(size); - write_path(ctx, x, y, &approx); - } - Geometry::Line(target) => { - let dx = target.x.to_f32(); - let dy = target.y.to_f32(); - ctx.content.move_to(x, y); - ctx.content.line_to(x + dx, y + dy); - } Geometry::Path(ref path) => { write_path(ctx, x, y, path); } @@ -469,10 +465,10 @@ fn write_image(ctx: &mut PageContext, x: f32, y: f32, image: &Image, size: Size) /// Save a link for later writing in the annotations dictionary. fn write_link(ctx: &mut PageContext, pos: Point, dest: &Destination, size: Size) { - let mut min_x = Length::inf(); - let mut min_y = Length::inf(); - let mut max_x = -Length::inf(); - let mut max_y = -Length::inf(); + let mut min_x = Abs::inf(); + let mut min_y = Abs::inf(); + let mut max_x = -Abs::inf(); + let mut max_y = -Abs::inf(); // Compute the bounding box of the transformed link. for point in [ diff --git a/src/export/render.rs b/src/export/render.rs index e8702f34..df8f512f 100644 --- a/src/export/render.rs +++ b/src/export/render.rs @@ -10,7 +10,7 @@ use usvg::FitTo; use crate::frame::{Element, Frame, Group, Text}; use crate::geom::{ - self, Geometry, Length, Paint, PathElement, Shape, Size, Stroke, Transform, + self, Abs, Geometry, Paint, PathElement, Shape, Size, Stroke, Transform, }; use crate::image::{DecodedImage, Image}; @@ -286,18 +286,17 @@ fn render_shape( shape: &Shape, ) -> Option<()> { let path = match shape.geometry { + Geometry::Line(target) => { + let mut builder = sk::PathBuilder::new(); + builder.line_to(target.x.to_f32(), target.y.to_f32()); + builder.finish()? + } Geometry::Rect(size) => { let w = size.x.to_f32(); let h = size.y.to_f32(); let rect = sk::Rect::from_xywh(0.0, 0.0, w, h)?; sk::PathBuilder::from_rect(rect) } - Geometry::Ellipse(size) => convert_path(&geom::Path::ellipse(size))?, - Geometry::Line(target) => { - let mut builder = sk::PathBuilder::new(); - builder.line_to(target.x.to_f32(), target.y.to_f32()); - builder.finish()? - } Geometry::Path(ref path) => convert_path(path)?, }; @@ -460,12 +459,12 @@ impl OutlineBuilder for WrappedPathBuilder { } /// Additional methods for [`Length`]. -trait LengthExt { - /// Convert an em length to a number of points as f32. +trait AbsExt { + /// Convert to a number of points as f32. fn to_f32(self) -> f32; } -impl LengthExt for Length { +impl AbsExt for Abs { fn to_f32(self) -> f32 { self.to_pt() as f32 } |
