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 94 bool syncLoad = false; 95 std::optional<std::vector<float>> colorFilterMatrix; 96 std::optional<RefPtr<DrawingColorFilter>> drawingColorFilter; 97 98 bool operator==(const ImageSpanAttribute& attribute) const 99 { 100 return size == attribute.size && verticalAlign == attribute.verticalAlign && objectFit == attribute.objectFit && 101 marginProp == attribute.marginProp && borderRadius == attribute.borderRadius && 102 paddingProp == attribute.paddingProp; 103 } 104 ToStringImageSpanAttribute105 std::string ToString() const 106 { 107 auto jsonValue = JsonUtil::Create(true); 108 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, size); 109 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, verticalAlign); 110 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, objectFit); 111 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, marginProp); 112 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, borderRadius); 113 return jsonValue->ToString(); 114 } 115 }; 116 117 struct SpanOptionBase { 118 std::optional<int32_t> offset; 119 UserGestureOptions userGestureOption; 120 UserMouseOptions userMouseOption; 121 ToStringSpanOptionBase122 std::string ToString() const 123 { 124 auto jsonValue = JsonUtil::Create(true); 125 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, offset); 126 return jsonValue->ToString(); 127 } 128 }; 129 130 struct ImageSpanOptions : SpanOptionBase { 131 std::optional<int32_t> offset; 132 std::optional<std::string> image; 133 std::optional<std::string> bundleName; 134 std::optional<std::string> moduleName; 135 std::optional<RefPtr<PixelMap>> imagePixelMap; 136 std::optional<ImageSpanAttribute> imageAttribute; 137 std::optional<bool> isUriPureNumber; 138 ToStringImageSpanOptions139 std::string ToString() const 140 { 141 auto jsonValue = JsonUtil::Create(true); 142 JSON_STRING_PUT_OPTIONAL_INT(jsonValue, offset); 143 JSON_STRING_PUT_OPTIONAL_STRING(jsonValue, image); 144 JSON_STRING_PUT_OPTIONAL_STRING(jsonValue, bundleName); 145 JSON_STRING_PUT_OPTIONAL_STRING(jsonValue, moduleName); 146 JSON_STRING_PUT_OPTIONAL_STRING(jsonValue, image); 147 if (imagePixelMap && *imagePixelMap) { 148 std::string pixSize = "["; 149 pixSize += std::to_string((*imagePixelMap)->GetWidth()); 150 pixSize += "*"; 151 pixSize += std::to_string((*imagePixelMap)->GetHeight()); 152 pixSize += "]"; 153 jsonValue->Put("pixelMapSize", pixSize.c_str()); 154 } 155 JSON_STRING_PUT_OPTIONAL_STRINGABLE(jsonValue, imageAttribute); 156 return jsonValue->ToString(); 157 } 158 }; 159 } // namespace OHOS::Ace 160 namespace OHOS::Ace::NG { 161 constexpr Dimension TEXT_DEFAULT_FONT_SIZE = 16.0_fp; 162 using FONT_FEATURES_LIST = std::list<std::pair<std::string, int32_t>>; 163 struct FontStyle { 164 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontSize, Dimension); 165 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextColor, Color); 166 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextShadow, std::vector<Shadow>); 167 ACE_DEFINE_PROPERTY_GROUP_ITEM(ItalicFontStyle, Ace::FontStyle); 168 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontWeight, FontWeight); 169 ACE_DEFINE_PROPERTY_GROUP_ITEM(VariableFontWeight, int32_t); 170 ACE_DEFINE_PROPERTY_GROUP_ITEM(EnableVariableFontWeight, bool); 171 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontFamily, std::vector<std::string>); 172 ACE_DEFINE_PROPERTY_GROUP_ITEM(FontFeature, FONT_FEATURES_LIST); 173 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextDecoration, TextDecoration); 174 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextDecorationColor, Color); 175 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextDecorationStyle, TextDecorationStyle); 176 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextCase, TextCase); 177 ACE_DEFINE_PROPERTY_GROUP_ITEM(AdaptMinFontSize, Dimension); 178 ACE_DEFINE_PROPERTY_GROUP_ITEM(AdaptMaxFontSize, Dimension); 179 ACE_DEFINE_PROPERTY_GROUP_ITEM(LetterSpacing, Dimension); 180 ACE_DEFINE_PROPERTY_GROUP_ITEM(ForegroundColor, Color); 181 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolColorList, std::vector<Color>); 182 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolRenderingStrategy, uint32_t); 183 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolEffectStrategy, uint32_t); 184 ACE_DEFINE_PROPERTY_GROUP_ITEM(SymbolEffectOptions, SymbolEffectOptions); 185 ACE_DEFINE_PROPERTY_GROUP_ITEM(MinFontScale, float); 186 ACE_DEFINE_PROPERTY_GROUP_ITEM(MaxFontScale, float); 187 188 void UpdateColorByResourceId(); 189 }; 190 191 struct TextLineStyle { 192 ACE_DEFINE_PROPERTY_GROUP_ITEM(LineHeight, Dimension); 193 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextBaseline, TextBaseline); 194 ACE_DEFINE_PROPERTY_GROUP_ITEM(BaselineOffset, Dimension); 195 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextOverflow, TextOverflow); 196 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextAlign, TextAlign); 197 ACE_DEFINE_PROPERTY_GROUP_ITEM(MaxLength, uint32_t); 198 ACE_DEFINE_PROPERTY_GROUP_ITEM(MaxLines, uint32_t); 199 ACE_DEFINE_PROPERTY_GROUP_ITEM(HeightAdaptivePolicy, TextHeightAdaptivePolicy); 200 ACE_DEFINE_PROPERTY_GROUP_ITEM(TextIndent, Dimension); 201 ACE_DEFINE_PROPERTY_GROUP_ITEM(LeadingMargin, LeadingMargin); 202 ACE_DEFINE_PROPERTY_GROUP_ITEM(WordBreak, WordBreak); 203 ACE_DEFINE_PROPERTY_GROUP_ITEM(EllipsisMode, EllipsisMode); 204 ACE_DEFINE_PROPERTY_GROUP_ITEM(LineSpacing, Dimension); 205 ACE_DEFINE_PROPERTY_GROUP_ITEM(NumberOfLines, int32_t); 206 ACE_DEFINE_PROPERTY_GROUP_ITEM(LineBreakStrategy, LineBreakStrategy); 207 ACE_DEFINE_PROPERTY_GROUP_ITEM(HalfLeading, bool); 208 ACE_DEFINE_PROPERTY_GROUP_ITEM(AllowScale, bool); 209 }; 210 211 struct HandleInfoNG { UpdateOffsetHandleInfoNG212 void UpdateOffset(const OffsetF& offset) 213 { 214 rect.SetOffset(offset); 215 } 216 AddOffsetHandleInfoNG217 void AddOffset(float x, float y) 218 { 219 auto offset = rect.GetOffset(); 220 offset.AddX(x); 221 offset.AddY(y); 222 UpdateOffset(offset); 223 } 224 225 bool operator==(const HandleInfoNG& handleInfo) const 226 { 227 228 return rect == handleInfo.rect && index == handleInfo.index; 229 } 230 231 bool operator!=(const HandleInfoNG& handleInfo) const 232 { 233 return !operator==(handleInfo); 234 } 235 236 int32_t index = 0; 237 RectF rect; 238 }; 239 240 TextStyle CreateTextStyleUsingTheme(const std::unique_ptr<FontStyle>& fontStyle, 241 const std::unique_ptr<TextLineStyle>& textLineStyle, const RefPtr<TextTheme>& textTheme); 242 243 TextStyle CreateTextStyleUsingThemeWithText(const RefPtr<FrameNode> frameNode, 244 const std::unique_ptr<FontStyle>& fontStyle, const std::unique_ptr<TextLineStyle>& textLineStyle, 245 const RefPtr<TextTheme>& textTheme); 246 247 void UseSelfStyle(const std::unique_ptr<FontStyle>& fontStyle, 248 const std::unique_ptr<TextLineStyle>& textLineStyle, TextStyle& textStyle); 249 250 std::string GetFontFamilyInJson(const std::optional<std::vector<std::string>>& value); 251 std::string GetFontStyleInJson(const std::optional<Ace::FontStyle>& value); 252 std::string GetFontWeightInJson(const std::optional<FontWeight>& value); 253 std::string GetFontSizeInJson(const std::optional<Dimension>& value); 254 std::string GetSymbolRenderingStrategyInJson(const std::optional<uint32_t>& value); 255 std::string GetSymbolEffectStrategyInJson(const std::optional<uint32_t>& value); 256 std::string GetLineBreakStrategyInJson(const std::optional<Ace::LineBreakStrategy>& value); 257 std::string GetSymbolEffectOptionsInJson(const std::optional<SymbolEffectOptions>& value); 258 } // namespace OHOS::Ace::NG 259 260 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_STYLES_H 261