|
5 | 5 | //! and functions to ultimately populate them to the file. |
6 | 6 |
|
7 | 7 | use std::collections::HashMap; |
| 8 | +use std::rc::Rc; |
8 | 9 |
|
9 | 10 | use pdf_writer::types::{MaskType, ShadingType}; |
10 | 11 | use pdf_writer::writers::{ExtGraphicsState, Resources, ShadingPattern}; |
11 | 12 | use pdf_writer::{Finish, Name, PdfWriter, Rect, Ref}; |
12 | | -use usvg::{NodeKind, Transform, Tree}; |
| 13 | +use usvg::{Transform, Tree}; |
13 | 14 |
|
14 | 15 | use super::{content_stream, form_xobject, Context, CoordToPdf}; |
15 | 16 | use crate::render::Gradient; |
@@ -230,32 +231,38 @@ pub fn write_xobjects(pending_xobjects: &[(u32, Ref)], resources: &mut Resources |
230 | 231 | /// Write the content streams of the used masks stored in the context to the |
231 | 232 | /// file. |
232 | 233 | pub(crate) fn write_masks(tree: &Tree, writer: &mut PdfWriter, ctx: &mut Context) { |
233 | | - for (id, gp) in ctx.pending_groups.clone() { |
234 | | - let mask_node = tree.defs_by_id(&id).unwrap(); |
235 | | - let borrowed = mask_node.borrow(); |
| 234 | + let mut masks: Vec<Rc<usvg::Mask>> = Vec::new(); |
| 235 | + tree.masks(|mask| { |
| 236 | + if masks.iter().find(|m| m.id == mask.id).is_some() { |
| 237 | + return; |
| 238 | + } |
236 | 239 |
|
237 | | - if let NodeKind::Mask(_) = *borrowed { |
238 | | - ctx.push(); |
239 | | - ctx.initial_mask = gp.initial_mask; |
| 240 | + masks.push(mask); |
| 241 | + }); |
240 | 242 |
|
241 | | - // Get the context of where the pending group was originally in the tree |
242 | | - let old = ctx.c.set_transform(gp.transform); |
| 243 | + for (id, gp) in ctx.pending_groups.clone() { |
| 244 | + let mask_node = masks.iter().find(|m| m.id == id).unwrap(); |
243 | 245 |
|
244 | | - let content = content_stream(&mask_node, writer, ctx); |
| 246 | + ctx.push(); |
| 247 | + ctx.initial_mask = gp.initial_mask; |
245 | 248 |
|
246 | | - let mut group = |
247 | | - form_xobject(writer, gp.reference, &content, gp.bbox, ctx.compress, true); |
| 249 | + // Get the context of where the pending group was originally in the tree |
| 250 | + let old = ctx.c.set_transform(gp.transform); |
248 | 251 |
|
249 | | - if let Some(matrix) = gp.matrix { |
250 | | - group.matrix(matrix); |
251 | | - } |
| 252 | + let content = content_stream(&mask_node.root, writer, ctx); |
252 | 253 |
|
253 | | - ctx.c.set_transform(old); |
| 254 | + let mut group = |
| 255 | + form_xobject(writer, gp.reference, &content, gp.bbox, ctx.compress, true); |
254 | 256 |
|
255 | | - let mut resources = group.resources(); |
256 | | - ctx.pop(&mut resources); |
257 | | - resources.finish(); |
| 257 | + if let Some(matrix) = gp.matrix { |
| 258 | + group.matrix(matrix); |
258 | 259 | } |
| 260 | + |
| 261 | + ctx.c.set_transform(old); |
| 262 | + |
| 263 | + let mut resources = group.resources(); |
| 264 | + ctx.pop(&mut resources); |
| 265 | + resources.finish(); |
259 | 266 | } |
260 | 267 |
|
261 | 268 | ctx.initial_mask = None; |
|
0 commit comments