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 <vector>
17 #include "nativeFontNdk.h"
18
19 #define SUCCESS 0
20 #define FAIL (-1)
21 #define ARR_NUM_0 0
22 #define ARR_NUM_1 1
23 #define ARR_NUM_2 2
24 #define ARR_NUM_3 3
25 #define ARR_NUM_4 4
26 #define ARR_NUM_5 5
27 #define NUM_20 20
28 #define NUM_30 30
29 #define NUM_40 40
30 #define NUM_50 50
31 #define DOUBLE_NUM_500 500.0
32 #define DOUBLE_NUM_10 10.0
33 #define DOUBLE_NUM_15 15.0
34 #define NEGATIVE_NUM_1 (-1)
35 #define NEGATIVE_NUM_100 (-100)
36 #define NUM_0 0
37 #define NUM_1 1
38 #define NUM_5 5
39 #define NUM_5000 5000
40 #define NUM_10000 10000
41
42 namespace {
43 const double DEFAULT_FONT_SIZE = 50;
44 const double MAX_WIDTH = 800.0;
45 }
46
47 static OH_Drawing_TypographyStyle *typoStyle_ = nullptr;
48 static OH_Drawing_TextStyle *txtStyle_ = nullptr;
49 static OH_Drawing_FontCollection *fontCollection_ = nullptr;
50 static OH_Drawing_TypographyCreate *handler_ = nullptr;
51 static OH_Drawing_Typography *typography_ = nullptr;
52 static OH_Drawing_Bitmap *cBitmap_ = nullptr;
53 static OH_Drawing_Canvas *canvas_ = nullptr;
54 static OH_Drawing_TypographyCreate *handler2_ = nullptr;
55 static OH_Drawing_FontCollection *fontCollection2_ = nullptr;
56 static OH_Drawing_TextStyle *txtStyle2_ = nullptr;
57 static OH_Drawing_TypographyStyle *typoStyle2_ = nullptr;
58
PrepareCreateTextLine(const std::string & text)59 static void PrepareCreateTextLine(const std::string &text)
60 {
61 double maxWidth = DOUBLE_NUM_500;
62 uint32_t height = NUM_40;
63 typoStyle_ = OH_Drawing_CreateTypographyStyle();
64 txtStyle_ = OH_Drawing_CreateTextStyle();
65 fontCollection_ = OH_Drawing_CreateFontCollection();
66 handler_ = OH_Drawing_CreateTypographyHandler(typoStyle_, fontCollection_);
67 OH_Drawing_SetTextStyleColor(txtStyle_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
68 double fontSize = NUM_30;
69 OH_Drawing_SetTextStyleFontSize(txtStyle_, fontSize);
70 OH_Drawing_SetTextStyleFontWeight(txtStyle_, FONT_WEIGHT_400);
71 bool halfLeading = true;
72 OH_Drawing_SetTextStyleHalfLeading(txtStyle_, halfLeading);
73 const char *fontFamilies[] = {"Roboto"};
74 OH_Drawing_SetTextStyleFontFamilies(txtStyle_, 1, fontFamilies);
75 OH_Drawing_TypographyHandlerPushTextStyle(handler_, txtStyle_);
76 OH_Drawing_TypographyHandlerAddText(handler_, text.c_str());
77 OH_Drawing_TypographyHandlerPopTextStyle(handler_);
78 typography_ = OH_Drawing_CreateTypography(handler_);
79 OH_Drawing_TypographyLayout(typography_, maxWidth);
80 double position[2] = {DOUBLE_NUM_10, DOUBLE_NUM_15};
81 cBitmap_ = OH_Drawing_BitmapCreate();
82 OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
83 uint32_t width = NUM_20;
84 OH_Drawing_BitmapBuild(cBitmap_, width, height, &cFormat);
85 canvas_ = OH_Drawing_CanvasCreate();
86 OH_Drawing_CanvasBind(canvas_, cBitmap_);
87 OH_Drawing_CanvasClear(canvas_, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
88 OH_Drawing_TypographyPaint(typography_, canvas_, position[0], position[1]);
89 }
90
TearDown()91 static void TearDown()
92 {
93 if (canvas_ != nullptr) {
94 OH_Drawing_CanvasDestroy(canvas_);
95 canvas_ = nullptr;
96 }
97 if (typography_ != nullptr) {
98 OH_Drawing_DestroyTypography(typography_);
99 typography_ = nullptr;
100 }
101 if (handler_ != nullptr) {
102 OH_Drawing_DestroyTypographyHandler(handler_);
103 handler_ = nullptr;
104 }
105 if (txtStyle_ != nullptr) {
106 OH_Drawing_DestroyTextStyle(txtStyle_);
107 txtStyle_ = nullptr;
108 }
109 if (typoStyle_ != nullptr) {
110 OH_Drawing_DestroyTypographyStyle(typoStyle_);
111 typoStyle_ = nullptr;
112 }
113 if (cBitmap_ != nullptr) {
114 OH_Drawing_BitmapDestroy(cBitmap_);
115 cBitmap_ = nullptr;
116 }
117 if (fontCollection_ != nullptr) {
118 OH_Drawing_DestroyFontCollection(fontCollection_);
119 fontCollection_ = nullptr;
120 }
121 }
122
PrepareTypographyCreate(const char * text)123 static void PrepareTypographyCreate(const char *text)
124 {
125 fontCollection2_ = OH_Drawing_CreateFontCollection();
126 typoStyle2_ = OH_Drawing_CreateTypographyStyle();
127 handler2_ = OH_Drawing_CreateTypographyHandler(typoStyle2_, fontCollection2_);
128 txtStyle2_ = OH_Drawing_CreateTextStyle();
129 OH_Drawing_SetTextStyleColor(txtStyle2_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
130 double fontSize = NUM_30;
131 OH_Drawing_SetTextStyleFontSize(txtStyle2_, fontSize);
132 OH_Drawing_SetTextStyleFontWeight(txtStyle2_, FONT_WEIGHT_400);
133 OH_Drawing_SetTextStyleBaseLine(txtStyle2_, TEXT_BASELINE_ALPHABETIC);
134 const char *fontFamilies[] = {"Roboto"};
135 OH_Drawing_SetTextStyleFontFamilies(txtStyle2_, 1, fontFamilies);
136 OH_Drawing_TypographyHandlerPushTextStyle(handler2_, txtStyle2_);
137 if (text != nullptr) {
138 OH_Drawing_TypographyHandlerAddText(handler2_, text);
139 }
140 }
141
TypographyTearDown()142 static void TypographyTearDown()
143 {
144 if (handler2_ != nullptr) {
145 OH_Drawing_DestroyTypographyHandler(handler2_);
146 handler2_ = nullptr;
147 }
148 if (txtStyle2_ != nullptr) {
149 OH_Drawing_DestroyTextStyle(txtStyle2_);
150 txtStyle2_ = nullptr;
151 }
152 if (fontCollection2_ != nullptr) {
153 OH_Drawing_DestroyFontCollection(fontCollection2_);
154 fontCollection2_ = nullptr;
155 }
156 if (typoStyle2_ != nullptr) {
157 OH_Drawing_DestroyTypographyStyle(typoStyle2_);
158 typoStyle2_ = nullptr;
159 }
160 }
161
GetUnresolvedGlyphCount(const void * text,size_t length,OH_Drawing_TextEncoding encode)162 static int32_t GetUnresolvedGlyphCount(const void* text, size_t length, OH_Drawing_TextEncoding encode)
163 {
164 auto typoStyle = OH_Drawing_CreateTypographyStyle();
165 auto handler = OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_GetFontCollectionGlobalInstance());
166 OH_Drawing_TypographyHandlerAddEncodedText(handler, text, length, encode);
167 auto typography = OH_Drawing_CreateTypography(handler);
168 OH_Drawing_TypographyLayout(typography, 100.0f);
169 int32_t unresolved = OH_Drawing_TypographyGetUnresolvedGlyphsCount(typography);
170 OH_Drawing_DestroyTypography(typography);
171 OH_Drawing_DestroyTypographyHandler(handler);
172 OH_Drawing_DestroyTypographyStyle(typoStyle);
173 return unresolved;
174 }
175
OHDrawingFontGetPathForGlyph001(napi_env env,napi_callback_info info)176 napi_value OHDrawingFontGetPathForGlyph001(napi_env env, napi_callback_info info)
177 {
178 napi_value result = nullptr;
179 napi_create_array_with_length(env, ARR_NUM_2, &result);
180 napi_value result1 = nullptr;
181 napi_value result2 = nullptr;
182
183 OH_Drawing_Font *font = OH_Drawing_FontCreate();
184 if (font != nullptr) {
185 napi_create_int32(env, SUCCESS, &result1);
186 } else {
187 napi_create_int32(env, FAIL, &result1);
188 }
189 napi_set_element(env, result, ARR_NUM_0, result1);
190
191 OH_Drawing_FontSetTextSize(font, NUM_50);
192 uint16_t glyphsNotExist = 65535;
193 OH_Drawing_Path *path = OH_Drawing_PathCreate();
194 if ((path != nullptr) &&
195 (OH_Drawing_FontGetPathForGlyph(font, glyphsNotExist, path) == OH_DRAWING_ERROR_INVALID_PARAMETER)) {
196 napi_create_int32(env, SUCCESS, &result2);
197 } else {
198 napi_create_int32(env, FAIL, &result2);
199 }
200 napi_set_element(env, result, ARR_NUM_1, result2);
201 if (path != nullptr) {
202 OH_Drawing_PathDestroy(path);
203 }
204 OH_Drawing_FontDestroy(font);
205 return result;
206 }
207
OHDrawingFontGetPathForGlyph002(napi_env env,napi_callback_info info)208 napi_value OHDrawingFontGetPathForGlyph002(napi_env env, napi_callback_info info)
209 {
210 napi_value result = nullptr;
211 napi_create_array_with_length(env, ARR_NUM_3, &result);
212 napi_value result1 = nullptr;
213 napi_value result2 = nullptr;
214 napi_value result3 = nullptr;
215
216 OH_Drawing_Font *font = OH_Drawing_FontCreate();
217 if (font != nullptr) {
218 napi_create_int32(env, SUCCESS, &result1);
219 } else {
220 napi_create_int32(env, FAIL, &result1);
221 }
222 napi_set_element(env, result, ARR_NUM_0, result1);
223
224 OH_Drawing_FontSetTextSize(font, NUM_50);
225 const char *str = "hello world";
226 uint32_t count = OH_Drawing_FontCountText(font, str, strlen(str), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8);
227 if (strlen(str) == count) {
228 napi_create_int32(env, SUCCESS, &result2);
229 } else {
230 napi_create_int32(env, FAIL, &result2);
231 }
232 napi_set_element(env, result, ARR_NUM_1, result2);
233
234 uint16_t glyphs[count];
235 OH_Drawing_FontTextToGlyphs(font, str, strlen(str), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8,
236 glyphs, count);
237 OH_Drawing_Path *path = OH_Drawing_PathCreate();
238 if ((path != nullptr) &&
239 (OH_Drawing_FontGetPathForGlyph(nullptr, glyphs[0], path) == OH_DRAWING_ERROR_INVALID_PARAMETER) &&
240 (OH_Drawing_FontGetPathForGlyph(font, glyphs[0], nullptr) == OH_DRAWING_ERROR_INVALID_PARAMETER)) {
241 napi_create_int32(env, SUCCESS, &result3);
242 } else {
243 napi_create_int32(env, FAIL, &result3);
244 }
245 napi_set_element(env, result, ARR_NUM_2, result3);
246
247 if (path != nullptr) {
248 OH_Drawing_PathDestroy(path);
249 }
250 OH_Drawing_FontDestroy(font);
251 return result;
252 }
253
OHDrawingFontGetPathForGlyph003(napi_env env,napi_callback_info info)254 napi_value OHDrawingFontGetPathForGlyph003(napi_env env, napi_callback_info info)
255 {
256 napi_value result = nullptr;
257 napi_create_array_with_length(env, ARR_NUM_3, &result);
258 napi_value result1 = nullptr;
259 napi_value result2 = nullptr;
260 napi_value result3 = nullptr;
261
262 OH_Drawing_Font *font = OH_Drawing_FontCreate();
263 if (font != nullptr) {
264 napi_create_int32(env, SUCCESS, &result1);
265 } else {
266 napi_create_int32(env, FAIL, &result1);
267 }
268 napi_set_element(env, result, ARR_NUM_0, result1);
269
270 OH_Drawing_FontSetTextSize(font, NUM_50);
271 const char *space = " ";
272 uint32_t count = OH_Drawing_FontCountText(font, space, strlen(space), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8);
273 if (strlen(space) == count) {
274 napi_create_int32(env, SUCCESS, &result2);
275 } else {
276 napi_create_int32(env, FAIL, &result2);
277 }
278 napi_set_element(env, result, ARR_NUM_1, result2);
279
280 uint16_t glyphs[count];
281 OH_Drawing_FontTextToGlyphs(font, space, strlen(space), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8,
282 glyphs, count);
283 OH_Drawing_Path *path = OH_Drawing_PathCreate();
284 if ((path != nullptr) &&
285 (OH_Drawing_FontGetPathForGlyph(font, glyphs[0], path) == OH_DRAWING_SUCCESS) &&
286 (OH_Drawing_PathGetLength(path, false) == 0) &&
287 (OH_Drawing_PathIsClosed(path, false) == false)) {
288 napi_create_int32(env, SUCCESS, &result3);
289 } else {
290 napi_create_int32(env, FAIL, &result3);
291 }
292 napi_set_element(env, result, ARR_NUM_2, result3);
293
294 if (path != nullptr) {
295 OH_Drawing_PathDestroy(path);
296 }
297 OH_Drawing_FontDestroy(font);
298 return result;
299 }
300
OHDrawingFontGetPathForGlyph004(napi_env env,napi_callback_info info)301 napi_value OHDrawingFontGetPathForGlyph004(napi_env env, napi_callback_info info)
302 {
303 napi_value result = nullptr;
304 napi_create_array_with_length(env, ARR_NUM_3, &result);
305 napi_value result1 = nullptr;
306 napi_value result2 = nullptr;
307 napi_value result3 = nullptr;
308
309 OH_Drawing_Font *font = OH_Drawing_FontCreate();
310 if (font != nullptr) {
311 napi_create_int32(env, SUCCESS, &result1);
312 } else {
313 napi_create_int32(env, FAIL, &result1);
314 }
315 napi_set_element(env, result, ARR_NUM_0, result1);
316
317 OH_Drawing_FontSetTextSize(font, NUM_50);
318 const char *str = "helloworld";
319 uint32_t count = OH_Drawing_FontCountText(font, str, strlen(str), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8);
320 if (strlen(str) == count) {
321 napi_create_int32(env, SUCCESS, &result2);
322 } else {
323 napi_create_int32(env, FAIL, &result2);
324 }
325 napi_set_element(env, result, ARR_NUM_1, result2);
326
327 uint16_t glyphs[count];
328 OH_Drawing_FontTextToGlyphs(font, str, strlen(str), OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8,
329 glyphs, count);
330 for (int i = 0; i < count; i++) {
331 OH_Drawing_Path *path = OH_Drawing_PathCreate();
332 if ((path != nullptr) &&
333 (OH_Drawing_FontGetPathForGlyph(font, glyphs[i], path) == OH_DRAWING_SUCCESS) &&
334 (OH_Drawing_PathGetLength(path, false) > 0) &&
335 (OH_Drawing_PathIsClosed(path, false) == true)) {
336 napi_create_int32(env, SUCCESS, &result3);
337 } else {
338 napi_create_int32(env, FAIL, &result3);
339 }
340 napi_set_element(env, result, ARR_NUM_2, result3);
341 if (path != nullptr) {
342 OH_Drawing_PathDestroy(path);
343 }
344 }
345 OH_Drawing_FontDestroy(font);
346 return result;
347 }
348
SetFontSize(OH_Drawing_TextStyle * txtStyle,double size,int weight,OH_Drawing_TextBadgeType badge)349 static int SetFontSize(OH_Drawing_TextStyle *txtStyle, double size, int weight, OH_Drawing_TextBadgeType badge)
350 {
351 OH_Drawing_SetTextStyleFontSize(txtStyle, size);
352 OH_Drawing_SetTextStyleFontWeight(txtStyle, weight);
353 OH_Drawing_SetTextStyleBadgeType(txtStyle, badge);
354 return 0;
355 }
356
DestoryTypographyTextStyle(OH_Drawing_Typography * typography,OH_Drawing_TypographyCreate * handler,OH_Drawing_TextStyle * txtStyle)357 static int DestoryTypographyTextStyle(OH_Drawing_Typography *typography, OH_Drawing_TypographyCreate *handler,
358 OH_Drawing_TextStyle *txtStyle)
359 {
360 if (typography != nullptr) {
361 OH_Drawing_DestroyTypography(typography);
362 typography = nullptr;
363 }
364
365 if (handler != nullptr) {
366 OH_Drawing_DestroyTypographyHandler(handler);
367 handler = nullptr;
368 }
369
370 if (txtStyle != nullptr) {
371 OH_Drawing_DestroyTextStyle(txtStyle);
372 txtStyle = nullptr;
373 }
374
375 return 0;
376 }
377
OHDrawingSetTextStyleBadgeType001(napi_env env,napi_callback_info info)378 napi_value OHDrawingSetTextStyleBadgeType001(napi_env env, napi_callback_info info)
379 {
380 napi_value result = nullptr;
381 napi_create_array_with_length(env, ARR_NUM_3, &result);
382 napi_value result1 = nullptr;
383 napi_value result2 = nullptr;
384 napi_value result3 = nullptr;
385
386 OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
387 OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
388 OH_Drawing_TypographyCreate *handler =
389 OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
390 OH_Drawing_TypographyCreate *superTxtHandler =
391 OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
392
393 OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0xFF));
394 SetFontSize(txtStyle, DEFAULT_FONT_SIZE, FONT_WEIGHT_400, OH_Drawing_TextBadgeType::TEXT_BADGE_NONE);
395
396 OH_Drawing_TextStyle *superTxtStyle = OH_Drawing_CreateTextStyle();
397 if (superTxtStyle != nullptr) {
398 napi_create_int32(env, SUCCESS, &result1);
399 } else {
400 napi_create_int32(env, FAIL, &result1);
401 }
402 napi_set_element(env, result, ARR_NUM_0, result1);
403 SetFontSize(superTxtStyle, DEFAULT_FONT_SIZE, FONT_WEIGHT_400, OH_Drawing_TextBadgeType::TEXT_SUPERSCRIPT);
404
405 const char *text = "OpenHarmony";
406 OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
407 OH_Drawing_TypographyHandlerAddText(handler, text);
408 OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
409 if (typography != nullptr) {
410 napi_create_int32(env, SUCCESS, &result2);
411 } else {
412 napi_create_int32(env, FAIL, &result2);
413 }
414 napi_set_element(env, result, ARR_NUM_1, result2);
415 OH_Drawing_TypographyLayout(typography, MAX_WIDTH);
416
417 OH_Drawing_TypographyHandlerPushTextStyle(superTxtHandler, superTxtStyle);
418 OH_Drawing_TypographyHandlerAddText(superTxtHandler, text);
419 OH_Drawing_Typography *superTxtTypography = OH_Drawing_CreateTypography(superTxtHandler);
420 if (superTxtTypography != nullptr) {
421 napi_create_int32(env, SUCCESS, &result3);
422 } else {
423 napi_create_int32(env, FAIL, &result3);
424 }
425 napi_set_element(env, result, ARR_NUM_2, result3);
426 OH_Drawing_TypographyLayout(superTxtTypography, MAX_WIDTH);
427
428 DestoryTypographyTextStyle(typography, handler, txtStyle);
429 DestoryTypographyTextStyle(superTxtTypography, superTxtHandler, superTxtStyle);
430 return result;
431 }
432
OHDrawingSetTextStyleBadgeType002(napi_env env,napi_callback_info info)433 napi_value OHDrawingSetTextStyleBadgeType002(napi_env env, napi_callback_info info)
434 {
435 napi_value result = nullptr;
436 napi_create_array_with_length(env, ARR_NUM_2, &result);
437 napi_value result1 = nullptr;
438 napi_value result2 = nullptr;
439
440 OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
441 OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
442 OH_Drawing_TypographyCreate *handler =
443 OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
444 OH_Drawing_TypographyCreate *subTxtHandler =
445 OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
446
447 OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0xFF));
448 OH_Drawing_SetTextStyleFontSize(txtStyle, DEFAULT_FONT_SIZE);
449 OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
450 OH_Drawing_SetTextStyleBadgeType(txtStyle, OH_Drawing_TextBadgeType::TEXT_BADGE_NONE);
451
452 OH_Drawing_TextStyle *subTxtStyle = OH_Drawing_CreateTextStyle();
453 if (subTxtStyle != nullptr) {
454 napi_create_int32(env, SUCCESS, &result1);
455 } else {
456 napi_create_int32(env, FAIL, &result1);
457 }
458 napi_set_element(env, result, ARR_NUM_0, result1);
459 OH_Drawing_SetTextStyleFontSize(subTxtStyle, DEFAULT_FONT_SIZE);
460 OH_Drawing_SetTextStyleFontWeight(subTxtStyle, FONT_WEIGHT_400);
461 OH_Drawing_SetTextStyleBadgeType(subTxtStyle, OH_Drawing_TextBadgeType::TEXT_SUBSCRIPT);
462
463 const char *text = "ÄãºÃÊÀ½ç";
464 OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
465 OH_Drawing_TypographyHandlerAddText(handler, text);
466 OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
467 OH_Drawing_TypographyLayout(typography, MAX_WIDTH);
468
469 OH_Drawing_TypographyHandlerPushTextStyle(subTxtHandler, subTxtStyle);
470 OH_Drawing_TypographyHandlerAddText(subTxtHandler, text);
471 OH_Drawing_Typography *subTxtTypography = OH_Drawing_CreateTypography(subTxtHandler);
472 if (subTxtTypography != nullptr) {
473 napi_create_int32(env, SUCCESS, &result2);
474 } else {
475 napi_create_int32(env, FAIL, &result2);
476 }
477 napi_set_element(env, result, ARR_NUM_1, result2);
478 OH_Drawing_TypographyLayout(subTxtTypography, MAX_WIDTH);
479
480 OH_Drawing_DestroyTypography(typography);
481 OH_Drawing_DestroyTypographyHandler(handler);
482 OH_Drawing_DestroyTextStyle(txtStyle);
483
484 OH_Drawing_DestroyTypography(subTxtTypography);
485 OH_Drawing_DestroyTypographyHandler(subTxtHandler);
486 OH_Drawing_DestroyTextStyle(subTxtStyle);
487 return result;
488 }
489
OHDrawingTypographyHandlerAddEncodedText001(napi_env env,napi_callback_info info)490 napi_value OHDrawingTypographyHandlerAddEncodedText001(napi_env env, napi_callback_info info)
491 {
492 napi_value result = nullptr;
493 napi_create_array_with_length(env, ARR_NUM_5, &result);
494 napi_value result1 = nullptr;
495 napi_value result2 = nullptr;
496 napi_value result3 = nullptr;
497 napi_value result4 = nullptr;
498 napi_value result5 = nullptr;
499 // Basic ASCII characters
500 const char ascii[] = u8"Hello World";
501 if (GetUnresolvedGlyphCount(ascii, sizeof(ascii), TEXT_ENCODING_UTF8) == NUM_0) {
502 napi_create_int32(env, SUCCESS, &result1);
503 } else {
504 napi_create_int32(env, FAIL, &result1);
505 }
506 napi_set_element(env, result, ARR_NUM_0, result1);
507 // Non-ASCII characters (Chinese)
508 const char chinese[] = u8"你好世界";
509 if (GetUnresolvedGlyphCount(chinese, sizeof(chinese), TEXT_ENCODING_UTF8) == NUM_0) {
510 napi_create_int32(env, SUCCESS, &result2);
511 } else {
512 napi_create_int32(env, FAIL, &result2);
513 }
514 napi_set_element(env, result, ARR_NUM_1, result2);
515 // Special characters
516 const char tabsNewline[] = u8"Tabs\tNewline\n";
517 if (GetUnresolvedGlyphCount(tabsNewline, sizeof(tabsNewline), TEXT_ENCODING_UTF8) == NUM_0) {
518 napi_create_int32(env, SUCCESS, &result3);
519 } else {
520 napi_create_int32(env, FAIL, &result3);
521 }
522 napi_set_element(env, result, ARR_NUM_2, result3);
523 // 4-byte emoji characters
524 const char emoji[] = u8"";
525 if (GetUnresolvedGlyphCount(emoji, sizeof(emoji), TEXT_ENCODING_UTF8) == NUM_0) {
526 napi_create_int32(env, SUCCESS, &result4);
527 } else {
528 napi_create_int32(env, FAIL, &result4);
529 }
530 napi_set_element(env, result, ARR_NUM_3, result4);
531 // Mixed case and symbols
532 const char mixedCaseSymbols[] = u8"Aa1!Ωπ";
533 if (GetUnresolvedGlyphCount(mixedCaseSymbols, sizeof(mixedCaseSymbols), TEXT_ENCODING_UTF8) == NUM_0) {
534 napi_create_int32(env, SUCCESS, &result5);
535 } else {
536 napi_create_int32(env, FAIL, &result5);
537 }
538 napi_set_element(env, result, ARR_NUM_4, result5);
539 return result;
540 }
541
OHDrawingTypographyHandlerAddEncodedText002(napi_env env,napi_callback_info info)542 napi_value OHDrawingTypographyHandlerAddEncodedText002(napi_env env, napi_callback_info info)
543 {
544 napi_value result = nullptr;
545
546 // Invalid continuation bytes
547 const char invalid1[] = "abc\x80\xff";
548 GetUnresolvedGlyphCount(invalid1, sizeof(invalid1), TEXT_ENCODING_UTF8);
549
550 // Truncated multi-byte sequence
551 const char invalid2[] = u8"正常\xE6\x97继续";
552 GetUnresolvedGlyphCount(invalid2, sizeof(invalid2), TEXT_ENCODING_UTF8);
553
554 // Overlong encoding
555 const char invalid3[] = "\xF0\x82\x82\xAC"; // Overlong € symbol
556 GetUnresolvedGlyphCount(invalid3, sizeof(invalid3), TEXT_ENCODING_UTF8);
557 napi_create_int32(env, SUCCESS, &result);
558 return result;
559 }
560
OHDrawingTypographyHandlerAddEncodedText003(napi_env env,napi_callback_info info)561 napi_value OHDrawingTypographyHandlerAddEncodedText003(napi_env env, napi_callback_info info)
562 {
563 napi_value result = nullptr;
564 napi_create_array_with_length(env, ARR_NUM_4, &result);
565 napi_value result1 = nullptr;
566 napi_value result2 = nullptr;
567 napi_value result3 = nullptr;
568 napi_value result4 = nullptr;
569
570 // Basic BMP characters
571 const char16_t ascii16[] = u"Hello World";
572 if (GetUnresolvedGlyphCount(ascii16, sizeof(ascii16), TEXT_ENCODING_UTF16) == NUM_0) {
573 napi_create_int32(env, SUCCESS, &result1);
574 } else {
575 napi_create_int32(env, FAIL, &result1);
576 }
577 napi_set_element(env, result, ARR_NUM_0, result1);
578
579 // Non-BMP characters (emoji)
580 const char16_t emoji16[] = u"";
581 if (GetUnresolvedGlyphCount(emoji16, sizeof(emoji16), TEXT_ENCODING_UTF16) == NUM_0) {
582 napi_create_int32(env, SUCCESS, &result2);
583 } else {
584 napi_create_int32(env, FAIL, &result2);
585 }
586 napi_set_element(env, result, ARR_NUM_1, result2);
587
588 // RTL text with control characters
589 const char16_t rtlText16[] = u"\x202E右到左文本";
590 if (GetUnresolvedGlyphCount(rtlText16, sizeof(rtlText16), TEXT_ENCODING_UTF16) == NUM_0) {
591 napi_create_int32(env, SUCCESS, &result3);
592 } else {
593 napi_create_int32(env, FAIL, &result3);
594 }
595 napi_set_element(env, result, ARR_NUM_2, result3);
596
597 // Combining characters
598 const char16_t combiningChars16[] = u"A\u0300\u0301";
599 if (GetUnresolvedGlyphCount(combiningChars16, sizeof(combiningChars16), TEXT_ENCODING_UTF16) == NUM_0) {
600 napi_create_int32(env, SUCCESS, &result4);
601 } else {
602 napi_create_int32(env, FAIL, &result4);
603 }
604 napi_set_element(env, result, ARR_NUM_3, result4);
605 return result;
606 }
607
OHDrawingTypographyHandlerAddEncodedText004(napi_env env,napi_callback_info info)608 napi_value OHDrawingTypographyHandlerAddEncodedText004(napi_env env, napi_callback_info info)
609 {
610 napi_value result = nullptr;
611
612 // Unpaired high surrogate
613 const char16_t invalid1[] = u"\xD800\x0041";
614 GetUnresolvedGlyphCount(invalid1, sizeof(invalid1), TEXT_ENCODING_UTF16);
615
616 // Unpaired low surrogate
617 const char16_t invalid2[] = u"\xDC00";
618 GetUnresolvedGlyphCount(invalid2, sizeof(invalid2), TEXT_ENCODING_UTF16);
619
620 // Swapped surrogate pair
621 const char16_t invalid3[] = u"\xDC00\xD800";
622 GetUnresolvedGlyphCount(invalid3, sizeof(invalid3), TEXT_ENCODING_UTF16);
623 napi_create_int32(env, SUCCESS, &result);
624 return result;
625 }
626
OHDrawingTypographyHandlerAddEncodedText005(napi_env env,napi_callback_info info)627 napi_value OHDrawingTypographyHandlerAddEncodedText005(napi_env env, napi_callback_info info)
628 {
629 napi_value result = nullptr;
630 napi_create_array_with_length(env, ARR_NUM_3, &result);
631 napi_value result1 = nullptr;
632 napi_value result2 = nullptr;
633 napi_value result3 = nullptr;
634
635 // Basic ASCII range
636 const char32_t ascii32[] = U"ASCII Text!";
637 if (GetUnresolvedGlyphCount(ascii32, sizeof(ascii32), TEXT_ENCODING_UTF32) == NUM_0) {
638 napi_create_int32(env, SUCCESS, &result1);
639 } else {
640 napi_create_int32(env, FAIL, &result1);
641 }
642 napi_set_element(env, result, ARR_NUM_0, result1);
643
644 // Non-BMP characters
645 const char32_t emoji32[] = { 0x1F600, 0x1F308, NUM_0 }; //
646 if (GetUnresolvedGlyphCount(emoji32, sizeof(emoji32), TEXT_ENCODING_UTF32) == NUM_0) {
647 napi_create_int32(env, SUCCESS, &result2);
648 } else {
649 napi_create_int32(env, FAIL, &result2);
650 }
651 napi_set_element(env, result, ARR_NUM_1, result2);
652
653 // Maximum valid code point
654 const char32_t maxUnicode32[] = { 0x10FFFF };
655 if (GetUnresolvedGlyphCount(maxUnicode32, sizeof(maxUnicode32), TEXT_ENCODING_UTF32) == NUM_1) {
656 napi_create_int32(env, SUCCESS, &result3);
657 } else {
658 napi_create_int32(env, FAIL, &result3);
659 }
660 napi_set_element(env, result, ARR_NUM_2, result3);
661 return result;
662 }
663
OHDrawingTypographyHandlerAddEncodedText006(napi_env env,napi_callback_info info)664 napi_value OHDrawingTypographyHandlerAddEncodedText006(napi_env env, napi_callback_info info)
665 {
666 napi_value result = nullptr;
667
668 // Code point beyond U+10FFFF
669 const char32_t invalid1[] = { 0x110000 };
670 GetUnresolvedGlyphCount(invalid1, sizeof(invalid1), TEXT_ENCODING_UTF32);
671
672 // Surrogate code points
673 const char32_t invalid2[] = { 0xD800, 0xDFFF };
674 GetUnresolvedGlyphCount(invalid2, sizeof(invalid2), TEXT_ENCODING_UTF32);
675
676 // Negative value
677 const int invalid3[] = { NEGATIVE_NUM_100 };
678 GetUnresolvedGlyphCount(invalid3, sizeof(invalid3), TEXT_ENCODING_UTF32);
679 napi_create_int32(env, SUCCESS, &result);
680 return result;
681 }
682
OHDrawingTypographyHandlerAddEncodedText007(napi_env env,napi_callback_info info)683 napi_value OHDrawingTypographyHandlerAddEncodedText007(napi_env env, napi_callback_info info)
684 {
685 napi_value result = nullptr;
686 napi_create_array_with_length(env, ARR_NUM_4, &result);
687 napi_value result1 = nullptr;
688 napi_value result2 = nullptr;
689 napi_value result3 = nullptr;
690 napi_value result4 = nullptr;
691
692 // Empty input with different encodings
693 if ((GetUnresolvedGlyphCount("", NUM_0, TEXT_ENCODING_UTF8) == NEGATIVE_NUM_1) &&
694 (GetUnresolvedGlyphCount(u"", NUM_0, TEXT_ENCODING_UTF16) == NEGATIVE_NUM_1) &&
695 (GetUnresolvedGlyphCount(U"", NUM_0, TEXT_ENCODING_UTF32) == NEGATIVE_NUM_1)) {
696 napi_create_int32(env, SUCCESS, &result1);
697 } else {
698 napi_create_int32(env, FAIL, &result1);
699 }
700 napi_set_element(env, result, ARR_NUM_0, result1);
701
702 // Null pointer with zero length
703 if (GetUnresolvedGlyphCount(nullptr, NUM_0, TEXT_ENCODING_UTF8) == NEGATIVE_NUM_1) {
704 napi_create_int32(env, SUCCESS, &result2);
705 } else {
706 napi_create_int32(env, FAIL, &result2);
707 }
708 napi_set_element(env, result, ARR_NUM_1, result2);
709
710 // Maximum code point boundary
711 const char32_t boundary1[] = { 0x10FFFF, 0x0000 };
712 if (GetUnresolvedGlyphCount(boundary1, sizeof(boundary1), TEXT_ENCODING_UTF32) == NUM_1) {
713 napi_create_int32(env, SUCCESS, &result3);
714 } else {
715 napi_create_int32(env, FAIL, &result3);
716 }
717 napi_set_element(env, result, ARR_NUM_2, result3);
718
719 // Minimum code point boundary
720 const char32_t boundary2[] = { 0x0000 };
721 if (GetUnresolvedGlyphCount(boundary2, sizeof(boundary2), TEXT_ENCODING_UTF32) == NUM_0) {
722 napi_create_int32(env, SUCCESS, &result4);
723 } else {
724 napi_create_int32(env, FAIL, &result4);
725 }
726 napi_set_element(env, result, ARR_NUM_3, result4);
727 return result;
728 }
729
OHDrawingTypographyHandlerAddEncodedText008(napi_env env,napi_callback_info info)730 napi_value OHDrawingTypographyHandlerAddEncodedText008(napi_env env, napi_callback_info info)
731 {
732 napi_value result = nullptr;
733 napi_create_array_with_length(env, ARR_NUM_3, &result);
734 napi_value result1 = nullptr;
735 napi_value result2 = nullptr;
736 napi_value result3 = nullptr;
737
738 // Large UTF-8 text
739 std::vector<char> bigUtf8(NUM_10000, 'A');
740 if (GetUnresolvedGlyphCount(bigUtf8.data(), bigUtf8.size(), TEXT_ENCODING_UTF8) == NUM_0) {
741 napi_create_int32(env, SUCCESS, &result1);
742 } else {
743 napi_create_int32(env, FAIL, &result1);
744 }
745 napi_set_element(env, result, ARR_NUM_0, result1);
746
747 // Large UTF-16 text with surrogate pairs
748 std::vector<char16_t> bigUtf16;
749 for (int i = NUM_0; i < NUM_10000; ++i) {
750 bigUtf16.push_back(0xD83D);
751 bigUtf16.push_back(0xDE00);
752 }
753 if (GetUnresolvedGlyphCount(bigUtf16.data(), bigUtf16.size(), TEXT_ENCODING_UTF16) == NUM_0) {
754 napi_create_int32(env, SUCCESS, &result2);
755 } else {
756 napi_create_int32(env, FAIL, &result2);
757 }
758 napi_set_element(env, result, ARR_NUM_1, result2);
759
760 // Mixed valid/invalid UTF-32 data
761 std::vector<char32_t> mixed_utf32(NUM_10000, 0x1F600);
762 mixed_utf32[NUM_5000] = 0x110000; // Insert invalid code point
763 if (GetUnresolvedGlyphCount(mixed_utf32.data(), mixed_utf32.size(), TEXT_ENCODING_UTF32) == NUM_0) {
764 napi_create_int32(env, SUCCESS, &result3);
765 } else {
766 napi_create_int32(env, FAIL, &result3);
767 }
768 napi_set_element(env, result, ARR_NUM_2, result3);
769 return result;
770 }
771
OHDrawingTypographyHandlerAddEncodedText009(napi_env env,napi_callback_info info)772 napi_value OHDrawingTypographyHandlerAddEncodedText009(napi_env env, napi_callback_info info)
773 {
774 napi_value result = nullptr;
775 napi_create_array_with_length(env, ARR_NUM_3, &result);
776 napi_value result1 = nullptr;
777 napi_value result2 = nullptr;
778 napi_value result3 = nullptr;
779
780 // Invalid encoding type
781 const char testStr[] = "test";
782 if (GetUnresolvedGlyphCount(testStr, sizeof(testStr), TEXT_ENCODING_GLYPH_ID) == NEGATIVE_NUM_1) {
783 napi_create_int32(env, SUCCESS, &result1);
784 } else {
785 napi_create_int32(env, FAIL, &result1);
786 }
787 napi_set_element(env, result, ARR_NUM_0, result1);
788
789 // Mismatched encoding and data
790 const char16_t utf16Data[] = u"test";
791 if (GetUnresolvedGlyphCount(utf16Data, sizeof(utf16Data), TEXT_ENCODING_UTF8) == NUM_0) {
792 napi_create_int32(env, SUCCESS, &result2);
793 } else {
794 napi_create_int32(env, FAIL, &result2);
795 }
796 napi_set_element(env, result, ARR_NUM_1, result2);
797
798 // Null pointer with non-zero length
799 if (GetUnresolvedGlyphCount(nullptr, NUM_5, TEXT_ENCODING_UTF16) == NEGATIVE_NUM_1) {
800 napi_create_int32(env, SUCCESS, &result3);
801 } else {
802 napi_create_int32(env, FAIL, &result3);
803 }
804 napi_set_element(env, result, ARR_NUM_2, result3);
805 return result;
806 }
807