1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.. All rights reserved.
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 "rosen_text/properties/rosen_converter_txt.h"
17 #include "engine_adapter/skia_adapter/skia_paint.h"
18
19 namespace rosen {
RosenConvertTxtFontWeight(FontWeight fontWeight)20 txt::FontWeight RosenConvertTxtFontWeight(FontWeight fontWeight)
21 {
22 txt::FontWeight txtFontWeight;
23 switch (fontWeight) {
24 case FontWeight::W100:
25 txtFontWeight = txt::FontWeight::w100;
26 break;
27 case FontWeight::W200:
28 txtFontWeight = txt::FontWeight::w200;
29 break;
30 case FontWeight::W300:
31 txtFontWeight = txt::FontWeight::w300;
32 break;
33 case FontWeight::W400:
34 txtFontWeight = txt::FontWeight::w400;
35 break;
36 case FontWeight::W500:
37 txtFontWeight = txt::FontWeight::w500;
38 break;
39 case FontWeight::W600:
40 txtFontWeight = txt::FontWeight::w600;
41 break;
42 case FontWeight::W700:
43 txtFontWeight = txt::FontWeight::w700;
44 break;
45 case FontWeight::W800:
46 txtFontWeight = txt::FontWeight::w800;
47 break;
48 case FontWeight::W900:
49 txtFontWeight = txt::FontWeight::w900;
50 break;
51 default:
52 txtFontWeight = txt::FontWeight::w400;
53 break;
54 }
55 return txtFontWeight;
56 }
57
RosenConvertTxtFontStyle(FontStyle fontStyle)58 txt::FontStyle RosenConvertTxtFontStyle(FontStyle fontStyle)
59 {
60 txt::FontStyle txtFontStyle;
61 switch (fontStyle) {
62 case FontStyle::NORMAL:
63 txtFontStyle = txt::FontStyle::normal;
64 break;
65 case FontStyle::ITALIC:
66 txtFontStyle = txt::FontStyle::italic;
67 break;
68 default:
69 txtFontStyle = txt::FontStyle::normal;
70 break;
71 }
72 return txtFontStyle;
73 }
74
RosenConvertTxtTextBaseline(TextBaseline textBaseline)75 txt::TextBaseline RosenConvertTxtTextBaseline(TextBaseline textBaseline)
76 {
77 txt::TextBaseline txtTextBaseline;
78 switch (textBaseline) {
79 case TextBaseline::ALPHABETIC:
80 txtTextBaseline = txt::TextBaseline::kAlphabetic;
81 break;
82 case TextBaseline::IDEOGRAPHIC:
83 txtTextBaseline = txt::TextBaseline::kIdeographic;
84 break;
85 default:
86 txtTextBaseline = txt::TextBaseline::kAlphabetic;
87 break;
88 }
89 return txtTextBaseline;
90 }
91
RosenConvertTxtTextDecoration(TextDecoration textDecoration)92 txt::TextDecoration RosenConvertTxtTextDecoration(TextDecoration textDecoration)
93 {
94 txt::TextDecoration txtTextDecoration;
95 switch (textDecoration) {
96 case TextDecoration::NONE:
97 txtTextDecoration = txt::TextDecoration::kNone;
98 break;
99 case TextDecoration::UNDERLINE:
100 txtTextDecoration = txt::TextDecoration::kUnderline;
101 break;
102 case TextDecoration::OVERLINE:
103 txtTextDecoration = txt::TextDecoration::kOverline;
104 break;
105 case TextDecoration::LINETHROUGH:
106 txtTextDecoration = txt::TextDecoration::kLineThrough;
107 break;
108 default:
109 txtTextDecoration = txt::TextDecoration::kNone;
110 break;
111 }
112 return txtTextDecoration;
113 }
114
RosenConvertTxtTextDecorationStyle(TextDecorationStyle textDecorationStyle)115 txt::TextDecorationStyle RosenConvertTxtTextDecorationStyle(TextDecorationStyle textDecorationStyle)
116 {
117 txt::TextDecorationStyle txtTextDecorationStyle;
118 switch (textDecorationStyle) {
119 case TextDecorationStyle::SOLID:
120 txtTextDecorationStyle = txt::TextDecorationStyle::kSolid;
121 break;
122 case TextDecorationStyle::DOUBLE:
123 txtTextDecorationStyle = txt::TextDecorationStyle::kDouble;
124 break;
125 case TextDecorationStyle::DOTTED:
126 txtTextDecorationStyle = txt::TextDecorationStyle::kDotted;
127 break;
128 case TextDecorationStyle::DASHED:
129 txtTextDecorationStyle = txt::TextDecorationStyle::kDashed;
130 break;
131 case TextDecorationStyle::WAVY:
132 txtTextDecorationStyle = txt::TextDecorationStyle::kWavy;
133 break;
134 default:
135 txtTextDecorationStyle = txt::TextDecorationStyle::kSolid;
136 break;
137 }
138 return txtTextDecorationStyle;
139 }
140
RosenConverMinkinBreakStrategy(BreakStrategy breakStrategy)141 minikin::BreakStrategy RosenConverMinkinBreakStrategy(BreakStrategy breakStrategy)
142 {
143 minikin::BreakStrategy minkinBreakStrategy;
144 switch (breakStrategy) {
145 case BreakStrategy::BreakStrategyGreedy:
146 minkinBreakStrategy = minikin::BreakStrategy::kBreakStrategy_Greedy;
147 break;
148 case BreakStrategy::BreakStrategyHighQuality:
149 minkinBreakStrategy = minikin::BreakStrategy::kBreakStrategy_HighQuality;
150 break;
151 case BreakStrategy::BreakStrategyBalanced:
152 minkinBreakStrategy = minikin::BreakStrategy::kBreakStrategy_Balanced;
153 break;
154 default:
155 minkinBreakStrategy = minikin::BreakStrategy::kBreakStrategy_Greedy;
156 break;
157 }
158 return minkinBreakStrategy;
159 }
160
161 #if !defined(USE_CANVASKIT0310_SKIA) && !defined(NEW_SKIA)
RosenConverMinkinWordBreakType(WordBreakType wordBreakType)162 minikin::WordBreakType RosenConverMinkinWordBreakType(WordBreakType wordBreakType)
163 {
164 minikin::WordBreakType minkinWordBreakType;
165 switch (wordBreakType) {
166 case WordBreakType::WordBreakTypeNormal:
167 minkinWordBreakType = minikin::WordBreakType::kWordBreakType_Normal;
168 break;
169 case WordBreakType::WordBreakTypeBreakAll:
170 minkinWordBreakType = minikin::WordBreakType::kWordBreakType_BreakAll;
171 break;
172 case WordBreakType::WordBreakTypeBreakWord:
173 minkinWordBreakType = minikin::WordBreakType::kWordBreakType_BreakWord;
174 break;
175 default:
176 minkinWordBreakType = minikin::WordBreakType::kWordBreakType_Normal;
177 break;
178 }
179 return minkinWordBreakType;
180 }
181 #endif
182
RosenConvertTxtTextDirection(TextDirection textDirection)183 txt::TextDirection RosenConvertTxtTextDirection(TextDirection textDirection)
184 {
185 txt::TextDirection txtTextDirection;
186 switch (textDirection) {
187 case TextDirection::RTL:
188 txtTextDirection = txt::TextDirection::rtl;
189 break;
190 case TextDirection::LTR:
191 txtTextDirection = txt::TextDirection::ltr;
192 break;
193 default:
194 txtTextDirection = txt::TextDirection::rtl;
195 break;
196 }
197 return txtTextDirection;
198 }
199
RosenConvertTxtTextAlign(TextAlign textAlign)200 txt::TextAlign RosenConvertTxtTextAlign(TextAlign textAlign)
201 {
202 txt::TextAlign txtTextAlign;
203 switch (textAlign) {
204 case TextAlign::LEFT:
205 txtTextAlign = txt::TextAlign::left;
206 break;
207 case TextAlign::RIGHT:
208 txtTextAlign = txt::TextAlign::right;
209 break;
210 case TextAlign::CENTER:
211 txtTextAlign = txt::TextAlign::center;
212 break;
213 case TextAlign::JUSTIFY:
214 txtTextAlign = txt::TextAlign::justify;
215 break;
216 case TextAlign::START:
217 txtTextAlign = txt::TextAlign::start;
218 break;
219 case TextAlign::END:
220 txtTextAlign = txt::TextAlign::end;
221 break;
222 default:
223 txtTextAlign = txt::TextAlign::left;
224 break;
225 }
226 return txtTextAlign;
227 }
228
RosenConvertAlignment(const PlaceholderAlignment & alignment)229 txt::PlaceholderAlignment RosenConvertAlignment(const PlaceholderAlignment& alignment)
230 {
231 txt::PlaceholderAlignment txtAalignment;
232 switch (alignment) {
233 case PlaceholderAlignment::BASELINE:
234 txtAalignment = txt::PlaceholderAlignment::kBaseline;
235 break;
236 case PlaceholderAlignment::ABOVEBASELINE:
237 txtAalignment = txt::PlaceholderAlignment::kAboveBaseline;
238 break;
239 case PlaceholderAlignment::BELOWBASELINE:
240 txtAalignment = txt::PlaceholderAlignment::kBelowBaseline;
241 break;
242 case PlaceholderAlignment::TOP:
243 txtAalignment = txt::PlaceholderAlignment::kTop;
244 break;
245 case PlaceholderAlignment::BOTTOM:
246 txtAalignment = txt::PlaceholderAlignment::kBottom;
247 break;
248 case PlaceholderAlignment::MIDDLE:
249 txtAalignment = txt::PlaceholderAlignment::kMiddle;
250 break;
251 default:
252 txtAalignment = txt::PlaceholderAlignment::kBaseline;
253 break;
254 }
255 return txtAalignment;
256 }
257
RosenConvertPlaceholderRun(const PlaceholderRun & placeholderRun)258 txt::PlaceholderRun RosenConvertPlaceholderRun(const PlaceholderRun& placeholderRun)
259 {
260 txt::PlaceholderRun txtPlaceholderRun;
261 txtPlaceholderRun.width = placeholderRun.width_;
262 txtPlaceholderRun.height = placeholderRun.height_;
263 txtPlaceholderRun.alignment = RosenConvertAlignment(placeholderRun.placeholderalignment_);
264 txtPlaceholderRun.baseline_offset = placeholderRun.baselineOffset_;
265 return txtPlaceholderRun;
266 }
267
RosenConvertTxtStyle(const TextStyle & textStyle,txt::TextStyle & txtStyle)268 void RosenConvertTxtStyle(const TextStyle& textStyle, txt::TextStyle& txtStyle)
269 {
270 txtStyle.color = textStyle.color_.CastToColorQuad();
271 txtStyle.decoration = RosenConvertTxtTextDecoration(textStyle.decoration_);
272 txtStyle.decoration_color = textStyle.decorationColor_.CastToColorQuad();
273 txtStyle.decoration_style = RosenConvertTxtTextDecorationStyle(textStyle.decorationStyle_);
274 txtStyle.decoration_thickness_multiplier = textStyle.decorationThicknessMultiplier_;
275 txtStyle.font_weight = RosenConvertTxtFontWeight(textStyle.fontWeight_);
276 txtStyle.font_style = RosenConvertTxtFontStyle(textStyle.fontStyle_);
277 txtStyle.text_baseline = RosenConvertTxtTextBaseline(textStyle.textBaseline_);
278 txtStyle.font_families = textStyle.fontFamilies_;
279 txtStyle.font_size = textStyle.fontSize_;
280 txtStyle.letter_spacing = textStyle.letterSpacing_;
281 txtStyle.word_spacing = textStyle.wordSpacing_;
282 txtStyle.height = textStyle.height_;
283 txtStyle.has_height_override = textStyle.hasHeightOverride_;
284 txtStyle.locale = textStyle.locale_;
285 txtStyle.has_background = textStyle.hasBackground_;
286 OHOS::Rosen::Drawing::SkiaPaint skiaPaint;
287 skiaPaint.PenToSkPaint(textStyle.background_, txtStyle.background);
288 txtStyle.has_foreground = textStyle.hasForeground_;
289 skiaPaint.PenToSkPaint(textStyle.foreground_, txtStyle.foreground);
290 auto textShadows = textStyle.textShadows_;
291 if (!textShadows.empty()) {
292 for (auto shadow : textShadows) {
293 txt::TextShadow txtShadow;
294 txtShadow.color = shadow.color_.CastToColorQuad();
295 txtShadow.offset = SkPoint::Make(shadow.offset_.GetX(), shadow.offset_.GetY());
296 #if defined(USE_CANVASKIT0310_SKIA) || defined(NEW_SKIA)
297 // new flutter libtxt not have blur_radius, use blur_sigma
298 txtShadow.blur_sigma = shadow.blurRadius_;
299 #else
300 txtShadow.blur_radius = shadow.blurRadius_;
301 #endif
302 txtStyle.text_shadows.emplace_back(txtShadow);
303 }
304 }
305
306 auto fontFeatures = textStyle.fontFeatures_.getGetFontFeatures();
307 if (!fontFeatures.empty()) {
308 txt::FontFeatures features;
309 for (auto iter = fontFeatures.begin(); iter != fontFeatures.end(); ++iter) {
310 features.SetFeature(iter->first, iter->second);
311 }
312 txtStyle.font_features = features;
313 }
314 }
315
RosenConvertTypographyStyle(const TypographyStyle & typographyStyle,txt::ParagraphStyle & txtParagraphStyle)316 void RosenConvertTypographyStyle(const TypographyStyle& typographyStyle, txt::ParagraphStyle& txtParagraphStyle)
317 {
318 txtParagraphStyle.font_weight = RosenConvertTxtFontWeight(typographyStyle.fontWeight_);
319 txtParagraphStyle.font_style = RosenConvertTxtFontStyle(typographyStyle.fontStyle_);
320 txtParagraphStyle.font_family = typographyStyle.fontFamily_;
321 txtParagraphStyle.font_size = typographyStyle.fontSize_;
322 txtParagraphStyle.height = typographyStyle.height_;
323 txtParagraphStyle.has_height_override = typographyStyle.hasHeightOverride_;
324 txtParagraphStyle.strut_enabled = typographyStyle.strutEnabled_;
325 txtParagraphStyle.strut_font_weight = RosenConvertTxtFontWeight(typographyStyle.strutFontWeight_);
326 txtParagraphStyle.strut_font_style = RosenConvertTxtFontStyle(typographyStyle.strutFontStyle_);
327 txtParagraphStyle.strut_font_families = typographyStyle.strutFontFamilies_;
328 txtParagraphStyle.strut_font_size = typographyStyle.strutFontSize_;
329 txtParagraphStyle.strut_height = typographyStyle.strutHeight_;
330 txtParagraphStyle.strut_has_height_override = typographyStyle.strutHasHeightOverride_;
331 txtParagraphStyle.strut_leading = typographyStyle.strutLeading_;
332 txtParagraphStyle.force_strut_height = typographyStyle.forceStrutHeight_;
333 txtParagraphStyle.text_align = RosenConvertTxtTextAlign(typographyStyle.textAlign_);
334 txtParagraphStyle.text_direction = RosenConvertTxtTextDirection(typographyStyle.textDirection_);
335 txtParagraphStyle.max_lines = typographyStyle.maxLines_;
336 txtParagraphStyle.ellipsis = typographyStyle.ellipsis_;
337 txtParagraphStyle.locale = typographyStyle.locale_;
338 txtParagraphStyle.break_strategy = RosenConverMinkinBreakStrategy(typographyStyle.breakStrategy_);
339 #if !defined(USE_CANVASKIT0310_SKIA) && !defined(NEW_SKIA)
340 txtParagraphStyle.word_break_type = RosenConverMinkinWordBreakType(typographyStyle.wordBreakType_);
341 #endif
342 }
343
TxtConvertRosenTextDirection(const txt::TextDirection & txtTextBox)344 TextDirection TxtConvertRosenTextDirection(const txt::TextDirection& txtTextBox)
345 {
346 TextDirection textDirection;
347 switch (txtTextBox) {
348 case txt::TextDirection::rtl:
349 textDirection = TextDirection::RTL;
350 break;
351 case txt::TextDirection::ltr:
352 textDirection = TextDirection::LTR;
353 break;
354 default:
355 textDirection = TextDirection::RTL;
356 break;
357 }
358 return textDirection;
359 }
360
TxtConvertRosenTextBox(txt::Paragraph::TextBox & txtTextBox)361 TypographyProperties::TextBox TxtConvertRosenTextBox(txt::Paragraph::TextBox& txtTextBox)
362 {
363 OHOS::Rosen::Drawing::Rect textRect(txtTextBox.rect.left(), txtTextBox.rect.top(),
364 txtTextBox.rect.right(), txtTextBox.rect.bottom());
365 TypographyProperties::TextBox rosenTextBox =
366 TypographyProperties::TextBox(textRect, TxtConvertRosenTextDirection(txtTextBox.direction));
367 return rosenTextBox;
368 }
369
RosenConvertTxtRectHeightStyle(TypographyProperties::RectHeightStyle heightStyle)370 txt::Paragraph::RectHeightStyle RosenConvertTxtRectHeightStyle(TypographyProperties::RectHeightStyle heightStyle)
371 {
372 txt::Paragraph::RectHeightStyle txtRectHeightStyle;
373 switch (heightStyle) {
374 case TypographyProperties::RectHeightStyle::TIGHT:
375 txtRectHeightStyle = txt::Paragraph::RectHeightStyle::kTight;
376 break;
377 case TypographyProperties::RectHeightStyle::MAX:
378 txtRectHeightStyle = txt::Paragraph::RectHeightStyle::kMax;
379 break;
380 case TypographyProperties::RectHeightStyle::INCLUDELINESPACEMIDDLE:
381 txtRectHeightStyle = txt::Paragraph::RectHeightStyle::kIncludeLineSpacingMiddle;
382 break;
383 case TypographyProperties::RectHeightStyle::INCLUDELINESPACETOP:
384 txtRectHeightStyle = txt::Paragraph::RectHeightStyle::kIncludeLineSpacingTop;
385 break;
386 case TypographyProperties::RectHeightStyle::INCLUDELINESPACEBOTTOM:
387 txtRectHeightStyle = txt::Paragraph::RectHeightStyle::kIncludeLineSpacingBottom;
388 break;
389 case TypographyProperties::RectHeightStyle::STRUCT:
390 txtRectHeightStyle = txt::Paragraph::RectHeightStyle::kStrut;
391 break;
392 default:
393 txtRectHeightStyle = txt::Paragraph::RectHeightStyle::kTight;
394 break;
395 }
396 return txtRectHeightStyle;
397 }
398
RosenConvertTxtRectWidthStyle(TypographyProperties::RectWidthStyle widthStyle)399 txt::Paragraph::RectWidthStyle RosenConvertTxtRectWidthStyle(TypographyProperties::RectWidthStyle widthStyle)
400 {
401 txt::Paragraph::RectWidthStyle txtRectWidthStyle;
402 switch (widthStyle) {
403 case TypographyProperties::RectWidthStyle::TIGHT:
404 txtRectWidthStyle = txt::Paragraph::RectWidthStyle::kTight;
405 break;
406 case TypographyProperties::RectWidthStyle::MAX:
407 txtRectWidthStyle = txt::Paragraph::RectWidthStyle::kMax;
408 break;
409 default:
410 txtRectWidthStyle = txt::Paragraph::RectWidthStyle::kTight;
411 break;
412 }
413 return txtRectWidthStyle;
414 }
415
TxtConvertRosenAffinity(const txt::Paragraph::Affinity & txtAffinity)416 TypographyProperties::Affinity TxtConvertRosenAffinity(const txt::Paragraph::Affinity& txtAffinity)
417 {
418 TypographyProperties::Affinity rosenAffinity;
419 switch (txtAffinity) {
420 case txt::Paragraph::Affinity::UPSTREAM:
421 rosenAffinity = TypographyProperties::Affinity::UPSTREAM;
422 break;
423 case txt::Paragraph::Affinity::DOWNSTREAM:
424 rosenAffinity = TypographyProperties::Affinity::DOWNSTREAM;
425 break;
426 default:
427 rosenAffinity = TypographyProperties::Affinity::UPSTREAM;
428 }
429 return rosenAffinity;
430 }
431
432
TxtConvertPosAndAffinity(txt::Paragraph::PositionWithAffinity & posAndAffinity)433 TypographyProperties::PositionAndAffinity TxtConvertPosAndAffinity(
434 txt::Paragraph::PositionWithAffinity& posAndAffinity)
435 {
436 TypographyProperties::PositionAndAffinity positionAndAffinity =
437 TypographyProperties::PositionAndAffinity(posAndAffinity.position,
438 TxtConvertRosenAffinity(posAndAffinity.affinity));
439 return positionAndAffinity;
440 }
441
TxtConvertRange(txt::Paragraph::Range<size_t> & txtRange)442 TypographyProperties::Range<size_t> TxtConvertRange(txt::Paragraph::Range<size_t>& txtRange)
443 {
444 TypographyProperties::Range<size_t> range = TypographyProperties::Range(txtRange.start, txtRange.end);
445 return range;
446 }
447 } // namespace rosen
448