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_NODE_CPP_
18 #define _TVG_SHAPE_NODE_CPP_
20 #include "tvgCommon.h"
21 #include "tvgShapePath.h"
23 /************************************************************************/
24 /* Internal Class Implementation */
25 /************************************************************************/
43 struct ShapeNode::Impl
45 ShapeTransform *transform = nullptr;
46 ShapeFill *fill = nullptr;
47 ShapeStroke *stroke = nullptr;
48 ShapePath *path = nullptr;
49 uint8_t color[4] = {0, 0, 0, 0}; //r, g, b, a
50 void *edata = nullptr; //engine data
52 Impl() : path(new ShapePath)
58 if (path) delete(path);
59 if (stroke) delete(stroke);
60 if (fill) delete(fill);
61 if (transform) delete(transform);
66 /************************************************************************/
67 /* External Class Implementation */
68 /************************************************************************/
70 ShapeNode :: ShapeNode() : pImpl(make_unique<Impl>())
75 ShapeNode :: ~ShapeNode()
77 cout << "ShapeNode(" << this << ") destroyed!" << endl;
81 unique_ptr<ShapeNode> ShapeNode::gen() noexcept
83 return unique_ptr<ShapeNode>(new ShapeNode);
87 int ShapeNode :: update(RasterMethod* engine) noexcept
89 auto impl = pImpl.get();
92 impl->edata = engine->prepare(*this, impl->edata, RasterMethod::UpdateFlag::All);
93 if (impl->edata) return 0;
98 int ShapeNode :: clear() noexcept
100 auto impl = pImpl.get();
103 return impl->path->clear();
107 int ShapeNode :: pathCommands(const PathCommand** cmds) const noexcept
109 auto impl = pImpl.get();
110 assert(impl && cmds);
112 *cmds = impl->path->cmds;
114 return impl->path->cmdCnt;
118 int ShapeNode :: pathCoords(const Point** pts) const noexcept
120 auto impl = pImpl.get();
123 *pts = impl->path->pts;
125 return impl->path->ptsCnt;
129 int ShapeNode :: appendCircle(float cx, float cy, float radius) noexcept
135 int ShapeNode :: appendRect(float x, float y, float w, float h, float radius) noexcept
137 auto impl = pImpl.get();
140 //clamping radius by minimum size
141 auto min = (w < h ? w : h) * 0.5f;
142 if (radius > min) radius = min;
146 impl->path->reserve(5, 4);
147 impl->path->moveTo(x, y);
148 impl->path->lineTo(x + w, y);
149 impl->path->lineTo(x + w, y + h);
150 impl->path->lineTo(x, y + h);
153 } else if (w == h && radius * 2 == w) {
154 appendCircle(x + (w * 0.5f), y + (h * 0.5f), radius);
163 int ShapeNode :: fill(size_t r, size_t g, size_t b, size_t a) noexcept
165 auto impl = pImpl.get();
177 int ShapeNode :: fill(size_t* r, size_t* g, size_t* b, size_t* a) const noexcept
179 auto impl = pImpl.get();
182 if (r) *r = impl->color[0];
183 if (g) *g = impl->color[1];
184 if (b) *b = impl->color[2];
185 if (a) *a = impl->color[3];
190 #endif //_TVG_SHAPE_NODE_CPP_