• 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 #include "js_drawing_utils.h"
17 
18 namespace OHOS::Rosen {
19 namespace Drawing {
BindNativeFunction(napi_env env,napi_value object,const char * name,const char * moduleName,napi_callback func)20 void BindNativeFunction(napi_env env, napi_value object, const char* name, const char* moduleName, napi_callback func)
21 {
22     std::string fullName;
23     if (moduleName) {
24         fullName = moduleName;
25         fullName += '.';
26     }
27     fullName += name;
28     napi_value funcValue = nullptr;
29     napi_create_function(env, fullName.c_str(), fullName.size(), func, nullptr, &funcValue);
30     napi_set_named_property(env, object, fullName.c_str(), funcValue);
31 }
32 
CreateJsError(napi_env env,int32_t errCode,const std::string & message)33 napi_value CreateJsError(napi_env env, int32_t errCode, const std::string& message)
34 {
35     napi_value result = nullptr;
36     napi_create_error(env, CreateJsValue(env, errCode), CreateJsValue(env, message), &result);
37     return result;
38 }
39 
ConvertFromJsTextEncoding(napi_env env,TextEncoding & textEncoding,napi_value nativeType)40 bool ConvertFromJsTextEncoding(napi_env env, TextEncoding& textEncoding, napi_value nativeType)
41 {
42     napi_value type = nativeType;
43     if (type == nullptr) {
44         return false;
45     }
46     uint32_t resultValue = 0;
47     napi_get_value_uint32(env, type, &resultValue);
48 
49     switch (resultValue) {
50         case 0: // 0: TextEncoding::UTF8
51             textEncoding = TextEncoding::UTF8;
52             break;
53         case 1: // 1: TextEncoding::UTF16
54             textEncoding = TextEncoding::UTF16;
55             break;
56         case 2: // 2: TextEncoding::UTF32
57             textEncoding = TextEncoding::UTF32;
58             break;
59         case 3: // 3: TextEncoding::GLYPH_ID
60             textEncoding = TextEncoding::GLYPH_ID;
61             break;
62         default: // default: TextEncoding::UTF8
63             textEncoding = TextEncoding::UTF8;
64             break;
65     }
66     return true;
67 }
68 
NapiThrowError(napi_env env,DrawingErrorCode err,const std::string & message)69 napi_value NapiThrowError(napi_env env, DrawingErrorCode err, const std::string& message)
70 {
71     napi_throw(env, CreateJsError(env, static_cast<int32_t>(err), message));
72     return NapiGetUndefined(env);
73 }
74 } // namespace Drawing
75 } // namespace OHOS::Rosen
76