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 ImageSpanSize { 56 std::optional<CalcDimension> width; 57 std::optional<CalcDimension> height; 58 59 bool operator==(const ImageSpanSize& other) const 60 { 61 return width == other.width && height == other.height; 62 } 63 GetSizeImageSpanSize64 NG::CalcSize GetSize() const 65 { 66 std::optional<NG::CalcLength> tmpWidth = std::nullopt; 67 if (width.has_value()) { 68 tmpWidth = NG::CalcLength(width->ConvertToPx()); 69 } 70 std::optional<NG::CalcLength> tmpHeight = std::nullopt; 71 if (height.has_value()) { 72 tmpHeight = NG::CalcLength(height->ConvertToPx()); 73 } 74 return {tmpWidth, tmpHeight}; 75 } 76 ToStringImageSpanSize77 std::string ToString() const 78 { 79 auto jsonValue = JsonUtil::Create(true); 80 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, width); 81 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, height); 82 return jsonValue->ToString(); 83 } 84 }; 85 86 struct ImageSpanAttribute { 87 std::optional<ImageSpanSize> size; 88 std::optional<VerticalAlign> verticalAlign; 89 std::optional<ImageFit> objectFit; 90 std::optional<OHOS::Ace::NG::MarginProperty> marginProp; 91 std::optional<OHOS::Ace::NG::BorderRadiusProperty> borderRadius; 92 std::optional<OHOS::Ace::NG::PaddingProperty> paddingProp; 93 bool syncLoad = false; 94 std::optional<std::vector<float>> colorFilterMatrix; 95 std::optional<RefPtr<DrawingColorFilter>> drawingColorFilter; 96 97 bool operator==(const ImageSpanAttribute& attribute) const 98 { 99 return size == attribute.size && verticalAlign == attribute.verticalAlign && objectFit == attribute.objectFit && 100 marginProp == attribute.marginProp && borderRadius == attribute.borderRadius && 101 paddingProp == attribute.paddingProp; 102 } 103 ToStringImageSpanAttribute104 std::string ToString() const 105 { 106 auto jsonValue = JsonUtil::Create(true); 107 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, size); 108 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, verticalAlign); 109 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, objectFit); 110 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, marginProp); 111 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, borderRadius); 112 return jsonValue->ToString(); 113 } 114 }; 115 116 struct SpanOptionBase { 117 std::optional<int32_t> offset; 118 UserGestureOptions userGestureOption; 119 UserMouseOptions userMouseOption; 120 std::optional<Color> dragBackgroundColor; 121 bool isDragShadowNeeded = true; 122 ToStringSpanOptionBase123 std::string ToString() const 124 { 125 auto jsonValue = JsonUtil::Create(true); 126 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, offset); 127 return jsonValue->ToString(); 128 } 129 }; 130 131 struct ImageSpanOptions : SpanOptionBase { 132 std::optional<int32_t> offset; 133 std::optional<std::string> image; 134 std::optional<std::string> bundleName; 135 std::optional<std::string> moduleName; 136 std::optional<RefPtr<PixelMap>> imagePixelMap; 137 std::optional<ImageSpanAttribute> imageAttribute; 138 std::optional<bool> isUriPureNumber; 139 HasValueImageSpanOptions140 bool HasValue() const 141 { 142 return offset.has_value() || image.has_value() || bundleName.has_value() || moduleName.has_value() || 143 imagePixelMap.has_value() || imageAttribute.has_value(); 144 } 145 ToStringImageSpanOptions146 std::string ToString() const 147 { 148 auto jsonValue = JsonUtil::Create(true); 149 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, offset); 150 if (imagePixelMap && *imagePixelMap) { 151 std::string pixSize = "["; 152 pixSize += std::to_string((*imagePixelMap)->GetWidth()); 153 pixSize += "*"; 154 pixSize += std::to_string((*imagePixelMap)->GetHeight()); 155 pixSize += "]"; 156 jsonValue->Put("pixelMapSize", pixSize.c_str()); 157 } 158 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, imageAttribute); 159 #ifndef IS_RELEASE_VERSION 160 JSON_STRING_PUT_OPTIONAL_STRING(jsonValue, image); 161 JSON_STRING_PUT_OPTIONAL_STRING(jsonValue, bundleName); 162 JSON_STRING_PUT_OPTIONAL_STRING(jsonValue, moduleName); 163 #endif 164 return jsonValue->ToString(); 165 } 166 }; 167 } // namespace OHOS::Ace 168 namespace OHOS::Ace::NG { 169 constexpr Dimension TEXT_DEFAULT_FONT_SIZE = 16.0_fp; 170 using FONT_FEATURES_LIST = std::list<std::pair<std::string, int32_t>>; 171 struct FontStyle { 172 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontSize, Dimension); 173 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextColor, Color); 174 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextShadow, std::vector<Shadow>); 175 ACE_DEFINE_PROPERTY_GROUP_ITEM(ItalicFontStyle, Ace::FontStyle); 176 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontWeight, FontWeight); 177 ACE_DEFINE_PROPERTY_GROUP_ITEM(VariableFontWeight, int32_t); 178 ACE_DEFINE_PROPERTY_GROUP_ITEM(EnableVariableFontWeight, bool); 179 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontFamily, std::vector<std::string>); 180 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontFeature, FONT_FEATURES_LIST); 181 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextDecoration, TextDecoration); 182 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextDecorationColor, Color); 183 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextDecorationStyle, TextDecorationStyle); 184 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextCase, TextCase); 185 ACE_DEFINE_PROPERTY_GROUP_ITEM(AdaptMinFontSize, Dimension); 186 ACE_DEFINE_PROPERTY_GROUP_ITEM(AdaptMaxFontSize, Dimension); 187 ACE_DEFINE_PROPERTY_GROUP_ITEM(LetterSpacing, Dimension); 188 ACE_DEFINE_PROPERTY_GROUP_ITEM(ForegroundColor, Color); 189 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolColorList, std::vector<Color>); 190 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolRenderingStrategy, uint32_t); 191 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolEffectStrategy, uint32_t); 192 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolEffectOptions, SymbolEffectOptions); 193 ACE_DEFINE_PROPERTY_GROUP_ITEM(MinFontScale, float); 194 ACE_DEFINE_PROPERTY_GROUP_ITEM(MaxFontScale, float); 195 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolType, SymbolType); 196 197 void UpdateColorByResourceId(); 198 }; 199 200 struct TextLineStyle { 201 ACE_DEFINE_PROPERTY_GROUP_ITEM(LineHeight, Dimension); 202 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextBaseline, TextBaseline); 203 ACE_DEFINE_PROPERTY_GROUP_ITEM(BaselineOffset, Dimension); 204 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextOverflow, TextOverflow); 205 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextAlign, TextAlign); 206 ACE_DEFINE_PROPERTY_GROUP_ITEM(MaxLength, uint32_t); 207 ACE_DEFINE_PROPERTY_GROUP_ITEM(MaxLines, uint32_t); 208 ACE_DEFINE_PROPERTY_GROUP_ITEM(HeightAdaptivePolicy, TextHeightAdaptivePolicy); 209 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextIndent, Dimension); 210 ACE_DEFINE_PROPERTY_GROUP_ITEM(LeadingMargin, LeadingMargin); 211 ACE_DEFINE_PROPERTY_GROUP_ITEM(WordBreak, WordBreak); 212 ACE_DEFINE_PROPERTY_GROUP_ITEM(EllipsisMode, EllipsisMode); 213 ACE_DEFINE_PROPERTY_GROUP_ITEM(LineSpacing, Dimension); 214 ACE_DEFINE_PROPERTY_GROUP_ITEM(NumberOfLines, int32_t); 215 ACE_DEFINE_PROPERTY_GROUP_ITEM(LineBreakStrategy, LineBreakStrategy); 216 ACE_DEFINE_PROPERTY_GROUP_ITEM(HalfLeading, bool); 217 ACE_DEFINE_PROPERTY_GROUP_ITEM(AllowScale, bool); 218 ACE_DEFINE_PROPERTY_GROUP_ITEM(ParagraphSpacing, Dimension); 219 }; 220 221 struct HandleInfoNG { UpdateOffsetHandleInfoNG222 void UpdateOffset(const OffsetF& offset) 223 { 224 rect.SetOffset(offset); 225 } 226 AddOffsetHandleInfoNG227 void AddOffset(float x, float y) 228 { 229 auto offset = rect.GetOffset(); 230 offset.AddX(x); 231 offset.AddY(y); 232 UpdateOffset(offset); 233 } 234 235 bool operator==(const HandleInfoNG& handleInfo) const 236 { 237 238 return rect == handleInfo.rect && index == handleInfo.index; 239 } 240 241 bool operator!=(const HandleInfoNG& handleInfo) const 242 { 243 return !operator==(handleInfo); 244 } 245 246 int32_t index = 0; 247 RectF rect; 248 RectF originalRect; 249 }; 250 251 TextStyle CreateTextStyleUsingTheme(const std::unique_ptr<FontStyle>& fontStyle, 252 const std::unique_ptr<TextLineStyle>& textLineStyle, const RefPtr<TextTheme>& textTheme); 253 254 TextStyle CreateTextStyleUsingThemeWithText(const RefPtr<FrameNode> frameNode, 255 const std::unique_ptr<FontStyle>& fontStyle, const std::unique_ptr<TextLineStyle>& textLineStyle, 256 const RefPtr<TextTheme>& textTheme); 257 258 void UseSelfStyle(const std::unique_ptr<FontStyle>& fontStyle, 259 const std::unique_ptr<TextLineStyle>& textLineStyle, TextStyle& textStyle); 260 261 std::string GetFontFamilyInJson(const std::optional<std::vector<std::string>>& value); 262 std::string GetFontStyleInJson(const std::optional<Ace::FontStyle>& value); 263 std::string GetFontWeightInJson(const std::optional<FontWeight>& value); 264 std::string GetFontSizeInJson(const std::optional<Dimension>& value); 265 std::string GetSymbolRenderingStrategyInJson(const std::optional<uint32_t>& value); 266 std::string GetSymbolEffectStrategyInJson(const std::optional<uint32_t>& value); 267 std::string GetLineBreakStrategyInJson(const std::optional<Ace::LineBreakStrategy>& value); 268 std::string GetSymbolEffectOptionsInJson(const std::optional<SymbolEffectOptions>& value); 269 } // namespace OHOS::Ace::NG 270 271 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_STYLES_H 272