From bf09d04cd84bbc0f4bddefdbf65d4825bf4a8a3d Mon Sep 17 00:00:00 2001 From: "xucheng57@huawei.com" Date: Wed, 13 Apr 2022 16:08:40 +0800 Subject: [PATCH] =?UTF-8?q?Ace=20=E5=9B=BE=E5=BD=A2=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E6=89=B9=E6=AC=A1=E6=8F=90=E4=BA=A4=20=E6=B8=90=E5=8F=98?= =?UTF-8?q?=E3=80=81pattern=EF=BC=8CLinedash=E3=80=81=E6=B7=B7=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xucheng57 --- frameworks/src/core/base/js_fwk_common.cpp | 46 + frameworks/src/core/base/js_fwk_common.h | 9 + frameworks/src/core/base/keys.h | 8 + .../src/core/components/canvas_component.cpp | 1050 ++++++++++++++++- .../src/core/components/canvas_component.h | 146 ++- .../src/core/components/image_component.cpp | 19 + .../src/core/modules/presets/image_module.cpp | 320 +++++ .../src/core/modules/presets/image_module.h | 127 ++ .../src/core/modules/presets/intl_module.cpp | 2 + .../src/core/modules/presets/intl_module.h | 1 - 10 files changed, 1673 insertions(+), 55 deletions(-) diff --git a/frameworks/src/core/base/js_fwk_common.cpp b/frameworks/src/core/base/js_fwk_common.cpp index 5a2c743..ee8fc9a 100644 --- a/frameworks/src/core/base/js_fwk_common.cpp +++ b/frameworks/src/core/base/js_fwk_common.cpp @@ -215,6 +215,19 @@ int16_t IntegerOf(jerry_value_t source) return static_cast(number); } +float FloatOf(jerry_value_t source) +{ + float value = 0; + if (jerry_value_is_number(source)) { + value = static_cast(jerry_get_number_value(source)); + } else { + char* cstr = MallocStringOf(source); + value = static_cast(atof(cstr)); + ACE_FREE(cstr); + } + return value; +} + jerry_value_t WatcherCallbackFunc(const jerry_value_t func, const jerry_value_t context, const jerry_value_t *args, @@ -1312,5 +1325,38 @@ uint16_t ParseKeyIdFromJSString(const jerry_value_t str) } return keyId; } +int8_t ParseLineCap(const char *lineCap) +{ + if (lineCap == nullptr) { + return -1; + } + if (strcasecmp(lineCap, LINECAP_BUTT) == 0) { + return BUTT_VALUE; + } + if (strcasecmp(lineCap, LINECAP_SQUARE) == 0) { + return SQUARE_VALUE; + } + if (strcasecmp(lineCap, LINECAP_ROUND) == 0) { + return ROUND_VALUE; + } + return -1; +} + +int8_t ParseLineJoin(const char *lineJoin) +{ + if (lineJoin == nullptr) { + return -1; + } + if (strcasecmp(lineJoin, LINEJOIN_MITER) == 0) { + return LINEJOIN_MITER_VALUE; + } + if (strcasecmp(lineJoin, LINEJOIN_ROUND) == 0) { + return LINEJOIN_ROUND_VALUE; + } + if (strcasecmp(lineJoin, LINEJOIN_BEVEL) == 0) { + return LINEJOIN_BEVEL_VALUE; + } + return -1; +} } // namespace ACELite } // namespace OHOS diff --git a/frameworks/src/core/base/js_fwk_common.h b/frameworks/src/core/base/js_fwk_common.h index 41427c8..019b9d7 100644 --- a/frameworks/src/core/base/js_fwk_common.h +++ b/frameworks/src/core/base/js_fwk_common.h @@ -188,6 +188,7 @@ char *MallocStringOf(jerry_value_t source); // convert one jerry string value to char*, and return the string length char *MallocStringOf(jerry_value_t source, uint16_t *strLength); int16_t IntegerOf(jerry_value_t source); +float FloatOf(jerry_value_t source); bool BoolOf(jerry_value_t source); // relocate file name to full path of the current app path @@ -297,6 +298,12 @@ constexpr char PATH_DEFAULT[] = "/"; constexpr char PREFIX_HEX_COLOR[] = "#"; constexpr char PREFIX_RGB_COLOR[] = "rgb"; constexpr char PREFIX_RGBA_COLOR[] = "rgba"; +constexpr char LINEJOIN_MITER[] = "miter"; +constexpr char LINEJOIN_ROUND[] = "round"; +constexpr char LINEJOIN_BEVEL[] = "bevel"; +constexpr char LINECAP_BUTT[] = "butt"; +constexpr char LINECAP_SQUARE[] = "square"; +constexpr char LINECAP_ROUND[] = "round"; constexpr uint8_t ALPHA_MAX = 255; constexpr char BRACE_OPEN = '('; constexpr char BRACE_CLOSE = ')'; @@ -311,6 +318,8 @@ bool ParseHexColor(const char * const source, uint32_t &color, uint8_t &alpha); bool ParseRgbaColor(const char * const source, uint32_t &color, uint8_t &alpha); bool ParseColor(const char * const source, uint32_t &color, uint8_t &alpha); bool CopyFontFamily(char *&destination, const char * const fontFamily, uint32_t fontFamilyNameLen = 0); +int8_t ParseLineCap(const char* lineCap); +int8_t ParseLineJoin(const char* lineJoin); constexpr int16_t BUTT_VALUE = 0; constexpr int16_t SQUARE_VALUE = 1; diff --git a/frameworks/src/core/base/keys.h b/frameworks/src/core/base/keys.h index 51a334a..eec5f01 100644 --- a/frameworks/src/core/base/keys.h +++ b/frameworks/src/core/base/keys.h @@ -72,6 +72,8 @@ enum { #if (FEATURE_COMPONENT_CANVAS == 1) KEYWORD(CANVAS, canvas) // tag name #endif // FEATURE_COMPONENT_CANVAS + KEYWORD(CANVASGRADIENT, canvasGradient) // gradient fillstyle or strokestyle + KEYWORD(CANVASPATTERN, canvasPattern) // pattern fillstyle or strokestyle KEYWORD(CENTER, center) // text align type KEYWORD(CENTER_X, centerX) // circle progress x KEYWORD(CENTER_Y, centerY) // circle progress y @@ -180,6 +182,7 @@ enum { #endif // FEATURE_COMPONENT_VIDEO KEYWORD(NAME, name) // the attribute name KEYWORD(NONE, none) // animation fill value + KEYWORD(NO_REPEAT, no-repeat) // no-repeat pattern type #if (FEATURE_DATE_FORMAT == 1) KEYWORD(NUMERIC, numeric) #endif @@ -212,6 +215,9 @@ enum { KEYWORD(PAUSE, pause) // video component pause event tag #endif // FEATURE_COMPONENT_VIDEO KEYWORD(REVERSE, reverse) // image-animator attribute + KEYWORD(REPEAT, repeat) // repeat pattern type + KEYWORD(REPEAT_X, repeat-x) // repeat-x pattern type + KEYWORD(REPEAT_Y, repeat-y) // repeat-y pattern type KEYWORD(RIGHT, right) // text align type KEYWORD(ROTATE, rotate) // animation rotate KEYWORD(ROW, row) // layout style @@ -246,6 +252,8 @@ enum { KEYWORD(SPACE_BETWEEN, space-between) // layout style KEYWORD(SPACE_EVENLY, space-evenly) // layout style KEYWORD(SRC, src) // common attributes, use for image view, video compnent + KEYWORD(IMAGE_WIDTH, width) // common attributes, use for image view, video compnent + KEYWORD(IMAGE_HEIGHT, height) // common attributes, use for image view, video compnent KEYWORD(STACK, stack) // tag name KEYWORD(STANDARD, standard) // text font size type KEYWORD(STOP, stop) // image-animator stop event, video component end event tag diff --git a/frameworks/src/core/components/canvas_component.cpp b/frameworks/src/core/components/canvas_component.cpp index 6650b77..26dae1e 100644 --- a/frameworks/src/core/components/canvas_component.cpp +++ b/frameworks/src/core/components/canvas_component.cpp @@ -47,9 +47,18 @@ const char * const CanvasComponent::ATTR_STROKESTYLE = "strokeStyle"; const char * const CanvasComponent::ATTR_LINEWIDTH = "lineWidth"; const char * const CanvasComponent::ATTR_FONT = "font"; const char * const CanvasComponent::ATTR_TEXTALIGN = "textAlign"; +const char * const CanvasComponent::ATTR_LINECAP = "lineCap"; +const char * const CanvasComponent::ATTR_LINEJOIN = "lineJoin"; +const char * const CanvasComponent::ATTR_MITERLIMIT = "miterLimit"; +const char * const CanvasComponent::ATTR_LINEDASHOFFSET = "lineDashOffset"; const char * const CanvasComponent::ATTR_GLOBALALPHA = "globalAlpha"; +const char * const CanvasComponent::ATTR_GLOBALCOMPOSITEOPERATION = "globalCompositeOperation"; +const char * const CanvasComponent::ATTR_SHADOWOFFSETX = "shadowOffsetX"; +const char * const CanvasComponent::ATTR_SHADOWOFFSETY = "shadowOffsetY"; +const char * const CanvasComponent::ATTR_SHADOWCOLOR = "shadowColor"; +const char * const CanvasComponent::ATTR_SHADOWBLUR = "shadowBlur"; // API-method -const char * const CanvasComponent::FUNC_GETdom = "getDom"; +const char * const CanvasComponent::FUNC_GETCONTEXT = "getContext"; const char * const CanvasComponent::FUNC_FILLRECT = "fillRect"; const char * const CanvasComponent::FUNC_STROKERECT = "strokeRect"; const char * const CanvasComponent::FUNC_FILLTEXT = "fillText"; @@ -57,14 +66,22 @@ const char * const CanvasComponent::FUNC_BEGINPATH = "beginPath"; const char * const CanvasComponent::FUNC_MOVETO = "moveTo"; const char * const CanvasComponent::FUNC_LINETO = "lineTo"; const char * const CanvasComponent::FUNC_RECT = "rect"; +const char * const CanvasComponent::FUNC_CLEANRECT = "cleanRect"; const char * const CanvasComponent::FUNC_ARC = "arc"; const char * const CanvasComponent::FUNC_CLOSEPATH = "closePath"; const char * const CanvasComponent::FUNC_STROKE = "stroke"; +const char * const CanvasComponent::FUNC_CREATELINEARGRADIENT = "createLinearGradient"; +const char * const CanvasComponent::FUNC_CREATERADIALGRADIENT = "createRadialGradient"; +const char * const CanvasComponent::FUNC_ADDCOLORSTOP = "addColorStop"; +const char * const CanvasComponent::FUNC_CREATEPATTERN = "createPattern"; const char * const CanvasComponent::FUNC_FILL = "fill"; +const char * const CanvasComponent::FUNC_DRAWIMAGE = "drawImage"; const char * const CanvasComponent::FUNC_SETLINEDASH = "setLineDash"; const char * const CanvasComponent::FUNC_GETLINEDASH = "getLineDash"; +const char * const CanvasComponent::FUNC_STROKETEXT = "strokeText"; const char * const CanvasComponent::FUNC_ROTATE = "rotate"; const char * const CanvasComponent::FUNC_SCALE = "scale"; +const char * const CanvasComponent::FUNC_MEASURETEXT = "measureText"; const char * const CanvasComponent::FUNC_TRANSLATE = "translate"; const char * const CanvasComponent::FUNC_TRANFORM = "transform"; const char * const CanvasComponent::FUNC_SETTRANFORM = "setTransform"; @@ -78,12 +95,21 @@ const AttrMap CanvasComponent::attrMap_[] = { {ATTR_LINEWIDTH, LineWidthSetter, LineWidthGetter}, {ATTR_FONT, FontSetter, FontGetter}, {ATTR_TEXTALIGN, TextAlignSetter, TextAlignGetter}, - {ATTR_GLOBALALPHA, GlobalAlphaSetter, GlobalAlphaGetter} + {ATTR_SHADOWOFFSETX, ShadowOffsetXSetter, ShadowOffsetXGetter}, + {ATTR_SHADOWOFFSETY, ShadowOffsetYSetter, ShadowOffsetYGetter}, + {ATTR_SHADOWCOLOR, ShadowColorSetter, ShadowColorGetter}, + {ATTR_SHADOWBLUR, ShadowBlurSetter, ShadowBlurGetter}, + {ATTR_LINECAP, LineCapSetter, LineCapGetter}, + {ATTR_LINEJOIN, LineJoinSetter, LineJoinGetter}, + {ATTR_MITERLIMIT, MiterLimitSetter, MiterLimitGetter}, + {ATTR_LINEDASHOFFSET, LineDashOffsetSetter, LineDashOffsetGetter}, + {ATTR_GLOBALALPHA, GlobalAlphaSetter, GlobalAlphaGetter}, + {ATTR_GLOBALCOMPOSITEOPERATION, GlobalCompositeOperationSetter, GlobalCompositeOperationGetter} }; // create canvas draw method-callback mapping const MethodMap CanvasComponent::methodMap_[] = { - {FUNC_GETdom, GetDom}, + {FUNC_GETCONTEXT, GetContext}, {FUNC_FILLRECT, FillRect}, {FUNC_STROKERECT, StrokeRect}, {FUNC_FILLTEXT, FillText}, @@ -91,12 +117,22 @@ const MethodMap CanvasComponent::methodMap_[] = { {FUNC_MOVETO, MoveTo}, {FUNC_LINETO, LineTo}, {FUNC_RECT, Rect}, + {FUNC_CLEANRECT, CleanRect}, {FUNC_ARC, Arc}, {FUNC_CLOSEPATH, ClosePath}, {FUNC_STROKE, Stroke}, + {FUNC_CREATELINEARGRADIENT, CreateLInearGradient}, + {FUNC_CREATERADIALGRADIENT, CreateRadialGradient}, + {FUNC_ADDCOLORSTOP, AddColorStop}, + {FUNC_CREATEPATTERN, CreatePattern}, {FUNC_FILL, Fill}, + {FUNC_DRAWIMAGE, DrawImage}, + {FUNC_SETLINEDASH, SetLineDash}, + {FUNC_GETLINEDASH, GetLineDash}, + {FUNC_STROKETEXT, StrokeText}, {FUNC_ROTATE, Rotate}, {FUNC_SCALE, Scale}, + {FUNC_MEASURETEXT, MeasureText}, {FUNC_TRANSLATE, Translate}, {FUNC_TRANFORM, Transform}, {FUNC_SETTRANFORM, SetTransform}, @@ -115,12 +151,18 @@ CanvasComponent::CanvasComponent(jerry_value_t options, jerry_value_t children, strokeStyleValue_(nullptr), fontValue_(nullptr), textAlignValue_(nullptr), + shadowOffsetXValue_(0), + shadowOffsetYValue_(0), + shadowBlurValue_(0), + shadowColorValue_(nullptr), lineWidthValue_(1), lineCapValue_(nullptr), lineJoinValue_(nullptr), miterLimitValue_(DEFAULT_MITERLIMIT), lineDashOffsetValue_(0), - colorStopValue_(nullptr) + colorStopValue_(nullptr), + patternPathValue_(nullptr), + patternRepeatTypeValue_(nullptr) { SetComponentName(K_CANVAS); // set default paint pattern @@ -190,15 +232,15 @@ UIView *CanvasComponent::GetComponentRootView() const return (const_cast(&canvas_)); } -jerry_value_t CanvasComponent::GetDom(const jerry_value_t func, - const jerry_value_t dom, - const jerry_value_t args[], - const jerry_length_t argsNum) +jerry_value_t CanvasComponent::GetContext(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum) { (void)func; (void)args; - if (argsNum != ArgsCount::NUM_1) { - HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of getdom method parameter error!"); + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of getContext method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("getdom method parameter error")); } @@ -241,7 +283,7 @@ jerry_value_t CanvasComponent::FillStyleSetter(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_1) { + if (argsNum < ArgsCount::NUM_1) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: fillStyle value error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("fillStyle value error")); } @@ -263,6 +305,14 @@ jerry_value_t CanvasComponent::FillStyleSetter(const jerry_value_t func, uint32_t color = 0; uint8_t alpha = OPA_OPAQUE; + uint16_t fillStyleId = KeyParser::ParseKeyId(component->fillStyleValue_, fillStyleLength); + if (fillStyleId == K_CANVASPATTERN) { + component->paint_.SetStyle(Paint::PaintStyle::PATTERN); + return UNDEFINED; + } + if (fillStyleId == K_CANVASGRADIENT) { + component->paint_.SetStyle(Paint::PaintStyle::GRADIENT); + } if (ParseColor(component->fillStyleValue_, color, alpha)) { component->paint_.SetFillColor(component->GetRGBColor(color)); component->paint_.SetOpacity(alpha); @@ -301,7 +351,7 @@ jerry_value_t CanvasComponent::StrokeStyleSetter(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_1) { + if (argsNum < ArgsCount::NUM_1) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: strokeStyle value error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("the value of strokeStyle is null")); @@ -324,6 +374,15 @@ jerry_value_t CanvasComponent::StrokeStyleSetter(const jerry_value_t func, uint32_t color = 0; uint8_t alpha = OPA_OPAQUE; + uint16_t strokeStyleId = KeyParser::ParseKeyId(component->strokeStyleValue_, strokeStyleLength); + if (strokeStyleId == K_CANVASPATTERN) { + component->paint_.SetStyle(Paint::PaintStyle::PATTERN); + return UNDEFINED; + } + if (strokeStyleId == K_CANVASGRADIENT) { + component->paint_.SetStyle(Paint::PaintStyle::GRADIENT); + return UNDEFINED; + } if (ParseColor(component->strokeStyleValue_, color, alpha)) { component->paint_.SetStrokeColor(component->GetRGBColor(color)); component->paint_.SetOpacity(alpha); @@ -362,7 +421,7 @@ jerry_value_t CanvasComponent::LineWidthSetter(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_1) { + if (argsNum < ArgsCount::NUM_1) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineWidth value error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("lineWidth value error!")); } @@ -402,7 +461,7 @@ jerry_value_t CanvasComponent::FontSetter(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_1) { + if (argsNum < ArgsCount::NUM_1) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: font value error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("font value error")); @@ -556,17 +615,169 @@ jerry_value_t CanvasComponent::TextAlignGetter(const jerry_value_t func, return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("textAlign value error")); } -jerry_value_t CanvasComponent::GlobalAlphaSetter(const jerry_value_t func, +jerry_value_t CanvasComponent::ShadowOffsetXSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t *args, + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: shadowOffsetX value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("shadowOffsetX value error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + component->shadowOffsetXValue_ = IntegerOf(args[ArgsIndex::IDX_0]); + component->paint_.SetShadowOffsetX(component->shadowOffsetXValue_); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::ShadowOffsetXGetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(dom)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + return jerry_create_number(component->shadowOffsetXValue_); +} + +jerry_value_t CanvasComponent::ShadowOffsetYSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t *args, + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: shadowOffsetY value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("shadowOffsetY value error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + component->shadowOffsetYValue_ = IntegerOf(args[ArgsIndex::IDX_0]); + component->paint_.SetShadowOffsetY(component->shadowOffsetYValue_); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::ShadowOffsetYGetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(dom)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + return jerry_create_number(component->shadowOffsetYValue_); +} + +jerry_value_t CanvasComponent::ShadowBlurSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t *args, + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: shadowBlur value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("shadowBlur value error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + component->shadowBlurValue_ = IntegerOf(args[ArgsIndex::IDX_0]); + component->paint_.SetShadowBlur(component->shadowBlurValue_); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::ShadowBlurGetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(dom)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + return jerry_create_number(component->shadowBlurValue_); +} + +jerry_value_t CanvasComponent::ShadowColorSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: shadowColor value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("shadowColor value error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + ACE_FREE(component->shadowColorValue_); + + component->shadowColorValue_ = MallocStringOf(args[ArgsIndex::IDX_0]); + if (component->shadowColorValue_ == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: shadowColor value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("shadowColor value erro!")); + } + + uint32_t color = 0; + uint8_t alpha = OPA_OPAQUE; + ParseColor(component->shadowColorValue_, color, alpha); + ColorType shadowColor = component->GetRGBColor(color); + shadowColor.alpha = alpha; + component->paint_.SetShadowColor(shadowColor); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::ShadowColorGetter(const jerry_value_t func, const jerry_value_t dom, const jerry_value_t args[], const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_1) { - HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineWidth value error!"); - return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("lineWidth value error!")); - } - + (void)args; + (void)argsNum; CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(dom)); if (component == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); @@ -574,7 +785,224 @@ jerry_value_t CanvasComponent::GlobalAlphaSetter(const jerry_value_t func, reinterpret_cast("get canvas component from js object failed")); } - double globalAlpha = jerry_get_number_value(args[ArgsIndex::IDX_0]); + if (component->shadowColorValue_ == nullptr) { + CopyFontFamily(component->shadowColorValue_, DEFAULT_FILLSTYLE); + } + + if (component->shadowColorValue_ != nullptr) { + return jerry_create_string(reinterpret_cast(component->shadowColorValue_)); + } + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("shadowColor value error")); +} +jerry_value_t CanvasComponent::LineCapSetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineCap value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("lineCap value error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(contex)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + ACE_FREE(component->lineCapValue_); + + uint16_t length = 0; + component->lineCapValue_ = MallocStringOf(args[ArgsIndex::IDX_0], &length); + if (component->lineCapValue_ == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineCap value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("lineCap value error")); + } + + int8_t lineCap = ParseLineCap(component->lineCapValue_); + component->paint_.SetLineCap((LineCap)lineCap); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::LineCapGetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(contex)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + if (component->lineCapValue_ == nullptr) { + CopyFontFamily(component->lineCapValue_, DEFAULT_LINECAP); + } + if (component->lineCapValue_ != nullptr) { + return jerry_create_string(reinterpret_cast(component->lineCapValue_)); + } + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("lineCap value error")); +} + +jerry_value_t CanvasComponent::LineJoinSetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineCap value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("lineCap value error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(contex)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + ACE_FREE(component->lineJoinValue_); + + uint16_t length = 0; + component->lineJoinValue_ = MallocStringOf(args[ArgsIndex::IDX_0], &length); + if (component->lineJoinValue_ == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineJoin value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("lineJoin value error")); + } + + uint8_t lineJoin = ParseLineJoin(component->lineJoinValue_); + component->paint_.SetLineJoin((LineJoin)lineJoin); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::LineJoinGetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(contex)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + if (component->lineJoinValue_ == nullptr) { + CopyFontFamily(component->lineJoinValue_, DEFAULT_LINEJOIN); + } + if (component->lineJoinValue_ != nullptr) { + return jerry_create_string(reinterpret_cast(component->lineJoinValue_)); + } + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("lineJoin value error")); +} + +jerry_value_t CanvasComponent::MiterLimitSetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: miterLimit value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("lineWidth value error!")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(contex)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + component->miterLimitValue_ = FloatOf(args[ArgsIndex::IDX_0]); + component->paint_.SetMiterLimit(component->miterLimitValue_); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::MiterLimitGetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(contex)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + return jerry_create_number(component->miterLimitValue_); +} + +jerry_value_t CanvasComponent::LineDashOffsetSetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineWidth value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("lineWidth value error!")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(contex)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + component->lineDashOffsetValue_ = FloatOf(args[ArgsIndex::IDX_0]); + component->paint_.SetLineDashOffset(component->lineDashOffsetValue_); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::LineDashOffsetGetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(contex)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + return jerry_create_number(component->lineDashOffsetValue_); +} + +jerry_value_t CanvasComponent::GlobalAlphaSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineWidth value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("lineWidth value error!")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + float globalAlpha = FloatOf(args[ArgsIndex::IDX_0]); component->paint_.SetGlobalAlpha(globalAlpha); return UNDEFINED; } @@ -596,13 +1024,84 @@ jerry_value_t CanvasComponent::GlobalAlphaGetter(const jerry_value_t func, return jerry_create_number(component->paint_.GetGlobalAlpha()); } +jerry_value_t CanvasComponent::GlobalCompositeOperationSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: lineWidth value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("lineWidth value error!")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + char *globalCompositeOperation = MallocStringOf(args[ArgsIndex::IDX_0]); + if (globalCompositeOperation == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get text value failed"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("get text value failed")); + } + + if (strcmp(globalCompositeOperation, "source-over") == 0) { + component->paint_.SetGlobalCompositeOperation(OHOS::SOURCE_OVER); + } else if (strcmp(globalCompositeOperation, "source-atop") == 0) { + component->paint_.SetGlobalCompositeOperation(OHOS::SOURCE_ATOP); + } else if (strcmp(globalCompositeOperation, "source-in") == 0) { + component->paint_.SetGlobalCompositeOperation(OHOS::SOURCE_IN); + } else if (strcmp(globalCompositeOperation, "source-out") == 0) { + component->paint_.SetGlobalCompositeOperation(OHOS::SOURCE_OUT); + } else if (strcmp(globalCompositeOperation, "destination-over") == 0) { + component->paint_.SetGlobalCompositeOperation(OHOS::DESTINATION_OVER); + } else if (strcmp(globalCompositeOperation, "destination-atop") == 0) { + component->paint_.SetGlobalCompositeOperation(OHOS::DESTINATION_ATOP); + } else if (strcmp(globalCompositeOperation, "destination-in") == 0) { + component->paint_.SetGlobalCompositeOperation(OHOS::DESTINATION_IN); + } else if (strcmp(globalCompositeOperation, "destination-out") == 0) { + component->paint_.SetGlobalCompositeOperation(OHOS::DESTINATION_OUT); + } else if (strcmp(globalCompositeOperation, "lighter") == 0) { + component->paint_.SetGlobalCompositeOperation(OHOS::LIGHTER); + } else if (strcmp(globalCompositeOperation, "copy") == 0) { + component->paint_.SetGlobalCompositeOperation(OHOS::COPY); + } else if (strcmp(globalCompositeOperation, "xor") == 0) { + component->paint_.SetGlobalCompositeOperation(OHOS::XOR); + } + + ACE_FREE(globalCompositeOperation); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::GlobalCompositeOperationGetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(dom)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + int8_t globalCompositeOperation = component->paint_.GetGlobalCompositeOperation(); + return jerry_create_number(globalCompositeOperation); +} + jerry_value_t CanvasComponent::FillRect(const jerry_value_t func, const jerry_value_t dom, const jerry_value_t args[], const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_4) { + if (argsNum < ArgsCount::NUM_4) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of fillRect method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("fillRect method parameter error")); @@ -636,7 +1135,7 @@ jerry_value_t CanvasComponent::StrokeRect(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_4) { + if (argsNum < ArgsCount::NUM_4) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of strokeRect method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("strokeRect method parameter error")); @@ -670,7 +1169,7 @@ jerry_value_t CanvasComponent::FillText(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_3) { + if (argsNum < ArgsCount::NUM_3) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of fillText method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("fillText method parameter error")); @@ -716,13 +1215,64 @@ jerry_value_t CanvasComponent::FillText(const jerry_value_t func, return UNDEFINED; } + +jerry_value_t CanvasComponent::StrokeText(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_5) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of strokeText method parameter error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("strokeText method parameter error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + char *textValue = MallocStringOf(args[ArgsIndex::IDX_0]); + if (textValue == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get text value failed"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("get text value failed")); + } + + int16_t startX = IntegerOf(args[ArgsIndex::IDX_1]); + int16_t startY = IntegerOf(args[ArgsIndex::IDX_2]); + int16_t maxWidth = component->canvas_.GetWidth(); + + // FILL_STYLE: strokeText support fill shapses only + component->paint_.SetStyle(component->paint_.FILL_STYLE); + + Point startPoint; + const int8_t half = 2; + startPoint.x = startX; + startPoint.y = startY; + + // if textAlign=left, not need transfer startX, if textAlign=right or center, need transfer startX + if (component->fontStyle_.align == TEXT_ALIGNMENT_RIGHT) { + startPoint.x = startX - maxWidth; + } else if (component->fontStyle_.align == TEXT_ALIGNMENT_CENTER) { + startPoint.x = (startX + startX - maxWidth) / half; + } else { + startPoint.x = startX; + } + + component->canvas_.StrokeText(textValue, startPoint, component->fontStyle_, component->paint_); + return UNDEFINED; +} + jerry_value_t CanvasComponent::Rotate(const jerry_value_t func, const jerry_value_t dom, const jerry_value_t args[], const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_1) { + if (argsNum < ArgsCount::NUM_1) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of rotate method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("rotate method parameter error")); @@ -735,7 +1285,7 @@ jerry_value_t CanvasComponent::Rotate(const jerry_value_t func, reinterpret_cast("get canvas component from js object failed")); } - double angle = jerry_get_number_value(args[ArgsIndex::IDX_0]); + float angle = FloatOf(args[ArgsIndex::IDX_0]); component->paint_.Rotate(angle); return UNDEFINED; } @@ -746,7 +1296,7 @@ jerry_value_t CanvasComponent::Scale(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_2) { + if (argsNum < ArgsCount::NUM_2) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of scale method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("scale method parameter error")); @@ -759,19 +1309,57 @@ jerry_value_t CanvasComponent::Scale(const jerry_value_t func, reinterpret_cast("get canvas component from js object failed")); } - double scaleX = jerry_get_number_value(args[ArgsIndex::IDX_0]); - double scaleY = jerry_get_number_value(args[ArgsIndex::IDX_1]); + float scaleX = FloatOf(args[ArgsIndex::IDX_0]); + float scaleY = FloatOf(args[ArgsIndex::IDX_1]); component->paint_.Scale(scaleX, scaleY); return UNDEFINED; } +jerry_value_t CanvasComponent::MeasureText(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of measureText method parameter error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("measureText method parameter error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + char *textValue = MallocStringOf(args[ArgsIndex::IDX_0]); + if (textValue == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get text value failed"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("get text value failed")); + } + + if (IS_UNDEFINED(component->measureTextObject_)) { + component->measureTextObject_ = jerry_create_object(); + } + if (IS_UNDEFINED(component->measureTextWidthString_)) { + component->measureTextWidthString_ = jerry_create_string(reinterpret_cast("width")); + } + Point ponit = component->canvas_.MeasureText(textValue, component->fontStyle_); + jerry_set_property(component->measureTextObject_, component->measureTextWidthString_, ponit.x); + + ACE_FREE(textValue); + return component->measureTextObject_; +} + jerry_value_t CanvasComponent::Translate(const jerry_value_t func, const jerry_value_t dom, const jerry_value_t args[], const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_2) { + if (argsNum < ArgsCount::NUM_2) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of translate method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("translate method parameter error")); @@ -797,7 +1385,7 @@ jerry_value_t CanvasComponent::Transform(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_6) { + if (argsNum < ArgsCount::NUM_6) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of rotate method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("rotate method parameter error")); @@ -810,12 +1398,12 @@ jerry_value_t CanvasComponent::Transform(const jerry_value_t func, reinterpret_cast("get canvas component from js object failed")); } - double scaleX = jerry_get_number_value(args[ArgsIndex::IDX_0]); - double shearX = jerry_get_number_value(args[ArgsIndex::IDX_1]); - double shearY = jerry_get_number_value(args[ArgsIndex::IDX_2]); - double scaleY = jerry_get_number_value(args[ArgsIndex::IDX_3]); - double translateX = jerry_get_number_value(args[ArgsIndex::IDX_4]); - double translateY = jerry_get_number_value(args[ArgsIndex::IDX_5]); + float scaleX = FloatOf(args[ArgsIndex::IDX_0]); + float shearX = FloatOf(args[ArgsIndex::IDX_1]); + float shearY = FloatOf(args[ArgsIndex::IDX_2]); + float scaleY = FloatOf(args[ArgsIndex::IDX_3]); + int16_t translateX = IntegerOf(args[ArgsIndex::IDX_4]); + int16_t translateY = IntegerOf(args[ArgsIndex::IDX_5]); component->paint_.Transform(scaleX, shearX, shearY, scaleY, translateX, translateY); return UNDEFINED; } @@ -827,7 +1415,7 @@ jerry_value_t CanvasComponent::SetTransform(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_6) { + if (argsNum < ArgsCount::NUM_6) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of rotate method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("rotate method parameter error")); @@ -840,12 +1428,12 @@ jerry_value_t CanvasComponent::SetTransform(const jerry_value_t func, reinterpret_cast("get canvas component from js object failed")); } - double scaleX = jerry_get_number_value(args[ArgsIndex::IDX_0]); - double shearX = jerry_get_number_value(args[ArgsIndex::IDX_1]); - double shearY = jerry_get_number_value(args[ArgsIndex::IDX_2]); - double scaleY = jerry_get_number_value(args[ArgsIndex::IDX_3]); - double translateX = jerry_get_number_value(args[ArgsIndex::IDX_4]); - double translateY = jerry_get_number_value(args[ArgsIndex::IDX_5]); + float scaleX = FloatOf(args[ArgsIndex::IDX_0]); + float shearX = FloatOf(args[ArgsIndex::IDX_1]); + float shearY = FloatOf(args[ArgsIndex::IDX_2]); + float scaleY = FloatOf(args[ArgsIndex::IDX_3]); + int16_t translateX = IntegerOf(args[ArgsIndex::IDX_4]); + int16_t translateY = IntegerOf(args[ArgsIndex::IDX_5]); component->paint_.SetTransform(scaleX, shearX, shearY, scaleY, translateX, translateY); return UNDEFINED; } @@ -894,7 +1482,7 @@ jerry_value_t CanvasComponent::DrawCircle(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_3) { + if (argsNum < ArgsCount::NUM_3) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of arc method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("arc method parameter error")); @@ -944,7 +1532,7 @@ jerry_value_t CanvasComponent::MoveTo(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_2) { + if (argsNum < ArgsCount::NUM_2) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of moveTo method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("moveTo method parameter error")); @@ -973,7 +1561,7 @@ jerry_value_t CanvasComponent::LineTo(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_2) { + if (argsNum < ArgsCount::NUM_2) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of lineTo method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("lineTo method parameter error")); @@ -1002,7 +1590,7 @@ jerry_value_t CanvasComponent::Rect(const jerry_value_t func, const jerry_length_t argsNum) { (void)func; - if (argsNum != ArgsCount::NUM_4) { + if (argsNum < ArgsCount::NUM_4) { HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of rect method parameter error!"); return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("rect method parameter error")); @@ -1027,6 +1615,37 @@ jerry_value_t CanvasComponent::Rect(const jerry_value_t func, return UNDEFINED; } + +jerry_value_t CanvasComponent::CleanRect(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_4) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of rect method parameter error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("rect method parameter error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + int16_t x = IntegerOf(args[ArgsIndex::IDX_0]); + int16_t y = IntegerOf(args[ArgsIndex::IDX_1]); + int16_t width = IntegerOf(args[ArgsIndex::IDX_2]); + int16_t height = IntegerOf(args[ArgsIndex::IDX_3]); + + Point point; + point.x = x; + point.y = y; + component->canvas_.ClearRect(point, height, width); + return UNDEFINED; +} jerry_value_t CanvasComponent::Arc(const jerry_value_t func, const jerry_value_t dom, const jerry_value_t args[], @@ -1123,6 +1742,152 @@ jerry_value_t CanvasComponent::Stroke(const jerry_value_t func, return UNDEFINED; } +jerry_value_t CanvasComponent::CreateLInearGradient(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_4) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of createLinearGradient method parameter error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("createLinearGradient method param error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + float startX = FloatOf(args[ArgsIndex::IDX_0]); + float startY = FloatOf(args[ArgsIndex::IDX_1]); + float endX = FloatOf(args[ArgsIndex::IDX_2]); + float endY = FloatOf(args[ArgsIndex::IDX_3]); + component->paint_.createLinearGradient(startX, startY, endX, endY); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::CreateRadialGradient(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_6) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of createRadialGradient method parameter error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("createRadialGradient method param error")); + } + + CanvasComponent *component = static_cast(ComponentUtils:: + GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + float startX = FloatOf(args[ArgsIndex::IDX_0]); + float startY = FloatOf(args[ArgsIndex::IDX_1]); + float startR = FloatOf(args[ArgsIndex::IDX_2]); + float endX = FloatOf(args[ArgsIndex::IDX_3]); + float endY = FloatOf(args[ArgsIndex::IDX_4]); + float endR = FloatOf(args[ArgsIndex::IDX_5]); + component->paint_.createRadialGradient(startX, startY, startR, endX, endY, endR); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::AddColorStop(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_2) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of addColorStop method parameter error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("addColorStop method parameter error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + ACE_FREE(component->colorStopValue_); + float stop = FloatOf(args[ArgsIndex::IDX_0]); + component->colorStopValue_ = MallocStringOf(args[ArgsIndex::IDX_1]); + if (component->colorStopValue_ == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: colorStop value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("colorStop value erro!")); + } + + uint32_t color = 0; + uint8_t alpha = OPA_OPAQUE; + ParseColor(component->colorStopValue_, color, alpha); + ColorType colorStop = component->GetRGBColor(color); + colorStop.alpha = alpha; + component->paint_.addColorStop(stop, colorStop); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::CreatePattern(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + if (argsNum < ArgsCount::NUM_2) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: the number of createPattern method parameter error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("createPattern method parameter error")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + + ACE_FREE(component->patternPathValue_); + ACE_FREE(component->patternRepeatTypeValue_); + uint16_t patternRepeatTypeLength = 0; + component->patternPathValue_ = MallocStringOf(args[ArgsIndex::IDX_0]); + component->patternRepeatTypeValue_ = MallocStringOf(args[ArgsIndex::IDX_1], &patternRepeatTypeLength); + if (component->patternPathValue_ == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: patternPath value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast("patternPath value erro!")); + } + if (component->patternRepeatTypeValue_ == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: patternRepeatType value error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("patternRepeatType value erro!")); + } + uint16_t patternRepeatTypeId = KeyParser::ParseKeyId(component->patternRepeatTypeValue_, patternRepeatTypeLength); + if (IsFileExisted(component->patternPathValue_)) { + switch (patternRepeatTypeId) { + case K_NO_REPEAT: + component->paint_.CreatePattern(component->patternPathValue_, NO_REPEAT); + break; + case K_REPEAT: + component->paint_.CreatePattern(component->patternPathValue_, REPEAT); + break; + case K_REPEAT_X: + component->paint_.CreatePattern(component->patternPathValue_, REPEAT_X); + break; + case K_REPEAT_Y: + component->paint_.CreatePattern(component->patternPathValue_, REPEAT_Y); + break; + } + } + return UNDEFINED; +} + jerry_value_t CanvasComponent::Fill(const jerry_value_t func, const jerry_value_t dom, const jerry_value_t args[], @@ -1141,6 +1906,195 @@ jerry_value_t CanvasComponent::Fill(const jerry_value_t func, return UNDEFINED; } +jerry_value_t CanvasComponent::ParseImageName(const jerry_value_t args[], + char*& imageName, + int16_t &width, + int16_t &height) +{ + ImageComponent* imageCom = nullptr; + const char *src = nullptr; + char* imageNameBuf = nullptr; + // The parameter is the file name + if (jerry_value_is_string(args[ArgsIndex::IDX_0])) { + imageNameBuf = MallocStringOf(args[ArgsIndex::IDX_0]); + if (imageNameBuf == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get imageName value failed"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get imageName value failed")); + } + imageName = imageNameBuf; + return UNDEFINED; + // Parameters are objects retrieved from HML + } else if (jerry_value_is_object(args[ArgsIndex::IDX_0])) { + void *nativePtr = nullptr; + jerry_get_object_native_pointer(args[ArgsIndex::IDX_0], &nativePtr, nullptr); + imageCom = reinterpret_cast(nativePtr); + if (imageCom != nullptr) { + src = imageCom->GetSrc(); + if (src == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get imagePath value failed"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get imagePath value failed")); + } + size_t strLen = strlen(src) + 1; + imageName = static_cast(ace_malloc(strLen)); + if (memcpy_s(imageName, strLen, src, strLen) != EOK) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get imagePath value failed"); + return UNDEFINED; + } + return UNDEFINED; + } else { + // The argument is the object passed through the new Image() + imageName = GetImageObjectParam(args, width, height); + if (strlen(imageName) < 1) { + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get imageName value failed")); + } + return UNDEFINED; + } + } else { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get imageName value failed"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get imageName value failed")); + } + return UNDEFINED; +} + +char* CanvasComponent::GetImageObjectParam(const jerry_value_t args[], int16_t &width, int16_t &height) +{ + jerry_value_t src = jerryx_get_property_str(args[ArgsIndex::IDX_0], ImageModule::attrSrc); + char* srcVal = MallocStringOf(src); + jerry_value_t jWidth = jerryx_get_property_str(args[ArgsIndex::IDX_0], ImageModule::attrWidth); + jerry_value_t jHeight = jerryx_get_property_str(args[ArgsIndex::IDX_0], ImageModule::attrHeight); + width = IntegerOf(jWidth); + height = IntegerOf(jHeight); + return srcVal; +} + +jerry_value_t CanvasComponent::DrawImage(const jerry_value_t func, const jerry_value_t context, + const jerry_value_t args[], const jerry_length_t argsNum) +{ + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: DrawImage arg error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("canvas_component: DrawImage arg error!")); + } + char* imageName = nullptr; + int16_t startX = 0; + int16_t startY = 0; + int16_t width = -1; + int16_t height = -1; + if (ParseImageName(args, imageName, width, height) != UNDEFINED) { + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("parse Imagename function error")); + } + if (argsNum >= ArgsCount::NUM_2) { + startX = IntegerOf(args[ArgsIndex::IDX_1]); + } + if (argsNum >= ArgsCount::NUM_3) { + startY = IntegerOf(args[ArgsIndex::IDX_2]); + } + if (argsNum >= ArgsCount::NUM_4) { + width = IntegerOf(args[ArgsIndex::IDX_3]); + } + if (argsNum >= ArgsCount::NUM_5) { + height = IntegerOf(args[ArgsIndex::IDX_4]); + } + Point startLocat = {startX, startY}; + if (width > 0 || height > 0) { + component->canvas_.DrawImage(startLocat, imageName, component->paint_, width, height); + } else { + component->canvas_.DrawImage(startLocat, imageName, component->paint_); + } + ACE_FREE(imageName); + return UNDEFINED; +} + +jerry_value_t CanvasComponent::SetLineDash(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + if (argsNum < ArgsCount::NUM_1) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: SetLineDash arg error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("canvas_component: DrawImage arg error!")); + } + + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + jerry_value_t array = args[ArgsIndex::IDX_0]; + if (!jerry_value_is_array(array)) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: SetLineDash arg error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("canvas_component: DrawImage arg error!")); + } + uint32_t len = jerry_get_array_length(array); + const uint32_t maxSize = 1024; + if (len == 0 || len > maxSize * maxSize) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: len = 0 error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("canvas_component: len = 0 error!")); + } + float *buff = new float[len]; + if (buff == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: new buff error!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("canvas_component: new buff error!")); + } + for (uint32_t i = 0; i < len; i++) { + jerry_value_t val = jerry_get_property_by_index(array, i); + float num = 0; + if (jerry_value_is_number(val)) { + num = jerry_get_number_value(val); + } else if (jerry_value_is_string(val)) { + char *value = MallocStringOf(val); + num = atof(value); + ACE_FREE(value); + } else { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: SetLineDash arg error!"); + } + + buff[i] = num; + jerry_release_value(val); + } + + component->paint_.SetLineDash(buff, len); + delete[] buff; + return UNDEFINED; +} + +jerry_value_t CanvasComponent::GetLineDash(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + CanvasComponent *component = static_cast(ComponentUtils::GetComponentFromBindingObject(context)); + if (component == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "canvas_component: get canvas component from js object failed!"); + return jerry_create_error(JERRY_ERROR_TYPE, + reinterpret_cast("get canvas component from js object failed")); + } + float* pArr = component->paint_.GetLineDash(); + uint32_t length = component->paint_.GetLineDashCount(); + jerry_release_value(component->dashArray_); + component->dashArray_ = jerry_create_array(length); + for (uint32_t i = 0; i < length; i++) { + jerry_set_property_by_index(component->dashArray_, i, jerry_create_number(pArr[i])); + } + return component->dashArray_; +} + void CanvasComponent::GetSubFont(const char *font, const uint8_t index, char *&subFont) const { if (font == nullptr || *font == '\0') { diff --git a/frameworks/src/core/components/canvas_component.h b/frameworks/src/core/components/canvas_component.h index cdb04f0..2c27328 100644 --- a/frameworks/src/core/components/canvas_component.h +++ b/frameworks/src/core/components/canvas_component.h @@ -68,10 +68,10 @@ private: const char *drawMethodName, jerry_external_handler_t handler); - static jerry_value_t GetDom(const jerry_value_t func, - const jerry_value_t dom, - const jerry_value_t args[], - const jerry_length_t argsNum); + static jerry_value_t GetContext(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum); static jerry_value_t FillStyleSetter(const jerry_value_t func, const jerry_value_t dom, @@ -123,6 +123,46 @@ private: const jerry_value_t args[], const jerry_length_t argsNum); + static jerry_value_t LineCapSetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t LineCapGetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t LineJoinSetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t LineJoinGetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t MiterLimitSetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t MiterLimitGetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t LineDashOffsetSetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t LineDashOffsetGetter(const jerry_value_t func, + const jerry_value_t contex, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t GlobalAlphaSetter(const jerry_value_t func, const jerry_value_t dom, const jerry_value_t args[], @@ -133,6 +173,53 @@ private: const jerry_value_t args[], const jerry_length_t argsNum); + static jerry_value_t GlobalCompositeOperationSetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t ShadowOffsetXSetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t ShadowOffsetXGetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t ShadowOffsetYSetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t ShadowOffsetYGetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t ShadowColorSetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t ShadowColorGetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t ShadowBlurSetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t ShadowBlurGetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t GlobalCompositeOperationGetter(const jerry_value_t func, + const jerry_value_t dom, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t FillRect(const jerry_value_t func, const jerry_value_t dom, const jerry_value_t args[], @@ -193,6 +280,24 @@ private: const jerry_value_t args[], const jerry_length_t argsNum); + static jerry_value_t DrawImage(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t SetLineDash(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t GetLineDash(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + + static jerry_value_t StrokeText(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t Rotate(const jerry_value_t func, const jerry_value_t dom, const jerry_value_t args[], @@ -203,6 +308,11 @@ private: const jerry_value_t args[], const jerry_length_t argsNum); + static jerry_value_t MeasureText(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t Translate(const jerry_value_t func, const jerry_value_t dom, const jerry_value_t args[], @@ -233,6 +343,11 @@ private: const jerry_value_t args[], const jerry_length_t argsNum); + static jerry_value_t CreatePattern(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t Save(const jerry_value_t func, const jerry_value_t dom, const jerry_value_t args[], @@ -247,6 +362,11 @@ private: const jerry_value_t dom, const jerry_value_t args[], const jerry_length_t argsNum); + static jerry_value_t ParseImageName(const jerry_value_t args[], + char*& imageName, + int16_t &width, + int16_t &height); + static char* GetImageObjectParam(const jerry_value_t args[], int16_t &width, int16_t &height); UICanvas canvas_; jerry_value_t dom_; jerry_value_t dashArray_; @@ -258,14 +378,20 @@ private: char *strokeStyleValue_; char *fontValue_; char *textAlignValue_; + int16_t shadowOffsetXValue_; + int16_t shadowOffsetYValue_; + int16_t shadowBlurValue_; + char *shadowColorValue_; int16_t lineWidthValue_; char *lineCapValue_; char *lineJoinValue_; - double miterLimitValue_; + float miterLimitValue_; float lineDashOffsetValue_; char *colorStopValue_; + char *patternPathValue_; + char *patternRepeatTypeValue_; static const char * const DEFAULT_FILLSTYLE; static const char * const DEFAULT_STROKESTYLE; @@ -290,7 +416,11 @@ private: static const char * const ATTR_GLOBALALPHA; static const char * const ATTR_GLOBALCOMPOSITEOPERATION; - static const char * const FUNC_GETdom; + static const char * const ATTR_SHADOWOFFSETX; + static const char * const ATTR_SHADOWOFFSETY; + static const char * const ATTR_SHADOWBLUR; + static const char * const ATTR_SHADOWCOLOR; + static const char * const FUNC_GETCONTEXT; static const char * const FUNC_FILLRECT; static const char * const FUNC_STROKERECT; static const char * const FUNC_FILLTEXT; @@ -298,15 +428,18 @@ private: static const char * const FUNC_MOVETO; static const char * const FUNC_LINETO; static const char * const FUNC_RECT; + static const char * const FUNC_CLEANRECT; static const char * const FUNC_ARC; static const char * const FUNC_CLOSEPATH; static const char * const FUNC_STROKE; static const char * const FUNC_FILL; + static const char * const FUNC_DRAWIMAGE; static const char * const FUNC_SETLINEDASH; static const char * const FUNC_GETLINEDASH; static const char * const FUNC_STROKETEXT; static const char * const FUNC_ROTATE; static const char * const FUNC_SCALE; + static const char * const FUNC_MEASURETEXT; static const char * const FUNC_TRANSLATE; static const char * const FUNC_TRANFORM; static const char * const FUNC_SETTRANFORM; @@ -316,6 +449,7 @@ private: static const char * const FUNC_CREATELINEARGRADIENT; static const char * const FUNC_CREATERADIALGRADIENT; static const char * const FUNC_ADDCOLORSTOP; + static const char * const FUNC_CREATEPATTERN; }; } // namespace ACELite } // namespace OHOS diff --git a/frameworks/src/core/components/image_component.cpp b/frameworks/src/core/components/image_component.cpp index da63186..7ab65b3 100644 --- a/frameworks/src/core/components/image_component.cpp +++ b/frameworks/src/core/components/image_component.cpp @@ -32,6 +32,11 @@ ImageComponent::ImageComponent(jerry_value_t options, jerry_value_t children, Ap SetComponentName(K_IMAGE); } +const char *ImageComponent::GetSrc() +{ + return imageView_.GetPath(); +} + bool ImageComponent::CreateNativeViews() { // set default value @@ -57,6 +62,16 @@ bool ImageComponent::SetPrivateAttribute(uint16_t attrKeyId, jerry_value_t attrV ACE_FREE(src); break; } + case K_WIDTH: { + int16_t width = IntegerOf(attrValue); + imageView_.SetWidth(width); + break; + } + case K_HEIGHT: { + int16_t height = IntegerOf(attrValue); + imageView_.SetWidth(height); + break; + } default: setResult = false; break; @@ -95,11 +110,15 @@ bool ImageComponent::ApplyPrivateStyle(const AppStyleItem *style) break; } case K_HEIGHT: { + int16_t value = GetStyleNumValue(style); + imageView_.SetHeight(value); hasSetHeight_ = true; setResult = false; break; } case K_WIDTH: { + int16_t value = GetStyleNumValue(style); + imageView_.SetWidth(value); hasSetWidth_ = true; setResult = false; break; diff --git a/frameworks/src/core/modules/presets/image_module.cpp b/frameworks/src/core/modules/presets/image_module.cpp index be66567..0bc39a3 100644 --- a/frameworks/src/core/modules/presets/image_module.cpp +++ b/frameworks/src/core/modules/presets/image_module.cpp @@ -12,3 +12,323 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include + +#include "ace_log.h" +#include "locale_util.h" +#include "key_parser.h" +#include "keys.h" +#include "image.h" +#include "image_module.h" +namespace OHOS { +namespace ACELite { +constexpr jerry_object_native_info_t ImageModule::gcCallback; +const char * const ImageModule::attrOnload = "onload"; +const char * const ImageModule::attrOnError = "onerror"; +const char * const ImageModule::attrSrc = "src"; +const char * const ImageModule::attrWidth = "width"; +const char * const ImageModule::attrHeight = "height"; +const char * const ImageModule::className = "Image"; + +void ImageModule::Init(jerry_value_t intlHandle) +{ + (void)intlHandle; + jerry_value_t gObj = jerry_get_global_object(); + JerrySetFuncProperty(gObj, className, CreateImage); +} +void ImageModule::RegisterAttributeFunc(jerry_value_t canvas2dContext, + const char *attributeName, + jerry_external_handler_t setterHandler, + jerry_external_handler_t getterHandler) +{ + // register canvas js attribute setter property via the jerry_define_own_property method + jerry_value_t propName = jerry_create_string(reinterpret_cast(attributeName)); + jerry_property_descriptor_t desc; + jerry_init_property_descriptor_fields(&desc); + desc.is_set_defined = true; + desc.setter = jerry_create_external_function(setterHandler); + desc.is_get_defined = true; + desc.getter = jerry_create_external_function(getterHandler); + jerry_value_t returnValue = jerry_define_own_property(canvas2dContext, propName, &desc); + jerry_free_property_descriptor_fields(&desc); + ReleaseJerryValue(propName, returnValue, VA_ARG_END_FLAG); +} +jerry_value_t ImageModule::CreateImage(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + if (!jerry_value_is_constructor(func)) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("use new to create Image")); + } + ImageModule *imageModule = new ImageModule(); + if (imageModule == nullptr) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("memory is not enough")); + } + + imageModule->width_ = -1; + imageModule->height_ = -1; + if (argsNum >= 1) { + if (jerry_value_is_number(args[0])) { + imageModule->width_ = jerry_value_to_number(args[0]); + } else if (jerry_value_is_string(args[0])) { + char* val = MallocStringOf(args[0]); + imageModule->width_ = atoi(val); + ACE_FREE(val); + } + } + if (argsNum >= static_cast(ArgsCount::NUM_2)) { + if (jerry_value_is_number(args[1])) { + imageModule->height_ = jerry_value_to_number(args[1]); + } else if (jerry_value_is_string(args[1])) { + char* val = MallocStringOf(args[1]); + imageModule->height_ = atoi(val); + ACE_FREE(val); + } + } + + jerry_set_object_native_pointer(context, imageModule, &gcCallback); + RegisterAttributeFunc(context, attrWidth, OnLoadSetter, OnLoadGetter); + RegisterAttributeFunc(context, attrOnError, OnErrorSetter, OnErrorGetter); + RegisterAttributeFunc(context, attrSrc, OnSrcSetter, OnSrcGetter); + RegisterAttributeFunc(context, attrWidth, OnWidthSetter, OnWidthGetter); + RegisterAttributeFunc(context, attrHeight, OnHeightSetter, OnHeightGetter); + return UNDEFINED; +} + +jerry_value_t ImageModule::OnLoadSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + ImageModule* imageModel = nullptr; + jerry_get_object_native_pointer(context, (void**)&imageModel, &gcCallback); + if (imageModel == nullptr) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("get imageModel fail")); + } + if (argsNum == 1 && jerry_value_is_function(args[0])) { + jerry_release_value(imageModel->onLoadFunc_); + imageModel->onLoadFunc_ = args[0]; + } else { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("set imageModel onload arg fail")); + } + return UNDEFINED; +} + +jerry_value_t ImageModule::OnLoadGetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + ImageModule* imageModel = nullptr; + jerry_get_object_native_pointer(context, (void**)&imageModel, &gcCallback); + if (imageModel == nullptr) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("get imageModel fail")); + } + return imageModel->onLoadFunc_; +} + +jerry_value_t ImageModule::OnErrorSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + ImageModule* imageModel = nullptr; + jerry_get_object_native_pointer(context, (void**)&imageModel, &gcCallback); + if (imageModel == nullptr) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("get imageModel fail")); + } + if (argsNum >= 1 && jerry_value_is_function(args[0])) { + jerry_release_value(imageModel->onErrorFunc_); + imageModel->onErrorFunc_ = args[0]; + } else { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("set imageModel onload arg fail")); + } + return UNDEFINED; +} + +jerry_value_t ImageModule::OnErrorGetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + ImageModule* imageModel = nullptr; + jerry_get_object_native_pointer(context, (void**)&imageModel, &gcCallback); + if (imageModel == nullptr) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("get imageModel fail")); + } + return imageModel->onErrorFunc_; +} + +jerry_value_t ImageModule::OnSrcSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + ImageModule* imageModel = nullptr; + jerry_get_object_native_pointer(context, (void**)&imageModel, &gcCallback); + if (imageModel == nullptr) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("get imageModel fail")); + } + if (argsNum >= 1) { + ACE_FREE(imageModel->src_); + imageModel->src_ = MallocStringOf(args[0]); + } + if (imageModel->src_ == nullptr || strlen(imageModel->src_) == 0) { + OnCallBack(context, imageModel, false, "src is null"); + return UNDEFINED; + } + jerry_release_value(imageModel->jerrySrc_); + imageModel->jerrySrc_ = jerry_create_string(reinterpret_cast(imageModel->src_)); + Image image; + bool isLoad = image.PreParse(imageModel->src_); + if (!isLoad) { + OnCallBack(context, imageModel, false, "unknown image"); + return UNDEFINED; + } + OnCallBack(context, imageModel, true, "load sucess"); + return UNDEFINED; +} + +jerry_value_t ImageModule::OnSrcGetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + ImageModule* imageModel = nullptr; + jerry_get_object_native_pointer(context, (void**)&imageModel, &gcCallback); + if (imageModel == nullptr) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("get imageModel fail")); + } + if (jerry_value_is_string(imageModel->jerrySrc_)) { + return imageModel->jerrySrc_; + } + return UNDEFINED; +} + +jerry_value_t ImageModule::OnHeightSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + ImageModule* imageModel = nullptr; + jerry_get_object_native_pointer(context, (void**)&imageModel, &gcCallback); + if (imageModel == nullptr) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("get imageModel fail")); + } + if (argsNum < 1) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("set height fail")); + } + imageModel->height_ = IntegerOf(args[0]); + return UNDEFINED; +} + +jerry_value_t ImageModule::OnHeightGetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + ImageModule* imageModel = nullptr; + jerry_get_object_native_pointer(context, (void**)&imageModel, &gcCallback); + if (imageModel == nullptr) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("get imageModel fail")); + } + return imageModel->height_; +} + +jerry_value_t ImageModule::OnWidthSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + ImageModule* imageModel = nullptr; + jerry_get_object_native_pointer(context, (void**)&imageModel, &gcCallback); + if (imageModel == nullptr) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("get imageModel fail")); + } + if (argsNum < 1) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("set width fail")); + } + imageModel->width_ = IntegerOf(args[0]); + return UNDEFINED; +} + +jerry_value_t ImageModule::OnWidthGetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum) +{ + (void)func; + (void)args; + (void)argsNum; + ImageModule* imageModel = nullptr; + jerry_get_object_native_pointer(context, (void**)&imageModel, &gcCallback); + if (imageModel == nullptr) { + return jerry_create_error(JERRY_ERROR_EVAL, + reinterpret_cast("get imageModel fail")); + } + return imageModel->width_; +} + +void ImageModule::OnCallBack(const jerry_value_t context, + const ImageModule *imageModule, + bool isSucess, + const char* msg) +{ + jerry_value_t targetFunction; + if (imageModule == nullptr) { + return; + } + if (isSucess) { + targetFunction = imageModule->onLoadFunc_; + } else { + targetFunction = imageModule->onErrorFunc_; + } + if (!jerry_value_is_function(targetFunction)) { + return; + } + const int16_t argcount = 1; + jerry_value_t arg = jerry_create_string(reinterpret_cast(msg)); + jerry_value_t args[argcount] = {arg}; + jerry_value_t ret_val = jerry_call_function (targetFunction, context, args, argcount); + if (jerry_value_is_error (ret_val)) { + HILOG_ERROR(HILOG_MODULE_ACE, "call back failed!"); + } + jerry_release_value(arg); + jerry_release_value (ret_val); +} +} +} + diff --git a/frameworks/src/core/modules/presets/image_module.h b/frameworks/src/core/modules/presets/image_module.h index 14b2730..e03f628 100644 --- a/frameworks/src/core/modules/presets/image_module.h +++ b/frameworks/src/core/modules/presets/image_module.h @@ -16,4 +16,131 @@ #ifndef OHOS_ACELITE_IMAGE_MODEL_H #define OHOS_ACELITE_IMAGE_MODEL_H +#include "acelite_config.h" +#include "image_component.h" +#include "gfx_utils/list.h" +#include "js_fwk_common.h" +namespace OHOS { +namespace ACELite { +class ImageModule final : public MemoryHeap { +enum class ArgsCount { NUM_1 = 1, NUM_2, NUM_3, NUM_4, NUM_5, NUM_6 }; +public: + const char* GetSrc() + { + return src_; + } + + int16_t GetWidth() + { + return width_; + } + + int16_t GetHeight() + { + return height_; + } + + ACE_DISALLOW_COPY(ImageModule); + /** + * @brief register Image object + */ + static void Init(jerry_value_t intlHandle); + + static void RegisterAttributeFunc(jerry_value_t canvas2dContext, + const char *attributeName, + jerry_external_handler_t setterHandler, + jerry_external_handler_t getterHandler); +#if ENABLED_DELETE_MODULE_IAMGE + static void DeleteImage(void* ptr) + { + ImageModule* image = reinterpret_cast(ptr); + ACE_DELETE(image); + } #endif + + static void OnCallBack(const jerry_value_t context, const ImageModule *imageModule, bool isSucess, const char* msg); + // the handle to release the native value when the js value number format object is not needed. + static constexpr jerry_object_native_info_t gcCallback = { +#if ENABLED_DELETE_MODULE_IAMGE + .free_cb = DeleteImage +#endif + }; + static const char * const attrOnload; + static const char * const attrOnError; + static const char * const attrSrc; + static const char * const attrWidth; + static const char * const attrHeight; + static const char * const className; +private: + ImageModule() + : src_(nullptr), + width_(-1), + height_(-1), + onLoadFunc_(UNDEFINED), + onErrorFunc_(UNDEFINED), + jerrySrc_(UNDEFINED) {} + + ~ImageModule() + { + jerry_release_value(onLoadFunc_); + jerry_release_value(onErrorFunc_); + jerry_release_value(jerrySrc_); + ACE_FREE(src_); + } + + static jerry_value_t CreateImage(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t OnLoadSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t OnLoadGetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t OnErrorSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t OnErrorGetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t OnSrcSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t OnSrcGetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t OnHeightSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t OnHeightGetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t OnWidthSetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + static jerry_value_t OnWidthGetter(const jerry_value_t func, + const jerry_value_t context, + const jerry_value_t args[], + const jerry_length_t argsNum); + +private: + char* src_; + int16_t width_; + int16_t height_; + jerry_value_t onLoadFunc_; + jerry_value_t onErrorFunc_; + jerry_value_t jerrySrc_; +}; +} +} +#endif // NUMBER_FORMAT_MODEL_H diff --git a/frameworks/src/core/modules/presets/intl_module.cpp b/frameworks/src/core/modules/presets/intl_module.cpp index ee37360..59b97ae 100644 --- a/frameworks/src/core/modules/presets/intl_module.cpp +++ b/frameworks/src/core/modules/presets/intl_module.cpp @@ -20,6 +20,7 @@ #include "global.h" #include "string.h" #include "number_format_module.h" +#include "image_module.h" #include "js_fwk_common.h" namespace OHOS { namespace ACELite { @@ -33,6 +34,7 @@ void IntlModule::Init() #if (FEATURE_DATE_FORMAT == 1) DateTimeFormatModule::Init(baseObj); #endif + ImageModule::Init(baseObj); JerrySetNamedProperty(globalObj, "Intl", baseObj); jerry_release_value(globalObj); } diff --git a/frameworks/src/core/modules/presets/intl_module.h b/frameworks/src/core/modules/presets/intl_module.h index bcaa642..7f7d821 100644 --- a/frameworks/src/core/modules/presets/intl_module.h +++ b/frameworks/src/core/modules/presets/intl_module.h @@ -45,7 +45,6 @@ private: } } #endif // FEATURE_INTL_MODULE -#include "image_module.h" namespace OHOS { namespace ACELite { class IntlControlModule final {