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