• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "c/drawing_text_typography.h"
17 
18 #include "rosen_text/ui/font_collection.h"
19 #include "rosen_text/ui/typography.h"
20 #include "rosen_text/ui/typography_create.h"
21 
22 #include <codecvt>
23 #include <locale>
24 #include <vector>
25 #include <string>
26 
27 using namespace rosen;
28 
29 template<typename T1, typename T2>
ConvertToOriginalText(T2 * ptr)30 inline T1* ConvertToOriginalText(T2* ptr)
31 {
32     return reinterpret_cast<T1*>(ptr);
33 }
34 
35 template<typename T1, typename T2>
ConvertToNDKText(T2 * ptr)36 inline T1* ConvertToNDKText(T2* ptr)
37 {
38     return reinterpret_cast<T1*>(ptr);
39 }
40 
OH_Drawing_CreateTypographyStyle(void)41 OH_Drawing_TypographyStyle* OH_Drawing_CreateTypographyStyle(void)
42 {
43     return (OH_Drawing_TypographyStyle*)new TypographyStyle;
44 }
45 
OH_Drawing_DestroyTypographyStyle(OH_Drawing_TypographyStyle * style)46 void OH_Drawing_DestroyTypographyStyle(OH_Drawing_TypographyStyle* style)
47 {
48     delete ConvertToOriginalText<TypographyStyle>(style);
49 }
50 
OH_Drawing_SetTypographyTextDirection(OH_Drawing_TypographyStyle * style,int direction)51 void OH_Drawing_SetTypographyTextDirection(OH_Drawing_TypographyStyle* style, int direction)
52 {
53     TextDirection textDirection;
54     switch (direction) {
55         case TEXT_DIRECTION_RTL: {
56             textDirection = TextDirection::RTL;
57             break;
58         }
59         case TEXT_DIRECTION_LTR: {
60             textDirection = TextDirection::LTR;
61             break;
62         }
63         default: {
64             textDirection = TextDirection::LTR;
65             break;
66         }
67     }
68     ConvertToOriginalText<TypographyStyle>(style)->textDirection_ = textDirection;
69 }
70 
OH_Drawing_SetTypographyTextAlign(OH_Drawing_TypographyStyle * style,int align)71 void OH_Drawing_SetTypographyTextAlign(OH_Drawing_TypographyStyle* style, int align)
72 {
73     TextAlign textAlign;
74     switch (align) {
75         case TEXT_ALIGN_LEFT: {
76             textAlign = TextAlign::LEFT;
77             break;
78         }
79         case TEXT_ALIGN_RIGHT: {
80             textAlign = TextAlign::RIGHT;
81             break;
82         }
83         case TEXT_ALIGN_CENTER: {
84             textAlign = TextAlign::CENTER;
85             break;
86         }
87         case TEXT_ALIGN_JUSTIFY: {
88             textAlign = TextAlign::JUSTIFY;
89             break;
90         }
91         case TEXT_ALIGN_START: {
92             textAlign = TextAlign::START;
93             break;
94         }
95         case TEXT_ALIGN_END: {
96             textAlign = TextAlign::END;
97             break;
98         }
99         default: {
100             textAlign = TextAlign::LEFT;
101         }
102     }
103     ConvertToOriginalText<TypographyStyle>(style)->textAlign_ = textAlign;
104 }
105 
OH_Drawing_SetTypographyTextMaxLines(OH_Drawing_TypographyStyle * style,int lineNumber)106 void OH_Drawing_SetTypographyTextMaxLines(OH_Drawing_TypographyStyle* style, int lineNumber)
107 {
108     ConvertToOriginalText<TypographyStyle>(style)->maxLines_ = static_cast<size_t>(lineNumber);
109 }
110 
OH_Drawing_CreateTextStyle(void)111 OH_Drawing_TextStyle* OH_Drawing_CreateTextStyle(void)
112 {
113     return (OH_Drawing_TextStyle*)new TextStyle;
114 }
115 
OH_Drawing_DestroyTextStyle(OH_Drawing_TextStyle * style)116 void OH_Drawing_DestroyTextStyle(OH_Drawing_TextStyle* style)
117 {
118     delete ConvertToOriginalText<TextStyle>(style);
119 }
120 
OH_Drawing_SetTextStyleColor(OH_Drawing_TextStyle * style,uint32_t color)121 void OH_Drawing_SetTextStyleColor(OH_Drawing_TextStyle* style, uint32_t color)
122 {
123     ConvertToOriginalText<TextStyle>(style)->color_.SetColorQuad(color);
124 }
125 
OH_Drawing_SetTextStyleFontSize(OH_Drawing_TextStyle * style,double fontSize)126 void OH_Drawing_SetTextStyleFontSize(OH_Drawing_TextStyle* style, double fontSize)
127 {
128     ConvertToOriginalText<TextStyle>(style)->fontSize_ = fontSize;
129 }
130 
OH_Drawing_SetTextStyleFontWeight(OH_Drawing_TextStyle * style,int fontWeight)131 void OH_Drawing_SetTextStyleFontWeight(OH_Drawing_TextStyle* style, int fontWeight)
132 {
133     FontWeight rosenFontWeight;
134     switch (fontWeight) {
135         case FONT_WEIGHT_100: {
136             rosenFontWeight = FontWeight::W100;
137             break;
138         }
139         case FONT_WEIGHT_200: {
140             rosenFontWeight = FontWeight::W200;
141             break;
142         }
143         case FONT_WEIGHT_300: {
144             rosenFontWeight = FontWeight::W300;
145             break;
146         }
147         case FONT_WEIGHT_400: {
148             rosenFontWeight = FontWeight::W400;
149             break;
150         }
151         case FONT_WEIGHT_500: {
152             rosenFontWeight = FontWeight::W500;
153             break;
154         }
155         case FONT_WEIGHT_600: {
156             rosenFontWeight = FontWeight::W600;
157             break;
158         }
159         case FONT_WEIGHT_700: {
160             rosenFontWeight = FontWeight::W700;
161             break;
162         }
163         case FONT_WEIGHT_800: {
164             rosenFontWeight = FontWeight::W800;
165             break;
166         }
167         case FONT_WEIGHT_900: {
168             rosenFontWeight = FontWeight::W900;
169             break;
170         }
171         default: {
172             rosenFontWeight = FontWeight::W400;
173         }
174     }
175     ConvertToOriginalText<TextStyle>(style)->fontWeight_ = rosenFontWeight;
176 }
177 
OH_Drawing_SetTextStyleBaseLine(OH_Drawing_TextStyle * style,int baseline)178 void OH_Drawing_SetTextStyleBaseLine(OH_Drawing_TextStyle* style, int baseline)
179 {
180     TextBaseline rosenBaseLine;
181     switch (baseline) {
182         case TEXT_BASELINE_ALPHABETIC: {
183             rosenBaseLine = TextBaseline::ALPHABETIC;
184             break;
185         }
186         case TEXT_BASELINE_IDEOGRAPHIC: {
187             rosenBaseLine = TextBaseline::IDEOGRAPHIC;
188             break;
189         }
190         default: {
191             rosenBaseLine = TextBaseline::ALPHABETIC;
192         }
193     }
194     ConvertToOriginalText<TextStyle>(style)->textBaseline_ = rosenBaseLine;
195 }
196 
OH_Drawing_SetTextStyleDecoration(OH_Drawing_TextStyle * style,int decoration)197 void OH_Drawing_SetTextStyleDecoration(OH_Drawing_TextStyle* style, int decoration)
198 {
199     TextDecoration rosenDecoration;
200     switch (decoration) {
201         case TEXT_DECORATION_NONE: {
202             rosenDecoration = TextDecoration::NONE;
203             break;
204         }
205         case TEXT_DECORATION_UNDERLINE: {
206             rosenDecoration = TextDecoration::UNDERLINE;
207             break;
208         }
209         case TEXT_DECORATION_OVERLINE: {
210             rosenDecoration = TextDecoration::OVERLINE;
211             break;
212         }
213         case TEXT_DECORATION_LINE_THROUGH: {
214             rosenDecoration = TextDecoration::LINETHROUGH;
215             break;
216         }
217         default: {
218             rosenDecoration = TextDecoration::NONE;
219         }
220     }
221     ConvertToOriginalText<TextStyle>(style)->decoration_ = rosenDecoration;
222 }
223 
OH_Drawing_SetTextStyleDecorationColor(OH_Drawing_TextStyle * style,uint32_t color)224 void OH_Drawing_SetTextStyleDecorationColor(OH_Drawing_TextStyle* style, uint32_t color)
225 {
226     ConvertToOriginalText<TextStyle>(style)->decorationColor_.SetColorQuad(color);
227 }
228 
OH_Drawing_SetTextStyleFontHeight(OH_Drawing_TextStyle * style,double fontHeight)229 void OH_Drawing_SetTextStyleFontHeight(OH_Drawing_TextStyle* style, double fontHeight)
230 {
231     ConvertToOriginalText<TextStyle>(style)->height_ = fontHeight;
232 }
233 
OH_Drawing_SetTextStyleFontFamilies(OH_Drawing_TextStyle * style,int fontFamiliesNumber,const char * fontFamilies[])234 void OH_Drawing_SetTextStyleFontFamilies(OH_Drawing_TextStyle* style,
235     int fontFamiliesNumber, const char* fontFamilies[])
236 {
237     std::vector<std::string> rosenFontFamilies;
238     for (int i = 0; i < fontFamiliesNumber; i++) {
239         rosenFontFamilies.emplace_back(fontFamilies[i]);
240     }
241     ConvertToOriginalText<TextStyle>(style)->fontFamilies_ = rosenFontFamilies;
242 }
243 
OH_Drawing_SetTextStyleFontStyle(OH_Drawing_TextStyle * style,int fontStyle)244 void OH_Drawing_SetTextStyleFontStyle(OH_Drawing_TextStyle* style, int fontStyle)
245 {
246     FontStyle rosenFontStyle;
247     switch (fontStyle) {
248         case FONT_STYLE_NORMAL: {
249             rosenFontStyle = FontStyle::NORMAL;
250             break;
251         }
252         case FONT_STYLE_ITALIC: {
253             rosenFontStyle = FontStyle::ITALIC;
254             break;
255         }
256         default: {
257             rosenFontStyle = FontStyle::NORMAL;
258         }
259     }
260     ConvertToOriginalText<TextStyle>(style)->fontStyle_ = rosenFontStyle;
261 }
262 
OH_Drawing_SetTextStyleLocale(OH_Drawing_TextStyle * style,const char * locale)263 void OH_Drawing_SetTextStyleLocale(OH_Drawing_TextStyle* style, const char* locale)
264 {
265     ConvertToOriginalText<TextStyle>(style)->locale_ = locale;
266 }
267 
OH_Drawing_CreateTypographyHandler(OH_Drawing_TypographyStyle * style,OH_Drawing_FontCollection * fontCollection)268 OH_Drawing_TypographyCreate* OH_Drawing_CreateTypographyHandler(OH_Drawing_TypographyStyle* style,
269     OH_Drawing_FontCollection* fontCollection)
270 {
271     const TypographyStyle* typoStyle = ConvertToOriginalText<TypographyStyle>(style);
272     std::unique_ptr<TypographyCreate> handler = TypographyCreate::CreateRosenBuilder(*typoStyle,
273         std::shared_ptr<FontCollection>(ConvertToOriginalText<FontCollection>(fontCollection)));
274     return ConvertToNDKText<OH_Drawing_TypographyCreate>(handler.release());
275 }
276 
OH_Drawing_DestroyTypographyHandler(OH_Drawing_TypographyCreate * handler)277 void OH_Drawing_DestroyTypographyHandler(OH_Drawing_TypographyCreate* handler)
278 {
279     delete ConvertToOriginalText<TypographyCreate>(handler);
280 }
281 
OH_Drawing_TypographyHandlerPushTextStyle(OH_Drawing_TypographyCreate * handler,OH_Drawing_TextStyle * style)282 void OH_Drawing_TypographyHandlerPushTextStyle(OH_Drawing_TypographyCreate* handler, OH_Drawing_TextStyle* style)
283 {
284     const TextStyle* rosenTextStyle = ConvertToOriginalText<TextStyle>(style);
285     ConvertToOriginalText<TypographyCreate>(handler)->PushStyle(*rosenTextStyle);
286 }
287 
OH_Drawing_TypographyHandlerAddText(OH_Drawing_TypographyCreate * handler,const char * text)288 void OH_Drawing_TypographyHandlerAddText(OH_Drawing_TypographyCreate* handler, const char* text)
289 {
290     const std::u16string wideText =
291         std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(text);
292     ConvertToOriginalText<TypographyCreate>(handler)->AddText(wideText);
293 }
294 
OH_Drawing_TypographyHandlerPopTextStyle(OH_Drawing_TypographyCreate * handler)295 void OH_Drawing_TypographyHandlerPopTextStyle(OH_Drawing_TypographyCreate* handler)
296 {
297     ConvertToOriginalText<TypographyCreate>(handler)->Pop();
298 }
299 
OH_Drawing_CreateTypography(OH_Drawing_TypographyCreate * handler)300 OH_Drawing_Typography* OH_Drawing_CreateTypography(OH_Drawing_TypographyCreate* handler)
301 {
302     TypographyCreate* rosenHandler = ConvertToOriginalText<TypographyCreate>(handler);
303     std::unique_ptr<Typography> typography = rosenHandler->Build();
304     return ConvertToNDKText<OH_Drawing_Typography>(typography.release());
305 }
306 
OH_Drawing_DestroyTypography(OH_Drawing_Typography * typography)307 void OH_Drawing_DestroyTypography(OH_Drawing_Typography* typography)
308 {
309     delete ConvertToOriginalText<Typography>(typography);
310 }
311 
OH_Drawing_TypographyLayout(OH_Drawing_Typography * typography,double maxWidth)312 void OH_Drawing_TypographyLayout(OH_Drawing_Typography* typography, double maxWidth)
313 {
314     ConvertToOriginalText<Typography>(typography)->Layout(maxWidth);
315 }
316 
OH_Drawing_TypographyPaint(OH_Drawing_Typography * typography,OH_Drawing_Canvas * canvas,double potisionX,double potisionY)317 void OH_Drawing_TypographyPaint(OH_Drawing_Typography* typography, OH_Drawing_Canvas* canvas,
318     double potisionX, double potisionY)
319 {
320     ConvertToOriginalText<Typography>(typography)->Paint(reinterpret_cast<OHOS::Rosen::Drawing::Canvas*>(canvas),
321         potisionX, potisionY);
322 }
323 
OH_Drawing_TypographyGetMaxWidth(OH_Drawing_Typography * typography)324 double OH_Drawing_TypographyGetMaxWidth(OH_Drawing_Typography* typography)
325 {
326     return ConvertToOriginalText<Typography>(typography)->GetMaxWidth();
327 }
328 
OH_Drawing_TypographyGetHeight(OH_Drawing_Typography * typography)329 double OH_Drawing_TypographyGetHeight(OH_Drawing_Typography* typography)
330 {
331     return ConvertToOriginalText<Typography>(typography)->GetHeight();
332 }
333 
OH_Drawing_TypographyGetLongestLine(OH_Drawing_Typography * typography)334 double OH_Drawing_TypographyGetLongestLine(OH_Drawing_Typography* typography)
335 {
336     return ConvertToOriginalText<Typography>(typography)->GetLongestLine();
337 }
338 
OH_Drawing_TypographyGetMinIntrinsicWidth(OH_Drawing_Typography * typography)339 double OH_Drawing_TypographyGetMinIntrinsicWidth(OH_Drawing_Typography* typography)
340 {
341     return ConvertToOriginalText<Typography>(typography)->GetMinIntrinsicWidth();
342 }
343 
OH_Drawing_TypographyGetMaxIntrinsicWidth(OH_Drawing_Typography * typography)344 double OH_Drawing_TypographyGetMaxIntrinsicWidth(OH_Drawing_Typography* typography)
345 {
346     return ConvertToOriginalText<Typography>(typography)->GetMaxIntrinsicWidth();
347 }
348 
OH_Drawing_TypographyGetAlphabeticBaseline(OH_Drawing_Typography * typography)349 double OH_Drawing_TypographyGetAlphabeticBaseline(OH_Drawing_Typography* typography)
350 {
351     return ConvertToOriginalText<Typography>(typography)->GetAlphabeticBaseline();
352 }
353 
OH_Drawing_TypographyGetIdeographicBaseline(OH_Drawing_Typography * typography)354 double OH_Drawing_TypographyGetIdeographicBaseline(OH_Drawing_Typography* typography)
355 {
356     return ConvertToOriginalText<Typography>(typography)->GetIdeographicBaseline();
357 }
358