1
//! The `marker` element, and geometry computations for markers.
2

            
3
use std::f64::consts::*;
4
use std::ops::Deref;
5

            
6
use cssparser::Parser;
7
use markup5ever::{expanded_name, local_name, namespace_url, ns};
8

            
9
use crate::angle::Angle;
10
use crate::aspect_ratio::*;
11
use crate::bbox::BoundingBox;
12
use crate::borrow_element_as;
13
use crate::document::AcquiredNodes;
14
use crate::drawing_ctx::{DrawingCtx, Viewport};
15
use crate::element::{set_attribute, ElementTrait};
16
use crate::error::*;
17
use crate::float_eq_cairo::ApproxEqCairo;
18
use crate::layout::{self, Shape, StackingContext};
19
use crate::length::*;
20
use crate::node::{CascadedValues, Node, NodeBorrow, NodeDraw};
21
use crate::parse_identifiers;
22
use crate::parsers::{Parse, ParseValue};
23
use crate::path_builder::{arc_segment, ArcParameterization, CubicBezierCurve, Path, PathCommand};
24
use crate::rect::Rect;
25
use crate::rsvg_log;
26
use crate::session::Session;
27
use crate::transform::Transform;
28
use crate::viewbox::*;
29
use crate::xml::Attributes;
30

            
31
// markerUnits attribute: https://www.w3.org/TR/SVG/painting.html#MarkerElement
32
388
#[derive(Debug, Default, Copy, Clone, PartialEq)]
33
enum MarkerUnits {
34
    UserSpaceOnUse,
35
    #[default]
36
96
    StrokeWidth,
37
}
38

            
39
impl Parse for MarkerUnits {
40
31
    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<MarkerUnits, ParseError<'i>> {
41
61
        Ok(parse_identifiers!(
42
            parser,
43
            "userSpaceOnUse" => MarkerUnits::UserSpaceOnUse,
44
            "strokeWidth" => MarkerUnits::StrokeWidth,
45
1
        )?)
46
31
    }
47
}
48

            
49
// orient attribute: https://www.w3.org/TR/SVG/painting.html#MarkerElement
50
12
#[derive(Debug, Copy, Clone, PartialEq)]
51
enum MarkerOrient {
52
    Auto,
53
    AutoStartReverse,
54
22
    Angle(Angle),
55
}
56

            
57
impl Default for MarkerOrient {
58
    #[inline]
59
96
    fn default() -> MarkerOrient {
60
96
        MarkerOrient::Angle(Angle::new(0.0))
61
96
    }
62
}
63

            
64
impl Parse for MarkerOrient {
65
80
    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<MarkerOrient, ParseError<'i>> {
66
80
        if parser
67
80
            .try_parse(|p| p.expect_ident_matching("auto"))
68
80
            .is_ok()
69
        {
70
58
            return Ok(MarkerOrient::Auto);
71
        }
72

            
73
22
        if parser
74
22
            .try_parse(|p| p.expect_ident_matching("auto-start-reverse"))
75
22
            .is_ok()
76
        {
77
2
            Ok(MarkerOrient::AutoStartReverse)
78
        } else {
79
20
            Angle::parse(parser).map(MarkerOrient::Angle)
80
        }
81
80
    }
82
}
83

            
84
pub struct Marker {
85
    units: MarkerUnits,
86
    ref_x: Length<Horizontal>,
87
    ref_y: Length<Vertical>,
88
    width: ULength<Horizontal>,
89
    height: ULength<Vertical>,
90
    orient: MarkerOrient,
91
    aspect: AspectRatio,
92
    vbox: Option<ViewBox>,
93
}
94

            
95
impl Default for Marker {
96
96
    fn default() -> Marker {
97
96
        Marker {
98
96
            units: MarkerUnits::default(),
99
96
            ref_x: Default::default(),
100
96
            ref_y: Default::default(),
101
            // the following two are per the spec
102
96
            width: ULength::<Horizontal>::parse_str("3").unwrap(),
103
96
            height: ULength::<Vertical>::parse_str("3").unwrap(),
104
96
            orient: MarkerOrient::default(),
105
96
            aspect: AspectRatio::default(),
106
96
            vbox: None,
107
        }
108
96
    }
109
}
110

            
111
impl Marker {
112
194
    fn render(
113
        &self,
114
        node: &Node,
115
        acquired_nodes: &mut AcquiredNodes<'_>,
116
        viewport: &Viewport,
117
        draw_ctx: &mut DrawingCtx,
118
        xpos: f64,
119
        ypos: f64,
120
        computed_angle: Angle,
121
        line_width: f64,
122
        clipping: bool,
123
        marker_type: MarkerType,
124
        marker: &layout::Marker,
125
    ) -> Result<BoundingBox, InternalRenderingError> {
126
194
        let mut cascaded = CascadedValues::new_from_node(node);
127
194
        cascaded.context_fill = Some(marker.context_fill.clone());
128
194
        cascaded.context_stroke = Some(marker.context_stroke.clone());
129

            
130
194
        let values = cascaded.get();
131

            
132
194
        let params = NormalizeParams::new(values, viewport);
133

            
134
194
        let marker_width = self.width.to_user(&params);
135
194
        let marker_height = self.height.to_user(&params);
136

            
137
194
        if marker_width.approx_eq_cairo(0.0) || marker_height.approx_eq_cairo(0.0) {
138
            // markerWidth or markerHeight set to 0 disables rendering of the element
139
            // https://www.w3.org/TR/SVG/painting.html#MarkerWidthAttribute
140
            return Ok(draw_ctx.empty_bbox());
141
        }
142

            
143
194
        let rotation = match self.orient {
144
33
            MarkerOrient::Auto => computed_angle,
145
            MarkerOrient::AutoStartReverse => {
146
2
                if marker_type == MarkerType::Start {
147
1
                    computed_angle.flip()
148
                } else {
149
1
                    computed_angle
150
                }
151
            }
152
159
            MarkerOrient::Angle(a) => a,
153
        };
154

            
155
194
        let mut transform = Transform::new_translate(xpos, ypos).pre_rotate(rotation);
156

            
157
369
        if self.units == MarkerUnits::StrokeWidth {
158
175
            transform = transform.pre_scale(line_width, line_width);
159
        }
160

            
161
194
        let content_viewport = if let Some(vbox) = self.vbox {
162
155
            if vbox.is_empty() {
163
1
                return Ok(draw_ctx.empty_bbox());
164
            }
165

            
166
154
            let r = self
167
                .aspect
168
154
                .compute(&vbox, &Rect::from_size(marker_width, marker_height));
169

            
170
154
            let (vb_width, vb_height) = vbox.size();
171
154
            transform = transform.pre_scale(r.width() / vb_width, r.height() / vb_height);
172

            
173
154
            viewport.with_view_box(vb_width, vb_height)
174
        } else {
175
39
            viewport.with_view_box(marker_width, marker_height)
176
        };
177

            
178
193
        let content_params = NormalizeParams::new(values, &content_viewport);
179

            
180
193
        transform = transform.pre_translate(
181
193
            -self.ref_x.to_user(&content_params),
182
193
            -self.ref_y.to_user(&content_params),
183
        );
184

            
185
        // FIXME: This is the only place in the code where we pass a Some(rect) to
186
        // StackingContext::new() for its clip_rect argument.  The effect is to clip to
187
        // the viewport that the current marker should establish, but this code for
188
        // drawing markers does not yet use the viewports machinery and instead does
189
        // things by hand.  We should encode the information about the overflow property
190
        // in the viewport, so it knows whether to clip or not.
191
193
        let clip_rect = if values.is_overflow() {
192
9
            None
193
220
        } else if let Some(vbox) = self.vbox {
194
148
            Some(*vbox)
195
        } else {
196
36
            Some(Rect::from_size(marker_width, marker_height))
197
        };
198

            
199
193
        let elt = node.borrow_element();
200
193
        let stacking_ctx = StackingContext::new(
201
193
            draw_ctx.session(),
202
            acquired_nodes,
203
193
            &elt,
204
193
            transform,
205
193
            clip_rect,
206
            values,
207
        );
208

            
209
193
        draw_ctx.with_discrete_layer(
210
            &stacking_ctx,
211
            acquired_nodes,
212
            &content_viewport,
213
193
            clipping,
214
386
            &mut |an, dc| node.draw_children(an, &cascaded, &content_viewport, dc, clipping),
215
        )
216
194
    }
217
}
218

            
219
impl ElementTrait for Marker {
220
96
    fn set_attributes(&mut self, attrs: &Attributes, session: &Session) {
221
762
        for (attr, value) in attrs.iter() {
222
666
            match attr.expanded() {
223
                expanded_name!("", "markerUnits") => {
224
27
                    set_attribute(&mut self.units, attr.parse(value), session)
225
                }
226
                expanded_name!("", "refX") => {
227
87
                    set_attribute(&mut self.ref_x, attr.parse(value), session)
228
                }
229
                expanded_name!("", "refY") => {
230
87
                    set_attribute(&mut self.ref_y, attr.parse(value), session)
231
                }
232
                expanded_name!("", "markerWidth") => {
233
44
                    set_attribute(&mut self.width, attr.parse(value), session)
234
                }
235
                expanded_name!("", "markerHeight") => {
236
44
                    set_attribute(&mut self.height, attr.parse(value), session)
237
                }
238
                expanded_name!("", "orient") => {
239
70
                    set_attribute(&mut self.orient, attr.parse(value), session)
240
                }
241
                expanded_name!("", "preserveAspectRatio") => {
242
                    set_attribute(&mut self.aspect, attr.parse(value), session)
243
                }
244
                expanded_name!("", "viewBox") => {
245
34
                    set_attribute(&mut self.vbox, attr.parse(value), session)
246
                }
247
                _ => (),
248
            }
249
666
        }
250
96
    }
251
}
252

            
253
// Machinery to figure out marker orientations
254
42
#[derive(Debug, PartialEq)]
255
enum Segment {
256
    Degenerate {
257
        // A single lone point
258
        x: f64,
259
        y: f64,
260
    },
261

            
262
    LineOrCurve {
263
63
        x1: f64,
264
42
        y1: f64,
265
63
        x2: f64,
266
63
        y2: f64,
267
63
        x3: f64,
268
63
        y3: f64,
269
63
        x4: f64,
270
63
        y4: f64,
271
    },
272
}
273

            
274
impl Segment {
275
1
    fn degenerate(x: f64, y: f64) -> Segment {
276
1
        Segment::Degenerate { x, y }
277
1
    }
278

            
279
224
    fn curve(x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64, x4: f64, y4: f64) -> Segment {
280
224
        Segment::LineOrCurve {
281
            x1,
282
            y1,
283
            x2,
284
            y2,
285
            x3,
286
            y3,
287
            x4,
288
            y4,
289
        }
290
224
    }
291

            
292
191
    fn line(x1: f64, y1: f64, x2: f64, y2: f64) -> Segment {
293
191
        Segment::curve(x1, y1, x2, y2, x1, y1, x2, y2)
294
191
    }
295

            
296
    // If the segment has directionality, returns two vectors (v1x, v1y, v2x, v2y); otherwise,
297
    // returns None.  The vectors are the tangents at the beginning and at the end of the segment,
298
    // respectively.  A segment does not have directionality if it is degenerate (i.e. a single
299
    // point) or a zero-length segment, i.e. where all four control points are coincident (the first
300
    // and last control points may coincide, but the others may define a loop - thus nonzero length)
301
384
    fn get_directionalities(&self) -> Option<(f64, f64, f64, f64)> {
302
384
        match *self {
303
1
            Segment::Degenerate { .. } => None,
304

            
305
            Segment::LineOrCurve {
306
383
                x1,
307
383
                y1,
308
383
                x2,
309
383
                y2,
310
383
                x3,
311
383
                y3,
312
383
                x4,
313
383
                y4,
314
            } => {
315
383
                let coincide_1_and_2 = points_equal(x1, y1, x2, y2);
316
383
                let coincide_1_and_3 = points_equal(x1, y1, x3, y3);
317
383
                let coincide_1_and_4 = points_equal(x1, y1, x4, y4);
318
383
                let coincide_2_and_3 = points_equal(x2, y2, x3, y3);
319
383
                let coincide_2_and_4 = points_equal(x2, y2, x4, y4);
320
383
                let coincide_3_and_4 = points_equal(x3, y3, x4, y4);
321

            
322
383
                if coincide_1_and_2 && coincide_1_and_3 && coincide_1_and_4 {
323
20
                    None
324
363
                } else if coincide_1_and_2 && coincide_1_and_3 {
325
3
                    Some((x4 - x1, y4 - y1, x4 - x3, y4 - y3))
326
360
                } else if coincide_1_and_2 && coincide_3_and_4 {
327
3
                    Some((x4 - x1, y4 - y1, x4 - x1, y4 - y1))
328
357
                } else if coincide_2_and_3 && coincide_2_and_4 {
329
3
                    Some((x2 - x1, y2 - y1, x4 - x1, y4 - y1))
330
354
                } else if coincide_1_and_2 {
331
3
                    Some((x3 - x1, y3 - y1, x4 - x3, y4 - y3))
332
351
                } else if coincide_3_and_4 {
333
3
                    Some((x2 - x1, y2 - y1, x4 - x2, y4 - y2))
334
                } else {
335
348
                    Some((x2 - x1, y2 - y1, x4 - x3, y4 - y3))
336
                }
337
            }
338
        }
339
384
    }
340
}
341

            
342
2296
fn points_equal(x1: f64, y1: f64, x2: f64, y2: f64) -> bool {
343
2296
    x1.approx_eq_cairo(x2) && y1.approx_eq_cairo(y2)
344
2296
}
345

            
346
enum SegmentState {
347
    Initial,
348
    NewSubpath,
349
    InSubpath,
350
    ClosedSubpath,
351
}
352

            
353
10
#[derive(Debug, PartialEq)]
354
5
struct Segments(Vec<Segment>);
355

            
356
impl Deref for Segments {
357
    type Target = [Segment];
358

            
359
743
    fn deref(&self) -> &[Segment] {
360
743
        &self.0
361
743
    }
362
}
363

            
364
// This converts a path builder into a vector of curveto-like segments.
365
// Each segment can be:
366
//
367
// 1. Segment::Degenerate => the segment is actually a single point (x, y)
368
//
369
// 2. Segment::LineOrCurve => either a lineto or a curveto (or the effective
370
// lineto that results from a closepath).
371
// We have the following points:
372
//       P1 = (x1, y1)
373
//       P2 = (x2, y2)
374
//       P3 = (x3, y3)
375
//       P4 = (x4, y4)
376
//
377
// The start and end points are P1 and P4, respectively.
378
// The tangent at the start point is given by the vector (P2 - P1).
379
// The tangent at the end point is given by the vector (P4 - P3).
380
// The tangents also work if the segment refers to a lineto (they will
381
// both just point in the same direction).
382
impl From<&Path> for Segments {
383
69
    fn from(path: &Path) -> Segments {
384
        let mut last_x: f64;
385
        let mut last_y: f64;
386

            
387
69
        let mut cur_x: f64 = 0.0;
388
69
        let mut cur_y: f64 = 0.0;
389
69
        let mut subpath_start_x: f64 = 0.0;
390
69
        let mut subpath_start_y: f64 = 0.0;
391

            
392
69
        let mut segments = Vec::new();
393
69
        let mut state = SegmentState::Initial;
394

            
395
340
        for path_command in path.iter() {
396
271
            last_x = cur_x;
397
271
            last_y = cur_y;
398

            
399
271
            match path_command {
400
77
                PathCommand::MoveTo(x, y) => {
401
77
                    cur_x = x;
402
77
                    cur_y = y;
403

            
404
77
                    subpath_start_x = cur_x;
405
77
                    subpath_start_y = cur_y;
406

            
407
77
                    match state {
408
70
                        SegmentState::Initial | SegmentState::InSubpath => {
409
                            // Ignore the very first moveto in a sequence (Initial state),
410
                            // or if we were already drawing within a subpath, start
411
                            // a new subpath.
412
70
                            state = SegmentState::NewSubpath;
413
                        }
414

            
415
                        SegmentState::NewSubpath => {
416
                            // We had just begun a new subpath (i.e. from a moveto) and we got
417
                            // another moveto?  Output a stray point for the
418
                            // previous moveto.
419
                            segments.push(Segment::degenerate(last_x, last_y));
420
                            state = SegmentState::NewSubpath;
421
                        }
422

            
423
7
                        SegmentState::ClosedSubpath => {
424
                            // Cairo outputs a moveto after every closepath, so that subsequent
425
                            // lineto/curveto commands will start at the closed vertex.
426
                            // We don't want to actually emit a point (a degenerate segment) in
427
                            // that artificial-moveto case.
428
                            //
429
                            // We'll reset to the Initial state so that a subsequent "real"
430
                            // moveto will be handled as the beginning of a new subpath, or a
431
                            // degenerate point, as usual.
432
7
                            state = SegmentState::Initial;
433
                        }
434
                    }
435
                }
436

            
437
145
                PathCommand::LineTo(x, y) => {
438
145
                    cur_x = x;
439
145
                    cur_y = y;
440

            
441
145
                    segments.push(Segment::line(last_x, last_y, cur_x, cur_y));
442

            
443
145
                    state = SegmentState::InSubpath;
444
145
                }
445

            
446
19
                PathCommand::CurveTo(curve) => {
447
                    let CubicBezierCurve {
448
19
                        pt1: (x2, y2),
449
19
                        pt2: (x3, y3),
450
19
                        to,
451
                    } = curve;
452
19
                    cur_x = to.0;
453
19
                    cur_y = to.1;
454

            
455
19
                    segments.push(Segment::curve(last_x, last_y, x2, y2, x3, y3, cur_x, cur_y));
456

            
457
19
                    state = SegmentState::InSubpath;
458
19
                }
459

            
460
4
                PathCommand::Arc(arc) => {
461
4
                    cur_x = arc.to.0;
462
4
                    cur_y = arc.to.1;
463

            
464
4
                    match arc.center_parameterization() {
465
                        ArcParameterization::CenterParameters {
466
4
                            center,
467
4
                            radii,
468
4
                            theta1,
469
4
                            delta_theta,
470
                        } => {
471
4
                            let rot = arc.x_axis_rotation;
472
4
                            let theta2 = theta1 + delta_theta;
473
4
                            let n_segs = (delta_theta / (PI * 0.5 + 0.001)).abs().ceil() as u32;
474
4
                            let d_theta = delta_theta / f64::from(n_segs);
475

            
476
                            let segment1 =
477
4
                                arc_segment(center, radii, rot, theta1, theta1 + d_theta);
478
                            let segment2 =
479
4
                                arc_segment(center, radii, rot, theta2 - d_theta, theta2);
480

            
481
4
                            let (x2, y2) = segment1.pt1;
482
4
                            let (x3, y3) = segment2.pt2;
483
4
                            segments
484
4
                                .push(Segment::curve(last_x, last_y, x2, y2, x3, y3, cur_x, cur_y));
485

            
486
4
                            state = SegmentState::InSubpath;
487
4
                        }
488
                        ArcParameterization::LineTo => {
489
                            segments.push(Segment::line(last_x, last_y, cur_x, cur_y));
490

            
491
                            state = SegmentState::InSubpath;
492
                        }
493
                        ArcParameterization::Omit => {}
494
                    }
495
                }
496

            
497
26
                PathCommand::ClosePath => {
498
26
                    cur_x = subpath_start_x;
499
26
                    cur_y = subpath_start_y;
500

            
501
26
                    segments.push(Segment::line(last_x, last_y, cur_x, cur_y));
502

            
503
26
                    state = SegmentState::ClosedSubpath;
504
                }
505
            }
506
        }
507

            
508
69
        if let SegmentState::NewSubpath = state {
509
            // Output a lone point if we started a subpath with a moveto
510
            // command, but there are no subsequent commands.
511
            segments.push(Segment::degenerate(cur_x, cur_y));
512
        };
513

            
514
69
        Segments(segments)
515
69
    }
516
}
517

            
518
// The SVG spec 1.1 says http://www.w3.org/TR/SVG/implnote.html#PathElementImplementationNotes
519
// Certain line-capping and line-joining situations and markers
520
// require that a path segment have directionality at its start and
521
// end points. Zero-length path segments have no directionality. In
522
// these cases, the following algorithm is used to establish
523
// directionality:  to determine the directionality of the start
524
// point of a zero-length path segment, go backwards in the path
525
// data specification within the current subpath until you find a
526
// segment which has directionality at its end point (e.g., a path
527
// segment with non-zero length) and use its ending direction;
528
// otherwise, temporarily consider the start point to lack
529
// directionality. Similarly, to determine the directionality of the
530
// end point of a zero-length path segment, go forwards in the path
531
// data specification within the current subpath until you find a
532
// segment which has directionality at its start point (e.g., a path
533
// segment with non-zero length) and use its starting direction;
534
// otherwise, temporarily consider the end point to lack
535
// directionality. If the start point has directionality but the end
536
// point doesn't, then the end point uses the start point's
537
// directionality. If the end point has directionality but the start
538
// point doesn't, then the start point uses the end point's
539
// directionality. Otherwise, set the directionality for the path
540
// segment's start and end points to align with the positive x-axis
541
// in user space.
542
impl Segments {
543
173
    fn find_incoming_angle_backwards(&self, start_index: usize) -> Option<Angle> {
544
        // "go backwards ... within the current subpath until ... segment which has directionality
545
        // at its end point"
546
182
        for segment in self[..=start_index].iter().rev() {
547
181
            match *segment {
548
                Segment::Degenerate { .. } => {
549
                    return None; // reached the beginning of the subpath as we ran into a standalone point
550
                }
551

            
552
181
                Segment::LineOrCurve { .. } => match segment.get_directionalities() {
553
172
                    Some((_, _, v2x, v2y)) => {
554
172
                        return Some(Angle::from_vector(v2x, v2y));
555
                    }
556
                    None => {
557
                        continue;
558
                    }
559
                },
560
            }
561
        }
562

            
563
1
        None
564
173
    }
565

            
566
186
    fn find_outgoing_angle_forwards(&self, start_index: usize) -> Option<Angle> {
567
        // "go forwards ... within the current subpath until ... segment which has directionality at
568
        // its start point"
569
195
        for segment in &self[start_index..] {
570
192
            match *segment {
571
                Segment::Degenerate { .. } => {
572
                    return None; // reached the end of a subpath as we ran into a standalone point
573
                }
574

            
575
192
                Segment::LineOrCurve { .. } => match segment.get_directionalities() {
576
183
                    Some((v1x, v1y, _, _)) => {
577
183
                        return Some(Angle::from_vector(v1x, v1y));
578
                    }
579
                    None => {
580
                        continue;
581
                    }
582
                },
583
            }
584
        }
585

            
586
3
        None
587
186
    }
588
}
589

            
590
// From SVG's marker-start, marker-mid, marker-end properties
591
11
#[derive(Debug, Copy, Clone, PartialEq)]
592
enum MarkerType {
593
    Start,
594
    Middle,
595
    End,
596
}
597

            
598
194
fn emit_marker_by_node(
599
    viewport: &Viewport,
600
    draw_ctx: &mut DrawingCtx,
601
    acquired_nodes: &mut AcquiredNodes<'_>,
602
    marker: &layout::Marker,
603
    xpos: f64,
604
    ypos: f64,
605
    computed_angle: Angle,
606
    line_width: f64,
607
    clipping: bool,
608
    marker_type: MarkerType,
609
) -> Result<BoundingBox, InternalRenderingError> {
610
194
    match acquired_nodes.acquire_ref(marker.node_ref.as_ref().unwrap()) {
611
194
        Ok(acquired) => {
612
194
            let node = acquired.get();
613

            
614
194
            let marker_elt = borrow_element_as!(node, Marker);
615

            
616
194
            marker_elt.render(
617
                node,
618
                acquired_nodes,
619
                viewport,
620
                draw_ctx,
621
                xpos,
622
                ypos,
623
                computed_angle,
624
                line_width,
625
                clipping,
626
                marker_type,
627
                marker,
628
            )
629
194
        }
630

            
631
        Err(e) => {
632
            rsvg_log!(draw_ctx.session(), "could not acquire marker: {}", e);
633
            Ok(draw_ctx.empty_bbox())
634
        }
635
    }
636
194
}
637

            
638
#[derive(Debug, Copy, Clone, PartialEq)]
639
enum MarkerEndpoint {
640
    Start,
641
    End,
642
}
643

            
644
237
fn emit_marker<E>(
645
    segment: &Segment,
646
    endpoint: MarkerEndpoint,
647
    marker_type: MarkerType,
648
    orient: Angle,
649
    emit_fn: &mut E,
650
) -> Result<BoundingBox, InternalRenderingError>
651
where
652
    E: FnMut(MarkerType, f64, f64, Angle) -> Result<BoundingBox, InternalRenderingError>,
653
{
654
474
    let (x, y) = match *segment {
655
        Segment::Degenerate { x, y } => (x, y),
656

            
657
237
        Segment::LineOrCurve { x1, y1, x4, y4, .. } => match endpoint {
658
173
            MarkerEndpoint::Start => (x1, y1),
659
64
            MarkerEndpoint::End => (x4, y4),
660
        },
661
    };
662

            
663
237
    emit_fn(marker_type, x, y, orient)
664
237
}
665

            
666
948453
pub fn render_markers_for_shape(
667
    shape: &Shape,
668
    viewport: &Viewport,
669
    draw_ctx: &mut DrawingCtx,
670
    acquired_nodes: &mut AcquiredNodes<'_>,
671
    clipping: bool,
672
) -> Result<BoundingBox, InternalRenderingError> {
673
948453
    if shape.stroke.width.approx_eq_cairo(0.0) {
674
19
        return Ok(draw_ctx.empty_bbox());
675
    }
676

            
677
948434
    if shape.marker_start.node_ref.is_none()
678
948369
        && shape.marker_mid.node_ref.is_none()
679
948284
        && shape.marker_end.node_ref.is_none()
680
    {
681
948242
        return Ok(draw_ctx.empty_bbox());
682
    }
683

            
684
62
    emit_markers_for_path(
685
62
        &shape.path,
686
62
        draw_ctx.empty_bbox(),
687
290
        &mut |marker_type: MarkerType, x: f64, y: f64, computed_angle: Angle| {
688
228
            let marker = match marker_type {
689
62
                MarkerType::Start => &shape.marker_start,
690
104
                MarkerType::Middle => &shape.marker_mid,
691
62
                MarkerType::End => &shape.marker_end,
692
            };
693

            
694
228
            if marker.node_ref.is_some() {
695
194
                emit_marker_by_node(
696
194
                    viewport,
697
194
                    draw_ctx,
698
194
                    acquired_nodes,
699
194
                    marker,
700
                    x,
701
                    y,
702
                    computed_angle,
703
194
                    shape.stroke.width,
704
194
                    clipping,
705
                    marker_type,
706
                )
707
            } else {
708
34
                Ok(draw_ctx.empty_bbox())
709
            }
710
228
        },
711
    )
712
948323
}
713

            
714
64
fn emit_markers_for_path<E>(
715
    path: &Path,
716
    empty_bbox: BoundingBox,
717
    emit_fn: &mut E,
718
) -> Result<BoundingBox, InternalRenderingError>
719
where
720
    E: FnMut(MarkerType, f64, f64, Angle) -> Result<BoundingBox, InternalRenderingError>,
721
{
722
    enum SubpathState {
723
        NoSubpath,
724
        InSubpath,
725
    }
726

            
727
64
    let mut bbox = empty_bbox;
728

            
729
    // Convert the path to a list of segments and bare points
730
64
    let segments = Segments::from(path);
731

            
732
64
    let mut subpath_state = SubpathState::NoSubpath;
733

            
734
237
    for (i, segment) in segments.iter().enumerate() {
735
173
        match *segment {
736
            Segment::Degenerate { .. } => {
737
                if let SubpathState::InSubpath = subpath_state {
738
                    assert!(i > 0);
739

            
740
                    // Got a lone point after a subpath; render the subpath's end marker first
741
                    let angle = segments
742
                        .find_incoming_angle_backwards(i - 1)
743
                        .unwrap_or_else(|| Angle::new(0.0));
744
                    let marker_bbox = emit_marker(
745
                        &segments[i - 1],
746
                        MarkerEndpoint::End,
747
                        MarkerType::End,
748
                        angle,
749
                        emit_fn,
750
                    )?;
751
                    bbox.insert(&marker_bbox);
752
                }
753

            
754
                // Render marker for the lone point; no directionality
755
                let marker_bbox = emit_marker(
756
                    segment,
757
                    MarkerEndpoint::Start,
758
                    MarkerType::Middle,
759
                    Angle::new(0.0),
760
                    emit_fn,
761
                )?;
762
                bbox.insert(&marker_bbox);
763

            
764
                subpath_state = SubpathState::NoSubpath;
765
            }
766

            
767
            Segment::LineOrCurve { .. } => {
768
                // Not a degenerate segment
769
173
                match subpath_state {
770
                    SubpathState::NoSubpath => {
771
64
                        let angle = segments
772
                            .find_outgoing_angle_forwards(i)
773
1
                            .unwrap_or_else(|| Angle::new(0.0));
774
64
                        let marker_bbox = emit_marker(
775
                            segment,
776
64
                            MarkerEndpoint::Start,
777
64
                            MarkerType::Start,
778
                            angle,
779
                            emit_fn,
780
                        )?;
781
64
                        bbox.insert(&marker_bbox);
782

            
783
64
                        subpath_state = SubpathState::InSubpath;
784
64
                    }
785

            
786
                    SubpathState::InSubpath => {
787
109
                        assert!(i > 0);
788

            
789
109
                        let incoming = segments.find_incoming_angle_backwards(i - 1);
790
109
                        let outgoing = segments.find_outgoing_angle_forwards(i);
791

            
792
109
                        let angle = match (incoming, outgoing) {
793
107
                            (Some(incoming), Some(outgoing)) => incoming.bisect(outgoing),
794
2
                            (Some(incoming), _) => incoming,
795
                            (_, Some(outgoing)) => outgoing,
796
                            _ => Angle::new(0.0),
797
                        };
798

            
799
109
                        let marker_bbox = emit_marker(
800
                            segment,
801
109
                            MarkerEndpoint::Start,
802
109
                            MarkerType::Middle,
803
109
                            angle,
804
                            emit_fn,
805
                        )?;
806
109
                        bbox.insert(&marker_bbox);
807
                    }
808
                }
809
            }
810
        }
811
    }
812

            
813
    // Finally, render the last point
814
64
    if !segments.is_empty() {
815
64
        let segment = &segments[segments.len() - 1];
816
64
        if let Segment::LineOrCurve { .. } = *segment {
817
64
            let incoming = segments
818
64
                .find_incoming_angle_backwards(segments.len() - 1)
819
1
                .unwrap_or_else(|| Angle::new(0.0));
820

            
821
            let angle = {
822
64
                if let PathCommand::ClosePath = path.iter().nth(segments.len()).unwrap() {
823
13
                    let outgoing = segments
824
                        .find_outgoing_angle_forwards(0)
825
                        .unwrap_or_else(|| Angle::new(0.0));
826
13
                    incoming.bisect(outgoing)
827
                } else {
828
51
                    incoming
829
                }
830
            };
831

            
832
64
            let marker_bbox = emit_marker(
833
                segment,
834
64
                MarkerEndpoint::End,
835
64
                MarkerType::End,
836
64
                angle,
837
                emit_fn,
838
            )?;
839
64
            bbox.insert(&marker_bbox);
840
        }
841
    }
842

            
843
64
    Ok(bbox)
844
64
}
845

            
846
#[cfg(test)]
847
mod parser_tests {
848
    use super::*;
849

            
850
    #[test]
851
2
    fn parsing_invalid_marker_units_yields_error() {
852
1
        assert!(MarkerUnits::parse_str("").is_err());
853
1
        assert!(MarkerUnits::parse_str("foo").is_err());
854
2
    }
855

            
856
    #[test]
857
2
    fn parses_marker_units() {
858
1
        assert_eq!(
859
1
            MarkerUnits::parse_str("userSpaceOnUse").unwrap(),
860
            MarkerUnits::UserSpaceOnUse
861
        );
862
1
        assert_eq!(
863
1
            MarkerUnits::parse_str("strokeWidth").unwrap(),
864
            MarkerUnits::StrokeWidth
865
        );
866
2
    }
867

            
868
    #[test]
869
2
    fn parsing_invalid_marker_orient_yields_error() {
870
1
        assert!(MarkerOrient::parse_str("").is_err());
871
1
        assert!(MarkerOrient::parse_str("blah").is_err());
872
1
        assert!(MarkerOrient::parse_str("45blah").is_err());
873
2
    }
874

            
875
    #[test]
876
2
    fn parses_marker_orient() {
877
1
        assert_eq!(MarkerOrient::parse_str("auto").unwrap(), MarkerOrient::Auto);
878
1
        assert_eq!(
879
1
            MarkerOrient::parse_str("auto-start-reverse").unwrap(),
880
            MarkerOrient::AutoStartReverse
881
        );
882

            
883
1
        assert_eq!(
884
1
            MarkerOrient::parse_str("0").unwrap(),
885
1
            MarkerOrient::Angle(Angle::new(0.0))
886
        );
887
1
        assert_eq!(
888
1
            MarkerOrient::parse_str("180").unwrap(),
889
1
            MarkerOrient::Angle(Angle::from_degrees(180.0))
890
        );
891
1
        assert_eq!(
892
1
            MarkerOrient::parse_str("180deg").unwrap(),
893
1
            MarkerOrient::Angle(Angle::from_degrees(180.0))
894
        );
895
1
        assert_eq!(
896
1
            MarkerOrient::parse_str("-400grad").unwrap(),
897
1
            MarkerOrient::Angle(Angle::from_degrees(-360.0))
898
        );
899
1
        assert_eq!(
900
1
            MarkerOrient::parse_str("1rad").unwrap(),
901
1
            MarkerOrient::Angle(Angle::new(1.0))
902
        );
903
2
    }
904
}
905

            
906
#[cfg(test)]
907
mod directionality_tests {
908
    use super::*;
909
    use crate::path_builder::PathBuilder;
910

            
911
    // Single open path; the easy case
912
1
    fn setup_open_path() -> Segments {
913
1
        let mut builder = PathBuilder::default();
914

            
915
1
        builder.move_to(10.0, 10.0);
916
1
        builder.line_to(20.0, 10.0);
917
1
        builder.line_to(20.0, 20.0);
918

            
919
1
        Segments::from(&builder.into_path())
920
1
    }
921

            
922
    #[test]
923
2
    fn path_to_segments_handles_open_path() {
924
2
        let expected_segments: Segments = Segments(vec![
925
1
            Segment::line(10.0, 10.0, 20.0, 10.0),
926
1
            Segment::line(20.0, 10.0, 20.0, 20.0),
927
        ]);
928

            
929
2
        assert_eq!(setup_open_path(), expected_segments);
930
2
    }
931

            
932
1
    fn setup_multiple_open_subpaths() -> Segments {
933
1
        let mut builder = PathBuilder::default();
934

            
935
1
        builder.move_to(10.0, 10.0);
936
1
        builder.line_to(20.0, 10.0);
937
1
        builder.line_to(20.0, 20.0);
938

            
939
1
        builder.move_to(30.0, 30.0);
940
1
        builder.line_to(40.0, 30.0);
941
1
        builder.curve_to(50.0, 35.0, 60.0, 60.0, 70.0, 70.0);
942
1
        builder.line_to(80.0, 90.0);
943

            
944
1
        Segments::from(&builder.into_path())
945
1
    }
946

            
947
    #[test]
948
2
    fn path_to_segments_handles_multiple_open_subpaths() {
949
2
        let expected_segments: Segments = Segments(vec![
950
1
            Segment::line(10.0, 10.0, 20.0, 10.0),
951
1
            Segment::line(20.0, 10.0, 20.0, 20.0),
952
1
            Segment::line(30.0, 30.0, 40.0, 30.0),
953
1
            Segment::curve(40.0, 30.0, 50.0, 35.0, 60.0, 60.0, 70.0, 70.0),
954
1
            Segment::line(70.0, 70.0, 80.0, 90.0),
955
        ]);
956

            
957
2
        assert_eq!(setup_multiple_open_subpaths(), expected_segments);
958
2
    }
959

            
960
    // Closed subpath; must have a line segment back to the first point
961
1
    fn setup_closed_subpath() -> Segments {
962
1
        let mut builder = PathBuilder::default();
963

            
964
1
        builder.move_to(10.0, 10.0);
965
1
        builder.line_to(20.0, 10.0);
966
1
        builder.line_to(20.0, 20.0);
967
1
        builder.close_path();
968

            
969
1
        Segments::from(&builder.into_path())
970
1
    }
971

            
972
    #[test]
973
2
    fn path_to_segments_handles_closed_subpath() {
974
2
        let expected_segments: Segments = Segments(vec![
975
1
            Segment::line(10.0, 10.0, 20.0, 10.0),
976
1
            Segment::line(20.0, 10.0, 20.0, 20.0),
977
1
            Segment::line(20.0, 20.0, 10.0, 10.0),
978
        ]);
979

            
980
2
        assert_eq!(setup_closed_subpath(), expected_segments);
981
2
    }
982

            
983
    // Multiple closed subpaths; each must have a line segment back to their
984
    // initial points, with no degenerate segments between subpaths.
985
1
    fn setup_multiple_closed_subpaths() -> Segments {
986
1
        let mut builder = PathBuilder::default();
987

            
988
1
        builder.move_to(10.0, 10.0);
989
1
        builder.line_to(20.0, 10.0);
990
1
        builder.line_to(20.0, 20.0);
991
1
        builder.close_path();
992

            
993
1
        builder.move_to(30.0, 30.0);
994
1
        builder.line_to(40.0, 30.0);
995
1
        builder.curve_to(50.0, 35.0, 60.0, 60.0, 70.0, 70.0);
996
1
        builder.line_to(80.0, 90.0);
997
1
        builder.close_path();
998

            
999
1
        Segments::from(&builder.into_path())
1
    }
    #[test]
2
    fn path_to_segments_handles_multiple_closed_subpaths() {
2
        let expected_segments: Segments = Segments(vec![
1
            Segment::line(10.0, 10.0, 20.0, 10.0),
1
            Segment::line(20.0, 10.0, 20.0, 20.0),
1
            Segment::line(20.0, 20.0, 10.0, 10.0),
1
            Segment::line(30.0, 30.0, 40.0, 30.0),
1
            Segment::curve(40.0, 30.0, 50.0, 35.0, 60.0, 60.0, 70.0, 70.0),
1
            Segment::line(70.0, 70.0, 80.0, 90.0),
1
            Segment::line(80.0, 90.0, 30.0, 30.0),
        ]);
2
        assert_eq!(setup_multiple_closed_subpaths(), expected_segments);
2
    }
    // A lineto follows the first closed subpath, with no moveto to start the second subpath.
    // The lineto must start at the first point of the first subpath.
1
    fn setup_no_moveto_after_closepath() -> Segments {
1
        let mut builder = PathBuilder::default();
1
        builder.move_to(10.0, 10.0);
1
        builder.line_to(20.0, 10.0);
1
        builder.line_to(20.0, 20.0);
1
        builder.close_path();
1
        builder.line_to(40.0, 30.0);
1
        Segments::from(&builder.into_path())
1
    }
    #[test]
2
    fn path_to_segments_handles_no_moveto_after_closepath() {
2
        let expected_segments: Segments = Segments(vec![
1
            Segment::line(10.0, 10.0, 20.0, 10.0),
1
            Segment::line(20.0, 10.0, 20.0, 20.0),
1
            Segment::line(20.0, 20.0, 10.0, 10.0),
1
            Segment::line(10.0, 10.0, 40.0, 30.0),
        ]);
2
        assert_eq!(setup_no_moveto_after_closepath(), expected_segments);
2
    }
    // Sequence of moveto; should generate degenerate points.
    // This test is not enabled right now!  We create the
    // path fixtures with Cairo, and Cairo compresses
    // sequences of moveto into a single one.  So, we can't
    // really test this, as we don't get the fixture we want.
    //
    // Eventually we'll probably have to switch librsvg to
    // its own internal path representation which should
    // allow for unelided path commands, and which should
    // only build a cairo_path_t for the final rendering step.
    //
    // fn setup_sequence_of_moveto () -> Segments {
    // let mut builder = PathBuilder::default ();
    //
    // builder.move_to (10.0, 10.0);
    // builder.move_to (20.0, 20.0);
    // builder.move_to (30.0, 30.0);
    // builder.move_to (40.0, 40.0);
    //
    // Segments::from(&builder.into_path())
    // }
    //
    // #[test]
    // fn path_to_segments_handles_sequence_of_moveto () {
    // let expected_segments: Segments = Segments(vec! [
    // Segment::degenerate(10.0, 10.0),
    // Segment::degenerate(20.0, 20.0),
    // Segment::degenerate(30.0, 30.0),
    // Segment::degenerate(40.0, 40.0),
    // ]);
    //
    // assert_eq!(setup_sequence_of_moveto(), expected_segments);
    // }
    #[test]
2
    fn degenerate_segment_has_no_directionality() {
1
        let s = Segment::degenerate(1.0, 2.0);
1
        assert!(s.get_directionalities().is_none());
2
    }
    #[test]
2
    fn line_segment_has_directionality() {
1
        let s = Segment::line(1.0, 2.0, 3.0, 4.0);
1
        let (v1x, v1y, v2x, v2y) = s.get_directionalities().unwrap();
1
        assert_eq!((2.0, 2.0), (v1x, v1y));
1
        assert_eq!((2.0, 2.0), (v2x, v2y));
2
    }
    #[test]
2
    fn line_segment_with_coincident_ends_has_no_directionality() {
1
        let s = Segment::line(1.0, 2.0, 1.0, 2.0);
1
        assert!(s.get_directionalities().is_none());
2
    }
    #[test]
2
    fn curve_has_directionality() {
1
        let s = Segment::curve(1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 20.0, 33.0);
1
        let (v1x, v1y, v2x, v2y) = s.get_directionalities().unwrap();
1
        assert_eq!((2.0, 3.0), (v1x, v1y));
1
        assert_eq!((12.0, 20.0), (v2x, v2y));
2
    }
    #[test]
2
    fn curves_with_loops_and_coincident_ends_have_directionality() {
1
        let s = Segment::curve(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0);
1
        let (v1x, v1y, v2x, v2y) = s.get_directionalities().unwrap();
1
        assert_eq!((2.0, 2.0), (v1x, v1y));
1
        assert_eq!((-4.0, -4.0), (v2x, v2y));
1
        let s = Segment::curve(1.0, 2.0, 1.0, 2.0, 3.0, 4.0, 1.0, 2.0);
1
        let (v1x, v1y, v2x, v2y) = s.get_directionalities().unwrap();
1
        assert_eq!((2.0, 2.0), (v1x, v1y));
1
        assert_eq!((-2.0, -2.0), (v2x, v2y));
1
        let s = Segment::curve(1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 1.0, 2.0);
1
        let (v1x, v1y, v2x, v2y) = s.get_directionalities().unwrap();
1
        assert_eq!((2.0, 2.0), (v1x, v1y));
1
        assert_eq!((-2.0, -2.0), (v2x, v2y));
2
    }
    #[test]
2
    fn curve_with_coincident_control_points_has_no_directionality() {
1
        let s = Segment::curve(1.0, 2.0, 1.0, 2.0, 1.0, 2.0, 1.0, 2.0);
1
        assert!(s.get_directionalities().is_none());
2
    }
    #[test]
2
    fn curve_with_123_coincident_has_directionality() {
1
        let s = Segment::curve(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20.0, 40.0);
1
        let (v1x, v1y, v2x, v2y) = s.get_directionalities().unwrap();
1
        assert_eq!((20.0, 40.0), (v1x, v1y));
1
        assert_eq!((20.0, 40.0), (v2x, v2y));
2
    }
    #[test]
2
    fn curve_with_234_coincident_has_directionality() {
1
        let s = Segment::curve(20.0, 40.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
1
        let (v1x, v1y, v2x, v2y) = s.get_directionalities().unwrap();
1
        assert_eq!((-20.0, -40.0), (v1x, v1y));
1
        assert_eq!((-20.0, -40.0), (v2x, v2y));
2
    }
    #[test]
2
    fn curve_with_12_34_coincident_has_directionality() {
1
        let s = Segment::curve(20.0, 40.0, 20.0, 40.0, 60.0, 70.0, 60.0, 70.0);
1
        let (v1x, v1y, v2x, v2y) = s.get_directionalities().unwrap();
1
        assert_eq!((40.0, 30.0), (v1x, v1y));
1
        assert_eq!((40.0, 30.0), (v2x, v2y));
2
    }
}
#[cfg(test)]
mod marker_tests {
    use super::*;
    use crate::path_builder::PathBuilder;
    #[test]
2
    fn emits_for_open_subpath() {
1
        let mut builder = PathBuilder::default();
1
        builder.move_to(0.0, 0.0);
1
        builder.line_to(1.0, 0.0);
1
        builder.line_to(1.0, 1.0);
1
        builder.line_to(0.0, 1.0);
1
        let mut v = Vec::new();
1
        assert!(emit_markers_for_path(
1
            &builder.into_path(),
1
            BoundingBox::new(),
5
            &mut |marker_type: MarkerType,
                  x: f64,
                  y: f64,
                  computed_angle: Angle|
             -> Result<BoundingBox, InternalRenderingError> {
4
                v.push((marker_type, x, y, computed_angle));
4
                Ok(BoundingBox::new())
4
            }
        )
        .is_ok());
2
        assert_eq!(
            v,
2
            vec![
1
                (MarkerType::Start, 0.0, 0.0, Angle::new(0.0)),
1
                (MarkerType::Middle, 1.0, 0.0, Angle::from_vector(1.0, 1.0)),
1
                (MarkerType::Middle, 1.0, 1.0, Angle::from_vector(-1.0, 1.0)),
1
                (MarkerType::End, 0.0, 1.0, Angle::from_vector(-1.0, 0.0)),
            ]
        );
2
    }
    #[test]
2
    fn emits_for_closed_subpath() {
1
        let mut builder = PathBuilder::default();
1
        builder.move_to(0.0, 0.0);
1
        builder.line_to(1.0, 0.0);
1
        builder.line_to(1.0, 1.0);
1
        builder.line_to(0.0, 1.0);
1
        builder.close_path();
1
        let mut v = Vec::new();
1
        assert!(emit_markers_for_path(
1
            &builder.into_path(),
1
            BoundingBox::new(),
6
            &mut |marker_type: MarkerType,
                  x: f64,
                  y: f64,
                  computed_angle: Angle|
             -> Result<BoundingBox, InternalRenderingError> {
5
                v.push((marker_type, x, y, computed_angle));
5
                Ok(BoundingBox::new())
5
            }
        )
        .is_ok());
2
        assert_eq!(
            v,
2
            vec![
1
                (MarkerType::Start, 0.0, 0.0, Angle::new(0.0)),
1
                (MarkerType::Middle, 1.0, 0.0, Angle::from_vector(1.0, 1.0)),
1
                (MarkerType::Middle, 1.0, 1.0, Angle::from_vector(-1.0, 1.0)),
1
                (MarkerType::Middle, 0.0, 1.0, Angle::from_vector(-1.0, -1.0)),
1
                (MarkerType::End, 0.0, 0.0, Angle::from_vector(1.0, -1.0)),
            ]
        );
2
    }
}