2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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.
19 #include <dali/internal/canvas-renderer/ubuntu/shape-impl-ubuntu.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/object/type-registry.h>
31 namespace // unnamed namespace
34 Dali::BaseHandle Create()
36 return Dali::BaseHandle();
39 Dali::TypeRegistration type(typeid(Dali::CanvasRenderer::Shape), typeid(Dali::BaseHandle), Create);
41 } // unnamed namespace
43 ShapeUbuntu* ShapeUbuntu::New()
45 return new ShapeUbuntu();
48 ShapeUbuntu::ShapeUbuntu()
56 ShapeUbuntu::~ShapeUbuntu()
60 void ShapeUbuntu::Initialize()
63 mTvgShape = tvg::Shape::gen().release();
66 DALI_LOG_ERROR("Shape is null [%p]\n", this);
70 Drawable::SetObject(static_cast<void*>(mTvgShape));
71 Drawable::SetType(Drawable::Types::SHAPE);
75 bool ShapeUbuntu::AddRect(Rect<float> rect, Vector2 roundedCorner)
78 if(!Drawable::GetObject() || !mTvgShape)
80 DALI_LOG_ERROR("Shape is null [%p]\n", this);
83 if(static_cast<tvg::Shape*>(mTvgShape)->appendRect(rect.x, rect.y, rect.width, rect.height, roundedCorner.x, roundedCorner.y) != tvg::Result::Success)
85 DALI_LOG_ERROR("Add Rect Fail [%p]\n", this);
88 Drawable::SetChanged(true);
95 bool ShapeUbuntu::AddCircle(Vector2 center, Vector2 radius)
98 if(!Drawable::GetObject() || !mTvgShape)
100 DALI_LOG_ERROR("Shape is null [%p]\n", this);
103 if(static_cast<tvg::Shape*>(mTvgShape)->appendCircle(center.x, center.y, radius.x, radius.y) != tvg::Result::Success)
105 DALI_LOG_ERROR("Add Circle Fail [%p]\n", this);
108 Drawable::SetChanged(true);
115 bool ShapeUbuntu::AddArc(Vector2 center, float radius, float startAngle, float sweep, bool pie)
117 #ifdef THORVG_SUPPORT
118 if(!Drawable::GetObject() || !mTvgShape)
120 DALI_LOG_ERROR("Shape is null\n");
124 if(static_cast<tvg::Shape*>(mTvgShape)->appendArc(center.x, center.y, radius, startAngle, sweep, pie) != tvg::Result::Success)
126 DALI_LOG_ERROR("Add arc fail.\n");
129 Drawable::SetChanged(true);
136 bool ShapeUbuntu::AddMoveTo(Vector2 point)
138 #ifdef THORVG_SUPPORT
139 if(!Drawable::GetObject() || !mTvgShape)
141 DALI_LOG_ERROR("Shape is null\n");
145 if(static_cast<tvg::Shape*>(mTvgShape)->moveTo(point.x, point.y) != tvg::Result::Success)
147 DALI_LOG_ERROR("AddMoveTo() fail.\n");
150 Drawable::SetChanged(true);
157 bool ShapeUbuntu::AddLineTo(Vector2 line)
159 #ifdef THORVG_SUPPORT
160 if(!Drawable::GetObject() || !mTvgShape)
162 DALI_LOG_ERROR("Shape is null\n");
166 if(static_cast<tvg::Shape*>(mTvgShape)->lineTo(line.x, line.y) != tvg::Result::Success)
168 DALI_LOG_ERROR("AddLineTo() fail.\n");
171 Drawable::SetChanged(true);
178 bool ShapeUbuntu::AddCubicTo(Vector2 controlPoint1, Vector2 controlPoint2, Vector2 endPoint)
180 #ifdef THORVG_SUPPORT
181 if(!Drawable::GetObject() || !mTvgShape)
183 DALI_LOG_ERROR("Shape is null\n");
187 if(static_cast<tvg::Shape*>(mTvgShape)->cubicTo(controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y) != tvg::Result::Success)
189 DALI_LOG_ERROR("cubicTo() fail.\n");
192 Drawable::SetChanged(true);
199 bool ShapeUbuntu::Close()
201 #ifdef THORVG_SUPPORT
202 if(!Drawable::GetObject() || !mTvgShape)
204 DALI_LOG_ERROR("Shape is null\n");
208 if(static_cast<tvg::Shape*>(mTvgShape)->close() != tvg::Result::Success)
210 DALI_LOG_ERROR("close() fail.\n");
213 Drawable::SetChanged(true);
220 bool ShapeUbuntu::ResetPath()
222 #ifdef THORVG_SUPPORT
223 if(!Drawable::GetObject() || !mTvgShape)
225 DALI_LOG_ERROR("Shape is null\n");
229 if(static_cast<tvg::Shape*>(mTvgShape)->reset() != tvg::Result::Success)
231 DALI_LOG_ERROR("reset() fail.\n");
234 Drawable::SetChanged(true);
241 bool ShapeUbuntu::SetFillColor(Vector4 color)
243 #ifdef THORVG_SUPPORT
244 if(!Drawable::GetObject() || !mTvgShape)
246 DALI_LOG_ERROR("Shape is null [%p]\n", this);
250 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)
252 DALI_LOG_ERROR("SetFillColor fail [%p]\n", this);
255 Drawable::SetChanged(true);
262 Vector4 ShapeUbuntu::GetFillColor() const
264 #ifdef THORVG_SUPPORT
265 if(!Drawable::GetObject() || !mTvgShape)
267 DALI_LOG_ERROR("Shape is null [%p]\n", this);
268 return Vector4(0, 0, 0, 0);
272 if(static_cast<tvg::Shape*>(mTvgShape)->fillColor(&r, &g, &b, &a) != tvg::Result::Success)
274 DALI_LOG_ERROR("GetFillColor fail [%p]\n", this);
275 return Vector4(0, 0, 0, 0);
277 return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
279 return Vector4::ZERO;
283 bool ShapeUbuntu::SetFillRule(Dali::CanvasRenderer::Shape::FillRule rule)
285 #ifdef THORVG_SUPPORT
286 if(!Drawable::GetObject() || !mTvgShape)
288 DALI_LOG_ERROR("Shape is null\n");
291 if(static_cast<tvg::Shape*>(mTvgShape)->fill(static_cast<tvg::FillRule>(rule)) != tvg::Result::Success)
293 DALI_LOG_ERROR("SetFillRule fail.\n");
296 Drawable::SetChanged(true);
303 Dali::CanvasRenderer::Shape::FillRule ShapeUbuntu::GetFillRule() const
305 #ifdef THORVG_SUPPORT
306 if(!Drawable::GetObject() || !mTvgShape)
308 DALI_LOG_ERROR("Shape is null\n");
309 return Dali::CanvasRenderer::Shape::FillRule::WINDING;
312 tvg::FillRule rule = static_cast<tvg::Shape*>(mTvgShape)->fillRule();
314 return static_cast<Dali::CanvasRenderer::Shape::FillRule>(rule);
316 return Dali::CanvasRenderer::Shape::FillRule::WINDING;
319 bool ShapeUbuntu::SetStrokeWidth(float width)
321 #ifdef THORVG_SUPPORT
322 if(!Drawable::GetObject() || !mTvgShape)
324 DALI_LOG_ERROR("Shape is null\n");
328 if(static_cast<tvg::Shape*>(mTvgShape)->stroke(width) != tvg::Result::Success)
330 DALI_LOG_ERROR("SetStrokeWidth fail.\n");
333 Drawable::SetChanged(true);
340 float ShapeUbuntu::GetStrokeWidth() const
342 #ifdef THORVG_SUPPORT
343 if(!Drawable::GetObject() || !mTvgShape)
345 DALI_LOG_ERROR("Shape is null\n");
348 return static_cast<tvg::Shape*>(mTvgShape)->strokeWidth();
354 bool ShapeUbuntu::SetStrokeColor(Vector4 color)
356 #ifdef THORVG_SUPPORT
357 if(!Drawable::GetObject() || !mTvgShape)
359 DALI_LOG_ERROR("Shape is null\n");
363 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)
365 DALI_LOG_ERROR("SetStrokeColor fail.\n");
368 Drawable::SetChanged(true);
375 Vector4 ShapeUbuntu::GetStrokeColor() const
377 #ifdef THORVG_SUPPORT
378 if(!Drawable::GetObject() || !mTvgShape)
380 DALI_LOG_ERROR("Shape is null\n");
381 return Vector4(0, 0, 0, 0);
386 if(static_cast<tvg::Shape*>(mTvgShape)->strokeColor(&r, &g, &b, &a) != tvg::Result::Success)
388 DALI_LOG_ERROR("GetStrokeColor fail.\n");
389 return Vector4(0, 0, 0, 0);
391 return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
393 return Vector4(0, 0, 0, 0);
397 bool ShapeUbuntu::SetStrokeDash(const Dali::Vector<float> dashPattern)
399 #ifdef THORVG_SUPPORT
400 if(!Drawable::GetObject() || !mTvgShape)
402 DALI_LOG_ERROR("Shape is null\n");
406 float* tvgDashPattern = (float*)alloca(sizeof(float) * dashPattern.Count());
408 for(unsigned int i = 0u; i < dashPattern.Count(); ++i)
410 tvgDashPattern[i] = dashPattern[i];
413 if(static_cast<tvg::Shape*>(mTvgShape)->stroke(tvgDashPattern, dashPattern.Count()) != tvg::Result::Success)
415 DALI_LOG_ERROR("SetStrokeDash fail.\n");
418 Drawable::SetChanged(true);
425 Dali::Vector<float> ShapeUbuntu::GetStrokeDash() const
427 #ifdef THORVG_SUPPORT
428 if(!Drawable::GetObject() || !mTvgShape)
430 DALI_LOG_ERROR("Shape is null\n");
431 return Vector<float>();
433 const float* tvgDashPattern = nullptr;
436 count = static_cast<tvg::Shape*>(mTvgShape)->strokeDash(&tvgDashPattern);
437 if(!tvgDashPattern || count <= 0)
439 DALI_LOG_ERROR("GetStrokeDash() fail.\n");
440 return Vector<float>();
443 Dali::Vector<float> dashPattern;
445 dashPattern.Reserve(count);
447 for(unsigned int i = 0u; i < count; ++i)
449 dashPattern.PushBack(tvgDashPattern[i]);
453 return Vector<float>();
457 bool ShapeUbuntu::SetStrokeCap(Dali::CanvasRenderer::Shape::StrokeCap cap)
459 #ifdef THORVG_SUPPORT
460 if(!Drawable::GetObject() || !mTvgShape)
462 DALI_LOG_ERROR("Shape is null\n");
465 if(static_cast<tvg::Shape*>(mTvgShape)->stroke(static_cast<tvg::StrokeCap>(cap)) != tvg::Result::Success)
467 DALI_LOG_ERROR("SetStrokeCap fail.\n");
470 Drawable::SetChanged(true);
477 Dali::CanvasRenderer::Shape::StrokeCap ShapeUbuntu::GetStrokeCap() const
479 #ifdef THORVG_SUPPORT
480 if(!Drawable::GetObject() || !mTvgShape)
482 DALI_LOG_ERROR("Shape is null\n");
483 return Dali::CanvasRenderer::Shape::StrokeCap::SQUARE;
486 tvg::StrokeCap cap = static_cast<tvg::Shape*>(mTvgShape)->strokeCap();
488 return static_cast<Dali::CanvasRenderer::Shape::StrokeCap>(cap);
490 return Dali::CanvasRenderer::Shape::StrokeCap::SQUARE;
493 bool ShapeUbuntu::SetStrokeJoin(Dali::CanvasRenderer::Shape::StrokeJoin join)
495 #ifdef THORVG_SUPPORT
496 if(!Drawable::GetObject() || !mTvgShape)
498 DALI_LOG_ERROR("Shape is null\n");
502 if(static_cast<tvg::Shape*>(mTvgShape)->stroke(static_cast<tvg::StrokeJoin>(join)) != tvg::Result::Success)
504 DALI_LOG_ERROR("SetStrokejoin fail.\n");
507 Drawable::SetChanged(true);
514 Dali::CanvasRenderer::Shape::StrokeJoin ShapeUbuntu::GetStrokeJoin() const
516 #ifdef THORVG_SUPPORT
517 if(!Drawable::GetObject() || !mTvgShape)
519 DALI_LOG_ERROR("Shape is null\n");
520 return Dali::CanvasRenderer::Shape::StrokeJoin::BEVEL;
523 tvg::StrokeJoin join = static_cast<tvg::Shape*>(mTvgShape)->strokeJoin();
525 return static_cast<Dali::CanvasRenderer::Shape::StrokeJoin>(join);
527 return Dali::CanvasRenderer::Shape::StrokeJoin::BEVEL;
530 } // namespace Adaptor
532 } // namespace Internal