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