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