summaryrefslogtreecommitdiff
path: root/src/backend_qt/stroke.rs
blob: 692169d7621f06cead65ea12c4cee723dd429d0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// external
use qt;
use usvg;

// self
use super::prelude::*;
use super::{
    gradient,
    pattern,
};


pub fn apply(
    tree: &usvg::Tree,
    stroke: &Option<usvg::Stroke>,
    opt: &Options,
    bbox: Rect,
    p: &qt::Painter,
) {
    match *stroke {
        Some(ref stroke) => {
            let mut pen = qt::Pen::new();
            let opacity = stroke.opacity;

            match stroke.paint {
                usvg::Paint::Color(c) => {
                    // a-stroke-opacity-001.svg
                    // a-stroke-opacity-002.svg
                    // a-stroke-opacity-006.svg
                    let a = f64_bound(0.0, *opacity * 255.0, 255.0) as u8;
                    pen.set_color(c.red, c.green, c.blue, a);
                }
                usvg::Paint::Link(ref id) => {
                    // a-stroke-002.svg
                    // a-stroke-003.svg
                    // a-stroke-004.svg
                    // a-stroke-007.svg
                    // a-stroke-008.svg
                    // a-stroke-009.svg
                    let mut brush = qt::Brush::new();

                    if let Some(node) = tree.defs_by_id(id) {
                        match *node.borrow() {
                            usvg::NodeKind::LinearGradient(ref lg) => {
                                gradient::prepare_linear(lg, opacity, bbox, &mut brush);
                            }
                            usvg::NodeKind::RadialGradient(ref rg) => {
                                gradient::prepare_radial(rg, opacity, bbox, &mut brush);
                            }
                            usvg::NodeKind::Pattern(ref pattern) => {
                                let ts = p.get_transform();
                                pattern::apply(&node, pattern, opt, ts, bbox, opacity, &mut brush);
                            }
                            _ => {}
                        }
                    }

                    pen.set_brush(brush);
                }
            }

            // a-stroke-linecap-001.svg
            // a-stroke-linecap-002.svg
            // a-stroke-linecap-003.svg
            let linecap = match stroke.linecap {
                usvg::LineCap::Butt => qt::LineCap::FlatCap,
                usvg::LineCap::Round => qt::LineCap::RoundCap,
                usvg::LineCap::Square => qt::LineCap::SquareCap,
            };
            pen.set_line_cap(linecap);

            // a-stroke-linejoin-001.svg
            // a-stroke-linejoin-002.svg
            // a-stroke-linejoin-003.svg
            let linejoin = match stroke.linejoin {
                usvg::LineJoin::Miter => qt::LineJoin::MiterJoin,
                usvg::LineJoin::Round => qt::LineJoin::RoundJoin,
                usvg::LineJoin::Bevel => qt::LineJoin::BevelJoin,
            };
            pen.set_line_join(linejoin);

            // a-stroke-miterlimit-002.svg
            pen.set_miter_limit(stroke.miterlimit);
            // a-stroke-width-002.svg
            pen.set_width(stroke.width);

            // a-stroke-dasharray-001.svg
            // a-stroke-dasharray-002.svg
            // a-stroke-dashoffset-001.svg
            // a-stroke-dashoffset-002.svg
            // a-stroke-dashoffset-006.svg
            if let Some(ref list) = stroke.dasharray {
                pen.set_dash_offset(stroke.dashoffset);
                pen.set_dash_array(list);
            }

            p.set_pen(pen);
        }
        None => {
            // a-stroke-006.svg
            p.reset_pen();
        }
    }
}