summaryrefslogtreecommitdiff
path: root/src/render_qt/pattern.rs
blob: 3db3916242e8d413c4d871da46ae90d017f43e35 (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
// 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;
use math::*;
use traits::{
    ConvTransform,
    TransformFromBBox,
};
use utils;
use {
    Options,
};

pub fn apply(
    pattern_node: tree::NodeRef,
    pattern: &tree::Pattern,
    opt: &Options,
    global_ts: qt::Transform,
    bbox: Rect,
    brush: &mut qt::Brush,
) {
    let r = if pattern.units == tree::Units::ObjectBoundingBox {
        pattern.rect.transform(tree::Transform::from_bbox(bbox))
    } else {
        pattern.rect
    };

    let global_ts = tree::Transform::from_native(&global_ts);
    let (sx, sy) = global_ts.get_scale();

    let img_size = Size::new(r.width() * sx, r.height() * sy).to_screen_size();
    let img = qt::Image::new(img_size.width as u32, img_size.height as u32);
    let mut img = match img {
        Some(img) => img,
        None => {
            // TODO: add expected image size
            warn!("Subimage creation failed.");
            return;
        }
    };

    img.set_dpi(opt.dpi);
    img.fill(0, 0, 0, 0);

    let p = qt::Painter::new(&img);

    p.apply_transform(&qt::Transform::new(sx, 0.0, 0.0, sy, 0.0, 0.0));
    if let Some(vbox) = pattern.view_box {
        let img_view = Rect::from_xywh(0.0, 0.0, r.width(), r.height());
        let (dx, dy, sx2, sy2) = utils::view_box_transform(vbox, img_view);
        p.apply_transform(&qt::Transform::new(sx2, 0.0, 0.0, sy2, dx, dy));
    }
    if pattern.content_units == tree::Units::ObjectBoundingBox {
        p.apply_transform(&qt::Transform::from_bbox(bbox));
    }

    super::render_group(pattern_node, opt, img_size, &p);
    p.end();

    brush.set_pattern(img);

    let mut ts = tree::Transform::default();
    ts.append(&pattern.transform);
    ts.translate(r.x(), r.y());
    ts.scale(1.0 / sx, 1.0 / sy);
    brush.set_transform(ts.to_native());
}