Merge "atspi: remove undefined method" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / canvas-renderer / ubuntu / shape-impl-ubuntu.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/ubuntu/shape-impl-ubuntu.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 ShapeUbuntu* ShapeUbuntu::New()
44 {
45   return new ShapeUbuntu();
46 }
47
48 ShapeUbuntu::ShapeUbuntu()
49 #ifdef THORVG_SUPPORT
50 : mTvgShape(nullptr)
51 #endif
52 {
53   Initialize();
54 }
55
56 ShapeUbuntu::~ShapeUbuntu()
57 {
58 }
59
60 void ShapeUbuntu::Initialize()
61 {
62 #ifdef THORVG_SUPPORT
63   mTvgShape = tvg::Shape::gen().release();
64   if(!mTvgShape)
65   {
66     DALI_LOG_ERROR("Shape is null [%p]\n", this);
67   }
68
69   Drawable::Create();
70   Drawable::SetObject(static_cast<void*>(mTvgShape));
71 #endif
72 }
73
74 bool ShapeUbuntu::AddRect(Rect<float> rect, Vector2 roundedCorner)
75 {
76 #ifdef THORVG_SUPPORT
77   if(!mTvgShape)
78   {
79     DALI_LOG_ERROR("Shape is null [%p]\n", this);
80     return false;
81   }
82   if(static_cast<tvg::Shape*>(mTvgShape)->appendRect(rect.x, rect.y, rect.width, rect.height, roundedCorner.x, roundedCorner.y) != tvg::Result::Success)
83   {
84     DALI_LOG_ERROR("Add Rect Fail [%p]\n", this);
85     return false;
86   }
87   Drawable::SetChanged(true);
88   return true;
89 #else
90   return false;
91 #endif
92 }
93
94 bool ShapeUbuntu::AddCircle(Vector2 center, Vector2 radius)
95 {
96 #ifdef THORVG_SUPPORT
97   if(!mTvgShape)
98   {
99     DALI_LOG_ERROR("Shape is null [%p]\n", this);
100     return false;
101   }
102   if(static_cast<tvg::Shape*>(mTvgShape)->appendCircle(center.x, center.y, radius.x, radius.y) != tvg::Result::Success)
103   {
104     DALI_LOG_ERROR("Add Circle Fail [%p]\n", this);
105     return false;
106   }
107   Drawable::SetChanged(true);
108   return true;
109 #else
110   return false;
111 #endif
112 }
113
114 bool ShapeUbuntu::AddArc(Vector2 center, float radius, float startAngle, float sweep, bool pie)
115 {
116 #ifdef THORVG_SUPPORT
117   if(!mTvgShape)
118   {
119     DALI_LOG_ERROR("Shape is null\n");
120     return false;
121   }
122
123   if(static_cast<tvg::Shape*>(mTvgShape)->appendArc(center.x, center.y, radius, startAngle, sweep, pie) != tvg::Result::Success)
124   {
125     DALI_LOG_ERROR("Add arc fail.\n");
126     return false;
127   }
128   Drawable::SetChanged(true);
129   return true;
130 #else
131   return false;
132 #endif
133 }
134
135 bool ShapeUbuntu::AddMoveTo(Vector2 point)
136 {
137 #ifdef THORVG_SUPPORT
138   if(!mTvgShape)
139   {
140     DALI_LOG_ERROR("Shape is null\n");
141     return false;
142   }
143
144   if(static_cast<tvg::Shape*>(mTvgShape)->moveTo(point.x, point.y) != tvg::Result::Success)
145   {
146     DALI_LOG_ERROR("AddMoveTo() fail.\n");
147     return false;
148   }
149   Drawable::SetChanged(true);
150   return true;
151 #else
152   return false;
153 #endif
154 }
155
156 bool ShapeUbuntu::AddLineTo(Vector2 line)
157 {
158 #ifdef THORVG_SUPPORT
159   if(!mTvgShape)
160   {
161     DALI_LOG_ERROR("Shape is null\n");
162     return false;
163   }
164
165   if(static_cast<tvg::Shape*>(mTvgShape)->lineTo(line.x, line.y) != tvg::Result::Success)
166   {
167     DALI_LOG_ERROR("AddLineTo() fail.\n");
168     return false;
169   }
170   Drawable::SetChanged(true);
171   return true;
172 #else
173   return false;
174 #endif
175 }
176
177 bool ShapeUbuntu::AddCubicTo(Vector2 controlPoint1, Vector2 controlPoint2, Vector2 endPoint)
178 {
179 #ifdef THORVG_SUPPORT
180   if(!mTvgShape)
181   {
182     DALI_LOG_ERROR("Shape is null\n");
183     return false;
184   }
185
186   if(static_cast<tvg::Shape*>(mTvgShape)->cubicTo(controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y) != tvg::Result::Success)
187   {
188     DALI_LOG_ERROR("cubicTo() fail.\n");
189     return false;
190   }
191   Drawable::SetChanged(true);
192   return true;
193 #else
194   return false;
195 #endif
196 }
197
198 bool ShapeUbuntu::Close()
199 {
200 #ifdef THORVG_SUPPORT
201   if(!mTvgShape)
202   {
203     DALI_LOG_ERROR("Shape is null\n");
204     return false;
205   }
206
207   if(static_cast<tvg::Shape*>(mTvgShape)->close() != tvg::Result::Success)
208   {
209     DALI_LOG_ERROR("close() fail.\n");
210     return false;
211   }
212   Drawable::SetChanged(true);
213   return true;
214 #else
215   return false;
216 #endif
217 }
218
219 bool ShapeUbuntu::SetFillColor(Vector4 color)
220 {
221 #ifdef THORVG_SUPPORT
222   if(!mTvgShape)
223   {
224     DALI_LOG_ERROR("Shape is null [%p]\n", this);
225     return false;
226   }
227
228   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)
229   {
230     DALI_LOG_ERROR("SetFillColor fail [%p]\n", this);
231     return false;
232   }
233   Drawable::SetChanged(true);
234   return true;
235 #else
236   return false;
237 #endif
238 }
239
240 Vector4 ShapeUbuntu::GetFillColor() const
241 {
242 #ifdef THORVG_SUPPORT
243   if(!mTvgShape)
244   {
245     DALI_LOG_ERROR("Shape is null [%p]\n", this);
246     return Vector4(0, 0, 0, 0);
247   }
248   uint8_t r, g, b, a;
249
250   if(static_cast<tvg::Shape*>(mTvgShape)->fillColor(&r, &g, &b, &a) != tvg::Result::Success)
251   {
252     DALI_LOG_ERROR("GetFillColor fail [%p]\n", this);
253     return Vector4(0, 0, 0, 0);
254   }
255   return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
256 #else
257   return Vector4::ZERO;
258 #endif
259 }
260
261 bool ShapeUbuntu::SetFillRule(Dali::CanvasRenderer::Shape::FillRule rule)
262 {
263 #ifdef THORVG_SUPPORT
264   if(!mTvgShape)
265   {
266     DALI_LOG_ERROR("Shape is null\n");
267     return false;
268   }
269   if(static_cast<tvg::Shape*>(mTvgShape)->fill(static_cast<tvg::FillRule>(rule)) != tvg::Result::Success)
270   {
271     DALI_LOG_ERROR("SetFillRule fail.\n");
272     return false;
273   }
274   Drawable::SetChanged(true);
275   return true;
276 #else
277   return false;
278 #endif
279 }
280
281 Dali::CanvasRenderer::Shape::FillRule ShapeUbuntu::GetFillRule() const
282 {
283 #ifdef THORVG_SUPPORT
284   if(!mTvgShape)
285   {
286     DALI_LOG_ERROR("Shape is null\n");
287     return Dali::CanvasRenderer::Shape::FillRule::WINDING;
288   }
289
290   tvg::FillRule rule = static_cast<tvg::Shape*>(mTvgShape)->fillRule();
291
292   return static_cast<Dali::CanvasRenderer::Shape::FillRule>(rule);
293 #endif
294   return Dali::CanvasRenderer::Shape::FillRule::WINDING;
295 }
296
297 bool ShapeUbuntu::SetStrokeWidth(float width)
298 {
299 #ifdef THORVG_SUPPORT
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(width) != tvg::Result::Success)
307   {
308     DALI_LOG_ERROR("SetStrokeWidth fail.\n");
309     return false;
310   }
311   Drawable::SetChanged(true);
312   return true;
313 #else
314   return false;
315 #endif
316 }
317
318 float ShapeUbuntu::GetStrokeWidth() const
319 {
320 #ifdef THORVG_SUPPORT
321   if(!mTvgShape)
322   {
323     DALI_LOG_ERROR("Shape is null\n");
324     return false;
325   }
326   return static_cast<tvg::Shape*>(mTvgShape)->strokeWidth();
327 #else
328   return false;
329 #endif
330 }
331
332 bool ShapeUbuntu::SetStrokeColor(Vector4 color)
333 {
334 #ifdef THORVG_SUPPORT
335   if(!mTvgShape)
336   {
337     DALI_LOG_ERROR("Shape is null\n");
338     return false;
339   }
340
341   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)
342   {
343     DALI_LOG_ERROR("SetStrokeColor fail.\n");
344     return false;
345   }
346   Drawable::SetChanged(true);
347   return true;
348 #else
349   return false;
350 #endif
351 }
352
353 Vector4 ShapeUbuntu::GetStrokeColor() const
354 {
355 #ifdef THORVG_SUPPORT
356   if(!mTvgShape)
357   {
358     DALI_LOG_ERROR("Shape is null\n");
359     return Vector4(0, 0, 0, 0);
360   }
361
362   uint8_t r, g, b, a;
363
364   if(static_cast<tvg::Shape*>(mTvgShape)->strokeColor(&r, &g, &b, &a) != tvg::Result::Success)
365   {
366     DALI_LOG_ERROR("GetStrokeColor fail.\n");
367     return Vector4(0, 0, 0, 0);
368   }
369   return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
370 #else
371   return Vector4(0, 0, 0, 0);
372 #endif
373 }
374
375 bool ShapeUbuntu::SetStrokeDash(const Dali::Vector<float> dashPattern)
376 {
377 #ifdef THORVG_SUPPORT
378   if(!mTvgShape)
379   {
380     DALI_LOG_ERROR("Shape is null\n");
381     return false;
382   }
383
384   float* tvgDashPattern = (float*)alloca(sizeof(float) * dashPattern.Count());
385
386   for(unsigned int i = 0u; i < dashPattern.Count(); ++i)
387   {
388     tvgDashPattern[i] = dashPattern[i];
389   }
390
391   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(tvgDashPattern, dashPattern.Count()) != tvg::Result::Success)
392   {
393     DALI_LOG_ERROR("SetStrokeDash fail.\n");
394     return false;
395   }
396   Drawable::SetChanged(true);
397   return true;
398 #else
399   return false;
400 #endif
401 }
402
403 Dali::Vector<float> ShapeUbuntu::GetStrokeDash() const
404 {
405 #ifdef THORVG_SUPPORT
406   if(!mTvgShape)
407   {
408     DALI_LOG_ERROR("Shape is null\n");
409     return Vector<float>();
410   }
411   const float* tvgDashPattern = nullptr;
412   uint32_t     count          = 0;
413
414   count = static_cast<tvg::Shape*>(mTvgShape)->strokeDash(&tvgDashPattern);
415   if(!tvgDashPattern || count <= 0)
416   {
417     DALI_LOG_ERROR("GetStrokeDash() fail.\n");
418     return Vector<float>();
419   }
420
421   Dali::Vector<float> dashPattern;
422
423   dashPattern.Reserve(count);
424
425   for(unsigned int i = 0u; i < count; ++i)
426   {
427     dashPattern.PushBack(tvgDashPattern[i]);
428   }
429   return dashPattern;
430 #else
431   return Vector<float>();
432 #endif
433 }
434
435 bool ShapeUbuntu::SetStrokeCap(Dali::CanvasRenderer::Shape::StrokeCap cap)
436 {
437 #ifdef THORVG_SUPPORT
438   if(!mTvgShape)
439   {
440     DALI_LOG_ERROR("Shape is null\n");
441     return false;
442   }
443   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(static_cast<tvg::StrokeCap>(cap)) != tvg::Result::Success)
444   {
445     DALI_LOG_ERROR("SetStrokeCap fail.\n");
446     return false;
447   }
448   Drawable::SetChanged(true);
449   return true;
450 #else
451   return false;
452 #endif
453 }
454
455 Dali::CanvasRenderer::Shape::StrokeCap ShapeUbuntu::GetStrokeCap() const
456 {
457 #ifdef THORVG_SUPPORT
458   if(!mTvgShape)
459   {
460     DALI_LOG_ERROR("Shape is null\n");
461     return Dali::CanvasRenderer::Shape::StrokeCap::SQUARE;
462   }
463
464   tvg::StrokeCap cap = static_cast<tvg::Shape*>(mTvgShape)->strokeCap();
465
466   return static_cast<Dali::CanvasRenderer::Shape::StrokeCap>(cap);
467 #endif
468   return Dali::CanvasRenderer::Shape::StrokeCap::SQUARE;
469 }
470
471 bool ShapeUbuntu::SetStrokeJoin(Dali::CanvasRenderer::Shape::StrokeJoin join)
472 {
473 #ifdef THORVG_SUPPORT
474   if(!mTvgShape)
475   {
476     DALI_LOG_ERROR("Shape is null\n");
477     return false;
478   }
479
480   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(static_cast<tvg::StrokeJoin>(join)) != tvg::Result::Success)
481   {
482     DALI_LOG_ERROR("SetStrokejoin fail.\n");
483     return false;
484   }
485   Drawable::SetChanged(true);
486   return true;
487 #else
488   return false;
489 #endif
490 }
491
492 Dali::CanvasRenderer::Shape::StrokeJoin ShapeUbuntu::GetStrokeJoin() const
493 {
494 #ifdef THORVG_SUPPORT
495   if(!mTvgShape)
496   {
497     DALI_LOG_ERROR("Shape is null\n");
498     return Dali::CanvasRenderer::Shape::StrokeJoin::BEVEL;
499   }
500
501   tvg::StrokeJoin join = static_cast<tvg::Shape*>(mTvgShape)->strokeJoin();
502
503   return static_cast<Dali::CanvasRenderer::Shape::StrokeJoin>(join);
504 #endif
505   return Dali::CanvasRenderer::Shape::StrokeJoin::BEVEL;
506 }
507
508 } // namespace Adaptor
509
510 } // namespace Internal
511
512 } // namespace Dali