1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup GraphicGeometry
18  * @{
19  *
20  * @brief Defines Graphic operation
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file common_basics.h
28  *
29  * @brief Defines Basic data types and general functions of graphic operations
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_COMMON_BASICS_H
36 #define GRAPHIC_LITE_COMMON_BASICS_H
37 
38 #include "gfx_utils/graphic_math.h"
39 #include "graphic_config.h"
40 namespace OHOS {
41 enum VertexGenerateFlags {
42     GENERATE_DASH,
43     GENERATE_STROKE
44 };
45 
46 /**
47  * @brief Fill rule
48  * @since 1.0
49  * @version 1.0
50  */
51 enum FillingRule {
52     FILL_NON_ZERO,
53     FILL_EVEN_ODD
54 };
55 
56 
57 /**
58  * repeat Spread the whole canvas
59  * repeat-x Repeat on the X axis of the canvas
60  * repeat-y Repeat on the Y axis of the canvas
61  * no-repeat Do not repeat on canvas
62  */
63 enum PatternRepeatMode {
64     REPEAT,
65     REPEAT_X,
66     REPEAT_Y,
67     NO_REPEAT,
68 };
69 
70 /**
71  * @brief Sub - pixel offset and mask flag
72  * @since 1.0
73  * @version 1.0
74  */
75 enum PolySubpixelScale {
76     POLY_SUBPIXEL_SHIFT = 8,
77     POLY_SUBPIXEL_SCALE = 1 << POLY_SUBPIXEL_SHIFT,
78     POLY_SUBPIXEL_MASK = POLY_SUBPIXEL_SCALE - 1
79 };
80 
81 enum GradientSubpixelScale {
82     GRADIENT_SUBPIXEL_SHIFT = 4,
83     GRADIENT_SUBPIXEL_SCALE = 1 << GRADIENT_SUBPIXEL_SHIFT,
84     GRADIENT_SUBPIXEL_MASK = GRADIENT_SUBPIXEL_SCALE - 1
85 };
86 /**
87  * @brief Elastic treatment of coverage
88  * @since 1.0
89  * @version 1.0
90  */
91 enum CoverScale {
92     COVER_SHIFT = 8,
93     COVER_SIZE = 1 << COVER_SHIFT,
94     COVER_MASK = COVER_SIZE - 1,
95     COVER_NONE = 0,
96     COVER_FULL = COVER_MASK
97 };
98 
99 enum PathCommands {
100     PATH_CMD_STOP = 0,
101     PATH_CMD_MOVE_TO = 1,
102     PATH_CMD_LINE_TO = 2,
103     PATH_CMD_CURVE3 = 3,      // Conic command
104     PATH_CMD_CURVE4 = 4,      // Cubic curve command
105     PATH_CMD_CURVEN = 5,      // Curve command
106     PATH_CMD_CARROM = 6,      // Conic command
107     PATH_CMD_UBSPLINE = 7,    // Curve command
108     PATH_CMD_END_POLY = 0x0F, // Polygon closure
109     PATH_CMD_MASK = 0x0F
110 };
111 
112 enum PathFlags {
113     PATH_FLAGS_NONE = 0,
114     PATH_FLAGS_CLOCKWISE = 0x10,      // Clockwise
115     PATH_FLAGS_ANTI_CLOCKWISE = 0x20, // anti-clockwise
116     PATH_FLAGS_CLOSE = 0x40,
117     PATH_FLAGS_MASK = 0xF0
118 };
119 
120 /**
121  * @brief Four cases of collinearity
122  * @since 1.0
123  * @version 1.0
124  */
125 enum Recursive {
126     COLLINEAR = 0,
127     COLLINEAR1,
128     COLLINEAR2,
129     COLLINEAR3
130 };
131 enum {
132     CLIPPING_FLAGS_X1_CLIPPED = 4,
133     CLIPPING_FLAGS_X2_CLIPPED = 1,
134     CLIPPING_FLAGS_Y1_CLIPPED = 8,
135     CLIPPING_FLAGS_Y2_CLIPPED = 2,
136     CLIPPING_FLAGS_X_CLIPPED = CLIPPING_FLAGS_X1_CLIPPED | CLIPPING_FLAGS_X2_CLIPPED,
137     CLIPPING_FLAGS_Y_CLIPPED = CLIPPING_FLAGS_Y1_CLIPPED | CLIPPING_FLAGS_Y2_CLIPPED
138 };
139 
140 enum GlobalCompositeOperation {
141     /**
142      * Default. Displays the source image on the target image.
143      */
144     SOURCE_OVER,
145     /**
146      * Displays the source image at the top of the target image.
147      * The part of the source image outside the target image is invisible.
148      */
149     SOURCE_ATOP,
150     /**
151      * Displays the source image in the target image.
152      * Only the source image part within the target image will be displayed, and the target image is transparent.
153      */
154     SOURCE_IN,
155     /**
156      * Displays the source image outside the target image.
157      * Only the source image part other than the target image will be displayed,and the target image is transparent
158      */
159     SOURCE_OUT,
160     /**
161      * Displays the target image on the source image.
162      */
163     DESTINATION_OVER,
164     /**
165      * Displays the target image at the top of the source image.
166      * The part of the target image outside the source image is invisible.
167      */
168     DESTINATION_ATOP,
169     /**
170      * Displays the target image in the source image.
171      * Only the target image part within the source image will be displayed, and the source image is transparent.
172      */
173     DESTINATION_IN,
174     /**
175      * Displays the target image outside the source image.
176      * Only the part of the target image other than the source image will be displayed,
177      * and the source image is transparent.
178      */
179     DESTINATION_OUT,
180     /**
181      * Display source image + target image.
182      */
183     LIGHTER,
184     /**
185      * Displays the source image. Ignore the target image.
186      */
187     COPY,
188     /**
189      * Use the XOR operation to combine the source image with the target image.
190      */
191     XOR
192 };
193 
194 const float COEFFICIENT = 0.7f;
195 const float ALPHA_HALF = 0.5f;
196 const float VERTEX_DIST_EPSILON = 1e-14;
197 const float INTERSECTIONEPSILON = 1.0e-30;
198 /**
199  * @brief PI
200  */
201 const float PI = 3.1415926f;
202 /**
203  * @brief Semicircle arc angle
204  */
205 const float RAD_BASE = 180.0f;
206 const float ROUND_BASE = 0.5f;
207 
208 /**
209  * @brief Degree of flat angle
210  */
211 const float BOXER = 180.0f;
212 
213 /**
214  * @brief BUF_SIZE
215  */
216 const float BUF_SIZE = 1024.0f;
217 /**
218  * @brief Maximum color
219  */
220 const uint32_t MAX_COLOR_NUM = 255;
221 /**
222  * @brief Maximum array length of colors
223  */
224 const uint32_t MAX_COLOR_SIZE = 256;
225 
226 /**
227  * @brief Half the maximum value of the color
228  */
229 const uint32_t HALF_COLOR_NUM = 128;
230 /**
231  * @brief One eighth of the maximum value of the color
232  */
233 const uint32_t EIGHTH_COLOR_NUM = 32;
234 /**
235  * @brief One thirty second of the maximum value of the color
236  */
237 const uint32_t THIRTY_TWO_COLOR_NUM = 8;
238 
239 /**
240  * @brief Default maximum miter
241  */
242 const uint32_t DEFAULTMITERLIMIT = 10;
243 /**
244  * @brief Length of one byte
245  */
246 const int32_t BYTE_LENGTH = 8;
247 const int32_t CELLS_SIZE = 16;
248 /**
249  * @brief Maximum length of dash array
250  */
251 const int32_t MAX_DASHES = 32;
252 /**
253  * @brief Default block offset
254  */
255 const int32_t BLOCK_SHIFT_SIZE = 6;
256 /**
257  * @brief The number of vertices required to draw the ellipse
258  */
259 const int32_t ELLIPSE_VERTEX_NUM = 4;
260 /**
261  * The general stride is four times the width
262  */
263 const int32_t STRIDE_FACTOR = 4;
264 const int32_t PXSIZE2STRIDE_FACTOR = 3;
265 /**
266  * @brief 1 / √ 2 is 1 divided by √ 2
267  */
268 const float SIN45 = 0.7071068f;
269 /**
270  * @brief 1 / √ 2 is 1 divided by √ 2
271  */
272 const float COS45 = 0.7071068f;
273 /**
274  * @brief twice as much
275  */
276 const int32_t TWO_TIMES = 2;
277 /**
278  * @brief triple
279  */
280 const int32_t THREE_TIMES = 3;
281 /**
282  * @brief fourfold
283  */
284 const int32_t FOUR_TIMES = 4;
285 /**
286  * @brief Sixfold
287  */
288 const int32_t SIX_TIMES = 6;
289 /**
290  * @brief hundredfold
291  */
292 const int32_t HUNDRED_TIMES = 100;
293 /**
294  * @brief Take two steps at a time
295  */
296 const int32_t TWO_STEP = 2;
297 const float HALFNUM = 0.5f;
298 const float FLOATNUM = 2.0f;
299 const float RADDALETAELPS = 0.125f;
300 
301 const int32_t CONSTITUTION = 16384;
302 /**
303  * @brief Directly below
304  */
305 const int32_t DIRECTLY_BELOW = 10;
306 
307 /**
308  * @brief Determines whether the value is a vertex
309  * @since 1.0
310  * @version 1.0
311  */
IsVertex(uint32_t val)312 inline bool IsVertex(uint32_t val)
313 {
314     return val < PATH_CMD_END_POLY && val >= PATH_CMD_MOVE_TO;
315 }
316 
317 /**
318  * @brief Judge whether the current status is MOVE_TO.
319  * @since 1.0
320  * @version 1.0
321  */
IsMoveTo(uint32_t val)322 inline bool IsMoveTo(uint32_t val)
323 {
324     return PATH_CMD_MOVE_TO == val;
325 }
326 
327 /**
328  * @brief Judge whether the current status is stop status
329  * @since 1.0
330  * @version 1.0
331  */
IsStop(uint32_t val)332 inline bool IsStop(uint32_t val)
333 {
334     return PATH_CMD_STOP == val;
335 }
336 
337 /**
338  * @brief Judge whether the current state is the state of drawing the end of the curve
339  * @since 1.0
340  * @version 1.0
341  */
IsEndPoly(uint32_t val)342 inline bool IsEndPoly(uint32_t val)
343 {
344     return PATH_CMD_END_POLY == (val & PATH_CMD_MASK);
345 }
346 
347 /**
348  * @brief Judge whether the current state is the closed state of drawing curve
349  * @since 1.0
350  * @version 1.0
351  */
IsClose(uint32_t val)352 inline bool IsClose(uint32_t val)
353 {
354     return (val & ~(PATH_FLAGS_ANTI_CLOCKWISE | PATH_FLAGS_CLOCKWISE)) == (PATH_CMD_END_POLY | PATH_FLAGS_CLOSE);
355 }
356 
357 /**
358  * @brief Get closed state
359  * @since 1.0
360  * @version 1.0
361  */
GetCloseFlag(uint32_t val)362 inline uint32_t GetCloseFlag(uint32_t val)
363 {
364     return val & PATH_FLAGS_CLOSE;
365 }
366 
367 template <class T>
368 struct GeometryArrayAllocator {
369     /**
370      * @brief Array memory allocation
371      * @since 1.0
372      * @version 1.0
373      */
AllocateGeometryArrayAllocator374     static T* Allocate(uint32_t num)
375     {
376         if (num < 1) {
377             num = 1;
378         }
379         return static_cast<T*>(UIMalloc(num * sizeof(T)));
380     }
381     /**
382      * @brief Array memory free
383      * @since 1.0
384      * @version 1.0
385      */
DeallocateGeometryArrayAllocator386     static void Deallocate(T* ptr, uint32_t)
387     {
388         UIFree(ptr);
389     }
390 };
391 
392 struct RowData {
393     int32_t x1;
394     int32_t x2;
395     const uint8_t* ptr;
RowDataRowData396     RowData() {}
RowDataRowData397     RowData(int32_t x1_, int32_t x2_, const uint8_t* ptr_)
398         : x1(x1_), x2(x2_), ptr(ptr_) {}
399 };
400 /* Indicates a point in float */
401 struct PointF {
402     float x;
403     float y;
PointFPointF404     PointF() {}
PointFPointF405     PointF(float x_, float y_) : x(x_), y(y_) {}
406 };
407 } // namespace OHOS
408 #endif
409