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 <vector>
24 #include <string>
25
26 using namespace rosen;
27
ConvertToOriginalText(OH_Drawing_FontCollection * fontCollection)28 static FontCollection* ConvertToOriginalText(OH_Drawing_FontCollection* fontCollection)
29 {
30 return reinterpret_cast<FontCollection*>(fontCollection);
31 }
32
ConvertToOriginalText(OH_Drawing_TypographyStyle * style)33 static TypographyStyle* ConvertToOriginalText(OH_Drawing_TypographyStyle* style)
34 {
35 return reinterpret_cast<TypographyStyle*>(style);
36 }
37
ConvertToOriginalText(OH_Drawing_TypographyCreate * handler)38 static TypographyCreate* ConvertToOriginalText(OH_Drawing_TypographyCreate* handler)
39 {
40 return reinterpret_cast<TypographyCreate*>(handler);
41 }
42
ConvertToNDKText(TypographyCreate * handler)43 static OH_Drawing_TypographyCreate* ConvertToNDKText(TypographyCreate* handler)
44 {
45 return reinterpret_cast<OH_Drawing_TypographyCreate*>(handler);
46 }
47
ConvertToOriginalText(OH_Drawing_TextStyle * style)48 static TextStyle* ConvertToOriginalText(OH_Drawing_TextStyle* style)
49 {
50 return reinterpret_cast<TextStyle*>(style);
51 }
52
ConvertToOriginalText(OH_Drawing_Typography * typography)53 static Typography* ConvertToOriginalText(OH_Drawing_Typography* typography)
54 {
55 return reinterpret_cast<Typography*>(typography);
56 }
57
ConvertToNDKText(Typography * typography)58 static OH_Drawing_Typography* ConvertToNDKText(Typography* typography)
59 {
60 return reinterpret_cast<OH_Drawing_Typography*>(typography);
61 }
62
OH_Drawing_CreateTypographyStyle(void)63 OH_Drawing_TypographyStyle* OH_Drawing_CreateTypographyStyle(void)
64 {
65 return (OH_Drawing_TypographyStyle*)new TypographyStyle;
66 }
67
OH_Drawing_DestroyTypographyStyle(OH_Drawing_TypographyStyle * style)68 void OH_Drawing_DestroyTypographyStyle(OH_Drawing_TypographyStyle* style)
69 {
70 delete ConvertToOriginalText(style);
71 }
72
OH_Drawing_SetTypographyTextDirection(OH_Drawing_TypographyStyle * style,int direction)73 void OH_Drawing_SetTypographyTextDirection(OH_Drawing_TypographyStyle* style, int direction)
74 {
75 TextDirection textDirection;
76 switch (direction) {
77 case TEXT_DIRECTION_RTL: {
78 textDirection = TextDirection::RTL;
79 break;
80 }
81 case TEXT_DIRECTION_LTR: {
82 textDirection = TextDirection::LTR;
83 break;
84 }
85 default: {
86 textDirection = TextDirection::LTR;
87 break;
88 }
89 }
90 ConvertToOriginalText(style)->textDirection_ = textDirection;
91 }
92
OH_Drawing_SetTypographyTextAlign(OH_Drawing_TypographyStyle * style,int align)93 void OH_Drawing_SetTypographyTextAlign(OH_Drawing_TypographyStyle* style, int align)
94 {
95 TextAlign textAlign;
96 switch (align) {
97 case TEXT_ALIGN_LEFT: {
98 textAlign = TextAlign::LEFT;
99 break;
100 }
101 case TEXT_ALIGN_RIGHT: {
102 textAlign = TextAlign::RIGHT;
103 break;
104 }
105 case TEXT_ALIGN_CENTER: {
106 textAlign = TextAlign::CENTER;
107 break;
108 }
109 case TEXT_ALIGN_JUSTIFY: {
110 textAlign = TextAlign::JUSTIFY;
111 break;
112 }
113 case TEXT_ALIGN_START: {
114 textAlign = TextAlign::START;
115 break;
116 }
117 case TEXT_ALIGN_END: {
118 textAlign = TextAlign::END;
119 break;
120 }
121 default: {
122 textAlign = TextAlign::LEFT;
123 }
124 }
125 ConvertToOriginalText(style)->textAlign_ = textAlign;
126 }
127
OH_Drawing_SetTypographyTextMaxLines(OH_Drawing_TypographyStyle * style,int lineNumber)128 void OH_Drawing_SetTypographyTextMaxLines(OH_Drawing_TypographyStyle* style, int lineNumber)
129 {
130 ConvertToOriginalText(style)->maxLines_ = static_cast<size_t>(lineNumber);
131 }
132
OH_Drawing_CreateTextStyle(void)133 OH_Drawing_TextStyle* OH_Drawing_CreateTextStyle(void)
134 {
135 return (OH_Drawing_TextStyle*)new TextStyle;
136 }
137
OH_Drawing_DestroyTextStyle(OH_Drawing_TextStyle * style)138 void OH_Drawing_DestroyTextStyle(OH_Drawing_TextStyle* style)
139 {
140 delete ConvertToOriginalText(style);
141 }
142
OH_Drawing_SetTextStyleColor(OH_Drawing_TextStyle * style,uint32_t color)143 void OH_Drawing_SetTextStyleColor(OH_Drawing_TextStyle* style, uint32_t color)
144 {
145 ConvertToOriginalText(style)->color_.SetColorQuad(color);
146 }
147
OH_Drawing_SetTextStyleFontSize(OH_Drawing_TextStyle * style,double fontSize)148 void OH_Drawing_SetTextStyleFontSize(OH_Drawing_TextStyle* style, double fontSize)
149 {
150 ConvertToOriginalText(style)->fontSize_ = fontSize;
151 }
152
OH_Drawing_SetTextStyleFontWeight(OH_Drawing_TextStyle * style,int fontWeight)153 void OH_Drawing_SetTextStyleFontWeight(OH_Drawing_TextStyle* style, int fontWeight)
154 {
155 FontWeight rosenFontWeight;
156 switch (fontWeight) {
157 case FONT_WEIGHT_100: {
158 rosenFontWeight = FontWeight::W100;
159 break;
160 }
161 case FONT_WEIGHT_200: {
162 rosenFontWeight = FontWeight::W200;
163 break;
164 }
165 case FONT_WEIGHT_300: {
166 rosenFontWeight = FontWeight::W300;
167 break;
168 }
169 case FONT_WEIGHT_400: {
170 rosenFontWeight = FontWeight::W400;
171 break;
172 }
173 case FONT_WEIGHT_500: {
174 rosenFontWeight = FontWeight::W500;
175 break;
176 }
177 case FONT_WEIGHT_600: {
178 rosenFontWeight = FontWeight::W600;
179 break;
180 }
181 case FONT_WEIGHT_700: {
182 rosenFontWeight = FontWeight::W700;
183 break;
184 }
185 case FONT_WEIGHT_800: {
186 rosenFontWeight = FontWeight::W800;
187 break;
188 }
189 case FONT_WEIGHT_900: {
190 rosenFontWeight = FontWeight::W900;
191 break;
192 }
193 default: {
194 rosenFontWeight = FontWeight::W400;
195 }
196 }
197 ConvertToOriginalText(style)->fontWeight_ = rosenFontWeight;
198 }
199
OH_Drawing_SetTextStyleBaseLine(OH_Drawing_TextStyle * style,int baseline)200 void OH_Drawing_SetTextStyleBaseLine(OH_Drawing_TextStyle* style, int baseline)
201 {
202 TextBaseline rosenBaseLine;
203 switch (baseline) {
204 case TEXT_BASELINE_ALPHABETIC: {
205 rosenBaseLine = TextBaseline::ALPHABETIC;
206 break;
207 }
208 case TEXT_BASELINE_IDEOGRAPHIC: {
209 rosenBaseLine = TextBaseline::IDEOGRAPHIC;
210 break;
211 }
212 default: {
213 rosenBaseLine = TextBaseline::ALPHABETIC;
214 }
215 }
216 ConvertToOriginalText(style)->textBaseline_ = rosenBaseLine;
217 }
218
OH_Drawing_SetTextStyleDecoration(OH_Drawing_TextStyle * style,int decoration)219 void OH_Drawing_SetTextStyleDecoration(OH_Drawing_TextStyle* style, int decoration)
220 {
221 TextDecoration rosenDecoration;
222 switch (decoration) {
223 case TEXT_DECORATION_NONE: {
224 rosenDecoration = TextDecoration::NONE;
225 break;
226 }
227 case TEXT_DECORATION_UNDERLINE: {
228 rosenDecoration = TextDecoration::UNDERLINE;
229 break;
230 }
231 case TEXT_DECORATION_OVERLINE: {
232 rosenDecoration = TextDecoration::OVERLINE;
233 break;
234 }
235 case TEXT_DECORATION_LINE_THROUGH: {
236 rosenDecoration = TextDecoration::LINETHROUGH;
237 break;
238 }
239 default: {
240 rosenDecoration = TextDecoration::NONE;
241 }
242 }
243 ConvertToOriginalText(style)->decoration_ = rosenDecoration;
244 }
245
OH_Drawing_SetTextStyleDecorationColor(OH_Drawing_TextStyle * style,uint32_t color)246 void OH_Drawing_SetTextStyleDecorationColor(OH_Drawing_TextStyle* style, uint32_t color)
247 {
248 ConvertToOriginalText(style)->decorationColor_.SetColorQuad(color);
249 }
250
OH_Drawing_SetTextStyleFontHeight(OH_Drawing_TextStyle * style,double fontHeight)251 void OH_Drawing_SetTextStyleFontHeight(OH_Drawing_TextStyle* style, double fontHeight)
252 {
253 ConvertToOriginalText(style)->height_ = fontHeight;
254 }
255
OH_Drawing_SetTextStyleFontFamilies(OH_Drawing_TextStyle * style,int fontFamiliesNumber,const char * fontFamilies[])256 void OH_Drawing_SetTextStyleFontFamilies(OH_Drawing_TextStyle* style,
257 int fontFamiliesNumber, const char* fontFamilies[])
258 {
259 std::vector<std::string> rosenFontFamilies;
260 for (int i = 0; i < fontFamiliesNumber; i++) {
261 rosenFontFamilies.emplace_back(fontFamilies[i]);
262 }
263 ConvertToOriginalText(style)->fontFamilies_ = rosenFontFamilies;
264 }
265
OH_Drawing_SetTextStyleFontStyle(OH_Drawing_TextStyle * style,int fontStyle)266 void OH_Drawing_SetTextStyleFontStyle(OH_Drawing_TextStyle* style, int fontStyle)
267 {
268 FontStyle rosenFontStyle;
269 switch (fontStyle) {
270 case FONT_STYLE_NORMAL: {
271 rosenFontStyle = FontStyle::NORMAL;
272 break;
273 }
274 case FONT_STYLE_ITALIC: {
275 rosenFontStyle = FontStyle::ITALIC;
276 break;
277 }
278 default: {
279 rosenFontStyle = FontStyle::NORMAL;
280 }
281 }
282 ConvertToOriginalText(style)->fontStyle_ = rosenFontStyle;
283 }
284
OH_Drawing_SetTextStyleLocale(OH_Drawing_TextStyle * style,const char * locale)285 void OH_Drawing_SetTextStyleLocale(OH_Drawing_TextStyle* style, const char* locale)
286 {
287 ConvertToOriginalText(style)->locale_ = locale;
288 }
289
OH_Drawing_CreateTypographyHandler(OH_Drawing_TypographyStyle * style,OH_Drawing_FontCollection * fontCollection)290 OH_Drawing_TypographyCreate* OH_Drawing_CreateTypographyHandler(OH_Drawing_TypographyStyle* style,
291 OH_Drawing_FontCollection* fontCollection)
292 {
293 const TypographyStyle* typoStyle = ConvertToOriginalText(style);
294 std::unique_ptr<TypographyCreate> handler = TypographyCreate::CreateRosenBuilder(*typoStyle,
295 std::shared_ptr<FontCollection>(ConvertToOriginalText(fontCollection)));
296 return ConvertToNDKText(handler.release());
297 }
298
OH_Drawing_DestroyTypographyHandler(OH_Drawing_TypographyCreate * handler)299 void OH_Drawing_DestroyTypographyHandler(OH_Drawing_TypographyCreate* handler)
300 {
301 delete ConvertToOriginalText(handler);
302 }
303
OH_Drawing_TypographyHandlerPushTextStyle(OH_Drawing_TypographyCreate * handler,OH_Drawing_TextStyle * style)304 void OH_Drawing_TypographyHandlerPushTextStyle(OH_Drawing_TypographyCreate* handler, OH_Drawing_TextStyle* style)
305 {
306 const TextStyle* rosenTextStyle = ConvertToOriginalText(style);
307 ConvertToOriginalText(handler)->PushStyle(*rosenTextStyle);
308 }
309
OH_Drawing_TypographyHandlerAddText(OH_Drawing_TypographyCreate * handler,const char * text)310 void OH_Drawing_TypographyHandlerAddText(OH_Drawing_TypographyCreate* handler, const char* text)
311 {
312 const std::u16string wideText =
313 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(text);
314 ConvertToOriginalText(handler)->AddText(wideText);
315 }
316
OH_Drawing_TypographyHandlerPopTextStyle(OH_Drawing_TypographyCreate * handler)317 void OH_Drawing_TypographyHandlerPopTextStyle(OH_Drawing_TypographyCreate* handler)
318 {
319 ConvertToOriginalText(handler)->Pop();
320 }
321
OH_Drawing_CreateTypography(OH_Drawing_TypographyCreate * handler)322 OH_Drawing_Typography* OH_Drawing_CreateTypography(OH_Drawing_TypographyCreate* handler)
323 {
324 TypographyCreate* rosenHandler = ConvertToOriginalText(handler);
325 std::unique_ptr<Typography> typography = rosenHandler->Build();
326 return ConvertToNDKText(typography.release());
327 }
328
OH_Drawing_DestroyTypography(OH_Drawing_Typography * typography)329 void OH_Drawing_DestroyTypography(OH_Drawing_Typography* typography)
330 {
331 delete ConvertToOriginalText(typography);
332 }
333
OH_Drawing_TypographyLayout(OH_Drawing_Typography * typography,double maxWidth)334 void OH_Drawing_TypographyLayout(OH_Drawing_Typography* typography, double maxWidth)
335 {
336 ConvertToOriginalText(typography)->Layout(maxWidth);
337 }
338
OH_Drawing_TypographyPaint(OH_Drawing_Typography * typography,OH_Drawing_Canvas * canvas,double potisionX,double potisionY)339 void OH_Drawing_TypographyPaint(OH_Drawing_Typography* typography, OH_Drawing_Canvas* canvas,
340 double potisionX, double potisionY)
341 {
342 ConvertToOriginalText(typography)->Paint(reinterpret_cast<OHOS::Rosen::Drawing::Canvas*>(canvas),
343 potisionX, potisionY);
344 }
345