-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrvg-driver-qt5.cpp
296 lines (229 loc) · 6.58 KB
/
rvg-driver-qt5.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Stroke-to-fill conversion program and test harness
// Copyright (C) 2020 Diego Nehab
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// Contact information: diego.nehab@gmail.com
//
#include <string>
#include <sstream>
#include <cmath>
#include <vector>
#include "rvg-lua.h"
#include "rvg-lua-facade.h"
#include "rvg-i-scene-data.h"
#include "rvg-input-path-f-xform.h"
#include "rvg-image.h"
#include "rvg-pngio.h"
#include "rvg-xform.h"
#include "rvg-tuple.h"
#include "rvg-bezier.h"
#include "rvg-scene.h"
#include "rvg-viewport.h"
#include "rvg-i-sink.h"
#include "rvg-driver-qt5.h"
#include "qpainter.h"
#include "qpainterpath.h"
#include "qbuffer.h"
namespace rvg { namespace driver { namespace qt5 {
class input_path_f_to_qpainter_path final :
public i_input_path<input_path_f_to_qpainter_path> {
QPainterPath *m_path;
public:
input_path_f_to_qpainter_path(QPainterPath *path) : m_path(path) { }
private:
friend i_input_path<input_path_f_to_qpainter_path>;
void do_begin_contour(float x0, float y0) {
m_path->moveTo(x0, y0);
}
void do_end_open_contour(float x0, float y0) {
(void)x0;
(void)y0;
}
void do_end_closed_contour(float x0, float y0) {
(void)x0;
(void)y0;
m_path->closeSubpath();
}
void do_linear_segment(float x0, float y0, float x1, float y1) {
(void)x0; (void)y0;
m_path->lineTo(x1, y1);
}
void do_quadratic_segment(float x0, float y0, float x1, float y1,
float x2, float y2) {
(void)x0; (void)y0;
m_path->quadTo(x1, y1, x2, y2);
}
void do_rational_quadratic_segment(float x0, float y0, float x1, float y1,
float w1, float x2, float y2) {
(void)x0; (void)y0;
(void)x1; (void)y1; (void)w1;
(void)x2; (void)y2;
// Should never be called
}
void do_cubic_segment(float x0, float y0, float x1, float y1,
float x2, float y2, float x3, float y3) {
(void)x0; (void)y0;
m_path->cubicTo(x1, y1, x2, y2, x3, y3);
}
};
class qt_scene_iterator : public i_scene_data<qt_scene_iterator> {
public:
std::vector<xform> xf_stack;
QPainter *painter;
qt_scene_iterator(QPainter *p, const xform &xf) : painter(p) { push_xf(xf);}
void push_xf(const xform &xf) {
xf_stack.push_back(top_xf() * xf);
}
void pop_xf() {
xf_stack.pop_back();
}
xform top_xf() {
static xform id;
return xf_stack.empty() ? id : xf_stack.back();
}
void set_paint(const paint &p, QBrush &brush) {
RGBA8 color = p.get_solid_color();
float r = (float)color[0] / 255.f;
float g = (float)color[1] / 255.f;
float b = (float)color[2] / 255.f;
float a = (float)color[3] / 255.f;
brush = QBrush(QColor::fromRgbF(r, g, b, a));
}
void do_painted_shape(e_winding_rule wr, const shape &s, const paint &p) {
auto pd = s.as_path_data_ptr();
QPainterPath qt_input_path;
QBrush brush;
auto iter = input_path_f_to_qpainter_path(&qt_input_path);
set_paint(p, brush);
push_xf(s.get_xf());
pd->iterate(make_input_path_f_xform(top_xf(), iter));
pop_xf();
if (wr == e_winding_rule::odd) {
qt_input_path.setFillRule(Qt::OddEvenFill);
} else {
qt_input_path.setFillRule(Qt::WindingFill);
}
painter->fillPath(qt_input_path, brush);
}
void do_tensor_product_patch(const patch<16,4> &tpp) {
(void) tpp;
}
void do_coons_patch(const patch<12,4> &cp) {
(void) cp;
}
void do_gouraud_triangle(const patch<3,3> >) {
(void) gt;
}
void do_stencil_shape(e_winding_rule wr, const shape &s) {
(void)wr;
(void)s;
}
void do_begin_clip(uint16_t depth) {
(void)depth;
}
void do_activate_clip(uint16_t depth) {
(void)depth;
}
void do_end_clip(uint16_t depth) {
(void)depth;
}
void do_begin_fade(uint16_t depth, uint8_t opacity) {
(void)depth;
(void)opacity;
}
void do_end_fade(uint16_t depth, uint8_t opacity) {
(void)depth;
(void)opacity;
}
void do_begin_blur(uint16_t depth, float radius) {
(void)depth;
(void)radius;
}
void do_end_blur(uint16_t depth, float radius) {
(void)depth;
(void)radius;
}
void do_begin_transform(uint16_t depth, const xform &xf) {
(void)depth;
push_xf(xf);
}
void do_end_transform(uint16_t depth, const xform &xf) {
(void)depth;
(void)xf;
pop_xf();
}
};
const scene &accelerate(const scene &c, const window &w,
const viewport &v) {
(void)w;
(void)v;
return c;
}
void render(const scene &c, const window &w, const viewport &v,
FILE *out, const std::vector<std::string> &args) {
(void)out; (void)args;
int xl, yb, xr, yt;
std::tie(xl, yb) = v.bl();
std::tie(xr, yt) = v.tr();
int width = std::abs(xl - xr);
int height = std::abs(yt - yb);
xform flip = make_translation(0.f, -static_cast<float>(yb)).
scaled(1.f, -1.f).translated(0.f, static_cast<float>(yt));
xform screen_xf = flip * c.get_xf().windowviewport(w, v).translated(-xl, yb);
QImage dev(QSize(width, height), QImage::Format_ARGB32);
dev.fill(0);
QPainter painter(&dev);
painter.setRenderHint(QPainter::Antialiasing);
qt_scene_iterator iter(&painter, screen_xf);
c.get_scene_data().iterate(iter);
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
dev.save(&buffer, "PNG");
fwrite(ba.data(), sizeof(char), ba.length(), out);
}
}}}
// Lua version of the accelerate function.
// Since there is no acceleration, we simply
// and return the input scene unmodified.
static int luaaccelerate(lua_State *L) {
rvg_lua_push<rvg::scene>(L, rvg::driver::qt5::accelerate(
rvg_lua_check<rvg::scene>(L, 1),
rvg_lua_check<rvg::window>(L, 2),
rvg_lua_check<rvg::viewport>(L, 3)));
return 1;
}
// Lua version of render function
static int luarender(lua_State *L) {
auto c = rvg_lua_check<rvg::scene>(L, 1);
auto w = rvg_lua_check<rvg::window>(L, 2);
auto v = rvg_lua_check<rvg::viewport>(L, 3);
rvg::driver::qt5::render(c, w, v, rvg_lua_check_file(L, 4));
return 0;
}
// List of Lua functions exported into driver table
static const luaL_Reg modrg[] = {
{"render", luarender },
{"accelerate", luaaccelerate },
{NULL, NULL}
};
// Lua function invoked to be invoked by require"driver.cairo"
extern "C"
#ifndef _WIN32
__attribute__((visibility("default")))
#else
__declspec(dllexport)
#endif
int luaopen_driver_qt5(lua_State *L) {
rvg_lua_facade_new_driver(L, modrg);
return 1;
}