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