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/tizen/shape-impl-tizen.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 ShapeTizen* ShapeTizen::New()
45 return new ShapeTizen();
48 ShapeTizen::ShapeTizen()
59 ShapeTizen::~ShapeTizen()
63 void ShapeTizen::Initialize()
66 mTvgShape = tvg::Shape::gen().release();
69 DALI_LOG_ERROR("Shape is null [%p]\n", this);
73 Drawable::SetObject(static_cast<void*>(mTvgShape));
74 Drawable::SetType(Drawable::Types::SHAPE);
78 bool ShapeTizen::AddRect(Rect<float> rect, Vector2 roundedCorner)
81 if(!Drawable::GetObject() || !mTvgShape)
83 DALI_LOG_ERROR("Shape is null [%p]\n", this);
86 if(static_cast<tvg::Shape*>(mTvgShape)->appendRect(rect.x, rect.y, rect.width, rect.height, roundedCorner.x, roundedCorner.y) != tvg::Result::Success)
88 DALI_LOG_ERROR("Add Rect Fail [%p]\n", this);
91 Drawable::SetChanged(true);
98 bool ShapeTizen::AddCircle(Vector2 center, Vector2 radius)
100 #ifdef THORVG_SUPPORT
101 if(!Drawable::GetObject() || !mTvgShape)
103 DALI_LOG_ERROR("Shape is null [%p]\n", this);
106 if(static_cast<tvg::Shape*>(mTvgShape)->appendCircle(center.x, center.y, radius.x, radius.y) != tvg::Result::Success)
108 DALI_LOG_ERROR("Add Circle Fail [%p]\n", this);
111 Drawable::SetChanged(true);
118 bool ShapeTizen::AddArc(Vector2 center, float radius, float startAngle, float sweep, bool pie)
120 #ifdef THORVG_SUPPORT
121 if(!Drawable::GetObject() || !mTvgShape)
123 DALI_LOG_ERROR("Shape is null\n");
127 if(static_cast<tvg::Shape*>(mTvgShape)->appendArc(center.x, center.y, radius, startAngle, sweep, pie) != tvg::Result::Success)
129 DALI_LOG_ERROR("Add arc fail.\n");
132 Drawable::SetChanged(true);
139 bool ShapeTizen::AddMoveTo(Vector2 point)
141 #ifdef THORVG_SUPPORT
142 if(!Drawable::GetObject() || !mTvgShape)
144 DALI_LOG_ERROR("Shape is null\n");
148 if(static_cast<tvg::Shape*>(mTvgShape)->moveTo(point.x, point.y) != tvg::Result::Success)
150 DALI_LOG_ERROR("AddMoveTo() fail.\n");
153 Drawable::SetChanged(true);
160 bool ShapeTizen::AddLineTo(Vector2 line)
162 #ifdef THORVG_SUPPORT
163 if(!Drawable::GetObject() || !mTvgShape)
165 DALI_LOG_ERROR("Shape is null\n");
169 if(static_cast<tvg::Shape*>(mTvgShape)->lineTo(line.x, line.y) != tvg::Result::Success)
171 DALI_LOG_ERROR("AddLineTo() fail.\n");
174 Drawable::SetChanged(true);
181 bool ShapeTizen::AddCubicTo(Vector2 controlPoint1, Vector2 controlPoint2, Vector2 endPoint)
183 #ifdef THORVG_SUPPORT
184 if(!Drawable::GetObject() || !mTvgShape)
186 DALI_LOG_ERROR("Shape is null\n");
190 if(static_cast<tvg::Shape*>(mTvgShape)->cubicTo(controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y) != tvg::Result::Success)
192 DALI_LOG_ERROR("cubicTo() fail.\n");
195 Drawable::SetChanged(true);
202 bool ShapeTizen::AddPath(Dali::CanvasRenderer::Shape::PathCommands& pathCommand)
204 #ifdef THORVG_SUPPORT
205 if(!Drawable::GetObject() || !mTvgShape)
207 DALI_LOG_ERROR("Shape is null\n");
211 if(static_cast<tvg::Shape*>(mTvgShape)->appendPath(reinterpret_cast<const tvg::PathCommand*>(pathCommand.mCommands), pathCommand.mCommandCount, static_cast<const tvg::Point*>(static_cast<void*>(pathCommand.mPoints)), pathCommand.mPointCount) != tvg::Result::Success)
213 DALI_LOG_ERROR("AddPath() fail.\n");
216 Drawable::SetChanged(true);
223 bool ShapeTizen::Close()
225 #ifdef THORVG_SUPPORT
226 if(!Drawable::GetObject() || !mTvgShape)
228 DALI_LOG_ERROR("Shape is null\n");
232 if(static_cast<tvg::Shape*>(mTvgShape)->close() != tvg::Result::Success)
234 DALI_LOG_ERROR("close() fail.\n");
237 Drawable::SetChanged(true);
244 bool ShapeTizen::ResetPath()
246 #ifdef THORVG_SUPPORT
247 if(!Drawable::GetObject() || !mTvgShape)
249 DALI_LOG_ERROR("Shape is null\n");
253 if(static_cast<tvg::Shape*>(mTvgShape)->reset() != tvg::Result::Success)
255 DALI_LOG_ERROR("reset() fail.\n");
258 Drawable::SetChanged(true);
265 bool ShapeTizen::SetFillColor(Vector4 color)
267 #ifdef THORVG_SUPPORT
268 if(!Drawable::GetObject() || !mTvgShape)
270 DALI_LOG_ERROR("Shape is null [%p]\n", this);
274 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)
276 DALI_LOG_ERROR("SetFillColor fail [%p]\n", this);
279 Drawable::SetChanged(true);
286 Vector4 ShapeTizen::GetFillColor() const
288 #ifdef THORVG_SUPPORT
289 if(!Drawable::GetObject() || !mTvgShape)
291 DALI_LOG_ERROR("Shape is null [%p]\n", this);
292 return Vector4(0, 0, 0, 0);
296 if(static_cast<tvg::Shape*>(mTvgShape)->fillColor(&r, &g, &b, &a) != tvg::Result::Success)
298 DALI_LOG_ERROR("GetFillColor fail [%p]\n", this);
299 return Vector4(0, 0, 0, 0);
301 return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
303 return Vector4::ZERO;
307 bool ShapeTizen::SetFillGradient(Dali::CanvasRenderer::Gradient& gradient)
309 #ifdef THORVG_SUPPORT
310 if(!Drawable::GetObject() || !mTvgShape)
312 DALI_LOG_ERROR("Shape is null [%p]\n", this);
315 mFillGradient = gradient;
316 Drawable::SetChanged(true);
323 Dali::CanvasRenderer::Gradient ShapeTizen::GetFillGradient() const
325 return mFillGradient;
328 bool ShapeTizen::SetFillRule(Dali::CanvasRenderer::Shape::FillRule rule)
330 #ifdef THORVG_SUPPORT
331 if(!Drawable::GetObject() || !mTvgShape)
333 DALI_LOG_ERROR("Shape is null\n");
336 if(static_cast<tvg::Shape*>(mTvgShape)->fill(static_cast<tvg::FillRule>(rule)) != tvg::Result::Success)
338 DALI_LOG_ERROR("SetFillRule fail.\n");
341 Drawable::SetChanged(true);
348 Dali::CanvasRenderer::Shape::FillRule ShapeTizen::GetFillRule() const
350 #ifdef THORVG_SUPPORT
351 if(!Drawable::GetObject() || !mTvgShape)
353 DALI_LOG_ERROR("Shape is null\n");
354 return Dali::CanvasRenderer::Shape::FillRule::WINDING;
357 tvg::FillRule rule = static_cast<tvg::Shape*>(mTvgShape)->fillRule();
359 return static_cast<Dali::CanvasRenderer::Shape::FillRule>(rule);
361 return Dali::CanvasRenderer::Shape::FillRule::WINDING;
364 bool ShapeTizen::SetStrokeWidth(float width)
366 #ifdef THORVG_SUPPORT
367 if(!Drawable::GetObject() || !mTvgShape)
369 DALI_LOG_ERROR("Shape is null\n");
373 if(static_cast<tvg::Shape*>(mTvgShape)->stroke(width) != tvg::Result::Success)
375 DALI_LOG_ERROR("SetStrokeWidth fail.\n");
378 Drawable::SetChanged(true);
385 float ShapeTizen::GetStrokeWidth() const
387 #ifdef THORVG_SUPPORT
388 if(!Drawable::GetObject() || !mTvgShape)
390 DALI_LOG_ERROR("Shape is null\n");
393 return static_cast<tvg::Shape*>(mTvgShape)->strokeWidth();
399 bool ShapeTizen::SetStrokeColor(Vector4 color)
401 #ifdef THORVG_SUPPORT
402 if(!Drawable::GetObject() || !mTvgShape)
404 DALI_LOG_ERROR("Shape is null\n");
408 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)
410 DALI_LOG_ERROR("SetStrokeColor fail.\n");
413 Drawable::SetChanged(true);
420 Vector4 ShapeTizen::GetStrokeColor() const
422 #ifdef THORVG_SUPPORT
423 if(!Drawable::GetObject() || !mTvgShape)
425 DALI_LOG_ERROR("Shape is null\n");
426 return Vector4(0, 0, 0, 0);
431 if(static_cast<tvg::Shape*>(mTvgShape)->strokeColor(&r, &g, &b, &a) != tvg::Result::Success)
433 DALI_LOG_ERROR("GetStrokeColor fail.\n");
434 return Vector4(0, 0, 0, 0);
436 return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
438 return Vector4(0, 0, 0, 0);
442 bool ShapeTizen::SetStrokeGradient(Dali::CanvasRenderer::Gradient& gradient)
444 #ifdef THORVG_SUPPORT
445 if(!Drawable::GetObject() || !mTvgShape)
447 DALI_LOG_ERROR("Shape is null [%p]\n", this);
450 mStrokeGradient = gradient;
451 Drawable::SetChanged(true);
458 Dali::CanvasRenderer::Gradient ShapeTizen::GetStrokeGradient() const
460 return mStrokeGradient;
463 bool ShapeTizen::SetStrokeDash(const Dali::Vector<float> dashPattern)
465 #ifdef THORVG_SUPPORT
466 if(!Drawable::GetObject() || !mTvgShape)
468 DALI_LOG_ERROR("Shape is null\n");
472 float* tvgDashPattern = (float*)alloca(sizeof(float) * dashPattern.Count());
474 for(unsigned int i = 0u; i < dashPattern.Count(); ++i)
476 tvgDashPattern[i] = dashPattern[i];
479 if(static_cast<tvg::Shape*>(mTvgShape)->stroke(tvgDashPattern, dashPattern.Count()) != tvg::Result::Success)
481 DALI_LOG_ERROR("SetStrokeDash fail.\n");
484 Drawable::SetChanged(true);
491 Dali::Vector<float> ShapeTizen::GetStrokeDash() const
493 #ifdef THORVG_SUPPORT
494 if(!Drawable::GetObject() || !mTvgShape)
496 DALI_LOG_ERROR("Shape is null\n");
497 return Vector<float>();
499 const float* tvgDashPattern = nullptr;
502 count = static_cast<tvg::Shape*>(mTvgShape)->strokeDash(&tvgDashPattern);
503 if(!tvgDashPattern || count <= 0)
505 DALI_LOG_ERROR("GetStrokeDash() fail.\n");
506 return Vector<float>();
509 Dali::Vector<float> dashPattern;
511 dashPattern.Reserve(count);
513 for(unsigned int i = 0u; i < count; ++i)
515 dashPattern.PushBack(tvgDashPattern[i]);
519 return Vector<float>();
523 bool ShapeTizen::SetStrokeCap(Dali::CanvasRenderer::Shape::StrokeCap cap)
525 #ifdef THORVG_SUPPORT
526 if(!Drawable::GetObject() || !mTvgShape)
528 DALI_LOG_ERROR("Shape is null\n");
531 if(static_cast<tvg::Shape*>(mTvgShape)->stroke(static_cast<tvg::StrokeCap>(cap)) != tvg::Result::Success)
533 DALI_LOG_ERROR("SetStrokeCap fail.\n");
536 Drawable::SetChanged(true);
543 Dali::CanvasRenderer::Shape::StrokeCap ShapeTizen::GetStrokeCap() const
545 #ifdef THORVG_SUPPORT
546 if(!Drawable::GetObject() || !mTvgShape)
548 DALI_LOG_ERROR("Shape is null\n");
549 return Dali::CanvasRenderer::Shape::StrokeCap::SQUARE;
552 tvg::StrokeCap cap = static_cast<tvg::Shape*>(mTvgShape)->strokeCap();
554 return static_cast<Dali::CanvasRenderer::Shape::StrokeCap>(cap);
556 return Dali::CanvasRenderer::Shape::StrokeCap::SQUARE;
559 bool ShapeTizen::SetStrokeJoin(Dali::CanvasRenderer::Shape::StrokeJoin join)
561 #ifdef THORVG_SUPPORT
562 if(!Drawable::GetObject() || !mTvgShape)
564 DALI_LOG_ERROR("Shape is null\n");
568 if(static_cast<tvg::Shape*>(mTvgShape)->stroke(static_cast<tvg::StrokeJoin>(join)) != tvg::Result::Success)
570 DALI_LOG_ERROR("SetStrokejoin fail.\n");
573 Drawable::SetChanged(true);
580 Dali::CanvasRenderer::Shape::StrokeJoin ShapeTizen::GetStrokeJoin() const
582 #ifdef THORVG_SUPPORT
583 if(!Drawable::GetObject() || !mTvgShape)
585 DALI_LOG_ERROR("Shape is null\n");
586 return Dali::CanvasRenderer::Shape::StrokeJoin::BEVEL;
589 tvg::StrokeJoin join = static_cast<tvg::Shape*>(mTvgShape)->strokeJoin();
591 return static_cast<Dali::CanvasRenderer::Shape::StrokeJoin>(join);
593 return Dali::CanvasRenderer::Shape::StrokeJoin::BEVEL;
596 } // namespace Adaptor
598 } // namespace Internal