-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrvg-facade-stencil-compound.h
322 lines (278 loc) · 9.6 KB
/
rvg-facade-stencil-compound.h
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// 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
//
#ifndef RVG_FACADE_STENCIL_COMPOUND_H
#define RVG_FACADE_STENCIL_COMPOUND_H
#include <vector>
#include "rvg-xform.h"
#include "rvg-i-xformable.h"
#include "rvg-shape.h"
#include "rvg-meta.h"
#include "rvg-winding-rule.h"
#include "rvg-stencil-shape-data.h"
namespace rvg_facade {
class stencil_compound final: public rvg::i_xformable<stencil_compound> {
using xformable_base = rvg::i_xformable<stencil_compound>;
public:
enum class e_type {
stencil_shape,
xformed,
clipped,
empty,
};
class xformed_data {
std::vector<stencil_compound> m_stencil;
public:
template <typename S>
xformed_data(S &&s,
typename std::enable_if<
rvg::meta::forward_same_or_convertible<S,
std::vector<stencil_compound>>::value
>::type * = nullptr):
m_stencil(std::forward<S>(s)) { ; }
const std::vector<stencil_compound> &get_stencil(void) const {
return m_stencil;
}
};
class clipped_data {
std::vector<stencil_compound> m_clipper;
std::vector<stencil_compound> m_clippee;
public:
template <typename ER, typename EE>
clipped_data(ER &&clipper, EE &&clippee,
typename std::enable_if<
rvg::meta::forward_same_or_convertible<ER,
std::vector<stencil_compound>>::value &&
rvg::meta::forward_same_or_convertible<EE,
std::vector<stencil_compound>>::value
>::type * = nullptr):
m_clipper(std::forward<ER>(clipper)),
m_clippee(std::forward<EE>(clippee)) { ; }
const std::vector<stencil_compound> &get_clipper(void) const {
return m_clipper;
}
const std::vector<stencil_compound> &get_clippee(void) const {
return m_clippee;
}
};
private:
e_type m_type;
//??D Change to std::variant when we switch to C++17?
union data {
data() { ; }
~data() { ; }
rvg::stencil_shape_data stencil_shape;
xformed_data xformed;
clipped_data clipped;
} m_union;
public:
stencil_compound(void): m_type(e_type::empty) { ; }
template <typename S>
stencil_compound(S &&sd, typename std::enable_if<
std::is_same<
typename std::remove_reference<S>::type,
rvg::stencil_shape_data
>::value>::type * = nullptr):
m_type(e_type::stencil_shape) {
new (&m_union.stencil_shape) rvg::stencil_shape_data{std::forward<S>(sd)};
}
template <typename C>
stencil_compound(C &&c, typename std::enable_if<
std::is_same<
typename std::remove_reference<C>::type,
clipped_data
>::value>::type * = nullptr):
m_type(e_type::clipped) {
new (&m_union.clipped) clipped_data{std::forward<C>(c)};
}
template <typename X>
stencil_compound(X &&t, typename std::enable_if<
std::is_same<
typename std::remove_reference<X>::type,
xformed_data
>::value>::type * = nullptr):
m_type(e_type::xformed) {
new (&m_union.xformed) xformed_data{std::forward<X>(t)};
}
stencil_compound(stencil_compound &&other):
xformable_base(std::move(other)),
m_type(std::move(other.m_type)) {
switch (m_type) {
case e_type::stencil_shape:
new (&m_union.stencil_shape)
rvg::stencil_shape_data{std::move(other.m_union.stencil_shape)};
break;
case e_type::xformed:
new (&m_union.xformed)
xformed_data{std::move(other.m_union.xformed)};
break;
case e_type::clipped:
new (&m_union.clipped)
clipped_data{std::move(other.m_union.clipped)};
break;
case e_type::empty:
break;
}
}
stencil_compound(const stencil_compound &other):
xformable_base(other),
m_type(other.m_type) {
switch (m_type) {
case e_type::stencil_shape:
new (&m_union.stencil_shape)
rvg::stencil_shape_data{other.m_union.stencil_shape};
break;
case e_type::xformed:
new (&m_union.xformed) xformed_data(other.m_union.xformed);
break;
case e_type::clipped:
new (&m_union.clipped) clipped_data(other.m_union.clipped);
break;
case e_type::empty:
break;
}
}
~stencil_compound() {
using rvg::stencil_shape_data;
switch (m_type) {
case e_type::stencil_shape:
m_union.stencil_shape.~stencil_shape_data();
break;
case e_type::xformed:
m_union.xformed.~xformed_data();
break;
case e_type::clipped:
m_union.clipped.~clipped_data();
break;
case e_type::empty:
break;
}
}
stencil_compound &operator=(stencil_compound &&other) {
if (this != &other) {
if (m_type == other.m_type) {
xformable_base::operator=(std::move(other));
switch (m_type) {
case e_type::stencil_shape:
m_union.stencil_shape =
std::move(other.m_union.stencil_shape);
break;
case e_type::xformed:
m_union.xformed = std::move(other.m_union.xformed);
break;
case e_type::clipped:
m_union.clipped = std::move(other.m_union.clipped);
break;
case e_type::empty:
break;
}
} else {
this->~stencil_compound();
new (this) stencil_compound(std::move(other));
}
}
return *this;
}
stencil_compound &operator=(const stencil_compound &other) {
if (m_type == other.m_type) {
xformable_base::operator=(other);
switch (m_type) {
case e_type::stencil_shape:
m_union.stencil_shape = other.m_union.stencil_shape;
break;
case e_type::xformed:
m_union.xformed = other.m_union.xformed;
break;
case e_type::clipped:
m_union.clipped = other.m_union.clipped;
break;
case e_type::empty:
break;
}
} else {
this->~stencil_compound();
new (this) stencil_compound(other);
}
return *this;
}
const e_type &get_type(void) const {
return m_type;
}
bool is_stencil_shape(void) const {
return m_type == e_type::stencil_shape;
}
bool is_xformed(void) const {
return m_type == e_type::xformed;
}
bool is_clipped(void) const {
return m_type == e_type::clipped;
}
bool is_empty(void) const {
return m_type == e_type::empty;
}
const rvg::stencil_shape_data &get_stencil_shape(void) const {
assert(is_stencil_shape());
return m_union.stencil_shape;
}
const xformed_data &get_xformed(void) const {
assert(is_xformed());
return m_union.xformed;
}
const clipped_data &get_clipped(void) const {
assert(is_clipped());
return m_union.clipped;
}
};
template <typename S>
stencil_compound make_stencil_compound_stencil_shape(rvg::e_winding_rule r, S &&s,
typename std::enable_if<
rvg::meta::forward_same_or_convertible<S, rvg::shape>::value
>::type * = nullptr) {
return stencil_compound{
rvg::stencil_shape_data{r,
rvg::make_intrusive<rvg::shape>(std::forward<S>(s)) } };
}
template <typename R, typename P>
stencil_compound make_stencil_compound_clipped(R &&clipper, P &&clippee,
typename std::enable_if<
rvg::meta::forward_same_or_convertible<
R,
std::vector<stencil_compound>
>::value &&
rvg::meta::forward_same_or_convertible<
P,
std::vector<stencil_compound>
>::value
>::type * = nullptr) {
return stencil_compound{ stencil_compound::clipped_data{
std::forward<R>(clipper), std::forward<P>(clippee)} };
}
template <typename X, typename S>
stencil_compound make_stencil_compound_xformed(X &xf, S &&s,
typename std::enable_if<
rvg::meta::forward_same_or_convertible<
X,
rvg::xform
>::value &&
rvg::meta::forward_same_or_convertible<
S,
std::vector<stencil_compound>
>::value
>::type * = nullptr) {
return stencil_compound{ stencil_compound::xformed_data{
std::forward<S>(s)} }.transformed(xf);
}
} // namespace rvg_facade
#endif