common: fix compile warnings.
[platform/core/graphics/tizenvg.git] / src / lib / tvgShapeImpl.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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 #ifndef _TVG_SHAPE_IMPL_H_
18 #define _TVG_SHAPE_IMPL_H_
19
20 #include "tvgCommon.h"
21 #include "tvgShapePath.h"
22
23 /************************************************************************/
24 /* Internal Class Implementation                                        */
25 /************************************************************************/
26
27 struct ShapeStroke
28 {
29     float width = 0;
30     uint8_t color[4] = {0, 0, 0, 0};
31     float* dashPattern = nullptr;
32     uint32_t dashCnt = 0;
33     StrokeCap cap = StrokeCap::Square;
34     StrokeJoin join = StrokeJoin::Bevel;
35
36     ~ShapeStroke()
37     {
38         if (dashPattern) free(dashPattern);
39     }
40 };
41
42
43 struct Shape::Impl
44 {
45     ShapePath *path = nullptr;
46     Fill *fill = nullptr;
47     ShapeStroke *stroke = nullptr;
48     uint8_t color[4] = {0, 0, 0, 0};    //r, g, b, a
49     void *edata = nullptr;              //engine data
50     Shape *shape = nullptr;
51     uint32_t flag = RenderUpdateFlag::None;
52
53     Impl(Shape* s) : path(new ShapePath), shape(s)
54     {
55     }
56
57     ~Impl()
58     {
59         if (path) delete(path);
60         if (fill) delete(fill);
61         if (stroke) delete(stroke);
62     }
63
64     bool dispose(RenderMethod& renderer)
65     {
66         return renderer.dispose(*shape, edata);
67     }
68
69     bool render(RenderMethod& renderer)
70     {
71         return renderer.render(*shape, edata);
72     }
73
74     bool update(RenderMethod& renderer, const RenderTransform* transform, RenderUpdateFlag pFlag)
75     {
76         edata = renderer.prepare(*shape, edata, transform, static_cast<RenderUpdateFlag>(pFlag | flag));
77
78         flag = RenderUpdateFlag::None;
79
80         if (edata) return true;
81         return false;
82     }
83
84     bool bounds(float* x, float* y, float* w, float* h)
85     {
86         if (!path) return false;
87         return path->bounds(x, y, w, h);
88     }
89
90     bool strokeWidth(float width)
91     {
92         //TODO: Size Exception?
93
94         if (!stroke) stroke = new ShapeStroke();
95         if (!stroke) return false;
96
97         stroke->width = width;
98         flag |= RenderUpdateFlag::Stroke;
99
100         return true;
101     }
102
103     bool strokeCap(StrokeCap cap)
104     {
105         if (!stroke) stroke = new ShapeStroke();
106         if (!stroke) return false;
107
108         stroke->cap = cap;
109         flag |= RenderUpdateFlag::Stroke;
110
111         return true;
112     }
113
114     bool strokeJoin(StrokeJoin join)
115     {
116         if (!stroke) stroke = new ShapeStroke();
117         if (!stroke) return false;
118
119         stroke->join = join;
120         flag |= RenderUpdateFlag::Stroke;
121
122         return true;
123     }
124
125     bool strokeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
126     {
127         if (!stroke) stroke = new ShapeStroke();
128         if (!stroke) return false;
129
130         stroke->color[0] = r;
131         stroke->color[1] = g;
132         stroke->color[2] = b;
133         stroke->color[3] = a;
134
135         flag |= RenderUpdateFlag::Stroke;
136
137         return true;
138     }
139
140     bool strokeDash(const float* pattern, uint32_t cnt)
141     {
142         assert(pattern);
143
144        if (!stroke) stroke = new ShapeStroke();
145        if (!stroke) return false;
146
147         if (stroke->dashCnt != cnt) {
148             if (stroke->dashPattern) free(stroke->dashPattern);
149             stroke->dashPattern = nullptr;
150         }
151
152         if (!stroke->dashPattern) stroke->dashPattern = static_cast<float*>(malloc(sizeof(float) * cnt));
153         assert(stroke->dashPattern);
154
155         for (uint32_t i = 0; i < cnt; ++i)
156             stroke->dashPattern[i] = pattern[i];
157
158         stroke->dashCnt = cnt;
159         flag |= RenderUpdateFlag::Stroke;
160
161         return true;
162     }
163 };
164
165 #endif //_TVG_SHAPE_IMPL_H_