• 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 "constants_converter.h"
17 
18 #include "flutter/third_party/txt/src/txt/font_style.h"
19 #include "flutter/third_party/txt/src/txt/font_weight.h"
20 #include "flutter/third_party/txt/src/txt/paragraph_style.h"
21 #include "flutter/third_party/txt/src/txt/text_decoration.h"
22 
23 #include "base/i18n/localization.h"
24 #include "core/components/common/properties/color.h"
25 #include "core/components/common/properties/text_style.h"
26 
27 namespace OHOS::Ace::Constants {
28 
ConvertTxtFontWeight(FontWeight fontWeight)29 txt::FontWeight ConvertTxtFontWeight(FontWeight fontWeight)
30 {
31     txt::FontWeight convertValue;
32     switch (fontWeight) {
33         case FontWeight::W100:
34         case FontWeight::LIGHTER:
35             convertValue = txt::FontWeight::w100;
36             break;
37         case FontWeight::W200:
38             convertValue = txt::FontWeight::w200;
39             break;
40         case FontWeight::W300:
41             convertValue = txt::FontWeight::w300;
42             break;
43         case FontWeight::W400:
44         case FontWeight::NORMAL:
45         case FontWeight::REGULAR:
46             convertValue = txt::FontWeight::w400;
47             break;
48         case FontWeight::W500:
49         case FontWeight::MEDIUM:
50             convertValue = txt::FontWeight::w500;
51             break;
52         case FontWeight::W600:
53             convertValue = txt::FontWeight::w600;
54             break;
55         case FontWeight::W700:
56         case FontWeight::BOLD:
57             convertValue = txt::FontWeight::w700;
58             break;
59         case FontWeight::W800:
60             convertValue = txt::FontWeight::w800;
61             break;
62         case FontWeight::W900:
63         case FontWeight::BOLDER:
64             convertValue = txt::FontWeight::w900;
65             break;
66         default:
67             LOGW("FontWeight setting error! Now using default FontWeight.");
68             convertValue = txt::FontWeight::w400;
69             break;
70     }
71     return convertValue;
72 }
73 
ConvertTxtFontStyle(FontStyle fontStyle)74 txt::FontStyle ConvertTxtFontStyle(FontStyle fontStyle)
75 {
76     txt::FontStyle convertValue;
77     switch (fontStyle) {
78         case FontStyle::NORMAL:
79             convertValue = txt::FontStyle::normal;
80             break;
81         case FontStyle::ITALIC:
82             convertValue = txt::FontStyle::italic;
83             break;
84         default:
85             LOGW("FontStyle setting error! Now using default FontStyle");
86             convertValue = txt::FontStyle::normal;
87             break;
88     }
89     return convertValue;
90 }
91 
ConvertTxtTextBaseline(TextBaseline textBaseline)92 txt::TextBaseline ConvertTxtTextBaseline(TextBaseline textBaseline)
93 {
94     txt::TextBaseline convertValue;
95     switch (textBaseline) {
96         case TextBaseline::ALPHABETIC:
97             convertValue = txt::TextBaseline::kAlphabetic;
98             break;
99         case TextBaseline::IDEOGRAPHIC:
100             convertValue = txt::TextBaseline::kIdeographic;
101             break;
102         default:
103             LOGD("TextBaseline setting error! Now using default TextBaseline");
104             convertValue = txt::TextBaseline::kAlphabetic;
105             break;
106     }
107     return convertValue;
108 }
109 
ConvertTxtTextAlign(TextAlign textAlign)110 txt::TextAlign ConvertTxtTextAlign(TextAlign textAlign)
111 {
112     txt::TextAlign convertValue;
113     switch (textAlign) {
114         case TextAlign::LEFT:
115             convertValue = txt::TextAlign::left;
116             break;
117         case TextAlign::RIGHT:
118             convertValue = txt::TextAlign::right;
119             break;
120         case TextAlign::CENTER:
121             convertValue = txt::TextAlign::center;
122             break;
123         case TextAlign::JUSTIFY:
124             convertValue = txt::TextAlign::justify;
125             break;
126         case TextAlign::START:
127             convertValue = txt::TextAlign::start;
128             break;
129         case TextAlign::END:
130             convertValue = txt::TextAlign::end;
131             break;
132         default:
133             LOGW("TextAlign setting error! Now using default TextAlign");
134             convertValue = txt::TextAlign::start;
135             break;
136     }
137     return convertValue;
138 }
139 
ConvertTxtTextDirection(TextDirection textDirection)140 txt::TextDirection ConvertTxtTextDirection(TextDirection textDirection)
141 {
142     txt::TextDirection convertValue;
143     switch (textDirection) {
144         case TextDirection::RTL:
145             convertValue = txt::TextDirection::rtl;
146             break;
147         case TextDirection::LTR:
148             convertValue = txt::TextDirection::ltr;
149             break;
150         default:
151             LOGW("TextDirection setting error! Now using default TextDirection");
152             convertValue = txt::TextDirection::ltr;
153             break;
154     }
155     return convertValue;
156 }
157 
ConvertSkColor(Color color)158 SkColor ConvertSkColor(Color color)
159 {
160     return color.GetValue();
161 }
162 
ConvertTxtTextDecoration(TextDecoration textDecoration)163 txt::TextDecoration ConvertTxtTextDecoration(TextDecoration textDecoration)
164 {
165     txt::TextDecoration convertValue = txt::TextDecoration::kNone;
166     switch (textDecoration) {
167         case TextDecoration::NONE:
168             convertValue = txt::TextDecoration::kNone;
169             break;
170         case TextDecoration::UNDERLINE:
171             convertValue = txt::TextDecoration::kUnderline;
172             break;
173         case TextDecoration::OVERLINE:
174             convertValue = txt::TextDecoration::kOverline;
175             break;
176         case TextDecoration::LINE_THROUGH:
177             convertValue = txt::TextDecoration::kLineThrough;
178             break;
179         default:
180             LOGW("TextDecoration setting error! Now using default TextDecoration");
181             break;
182     }
183     return convertValue;
184 }
185 
ConvertTxtStyle(const TextStyle & textStyle,const WeakPtr<PipelineBase> & context,txt::TextStyle & txtStyle)186 void ConvertTxtStyle(const TextStyle& textStyle, const WeakPtr<PipelineBase>& context, txt::TextStyle& txtStyle)
187 {
188     txtStyle.color = ConvertSkColor(textStyle.GetTextColor());
189     txtStyle.font_weight = ConvertTxtFontWeight(textStyle.GetFontWeight());
190     // Font size must be px when transferring to txt::TextStyle
191     auto pipelineContext = context.Upgrade();
192     if (pipelineContext) {
193         txtStyle.font_size = pipelineContext->NormalizeToPx(textStyle.GetFontSize());
194         if (textStyle.IsAllowScale() || textStyle.GetFontSize().Unit() == DimensionUnit::FP) {
195             txtStyle.font_size =
196                 pipelineContext->NormalizeToPx(textStyle.GetFontSize() * pipelineContext->GetFontScale());
197         }
198     } else {
199         txtStyle.font_size = textStyle.GetFontSize().Value();
200     }
201     txtStyle.font_style = ConvertTxtFontStyle(textStyle.GetFontStyle());
202 
203     if (textStyle.GetWordSpacing().Unit() == DimensionUnit::PERCENT) {
204         txtStyle.word_spacing = textStyle.GetWordSpacing().Value() * txtStyle.font_size;
205     } else {
206         if (pipelineContext) {
207             txtStyle.word_spacing = pipelineContext->NormalizeToPx(textStyle.GetWordSpacing());
208         } else {
209             txtStyle.word_spacing = textStyle.GetWordSpacing().Value();
210         }
211     }
212     if (pipelineContext) {
213         txtStyle.letter_spacing = pipelineContext->NormalizeToPx(textStyle.GetLetterSpacing());
214     }
215     txtStyle.text_baseline = ConvertTxtTextBaseline(textStyle.GetTextBaseline());
216     txtStyle.decoration = ConvertTxtTextDecoration(textStyle.GetTextDecoration());
217     txtStyle.decoration_color = ConvertSkColor(textStyle.GetTextDecorationColor());
218     txtStyle.font_families = textStyle.GetFontFamilies();
219     txtStyle.locale = Localization::GetInstance()->GetFontLocale();
220 
221     for (auto& spanShadow : textStyle.GetTextShadows()) {
222         txt::TextShadow txtShadow;
223         txtShadow.color = spanShadow.GetColor().GetValue();
224         txtShadow.offset.fX = static_cast<SkScalar>(spanShadow.GetOffset().GetX());
225         txtShadow.offset.fY = static_cast<SkScalar>(spanShadow.GetOffset().GetY());
226 #ifdef NG_BUILD
227         txtShadow.blur_sigma = spanShadow.GetBlurRadius();
228 #else
229         txtShadow.blur_radius = spanShadow.GetBlurRadius();
230 #endif
231         txtStyle.text_shadows.emplace_back(txtShadow);
232     }
233 
234     if (textStyle.GetLineHeight().Unit() == DimensionUnit::PERCENT) {
235         txtStyle.has_height_override = true;
236         txtStyle.height = textStyle.GetLineHeight().Value();
237     } else {
238         double fontSize = txtStyle.font_size;
239         double lineHeight = textStyle.GetLineHeight().Value();
240         if (pipelineContext) {
241             lineHeight = pipelineContext->NormalizeToPx(textStyle.GetLineHeight());
242         }
243         txtStyle.has_height_override = textStyle.HasHeightOverride();
244         if (!NearEqual(lineHeight, fontSize) && (lineHeight > 0.0) && (!NearZero(fontSize))) {
245             txtStyle.height = lineHeight / fontSize;
246         } else {
247             LOGD("use default text style height value.");
248             txtStyle.height = 1;
249             static const int32_t BEGIN_VERSION = 6;
250             auto isBeginVersion = pipelineContext && pipelineContext->GetMinPlatformVersion() >= BEGIN_VERSION;
251             if (NearZero(lineHeight) || (!isBeginVersion && NearEqual(lineHeight, fontSize))) {
252                 txtStyle.has_height_override = false;
253             }
254         }
255     }
256 
257     // set font variant
258     auto fontFeatures = textStyle.GetFontFeatures();
259     if (!fontFeatures.empty()) {
260         txt::FontFeatures features;
261         for (auto iter = fontFeatures.begin(); iter != fontFeatures.end(); ++iter) {
262             features.SetFeature(iter->first, iter->second);
263         }
264         txtStyle.font_features = features;
265     }
266 }
267 
ConvertSkRect(SkRect skRect)268 Rect ConvertSkRect(SkRect skRect)
269 {
270     Rect result;
271     result.SetLeft(skRect.fLeft);
272     result.SetTop(skRect.fTop);
273     result.SetWidth(skRect.width());
274     result.SetHeight(skRect.height());
275     return result;
276 }
277 
278 } // namespace OHOS::Ace::Constants
279