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