-
Notifications
You must be signed in to change notification settings - Fork 1
/
CairoPaintedItem.cpp
72 lines (53 loc) · 1.58 KB
/
CairoPaintedItem.cpp
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
#include "cairo/cairo.h"
#include <QPainter>
#include "CairoPaintedItem.hpp"
struct CairoPaintedItem::S
{
static inline std::size_t size_;
static inline unsigned char* data_;
};
//////////////////////////////////////////////////////////////////////////////
CairoPaintedItem::~CairoPaintedItem() { cairo_destroy(cr_); }
//////////////////////////////////////////////////////////////////////////////
void CairoPaintedItem::init(cairo_t* const cr, int, int)
{
cairo_set_line_width(cr, 1.);
cairo_translate(cr, .5, .5);
}
//////////////////////////////////////////////////////////////////////////////
void CairoPaintedItem::paint(QPainter* const p)
{
auto d(S::data_);
int const w(width()), h(height());
{
auto cr(cr_);
if ((w != w_) || (h != h_) || (d != d_))
{
w_ = w; h_ = h;
//
auto const stride(stride_ =
cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
if (auto const size(h * std::size_t(stride)); S::size_ < size)
{
delete [] d; S::data_ = d = new unsigned char[S::size_ = size];
}
d_ = d; // multiple instances
//
cairo_destroy(cr);
auto const srf(cairo_image_surface_create_for_data(
d, CAIRO_FORMAT_ARGB32, w, h, stride));
cr_ = cr = cairo_create(srf);
cairo_surface_destroy(srf);
//
init(cr, w, h);
}
//
cairo_save(cr);
draw(cr, w, h);
cairo_restore(cr);
}
//
p->setCompositionMode(QPainter::CompositionMode_Source);
p->drawPixmap(QPoint{}, QPixmap::fromImage(
{d, w, h, stride_, QImage::Format_ARGB32_Premultiplied}));
}