• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "bridge/declarative_frontend/jsview/js_canvas_gradient.h"
17 #include "bridge/declarative_frontend/jsview/js_rendering_context.h"
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19 
20 namespace OHOS::Ace::Framework {
21 
JSCanvasGradient()22 JSCanvasGradient::JSCanvasGradient() : isColorStopValid_(true) {}
23 
Constructor(const JSCallbackInfo & args)24 void JSCanvasGradient::Constructor(const JSCallbackInfo& args)
25 {
26     auto jsCanvasGradient = Referenced::MakeRefPtr<JSCanvasGradient>();
27     jsCanvasGradient->IncRefCount();
28     args.SetReturnValue(Referenced::RawPtr(jsCanvasGradient));
29 }
30 
Destructor(JSCanvasGradient * controller)31 void JSCanvasGradient::Destructor(JSCanvasGradient* controller)
32 {
33     if (controller != nullptr) {
34         controller->DecRefCount();
35         delete controller->GetGradient();
36     }
37 }
38 
JSBind(BindingTarget globalObj)39 void JSCanvasGradient::JSBind(BindingTarget globalObj)
40 {
41     JSClass<JSCanvasGradient>::Declare("CanvasGradient");
42     JSClass<JSCanvasGradient>::CustomMethod("addColorStop", &JSCanvasGradient::addColorStop);
43     JSClass<JSCanvasGradient>::Bind(globalObj, JSCanvasGradient::Constructor, JSCanvasGradient::Destructor);
44 }
45 
addColorStop(const JSCallbackInfo & info)46 void JSCanvasGradient::addColorStop(const JSCallbackInfo& info)
47 {
48     if (!isColorStopValid_ && gradient_->GetColors().empty()) {
49         isColorStopValid_ = true;
50     }
51     if (isColorStopValid_ && info[0]->IsNumber() && info[1]->IsString()) {
52         double offset = 0.0;
53         JSViewAbstract::ParseJsDouble(info[0], offset);
54         if (offset < 0 || offset > 1) {
55             LOGE("offset not valid!");
56             isColorStopValid_ = false;
57             // if the offset is invalid, fill the shape with transparent
58             gradient_->ClearColors();
59             GradientColor color;
60             color.SetColor(Color::TRANSPARENT);
61             color.SetDimension(0.0);
62             gradient_->AddColor(color);
63             gradient_->AddColor(color);
64             return;
65         }
66         std::string jsColor;
67         GradientColor color;
68         JSViewAbstract::ParseJsString(info[1], jsColor);
69         color.SetColor(Color::FromString(jsColor));
70         color.SetDimension(offset);
71         if (gradient_) {
72             gradient_->AddColor(color);
73         }
74         auto colorSize = gradient_->GetColors().size();
75         // prevent setting only one colorStop
76         if (colorSize == 1) {
77             gradient_->AddColor(color);
78         }
79     }
80 }
81 
82 } // namespace OHOS::Ace::Framework
83