• 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 <string>
17 #include <fstream>
18 #include <filesystem>
19 #include <js_native_api.h>
20 #include "napi/native_api.h"
21 #include "hilog/log.h"
22 #include "native_drawing/drawing_bitmap.h"
23 #include "native_drawing/drawing_brush.h"
24 #include "native_drawing/drawing_canvas.h"
25 #include "native_drawing/drawing_color.h"
26 #include "native_drawing/drawing_font.h"
27 #include "native_drawing/drawing_font_collection.h"
28 #include "native_drawing/drawing_path.h"
29 #include "native_drawing/drawing_point.h"
30 #include "native_drawing/drawing_pen.h"
31 #include "native_drawing/drawing_rect.h"
32 #include "native_drawing/drawing_text_declaration.h"
33 #include "native_drawing/drawing_text_typography.h"
34 #include "native_drawing/drawing_text_font_descriptor.h"
35 #include "native_drawing/drawing_register_font.h"
36 
37 #define SUCCESS 0
38 #define FAIL (-1)
39 #define ARR_NUM_0 0
40 #define ARR_NUM_1 1
41 #define ARR_NUM_2 2
42 #define ARR_NUM_3 3
43 #define ARR_NUM_4 4
44 #define ALIGNMENT600 600
45 #define ALIGNMENT700 700
46 #define ALIGNMENT20 20
47 #define TEXTLINE30 30
48 #define TEXTLINE250 250
49 #define HEIGHT40 40
50 #define NUM_50 50
51 #define NUM_500 500.0
52 #define INT_NUM_2 2
53 #define INT_NUM_100 100
54 #define INT_NUM_200 200
55 #define INT_NUM_5 5
56 #define INT_NUM_400 400
57 #define INT_NUM_500 500
58 #define INT_NUM_142 142
59 #define DOUBLE_NUM_2 2.0
60 #define DOUBLE_NUM_0 0.0
61 #define DOUBLE_NEGATIVE_NUM_1 (-1.0)
62 #define DOUBLE_NUM_05 0.5
63 #define DOUBLE_NUM_100 100.0
64 #define DOUBLE_NUM_800 800.0
65 
66 static OH_Drawing_TypographyStyle *typoStyle_ = nullptr;
67 static OH_Drawing_TextStyle *txtStyle_ = nullptr;
68 static OH_Drawing_FontCollection *fontCollection_ = nullptr;
69 static OH_Drawing_TypographyCreate *handler_ = nullptr;
70 static OH_Drawing_Typography *typography_ = nullptr;
71 static OH_Drawing_Bitmap *cBitmap_ = nullptr;
72 static OH_Drawing_Canvas *canvas_ = nullptr;
73 static OH_Drawing_TypographyCreate *handler2_ = nullptr;
74 static OH_Drawing_FontCollection *fontCollection2_ = nullptr;
75 static OH_Drawing_TextStyle *txtStyle2_ = nullptr;
76 static OH_Drawing_TypographyStyle *typoStyle2_ = nullptr;
77 std::string g_text;
78 
PrepareCreateTextLine(const std::string & text)79 static void PrepareCreateTextLine(const std::string &text)
80 {
81     double maxWidth = 500.0;
82     uint32_t height = 40;
83     typoStyle_ = OH_Drawing_CreateTypographyStyle();
84     txtStyle_ = OH_Drawing_CreateTextStyle();
85     fontCollection_ = OH_Drawing_CreateFontCollection();
86     handler_ = OH_Drawing_CreateTypographyHandler(typoStyle_, fontCollection_);
87     OH_Drawing_SetTextStyleColor(txtStyle_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
88     double fontSize = 30;
89     OH_Drawing_SetTextStyleFontSize(txtStyle_, fontSize);
90     OH_Drawing_SetTextStyleFontWeight(txtStyle_, FONT_WEIGHT_400);
91     bool halfLeading = true;
92     OH_Drawing_SetTextStyleHalfLeading(txtStyle_, halfLeading);
93     const char *fontFamilies[] = {"Roboto"};
94     OH_Drawing_SetTextStyleFontFamilies(txtStyle_, 1, fontFamilies);
95     OH_Drawing_TypographyHandlerPushTextStyle(handler_, txtStyle_);
96     OH_Drawing_TypographyHandlerAddText(handler_, text.c_str());
97     OH_Drawing_TypographyHandlerPopTextStyle(handler_);
98     typography_ = OH_Drawing_CreateTypography(handler_);
99     OH_Drawing_TypographyLayout(typography_, maxWidth);
100     double position[2] = {10.0, 15.0};
101     cBitmap_ = OH_Drawing_BitmapCreate();
102     OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
103     uint32_t width = 20;
104     OH_Drawing_BitmapBuild(cBitmap_, width, height, &cFormat);
105     canvas_ = OH_Drawing_CanvasCreate();
106     OH_Drawing_CanvasBind(canvas_, cBitmap_);
107     OH_Drawing_CanvasClear(canvas_, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
108     OH_Drawing_TypographyPaint(typography_, canvas_, position[0], position[1]);
109 }
110 
TearDown()111 static void TearDown()
112 {
113     if (canvas_ != nullptr) {
114         OH_Drawing_CanvasDestroy(canvas_);
115         canvas_ = nullptr;
116     }
117     if (typography_ != nullptr) {
118         OH_Drawing_DestroyTypography(typography_);
119         typography_ = nullptr;
120     }
121     if (handler_ != nullptr) {
122         OH_Drawing_DestroyTypographyHandler(handler_);
123         handler_ = nullptr;
124     }
125     if (txtStyle_ != nullptr) {
126         OH_Drawing_DestroyTextStyle(txtStyle_);
127         txtStyle_ = nullptr;
128     }
129     if (typoStyle_ != nullptr) {
130         OH_Drawing_DestroyTypographyStyle(typoStyle_);
131         typoStyle_ = nullptr;
132     }
133     if (cBitmap_ != nullptr) {
134         OH_Drawing_BitmapDestroy(cBitmap_);
135         cBitmap_ = nullptr;
136     }
137     if (fontCollection_ != nullptr) {
138         OH_Drawing_DestroyFontCollection(fontCollection_);
139         fontCollection_ = nullptr;
140     }
141 }
142 
PrepareTypographyCreate(const char * text)143 static void PrepareTypographyCreate(const char *text)
144 {
145     fontCollection2_ = OH_Drawing_CreateFontCollection();
146     typoStyle2_ = OH_Drawing_CreateTypographyStyle();
147     handler2_ = OH_Drawing_CreateTypographyHandler(typoStyle2_, fontCollection2_);
148     txtStyle2_ = OH_Drawing_CreateTextStyle();
149     OH_Drawing_SetTextStyleColor(txtStyle2_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
150     double fontSize = 30;
151     OH_Drawing_SetTextStyleFontSize(txtStyle2_, fontSize);
152     OH_Drawing_SetTextStyleFontWeight(txtStyle2_, FONT_WEIGHT_400);
153     OH_Drawing_SetTextStyleBaseLine(txtStyle2_, TEXT_BASELINE_ALPHABETIC);
154     const char *fontFamilies[] = {"Roboto"};
155     OH_Drawing_SetTextStyleFontFamilies(txtStyle2_, 1, fontFamilies);
156     OH_Drawing_TypographyHandlerPushTextStyle(handler2_, txtStyle2_);
157     if (text != nullptr) {
158         OH_Drawing_TypographyHandlerAddText(handler2_, text);
159     }
160 }
161 
TypographyTearDown()162 static void TypographyTearDown()
163 {
164     if (handler2_ != nullptr) {
165         OH_Drawing_DestroyTypographyHandler(handler2_);
166         handler2_ = nullptr;
167     }
168     if (txtStyle2_ != nullptr) {
169         OH_Drawing_DestroyTextStyle(txtStyle2_);
170         txtStyle2_ = nullptr;
171     }
172     if (fontCollection2_ != nullptr) {
173         OH_Drawing_DestroyFontCollection(fontCollection2_);
174         fontCollection2_ = nullptr;
175     }
176     if (typoStyle2_ != nullptr) {
177         OH_Drawing_DestroyTypographyStyle(typoStyle2_);
178         typoStyle2_ = nullptr;
179     }
180 }
181 
OHDrawingGetFontDescriptorByFullName001(napi_env env,napi_callback_info info)182 static napi_value OHDrawingGetFontDescriptorByFullName001(napi_env env, napi_callback_info info)
183 {
184     napi_value result = nullptr;
185     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType(0b10000);
186     OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(nullptr, fontType);
187     if (descriptor == nullptr) {
188         napi_create_int32(env, SUCCESS, &result);
189     } else {
190         napi_create_int32(env, FAIL, &result);
191     }
192     return result;
193 }
194 
OHDrawingGetFontDescriptorByFullName002(napi_env env,napi_callback_info info)195 static napi_value OHDrawingGetFontDescriptorByFullName002(napi_env env, napi_callback_info info)
196 {
197     napi_value result = nullptr;
198     const uint8_t ttfFullname[] = {
199         0x4F, 0x60,
200         0x59, 0x7D,
201         0x00, 0x6F,
202         0x00, 0x70,
203         0x00, 0x65,
204         0x00, 0x6E,
205         0x00, 0x68,
206         0x00, 0x61,
207         0x00, 0x72,
208         0x00, 0x6D,
209         0x00, 0x6F,
210         0x00, 0x6E,
211         0x00, 0x79
212     };
213     OH_Drawing_String drawingString;
214     drawingString.strData = const_cast<uint8_t*>(ttfFullname);
215     drawingString.strLen = sizeof(ttfFullname);
216     OH_Drawing_FontDescriptor *descriptor =
217         OH_Drawing_GetFontDescriptorByFullName(&drawingString, OH_Drawing_SystemFontType::ALL);
218     if (descriptor == nullptr) {
219         napi_create_int32(env, SUCCESS, &result);
220     } else {
221         napi_create_int32(env, FAIL, &result);
222     }
223     return result;
224 }
225 
OHDrawingGetFontDescriptorByFullName003(napi_env env,napi_callback_info info)226 static napi_value OHDrawingGetFontDescriptorByFullName003(napi_env env, napi_callback_info info)
227 {
228     napi_value result = nullptr;
229     napi_create_array_with_length(env, ARR_NUM_4, &result);
230     napi_value result1 = nullptr;
231     napi_value result2 = nullptr;
232     napi_value result3 = nullptr;
233     napi_value result4 = nullptr;
234 
235     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::GENERIC;
236     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
237     if (fontList != nullptr) {
238         napi_create_int32(env, SUCCESS, &result1);
239     } else {
240         napi_create_int32(env, FAIL, &result1);
241     }
242     napi_set_element(env, result, ARR_NUM_0, result1);
243 
244     size_t size = OH_Drawing_GetDrawingArraySize(fontList);
245     if (size != 0) {
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     for (size_t i = 0; i < size; i++) {
253         const OH_Drawing_String *fontFullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
254         if (fontFullName != nullptr) {
255             napi_create_int32(env, SUCCESS, &result3);
256         } else {
257             napi_create_int32(env, FAIL, &result3);
258         }
259         napi_set_element(env, result, ARR_NUM_2, result3);
260 
261         OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(fontFullName, fontType);
262         if (descriptor != nullptr) {
263             napi_create_int32(env, SUCCESS, &result4);
264         } else {
265             napi_create_int32(env, FAIL, &result4);
266         }
267         napi_set_element(env, result, ARR_NUM_3, result4);
268         OH_Drawing_DestroyFontDescriptor(descriptor);
269     }
270     OH_Drawing_DestroySystemFontFullNames(fontList);
271     return result;
272 }
273 
OHDrawingGetFontDescriptorByFullName004(napi_env env,napi_callback_info info)274 static napi_value OHDrawingGetFontDescriptorByFullName004(napi_env env, napi_callback_info info)
275 {
276     napi_value result = nullptr;
277     napi_create_array_with_length(env, ARR_NUM_4, &result);
278     napi_value result1 = nullptr;
279     napi_value result2 = nullptr;
280     napi_value result3 = nullptr;
281     napi_value result4 = nullptr;
282 
283     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType(ALL | STYLISH);
284     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
285     if (fontList != nullptr) {
286         napi_create_int32(env, SUCCESS, &result1);
287     } else {
288         napi_create_int32(env, FAIL, &result1);
289     }
290     napi_set_element(env, result, ARR_NUM_0, result1);
291 
292     size_t size = OH_Drawing_GetDrawingArraySize(fontList);
293     if (size != 0) {
294         napi_create_int32(env, SUCCESS, &result2);
295     } else {
296         napi_create_int32(env, FAIL, &result2);
297     }
298     napi_set_element(env, result, ARR_NUM_1, result2);
299 
300     for (size_t i = 0; i < size; i++) {
301         const OH_Drawing_String *fontFullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, i);
302         if (fontFullName != nullptr) {
303             napi_create_int32(env, SUCCESS, &result3);
304         } else {
305             napi_create_int32(env, FAIL, &result3);
306         }
307         napi_set_element(env, result, ARR_NUM_2, result3);
308 
309         OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(fontFullName, fontType);
310         if (descriptor != nullptr) {
311             napi_create_int32(env, SUCCESS, &result4);
312         } else {
313             napi_create_int32(env, FAIL, &result4);
314         }
315         napi_set_element(env, result, ARR_NUM_3, result4);
316         OH_Drawing_DestroyFontDescriptor(descriptor);
317     }
318     OH_Drawing_DestroySystemFontFullNames(fontList);
319     return result;
320 }
321 
OHDrawingGetSystemFontFullNamesByType001(napi_env env,napi_callback_info info)322 static napi_value OHDrawingGetSystemFontFullNamesByType001(napi_env env, napi_callback_info info)
323 {
324     napi_value result = nullptr;
325     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType(0b10000);
326     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
327     if (fontList == nullptr) {
328         napi_create_int32(env, SUCCESS, &result);
329     } else {
330         napi_create_int32(env, FAIL, &result);
331     }
332     return result;
333 }
334 
OHDrawingGetSystemFontFullNamesByType002(napi_env env,napi_callback_info info)335 static napi_value OHDrawingGetSystemFontFullNamesByType002(napi_env env, napi_callback_info info)
336 {
337     napi_value result = nullptr;
338     napi_create_array_with_length(env, ARR_NUM_4, &result);
339     napi_value result1 = nullptr;
340     napi_value result2 = nullptr;
341     napi_value result3 = nullptr;
342     napi_value result4 = nullptr;
343 
344     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::GENERIC;
345     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
346     if (fontList != nullptr) {
347         napi_create_int32(env, SUCCESS, &result1);
348     } else {
349         napi_create_int32(env, FAIL, &result1);
350     }
351     napi_set_element(env, result, ARR_NUM_0, result1);
352 
353     const OH_Drawing_String *fullName = OH_Drawing_GetSystemFontFullNameByIndex(fontList, INT_NUM_500);
354     if (fullName == nullptr) {
355         napi_create_int32(env, SUCCESS, &result2);
356     } else {
357         napi_create_int32(env, FAIL, &result2);
358     }
359     OH_Drawing_DestroySystemFontFullNames(fontList);
360     napi_set_element(env, result, ARR_NUM_1, result2);
361 
362     const OH_Drawing_String *fullName1 = OH_Drawing_GetSystemFontFullNameByIndex(nullptr, 0);
363     if (fullName1 == nullptr) {
364         napi_create_int32(env, SUCCESS, &result3);
365     } else {
366         napi_create_int32(env, FAIL, &result3);
367     }
368     napi_set_element(env, result, ARR_NUM_2, result3);
369 
370     const OH_Drawing_String *fullName2 = OH_Drawing_GetSystemFontFullNameByIndex(nullptr, INT_NUM_500);
371     if (fullName2 == nullptr) {
372         napi_create_int32(env, SUCCESS, &result4);
373     } else {
374         napi_create_int32(env, FAIL, &result4);
375     }
376     napi_set_element(env, result, ARR_NUM_3, result4);
377     OH_Drawing_DestroySystemFontFullNames(nullptr);
378     return result;
379 }
380 
OHDrawingGetSystemFontFullNameByIndex001(napi_env env,napi_callback_info info)381 static napi_value OHDrawingGetSystemFontFullNameByIndex001(napi_env env, napi_callback_info info)
382 {
383     napi_value result = nullptr;
384     const OH_Drawing_String *fullName = OH_Drawing_GetSystemFontFullNameByIndex(nullptr, 0 | 500);
385     if (fullName == nullptr) {
386         napi_create_int32(env, SUCCESS, &result);
387     } else {
388         napi_create_int32(env, FAIL, &result);
389     }
390     OH_Drawing_DestroySystemFontFullNames(nullptr);
391     return result;
392 }
393 
OHDrawingGetDrawingArraySize001(napi_env env,napi_callback_info info)394 static napi_value OHDrawingGetDrawingArraySize001(napi_env env, napi_callback_info info)
395 {
396     napi_value result = nullptr;
397     OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::GENERIC;
398     OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
399     size_t size = OH_Drawing_GetDrawingArraySize(fontList);
400     if (size != 0) {
401         napi_create_int32(env, SUCCESS, &result);
402     } else {
403         napi_create_int32(env, FAIL, &result);
404     }
405     OH_Drawing_DestroySystemFontFullNames(fontList);
406     return result;
407 }
408 
OHDrawingGetFontCollectionGlobalInstance001(napi_env env,napi_callback_info info)409 static napi_value OHDrawingGetFontCollectionGlobalInstance001(napi_env env, napi_callback_info info)
410 {
411     napi_value result = nullptr;
412     OH_Drawing_FontCollection *fontCollection = OH_Drawing_GetFontCollectionGlobalInstance();
413     if (fontCollection != nullptr) {
414         napi_create_int32(env, SUCCESS, &result);
415     } else {
416         napi_create_int32(env, FAIL, &result);
417     }
418     return result;
419 }
420 
421 EXTERN_C_START
Init(napi_env env,napi_value exports)422 static napi_value Init(napi_env env, napi_value exports)
423 {
424     napi_property_descriptor desc[] = {
425         {"oHDrawingGetFontDescriptorByFullName001", nullptr, OHDrawingGetFontDescriptorByFullName001,
426          nullptr, nullptr, nullptr, napi_default, nullptr},
427         {"oHDrawingGetFontDescriptorByFullName002", nullptr, OHDrawingGetFontDescriptorByFullName002,
428          nullptr, nullptr, nullptr, napi_default, nullptr},
429         {"oHDrawingGetFontDescriptorByFullName003", nullptr, OHDrawingGetFontDescriptorByFullName003,
430          nullptr, nullptr, nullptr, napi_default, nullptr},
431         {"oHDrawingGetFontDescriptorByFullName004", nullptr, OHDrawingGetFontDescriptorByFullName004,
432          nullptr, nullptr, nullptr, napi_default, nullptr},
433         {"oHDrawingGetSystemFontFullNamesByType001", nullptr, OHDrawingGetSystemFontFullNamesByType001,
434          nullptr, nullptr, nullptr, napi_default, nullptr},
435         {"oHDrawingGetSystemFontFullNamesByType002", nullptr, OHDrawingGetSystemFontFullNamesByType002,
436          nullptr, nullptr, nullptr, napi_default, nullptr},
437         {"oHDrawingGetSystemFontFullNameByIndex001", nullptr, OHDrawingGetSystemFontFullNameByIndex001,
438          nullptr, nullptr, nullptr, napi_default, nullptr},
439         {"oHDrawingGetDrawingArraySize001", nullptr, OHDrawingGetDrawingArraySize001,
440          nullptr, nullptr, nullptr, napi_default, nullptr},
441         {"oHDrawingGetFontCollectionGlobalInstance001", nullptr, OHDrawingGetFontCollectionGlobalInstance001,
442          nullptr, nullptr, nullptr, napi_default, nullptr},
443     };
444     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
445     return exports;
446 }
447 
448 EXTERN_C_END
449 
450 static napi_module demoModule = {
451     .nm_version = 1,
452     .nm_flags = 0,
453     .nm_filename = nullptr,
454     .nm_register_func = Init,
455     .nm_modname = "nativeFontNdk",
456     .nm_priv = ((void *)0),
457     .reserved = {0},
458 };
459 
RegisterEntryModule(void)460 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }