1 /*
2  * Copyright (c) 2020-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 #ifndef OHOS_ACELITE_CANVAS_COMPONENT_H
17 #define OHOS_ACELITE_CANVAS_COMPONENT_H
18 
19 #include "acelite_config.h"
20 
21 #if (FEATURE_COMPONENT_CANVAS == 1)
22 #include "component.h"
23 #include "non_copyable.h"
24 #include "ui_canvas.h"
25 #include <string>
26 namespace OHOS {
27 namespace ACELite {
28 enum ArgsCount : uint8_t { NUM_1 = 1, NUM_2, NUM_3, NUM_4, NUM_5, NUM_6 };
29 enum ArgsIndex : uint8_t { IDX_0, IDX_1, IDX_2, IDX_3, IDX_4, IDX_5 };
30 struct AttrMap {
31     ACE_DISALLOW_COPY_AND_MOVE(AttrMap);
32     const char *attrName;
33     jerry_external_handler_t setterName;
34     jerry_external_handler_t getterName;
35 };
36 struct MethodMap {
37     ACE_DISALLOW_COPY_AND_MOVE(MethodMap);
38     const char *methodName;
39     jerry_external_handler_t callbackName;
40 };
41 
42 class CanvasComponent final : public Component {
43 public:
44     ACE_DISALLOW_COPY_AND_MOVE(CanvasComponent);
45     CanvasComponent() = delete;
46     CanvasComponent(jerry_value_t options, jerry_value_t children, AppStyleManager *styleManager);
~CanvasComponent()47     ~CanvasComponent() override {}
48 
49 protected:
50     bool CreateNativeViews() override;
51     void ReleaseNativeViews() override;
52     UIView *GetComponentRootView() const override;
53 
54 private:
55     void GetSubFont(const char *font, const uint8_t index, char *&subFont) const;
56     bool FormatArcAngle(double sAngle,
57                         double eAngle,
58                         int16_t &startAngle,
59                         int16_t &endAngle,
60                         bool counterClockwise) const;
61 
62     static void RegisterAttributeFunc(jerry_value_t canvas2ddom,
63                                       const char *attributeName,
64                                       jerry_external_handler_t setterHandler,
65                                       jerry_external_handler_t getterHandler);
66 
67     static void RegisterDrawMethodFunc(jerry_value_t canvas2ddom,
68                                        const char *drawMethodName,
69                                        jerry_external_handler_t handler);
70 
71     static jerry_value_t GetContext(const jerry_value_t func,
72                                     const jerry_value_t dom,
73                                     const jerry_value_t args[],
74                                     const jerry_length_t argsNum);
75 
76     static jerry_value_t FillStyleSetter(const jerry_value_t func,
77                                          const jerry_value_t dom,
78                                          const jerry_value_t args[],
79                                          const jerry_length_t argsNum);
80 
81     static jerry_value_t FillStyleGetter(const jerry_value_t func,
82                                          const jerry_value_t dom,
83                                          const jerry_value_t args[],
84                                          const jerry_length_t argsNum);
85 
86     static jerry_value_t StrokeStyleSetter(const jerry_value_t func,
87                                            const jerry_value_t dom,
88                                            const jerry_value_t args[],
89                                            const jerry_length_t argsNum);
90 
91     static jerry_value_t StrokeStyleGetter(const jerry_value_t func,
92                                            const jerry_value_t dom,
93                                            const jerry_value_t args[],
94                                            const jerry_length_t argsNum);
95 
96     static jerry_value_t LineWidthSetter(const jerry_value_t func,
97                                          const jerry_value_t dom,
98                                          const jerry_value_t args[],
99                                          const jerry_length_t argsNum);
100 
101     static jerry_value_t LineWidthGetter(const jerry_value_t func,
102                                          const jerry_value_t dom,
103                                          const jerry_value_t args[],
104                                          const jerry_length_t argsNum);
105 
106     static jerry_value_t FontSetter(const jerry_value_t func,
107                                     const jerry_value_t dom,
108                                     const jerry_value_t args[],
109                                     const jerry_length_t argsNum);
110 
111     static jerry_value_t FontGetter(const jerry_value_t func,
112                                     const jerry_value_t dom,
113                                     const jerry_value_t args[],
114                                     const jerry_length_t argsNum);
115 
116     static jerry_value_t TextAlignSetter(const jerry_value_t func,
117                                          const jerry_value_t dom,
118                                          const jerry_value_t args[],
119                                          const jerry_length_t argsNum);
120 
121     static jerry_value_t TextAlignGetter(const jerry_value_t func,
122                                          const jerry_value_t contex,
123                                          const jerry_value_t args[],
124                                          const jerry_length_t argsNum);
125 #if GRAPHIC_ENABLE_LINECAP_FLAG
126     static jerry_value_t LineCapSetter(const jerry_value_t func,
127                                        const jerry_value_t contex,
128                                        const jerry_value_t args[],
129                                        const jerry_length_t argsNum);
130 
131     static jerry_value_t LineCapGetter(const jerry_value_t func,
132                                        const jerry_value_t context,
133                                        const jerry_value_t args[],
134                                        const jerry_length_t argsNum);
135 #endif
136 #if GRAPHIC_ENABLE_LINEJOIN_FLAG
137     static jerry_value_t LineJoinSetter(const jerry_value_t func,
138                                         const jerry_value_t contex,
139                                         const jerry_value_t args[],
140                                         const jerry_length_t argsNum);
141 
142     static jerry_value_t LineJoinGetter(const jerry_value_t func,
143                                         const jerry_value_t contex,
144                                         const jerry_value_t args[],
145                                         const jerry_length_t argsNum);
146 
147     static jerry_value_t MiterLimitSetter(const jerry_value_t func,
148                                           const jerry_value_t contex,
149                                           const jerry_value_t args[],
150                                           const jerry_length_t argsNum);
151 
152     static jerry_value_t MiterLimitGetter(const jerry_value_t func,
153                                           const jerry_value_t contex,
154                                           const jerry_value_t args[],
155                                           const jerry_length_t argsNum);
156 #endif
157 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
158     static jerry_value_t LineDashOffsetSetter(const jerry_value_t func,
159                                               const jerry_value_t contex,
160                                               const jerry_value_t args[],
161                                               const jerry_length_t argsNum);
162 
163     static jerry_value_t LineDashOffsetGetter(const jerry_value_t func,
164                                               const jerry_value_t contex,
165                                               const jerry_value_t args[],
166                                               const jerry_length_t argsNum);
167 #endif
168     static jerry_value_t GlobalAlphaSetter(const jerry_value_t func,
169                                            const jerry_value_t dom,
170                                            const jerry_value_t args[],
171                                            const jerry_length_t argsNum);
172 
173     static jerry_value_t GlobalAlphaGetter(const jerry_value_t func,
174                                            const jerry_value_t dom,
175                                            const jerry_value_t args[],
176                                            const jerry_length_t argsNum);
177 
178     static jerry_value_t GlobalCompositeOperationSetter(const jerry_value_t func,
179                                                         const jerry_value_t dom,
180                                                         const jerry_value_t args[],
181                                                         const jerry_length_t argsNum);
182 
183 #if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG
184     static jerry_value_t ShadowOffsetXSetter(const jerry_value_t func,
185                                              const jerry_value_t dom,
186                                              const jerry_value_t args[],
187                                              const jerry_length_t argsNum);
188 
189     static jerry_value_t ShadowOffsetXGetter(const jerry_value_t func,
190                                              const jerry_value_t dom,
191                                              const jerry_value_t args[],
192                                              const jerry_length_t argsNum);
193     static jerry_value_t ShadowOffsetYSetter(const jerry_value_t func,
194                                              const jerry_value_t dom,
195                                              const jerry_value_t args[],
196                                              const jerry_length_t argsNum);
197 
198     static jerry_value_t ShadowOffsetYGetter(const jerry_value_t func,
199                                              const jerry_value_t dom,
200                                              const jerry_value_t args[],
201                                              const jerry_length_t argsNum);
202     static jerry_value_t ShadowColorSetter(const jerry_value_t func,
203                                            const jerry_value_t dom,
204                                            const jerry_value_t args[],
205                                            const jerry_length_t argsNum);
206 
207     static jerry_value_t ShadowColorGetter(const jerry_value_t func,
208                                            const jerry_value_t dom,
209                                            const jerry_value_t args[],
210                                            const jerry_length_t argsNum);
211     static jerry_value_t ShadowBlurSetter(const jerry_value_t func,
212                                           const jerry_value_t dom,
213                                           const jerry_value_t args[],
214                                           const jerry_length_t argsNum);
215 
216     static jerry_value_t ShadowBlurGetter(const jerry_value_t func,
217                                           const jerry_value_t dom,
218                                           const jerry_value_t args[],
219                                           const jerry_length_t argsNum);
220 #endif
221 
222     static jerry_value_t GlobalCompositeOperationGetter(const jerry_value_t func,
223                                                         const jerry_value_t dom,
224                                                         const jerry_value_t args[],
225                                                         const jerry_length_t argsNum);
226 
227     static jerry_value_t FillRect(const jerry_value_t func,
228                                   const jerry_value_t dom,
229                                   const jerry_value_t args[],
230                                   const jerry_length_t argsNum);
231 
232     static jerry_value_t StrokeRect(const jerry_value_t func,
233                                     const jerry_value_t dom,
234                                     const jerry_value_t args[],
235                                     const jerry_length_t argsNum);
236 
237     static jerry_value_t FillText(const jerry_value_t func,
238                                   const jerry_value_t dom,
239                                   const jerry_value_t args[],
240                                   const jerry_length_t argsNum);
241 
242     static jerry_value_t BeginPath(const jerry_value_t func,
243                                    const jerry_value_t dom,
244                                    const jerry_value_t args[],
245                                    const jerry_length_t argsNum);
246 
247     static jerry_value_t MoveTo(const jerry_value_t func,
248                                 const jerry_value_t dom,
249                                 const jerry_value_t args[],
250                                 const jerry_length_t argsNum);
251 
252     static jerry_value_t LineTo(const jerry_value_t func,
253                                 const jerry_value_t dom,
254                                 const jerry_value_t args[],
255                                 const jerry_length_t argsNum);
256 
257     static jerry_value_t Rect(const jerry_value_t func,
258                               const jerry_value_t dom,
259                               const jerry_value_t args[],
260                               const jerry_length_t argsNum);
261 
262     static jerry_value_t CleanRect(const jerry_value_t func,
263                                    const jerry_value_t dom,
264                                    const jerry_value_t args[],
265                                    const jerry_length_t argsNum);
266 
267     static jerry_value_t Arc(const jerry_value_t func,
268                              const jerry_value_t dom,
269                              const jerry_value_t args[],
270                              const jerry_length_t argsNum);
271 
272     static jerry_value_t ClosePath(const jerry_value_t func,
273                                    const jerry_value_t dom,
274                                    const jerry_value_t args[],
275                                    const jerry_length_t argsNum);
276 
277     static jerry_value_t Stroke(const jerry_value_t func,
278                                 const jerry_value_t dom,
279                                 const jerry_value_t args[],
280                                 const jerry_length_t argsNum);
281 
282     static jerry_value_t Fill(const jerry_value_t func,
283                               const jerry_value_t dom,
284                               const jerry_value_t args[],
285                               const jerry_length_t argsNum);
286 #if GRAPHIC_ENABLE_DRAW_IMAGE_FLAG
287     static jerry_value_t DrawImage(const jerry_value_t func,
288                                    const jerry_value_t context,
289                                    const jerry_value_t args[],
290                                    const jerry_length_t argsNum);
291 #endif
292 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
293     static jerry_value_t SetLineDash(const jerry_value_t func,
294                                      const jerry_value_t context,
295                                      const jerry_value_t args[],
296                                      const jerry_length_t argsNum);
297     static jerry_value_t GetLineDash(const jerry_value_t func,
298                                      const jerry_value_t context,
299                                      const jerry_value_t args[],
300                                      const jerry_length_t argsNum);
301 #endif
302 #if GRAPHIC_ENABLE_DRAW_TEXT_FLAG
303     static jerry_value_t StrokeText(const jerry_value_t func,
304                                     const jerry_value_t context,
305                                     const jerry_value_t args[],
306                                     const jerry_length_t argsNum);
307 #endif
308     static jerry_value_t Rotate(const jerry_value_t func,
309                                 const jerry_value_t dom,
310                                 const jerry_value_t args[],
311                                 const jerry_length_t argsNum);
312 
313     static jerry_value_t Scale(const jerry_value_t func,
314                                const jerry_value_t dom,
315                                const jerry_value_t args[],
316                                const jerry_length_t argsNum);
317 
318     static jerry_value_t MeasureText(const jerry_value_t func,
319                                      const jerry_value_t context,
320                                      const jerry_value_t args[],
321                                      const jerry_length_t argsNum);
322 
323     static jerry_value_t Translate(const jerry_value_t func,
324                                    const jerry_value_t dom,
325                                    const jerry_value_t args[],
326                                    const jerry_length_t argsNum);
327 
328     static jerry_value_t Transform(const jerry_value_t func,
329                                    const jerry_value_t dom,
330                                    const jerry_value_t args[],
331                                    const jerry_length_t argsNum);
332 
333     static jerry_value_t SetTransform(const jerry_value_t func,
334                                       const jerry_value_t dom,
335                                       const jerry_value_t args[],
336                                       const jerry_length_t argsNum);
337 #if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
338     static jerry_value_t CreateLInearGradient(const jerry_value_t func,
339                                               const jerry_value_t dom,
340                                               const jerry_value_t args[],
341                                               const jerry_length_t argsNum);
342 
343     static jerry_value_t CreateRadialGradient(const jerry_value_t func,
344                                               const jerry_value_t dom,
345                                               const jerry_value_t args[],
346                                               const jerry_length_t argsNum);
347 
348     static jerry_value_t AddColorStop(const jerry_value_t func,
349                                       const jerry_value_t dom,
350                                       const jerry_value_t args[],
351                                       const jerry_length_t argsNum);
352 #endif
353 #if GRAPHIC_ENABLE_PATTERN_FILL_FLAG
354     static jerry_value_t CreatePattern(const jerry_value_t func,
355                                        const jerry_value_t context,
356                                        const jerry_value_t args[],
357                                        const jerry_length_t argsNum);
358 #endif
359     static jerry_value_t Save(const jerry_value_t func,
360                               const jerry_value_t dom,
361                               const jerry_value_t args[],
362                               const jerry_length_t argsNum);
363 
364     static jerry_value_t Restore(const jerry_value_t func,
365                                  const jerry_value_t dom,
366                                  const jerry_value_t args[],
367                                  const jerry_length_t argsNum);
368 
369     static jerry_value_t DrawCircle(const jerry_value_t func,
370                                     const jerry_value_t dom,
371                                     const jerry_value_t args[],
372                                     const jerry_length_t argsNum);
373     static jerry_value_t ParseImageName(const jerry_value_t args[],
374                                         char*& imageName,
375                                         int16_t &width,
376                                         int16_t &height);
377     static char* GetImageObjectParam(const jerry_value_t args[], int16_t &width, int16_t &height);
378     UICanvas canvas_;
379     jerry_value_t dom_;
380     jerry_value_t dashArray_;
381     jerry_value_t measureTextObject_;
382     jerry_value_t measureTextWidthString_;
383     Paint paint_;
384     UICanvas::FontStyle fontStyle_;
385     char *fillStyleValue_;
386     char *strokeStyleValue_;
387     char *fontValue_;
388     char *textAlignValue_;
389 #if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG
390     int16_t shadowOffsetXValue_;
391     int16_t shadowOffsetYValue_;
392     int16_t shadowBlurValue_;
393     char *shadowColorValue_;
394 #endif
395     int16_t lineWidthValue_;
396 
397     char *lineCapValue_;
398     char *lineJoinValue_;
399 #if GRAPHIC_ENABLE_LINEJOIN_FLAG
400     float miterLimitValue_;
401 #endif
402 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
403     float lineDashOffsetValue_;
404 #endif
405 #if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
406     char *colorStopValue_;
407 #endif
408 #if GRAPHIC_ENABLE_PATTERN_FILL_FLAG
409     char *patternPathValue_;
410     char *patternRepeatTypeValue_;
411 #endif
412     static const char * const DEFAULT_FILLSTYLE;
413     static const char * const DEFAULT_STROKESTYLE;
414     static const char * const DEFAULT_TEXTALIGN;
415 #if GRAPHIC_ENABLE_LINECAP_FLAG
416     static const char * const DEFAULT_LINECAP;
417 #endif
418 #if GRAPHIC_ENABLE_LINEJOIN_FLAG
419     static const char * const DEFAULT_LINEJOIN;
420     static const int16_t DEFAULT_MITERLIMIT;
421 #endif
422 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
423     static const int16_t DEFAULT_LINEDASHOFFSET;
424 #endif
425     static const AttrMap attrMap_[];
426     static const MethodMap methodMap_[];
427     static const char * const ATTR_FILLSTYLE;
428     static const char * const ATTR_STROKESTYLE;
429     static const char * const ATTR_LINEWIDTH;
430     static const char * const ATTR_FONT;
431     static const char * const ATTR_TEXTALIGN;
432 #if GRAPHIC_ENABLE_LINECAP_FLAG
433     static const char * const ATTR_LINECAP;
434 #endif
435 #if GRAPHIC_ENABLE_LINEJOIN_FLAG
436     static const char * const ATTR_LINEJOIN;
437     static const char * const ATTR_MITERLIMIT;
438 #endif
439 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
440     static const char * const ATTR_LINEDASHOFFSET;
441 #endif
442     static const char * const ATTR_GLOBALALPHA;
443     static const char * const ATTR_GLOBALCOMPOSITEOPERATION;
444 #if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG
445     static const char * const ATTR_SHADOWOFFSETX;
446     static const char * const ATTR_SHADOWOFFSETY;
447     static const char * const ATTR_SHADOWBLUR;
448     static const char * const ATTR_SHADOWCOLOR;
449 #endif
450     static const char * const FUNC_GETCONTEXT;
451     static const char * const FUNC_FILLRECT;
452     static const char * const FUNC_STROKERECT;
453     static const char * const FUNC_FILLTEXT;
454     static const char * const FUNC_BEGINPATH;
455     static const char * const FUNC_MOVETO;
456     static const char * const FUNC_LINETO;
457     static const char * const FUNC_RECT;
458     static const char * const FUNC_CLEANRECT;
459     static const char * const FUNC_ARC;
460     static const char * const FUNC_CLOSEPATH;
461     static const char * const FUNC_STROKE;
462     static const char * const FUNC_FILL;
463 #if GRAPHIC_ENABLE_DRAW_IMAGE_FLAG
464     static const char * const FUNC_DRAWIMAGE;
465 #endif
466 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
467     static const char * const FUNC_SETLINEDASH;
468     static const char * const FUNC_GETLINEDASH;
469 #endif
470 #if GRAPHIC_ENABLE_DRAW_TEXT_FLAG
471     static const char * const FUNC_STROKETEXT;
472 #endif
473     static const char * const FUNC_ROTATE;
474     static const char * const FUNC_SCALE;
475     static const char * const FUNC_MEASURETEXT;
476     static const char * const FUNC_TRANSLATE;
477     static const char * const FUNC_TRANFORM;
478     static const char * const FUNC_SETTRANFORM;
479     static const char * const FUNC_SAVE;
480     static const char * const FUNC_RESTORE;
481     static const char * const FUNC_DRAWCIRCLE;
482 #if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
483     static const char * const FUNC_CREATELINEARGRADIENT;
484     static const char * const FUNC_CREATERADIALGRADIENT;
485     static const char * const FUNC_ADDCOLORSTOP;
486 #endif
487 #if GRAPHIC_ENABLE_PATTERN_FILL_FLAG
488     static const char * const FUNC_CREATEPATTERN;
489 #endif
490 };
491 } // namespace ACELite
492 } // namespace OHOS
493 #endif // FEATURE_COMPONENT_CANVAS
494 #endif // OHOS_ACELITE_CANVAS_COMPONENT_H
495