1 /*
2 * Copyright (C) 2025 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 "ani_drawing_utils.h"
17
18 namespace OHOS {
19 namespace Rosen {
20 namespace Drawing {
21
AniThrowError(ani_env * env,const std::string & message)22 ani_status AniThrowError(ani_env* env, const std::string& message)
23 {
24 ani_class errCls;
25 const char* className = "Lescompat/Error;";
26 if (ANI_OK != env->FindClass(className, &errCls)) {
27 ROSEN_LOGE("Not found %{public}s", className);
28 return ANI_ERROR;
29 }
30
31 ani_method errCtor;
32 if (ANI_OK != env->Class_FindMethod(errCls, "<ctor>", "Lstd/core/String;Lescompat/ErrorOptions;:V", &errCtor)) {
33 ROSEN_LOGE("get errCtor Failed %{public}s", className);
34 return ANI_ERROR;
35 }
36
37 ani_object errObj;
38 if (ANI_OK != env->Object_New(errCls, errCtor, &errObj, CreateAniString(env, message))) {
39 ROSEN_LOGE("Create Object Failed %{public}s", className);
40 return ANI_ERROR;
41 }
42
43 env->ThrowError(static_cast<ani_error>(errObj));
44 return ANI_OK;
45 }
46
GetColorQuadFromColorObj(ani_env * env,ani_object obj,Drawing::ColorQuad & color)47 bool GetColorQuadFromColorObj(ani_env* env, ani_object obj, Drawing::ColorQuad &color)
48 {
49 ani_class colorClass;
50 env->FindClass("L@ohos/graphics/common2D/common2D/Color;", &colorClass);
51 ani_boolean isColorClass;
52 env->Object_InstanceOf(obj, colorClass, &isColorClass);
53
54 if (!isColorClass) {
55 return false;
56 }
57
58 ani_double alpha;
59 ani_double red;
60 ani_double green;
61 ani_double blue;
62
63 if ((env->Object_GetPropertyByName_Double(obj, "alpha", &alpha) != ANI_OK) ||
64 (env->Object_GetPropertyByName_Double(obj, "red", &red) != ANI_OK) ||
65 (env->Object_GetPropertyByName_Double(obj, "green", &green) != ANI_OK) ||
66 (env->Object_GetPropertyByName_Double(obj, "blue", &blue) != ANI_OK)) {
67 ROSEN_LOGE("GetColorQuadFromParam failed by Color class");
68 return false;
69 }
70
71 if (alpha < 0 || alpha > Color::RGB_MAX ||
72 red < 0 || red > Color::RGB_MAX ||
73 green < 0 || green > Color::RGB_MAX ||
74 blue < 0 || blue > Color::RGB_MAX) {
75 ROSEN_LOGE("GetColorQuadFromParam failed by Color class invaild value");
76 return false;
77 }
78
79 color = Color::ColorQuadSetARGB(static_cast<uint32_t>(alpha), static_cast<uint32_t>(red),
80 static_cast<uint32_t>(green), static_cast<uint32_t>(blue));
81 return true;
82 }
83
GetColorQuadFromParam(ani_env * env,ani_object obj,Drawing::ColorQuad & color)84 bool GetColorQuadFromParam(ani_env* env, ani_object obj, Drawing::ColorQuad &color)
85 {
86 ani_class doubleClass;
87 env->FindClass("Lstd/core/Double;", &doubleClass);
88
89 ani_boolean isNumber;
90 env->Object_InstanceOf(obj, doubleClass, &isNumber);
91 if (isNumber) {
92 ani_double aniColor;
93 if (ANI_OK != env->Object_CallMethodByName_Double(obj, "doubleValue", nullptr, &aniColor)) {
94 ROSEN_LOGE("GetColorQuadFromParam failed by double vaule");
95 return false;
96 }
97 color = static_cast<ColorQuad>(aniColor);
98 return true;
99 }
100
101 return GetColorQuadFromColorObj(env, obj, color);
102 }
103
GetRectFromAniRectObj(ani_env * env,ani_object obj,Drawing::Rect & rect)104 bool GetRectFromAniRectObj(ani_env* env, ani_object obj, Drawing::Rect& rect)
105 {
106 ani_class rectClass;
107 env->FindClass("L@ohos/graphics/common2D/common2D/Rect;", &rectClass);
108 ani_boolean isRectClass;
109 env->Object_InstanceOf(obj, rectClass, &isRectClass);
110
111 if (!isRectClass) {
112 return false;
113 }
114
115 ani_double left;
116 ani_double top;
117 ani_double right;
118 ani_double bottom;
119
120 if ((env->Object_GetPropertyByName_Double(obj, "left", &left) != ANI_OK) ||
121 (env->Object_GetPropertyByName_Double(obj, "top", &top) != ANI_OK) ||
122 (env->Object_GetPropertyByName_Double(obj, "right", &right) != ANI_OK) ||
123 (env->Object_GetPropertyByName_Double(obj, "bottom", &bottom) != ANI_OK)) {
124 ROSEN_LOGE("GetRectFromAniRectObj failed");
125 return false;
126 }
127
128 rect.SetLeft(left);
129 rect.SetTop(top);
130 rect.SetRight(right);
131 rect.SetBottom(bottom);
132 return true;
133 }
134
CreateAniUndefined(ani_env * env)135 ani_object CreateAniUndefined(ani_env* env)
136 {
137 ani_ref aniRef;
138 env->GetUndefined(&aniRef);
139 return static_cast<ani_object>(aniRef);
140 }
141
CreateAniObject(ani_env * env,const char * className,const char * methodSig,...)142 ani_object CreateAniObject(ani_env* env, const char* className, const char* methodSig, ...)
143 {
144 ani_class aniClass;
145 if (env->FindClass(className, &aniClass) != ANI_OK) {
146 ROSEN_LOGE("[Drawing] CreateAniObject FindClass failed");
147 return CreateAniUndefined(env);
148 }
149
150 ani_method aniConstructor;
151 if (env->Class_FindMethod(aniClass, "<ctor>", methodSig, &aniConstructor) != ANI_OK) {
152 ROSEN_LOGE("[Drawing] CreateAniObject Class_FindMethod failed");
153 return CreateAniUndefined(env);
154 }
155
156 ani_object aniObject;
157 va_list args;
158 va_start(args, methodSig);
159 if (env->Object_New_V(aniClass, aniConstructor, &aniObject, args) != ANI_OK) {
160 ROSEN_LOGE("[Drawing] CreateAniObject Object_New failed");
161 va_end(args);
162 return CreateAniUndefined(env);
163 }
164 va_end(args);
165
166 return aniObject;
167 }
168 } // namespace Drawing
169 } // namespace Rosen
170 } // namespace OHOS