• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_JS_DRAWING_UTILS_H
17 #define OHOS_JS_DRAWING_UTILS_H
18 
19 #include <map>
20 #if defined(ROSEN_OHOS) || defined(ROSEN_ARKUI_X)
21 #include "hilog/log.h"
22 #endif
23 
24 #include "common/rs_common_def.h"
25 #include "draw/color.h"
26 #include "draw/shadow.h"
27 #include "native_engine/native_engine.h"
28 #include "native_engine/native_value.h"
29 #if defined(ROSEN_OHOS) || defined(ROSEN_ARKUI_X)
30 #include "pixel_map.h"
31 #include "pixel_map_napi.h"
32 #endif
33 #include "text/font.h"
34 #include "text/font_mgr.h"
35 #include "text/font_metrics.h"
36 #include "text/font_types.h"
37 #include "utils/point.h"
38 #include "utils/point3.h"
39 #include "utils/rect.h"
40 
41 namespace OHOS::Rosen {
42 #if defined(ROSEN_OHOS) || defined(ROSEN_ARKUI_X)
43 using namespace Media;
44 #endif
45 // used for test
46 class JsDrawingTestUtils {
47 public:
GetDrawingTestDisabled()48     static bool GetDrawingTestDisabled() { return closeDrawingTest_; }
49 private:
50     static bool closeDrawingTest_;
51 };
52 
53 #ifdef JS_DRAWING_TEST
54 #define JS_CALL_DRAWING_FUNC(func)                                  \
55     do {                                                            \
56         if (LIKELY(JsDrawingTestUtils::GetDrawingTestDisabled())) { \
57             func;                                                   \
58         }                                                           \
59     } while (0)
60 #else
61 #define JS_CALL_DRAWING_FUNC(func)           \
62     do {                                     \
63         func;                                \
64     } while (0)
65 #endif
66 
67 #define CHECK_PARAM_NUMBER_WITH_OPTIONAL_PARAMS(argv, argc, minNum, maxNum)                                            \
68     do {                                                                                                               \
69         if (napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok || argc < minNum || argc > maxNum) { \
70             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
71                 std::string("Incorrect number of ") + __FUNCTION__ + " parameters.");                                  \
72         }                                                                                                              \
73     } while (0)
74 
75 #define CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, paramNum)                                                     \
76     do {                                                                                                               \
77         size_t argc = paramNum;                                                                                        \
78         if (napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok || argc != paramNum) {               \
79             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
80                 std::string("Incorrect number of ") + __FUNCTION__ + " parameters.");                                  \
81         }                                                                                                              \
82     } while (0)
83 
84 #define GET_DOUBLE_PARAM(argc, value)                                                                                  \
85     do {                                                                                                               \
86         if (napi_get_value_double(env, argv[argc], &value) != napi_ok) {                                               \
87             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
88                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");            \
89         }                                                                                                              \
90     } while (0)
91 
92 #define GET_UINT32_PARAM(argc, value)                                                                                  \
93     do {                                                                                                               \
94         if (napi_get_value_uint32(env, argv[argc], &value) != napi_ok) {                                               \
95             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
96                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");            \
97         }                                                                                                              \
98     } while (0)
99 
100 // get int32 number and check >= 0
101 #define GET_INT32_CHECK_GE_ZERO_PARAM(argc, value)                                                                     \
102     do {                                                                                                               \
103         if (napi_get_value_int32(env, argv[argc], &value) != napi_ok || value < 0) {                                   \
104             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
105                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");            \
106         }                                                                                                              \
107     } while (0)
108 
109 #define GET_DOUBLE_CHECK_GT_ZERO_PARAM(argc, value)                                                                    \
110     do {                                                                                                               \
111         if (napi_get_value_double(env, argv[argc], &value) != napi_ok) {                                               \
112             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
113                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");            \
114         }                                                                                                              \
115         if (value <= 0.0) {                                                                                            \
116             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
117                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) +                       \
118                 " range. It should be greater than 0.");                                                               \
119         }                                                                                                              \
120     } while (0)
121 
122 #define GET_INT32_PARAM(argc, value)                                                                                   \
123     do {                                                                                                               \
124         if (napi_get_value_int32(env, argv[argc], &value) != napi_ok) {                                                \
125             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
126                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");            \
127         }                                                                                                              \
128     } while (0)
129 
130 #define GET_COLOR_PARAM(argc, value)                                                                                   \
131     do {                                                                                                               \
132         if (napi_get_value_int32(env, argv[argc], &value) != napi_ok ||  value < 0 ||  value > 255) {                  \
133             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
134                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");            \
135         }                                                                                                              \
136     } while (0)
137 
138 #define GET_BOOLEAN_PARAM(argc, value)                                                                                 \
139     do {                                                                                                               \
140         if (napi_get_value_bool(env, argv[argc], &value) != napi_ok) {                                                 \
141             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
142                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");            \
143         }                                                                                                              \
144     } while (0)
145 
146 #define GET_UNWRAP_PARAM(argc, value)                                                                                  \
147     do {                                                                                                               \
148         if ((napi_unwrap(env, argv[argc], reinterpret_cast<void**>(&value)) != napi_ok) || value == nullptr) {         \
149             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
150                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");            \
151         }                                                                                                              \
152     } while (0)
153 
154 #define GET_UNWRAP_PARAM_OR_NULL(argc, value)                                                                          \
155     do {                                                                                                               \
156         napi_valuetype valueType = napi_undefined;                                                                     \
157         if (napi_typeof(env, argv[argc], &valueType) != napi_ok) {                                                     \
158             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
159                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");            \
160         }                                                                                                              \
161         if (valueType != napi_null && valueType != napi_object) {                                                      \
162             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
163                 std::string("Incorrect valueType ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");  \
164         }                                                                                                              \
165         if (valueType == napi_object && napi_unwrap(env, argv[argc], reinterpret_cast<void**>(&value)) != napi_ok) {   \
166             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
167                 std::string("Incorrect unwrap ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");     \
168         }                                                                                                              \
169     } while (0)
170 
171 #define GET_JSVALUE_PARAM(argc, value)                                                                                 \
172     do {                                                                                                               \
173         if (!ConvertFromJsValue(env, argv[argc], value)) {                                                             \
174             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
175                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " type.");            \
176         }                                                                                                              \
177     } while (0)
178 
179 #define GET_ENUM_PARAM(argc, value, lo, hi)                                                                            \
180     do {                                                                                                               \
181         GET_INT32_PARAM(argc, value);                                                                                  \
182         if (value < lo || value > hi) {                                                                                \
183             return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,                                          \
184                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " range.");           \
185         }                                                                                                              \
186     } while (0)
187 
188 #define GET_ENUM_PARAM_RANGE(argc, value, lo, hi)                                                                      \
189     do {                                                                                                               \
190         GET_INT32_PARAM(argc, value);                                                                                  \
191         if (value < lo || value > hi) {                                                                                \
192             return NapiThrowError(env, DrawingErrorCode::ERROR_PARAM_VERIFICATION_FAILED,                              \
193                 std::string("Incorrect ") + __FUNCTION__ + " parameter" + std::to_string(argc) + " range.");           \
194         }                                                                                                              \
195     } while (0)
196 
197 namespace Drawing {
198 constexpr char THEME_FONT[] = "OhosThemeFont";
199 constexpr size_t ARGC_ZERO = 0;
200 constexpr size_t ARGC_ONE = 1;
201 constexpr size_t ARGC_TWO = 2;
202 constexpr size_t ARGC_THREE = 3;
203 constexpr size_t ARGC_FOUR = 4;
204 constexpr size_t ARGC_FIVE = 5;
205 constexpr size_t ARGC_SIX = 6;
206 constexpr size_t ARGC_SEVEN = 7;
207 constexpr size_t ARGC_EIGHT = 8;
208 constexpr size_t ARGC_NINE = 9;
209 constexpr int NUMBER_TWO = 2;
210 constexpr int MAX_PAIRS_PATHVERB = 4;
211 constexpr int MAX_ELEMENTSIZE = 3000 * 3000;
212 extern const char* const JSCOLOR[4];
213 
214 enum class DrawingErrorCode : int32_t {
215     OK = 0,
216     ERROR_NO_PERMISSION = 201, // the value do not change. It is defined on all system
217     ERROR_INVALID_PARAM = 401, // the value do not change. It is defined on all system
218     ERROR_DEVICE_NOT_SUPPORT = 801, // the value do not change. It is defined on all system
219     ERROR_PARAM_VERIFICATION_FAILED = 25900001, // after api 18, no throw 401
220 };
221 
222 template<class T>
223 T* CheckParamsAndGetThis(const napi_env env, napi_callback_info info, const char* name = nullptr)
224 {
225     if (env == nullptr || info == nullptr) {
226         return nullptr;
227     }
228     napi_value object = nullptr;
229     napi_value propertyNameValue = nullptr;
230     napi_value pointerValue = nullptr;
231     napi_get_cb_info(env, info, nullptr, nullptr, &object, nullptr);
232     if (object != nullptr && name != nullptr) {
233         napi_create_string_utf8(env, name, NAPI_AUTO_LENGTH, &propertyNameValue);
234     }
235     napi_value& resObject = propertyNameValue ? propertyNameValue : object;
236     if (resObject) {
237         return napi_unwrap(env, resObject, (void **)(&pointerValue)) == napi_ok ?
238             reinterpret_cast<T*>(pointerValue) : nullptr;
239     }
240     return nullptr;
241 }
242 
243 template<typename T, size_t N>
ArraySize(T (&)[N])244 inline constexpr size_t ArraySize(T (&)[N]) noexcept
245 {
246     return N;
247 }
248 
CreateJsUndefined(napi_env env)249 inline napi_value CreateJsUndefined(napi_env env)
250 {
251     napi_value result = nullptr;
252     napi_get_undefined(env, &result);
253     return result;
254 }
255 
CreateJsNull(napi_env env)256 inline napi_value CreateJsNull(napi_env env)
257 {
258     napi_value result = nullptr;
259     napi_get_null(env, &result);
260     return result;
261 }
262 
CreateJsNumber(napi_env env,int32_t value)263 inline napi_value CreateJsNumber(napi_env env, int32_t value)
264 {
265     napi_value result = nullptr;
266     napi_create_int32(env, value, &result);
267     return result;
268 }
269 
CreateJsNumber(napi_env env,uint32_t value)270 inline napi_value CreateJsNumber(napi_env env, uint32_t value)
271 {
272     napi_value result = nullptr;
273     napi_create_uint32(env, value, &result);
274     return result;
275 }
276 
CreateJsNumber(napi_env env,int64_t value)277 inline napi_value CreateJsNumber(napi_env env, int64_t value)
278 {
279     napi_value result = nullptr;
280     napi_create_int64(env, value, &result);
281     return result;
282 }
283 
CreateJsNumber(napi_env env,double value)284 inline napi_value CreateJsNumber(napi_env env, double value)
285 {
286     napi_value result = nullptr;
287     napi_create_double(env, value, &result);
288     return result;
289 }
290 
291 template<class T>
CreateJsValue(napi_env env,const T & value)292 napi_value CreateJsValue(napi_env env, const T& value)
293 {
294     using ValueType = std::remove_cv_t<std::remove_reference_t<T>>;
295     napi_value result = nullptr;
296     if constexpr (std::is_same_v<ValueType, bool>) {
297         napi_get_boolean(env, value, &result);
298         return result;
299     } else if constexpr (std::is_arithmetic_v<ValueType>) {
300         return CreateJsNumber(env, value);
301     } else if constexpr (std::is_same_v<ValueType, std::string>) {
302         napi_create_string_utf8(env, value.c_str(), value.length(), &result);
303         return result;
304     } else if constexpr (std::is_enum_v<ValueType>) {
305         return CreateJsNumber(env, static_cast<std::make_signed_t<ValueType>>(value));
306     } else if constexpr (std::is_same_v<ValueType, const char*>) {
307         (value != nullptr) ? napi_create_string_utf8(env, value, strlen(value), &result) :
308             napi_get_undefined(env, &result);
309         return result;
310     }
311 }
312 
ConvertFromJsNumber(napi_env env,napi_value jsValue,int32_t & value)313 inline bool ConvertFromJsNumber(napi_env env, napi_value jsValue, int32_t& value)
314 {
315     return napi_get_value_int32(env, jsValue, &value) == napi_ok;
316 }
317 
ConvertFromJsNumber(napi_env env,napi_value jsValue,uint32_t & value)318 inline bool ConvertFromJsNumber(napi_env env, napi_value jsValue, uint32_t& value)
319 {
320     return napi_get_value_uint32(env, jsValue, &value) == napi_ok;
321 }
322 
ConvertFromJsNumber(napi_env env,napi_value jsValue,int64_t & value)323 inline bool ConvertFromJsNumber(napi_env env, napi_value jsValue, int64_t& value)
324 {
325     return napi_get_value_int64(env, jsValue, &value) == napi_ok;
326 }
327 
ConvertFromJsNumber(napi_env env,napi_value jsValue,double & value)328 inline bool ConvertFromJsNumber(napi_env env, napi_value jsValue, double& value)
329 {
330     return napi_get_value_double(env, jsValue, &value) == napi_ok;
331 }
332 
ConvertFromJsNumber(napi_env env,napi_value jsValue,bool & value)333 inline bool ConvertFromJsNumber(napi_env env, napi_value jsValue, bool& value)
334 {
335     return napi_get_value_bool(env, jsValue, &value) == napi_ok;
336 }
337 
338 template<class T>
ConvertFromJsValue(napi_env env,napi_value jsValue,T & value)339 bool ConvertFromJsValue(napi_env env, napi_value jsValue, T& value)
340 {
341     if (jsValue == nullptr) {
342         return false;
343     }
344 
345     using ValueType = std::remove_cv_t<std::remove_reference_t<T>>;
346     if constexpr (std::is_same_v<ValueType, bool>) {
347         return napi_get_value_bool(env, jsValue, &value) == napi_ok;
348     } else if constexpr (std::is_arithmetic_v<ValueType>) {
349         return ConvertFromJsNumber(env, jsValue, value);
350     } else if constexpr (std::is_same_v<ValueType, std::string>) {
351         size_t len = 0;
352         if (napi_get_value_string_utf8(env, jsValue, nullptr, 0, &len) != napi_ok) {
353             return false;
354         }
355         auto buffer = std::make_unique<char[]>(len + 1);
356         size_t strLength = 0;
357         if (napi_get_value_string_utf8(env, jsValue, buffer.get(), len + 1, &strLength) == napi_ok) {
358             value = buffer.get();
359             return true;
360         }
361         return false;
362     } else if constexpr (std::is_enum_v<ValueType>) {
363         std::make_signed_t<ValueType> numberValue = 0;
364         if (!ConvertFromJsNumber(env, jsValue, numberValue)) {
365             return false;
366         }
367         value = static_cast<ValueType>(numberValue);
368         return true;
369     }
370     return false;
371 }
372 
373 bool ConvertFromJsColor(napi_env env, napi_value jsValue, int32_t* argb, size_t size);
374 
375 bool ConvertFromJsColor4F(napi_env env, napi_value jsValue, double* argbF, size_t size);
376 
377 bool ConvertFromAdaptHexJsColor(napi_env env, napi_value jsValue, Drawing::ColorQuad& jsColor);
378 
379 bool ConvertFromAdaptJsColor4F(napi_env env, napi_value jsValue, Drawing::Color4f& jsColor4F);
380 
381 bool ConvertFromJsRect(napi_env env, napi_value jsValue, double* ltrb, size_t size);
382 
383 bool ConvertFromJsIRect(napi_env env, napi_value jsValue, int32_t* ltrb, size_t size);
384 
385 bool ConvertFromJsPoint(napi_env env, napi_value jsValue, double* point, size_t size);
386 
387 bool ConvertFromJsPoint3d(napi_env env, napi_value src, Point3& point3d);
388 
389 bool ConvertFromJsShadowFlag(
390     napi_env env, napi_value src, ShadowFlags& shadowFlag, ShadowFlags defaultFlag = ShadowFlags::NONE);
391 
ConvertFromJsNumber(napi_env env,napi_value jsValue,int32_t & value,int32_t lo,int32_t hi)392 inline bool ConvertFromJsNumber(napi_env env, napi_value jsValue, int32_t& value, int32_t lo, int32_t hi)
393 {
394     return napi_get_value_int32(env, jsValue, &value) == napi_ok && value >= lo && value <= hi;
395 }
396 
GetDrawingPointXFromJsNumber(napi_env env,napi_value argValue,Drawing::Point & point)397 inline bool GetDrawingPointXFromJsNumber(napi_env env, napi_value argValue, Drawing::Point& point)
398 {
399     napi_value objValue = nullptr;
400     double targetX = 0;
401     if (napi_get_named_property(env, argValue, "x", &objValue) != napi_ok ||
402         napi_get_value_double(env, objValue, &targetX) != napi_ok) {
403         return false;
404     }
405     point.SetX(targetX);
406     return true;
407 }
408 
GetDrawingPointYFromJsNumber(napi_env env,napi_value argValue,Drawing::Point & point)409 inline bool GetDrawingPointYFromJsNumber(napi_env env, napi_value argValue, Drawing::Point& point)
410 {
411     napi_value objValue = nullptr;
412     double targetY = 0;
413     if (napi_get_named_property(env, argValue, "y", &objValue) != napi_ok ||
414         napi_get_value_double(env, objValue, &targetY) != napi_ok) {
415         return false;
416     }
417     point.SetY(targetY);
418     return true;
419 }
420 
GetDrawingPointFromJsValue(napi_env env,napi_value argValue,Drawing::Point & point)421 inline bool GetDrawingPointFromJsValue(napi_env env, napi_value argValue, Drawing::Point& point)
422 {
423     return GetDrawingPointXFromJsNumber(env, argValue, point) &&
424            GetDrawingPointYFromJsNumber(env, argValue, point);
425 }
426 
427 bool ConvertFromJsPointsArray(napi_env env, napi_value array, Drawing::Point* points, uint32_t count);
428 
429 bool ConvertFromJsPointsArrayOffset(napi_env env, napi_value array, Drawing::Point* points,
430     uint32_t count, uint32_t offset);
431 
GetDoubleAndConvertToJsValue(napi_env env,double d)432 inline napi_value GetDoubleAndConvertToJsValue(napi_env env, double d)
433 {
434     napi_value value = nullptr;
435     (void)napi_create_double(env, d, &value);
436     return value;
437 }
438 
GetStringAndConvertToJsValue(napi_env env,std::string str)439 inline napi_value GetStringAndConvertToJsValue(napi_env env, std::string str)
440 {
441     napi_value objValue = nullptr;
442     napi_create_string_utf8(env, str.c_str(), str.length(), &objValue);
443     return objValue;
444 }
445 
446 napi_value GetFontMetricsAndConvertToJsValue(napi_env env, FontMetrics* metrics);
447 
LtrbConvertToJsRect(napi_env env,napi_value jsValue,double left,double top,double right,double bottom)448 inline void LtrbConvertToJsRect(napi_env env, napi_value jsValue, double left, double top, double right, double bottom)
449 {
450     napi_set_named_property(env, jsValue, "left", CreateJsNumber(env, left));
451     napi_set_named_property(env, jsValue, "top", CreateJsNumber(env, top));
452     napi_set_named_property(env, jsValue, "right", CreateJsNumber(env, right));
453     napi_set_named_property(env, jsValue, "bottom", CreateJsNumber(env, bottom));
454 }
455 
DrawingRectConvertToJsRect(napi_env env,napi_value jsValue,const Rect & rect)456 inline void DrawingRectConvertToJsRect(napi_env env, napi_value jsValue, const Rect& rect)
457 {
458     LtrbConvertToJsRect(env, jsValue, rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom());
459 }
460 
GetRectAndConvertToJsValue(napi_env env,double left,double top,double right,double bottom)461 inline napi_value GetRectAndConvertToJsValue(napi_env env, double left, double top, double right, double bottom)
462 {
463     napi_value objValue = nullptr;
464     napi_create_object(env, &objValue);
465     if (objValue != nullptr) {
466         LtrbConvertToJsRect(env, objValue, left, top, right, bottom);
467     }
468     return objValue;
469 }
470 
GetRectAndConvertToJsValue(napi_env env,const Rect & rect)471 inline napi_value GetRectAndConvertToJsValue(napi_env env, const Rect& rect)
472 {
473     return GetRectAndConvertToJsValue(env, rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom());
474 }
475 
ConvertPointToJsValue(napi_env env,const Drawing::Point & point)476 inline napi_value ConvertPointToJsValue(napi_env env, const Drawing::Point& point)
477 {
478     napi_value objValue = nullptr;
479     napi_create_object(env, &objValue);
480     if (objValue != nullptr) {
481         if (napi_set_named_property(env, objValue, "x", CreateJsNumber(env, point.GetX())) != napi_ok ||
482             napi_set_named_property(env, objValue, "y", CreateJsNumber(env, point.GetY())) != napi_ok) {
483             return nullptr;
484         }
485     }
486     return objValue;
487 }
488 
NapiGetUndefined(napi_env env)489 inline napi_value NapiGetUndefined(napi_env env)
490 {
491     napi_value result = nullptr;
492     napi_get_undefined(env, &result);
493     return result;
494 }
495 
496 void BindNativeFunction(napi_env env, napi_value object, const char* name, const char* moduleName, napi_callback func);
497 napi_value CreateJsError(napi_env env, int32_t errCode, const std::string& message);
498 
499 bool ConvertFromJsTextEncoding(napi_env env, TextEncoding& textEncoding, napi_value nativeType);
500 
501 void MakeFontFeaturesFromJsArray(napi_env env, std::shared_ptr<DrawingFontFeatures> features,
502     uint32_t size, napi_value& array);
503 
GetColorAndConvertToJsValue(napi_env env,const Color & color)504 inline napi_value GetColorAndConvertToJsValue(napi_env env, const Color& color)
505 {
506     napi_value objValue = nullptr;
507     napi_create_object(env, &objValue);
508     if (objValue != nullptr) {
509         napi_set_named_property(env, objValue, "alpha", CreateJsNumber(env, color.GetAlpha()));
510         napi_set_named_property(env, objValue, "red", CreateJsNumber(env, color.GetRed()));
511         napi_set_named_property(env, objValue, "green", CreateJsNumber(env, color.GetGreen()));
512         napi_set_named_property(env, objValue, "blue", CreateJsNumber(env, color.GetBlue()));
513     }
514     return objValue;
515 }
516 
GetColor4FAndConvertToJsValue(napi_env env,const Color4f & color4f)517 inline napi_value GetColor4FAndConvertToJsValue(napi_env env, const Color4f& color4f)
518 {
519     napi_value objValue = nullptr;
520     napi_create_object(env, &objValue);
521     if (objValue != nullptr) {
522         napi_set_named_property(env, objValue, "alpha", CreateJsNumber(env, color4f.alphaF_));
523         napi_set_named_property(env, objValue, "red", CreateJsNumber(env, color4f.redF_));
524         napi_set_named_property(env, objValue, "green", CreateJsNumber(env, color4f.greenF_));
525         napi_set_named_property(env, objValue, "blue", CreateJsNumber(env, color4f.blueF_));
526     }
527     return objValue;
528 }
529 
530 napi_value NapiThrowError(napi_env env, DrawingErrorCode err, const std::string& message);
531 
532 std::shared_ptr<Font> GetThemeFont(std::shared_ptr<Font> font);
533 std::shared_ptr<Font> MatchThemeFont(std::shared_ptr<Font> font, int32_t unicode);
534 std::shared_ptr<FontMgr> GetFontMgr(std::shared_ptr<Font> font);
535 class Bitmap;
536 class ColorSpace;
537 #if defined(ROSEN_OHOS) || defined(ROSEN_ARKUI_X)
538 extern std::shared_ptr<Drawing::ColorSpace> ColorSpaceToDrawingColorSpace(Media::ColorSpace colorSpace);
539 extern Drawing::ColorType PixelFormatToDrawingColorType(Media::PixelFormat pixelFormat);
540 extern Drawing::AlphaType AlphaTypeToDrawingAlphaType(Media::AlphaType alphaType);
541 extern bool ExtracetDrawingBitmap(std::shared_ptr<Media::PixelMap> pixelMap, Drawing::Bitmap& bitmap);
542 extern std::shared_ptr<Drawing::Image> ExtractDrawingImage(std::shared_ptr<Media::PixelMap> pixelMap);
543 #endif
544 } // namespace Drawing
545 } // namespace OHOS::Rosen
546 
547 #if defined(ROSEN_OHOS) || defined(ROSEN_ARKUI_X)
548 
549 #undef LOG_DOMAIN
550 #define LOG_DOMAIN 0xD001400
551 
552 #undef LOG_TAG
553 #define LOG_TAG "JsDrawing"
554 
555 #define ROSEN_LOGI(format, ...)              \
556     HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__)
557 #define ROSEN_LOGD(format, ...)               \
558     HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__)
559 #define ROSEN_LOGE(format, ...)               \
560     HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__)
561 #else
562 #define ROSEN_LOGI(format, ...)
563 #define ROSEN_LOGD(format, ...)
564 #define ROSEN_LOGE(format, ...)
565 #endif
566 
567 #endif // OHOS_JS_DRAWING_UTILS_H