[Tizen] CanvasRenderer::Shape: Add ResetPath() method for reset path information
[platform/core/uifw/dali-adaptor.git] / dali / internal / canvas-renderer / tizen / shape-impl-tizen.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/canvas-renderer/tizen/shape-impl-tizen.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/object/type-registry.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 namespace Adaptor
30 {
31 namespace // unnamed namespace
32 {
33 // Type Registration
34 Dali::BaseHandle Create()
35 {
36   return Dali::BaseHandle();
37 }
38
39 Dali::TypeRegistration type(typeid(Dali::CanvasRenderer::Shape), typeid(Dali::BaseHandle), Create);
40
41 } // unnamed namespace
42
43 ShapeTizen* ShapeTizen::New()
44 {
45   return new ShapeTizen();
46 }
47
48 ShapeTizen::ShapeTizen()
49 : mTvgShape(nullptr)
50 {
51   Initialize();
52 }
53
54 ShapeTizen::~ShapeTizen()
55 {
56 }
57
58 void ShapeTizen::Initialize()
59 {
60   mTvgShape = tvg::Shape::gen().release();
61   if(!mTvgShape)
62   {
63     DALI_LOG_ERROR("Shape is null [%p]\n", this);
64   }
65
66   Drawable::Create();
67   Drawable::SetObject(static_cast<void*>(mTvgShape));
68 }
69
70 bool ShapeTizen::AddRect(Rect<float> rect, Vector2 roundedCorner)
71 {
72   if(!mTvgShape)
73   {
74     DALI_LOG_ERROR("Shape is null [%p]\n", this);
75     return false;
76   }
77   if(static_cast<tvg::Shape*>(mTvgShape)->appendRect(rect.x, rect.y, rect.width, rect.height, roundedCorner.x, roundedCorner.y) != tvg::Result::Success)
78   {
79     DALI_LOG_ERROR("Add Rect Fail [%p]\n", this);
80     return false;
81   }
82   Drawable::SetChanged(true);
83   return true;
84 }
85
86 bool ShapeTizen::AddCircle(Vector2 center, Vector2 radius)
87 {
88   if(!mTvgShape)
89   {
90     DALI_LOG_ERROR("Shape is null [%p]\n", this);
91     return false;
92   }
93   if(static_cast<tvg::Shape*>(mTvgShape)->appendCircle(center.x, center.y, radius.x, radius.y) != tvg::Result::Success)
94   {
95     DALI_LOG_ERROR("Add Circle Fail [%p]\n", this);
96     return false;
97   }
98   Drawable::SetChanged(true);
99   return true;
100 }
101
102 bool ShapeTizen::AddArc(Vector2 center, float radius, float startAngle, float sweep, bool pie)
103 {
104   if(!mTvgShape)
105   {
106     DALI_LOG_ERROR("Shape is null\n");
107     return false;
108   }
109
110   if(static_cast<tvg::Shape*>(mTvgShape)->appendArc(center.x, center.y, radius, startAngle, sweep, pie) != tvg::Result::Success)
111   {
112     DALI_LOG_ERROR("Add arc fail.\n");
113     return false;
114   }
115   Drawable::SetChanged(true);
116   return true;
117 }
118
119 bool ShapeTizen::AddMoveTo(Vector2 point)
120 {
121   if(!mTvgShape)
122   {
123     DALI_LOG_ERROR("Shape is null\n");
124     return false;
125   }
126
127   if(static_cast<tvg::Shape*>(mTvgShape)->moveTo(point.x, point.y) != tvg::Result::Success)
128   {
129     DALI_LOG_ERROR("AddMoveTo() fail.\n");
130     return false;
131   }
132   Drawable::SetChanged(true);
133   return true;
134 }
135
136 bool ShapeTizen::AddLineTo(Vector2 line)
137 {
138   if(!mTvgShape)
139   {
140     DALI_LOG_ERROR("Shape is null\n");
141     return false;
142   }
143
144   if(static_cast<tvg::Shape*>(mTvgShape)->lineTo(line.x, line.y) != tvg::Result::Success)
145   {
146     DALI_LOG_ERROR("AddLineTo() fail.\n");
147     return false;
148   }
149   Drawable::SetChanged(true);
150   return true;
151 }
152
153 bool ShapeTizen::AddCubicTo(Vector2 controlPoint1, Vector2 controlPoint2, Vector2 endPoint)
154 {
155   if(!mTvgShape)
156   {
157     DALI_LOG_ERROR("Shape is null\n");
158     return false;
159   }
160
161   if(static_cast<tvg::Shape*>(mTvgShape)->cubicTo(controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y) != tvg::Result::Success)
162   {
163     DALI_LOG_ERROR("cubicTo() fail.\n");
164     return false;
165   }
166   Drawable::SetChanged(true);
167   return true;
168 }
169
170 bool ShapeTizen::Close()
171 {
172   if(!mTvgShape)
173   {
174     DALI_LOG_ERROR("Shape is null\n");
175     return false;
176   }
177
178   if(static_cast<tvg::Shape*>(mTvgShape)->close() != tvg::Result::Success)
179   {
180     DALI_LOG_ERROR("close() fail.\n");
181     return false;
182   }
183   Drawable::SetChanged(true);
184   return true;
185 }
186
187 bool ShapeTizen::ResetPath()
188 {
189 #ifdef THORVG_SUPPORT
190   if(!mTvgShape)
191   {
192     DALI_LOG_ERROR("Shape is null\n");
193     return false;
194   }
195
196   if(static_cast<tvg::Shape*>(mTvgShape)->reset() != tvg::Result::Success)
197   {
198     DALI_LOG_ERROR("reset() fail.\n");
199     return false;
200   }
201   Drawable::SetChanged(true);
202   return true;
203 #else
204   return false;
205 #endif
206 }
207
208 bool ShapeTizen::SetFillColor(Vector4 color)
209 {
210   if(!mTvgShape)
211   {
212     DALI_LOG_ERROR("Shape is null [%p]\n", this);
213     return false;
214   }
215
216   if(static_cast<tvg::Shape*>(mTvgShape)->fill((uint8_t)(color.r * 255.f), (uint8_t)(color.g * 255.f), (uint8_t)(color.b * 255.f), (uint8_t)(color.a * 255.f)) != tvg::Result::Success)
217   {
218     DALI_LOG_ERROR("SetFillColor fail [%p]\n", this);
219     return false;
220   }
221   Drawable::SetChanged(true);
222   return true;
223 }
224
225 Vector4 ShapeTizen::GetFillColor() const
226 {
227   if(!mTvgShape)
228   {
229     DALI_LOG_ERROR("Shape is null [%p]\n", this);
230     return Vector4(0, 0, 0, 0);
231   }
232   uint8_t r, g, b, a;
233
234   if(static_cast<tvg::Shape*>(mTvgShape)->fillColor(&r, &g, &b, &a) != tvg::Result::Success)
235   {
236     DALI_LOG_ERROR("GetFillColor fail [%p]\n", this);
237     return Vector4(0, 0, 0, 0);
238   }
239   return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
240 }
241
242 bool ShapeTizen::SetFillRule(Dali::CanvasRenderer::Shape::FillRule rule)
243 {
244   if(!mTvgShape)
245   {
246     DALI_LOG_ERROR("Shape is null\n");
247     return false;
248   }
249   if(static_cast<tvg::Shape*>(mTvgShape)->fill(static_cast<tvg::FillRule>(rule)) != tvg::Result::Success)
250   {
251     DALI_LOG_ERROR("SetFillRule fail.\n");
252     return false;
253   }
254   Drawable::SetChanged(true);
255   return true;
256 }
257
258 Dali::CanvasRenderer::Shape::FillRule ShapeTizen::GetFillRule() const
259 {
260   if(!mTvgShape)
261   {
262     DALI_LOG_ERROR("Shape is null\n");
263     return Dali::CanvasRenderer::Shape::FillRule::WINDING;
264   }
265
266   tvg::FillRule rule = static_cast<tvg::Shape*>(mTvgShape)->fillRule();
267
268   return static_cast<Dali::CanvasRenderer::Shape::FillRule>(rule);
269 }
270
271 bool ShapeTizen::SetStrokeWidth(float width)
272 {
273   if(!mTvgShape)
274   {
275     DALI_LOG_ERROR("Shape is null\n");
276     return false;
277   }
278
279   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(width) != tvg::Result::Success)
280   {
281     DALI_LOG_ERROR("SetStrokeWidth fail.\n");
282     return false;
283   }
284   Drawable::SetChanged(true);
285   return true;
286 }
287
288 float ShapeTizen::GetStrokeWidth() const
289 {
290   if(!mTvgShape)
291   {
292     DALI_LOG_ERROR("Shape is null\n");
293     return false;
294   }
295   return static_cast<tvg::Shape*>(mTvgShape)->strokeWidth();
296 }
297
298 bool ShapeTizen::SetStrokeColor(Vector4 color)
299 {
300   if(!mTvgShape)
301   {
302     DALI_LOG_ERROR("Shape is null\n");
303     return false;
304   }
305
306   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(color.r * 255.f, color.g * 255.f, color.b * 255.f, color.a * 255.f) != tvg::Result::Success)
307   {
308     DALI_LOG_ERROR("SetStrokeColor fail.\n");
309     return false;
310   }
311   Drawable::SetChanged(true);
312   return true;
313 }
314
315 Vector4 ShapeTizen::GetStrokeColor() const
316 {
317   if(!mTvgShape)
318   {
319     DALI_LOG_ERROR("Shape is null\n");
320     return Vector4(0, 0, 0, 0);
321   }
322
323   uint8_t r, g, b, a;
324
325   if(static_cast<tvg::Shape*>(mTvgShape)->strokeColor(&r, &g, &b, &a) != tvg::Result::Success)
326   {
327     DALI_LOG_ERROR("GetStrokeColor fail.\n");
328     return Vector4(0, 0, 0, 0);
329   }
330   return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
331 }
332
333 bool ShapeTizen::SetStrokeDash(const Dali::Vector<float> dashPattern)
334 {
335   if(!mTvgShape)
336   {
337     DALI_LOG_ERROR("Shape is null\n");
338     return false;
339   }
340
341   float* tvgDashPattern = (float*)alloca(sizeof(float) * dashPattern.Count());
342
343   for(unsigned int i = 0u; i < dashPattern.Count(); ++i)
344   {
345     tvgDashPattern[i] = dashPattern[i];
346   }
347
348   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(tvgDashPattern, dashPattern.Count()) != tvg::Result::Success)
349   {
350     DALI_LOG_ERROR("SetStrokeDash fail.\n");
351     return false;
352   }
353   Drawable::SetChanged(true);
354   return true;
355 }
356
357 Dali::Vector<float> ShapeTizen::GetStrokeDash() const
358 {
359   if(!mTvgShape)
360   {
361     DALI_LOG_ERROR("Shape is null\n");
362     return Vector<float>();
363   }
364   const float* tvgDashPattern = nullptr;
365   uint32_t     count          = 0;
366
367   count = static_cast<tvg::Shape*>(mTvgShape)->strokeDash(&tvgDashPattern);
368   if(!tvgDashPattern || count <= 0)
369   {
370     DALI_LOG_ERROR("GetStrokeDash() fail.\n");
371     return Vector<float>();
372   }
373
374   Dali::Vector<float> dashPattern;
375
376   dashPattern.Reserve(count);
377
378   for(unsigned int i = 0u; i < count; ++i)
379   {
380     dashPattern.PushBack(tvgDashPattern[i]);
381   }
382   return dashPattern;
383 }
384
385 bool ShapeTizen::SetStrokeCap(Dali::CanvasRenderer::Shape::StrokeCap cap)
386 {
387   if(!mTvgShape)
388   {
389     DALI_LOG_ERROR("Shape is null\n");
390     return false;
391   }
392
393   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(static_cast<tvg::StrokeCap>(cap)) != tvg::Result::Success)
394   {
395     DALI_LOG_ERROR("SetStrokeCap fail.\n");
396     return false;
397   }
398   Drawable::SetChanged(true);
399   return true;
400 }
401
402 Dali::CanvasRenderer::Shape::StrokeCap ShapeTizen::GetStrokeCap() const
403 {
404   if(!mTvgShape)
405   {
406     DALI_LOG_ERROR("Shape is null\n");
407     return Dali::CanvasRenderer::Shape::StrokeCap::SQUARE;
408   }
409
410   tvg::StrokeCap cap = static_cast<tvg::Shape*>(mTvgShape)->strokeCap();
411
412   return static_cast<Dali::CanvasRenderer::Shape::StrokeCap>(cap);
413 }
414
415 bool ShapeTizen::SetStrokeJoin(Dali::CanvasRenderer::Shape::StrokeJoin join)
416 {
417   if(!mTvgShape)
418   {
419     DALI_LOG_ERROR("Shape is null\n");
420     return false;
421   }
422
423   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(static_cast<tvg::StrokeJoin>(join)) != tvg::Result::Success)
424   {
425     DALI_LOG_ERROR("SetStrokejoin fail.\n");
426     return false;
427   }
428   Drawable::SetChanged(true);
429   return true;
430 }
431
432 Dali::CanvasRenderer::Shape::StrokeJoin ShapeTizen::GetStrokeJoin() const
433 {
434   if(!mTvgShape)
435   {
436     DALI_LOG_ERROR("Shape is null\n");
437     return Dali::CanvasRenderer::Shape::StrokeJoin::BEVEL;
438   }
439
440   tvg::StrokeJoin join = static_cast<tvg::Shape*>(mTvgShape)->strokeJoin();
441
442   return static_cast<Dali::CanvasRenderer::Shape::StrokeJoin>(join);
443 }
444
445 } // namespace Adaptor
446
447 } // namespace Internal
448
449 } // namespace Dali