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 "font_napi/js_typeface.h"
17 #include "js_drawing_utils.h"
18
19 #ifdef ROSEN_OHOS
20 #include <parameters.h>
21 #endif
22
23 #include "draw/color.h"
24 #include "rosen_text/font_collection.h"
25
26 namespace OHOS::Rosen {
27
28 #ifdef ROSEN_OHOS
29 bool JsDrawingTestUtils::closeDrawingTest_ =
30 std::atoi((OHOS::system::GetParameter("persist.sys.graphic.drawing.test", "0").c_str())) != 1;
31 #else
32 bool JsDrawingTestUtils::closeDrawingTest_ = true;
33 #endif
34
35 namespace Drawing {
BindNativeFunction(napi_env env,napi_value object,const char * name,const char * moduleName,napi_callback func)36 void BindNativeFunction(napi_env env, napi_value object, const char* name, const char* moduleName, napi_callback func)
37 {
38 std::string fullName;
39 if (moduleName) {
40 fullName = moduleName;
41 fullName += '.';
42 }
43 fullName += name;
44 napi_value funcValue = nullptr;
45 napi_create_function(env, fullName.c_str(), fullName.size(), func, nullptr, &funcValue);
46 napi_set_named_property(env, object, fullName.c_str(), funcValue);
47 }
48
CreateJsError(napi_env env,int32_t errCode,const std::string & message)49 napi_value CreateJsError(napi_env env, int32_t errCode, const std::string& message)
50 {
51 napi_value result = nullptr;
52 napi_create_error(env, CreateJsValue(env, errCode), CreateJsValue(env, message), &result);
53 return result;
54 }
55
ConvertFromJsTextEncoding(napi_env env,TextEncoding & textEncoding,napi_value nativeType)56 bool ConvertFromJsTextEncoding(napi_env env, TextEncoding& textEncoding, napi_value nativeType)
57 {
58 napi_value type = nativeType;
59 if (type == nullptr) {
60 return false;
61 }
62 uint32_t resultValue = 0;
63 napi_get_value_uint32(env, type, &resultValue);
64
65 switch (resultValue) {
66 case 0: // 0: TextEncoding::UTF8
67 textEncoding = TextEncoding::UTF8;
68 break;
69 case 1: // 1: TextEncoding::UTF16
70 textEncoding = TextEncoding::UTF16;
71 break;
72 case 2: // 2: TextEncoding::UTF32
73 textEncoding = TextEncoding::UTF32;
74 break;
75 case 3: // 3: TextEncoding::GLYPH_ID
76 textEncoding = TextEncoding::GLYPH_ID;
77 break;
78 default: // default: TextEncoding::UTF8
79 textEncoding = TextEncoding::UTF8;
80 break;
81 }
82 return true;
83 }
84
NapiThrowError(napi_env env,DrawingErrorCode err,const std::string & message)85 napi_value NapiThrowError(napi_env env, DrawingErrorCode err, const std::string& message)
86 {
87 napi_throw(env, CreateJsError(env, static_cast<int32_t>(err), message));
88 return nullptr;
89 }
90
91 static const char* g_argbString[4] = {"alpha", "red", "green", "blue"};
92 static const char* g_ltrbString[4] = {"left", "top", "right", "bottom"};
93 static const char* g_pointString[2] = {"x", "y"};
94
ConvertFromJsColor(napi_env env,napi_value jsValue,int32_t * argb,size_t size)95 bool ConvertFromJsColor(napi_env env, napi_value jsValue, int32_t* argb, size_t size)
96 {
97 napi_value tempValue = nullptr;
98 for (size_t idx = 0; idx < size; idx++) {
99 int32_t* curChannel = argb + idx;
100 napi_get_named_property(env, jsValue, g_argbString[idx], &tempValue);
101 if (napi_get_value_int32(env, tempValue, curChannel) != napi_ok ||
102 *curChannel < 0 || *curChannel > Color::RGB_MAX) {
103 return false;
104 }
105 }
106 return true;
107 }
108
ConvertFromJsRect(napi_env env,napi_value jsValue,double * ltrb,size_t size)109 bool ConvertFromJsRect(napi_env env, napi_value jsValue, double* ltrb, size_t size)
110 {
111 napi_value tempValue = nullptr;
112 for (size_t idx = 0; idx < size; idx++) {
113 double* curEdge = ltrb + idx;
114 napi_get_named_property(env, jsValue, g_ltrbString[idx], &tempValue);
115 if (napi_get_value_double(env, tempValue, curEdge) != napi_ok) {
116 return false;
117 }
118 }
119 return true;
120 }
121
ConvertFromJsIRect(napi_env env,napi_value jsValue,int32_t * ltrb,size_t size)122 bool ConvertFromJsIRect(napi_env env, napi_value jsValue, int32_t* ltrb, size_t size)
123 {
124 napi_value tempValue = nullptr;
125 for (size_t idx = 0; idx < size; idx++) {
126 int32_t* curEdge = ltrb + idx;
127 napi_get_named_property(env, jsValue, g_ltrbString[idx], &tempValue);
128 if (napi_get_value_int32(env, tempValue, curEdge) != napi_ok) {
129 return false;
130 }
131 }
132 return true;
133 }
134
ConvertFromJsShadowFlag(napi_env env,napi_value src,ShadowFlags & shadowFlag,ShadowFlags defaultFlag)135 bool ConvertFromJsShadowFlag(napi_env env, napi_value src, ShadowFlags& shadowFlag, ShadowFlags defaultFlag)
136 {
137 if (src == nullptr) {
138 return false;
139 }
140 uint32_t value = 0;
141 if (!ConvertFromJsValue(env, src, value)) {
142 return false;
143 }
144 shadowFlag = defaultFlag;
145 if (value >= static_cast<uint32_t>(ShadowFlags::NONE) && value <= static_cast<uint32_t>(ShadowFlags::ALL)) {
146 shadowFlag = static_cast<ShadowFlags>(value);
147 }
148 return true;
149 }
150
ConvertFromJsPoint3d(napi_env env,napi_value src,Point3 & point3d)151 bool ConvertFromJsPoint3d(napi_env env, napi_value src, Point3& point3d)
152 {
153 if (src == nullptr) {
154 return false;
155 }
156 napi_value tempValue = nullptr;
157 double x = 0.0;
158 double y = 0.0;
159 double z = 0.0;
160 napi_get_named_property(env, src, "x", &tempValue);
161 bool isXOk = ConvertFromJsValue(env, tempValue, x);
162 napi_get_named_property(env, src, "y", &tempValue);
163 bool isYOk = ConvertFromJsValue(env, tempValue, y);
164 napi_get_named_property(env, src, "z", &tempValue);
165 bool isZOk = ConvertFromJsValue(env, tempValue, z);
166 if (!(isXOk && isYOk && isZOk)) {
167 return false;
168 }
169 point3d = Point3(x, y, z);
170 return true;
171 }
172
ConvertFromJsPoint(napi_env env,napi_value jsValue,double * point,size_t size)173 bool ConvertFromJsPoint(napi_env env, napi_value jsValue, double* point, size_t size)
174 {
175 napi_value tempValue = nullptr;
176 for (size_t idx = 0; idx < size; idx++) {
177 double* curEdge = point + idx;
178 napi_get_named_property(env, jsValue, g_pointString[idx], &tempValue);
179 if (napi_get_value_double(env, tempValue, curEdge) != napi_ok) {
180 return false;
181 }
182 }
183 return true;
184 }
185
ConvertFromJsPointsArray(napi_env env,napi_value array,Drawing::Point * points,uint32_t count)186 bool ConvertFromJsPointsArray(napi_env env, napi_value array, Drawing::Point* points, uint32_t count)
187 {
188 for (uint32_t i = 0; i < count; i++) {
189 napi_value tempPoint = nullptr;
190 if (napi_get_element(env, array, i, &tempPoint) != napi_ok) {
191 return false;
192 }
193 if (!GetDrawingPointFromJsValue(env, tempPoint, points[i])) {
194 return false;
195 }
196 }
197 return true;
198 }
199
GetFontMetricsAndConvertToJsValue(napi_env env,FontMetrics * metrics)200 napi_value GetFontMetricsAndConvertToJsValue(napi_env env, FontMetrics* metrics)
201 {
202 napi_value objValue = nullptr;
203 napi_create_object(env, &objValue);
204 if (metrics != nullptr && objValue != nullptr) {
205 napi_set_named_property(env, objValue, "top", CreateJsNumber(env, metrics->fTop));
206 napi_set_named_property(env, objValue, "ascent", CreateJsNumber(env, metrics->fAscent));
207 napi_set_named_property(env, objValue, "descent", CreateJsNumber(env, metrics->fDescent));
208 napi_set_named_property(env, objValue, "bottom", CreateJsNumber(env, metrics->fBottom));
209 napi_set_named_property(env, objValue, "leading", CreateJsNumber(env, metrics->fLeading));
210 napi_set_named_property(env, objValue, "flags", CreateJsNumber(env, metrics->fFlags));
211 napi_set_named_property(env, objValue, "avgCharWidth", CreateJsNumber(env, metrics->fAvgCharWidth));
212 napi_set_named_property(env, objValue, "maxCharWidth", CreateJsNumber(env, metrics->fMaxCharWidth));
213 napi_set_named_property(env, objValue, "xMin", CreateJsNumber(env, metrics->fXMin));
214 napi_set_named_property(env, objValue, "xMax", CreateJsNumber(env, metrics->fXMax));
215 napi_set_named_property(env, objValue, "xHeight", CreateJsNumber(env, metrics->fXHeight));
216 napi_set_named_property(env, objValue, "capHeight", CreateJsNumber(env, metrics->fCapHeight));
217 napi_set_named_property(env, objValue, "underlineThickness", CreateJsNumber(env,
218 metrics->fUnderlineThickness));
219 napi_set_named_property(env, objValue, "underlinePosition", CreateJsNumber(env,
220 metrics->fUnderlinePosition));
221 napi_set_named_property(env, objValue, "strikethroughThickness", CreateJsNumber(env,
222 metrics->fStrikeoutThickness));
223 napi_set_named_property(env, objValue, "strikethroughPosition", CreateJsNumber(env,
224 metrics->fStrikeoutPosition));
225 }
226 return objValue;
227 }
228
GetThemeFont(std::shared_ptr<Font> font)229 std::shared_ptr<Font> GetThemeFont(std::shared_ptr<Font> font)
230 {
231 if (!font->IsThemeFontFollowed() || font->GetTypeface() != JsTypeface::GetZhCnTypeface()) {
232 return nullptr;
233 }
234 std::shared_ptr<FontCollection> fontCollection = FontCollection::Create();
235 if (fontCollection == nullptr) {
236 return nullptr;
237 }
238 std::shared_ptr<FontMgr> fontMgr = fontCollection->GetFontMgr();
239 if (fontMgr == nullptr) {
240 return nullptr;
241 }
242 std::shared_ptr<Typeface> themeTypeface =
243 std::shared_ptr<Typeface>(fontMgr->MatchFamilyStyle(THEME_FONT, FontStyle()));
244 if (themeTypeface == nullptr) {
245 return nullptr;
246 }
247 std::shared_ptr<Font> themeFont = std::make_shared<Font>(*font);
248 themeFont->SetTypeface(themeTypeface);
249 return themeFont;
250 }
251 } // namespace Drawing
252 } // namespace OHOS::Rosen
253