summaryrefslogtreecommitdiff
path: root/src/export/pdf
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-10-28 16:43:38 +0200
committerLaurenz <laurmaedje@gmail.com>2022-10-28 16:43:38 +0200
commit95e9134a3c7d7a14d8c8928413fdffc665671895 (patch)
tree822b5f6c2d23aba33cfe4d199405e493e37c3d70 /src/export/pdf
parent66030ae5d73d85a0463562230b87f8ec7554c746 (diff)
Refactor `geom` module
Diffstat (limited to 'src/export/pdf')
-rw-r--r--src/export/pdf/mod.rs10
-rw-r--r--src/export/pdf/outline.rs6
-rw-r--r--src/export/pdf/page.rs34
3 files changed, 23 insertions, 27 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 [