summaryrefslogtreecommitdiff
path: root/src/render_qt/clippath.rs
blob: fcd394b3a4c73700342595c6dae0107ca874bdbc (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
// 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;

// self
use tree::{
    self,
    NodeExt,
};
use math::*;
use traits::{
    ConvTransform,
    TransformFromBBox,
};
use super::{
    path,
    text,
};
use {
    Options,
};


pub fn apply(
    node: tree::NodeRef,
    cp: &tree::ClipPath,
    opt: &Options,
    bbox: Rect,
    img_size: ScreenSize,
    p: &qt::Painter,
) {
    let mut clip_img = qt::Image::new(
        img_size.width as u32,
        img_size.height as u32,
    ).unwrap(); // TODO: remove

    clip_img.fill(0, 0, 0, 255);
    clip_img.set_dpi(opt.dpi);

    let clip_p = qt::Painter::new(&clip_img);
    clip_p.set_transform(&p.get_transform());
    clip_p.apply_transform(&cp.transform.to_native());

    if cp.units == tree::Units::ObjectBoundingBox {
        clip_p.apply_transform(&qt::Transform::from_bbox(bbox));
    }

    clip_p.set_composition_mode(qt::CompositionMode::CompositionMode_Clear);

    let ts = clip_p.get_transform();
    for node in node.children() {
        clip_p.apply_transform(&node.transform().to_native());

        match *node.value() {
            tree::NodeKind::Path(ref path_node) => {
                path::draw(node.tree(), path_node, opt, &clip_p);
            }
            tree::NodeKind::Text(_) => {
                text::draw(node, opt, &clip_p);
            }
            _ => {}
        }

        clip_p.set_transform(&ts);
    }

    clip_p.end();

    p.set_transform(&qt::Transform::default());
    p.set_composition_mode(qt::CompositionMode::CompositionMode_DestinationOut);
    p.draw_image(0.0, 0.0, &clip_img);
}