Debian Codegraph @

thirdparty/thorvg/src/renderer/tvgShape.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #ifndef _TVG_SHAPE_H_
24 #define _TVG_SHAPE_H_
25
26 #include <memory.h>
27 #include "tvgMath.h"
28 #include "tvgPaint.h"
29
30
31 struct Shape::Impl
32 {
33 RenderShape rs; //shape data
34 RenderData rd = nullptr; //engine data
35 Shape* shape;
36 RenderUpdateFlag rFlag = RenderUpdateFlag::None;
37 uint8_t cFlag = CompositionFlag::Invalid;
38 uint8_t opacity; //for composition
39
40 Impl(Shape* s) : shape(s)
41 {
42 }
43
44 ~Impl()
45 {
46 if (auto renderer = PP(shape)->renderer) {
47 renderer->dispose(rd);
48 }
49 }
50
51 bool render(RenderMethod* renderer)
52 {
53 if (!rd) return false;
54
55 RenderCompositor* cmp = nullptr;
56
57 renderer->blend(PP(shape)->blendMethod);
58
59 if (cFlag) {
60 cmp = renderer->target(bounds(renderer), renderer->colorSpace(), static_cast<CompositionFlag>(cFlag));
61 renderer->beginComposite(cmp, CompositeMethod::None, opacity);
62 }
63
64 auto ret = renderer->renderShape(rd);
65 if (cmp) renderer->endComposite(cmp);
66 return ret;
67 }
68
69 bool needComposition(uint8_t opacity)
70 {
71 cFlag = CompositionFlag::Invalid;
72
73 if (opacity == 0) return false;
74
75 //Shape composition is only necessary when stroking & fill are valid.
76 if (!rs.stroke || rs.stroke->width < FLOAT_EPSILON || (!rs.stroke->fill && rs.stroke->color[3] == 0)) return false;
77 if (!rs.fill && rs.color[3] == 0) return false;
78
79 //translucent fill & stroke
80 if (opacity < 255) {
81 cFlag = CompositionFlag::Opacity;
82 return true;
83 }
84
85 //Composition test
86 const Paint* target;
87 auto method = shape->composite(&target);
88 if (!target || method == CompositeMethod::ClipPath) return false;
89 if (target->pImpl->opacity == 255 || target->pImpl->opacity == 0) {
90 if (target->type() == Type::Shape) {
91 auto shape = static_cast<const Shape*>(target);
92 if (!shape->fill()) {
93 uint8_t r, g, b, a;
94 shape->fillColor(&r, &g, &b, &a);
95 if (a == 0 || a == 255) {
96 if (method == CompositeMethod::LumaMask || method == CompositeMethod::InvLumaMask) {
97 if ((r == 255 && g == 255 && b == 255) || (r == 0 && g == 0 && b == 0)) return false;
98 } else return false;
99 }
100 }
101 }
102 }
103
104 cFlag = CompositionFlag::Masking;
105 return true;
106 }
107
108 RenderData update(RenderMethod* renderer, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag pFlag, bool clipper)
109 {
110 if (static_cast<RenderUpdateFlag>(pFlag | rFlag) == RenderUpdateFlag::None) return rd;
111
112 if (needComposition(opacity)) {
113 /* Overriding opacity value. If this scene is half-translucent,
114 It must do intermediate composition with that opacity value. */
115 this->opacity = opacity;
116 opacity = 255;
117 }
118
119 rd = renderer->prepare(rs, rd, transform, clips, opacity, static_cast<RenderUpdateFlag>(pFlag | rFlag), clipper);
120 rFlag = RenderUpdateFlag::None;
121 return rd;
122 }
123
124 RenderRegion bounds(RenderMethod* renderer)
125 {
126 if (!rd) return {0, 0, 0, 0};
127 return renderer->region(rd);
128 }
129
130 bool bounds(float* x, float* y, float* w, float* h, bool stroking)
131 {
132 //Path bounding size
133 if (rs.path.pts.count > 0 ) {
134 auto pts = rs.path.pts.begin();
135 Point min = { pts->x, pts->y };
136 Point max = { pts->x, pts->y };
137
138 for (auto pts2 = pts + 1; pts2 < rs.path.pts.end(); ++pts2) {
139 if (pts2->x < min.x) min.x = pts2->x;
140 if (pts2->y < min.y) min.y = pts2->y;
141 if (pts2->x > max.x) max.x = pts2->x;
142 if (pts2->y > max.y) max.y = pts2->y;
143 }
144
145 if (x) *x = min.x;
146 if (y) *y = min.y;
147 if (w) *w = max.x - min.x;
148 if (h) *h = max.y - min.y;
149 }
150
151 //Stroke feathering
152 if (stroking && rs.stroke) {
153 if (x) *x -= rs.stroke->width * 0.5f;
154 if (y) *y -= rs.stroke->width * 0.5f;
155 if (w) *w += rs.stroke->width;
156 if (h) *h += rs.stroke->width;
157 }
158 return rs.path.pts.count > 0 ? true : false;
159 }
160
161 void reserveCmd(uint32_t cmdCnt)
162 {
163 rs.path.cmds.reserve(cmdCnt);
164 }
165
166 void reservePts(uint32_t ptsCnt)
167 {
168 rs.path.pts.reserve(ptsCnt);
169 }
170
171 void grow(uint32_t cmdCnt, uint32_t ptsCnt)
172 {
173 rs.path.cmds.grow(cmdCnt);
174 rs.path.pts.grow(ptsCnt);
175 }
176
177 void append(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt)
178 {
179 memcpy(rs.path.cmds.end(), cmds, sizeof(PathCommand) * cmdCnt);
180 memcpy(rs.path.pts.end(), pts, sizeof(Point) * ptsCnt);
181 rs.path.cmds.count += cmdCnt;
182 rs.path.pts.count += ptsCnt;
183 }
184
185 void moveTo(float x, float y)
186 {
187 rs.path.cmds.push(PathCommand::MoveTo);
188 rs.path.pts.push({x, y});
189 }
190
191 void lineTo(float x, float y)
192 {
193 rs.path.cmds.push(PathCommand::LineTo);
194 rs.path.pts.push({x, y});
195 }
196
197 void cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y)
198 {
199 rs.path.cmds.push(PathCommand::CubicTo);
200 rs.path.pts.push({cx1, cy1});
201 rs.path.pts.push({cx2, cy2});
202 rs.path.pts.push({x, y});
203 }
204
205 void close()
206 {
207 //Don't close multiple times.
208 if (rs.path.cmds.count > 0 && rs.path.cmds.last() == PathCommand::Close) return;
209
210 rs.path.cmds.push(PathCommand::Close);
211 }
212
213 void strokeWidth(float width)
214 {
215 if (!rs.stroke) rs.stroke = new RenderStroke();
216 rs.stroke->width = width;
217 rFlag |= RenderUpdateFlag::Stroke;
218 }
219
220 void strokeTrim(float begin, float end, bool simultaneous)
221 {
222 if (!rs.stroke) {
223 if (begin == 0.0f && end == 1.0f) return;
224 rs.stroke = new RenderStroke();
225 }
226
227 if (tvg::equal(rs.stroke->trim.begin, begin) && tvg::equal(rs.stroke->trim.end, end) &&
228 rs.stroke->trim.simultaneous == simultaneous) return;
229
230 rs.stroke->trim.begin = begin;
231 rs.stroke->trim.end = end;
232 rs.stroke->trim.simultaneous = simultaneous;
233 rFlag |= RenderUpdateFlag::Stroke;
234 }
235
236 bool strokeTrim(float* begin, float* end)
237 {
238 if (rs.stroke) {
239 if (begin) *begin = rs.stroke->trim.begin;
240 if (end) *end = rs.stroke->trim.end;
241 return rs.stroke->trim.simultaneous;
242 } else {
243 if (begin) *begin = 0.0f;
244 if (end) *end = 1.0f;
245 return false;
246 }
247 }
248
249 void strokeCap(StrokeCap cap)
250 {
251 if (!rs.stroke) rs.stroke = new RenderStroke();
252 rs.stroke->cap = cap;
253 rFlag |= RenderUpdateFlag::Stroke;
254 }
255
256 void strokeJoin(StrokeJoin join)
257 {
258 if (!rs.stroke) rs.stroke = new RenderStroke();
259 rs.stroke->join = join;
260 rFlag |= RenderUpdateFlag::Stroke;
261 }
262
263 void strokeMiterlimit(float miterlimit)
264 {
265 if (!rs.stroke) rs.stroke = new RenderStroke();
266 rs.stroke->miterlimit = miterlimit;
267 rFlag |= RenderUpdateFlag::Stroke;
268 }
269
270 void strokeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
271 {
272 if (!rs.stroke) rs.stroke = new RenderStroke();
273 if (rs.stroke->fill) {
274 delete(rs.stroke->fill);
275 rs.stroke->fill = nullptr;
276 rFlag |= RenderUpdateFlag::GradientStroke;
277 }
278
279 rs.stroke->color[0] = r;
280 rs.stroke->color[1] = g;
281 rs.stroke->color[2] = b;
282 rs.stroke->color[3] = a;
283
284 rFlag |= RenderUpdateFlag::Stroke;
285 }
286
287 Result strokeFill(unique_ptr<Fill> f)
288 {
289 auto p = f.release();
290 if (!p) return Result::MemoryCorruption;
291
292 if (!rs.stroke) rs.stroke = new RenderStroke();
293 if (rs.stroke->fill && rs.stroke->fill != p) delete(rs.stroke->fill);
294 rs.stroke->fill = p;
295 rs.stroke->color[3] = 0;
296
297 rFlag |= RenderUpdateFlag::Stroke;
298 rFlag |= RenderUpdateFlag::GradientStroke;
299
300 return Result::Success;
301 }
302
303 Result strokeDash(const float* pattern, uint32_t cnt, float offset)
304 {
305 if ((cnt == 1) || (!pattern && cnt > 0) || (pattern && cnt == 0)) {
306 return Result::InvalidArguments;
307 }
308
309 for (uint32_t i = 0; i < cnt; i++) {
310 if (pattern[i] < FLOAT_EPSILON) return Result::InvalidArguments;
311 }
312
313 //Reset dash
314 if (!pattern && cnt == 0) {
315 free(rs.stroke->dashPattern);
316 rs.stroke->dashPattern = nullptr;
317 } else {
318 if (!rs.stroke) rs.stroke = new RenderStroke();
319 if (rs.stroke->dashCnt != cnt) {
320 free(rs.stroke->dashPattern);
321 rs.stroke->dashPattern = nullptr;
322 }
323 if (!rs.stroke->dashPattern) {
324 rs.stroke->dashPattern = static_cast<float*>(malloc(sizeof(float) * cnt));
325 if (!rs.stroke->dashPattern) return Result::FailedAllocation;
326 }
327 for (uint32_t i = 0; i < cnt; ++i) {
328 rs.stroke->dashPattern[i] = pattern[i];
329 }
330 }
331 rs.stroke->dashCnt = cnt;
332 rs.stroke->dashOffset = offset;
333 rFlag |= RenderUpdateFlag::Stroke;
334
335 return Result::Success;
336 }
337
338 bool strokeFirst()
339 {
340 if (!rs.stroke) return true;
341 return rs.stroke->strokeFirst;
342 }
343
344 void strokeFirst(bool strokeFirst)
345 {
346 if (!rs.stroke) rs.stroke = new RenderStroke();
347 rs.stroke->strokeFirst = strokeFirst;
348 rFlag |= RenderUpdateFlag::Stroke;
349 }
350
351 void update(RenderUpdateFlag flag)
352 {
353 rFlag |= flag;
354 }
355
356 Paint* duplicate(Paint* ret)
357 {
358 auto shape = static_cast<Shape*>(ret);
359 if (shape) shape->reset();
360 else shape = Shape::gen().release();
361
362 auto dup = shape->pImpl;
363 delete(dup->rs.fill);
364
365 //Default Properties
366 dup->rFlag = RenderUpdateFlag::All;
367 dup->rs.rule = rs.rule;
368
369 //Color
370 memcpy(dup->rs.color, rs.color, sizeof(rs.color));
371
372 //Path
373 dup->rs.path.cmds.push(rs.path.cmds);
374 dup->rs.path.pts.push(rs.path.pts);
375
376 //Stroke
377 if (rs.stroke) {
378 if (!dup->rs.stroke) dup->rs.stroke = new RenderStroke;
379 *dup->rs.stroke = *rs.stroke;
380 } else {
381 delete(dup->rs.stroke);
382 dup->rs.stroke = nullptr;
383 }
384
385 //Fill
386 if (rs.fill) dup->rs.fill = rs.fill->duplicate();
387 else dup->rs.fill = nullptr;
388
389 return shape;
390 }
391
392 void reset()
393 {
394 PP(shape)->reset();
395 rs.path.cmds.clear();
396 rs.path.pts.clear();
397
398 rs.color[3] = 0;
399 rs.rule = FillRule::Winding;
400
401 delete(rs.stroke);
402 rs.stroke = nullptr;
403
404 delete(rs.fill);
405 rs.fill = nullptr;
406 }
407
408 Iterator* iterator()
409 {
410 return nullptr;
411 }
412 };
413
414 #endif //_TVG_SHAPE_H_