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 "nativeFontNdk.h"
17
18 #define SUCCESS 0
19 #define FAIL (-1)
20 #define ARR_NUM_0 0
21 #define ARR_NUM_1 1
22 #define ARR_NUM_2 2
23 #define ARR_NUM_3 3
24 #define ARR_NUM_4 4
25 #define NUM_20 20
26 #define NUM_30 30
27 #define NUM_40 40
28 #define NUM_50 50
29 #define DOUBLE_NUM_500 500.0
30 #define DOUBLE_NUM_10 10.0
31 #define DOUBLE_NUM_15 15.0
32
33 static OH_Drawing_TypographyStyle *typoStyle_ = nullptr;
34 static OH_Drawing_TextStyle *txtStyle_ = nullptr;
35 static OH_Drawing_FontCollection *fontCollection_ = nullptr;
36 static OH_Drawing_TypographyCreate *handler_ = nullptr;
37 static OH_Drawing_Typography *typography_ = nullptr;
38 static OH_Drawing_Bitmap *cBitmap_ = nullptr;
39 static OH_Drawing_Canvas *canvas_ = nullptr;
40 static OH_Drawing_TypographyCreate *handler2_ = nullptr;
41 static OH_Drawing_FontCollection *fontCollection2_ = nullptr;
42 static OH_Drawing_TextStyle *txtStyle2_ = nullptr;
43 static OH_Drawing_TypographyStyle *typoStyle2_ = nullptr;
44
PrepareCreateTextLine(const std::string & text)45 static void PrepareCreateTextLine(const std::string &text)
46 {
47 double maxWidth = DOUBLE_NUM_500;
48 uint32_t height = NUM_40;
49 typoStyle_ = OH_Drawing_CreateTypographyStyle();
50 txtStyle_ = OH_Drawing_CreateTextStyle();
51 fontCollection_ = OH_Drawing_CreateFontCollection();
52 handler_ = OH_Drawing_CreateTypographyHandler(typoStyle_, fontCollection_);
53 OH_Drawing_SetTextStyleColor(txtStyle_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
54 double fontSize = NUM_30;
55 OH_Drawing_SetTextStyleFontSize(txtStyle_, fontSize);
56 OH_Drawing_SetTextStyleFontWeight(txtStyle_, FONT_WEIGHT_400);
57 bool halfLeading = true;
58 OH_Drawing_SetTextStyleHalfLeading(txtStyle_, halfLeading);
59 const char *fontFamilies[] = {"Roboto"};
60 OH_Drawing_SetTextStyleFontFamilies(txtStyle_, 1, fontFamilies);
61 OH_Drawing_TypographyHandlerPushTextStyle(handler_, txtStyle_);
62 OH_Drawing_TypographyHandlerAddText(handler_, text.c_str());
63 OH_Drawing_TypographyHandlerPopTextStyle(handler_);
64 typography_ = OH_Drawing_CreateTypography(handler_);
65 OH_Drawing_TypographyLayout(typography_, maxWidth);
66 double position[2] = {DOUBLE_NUM_10, DOUBLE_NUM_15};
67 cBitmap_ = OH_Drawing_BitmapCreate();
68 OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
69 uint32_t width = NUM_20;
70 OH_Drawing_BitmapBuild(cBitmap_, width, height, &cFormat);
71 canvas_ = OH_Drawing_CanvasCreate();
72 OH_Drawing_CanvasBind(canvas_, cBitmap_);
73 OH_Drawing_CanvasClear(canvas_, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
74 OH_Drawing_TypographyPaint(typography_, canvas_, position[0], position[1]);
75 }
76
TearDown()77 static void TearDown()
78 {
79 if (canvas_ != nullptr) {
80 OH_Drawing_CanvasDestroy(canvas_);
81 canvas_ = nullptr;
82 }
83 if (typography_ != nullptr) {
84 OH_Drawing_DestroyTypography(typography_);
85 typography_ = nullptr;
86 }
87 if (handler_ != nullptr) {
88 OH_Drawing_DestroyTypographyHandler(handler_);
89 handler_ = nullptr;
90 }
91 if (txtStyle_ != nullptr) {
92 OH_Drawing_DestroyTextStyle(txtStyle_);
93 txtStyle_ = nullptr;
94 }
95 if (typoStyle_ != nullptr) {
96 OH_Drawing_DestroyTypographyStyle(typoStyle_);
97 typoStyle_ = nullptr;
98 }
99 if (cBitmap_ != nullptr) {
100 OH_Drawing_BitmapDestroy(cBitmap_);
101 cBitmap_ = nullptr;
102 }
103 if (fontCollection_ != nullptr) {
104 OH_Drawing_DestroyFontCollection(fontCollection_);
105 fontCollection_ = nullptr;
106 }
107 }
108
PrepareTypographyCreate(const char * text)109 static void PrepareTypographyCreate(const char *text)
110 {
111 fontCollection2_ = OH_Drawing_CreateFontCollection();
112 typoStyle2_ = OH_Drawing_CreateTypographyStyle();
113 handler2_ = OH_Drawing_CreateTypographyHandler(typoStyle2_, fontCollection2_);
114 txtStyle2_ = OH_Drawing_CreateTextStyle();
115 OH_Drawing_SetTextStyleColor(txtStyle2_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
116 double fontSize = NUM_30;
117 OH_Drawing_SetTextStyleFontSize(txtStyle2_, fontSize);
118 OH_Drawing_SetTextStyleFontWeight(txtStyle2_, FONT_WEIGHT_400);
119 OH_Drawing_SetTextStyleBaseLine(txtStyle2_, TEXT_BASELINE_ALPHABETIC);
120 const char *fontFamilies[] = {"Roboto"};
121 OH_Drawing_SetTextStyleFontFamilies(txtStyle2_, 1, fontFamilies);
122 OH_Drawing_TypographyHandlerPushTextStyle(handler2_, txtStyle2_);
123 if (text != nullptr) {
124 OH_Drawing_TypographyHandlerAddText(handler2_, text);
125 }
126 }
127
TypographyTearDown()128 static void TypographyTearDown()
129 {
130 if (handler2_ != nullptr) {
131 OH_Drawing_DestroyTypographyHandler(handler2_);
132 handler2_ = nullptr;
133 }
134 if (txtStyle2_ != nullptr) {
135 OH_Drawing_DestroyTextStyle(txtStyle2_);
136 txtStyle2_ = nullptr;
137 }
138 if (fontCollection2_ != nullptr) {
139 OH_Drawing_DestroyFontCollection(fontCollection2_);
140 fontCollection2_ = nullptr;
141 }
142 if (typoStyle2_ != nullptr) {
143 OH_Drawing_DestroyTypographyStyle(typoStyle2_);
144 typoStyle2_ = nullptr;
145 }
146 }
147
OHDrawingFontGetPathForGlyph001(napi_env env,napi_callback_info info)148 napi_value OHDrawingFontGetPathForGlyph001(napi_env env, napi_callback_info info)
149 {
150 napi_value result = nullptr;
151 napi_create_array_with_length(env, ARR_NUM_2, &result);
152 napi_value result1 = nullptr;
153 napi_value result2 = nullptr;
154
155 OH_Drawing_Font *font = OH_Drawing_FontCreate();
156 if (font != nullptr) {
157 napi_create_int32(env, SUCCESS, &result1);
158 } else {
159 napi_create_int32(env, FAIL, &result1);
160 }
161 napi_set_element(env, result, ARR_NUM_0, result1);
162
163 OH_Drawing_FontSetTextSize(font, NUM_50);
164 uint16_t glyphsNotExist = 65535;
165 OH_Drawing_Path *path = OH_Drawing_PathCreate();
166 if ((path != nullptr) &&
167 (OH_Drawing_FontGetPathForGlyph(font, glyphsNotExist, path) == OH_DRAWING_ERROR_INVALID_PARAMETER)) {
168 napi_create_int32(env, SUCCESS, &result2);
169 } else {
170 napi_create_int32(env, FAIL, &result2);
171 }
172 napi_set_element(env, result, ARR_NUM_1, result2);
173 if (path != nullptr) {
174 OH_Drawing_PathDestroy(path);
175 }
176 OH_Drawing_FontDestroy(font);
177 return result;
178 }
179
OHDrawingFontGetPathForGlyph002(napi_env env,napi_callback_info info)180 napi_value OHDrawingFontGetPathForGlyph002(napi_env env, napi_callback_info info)
181 {
182 napi_value result = nullptr;
183 napi_create_array_with_length(env, ARR_NUM_3, &result);
184 napi_value result1 = nullptr;
185 napi_value result2 = nullptr;
186 napi_value result3 = nullptr;
187
188 OH_Drawing_Font *font = OH_Drawing_FontCreate();
189 if (font != nullptr) {
190 napi_create_int32(env, SUCCESS, &result1);
191 } else {
192 napi_create_int32(env, FAIL, &result1);
193 }
194 napi_set_element(env, result, ARR_NUM_0, result1);
195
196 OH_Drawing_FontSetTextSize(font, NUM_50);
197 const char *str = "hello world";
198 uint32_t count = OH_Drawing_FontCountText(font, str, strlen(str), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8);
199 if (strlen(str) == count) {
200 napi_create_int32(env, SUCCESS, &result2);
201 } else {
202 napi_create_int32(env, FAIL, &result2);
203 }
204 napi_set_element(env, result, ARR_NUM_1, result2);
205
206 uint16_t glyphs[count];
207 OH_Drawing_FontTextToGlyphs(font, str, strlen(str), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8,
208 glyphs, count);
209 OH_Drawing_Path *path = OH_Drawing_PathCreate();
210 if ((path != nullptr) &&
211 (OH_Drawing_FontGetPathForGlyph(nullptr, glyphs[0], path) == OH_DRAWING_ERROR_INVALID_PARAMETER) &&
212 (OH_Drawing_FontGetPathForGlyph(font, glyphs[0], nullptr) == OH_DRAWING_ERROR_INVALID_PARAMETER)) {
213 napi_create_int32(env, SUCCESS, &result3);
214 } else {
215 napi_create_int32(env, FAIL, &result3);
216 }
217 napi_set_element(env, result, ARR_NUM_2, result3);
218
219 if (path != nullptr) {
220 OH_Drawing_PathDestroy(path);
221 }
222 OH_Drawing_FontDestroy(font);
223 return result;
224 }
225
OHDrawingFontGetPathForGlyph003(napi_env env,napi_callback_info info)226 napi_value OHDrawingFontGetPathForGlyph003(napi_env env, napi_callback_info info)
227 {
228 napi_value result = nullptr;
229 napi_create_array_with_length(env, ARR_NUM_3, &result);
230 napi_value result1 = nullptr;
231 napi_value result2 = nullptr;
232 napi_value result3 = nullptr;
233
234 OH_Drawing_Font *font = OH_Drawing_FontCreate();
235 if (font != nullptr) {
236 napi_create_int32(env, SUCCESS, &result1);
237 } else {
238 napi_create_int32(env, FAIL, &result1);
239 }
240 napi_set_element(env, result, ARR_NUM_0, result1);
241
242 OH_Drawing_FontSetTextSize(font, NUM_50);
243 const char *space = " ";
244 uint32_t count = OH_Drawing_FontCountText(font, space, strlen(space), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8);
245 if (strlen(space) == count) {
246 napi_create_int32(env, SUCCESS, &result2);
247 } else {
248 napi_create_int32(env, FAIL, &result2);
249 }
250 napi_set_element(env, result, ARR_NUM_1, result2);
251
252 uint16_t glyphs[count];
253 OH_Drawing_FontTextToGlyphs(font, space, strlen(space), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8,
254 glyphs, count);
255 OH_Drawing_Path *path = OH_Drawing_PathCreate();
256 if ((path != nullptr) &&
257 (OH_Drawing_FontGetPathForGlyph(font, glyphs[0], path) == OH_DRAWING_SUCCESS) &&
258 (OH_Drawing_PathGetLength(path, false) == 0) &&
259 (OH_Drawing_PathIsClosed(path, false) == false)) {
260 napi_create_int32(env, SUCCESS, &result3);
261 } else {
262 napi_create_int32(env, FAIL, &result3);
263 }
264 napi_set_element(env, result, ARR_NUM_2, result3);
265
266 if (path != nullptr) {
267 OH_Drawing_PathDestroy(path);
268 }
269 OH_Drawing_FontDestroy(font);
270 return result;
271 }
272
OHDrawingFontGetPathForGlyph004(napi_env env,napi_callback_info info)273 napi_value OHDrawingFontGetPathForGlyph004(napi_env env, napi_callback_info info)
274 {
275 napi_value result = nullptr;
276 napi_create_array_with_length(env, ARR_NUM_3, &result);
277 napi_value result1 = nullptr;
278 napi_value result2 = nullptr;
279 napi_value result3 = nullptr;
280
281 OH_Drawing_Font *font = OH_Drawing_FontCreate();
282 if (font != nullptr) {
283 napi_create_int32(env, SUCCESS, &result1);
284 } else {
285 napi_create_int32(env, FAIL, &result1);
286 }
287 napi_set_element(env, result, ARR_NUM_0, result1);
288
289 OH_Drawing_FontSetTextSize(font, NUM_50);
290 const char *str = "helloworld";
291 uint32_t count = OH_Drawing_FontCountText(font, str, strlen(str), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8);
292 if (strlen(str) == count) {
293 napi_create_int32(env, SUCCESS, &result2);
294 } else {
295 napi_create_int32(env, FAIL, &result2);
296 }
297 napi_set_element(env, result, ARR_NUM_1, result2);
298
299 uint16_t glyphs[count];
300 OH_Drawing_FontTextToGlyphs(font, str, strlen(str), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8,
301 glyphs, count);
302 for (int i = 0; i < count; i++) {
303 OH_Drawing_Path *path = OH_Drawing_PathCreate();
304 if ((path != nullptr) &&
305 (OH_Drawing_FontGetPathForGlyph(font, glyphs[i], path) == OH_DRAWING_SUCCESS) &&
306 (OH_Drawing_PathGetLength(path, false) > 0) &&
307 (OH_Drawing_PathIsClosed(path, false) == true)) {
308 napi_create_int32(env, SUCCESS, &result3);
309 } else {
310 napi_create_int32(env, FAIL, &result3);
311 }
312 napi_set_element(env, result, ARR_NUM_2, result3);
313 if (path != nullptr) {
314 OH_Drawing_PathDestroy(path);
315 }
316 }
317 OH_Drawing_FontDestroy(font);
318 return result;
319 }