1 /* 2 * Copyright (c) 2022-2023 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_NG_PATTERNS_TEXT_TEXT_STYLES_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_STYLES_H 18 19 #include "core/components/common/properties/text_style.h" 20 #include "core/components/text/text_theme.h" 21 #include "core/components_ng/pattern/symbol/symbol_effect_options.h" 22 #include "core/components_ng/property/measure_property.h" 23 #include "core/components_ng/property/property.h" 24 #include "core/components_ng/render/paragraph.h" 25 #include "core/components_v2/inspector/utils.h" 26 27 namespace OHOS::Ace { 28 29 struct CustomSpanMeasureInfo { 30 float fontSize = 0.0f; 31 }; 32 33 struct CustomSpanOptions { 34 float x = 0.0f; 35 float lineTop = 0.0f; 36 float lineBottom = 0.0f; 37 float baseline = 0.0f; 38 }; 39 40 struct CustomSpanMetrics { 41 float width = 0.0f; 42 std::optional<float> height; 43 }; 44 45 struct UserGestureOptions { 46 GestureEventFunc onClick; 47 GestureEventFunc onLongPress; 48 GestureEventFunc onDoubleClick; 49 }; 50 51 struct UserMouseOptions { 52 OnHoverFunc onHover; 53 }; 54 55 struct TextDecorationOptions { 56 std::optional<bool> enableMultiType; 57 IsEqualTextDecorationOptions58 bool IsEqual(const TextDecorationOptions& other) const 59 { 60 return this->enableMultiType.has_value() == other.enableMultiType.has_value() && 61 this->enableMultiType.value_or(false) == other.enableMultiType.value_or(false); 62 } 63 64 bool operator==(const TextDecorationOptions& other) const 65 { 66 return IsEqual(other); 67 } 68 69 bool operator!=(const TextDecorationOptions& other) const 70 { 71 return !IsEqual(other); 72 } 73 }; 74 75 struct ImageSpanSize { 76 std::optional<CalcDimension> width; 77 std::optional<CalcDimension> height; 78 79 bool operator==(const ImageSpanSize& other) const 80 { 81 return width == other.width && height == other.height; 82 } 83 GetSizeImageSpanSize84 NG::CalcSize GetSize() const 85 { 86 std::optional<NG::CalcLength> tmpWidth = std::nullopt; 87 if (width.has_value()) { 88 tmpWidth = NG::CalcLength(width->ConvertToPx()); 89 } 90 std::optional<NG::CalcLength> tmpHeight = std::nullopt; 91 if (height.has_value()) { 92 tmpHeight = NG::CalcLength(height->ConvertToPx()); 93 } 94 return {tmpWidth, tmpHeight}; 95 } 96 ToStringImageSpanSize97 std::string ToString() const 98 { 99 auto jsonValue = JsonUtil::Create(true); 100 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, width); 101 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, height); 102 return jsonValue->ToString(); 103 } 104 }; 105 106 struct ImageSpanAttribute { 107 std::optional<ImageSpanSize> size; 108 std::optional<VerticalAlign> verticalAlign; 109 std::optional<ImageFit> objectFit; 110 std::optional<OHOS::Ace::NG::MarginProperty> marginProp; 111 std::optional<OHOS::Ace::NG::BorderRadiusProperty> borderRadius; 112 std::optional<OHOS::Ace::NG::PaddingProperty> paddingProp; 113 114 bool syncLoad = false; 115 std::optional<std::vector<float>> colorFilterMatrix; 116 std::optional<RefPtr<DrawingColorFilter>> drawingColorFilter; 117 118 bool operator==(const ImageSpanAttribute& attribute) const 119 { 120 return size == attribute.size && verticalAlign == attribute.verticalAlign && objectFit == attribute.objectFit && 121 marginProp == attribute.marginProp && borderRadius == attribute.borderRadius && 122 paddingProp == attribute.paddingProp; 123 } 124 ToStringImageSpanAttribute125 std::string ToString() const 126 { 127 auto jsonValue = JsonUtil::Create(true); 128 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, size); 129 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, verticalAlign); 130 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, objectFit); 131 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, marginProp); 132 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, borderRadius); 133 return jsonValue->ToString(); 134 } 135 }; 136 137 enum class OptionSource { EXTERNAL_API = 0, USER_PASTE, UNDO_REDO, IME_INSERT, COLLBORATION }; 138 139 struct SpanOptionBase { 140 std::optional<int32_t> offset; 141 UserGestureOptions userGestureOption; 142 UserMouseOptions userMouseOption; 143 std::optional<Color> dragBackgroundColor; 144 bool isDragShadowNeeded = true; 145 OptionSource optionSource = OptionSource::EXTERNAL_API; 146 ToStringSpanOptionBase147 std::string ToString() const 148 { 149 auto jsonValue = JsonUtil::Create(true); 150 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, offset); 151 return jsonValue->ToString(); 152 } 153 }; 154 155 struct ImageSpanOptions : SpanOptionBase { 156 std::optional<int32_t> offset; 157 std::optional<std::string> image; 158 std::optional<std::string> bundleName; 159 std::optional<std::string> moduleName; 160 std::optional<RefPtr<PixelMap>> imagePixelMap; 161 std::optional<ImageSpanAttribute> imageAttribute; 162 std::optional<bool> isUriPureNumber; 163 HasValueImageSpanOptions164 bool HasValue() const 165 { 166 return offset.has_value() || image.has_value() || bundleName.has_value() || moduleName.has_value() || 167 imagePixelMap.has_value() || imageAttribute.has_value(); 168 } 169 ToStringImageSpanOptions170 std::string ToString() const 171 { 172 auto jsonValue = JsonUtil::Create(true); 173 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, offset); 174 if (imagePixelMap && *imagePixelMap) { 175 std::string pixSize = "["; 176 pixSize += std::to_string((*imagePixelMap)->GetWidth()); 177 pixSize += "*"; 178 pixSize += std::to_string((*imagePixelMap)->GetHeight()); 179 pixSize += "]"; 180 jsonValue->Put("pixelMapSize", pixSize.c_str()); 181 } 182 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, imageAttribute); 183 #ifndef IS_RELEASE_VERSION 184 JSON_STRING_PUT_OPTIONAL_STRING(jsonValue, image); 185 JSON_STRING_PUT_OPTIONAL_STRING(jsonValue, bundleName); 186 JSON_STRING_PUT_OPTIONAL_STRING(jsonValue, moduleName); 187 #endif 188 return jsonValue->ToString(); 189 } 190 }; 191 } // namespace OHOS::Ace 192 namespace OHOS::Ace::NG { 193 class TextLayoutProperty; 194 constexpr Dimension TEXT_DEFAULT_FONT_SIZE = 16.0_fp; 195 constexpr Dimension TEXT_DEFAULT_STROKE_WIDTH = 0.0_fp; 196 using FONT_FEATURES_LIST = std::list<std::pair<std::string, int32_t>>; 197 struct FontStyle { 198 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontSize, Dimension); 199 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextColor, Color); 200 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextShadow, std::vector<Shadow>); 201 ACE_DEFINE_PROPERTY_GROUP_ITEM(ItalicFontStyle, Ace::FontStyle); 202 ACE_DEFINE_PROPERTY_GROUP_ITEM(Superscript, SuperscriptStyle); 203 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontWeight, FontWeight); 204 ACE_DEFINE_PROPERTY_GROUP_ITEM(VariableFontWeight, int32_t); 205 ACE_DEFINE_PROPERTY_GROUP_ITEM(EnableVariableFontWeight, bool); 206 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontFamily, std::vector<std::string>); 207 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontFeature, FONT_FEATURES_LIST); 208 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextDecoration, std::vector<TextDecoration>); 209 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextDecorationColor, Color); 210 ACE_DEFINE_PROPERTY_GROUP_ITEM(StrokeWidth, Dimension); 211 ACE_DEFINE_PROPERTY_GROUP_ITEM(StrokeColor, Color); 212 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextDecorationStyle, TextDecorationStyle); 213 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextDecorationOptions, TextDecorationOptions); 214 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextCase, TextCase); 215 ACE_DEFINE_PROPERTY_GROUP_ITEM(AdaptMinFontSize, Dimension); 216 ACE_DEFINE_PROPERTY_GROUP_ITEM(AdaptMaxFontSize, Dimension); 217 ACE_DEFINE_PROPERTY_GROUP_ITEM(LetterSpacing, Dimension); 218 ACE_DEFINE_PROPERTY_GROUP_ITEM(ForegroundColor, Color); 219 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolColorList, std::vector<Color>); 220 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolRenderingStrategy, uint32_t); 221 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolEffectStrategy, uint32_t); 222 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolEffectOptions, SymbolEffectOptions); 223 ACE_DEFINE_PROPERTY_GROUP_ITEM(MinFontScale, float); 224 ACE_DEFINE_PROPERTY_GROUP_ITEM(MaxFontScale, float); 225 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolType, SymbolType); 226 ACE_DEFINE_PROPERTY_GROUP_ITEM(LineThicknessScale, float); 227 228 void UpdateColorByResourceId(); 229 GetTextDecorationFirstFontStyle230 TextDecoration GetTextDecorationFirst() const 231 { 232 auto decorations = GetTextDecoration(); 233 if (!decorations.has_value()) { 234 return TextDecoration::NONE; 235 } 236 return decorations.value().size() > 0 ? 237 decorations.value()[0] : TextDecoration::NONE; 238 } 239 }; 240 241 struct TextLineStyle { 242 ACE_DEFINE_PROPERTY_GROUP_ITEM(LineHeight, Dimension); 243 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextBaseline, TextBaseline); 244 ACE_DEFINE_PROPERTY_GROUP_ITEM(BaselineOffset, Dimension); 245 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextOverflow, TextOverflow); 246 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextAlign, TextAlign); 247 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextVerticalAlign, TextVerticalAlign); 248 ACE_DEFINE_PROPERTY_GROUP_ITEM(MaxLength, uint32_t); 249 ACE_DEFINE_PROPERTY_GROUP_ITEM(MaxLines, uint32_t); 250 ACE_DEFINE_PROPERTY_GROUP_ITEM(OverflowMode, OverflowMode); 251 ACE_DEFINE_PROPERTY_GROUP_ITEM(HeightAdaptivePolicy, TextHeightAdaptivePolicy); 252 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextIndent, Dimension); 253 ACE_DEFINE_PROPERTY_GROUP_ITEM(LeadingMargin, LeadingMargin); 254 ACE_DEFINE_PROPERTY_GROUP_ITEM(WordBreak, WordBreak); 255 ACE_DEFINE_PROPERTY_GROUP_ITEM(EllipsisMode, EllipsisMode); 256 ACE_DEFINE_PROPERTY_GROUP_ITEM(LineSpacing, Dimension); 257 ACE_DEFINE_PROPERTY_GROUP_ITEM(IsOnlyBetweenLines, bool); 258 ACE_DEFINE_PROPERTY_GROUP_ITEM(NumberOfLines, int32_t); 259 ACE_DEFINE_PROPERTY_GROUP_ITEM(LineBreakStrategy, LineBreakStrategy); 260 ACE_DEFINE_PROPERTY_GROUP_ITEM(HalfLeading, bool); 261 ACE_DEFINE_PROPERTY_GROUP_ITEM(AllowScale, bool); 262 ACE_DEFINE_PROPERTY_GROUP_ITEM(ParagraphSpacing, Dimension); 263 ACE_DEFINE_PROPERTY_GROUP_ITEM(OptimizeTrailingSpace, bool); 264 }; 265 266 struct HandleInfoNG { UpdateOffsetHandleInfoNG267 void UpdateOffset(const OffsetF& offset) 268 { 269 rect.SetOffset(offset); 270 } 271 AddOffsetHandleInfoNG272 void AddOffset(float x, float y) 273 { 274 auto offset = rect.GetOffset(); 275 offset.AddX(x); 276 offset.AddY(y); 277 UpdateOffset(offset); 278 } 279 280 bool operator==(const HandleInfoNG& handleInfo) const 281 { 282 return rect == handleInfo.rect && index == handleInfo.index; 283 } 284 285 bool operator!=(const HandleInfoNG& handleInfo) const 286 { 287 return !operator==(handleInfo); 288 } 289 290 int32_t index = 0; 291 RectF rect; 292 RectF originalRect; 293 }; 294 295 PlaceholderAlignment GetPlaceHolderAlignmentFromVerticalAlign(VerticalAlign verticalAlign); 296 297 TextStyle CreateTextStyleUsingTheme(const std::unique_ptr<FontStyle>& fontStyle, 298 const std::unique_ptr<TextLineStyle>& textLineStyle, const RefPtr<TextTheme>& textTheme, bool isSymbol = false); 299 300 void CreateTextStyleUsingTheme(const RefPtr<TextLayoutProperty>& property, const RefPtr<TextTheme>& textTheme, 301 TextStyle& textStyle, bool isSymbol = false); 302 303 void UseSelfStyle(const std::unique_ptr<FontStyle>& fontStyle, const std::unique_ptr<TextLineStyle>& textLineStyle, 304 TextStyle& textStyle, bool isSymbol = false); 305 306 void UseSelfStyleWithTheme(const RefPtr<TextLayoutProperty>& property, TextStyle& textStyle, 307 const RefPtr<TextTheme>& textTheme, bool isSymbol = false); 308 309 std::string GetFontFamilyInJson(const std::optional<std::vector<std::string>>& value); 310 std::string GetFontStyleInJson(const std::optional<Ace::FontStyle>& value); 311 std::string GetFontWeightInJson(const std::optional<FontWeight>& value); 312 std::string GetFontSizeInJson(const std::optional<Dimension>& value); 313 std::string GetSymbolRenderingStrategyInJson(const std::optional<uint32_t>& value); 314 std::string GetSymbolEffectStrategyInJson(const std::optional<uint32_t>& value); 315 std::string GetLineBreakStrategyInJson(const std::optional<Ace::LineBreakStrategy>& value); 316 std::string GetSymbolEffectOptionsInJson(const std::optional<SymbolEffectOptions>& value); 317 std::unique_ptr<JsonValue> GetSymbolShadowInJson(const std::optional<SymbolShadow>& value); 318 std::unique_ptr<JsonValue> GetShaderStyleInJson(const std::optional<std::vector<SymbolGradient>>& value); 319 } // namespace OHOS::Ace::NG 320 321 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_STYLES_H 322