Skip to content

Commit 3748f4b

Browse files
committed
Implemented preliminary fix of wrong Rust logo
1 parent b24680b commit 3748f4b

6 files changed

Lines changed: 97 additions & 9 deletions

File tree

src/defer.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::collections::HashMap;
99
use pdf_writer::types::{MaskType, ShadingType};
1010
use pdf_writer::writers::{ExtGraphicsState, Resources, ShadingPattern};
1111
use pdf_writer::{Finish, Name, PdfWriter, Rect, Ref};
12-
use usvg::{NodeKind, Tree};
12+
use usvg::{NodeKind, Transform, Tree};
1313

1414
use super::{content_stream, form_xobject, Context, CoordToPdf};
1515
use crate::render::Gradient;
@@ -130,6 +130,9 @@ pub struct PendingGroup {
130130
/// A transformation matrix to allow for a different coordinate system use
131131
/// in the object.
132132
pub matrix: Option<[f32; 6]>,
133+
// Contains the transform of the context where the pending group
134+
// was in the tree
135+
pub transform: Transform,
133136
/// An SVG ID to a mask that should be applied at the start of the content
134137
/// stream.
135138
pub initial_mask: Option<String>,
@@ -236,6 +239,9 @@ pub(crate) fn write_masks(tree: &Tree, writer: &mut PdfWriter, ctx: &mut Context
236239
ctx.push();
237240
ctx.initial_mask = gp.initial_mask;
238241

242+
// Get the context of where the pending group was originally in the tree
243+
let old = ctx.c.set_transform(gp.transform);
244+
239245
let content = content_stream(&mask_node, writer, ctx);
240246

241247
let mut group =
@@ -245,6 +251,8 @@ pub(crate) fn write_masks(tree: &Tree, writer: &mut PdfWriter, ctx: &mut Context
245251
group.matrix(matrix);
246252
}
247253

254+
ctx.c.set_transform(old);
255+
248256
let mut resources = group.resources();
249257
ctx.pop(&mut resources);
250258
resources.finish();

src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,12 +563,18 @@ fn apply_mask(
563563

564564
apply_mask(mask.mask.as_ref(), mask.rect, pdf_bbox, ctx);
565565

566-
ctx.pending_groups.insert(mask.id.clone(), PendingGroup {
567-
reference,
568-
bbox,
569-
matrix,
570-
initial_mask: mask.mask.clone(),
571-
});
566+
let context_transform = ctx.c.get_transform();
567+
568+
ctx.pending_groups.insert(
569+
mask.id.clone(),
570+
PendingGroup {
571+
reference,
572+
bbox,
573+
matrix,
574+
transform: context_transform,
575+
initial_mask: mask.mask.clone(),
576+
},
577+
);
572578

573579
Some(reference)
574580
} else {

src/render.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,6 @@ impl Render for usvg::Group {
539539
content.save_state();
540540

541541
apply_clip_path(self.clip_path.as_ref(), content, ctx);
542-
ctx.c.set_transform(old);
543542

544543
if let Some(reference) = apply_mask(self.mask.as_ref(), bbox, pdf_bbox, ctx) {
545544
let num = ctx.alloc_gs();
@@ -554,6 +553,8 @@ impl Render for usvg::Group {
554553
.push(PendingGS::fill_opacity(self.opacity.value() as f32, num));
555554
}
556555

556+
ctx.c.set_transform(old);
557+
557558
content.x_object(Name(name.as_bytes()));
558559
content.restore_state();
559560
ctx.pending_xobjects.push((num, group_ref));

src/scale.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ impl CoordToPdf {
171171
old
172172
}
173173

174-
pub fn set_transform(&mut self, transform: Transform) {
174+
pub fn set_transform(&mut self, transform: Transform) -> Transform {
175+
let old = self.transform;
175176
self.transform = transform;
177+
old
178+
}
179+
180+
pub fn get_transform(&self) -> Transform {
181+
self.transform
176182
}
177183
}

tests/circle.svg

Lines changed: 10 additions & 0 deletions
Loading

tests/rust_logo.svg

Lines changed: 57 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)