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