Skip to content

Commit 0363ba0

Browse files
authored
Merge pull request #19 from LaurenzV/main
Fixes #9
2 parents bd09bea + 73bd6ea commit 0363ba0

5 files changed

Lines changed: 48 additions & 33 deletions

File tree

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ fn apply_clip_path(path_id: Option<&String>, content: &mut Content, ctx: &mut Co
519519
if let NodeKind::ClipPath(ref path) = *clip_path.borrow() {
520520
apply_clip_path(path.clip_path.as_ref(), content, ctx);
521521

522+
let old = ctx.c.concat_transform(path.transform);
523+
522524
for child in clip_path.children() {
523525
match *child.borrow() {
524526
NodeKind::Path(ref path) => {
@@ -530,6 +532,8 @@ fn apply_clip_path(path_id: Option<&String>, content: &mut Content, ctx: &mut Co
530532
_ => unreachable!(),
531533
}
532534
}
535+
536+
ctx.c.set_transform(old);
533537
} else {
534538
unreachable!();
535539
}

src/render.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,9 @@ fn prep_pattern(
446446
let matrix = transform_to_matrix(pattern.transform);
447447
let pdf_rect = ctx.c.pdf_rect(rect);
448448

449-
let mut inner_matrix = if let Some(viewbox) = pattern.view_box {
449+
let mut inner_transform = if let Some(viewbox) = pattern.view_box {
450450
CoordToPdf::new((rect.width(), rect.height()), ctx.c.dpi(), viewbox, None)
451-
.uncorrected_matrix()
451+
.uncorrected_transformation()
452452
} else if pattern.content_units == Units::ObjectBoundingBox {
453453
let viewbox = ViewBox {
454454
rect: usvg::Rect::new(0.0, 0.0, 1.0, 1.0).unwrap(),
@@ -460,20 +460,20 @@ fn prep_pattern(
460460
};
461461

462462
CoordToPdf::new((bbox.width(), bbox.height()), ctx.c.dpi(), viewbox, None)
463-
.uncorrected_matrix()
463+
.uncorrected_transformation()
464464
} else {
465-
[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]
465+
Transform::new(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
466466
};
467467

468468
ctx.push();
469469

470-
inner_matrix[4] += rect.x();
471-
inner_matrix[5] += rect.y();
470+
inner_transform.e += rect.x();
471+
inner_transform.f += rect.y();
472472

473-
let old = ctx.c.transform(inner_matrix);
473+
let old = ctx.c.concat_transform(inner_transform);
474474

475475
let pattern_stream = content_stream(node, writer, ctx);
476-
ctx.c.transform(old);
476+
ctx.c.set_transform(old);
477477

478478
let pattern_ref = ctx.alloc_ref();
479479
let mut pdf_pattern = writer.tiling_pattern(pattern_ref, &pattern_stream);
@@ -509,22 +509,16 @@ impl Render for usvg::Group {
509509
ctx.push();
510510

511511
let group_ref = ctx.alloc_ref();
512-
let child_content = content_stream(&node, writer, ctx);
513512

514513
let bbox = node
515514
.calculate_bbox()
516515
.and_then(|b| b.to_rect())
517516
.unwrap_or_else(|| usvg::Rect::new(0.0, 0.0, 1.0, 1.0).unwrap());
518517

519518
let pdf_bbox = ctx.c.pdf_rect(bbox);
520-
let old = ctx.c.transform([
521-
self.transform.a,
522-
self.transform.b,
523-
self.transform.c,
524-
self.transform.d,
525-
self.transform.e,
526-
self.transform.f,
527-
]);
519+
let old = ctx.c.concat_transform(self.transform);
520+
521+
let child_content = content_stream(&node, writer, ctx);
528522

529523
// Every group is an isolated transparency group, it needs to be painted
530524
// onto its own canvas.
@@ -545,7 +539,7 @@ impl Render for usvg::Group {
545539
content.save_state();
546540

547541
apply_clip_path(self.clip_path.as_ref(), content, ctx);
548-
ctx.c.transform(old);
542+
ctx.c.set_transform(old);
549543

550544
if let Some(reference) = apply_mask(self.mask.as_ref(), bbox, pdf_bbox, ctx) {
551545
let num = ctx.alloc_gs();

src/scale.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Provide transformations between PDF and SVG coordinate systems.
22
3-
use usvg::{Align, AspectRatio, ViewBox};
3+
use usvg::{Align, AspectRatio, Transform, ViewBox};
44

55
/// Convert point data between two coordinate systems.
66
#[derive(Debug, Copy, Clone)]
@@ -11,7 +11,7 @@ pub struct CoordToPdf {
1111
offset_y: f64,
1212
height_y: f64,
1313
dpi: f64,
14-
matrix: [f64; 6],
14+
transform: Transform,
1515
}
1616

1717
impl CoordToPdf {
@@ -94,7 +94,7 @@ impl CoordToPdf {
9494
offset_y,
9595
height_y: viewport.1,
9696
dpi,
97-
matrix: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
97+
transform: Transform::new(1.0, 0.0, 0.0, 1.0, 0.0, 0.0),
9898
}
9999
}
100100

@@ -138,18 +138,18 @@ impl CoordToPdf {
138138
self.dpi
139139
}
140140

141-
/// Get the transformation matrix for this converter but without accounting
141+
/// Get the transformation for this converter but without accounting
142142
/// for either DPI or that the PDF coordinate system is flipped. This is
143143
/// useful for converting between two SVG coordinate systems.
144-
pub fn uncorrected_matrix(&self) -> [f64; 6] {
145-
[
144+
pub fn uncorrected_transformation(&self) -> Transform {
145+
Transform::new(
146146
self.factor_x,
147147
0.0,
148148
0.0,
149149
self.factor_y,
150150
self.offset_x,
151151
self.offset_y,
152-
]
152+
)
153153
}
154154

155155
/// Transform a rectangle from SVG to PDF formats.
@@ -159,18 +159,19 @@ impl CoordToPdf {
159159
pdf_writer::Rect::new(x1, y1, x2, y2)
160160
}
161161

162-
/// Apply a transformation matrix to a point.
162+
/// Apply a transformation to a point.
163163
fn apply(&self, point: (f64, f64)) -> (f64, f64) {
164-
(
165-
point.0 * self.matrix[0] + point.1 * self.matrix[1] + self.matrix[4],
166-
point.0 * self.matrix[2] + point.1 * self.matrix[3] + self.matrix[5],
167-
)
164+
self.transform.apply(point.0, point.1)
168165
}
169166

170167
/// Set a pre-transformation, overriding the old one.
171-
pub fn transform(&mut self, matrix: [f64; 6]) -> [f64; 6] {
172-
let old = self.matrix;
173-
self.matrix = matrix;
168+
pub fn concat_transform(&mut self, add_transform: Transform) -> Transform {
169+
let old = self.transform.clone();
170+
self.transform.append(&add_transform);
174171
old
175172
}
173+
174+
pub fn set_transform(&mut self, transform: Transform) {
175+
self.transform = transform;
176+
}
176177
}

tests/clip_line.svg

Lines changed: 9 additions & 0 deletions
Loading

tests/green_square.svg

Lines changed: 7 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)