1 /*
2 * Copyright (c) 2021-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 "drawing_text_typography.h"
17
18 #include <codecvt>
19 #include <locale>
20 #include <shared_mutex>
21 #include <string>
22 #include <unicode/brkiter.h>
23 #include <vector>
24
25 #include "array_mgr.h"
26 #include "font_config.h"
27 #include "font_parser.h"
28 #include "font_utils.h"
29 #include "txt/text_bundle_config_parser.h"
30 #include "rosen_text/font_collection.h"
31 #include "rosen_text/typography.h"
32 #include "rosen_text/typography_create.h"
33 #include "unicode/putil.h"
34
35 #include "utils/log.h"
36 #include "utils/object_mgr.h"
37 #include "utils/string_util.h"
38
39 using namespace OHOS::Rosen;
40
41 namespace {
Init()42 __attribute__((constructor)) void Init()
43 {
44 #ifndef _WIN32
45 u_setDataDirectory("/system/usr/ohos_icu");
46 #else
47 u_setDataDirectory(".");
48 #endif
49 }
50 } // namespace
51
52 static std::shared_ptr<OHOS::Rosen::Drawing::ObjectMgr> objectMgr = OHOS::Rosen::Drawing::ObjectMgr::GetInstance();
53 static std::map<void*, size_t> arrSizeMgr;
54 static std::shared_mutex arrSizeMgrMutex;
55
GetArrSizeFromMgr(void * arrPtr)56 static size_t GetArrSizeFromMgr(void* arrPtr)
57 {
58 std::shared_lock<std::shared_mutex> lock(arrSizeMgrMutex);
59 auto itr = arrSizeMgr.find(arrPtr);
60 return itr != arrSizeMgr.end() ? itr->second : 0;
61 }
62
MgrSetArrSize(void * arrPtr,size_t arrSize)63 static void MgrSetArrSize(void* arrPtr, size_t arrSize)
64 {
65 std::unique_lock<std::shared_mutex> lock(arrSizeMgrMutex);
66 arrSizeMgr[arrPtr] = arrSize;
67 }
68
MgrRemoveSize(void * arrPtr)69 static void MgrRemoveSize(void* arrPtr)
70 {
71 std::unique_lock<std::shared_mutex> lock(arrSizeMgrMutex);
72 arrSizeMgr.erase(arrPtr);
73 }
74
75 template<typename T1, typename T2>
ConvertToOriginalText(T2 * ptr)76 inline T1* ConvertToOriginalText(T2* ptr)
77 {
78 return reinterpret_cast<T1*>(ptr);
79 }
80
81 template<typename T1, typename T2>
ConvertToNDKText(T2 * ptr)82 inline T1* ConvertToNDKText(T2* ptr)
83 {
84 return reinterpret_cast<T1*>(ptr);
85 }
86
OH_Drawing_CreateTypographyStyle(void)87 OH_Drawing_TypographyStyle* OH_Drawing_CreateTypographyStyle(void)
88 {
89 return (OH_Drawing_TypographyStyle*)new (std::nothrow) TypographyStyle;
90 }
91
OH_Drawing_DestroyTypographyStyle(OH_Drawing_TypographyStyle * style)92 void OH_Drawing_DestroyTypographyStyle(OH_Drawing_TypographyStyle* style)
93 {
94 delete ConvertToOriginalText<TypographyStyle>(style);
95 }
96
OH_Drawing_SetTypographyTextDirection(OH_Drawing_TypographyStyle * style,int direction)97 void OH_Drawing_SetTypographyTextDirection(OH_Drawing_TypographyStyle* style, int direction)
98 {
99 if (!style) {
100 return;
101 }
102 TextDirection textDirection;
103 switch (direction) {
104 case TEXT_DIRECTION_RTL: {
105 textDirection = TextDirection::RTL;
106 break;
107 }
108 case TEXT_DIRECTION_LTR: {
109 textDirection = TextDirection::LTR;
110 break;
111 }
112 default: {
113 textDirection = TextDirection::LTR;
114 break;
115 }
116 }
117 ConvertToOriginalText<TypographyStyle>(style)->textDirection = textDirection;
118 }
119
OH_Drawing_SetTypographyTextAlign(OH_Drawing_TypographyStyle * style,int align)120 void OH_Drawing_SetTypographyTextAlign(OH_Drawing_TypographyStyle* style, int align)
121 {
122 if (!style) {
123 return;
124 }
125 TextAlign textAlign;
126 switch (align) {
127 case TEXT_ALIGN_LEFT: {
128 textAlign = TextAlign::LEFT;
129 break;
130 }
131 case TEXT_ALIGN_RIGHT: {
132 textAlign = TextAlign::RIGHT;
133 break;
134 }
135 case TEXT_ALIGN_CENTER: {
136 textAlign = TextAlign::CENTER;
137 break;
138 }
139 case TEXT_ALIGN_JUSTIFY: {
140 textAlign = TextAlign::JUSTIFY;
141 break;
142 }
143 case TEXT_ALIGN_START: {
144 textAlign = TextAlign::START;
145 break;
146 }
147 case TEXT_ALIGN_END: {
148 textAlign = TextAlign::END;
149 break;
150 }
151 default: {
152 textAlign = TextAlign::LEFT;
153 }
154 }
155 ConvertToOriginalText<TypographyStyle>(style)->textAlign = textAlign;
156 }
157
OH_Drawing_SetTypographyTextMaxLines(OH_Drawing_TypographyStyle * style,int lineNumber)158 void OH_Drawing_SetTypographyTextMaxLines(OH_Drawing_TypographyStyle* style, int lineNumber)
159 {
160 if (!style) {
161 return;
162 }
163 lineNumber = lineNumber < 0 ? 0 : lineNumber;
164 ConvertToOriginalText<TypographyStyle>(style)->maxLines = static_cast<size_t>(lineNumber);
165 }
166
OH_Drawing_CreateTextStyle(void)167 OH_Drawing_TextStyle* OH_Drawing_CreateTextStyle(void)
168 {
169 return (OH_Drawing_TextStyle*)new (std::nothrow) TextStyle;
170 }
171
OH_Drawing_DestroyTextStyle(OH_Drawing_TextStyle * style)172 void OH_Drawing_DestroyTextStyle(OH_Drawing_TextStyle* style)
173 {
174 delete ConvertToOriginalText<TextStyle>(style);
175 }
176
OH_Drawing_SetTextStyleColor(OH_Drawing_TextStyle * style,uint32_t color)177 void OH_Drawing_SetTextStyleColor(OH_Drawing_TextStyle* style, uint32_t color)
178 {
179 if (!style) {
180 return;
181 }
182 ConvertToOriginalText<TextStyle>(style)->color.SetColorQuad(color);
183 }
184
OH_Drawing_SetTextStyleFontSize(OH_Drawing_TextStyle * style,double fontSize)185 void OH_Drawing_SetTextStyleFontSize(OH_Drawing_TextStyle* style, double fontSize)
186 {
187 if (!style) {
188 return;
189 }
190 ConvertToOriginalText<TextStyle>(style)->fontSize = fontSize;
191 }
192
GetFontWeight(int fontWeight)193 static FontWeight GetFontWeight(int fontWeight)
194 {
195 FontWeight rosenFontWeight;
196 switch (fontWeight) {
197 case FONT_WEIGHT_100: {
198 rosenFontWeight = FontWeight::W100;
199 break;
200 }
201 case FONT_WEIGHT_200: {
202 rosenFontWeight = FontWeight::W200;
203 break;
204 }
205 case FONT_WEIGHT_300: {
206 rosenFontWeight = FontWeight::W300;
207 break;
208 }
209 case FONT_WEIGHT_400: {
210 rosenFontWeight = FontWeight::W400;
211 break;
212 }
213 case FONT_WEIGHT_500: {
214 rosenFontWeight = FontWeight::W500;
215 break;
216 }
217 case FONT_WEIGHT_600: {
218 rosenFontWeight = FontWeight::W600;
219 break;
220 }
221 case FONT_WEIGHT_700: {
222 rosenFontWeight = FontWeight::W700;
223 break;
224 }
225 case FONT_WEIGHT_800: {
226 rosenFontWeight = FontWeight::W800;
227 break;
228 }
229 case FONT_WEIGHT_900: {
230 rosenFontWeight = FontWeight::W900;
231 break;
232 }
233 default: {
234 rosenFontWeight = FontWeight::W400;
235 }
236 }
237 return rosenFontWeight;
238 }
239
OH_Drawing_SetTextStyleFontWeight(OH_Drawing_TextStyle * style,int fontWeight)240 void OH_Drawing_SetTextStyleFontWeight(OH_Drawing_TextStyle* style, int fontWeight)
241 {
242 if (!style) {
243 return;
244 }
245 FontWeight rosenFontWeight = GetFontWeight(fontWeight);
246 ConvertToOriginalText<TextStyle>(style)->fontWeight = rosenFontWeight;
247 }
248
OH_Drawing_SetTextStyleBaseLine(OH_Drawing_TextStyle * style,int baseline)249 void OH_Drawing_SetTextStyleBaseLine(OH_Drawing_TextStyle* style, int baseline)
250 {
251 if (!style) {
252 return;
253 }
254 TextBaseline rosenBaseLine;
255 switch (baseline) {
256 case TEXT_BASELINE_ALPHABETIC: {
257 rosenBaseLine = TextBaseline::ALPHABETIC;
258 break;
259 }
260 case TEXT_BASELINE_IDEOGRAPHIC: {
261 rosenBaseLine = TextBaseline::IDEOGRAPHIC;
262 break;
263 }
264 default: {
265 rosenBaseLine = TextBaseline::ALPHABETIC;
266 }
267 }
268 ConvertToOriginalText<TextStyle>(style)->baseline = rosenBaseLine;
269 }
270
OH_Drawing_SetTextStyleDecoration(OH_Drawing_TextStyle * style,int decoration)271 void OH_Drawing_SetTextStyleDecoration(OH_Drawing_TextStyle* style, int decoration)
272 {
273 if (!style) {
274 return;
275 }
276 TextDecoration rosenDecoration;
277 switch (decoration) {
278 case TEXT_DECORATION_NONE: {
279 rosenDecoration = TextDecoration::NONE;
280 break;
281 }
282 case TEXT_DECORATION_UNDERLINE: {
283 rosenDecoration = TextDecoration::UNDERLINE;
284 break;
285 }
286 case TEXT_DECORATION_OVERLINE: {
287 rosenDecoration = TextDecoration::OVERLINE;
288 break;
289 }
290 case TEXT_DECORATION_LINE_THROUGH: {
291 rosenDecoration = TextDecoration::LINE_THROUGH;
292 break;
293 }
294 default: {
295 rosenDecoration = TextDecoration::NONE;
296 }
297 }
298 ConvertToOriginalText<TextStyle>(style)->decoration = rosenDecoration;
299 }
300
OH_Drawing_AddTextStyleDecoration(OH_Drawing_TextStyle * style,int decoration)301 void OH_Drawing_AddTextStyleDecoration(OH_Drawing_TextStyle* style, int decoration)
302 {
303 if (decoration < 0) {
304 return;
305 }
306 unsigned int uintDecoration = static_cast<unsigned int>(decoration);
307 if (style == nullptr || (uintDecoration & (~(TextDecoration::UNDERLINE | TextDecoration::OVERLINE |
308 TextDecoration::LINE_THROUGH)))) {
309 return;
310 }
311 TextStyle* rosenTextStyle = ConvertToOriginalText<TextStyle>(style);
312 if (rosenTextStyle) {
313 rosenTextStyle->decoration = static_cast<TextDecoration>(rosenTextStyle->decoration | uintDecoration);
314 }
315 }
316
OH_Drawing_RemoveTextStyleDecoration(OH_Drawing_TextStyle * style,int decoration)317 void OH_Drawing_RemoveTextStyleDecoration(OH_Drawing_TextStyle* style, int decoration)
318 {
319 if (decoration < 0) {
320 return;
321 }
322 unsigned int uintDecoration = static_cast<unsigned int>(decoration);
323 if (style == nullptr || (uintDecoration & (~(TextDecoration::UNDERLINE | TextDecoration::OVERLINE |
324 TextDecoration::LINE_THROUGH)))) {
325 return;
326 }
327 TextStyle* rosenTextStyle = ConvertToOriginalText<TextStyle>(style);
328 if (rosenTextStyle) {
329 rosenTextStyle->decoration = static_cast<TextDecoration>(rosenTextStyle->decoration & ~uintDecoration);
330 }
331 }
332
OH_Drawing_SetTextStyleDecorationColor(OH_Drawing_TextStyle * style,uint32_t color)333 void OH_Drawing_SetTextStyleDecorationColor(OH_Drawing_TextStyle* style, uint32_t color)
334 {
335 if (!style) {
336 return;
337 }
338 ConvertToOriginalText<TextStyle>(style)->decorationColor.SetColorQuad(color);
339 }
340
OH_Drawing_SetTextStyleFontHeight(OH_Drawing_TextStyle * style,double fontHeight)341 void OH_Drawing_SetTextStyleFontHeight(OH_Drawing_TextStyle* style, double fontHeight)
342 {
343 if (!style) {
344 return;
345 }
346 ConvertToOriginalText<TextStyle>(style)->heightScale = fontHeight;
347 ConvertToOriginalText<TextStyle>(style)->heightOnly = true;
348 }
349
OH_Drawing_SetTextStyleFontFamilies(OH_Drawing_TextStyle * style,int fontFamiliesNumber,const char * fontFamilies[])350 void OH_Drawing_SetTextStyleFontFamilies(
351 OH_Drawing_TextStyle* style, int fontFamiliesNumber, const char* fontFamilies[])
352 {
353 if (style == nullptr || fontFamilies == nullptr) {
354 return;
355 }
356 std::vector<std::string> rosenFontFamilies;
357 for (int i = 0; i < fontFamiliesNumber; i++) {
358 if (fontFamilies[i]) {
359 rosenFontFamilies.emplace_back(fontFamilies[i]);
360 } else {
361 LOGE("Null fontFamilies[%{public}d]", i);
362 return;
363 }
364 }
365 ConvertToOriginalText<TextStyle>(style)->fontFamilies = rosenFontFamilies;
366 }
367
OH_Drawing_SetTextStyleFontStyle(OH_Drawing_TextStyle * style,int fontStyle)368 void OH_Drawing_SetTextStyleFontStyle(OH_Drawing_TextStyle* style, int fontStyle)
369 {
370 if (!style) {
371 return;
372 }
373 FontStyle rosenFontStyle;
374 switch (fontStyle) {
375 case FONT_STYLE_NORMAL: {
376 rosenFontStyle = FontStyle::NORMAL;
377 break;
378 }
379 case FONT_STYLE_ITALIC:
380 case FONT_STYLE_OBLIQUE: {
381 rosenFontStyle = FontStyle::ITALIC;
382 break;
383 }
384 default: {
385 rosenFontStyle = FontStyle::NORMAL;
386 }
387 }
388 ConvertToOriginalText<TextStyle>(style)->fontStyle = rosenFontStyle;
389 }
390
OH_Drawing_SetTextStyleLocale(OH_Drawing_TextStyle * style,const char * locale)391 void OH_Drawing_SetTextStyleLocale(OH_Drawing_TextStyle* style, const char* locale)
392 {
393 if (!style) {
394 return;
395 }
396 ConvertToOriginalText<TextStyle>(style)->locale = locale;
397 }
398
OH_Drawing_CreateTypographyHandler(OH_Drawing_TypographyStyle * style,OH_Drawing_FontCollection * fontCollection)399 OH_Drawing_TypographyCreate* OH_Drawing_CreateTypographyHandler(
400 OH_Drawing_TypographyStyle* style, OH_Drawing_FontCollection* fontCollection)
401 {
402 if (!style || !fontCollection) {
403 return nullptr;
404 }
405
406 std::unique_ptr<TypographyCreate> handler;
407 const TypographyStyle* typoStyle = ConvertToOriginalText<TypographyStyle>(style);
408
409 if (auto fc = OHOS::Rosen::Drawing::FontCollectionMgr::GetInstance().Find(fontCollection)) {
410 handler = TypographyCreate::Create(*typoStyle, fc);
411 } else {
412 objectMgr->RemoveObject(fontCollection);
413
414 handler = TypographyCreate::Create(
415 *typoStyle, std::shared_ptr<FontCollection>(ConvertToOriginalText<FontCollection>(fontCollection)));
416 }
417
418 return ConvertToNDKText<OH_Drawing_TypographyCreate>(handler.release());
419 }
420
OH_Drawing_DestroyTypographyHandler(OH_Drawing_TypographyCreate * handler)421 void OH_Drawing_DestroyTypographyHandler(OH_Drawing_TypographyCreate* handler)
422 {
423 delete ConvertToOriginalText<TypographyCreate>(handler);
424 }
425
OH_Drawing_TypographyHandlerPushTextStyle(OH_Drawing_TypographyCreate * handler,OH_Drawing_TextStyle * style)426 void OH_Drawing_TypographyHandlerPushTextStyle(OH_Drawing_TypographyCreate* handler, OH_Drawing_TextStyle* style)
427 {
428 if (!handler || !style) {
429 return;
430 }
431 const TextStyle* rosenTextStyle = ConvertToOriginalText<TextStyle>(style);
432 ConvertToOriginalText<TypographyCreate>(handler)->PushStyle(*rosenTextStyle);
433 }
434
OH_Drawing_TypographyHandlerAddText(OH_Drawing_TypographyCreate * handler,const char * text)435 void OH_Drawing_TypographyHandlerAddText(OH_Drawing_TypographyCreate* handler, const char* text)
436 {
437 if (!text || !handler) {
438 LOGE("null text");
439 return;
440 }
441
442 std::u16string wideText;
443 if (OHOS::Rosen::SPText::TextBundleConfigParser::GetInstance()
444 .IsTargetApiVersion(OHOS::Rosen::SPText::SINCE_API18_VERSION)) {
445 wideText = Str8ToStr16ByIcu(text);
446 } else {
447 if (!IsUtf8(text, strlen(text))) {
448 LOGE("text is not utf-8");
449 return;
450 }
451 wideText = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(text);
452 }
453
454 ConvertToOriginalText<TypographyCreate>(handler)->AppendText(wideText);
455 }
456
OH_Drawing_TypographyHandlerPopTextStyle(OH_Drawing_TypographyCreate * handler)457 void OH_Drawing_TypographyHandlerPopTextStyle(OH_Drawing_TypographyCreate* handler)
458 {
459 if (!handler) {
460 return;
461 }
462 ConvertToOriginalText<TypographyCreate>(handler)->PopStyle();
463 }
464
OH_Drawing_CreateTypography(OH_Drawing_TypographyCreate * handler)465 OH_Drawing_Typography* OH_Drawing_CreateTypography(OH_Drawing_TypographyCreate* handler)
466 {
467 if (!handler) {
468 return nullptr;
469 }
470 TypographyCreate* rosenHandler = ConvertToOriginalText<TypographyCreate>(handler);
471 std::unique_ptr<Typography> typography = rosenHandler->CreateTypography();
472 return ConvertToNDKText<OH_Drawing_Typography>(typography.release());
473 }
474
OH_Drawing_DestroyTypography(OH_Drawing_Typography * typography)475 void OH_Drawing_DestroyTypography(OH_Drawing_Typography* typography)
476 {
477 delete ConvertToOriginalText<Typography>(typography);
478 }
479
OH_Drawing_TypographyLayout(OH_Drawing_Typography * typography,double maxWidth)480 void OH_Drawing_TypographyLayout(OH_Drawing_Typography* typography, double maxWidth)
481 {
482 if (!typography) {
483 return;
484 }
485 ConvertToOriginalText<Typography>(typography)->Layout(maxWidth);
486 }
487
OH_Drawing_TypographyPaint(OH_Drawing_Typography * typography,OH_Drawing_Canvas * canvas,double potisionX,double potisionY)488 void OH_Drawing_TypographyPaint(
489 OH_Drawing_Typography* typography, OH_Drawing_Canvas* canvas, double potisionX, double potisionY)
490 {
491 if (!typography || !canvas) {
492 return;
493 }
494 ConvertToOriginalText<Typography>(typography)
495 ->Paint(reinterpret_cast<OHOS::Rosen::Drawing::Canvas*>(canvas), potisionX, potisionY);
496 }
497
OH_Drawing_TypographyPaintOnPath(OH_Drawing_Typography * typography,OH_Drawing_Canvas * canvas,OH_Drawing_Path * path,double hOffset,double vOffset)498 void OH_Drawing_TypographyPaintOnPath(
499 OH_Drawing_Typography* typography, OH_Drawing_Canvas* canvas, OH_Drawing_Path* path, double hOffset, double vOffset)
500 {
501 if (!typography || !canvas) {
502 return;
503 }
504 auto drawingCanvas = reinterpret_cast<OHOS::Rosen::Drawing::Canvas*>(canvas);
505 if (!path) {
506 return;
507 }
508 auto drawingpath = reinterpret_cast<OHOS::Rosen::Drawing::Path*>(path);
509 if (drawingCanvas && drawingCanvas->GetDrawingType() == OHOS::Rosen::Drawing::DrawingType::RECORDING) {
510 (static_cast<OHOS::Rosen::Drawing::RecordingCanvas*>(drawingCanvas))->SetIsCustomTypeface(true);
511 }
512 ConvertToOriginalText<Typography>(typography)->Paint(drawingCanvas, drawingpath, hOffset, vOffset);
513 }
514
OH_Drawing_TypographyGetMaxWidth(OH_Drawing_Typography * typography)515 double OH_Drawing_TypographyGetMaxWidth(OH_Drawing_Typography* typography)
516 {
517 if (!typography) {
518 return 0.0;
519 }
520 return ConvertToOriginalText<Typography>(typography)->GetMaxWidth();
521 }
522
OH_Drawing_TypographyGetHeight(OH_Drawing_Typography * typography)523 double OH_Drawing_TypographyGetHeight(OH_Drawing_Typography* typography)
524 {
525 if (!typography) {
526 return 0.0;
527 }
528 return ConvertToOriginalText<Typography>(typography)->GetHeight();
529 }
530
OH_Drawing_TypographyGetLongestLine(OH_Drawing_Typography * typography)531 double OH_Drawing_TypographyGetLongestLine(OH_Drawing_Typography* typography)
532 {
533 if (!typography) {
534 return 0.0;
535 }
536 return ConvertToOriginalText<Typography>(typography)->GetActualWidth();
537 }
538
OH_Drawing_TypographyGetLongestLineWithIndent(OH_Drawing_Typography * typography)539 double OH_Drawing_TypographyGetLongestLineWithIndent(OH_Drawing_Typography* typography)
540 {
541 if (typography == nullptr) {
542 return 0.0;
543 }
544 return ConvertToOriginalText<Typography>(typography)->GetLongestLineWithIndent();
545 }
546
OH_Drawing_TypographyGetMinIntrinsicWidth(OH_Drawing_Typography * typography)547 double OH_Drawing_TypographyGetMinIntrinsicWidth(OH_Drawing_Typography* typography)
548 {
549 if (!typography) {
550 return 0.0;
551 }
552 return ConvertToOriginalText<Typography>(typography)->GetMinIntrinsicWidth();
553 }
554
OH_Drawing_TypographyGetMaxIntrinsicWidth(OH_Drawing_Typography * typography)555 double OH_Drawing_TypographyGetMaxIntrinsicWidth(OH_Drawing_Typography* typography)
556 {
557 if (!typography) {
558 return 0.0;
559 }
560 return ConvertToOriginalText<Typography>(typography)->GetMaxIntrinsicWidth();
561 }
562
OH_Drawing_TypographyGetAlphabeticBaseline(OH_Drawing_Typography * typography)563 double OH_Drawing_TypographyGetAlphabeticBaseline(OH_Drawing_Typography* typography)
564 {
565 if (!typography) {
566 return 0.0;
567 }
568 return ConvertToOriginalText<Typography>(typography)->GetAlphabeticBaseline();
569 }
570
OH_Drawing_TypographyGetIdeographicBaseline(OH_Drawing_Typography * typography)571 double OH_Drawing_TypographyGetIdeographicBaseline(OH_Drawing_Typography* typography)
572 {
573 if (!typography) {
574 return 0.0;
575 }
576 return ConvertToOriginalText<Typography>(typography)->GetIdeographicBaseline();
577 }
578
OH_Drawing_TypographyHandlerAddPlaceholder(OH_Drawing_TypographyCreate * handler,OH_Drawing_PlaceholderSpan * span)579 void OH_Drawing_TypographyHandlerAddPlaceholder(OH_Drawing_TypographyCreate* handler, OH_Drawing_PlaceholderSpan* span)
580 {
581 if (!handler || !span) {
582 return;
583 }
584 auto originalPlaceholderSpan = ConvertToOriginalText<PlaceholderSpan>(span);
585 ConvertToOriginalText<TypographyCreate>(handler)->AppendPlaceholder(*originalPlaceholderSpan);
586 }
587
OH_Drawing_TypographyDidExceedMaxLines(OH_Drawing_Typography * typography)588 bool OH_Drawing_TypographyDidExceedMaxLines(OH_Drawing_Typography* typography)
589 {
590 if (!typography) {
591 return false;
592 }
593 return ConvertToOriginalText<Typography>(typography)->DidExceedMaxLines();
594 }
595
OH_Drawing_TypographyGetRectsForRange(OH_Drawing_Typography * typography,size_t start,size_t end,OH_Drawing_RectHeightStyle heightStyle,OH_Drawing_RectWidthStyle widthStyle)596 OH_Drawing_TextBox* OH_Drawing_TypographyGetRectsForRange(OH_Drawing_Typography* typography, size_t start, size_t end,
597 OH_Drawing_RectHeightStyle heightStyle, OH_Drawing_RectWidthStyle widthStyle)
598 {
599 if (!typography) {
600 return nullptr;
601 }
602 std::vector<TextRect>* originalVector = new (std::nothrow) std::vector<TextRect>;
603 if (originalVector == nullptr) {
604 return nullptr;
605 }
606 auto originalRectHeightStyle = ConvertToOriginalText<TextRectHeightStyle>(&heightStyle);
607 auto originalRectWidthStyle = ConvertToOriginalText<TextRectWidthStyle>(&widthStyle);
608 *originalVector = ConvertToOriginalText<Typography>(typography)
609 ->GetTextRectsByBoundary(start, end, *originalRectHeightStyle, *originalRectWidthStyle);
610 return (OH_Drawing_TextBox*)originalVector;
611 }
612
OH_Drawing_TypographyGetRectsForPlaceholders(OH_Drawing_Typography * typography)613 OH_Drawing_TextBox* OH_Drawing_TypographyGetRectsForPlaceholders(OH_Drawing_Typography* typography)
614 {
615 if (!typography) {
616 return nullptr;
617 }
618 std::vector<TextRect>* originalVector = new (std::nothrow) std::vector<TextRect>;
619 if (originalVector == nullptr) {
620 return nullptr;
621 }
622 *originalVector = ConvertToOriginalText<Typography>(typography)->GetTextRectsOfPlaceholders();
623 return (OH_Drawing_TextBox*)originalVector;
624 }
625
OH_Drawing_GetLeftFromTextBox(OH_Drawing_TextBox * textbox,int index)626 float OH_Drawing_GetLeftFromTextBox(OH_Drawing_TextBox* textbox, int index)
627 {
628 if (!textbox) {
629 return 0.0;
630 }
631 std::vector<TextRect>* textboxVector = ConvertToOriginalText<std::vector<TextRect>>(textbox);
632 if (index >= 0 && index < static_cast<int>(textboxVector->size())) {
633 return (*textboxVector)[index].rect.left_;
634 } else {
635 return 0.0;
636 }
637 }
638
OH_Drawing_GetRightFromTextBox(OH_Drawing_TextBox * textbox,int index)639 float OH_Drawing_GetRightFromTextBox(OH_Drawing_TextBox* textbox, int index)
640 {
641 if (!textbox) {
642 return 0.0;
643 }
644 std::vector<TextRect>* textboxVector = ConvertToOriginalText<std::vector<TextRect>>(textbox);
645 if (index >= 0 && index < static_cast<int>(textboxVector->size())) {
646 return (*textboxVector)[index].rect.right_;
647 } else {
648 return 0.0;
649 }
650 }
651
OH_Drawing_GetTopFromTextBox(OH_Drawing_TextBox * textbox,int index)652 float OH_Drawing_GetTopFromTextBox(OH_Drawing_TextBox* textbox, int index)
653 {
654 if (!textbox) {
655 return 0.0;
656 }
657 std::vector<TextRect>* textboxVector = ConvertToOriginalText<std::vector<TextRect>>(textbox);
658 if (index >= 0 && index < static_cast<int>(textboxVector->size())) {
659 return (*textboxVector)[index].rect.top_;
660 } else {
661 return 0.0;
662 }
663 }
664
OH_Drawing_GetBottomFromTextBox(OH_Drawing_TextBox * textbox,int index)665 float OH_Drawing_GetBottomFromTextBox(OH_Drawing_TextBox* textbox, int index)
666 {
667 if (!textbox) {
668 return 0.0;
669 }
670 std::vector<TextRect>* textboxVector = ConvertToOriginalText<std::vector<TextRect>>(textbox);
671 if (index >= 0 && index < static_cast<int>(textboxVector->size())) {
672 return (*textboxVector)[index].rect.bottom_;
673 } else {
674 return 0.0;
675 }
676 }
677
OH_Drawing_GetTextDirectionFromTextBox(OH_Drawing_TextBox * textbox,int index)678 int OH_Drawing_GetTextDirectionFromTextBox(OH_Drawing_TextBox* textbox, int index)
679 {
680 if (!textbox) {
681 return 0;
682 }
683 std::vector<TextRect>* textboxVector = ConvertToOriginalText<std::vector<TextRect>>(textbox);
684 if (index >= 0 && index < static_cast<int>(textboxVector->size())) {
685 TextDirection textDirection = (*textboxVector)[index].direction;
686 switch (textDirection) {
687 case TextDirection::RTL: {
688 return 0;
689 }
690 case TextDirection::LTR: {
691 return 1;
692 }
693 default: {
694 return 0;
695 }
696 }
697 } else {
698 return 0;
699 }
700 }
701
OH_Drawing_GetSizeOfTextBox(OH_Drawing_TextBox * textbox)702 size_t OH_Drawing_GetSizeOfTextBox(OH_Drawing_TextBox* textbox)
703 {
704 if (!textbox) {
705 return 0;
706 }
707 std::vector<TextRect>* textboxVector = ConvertToOriginalText<std::vector<TextRect>>(textbox);
708 return textboxVector->size();
709 }
710
OH_Drawing_TypographyGetGlyphPositionAtCoordinate(OH_Drawing_Typography * typography,double dx,double dy)711 OH_Drawing_PositionAndAffinity* OH_Drawing_TypographyGetGlyphPositionAtCoordinate(
712 OH_Drawing_Typography* typography, double dx, double dy)
713 {
714 if (!typography) {
715 return nullptr;
716 }
717 IndexAndAffinity* originalPositionAndAffinity = new (std::nothrow) IndexAndAffinity(0, Affinity::PREV);
718 if (originalPositionAndAffinity == nullptr) {
719 return nullptr;
720 }
721 return (OH_Drawing_PositionAndAffinity*)originalPositionAndAffinity;
722 }
723
OH_Drawing_TypographyGetGlyphPositionAtCoordinateWithCluster(OH_Drawing_Typography * typography,double dx,double dy)724 OH_Drawing_PositionAndAffinity* OH_Drawing_TypographyGetGlyphPositionAtCoordinateWithCluster(
725 OH_Drawing_Typography* typography, double dx, double dy)
726 {
727 if (!typography) {
728 return nullptr;
729 }
730 IndexAndAffinity* originalPositionAndAffinity = new (std::nothrow) IndexAndAffinity(0, Affinity::PREV);
731 if (originalPositionAndAffinity == nullptr) {
732 return nullptr;
733 }
734 *originalPositionAndAffinity = ConvertToOriginalText<Typography>(typography)->GetGlyphIndexByCoordinate(dx, dy);
735 return (OH_Drawing_PositionAndAffinity*)originalPositionAndAffinity;
736 }
737
OH_Drawing_GetPositionFromPositionAndAffinity(OH_Drawing_PositionAndAffinity * positionandaffinity)738 size_t OH_Drawing_GetPositionFromPositionAndAffinity(OH_Drawing_PositionAndAffinity* positionandaffinity)
739 {
740 if (!positionandaffinity) {
741 return 0;
742 }
743 IndexAndAffinity* textIndexAndAffinity = ConvertToOriginalText<IndexAndAffinity>(positionandaffinity);
744 return textIndexAndAffinity->index;
745 }
746
OH_Drawing_GetAffinityFromPositionAndAffinity(OH_Drawing_PositionAndAffinity * positionandaffinity)747 int OH_Drawing_GetAffinityFromPositionAndAffinity(OH_Drawing_PositionAndAffinity* positionandaffinity)
748 {
749 if (!positionandaffinity) {
750 return 0;
751 }
752 IndexAndAffinity* textIndexAndAffinity = ConvertToOriginalText<IndexAndAffinity>(positionandaffinity);
753 switch (textIndexAndAffinity->affinity) {
754 case Affinity::PREV: {
755 return 0;
756 }
757 case Affinity::NEXT: {
758 return 1;
759 }
760 default: {
761 return 0;
762 }
763 }
764 }
765
OH_Drawing_TypographyGetWordBoundary(OH_Drawing_Typography * typography,size_t offset)766 OH_Drawing_Range* OH_Drawing_TypographyGetWordBoundary(OH_Drawing_Typography* typography, size_t offset)
767 {
768 if (!typography) {
769 return nullptr;
770 }
771 Boundary* originalRange = new (std::nothrow) Boundary(0, 0);
772 if (originalRange == nullptr) {
773 return nullptr;
774 }
775 *originalRange = ConvertToOriginalText<Typography>(typography)->GetWordBoundaryByIndex(offset);
776 return (OH_Drawing_Range*)originalRange;
777 }
778
OH_Drawing_GetStartFromRange(OH_Drawing_Range * range)779 size_t OH_Drawing_GetStartFromRange(OH_Drawing_Range* range)
780 {
781 if (!range) {
782 return 0;
783 }
784 Boundary* boundary = ConvertToOriginalText<Boundary>(range);
785 return boundary->leftIndex;
786 }
787
OH_Drawing_GetEndFromRange(OH_Drawing_Range * range)788 size_t OH_Drawing_GetEndFromRange(OH_Drawing_Range* range)
789 {
790 if (!range) {
791 return 0;
792 }
793 Boundary* boundary = ConvertToOriginalText<Boundary>(range);
794 return boundary->rightIndex;
795 }
796
OH_Drawing_TypographyGetLineCount(OH_Drawing_Typography * typography)797 size_t OH_Drawing_TypographyGetLineCount(OH_Drawing_Typography* typography)
798 {
799 if (!typography) {
800 return 0;
801 }
802 return ConvertToOriginalText<Typography>(typography)->GetLineCount();
803 }
804
OH_Drawing_SetTextStyleDecorationStyle(OH_Drawing_TextStyle * style,int decorationStyle)805 void OH_Drawing_SetTextStyleDecorationStyle(OH_Drawing_TextStyle* style, int decorationStyle)
806 {
807 if (!style) {
808 return;
809 }
810 TextDecorationStyle rosenDecorationStyle;
811 switch (decorationStyle) {
812 case TEXT_DECORATION_STYLE_SOLID: {
813 rosenDecorationStyle = TextDecorationStyle::SOLID;
814 break;
815 }
816 case TEXT_DECORATION_STYLE_DOUBLE: {
817 rosenDecorationStyle = TextDecorationStyle::DOUBLE;
818 break;
819 }
820 case TEXT_DECORATION_STYLE_DOTTED: {
821 rosenDecorationStyle = TextDecorationStyle::DOTTED;
822 break;
823 }
824 case TEXT_DECORATION_STYLE_DASHED: {
825 rosenDecorationStyle = TextDecorationStyle::DASHED;
826 break;
827 }
828 case TEXT_DECORATION_STYLE_WAVY: {
829 rosenDecorationStyle = TextDecorationStyle::WAVY;
830 break;
831 }
832 default: {
833 rosenDecorationStyle = TextDecorationStyle::SOLID;
834 }
835 }
836 ConvertToOriginalText<TextStyle>(style)->decorationStyle = rosenDecorationStyle;
837 }
838
OH_Drawing_SetTextStyleDecorationThicknessScale(OH_Drawing_TextStyle * style,double decorationThicknessScale)839 void OH_Drawing_SetTextStyleDecorationThicknessScale(OH_Drawing_TextStyle* style, double decorationThicknessScale)
840 {
841 if (!style) {
842 return;
843 }
844 ConvertToOriginalText<TextStyle>(style)->decorationThicknessScale = decorationThicknessScale;
845 }
846
OH_Drawing_SetTextStyleLetterSpacing(OH_Drawing_TextStyle * style,double letterSpacing)847 void OH_Drawing_SetTextStyleLetterSpacing(OH_Drawing_TextStyle* style, double letterSpacing)
848 {
849 if (!style) {
850 return;
851 }
852 ConvertToOriginalText<TextStyle>(style)->letterSpacing = letterSpacing;
853 }
854
OH_Drawing_SetTextStyleWordSpacing(OH_Drawing_TextStyle * style,double wordSpacing)855 void OH_Drawing_SetTextStyleWordSpacing(OH_Drawing_TextStyle* style, double wordSpacing)
856 {
857 if (!style) {
858 return;
859 }
860 ConvertToOriginalText<TextStyle>(style)->wordSpacing = wordSpacing;
861 }
862
OH_Drawing_SetTextStyleHalfLeading(OH_Drawing_TextStyle * style,bool halfLeading)863 void OH_Drawing_SetTextStyleHalfLeading(OH_Drawing_TextStyle* style, bool halfLeading)
864 {
865 if (!style) {
866 return;
867 }
868 ConvertToOriginalText<TextStyle>(style)->halfLeading = halfLeading;
869 }
870
OH_Drawing_SetTextStyleEllipsis(OH_Drawing_TextStyle * style,const char * ellipsis)871 void OH_Drawing_SetTextStyleEllipsis(OH_Drawing_TextStyle* style, const char* ellipsis)
872 {
873 if (!style || !ellipsis) {
874 return;
875 }
876 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
877 std::u16string u16Ellipsis = converter.from_bytes(ellipsis);
878 ConvertToOriginalText<TextStyle>(style)->ellipsis = u16Ellipsis;
879 }
880
OH_Drawing_SetTextStyleEllipsisModal(OH_Drawing_TextStyle * style,int ellipsisModal)881 void OH_Drawing_SetTextStyleEllipsisModal(OH_Drawing_TextStyle* style, int ellipsisModal)
882 {
883 if (!style) {
884 return;
885 }
886 EllipsisModal rosenEllipsisModal;
887 switch (ellipsisModal) {
888 case ELLIPSIS_MODAL_HEAD: {
889 rosenEllipsisModal = EllipsisModal::HEAD;
890 break;
891 }
892 case ELLIPSIS_MODAL_MIDDLE: {
893 rosenEllipsisModal = EllipsisModal::MIDDLE;
894 break;
895 }
896 case ELLIPSIS_MODAL_TAIL: {
897 rosenEllipsisModal = EllipsisModal::TAIL;
898 break;
899 }
900 default: {
901 rosenEllipsisModal = EllipsisModal::TAIL;
902 }
903 }
904 ConvertToOriginalText<TextStyle>(style)->ellipsisModal = rosenEllipsisModal;
905 }
906
OH_Drawing_SetTypographyTextBreakStrategy(OH_Drawing_TypographyStyle * style,int breakStrategy)907 void OH_Drawing_SetTypographyTextBreakStrategy(OH_Drawing_TypographyStyle* style, int breakStrategy)
908 {
909 if (!style) {
910 return;
911 }
912 BreakStrategy rosenBreakStrategy;
913 switch (breakStrategy) {
914 case BREAK_STRATEGY_GREEDY: {
915 rosenBreakStrategy = BreakStrategy::GREEDY;
916 break;
917 }
918 case BREAK_STRATEGY_HIGH_QUALITY: {
919 rosenBreakStrategy = BreakStrategy::HIGH_QUALITY;
920 break;
921 }
922 case BREAK_STRATEGY_BALANCED: {
923 rosenBreakStrategy = BreakStrategy::BALANCED;
924 break;
925 }
926 default: {
927 rosenBreakStrategy = BreakStrategy::GREEDY;
928 }
929 }
930 ConvertToOriginalText<TypographyStyle>(style)->breakStrategy = rosenBreakStrategy;
931 }
932
OH_Drawing_SetTypographyTextWordBreakType(OH_Drawing_TypographyStyle * style,int wordBreakType)933 void OH_Drawing_SetTypographyTextWordBreakType(OH_Drawing_TypographyStyle* style, int wordBreakType)
934 {
935 if (!style) {
936 return;
937 }
938 WordBreakType rosenWordBreakType;
939 switch (wordBreakType) {
940 case WORD_BREAK_TYPE_NORMAL: {
941 rosenWordBreakType = WordBreakType::NORMAL;
942 break;
943 }
944 case WORD_BREAK_TYPE_BREAK_ALL: {
945 rosenWordBreakType = WordBreakType::BREAK_ALL;
946 break;
947 }
948 case WORD_BREAK_TYPE_BREAK_WORD: {
949 rosenWordBreakType = WordBreakType::BREAK_WORD;
950 break;
951 }
952 case WORD_BREAK_TYPE_BREAK_HYPHEN: {
953 rosenWordBreakType = WordBreakType::BREAK_HYPHEN;
954 break;
955 }
956 default: {
957 rosenWordBreakType = WordBreakType::BREAK_WORD;
958 }
959 }
960 ConvertToOriginalText<TypographyStyle>(style)->wordBreakType = rosenWordBreakType;
961 }
962
OH_Drawing_SetTypographyTextEllipsisModal(OH_Drawing_TypographyStyle * style,int ellipsisModal)963 void OH_Drawing_SetTypographyTextEllipsisModal(OH_Drawing_TypographyStyle* style, int ellipsisModal)
964 {
965 if (!style) {
966 return;
967 }
968 EllipsisModal rosenEllipsisModal;
969 switch (ellipsisModal) {
970 case ELLIPSIS_MODAL_HEAD: {
971 rosenEllipsisModal = EllipsisModal::HEAD;
972 break;
973 }
974 case ELLIPSIS_MODAL_MIDDLE: {
975 rosenEllipsisModal = EllipsisModal::MIDDLE;
976 break;
977 }
978 case ELLIPSIS_MODAL_TAIL: {
979 rosenEllipsisModal = EllipsisModal::TAIL;
980 break;
981 }
982 default: {
983 rosenEllipsisModal = EllipsisModal::TAIL;
984 }
985 }
986 ConvertToOriginalText<TypographyStyle>(style)->ellipsisModal = rosenEllipsisModal;
987 }
988
OH_Drawing_TypographyGetLineHeight(OH_Drawing_Typography * typography,int lineNumber)989 double OH_Drawing_TypographyGetLineHeight(OH_Drawing_Typography* typography, int lineNumber)
990 {
991 if (!typography) {
992 return 0.0;
993 }
994 Typography* typographyInner = ConvertToOriginalText<Typography>(typography);
995 return typographyInner->GetLineHeight(lineNumber);
996 }
997
OH_Drawing_TypographyGetLineWidth(OH_Drawing_Typography * typography,int lineNumber)998 double OH_Drawing_TypographyGetLineWidth(OH_Drawing_Typography* typography, int lineNumber)
999 {
1000 if (!typography) {
1001 return 0.0;
1002 }
1003 Typography* typographyInner = ConvertToOriginalText<Typography>(typography);
1004 return typographyInner->GetLineWidth(lineNumber);
1005 }
1006
OH_Drawing_TypographyGetLineTextRange(OH_Drawing_Typography * typography,int lineNumber,bool includeSpaces)1007 OH_Drawing_Range* OH_Drawing_TypographyGetLineTextRange(
1008 OH_Drawing_Typography* typography, int lineNumber, bool includeSpaces)
1009 {
1010 if (!typography) {
1011 return nullptr;
1012 }
1013 Boundary* originalRange = new (std::nothrow) Boundary(0, 0);
1014 if (originalRange == nullptr) {
1015 return nullptr;
1016 }
1017 *originalRange = ConvertToOriginalText<Typography>(typography)->GetActualTextRange(lineNumber, includeSpaces);
1018 return (OH_Drawing_Range*)originalRange;
1019 }
1020
ConvertFontMetrics(const Drawing::FontMetrics & fontMetrics,OH_Drawing_Font_Metrics & drawingFontMetrics)1021 static void ConvertFontMetrics(const Drawing::FontMetrics& fontMetrics, OH_Drawing_Font_Metrics& drawingFontMetrics)
1022 {
1023 drawingFontMetrics.flags = fontMetrics.fFlags;
1024 drawingFontMetrics.top = fontMetrics.fTop;
1025 drawingFontMetrics.ascent = fontMetrics.fAscent;
1026 drawingFontMetrics.descent = fontMetrics.fDescent;
1027 drawingFontMetrics.bottom = fontMetrics.fBottom;
1028 drawingFontMetrics.leading = fontMetrics.fLeading;
1029 drawingFontMetrics.avgCharWidth = fontMetrics.fAvgCharWidth;
1030 drawingFontMetrics.maxCharWidth = fontMetrics.fMaxCharWidth;
1031 drawingFontMetrics.xMin = fontMetrics.fXMin;
1032 drawingFontMetrics.xMax = fontMetrics.fXMax;
1033 drawingFontMetrics.xHeight = fontMetrics.fXHeight;
1034 drawingFontMetrics.capHeight = fontMetrics.fCapHeight;
1035 drawingFontMetrics.underlineThickness = fontMetrics.fUnderlineThickness;
1036 drawingFontMetrics.underlinePosition = fontMetrics.fUnderlinePosition;
1037 drawingFontMetrics.strikeoutThickness = fontMetrics.fStrikeoutThickness;
1038 drawingFontMetrics.strikeoutPosition = fontMetrics.fStrikeoutPosition;
1039 }
1040
ConvertLineMetrics(const LineMetrics & lineMetrics,OH_Drawing_LineMetrics & drawingLineMetrics)1041 static void ConvertLineMetrics(const LineMetrics& lineMetrics, OH_Drawing_LineMetrics& drawingLineMetrics)
1042 {
1043 drawingLineMetrics.ascender = lineMetrics.ascender;
1044 drawingLineMetrics.descender = lineMetrics.descender;
1045 drawingLineMetrics.capHeight = lineMetrics.capHeight;
1046 drawingLineMetrics.xHeight = lineMetrics.xHeight;
1047 drawingLineMetrics.width = lineMetrics.width;
1048 drawingLineMetrics.height = lineMetrics.height;
1049 drawingLineMetrics.x = lineMetrics.x;
1050 drawingLineMetrics.y = lineMetrics.y;
1051 drawingLineMetrics.startIndex = lineMetrics.startIndex;
1052 drawingLineMetrics.endIndex = lineMetrics.endIndex;
1053
1054 ConvertFontMetrics(lineMetrics.firstCharMetrics, drawingLineMetrics.firstCharMetrics);
1055 }
1056
OH_Drawing_TypographyGetLineInfo(OH_Drawing_Typography * typography,int lineNumber,bool oneLine,bool includeWhitespace,OH_Drawing_LineMetrics * drawingLineMetrics)1057 bool OH_Drawing_TypographyGetLineInfo(OH_Drawing_Typography* typography, int lineNumber, bool oneLine,
1058 bool includeWhitespace, OH_Drawing_LineMetrics* drawingLineMetrics)
1059 {
1060 Typography* typographyInner = ConvertToOriginalText<Typography>(typography);
1061 if (typographyInner == nullptr || drawingLineMetrics == nullptr) {
1062 return false;
1063 }
1064
1065 LineMetrics lineMetrics;
1066 if (!typographyInner->GetLineInfo(lineNumber, oneLine, includeWhitespace, &lineMetrics)) {
1067 return false;
1068 }
1069
1070 ConvertLineMetrics(lineMetrics, *drawingLineMetrics);
1071 return true;
1072 }
1073
OH_Drawing_SetTextStyleForegroundBrush(OH_Drawing_TextStyle * style,OH_Drawing_Brush * foregroundBrush)1074 void OH_Drawing_SetTextStyleForegroundBrush(OH_Drawing_TextStyle* style, OH_Drawing_Brush* foregroundBrush)
1075 {
1076 if (style == nullptr || foregroundBrush == nullptr) {
1077 return;
1078 }
1079 ConvertToOriginalText<TextStyle>(style)->foregroundBrush = *reinterpret_cast<Drawing::Brush*>(foregroundBrush);
1080 }
1081
OH_Drawing_TextStyleGetForegroundBrush(OH_Drawing_TextStyle * style,OH_Drawing_Brush * foregroundBrush)1082 void OH_Drawing_TextStyleGetForegroundBrush(OH_Drawing_TextStyle* style, OH_Drawing_Brush* foregroundBrush)
1083 {
1084 if (style == nullptr || foregroundBrush == nullptr) {
1085 return;
1086 }
1087
1088 Drawing::Brush* brush = reinterpret_cast<Drawing::Brush*>(foregroundBrush);
1089 *brush = *ConvertToOriginalText<TextStyle>(style)->foregroundBrush;
1090 }
1091
OH_Drawing_SetTextStyleForegroundPen(OH_Drawing_TextStyle * style,OH_Drawing_Pen * foregroundPen)1092 void OH_Drawing_SetTextStyleForegroundPen(OH_Drawing_TextStyle* style, OH_Drawing_Pen* foregroundPen)
1093 {
1094 if (style == nullptr || foregroundPen == nullptr) {
1095 return;
1096 }
1097 ConvertToOriginalText<TextStyle>(style)->foregroundPen = *reinterpret_cast<Drawing::Pen*>(foregroundPen);
1098 }
1099
OH_Drawing_TextStyleGetForegroundPen(OH_Drawing_TextStyle * style,OH_Drawing_Pen * foregroundPen)1100 void OH_Drawing_TextStyleGetForegroundPen(OH_Drawing_TextStyle* style, OH_Drawing_Pen* foregroundPen)
1101 {
1102 if (style == nullptr || foregroundPen == nullptr) {
1103 return;
1104 }
1105
1106 Drawing::Pen* pen = reinterpret_cast<Drawing::Pen*>(foregroundPen);
1107 *pen = *ConvertToOriginalText<TextStyle>(style)->foregroundPen;
1108 }
1109
OH_Drawing_SetTextStyleBackgroundBrush(OH_Drawing_TextStyle * style,OH_Drawing_Brush * backgroundBrush)1110 void OH_Drawing_SetTextStyleBackgroundBrush(OH_Drawing_TextStyle* style, OH_Drawing_Brush* backgroundBrush)
1111 {
1112 if (style == nullptr || backgroundBrush == nullptr) {
1113 return;
1114 }
1115 ConvertToOriginalText<TextStyle>(style)->backgroundBrush = *reinterpret_cast<Drawing::Brush*>(backgroundBrush);
1116 }
1117
OH_Drawing_TextStyleGetBackgroundBrush(OH_Drawing_TextStyle * style,OH_Drawing_Brush * backgroundBrush)1118 void OH_Drawing_TextStyleGetBackgroundBrush(OH_Drawing_TextStyle* style, OH_Drawing_Brush* backgroundBrush)
1119 {
1120 if (style == nullptr || backgroundBrush == nullptr) {
1121 return;
1122 }
1123
1124 Drawing::Brush* brush = reinterpret_cast<Drawing::Brush*>(backgroundBrush);
1125 *brush = *ConvertToOriginalText<TextStyle>(style)->backgroundBrush;
1126 }
1127
OH_Drawing_SetTextStyleBackgroundPen(OH_Drawing_TextStyle * style,OH_Drawing_Pen * backgroundPen)1128 void OH_Drawing_SetTextStyleBackgroundPen(OH_Drawing_TextStyle* style, OH_Drawing_Pen* backgroundPen)
1129 {
1130 if (style == nullptr || backgroundPen == nullptr) {
1131 return;
1132 }
1133 ConvertToOriginalText<TextStyle>(style)->backgroundPen = *reinterpret_cast<Drawing::Pen*>(backgroundPen);
1134 }
1135
OH_Drawing_TextStyleGetBackgroundPen(OH_Drawing_TextStyle * style,OH_Drawing_Pen * backgroundPen)1136 void OH_Drawing_TextStyleGetBackgroundPen(OH_Drawing_TextStyle* style, OH_Drawing_Pen* backgroundPen)
1137 {
1138 if (style == nullptr || backgroundPen == nullptr) {
1139 return;
1140 }
1141
1142 Drawing::Pen* pen = reinterpret_cast<Drawing::Pen*>(backgroundPen);
1143 *pen = *ConvertToOriginalText<TextStyle>(style)->backgroundPen;
1144 }
1145
OH_Drawing_CreateFontDescriptor(void)1146 OH_Drawing_FontDescriptor* OH_Drawing_CreateFontDescriptor(void)
1147 {
1148 OH_Drawing_FontDescriptor* desc = new (std::nothrow) OH_Drawing_FontDescriptor;
1149 if (desc == nullptr) {
1150 return nullptr;
1151 }
1152 desc->path = nullptr;
1153 desc->postScriptName = nullptr;
1154 desc->fullName = nullptr;
1155 desc->fontFamily = nullptr;
1156 desc->fontSubfamily = nullptr;
1157 desc->weight = 0;
1158 desc->width = 0;
1159 desc->italic = 0;
1160 desc->monoSpace = false;
1161 desc->symbolic = false;
1162 return desc;
1163 }
1164
OH_Drawing_DestroyFontDescriptor(OH_Drawing_FontDescriptor * descriptor)1165 void OH_Drawing_DestroyFontDescriptor(OH_Drawing_FontDescriptor* descriptor)
1166 {
1167 if (descriptor != nullptr) {
1168 free(descriptor->path);
1169 free(descriptor->postScriptName);
1170 free(descriptor->fullName);
1171 free(descriptor->fontFamily);
1172 free(descriptor->fontSubfamily);
1173 delete descriptor;
1174 }
1175 }
1176
OH_Drawing_CreateFontParser(void)1177 OH_Drawing_FontParser* OH_Drawing_CreateFontParser(void)
1178 {
1179 auto fontParser = new (std::nothrow) TextEngine::FontParser;
1180 if (fontParser == nullptr) {
1181 return nullptr;
1182 }
1183 return (OH_Drawing_FontParser*)fontParser;
1184 }
1185
OH_Drawing_DestroyFontParser(OH_Drawing_FontParser * parser)1186 void OH_Drawing_DestroyFontParser(OH_Drawing_FontParser* parser)
1187 {
1188 if (parser) {
1189 delete ConvertToOriginalText<TextEngine::FontParser>(parser);
1190 parser = nullptr;
1191 }
1192 }
1193
SetFontConfigInfoErrorCode(const OH_Drawing_FontConfigInfoErrorCode srcCode,OH_Drawing_FontConfigInfoErrorCode * dstCode)1194 static void SetFontConfigInfoErrorCode(
1195 const OH_Drawing_FontConfigInfoErrorCode srcCode, OH_Drawing_FontConfigInfoErrorCode* dstCode)
1196 {
1197 if (!dstCode) {
1198 return;
1199 }
1200 *dstCode = srcCode;
1201 }
1202
CopyStrData(char ** destination,const std::string & source,OH_Drawing_FontConfigInfoErrorCode * code=nullptr)1203 static bool CopyStrData(
1204 char** destination, const std::string& source, OH_Drawing_FontConfigInfoErrorCode* code = nullptr)
1205 {
1206 if (destination == nullptr || source.empty()) {
1207 SetFontConfigInfoErrorCode(OH_Drawing_FontConfigInfoErrorCode::ERROR_FONT_CONFIG_INFO_COPY_STRING_DATA, code);
1208 return false;
1209 }
1210 size_t destinationSize = source.size() + 1;
1211 *destination = new (std::nothrow) char[destinationSize];
1212 if (!(*destination)) {
1213 SetFontConfigInfoErrorCode(OH_Drawing_FontConfigInfoErrorCode::ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY, code);
1214 return false;
1215 }
1216 auto retCopy = strcpy_s(*destination, destinationSize, source.c_str());
1217 if (retCopy != 0) {
1218 delete[] *destination;
1219 *destination = nullptr;
1220 SetFontConfigInfoErrorCode(OH_Drawing_FontConfigInfoErrorCode::ERROR_FONT_CONFIG_INFO_COPY_STRING_DATA, code);
1221 return false;
1222 }
1223 SetFontConfigInfoErrorCode(OH_Drawing_FontConfigInfoErrorCode::SUCCESS_FONT_CONFIG_INFO, code);
1224 return true;
1225 }
1226
OH_Drawing_FontParserGetSystemFontList(OH_Drawing_FontParser * fontParser,size_t * num)1227 char** OH_Drawing_FontParserGetSystemFontList(OH_Drawing_FontParser* fontParser, size_t* num)
1228 {
1229 if (fontParser == nullptr || num == nullptr) {
1230 return nullptr;
1231 }
1232 char** fontList = nullptr;
1233 icu::Locale locale = icu::Locale::getDefault();
1234 std::vector<TextEngine::FontParser::FontDescriptor> systemFontList =
1235 ConvertToOriginalText<TextEngine::FontParser>(fontParser)->GetVisibilityFonts(std::string(locale.getName()));
1236
1237 if (systemFontList.empty()) {
1238 *num = 0;
1239 return nullptr;
1240 }
1241 fontList = new (std::nothrow) char* [systemFontList.size()];
1242 if (fontList == nullptr) {
1243 return nullptr;
1244 }
1245 for (size_t i = 0; i < systemFontList.size(); ++i) {
1246 fontList[i] = nullptr;
1247 bool res = CopyStrData(&fontList[i], systemFontList[i].fullName);
1248 if (!res) {
1249 for (size_t j = 0; j <= i; j++) {
1250 delete[] fontList[j];
1251 }
1252 delete[] fontList;
1253 fontList = nullptr;
1254 *num = 0;
1255 return nullptr;
1256 }
1257 }
1258 *num = systemFontList.size();
1259 return fontList;
1260 }
1261
OH_Drawing_DestroySystemFontList(char ** fontList,size_t num)1262 void OH_Drawing_DestroySystemFontList(char** fontList, size_t num)
1263 {
1264 if (fontList == nullptr) {
1265 return;
1266 }
1267 for (size_t i = 0; i < num; ++i) {
1268 if (fontList[i] == nullptr) {
1269 continue;
1270 }
1271 delete[] fontList[i];
1272 fontList[i] = nullptr;
1273 }
1274 delete[] fontList;
1275 fontList = nullptr;
1276 }
1277
OH_Drawing_FontParserGetFontByName(OH_Drawing_FontParser * fontParser,const char * name)1278 OH_Drawing_FontDescriptor* OH_Drawing_FontParserGetFontByName(OH_Drawing_FontParser* fontParser, const char* name)
1279 {
1280 if (fontParser == nullptr || name == nullptr) {
1281 return nullptr;
1282 }
1283 std::vector<TextEngine::FontParser::FontDescriptor> systemFontList =
1284 ConvertToOriginalText<TextEngine::FontParser>(fontParser)->GetVisibilityFonts();
1285 for (size_t i = 0; i < systemFontList.size(); ++i) {
1286 if (strcmp(name, systemFontList[i].fullName.c_str()) != 0) {
1287 continue;
1288 }
1289 OH_Drawing_FontDescriptor* descriptor = OH_Drawing_CreateFontDescriptor();
1290 if (descriptor == nullptr) {
1291 return nullptr;
1292 }
1293 if (!OHOS::Rosen::Drawing::CopyFontDescriptor(descriptor, systemFontList[i])) {
1294 OH_Drawing_DestroyFontDescriptor(descriptor);
1295 return nullptr;
1296 }
1297 return descriptor;
1298 }
1299 return nullptr;
1300 }
1301
OH_Drawing_TypographyGetLineMetrics(OH_Drawing_Typography * typography)1302 OH_Drawing_LineMetrics* OH_Drawing_TypographyGetLineMetrics(OH_Drawing_Typography* typography)
1303 {
1304 if (typography == nullptr) {
1305 return nullptr;
1306 }
1307 auto lineMetrics = ConvertToOriginalText<Typography>(typography)->GetLineMetrics();
1308 if (lineMetrics.size() == 0) {
1309 return nullptr;
1310 }
1311 OH_Drawing_LineMetrics* lineMetricsArr = new (std::nothrow) OH_Drawing_LineMetrics[lineMetrics.size()];
1312 if (lineMetricsArr == nullptr) {
1313 return nullptr;
1314 }
1315 for (size_t i = 0; i < lineMetrics.size(); ++i) {
1316 ConvertLineMetrics(lineMetrics[i], lineMetricsArr[i]);
1317 }
1318 MgrSetArrSize(static_cast<void*>(lineMetricsArr), lineMetrics.size());
1319 return lineMetricsArr;
1320 }
1321
OH_Drawing_LineMetricsGetSize(OH_Drawing_LineMetrics * lineMetrics)1322 size_t OH_Drawing_LineMetricsGetSize(OH_Drawing_LineMetrics* lineMetrics)
1323 {
1324 if (lineMetrics == nullptr) {
1325 return 0;
1326 }
1327 return GetArrSizeFromMgr(static_cast<void*>(lineMetrics));
1328 }
1329
OH_Drawing_DestroyLineMetrics(OH_Drawing_LineMetrics * lineMetrics)1330 void OH_Drawing_DestroyLineMetrics(OH_Drawing_LineMetrics* lineMetrics)
1331 {
1332 if (lineMetrics) {
1333 MgrRemoveSize(static_cast<void*>(lineMetrics));
1334 delete[] lineMetrics;
1335 lineMetrics = nullptr;
1336 }
1337 }
1338
OH_Drawing_TypographyGetLineMetricsAt(OH_Drawing_Typography * typography,int lineNumber,OH_Drawing_LineMetrics * lineMetric)1339 bool OH_Drawing_TypographyGetLineMetricsAt(
1340 OH_Drawing_Typography* typography, int lineNumber, OH_Drawing_LineMetrics* lineMetric)
1341 {
1342 if (typography == nullptr || lineMetric == nullptr) {
1343 return false;
1344 }
1345 LineMetrics metric;
1346 if (ConvertToOriginalText<Typography>(typography)->GetLineMetricsAt(lineNumber, &metric)) {
1347 ConvertLineMetrics(metric, *lineMetric);
1348 return true;
1349 }
1350 return false;
1351 }
1352
OH_Drawing_TypographyGetIndentsWithIndex(OH_Drawing_Typography * typography,int index)1353 float OH_Drawing_TypographyGetIndentsWithIndex(OH_Drawing_Typography* typography, int index)
1354 {
1355 if (typography == nullptr || index < 0) {
1356 return 0.0;
1357 }
1358 Typography* innerTypography = ConvertToOriginalText<Typography>(typography);
1359 if (innerTypography == nullptr) {
1360 return 0.0;
1361 }
1362 return innerTypography->DetectIndents(static_cast<size_t>(index));
1363 }
1364
OH_Drawing_TypographySetIndents(OH_Drawing_Typography * typography,int indentsNumber,const float indents[])1365 void OH_Drawing_TypographySetIndents(OH_Drawing_Typography* typography, int indentsNumber, const float indents[])
1366 {
1367 if (typography == nullptr || indents == nullptr) {
1368 return;
1369 }
1370 std::vector<float> rosenIndents;
1371 for (int i = 0; i < indentsNumber; i++) {
1372 rosenIndents.emplace_back(indents[i]);
1373 }
1374 ConvertToOriginalText<Typography>(typography)->SetIndents(rosenIndents);
1375 }
1376
OH_Drawing_CreateTextShadow(void)1377 OH_Drawing_TextShadow* OH_Drawing_CreateTextShadow(void)
1378 {
1379 return (OH_Drawing_TextShadow*)new (std::nothrow) TextShadow;
1380 }
1381
OH_Drawing_DestroyTextShadow(OH_Drawing_TextShadow * shadow)1382 void OH_Drawing_DestroyTextShadow(OH_Drawing_TextShadow* shadow)
1383 {
1384 if (shadow == nullptr) {
1385 return;
1386 }
1387 delete ConvertToOriginalText<TextShadow>(shadow);
1388 shadow = NULL;
1389 }
1390
OH_Drawing_TextStyleGetShadowCount(OH_Drawing_TextStyle * style)1391 int OH_Drawing_TextStyleGetShadowCount(OH_Drawing_TextStyle* style)
1392 {
1393 if (style == nullptr) {
1394 return 0;
1395 }
1396 return ConvertToOriginalText<TextStyle>(style)->shadows.size();
1397 }
1398
OH_Drawing_TextStyleGetShadows(OH_Drawing_TextStyle * style)1399 OH_Drawing_TextShadow* OH_Drawing_TextStyleGetShadows(OH_Drawing_TextStyle* style)
1400 {
1401 if (style) {
1402 std::vector<TextShadow>* originalShadows = new (std::nothrow) std::vector<TextShadow>;
1403 if (originalShadows) {
1404 *originalShadows = ConvertToOriginalText<TextStyle>(style)->shadows;
1405 return (OH_Drawing_TextShadow*)originalShadows;
1406 } else {
1407 LOGE("Null textshadow");
1408 return nullptr;
1409 }
1410 }
1411 return nullptr;
1412 }
1413
OH_Drawing_SetTextShadow(OH_Drawing_TextShadow * shadow,uint32_t color,OH_Drawing_Point * offset,double blurRadius)1414 void OH_Drawing_SetTextShadow(
1415 OH_Drawing_TextShadow* shadow, uint32_t color, OH_Drawing_Point* offset, double blurRadius)
1416 {
1417 if (!shadow || !offset) {
1418 return;
1419 }
1420
1421 auto* tailoredShadow = reinterpret_cast<TextShadow*>(shadow);
1422 tailoredShadow->blurRadius = blurRadius;
1423 tailoredShadow->color = Drawing::Color(color);
1424 tailoredShadow->offset = *reinterpret_cast<Drawing::Point*>(offset);
1425 return;
1426 }
1427
OH_Drawing_TextStyleAddShadow(OH_Drawing_TextStyle * style,OH_Drawing_TextShadow * shadow)1428 void OH_Drawing_TextStyleAddShadow(OH_Drawing_TextStyle* style, OH_Drawing_TextShadow* shadow)
1429 {
1430 if (shadow == nullptr || style == nullptr) {
1431 return;
1432 }
1433 ConvertToOriginalText<TextStyle>(style)->shadows.emplace_back(*(ConvertToOriginalText<TextShadow>(shadow)));
1434 }
1435
OH_Drawing_TextStyleClearShadows(OH_Drawing_TextStyle * style)1436 void OH_Drawing_TextStyleClearShadows(OH_Drawing_TextStyle* style)
1437 {
1438 if (style) {
1439 ConvertToOriginalText<TextStyle>(style)->shadows.clear();
1440 }
1441 }
1442
OH_Drawing_TextStyleGetShadowWithIndex(OH_Drawing_TextStyle * style,int index)1443 OH_Drawing_TextShadow* OH_Drawing_TextStyleGetShadowWithIndex(OH_Drawing_TextStyle* style, int index)
1444 {
1445 if (style == nullptr || index < 0) {
1446 return nullptr;
1447 }
1448 if (index >= static_cast<int>(ConvertToOriginalText<TextStyle>(style)->shadows.size())) {
1449 return nullptr;
1450 }
1451 return (OH_Drawing_TextShadow*)(&(ConvertToOriginalText<TextStyle>(style)->shadows.at(index)));
1452 }
1453
OH_Drawing_DestroyTextShadows(OH_Drawing_TextShadow * shadow)1454 void OH_Drawing_DestroyTextShadows(OH_Drawing_TextShadow* shadow)
1455 {
1456 if (shadow == nullptr) {
1457 return;
1458 }
1459 delete ConvertToOriginalText<std::vector<TextShadow>>(shadow);
1460 shadow = NULL;
1461 }
1462
OH_Drawing_SetTypographyTextFontWeight(OH_Drawing_TypographyStyle * style,int weight)1463 void OH_Drawing_SetTypographyTextFontWeight(OH_Drawing_TypographyStyle* style, int weight)
1464 {
1465 if (style == nullptr) {
1466 return;
1467 }
1468 FontWeight fontWeight;
1469 switch (weight) {
1470 case FONT_WEIGHT_100: {
1471 fontWeight = FontWeight::W100;
1472 break;
1473 }
1474 case FONT_WEIGHT_200: {
1475 fontWeight = FontWeight::W200;
1476 break;
1477 }
1478 case FONT_WEIGHT_300: {
1479 fontWeight = FontWeight::W300;
1480 break;
1481 }
1482 case FONT_WEIGHT_400: {
1483 fontWeight = FontWeight::W400;
1484 break;
1485 }
1486 case FONT_WEIGHT_500: {
1487 fontWeight = FontWeight::W500;
1488 break;
1489 }
1490 case FONT_WEIGHT_600: {
1491 fontWeight = FontWeight::W600;
1492 break;
1493 }
1494 case FONT_WEIGHT_700: {
1495 fontWeight = FontWeight::W700;
1496 break;
1497 }
1498 case FONT_WEIGHT_800: {
1499 fontWeight = FontWeight::W800;
1500 break;
1501 }
1502 case FONT_WEIGHT_900: {
1503 fontWeight = FontWeight::W900;
1504 break;
1505 }
1506 default: {
1507 fontWeight = FontWeight::W400;
1508 }
1509 }
1510 ConvertToOriginalText<TypographyStyle>(style)->fontWeight = fontWeight;
1511 }
1512
OH_Drawing_SetTypographyTextFontStyle(OH_Drawing_TypographyStyle * style,int fontStyle)1513 void OH_Drawing_SetTypographyTextFontStyle(OH_Drawing_TypographyStyle* style, int fontStyle)
1514 {
1515 if (style == nullptr) {
1516 return;
1517 }
1518 FontStyle rosenFontStyle;
1519 switch (fontStyle) {
1520 case FONT_STYLE_NORMAL: {
1521 rosenFontStyle = FontStyle::NORMAL;
1522 break;
1523 }
1524 case FONT_STYLE_ITALIC: {
1525 rosenFontStyle = FontStyle::ITALIC;
1526 break;
1527 }
1528 default: {
1529 rosenFontStyle = FontStyle::NORMAL;
1530 }
1531 }
1532 ConvertToOriginalText<TypographyStyle>(style)->fontStyle = rosenFontStyle;
1533 }
1534
OH_Drawing_SetTypographyTextFontFamily(OH_Drawing_TypographyStyle * style,const char * fontFamily)1535 void OH_Drawing_SetTypographyTextFontFamily(OH_Drawing_TypographyStyle* style, const char* fontFamily)
1536 {
1537 if (style) {
1538 ConvertToOriginalText<TypographyStyle>(style)->fontFamily = fontFamily;
1539 }
1540 }
1541
OH_Drawing_SetTypographyTextFontSize(OH_Drawing_TypographyStyle * style,double fontSize)1542 void OH_Drawing_SetTypographyTextFontSize(OH_Drawing_TypographyStyle* style, double fontSize)
1543 {
1544 if (style) {
1545 ConvertToOriginalText<TypographyStyle>(style)->fontSize = fontSize;
1546 }
1547 }
1548
OH_Drawing_SetTypographyTextFontHeight(OH_Drawing_TypographyStyle * style,double fontHeight)1549 void OH_Drawing_SetTypographyTextFontHeight(OH_Drawing_TypographyStyle* style, double fontHeight)
1550 {
1551 if (style) {
1552 ConvertToOriginalText<TypographyStyle>(style)->heightScale = fontHeight < 0 ? 0 : fontHeight;
1553 ConvertToOriginalText<TypographyStyle>(style)->heightOnly = true;
1554 }
1555 }
1556
OH_Drawing_SetTypographyTextHalfLeading(OH_Drawing_TypographyStyle * style,bool halfLeading)1557 void OH_Drawing_SetTypographyTextHalfLeading(OH_Drawing_TypographyStyle* style, bool halfLeading)
1558 {
1559 if (style) {
1560 ConvertToOriginalText<TypographyStyle>(style)->halfLeading = halfLeading;
1561 }
1562 }
1563
OH_Drawing_SetTypographyTextUseLineStyle(OH_Drawing_TypographyStyle * style,bool useLineStyle)1564 void OH_Drawing_SetTypographyTextUseLineStyle(OH_Drawing_TypographyStyle* style, bool useLineStyle)
1565 {
1566 if (style) {
1567 ConvertToOriginalText<TypographyStyle>(style)->useLineStyle = useLineStyle;
1568 }
1569 }
1570
OH_Drawing_SetTypographyTextLineStyleFontWeight(OH_Drawing_TypographyStyle * style,int weight)1571 void OH_Drawing_SetTypographyTextLineStyleFontWeight(OH_Drawing_TypographyStyle* style, int weight)
1572 {
1573 if (style == nullptr) {
1574 return;
1575 }
1576 FontWeight fontWeight;
1577 switch (weight) {
1578 case FONT_WEIGHT_100: {
1579 fontWeight = FontWeight::W100;
1580 break;
1581 }
1582 case FONT_WEIGHT_200: {
1583 fontWeight = FontWeight::W200;
1584 break;
1585 }
1586 case FONT_WEIGHT_300: {
1587 fontWeight = FontWeight::W300;
1588 break;
1589 }
1590 case FONT_WEIGHT_400: {
1591 fontWeight = FontWeight::W400;
1592 break;
1593 }
1594 case FONT_WEIGHT_500: {
1595 fontWeight = FontWeight::W500;
1596 break;
1597 }
1598 case FONT_WEIGHT_600: {
1599 fontWeight = FontWeight::W600;
1600 break;
1601 }
1602 case FONT_WEIGHT_700: {
1603 fontWeight = FontWeight::W700;
1604 break;
1605 }
1606 case FONT_WEIGHT_800: {
1607 fontWeight = FontWeight::W800;
1608 break;
1609 }
1610 case FONT_WEIGHT_900: {
1611 fontWeight = FontWeight::W900;
1612 break;
1613 }
1614 default: {
1615 fontWeight = FontWeight::W400;
1616 }
1617 }
1618 ConvertToOriginalText<TypographyStyle>(style)->lineStyleFontWeight = fontWeight;
1619 }
1620
OH_Drawing_SetTypographyTextLineStyleFontStyle(OH_Drawing_TypographyStyle * style,int fontStyle)1621 void OH_Drawing_SetTypographyTextLineStyleFontStyle(OH_Drawing_TypographyStyle* style, int fontStyle)
1622 {
1623 if (style == nullptr) {
1624 return;
1625 }
1626 FontStyle rosenFontStyle;
1627 switch (fontStyle) {
1628 case FONT_STYLE_NORMAL: {
1629 rosenFontStyle = FontStyle::NORMAL;
1630 break;
1631 }
1632 case FONT_STYLE_ITALIC: {
1633 rosenFontStyle = FontStyle::ITALIC;
1634 break;
1635 }
1636 default: {
1637 rosenFontStyle = FontStyle::NORMAL;
1638 }
1639 }
1640 ConvertToOriginalText<TypographyStyle>(style)->lineStyleFontStyle = rosenFontStyle;
1641 }
1642
OH_Drawing_SetTypographyTextLineStyleFontFamilies(OH_Drawing_TypographyStyle * style,int fontFamiliesNumber,const char * fontFamilies[])1643 void OH_Drawing_SetTypographyTextLineStyleFontFamilies(
1644 OH_Drawing_TypographyStyle* style, int fontFamiliesNumber, const char* fontFamilies[])
1645 {
1646 if (style != nullptr && fontFamilies != nullptr) {
1647 std::vector<std::string> rosenFontFamilies;
1648 for (int i = 0; i < fontFamiliesNumber; i++) {
1649 rosenFontFamilies.emplace_back(fontFamilies[i]);
1650 }
1651 ConvertToOriginalText<TypographyStyle>(style)->lineStyleFontFamilies = rosenFontFamilies;
1652 }
1653 }
1654
OH_Drawing_SetTypographyTextLineStyleFontSize(OH_Drawing_TypographyStyle * style,double lineStyleFontSize)1655 void OH_Drawing_SetTypographyTextLineStyleFontSize(OH_Drawing_TypographyStyle* style, double lineStyleFontSize)
1656 {
1657 if (style == nullptr) {
1658 return;
1659 }
1660 TypographyStyle* typoStyle = ConvertToOriginalText<TypographyStyle>(style);
1661 if (typoStyle == nullptr) {
1662 return;
1663 }
1664 typoStyle->lineStyleFontSize = lineStyleFontSize;
1665 }
1666
OH_Drawing_SetTypographyTextLineStyleFontHeight(OH_Drawing_TypographyStyle * style,double lineStyleFontHeight)1667 void OH_Drawing_SetTypographyTextLineStyleFontHeight(OH_Drawing_TypographyStyle* style, double lineStyleFontHeight)
1668 {
1669 if (style == nullptr) {
1670 return;
1671 }
1672 TypographyStyle* typographyStyle = ConvertToOriginalText<TypographyStyle>(style);
1673 if (typographyStyle == nullptr) {
1674 return;
1675 }
1676 typographyStyle->lineStyleHeightScale = lineStyleFontHeight;
1677 if (!typographyStyle->lineStyleHeightOnlyInit) {
1678 typographyStyle->lineStyleHeightOnly = true;
1679 }
1680 }
1681
OH_Drawing_SetTypographyTextLineStyleHalfLeading(OH_Drawing_TypographyStyle * style,bool lineStyleHalfLeading)1682 void OH_Drawing_SetTypographyTextLineStyleHalfLeading(OH_Drawing_TypographyStyle* style, bool lineStyleHalfLeading)
1683 {
1684 if (style == nullptr) {
1685 return;
1686 }
1687 TypographyStyle* typoStyle = ConvertToOriginalText<TypographyStyle>(style);
1688 if (typoStyle == nullptr) {
1689 return;
1690 }
1691 typoStyle->lineStyleHalfLeading = lineStyleHalfLeading;
1692 }
1693
OH_Drawing_SetTypographyTextLineStyleSpacingScale(OH_Drawing_TypographyStyle * style,double spacingScale)1694 void OH_Drawing_SetTypographyTextLineStyleSpacingScale(OH_Drawing_TypographyStyle* style, double spacingScale)
1695 {
1696 if (style) {
1697 ConvertToOriginalText<TypographyStyle>(style)->lineStyleSpacingScale = spacingScale;
1698 }
1699 }
1700
OH_Drawing_SetTypographyTextLineStyleOnly(OH_Drawing_TypographyStyle * style,bool lineStyleOnly)1701 void OH_Drawing_SetTypographyTextLineStyleOnly(OH_Drawing_TypographyStyle* style, bool lineStyleOnly)
1702 {
1703 if (style) {
1704 ConvertToOriginalText<TypographyStyle>(style)->lineStyleOnly = lineStyleOnly;
1705 }
1706 }
1707
OH_Drawing_TextStyleGetFontMetrics(OH_Drawing_Typography * typography,OH_Drawing_TextStyle * style,OH_Drawing_Font_Metrics * fontmetrics)1708 bool OH_Drawing_TextStyleGetFontMetrics(
1709 OH_Drawing_Typography* typography, OH_Drawing_TextStyle* style, OH_Drawing_Font_Metrics* fontmetrics)
1710 {
1711 bool startGetMetrics = false;
1712 if (!typography || !style || !fontmetrics) {
1713 return false;
1714 }
1715 auto textstyle = ConvertToOriginalText<const OHOS::Rosen::TextStyle>(style);
1716 auto txtSKTypograph = ConvertToOriginalText<Typography>(typography);
1717 const OHOS::Rosen::Drawing::FontMetrics fontmetricsResult = txtSKTypograph->GetFontMetrics(*textstyle);
1718 ConvertFontMetrics(fontmetricsResult, *fontmetrics);
1719 startGetMetrics = true;
1720 return startGetMetrics;
1721 }
1722
OH_Drawing_SetTypographyTextStyle(OH_Drawing_TypographyStyle * handler,OH_Drawing_TextStyle * style)1723 void OH_Drawing_SetTypographyTextStyle(OH_Drawing_TypographyStyle* handler, OH_Drawing_TextStyle* style)
1724 {
1725 if (!handler || !style) {
1726 return;
1727 }
1728 auto rosenTypographStyle = ConvertToOriginalText<OHOS::Rosen::TypographyStyle>(handler);
1729 auto rosenTextStyle = ConvertToOriginalText<OHOS::Rosen::TextStyle>(style);
1730 rosenTypographStyle->SetTextStyle(*rosenTextStyle);
1731 return;
1732 }
1733
OH_Drawing_SetTypographyTextLocale(OH_Drawing_TypographyStyle * style,const char * locale)1734 void OH_Drawing_SetTypographyTextLocale(OH_Drawing_TypographyStyle* style, const char* locale)
1735 {
1736 if (!style) {
1737 return;
1738 }
1739 ConvertToOriginalText<TypographyStyle>(style)->locale = locale;
1740 }
1741
OH_Drawing_SetTypographyTextSplitRatio(OH_Drawing_TypographyStyle * style,float textSplitRatio)1742 void OH_Drawing_SetTypographyTextSplitRatio(OH_Drawing_TypographyStyle* style, float textSplitRatio)
1743 {
1744 if (!style) {
1745 return;
1746 }
1747 ConvertToOriginalText<TypographyStyle>(style)->textSplitRatio = textSplitRatio;
1748 }
1749
OH_Drawing_TypographyGetTextStyle(OH_Drawing_TypographyStyle * style)1750 OH_Drawing_TextStyle* OH_Drawing_TypographyGetTextStyle(OH_Drawing_TypographyStyle* style)
1751 {
1752 if (!style) {
1753 return nullptr;
1754 }
1755 TextStyle* originalTextStyle = new (std::nothrow) TextStyle;
1756 if (!originalTextStyle) {
1757 return nullptr;
1758 }
1759 *originalTextStyle = ConvertToOriginalText<TypographyStyle>(style)->GetTextStyle();
1760 return (OH_Drawing_TextStyle*)originalTextStyle;
1761 }
1762
OH_Drawing_TypographyGetEffectiveAlignment(OH_Drawing_TypographyStyle * style)1763 int OH_Drawing_TypographyGetEffectiveAlignment(OH_Drawing_TypographyStyle* style)
1764 {
1765 if (!style) {
1766 return 0;
1767 }
1768 TextAlign originalTextAlign = ConvertToOriginalText<TypographyStyle>(style)->GetEffectiveAlign();
1769 return static_cast<int>(originalTextAlign);
1770 }
1771
OH_Drawing_TypographyIsLineUnlimited(OH_Drawing_TypographyStyle * style)1772 bool OH_Drawing_TypographyIsLineUnlimited(OH_Drawing_TypographyStyle* style)
1773 {
1774 if (!style) {
1775 return false;
1776 }
1777 return ConvertToOriginalText<TypographyStyle>(style)->IsUnlimitedLines();
1778 }
1779
OH_Drawing_TypographyIsEllipsized(OH_Drawing_TypographyStyle * style)1780 bool OH_Drawing_TypographyIsEllipsized(OH_Drawing_TypographyStyle* style)
1781 {
1782 if (!style) {
1783 return false;
1784 }
1785 return ConvertToOriginalText<TypographyStyle>(style)->IsEllipsized();
1786 }
1787
OH_Drawing_SetTypographyTextEllipsis(OH_Drawing_TypographyStyle * style,const char * ellipsis)1788 void OH_Drawing_SetTypographyTextEllipsis(OH_Drawing_TypographyStyle* style, const char* ellipsis)
1789 {
1790 if (!style || !ellipsis) {
1791 return;
1792 }
1793 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
1794 std::u16string u16Ellipsis = converter.from_bytes(ellipsis);
1795 ConvertToOriginalText<TypographyStyle>(style)->ellipsis = u16Ellipsis;
1796 }
1797
OH_Drawing_TextStyleSetBackgroundRect(OH_Drawing_TextStyle * style,const OH_Drawing_RectStyle_Info * rectStyleInfo,int styleId)1798 void OH_Drawing_TextStyleSetBackgroundRect(
1799 OH_Drawing_TextStyle* style, const OH_Drawing_RectStyle_Info* rectStyleInfo, int styleId)
1800 {
1801 if (style == nullptr || rectStyleInfo == nullptr) {
1802 return;
1803 }
1804 TextStyle* convertStyle = ConvertToOriginalText<TextStyle>(style);
1805 if (convertStyle == nullptr) {
1806 return;
1807 }
1808 RectStyle& rectStyle = convertStyle->backgroundRect;
1809 rectStyle.color = rectStyleInfo->color;
1810 rectStyle.leftTopRadius = rectStyleInfo->leftTopRadius;
1811 rectStyle.rightTopRadius = rectStyleInfo->rightTopRadius;
1812 rectStyle.rightBottomRadius = rectStyleInfo->rightBottomRadius;
1813 rectStyle.leftBottomRadius = rectStyleInfo->leftBottomRadius;
1814 convertStyle->styleId = styleId;
1815 }
1816
OH_Drawing_TypographyHandlerAddSymbol(OH_Drawing_TypographyCreate * handler,uint32_t symbol)1817 void OH_Drawing_TypographyHandlerAddSymbol(OH_Drawing_TypographyCreate* handler, uint32_t symbol)
1818 {
1819 if (!handler) {
1820 return;
1821 }
1822 TypographyCreate* convertHandler = ConvertToOriginalText<TypographyCreate>(handler);
1823 if (convertHandler) {
1824 convertHandler->AppendSymbol(symbol);
1825 }
1826 }
1827
OH_Drawing_TextStyleAddFontFeature(OH_Drawing_TextStyle * style,const char * tag,int value)1828 void OH_Drawing_TextStyleAddFontFeature(OH_Drawing_TextStyle* style, const char* tag, int value)
1829 {
1830 if (style == nullptr || tag == nullptr) {
1831 return;
1832 }
1833 TextStyle* convertStyle = ConvertToOriginalText<TextStyle>(style);
1834 if (convertStyle) {
1835 convertStyle->fontFeatures.SetFeature(tag, value);
1836 }
1837 }
1838
OH_Drawing_TextStyleGetFontFeatureSize(OH_Drawing_TextStyle * style)1839 size_t OH_Drawing_TextStyleGetFontFeatureSize(OH_Drawing_TextStyle* style)
1840 {
1841 if (style == nullptr) {
1842 return 0;
1843 }
1844 TextStyle* convertStyle = ConvertToOriginalText<TextStyle>(style);
1845 if (convertStyle) {
1846 return (convertStyle->fontFeatures.GetFontFeatures()).size();
1847 }
1848 return 0;
1849 }
1850
OH_Drawing_TextStyleClearFontFeature(OH_Drawing_TextStyle * style)1851 void OH_Drawing_TextStyleClearFontFeature(OH_Drawing_TextStyle* style)
1852 {
1853 if (style == nullptr) {
1854 return;
1855 }
1856 TextStyle* convertStyle = ConvertToOriginalText<TextStyle>(style);
1857 if (convertStyle == nullptr) {
1858 return;
1859 }
1860 convertStyle->fontFeatures.Clear();
1861 }
1862
OH_Drawing_TextStyleGetFontFeatures(OH_Drawing_TextStyle * style)1863 OH_Drawing_FontFeature* OH_Drawing_TextStyleGetFontFeatures(OH_Drawing_TextStyle* style)
1864 {
1865 TextStyle* convertStyle = ConvertToOriginalText<TextStyle>(style);
1866 if (style == nullptr || convertStyle == nullptr) {
1867 return nullptr;
1868 }
1869 auto& originMap = convertStyle->fontFeatures.GetFontFeatures();
1870 size_t fontFeatureSize = originMap.size();
1871 if (fontFeatureSize <= 0) {
1872 return nullptr;
1873 }
1874 OH_Drawing_FontFeature* fontFeatureArray = new (std::nothrow) OH_Drawing_FontFeature[fontFeatureSize];
1875 if (!fontFeatureArray) {
1876 return nullptr;
1877 }
1878 size_t index = 0;
1879 for (auto& kv : originMap) {
1880 (fontFeatureArray + index)->tag = new (std::nothrow) char[(kv.first).size() + 1];
1881 if ((fontFeatureArray + index)->tag == nullptr) {
1882 for (size_t j = 0; j < index; j++) {
1883 delete[] (fontFeatureArray + j)->tag;
1884 (fontFeatureArray + j)->tag = nullptr;
1885 }
1886 delete[] fontFeatureArray;
1887 return nullptr;
1888 }
1889 auto result = strcpy_s((fontFeatureArray + index)->tag, ((kv.first).size() + 1), (kv.first).c_str());
1890 if (result != 0) {
1891 OH_Drawing_TextStyleDestroyFontFeatures(fontFeatureArray, index);
1892 return nullptr;
1893 }
1894 (fontFeatureArray + index)->value = kv.second;
1895 index++;
1896 }
1897 return fontFeatureArray;
1898 }
1899
OH_Drawing_TextStyleDestroyFontFeatures(OH_Drawing_FontFeature * fontFeature,size_t fontFeatureSize)1900 void OH_Drawing_TextStyleDestroyFontFeatures(OH_Drawing_FontFeature* fontFeature, size_t fontFeatureSize)
1901 {
1902 if (fontFeature == nullptr) {
1903 return;
1904 }
1905 for (int i = 0; i < static_cast<int>(fontFeatureSize); i++) {
1906 if ((fontFeature + i)->tag == nullptr) {
1907 continue;
1908 }
1909 delete[] (fontFeature + i)->tag;
1910 (fontFeature + i)->tag = nullptr;
1911 }
1912 delete[] fontFeature;
1913 fontFeature = nullptr;
1914 }
1915
OH_Drawing_TextStyleSetBaselineShift(OH_Drawing_TextStyle * style,double lineShift)1916 void OH_Drawing_TextStyleSetBaselineShift(OH_Drawing_TextStyle* style, double lineShift)
1917 {
1918 if (style == nullptr) {
1919 return;
1920 }
1921 TextStyle* convertStyle = ConvertToOriginalText<TextStyle>(style);
1922 if (convertStyle) {
1923 convertStyle->baseLineShift = lineShift;
1924 }
1925 }
1926
OH_Drawing_TextStyleGetBaselineShift(OH_Drawing_TextStyle * style)1927 double OH_Drawing_TextStyleGetBaselineShift(OH_Drawing_TextStyle* style)
1928 {
1929 TextStyle* convertStyle = ConvertToOriginalText<TextStyle>(style);
1930 if (convertStyle == nullptr) {
1931 return 0.0;
1932 }
1933 return convertStyle->baseLineShift;
1934 }
1935
OH_Drawing_TextStyleGetColor(OH_Drawing_TextStyle * style)1936 uint32_t OH_Drawing_TextStyleGetColor(OH_Drawing_TextStyle* style)
1937 {
1938 if (style == nullptr) {
1939 // 0xFFFFFFFF is default color.
1940 return 0xFFFFFFFF;
1941 }
1942 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
1943 if (textStyle == nullptr) {
1944 // 0xFFFFFFFF is default color.
1945 return 0xFFFFFFFF;
1946 }
1947 return textStyle->color.CastToColorQuad();
1948 }
1949
OH_Drawing_TextStyleGetDecorationStyle(OH_Drawing_TextStyle * style)1950 OH_Drawing_TextDecorationStyle OH_Drawing_TextStyleGetDecorationStyle(OH_Drawing_TextStyle* style)
1951 {
1952 if (style == nullptr) {
1953 return TEXT_DECORATION_STYLE_SOLID;
1954 }
1955 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
1956 if (textStyle == nullptr) {
1957 return TEXT_DECORATION_STYLE_SOLID;
1958 }
1959 return static_cast<OH_Drawing_TextDecorationStyle>(textStyle->decorationStyle);
1960 }
1961
OH_Drawing_TextStyleGetFontWeight(OH_Drawing_TextStyle * style)1962 OH_Drawing_FontWeight OH_Drawing_TextStyleGetFontWeight(OH_Drawing_TextStyle* style)
1963 {
1964 if (style == nullptr) {
1965 return FONT_WEIGHT_400;
1966 }
1967 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
1968 if (textStyle == nullptr) {
1969 return FONT_WEIGHT_400;
1970 }
1971 return static_cast<OH_Drawing_FontWeight>(textStyle->fontWeight);
1972 }
1973
OH_Drawing_TextStyleGetFontStyle(OH_Drawing_TextStyle * style)1974 OH_Drawing_FontStyle OH_Drawing_TextStyleGetFontStyle(OH_Drawing_TextStyle* style)
1975 {
1976 if (style == nullptr) {
1977 return FONT_STYLE_NORMAL;
1978 }
1979 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
1980 if (textStyle == nullptr) {
1981 return FONT_STYLE_NORMAL;
1982 }
1983 return static_cast<OH_Drawing_FontStyle>(textStyle->fontStyle);
1984 }
1985
OH_Drawing_TextStyleGetBaseline(OH_Drawing_TextStyle * style)1986 OH_Drawing_TextBaseline OH_Drawing_TextStyleGetBaseline(OH_Drawing_TextStyle* style)
1987 {
1988 if (style == nullptr) {
1989 return TEXT_BASELINE_ALPHABETIC;
1990 }
1991 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
1992 if (textStyle == nullptr) {
1993 return TEXT_BASELINE_ALPHABETIC;
1994 }
1995 return static_cast<OH_Drawing_TextBaseline>(textStyle->baseline);
1996 }
1997
OH_Drawing_TextStyleGetFontFamilies(OH_Drawing_TextStyle * style,size_t * num)1998 char** OH_Drawing_TextStyleGetFontFamilies(OH_Drawing_TextStyle* style, size_t* num)
1999 {
2000 if (style == nullptr || num == nullptr || ConvertToOriginalText<TextStyle>(style) == nullptr) {
2001 return nullptr;
2002 }
2003 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
2004 char** fontFamilies = nullptr;
2005 std::vector<std::string>& textStyleFontFamilies = textStyle->fontFamilies;
2006 fontFamilies = new (std::nothrow) char* [textStyleFontFamilies.size()];
2007 if (fontFamilies == nullptr) {
2008 return nullptr;
2009 }
2010 for (size_t i = 0; i < textStyleFontFamilies.size(); ++i) {
2011 bool res = CopyStrData(&fontFamilies[i], textStyleFontFamilies[i]);
2012 if (!res) {
2013 for (size_t j = 0; j < i; j++) {
2014 delete[] fontFamilies[j];
2015 fontFamilies[j] = nullptr;
2016 }
2017 delete[] fontFamilies;
2018 fontFamilies = nullptr;
2019 return nullptr;
2020 }
2021 }
2022 *num = textStyleFontFamilies.size();
2023 return fontFamilies;
2024 }
2025
OH_Drawing_TextStyleDestroyFontFamilies(char ** fontFamilies,size_t num)2026 void OH_Drawing_TextStyleDestroyFontFamilies(char** fontFamilies, size_t num)
2027 {
2028 if (fontFamilies == nullptr) {
2029 return;
2030 }
2031 for (size_t i = 0; i < num; ++i) {
2032 if (fontFamilies[i] != nullptr) {
2033 delete[] fontFamilies[i];
2034 fontFamilies[i] = nullptr;
2035 }
2036 }
2037 delete[] fontFamilies;
2038 fontFamilies = nullptr;
2039 }
2040
OH_Drawing_TextStyleGetFontSize(OH_Drawing_TextStyle * style)2041 double OH_Drawing_TextStyleGetFontSize(OH_Drawing_TextStyle* style)
2042 {
2043 if (style == nullptr) {
2044 return 0.0;
2045 }
2046 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
2047 if (textStyle == nullptr) {
2048 return 0.0;
2049 }
2050 return textStyle->fontSize;
2051 }
2052
OH_Drawing_TextStyleGetLetterSpacing(OH_Drawing_TextStyle * style)2053 double OH_Drawing_TextStyleGetLetterSpacing(OH_Drawing_TextStyle* style)
2054 {
2055 if (style == nullptr) {
2056 return 0.0;
2057 }
2058 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
2059 if (textStyle == nullptr) {
2060 return 0.0;
2061 }
2062 return textStyle->letterSpacing;
2063 }
2064
OH_Drawing_TextStyleGetWordSpacing(OH_Drawing_TextStyle * style)2065 double OH_Drawing_TextStyleGetWordSpacing(OH_Drawing_TextStyle* style)
2066 {
2067 if (style == nullptr) {
2068 return 0.0;
2069 }
2070 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
2071 if (textStyle == nullptr) {
2072 return 0.0;
2073 }
2074 return textStyle->wordSpacing;
2075 }
2076
OH_Drawing_TextStyleGetFontHeight(OH_Drawing_TextStyle * style)2077 double OH_Drawing_TextStyleGetFontHeight(OH_Drawing_TextStyle* style)
2078 {
2079 if (style == nullptr) {
2080 return 0.0;
2081 }
2082 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
2083 if (textStyle == nullptr) {
2084 return 0.0;
2085 }
2086 return textStyle->heightScale;
2087 }
2088
OH_Drawing_TextStyleGetHalfLeading(OH_Drawing_TextStyle * style)2089 bool OH_Drawing_TextStyleGetHalfLeading(OH_Drawing_TextStyle* style)
2090 {
2091 if (style == nullptr) {
2092 return false;
2093 }
2094 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
2095 if (textStyle == nullptr) {
2096 return false;
2097 }
2098 return textStyle->halfLeading;
2099 }
2100
OH_Drawing_TextStyleGetLocale(OH_Drawing_TextStyle * style)2101 const char* OH_Drawing_TextStyleGetLocale(OH_Drawing_TextStyle* style)
2102 {
2103 if (style == nullptr) {
2104 return nullptr;
2105 }
2106 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
2107 if (textStyle == nullptr) {
2108 return nullptr;
2109 }
2110 return textStyle->locale.c_str();
2111 }
2112
OH_Drawing_TypographyTextSetHeightBehavior(OH_Drawing_TypographyStyle * style,OH_Drawing_TextHeightBehavior heightMode)2113 void OH_Drawing_TypographyTextSetHeightBehavior(
2114 OH_Drawing_TypographyStyle* style, OH_Drawing_TextHeightBehavior heightMode)
2115 {
2116 TypographyStyle* convertStyle = ConvertToOriginalText<TypographyStyle>(style);
2117 if (convertStyle == nullptr) {
2118 return;
2119 }
2120 TextHeightBehavior rosenHeightBehavior;
2121 switch (heightMode) {
2122 case TEXT_HEIGHT_ALL: {
2123 rosenHeightBehavior = TextHeightBehavior::ALL;
2124 break;
2125 }
2126 case TEXT_HEIGHT_DISABLE_FIRST_ASCENT: {
2127 rosenHeightBehavior = TextHeightBehavior::DISABLE_FIRST_ASCENT;
2128 break;
2129 }
2130 case TEXT_HEIGHT_DISABLE_LAST_ASCENT: {
2131 rosenHeightBehavior = TextHeightBehavior::DISABLE_LAST_ASCENT;
2132 break;
2133 }
2134 case TEXT_HEIGHT_DISABLE_ALL: {
2135 rosenHeightBehavior = TextHeightBehavior::DISABLE_ALL;
2136 break;
2137 }
2138 default: {
2139 rosenHeightBehavior = TextHeightBehavior::ALL;
2140 }
2141 }
2142 convertStyle->textHeightBehavior = rosenHeightBehavior;
2143 }
2144
OH_Drawing_TypographyTextGetHeightBehavior(OH_Drawing_TypographyStyle * style)2145 OH_Drawing_TextHeightBehavior OH_Drawing_TypographyTextGetHeightBehavior(OH_Drawing_TypographyStyle* style)
2146 {
2147 TypographyStyle* convertStyle = ConvertToOriginalText<TypographyStyle>(style);
2148 if (convertStyle == nullptr) {
2149 return TEXT_HEIGHT_ALL;
2150 }
2151 TextHeightBehavior innerHeightBehavior = ConvertToOriginalText<TypographyStyle>(style)->textHeightBehavior;
2152 return static_cast<OH_Drawing_TextHeightBehavior>(innerHeightBehavior);
2153 }
2154
OH_Drawing_TypographyMarkDirty(OH_Drawing_Typography * typography)2155 void OH_Drawing_TypographyMarkDirty(OH_Drawing_Typography* typography)
2156 {
2157 if (typography == nullptr || ConvertToOriginalText<Typography>(typography) == nullptr) {
2158 return;
2159 }
2160 ConvertToOriginalText<Typography>(typography)->MarkDirty();
2161 }
2162
OH_Drawing_TypographyGetUnresolvedGlyphsCount(OH_Drawing_Typography * typography)2163 int32_t OH_Drawing_TypographyGetUnresolvedGlyphsCount(OH_Drawing_Typography* typography)
2164 {
2165 if (typography == nullptr || ConvertToOriginalText<Typography>(typography) == nullptr) {
2166 return 0;
2167 }
2168 return ConvertToOriginalText<Typography>(typography)->GetUnresolvedGlyphsCount();
2169 }
2170
OH_Drawing_TypographyUpdateFontSize(OH_Drawing_Typography * typography,size_t from,size_t to,float fontSize)2171 void OH_Drawing_TypographyUpdateFontSize(OH_Drawing_Typography* typography, size_t from, size_t to, float fontSize)
2172 {
2173 if (typography == nullptr || ConvertToOriginalText<Typography>(typography) == nullptr) {
2174 return;
2175 }
2176 ConvertToOriginalText<Typography>(typography)->UpdateFontSize(from, to, fontSize);
2177 }
2178
OH_Drawing_TypographyTextGetLineStyle(OH_Drawing_TypographyStyle * style)2179 bool OH_Drawing_TypographyTextGetLineStyle(OH_Drawing_TypographyStyle* style)
2180 {
2181 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2182 return false;
2183 }
2184 return ConvertToOriginalText<TypographyStyle>(style)->useLineStyle;
2185 }
2186
OH_Drawing_TypographyTextlineStyleGetFontWeight(OH_Drawing_TypographyStyle * style)2187 OH_Drawing_FontWeight OH_Drawing_TypographyTextlineStyleGetFontWeight(OH_Drawing_TypographyStyle* style)
2188 {
2189 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2190 return FONT_WEIGHT_400;
2191 }
2192 return static_cast<OH_Drawing_FontWeight>(ConvertToOriginalText<TypographyStyle>(style)->lineStyleFontWeight);
2193 }
2194
OH_Drawing_TypographyTextlineStyleGetFontStyle(OH_Drawing_TypographyStyle * style)2195 OH_Drawing_FontStyle OH_Drawing_TypographyTextlineStyleGetFontStyle(OH_Drawing_TypographyStyle* style)
2196 {
2197 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2198 return FONT_STYLE_NORMAL;
2199 }
2200 return static_cast<OH_Drawing_FontStyle>(ConvertToOriginalText<TypographyStyle>(style)->lineStyleFontStyle);
2201 }
2202
OH_Drawing_TypographyTextlineStyleGetFontFamilies(OH_Drawing_TypographyStyle * style,size_t * num)2203 char** OH_Drawing_TypographyTextlineStyleGetFontFamilies(OH_Drawing_TypographyStyle* style, size_t* num)
2204 {
2205 if (style == nullptr || num == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2206 return nullptr;
2207 }
2208 char** fontFamilie = nullptr;
2209 const std::vector<std::string>& systemFontFamilies =
2210 ConvertToOriginalText<TypographyStyle>(style)->lineStyleFontFamilies;
2211 if (systemFontFamilies.empty()) {
2212 *num = 0;
2213 return nullptr;
2214 }
2215 fontFamilie = new (std::nothrow) char* [systemFontFamilies.size()];
2216 if (!fontFamilie) {
2217 return nullptr;
2218 }
2219 for (size_t i = 0; i < systemFontFamilies.size(); ++i) {
2220 fontFamilie[i] = new (std::nothrow) char[systemFontFamilies[i].size() + 1];
2221 if (!fontFamilie[i]) {
2222 for (size_t j = 0; j < i; j++) {
2223 delete[] fontFamilie[j];
2224 fontFamilie[j] = nullptr;
2225 }
2226 delete[] fontFamilie;
2227 fontFamilie = nullptr;
2228 return nullptr;
2229 }
2230 auto retMemset =
2231 memset_s(fontFamilie[i], systemFontFamilies[i].size() + 1, '\0', systemFontFamilies[i].size() + 1);
2232 auto retCopy = strcpy_s(fontFamilie[i], systemFontFamilies[i].size() + 1, systemFontFamilies[i].c_str());
2233 if (retMemset != 0 || retCopy != 0) {
2234 for (size_t j = 0; j <= i; j++) {
2235 delete[] fontFamilie[j];
2236 fontFamilie[j] = nullptr;
2237 }
2238 delete[] fontFamilie;
2239 fontFamilie = nullptr;
2240 return nullptr;
2241 }
2242 }
2243 *num = systemFontFamilies.size();
2244 return fontFamilie;
2245 }
2246
OH_Drawing_TypographyTextlineStyleDestroyFontFamilies(char ** fontFamilies,size_t num)2247 void OH_Drawing_TypographyTextlineStyleDestroyFontFamilies(char** fontFamilies, size_t num)
2248 {
2249 if (fontFamilies == nullptr) {
2250 return;
2251 }
2252 for (size_t i = 0; i < num; ++i) {
2253 if (fontFamilies[i] == nullptr) {
2254 continue;
2255 }
2256 delete[] fontFamilies[i];
2257 fontFamilies[i] = nullptr;
2258 }
2259 delete[] fontFamilies;
2260 fontFamilies = nullptr;
2261 }
2262
OH_Drawing_TypographyTextlineStyleGetFontSize(OH_Drawing_TypographyStyle * style)2263 double OH_Drawing_TypographyTextlineStyleGetFontSize(OH_Drawing_TypographyStyle* style)
2264 {
2265 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2266 return 0;
2267 }
2268 return ConvertToOriginalText<TypographyStyle>(style)->lineStyleFontSize;
2269 }
2270
OH_Drawing_TypographyTextlineStyleGetHeightScale(OH_Drawing_TypographyStyle * style)2271 double OH_Drawing_TypographyTextlineStyleGetHeightScale(OH_Drawing_TypographyStyle* style)
2272 {
2273 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2274 return 0;
2275 }
2276 return ConvertToOriginalText<TypographyStyle>(style)->lineStyleHeightScale;
2277 }
2278
OH_Drawing_TypographyTextlineStyleGetHeightOnly(OH_Drawing_TypographyStyle * style)2279 bool OH_Drawing_TypographyTextlineStyleGetHeightOnly(OH_Drawing_TypographyStyle* style)
2280 {
2281 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2282 return false;
2283 }
2284 return ConvertToOriginalText<TypographyStyle>(style)->lineStyleHeightOnly;
2285 }
2286
OH_Drawing_TypographyTextlineStyleGetHalfLeading(OH_Drawing_TypographyStyle * style)2287 bool OH_Drawing_TypographyTextlineStyleGetHalfLeading(OH_Drawing_TypographyStyle* style)
2288 {
2289 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2290 return false;
2291 }
2292 return ConvertToOriginalText<TypographyStyle>(style)->lineStyleHalfLeading;
2293 }
2294
OH_Drawing_TypographyTextlineStyleGetSpacingScale(OH_Drawing_TypographyStyle * style)2295 double OH_Drawing_TypographyTextlineStyleGetSpacingScale(OH_Drawing_TypographyStyle* style)
2296 {
2297 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2298 return 0;
2299 }
2300 return ConvertToOriginalText<TypographyStyle>(style)->lineStyleSpacingScale;
2301 }
2302
OH_Drawing_TypographyTextlineGetStyleOnly(OH_Drawing_TypographyStyle * style)2303 bool OH_Drawing_TypographyTextlineGetStyleOnly(OH_Drawing_TypographyStyle* style)
2304 {
2305 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2306 return false;
2307 }
2308 return ConvertToOriginalText<TypographyStyle>(style)->lineStyleOnly;
2309 }
2310
OH_Drawing_TypographyGetTextAlign(OH_Drawing_TypographyStyle * style)2311 OH_Drawing_TextAlign OH_Drawing_TypographyGetTextAlign(OH_Drawing_TypographyStyle* style)
2312 {
2313 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2314 return TEXT_ALIGN_LEFT;
2315 }
2316 return static_cast<OH_Drawing_TextAlign>(ConvertToOriginalText<TypographyStyle>(style)->textAlign);
2317 }
2318
OH_Drawing_TypographyGetTextDirection(OH_Drawing_TypographyStyle * style)2319 OH_Drawing_TextDirection OH_Drawing_TypographyGetTextDirection(OH_Drawing_TypographyStyle* style)
2320 {
2321 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2322 return TEXT_DIRECTION_LTR;
2323 }
2324 return static_cast<OH_Drawing_TextDirection>(ConvertToOriginalText<TypographyStyle>(style)->textDirection);
2325 }
2326
OH_Drawing_TypographyGetTextMaxLines(OH_Drawing_TypographyStyle * style)2327 size_t OH_Drawing_TypographyGetTextMaxLines(OH_Drawing_TypographyStyle* style)
2328 {
2329 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2330 return 0;
2331 }
2332 return ConvertToOriginalText<TypographyStyle>(style)->maxLines;
2333 }
2334
OH_Drawing_TypographyGetTextEllipsis(OH_Drawing_TypographyStyle * style)2335 char* OH_Drawing_TypographyGetTextEllipsis(OH_Drawing_TypographyStyle* style)
2336 {
2337 if (style == nullptr || ConvertToOriginalText<TypographyStyle>(style) == nullptr) {
2338 return nullptr;
2339 }
2340 std::u16string ellipsis = ConvertToOriginalText<TypographyStyle>(style)->ellipsis;
2341 const char16_t* buffer = ellipsis.c_str();
2342 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
2343 std::string str = convert.to_bytes(buffer);
2344 char* result = new (std::nothrow) char[str.size() + 1];
2345 if (!result) {
2346 return nullptr;
2347 }
2348 if (strcpy_s(result, str.size() + 1, str.c_str()) != 0) {
2349 delete[] result;
2350 return nullptr;
2351 }
2352 return result;
2353 }
2354
OH_Drawing_TypographyDestroyEllipsis(char * ellipsis)2355 void OH_Drawing_TypographyDestroyEllipsis(char* ellipsis)
2356 {
2357 if (ellipsis == nullptr) {
2358 return;
2359 }
2360 delete[] ellipsis;
2361 ellipsis = nullptr;
2362 }
2363
OH_Drawing_TypographyStyleEquals(OH_Drawing_TypographyStyle * from,OH_Drawing_TypographyStyle * to)2364 bool OH_Drawing_TypographyStyleEquals(OH_Drawing_TypographyStyle* from, OH_Drawing_TypographyStyle* to)
2365 {
2366 if (from == to) {
2367 return true;
2368 }
2369 if (from == nullptr || to == nullptr) {
2370 return false;
2371 }
2372 return *ConvertToOriginalText<TypographyStyle>(from) == *ConvertToOriginalText<TypographyStyle>(to);
2373 }
2374
InitDrawingAliasInfoSet(const size_t aliasInfoSize,OH_Drawing_FontConfigInfoErrorCode & code)2375 static OH_Drawing_FontAliasInfo* InitDrawingAliasInfoSet(
2376 const size_t aliasInfoSize, OH_Drawing_FontConfigInfoErrorCode& code)
2377 {
2378 if (!aliasInfoSize) {
2379 code = SUCCESS_FONT_CONFIG_INFO;
2380 return nullptr;
2381 }
2382 if (aliasInfoSize >= std::numeric_limits<int16_t>::max()) {
2383 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2384 return nullptr;
2385 }
2386 OH_Drawing_FontAliasInfo* aliasInfoArray = new (std::nothrow) OH_Drawing_FontAliasInfo[aliasInfoSize];
2387 if (aliasInfoArray == nullptr) {
2388 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2389 return nullptr;
2390 }
2391
2392 for (size_t i = 0; i < aliasInfoSize;) {
2393 aliasInfoArray[i].familyName = nullptr;
2394 aliasInfoArray[i].weight = 0;
2395 i++;
2396 }
2397 code = SUCCESS_FONT_CONFIG_INFO;
2398 return aliasInfoArray;
2399 }
2400
ResetString(char ** ptr)2401 static void ResetString(char** ptr)
2402 {
2403 if (!ptr || !(*ptr)) {
2404 return;
2405 }
2406 delete[] (*ptr);
2407 (*ptr) = nullptr;
2408 }
2409
ResetDrawingAliasInfoSet(OH_Drawing_FontAliasInfo ** aliasInfoArray,size_t & aliasInfoSize)2410 static void ResetDrawingAliasInfoSet(OH_Drawing_FontAliasInfo** aliasInfoArray, size_t& aliasInfoSize)
2411 {
2412 if (aliasInfoArray == nullptr || *aliasInfoArray == nullptr) {
2413 return;
2414 }
2415
2416 for (size_t i = 0; i < aliasInfoSize; i++) {
2417 ResetString(&((*aliasInfoArray)[i].familyName));
2418 }
2419
2420 delete[] (*aliasInfoArray);
2421 (*aliasInfoArray) = nullptr;
2422 aliasInfoSize = 0;
2423 }
2424
InitDrawingAdjustInfoSet(const size_t adjustInfoSize,OH_Drawing_FontConfigInfoErrorCode & code)2425 static OH_Drawing_FontAdjustInfo* InitDrawingAdjustInfoSet(
2426 const size_t adjustInfoSize, OH_Drawing_FontConfigInfoErrorCode& code)
2427 {
2428 if (!adjustInfoSize) {
2429 code = SUCCESS_FONT_CONFIG_INFO;
2430 return nullptr;
2431 }
2432 if (adjustInfoSize >= std::numeric_limits<int16_t>::max()) {
2433 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2434 return nullptr;
2435 }
2436 OH_Drawing_FontAdjustInfo* adjustInfoArray = new (std::nothrow) OH_Drawing_FontAdjustInfo[adjustInfoSize];
2437 if (adjustInfoArray == nullptr) {
2438 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2439 return nullptr;
2440 }
2441
2442 for (size_t i = 0; i < adjustInfoSize;) {
2443 adjustInfoArray[i].weight = 0;
2444 adjustInfoArray[i].to = 0;
2445 i++;
2446 }
2447 code = SUCCESS_FONT_CONFIG_INFO;
2448 return adjustInfoArray;
2449 }
2450
ResetDrawingAdjustInfo(OH_Drawing_FontAdjustInfo ** adjustInfoArray,size_t & adjustInfoSize)2451 static void ResetDrawingAdjustInfo(OH_Drawing_FontAdjustInfo** adjustInfoArray, size_t& adjustInfoSize)
2452 {
2453 if (adjustInfoArray == nullptr || *adjustInfoArray == nullptr) {
2454 return;
2455 }
2456 delete[] (*adjustInfoArray);
2457 (*adjustInfoArray) = nullptr;
2458 adjustInfoSize = 0;
2459 }
2460
InitDrawingFontGenericInfoSet(const size_t fontGenericInfoSize,OH_Drawing_FontConfigInfoErrorCode & code)2461 static OH_Drawing_FontGenericInfo* InitDrawingFontGenericInfoSet(
2462 const size_t fontGenericInfoSize, OH_Drawing_FontConfigInfoErrorCode& code)
2463 {
2464 if (!fontGenericInfoSize) {
2465 code = ERROR_FONT_CONFIG_INFO_PARSE_FILE;
2466 return nullptr;
2467 }
2468 if (fontGenericInfoSize >= std::numeric_limits<int16_t>::max()) {
2469 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2470 return nullptr;
2471 }
2472
2473 OH_Drawing_FontGenericInfo* fontGenericInfoArray =
2474 new (std::nothrow) OH_Drawing_FontGenericInfo[fontGenericInfoSize];
2475 if (fontGenericInfoArray == nullptr) {
2476 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2477 return nullptr;
2478 }
2479
2480 for (size_t index = 0; index < fontGenericInfoSize;) {
2481 fontGenericInfoArray[index].familyName = nullptr;
2482 fontGenericInfoArray[index].aliasInfoSize = 0;
2483 fontGenericInfoArray[index].adjustInfoSize = 0;
2484 fontGenericInfoArray[index].aliasInfoSet = nullptr;
2485 fontGenericInfoArray[index].adjustInfoSet = nullptr;
2486 index++;
2487 }
2488 code = SUCCESS_FONT_CONFIG_INFO;
2489 return fontGenericInfoArray;
2490 }
2491
ResetDrawingFontGenericInfo(OH_Drawing_FontGenericInfo & fontGenericInfo)2492 static void ResetDrawingFontGenericInfo(OH_Drawing_FontGenericInfo& fontGenericInfo)
2493 {
2494 ResetString(&fontGenericInfo.familyName);
2495 ResetDrawingAliasInfoSet(&fontGenericInfo.aliasInfoSet, fontGenericInfo.aliasInfoSize);
2496 ResetDrawingAdjustInfo(&fontGenericInfo.adjustInfoSet, fontGenericInfo.adjustInfoSize);
2497 }
2498
ResetDrawingFontGenericInfoSet(OH_Drawing_FontGenericInfo ** fontGenericInfoArray,size_t & fontGenericInfoSize)2499 static void ResetDrawingFontGenericInfoSet(
2500 OH_Drawing_FontGenericInfo** fontGenericInfoArray, size_t& fontGenericInfoSize)
2501 {
2502 if (fontGenericInfoArray == nullptr || *fontGenericInfoArray == nullptr) {
2503 return;
2504 }
2505
2506 for (size_t i = 0; i < fontGenericInfoSize; i++) {
2507 ResetDrawingFontGenericInfo((*fontGenericInfoArray)[i]);
2508 }
2509
2510 delete[] (*fontGenericInfoArray);
2511 (*fontGenericInfoArray) = nullptr;
2512 fontGenericInfoSize = 0;
2513 }
2514
InitDrawingDrawingFallbackInfoSet(const size_t fallbackInfoSize,OH_Drawing_FontConfigInfoErrorCode & code)2515 static OH_Drawing_FontFallbackInfo* InitDrawingDrawingFallbackInfoSet(
2516 const size_t fallbackInfoSize, OH_Drawing_FontConfigInfoErrorCode& code)
2517 {
2518 if (!fallbackInfoSize) {
2519 code = SUCCESS_FONT_CONFIG_INFO;
2520 return nullptr;
2521 }
2522 if (fallbackInfoSize >= std::numeric_limits<int16_t>::max()) {
2523 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2524 return nullptr;
2525 }
2526 OH_Drawing_FontFallbackInfo* fallbackInfoArray = new (std::nothrow) OH_Drawing_FontFallbackInfo[fallbackInfoSize];
2527 if (fallbackInfoArray == nullptr) {
2528 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2529 return nullptr;
2530 }
2531
2532 for (size_t i = 0; i < fallbackInfoSize;) {
2533 fallbackInfoArray[i].language = nullptr;
2534 fallbackInfoArray[i].familyName = nullptr;
2535 i++;
2536 }
2537 code = SUCCESS_FONT_CONFIG_INFO;
2538 return fallbackInfoArray;
2539 }
2540
ResetDrawingFallbackInfo(OH_Drawing_FontFallbackInfo & fallbackInfo)2541 static void ResetDrawingFallbackInfo(OH_Drawing_FontFallbackInfo& fallbackInfo)
2542 {
2543 ResetString(&fallbackInfo.language);
2544 ResetString(&fallbackInfo.familyName);
2545 }
2546
ResetDrawingFallbackInfoSet(OH_Drawing_FontFallbackInfo ** fallbackInfoArray,size_t & fallbackInfoSize)2547 static void ResetDrawingFallbackInfoSet(OH_Drawing_FontFallbackInfo** fallbackInfoArray, size_t& fallbackInfoSize)
2548 {
2549 if (fallbackInfoArray == nullptr || *fallbackInfoArray == nullptr) {
2550 return;
2551 }
2552
2553 for (size_t i = 0; i < fallbackInfoSize; i++) {
2554 ResetDrawingFallbackInfo((*fallbackInfoArray)[i]);
2555 }
2556 delete[] (*fallbackInfoArray);
2557 (*fallbackInfoArray) = nullptr;
2558 fallbackInfoSize = 0;
2559 }
2560
InitDrawingFallbackGroupSet(const size_t fallbackGroupSize,OH_Drawing_FontConfigInfoErrorCode & code)2561 static OH_Drawing_FontFallbackGroup* InitDrawingFallbackGroupSet(
2562 const size_t fallbackGroupSize, OH_Drawing_FontConfigInfoErrorCode& code)
2563 {
2564 if (!fallbackGroupSize) {
2565 code = SUCCESS_FONT_CONFIG_INFO;
2566 return nullptr;
2567 }
2568 if (fallbackGroupSize >= std::numeric_limits<int16_t>::max()) {
2569 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2570 return nullptr;
2571 }
2572 OH_Drawing_FontFallbackGroup* fallbackGroupArray =
2573 new (std::nothrow) OH_Drawing_FontFallbackGroup[fallbackGroupSize];
2574 if (fallbackGroupArray == nullptr) {
2575 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2576 return nullptr;
2577 }
2578
2579 for (size_t i = 0; i < fallbackGroupSize;) {
2580 fallbackGroupArray[i].groupName = nullptr;
2581 fallbackGroupArray[i].fallbackInfoSize = 0;
2582 fallbackGroupArray[i].fallbackInfoSet = nullptr;
2583 i++;
2584 }
2585 code = SUCCESS_FONT_CONFIG_INFO;
2586 return fallbackGroupArray;
2587 }
2588
ResetDrawingFallbackGroup(OH_Drawing_FontFallbackGroup & fallbackGroup)2589 static void ResetDrawingFallbackGroup(OH_Drawing_FontFallbackGroup& fallbackGroup)
2590 {
2591 ResetString(&fallbackGroup.groupName);
2592 ResetDrawingFallbackInfoSet(&fallbackGroup.fallbackInfoSet, fallbackGroup.fallbackInfoSize);
2593 }
2594
ResetDrawingFallbackGroupSet(OH_Drawing_FontFallbackGroup ** fallbackGroupArray,size_t & fallbackGroupSize)2595 static void ResetDrawingFallbackGroupSet(OH_Drawing_FontFallbackGroup** fallbackGroupArray, size_t& fallbackGroupSize)
2596 {
2597 if (fallbackGroupArray == nullptr || *fallbackGroupArray == nullptr) {
2598 return;
2599 }
2600
2601 for (size_t i = 0; i < fallbackGroupSize; i++) {
2602 ResetDrawingFallbackGroup((*fallbackGroupArray)[i]);
2603 }
2604 delete[] (*fallbackGroupArray);
2605 (*fallbackGroupArray) = nullptr;
2606 fallbackGroupSize = 0;
2607 }
2608
InitDrawingFontConfigJsonInfo()2609 static OH_Drawing_FontConfigInfo* InitDrawingFontConfigJsonInfo()
2610 {
2611 OH_Drawing_FontConfigInfo* drawFontCfgInfo = new (std::nothrow) OH_Drawing_FontConfigInfo;
2612 if (drawFontCfgInfo == nullptr) {
2613 return nullptr;
2614 }
2615
2616 drawFontCfgInfo->fontDirSize = 0;
2617 drawFontCfgInfo->fontGenericInfoSize = 0;
2618 drawFontCfgInfo->fallbackGroupSize = 0;
2619 drawFontCfgInfo->fontDirSet = nullptr;
2620 drawFontCfgInfo->fontGenericInfoSet = nullptr;
2621 drawFontCfgInfo->fallbackGroupSet = nullptr;
2622
2623 return drawFontCfgInfo;
2624 }
2625
InitStringArray(const size_t charArraySize)2626 static char** InitStringArray(const size_t charArraySize)
2627 {
2628 if (!charArraySize || charArraySize >= std::numeric_limits<int16_t>::max()) {
2629 return nullptr;
2630 }
2631
2632 char** ptr = new (std::nothrow) char* [charArraySize];
2633 if (!ptr) {
2634 return nullptr;
2635 }
2636
2637 for (size_t i = 0; i < charArraySize; i++) {
2638 ptr[i] = nullptr;
2639 }
2640 return ptr;
2641 }
2642
ResetStringArray(char *** ptr,size_t & charArraySize)2643 static void ResetStringArray(char*** ptr, size_t& charArraySize)
2644 {
2645 if (ptr == nullptr || *ptr == nullptr) {
2646 return;
2647 }
2648 for (size_t i = 0; i < charArraySize; i++) {
2649 if (!((*ptr)[i])) {
2650 continue;
2651 }
2652 delete[] ((*ptr)[i]);
2653 ((*ptr)[i]) = nullptr;
2654 }
2655 delete[] (*ptr);
2656 (*ptr) = nullptr;
2657 charArraySize = 0;
2658 }
2659
ResetDrawingFontConfigJsonInfo(OH_Drawing_FontConfigInfo ** drawFontCfgInfo)2660 static void ResetDrawingFontConfigJsonInfo(OH_Drawing_FontConfigInfo** drawFontCfgInfo)
2661 {
2662 if (drawFontCfgInfo == nullptr || *drawFontCfgInfo == nullptr) {
2663 return;
2664 }
2665 delete (*drawFontCfgInfo);
2666 (*drawFontCfgInfo) = nullptr;
2667 }
2668
CopyDrawingFontDirSet(char *** drawFontDirSet,size_t & fontDirSize,const std::vector<std::string> & fontDirSet,OH_Drawing_FontConfigInfoErrorCode & code)2669 static bool CopyDrawingFontDirSet(char*** drawFontDirSet, size_t& fontDirSize,
2670 const std::vector<std::string>& fontDirSet, OH_Drawing_FontConfigInfoErrorCode& code)
2671 {
2672 if (!drawFontDirSet) {
2673 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2674 return false;
2675 }
2676 if (fontDirSet.empty()) {
2677 code = ERROR_FONT_CONFIG_INFO_PARSE_FILE;
2678 return false;
2679 }
2680
2681 size_t size = fontDirSet.size();
2682 (*drawFontDirSet) = InitStringArray(size);
2683 if (!(*drawFontDirSet)) {
2684 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2685 return false;
2686 }
2687
2688 for (size_t i = 0; i < size; ++i) {
2689 bool result = CopyStrData(&((*drawFontDirSet)[i]), fontDirSet.at(i), &code);
2690 if (result) {
2691 fontDirSize++;
2692 } else {
2693 break;
2694 }
2695 }
2696 if (fontDirSize != size) {
2697 ResetStringArray(drawFontDirSet, fontDirSize);
2698 return false;
2699 }
2700 return true;
2701 }
2702
CopyDrawingAliasInfo(OH_Drawing_FontAliasInfo & drawAliasInfo,const TextEngine::AliasInfo & aliasInfo,OH_Drawing_FontConfigInfoErrorCode & code)2703 static bool CopyDrawingAliasInfo(OH_Drawing_FontAliasInfo& drawAliasInfo, const TextEngine::AliasInfo& aliasInfo,
2704 OH_Drawing_FontConfigInfoErrorCode& code)
2705 {
2706 bool result = CopyStrData(&drawAliasInfo.familyName, aliasInfo.familyName, &code);
2707 if (!result) {
2708 return false;
2709 }
2710 drawAliasInfo.weight = aliasInfo.weight;
2711 code = SUCCESS_FONT_CONFIG_INFO;
2712 return true;
2713 }
2714
CopyDrawingAliasInfoSet(OH_Drawing_FontAliasInfo ** drawAliasInfoSet,size_t & aliasInfoSize,const std::vector<TextEngine::AliasInfo> & aliasSet,OH_Drawing_FontConfigInfoErrorCode & code)2715 static bool CopyDrawingAliasInfoSet(OH_Drawing_FontAliasInfo** drawAliasInfoSet, size_t& aliasInfoSize,
2716 const std::vector<TextEngine::AliasInfo>& aliasSet, OH_Drawing_FontConfigInfoErrorCode& code)
2717 {
2718 if (!drawAliasInfoSet) {
2719 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2720 return false;
2721 }
2722
2723 if (!aliasSet.empty()) {
2724 (*drawAliasInfoSet) = InitDrawingAliasInfoSet(aliasSet.size(), code);
2725 if (!(*drawAliasInfoSet)) {
2726 return false;
2727 }
2728 size_t aliasInfoCount = 0;
2729 for (; aliasInfoCount < aliasSet.size();) {
2730 bool result = CopyDrawingAliasInfo((*drawAliasInfoSet)[aliasInfoCount], aliasSet.at(aliasInfoCount), code);
2731 if (result) {
2732 ++aliasInfoCount;
2733 } else {
2734 break;
2735 }
2736 }
2737 aliasInfoSize = aliasInfoCount;
2738 if (aliasInfoSize != aliasSet.size()) {
2739 return false;
2740 }
2741 }
2742 return true;
2743 }
2744
CopyDrawingAdjustInfo(OH_Drawing_FontAdjustInfo & drawAdjustInfo,const TextEngine::AdjustInfo & adjustInfo)2745 static void CopyDrawingAdjustInfo(OH_Drawing_FontAdjustInfo& drawAdjustInfo, const TextEngine::AdjustInfo& adjustInfo)
2746 {
2747 drawAdjustInfo.weight = adjustInfo.origValue;
2748 drawAdjustInfo.to = adjustInfo.newValue;
2749 }
2750
CopyDrawingAdjustSet(OH_Drawing_FontAdjustInfo ** drawAdjustInfoSet,size_t & adjustInfoSize,const std::vector<TextEngine::AdjustInfo> & adjustSet,OH_Drawing_FontConfigInfoErrorCode & code)2751 static bool CopyDrawingAdjustSet(OH_Drawing_FontAdjustInfo** drawAdjustInfoSet, size_t& adjustInfoSize,
2752 const std::vector<TextEngine::AdjustInfo>& adjustSet, OH_Drawing_FontConfigInfoErrorCode& code)
2753 {
2754 if (!drawAdjustInfoSet) {
2755 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2756 return false;
2757 }
2758
2759 if (!adjustSet.empty()) {
2760 (*drawAdjustInfoSet) = InitDrawingAdjustInfoSet(adjustSet.size(), code);
2761 if (!(*drawAdjustInfoSet)) {
2762 return false;
2763 }
2764 size_t adjustInfoCount = 0;
2765 for (; adjustInfoCount < adjustSet.size();) {
2766 CopyDrawingAdjustInfo((*drawAdjustInfoSet)[adjustInfoCount], adjustSet.at(adjustInfoCount));
2767 ++adjustInfoCount;
2768 }
2769 adjustInfoSize = adjustInfoCount;
2770 if (adjustInfoSize != adjustSet.size()) {
2771 return false;
2772 }
2773 }
2774 code = SUCCESS_FONT_CONFIG_INFO;
2775 return true;
2776 }
2777
CopyDrawingFontGenericInfo(OH_Drawing_FontGenericInfo & drawFontGenericInfo,const TextEngine::FontGenericInfo & genericInfo,OH_Drawing_FontConfigInfoErrorCode & code)2778 static bool CopyDrawingFontGenericInfo(OH_Drawing_FontGenericInfo& drawFontGenericInfo,
2779 const TextEngine::FontGenericInfo& genericInfo, OH_Drawing_FontConfigInfoErrorCode& code)
2780 {
2781 bool result = CopyStrData(&drawFontGenericInfo.familyName, genericInfo.familyName, &code);
2782 if (!result) {
2783 return false;
2784 }
2785
2786 result = CopyDrawingAliasInfoSet(
2787 &drawFontGenericInfo.aliasInfoSet, drawFontGenericInfo.aliasInfoSize, genericInfo.aliasSet, code);
2788 if (!result) {
2789 return false;
2790 }
2791
2792 result = CopyDrawingAdjustSet(
2793 &drawFontGenericInfo.adjustInfoSet, drawFontGenericInfo.adjustInfoSize, genericInfo.adjustSet, code);
2794 if (!result) {
2795 return false;
2796 }
2797 return true;
2798 }
2799
CopyDrawingFontGenericInfoSetInner(OH_Drawing_FontGenericInfo ** fontGenericInfoSet,size_t & fontGenericInfoSize,const std::vector<TextEngine::FontGenericInfo> & genericSet,OH_Drawing_FontConfigInfoErrorCode & code)2800 static bool CopyDrawingFontGenericInfoSetInner(OH_Drawing_FontGenericInfo** fontGenericInfoSet,
2801 size_t& fontGenericInfoSize, const std::vector<TextEngine::FontGenericInfo>& genericSet,
2802 OH_Drawing_FontConfigInfoErrorCode& code)
2803 {
2804 if (!fontGenericInfoSet || !(*fontGenericInfoSet)) {
2805 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2806 return false;
2807 }
2808 size_t genericInfoNum = 0;
2809 for (; genericInfoNum < genericSet.size();) {
2810 auto& fontGenericInfo = (*fontGenericInfoSet)[genericInfoNum];
2811 bool result = CopyDrawingFontGenericInfo(fontGenericInfo, genericSet.at(genericInfoNum), code);
2812 if (!result) {
2813 ResetDrawingFontGenericInfo(fontGenericInfo);
2814 break;
2815 } else {
2816 ++genericInfoNum;
2817 }
2818 }
2819 fontGenericInfoSize = genericInfoNum;
2820 if (fontGenericInfoSize != genericSet.size()) {
2821 ResetDrawingFontGenericInfoSet(fontGenericInfoSet, fontGenericInfoSize);
2822 return false;
2823 }
2824 code = SUCCESS_FONT_CONFIG_INFO;
2825 return true;
2826 }
2827
CopyDrawingFallbackInfo(OH_Drawing_FontFallbackInfo & drawFallbackInfo,const TextEngine::FallbackInfo & fallbackInfo,OH_Drawing_FontConfigInfoErrorCode & code)2828 static bool CopyDrawingFallbackInfo(OH_Drawing_FontFallbackInfo& drawFallbackInfo,
2829 const TextEngine::FallbackInfo& fallbackInfo, OH_Drawing_FontConfigInfoErrorCode& code)
2830 {
2831 if (!fallbackInfo.font.empty() && !CopyStrData(&drawFallbackInfo.language, fallbackInfo.font, &code)) {
2832 return false;
2833 }
2834 if (!fallbackInfo.familyName.empty() &&
2835 !CopyStrData(&drawFallbackInfo.familyName, fallbackInfo.familyName, &code)) {
2836 return false;
2837 }
2838 return true;
2839 }
2840
CopyDrawingFallbackGroup(OH_Drawing_FontFallbackGroup & drawFallbackGroup,const TextEngine::FallbackGroup & fallbackGroup,OH_Drawing_FontConfigInfoErrorCode & code)2841 static bool CopyDrawingFallbackGroup(OH_Drawing_FontFallbackGroup& drawFallbackGroup,
2842 const TextEngine::FallbackGroup& fallbackGroup, OH_Drawing_FontConfigInfoErrorCode& code)
2843 {
2844 if (!fallbackGroup.groupName.empty()) {
2845 if (!CopyStrData(&drawFallbackGroup.groupName, fallbackGroup.groupName, &code)) {
2846 return false;
2847 }
2848 }
2849 auto& fallbackInfoSet = fallbackGroup.fallbackInfoSet;
2850 if (fallbackInfoSet.empty()) {
2851 code = SUCCESS_FONT_CONFIG_INFO;
2852 return true;
2853 }
2854 drawFallbackGroup.fallbackInfoSet = InitDrawingDrawingFallbackInfoSet(fallbackInfoSet.size(), code);
2855 if (!drawFallbackGroup.fallbackInfoSet) {
2856 return false;
2857 }
2858 size_t fallbackInfoCount = 0;
2859 for (; fallbackInfoCount < fallbackInfoSet.size();) {
2860 auto& fallbackInfo = drawFallbackGroup.fallbackInfoSet[fallbackInfoCount];
2861 bool res = CopyDrawingFallbackInfo(fallbackInfo, fallbackInfoSet[fallbackInfoCount], code);
2862 if (res) {
2863 ++fallbackInfoCount;
2864 } else {
2865 ResetDrawingFallbackInfo(fallbackInfo);
2866 break;
2867 }
2868 }
2869 drawFallbackGroup.fallbackInfoSize = fallbackInfoCount;
2870 if (drawFallbackGroup.fallbackInfoSize != fallbackInfoSet.size()) {
2871 return false;
2872 }
2873 return true;
2874 }
2875
CopyDrawingFallbackGroupSetInner(OH_Drawing_FontFallbackGroup ** drawFallbackGroupSet,size_t & fallbackGroupSize,const std::vector<TextEngine::FallbackGroup> & fallbackGroupSet,OH_Drawing_FontConfigInfoErrorCode & code)2876 static bool CopyDrawingFallbackGroupSetInner(OH_Drawing_FontFallbackGroup** drawFallbackGroupSet,
2877 size_t& fallbackGroupSize, const std::vector<TextEngine::FallbackGroup>& fallbackGroupSet,
2878 OH_Drawing_FontConfigInfoErrorCode& code)
2879 {
2880 if (!drawFallbackGroupSet) {
2881 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2882 return false;
2883 }
2884 (*drawFallbackGroupSet) = InitDrawingFallbackGroupSet(fallbackGroupSet.size(), code);
2885 if (!(*drawFallbackGroupSet)) {
2886 return false;
2887 }
2888 size_t fallbackGroupNum = 0;
2889 for (; fallbackGroupNum < fallbackGroupSet.size();) {
2890 auto& fallbackGroup = (*drawFallbackGroupSet)[fallbackGroupNum];
2891 bool res = CopyDrawingFallbackGroup(fallbackGroup, fallbackGroupSet.at(fallbackGroupNum), code);
2892 if (res) {
2893 fallbackGroupNum++;
2894 } else {
2895 ResetDrawingFallbackGroup(fallbackGroup);
2896 break;
2897 }
2898 }
2899 fallbackGroupSize = fallbackGroupNum;
2900 if (fallbackGroupSize != fallbackGroupSet.size()) {
2901 ResetDrawingFallbackGroupSet(drawFallbackGroupSet, fallbackGroupSize);
2902 return false;
2903 }
2904 return true;
2905 }
2906
CopyDrawingFontGenericInfoSet(OH_Drawing_FontConfigInfo ** drawFontCfgInfo,const std::vector<TextEngine::FontGenericInfo> & genericSet,OH_Drawing_FontConfigInfoErrorCode & code)2907 static bool CopyDrawingFontGenericInfoSet(OH_Drawing_FontConfigInfo** drawFontCfgInfo,
2908 const std::vector<TextEngine::FontGenericInfo>& genericSet, OH_Drawing_FontConfigInfoErrorCode& code)
2909 {
2910 if (!drawFontCfgInfo || !(*drawFontCfgInfo)) {
2911 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2912 return false;
2913 }
2914
2915 size_t size = genericSet.size();
2916 (*drawFontCfgInfo)->fontGenericInfoSet = InitDrawingFontGenericInfoSet(size, code);
2917 if (!(*drawFontCfgInfo)->fontGenericInfoSet) {
2918 ResetStringArray(&((*drawFontCfgInfo)->fontDirSet), (*drawFontCfgInfo)->fontDirSize);
2919 ResetDrawingFontConfigJsonInfo(drawFontCfgInfo);
2920 return false;
2921 }
2922
2923 bool result = CopyDrawingFontGenericInfoSetInner(
2924 &((*drawFontCfgInfo)->fontGenericInfoSet), (*drawFontCfgInfo)->fontGenericInfoSize, genericSet, code);
2925 if (!result) {
2926 ResetStringArray(&((*drawFontCfgInfo)->fontDirSet), (*drawFontCfgInfo)->fontDirSize);
2927 ResetDrawingFontConfigJsonInfo(drawFontCfgInfo);
2928 return false;
2929 }
2930 return true;
2931 }
2932
CopyDrawingFallbackGroupSet(OH_Drawing_FontConfigInfo ** drawFontCfgInfo,const std::vector<TextEngine::FallbackGroup> & fallbackGroupSet,OH_Drawing_FontConfigInfoErrorCode & code)2933 static bool CopyDrawingFallbackGroupSet(OH_Drawing_FontConfigInfo** drawFontCfgInfo,
2934 const std::vector<TextEngine::FallbackGroup>& fallbackGroupSet, OH_Drawing_FontConfigInfoErrorCode& code)
2935 {
2936 if (!drawFontCfgInfo || !(*drawFontCfgInfo)) {
2937 code = ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY;
2938 return false;
2939 }
2940
2941 if (fallbackGroupSet.empty()) {
2942 code = SUCCESS_FONT_CONFIG_INFO;
2943 return true;
2944 }
2945 bool result = CopyDrawingFallbackGroupSetInner(
2946 &((*drawFontCfgInfo)->fallbackGroupSet), (*drawFontCfgInfo)->fallbackGroupSize, fallbackGroupSet, code);
2947 if (!result) {
2948 ResetDrawingFontGenericInfoSet(
2949 &((*drawFontCfgInfo)->fontGenericInfoSet), (*drawFontCfgInfo)->fontGenericInfoSize);
2950 ResetStringArray(&((*drawFontCfgInfo)->fontDirSet), (*drawFontCfgInfo)->fontDirSize);
2951 ResetDrawingFontConfigJsonInfo(drawFontCfgInfo);
2952 return false;
2953 }
2954 return true;
2955 }
2956
OH_Drawing_GetSystemFontConfigInfo(OH_Drawing_FontConfigInfoErrorCode * errorCode)2957 OH_Drawing_FontConfigInfo* OH_Drawing_GetSystemFontConfigInfo(OH_Drawing_FontConfigInfoErrorCode* errorCode)
2958 {
2959 OH_Drawing_FontConfigInfoErrorCode code = ERROR_FONT_CONFIG_INFO_UNKNOWN;
2960 TextEngine::FontConfigJson fontConfigJson;
2961 int res = fontConfigJson.ParseFile();
2962 if (res) {
2963 code = ERROR_FONT_CONFIG_INFO_PARSE_FILE;
2964 SetFontConfigInfoErrorCode(code, errorCode);
2965 return nullptr;
2966 }
2967 auto fontCfgJsonInfo = fontConfigJson.GetFontConfigJsonInfo();
2968 if (!fontCfgJsonInfo) {
2969 SetFontConfigInfoErrorCode(OH_Drawing_FontConfigInfoErrorCode::ERROR_FONT_CONFIG_INFO_PARSE_FILE, errorCode);
2970 return nullptr;
2971 }
2972
2973 OH_Drawing_FontConfigInfo* drawFontCfgInfo = InitDrawingFontConfigJsonInfo();
2974 if (!drawFontCfgInfo) {
2975 SetFontConfigInfoErrorCode(OH_Drawing_FontConfigInfoErrorCode::ERROR_FONT_CONFIG_INFO_ALLOC_MEMORY, errorCode);
2976 return nullptr;
2977 }
2978
2979 bool result = CopyDrawingFontDirSet(
2980 &drawFontCfgInfo->fontDirSet, drawFontCfgInfo->fontDirSize, fontCfgJsonInfo->fontDirSet, code);
2981 if (!result) {
2982 ResetDrawingFontConfigJsonInfo(&drawFontCfgInfo);
2983 SetFontConfigInfoErrorCode(code, errorCode);
2984 return drawFontCfgInfo;
2985 }
2986
2987 result = CopyDrawingFontGenericInfoSet(&drawFontCfgInfo, fontCfgJsonInfo->genericSet, code);
2988 if (!result) {
2989 SetFontConfigInfoErrorCode(code, errorCode);
2990 return drawFontCfgInfo;
2991 }
2992
2993 CopyDrawingFallbackGroupSet(&drawFontCfgInfo, fontCfgJsonInfo->fallbackGroupSet, code);
2994 SetFontConfigInfoErrorCode(code, errorCode);
2995 return drawFontCfgInfo;
2996 }
2997
OH_Drawing_DestroySystemFontConfigInfo(OH_Drawing_FontConfigInfo * drawFontCfgInfo)2998 void OH_Drawing_DestroySystemFontConfigInfo(OH_Drawing_FontConfigInfo* drawFontCfgInfo)
2999 {
3000 if (drawFontCfgInfo == nullptr) {
3001 return;
3002 }
3003
3004 ResetDrawingFallbackGroupSet(&drawFontCfgInfo->fallbackGroupSet, drawFontCfgInfo->fallbackGroupSize);
3005 ResetDrawingFontGenericInfoSet(&drawFontCfgInfo->fontGenericInfoSet, drawFontCfgInfo->fontGenericInfoSize);
3006 ResetStringArray(&drawFontCfgInfo->fontDirSet, drawFontCfgInfo->fontDirSize);
3007 delete drawFontCfgInfo;
3008 }
3009
OH_Drawing_TextStyleIsEqual(const OH_Drawing_TextStyle * style,const OH_Drawing_TextStyle * comparedStyle)3010 bool OH_Drawing_TextStyleIsEqual(const OH_Drawing_TextStyle* style, const OH_Drawing_TextStyle* comparedStyle)
3011 {
3012 auto convertStyle = ConvertToOriginalText<const OHOS::Rosen::TextStyle>(style);
3013 auto convertComparedStyle = ConvertToOriginalText<const OHOS::Rosen::TextStyle>(comparedStyle);
3014 if ((convertStyle == nullptr && convertComparedStyle != nullptr) ||
3015 (convertStyle != nullptr && convertComparedStyle == nullptr)) {
3016 return false;
3017 }
3018 if (convertStyle == nullptr && convertComparedStyle == nullptr) {
3019 return true;
3020 }
3021 return convertStyle == convertComparedStyle || *convertStyle == *convertComparedStyle;
3022 }
3023
OH_Drawing_TextStyleIsEqualByFont(const OH_Drawing_TextStyle * style,const OH_Drawing_TextStyle * comparedStyle)3024 bool OH_Drawing_TextStyleIsEqualByFont(const OH_Drawing_TextStyle* style, const OH_Drawing_TextStyle* comparedStyle)
3025 {
3026 auto convertStyle = ConvertToOriginalText<const OHOS::Rosen::TextStyle>(style);
3027 auto convertComparedStyle = ConvertToOriginalText<const OHOS::Rosen::TextStyle>(comparedStyle);
3028 if (convertStyle == nullptr || convertComparedStyle == nullptr) {
3029 return false;
3030 }
3031 return convertStyle->EqualByFonts(*convertComparedStyle);
3032 }
3033
OH_Drawing_TextStyleIsAttributeMatched(const OH_Drawing_TextStyle * style,const OH_Drawing_TextStyle * comparedStyle,OH_Drawing_TextStyleType textStyleType)3034 bool OH_Drawing_TextStyleIsAttributeMatched(const OH_Drawing_TextStyle* style,
3035 const OH_Drawing_TextStyle* comparedStyle, OH_Drawing_TextStyleType textStyleType)
3036 {
3037 auto convertStyle = ConvertToOriginalText<const OHOS::Rosen::TextStyle>(style);
3038 auto convertComparedStyle = ConvertToOriginalText<const OHOS::Rosen::TextStyle>(comparedStyle);
3039 if (convertStyle == nullptr || convertComparedStyle == nullptr) {
3040 return false;
3041 }
3042 return convertStyle->MatchOneAttribute(static_cast<StyleType>(textStyleType), *convertComparedStyle);
3043 }
3044
OH_Drawing_TextStyleSetPlaceholder(OH_Drawing_TextStyle * style)3045 void OH_Drawing_TextStyleSetPlaceholder(OH_Drawing_TextStyle* style)
3046 {
3047 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
3048 if (textStyle == nullptr) {
3049 return;
3050 }
3051 textStyle->isPlaceholder = true;
3052 }
3053
OH_Drawing_TextStyleIsPlaceholder(OH_Drawing_TextStyle * style)3054 bool OH_Drawing_TextStyleIsPlaceholder(OH_Drawing_TextStyle* style)
3055 {
3056 TextStyle* textStyle = ConvertToOriginalText<TextStyle>(style);
3057 if (textStyle == nullptr) {
3058 return false;
3059 }
3060 return textStyle->isPlaceholder;
3061 }
3062
OH_Drawing_TypographyStyleGetEffectiveAlignment(OH_Drawing_TypographyStyle * style)3063 OH_Drawing_TextAlign OH_Drawing_TypographyStyleGetEffectiveAlignment(OH_Drawing_TypographyStyle* style)
3064 {
3065 TypographyStyle* typographyStyle = ConvertToOriginalText<TypographyStyle>(style);
3066 if (typographyStyle == nullptr) {
3067 return TEXT_ALIGN_START;
3068 }
3069 return static_cast<OH_Drawing_TextAlign>(typographyStyle->GetEffectiveAlign());
3070 }
3071
OH_Drawing_TypographyStyleIsHintEnabled(OH_Drawing_TypographyStyle * style)3072 bool OH_Drawing_TypographyStyleIsHintEnabled(OH_Drawing_TypographyStyle* style)
3073 {
3074 TypographyStyle* typographyStyle = ConvertToOriginalText<TypographyStyle>(style);
3075 if (typographyStyle == nullptr) {
3076 return false;
3077 }
3078 return typographyStyle->hintingIsOn;
3079 }
3080
OH_Drawing_SetTypographyStyleTextStrutStyle(OH_Drawing_TypographyStyle * style,OH_Drawing_StrutStyle * strutstyle)3081 void OH_Drawing_SetTypographyStyleTextStrutStyle(OH_Drawing_TypographyStyle* style, OH_Drawing_StrutStyle* strutstyle)
3082 {
3083 if (style == nullptr || strutstyle == nullptr) {
3084 return;
3085 }
3086 OH_Drawing_SetTypographyTextLineStyleFontWeight(style, strutstyle->weight);
3087 OH_Drawing_SetTypographyTextLineStyleFontStyle(style, strutstyle->style);
3088 OH_Drawing_SetTypographyTextLineStyleFontFamilies(
3089 style, strutstyle->familiesSize, const_cast<const char**>(strutstyle->families));
3090 OH_Drawing_SetTypographyTextLineStyleFontSize(style, strutstyle->size);
3091 OH_Drawing_SetTypographyTextLineStyleFontHeight(style, strutstyle->heightScale);
3092 OH_Drawing_SetTypographyTextLineStyleHalfLeading(style, strutstyle->halfLeading);
3093 OH_Drawing_SetTypographyTextLineStyleSpacingScale(style, strutstyle->leading);
3094 OH_Drawing_SetTypographyTextLineStyleOnly(style, strutstyle->forceStrutHeight);
3095 }
3096
OH_Drawing_TypographyStyleDestroyStrutStyle(OH_Drawing_StrutStyle * strutstyle)3097 void OH_Drawing_TypographyStyleDestroyStrutStyle(OH_Drawing_StrutStyle* strutstyle)
3098 {
3099 if (strutstyle == nullptr) {
3100 return;
3101 }
3102 if (strutstyle->familiesSize == 0 || strutstyle->families == nullptr) {
3103 delete strutstyle;
3104 strutstyle = nullptr;
3105 return;
3106 }
3107 for (size_t i = 0; i < strutstyle->familiesSize; i++) {
3108 if (strutstyle->families[i] != nullptr) {
3109 delete[] strutstyle->families[i];
3110 }
3111 }
3112 delete[] strutstyle->families;
3113 delete strutstyle;
3114 strutstyle = nullptr;
3115 }
3116
OH_Drawing_TypographyStyleGetStrutStyle(OH_Drawing_TypographyStyle * style)3117 OH_Drawing_StrutStyle* OH_Drawing_TypographyStyleGetStrutStyle(OH_Drawing_TypographyStyle* style)
3118 {
3119 TypographyStyle* typographyStyle = ConvertToOriginalText<TypographyStyle>(style);
3120 if (typographyStyle == nullptr) {
3121 return nullptr;
3122 }
3123 OH_Drawing_StrutStyle* strutstyle = new (std::nothrow) OH_Drawing_StrutStyle();
3124 if (strutstyle == nullptr) {
3125 return nullptr;
3126 }
3127 strutstyle->weight = (OH_Drawing_FontWeight)(typographyStyle->lineStyleFontWeight);
3128 strutstyle->style = (OH_Drawing_FontStyle)(typographyStyle->lineStyleFontStyle);
3129 strutstyle->size = typographyStyle->lineStyleFontSize;
3130 strutstyle->heightScale = typographyStyle->lineStyleHeightScale;
3131 strutstyle->heightOverride = typographyStyle->lineStyleHeightOnly;
3132 strutstyle->halfLeading = typographyStyle->lineStyleHalfLeading;
3133 strutstyle->leading = typographyStyle->lineStyleSpacingScale;
3134 strutstyle->forceStrutHeight = typographyStyle->lineStyleOnly;
3135 strutstyle->familiesSize = typographyStyle->lineStyleFontFamilies.size();
3136 if (strutstyle->familiesSize == 0) {
3137 strutstyle->families = nullptr;
3138 return strutstyle;
3139 }
3140 strutstyle->families = new (std::nothrow) char* [strutstyle->familiesSize];
3141 if (strutstyle->families == nullptr) {
3142 delete strutstyle;
3143 return nullptr;
3144 }
3145 for (size_t i = 0; i < strutstyle->familiesSize; i++) {
3146 int size = static_cast<int>(typographyStyle->lineStyleFontFamilies[i].size()) + 1;
3147 strutstyle->families[i] = new (std::nothrow) char[size];
3148 if (!strutstyle->families[i]) {
3149 for (size_t j = 0; j < i; j++) {
3150 delete[] strutstyle->families[j];
3151 }
3152 delete[] strutstyle->families;
3153 delete strutstyle;
3154 return nullptr;
3155 }
3156 if (strcpy_s(strutstyle->families[i], size, typographyStyle->lineStyleFontFamilies[i].c_str()) != 0) {
3157 for (size_t j = 0; j <= i; j++) {
3158 delete[] strutstyle->families[j];
3159 }
3160 delete[] strutstyle->families;
3161 delete strutstyle;
3162 return nullptr;
3163 }
3164 }
3165 return strutstyle;
3166 }
3167
OH_Drawing_TypographyStyleStrutStyleEquals(OH_Drawing_StrutStyle * from,OH_Drawing_StrutStyle * to)3168 bool OH_Drawing_TypographyStyleStrutStyleEquals(OH_Drawing_StrutStyle* from, OH_Drawing_StrutStyle* to)
3169 {
3170 if (from == nullptr || to == nullptr) {
3171 return false;
3172 }
3173 if (from->weight == to->weight && from->style == to->style && from->size == to->size &&
3174 from->heightScale == to->heightScale && from->heightOverride == to->heightOverride &&
3175 from->halfLeading == to->halfLeading && from->leading == to->leading &&
3176 from->forceStrutHeight == to->forceStrutHeight && from->familiesSize == to->familiesSize) {
3177 for (size_t i = 0; i < from->familiesSize; i++) {
3178 if (strcmp(from->families[i], to->families[i]) != 0) {
3179 return false;
3180 }
3181 }
3182 return true;
3183 }
3184 return false;
3185 }
3186
OH_Drawing_TypographyStyleSetHintsEnabled(OH_Drawing_TypographyStyle * style,bool hintsEnabled)3187 void OH_Drawing_TypographyStyleSetHintsEnabled(OH_Drawing_TypographyStyle* style, bool hintsEnabled)
3188 {
3189 TypographyStyle* typographyStyle = ConvertToOriginalText<TypographyStyle>(style);
3190 if (typographyStyle == nullptr) {
3191 return;
3192 }
3193 typographyStyle->hintingIsOn = hintsEnabled;
3194 }
3195
OH_Drawing_TypographyGetLineFontMetrics(OH_Drawing_Typography * typography,size_t lineNumber,size_t * fontMetricsSize)3196 OH_Drawing_Font_Metrics* OH_Drawing_TypographyGetLineFontMetrics(
3197 OH_Drawing_Typography* typography, size_t lineNumber, size_t* fontMetricsSize)
3198 {
3199 if (!typography || !fontMetricsSize || !lineNumber) {
3200 return nullptr;
3201 }
3202
3203 auto txtSKTypograph = ConvertToOriginalText<Typography>(typography);
3204 std::vector<Drawing::FontMetrics> grabFontMetrics;
3205 if (!txtSKTypograph->GetLineFontMetrics(lineNumber, *fontMetricsSize, grabFontMetrics)) {
3206 return nullptr;
3207 }
3208
3209 OH_Drawing_Font_Metrics* fontMetrics = new (std::nothrow) OH_Drawing_Font_Metrics[grabFontMetrics.size()];
3210 if (fontMetrics == nullptr || !grabFontMetrics.size()) {
3211 if (fontMetrics != nullptr) {
3212 delete[] fontMetrics;
3213 }
3214 return nullptr;
3215 }
3216
3217 for (size_t further = 0; further < grabFontMetrics.size(); further++) {
3218 ConvertFontMetrics(grabFontMetrics[further], fontMetrics[further]);
3219 }
3220 return fontMetrics;
3221 }
3222
OH_Drawing_TypographyDestroyLineFontMetrics(OH_Drawing_Font_Metrics * lineFontMetric)3223 void OH_Drawing_TypographyDestroyLineFontMetrics(OH_Drawing_Font_Metrics* lineFontMetric)
3224 {
3225 if (!lineFontMetric) {
3226 return;
3227 }
3228 delete[] lineFontMetric;
3229 lineFontMetric = nullptr;
3230 }
3231
GetFontStyle(OH_Drawing_FontStyle style)3232 static FontStyle GetFontStyle(OH_Drawing_FontStyle style)
3233 {
3234 FontStyle fontStyle;
3235 switch (style) {
3236 case FONT_STYLE_NORMAL: {
3237 fontStyle = FontStyle::NORMAL;
3238 break;
3239 }
3240 case FONT_STYLE_ITALIC:
3241 case FONT_STYLE_OBLIQUE: {
3242 fontStyle = FontStyle::ITALIC;
3243 break;
3244 }
3245 default: {
3246 fontStyle = FontStyle::NORMAL;
3247 }
3248 }
3249 return fontStyle;
3250 }
3251
GetFontWeight(OH_Drawing_FontWeight weight)3252 static FontWeight GetFontWeight(OH_Drawing_FontWeight weight)
3253 {
3254 FontWeight fontWeight;
3255 switch (weight) {
3256 case FONT_WEIGHT_100: {
3257 fontWeight = FontWeight::W100;
3258 break;
3259 }
3260 case FONT_WEIGHT_200: {
3261 fontWeight = FontWeight::W200;
3262 break;
3263 }
3264 case FONT_WEIGHT_300: {
3265 fontWeight = FontWeight::W300;
3266 break;
3267 }
3268 case FONT_WEIGHT_400: {
3269 fontWeight = FontWeight::W400;
3270 break;
3271 }
3272 case FONT_WEIGHT_500: {
3273 fontWeight = FontWeight::W500;
3274 break;
3275 }
3276 case FONT_WEIGHT_600: {
3277 fontWeight = FontWeight::W600;
3278 break;
3279 }
3280 case FONT_WEIGHT_700: {
3281 fontWeight = FontWeight::W700;
3282 break;
3283 }
3284 case FONT_WEIGHT_800: {
3285 fontWeight = FontWeight::W800;
3286 break;
3287 }
3288 case FONT_WEIGHT_900: {
3289 fontWeight = FontWeight::W900;
3290 break;
3291 }
3292 default: {
3293 fontWeight = FontWeight::W400;
3294 }
3295 }
3296 return fontWeight;
3297 }
3298
GetFontWidth(OH_Drawing_FontWidth width)3299 static FontWidth GetFontWidth(OH_Drawing_FontWidth width)
3300 {
3301 FontWidth fontWidth;
3302 switch (width) {
3303 case FONT_WIDTH_ULTRA_CONDENSED: {
3304 fontWidth = FontWidth::ULTRA_CONDENSED;
3305 break;
3306 }
3307 case FONT_WIDTH_EXTRA_CONDENSED: {
3308 fontWidth = FontWidth::EXTRA_CONDENSED;
3309 break;
3310 }
3311 case FONT_WIDTH_CONDENSED: {
3312 fontWidth = FontWidth::CONDENSED;
3313 break;
3314 }
3315 case FONT_WIDTH_SEMI_CONDENSED: {
3316 fontWidth = FontWidth::SEMI_CONDENSED;
3317 break;
3318 }
3319 case FONT_WIDTH_NORMAL: {
3320 fontWidth = FontWidth::NORMAL;
3321 break;
3322 }
3323 case FONT_WIDTH_SEMI_EXPANDED: {
3324 fontWidth = FontWidth::SEMI_EXPANDED;
3325 break;
3326 }
3327 case FONT_WIDTH_EXPANDED: {
3328 fontWidth = FontWidth::EXPANDED;
3329 break;
3330 }
3331 case FONT_WIDTH_EXTRA_EXPANDED: {
3332 fontWidth = FontWidth::EXTRA_EXPANDED;
3333 break;
3334 }
3335 case FONT_WIDTH_ULTRA_EXPANDED: {
3336 fontWidth = FontWidth::ULTRA_EXPANDED;
3337 break;
3338 }
3339 default: {
3340 fontWidth = FontWidth::NORMAL;
3341 }
3342 }
3343 return fontWidth;
3344 }
OH_Drawing_SetTextStyleFontStyleStruct(OH_Drawing_TextStyle * drawingTextStyle,OH_Drawing_FontStyleStruct fontStyle)3345 void OH_Drawing_SetTextStyleFontStyleStruct(
3346 OH_Drawing_TextStyle* drawingTextStyle, OH_Drawing_FontStyleStruct fontStyle)
3347 {
3348 TextStyle* style = ConvertToOriginalText<TextStyle>(drawingTextStyle);
3349 if (style == nullptr) {
3350 return;
3351 }
3352 style->fontWeight = GetFontWeight(fontStyle.weight);
3353 style->fontWidth = GetFontWidth(fontStyle.width);
3354 style->fontStyle = GetFontStyle(fontStyle.slant);
3355 }
3356
OH_Drawing_TextStyleGetFontStyleStruct(OH_Drawing_TextStyle * drawingTextStyle)3357 OH_Drawing_FontStyleStruct OH_Drawing_TextStyleGetFontStyleStruct(OH_Drawing_TextStyle* drawingTextStyle)
3358 {
3359 OH_Drawing_FontStyleStruct fontStyle;
3360 TextStyle* style = ConvertToOriginalText<TextStyle>(drawingTextStyle);
3361 if (style == nullptr) {
3362 fontStyle.weight = FONT_WEIGHT_400;
3363 fontStyle.width = FONT_WIDTH_NORMAL;
3364 fontStyle.slant = FONT_STYLE_NORMAL;
3365 return fontStyle;
3366 }
3367 fontStyle.weight = static_cast<OH_Drawing_FontWeight>(style->fontWeight);
3368 fontStyle.width = static_cast<OH_Drawing_FontWidth>(style->fontWidth);
3369 fontStyle.slant = static_cast<OH_Drawing_FontStyle>(style->fontStyle);
3370 return fontStyle;
3371 }
3372
OH_Drawing_SetTypographyStyleFontStyleStruct(OH_Drawing_TypographyStyle * drawingStyle,OH_Drawing_FontStyleStruct fontStyle)3373 void OH_Drawing_SetTypographyStyleFontStyleStruct(
3374 OH_Drawing_TypographyStyle* drawingStyle, OH_Drawing_FontStyleStruct fontStyle)
3375 {
3376 TypographyStyle* style = ConvertToOriginalText<TypographyStyle>(drawingStyle);
3377 if (style == nullptr) {
3378 return;
3379 }
3380 style->fontWeight = GetFontWeight(fontStyle.weight);
3381 style->fontWidth = GetFontWidth(fontStyle.width);
3382 style->fontStyle = GetFontStyle(fontStyle.slant);
3383 }
3384
OH_Drawing_TypographyStyleGetFontStyleStruct(OH_Drawing_TypographyStyle * drawingStyle)3385 OH_Drawing_FontStyleStruct OH_Drawing_TypographyStyleGetFontStyleStruct(OH_Drawing_TypographyStyle* drawingStyle)
3386 {
3387 OH_Drawing_FontStyleStruct fontStyle;
3388 TypographyStyle* style = ConvertToOriginalText<TypographyStyle>(drawingStyle);
3389 if (style == nullptr) {
3390 fontStyle.weight = FONT_WEIGHT_400;
3391 fontStyle.width = FONT_WIDTH_NORMAL;
3392 fontStyle.slant = FONT_STYLE_NORMAL;
3393 return fontStyle;
3394 }
3395 fontStyle.weight = static_cast<OH_Drawing_FontWeight>(style->fontWeight);
3396 fontStyle.width = static_cast<OH_Drawing_FontWidth>(style->fontWidth);
3397 fontStyle.slant = static_cast<OH_Drawing_FontStyle>(style->fontStyle);
3398 return fontStyle;
3399 }
3400
OH_Drawing_TypographyDestroyTextBox(OH_Drawing_TextBox * textBox)3401 void OH_Drawing_TypographyDestroyTextBox(OH_Drawing_TextBox* textBox)
3402 {
3403 if (!textBox) {
3404 return;
3405 }
3406
3407 std::vector<TextRect>* textRectArr = ConvertToOriginalText<std::vector<TextRect>>(textBox);
3408
3409 if (!textRectArr) {
3410 return;
3411 }
3412 delete textRectArr;
3413 textRectArr = nullptr;
3414 }
3415
OH_Drawing_TextStyleAddFontVariation(OH_Drawing_TextStyle * style,const char * axis,const float value)3416 void OH_Drawing_TextStyleAddFontVariation(OH_Drawing_TextStyle* style, const char* axis, const float value)
3417 {
3418 if (style == nullptr || axis == nullptr) {
3419 return;
3420 }
3421 TextStyle* convertStyle = ConvertToOriginalText<TextStyle>(style);
3422 if (convertStyle) {
3423 convertStyle->fontVariations.SetAxisValue(axis, value);
3424 }
3425 }
3426
OH_Drawing_CreateTextTab(OH_Drawing_TextAlign alignment,float location)3427 OH_Drawing_TextTab* OH_Drawing_CreateTextTab(OH_Drawing_TextAlign alignment, float location)
3428 {
3429 TextAlign textAlign;
3430 switch (alignment) {
3431 case TEXT_ALIGN_LEFT: {
3432 textAlign = TextAlign::LEFT;
3433 break;
3434 }
3435 case TEXT_ALIGN_RIGHT: {
3436 textAlign = TextAlign::RIGHT;
3437 break;
3438 }
3439 case TEXT_ALIGN_CENTER: {
3440 textAlign = TextAlign::CENTER;
3441 break;
3442 }
3443 default: {
3444 textAlign = TextAlign::LEFT;
3445 break;
3446 }
3447 }
3448 return (OH_Drawing_TextTab*)new TextTab(textAlign, location);
3449 }
3450
OH_Drawing_DestroyTextTab(OH_Drawing_TextTab * tab)3451 void OH_Drawing_DestroyTextTab(OH_Drawing_TextTab* tab)
3452 {
3453 delete ConvertToOriginalText<TextTab>(tab);
3454 }
3455
OH_Drawing_SetTypographyTextTab(OH_Drawing_TypographyStyle * style,OH_Drawing_TextTab * tab)3456 void OH_Drawing_SetTypographyTextTab(OH_Drawing_TypographyStyle* style, OH_Drawing_TextTab* tab)
3457 {
3458 if (!style || !tab) {
3459 return;
3460 }
3461 auto rosenTextTab = ConvertToOriginalText<OHOS::Rosen::TextTab>(tab);
3462 ConvertToOriginalText<TypographyStyle>(style)->tab = *rosenTextTab;
3463 }
3464
OH_Drawing_GetTextTabAlignment(OH_Drawing_TextTab * tab)3465 OH_Drawing_TextAlign OH_Drawing_GetTextTabAlignment(OH_Drawing_TextTab* tab)
3466 {
3467 if (tab == nullptr) {
3468 return TEXT_ALIGN_LEFT;
3469 }
3470 return static_cast<OH_Drawing_TextAlign>(ConvertToOriginalText<TextTab>(tab)->alignment);
3471 }
3472
OH_Drawing_GetTextTabLocation(OH_Drawing_TextTab * tab)3473 float OH_Drawing_GetTextTabLocation(OH_Drawing_TextTab* tab)
3474 {
3475 if (tab == nullptr) {
3476 return 0.0;
3477 }
3478 return ConvertToOriginalText<TextTab>(tab)->location;
3479 }
3480
OH_Drawing_GetDrawingArraySize(OH_Drawing_Array * drawingArray)3481 size_t OH_Drawing_GetDrawingArraySize(OH_Drawing_Array* drawingArray)
3482 {
3483 if (drawingArray == nullptr) {
3484 return 0;
3485 }
3486
3487 ObjectArray* array = ConvertToOriginalText<ObjectArray>(drawingArray);
3488 if (array == nullptr) {
3489 return 0;
3490 }
3491
3492 return array->num;
3493 }