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 #include "core/pipeline/base/constants.h"
19
20 namespace OHOS::Ace {
21
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_ && preferFontSizes_ == rhs.preferFontSizes_ &&
32 fontSize_ == rhs.fontSize_ && adaptMinFontSize_ == rhs.adaptMinFontSize_ &&
33 adaptMaxFontSize_ == rhs.adaptMaxFontSize_ && adaptFontSizeStep_ == rhs.adaptFontSizeStep_ &&
34 lineHeight_ == rhs.lineHeight_ && fontWeight_ == rhs.fontWeight_ && fontStyle_ == rhs.fontStyle_ &&
35 textBaseline_ == rhs.textBaseline_ && textOverflow_ == rhs.textOverflow_ && textAlign_ == rhs.textAlign_ &&
36 textColor_ == rhs.textColor_ && textDecoration_ == rhs.textDecoration_ && textShadows_ == rhs.textShadows_ &&
37 letterSpacing_ == rhs.letterSpacing_ && maxLines_ == rhs.maxLines_ && adaptTextSize_ == rhs.adaptTextSize_ &&
38 allowScale_ == rhs.allowScale_ && wordBreak_ == rhs.wordBreak_ &&
39 textDecorationColor_ == rhs.textDecorationColor_ && textCase_ == rhs.textCase_ &&
40 baselineOffset_ == rhs.baselineOffset_ && adaptHeight_ == rhs.adaptHeight_ &&
41 textIndent_ == rhs.textIndent_ && verticalAlign_ == rhs.verticalAlign_ && wordSpacing_ == rhs.wordSpacing_ &&
42 ellipsisMode_ == rhs.ellipsisMode_;
43 }
44
operator !=(const TextStyle & rhs) const45 bool TextStyle::operator!=(const TextStyle& rhs) const
46 {
47 return !(rhs == *this);
48 }
49
SetAdaptTextSize(const Dimension & maxFontSize,const Dimension & minFontSize,const Dimension & fontSizeStep)50 void TextStyle::SetAdaptTextSize(
51 const Dimension& maxFontSize, const Dimension& minFontSize, const Dimension& fontSizeStep)
52 {
53 adaptMaxFontSize_ = maxFontSize;
54 adaptMinFontSize_ = minFontSize;
55 adaptFontSizeStep_ = fontSizeStep;
56 adaptTextSize_ = true;
57 }
58
ToJsonValue(std::unique_ptr<JsonValue> & json,const std::optional<TextBackgroundStyle> & style)59 void TextBackgroundStyle::ToJsonValue(std::unique_ptr<JsonValue>& json, const std::optional<TextBackgroundStyle>& style)
60 {
61 NG::BorderRadiusProperty defaultRadius;
62 TextBackgroundStyle exportStyle = { .backgroundColor = Color::TRANSPARENT, .backgroundRadius = defaultRadius };
63 if (style.has_value()) {
64 exportStyle.backgroundColor = style.value().backgroundColor.value_or(Color::TRANSPARENT);
65 exportStyle.backgroundRadius = style.value().backgroundRadius.value_or(defaultRadius);
66 }
67
68 auto styleJson = JsonUtil::Create(true);
69 styleJson->Put("color", exportStyle.backgroundColor->ColorToString().c_str());
70 auto radiusJson = JsonUtil::Create(true);
71 exportStyle.backgroundRadius->ToJsonValue(radiusJson, styleJson);
72
73 json->Put("textBackgroundStyle", styleJson);
74 }
75 } // namespace OHOS::Ace
76