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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_TEXT_TEXT_DECLARATION_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_TEXT_TEXT_DECLARATION_H 18 19 #include "core/components/declaration/common/declaration.h" 20 #include "frameworks/bridge/common/dom/dom_type.h" 21 22 namespace OHOS::Ace { 23 24 struct TextSpecializedAttribute : Attribute { 25 std::string data; 26 }; 27 28 struct TextSpecializedStyle : Style { 29 TextStyle textStyle; 30 Color focusColor; 31 CopyOptions copyOptions; 32 bool isMaxWidthLayout = false; 33 bool autoMaxLines = false; 34 }; 35 36 enum class TextShadowSettings { 37 OFFSET_ONLY = 2, 38 OFFSET_EXTRA, 39 OFFSET_BLUR_CLOR, 40 }; 41 42 class TextDeclaration : public Declaration { 43 DECLARE_ACE_TYPE(TextDeclaration, Declaration); 44 45 public: 46 TextDeclaration() = default; 47 ~TextDeclaration() override = default; 48 49 void InitializeStyle() override; 50 GetData()51 const std::string& GetData() const 52 { 53 auto& attribute = static_cast<TextSpecializedAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 54 return attribute.data; 55 } 56 SetData(const std::string & data)57 void SetData(const std::string& data) 58 { 59 auto& attribute = MaybeResetAttribute<TextSpecializedAttribute>(AttributeTag::SPECIALIZED_ATTR); 60 CheckIsChanged(attribute.data, data); 61 attribute.data = data; 62 } 63 GetTextStyle()64 const TextStyle& GetTextStyle() const 65 { 66 auto& style = static_cast<TextSpecializedStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 67 return style.textStyle; 68 } 69 SetTextStyle(const TextStyle & textStyle)70 void SetTextStyle(const TextStyle& textStyle) 71 { 72 auto& style = MaybeResetStyle<TextSpecializedStyle>(StyleTag::SPECIALIZED_STYLE); 73 CheckIsChanged(style.textStyle, textStyle); 74 style.textStyle = textStyle; 75 } 76 GetFocusColor()77 const Color& GetFocusColor() const 78 { 79 auto& style = static_cast<TextSpecializedStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 80 return style.focusColor; 81 } 82 SetFocusColor(const Color & focusColor)83 void SetFocusColor(const Color& focusColor) 84 { 85 auto& style = MaybeResetStyle<TextSpecializedStyle>(StyleTag::SPECIALIZED_STYLE); 86 CheckIsChanged(style.focusColor, focusColor); 87 style.focusColor = focusColor; 88 } 89 GetCopyOption()90 const CopyOptions& GetCopyOption() const 91 { 92 auto& style = static_cast<TextSpecializedStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 93 return style.copyOptions; 94 } 95 SetCopyOption(const CopyOptions & copyOptions)96 void SetCopyOption(const CopyOptions& copyOptions) 97 { 98 auto& style = MaybeResetStyle<TextSpecializedStyle>(StyleTag::SPECIALIZED_STYLE); 99 CheckIsChanged(style.copyOptions, copyOptions); 100 style.copyOptions = copyOptions; 101 } 102 IsMaxWidthLayout()103 bool IsMaxWidthLayout() const 104 { 105 auto& style = static_cast<TextSpecializedStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 106 return style.isMaxWidthLayout; 107 } 108 SetIsMaxWidthLayout(bool isMaxWidthLayout)109 void SetIsMaxWidthLayout(bool isMaxWidthLayout) 110 { 111 auto& style = MaybeResetStyle<TextSpecializedStyle>(StyleTag::SPECIALIZED_STYLE); 112 CheckIsChanged(style.isMaxWidthLayout, isMaxWidthLayout); 113 style.isMaxWidthLayout = isMaxWidthLayout; 114 } 115 GetAutoMaxLines()116 bool GetAutoMaxLines() const 117 { 118 auto& style = static_cast<TextSpecializedStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 119 return style.autoMaxLines; 120 } 121 SetAutoMaxLines(bool autoMaxLines)122 void SetAutoMaxLines(bool autoMaxLines) 123 { 124 auto& style = MaybeResetStyle<TextSpecializedStyle>(StyleTag::SPECIALIZED_STYLE); 125 CheckIsChanged(style.autoMaxLines, autoMaxLines); 126 style.autoMaxLines = autoMaxLines; 127 } 128 IsChanged()129 bool IsChanged() const 130 { 131 return isChanged_; 132 } 133 SetIsChanged(bool isChanged)134 void SetIsChanged(bool isChanged) 135 { 136 isChanged_ = isChanged; 137 } 138 HasSetTextFontSize()139 bool HasSetTextFontSize() const 140 { 141 return hasSetTextFontSize_; 142 } 143 HasSetTextColor()144 bool HasSetTextColor() const 145 { 146 return hasSetTextColor_; 147 } 148 149 static std::vector<Shadow> ParseTextShadow(const std::string& val, TextDeclaration& declaration); 150 151 protected: 152 void InitSpecialized() override; 153 bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override; 154 bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override; 155 156 private: 157 template<class T> CheckIsChanged(const T & update,const T & val)158 void CheckIsChanged(const T& update, const T& val) 159 { 160 if (update != val) { 161 isChanged_ = true; 162 } 163 } 164 165 bool isChanged_ = false; 166 bool hasSetTextColor_ = false; 167 bool hasSetTextFontSize_ = false; 168 }; 169 170 } // namespace OHOS::Ace 171 172 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_TEXT_TEXT_DECLARATION_H 173