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 "core/components/common/properties/text_style.h"
17
18 namespace OHOS::Ace {
19 const std::vector<WordBreak> WORD_BREAK_TYPES = { WordBreak::NORMAL, WordBreak::BREAK_ALL, WordBreak::BREAK_WORD };
20 const std::vector<LineBreakStrategy> LINE_BREAK_STRATEGY_TYPES = { LineBreakStrategy::GREEDY,
21 LineBreakStrategy::HIGH_QUALITY, LineBreakStrategy::BALANCED };
TextStyle(const std::vector<std::string> & fontFamilies,double fontSize,FontWeight fontWeight,FontStyle fontStyle,const Color & textColor)22 TextStyle::TextStyle(const std::vector<std::string>& fontFamilies, double fontSize, FontWeight fontWeight,
23 FontStyle fontStyle, const Color& textColor)
24 : fontFamilies_(fontFamilies), fontSize_(fontSize), fontWeight_(fontWeight), fontStyle_(fontStyle),
25 textColor_(textColor)
26 {}
27
operator ==(const TextStyle & rhs) const28 bool TextStyle::operator==(const TextStyle& rhs) const
29 {
30 return fontFamilies_ == rhs.fontFamilies_ && fontFeatures_ == rhs.fontFeatures_ &&
31 textDecorationStyle_ == rhs.textDecorationStyle_ &&
32 preferFontSizes_ == rhs.preferFontSizes_ && fontSize_ == rhs.fontSize_ &&
33 adaptMinFontSize_ == rhs.adaptMinFontSize_ && adaptMaxFontSize_ == rhs.adaptMaxFontSize_ &&
34 adaptFontSizeStep_ == rhs.adaptFontSizeStep_ && lineHeight_ == rhs.lineHeight_ &&
35 fontWeight_ == rhs.fontWeight_ && fontStyle_ == rhs.fontStyle_ && textBaseline_ == rhs.textBaseline_ &&
36 textOverflow_ == rhs.textOverflow_ && textAlign_ == rhs.textAlign_ && textColor_ == rhs.textColor_ &&
37 textDecoration_ == rhs.textDecoration_ && textShadows_ == rhs.textShadows_ &&
38 letterSpacing_ == rhs.letterSpacing_ && maxLines_ == rhs.maxLines_ && adaptTextSize_ == rhs.adaptTextSize_ &&
39 allowScale_ == rhs.allowScale_ && wordBreak_ == rhs.wordBreak_ &&
40 textDecorationColor_ == rhs.textDecorationColor_ && textCase_ == rhs.textCase_ &&
41 baselineOffset_ == rhs.baselineOffset_ && adaptHeight_ == rhs.adaptHeight_ &&
42 textIndent_ == rhs.textIndent_ && verticalAlign_ == rhs.verticalAlign_ && wordSpacing_ == rhs.wordSpacing_ &&
43 ellipsisMode_ == rhs.ellipsisMode_ && lineBreakStrategy_ == rhs.lineBreakStrategy_ &&
44 textShadows_== rhs.textShadows_;
45 }
46
operator !=(const TextStyle & rhs) const47 bool TextStyle::operator!=(const TextStyle& rhs) const
48 {
49 return !(rhs == *this);
50 }
51
SetAdaptTextSize(const Dimension & maxFontSize,const Dimension & minFontSize,const Dimension & fontSizeStep)52 void TextStyle::SetAdaptTextSize(
53 const Dimension& maxFontSize, const Dimension& minFontSize, const Dimension& fontSizeStep)
54 {
55 adaptMaxFontSize_ = maxFontSize;
56 adaptMinFontSize_ = minFontSize;
57 adaptFontSizeStep_ = fontSizeStep;
58 adaptTextSize_ = true;
59 }
60
ToJsonValue(std::unique_ptr<JsonValue> & json,const std::optional<TextBackgroundStyle> & style,const NG::InspectorFilter & filter)61 void TextBackgroundStyle::ToJsonValue(std::unique_ptr<JsonValue>& json,
62 const std::optional<TextBackgroundStyle>& style, const NG::InspectorFilter& filter)
63 {
64 NG::BorderRadiusProperty defaultRadius;
65 TextBackgroundStyle exportStyle = { .backgroundColor = Color::TRANSPARENT, .backgroundRadius = defaultRadius };
66 if (style.has_value()) {
67 exportStyle.backgroundColor = style.value().backgroundColor.value_or(Color::TRANSPARENT);
68 exportStyle.backgroundRadius = style.value().backgroundRadius.value_or(defaultRadius);
69 }
70
71 auto styleJson = JsonUtil::Create(true);
72 styleJson->Put("color", exportStyle.backgroundColor->ColorToString().c_str());
73 auto radiusJson = JsonUtil::Create(true);
74 exportStyle.backgroundRadius->ToJsonValue(radiusJson, styleJson, filter);
75
76 json->PutExtAttr("textBackgroundStyle", styleJson, filter);
77 }
78
UpdateColorByResourceId()79 void TextStyle::UpdateColorByResourceId()
80 {
81 textColor_.UpdateColorByResourceId();
82 textDecorationColor_.UpdateColorByResourceId();
83 std::for_each(renderColors_.begin(), renderColors_.end(), [](Color& cl) { cl.UpdateColorByResourceId(); });
84 std::for_each(textShadows_.begin(), textShadows_.end(), [](Shadow& sd) { sd.UpdateColorByResourceId(); });
85 }
86
ToString() const87 std::string TextStyle::ToString() const
88 {
89 auto jsonValue = JsonUtil::Create(true);
90 JSON_STRING_PUT_STRINGABLE(jsonValue, fontSize_);
91 JSON_STRING_PUT_STRINGABLE(jsonValue, textColor_);
92 JSON_STRING_PUT_STRINGABLE(jsonValue, lineHeight_);
93 JSON_STRING_PUT_STRINGABLE(jsonValue, baselineOffset_);
94 JSON_STRING_PUT_STRINGABLE(jsonValue, wordSpacing_);
95 JSON_STRING_PUT_STRINGABLE(jsonValue, textIndent_);
96 JSON_STRING_PUT_STRINGABLE(jsonValue, letterSpacing_);
97 JSON_STRING_PUT_STRINGABLE(jsonValue, lineSpacing_);
98
99 JSON_STRING_PUT_INT(jsonValue, fontWeight_);
100 JSON_STRING_PUT_INT(jsonValue, fontStyle_);
101 JSON_STRING_PUT_INT(jsonValue, textBaseline_);
102 JSON_STRING_PUT_INT(jsonValue, textOverflow_);
103 JSON_STRING_PUT_INT(jsonValue, verticalAlign_);
104 JSON_STRING_PUT_INT(jsonValue, textAlign_);
105 JSON_STRING_PUT_INT(jsonValue, textDecorationStyle_);
106 JSON_STRING_PUT_INT(jsonValue, textDecoration_);
107 JSON_STRING_PUT_INT(jsonValue, whiteSpace_);
108 JSON_STRING_PUT_INT(jsonValue, wordBreak_);
109 JSON_STRING_PUT_INT(jsonValue, textCase_);
110 JSON_STRING_PUT_INT(jsonValue, ellipsisMode_);
111 JSON_STRING_PUT_INT(jsonValue, lineBreakStrategy_);
112
113 std::stringstream ss;
114 std::for_each(renderColors_.begin(), renderColors_.end(), [&ss](const Color& c) { ss << c.ToString() << ","; });
115 jsonValue->Put("renderColors", ss.str().c_str());
116 JSON_STRING_PUT_INT(jsonValue, renderStrategy_);
117 JSON_STRING_PUT_INT(jsonValue, effectStrategy_);
118 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, symbolEffectOptions_);
119
120 return jsonValue->ToString();
121 }
122
123 } // namespace OHOS::Ace
124