1
//! The main context structure which drives the drawing process.
2

            
3
use float_cmp::approx_eq;
4
use glib::translate::*;
5
use pango::ffi::PangoMatrix;
6
use pango::prelude::FontMapExt;
7
use regex::{Captures, Regex};
8
use std::cell::RefCell;
9
use std::convert::TryFrom;
10
use std::f64::consts::*;
11
use std::rc::Rc;
12
use std::{borrow::Cow, sync::OnceLock};
13

            
14
use crate::accept_language::UserLanguage;
15
use crate::aspect_ratio::AspectRatio;
16
use crate::bbox::BoundingBox;
17
use crate::color::color_to_rgba;
18
use crate::coord_units::CoordUnits;
19
use crate::document::{AcquiredNodes, NodeId};
20
use crate::dpi::Dpi;
21
use crate::element::{Element, ElementData};
22
use crate::error::{AcquireError, ImplementationLimit, InternalRenderingError};
23
use crate::filters::{self, FilterSpec};
24
use crate::float_eq_cairo::ApproxEqCairo;
25
use crate::gradient::{GradientVariant, SpreadMethod, UserSpaceGradient};
26
use crate::layout::{
27
    Filter, Image, Layer, LayerKind, Shape, StackingContext, Stroke, Text, TextSpan,
28
};
29
use crate::length::*;
30
use crate::marker;
31
use crate::node::{CascadedValues, Node, NodeBorrow, NodeDraw};
32
use crate::paint_server::{PaintSource, UserSpacePaintSource};
33
use crate::path_builder::*;
34
use crate::pattern::UserSpacePattern;
35
use crate::properties::{
36
    ClipRule, ComputedValues, FillRule, ImageRendering, MaskType, MixBlendMode, Opacity, Overflow,
37
    PaintTarget, ShapeRendering, StrokeLinecap, StrokeLinejoin, TextRendering,
38
};
39
use crate::rect::{rect_to_transform, IRect, Rect};
40
use crate::rsvg_log;
41
use crate::session::Session;
42
use crate::surface_utils::shared_surface::{
43
    ExclusiveImageSurface, Interpolation, SharedImageSurface, SurfaceType,
44
};
45
use crate::transform::{Transform, ValidTransform};
46
use crate::unit_interval::UnitInterval;
47
use crate::viewbox::ViewBox;
48
use crate::{borrow_element_as, is_element_of_type};
49

            
50
/// Opaque font options for a DrawingCtx.
51
///
52
/// This is used for DrawingCtx::create_pango_context.
53
pub struct FontOptions {
54
    options: cairo::FontOptions,
55
}
56

            
57
#[derive(Debug, Copy, Clone, PartialEq)]
58
pub enum ClipMode {
59
    ClipToViewport,
60
    NoClip,
61
}
62

            
63
/// Set path on the cairo context, or clear it.
64
/// This helper object keeps track whether the path has been set already,
65
/// so that it isn't recalculated every so often.
66
struct PathHelper<'a> {
67
    cr: &'a cairo::Context,
68
    transform: ValidTransform,
69
    path: &'a Path,
70
    is_square_linecap: bool,
71
    has_path: Option<bool>,
72
}
73

            
74
impl<'a> PathHelper<'a> {
75
948198
    pub fn new(
76
        cr: &'a cairo::Context,
77
        transform: ValidTransform,
78
        path: &'a Path,
79
        linecap: StrokeLinecap,
80
    ) -> Self {
81
948198
        PathHelper {
82
            cr,
83
            transform,
84
            path,
85
948198
            is_square_linecap: linecap == StrokeLinecap::Square,
86
948198
            has_path: None,
87
        }
88
948198
    }
89

            
90
2843786
    pub fn set(&mut self) -> Result<(), InternalRenderingError> {
91
2843786
        match self.has_path {
92
            Some(false) | None => {
93
948648
                self.has_path = Some(true);
94
948648
                self.cr.set_matrix(self.transform.into());
95
948648
                self.path.to_cairo(self.cr, self.is_square_linecap)
96
            }
97
1895138
            Some(true) => Ok(()),
98
        }
99
2843786
    }
100

            
101
1896191
    pub fn unset(&mut self) {
102
1896191
        match self.has_path {
103
            Some(true) | None => {
104
948783
                self.has_path = Some(false);
105
948783
                self.cr.new_path();
106
            }
107
            Some(false) => {}
108
        }
109
1896191
    }
110
}
111

            
112
/// Holds the size of the current viewport in the user's coordinate system.
113
105588
#[derive(Clone)]
114
pub struct Viewport {
115
52794
    pub dpi: Dpi,
116

            
117
    /// Corners of the current coordinate space.
118
52794
    pub vbox: ViewBox,
119

            
120
    /// The viewport's coordinate system, or "user coordinate system" in SVG terms.
121
52794
    transform: Transform,
122
}
123

            
124
impl Viewport {
125
    /// FIXME: this is just used in Handle::with_height_to_user(), and in length.rs's test suite.
126
    /// Find a way to do this without involving a default identity transform.
127
166
    pub fn new(dpi: Dpi, view_box_width: f64, view_box_height: f64) -> Viewport {
128
166
        Viewport {
129
            dpi,
130
166
            vbox: ViewBox::from(Rect::from_size(view_box_width, view_box_height)),
131
166
            transform: Default::default(),
132
        }
133
166
    }
134

            
135
    /// Creates a new viewport suitable for a certain kind of units.
136
    ///
137
    /// For `objectBoundingBox`, CSS lengths which are in percentages
138
    /// refer to the size of the current viewport.  Librsvg implements
139
    /// that by keeping the same current transformation matrix, and
140
    /// setting a viewport size of (1.0, 1.0).
141
    ///
142
    /// For `userSpaceOnUse`, we just duplicate the current viewport,
143
    /// since that kind of units means to use the current coordinate
144
    /// system unchanged.
145
501224
    pub fn with_units(&self, units: CoordUnits) -> Viewport {
146
501224
        match units {
147
404
            CoordUnits::ObjectBoundingBox => Viewport {
148
404
                dpi: self.dpi,
149
404
                vbox: ViewBox::from(Rect::from_size(1.0, 1.0)),
150
404
                transform: self.transform,
151
404
            },
152

            
153
500820
            CoordUnits::UserSpaceOnUse => Viewport {
154
500820
                dpi: self.dpi,
155
500820
                vbox: self.vbox,
156
500820
                transform: self.transform,
157
500820
            },
158
        }
159
501224
    }
160

            
161
    /// Returns a viewport with a new size for normalizing `Length` values.
162
193
    pub fn with_view_box(&self, width: f64, height: f64) -> Viewport {
163
193
        Viewport {
164
193
            dpi: self.dpi,
165
193
            vbox: ViewBox::from(Rect::from_size(width, height)),
166
193
            transform: self.transform,
167
        }
168
193
    }
169
}
170

            
171
pub struct DrawingCtx {
172
    session: Session,
173

            
174
    initial_viewport: Viewport,
175

            
176
    dpi: Dpi,
177

            
178
    cr_stack: Rc<RefCell<Vec<cairo::Context>>>,
179
    cr: cairo::Context,
180

            
181
    user_language: UserLanguage,
182

            
183
    drawsub_stack: Vec<Node>,
184

            
185
    svg_nesting: SvgNesting,
186

            
187
    measuring: bool,
188
    testing: bool,
189
}
190

            
191
pub enum DrawingMode {
192
    LimitToStack { node: Node, root: Node },
193

            
194
    OnlyNode(Node),
195
}
196

            
197
/// Whether an SVG document is being rendered standalone or referenced from an `<image>` element.
198
///
199
/// Normally, the coordinate system used when rendering a toplevel SVG is determined from the
200
/// initial viewport and the `<svg>` element's `viewBox` and `preserveAspectRatio` attributes.
201
/// However, when an SVG document is referenced from an `<image>` element, as in `<image href="foo.svg"/>`,
202
/// its `preserveAspectRatio` needs to be ignored so that the one from the `<image>` element can
203
/// be used instead.  This lets the parent document (the one with the `<image>` element) specify
204
/// how it wants the child SVG to be scaled into the viewport.
205
#[derive(Copy, Clone)]
206
pub enum SvgNesting {
207
    Standalone,
208
    ReferencedFromImageElement,
209
}
210

            
211
/// The toplevel drawing routine.
212
///
213
/// This creates a DrawingCtx internally and starts drawing at the specified `node`.
214
1105
pub fn draw_tree(
215
    session: Session,
216
    mode: DrawingMode,
217
    cr: &cairo::Context,
218
    viewport_rect: Rect,
219
    user_language: &UserLanguage,
220
    dpi: Dpi,
221
    svg_nesting: SvgNesting,
222
    measuring: bool,
223
    testing: bool,
224
    acquired_nodes: &mut AcquiredNodes<'_>,
225
) -> Result<BoundingBox, InternalRenderingError> {
226
2230
    let (drawsub_stack, node) = match mode {
227
1087
        DrawingMode::LimitToStack { node, root } => (node.ancestors().collect(), root),
228

            
229
18
        DrawingMode::OnlyNode(node) => (Vec::new(), node),
230
    };
231

            
232
1125
    let cascaded = CascadedValues::new_from_node(&node);
233

            
234
    // Preserve the user's transform and use it for the outermost bounding box.  All bounds/extents
235
    // will be converted to this transform in the end.
236
1103
    let user_transform = Transform::from(cr.matrix());
237
1100
    let mut user_bbox = BoundingBox::new().with_transform(user_transform);
238

            
239
    // https://www.w3.org/TR/SVG2/coords.html#InitialCoordinateSystem
240
    //
241
    // "For the outermost svg element, the SVG user agent must
242
    // determine an initial viewport coordinate system and an
243
    // initial user coordinate system such that the two
244
    // coordinates systems are identical. The origin of both
245
    // coordinate systems must be at the origin of the SVG
246
    // viewport."
247
    //
248
    // "... the initial viewport coordinate system (and therefore
249
    // the initial user coordinate system) must have its origin at
250
    // the top/left of the viewport"
251

            
252
    // Translate so (0, 0) is at the viewport's upper-left corner.
253
1105
    let transform = user_transform.pre_translate(viewport_rect.x0, viewport_rect.y0);
254

            
255
    // Here we exit immediately if the transform is not valid, since we are in the
256
    // toplevel drawing function.  Downstream cases would simply not render the current
257
    // element and ignore the error.
258
2210
    let valid_transform = ValidTransform::try_from(transform)?;
259
1102
    cr.set_matrix(valid_transform.into());
260

            
261
    // Per the spec, so the viewport has (0, 0) as upper-left.
262
1107
    let viewport_rect = viewport_rect.translate((-viewport_rect.x0, -viewport_rect.y0));
263
1105
    let initial_viewport = Viewport {
264
        dpi,
265
1107
        vbox: ViewBox::from(viewport_rect),
266
        transform,
267
    };
268

            
269
1107
    let mut draw_ctx = DrawingCtx::new(
270
1105
        session,
271
        cr,
272
        &initial_viewport,
273
1105
        user_language.clone(),
274
        dpi,
275
        svg_nesting,
276
        measuring,
277
        testing,
278
1107
        drawsub_stack,
279
1105
    );
280

            
281
1105
    let content_bbox = draw_ctx.draw_node_from_stack(
282
        &node,
283
        acquired_nodes,
284
        &cascaded,
285
        &initial_viewport,
286
        false,
287
1
    )?;
288

            
289
1104
    user_bbox.insert(&content_bbox);
290

            
291
1104
    Ok(user_bbox)
292
1105
}
293

            
294
2008847
pub fn with_saved_cr<O, F>(cr: &cairo::Context, f: F) -> Result<O, InternalRenderingError>
295
where
296
    F: FnOnce() -> Result<O, InternalRenderingError>,
297
{
298
2008847
    cr.save()?;
299
2005217
    match f() {
300
2007446
        Ok(o) => {
301
4017301
            cr.restore()?;
302
2008134
            Ok(o)
303
2008134
        }
304

            
305
37
        Err(e) => Err(e),
306
    }
307
2008171
}
308

            
309
impl Drop for DrawingCtx {
310
51883
    fn drop(&mut self) {
311
51883
        self.cr_stack.borrow_mut().pop();
312
51881
    }
313
}
314

            
315
const CAIRO_TAG_LINK: &str = "Link";
316

            
317
impl DrawingCtx {
318
1103
    fn new(
319
        session: Session,
320
        cr: &cairo::Context,
321
        initial_viewport: &Viewport,
322
        user_language: UserLanguage,
323
        dpi: Dpi,
324
        svg_nesting: SvgNesting,
325
        measuring: bool,
326
        testing: bool,
327
        drawsub_stack: Vec<Node>,
328
    ) -> DrawingCtx {
329
1101
        DrawingCtx {
330
1105
            session,
331
1105
            initial_viewport: initial_viewport.clone(),
332
            dpi,
333
1104
            cr_stack: Rc::new(RefCell::new(Vec::new())),
334
1101
            cr: cr.clone(),
335
1101
            user_language,
336
1101
            drawsub_stack,
337
            svg_nesting,
338
            measuring,
339
            testing,
340
        }
341
1101
    }
342

            
343
    /// Copies a `DrawingCtx` for temporary use on a Cairo surface.
344
    ///
345
    /// `DrawingCtx` maintains state using during the drawing process, and sometimes we
346
    /// would like to use that same state but on a different Cairo surface and context
347
    /// than the ones being used on `self`.  This function copies the `self` state into a
348
    /// new `DrawingCtx`, and ties the copied one to the supplied `cr`.
349
50778
    fn nested(&self, cr: cairo::Context) -> DrawingCtx {
350
50778
        let cr_stack = self.cr_stack.clone();
351

            
352
50778
        cr_stack.borrow_mut().push(self.cr.clone());
353

            
354
50778
        DrawingCtx {
355
50778
            session: self.session.clone(),
356
50778
            initial_viewport: self.initial_viewport.clone(),
357
50778
            dpi: self.dpi,
358
50778
            cr_stack,
359
50778
            cr,
360
50778
            user_language: self.user_language.clone(),
361
50778
            drawsub_stack: self.drawsub_stack.clone(),
362
50778
            svg_nesting: self.svg_nesting,
363
50778
            measuring: self.measuring,
364
50778
            testing: self.testing,
365
        }
366
50778
    }
367

            
368
7841246
    pub fn session(&self) -> &Session {
369
7841246
        &self.session
370
7841246
    }
371

            
372
33
    pub fn user_language(&self) -> &UserLanguage {
373
33
        &self.user_language
374
33
    }
375

            
376
1738
    pub fn toplevel_viewport(&self) -> Rect {
377
1738
        *self.initial_viewport.vbox
378
1738
    }
379

            
380
    /// Gets the transform that will be used on the target surface,
381
    /// whether using an isolated stacking context or not.
382
    ///
383
    /// This is only used in the text code, and we should probably try
384
    /// to remove it.
385
948998
    pub fn get_transform_for_stacking_ctx(
386
        &self,
387
        stacking_ctx: &StackingContext,
388
        clipping: bool,
389
    ) -> Result<ValidTransform, InternalRenderingError> {
390
949425
        if stacking_ctx.should_isolate() && !clipping {
391
427
            let affines = CompositingAffines::new(
392
427
                *self.get_transform(),
393
427
                self.initial_viewport.transform,
394
427
                self.cr_stack.borrow().len(),
395
427
            );
396

            
397
949425
            Ok(ValidTransform::try_from(affines.for_temporary_surface)?)
398
        } else {
399
948571
            Ok(self.get_transform())
400
        }
401
948998
    }
402

            
403
1119
    pub fn svg_nesting(&self) -> SvgNesting {
404
1119
        self.svg_nesting
405
1119
    }
406

            
407
1082
    pub fn is_measuring(&self) -> bool {
408
1082
        self.measuring
409
1082
    }
410

            
411
14
    pub fn is_testing(&self) -> bool {
412
14
        self.testing
413
14
    }
414

            
415
9457532
    pub fn get_transform(&self) -> ValidTransform {
416
9457532
        let t = Transform::from(self.cr.matrix());
417
9457532
        ValidTransform::try_from(t)
418
            .expect("Cairo should already have checked that its current transform is valid")
419
9457532
    }
420

            
421
3512906
    pub fn empty_bbox(&self) -> BoundingBox {
422
3512906
        BoundingBox::new().with_transform(*self.get_transform())
423
3512906
    }
424

            
425
740
    fn size_for_temporary_surface(&self) -> (i32, i32) {
426
740
        let rect = self.toplevel_viewport();
427

            
428
740
        let (viewport_width, viewport_height) = (rect.width(), rect.height());
429

            
430
740
        let (width, height) = self
431
            .initial_viewport
432
            .transform
433
            .transform_distance(viewport_width, viewport_height);
434

            
435
        // We need a size in whole pixels, so use ceil() to ensure the whole viewport fits
436
        // into the temporary surface.
437
740
        (width.ceil().abs() as i32, height.ceil().abs() as i32)
438
740
    }
439

            
440
360
    pub fn create_surface_for_toplevel_viewport(
441
        &self,
442
    ) -> Result<cairo::ImageSurface, InternalRenderingError> {
443
360
        let (w, h) = self.size_for_temporary_surface();
444

            
445
360
        Ok(cairo::ImageSurface::create(cairo::Format::ARgb32, w, h)?)
446
360
    }
447

            
448
380
    fn create_similar_surface_for_toplevel_viewport(
449
        &self,
450
        surface: &cairo::Surface,
451
    ) -> Result<cairo::Surface, InternalRenderingError> {
452
380
        let (w, h) = self.size_for_temporary_surface();
453

            
454
380
        Ok(cairo::Surface::create_similar(
455
            surface,
456
380
            cairo::Content::ColorAlpha,
457
            w,
458
            h,
459
        )?)
460
380
    }
461

            
462
    /// Creates a new coordinate space inside a viewport and sets a clipping rectangle.
463
    ///
464
    /// Note that this actually changes the `draw_ctx.cr`'s transformation to match
465
    /// the new coordinate space, but the old one is not restored after the
466
    /// result's `Viewport` is dropped.  Thus, this function must be called
467
    /// inside `with_saved_cr` or `draw_ctx.with_discrete_layer`.
468
1238
    pub fn push_new_viewport(
469
        &self,
470
        current_viewport: &Viewport,
471
        vbox: Option<ViewBox>,
472
        viewport_rect: Rect,
473
        preserve_aspect_ratio: AspectRatio,
474
        clip_mode: ClipMode,
475
    ) -> Option<Viewport> {
476
1238
        if let ClipMode::ClipToViewport = clip_mode {
477
49
            clip_to_rectangle(&self.cr, &viewport_rect);
478
        }
479

            
480
3714
        preserve_aspect_ratio
481
1238
            .viewport_to_viewbox_transform(vbox, &viewport_rect)
482
1240
            .unwrap_or_else(|_e| {
483
2
                match vbox {
484
                    None => unreachable!(
485
                        "viewport_to_viewbox_transform only returns errors when vbox != None"
486
                    ),
487
2
                    Some(v) => {
488
2
                        rsvg_log!(
489
2
                            self.session,
490
                            "ignoring viewBox ({}, {}, {}, {}) since it is not usable",
491
                            v.x0,
492
                            v.y0,
493
                            v.width(),
494
                            v.height()
495
                        );
496
                    }
497
                }
498
2
                None
499
2
            })
500
2471
            .map(|t| {
501
1233
                self.cr.transform(t.into());
502

            
503
1233
                Viewport {
504
1233
                    dpi: self.dpi,
505
1233
                    vbox: vbox.unwrap_or(current_viewport.vbox),
506
1233
                    transform: current_viewport.transform.post_transform(&t),
507
                }
508
1233
            })
509
1238
    }
510

            
511
2006161
    fn clip_to_node(
512
        &mut self,
513
        clip_node: &Option<Node>,
514
        acquired_nodes: &mut AcquiredNodes<'_>,
515
        viewport: &Viewport,
516
        bbox: &BoundingBox,
517
    ) -> Result<(), InternalRenderingError> {
518
2006161
        if clip_node.is_none() {
519
2006032
            return Ok(());
520
        }
521

            
522
129
        let node = clip_node.as_ref().unwrap();
523
129
        let units = borrow_element_as!(node, ClipPath).get_units();
524

            
525
129
        if let Ok(transform) = rect_to_transform(&bbox.rect, units) {
526
127
            let cascaded = CascadedValues::new_from_node(node);
527
127
            let values = cascaded.get();
528

            
529
43
            let node_transform = values.transform().post_transform(&transform);
530
2006204
            let transform_for_clip = ValidTransform::try_from(node_transform)?;
531

            
532
42
            let orig_transform = self.get_transform();
533
42
            self.cr.transform(transform_for_clip.into());
534

            
535
168
            for child in node.children().filter(|c| {
536
126
                c.is_element() && element_can_be_used_inside_clip_path(&c.borrow_element())
537
126
            }) {
538
42
                child.draw(
539
                    acquired_nodes,
540
42
                    &CascadedValues::clone_with_node(&cascaded, &child),
541
                    viewport,
542
                    self,
543
                    true,
544
42
                )?;
545
42
            }
546

            
547
42
            self.cr.clip();
548

            
549
42
            self.cr.set_matrix(orig_transform.into());
550
43
        }
551

            
552
44
        Ok(())
553
2006077
    }
554

            
555
60
    fn generate_cairo_mask(
556
        &mut self,
557
        mask_node: &Node,
558
        viewport: &Viewport,
559
        transform: Transform,
560
        bbox: &BoundingBox,
561
        acquired_nodes: &mut AcquiredNodes<'_>,
562
    ) -> Result<Option<cairo::ImageSurface>, InternalRenderingError> {
563
60
        if bbox.rect.is_none() {
564
            // The node being masked is empty / doesn't have a
565
            // bounding box, so there's nothing to mask!
566
1
            return Ok(None);
567
        }
568

            
569
59
        let _mask_acquired = match acquired_nodes.acquire_ref(mask_node) {
570
59
            Ok(n) => n,
571

            
572
            Err(AcquireError::CircularReference(_)) => {
573
                rsvg_log!(self.session, "circular reference in element {}", mask_node);
574
                return Ok(None);
575
            }
576

            
577
            _ => unreachable!(),
578
59
        };
579

            
580
59
        let mask_element = mask_node.borrow_element();
581
59
        let mask = borrow_element_as!(mask_node, Mask);
582

            
583
59
        let bbox_rect = bbox.rect.as_ref().unwrap();
584

            
585
59
        let cascaded = CascadedValues::new_from_node(mask_node);
586
59
        let values = cascaded.get();
587

            
588
59
        let mask_units = mask.get_units();
589

            
590
        let mask_rect = {
591
59
            let params = NormalizeParams::new(values, &viewport.with_units(mask_units));
592
59
            mask.get_rect(&params)
593
        };
594

            
595
59
        let mask_transform = values.transform().post_transform(&transform);
596
59
        let transform_for_mask = ValidTransform::try_from(mask_transform)?;
597

            
598
59
        let mask_content_surface = self.create_surface_for_toplevel_viewport()?;
599

            
600
        // Use a scope because mask_cr needs to release the
601
        // reference to the surface before we access the pixels
602
        {
603
59
            let mask_cr = cairo::Context::new(&mask_content_surface)?;
604
59
            mask_cr.set_matrix(transform_for_mask.into());
605

            
606
59
            let bbtransform = Transform::new_unchecked(
607
59
                bbox_rect.width(),
608
                0.0,
609
                0.0,
610
59
                bbox_rect.height(),
611
59
                bbox_rect.x0,
612
59
                bbox_rect.y0,
613
            );
614

            
615
59
            let clip_rect = if mask_units == CoordUnits::ObjectBoundingBox {
616
10
                bbtransform.transform_rect(&mask_rect)
617
            } else {
618
49
                mask_rect
619
            };
620

            
621
59
            clip_to_rectangle(&mask_cr, &clip_rect);
622

            
623
59
            if mask.get_content_units() == CoordUnits::ObjectBoundingBox {
624
5
                if bbox_rect.is_empty() {
625
1
                    return Ok(None);
626
                }
627
64
                mask_cr.transform(ValidTransform::try_from(bbtransform)?.into());
628
            }
629

            
630
58
            let mask_viewport = viewport.with_units(mask.get_content_units());
631

            
632
58
            let mut mask_draw_ctx = self.nested(mask_cr);
633

            
634
58
            let stacking_ctx = StackingContext::new(
635
58
                self.session(),
636
                acquired_nodes,
637
58
                &mask_element,
638
58
                Transform::identity(),
639
58
                None,
640
                values,
641
            );
642

            
643
58
            rsvg_log!(self.session, "(mask {}", mask_element);
644

            
645
58
            let res = mask_draw_ctx.with_discrete_layer(
646
                &stacking_ctx,
647
                acquired_nodes,
648
                &mask_viewport,
649
                false,
650
116
                &mut |an, dc| mask_node.draw_children(an, &cascaded, &mask_viewport, dc, false),
651
58
            );
652

            
653
58
            rsvg_log!(self.session, ")");
654

            
655
58
            res?;
656
59
        }
657

            
658
58
        let tmp = SharedImageSurface::wrap(mask_content_surface, SurfaceType::SRgb)?;
659

            
660
58
        let mask_result = match values.mask_type() {
661
57
            MaskType::Luminance => tmp.to_luminance_mask()?,
662
1
            MaskType::Alpha => tmp.extract_alpha(IRect::from_size(tmp.width(), tmp.height()))?,
663
        };
664

            
665
58
        let mask = mask_result.into_image_surface()?;
666

            
667
58
        Ok(Some(mask))
668
60
    }
669

            
670
2006101
    pub fn with_discrete_layer(
671
        &mut self,
672
        stacking_ctx: &StackingContext,
673
        acquired_nodes: &mut AcquiredNodes<'_>,
674
        viewport: &Viewport,
675
        clipping: bool,
676
        draw_fn: &mut dyn FnMut(
677
            &mut AcquiredNodes<'_>,
678
            &mut DrawingCtx,
679
        ) -> Result<BoundingBox, InternalRenderingError>,
680
    ) -> Result<BoundingBox, InternalRenderingError> {
681
2006101
        let stacking_ctx_transform = ValidTransform::try_from(stacking_ctx.transform)?;
682

            
683
2006100
        let orig_transform = self.get_transform();
684
2006100
        self.cr.transform(stacking_ctx_transform.into());
685

            
686
2006100
        let res = if clipping {
687
40
            draw_fn(acquired_nodes, self)
688
        } else {
689
4008587
            with_saved_cr(&self.cr.clone(), || {
690
2002527
                if let Some(ref link_target) = stacking_ctx.link_target {
691
2
                    self.link_tag_begin(link_target);
692
                }
693

            
694
2002527
                let Opacity(UnitInterval(opacity)) = stacking_ctx.opacity;
695

            
696
2002527
                let affine_at_start = self.get_transform();
697

            
698
2002527
                if let Some(rect) = stacking_ctx.clip_rect.as_ref() {
699
184
                    clip_to_rectangle(&self.cr, rect);
700
                }
701

            
702
                // Here we are clipping in user space, so the bbox doesn't matter
703
4005054
                self.clip_to_node(
704
2002527
                    &stacking_ctx.clip_in_user_space,
705
2002527
                    acquired_nodes,
706
2002527
                    viewport,
707
2002527
                    &self.empty_bbox(),
708
1
                )?;
709

            
710
2002526
                let should_isolate = stacking_ctx.should_isolate();
711

            
712
2003207
                let res = if should_isolate {
713
                    // Compute our assortment of affines
714

            
715
681
                    let affines = CompositingAffines::new(
716
681
                        *affine_at_start,
717
681
                        self.initial_viewport.transform,
718
681
                        self.cr_stack.borrow().len(),
719
681
                    );
720

            
721
                    // Create temporary surface and its cr
722

            
723
681
                    let cr = match stacking_ctx.filter {
724
380
                        None => cairo::Context::new(
725
760
                            &self
726
380
                                .create_similar_surface_for_toplevel_viewport(&self.cr.target())?,
727
380
                        )?,
728
                        Some(_) => {
729
301
                            cairo::Context::new(self.create_surface_for_toplevel_viewport()?)?
730
301
                        }
731
                    };
732

            
733
681
                    cr.set_matrix(ValidTransform::try_from(affines.for_temporary_surface)?.into());
734

            
735
681
                    let (source_surface, mut res, bbox) = {
736
681
                        let mut temporary_draw_ctx = self.nested(cr);
737

            
738
                        // Draw!
739

            
740
681
                        let res = draw_fn(acquired_nodes, &mut temporary_draw_ctx);
741

            
742
681
                        let bbox = if let Ok(ref bbox) = res {
743
681
                            *bbox
744
                        } else {
745
                            BoundingBox::new().with_transform(affines.for_temporary_surface)
746
                        };
747

            
748
1362
                        if let Some(ref filter) = stacking_ctx.filter {
749
301
                            let surface_to_filter = SharedImageSurface::copy_from_surface(
750
301
                                &cairo::ImageSurface::try_from(temporary_draw_ctx.cr.target())
751
                                    .unwrap(),
752
301
                            )?;
753

            
754
                            let stroke_paint_source =
755
301
                                Rc::new(filter.stroke_paint_source.to_user_space(
756
                                    &bbox.rect,
757
301
                                    viewport,
758
301
                                    &filter.normalize_values,
759
301
                                ));
760
                            let fill_paint_source =
761
301
                                Rc::new(filter.fill_paint_source.to_user_space(
762
                                    &bbox.rect,
763
301
                                    viewport,
764
301
                                    &filter.normalize_values,
765
301
                                ));
766

            
767
                            // Filter functions (like "blend()", not the <filter> element) require
768
                            // being resolved in userSpaceonUse units, since that is the default
769
                            // for primitive_units.  So, get the corresponding NormalizeParams
770
                            // here and pass them down.
771
301
                            let user_space_params = NormalizeParams::from_values(
772
301
                                &filter.normalize_values,
773
301
                                &viewport.with_units(CoordUnits::UserSpaceOnUse),
774
                            );
775

            
776
301
                            let filtered_surface = temporary_draw_ctx
777
                                .run_filters(
778
301
                                    viewport,
779
301
                                    surface_to_filter,
780
                                    filter,
781
301
                                    acquired_nodes,
782
301
                                    &stacking_ctx.element_name,
783
                                    &user_space_params,
784
301
                                    stroke_paint_source,
785
301
                                    fill_paint_source,
786
301
                                    bbox,
787
301
                                )?
788
                                .into_image_surface()?;
789

            
790
301
                            let generic_surface: &cairo::Surface = &filtered_surface; // deref to Surface
791

            
792
301
                            (generic_surface.clone(), res, bbox)
793
301
                        } else {
794
380
                            (temporary_draw_ctx.cr.target(), res, bbox)
795
                        }
796
681
                    };
797

            
798
                    // Set temporary surface as source
799

            
800
1362
                    self.cr
801
681
                        .set_matrix(ValidTransform::try_from(affines.compositing)?.into());
802
681
                    self.cr.set_source_surface(&source_surface, 0.0, 0.0)?;
803

            
804
                    // Clip
805

            
806
1362
                    self.cr.set_matrix(
807
681
                        ValidTransform::try_from(affines.outside_temporary_surface)?.into(),
808
                    );
809
1362
                    self.clip_to_node(
810
681
                        &stacking_ctx.clip_in_object_space,
811
681
                        acquired_nodes,
812
681
                        viewport,
813
                        &bbox,
814
                    )?;
815

            
816
                    // Mask
817

            
818
681
                    if let Some(ref mask_node) = stacking_ctx.mask {
819
120
                        res = res.and_then(|bbox| {
820
240
                            self.generate_cairo_mask(
821
60
                                mask_node,
822
60
                                viewport,
823
60
                                affines.for_temporary_surface,
824
                                &bbox,
825
60
                                acquired_nodes,
826
                            )
827
120
                            .and_then(|mask_surf| {
828
60
                                if let Some(surf) = mask_surf {
829
58
                                    self.cr.push_group();
830

            
831
116
                                    self.cr.set_matrix(
832
58
                                        ValidTransform::try_from(affines.compositing)?.into(),
833
                                    );
834
58
                                    self.cr.mask_surface(&surf, 0.0, 0.0)?;
835

            
836
118
                                    Ok(self.cr.pop_group_to_source()?)
837
58
                                } else {
838
2
                                    Ok(())
839
                                }
840
60
                            })
841
120
                            .map(|_: ()| bbox)
842
60
                        });
843
                    }
844

            
845
                    {
846
                        // Composite the temporary surface
847

            
848
1362
                        self.cr
849
681
                            .set_matrix(ValidTransform::try_from(affines.compositing)?.into());
850
681
                        self.cr.set_operator(stacking_ctx.mix_blend_mode.into());
851

            
852
681
                        if opacity < 1.0 {
853
313
                            self.cr.paint_with_alpha(opacity)?;
854
                        } else {
855
368
                            self.cr.paint()?;
856
                        }
857
                    }
858

            
859
681
                    self.cr.set_matrix(affine_at_start.into());
860
681
                    res
861
681
                } else {
862
2001845
                    draw_fn(acquired_nodes, self)
863
                };
864

            
865
2002526
                if stacking_ctx.link_target.is_some() {
866
2
                    self.link_tag_end();
867
                }
868

            
869
2002526
                res
870
2002527
            })
871
2006060
        };
872

            
873
2006100
        self.cr.set_matrix(orig_transform.into());
874
2006100
        res
875
2006101
    }
876

            
877
    /// Run the drawing function with the specified opacity
878
50026
    fn with_alpha(
879
        &mut self,
880
        opacity: UnitInterval,
881
        draw_fn: &mut dyn FnMut(&mut DrawingCtx) -> Result<BoundingBox, InternalRenderingError>,
882
    ) -> Result<BoundingBox, InternalRenderingError> {
883
50026
        let res;
884
50026
        let UnitInterval(o) = opacity;
885
100051
        if o < 1.0 {
886
1
            self.cr.push_group();
887
1
            res = draw_fn(self);
888
1
            self.cr.pop_group_to_source()?;
889
50027
            self.cr.paint_with_alpha(o)?;
890
        } else {
891
50025
            res = draw_fn(self);
892
        }
893

            
894
50026
        res
895
50026
    }
896

            
897
    /// Start a Cairo tag for PDF links
898
6
    fn link_tag_begin(&mut self, link_target: &str) {
899
6
        let attributes = format!("uri='{}'", escape_link_target(link_target));
900

            
901
6
        let cr = self.cr.clone();
902
6
        cr.tag_begin(CAIRO_TAG_LINK, &attributes);
903
6
    }
904

            
905
    /// End a Cairo tag for PDF links
906
6
    fn link_tag_end(&mut self) {
907
6
        self.cr.tag_end(CAIRO_TAG_LINK);
908
6
    }
909

            
910
301
    fn run_filters(
911
        &mut self,
912
        viewport: &Viewport,
913
        surface_to_filter: SharedImageSurface,
914
        filter: &Filter,
915
        acquired_nodes: &mut AcquiredNodes<'_>,
916
        node_name: &str,
917
        user_space_params: &NormalizeParams,
918
        stroke_paint_source: Rc<UserSpacePaintSource>,
919
        fill_paint_source: Rc<UserSpacePaintSource>,
920
        node_bbox: BoundingBox,
921
    ) -> Result<SharedImageSurface, InternalRenderingError> {
922
301
        let session = self.session();
923

            
924
        // We try to convert each item in the filter_list to a FilterSpec.
925
        //
926
        // However, the spec mentions, "If the filter references a non-existent object or
927
        // the referenced object is not a filter element, then the whole filter chain is
928
        // ignored." - https://www.w3.org/TR/filter-effects/#FilterProperty
929
        //
930
        // So, run through the filter_list and collect into a Result<Vec<FilterSpec>>.
931
        // This will return an Err if any of the conversions failed.
932
301
        let filter_specs = filter
933
            .filter_list
934
            .iter()
935
605
            .map(|filter_value| {
936
304
                filter_value.to_filter_spec(
937
304
                    acquired_nodes,
938
304
                    user_space_params,
939
304
                    filter.current_color,
940
304
                    viewport,
941
304
                    session,
942
304
                    node_name,
943
                )
944
304
            })
945
            .collect::<Result<Vec<FilterSpec>, _>>();
946

            
947
301
        match filter_specs {
948
298
            Ok(specs) => {
949
                // Start with the surface_to_filter, and apply each filter spec in turn;
950
                // the final result is our return value.
951
597
                specs.iter().try_fold(surface_to_filter, |surface, spec| {
952
299
                    filters::render(
953
299
                        spec,
954
299
                        stroke_paint_source.clone(),
955
299
                        fill_paint_source.clone(),
956
299
                        surface,
957
299
                        acquired_nodes,
958
299
                        self,
959
299
                        *self.get_transform(),
960
299
                        node_bbox,
961
299
                    )
962
299
                })
963
298
            }
964

            
965
3
            Err(e) => {
966
3
                rsvg_log!(
967
3
                    self.session,
968
                    "not rendering filter list on node {} because it was in error: {}",
969
                    node_name,
970
                    e
971
                );
972
                // just return the original surface without filtering it
973
3
                Ok(surface_to_filter)
974
            }
975
        }
976
301
    }
977

            
978
163
    fn set_gradient(&mut self, gradient: &UserSpaceGradient) -> Result<(), InternalRenderingError> {
979
163
        let g = match gradient.variant {
980
128
            GradientVariant::Linear { x1, y1, x2, y2 } => {
981
128
                cairo::Gradient::clone(&cairo::LinearGradient::new(x1, y1, x2, y2))
982
128
            }
983

            
984
            GradientVariant::Radial {
985
35
                cx,
986
35
                cy,
987
35
                r,
988
35
                fx,
989
35
                fy,
990
35
                fr,
991
35
            } => cairo::Gradient::clone(&cairo::RadialGradient::new(fx, fy, fr, cx, cy, r)),
992
        };
993

            
994
163
        g.set_matrix(ValidTransform::try_from(gradient.transform)?.into());
995
163
        g.set_extend(cairo::Extend::from(gradient.spread));
996

            
997
618
        for stop in &gradient.stops {
998
455
            let UnitInterval(stop_offset) = stop.offset;
999

            
455
            let rgba = color_to_rgba(&stop.color);
455
            g.add_color_stop_rgba(
                stop_offset,
455
                f64::from(rgba.red.unwrap_or(0)) / 255.0,
455
                f64::from(rgba.green.unwrap_or(0)) / 255.0,
455
                f64::from(rgba.blue.unwrap_or(0)) / 255.0,
455
                f64::from(rgba.alpha.unwrap_or(0.0)),
            );
        }
326
        Ok(self.cr.set_source(&g)?)
163
    }
500033
    fn set_pattern(
        &mut self,
        pattern: &UserSpacePattern,
        acquired_nodes: &mut AcquiredNodes<'_>,
    ) -> Result<bool, InternalRenderingError> {
        // Bail out early if the pattern has zero size, per the spec
500033
        if approx_eq!(f64, pattern.width, 0.0) || approx_eq!(f64, pattern.height, 0.0) {
9
            return Ok(false);
        }
        // Bail out early if this pattern has a circular reference
500024
        let pattern_node_acquired = match pattern.acquire_pattern_node(acquired_nodes) {
500024
            Ok(n) => n,
            Err(AcquireError::CircularReference(ref node)) => {
                rsvg_log!(self.session, "circular reference in element {}", node);
                return Ok(false);
            }
            _ => unreachable!(),
500024
        };
500024
        let pattern_node = pattern_node_acquired.get();
500024
        let taffine = self.get_transform().pre_transform(&pattern.transform);
500024
        let mut scwscale = (taffine.xx.powi(2) + taffine.xy.powi(2)).sqrt();
500024
        let mut schscale = (taffine.yx.powi(2) + taffine.yy.powi(2)).sqrt();
500024
        let pw: i32 = (pattern.width * scwscale) as i32;
500024
        let ph: i32 = (pattern.height * schscale) as i32;
500024
        if pw < 1 || ph < 1 {
449998
            return Ok(false);
        }
50026
        scwscale = f64::from(pw) / pattern.width;
50026
        schscale = f64::from(ph) / pattern.height;
        // Apply the pattern transform
100029
        let (affine, caffine) = if scwscale.approx_eq_cairo(1.0) && schscale.approx_eq_cairo(1.0) {
23
            (pattern.coord_transform, pattern.content_transform)
        } else {
50003
            (
100006
                pattern
                    .coord_transform
50003
                    .pre_scale(1.0 / scwscale, 1.0 / schscale),
50003
                pattern.content_transform.post_scale(scwscale, schscale),
            )
        };
        // Draw to another surface
50026
        let surface = self
            .cr
            .target()
100052
            .create_similar(cairo::Content::ColorAlpha, pw, ph)?;
50026
        let cr_pattern = cairo::Context::new(&surface)?;
        // Set up transformations to be determined by the contents units
50026
        let transform = ValidTransform::try_from(caffine)?;
50026
        cr_pattern.set_matrix(transform.into());
        // Draw everything
        {
50026
            let mut pattern_draw_ctx = self.nested(cr_pattern);
50026
            let pattern_viewport = Viewport {
50026
                dpi: self.dpi,
50026
                vbox: ViewBox::from(Rect::from_size(pattern.width, pattern.height)),
50026
                transform: *transform,
            };
50026
            pattern_draw_ctx
100052
                .with_alpha(pattern.opacity, &mut |dc| {
50026
                    let pattern_cascaded = CascadedValues::new_from_node(pattern_node);
50026
                    let pattern_values = pattern_cascaded.get();
50026
                    let elt = pattern_node.borrow_element();
50026
                    let stacking_ctx = StackingContext::new(
50026
                        self.session(),
50026
                        acquired_nodes,
50026
                        &elt,
50026
                        Transform::identity(),
50026
                        None,
                        pattern_values,
                    );
50026
                    dc.with_discrete_layer(
                        &stacking_ctx,
50026
                        acquired_nodes,
50026
                        &pattern_viewport,
                        false,
100052
                        &mut |an, dc| {
100052
                            pattern_node.draw_children(
                                an,
50026
                                &pattern_cascaded,
50026
                                &pattern_viewport,
                                dc,
                                false,
                            )
50026
                        },
                    )
50026
                })
50026
                .map(|_| ())?;
50026
        }
        // Set the final surface as a Cairo pattern into the Cairo context
50026
        let pattern = cairo::SurfacePattern::create(&surface);
50026
        if let Some(m) = affine.invert() {
50026
            pattern.set_matrix(ValidTransform::try_from(m)?.into());
50026
            pattern.set_extend(cairo::Extend::Repeat);
50026
            pattern.set_filter(cairo::Filter::Best);
550059
            self.cr.set_source(&pattern)?;
        }
50026
        Ok(true)
500033
    }
1899149
    fn set_paint_source(
        &mut self,
        paint_source: &UserSpacePaintSource,
        acquired_nodes: &mut AcquiredNodes<'_>,
    ) -> Result<bool, InternalRenderingError> {
1899149
        match *paint_source {
163
            UserSpacePaintSource::Gradient(ref gradient, _c) => {
1899149
                self.set_gradient(gradient)?;
163
                Ok(true)
163
            }
500033
            UserSpacePaintSource::Pattern(ref pattern, ref c) => {
500033
                if self.set_pattern(pattern, acquired_nodes)? {
50026
                    Ok(true)
450007
                } else if let Some(c) = c {
9
                    set_source_color_on_cairo(&self.cr, c);
9
                    Ok(true)
                } else {
449998
                    Ok(false)
                }
            }
949673
            UserSpacePaintSource::SolidColor(ref c) => {
949673
                set_source_color_on_cairo(&self.cr, c);
949673
                Ok(true)
949673
            }
449280
            UserSpacePaintSource::None => Ok(false),
        }
1899149
    }
    /// Computes and returns a surface corresponding to the given paint server.
13
    pub fn get_paint_source_surface(
        &mut self,
        width: i32,
        height: i32,
        acquired_nodes: &mut AcquiredNodes<'_>,
        paint_source: &UserSpacePaintSource,
    ) -> Result<SharedImageSurface, InternalRenderingError> {
13
        let mut surface = ExclusiveImageSurface::new(width, height, SurfaceType::SRgb)?;
26
        surface.draw(&mut |cr| {
13
            let mut temporary_draw_ctx = self.nested(cr);
            // FIXME: we are ignoring any error
            let had_paint_server =
13
                temporary_draw_ctx.set_paint_source(paint_source, acquired_nodes)?;
13
            if had_paint_server {
26
                temporary_draw_ctx.cr.paint()?;
            }
13
            Ok(())
13
        })?;
13
        Ok(surface.share()?)
13
    }
948589
    fn stroke(
        &mut self,
        cr: &cairo::Context,
        acquired_nodes: &mut AcquiredNodes<'_>,
        paint_source: &UserSpacePaintSource,
    ) -> Result<(), InternalRenderingError> {
948589
        let had_paint_server = self.set_paint_source(paint_source, acquired_nodes)?;
948589
        if had_paint_server {
1449736
            cr.stroke_preserve()?;
        }
948589
        Ok(())
948589
    }
948306
    fn fill(
        &mut self,
        cr: &cairo::Context,
        acquired_nodes: &mut AcquiredNodes<'_>,
        paint_source: &UserSpacePaintSource,
    ) -> Result<(), InternalRenderingError> {
948306
        let had_paint_server = self.set_paint_source(paint_source, acquired_nodes)?;
948306
        if had_paint_server {
1446570
            cr.fill_preserve()?;
        }
948306
        Ok(())
948306
    }
949123
    pub fn compute_path_extents(
        &self,
        path: &Path,
    ) -> Result<Option<Rect>, InternalRenderingError> {
949123
        if path.is_empty() {
18
            return Ok(None);
        }
949105
        let surface = cairo::RecordingSurface::create(cairo::Content::ColorAlpha, None)?;
949105
        let cr = cairo::Context::new(&surface)?;
1898168
        path.to_cairo(&cr, false)?;
948218
        let (x0, y0, x1, y1) = cr.path_extents()?;
948227
        Ok(Some(Rect::new(x0, y0, x1, y1)))
948827
    }
949044
    pub fn draw_layer(
        &mut self,
        layer: &Layer,
        acquired_nodes: &mut AcquiredNodes<'_>,
        clipping: bool,
        viewport: &Viewport,
    ) -> Result<BoundingBox, InternalRenderingError> {
949044
        match &layer.kind {
1896044
            LayerKind::Shape(shape) => self.draw_shape(
948022
                shape,
                &layer.stacking_ctx,
                acquired_nodes,
                clipping,
                viewport,
            ),
1828
            LayerKind::Text(text) => self.draw_text(
914
                text,
                &layer.stacking_ctx,
                acquired_nodes,
                clipping,
                viewport,
            ),
216
            LayerKind::Image(image) => self.draw_image(
108
                image,
                &layer.stacking_ctx,
                acquired_nodes,
                clipping,
                viewport,
            ),
        }
949044
    }
947719
    fn draw_shape(
        &mut self,
        shape: &Shape,
        stacking_ctx: &StackingContext,
        acquired_nodes: &mut AcquiredNodes<'_>,
        clipping: bool,
        viewport: &Viewport,
    ) -> Result<BoundingBox, InternalRenderingError> {
947719
        if shape.extents.is_none() {
18
            return Ok(self.empty_bbox());
        }
947701
        self.with_discrete_layer(
            stacking_ctx,
            acquired_nodes,
            viewport,
947701
            clipping,
1910332
            &mut |an, dc| {
962631
                let cr = dc.cr.clone();
962631
                let transform = dc.get_transform_for_stacking_ctx(stacking_ctx, clipping)?;
                let mut path_helper =
948721
                    PathHelper::new(&cr, transform, &shape.path, shape.stroke.line_cap);
948289
                if clipping {
39
                    if shape.is_visible {
38
                        cr.set_fill_rule(cairo::FillRule::from(shape.clip_rule));
38
                        path_helper.set()?;
                    }
39
                    return Ok(dc.empty_bbox());
                }
948250
                cr.set_antialias(cairo::Antialias::from(shape.shape_rendering));
948799
                setup_cr_for_stroke(&cr, &shape.stroke);
947805
                cr.set_fill_rule(cairo::FillRule::from(shape.fill_rule));
948359
                path_helper.set()?;
948132
                let bbox = compute_stroke_and_fill_box(
                    &cr,
948562
                    &shape.stroke,
948562
                    &shape.stroke_paint,
948562
                    &dc.initial_viewport,
                )?;
948255
                if shape.is_visible {
3793626
                    for &target in &shape.paint_order.targets {
                        // fill and stroke operations will preserve the path.
                        // markers operation will clear the path.
2844832
                        match target {
                            PaintTarget::Fill => {
948039
                                path_helper.set()?;
948010
                                dc.fill(&cr, an, &shape.fill_paint)?;
                            }
                            PaintTarget::Stroke => {
948270
                                path_helper.set()?;
948241
                                let backup_matrix = if shape.stroke.non_scaling {
1
                                    let matrix = cr.matrix();
1
                                    cr.set_matrix(
1
                                        ValidTransform::try_from(dc.initial_viewport.transform)?
                                            .into(),
                                    );
1
                                    Some(matrix)
                                } else {
948239
                                    None
                                };
1910871
                                dc.stroke(&cr, an, &shape.stroke_paint)?;
948463
                                if let Some(matrix) = backup_matrix {
1
                                    cr.set_matrix(matrix);
                                }
                            }
948703
                            PaintTarget::Markers => {
948523
                                path_helper.unset();
948490
                                marker::render_markers_for_shape(
949010
                                    shape, viewport, dc, an, clipping,
                                )?;
                            }
                        }
                    }
                }
953279
                path_helper.unset();
948608
                Ok(bbox)
948647
            },
        )
947719
    }
108
    fn paint_surface(
        &mut self,
        surface: &SharedImageSurface,
        width: f64,
        height: f64,
        image_rendering: ImageRendering,
    ) -> Result<(), cairo::Error> {
108
        let cr = self.cr.clone();
        // We need to set extend appropriately, so can't use cr.set_source_surface().
        //
        // If extend is left at its default value (None), then bilinear scaling uses
        // transparency outside of the image producing incorrect results.
        // For example, in svg1.1/filters-blend-01-b.svgthere's a completely
        // opaque 100×1 image of a gradient scaled to 100×98 which ends up
        // transparent almost everywhere without this fix (which it shouldn't).
108
        let ptn = surface.to_cairo_pattern();
108
        ptn.set_extend(cairo::Extend::Pad);
108
        let interpolation = Interpolation::from(image_rendering);
108
        ptn.set_filter(cairo::Filter::from(interpolation));
216
        cr.set_source(&ptn)?;
        // Clip is needed due to extend being set to pad.
108
        clip_to_rectangle(&cr, &Rect::from_size(width, height));
108
        cr.paint()
108
    }
108
    fn draw_image(
        &mut self,
        image: &Image,
        stacking_ctx: &StackingContext,
        acquired_nodes: &mut AcquiredNodes<'_>,
        clipping: bool,
        viewport: &Viewport,
    ) -> Result<BoundingBox, InternalRenderingError> {
108
        let image_width = image.surface.width();
108
        let image_height = image.surface.height();
108
        if clipping || image.rect.is_empty() || image_width == 0 || image_height == 0 {
            return Ok(self.empty_bbox());
        }
108
        let image_width = f64::from(image_width);
108
        let image_height = f64::from(image_height);
108
        let vbox = ViewBox::from(Rect::from_size(image_width, image_height));
216
        let clip_mode = if !(image.overflow == Overflow::Auto
108
            || image.overflow == Overflow::Visible)
108
            && image.aspect.is_slice()
        {
12
            ClipMode::ClipToViewport
        } else {
96
            ClipMode::NoClip
        };
        // The bounding box for <image> is decided by the values of the image's x, y, w, h
        // and not by the final computed image bounds.
108
        let bounds = self.empty_bbox().with_rect(image.rect);
108
        if image.is_visible {
108
            self.with_discrete_layer(
                stacking_ctx,
                acquired_nodes,
                viewport, // FIXME: should this be the push_new_viewport below?
                clipping,
216
                &mut |_an, dc| {
216
                    with_saved_cr(&dc.cr.clone(), || {
216
                        if let Some(_params) = dc.push_new_viewport(
108
                            viewport,
108
                            Some(vbox),
108
                            image.rect,
108
                            image.aspect,
108
                            clip_mode,
                        ) {
216
                            dc.paint_surface(
108
                                &image.surface,
108
                                image_width,
108
                                image_height,
108
                                image.image_rendering,
                            )?;
                        }
108
                        Ok(bounds)
108
                    })
108
                },
            )
        } else {
            Ok(bounds)
        }
108
    }
1037
    fn draw_text_span(
        &mut self,
        span: &TextSpan,
        acquired_nodes: &mut AcquiredNodes<'_>,
        clipping: bool,
    ) -> Result<BoundingBox, InternalRenderingError> {
1037
        let path = pango_layout_to_path(span.x, span.y, &span.layout, span.gravity)?;
1037
        if path.is_empty() {
            // Empty strings, or only-whitespace text, get turned into empty paths.
            // In that case, we really want to return "no bounds" rather than an
            // empty rectangle.
59
            return Ok(self.empty_bbox());
        }
        // #851 - We can't just render all text as paths for PDF; it
        // needs the actual text content so text is selectable by PDF
        // viewers.
978
        let can_use_text_as_path = self.cr.target().type_() != cairo::SurfaceType::Pdf;
1956
        with_saved_cr(&self.cr.clone(), || {
1956
            self.cr
978
                .set_antialias(cairo::Antialias::from(span.text_rendering));
978
            setup_cr_for_stroke(&self.cr, &span.stroke);
978
            if clipping {
1
                path.to_cairo(&self.cr, false)?;
1
                return Ok(self.empty_bbox());
            }
977
            path.to_cairo(&self.cr, false)?;
977
            let bbox = compute_stroke_and_fill_box(
977
                &self.cr,
977
                &span.stroke,
977
                &span.stroke_paint,
977
                &self.initial_viewport,
            )?;
977
            self.cr.new_path();
977
            if span.is_visible {
967
                if let Some(ref link_target) = span.link_target {
4
                    self.link_tag_begin(link_target);
                }
3868
                for &target in &span.paint_order.targets {
2901
                    match target {
                        PaintTarget::Fill => {
                            let had_paint_server =
966
                                self.set_paint_source(&span.fill_paint, acquired_nodes)?;
966
                            if had_paint_server {
964
                                if can_use_text_as_path {
960
                                    path.to_cairo(&self.cr, false)?;
960
                                    self.cr.fill()?;
960
                                    self.cr.new_path();
                                } else {
4
                                    self.cr.move_to(span.x, span.y);
4
                                    let matrix = self.cr.matrix();
4
                                    let rotation_from_gravity = span.gravity.to_rotation();
4
                                    if !rotation_from_gravity.approx_eq_cairo(0.0) {
                                        self.cr.rotate(-rotation_from_gravity);
                                    }
4
                                    pangocairo::functions::update_layout(&self.cr, &span.layout);
4
                                    pangocairo::functions::show_layout(&self.cr, &span.layout);
4
                                    self.cr.set_matrix(matrix);
                                }
                            }
                        }
                        PaintTarget::Stroke => {
                            let had_paint_server =
967
                                self.set_paint_source(&span.stroke_paint, acquired_nodes)?;
967
                            if had_paint_server {
35
                                path.to_cairo(&self.cr, false)?;
35
                                self.cr.stroke()?;
35
                                self.cr.new_path();
                            }
                        }
                        PaintTarget::Markers => {}
                    }
                }
967
                if span.link_target.is_some() {
4
                    self.link_tag_end();
                }
            }
977
            Ok(bbox)
978
        })
1037
    }
914
    fn draw_text(
        &mut self,
        text: &Text,
        stacking_ctx: &StackingContext,
        acquired_nodes: &mut AcquiredNodes<'_>,
        clipping: bool,
        viewport: &Viewport,
    ) -> Result<BoundingBox, InternalRenderingError> {
914
        self.with_discrete_layer(
            stacking_ctx,
            acquired_nodes,
            viewport,
914
            clipping,
1828
            &mut |an, dc| {
914
                let mut bbox = dc.empty_bbox();
1951
                for span in &text.spans {
1037
                    let span_bbox = dc.draw_text_span(span, an, clipping)?;
1037
                    bbox.insert(&span_bbox);
                }
914
                Ok(bbox)
914
            },
        )
914
    }
10
    pub fn get_snapshot(
        &self,
        width: i32,
        height: i32,
    ) -> Result<SharedImageSurface, InternalRenderingError> {
        // TODO: as far as I can tell this should not render elements past the last (topmost) one
        // with enable-background: new (because technically we shouldn't have been caching them).
        // Right now there are no enable-background checks whatsoever.
        //
        // Addendum: SVG 2 has deprecated the enable-background property, and replaced it with an
        // "isolation" property from the CSS Compositing and Blending spec.
        //
        // Deprecation:
        //   https://www.w3.org/TR/filter-effects-1/#AccessBackgroundImage
        //
        // BackgroundImage, BackgroundAlpha in the "in" attribute of filter primitives:
        //   https://www.w3.org/TR/filter-effects-1/#attr-valuedef-in-backgroundimage
        //
        // CSS Compositing and Blending, "isolation" property:
        //   https://www.w3.org/TR/compositing-1/#isolation
10
        let mut surface = ExclusiveImageSurface::new(width, height, SurfaceType::SRgb)?;
20
        surface.draw(&mut |cr| {
            // TODO: apparently DrawingCtx.cr_stack is just a way to store pairs of
            // (surface, transform).  Can we turn it into a DrawingCtx.surface_stack
            // instead?  See what CSS isolation would like to call that; are the pairs just
            // stacking contexts instead, or the result of rendering stacking contexts?
22
            for (depth, draw) in self.cr_stack.borrow().iter().enumerate() {
12
                let affines = CompositingAffines::new(
12
                    Transform::from(draw.matrix()),
12
                    self.initial_viewport.transform,
                    depth,
                );
12
                cr.set_matrix(ValidTransform::try_from(affines.for_snapshot)?.into());
12
                cr.set_source_surface(&draw.target(), 0.0, 0.0)?;
22
                cr.paint()?;
            }
10
            Ok(())
10
        })?;
10
        Ok(surface.share()?)
10
    }
25
    pub fn draw_node_to_surface(
        &mut self,
        node: &Node,
        acquired_nodes: &mut AcquiredNodes<'_>,
        cascaded: &CascadedValues<'_>,
        affine: Transform,
        width: i32,
        height: i32,
    ) -> Result<SharedImageSurface, InternalRenderingError> {
25
        let surface = cairo::ImageSurface::create(cairo::Format::ARgb32, width, height)?;
25
        let save_cr = self.cr.clone();
        {
25
            let cr = cairo::Context::new(&surface)?;
50
            cr.set_matrix(ValidTransform::try_from(affine)?.into());
25
            self.cr = cr;
25
            let viewport = Viewport {
25
                dpi: self.dpi,
                transform: affine,
25
                vbox: ViewBox::from(Rect::from_size(f64::from(width), f64::from(height))),
            };
25
            let _ = self.draw_node_from_stack(node, acquired_nodes, cascaded, &viewport, false)?;
25
        }
25
        self.cr = save_cr;
25
        Ok(SharedImageSurface::wrap(surface, SurfaceType::SRgb)?)
25
    }
1460358
    pub fn draw_node_from_stack(
        &mut self,
        node: &Node,
        acquired_nodes: &mut AcquiredNodes<'_>,
        cascaded: &CascadedValues<'_>,
        viewport: &Viewport,
        clipping: bool,
    ) -> Result<BoundingBox, InternalRenderingError> {
1460358
        let stack_top = self.drawsub_stack.pop();
1460358
        let draw = if let Some(ref top) = stack_top {
3116
            top == node
        } else {
1457242
            true
        };
1460555
        let res = if draw {
1458465
            node.draw(acquired_nodes, cascaded, viewport, self, clipping)
        } else {
1045
            Ok(self.empty_bbox())
        };
1458662
        if let Some(top) = stack_top {
2270
            self.drawsub_stack.push(top);
        }
1458662
        res
1458662
    }
500185
    pub fn draw_from_use_node(
        &mut self,
        node: &Node,
        acquired_nodes: &mut AcquiredNodes<'_>,
        values: &ComputedValues,
        use_rect: Rect,
        link: &NodeId,
        clipping: bool,
        viewport: &Viewport,
        fill_paint: Rc<PaintSource>,
        stroke_paint: Rc<PaintSource>,
    ) -> Result<BoundingBox, InternalRenderingError> {
        // <use> is an element that is used directly, unlike
        // <pattern>, which is used through a fill="url(#...)"
        // reference.  However, <use> will always reference another
        // element, potentially itself or an ancestor of itself (or
        // another <use> which references the first one, etc.).  So,
        // we acquire the <use> element itself so that circular
        // references can be caught.
500185
        let _self_acquired = match acquired_nodes.acquire_ref(node) {
500161
            Ok(n) => n,
            Err(AcquireError::CircularReference(_)) => {
4
                rsvg_log!(self.session, "circular reference in element {}", node);
4
                return Ok(self.empty_bbox());
            }
            _ => unreachable!(),
500165
        };
500161
        let acquired = match acquired_nodes.acquire(link) {
500178
            Ok(acquired) => acquired,
            Err(AcquireError::CircularReference(node)) => {
                rsvg_log!(self.session, "circular reference in element {}", node);
                return Ok(self.empty_bbox());
            }
            Err(AcquireError::MaxReferencesExceeded) => {
1
                return Err(InternalRenderingError::LimitExceeded(
1
                    ImplementationLimit::TooManyReferencedElements,
                ));
            }
            Err(AcquireError::InvalidLinkType(_)) => unreachable!(),
4
            Err(AcquireError::LinkNotFound(node_id)) => {
4
                rsvg_log!(
4
                    self.session,
                    "element {} references nonexistent \"{}\"",
                    node,
                    node_id
                );
4
                return Ok(self.empty_bbox());
4
            }
5
        };
        // width or height set to 0 disables rendering of the element
        // https://www.w3.org/TR/SVG/struct.html#UseElementWidthAttribute
500178
        if use_rect.is_empty() {
            return Ok(self.empty_bbox());
        }
500156
        let child = acquired.get();
500156
        if clipping && !element_can_be_used_inside_use_inside_clip_path(&child.borrow_element()) {
1
            return Ok(self.empty_bbox());
        }
500155
        let orig_transform = self.get_transform();
1000310
        self.cr
1000340
            .transform(ValidTransform::try_from(values.transform())?.into());
500155
        let use_element = node.borrow_element();
500155
        let defines_a_viewport = if is_element_of_type!(child, Symbol) {
9
            let symbol = borrow_element_as!(child, Symbol);
9
            Some((symbol.get_viewbox(), symbol.get_preserve_aspect_ratio()))
500155
        } else if is_element_of_type!(child, Svg) {
1
            let svg = borrow_element_as!(child, Svg);
1
            Some((svg.get_viewbox(), svg.get_preserve_aspect_ratio()))
1
        } else {
500145
            None
        };
500155
        let res = if let Some((viewbox, preserve_aspect_ratio)) = defines_a_viewport {
            // <symbol> and <svg> define a viewport, as described in the specification:
            // https://www.w3.org/TR/SVG2/struct.html#UseElement
            // https://gitlab.gnome.org/GNOME/librsvg/-/issues/875#note_1482705
10
            let elt = child.borrow_element();
10
            let child_values = elt.get_computed_values();
            // FIXME: do we need to look at preserveAspectRatio.slice, like in draw_image()?
10
            let clip_mode = if !child_values.is_overflow() {
5
                ClipMode::ClipToViewport
            } else {
5
                ClipMode::NoClip
            };
10
            let stacking_ctx = StackingContext::new(
10
                self.session(),
                acquired_nodes,
10
                &use_element,
10
                Transform::identity(),
10
                None,
                values,
            );
10
            self.with_discrete_layer(
                &stacking_ctx,
                acquired_nodes,
                viewport, // FIXME: should this be the child_viewport from below?
10
                clipping,
20
                &mut |an, dc| {
20
                    if let Some(child_viewport) = dc.push_new_viewport(
10
                        viewport,
10
                        viewbox,
10
                        use_rect,
10
                        preserve_aspect_ratio,
10
                        clip_mode,
                    ) {
20
                        child.draw_children(
                            an,
10
                            &CascadedValues::new_from_values(
10
                                child,
10
                                values,
10
                                Some(fill_paint.clone()),
10
                                Some(stroke_paint.clone()),
10
                            ),
                            &child_viewport,
                            dc,
10
                            clipping,
                        )
10
                    } else {
                        Ok(dc.empty_bbox())
                    }
10
                },
10
            )
10
        } else {
            // otherwise the referenced node is not a <symbol>; process it generically
500145
            let stacking_ctx = StackingContext::new(
500145
                self.session(),
                acquired_nodes,
500145
                &use_element,
500145
                Transform::new_translate(use_rect.x0, use_rect.y0),
500145
                None,
                values,
            );
500145
            self.with_discrete_layer(
                &stacking_ctx,
                acquired_nodes,
                viewport,
500145
                clipping,
1000290
                &mut |an, dc| {
1000290
                    child.draw(
                        an,
500145
                        &CascadedValues::new_from_values(
500145
                            child,
500145
                            values,
500145
                            Some(fill_paint.clone()),
500145
                            Some(stroke_paint.clone()),
500145
                        ),
500145
                        viewport,
                        dc,
500145
                        clipping,
                    )
500145
                },
500165
            )
500145
        };
500155
        self.cr.set_matrix(orig_transform.into());
1000293
        if let Ok(bbox) = res {
500138
            let mut res_bbox = BoundingBox::new().with_transform(*orig_transform);
500138
            res_bbox.insert(&bbox);
500138
            Ok(res_bbox)
        } else {
17
            res
        }
500164
    }
    /// Extracts the font options for the current state of the DrawingCtx.
    ///
    /// You can use the font options later with create_pango_context().
914
    pub fn get_font_options(&self) -> FontOptions {
914
        let mut options = cairo::FontOptions::new().unwrap();
914
        if self.testing {
869
            options.set_antialias(cairo::Antialias::Gray);
        }
914
        options.set_hint_style(cairo::HintStyle::None);
914
        options.set_hint_metrics(cairo::HintMetrics::Off);
914
        FontOptions { options }
914
    }
}
impl From<ImageRendering> for Interpolation {
174
    fn from(r: ImageRendering) -> Interpolation {
174
        match r {
            ImageRendering::Pixelated
            | ImageRendering::CrispEdges
1
            | ImageRendering::OptimizeSpeed => Interpolation::Nearest,
            ImageRendering::Smooth
            | ImageRendering::OptimizeQuality
            | ImageRendering::HighQuality
173
            | ImageRendering::Auto => Interpolation::Smooth,
        }
174
    }
}
/// Create a Pango context with a particular configuration.
1039
pub fn create_pango_context(font_options: &FontOptions, transform: &Transform) -> pango::Context {
1039
    let font_map = pangocairo::FontMap::default();
1039
    let context = font_map.create_context();
1039
    context.set_round_glyph_positions(false);
1039
    let pango_matrix = PangoMatrix {
1039
        xx: transform.xx,
1039
        xy: transform.xy,
1039
        yx: transform.yx,
1039
        yy: transform.yy,
1039
        x0: transform.x0,
1039
        y0: transform.y0,
    };
1039
    let pango_matrix_ptr: *const PangoMatrix = &pango_matrix;
1039
    let matrix = unsafe { pango::Matrix::from_glib_none(pango_matrix_ptr) };
1039
    context.set_matrix(Some(&matrix));
1039
    pangocairo::functions::context_set_font_options(&context, Some(&font_options.options));
    // Pango says this about pango_cairo_context_set_resolution():
    //
    //     Sets the resolution for the context. This is a scale factor between
    //     points specified in a #PangoFontDescription and Cairo units. The
    //     default value is 96, meaning that a 10 point font will be 13
    //     units high. (10 * 96. / 72. = 13.3).
    //
    // I.e. Pango font sizes in a PangoFontDescription are in *points*, not pixels.
    // However, we are normalizing everything to userspace units, which amount to
    // pixels.  So, we will use 72.0 here to make Pango not apply any further scaling
    // to the size values we give it.
    //
    // An alternative would be to divide our font sizes by (dpi_y / 72) to effectively
    // cancel out Pango's scaling, but it's probably better to deal with Pango-isms
    // right here, instead of spreading them out through our Length normalization
    // code.
1039
    pangocairo::functions::context_set_resolution(&context, 72.0);
1039
    context
1039
}
949718
pub fn set_source_color_on_cairo(cr: &cairo::Context, color: &cssparser::Color) {
949718
    let rgba = color_to_rgba(color);
949718
    cr.set_source_rgba(
949718
        f64::from(rgba.red.unwrap_or(0)) / 255.0,
949718
        f64::from(rgba.green.unwrap_or(0)) / 255.0,
949718
        f64::from(rgba.blue.unwrap_or(0)) / 255.0,
949718
        f64::from(rgba.alpha.unwrap_or(0.0)),
    );
949718
}
/// Converts a Pango layout to a Cairo path on the specified cr starting at (x, y).
/// Does not clear the current path first.
1037
fn pango_layout_to_cairo(
    x: f64,
    y: f64,
    layout: &pango::Layout,
    gravity: pango::Gravity,
    cr: &cairo::Context,
) {
1037
    let rotation_from_gravity = gravity.to_rotation();
1037
    let rotation = if !rotation_from_gravity.approx_eq_cairo(0.0) {
        Some(-rotation_from_gravity)
    } else {
1037
        None
    };
1037
    cr.move_to(x, y);
1037
    let matrix = cr.matrix();
1037
    if let Some(rot) = rotation {
        cr.rotate(rot);
    }
1037
    pangocairo::functions::update_layout(cr, layout);
1037
    pangocairo::functions::layout_path(cr, layout);
1037
    cr.set_matrix(matrix);
1037
}
/// Converts a Pango layout to a Path starting at (x, y).
1037
pub fn pango_layout_to_path(
    x: f64,
    y: f64,
    layout: &pango::Layout,
    gravity: pango::Gravity,
) -> Result<Path, InternalRenderingError> {
1037
    let surface = cairo::RecordingSurface::create(cairo::Content::ColorAlpha, None)?;
1037
    let cr = cairo::Context::new(&surface)?;
1037
    pango_layout_to_cairo(x, y, layout, gravity, &cr);
1037
    let cairo_path = cr.copy_path()?;
1037
    Ok(Path::from_cairo(cairo_path))
1037
}
// https://www.w3.org/TR/css-masking-1/#ClipPathElement
43
fn element_can_be_used_inside_clip_path(element: &Element) -> bool {
    use ElementData::*;
43
    matches!(
43
        element.element_data,
        Circle(_)
            | Ellipse(_)
            | Line(_)
            | Path(_)
            | Polygon(_)
            | Polyline(_)
            | Rect(_)
            | Text(_)
            | Use(_)
    )
43
}
// https://www.w3.org/TR/css-masking-1/#ClipPathElement
1
fn element_can_be_used_inside_use_inside_clip_path(element: &Element) -> bool {
    use ElementData::*;
1
    matches!(
1
        element.element_data,
        Circle(_) | Ellipse(_) | Line(_) | Path(_) | Polygon(_) | Polyline(_) | Rect(_) | Text(_)
    )
1
}
#[derive(Debug)]
struct CompositingAffines {
    pub outside_temporary_surface: Transform,
    #[allow(unused)]
    pub initial: Transform,
    pub for_temporary_surface: Transform,
    pub compositing: Transform,
    pub for_snapshot: Transform,
}
impl CompositingAffines {
1120
    fn new(current: Transform, initial: Transform, cr_stack_depth: usize) -> CompositingAffines {
1120
        let is_topmost_temporary_surface = cr_stack_depth == 0;
1120
        let initial_inverse = initial.invert().unwrap();
1120
        let outside_temporary_surface = if is_topmost_temporary_surface {
648
            current
        } else {
472
            current.post_transform(&initial_inverse)
        };
1120
        let (scale_x, scale_y) = initial.transform_distance(1.0, 1.0);
1120
        let for_temporary_surface = if is_topmost_temporary_surface {
648
            current
                .post_transform(&initial_inverse)
                .post_scale(scale_x, scale_y)
        } else {
472
            current
        };
1120
        let compositing = if is_topmost_temporary_surface {
648
            initial.pre_scale(1.0 / scale_x, 1.0 / scale_y)
        } else {
472
            Transform::identity()
        };
1120
        let for_snapshot = compositing.invert().unwrap();
1120
        CompositingAffines {
1120
            outside_temporary_surface,
1120
            initial,
1120
            for_temporary_surface,
1120
            compositing,
            for_snapshot,
        }
1120
    }
}
949077
fn compute_stroke_and_fill_extents(
    cr: &cairo::Context,
    stroke: &Stroke,
    stroke_paint_source: &UserSpacePaintSource,
    initial_viewport: &Viewport,
) -> Result<PathExtents, InternalRenderingError> {
    // Dropping the precision of cairo's bezier subdivision, yielding 2x
    // _rendering_ time speedups, are these rather expensive operations
    // really needed here? */
949077
    let backup_tolerance = cr.tolerance();
949077
    cr.set_tolerance(1.0);
    // Bounding box for fill
    //
    // Unlike the case for stroke, for fills we always compute the bounding box.
    // In GNOME we have SVGs for symbolic icons where each icon has a bounding
    // rectangle with no fill and no stroke, and inside it there are the actual
    // paths for the icon's shape.  We need to be able to compute the bounding
    // rectangle's extents, even when it has no fill nor stroke.
949077
    let (x0, y0, x1, y1) = cr.fill_extents()?;
949077
    let fill_extents = if x0 != 0.0 || y0 != 0.0 || x1 != 0.0 || y1 != 0.0 {
504381
        Some(Rect::new(x0, y0, x1, y1))
    } else {
444696
        None
    };
    // Bounding box for stroke
    //
    // When presented with a line width of 0, Cairo returns a
    // stroke_extents rectangle of (0, 0, 0, 0).  This would cause the
    // bbox to include a lone point at the origin, which is wrong, as a
    // stroke of zero width should not be painted, per
    // https://www.w3.org/TR/SVG2/painting.html#StrokeWidth
    //
    // So, see if the stroke width is 0 and just not include the stroke in the
    // bounding box if so.
1897296
    let stroke_extents = if !stroke.width.approx_eq_cairo(0.0)
948219
        && !matches!(stroke_paint_source, UserSpacePaintSource::None)
    {
501181
        let backup_matrix = if stroke.non_scaling {
1
            let matrix = cr.matrix();
949078
            cr.set_matrix(ValidTransform::try_from(initial_viewport.transform)?.into());
1
            Some(matrix)
        } else {
501179
            None
        };
501180
        let (x0, y0, x1, y1) = cr.stroke_extents()?;
501180
        if let Some(matrix) = backup_matrix {
1
            cr.set_matrix(matrix);
        }
501180
        Some(Rect::new(x0, y0, x1, y1))
    } else {
447897
        None
    };
    // objectBoundingBox
949077
    let (x0, y0, x1, y1) = cr.path_extents()?;
949077
    let path_extents = Some(Rect::new(x0, y0, x1, y1));
    // restore tolerance
949077
    cr.set_tolerance(backup_tolerance);
949077
    Ok(PathExtents {
        path_only: path_extents,
949077
        fill: fill_extents,
949077
        stroke: stroke_extents,
    })
949077
}
949881
fn compute_stroke_and_fill_box(
    cr: &cairo::Context,
    stroke: &Stroke,
    stroke_paint_source: &UserSpacePaintSource,
    initial_viewport: &Viewport,
) -> Result<BoundingBox, InternalRenderingError> {
    let extents =
949881
        compute_stroke_and_fill_extents(cr, stroke, stroke_paint_source, initial_viewport)?;
949881
    let ink_rect = match (extents.fill, extents.stroke) {
444466
        (None, None) => None,
4235
        (Some(f), None) => Some(f),
230
        (None, Some(s)) => Some(s),
500950
        (Some(f), Some(s)) => Some(f.union(&s)),
    };
949881
    let mut bbox = BoundingBox::new().with_transform(Transform::from(cr.matrix()));
949881
    if let Some(rect) = extents.path_only {
949774
        bbox = bbox.with_rect(rect);
    }
949881
    if let Some(ink_rect) = ink_rect {
505415
        bbox = bbox.with_ink_rect(ink_rect);
    }
949881
    Ok(bbox)
949881
}
948969
fn setup_cr_for_stroke(cr: &cairo::Context, stroke: &Stroke) {
948969
    cr.set_line_width(stroke.width);
948969
    cr.set_miter_limit(stroke.miter_limit.0);
948969
    cr.set_line_cap(cairo::LineCap::from(stroke.line_cap));
948969
    cr.set_line_join(cairo::LineJoin::from(stroke.line_join));
948969
    let total_length: f64 = stroke.dashes.iter().sum();
948969
    if total_length > 0.0 {
106
        cr.set_dash(&stroke.dashes, stroke.dash_offset);
    } else {
948863
        cr.set_dash(&[], 0.0);
    }
948969
}
/// escape quotes and backslashes with backslash
6
fn escape_link_target(value: &str) -> Cow<'_, str> {
    let regex = {
        static REGEX: OnceLock<Regex> = OnceLock::new();
9
        REGEX.get_or_init(|| Regex::new(r"['\\]").unwrap())
    };
6
    regex.replace_all(value, |caps: &Captures<'_>| {
        match caps.get(0).unwrap().as_str() {
            "'" => "\\'".to_owned(),
            "\\" => "\\\\".to_owned(),
            _ => unreachable!(),
        }
    })
6
}
400
fn clip_to_rectangle(cr: &cairo::Context, r: &Rect) {
400
    cr.rectangle(r.x0, r.y0, r.width(), r.height());
400
    cr.clip();
400
}
impl From<SpreadMethod> for cairo::Extend {
163
    fn from(s: SpreadMethod) -> cairo::Extend {
163
        match s {
157
            SpreadMethod::Pad => cairo::Extend::Pad,
3
            SpreadMethod::Reflect => cairo::Extend::Reflect,
3
            SpreadMethod::Repeat => cairo::Extend::Repeat,
        }
163
    }
}
impl From<StrokeLinejoin> for cairo::LineJoin {
949666
    fn from(j: StrokeLinejoin) -> cairo::LineJoin {
949666
        match j {
949533
            StrokeLinejoin::Miter => cairo::LineJoin::Miter,
131
            StrokeLinejoin::Round => cairo::LineJoin::Round,
2
            StrokeLinejoin::Bevel => cairo::LineJoin::Bevel,
        }
949666
    }
}
impl From<StrokeLinecap> for cairo::LineCap {
949176
    fn from(j: StrokeLinecap) -> cairo::LineCap {
949176
        match j {
948986
            StrokeLinecap::Butt => cairo::LineCap::Butt,
158
            StrokeLinecap::Round => cairo::LineCap::Round,
32
            StrokeLinecap::Square => cairo::LineCap::Square,
        }
949176
    }
}
impl From<MixBlendMode> for cairo::Operator {
681
    fn from(m: MixBlendMode) -> cairo::Operator {
        use cairo::Operator;
681
        match m {
665
            MixBlendMode::Normal => Operator::Over,
1
            MixBlendMode::Multiply => Operator::Multiply,
1
            MixBlendMode::Screen => Operator::Screen,
1
            MixBlendMode::Overlay => Operator::Overlay,
1
            MixBlendMode::Darken => Operator::Darken,
1
            MixBlendMode::Lighten => Operator::Lighten,
1
            MixBlendMode::ColorDodge => Operator::ColorDodge,
1
            MixBlendMode::ColorBurn => Operator::ColorBurn,
1
            MixBlendMode::HardLight => Operator::HardLight,
1
            MixBlendMode::SoftLight => Operator::SoftLight,
2
            MixBlendMode::Difference => Operator::Difference,
1
            MixBlendMode::Exclusion => Operator::Exclusion,
1
            MixBlendMode::Hue => Operator::HslHue,
1
            MixBlendMode::Saturation => Operator::HslSaturation,
1
            MixBlendMode::Color => Operator::HslColor,
1
            MixBlendMode::Luminosity => Operator::HslLuminosity,
        }
681
    }
}
impl From<ClipRule> for cairo::FillRule {
38
    fn from(c: ClipRule) -> cairo::FillRule {
38
        match c {
37
            ClipRule::NonZero => cairo::FillRule::Winding,
1
            ClipRule::EvenOdd => cairo::FillRule::EvenOdd,
        }
38
    }
}
impl From<FillRule> for cairo::FillRule {
947778
    fn from(f: FillRule) -> cairo::FillRule {
947778
        match f {
947614
            FillRule::NonZero => cairo::FillRule::Winding,
164
            FillRule::EvenOdd => cairo::FillRule::EvenOdd,
        }
947778
    }
}
impl From<ShapeRendering> for cairo::Antialias {
948315
    fn from(sr: ShapeRendering) -> cairo::Antialias {
948315
        match sr {
948302
            ShapeRendering::Auto | ShapeRendering::GeometricPrecision => cairo::Antialias::Default,
13
            ShapeRendering::OptimizeSpeed | ShapeRendering::CrispEdges => cairo::Antialias::None,
        }
948315
    }
}
impl From<TextRendering> for cairo::Antialias {
978
    fn from(tr: TextRendering) -> cairo::Antialias {
978
        match tr {
            TextRendering::Auto
            | TextRendering::OptimizeLegibility
978
            | TextRendering::GeometricPrecision => cairo::Antialias::Default,
            TextRendering::OptimizeSpeed => cairo::Antialias::None,
        }
978
    }
}
impl From<cairo::Matrix> for Transform {
    #[inline]
10416807
    fn from(m: cairo::Matrix) -> Self {
10416807
        Self::new_unchecked(m.xx(), m.yx(), m.xy(), m.yy(), m.x0(), m.y0())
10416807
    }
}
impl From<ValidTransform> for cairo::Matrix {
    #[inline]
6063184
    fn from(t: ValidTransform) -> cairo::Matrix {
6063184
        cairo::Matrix::new(t.xx, t.yx, t.xy, t.yy, t.x0, t.y0)
6063184
    }
}
/// Extents for a path in its current coordinate system.
///
/// Normally you'll want to convert this to a BoundingBox, which has knowledge about just
/// what that coordinate system is.
pub struct PathExtents {
    /// Extents of the "plain", unstroked path, or `None` if the path is empty.
    pub path_only: Option<Rect>,
    /// Extents of just the fill, or `None` if the path is empty.
    pub fill: Option<Rect>,
    /// Extents for the stroked path, or `None` if the path is empty or zero-width.
    pub stroke: Option<Rect>,
}
impl Path {
1898187
    pub fn to_cairo(
        &self,
        cr: &cairo::Context,
        is_square_linecap: bool,
    ) -> Result<(), InternalRenderingError> {
1898187
        assert!(!self.is_empty());
3832606
        for subpath in self.iter_subpath() {
            // If a subpath is empty and the linecap is a square, then draw a square centered on
            // the origin of the subpath. See #165.
1934419
            if is_square_linecap {
34
                let (x, y) = subpath.origin();
34
                if subpath.is_zero_length() {
1898187
                    let stroke_size = 0.002;
1
                    cr.move_to(x - stroke_size / 2., y);
1
                    cr.line_to(x + stroke_size / 2., y);
                }
            }
13838215
            for cmd in subpath.iter_commands() {
11903796
                cmd.to_cairo(cr);
            }
        }
        // We check the cr's status right after feeding it a new path for a few reasons:
        //
        // * Any of the individual path commands may cause the cr to enter an error state, for
        //   example, if they come with coordinates outside of Cairo's supported range.
        //
        // * The *next* call to the cr will probably be something that actually checks the status
        //   (i.e. in cairo-rs), and we don't want to panic there.
1898187
        cr.status().map_err(|e| e.into())
1898187
    }
    /// Converts a `cairo::Path` to a librsvg `Path`.
1038
    fn from_cairo(cairo_path: cairo::Path) -> Path {
1038
        let mut builder = PathBuilder::default();
        // Cairo has the habit of appending a MoveTo to some paths, but we don't want a
        // path for empty text to generate that lone point.  So, strip out paths composed
        // only of MoveTo.
1038
        if !cairo_path_is_only_move_tos(&cairo_path) {
252165
            for segment in cairo_path.iter() {
251189
                match segment {
19591
                    cairo::PathSegment::MoveTo((x, y)) => builder.move_to(x, y),
75945
                    cairo::PathSegment::LineTo((x, y)) => builder.line_to(x, y),
137041
                    cairo::PathSegment::CurveTo((x2, y2), (x3, y3), (x4, y4)) => {
137041
                        builder.curve_to(x2, y2, x3, y3, x4, y4)
                    }
18612
                    cairo::PathSegment::ClosePath => builder.close_path(),
                }
            }
        }
1037
        builder.into_path()
1032
    }
}
1038
fn cairo_path_is_only_move_tos(path: &cairo::Path) -> bool {
1038
    path.iter()
2017
        .all(|seg| matches!(seg, cairo::PathSegment::MoveTo((_, _))))
1038
}
impl PathCommand {
11909089
    fn to_cairo(&self, cr: &cairo::Context) {
11909089
        match *self {
1937972
            PathCommand::MoveTo(x, y) => cr.move_to(x, y),
7742687
            PathCommand::LineTo(x, y) => cr.line_to(x, y),
290420
            PathCommand::CurveTo(ref curve) => curve.to_cairo(cr),
2588
            PathCommand::Arc(ref arc) => arc.to_cairo(cr),
1935422
            PathCommand::ClosePath => cr.close_path(),
        }
11909089
    }
}
impl EllipticalArc {
2588
    fn to_cairo(&self, cr: &cairo::Context) {
2588
        match self.center_parameterization() {
            ArcParameterization::CenterParameters {
2588
                center,
2588
                radii,
2588
                theta1,
2588
                delta_theta,
            } => {
2588
                let n_segs = (delta_theta / (PI * 0.5 + 0.001)).abs().ceil() as u32;
2588
                let d_theta = delta_theta / f64::from(n_segs);
2588
                let mut theta = theta1;
6604
                for _ in 0..n_segs {
4016
                    arc_segment(center, radii, self.x_axis_rotation, theta, theta + d_theta)
                        .to_cairo(cr);
4016
                    theta += d_theta;
                }
            }
            ArcParameterization::LineTo => {
                let (x2, y2) = self.to;
                cr.line_to(x2, y2);
            }
            ArcParameterization::Omit => {}
        }
2588
    }
}
impl CubicBezierCurve {
294431
    fn to_cairo(&self, cr: &cairo::Context) {
294431
        let Self { pt1, pt2, to } = *self;
294431
        cr.curve_to(pt1.0, pt1.1, pt2.0, pt2.1, to.0, to.1);
294431
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
2
    fn rsvg_path_from_cairo_path() {
1
        let surface = cairo::ImageSurface::create(cairo::Format::ARgb32, 10, 10).unwrap();
1
        let cr = cairo::Context::new(&surface).unwrap();
1
        cr.move_to(1.0, 2.0);
1
        cr.line_to(3.0, 4.0);
1
        cr.curve_to(5.0, 6.0, 7.0, 8.0, 9.0, 10.0);
1
        cr.close_path();
1
        let cairo_path = cr.copy_path().unwrap();
1
        let path = Path::from_cairo(cairo_path);
2
        assert_eq!(
1
            path.iter().collect::<Vec<PathCommand>>(),
1
            vec![
1
                PathCommand::MoveTo(1.0, 2.0),
1
                PathCommand::LineTo(3.0, 4.0),
1
                PathCommand::CurveTo(CubicBezierCurve {
1
                    pt1: (5.0, 6.0),
1
                    pt2: (7.0, 8.0),
1
                    to: (9.0, 10.0),
                }),
1
                PathCommand::ClosePath,
1
                PathCommand::MoveTo(1.0, 2.0), // cairo inserts a MoveTo after ClosePath
            ],
        );
2
    }
}