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
36 #define SUCCESS 0
37 #define FAIL (-1)
38 #define ALIGNMENT600 600
39 #define ALIGNMENT700 700
40 #define ALIGNMENT20 20
41 #define TEXTLINE30 30
42 #define TEXTLINE250 250
43 #define HEIGHT40 40
44 #define NUM_50 50
45 #define NUM_500 500.0
46 #define INT_NUM_2 2
47 #define INT_NUM_100 100
48 #define INT_NUM_200 200
49 #define INT_NUM_5 5
50 #define DOUBLE_NUM_2 2.0
51 #define DOUBLE_NUM_0 0.0
52 #define DOUBLE_NEGATIVE_NUM_1 (-1.0)
53 #define DOUBLE_NUM_05 0.5
54 #define DOUBLE_NUM_100 100.0
55 #define DOUBLE_NUM_800 800.0
56
57 static OH_Drawing_TypographyStyle *typoStyle_ = nullptr;
58 static OH_Drawing_TextStyle *txtStyle_ = nullptr;
59 static OH_Drawing_FontCollection *fontCollection_ = nullptr;
60 static OH_Drawing_TypographyCreate *handler_ = nullptr;
61 static OH_Drawing_Typography *typography_ = nullptr;
62 static OH_Drawing_Bitmap *cBitmap_ = nullptr;
63 static OH_Drawing_Canvas *canvas_ = nullptr;
64 static OH_Drawing_TypographyCreate *handler2_ = nullptr;
65 static OH_Drawing_FontCollection *fontCollection2_ = nullptr;
66 static OH_Drawing_TextStyle *txtStyle2_ = nullptr;
67 static OH_Drawing_TypographyStyle *typoStyle2_ = nullptr;
68 std::string g_text;
69
PrepareCreateTextLine(const std::string & text)70 static void PrepareCreateTextLine(const std::string &text)
71 {
72 double maxWidth = 500.0;
73 uint32_t height = 40;
74 typoStyle_ = OH_Drawing_CreateTypographyStyle();
75 txtStyle_ = OH_Drawing_CreateTextStyle();
76 fontCollection_ = OH_Drawing_CreateFontCollection();
77 handler_ = OH_Drawing_CreateTypographyHandler(typoStyle_, fontCollection_);
78 OH_Drawing_SetTextStyleColor(txtStyle_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
79 double fontSize = 30;
80 OH_Drawing_SetTextStyleFontSize(txtStyle_, fontSize);
81 OH_Drawing_SetTextStyleFontWeight(txtStyle_, FONT_WEIGHT_400);
82 bool halfLeading = true;
83 OH_Drawing_SetTextStyleHalfLeading(txtStyle_, halfLeading);
84 const char *fontFamilies[] = {"Roboto"};
85 OH_Drawing_SetTextStyleFontFamilies(txtStyle_, 1, fontFamilies);
86 OH_Drawing_TypographyHandlerPushTextStyle(handler_, txtStyle_);
87 OH_Drawing_TypographyHandlerAddText(handler_, text.c_str());
88 OH_Drawing_TypographyHandlerPopTextStyle(handler_);
89 typography_ = OH_Drawing_CreateTypography(handler_);
90 OH_Drawing_TypographyLayout(typography_, maxWidth);
91 double position[2] = {10.0, 15.0};
92 cBitmap_ = OH_Drawing_BitmapCreate();
93 OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
94 uint32_t width = 20;
95 OH_Drawing_BitmapBuild(cBitmap_, width, height, &cFormat);
96 canvas_ = OH_Drawing_CanvasCreate();
97 OH_Drawing_CanvasBind(canvas_, cBitmap_);
98 OH_Drawing_CanvasClear(canvas_, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
99 OH_Drawing_TypographyPaint(typography_, canvas_, position[0], position[1]);
100 }
101
TearDown()102 static void TearDown()
103 {
104 if (canvas_ != nullptr) {
105 OH_Drawing_CanvasDestroy(canvas_);
106 canvas_ = nullptr;
107 }
108 if (typography_ != nullptr) {
109 OH_Drawing_DestroyTypography(typography_);
110 typography_ = nullptr;
111 }
112 if (handler_ != nullptr) {
113 OH_Drawing_DestroyTypographyHandler(handler_);
114 handler_ = nullptr;
115 }
116 if (txtStyle_ != nullptr) {
117 OH_Drawing_DestroyTextStyle(txtStyle_);
118 txtStyle_ = nullptr;
119 }
120 if (typoStyle_ != nullptr) {
121 OH_Drawing_DestroyTypographyStyle(typoStyle_);
122 typoStyle_ = nullptr;
123 }
124 if (cBitmap_ != nullptr) {
125 OH_Drawing_BitmapDestroy(cBitmap_);
126 cBitmap_ = nullptr;
127 }
128 if (fontCollection_ != nullptr) {
129 OH_Drawing_DestroyFontCollection(fontCollection_);
130 fontCollection_ = nullptr;
131 }
132 }
133
PrepareTypographyCreate(const char * text)134 static void PrepareTypographyCreate(const char *text)
135 {
136 fontCollection2_ = OH_Drawing_CreateFontCollection();
137 typoStyle2_ = OH_Drawing_CreateTypographyStyle();
138 handler2_ = OH_Drawing_CreateTypographyHandler(typoStyle2_, fontCollection2_);
139 txtStyle2_ = OH_Drawing_CreateTextStyle();
140 OH_Drawing_SetTextStyleColor(txtStyle2_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
141 double fontSize = 30;
142 OH_Drawing_SetTextStyleFontSize(txtStyle2_, fontSize);
143 OH_Drawing_SetTextStyleFontWeight(txtStyle2_, FONT_WEIGHT_400);
144 OH_Drawing_SetTextStyleBaseLine(txtStyle2_, TEXT_BASELINE_ALPHABETIC);
145 const char *fontFamilies[] = {"Roboto"};
146 OH_Drawing_SetTextStyleFontFamilies(txtStyle2_, 1, fontFamilies);
147 OH_Drawing_TypographyHandlerPushTextStyle(handler2_, txtStyle2_);
148 if (text != nullptr) {
149 OH_Drawing_TypographyHandlerAddText(handler2_, text);
150 }
151 }
152
TypographyTearDown()153 static void TypographyTearDown()
154 {
155 if (handler2_ != nullptr) {
156 OH_Drawing_DestroyTypographyHandler(handler2_);
157 handler2_ = nullptr;
158 }
159 if (txtStyle2_ != nullptr) {
160 OH_Drawing_DestroyTextStyle(txtStyle2_);
161 txtStyle2_ = nullptr;
162 }
163 if (fontCollection2_ != nullptr) {
164 OH_Drawing_DestroyFontCollection(fontCollection2_);
165 fontCollection2_ = nullptr;
166 }
167 if (typoStyle2_ != nullptr) {
168 OH_Drawing_DestroyTypographyStyle(typoStyle2_);
169 typoStyle2_ = nullptr;
170 }
171 }
172
OHDrawingGetFontDescriptorByFullName001(napi_env env,napi_callback_info info)173 static napi_value OHDrawingGetFontDescriptorByFullName001(napi_env env, napi_callback_info info)
174 {
175 napi_value result = nullptr;
176 OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType(0b10000);
177 OH_Drawing_FontDescriptor *descriptor = OH_Drawing_GetFontDescriptorByFullName(nullptr, fontType);
178 if (descriptor == nullptr) {
179 napi_create_int32(env, SUCCESS, &result);
180 } else {
181 napi_create_int32(env, FAIL, &result);
182 }
183 return result;
184 }
185
OHDrawingGetSystemFontFullNamesByType001(napi_env env,napi_callback_info info)186 static napi_value OHDrawingGetSystemFontFullNamesByType001(napi_env env, napi_callback_info info)
187 {
188 napi_value result = nullptr;
189 OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType(0b10000);
190 OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
191 if (fontList == nullptr) {
192 napi_create_int32(env, SUCCESS, &result);
193 } else {
194 napi_create_int32(env, FAIL, &result);
195 }
196 return result;
197 }
198
OHDrawingGetSystemFontFullNameByIndex001(napi_env env,napi_callback_info info)199 static napi_value OHDrawingGetSystemFontFullNameByIndex001(napi_env env, napi_callback_info info)
200 {
201 napi_value result = nullptr;
202 const OH_Drawing_String *fullName = OH_Drawing_GetSystemFontFullNameByIndex(nullptr, 0 | 500);
203 if (fullName == nullptr) {
204 napi_create_int32(env, SUCCESS, &result);
205 } else {
206 napi_create_int32(env, FAIL, &result);
207 }
208 OH_Drawing_DestroySystemFontFullNames(nullptr);
209 return result;
210 }
211
OHDrawingGetDrawingArraySize001(napi_env env,napi_callback_info info)212 static napi_value OHDrawingGetDrawingArraySize001(napi_env env, napi_callback_info info)
213 {
214 napi_value result = nullptr;
215 OH_Drawing_SystemFontType fontType = OH_Drawing_SystemFontType::GENERIC;
216 OH_Drawing_Array *fontList = OH_Drawing_GetSystemFontFullNamesByType(fontType);
217 size_t size = OH_Drawing_GetDrawingArraySize(fontList);
218 if (size != 0) {
219 napi_create_int32(env, SUCCESS, &result);
220 } else {
221 napi_create_int32(env, FAIL, &result);
222 }
223 OH_Drawing_DestroySystemFontFullNames(fontList);
224 return result;
225 }
226
OHDrawingGetFontCollectionGlobalInstance001(napi_env env,napi_callback_info info)227 static napi_value OHDrawingGetFontCollectionGlobalInstance001(napi_env env, napi_callback_info info)
228 {
229 napi_value result = nullptr;
230 OH_Drawing_FontCollection *fontCollection = OH_Drawing_GetFontCollectionGlobalInstance();
231 if (fontCollection != nullptr) {
232 napi_create_int32(env, SUCCESS, &result);
233 } else {
234 napi_create_int32(env, FAIL, &result);
235 }
236 return result;
237 }
238
239 EXTERN_C_START
Init(napi_env env,napi_value exports)240 static napi_value Init(napi_env env, napi_value exports)
241 {
242 napi_property_descriptor desc[] = {
243 {"oHDrawingGetFontDescriptorByFullName001", nullptr, OHDrawingGetFontDescriptorByFullName001,
244 nullptr, nullptr, nullptr, napi_default, nullptr},
245 {"oHDrawingGetSystemFontFullNamesByType001", nullptr, OHDrawingGetSystemFontFullNamesByType001,
246 nullptr, nullptr, nullptr, napi_default, nullptr},
247 {"oHDrawingGetSystemFontFullNameByIndex001", nullptr, OHDrawingGetSystemFontFullNameByIndex001,
248 nullptr, nullptr, nullptr, napi_default, nullptr},
249 {"oHDrawingGetDrawingArraySize001", nullptr, OHDrawingGetDrawingArraySize001,
250 nullptr, nullptr, nullptr, napi_default, nullptr},
251 {"oHDrawingGetFontCollectionGlobalInstance001", nullptr, OHDrawingGetFontCollectionGlobalInstance001,
252 nullptr, nullptr, nullptr, napi_default, nullptr},
253 };
254 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
255 return exports;
256 }
257
258 EXTERN_C_END
259
260 static napi_module demoModule = {
261 .nm_version = 1,
262 .nm_flags = 0,
263 .nm_filename = nullptr,
264 .nm_register_func = Init,
265 .nm_modname = "nativeFontNdk",
266 .nm_priv = ((void *)0),
267 .reserved = {0},
268 };
269
RegisterEntryModule(void)270 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }