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 #include "acelite_config.h"
17 
18 #if (defined(FEATURE_COMPONENT_CANVAS) && (FEATURE_COMPONENT_CANVAS == 1))
19 #include "canvas_component.h"
20 #include "js_fwk_common.h"
21 #include "product_adapter.h"
22 #include "securec.h"
23 #include "jerryscript.h"
24 #include "modules/presets/image_module.h"
25 #include "image_component.h"
26 
27 namespace OHOS {
28 namespace ACELite {
29 // default fill style color=black
30 const char * const CanvasComponent::DEFAULT_FILLSTYLE = "#000000";
31 // default stroke style color=black
32 const char * const CanvasComponent::DEFAULT_STROKESTYLE = "#000000";
33 // default text align=left
34 const char * const CanvasComponent::DEFAULT_TEXTALIGN = "left";
35 // default line lineCap
36 #if GRAPHIC_ENABLE_LINECAP_FLAG
37 const char * const CanvasComponent::DEFAULT_LINECAP = "butt";
38 #endif
39 // default line lineJoin
40 #if GRAPHIC_ENABLE_LINEJOIN_FLAG
41 const char * const CanvasComponent::DEFAULT_LINEJOIN = "miter";
42 // default line lineJoin
43 const int16_t CanvasComponent::DEFAULT_MITERLIMIT = 10;
44 #endif
45 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
46 // default lineDashOffset
47 const int16_t CanvasComponent::DEFAULT_LINEDASHOFFSET = 0;
48 #endif
49 // API-attribute
50 const char * const CanvasComponent::ATTR_FILLSTYLE = "fillStyle";
51 const char * const CanvasComponent::ATTR_STROKESTYLE = "strokeStyle";
52 const char * const CanvasComponent::ATTR_LINEWIDTH = "lineWidth";
53 const char * const CanvasComponent::ATTR_FONT = "font";
54 const char * const CanvasComponent::ATTR_TEXTALIGN = "textAlign";
55 #if GRAPHIC_ENABLE_LINECAP_FLAG
56 const char * const CanvasComponent::ATTR_LINECAP = "lineCap";
57 #endif
58 #if GRAPHIC_ENABLE_LINEJOIN_FLAG
59 const char * const CanvasComponent::ATTR_LINEJOIN = "lineJoin";
60 const char * const CanvasComponent::ATTR_MITERLIMIT = "miterLimit";
61 #endif
62 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
63 const char * const CanvasComponent::ATTR_LINEDASHOFFSET = "lineDashOffset";
64 #endif
65 const char * const CanvasComponent::ATTR_GLOBALALPHA = "globalAlpha";
66 const char * const CanvasComponent::ATTR_GLOBALCOMPOSITEOPERATION = "globalCompositeOperation";
67 #if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG
68 const char * const CanvasComponent::ATTR_SHADOWOFFSETX = "shadowOffsetX";
69 const char * const CanvasComponent::ATTR_SHADOWOFFSETY = "shadowOffsetY";
70 const char * const CanvasComponent::ATTR_SHADOWCOLOR = "shadowColor";
71 const char * const CanvasComponent::ATTR_SHADOWBLUR = "shadowBlur";
72 #endif
73 // API-method
74 const char * const CanvasComponent::FUNC_GETCONTEXT = "getContext";
75 const char * const CanvasComponent::FUNC_FILLRECT = "fillRect";
76 const char * const CanvasComponent::FUNC_STROKERECT = "strokeRect";
77 const char * const CanvasComponent::FUNC_FILLTEXT = "fillText";
78 const char * const CanvasComponent::FUNC_BEGINPATH = "beginPath";
79 const char * const CanvasComponent::FUNC_MOVETO = "moveTo";
80 const char * const CanvasComponent::FUNC_LINETO = "lineTo";
81 const char * const CanvasComponent::FUNC_RECT = "rect";
82 const char * const CanvasComponent::FUNC_CLEANRECT = "cleanRect";
83 const char * const CanvasComponent::FUNC_ARC = "arc";
84 const char * const CanvasComponent::FUNC_CLOSEPATH = "closePath";
85 const char * const CanvasComponent::FUNC_STROKE = "stroke";
86 #if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
87 const char * const CanvasComponent::FUNC_CREATELINEARGRADIENT = "createLinearGradient";
88 const char * const CanvasComponent::FUNC_CREATERADIALGRADIENT = "createRadialGradient";
89 const char * const CanvasComponent::FUNC_ADDCOLORSTOP = "addColorStop";
90 #endif
91 #if GRAPHIC_ENABLE_PATTERN_FILL_FLAG
92 const char * const CanvasComponent::FUNC_CREATEPATTERN = "createPattern";
93 #endif
94 const char * const CanvasComponent::FUNC_FILL = "fill";
95 #if GRAPHIC_ENABLE_DRAW_IMAGE_FLAG
96 const char * const CanvasComponent::FUNC_DRAWIMAGE = "drawImage";
97 #endif
98 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
99 const char * const CanvasComponent::FUNC_SETLINEDASH = "setLineDash";
100 const char * const CanvasComponent::FUNC_GETLINEDASH = "getLineDash";
101 #endif
102 #if GRAPHIC_ENABLE_DRAW_TEXT_FLAG
103 const char * const CanvasComponent::FUNC_STROKETEXT = "strokeText";
104 #endif
105 const char * const CanvasComponent::FUNC_ROTATE = "rotate";
106 const char * const CanvasComponent::FUNC_SCALE = "scale";
107 const char * const CanvasComponent::FUNC_MEASURETEXT = "measureText";
108 const char * const CanvasComponent::FUNC_TRANSLATE = "translate";
109 const char * const CanvasComponent::FUNC_TRANFORM = "transform";
110 const char * const CanvasComponent::FUNC_SETTRANFORM = "setTransform";
111 const char * const CanvasComponent::FUNC_SAVE = "save";
112 const char * const CanvasComponent::FUNC_RESTORE = "restore";
113 const char * const CanvasComponent::FUNC_DRAWCIRCLE = "drawCircle";
114 // create canvas draw attribute-callback mapping
115 const AttrMap CanvasComponent::attrMap_[] = {
116     {ATTR_FILLSTYLE, FillStyleSetter, FillStyleGetter},
117     {ATTR_STROKESTYLE, StrokeStyleSetter, StrokeStyleGetter},
118     {ATTR_LINEWIDTH, LineWidthSetter, LineWidthGetter},
119     {ATTR_FONT, FontSetter, FontGetter},
120     {ATTR_TEXTALIGN, TextAlignSetter, TextAlignGetter},
121 #if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG
122     {ATTR_SHADOWOFFSETX, ShadowOffsetXSetter, ShadowOffsetXGetter},
123     {ATTR_SHADOWOFFSETY, ShadowOffsetYSetter, ShadowOffsetYGetter},
124     {ATTR_SHADOWCOLOR, ShadowColorSetter, ShadowColorGetter},
125     {ATTR_SHADOWBLUR, ShadowBlurSetter, ShadowBlurGetter},
126 #endif
127 #if GRAPHIC_ENABLE_LINECAP_FLAG
128     {ATTR_LINECAP, LineCapSetter, LineCapGetter},
129 #endif
130 #if GRAPHIC_ENABLE_LINEJOIN_FLAG
131     {ATTR_LINEJOIN, LineJoinSetter, LineJoinGetter},
132     {ATTR_MITERLIMIT, MiterLimitSetter, MiterLimitGetter},
133 #endif
134 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
135     {ATTR_LINEDASHOFFSET, LineDashOffsetSetter, LineDashOffsetGetter},
136 #endif
137     {ATTR_GLOBALALPHA, GlobalAlphaSetter, GlobalAlphaGetter},
138     {ATTR_GLOBALCOMPOSITEOPERATION, GlobalCompositeOperationSetter, GlobalCompositeOperationGetter}
139 };
140 
141 // create canvas draw method-callback mapping
142 const MethodMap CanvasComponent::methodMap_[] = {
143     {FUNC_GETCONTEXT, GetContext},
144     {FUNC_FILLRECT, FillRect},
145     {FUNC_STROKERECT, StrokeRect},
146     {FUNC_FILLTEXT, FillText},
147     {FUNC_BEGINPATH, BeginPath},
148     {FUNC_MOVETO, MoveTo},
149     {FUNC_LINETO, LineTo},
150     {FUNC_RECT, Rect},
151     {FUNC_CLEANRECT, CleanRect},
152     {FUNC_ARC, Arc},
153     {FUNC_CLOSEPATH, ClosePath},
154     {FUNC_STROKE, Stroke},
155 #if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
156     {FUNC_CREATELINEARGRADIENT, CreateLInearGradient},
157     {FUNC_CREATERADIALGRADIENT, CreateRadialGradient},
158     {FUNC_ADDCOLORSTOP, AddColorStop},
159 #endif
160 #if GRAPHIC_ENABLE_PATTERN_FILL_FLAG
161     {FUNC_CREATEPATTERN, CreatePattern},
162 #endif
163     {FUNC_FILL, Fill},
164 #if GRAPHIC_ENABLE_DRAW_IMAGE_FLAG
165     {FUNC_DRAWIMAGE, DrawImage},
166 #endif
167 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
168     {FUNC_SETLINEDASH, SetLineDash},
169     {FUNC_GETLINEDASH, GetLineDash},
170 #endif
171 #if GRAPHIC_ENABLE_DRAW_TEXT_FLAG
172     {FUNC_STROKETEXT, StrokeText},
173 #endif
174     {FUNC_ROTATE, Rotate},
175     {FUNC_SCALE, Scale},
176     {FUNC_MEASURETEXT, MeasureText},
177     {FUNC_TRANSLATE, Translate},
178     {FUNC_TRANFORM, Transform},
179     {FUNC_SETTRANFORM, SetTransform},
180     {FUNC_SAVE, Save},
181     {FUNC_RESTORE, Restore},
182     {FUNC_DRAWCIRCLE, DrawCircle}
183 };
184 
CanvasComponent(jerry_value_t options,jerry_value_t children,AppStyleManager * styleManager)185 CanvasComponent::CanvasComponent(jerry_value_t options, jerry_value_t children, AppStyleManager *styleManager)
186     : Component(options, children, styleManager),
187       dom_(UNDEFINED),
188       dashArray_(UNDEFINED),
189       measureTextObject_(UNDEFINED),
190       measureTextWidthString_(UNDEFINED),
191       fillStyleValue_(nullptr),
192       strokeStyleValue_(nullptr),
193       fontValue_(nullptr),
194       textAlignValue_(nullptr),
195 #if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG
196       shadowOffsetXValue_(0),
197       shadowOffsetYValue_(0),
198       shadowBlurValue_(0),
199       shadowColorValue_(nullptr),
200 #endif
201 #if GRAPHIC_ENABLE_LINEJOIN_FLAG
202       miterLimitValue_(DEFAULT_MITERLIMIT),
203 #endif
204 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
205       lineDashOffsetValue_(0),
206 #endif
207 #if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
208       colorStopValue_(nullptr),
209 #endif
210 #if GRAPHIC_ENABLE_PATTERN_FILL_FLAG
211       patternPathValue_(nullptr),
212       patternRepeatTypeValue_(nullptr),
213 #endif
214       lineWidthValue_(1),
215       lineCapValue_(nullptr),
216       lineJoinValue_(nullptr)
217 {
218     SetComponentName(K_CANVAS);
219     // set default paint pattern
220     paint_.SetFillColor(Color::Black());
221     paint_.SetOpacity(OPA_OPAQUE);
222     paint_.SetStrokeColor(Color::Black());
223     paint_.SetStrokeWidth(1);
224 
225     // set default text font pattern
226     fontStyle_.align = TEXT_ALIGNMENT_LEFT;
227     fontStyle_.direct = TEXT_DIRECT_LTR;
228     fontStyle_.fontSize = ProductAdapter::GetDefaultFontSize();
229     char *defaultFontName = nullptr;
230     CopyFontFamily(defaultFontName, ProductAdapter::GetDefaultFontFamilyName());
231     fontStyle_.fontName = defaultFontName;
232     fontStyle_.letterSpace = DEFAULT_FONT_LETTERSPACE;
233     RegisterNamedFunction(methodMap_[0].methodName, methodMap_[0].callbackName);
234 }
235 
236 // Create UICanvas
CreateNativeViews()237 bool CanvasComponent::CreateNativeViews()
238 {
239     return true;
240 }
241 
ReleaseNativeViews()242 void CanvasComponent::ReleaseNativeViews()
243 {
244     ACE_FREE(fillStyleValue_);
245     ACE_FREE(strokeStyleValue_);
246     ACE_FREE(fontValue_);
247     ACE_FREE(textAlignValue_);
248     ACE_FREE(lineCapValue_);
249     ACE_FREE(lineJoinValue_);
250     // free fontStyle_.fontName memory which malloc in FontSetter method.
251     if (fontStyle_.fontName != nullptr) {
252         ace_free(const_cast<char *>(fontStyle_.fontName));
253         fontStyle_.fontName = nullptr;
254     }
255     if (!IS_UNDEFINED(dom_)) {
256         bool deleted = jerry_delete_object_native_pointer(dom_, nullptr);
257         if (!deleted) {
258             HILOG_WARN(HILOG_MODULE_ACE, "canvas_component: delete object native pointer dom_ failed!");
259         }
260         jerry_release_value(dom_);
261     }
262     if (!IS_UNDEFINED(dashArray_)) {
263         uint32_t length = jerry_get_array_length(dashArray_);
264         for (uint32_t i = 0; i < length; i++) {
265             jerry_value_t val = jerry_get_property_by_index(dashArray_, i);
266             jerry_release_value(val);
267         }
268         jerry_release_value(dashArray_);
269     }
270     if (!IS_UNDEFINED(jerry_get_property(measureTextObject_, measureTextWidthString_))) {
271         jerry_release_value(jerry_get_property(measureTextObject_, measureTextWidthString_));
272     }
273     if (!IS_UNDEFINED(measureTextWidthString_)) {
274         jerry_release_value(measureTextWidthString_);
275     }
276     if (!IS_UNDEFINED(measureTextObject_)) {
277         jerry_release_value(measureTextObject_);
278     }
279 }
280 
GetComponentRootView() const281 UIView *CanvasComponent::GetComponentRootView() const
282 {
283     return (const_cast<UICanvas *>(&canvas_));
284 }
285 
GetContext(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)286 jerry_value_t CanvasComponent::GetContext(const jerry_value_t func,
287                                           const jerry_value_t dom,
288                                           const jerry_value_t args[],
289                                           const jerry_length_t argsNum)
290 {
291     (void)func;
292     (void)args;
293     if (argsNum < ArgsCount::NUM_1) {
294         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of getContext method parameter error!");
295         return jerry_create_error(JERRY_ERROR_TYPE,
296                                   reinterpret_cast<const jerry_char_t *>("getdom method parameter error"));
297     }
298 
299     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
300     if (component == nullptr) {
301         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
302         return jerry_create_error(JERRY_ERROR_TYPE,
303                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
304     }
305 
306     if (IS_UNDEFINED(component->dom_)) {
307         component->dom_ = jerry_create_object();
308         jerry_set_object_native_pointer(component->dom_, component, nullptr);
309 
310         // register fillStyle, strokeStyle, lineWidth, font, textAlign attribute
311         uint16_t attrMapLength = sizeof(attrMap_) / sizeof(attrMap_[0]);
312         for (uint16_t index = 0; index < attrMapLength; index++) {
313             RegisterAttributeFunc(component->dom_, attrMap_[index].attrName, attrMap_[index].setterName,
314                                   attrMap_[index].getterName);
315         }
316 
317         // register fillRect, strokeRect, fillText, beginPath, moveTo, lineTo, rect, arc, closePath, stroke method
318         uint16_t methodMapLength = sizeof(methodMap_) / sizeof(methodMap_[0]);
319         for (uint16_t index = 1; index < methodMapLength; index++) {
320             RegisterDrawMethodFunc(component->dom_, methodMap_[index].methodName, methodMap_[index].callbackName);
321         }
322     }
323 
324     // BeginPath function need to be called after getting canvas API object
325     component->canvas_.BeginPath();
326 
327     jerry_acquire_value(component->dom_);
328     return component->dom_;
329 }
330 
FillStyleSetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)331 jerry_value_t CanvasComponent::FillStyleSetter(const jerry_value_t func,
332                                                const jerry_value_t dom,
333                                                const jerry_value_t args[],
334                                                const jerry_length_t argsNum)
335 {
336     (void)func;
337     if (argsNum < ArgsCount::NUM_1) {
338         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: fillStyle value error!");
339         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("fillStyle value error"));
340     }
341 
342     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
343     if (component == nullptr) {
344         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
345         return jerry_create_error(JERRY_ERROR_TYPE,
346                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
347     }
348 
349     ACE_FREE(component->fillStyleValue_);
350     uint16_t fillStyleLength = 0;
351     component->fillStyleValue_ = MallocStringOf(args[ArgsIndex::IDX_0], &fillStyleLength);
352     if (component->fillStyleValue_ == nullptr) {
353         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: fillStyle value error!");
354         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("fillStyle value erro!"));
355     }
356 
357     uint32_t color = 0;
358     uint8_t alpha = OPA_OPAQUE;
359     uint16_t fillStyleId = KeyParser::ParseKeyId(component->fillStyleValue_, fillStyleLength);
360     if (fillStyleId == K_CANVASPATTERN) {
361         component->paint_.SetStyle(Paint::PaintStyle::PATTERN);
362         return UNDEFINED;
363     }
364     if (fillStyleId == K_CANVASGRADIENT) {
365         component->paint_.SetStyle(Paint::PaintStyle::GRADIENT);
366     }
367     if (ParseColor(component->fillStyleValue_, color, alpha)) {
368         component->paint_.SetFillColor(component->GetRGBColor(color));
369         component->paint_.SetOpacity(alpha);
370     }
371     return UNDEFINED;
372 }
373 
FillStyleGetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)374 jerry_value_t CanvasComponent::FillStyleGetter(const jerry_value_t func,
375                                                const jerry_value_t dom,
376                                                const jerry_value_t args[],
377                                                const jerry_length_t argsNum)
378 {
379     (void)func;
380     (void)args;
381     (void)argsNum;
382     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
383     if (component == nullptr) {
384         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
385         return jerry_create_error(JERRY_ERROR_TYPE,
386                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
387     }
388 
389     if (component->fillStyleValue_ == nullptr) {
390         CopyFontFamily(component->fillStyleValue_, DEFAULT_FILLSTYLE);
391     }
392 
393     if (component->fillStyleValue_ != nullptr) {
394         return jerry_create_string(reinterpret_cast<const jerry_char_t *>(component->fillStyleValue_));
395     }
396     return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("fillStyle value error"));
397 }
398 
StrokeStyleSetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)399 jerry_value_t CanvasComponent::StrokeStyleSetter(const jerry_value_t func,
400                                                  const jerry_value_t dom,
401                                                  const jerry_value_t args[],
402                                                  const jerry_length_t argsNum)
403 {
404     (void)func;
405     if (argsNum < ArgsCount::NUM_1) {
406         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: strokeStyle value error!");
407         return jerry_create_error(JERRY_ERROR_TYPE,
408                                   reinterpret_cast<const jerry_char_t *>("the value of strokeStyle is null"));
409     }
410 
411     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
412     if (component == nullptr) {
413         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
414         return jerry_create_error(JERRY_ERROR_TYPE,
415                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
416     }
417 
418     ACE_FREE(component->strokeStyleValue_);
419     uint16_t strokeStyleLength = 0;
420     component->strokeStyleValue_ = MallocStringOf(args[ArgsIndex::IDX_0], &strokeStyleLength);
421     if (component->strokeStyleValue_ == nullptr) {
422         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: strokeStyle value error");
423         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("strokeStyle value error"));
424     }
425 
426     uint32_t color = 0;
427     uint8_t alpha = OPA_OPAQUE;
428     uint16_t strokeStyleId = KeyParser::ParseKeyId(component->strokeStyleValue_, strokeStyleLength);
429     if (strokeStyleId == K_CANVASPATTERN) {
430         component->paint_.SetStyle(Paint::PaintStyle::PATTERN);
431         return UNDEFINED;
432     }
433     if (strokeStyleId == K_CANVASGRADIENT) {
434         component->paint_.SetStyle(Paint::PaintStyle::GRADIENT);
435         return UNDEFINED;
436     }
437     if (ParseColor(component->strokeStyleValue_, color, alpha)) {
438         component->paint_.SetStrokeColor(component->GetRGBColor(color));
439         component->paint_.SetOpacity(alpha);
440     }
441     return UNDEFINED;
442 }
443 
StrokeStyleGetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)444 jerry_value_t CanvasComponent::StrokeStyleGetter(const jerry_value_t func,
445                                                  const jerry_value_t dom,
446                                                  const jerry_value_t args[],
447                                                  const jerry_length_t argsNum)
448 {
449     (void)func;
450     (void)args;
451     (void)argsNum;
452     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
453     if (component == nullptr) {
454         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
455         return jerry_create_error(JERRY_ERROR_TYPE,
456                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
457     }
458 
459     if (component->strokeStyleValue_ == nullptr) {
460         CopyFontFamily(component->strokeStyleValue_, DEFAULT_STROKESTYLE);
461     }
462 
463     if (component->strokeStyleValue_ != nullptr) {
464         return jerry_create_string(reinterpret_cast<const jerry_char_t *>(component->strokeStyleValue_));
465     }
466     return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("strokeStyle value error"));
467 }
468 
LineWidthSetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)469 jerry_value_t CanvasComponent::LineWidthSetter(const jerry_value_t func,
470                                                const jerry_value_t dom,
471                                                const jerry_value_t args[],
472                                                const jerry_length_t argsNum)
473 {
474     (void)func;
475     if (argsNum < ArgsCount::NUM_1) {
476         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineWidth value error!");
477         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("lineWidth value error!"));
478     }
479 
480     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
481     if (component == nullptr) {
482         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
483         return jerry_create_error(JERRY_ERROR_TYPE,
484                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
485     }
486 
487     component->lineWidthValue_ = IntegerOf(args[ArgsIndex::IDX_0]);
488     component->paint_.SetStrokeWidth(component->lineWidthValue_);
489     return UNDEFINED;
490 }
491 
LineWidthGetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)492 jerry_value_t CanvasComponent::LineWidthGetter(const jerry_value_t func,
493                                                const jerry_value_t dom,
494                                                const jerry_value_t args[],
495                                                const jerry_length_t argsNum)
496 {
497     (void)func;
498     (void)args;
499     (void)argsNum;
500     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
501     if (component == nullptr) {
502         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
503         return jerry_create_error(JERRY_ERROR_TYPE,
504                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
505     }
506     return jerry_create_number(component->lineWidthValue_);
507 }
508 
FontSetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)509 jerry_value_t CanvasComponent::FontSetter(const jerry_value_t func,
510                                           const jerry_value_t dom,
511                                           const jerry_value_t args[],
512                                           const jerry_length_t argsNum)
513 {
514     (void)func;
515     if (argsNum < ArgsCount::NUM_1) {
516         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: font value error!");
517         return jerry_create_error(JERRY_ERROR_TYPE,
518                                   reinterpret_cast<const jerry_char_t *>("font value error"));
519     }
520 
521     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
522     if (component == nullptr) {
523         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
524         return jerry_create_error(JERRY_ERROR_TYPE,
525                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
526     }
527 
528     ACE_FREE(component->fontValue_);
529 
530     component->fontValue_ = MallocStringOf(args[ArgsIndex::IDX_0]);
531     if (component->fontValue_ == nullptr) {
532         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: text font value error!");
533         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("text font value error"));
534     }
535 
536     const int8_t maxParamCount = 2;
537     for (int8_t index = 0; index < maxParamCount; index++) {
538         // get font size or font family from fontValue.
539         char *mallocSubFont = nullptr;
540         component->GetSubFont(component->fontValue_, index, mallocSubFont);
541         if (mallocSubFont == nullptr) {
542             HILOG_WARN(HILOG_MODULE_ACE, "canvas_component: get text font size or font family failed");
543             ace_free(component->fontValue_);
544             component->fontValue_ = nullptr;
545             return UNDEFINED;
546         }
547 
548         // if it is a font size
549         if (*mallocSubFont >= '0' && *mallocSubFont <= '9') {
550             component->fontStyle_.fontSize = strtol(mallocSubFont, nullptr, DEC);
551             ace_free(mallocSubFont);
552             mallocSubFont = nullptr;
553         } else { // if it is a font family
554             // free default component->fontStyle_.fontName memory.
555             if (component->fontStyle_.fontName != nullptr) {
556                 ace_free(const_cast<char *>(component->fontStyle_.fontName));
557                 component->fontStyle_.fontName = nullptr;
558             }
559             // set customized font name, the memory of mallocFontFamily need be free in CanvasFillText method.
560             component->fontStyle_.fontName = mallocSubFont;
561         }
562     }
563     return UNDEFINED;
564 }
565 
FontGetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)566 jerry_value_t CanvasComponent::FontGetter(const jerry_value_t func,
567                                           const jerry_value_t dom,
568                                           const jerry_value_t args[],
569                                           const jerry_length_t argsNum)
570 {
571     (void)func;
572     (void)args;
573     (void)argsNum;
574     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
575     if (component == nullptr) {
576         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
577         return jerry_create_error(JERRY_ERROR_TYPE,
578                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
579     }
580 
581     if (component->fontValue_ == nullptr) {
582         uint8_t defaultFontSize = ProductAdapter::GetDefaultFontSize();
583         const char *defaultFontName = ProductAdapter::GetDefaultFontFamilyName();
584         size_t nameLength = (defaultFontName != nullptr) ? strlen(defaultFontName) : 0;
585         const uint8_t maxNameLen = 32;
586         const uint8_t maxFontSizeLen = 16;
587         const uint8_t maxBufferLen = maxNameLen + maxFontSizeLen;
588         if (nameLength > 0 && (nameLength < maxNameLen) && (defaultFontSize < UINT8_MAX)) {
589             char defaultFont[maxBufferLen] = {0};
590             int length = sprintf_s(defaultFont, (maxBufferLen - 1), "%dpx %s", defaultFontSize, defaultFontName);
591             if (length > 0 && length < maxBufferLen) {
592                 CopyFontFamily(component->fontValue_, defaultFont, length);
593             }
594         }
595     }
596 
597     if (component->fontValue_ != nullptr) {
598         return jerry_create_string(reinterpret_cast<const jerry_char_t *>(component->fontValue_));
599     }
600     return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("text font value error"));
601 }
602 
TextAlignSetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)603 jerry_value_t CanvasComponent::TextAlignSetter(const jerry_value_t func,
604                                                const jerry_value_t dom,
605                                                const jerry_value_t args[],
606                                                const jerry_length_t argsNum)
607 {
608     (void)func;
609     if (argsNum < ArgsCount::NUM_1) {
610         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: textAlign value error!");
611         return jerry_create_error(JERRY_ERROR_TYPE,
612                                   reinterpret_cast<const jerry_char_t *>("textAlign value error"));
613     }
614 
615     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
616     if (component == nullptr) {
617         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
618         return jerry_create_error(JERRY_ERROR_TYPE,
619                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
620     }
621 
622     ACE_FREE(component->textAlignValue_);
623 
624     uint16_t textAlignLength = 0;
625     component->textAlignValue_ = MallocStringOf(args[ArgsIndex::IDX_0], &textAlignLength);
626     if (component->textAlignValue_ == nullptr) {
627         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: text align value error!");
628         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("text align value error"));
629     }
630 
631     uint16_t textAlignId = KeyParser::ParseKeyId(component->textAlignValue_, textAlignLength);
632     if (textAlignId == K_LEFT) {
633         component->fontStyle_.align = TEXT_ALIGNMENT_LEFT;
634     } else if (textAlignId == K_RIGHT) {
635         component->fontStyle_.align = TEXT_ALIGNMENT_RIGHT;
636     } else if (textAlignId == K_CENTER) {
637         component->fontStyle_.align = TEXT_ALIGNMENT_CENTER;
638     } else {
639         // do nothing(use default align=TEXT_ALIGNMENT_LEFT)
640     }
641     return UNDEFINED;
642 }
643 
TextAlignGetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)644 jerry_value_t CanvasComponent::TextAlignGetter(const jerry_value_t func,
645                                                const jerry_value_t dom,
646                                                const jerry_value_t args[],
647                                                const jerry_length_t argsNum)
648 {
649     (void)func;
650     (void)args;
651     (void)argsNum;
652     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
653     if (component == nullptr) {
654         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
655         return jerry_create_error(JERRY_ERROR_TYPE,
656                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
657     }
658 
659     if (component->textAlignValue_ == nullptr) {
660         CopyFontFamily(component->textAlignValue_, DEFAULT_TEXTALIGN);
661     }
662 
663     if (component->textAlignValue_ != nullptr) {
664         return jerry_create_string(reinterpret_cast<const jerry_char_t *>(component->textAlignValue_));
665     }
666     return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("textAlign value error"));
667 }
668 
669 #if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG
ShadowOffsetXSetter(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argsNum)670 jerry_value_t CanvasComponent::ShadowOffsetXSetter(const jerry_value_t func,
671                                                    const jerry_value_t context,
672                                                    const jerry_value_t *args,
673                                                    const jerry_length_t argsNum)
674 {
675     (void)func;
676     if (argsNum < ArgsCount::NUM_1) {
677         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: shadowOffsetX value error!");
678         return jerry_create_error(JERRY_ERROR_TYPE,
679                                   reinterpret_cast<const jerry_char_t *>("shadowOffsetX value error"));
680     }
681 
682     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
683     if (component == nullptr) {
684         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
685         return jerry_create_error(JERRY_ERROR_TYPE,
686                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
687     }
688     component->shadowOffsetXValue_ = IntegerOf(args[ArgsIndex::IDX_0]);
689     component->paint_.SetShadowOffsetX(component->shadowOffsetXValue_);
690     return UNDEFINED;
691 }
692 
ShadowOffsetXGetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)693 jerry_value_t CanvasComponent::ShadowOffsetXGetter(const jerry_value_t func,
694                                                    const jerry_value_t dom,
695                                                    const jerry_value_t args[],
696                                                    const jerry_length_t argsNum)
697 {
698     (void)func;
699     (void)args;
700     (void)argsNum;
701     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
702     if (component == nullptr) {
703         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
704         return jerry_create_error(JERRY_ERROR_TYPE,
705                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
706     }
707     return jerry_create_number(component->shadowOffsetXValue_);
708 }
709 
ShadowOffsetYSetter(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argsNum)710 jerry_value_t CanvasComponent::ShadowOffsetYSetter(const jerry_value_t func,
711                                                    const jerry_value_t context,
712                                                    const jerry_value_t *args,
713                                                    const jerry_length_t argsNum)
714 {
715     (void)func;
716     if (argsNum < ArgsCount::NUM_1) {
717         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: shadowOffsetY value error!");
718         return jerry_create_error(JERRY_ERROR_TYPE,
719                                   reinterpret_cast<const jerry_char_t *>("shadowOffsetY value error"));
720     }
721 
722     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
723     if (component == nullptr) {
724         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
725         return jerry_create_error(JERRY_ERROR_TYPE,
726                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
727     }
728     component->shadowOffsetYValue_ = IntegerOf(args[ArgsIndex::IDX_0]);
729     component->paint_.SetShadowOffsetY(component->shadowOffsetYValue_);
730     return UNDEFINED;
731 }
732 
ShadowOffsetYGetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)733 jerry_value_t CanvasComponent::ShadowOffsetYGetter(const jerry_value_t func,
734                                                    const jerry_value_t dom,
735                                                    const jerry_value_t args[],
736                                                    const jerry_length_t argsNum)
737 {
738     (void)func;
739     (void)args;
740     (void)argsNum;
741     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
742     if (component == nullptr) {
743         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
744         return jerry_create_error(JERRY_ERROR_TYPE,
745                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
746     }
747     return jerry_create_number(component->shadowOffsetYValue_);
748 }
749 
ShadowBlurSetter(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argsNum)750 jerry_value_t CanvasComponent::ShadowBlurSetter(const jerry_value_t func,
751                                                 const jerry_value_t context,
752                                                 const jerry_value_t *args,
753                                                 const jerry_length_t argsNum)
754 {
755     (void)func;
756     if (argsNum < ArgsCount::NUM_1) {
757         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: shadowBlur value error!");
758         return jerry_create_error(JERRY_ERROR_TYPE,
759                                   reinterpret_cast<const jerry_char_t *>("shadowBlur value error"));
760     }
761 
762     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
763     if (component == nullptr) {
764         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
765         return jerry_create_error(JERRY_ERROR_TYPE,
766                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
767     }
768     component->shadowBlurValue_ = IntegerOf(args[ArgsIndex::IDX_0]);
769     component->paint_.SetShadowBlur(component->shadowBlurValue_);
770     return UNDEFINED;
771 }
772 
ShadowBlurGetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)773 jerry_value_t CanvasComponent::ShadowBlurGetter(const jerry_value_t func,
774                                                 const jerry_value_t dom,
775                                                 const jerry_value_t args[],
776                                                 const jerry_length_t argsNum)
777 {
778     (void)func;
779     (void)args;
780     (void)argsNum;
781     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
782     if (component == nullptr) {
783         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
784         return jerry_create_error(JERRY_ERROR_TYPE,
785                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
786     }
787     return jerry_create_number(component->shadowBlurValue_);
788 }
789 
ShadowColorSetter(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)790 jerry_value_t CanvasComponent::ShadowColorSetter(const jerry_value_t func,
791                                                  const jerry_value_t context,
792                                                  const jerry_value_t args[],
793                                                  const jerry_length_t argsNum)
794 {
795     (void)func;
796     if (argsNum < ArgsCount::NUM_1) {
797         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: shadowColor value error!");
798         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("shadowColor value error"));
799     }
800 
801     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
802     if (component == nullptr) {
803         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
804         return jerry_create_error(JERRY_ERROR_TYPE,
805                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
806     }
807 
808     ACE_FREE(component->shadowColorValue_);
809 
810     component->shadowColorValue_ = MallocStringOf(args[ArgsIndex::IDX_0]);
811     if (component->shadowColorValue_ == nullptr) {
812         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: shadowColor value error!");
813         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("shadowColor value erro!"));
814     }
815 
816     uint32_t color = 0;
817     uint8_t alpha = OPA_OPAQUE;
818     ParseColor(component->shadowColorValue_, color, alpha);
819     ColorType shadowColor = component->GetRGBColor(color);
820     shadowColor.alpha = alpha;
821     component->paint_.SetShadowColor(shadowColor);
822     return UNDEFINED;
823 }
824 
ShadowColorGetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)825 jerry_value_t CanvasComponent::ShadowColorGetter(const jerry_value_t func,
826                                                  const jerry_value_t dom,
827                                                  const jerry_value_t args[],
828                                                  const jerry_length_t argsNum)
829 {
830     (void)func;
831     (void)args;
832     (void)argsNum;
833     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
834     if (component == nullptr) {
835         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
836         return jerry_create_error(JERRY_ERROR_TYPE,
837                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
838     }
839 
840     if (component->shadowColorValue_ == nullptr) {
841         CopyFontFamily(component->shadowColorValue_, DEFAULT_FILLSTYLE);
842     }
843 
844     if (component->shadowColorValue_ != nullptr) {
845         return jerry_create_string(reinterpret_cast<const jerry_char_t *>(component->shadowColorValue_));
846     }
847     return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("shadowColor value error"));
848 }
849 #endif
850 
851 #if GRAPHIC_ENABLE_LINECAP_FLAG
LineCapSetter(const jerry_value_t func,const jerry_value_t contex,const jerry_value_t args[],const jerry_length_t argsNum)852 jerry_value_t CanvasComponent::LineCapSetter(const jerry_value_t func,
853                                              const jerry_value_t contex,
854                                              const jerry_value_t args[],
855                                              const jerry_length_t argsNum)
856 {
857     (void)func;
858     if (argsNum < ArgsCount::NUM_1) {
859         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineCap value error!");
860         return jerry_create_error(JERRY_ERROR_TYPE,
861                                   reinterpret_cast<const jerry_char_t *>("lineCap value error"));
862     }
863 
864     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(contex));
865     if (component == nullptr) {
866         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
867         return jerry_create_error(JERRY_ERROR_TYPE,
868                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
869     }
870 
871     ACE_FREE(component->lineCapValue_);
872 
873     uint16_t length = 0;
874     component->lineCapValue_ = MallocStringOf(args[ArgsIndex::IDX_0], &length);
875     if (component->lineCapValue_ == nullptr) {
876         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineCap value error!");
877         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("lineCap value error"));
878     }
879 
880     int8_t lineCap = ParseLineCap(component->lineCapValue_);
881     component->paint_.SetLineCap((LineCap)lineCap);
882     return UNDEFINED;
883 }
884 
LineCapGetter(const jerry_value_t func,const jerry_value_t contex,const jerry_value_t args[],const jerry_length_t argsNum)885 jerry_value_t CanvasComponent::LineCapGetter(const jerry_value_t func,
886                                              const jerry_value_t contex,
887                                              const jerry_value_t args[],
888                                              const jerry_length_t argsNum)
889 {
890     (void)func;
891     (void)args;
892     (void)argsNum;
893     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(contex));
894     if (component == nullptr) {
895         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
896         return jerry_create_error(JERRY_ERROR_TYPE,
897                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
898     }
899     if (component->lineCapValue_ == nullptr) {
900         CopyFontFamily(component->lineCapValue_, DEFAULT_LINECAP);
901     }
902     if (component->lineCapValue_ != nullptr) {
903         return jerry_create_string(reinterpret_cast<const jerry_char_t *>(component->lineCapValue_));
904     }
905     return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("lineCap value error"));
906 }
907 #endif
908 
909 #if GRAPHIC_ENABLE_LINEJOIN_FLAG
LineJoinSetter(const jerry_value_t func,const jerry_value_t contex,const jerry_value_t args[],const jerry_length_t argsNum)910 jerry_value_t CanvasComponent::LineJoinSetter(const jerry_value_t func,
911                                               const jerry_value_t contex,
912                                               const jerry_value_t args[],
913                                               const jerry_length_t argsNum)
914 {
915     (void)func;
916     if (argsNum < ArgsCount::NUM_1) {
917         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineCap value error!");
918         return jerry_create_error(JERRY_ERROR_TYPE,
919                                   reinterpret_cast<const jerry_char_t *>("lineCap value error"));
920     }
921 
922     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(contex));
923     if (component == nullptr) {
924         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
925         return jerry_create_error(JERRY_ERROR_TYPE,
926                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
927     }
928 
929     ACE_FREE(component->lineJoinValue_);
930 
931     uint16_t length = 0;
932     component->lineJoinValue_ = MallocStringOf(args[ArgsIndex::IDX_0], &length);
933     if (component->lineJoinValue_ == nullptr) {
934         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineJoin value error!");
935         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("lineJoin value error"));
936     }
937 
938     uint8_t lineJoin = ParseLineJoin(component->lineJoinValue_);
939     component->paint_.SetLineJoin((LineJoin)lineJoin);
940     return UNDEFINED;
941 }
942 
LineJoinGetter(const jerry_value_t func,const jerry_value_t contex,const jerry_value_t args[],const jerry_length_t argsNum)943 jerry_value_t CanvasComponent::LineJoinGetter(const jerry_value_t func,
944                                               const jerry_value_t contex,
945                                               const jerry_value_t args[],
946                                               const jerry_length_t argsNum)
947 {
948     (void)func;
949     (void)args;
950     (void)argsNum;
951     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(contex));
952     if (component == nullptr) {
953         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
954         return jerry_create_error(JERRY_ERROR_TYPE,
955                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
956     }
957     if (component->lineJoinValue_ == nullptr) {
958         CopyFontFamily(component->lineJoinValue_, DEFAULT_LINEJOIN);
959     }
960     if (component->lineJoinValue_ != nullptr) {
961         return jerry_create_string(reinterpret_cast<const jerry_char_t *>(component->lineJoinValue_));
962     }
963     return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("lineJoin value error"));
964 }
965 
MiterLimitSetter(const jerry_value_t func,const jerry_value_t contex,const jerry_value_t args[],const jerry_length_t argsNum)966 jerry_value_t CanvasComponent::MiterLimitSetter(const jerry_value_t func,
967                                                 const jerry_value_t contex,
968                                                 const jerry_value_t args[],
969                                                 const jerry_length_t argsNum)
970 {
971     (void)func;
972     if (argsNum < ArgsCount::NUM_1) {
973         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: miterLimit value error!");
974         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("lineWidth value error!"));
975     }
976 
977     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(contex));
978     if (component == nullptr) {
979         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
980         return jerry_create_error(JERRY_ERROR_TYPE,
981                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
982     }
983     component->miterLimitValue_ = FloatOf(args[ArgsIndex::IDX_0]);
984     component->paint_.SetMiterLimit(component->miterLimitValue_);
985     return UNDEFINED;
986 }
987 
MiterLimitGetter(const jerry_value_t func,const jerry_value_t contex,const jerry_value_t args[],const jerry_length_t argsNum)988 jerry_value_t CanvasComponent::MiterLimitGetter(const jerry_value_t func,
989                                                 const jerry_value_t contex,
990                                                 const jerry_value_t args[],
991                                                 const jerry_length_t argsNum)
992 {
993     (void)func;
994     (void)args;
995     (void)argsNum;
996     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(contex));
997     if (component == nullptr) {
998         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
999         return jerry_create_error(JERRY_ERROR_TYPE,
1000                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1001     }
1002     return jerry_create_number(component->miterLimitValue_);
1003 }
1004 #endif
1005 
1006 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
LineDashOffsetSetter(const jerry_value_t func,const jerry_value_t contex,const jerry_value_t args[],const jerry_length_t argsNum)1007 jerry_value_t CanvasComponent::LineDashOffsetSetter(const jerry_value_t func,
1008                                                     const jerry_value_t contex,
1009                                                     const jerry_value_t args[],
1010                                                     const jerry_length_t argsNum)
1011 {
1012     (void)func;
1013     if (argsNum < ArgsCount::NUM_1) {
1014         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineWidth value error!");
1015         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("lineWidth value error!"));
1016     }
1017 
1018     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(contex));
1019     if (component == nullptr) {
1020         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1021         return jerry_create_error(JERRY_ERROR_TYPE,
1022                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1023     }
1024     component->lineDashOffsetValue_ = FloatOf(args[ArgsIndex::IDX_0]);
1025     component->paint_.SetLineDashOffset(component->lineDashOffsetValue_);
1026     return UNDEFINED;
1027 }
1028 
LineDashOffsetGetter(const jerry_value_t func,const jerry_value_t contex,const jerry_value_t args[],const jerry_length_t argsNum)1029 jerry_value_t CanvasComponent::LineDashOffsetGetter(const jerry_value_t func,
1030                                                     const jerry_value_t contex,
1031                                                     const jerry_value_t args[],
1032                                                     const jerry_length_t argsNum)
1033 {
1034     (void)func;
1035     (void)args;
1036     (void)argsNum;
1037     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(contex));
1038     if (component == nullptr) {
1039         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1040         return jerry_create_error(JERRY_ERROR_TYPE,
1041                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1042     }
1043     return jerry_create_number(component->lineDashOffsetValue_);
1044 }
1045 #endif
1046 
GlobalAlphaSetter(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)1047 jerry_value_t CanvasComponent::GlobalAlphaSetter(const jerry_value_t func,
1048                                                  const jerry_value_t context,
1049                                                  const jerry_value_t args[],
1050                                                  const jerry_length_t argsNum)
1051 {
1052     (void)func;
1053     if (argsNum < ArgsCount::NUM_1) {
1054         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineWidth value error!");
1055         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("lineWidth value error!"));
1056     }
1057 
1058     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
1059     if (component == nullptr) {
1060         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1061         return jerry_create_error(JERRY_ERROR_TYPE,
1062                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1063     }
1064 
1065     float globalAlpha = FloatOf(args[ArgsIndex::IDX_0]);
1066     component->paint_.SetGlobalAlpha(globalAlpha);
1067     return UNDEFINED;
1068 }
1069 
GlobalAlphaGetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1070 jerry_value_t CanvasComponent::GlobalAlphaGetter(const jerry_value_t func,
1071                                                  const jerry_value_t dom,
1072                                                  const jerry_value_t args[],
1073                                                  const jerry_length_t argsNum)
1074 {
1075     (void)func;
1076     (void)args;
1077     (void)argsNum;
1078     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1079     if (component == nullptr) {
1080         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1081         return jerry_create_error(JERRY_ERROR_TYPE,
1082                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1083     }
1084     return jerry_create_number(component->paint_.GetGlobalAlpha());
1085 }
1086 
GlobalCompositeOperationSetter(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)1087 jerry_value_t CanvasComponent::GlobalCompositeOperationSetter(const jerry_value_t func,
1088                                                               const jerry_value_t context,
1089                                                               const jerry_value_t args[],
1090                                                               const jerry_length_t argsNum)
1091 {
1092     (void)func;
1093     if (argsNum < ArgsCount::NUM_1) {
1094         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineWidth value error!");
1095         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("lineWidth value error!"));
1096     }
1097 
1098     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
1099     if (component == nullptr) {
1100         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1101         return jerry_create_error(JERRY_ERROR_TYPE,
1102                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1103     }
1104 
1105     char *globalCompositeOperation = MallocStringOf(args[ArgsIndex::IDX_0]);
1106     if (globalCompositeOperation == nullptr) {
1107         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get text value failed");
1108         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("get text value failed"));
1109     }
1110 
1111     if (strcmp(globalCompositeOperation, "source-over") == 0) {
1112         component->paint_.SetGlobalCompositeOperation(OHOS::SOURCE_OVER);
1113     } else if (strcmp(globalCompositeOperation, "source-atop") == 0) {
1114         component->paint_.SetGlobalCompositeOperation(OHOS::SOURCE_ATOP);
1115     } else if (strcmp(globalCompositeOperation, "source-in") == 0) {
1116         component->paint_.SetGlobalCompositeOperation(OHOS::SOURCE_IN);
1117     } else if (strcmp(globalCompositeOperation, "source-out") == 0) {
1118         component->paint_.SetGlobalCompositeOperation(OHOS::SOURCE_OUT);
1119     } else if (strcmp(globalCompositeOperation, "destination-over") == 0) {
1120         component->paint_.SetGlobalCompositeOperation(OHOS::DESTINATION_OVER);
1121     } else if (strcmp(globalCompositeOperation, "destination-atop") == 0) {
1122         component->paint_.SetGlobalCompositeOperation(OHOS::DESTINATION_ATOP);
1123     } else if (strcmp(globalCompositeOperation, "destination-in") == 0) {
1124         component->paint_.SetGlobalCompositeOperation(OHOS::DESTINATION_IN);
1125     } else if (strcmp(globalCompositeOperation, "destination-out") == 0) {
1126         component->paint_.SetGlobalCompositeOperation(OHOS::DESTINATION_OUT);
1127     } else if (strcmp(globalCompositeOperation, "lighter") == 0) {
1128         component->paint_.SetGlobalCompositeOperation(OHOS::LIGHTER);
1129     } else if (strcmp(globalCompositeOperation, "copy") == 0) {
1130         component->paint_.SetGlobalCompositeOperation(OHOS::COPY);
1131     } else if (strcmp(globalCompositeOperation, "xor") == 0) {
1132         component->paint_.SetGlobalCompositeOperation(OHOS::XOR);
1133     }
1134 
1135     ACE_FREE(globalCompositeOperation);
1136     return UNDEFINED;
1137 }
1138 
GlobalCompositeOperationGetter(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1139 jerry_value_t CanvasComponent::GlobalCompositeOperationGetter(const jerry_value_t func,
1140                                                               const jerry_value_t dom,
1141                                                               const jerry_value_t args[],
1142                                                               const jerry_length_t argsNum)
1143 {
1144     (void)func;
1145     (void)args;
1146     (void)argsNum;
1147     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1148     if (component == nullptr) {
1149         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1150         return jerry_create_error(JERRY_ERROR_TYPE,
1151                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1152     }
1153 
1154     int8_t globalCompositeOperation = component->paint_.GetGlobalCompositeOperation();
1155     return jerry_create_number(globalCompositeOperation);
1156 }
1157 
FillRect(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1158 jerry_value_t CanvasComponent::FillRect(const jerry_value_t func,
1159                                         const jerry_value_t dom,
1160                                         const jerry_value_t args[],
1161                                         const jerry_length_t argsNum)
1162 {
1163     (void)func;
1164     if (argsNum < ArgsCount::NUM_4) {
1165         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of fillRect method parameter error!");
1166         return jerry_create_error(JERRY_ERROR_TYPE,
1167                                   reinterpret_cast<const jerry_char_t *>("fillRect method parameter error"));
1168     }
1169 
1170     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1171     if (component == nullptr) {
1172         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1173         return jerry_create_error(JERRY_ERROR_TYPE,
1174                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1175     }
1176 
1177     int16_t startX = IntegerOf(args[ArgsIndex::IDX_0]);
1178     int16_t startY = IntegerOf(args[ArgsIndex::IDX_1]);
1179     int16_t width = IntegerOf(args[ArgsIndex::IDX_2]);
1180     int16_t height = IntegerOf(args[ArgsIndex::IDX_3]);
1181 
1182     // FILL_STYLE: fill rect support fill shapses only
1183     component->paint_.SetStyle(component->paint_.FILL_STYLE);
1184 
1185     Point startPoint;
1186     startPoint.x = startX;
1187     startPoint.y = startY;
1188     component->canvas_.DrawRect(startPoint, height, width, component->paint_);
1189     return UNDEFINED;
1190 }
1191 
StrokeRect(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1192 jerry_value_t CanvasComponent::StrokeRect(const jerry_value_t func,
1193                                           const jerry_value_t dom,
1194                                           const jerry_value_t args[],
1195                                           const jerry_length_t argsNum)
1196 {
1197     (void)func;
1198     if (argsNum < ArgsCount::NUM_4) {
1199         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of strokeRect method parameter error!");
1200         return jerry_create_error(JERRY_ERROR_TYPE,
1201                                   reinterpret_cast<const jerry_char_t *>("strokeRect method parameter error"));
1202     }
1203 
1204     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1205     if (component == nullptr) {
1206         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1207         return jerry_create_error(JERRY_ERROR_TYPE,
1208                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1209     }
1210 
1211 #if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
1212     int16_t startX = IntegerOf(args[ArgsIndex::IDX_0]);
1213     int16_t startY = IntegerOf(args[ArgsIndex::IDX_1]);
1214     int16_t width = IntegerOf(args[ArgsIndex::IDX_2]);
1215     int16_t height = IntegerOf(args[ArgsIndex::IDX_3]);
1216 
1217     // STROKE_STYLE: stroke rect support draw borders only
1218     component->paint_.SetStyle(component->paint_.STROKE_STYLE);
1219 
1220     Point startPoint;
1221     startPoint.x = startX;
1222     startPoint.y = startY;
1223     component->canvas_.StrokeRect(startPoint, height, width, component->paint_);
1224 #endif
1225     return UNDEFINED;
1226 }
1227 
FillText(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1228 jerry_value_t CanvasComponent::FillText(const jerry_value_t func,
1229                                         const jerry_value_t dom,
1230                                         const jerry_value_t args[],
1231                                         const jerry_length_t argsNum)
1232 {
1233     (void)func;
1234     if (argsNum < ArgsCount::NUM_3) {
1235         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of fillText method parameter error!");
1236         return jerry_create_error(JERRY_ERROR_TYPE,
1237                                   reinterpret_cast<const jerry_char_t *>("fillText method parameter error"));
1238     }
1239 
1240     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1241     if (component == nullptr) {
1242         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1243         return jerry_create_error(JERRY_ERROR_TYPE,
1244                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1245     }
1246 
1247     char *textValue = MallocStringOf(args[ArgsIndex::IDX_0]);
1248     if (textValue == nullptr) {
1249         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get text value failed");
1250         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("get text value failed"));
1251     }
1252 
1253     int16_t startX = IntegerOf(args[ArgsIndex::IDX_1]);
1254     int16_t startY = IntegerOf(args[ArgsIndex::IDX_2]);
1255     int16_t maxWidth = component->canvas_.GetWidth();
1256 
1257     // FILL_STYLE: fillText support fill shapses only
1258     component->paint_.SetStyle(component->paint_.FILL_STYLE);
1259 
1260     Point startPoint;
1261     const int8_t half = 2;
1262     startPoint.x = startX;
1263     startPoint.y = startY;
1264 
1265     // if textAlign=left, not need transfer startX, if textAlign=right or center, need transfer startX
1266     if (component->fontStyle_.align == TEXT_ALIGNMENT_RIGHT) {
1267         startPoint.x = startX - maxWidth;
1268     } else if (component->fontStyle_.align == TEXT_ALIGNMENT_CENTER) {
1269         startPoint.x = (startX + startX - maxWidth) / half;
1270     } else {
1271         startPoint.x = startX;
1272     }
1273 
1274     component->canvas_.DrawLabel(startPoint, textValue, maxWidth, component->fontStyle_, component->paint_);
1275 
1276     ACE_FREE(textValue);
1277     return UNDEFINED;
1278 }
1279 
1280 #if GRAPHIC_ENABLE_DRAW_TEXT_FLAG
StrokeText(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)1281 jerry_value_t CanvasComponent::StrokeText(const jerry_value_t func,
1282                                           const jerry_value_t context,
1283                                           const jerry_value_t args[],
1284                                           const jerry_length_t argsNum)
1285 {
1286     (void)func;
1287     if (argsNum < ArgsCount::NUM_5) {
1288         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of strokeText method parameter error!");
1289         return jerry_create_error(JERRY_ERROR_TYPE,
1290                                   reinterpret_cast<const jerry_char_t *>("strokeText method parameter error"));
1291     }
1292 
1293     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
1294     if (component == nullptr) {
1295         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1296         return jerry_create_error(JERRY_ERROR_TYPE,
1297                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1298     }
1299 
1300     char *textValue = MallocStringOf(args[ArgsIndex::IDX_0]);
1301     if (textValue == nullptr) {
1302         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get text value failed");
1303         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("get text value failed"));
1304     }
1305 
1306     int16_t startX = IntegerOf(args[ArgsIndex::IDX_1]);
1307     int16_t startY = IntegerOf(args[ArgsIndex::IDX_2]);
1308     int16_t maxWidth = component->canvas_.GetWidth();
1309 
1310     // FILL_STYLE: strokeText support fill shapses only
1311     component->paint_.SetStyle(component->paint_.FILL_STYLE);
1312 
1313     Point startPoint;
1314     const int8_t half = 2;
1315     startPoint.x = startX;
1316     startPoint.y = startY;
1317 
1318     // if textAlign=left, not need transfer startX, if textAlign=right or center, need transfer startX
1319     if (component->fontStyle_.align == TEXT_ALIGNMENT_RIGHT) {
1320         startPoint.x = startX - maxWidth;
1321     } else if (component->fontStyle_.align == TEXT_ALIGNMENT_CENTER) {
1322         startPoint.x = (startX + startX - maxWidth) / half;
1323     } else {
1324         startPoint.x = startX;
1325     }
1326 
1327     component->canvas_.StrokeText(textValue, startPoint, component->fontStyle_, component->paint_);
1328     return UNDEFINED;
1329 }
1330 #endif
1331 
Rotate(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1332 jerry_value_t CanvasComponent::Rotate(const jerry_value_t func,
1333                                       const jerry_value_t dom,
1334                                       const jerry_value_t args[],
1335                                       const jerry_length_t argsNum)
1336 {
1337     (void)func;
1338     if (argsNum < ArgsCount::NUM_1) {
1339         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of rotate method parameter error!");
1340         return jerry_create_error(JERRY_ERROR_TYPE,
1341                                   reinterpret_cast<const jerry_char_t *>("rotate method parameter error"));
1342     }
1343 
1344     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1345     if (component == nullptr) {
1346         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1347         return jerry_create_error(JERRY_ERROR_TYPE,
1348                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1349     }
1350 
1351     float angle = FloatOf(args[ArgsIndex::IDX_0]);
1352     component->paint_.Rotate(angle);
1353     return UNDEFINED;
1354 }
1355 
Scale(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1356 jerry_value_t CanvasComponent::Scale(const jerry_value_t func,
1357                                      const jerry_value_t dom,
1358                                      const jerry_value_t args[],
1359                                      const jerry_length_t argsNum)
1360 {
1361     (void)func;
1362     if (argsNum < ArgsCount::NUM_2) {
1363         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of scale method parameter error!");
1364         return jerry_create_error(JERRY_ERROR_TYPE,
1365                                   reinterpret_cast<const jerry_char_t *>("scale method parameter error"));
1366     }
1367 
1368     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1369     if (component == nullptr) {
1370         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1371         return jerry_create_error(JERRY_ERROR_TYPE,
1372                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1373     }
1374 
1375     float scaleX  = FloatOf(args[ArgsIndex::IDX_0]);
1376     float scaleY  = FloatOf(args[ArgsIndex::IDX_1]);
1377     component->paint_.Scale(scaleX, scaleY);
1378     return UNDEFINED;
1379 }
1380 
MeasureText(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)1381 jerry_value_t CanvasComponent::MeasureText(const jerry_value_t func,
1382                                            const jerry_value_t context,
1383                                            const jerry_value_t args[],
1384                                            const jerry_length_t argsNum)
1385 {
1386     (void)func;
1387     if (argsNum < ArgsCount::NUM_1) {
1388         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of measureText method parameter error!");
1389         return jerry_create_error(JERRY_ERROR_TYPE,
1390                                   reinterpret_cast<const jerry_char_t *>("measureText method parameter error"));
1391     }
1392 
1393     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
1394     if (component == nullptr) {
1395         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1396         return jerry_create_error(JERRY_ERROR_TYPE,
1397                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1398     }
1399 
1400     char *textValue = MallocStringOf(args[ArgsIndex::IDX_0]);
1401     if (textValue == nullptr) {
1402         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get text value failed");
1403         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("get text value failed"));
1404     }
1405 
1406     if (IS_UNDEFINED(component->measureTextObject_)) {
1407         component->measureTextObject_ = jerry_create_object();
1408     }
1409     if (IS_UNDEFINED(component->measureTextWidthString_)) {
1410         component->measureTextWidthString_ = jerry_create_string(reinterpret_cast<const jerry_char_t *>("width"));
1411     }
1412     Point ponit = component->canvas_.MeasureText(textValue, component->fontStyle_);
1413     jerry_set_property(component->measureTextObject_, component->measureTextWidthString_, ponit.x);
1414 
1415     ACE_FREE(textValue);
1416     return component->measureTextObject_;
1417 }
1418 
Translate(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1419 jerry_value_t CanvasComponent::Translate(const jerry_value_t func,
1420                                          const jerry_value_t dom,
1421                                          const jerry_value_t args[],
1422                                          const jerry_length_t argsNum)
1423 {
1424     (void)func;
1425     if (argsNum < ArgsCount::NUM_2) {
1426         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of translate method parameter error!");
1427         return jerry_create_error(JERRY_ERROR_TYPE,
1428                                   reinterpret_cast<const jerry_char_t *>("translate method parameter error"));
1429     }
1430 
1431     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1432     if (component == nullptr) {
1433         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1434         return jerry_create_error(JERRY_ERROR_TYPE,
1435                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1436     }
1437 
1438     int16_t positionX = IntegerOf(args[ArgsIndex::IDX_0]);
1439     int16_t positionY = IntegerOf(args[ArgsIndex::IDX_1]);
1440 
1441     component->paint_.Translate(positionX, positionY);
1442     return UNDEFINED;
1443 }
1444 
Transform(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1445 jerry_value_t CanvasComponent::Transform(const jerry_value_t func,
1446                                          const jerry_value_t dom,
1447                                          const jerry_value_t args[],
1448                                          const jerry_length_t argsNum)
1449 {
1450     (void)func;
1451     if (argsNum < ArgsCount::NUM_6) {
1452         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of rotate method parameter error!");
1453         return jerry_create_error(JERRY_ERROR_TYPE,
1454                                   reinterpret_cast<const jerry_char_t *>("rotate method parameter error"));
1455     }
1456 
1457     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1458     if (component == nullptr) {
1459         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1460         return jerry_create_error(JERRY_ERROR_TYPE,
1461                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1462     }
1463 
1464     float scaleX  = FloatOf(args[ArgsIndex::IDX_0]);
1465     float shearX  = FloatOf(args[ArgsIndex::IDX_1]);
1466     float shearY  = FloatOf(args[ArgsIndex::IDX_2]);
1467     float scaleY  = FloatOf(args[ArgsIndex::IDX_3]);
1468     int16_t translateX = IntegerOf(args[ArgsIndex::IDX_4]);
1469     int16_t translateY = IntegerOf(args[ArgsIndex::IDX_5]);
1470     component->paint_.Transform(scaleX, shearX, shearY, scaleY, translateX, translateY);
1471     return UNDEFINED;
1472 }
1473 
1474 
SetTransform(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1475 jerry_value_t CanvasComponent::SetTransform(const jerry_value_t func,
1476                                             const jerry_value_t dom,
1477                                             const jerry_value_t args[],
1478                                             const jerry_length_t argsNum)
1479 {
1480     (void)func;
1481     if (argsNum < ArgsCount::NUM_6) {
1482         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of rotate method parameter error!");
1483         return jerry_create_error(JERRY_ERROR_TYPE,
1484                                   reinterpret_cast<const jerry_char_t *>("rotate method parameter error"));
1485     }
1486 
1487     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1488     if (component == nullptr) {
1489         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1490         return jerry_create_error(JERRY_ERROR_TYPE,
1491                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1492     }
1493 
1494     float scaleX  = FloatOf(args[ArgsIndex::IDX_0]);
1495     float shearX  = FloatOf(args[ArgsIndex::IDX_1]);
1496     float shearY  = FloatOf(args[ArgsIndex::IDX_2]);
1497     float scaleY  = FloatOf(args[ArgsIndex::IDX_3]);
1498     int16_t translateX = IntegerOf(args[ArgsIndex::IDX_4]);
1499     int16_t translateY = IntegerOf(args[ArgsIndex::IDX_5]);
1500     component->paint_.SetTransform(scaleX, shearX, shearY, scaleY, translateX, translateY);
1501     return UNDEFINED;
1502 }
1503 
Save(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1504 jerry_value_t CanvasComponent::Save(const jerry_value_t func,
1505                                     const jerry_value_t dom,
1506                                     const jerry_value_t args[],
1507                                     const jerry_length_t argsNum)
1508 {
1509     (void)func;
1510 
1511     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1512     if (component == nullptr) {
1513         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1514         return jerry_create_error(JERRY_ERROR_TYPE,
1515                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1516     }
1517 
1518     component->canvas_.Save(component->paint_);
1519 
1520     return UNDEFINED;
1521 }
1522 
Restore(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1523 jerry_value_t CanvasComponent::Restore(const jerry_value_t func,
1524                                        const jerry_value_t dom,
1525                                        const jerry_value_t args[],
1526                                        const jerry_length_t argsNum)
1527 {
1528     (void)func;
1529 
1530     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1531     if (component == nullptr) {
1532         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1533         return jerry_create_error(JERRY_ERROR_TYPE,
1534                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1535     }
1536 
1537     component->paint_ = component->canvas_.Restore();
1538     return UNDEFINED;
1539 }
1540 
1541 
DrawCircle(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1542 jerry_value_t CanvasComponent::DrawCircle(const jerry_value_t func,
1543                                           const jerry_value_t dom,
1544                                           const jerry_value_t args[],
1545                                           const jerry_length_t argsNum)
1546 {
1547     (void)func;
1548     if (argsNum < ArgsCount::NUM_3) {
1549         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of arc method parameter error!");
1550         return jerry_create_error(JERRY_ERROR_TYPE,
1551                                   reinterpret_cast<const jerry_char_t *>("arc method parameter error"));
1552     }
1553 
1554     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1555     if (component == nullptr) {
1556         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1557         return jerry_create_error(JERRY_ERROR_TYPE,
1558                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1559     }
1560 
1561     int16_t centerX = IntegerOf(args[ArgsIndex::IDX_0]);
1562     int16_t centerY = IntegerOf(args[ArgsIndex::IDX_1]);
1563     int16_t radius = IntegerOf(args[ArgsIndex::IDX_2]);
1564 
1565     Point centerPoint;
1566     centerPoint.x = centerX;
1567     centerPoint.y = centerY;
1568 
1569     component->canvas_.DrawCircle(centerPoint, radius, component->paint_);
1570     return UNDEFINED;
1571 }
1572 
BeginPath(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1573 jerry_value_t CanvasComponent::BeginPath(const jerry_value_t func,
1574                                          const jerry_value_t dom,
1575                                          const jerry_value_t args[],
1576                                          const jerry_length_t argsNum)
1577 {
1578     (void)func;
1579     (void)args;
1580     (void)argsNum;
1581     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1582     if (component == nullptr) {
1583         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1584         return jerry_create_error(JERRY_ERROR_TYPE,
1585                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1586     }
1587 
1588     component->canvas_.BeginPath();
1589     return UNDEFINED;
1590 }
1591 
MoveTo(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1592 jerry_value_t CanvasComponent::MoveTo(const jerry_value_t func,
1593                                       const jerry_value_t dom,
1594                                       const jerry_value_t args[],
1595                                       const jerry_length_t argsNum)
1596 {
1597     (void)func;
1598     if (argsNum < ArgsCount::NUM_2) {
1599         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of moveTo method parameter error!");
1600         return jerry_create_error(JERRY_ERROR_TYPE,
1601                                   reinterpret_cast<const jerry_char_t *>("moveTo method parameter error"));
1602     }
1603 
1604     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1605     if (component == nullptr) {
1606         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1607         return jerry_create_error(JERRY_ERROR_TYPE,
1608                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1609     }
1610 
1611     int16_t x = IntegerOf(args[ArgsIndex::IDX_0]);
1612     int16_t y = IntegerOf(args[ArgsIndex::IDX_1]);
1613 
1614     Point point;
1615     point.x = x;
1616     point.y = y;
1617     component->canvas_.MoveTo(point);
1618     return UNDEFINED;
1619 }
1620 
LineTo(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1621 jerry_value_t CanvasComponent::LineTo(const jerry_value_t func,
1622                                       const jerry_value_t dom,
1623                                       const jerry_value_t args[],
1624                                       const jerry_length_t argsNum)
1625 {
1626     (void)func;
1627     if (argsNum < ArgsCount::NUM_2) {
1628         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of lineTo method parameter error!");
1629         return jerry_create_error(JERRY_ERROR_TYPE,
1630                                   reinterpret_cast<const jerry_char_t *>("lineTo method parameter error"));
1631     }
1632 
1633     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1634     if (component == nullptr) {
1635         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1636         return jerry_create_error(JERRY_ERROR_TYPE,
1637                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1638     }
1639 
1640     int16_t x = IntegerOf(args[ArgsIndex::IDX_0]);
1641     int16_t y = IntegerOf(args[ArgsIndex::IDX_1]);
1642 
1643     Point point;
1644     point.x = x;
1645     point.y = y;
1646     component->canvas_.LineTo(point);
1647     return UNDEFINED;
1648 }
1649 
Rect(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1650 jerry_value_t CanvasComponent::Rect(const jerry_value_t func,
1651                                     const jerry_value_t dom,
1652                                     const jerry_value_t args[],
1653                                     const jerry_length_t argsNum)
1654 {
1655     (void)func;
1656     if (argsNum < ArgsCount::NUM_4) {
1657         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of rect method parameter error!");
1658         return jerry_create_error(JERRY_ERROR_TYPE,
1659                                   reinterpret_cast<const jerry_char_t *>("rect method parameter error"));
1660     }
1661 
1662     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1663     if (component == nullptr) {
1664         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1665         return jerry_create_error(JERRY_ERROR_TYPE,
1666                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1667     }
1668 
1669     int16_t x = IntegerOf(args[ArgsIndex::IDX_0]);
1670     int16_t y = IntegerOf(args[ArgsIndex::IDX_1]);
1671     int16_t width = IntegerOf(args[ArgsIndex::IDX_2]);
1672     int16_t height = IntegerOf(args[ArgsIndex::IDX_3]);
1673 
1674     Point point;
1675     point.x = x;
1676     point.y = y;
1677     component->canvas_.AddRect(point, height, width);
1678     return UNDEFINED;
1679 }
1680 
CleanRect(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)1681 jerry_value_t CanvasComponent::CleanRect(const jerry_value_t func,
1682                                          const jerry_value_t context,
1683                                          const jerry_value_t args[],
1684                                          const jerry_length_t argsNum)
1685 {
1686     (void)func;
1687     if (argsNum < ArgsCount::NUM_4) {
1688         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of rect method parameter error!");
1689         return jerry_create_error(JERRY_ERROR_TYPE,
1690                                   reinterpret_cast<const jerry_char_t *>("rect method parameter error"));
1691     }
1692 
1693     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
1694     if (component == nullptr) {
1695         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1696         return jerry_create_error(JERRY_ERROR_TYPE,
1697                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1698     }
1699 
1700 #if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
1701     int16_t x = IntegerOf(args[ArgsIndex::IDX_0]);
1702     int16_t y = IntegerOf(args[ArgsIndex::IDX_1]);
1703     int16_t width = IntegerOf(args[ArgsIndex::IDX_2]);
1704     int16_t height = IntegerOf(args[ArgsIndex::IDX_3]);
1705 
1706     Point point;
1707     point.x = x;
1708     point.y = y;
1709     component->canvas_.ClearRect(point, height, width);
1710 #endif
1711     return UNDEFINED;
1712 }
Arc(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1713 jerry_value_t CanvasComponent::Arc(const jerry_value_t func,
1714                                    const jerry_value_t dom,
1715                                    const jerry_value_t args[],
1716                                    const jerry_length_t argsNum)
1717 {
1718     (void)func;
1719     if (argsNum != ArgsCount::NUM_5 && argsNum != ArgsCount::NUM_6) {
1720         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of arc method parameter error!");
1721         return jerry_create_error(JERRY_ERROR_TYPE,
1722                                   reinterpret_cast<const jerry_char_t *>("arc method parameter error"));
1723     }
1724 
1725     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1726     if (component == nullptr) {
1727         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1728         return jerry_create_error(JERRY_ERROR_TYPE,
1729                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1730     }
1731 
1732     int16_t centerX = IntegerOf(args[ArgsIndex::IDX_0]);
1733     int16_t centerY = IntegerOf(args[ArgsIndex::IDX_1]);
1734     int16_t radius = IntegerOf(args[ArgsIndex::IDX_2]);
1735     double sAngle = jerry_get_number_value(args[ArgsIndex::IDX_3]);
1736     double eAngle = jerry_get_number_value(args[ArgsIndex::IDX_4]);
1737 
1738     bool counterClockwise = false;
1739     if (argsNum == ArgsCount::NUM_6) {
1740         if (jerry_value_is_boolean(args[ArgsIndex::IDX_5])) {
1741             counterClockwise = BoolOf(args[ArgsIndex::IDX_5]);
1742         }
1743     }
1744 
1745     Point centerPoint;
1746     centerPoint.x = centerX;
1747     centerPoint.y = centerY;
1748     int16_t startAngle = 0;
1749     int16_t endAngle = 0;
1750 
1751     bool result = component->FormatArcAngle(sAngle, eAngle, startAngle, endAngle, counterClockwise);
1752     if (!result) {
1753         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("arc angle error"));
1754     }
1755 
1756     // 0 degree is at the 3 o'clock position of the arc's circle at JS layer,
1757     // but which is at the 12 o'clock position at Native layer.
1758     constexpr int16_t DEGREES_OFFSET = 90;
1759     constexpr int16_t DEGREES_360 = 360;
1760     if (((startAngle + DEGREES_OFFSET) <= INT16_MAX) && ((endAngle + DEGREES_OFFSET) <= INT16_MAX)) {
1761         startAngle = startAngle + DEGREES_OFFSET;
1762         endAngle = endAngle + DEGREES_OFFSET;
1763     } else {
1764         startAngle = startAngle + DEGREES_OFFSET - DEGREES_360;
1765         endAngle = endAngle + DEGREES_OFFSET - DEGREES_360;
1766     }
1767 
1768     component->canvas_.ArcTo(centerPoint, radius, startAngle, endAngle);
1769     return UNDEFINED;
1770 }
1771 
ClosePath(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1772 jerry_value_t CanvasComponent::ClosePath(const jerry_value_t func,
1773                                          const jerry_value_t dom,
1774                                          const jerry_value_t args[],
1775                                          const jerry_length_t argsNum)
1776 {
1777     (void)func;
1778     (void)args;
1779     (void)argsNum;
1780     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1781     if (component == nullptr) {
1782         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1783         return jerry_create_error(JERRY_ERROR_TYPE,
1784                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1785     }
1786 
1787     component->canvas_.ClosePath();
1788     return UNDEFINED;
1789 }
1790 
Stroke(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1791 jerry_value_t CanvasComponent::Stroke(const jerry_value_t func,
1792                                       const jerry_value_t dom,
1793                                       const jerry_value_t args[],
1794                                       const jerry_length_t argsNum)
1795 {
1796     (void)func;
1797     (void)args;
1798     (void)argsNum;
1799     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1800     if (component == nullptr) {
1801         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1802         return jerry_create_error(JERRY_ERROR_TYPE,
1803                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1804     }
1805     component->canvas_.DrawPath(component->paint_);
1806     return UNDEFINED;
1807 }
1808 
1809 #if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
CreateLInearGradient(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)1810 jerry_value_t CanvasComponent::CreateLInearGradient(const jerry_value_t func,
1811                                                     const jerry_value_t context,
1812                                                     const jerry_value_t args[],
1813                                                     const jerry_length_t argsNum)
1814 {
1815     (void)func;
1816     if (argsNum < ArgsCount::NUM_4) {
1817         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of createLinearGradient method parameter error!");
1818         return jerry_create_error(JERRY_ERROR_TYPE,
1819                                   reinterpret_cast<const jerry_char_t *>("createLinearGradient method param error"));
1820     }
1821 
1822     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
1823     if (component == nullptr) {
1824         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1825         return jerry_create_error(JERRY_ERROR_TYPE,
1826                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1827     }
1828 
1829     float startX = FloatOf(args[ArgsIndex::IDX_0]);
1830     float startY = FloatOf(args[ArgsIndex::IDX_1]);
1831     float endX = FloatOf(args[ArgsIndex::IDX_2]);
1832     float endY = FloatOf(args[ArgsIndex::IDX_3]);
1833     component->paint_.createLinearGradient(startX, startY, endX, endY);
1834     return UNDEFINED;
1835 }
1836 
CreateRadialGradient(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)1837 jerry_value_t CanvasComponent::CreateRadialGradient(const jerry_value_t func,
1838                                                     const jerry_value_t context,
1839                                                     const jerry_value_t args[],
1840                                                     const jerry_length_t argsNum)
1841 {
1842     (void)func;
1843     if (argsNum < ArgsCount::NUM_6) {
1844         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of createRadialGradient method parameter error!");
1845         return jerry_create_error(JERRY_ERROR_TYPE,
1846                                   reinterpret_cast<const jerry_char_t *>("createRadialGradient method param error"));
1847     }
1848 
1849     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::
1850                                                                 GetComponentFromBindingObject(context));
1851     if (component == nullptr) {
1852         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1853         return jerry_create_error(JERRY_ERROR_TYPE,
1854                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1855     }
1856 
1857     float startX = FloatOf(args[ArgsIndex::IDX_0]);
1858     float startY = FloatOf(args[ArgsIndex::IDX_1]);
1859     float startR = FloatOf(args[ArgsIndex::IDX_2]);
1860     float endX = FloatOf(args[ArgsIndex::IDX_3]);
1861     float endY = FloatOf(args[ArgsIndex::IDX_4]);
1862     float endR = FloatOf(args[ArgsIndex::IDX_5]);
1863     component->paint_.createRadialGradient(startX, startY, startR, endX, endY, endR);
1864     return UNDEFINED;
1865 }
1866 
AddColorStop(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)1867 jerry_value_t CanvasComponent::AddColorStop(const jerry_value_t func,
1868                                             const jerry_value_t context,
1869                                             const jerry_value_t args[],
1870                                             const jerry_length_t argsNum)
1871 {
1872     (void)func;
1873     if (argsNum < ArgsCount::NUM_2) {
1874         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of addColorStop method parameter error!");
1875         return jerry_create_error(JERRY_ERROR_TYPE,
1876                                   reinterpret_cast<const jerry_char_t *>("addColorStop method parameter error"));
1877     }
1878 
1879     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
1880     if (component == nullptr) {
1881         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1882         return jerry_create_error(JERRY_ERROR_TYPE,
1883                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1884     }
1885 
1886     ACE_FREE(component->colorStopValue_);
1887     float stop = FloatOf(args[ArgsIndex::IDX_0]);
1888     component->colorStopValue_ = MallocStringOf(args[ArgsIndex::IDX_1]);
1889     if (component->colorStopValue_ == nullptr) {
1890         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: colorStop value error!");
1891         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("colorStop value erro!"));
1892     }
1893 
1894     uint32_t color = 0;
1895     uint8_t alpha = OPA_OPAQUE;
1896     ParseColor(component->colorStopValue_, color, alpha);
1897     ColorType colorStop = component->GetRGBColor(color);
1898     colorStop.alpha = alpha;
1899     component->paint_.addColorStop(stop, colorStop);
1900     return UNDEFINED;
1901 }
1902 #endif
1903 
1904 #if GRAPHIC_ENABLE_PATTERN_FILL_FLAG
CreatePattern(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)1905 jerry_value_t CanvasComponent::CreatePattern(const jerry_value_t func,
1906                                              const jerry_value_t context,
1907                                              const jerry_value_t args[],
1908                                              const jerry_length_t argsNum)
1909 {
1910     (void)func;
1911     if (argsNum < ArgsCount::NUM_2) {
1912         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of createPattern method parameter error!");
1913         return jerry_create_error(JERRY_ERROR_TYPE,
1914                                   reinterpret_cast<const jerry_char_t *>("createPattern method parameter error"));
1915     }
1916 
1917     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
1918     if (component == nullptr) {
1919         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1920         return jerry_create_error(JERRY_ERROR_TYPE,
1921                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1922     }
1923 
1924     ACE_FREE(component->patternPathValue_);
1925     ACE_FREE(component->patternRepeatTypeValue_);
1926     uint16_t patternRepeatTypeLength = 0;
1927     component->patternPathValue_ = MallocStringOf(args[ArgsIndex::IDX_0]);
1928     component->patternRepeatTypeValue_ = MallocStringOf(args[ArgsIndex::IDX_1], &patternRepeatTypeLength);
1929     if (component->patternPathValue_ == nullptr) {
1930         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: patternPath value error!");
1931         return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t *>("patternPath value erro!"));
1932     }
1933     if (component->patternRepeatTypeValue_ == nullptr) {
1934         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: patternRepeatType value error!");
1935         return jerry_create_error(JERRY_ERROR_TYPE,
1936                                   reinterpret_cast<const jerry_char_t *>("patternRepeatType value erro!"));
1937     }
1938     uint16_t patternRepeatTypeId = KeyParser::ParseKeyId(component->patternRepeatTypeValue_, patternRepeatTypeLength);
1939     if (IsFileExisted(component->patternPathValue_)) {
1940         switch (patternRepeatTypeId) {
1941             case K_NO_REPEAT:
1942                 component->paint_.CreatePattern(component->patternPathValue_, NO_REPEAT);
1943                 break;
1944             case K_REPEAT:
1945                 component->paint_.CreatePattern(component->patternPathValue_, REPEAT);
1946                 break;
1947             case K_REPEAT_X:
1948                 component->paint_.CreatePattern(component->patternPathValue_, REPEAT_X);
1949                 break;
1950             case K_REPEAT_Y:
1951                 component->paint_.CreatePattern(component->patternPathValue_, REPEAT_Y);
1952                 break;
1953         }
1954     }
1955     return UNDEFINED;
1956 }
1957 #endif
1958 
Fill(const jerry_value_t func,const jerry_value_t dom,const jerry_value_t args[],const jerry_length_t argsNum)1959 jerry_value_t CanvasComponent::Fill(const jerry_value_t func,
1960                                     const jerry_value_t dom,
1961                                     const jerry_value_t args[],
1962                                     const jerry_length_t argsNum)
1963 {
1964     (void)func;
1965     (void)args;
1966     (void)argsNum;
1967     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(dom));
1968     if (component == nullptr) {
1969         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
1970         return jerry_create_error(JERRY_ERROR_TYPE,
1971                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
1972     }
1973 #if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
1974     component->canvas_.FillPath(component->paint_);
1975 #endif
1976     return UNDEFINED;
1977 }
1978 
ParseImageName(const jerry_value_t args[],char * & imageName,int16_t & width,int16_t & height)1979 jerry_value_t CanvasComponent::ParseImageName(const jerry_value_t args[],
1980                                               char*& imageName,
1981                                               int16_t &width,
1982                                               int16_t &height)
1983 {
1984     ImageComponent* imageCom = nullptr;
1985     const char *src = nullptr;
1986     char* imageNameBuf = nullptr;
1987     // The parameter is the file name
1988     if (jerry_value_is_string(args[ArgsIndex::IDX_0])) {
1989         imageNameBuf = MallocStringOf(args[ArgsIndex::IDX_0]);
1990         if (imageNameBuf == nullptr) {
1991             HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get imageName value failed");
1992             return jerry_create_error(JERRY_ERROR_TYPE,
1993                                       reinterpret_cast<const jerry_char_t *>("get imageName value failed"));
1994         }
1995         imageName = imageNameBuf;
1996         return UNDEFINED;
1997     // Parameters are objects retrieved from HML
1998     } else if (jerry_value_is_object(args[ArgsIndex::IDX_0])) {
1999         void *nativePtr = nullptr;
2000         jerry_get_object_native_pointer(args[ArgsIndex::IDX_0], &nativePtr, nullptr);
2001         imageCom = reinterpret_cast<ImageComponent*>(nativePtr);
2002         if (imageCom != nullptr) {
2003             src = imageCom->GetSrc();
2004             if (src == nullptr) {
2005                 HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get imagePath value failed");
2006                 return jerry_create_error(JERRY_ERROR_TYPE,
2007                                           reinterpret_cast<const jerry_char_t *>("get imagePath value failed"));
2008             }
2009             size_t strLen = strlen(src) + 1;
2010             imageName = static_cast<char*>(ace_malloc(strLen));
2011             if (memcpy_s(imageName, strLen, src, strLen) != EOK) {
2012                 HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get imagePath value failed");
2013                 return UNDEFINED;
2014             }
2015             return UNDEFINED;
2016         } else {
2017             // The argument is the object passed through the new Image()
2018             imageName = GetImageObjectParam(args, width, height);
2019             if (strlen(imageName) < 1) {
2020                 return jerry_create_error(JERRY_ERROR_TYPE,
2021                                           reinterpret_cast<const jerry_char_t *>("get imageName value failed"));
2022             }
2023             return UNDEFINED;
2024         }
2025     } else {
2026         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get imageName value failed");
2027         return jerry_create_error(JERRY_ERROR_TYPE,
2028                                   reinterpret_cast<const jerry_char_t *>("get imageName value failed"));
2029     }
2030     return UNDEFINED;
2031 }
2032 
GetImageObjectParam(const jerry_value_t args[],int16_t & width,int16_t & height)2033 char* CanvasComponent::GetImageObjectParam(const jerry_value_t args[], int16_t &width, int16_t &height)
2034 {
2035     jerry_value_t src = jerryx_get_property_str(args[ArgsIndex::IDX_0], ImageModule::attrSrc);
2036     char* srcVal = MallocStringOf(src);
2037     jerry_value_t jWidth = jerryx_get_property_str(args[ArgsIndex::IDX_0], ImageModule::attrWidth);
2038     jerry_value_t jHeight = jerryx_get_property_str(args[ArgsIndex::IDX_0], ImageModule::attrHeight);
2039     width = IntegerOf(jWidth);
2040     height = IntegerOf(jHeight);
2041     return srcVal;
2042 }
2043 
2044 #if GRAPHIC_ENABLE_DRAW_IMAGE_FLAG
DrawImage(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)2045 jerry_value_t CanvasComponent::DrawImage(const jerry_value_t func, const jerry_value_t context,
2046                                          const jerry_value_t args[], const jerry_length_t argsNum)
2047 {
2048     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
2049     if (component == nullptr) {
2050         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
2051         return jerry_create_error(JERRY_ERROR_TYPE,
2052                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
2053     }
2054     if (argsNum < ArgsCount::NUM_1) {
2055         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: DrawImage arg error!");
2056         return jerry_create_error(JERRY_ERROR_TYPE,
2057                                   reinterpret_cast<const jerry_char_t *>("canvas_component: DrawImage arg error!"));
2058     }
2059     char* imageName = nullptr;
2060     int16_t startX = 0;
2061     int16_t startY = 0;
2062     int16_t width = -1;
2063     int16_t height = -1;
2064     if (ParseImageName(args, imageName, width, height) != UNDEFINED) {
2065         return jerry_create_error(JERRY_ERROR_TYPE,
2066                                   reinterpret_cast<const jerry_char_t *>("parse Imagename function error"));
2067     }
2068     if (argsNum >= ArgsCount::NUM_2) {
2069         startX = IntegerOf(args[ArgsIndex::IDX_1]);
2070     }
2071     if (argsNum >= ArgsCount::NUM_3) {
2072         startY = IntegerOf(args[ArgsIndex::IDX_2]);
2073     }
2074     if (argsNum >= ArgsCount::NUM_4) {
2075         width = IntegerOf(args[ArgsIndex::IDX_3]);
2076     }
2077     if (argsNum >= ArgsCount::NUM_5) {
2078         height = IntegerOf(args[ArgsIndex::IDX_4]);
2079     }
2080     Point startLocat = {startX, startY};
2081     if (width > 0 || height > 0) {
2082         component->canvas_.DrawImage(startLocat, imageName, component->paint_, width, height);
2083     } else {
2084         component->canvas_.DrawImage(startLocat, imageName, component->paint_);
2085     }
2086     ACE_FREE(imageName);
2087     return UNDEFINED;
2088 }
2089 #endif
2090 
2091 #if GRAPHIC_ENABLE_DASH_GENERATE_FLAG
SetLineDash(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)2092 jerry_value_t CanvasComponent::SetLineDash(const jerry_value_t func,
2093                                            const jerry_value_t context,
2094                                            const jerry_value_t args[],
2095                                            const jerry_length_t argsNum)
2096 {
2097     if (argsNum < ArgsCount::NUM_1) {
2098         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: SetLineDash arg error!");
2099         return jerry_create_error(JERRY_ERROR_TYPE,
2100                                   reinterpret_cast<const jerry_char_t *>("canvas_component: DrawImage arg error!"));
2101     }
2102 
2103     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
2104     if (component == nullptr) {
2105         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
2106         return jerry_create_error(JERRY_ERROR_TYPE,
2107                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
2108     }
2109     jerry_value_t array = args[ArgsIndex::IDX_0];
2110     if (!jerry_value_is_array(array)) {
2111         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: SetLineDash arg error!");
2112         return jerry_create_error(JERRY_ERROR_TYPE,
2113                                   reinterpret_cast<const jerry_char_t *>("canvas_component: DrawImage arg error!"));
2114     }
2115     uint32_t len = jerry_get_array_length(array);
2116     const uint32_t maxSize = 1024;
2117     if (len == 0 || len > maxSize * maxSize) {
2118         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: len = 0 error!");
2119         return jerry_create_error(JERRY_ERROR_TYPE,
2120                                   reinterpret_cast<const jerry_char_t *>("canvas_component: len = 0 error!"));
2121     }
2122     float *buff = new float[len];
2123     if (buff == nullptr) {
2124         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: new buff error!");
2125         return jerry_create_error(JERRY_ERROR_TYPE,
2126                                   reinterpret_cast<const jerry_char_t *>("canvas_component: new buff error!"));
2127     }
2128     for (uint32_t i = 0; i < len; i++) {
2129         jerry_value_t val = jerry_get_property_by_index(array, i);
2130         float num = 0;
2131         if (jerry_value_is_number(val)) {
2132             num = jerry_get_number_value(val);
2133         } else if (jerry_value_is_string(val)) {
2134             char *value = MallocStringOf(val);
2135             if (value == nullptr) {
2136                 delete[] buff;
2137                 HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: value is nullptr error!");
2138                 return jerry_create_error(JERRY_ERROR_TYPE,
2139                     reinterpret_cast<const jerry_char_t *>("canvas_component:value is nullptr error!"));
2140             }
2141             num = atof(value);
2142             ACE_FREE(value);
2143         } else {
2144             HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: SetLineDash arg error!");
2145         }
2146 
2147         buff[i] = num;
2148         jerry_release_value(val);
2149     }
2150 
2151     component->paint_.SetLineDash(buff, len);
2152     delete[] buff;
2153     return UNDEFINED;
2154 }
2155 
GetLineDash(const jerry_value_t func,const jerry_value_t context,const jerry_value_t args[],const jerry_length_t argsNum)2156 jerry_value_t CanvasComponent::GetLineDash(const jerry_value_t func,
2157                                            const jerry_value_t context,
2158                                            const jerry_value_t args[],
2159                                            const jerry_length_t argsNum)
2160 {
2161     CanvasComponent *component = static_cast<CanvasComponent *>(ComponentUtils::GetComponentFromBindingObject(context));
2162     if (component == nullptr) {
2163         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!");
2164         return jerry_create_error(JERRY_ERROR_TYPE,
2165                                   reinterpret_cast<const jerry_char_t *>("get canvas component from js object failed"));
2166     }
2167     float* pArr = component->paint_.GetLineDash();
2168     uint32_t length = component->paint_.GetLineDashCount();
2169     jerry_release_value(component->dashArray_);
2170     component->dashArray_ = jerry_create_array(length);
2171     for (uint32_t i = 0; i < length; i++) {
2172         jerry_set_property_by_index(component->dashArray_, i, jerry_create_number(pArr[i]));
2173     }
2174     return component->dashArray_;
2175 }
2176 #endif
2177 
GetSubFont(const char * font,const uint8_t index,char * & subFont) const2178 void CanvasComponent::GetSubFont(const char *font, const uint8_t index, char *&subFont) const
2179 {
2180     if (font == nullptr || *font == '\0') {
2181         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get font attribute failed");
2182         return;
2183     }
2184 
2185     if (strlen(font) >= UINT16_MAX) {
2186         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: font size overflow.");
2187         return;
2188     }
2189 
2190     // skip space character at the head of string.
2191     while ((*font != '\0') && (*font == ' ')) {
2192         font++;
2193     }
2194 
2195     uint8_t currentIndex = 0;
2196     const char *currentPos = font;
2197     const char *endPos = font;
2198     while (*currentPos != '\0') {
2199         // find the sub font string
2200         if (currentIndex == index) {
2201             if (*endPos != ' ' && *endPos != '\0') {
2202                 endPos++;
2203                 continue;
2204             }
2205             uint16_t len = endPos - currentPos;
2206             uint16_t startIndex = currentPos - font;
2207             subFont = static_cast<char *>(ace_malloc(len + 1));
2208             if (subFont == nullptr) {
2209                 HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: malloc buffer failed for subFont");
2210                 return;
2211             }
2212             errno_t error = strncpy_s(subFont, len + 1, font + startIndex, len);
2213             if (error != 0) {
2214                 HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: copy string failed");
2215                 ace_free(subFont);
2216                 subFont = nullptr;
2217                 return;
2218             }
2219             subFont[len] = '\0';
2220             break;
2221         }
2222         // go forward to next sub font string.
2223         if (*(currentPos) == ' ' && *(currentPos + 1) != ' ') {
2224             currentIndex = currentIndex + 1;
2225             endPos = currentPos + 1;
2226         }
2227         currentPos++;
2228     }
2229 }
2230 
FormatArcAngle(double sAngle,double eAngle,int16_t & startAngle,int16_t & endAngle,bool counterClockwise) const2231 bool CanvasComponent::FormatArcAngle(double sAngle,
2232                                      double eAngle,
2233                                      int16_t &startAngle,
2234                                      int16_t &endAngle,
2235                                      bool counterClockwise) const
2236 {
2237     constexpr int16_t DEGREES_180 = 180;
2238     constexpr int16_t DEGREES_360 = 360;
2239 
2240     // calculate start-angle and end-angle degrees
2241     if (((DEGREES_180 * sAngle / UI_PI) > INT16_MAX) || ((DEGREES_180 * sAngle / UI_PI) < INT16_MIN) ||
2242         ((DEGREES_180 * eAngle / UI_PI) > INT16_MAX) || ((DEGREES_180 * eAngle / UI_PI) < INT16_MIN)) {
2243         HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: startAngle or endAngle overflow.");
2244         return false;
2245     }
2246 
2247     startAngle = static_cast<uint32_t>(DEGREES_180 * sAngle / UI_PI);
2248     endAngle = static_cast<uint32_t>(DEGREES_180 * eAngle / UI_PI);
2249 
2250     if (!counterClockwise) {
2251         // reset end-angle degrees when (endAngle - startAngle) > 360
2252         if ((endAngle - startAngle) > DEGREES_360) {
2253             endAngle = startAngle + DEGREES_360;
2254         }
2255         // reset start-angle or end-angle degrees when startAngle > endAngle
2256         if (startAngle <= endAngle) {
2257             return true;
2258         }
2259         while (startAngle >= endAngle) {
2260             if ((endAngle + DEGREES_360) > INT16_MAX) {
2261                 startAngle = startAngle - DEGREES_360;
2262                 break;
2263             }
2264             endAngle = endAngle + DEGREES_360;
2265         }
2266     } else {
2267         // reset end-angle degrees when (endAngle - startAngle) > 360
2268         if ((startAngle - endAngle) > DEGREES_360) {
2269             endAngle = startAngle - DEGREES_360;
2270         }
2271         // reset start-angle or end-angle degrees when startAngle < endAngle
2272         if (startAngle >= endAngle) {
2273             return true;
2274         }
2275         while (startAngle <= endAngle) {
2276             if ((endAngle - DEGREES_360) < INT16_MIN) {
2277                 startAngle = startAngle + DEGREES_360;
2278                 break;
2279             }
2280             endAngle = endAngle - DEGREES_360;
2281         }
2282     }
2283 
2284     return true;
2285 }
2286 
RegisterAttributeFunc(jerry_value_t canvas2ddom,const char * attributeName,jerry_external_handler_t setterHandler,jerry_external_handler_t getterHandler)2287 void CanvasComponent::RegisterAttributeFunc(jerry_value_t canvas2ddom,
2288                                             const char *attributeName,
2289                                             jerry_external_handler_t setterHandler,
2290                                             jerry_external_handler_t getterHandler)
2291 {
2292     // register canvas js attribute setter property via the jerry_define_own_property method
2293     jerry_value_t propName = jerry_create_string(reinterpret_cast<const jerry_char_t *>(attributeName));
2294     jerry_property_descriptor_t desc;
2295     jerry_init_property_descriptor_fields(&desc);
2296     desc.is_set_defined = true;
2297     desc.setter = jerry_create_external_function(setterHandler);
2298     desc.is_get_defined = true;
2299     desc.getter = jerry_create_external_function(getterHandler);
2300     jerry_value_t returnValue = jerry_define_own_property(canvas2ddom, propName, &desc);
2301     jerry_free_property_descriptor_fields(&desc);
2302     ReleaseJerryValue(propName, returnValue, VA_ARG_END_FLAG);
2303 }
2304 
RegisterDrawMethodFunc(jerry_value_t canvas2ddom,const char * drawMethodName,jerry_external_handler_t handler)2305 void CanvasComponent::RegisterDrawMethodFunc(jerry_value_t canvas2ddom,
2306                                              const char *drawMethodName,
2307                                              jerry_external_handler_t handler)
2308 {
2309     JerrySetFuncProperty(canvas2ddom, drawMethodName, handler);
2310 }
2311 } // namespace ACELite
2312 } // namespace OHOS
2313 #endif // FEATURE_COMPONENT_CANVAS
2314