2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef _TVG_SHAPE_CPP_
18 #define _TVG_SHAPE_CPP_
20 #include "tvgCommon.h"
21 #include "tvgShapeImpl.h"
23 /************************************************************************/
24 /* Internal Class Implementation */
25 /************************************************************************/
26 constexpr auto PATH_KAPPA = 0.552284f;
28 /************************************************************************/
29 /* External Class Implementation */
30 /************************************************************************/
32 Shape :: Shape() : pImpl(make_unique<Impl>())
36 Paint::pImpl.get()->ts = pImpl.get()->transformMethod();
45 unique_ptr<Shape> Shape::gen() noexcept
47 return unique_ptr<Shape>(new Shape);
51 Result Shape::reset() noexcept
55 IMPL->flag |= RenderUpdateFlag::Path;
57 return Result::Success;
61 uint32_t Shape::pathCommands(const PathCommand** cmds) const noexcept
65 *cmds = IMPL->path->cmds;
67 return IMPL->path->cmdCnt;
71 uint32_t Shape::pathCoords(const Point** pts) const noexcept
75 *pts = IMPL->path->pts;
77 return IMPL->path->ptsCnt;
81 Result Shape::appendPath(const PathCommand *cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) noexcept
83 if (cmdCnt < 0 || ptsCnt < 0 || !pts || !ptsCnt) return Result::InvalidArguments;
85 IMPL->path->grow(cmdCnt, ptsCnt);
86 IMPL->path->append(cmds, cmdCnt, pts, ptsCnt);
88 IMPL->flag |= RenderUpdateFlag::Path;
90 return Result::Success;
94 Result Shape::moveTo(float x, float y) noexcept
96 IMPL->path->moveTo(x, y);
98 IMPL->flag |= RenderUpdateFlag::Path;
100 return Result::Success;
104 Result Shape::lineTo(float x, float y) noexcept
106 IMPL->path->lineTo(x, y);
108 IMPL->flag |= RenderUpdateFlag::Path;
110 return Result::Success;
114 Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept
116 IMPL->path->cubicTo(cx1, cy1, cx2, cy2, x, y);
118 IMPL->flag |= RenderUpdateFlag::Path;
120 return Result::Success;
124 Result Shape::close() noexcept
128 IMPL->flag |= RenderUpdateFlag::Path;
130 return Result::Success;
134 Result Shape::appendCircle(float cx, float cy, float rx, float ry) noexcept
136 auto impl = pImpl.get();
138 auto rxKappa = rx * PATH_KAPPA;
139 auto ryKappa = ry * PATH_KAPPA;
141 impl->path->grow(6, 13);
142 impl->path->moveTo(cx, cy - ry);
143 impl->path->cubicTo(cx + rxKappa, cy - ry, cx + rx, cy - ryKappa, cx + rx, cy);
144 impl->path->cubicTo(cx + rx, cy + ryKappa, cx + rxKappa, cy + ry, cx, cy + ry);
145 impl->path->cubicTo(cx - rxKappa, cy + ry, cx - rx, cy + ryKappa, cx - rx, cy);
146 impl->path->cubicTo(cx - rx, cy - ryKappa, cx - rxKappa, cy - ry, cx, cy - ry);
149 impl->flag |= RenderUpdateFlag::Path;
151 return Result::Success;
155 Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) noexcept
157 auto impl = pImpl.get();
159 auto halfW = w * 0.5f;
160 auto halfH = h * 0.5f;
162 //clamping cornerRadius by minimum size
163 if (rx > halfW) rx = halfW;
164 if (ry > halfH) ry = halfH;
167 if (rx == 0 && ry == 0) {
168 impl->path->grow(5, 4);
169 impl->path->moveTo(x, y);
170 impl->path->lineTo(x + w, y);
171 impl->path->lineTo(x + w, y + h);
172 impl->path->lineTo(x, y + h);
175 } else if (fabsf(rx - halfW) < FLT_EPSILON && fabsf(ry - halfH) < FLT_EPSILON) {
176 return appendCircle(x + (w * 0.5f), y + (h * 0.5f), rx, ry);
178 auto hrx = rx * 0.5f;
179 auto hry = ry * 0.5f;
180 impl->path->grow(10, 17);
181 impl->path->moveTo(x + rx, y);
182 impl->path->lineTo(x + w - rx, y);
183 impl->path->cubicTo(x + w - rx + hrx, y, x + w, y + ry - hry, x + w, y + ry);
184 impl->path->lineTo(x + w, y + h - ry);
185 impl->path->cubicTo(x + w, y + h - ry + hry, x + w - rx + hrx, y + h, x + w - rx, y + h);
186 impl->path->lineTo(x + rx, y + h);
187 impl->path->cubicTo(x + rx - hrx, y + h, x, y + h - ry + hry, x, y + h - ry);
188 impl->path->lineTo(x, y + ry);
189 impl->path->cubicTo(x, y + ry - hry, x + rx - hrx, y, x + rx, y);
193 impl->flag |= RenderUpdateFlag::Path;
195 return Result::Success;
199 Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
201 auto impl = pImpl.get();
207 impl->flag |= RenderUpdateFlag::Color;
211 impl->fill = nullptr;
212 impl->flag |= RenderUpdateFlag::Gradient;
215 return Result::Success;
219 Result Shape::fill(unique_ptr<Fill> f) noexcept
221 auto impl = pImpl.get();
223 auto p = f.release();
224 if (!p) return Result::MemoryCorruption;
226 if (impl->fill) delete(impl->fill);
228 impl->flag |= RenderUpdateFlag::Gradient;
230 return Result::Success;
234 Result Shape::fill(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
236 auto impl = pImpl.get();
238 if (r) *r = impl->color[0];
239 if (g) *g = impl->color[1];
240 if (b) *b = impl->color[2];
241 if (a) *a = impl->color[3];
243 return Result::Success;
246 const Fill* Shape::fill() const noexcept
252 Result Shape::stroke(float width) noexcept
254 if (!IMPL->strokeWidth(width)) return Result::FailedAllocation;
256 return Result::Success;
260 float Shape::strokeWidth() const noexcept
262 if (!IMPL->stroke) return 0;
263 return IMPL->stroke->width;
267 Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
269 if (!IMPL->strokeColor(r, g, b, a)) return Result::FailedAllocation;
271 return Result::Success;
275 Result Shape::strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept
277 auto impl = pImpl.get();
279 if (!impl->stroke) return Result::InsufficientCondition;
281 if (r) *r = impl->stroke->color[0];
282 if (g) *g = impl->stroke->color[1];
283 if (b) *b = impl->stroke->color[2];
284 if (a) *a = impl->stroke->color[3];
286 return Result::Success;
290 Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept
292 if (cnt < 2 || !dashPattern) return Result::InvalidArguments;
294 if (!IMPL->strokeDash(dashPattern, cnt)) return Result::FailedAllocation;
296 return Result::Success;
300 uint32_t Shape::strokeDash(const float** dashPattern) const noexcept
302 if (!IMPL->stroke) return 0;
304 if (dashPattern) *dashPattern = IMPL->stroke->dashPattern;
305 return IMPL->stroke->dashCnt;
309 Result Shape::stroke(StrokeCap cap) noexcept
311 if (!IMPL->strokeCap(cap)) return Result::FailedAllocation;
313 return Result::Success;
317 Result Shape::stroke(StrokeJoin join) noexcept
319 if (!IMPL->strokeJoin(join)) return Result::FailedAllocation;
321 return Result::Success;
325 StrokeCap Shape::strokeCap() const noexcept
327 if (!IMPL->stroke) return StrokeCap::Square;
329 return IMPL->stroke->cap;
333 StrokeJoin Shape::strokeJoin() const noexcept
335 if (!IMPL->stroke) return StrokeJoin::Bevel;
337 return IMPL->stroke->join;
341 #endif //_TVG_SHAPE_CPP_