1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd.. All rights reserved. 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 ROSEN_TEXT_EXPORT_ROSEN_TEXT_TEXT_STYLE_H 17 #define ROSEN_TEXT_EXPORT_ROSEN_TEXT_TEXT_STYLE_H 18 19 #include <bitset> 20 #include <map> 21 #include <optional> 22 #include <string> 23 #include <vector> 24 25 #include "draw/pen.h" 26 #include "draw/brush.h" 27 #include "draw/color.h" 28 #include "utils/point.h" 29 #include "utils/scalar.h" 30 31 #include "common/rs_macros.h" 32 #include "typography_types.h" 33 #include "hm_symbol_txt.h" 34 35 namespace OHOS { 36 namespace Rosen { 37 enum class TextBadgeType { 38 BADGE_NONE, 39 SUPERSCRIPT, 40 SUBSCRIPT, 41 }; 42 43 enum class RelayoutTextStyleAttribute { 44 FONT_SIZE = 0, 45 FONT_WEIGHT = 1, 46 FONT_WIDTH = 2, 47 FONT_STYLE = 3, 48 FONT_FAMILIES = 4, 49 LETTER_SPACING = 5, 50 WORD_SPACING = 6, 51 HEIGHT_ONLY = 7, 52 HEIGHT_SCALE = 8, 53 FONT_FEATURES = 9, 54 FONT_VARIATIONS = 10, 55 BASELINE_SHIFT = 11, 56 DECORATION = 12, 57 DECORATION_COLOR = 13, 58 DECORATION_STYLE = 14, 59 DECORATION_THICKNESS_SCALE = 15, 60 BACKGROUND_RECT = 16, 61 STYLE_ID = 17, 62 FONT_COLOR = 18, 63 SHADOWS = 19, 64 HALF_LEADING = 20, 65 FOREGROUND_BRUSH = 21, 66 67 TEXT_STYLE_ATTRIBUTE_BUTT, 68 }; 69 70 class RS_EXPORT FontFeatures { 71 public: 72 void SetFeature(std::string tag, int value); 73 std::string GetFeatureSettings() const; 74 const std::vector<std::pair<std::string, int>> &GetFontFeatures() const; 75 FontFeatures() = default; 76 FontFeatures(const FontFeatures& other) = default; 77 FontFeatures& operator=(const FontFeatures& other) = default; 78 bool operator ==(const FontFeatures& rhs) const; 79 void Clear(); 80 81 private: 82 std::vector<std::pair<std::string, int>> featureSet_; 83 }; 84 85 class RS_EXPORT FontVariations { 86 public: 87 void SetAxisValue(const std::string& tag, float value); 88 const std::map<std::string, float>& GetAxisValues() const; 89 FontVariations() = default; 90 FontVariations(const FontVariations& other) = default; 91 FontVariations& operator=(const FontVariations& other) = default; 92 bool operator ==(const FontVariations& rhs) const; 93 void Clear(); 94 private: 95 std::map<std::string, float> axis_; 96 }; 97 98 struct RS_EXPORT TextShadow { 99 Drawing::Color color = Drawing::Color::COLOR_BLACK; 100 Drawing::Point offset; 101 double blurRadius = 0.0; 102 103 TextShadow(); 104 TextShadow(Drawing::Color shadowColor, Drawing::Point shadowOffset, double shadowBlurRadius); 105 TextShadow(const TextShadow& other) = default; 106 TextShadow& operator=(const TextShadow& other) = default; 107 bool operator ==(const TextShadow& rhs) const; 108 bool operator !=(const TextShadow& rhs) const; 109 bool HasShadow() const; 110 }; 111 112 struct RS_EXPORT RectStyle { 113 uint32_t color = 0; 114 double leftTopRadius = 0.0; 115 double rightTopRadius = 0.0; 116 double rightBottomRadius = 0.0; 117 double leftBottomRadius = 0.0; 118 bool operator ==(const RectStyle& rhs) const; 119 bool operator !=(const RectStyle& rhs) const; 120 }; 121 122 struct RS_EXPORT TextStyle { 123 Drawing::Color color = Drawing::Color::COLOR_WHITE; 124 TextDecoration decoration = TextDecoration::NONE; 125 Drawing::Color decorationColor = Drawing::Color::COLOR_TRANSPARENT; 126 TextDecorationStyle decorationStyle = TextDecorationStyle::SOLID; 127 double decorationThicknessScale = 1.0; 128 FontWeight fontWeight = FontWeight::W400; 129 FontWidth fontWidth = FontWidth::NORMAL; 130 FontStyle fontStyle = FontStyle::NORMAL; 131 TextBaseline baseline = TextBaseline::ALPHABETIC; 132 std::vector<std::string> fontFamilies; 133 double fontSize = 14.0; // default is libtxt text style font size 134 double letterSpacing = 0.0; 135 double wordSpacing = 0.0; 136 double heightScale = 1.0; 137 bool halfLeading = false; 138 bool heightOnly = false; 139 std::u16string ellipsis; 140 EllipsisModal ellipsisModal = EllipsisModal::TAIL; 141 std::string locale; 142 std::optional<Drawing::Brush> foregroundBrush; 143 std::optional<Drawing::Pen> foregroundPen; 144 std::optional<Drawing::Brush> backgroundBrush; 145 std::optional<Drawing::Pen> backgroundPen; 146 // if Pen and SkPaint are setting, use pen first 147 std::vector<TextShadow> shadows; 148 FontFeatures fontFeatures; 149 FontVariations fontVariations; 150 RectStyle backgroundRect{0, 0.0, 0.0, 0.0, 0.0}; 151 int styleId{0}; 152 size_t textStyleUid{0}; 153 TextStyle() = default; 154 TextStyle(const TextStyle& other) = default; 155 TextStyle& operator=(const TextStyle& other) = default; 156 bool operator ==(const TextStyle &rhs) const; 157 bool EqualByFonts(const TextStyle &rhs) const; 158 bool MatchOneAttribute(StyleType styleType, const TextStyle &rhs) const; 159 // symbol glyph 160 bool isSymbolGlyph{false}; 161 HMSymbolTxt symbol; 162 double baseLineShift{0.0f}; 163 bool isPlaceholder{false}; 164 std::bitset<static_cast<size_t>(RelayoutTextStyleAttribute::TEXT_STYLE_ATTRIBUTE_BUTT)> relayoutChangeBitmap; 165 TextBadgeType badgeType{TextBadgeType::BADGE_NONE}; 166 }; 167 } // namespace Rosen 168 } // namespace OHOS 169 170 #endif // ROSEN_TEXT_EXPORT_ROSEN_TEXT_TEXT_STYLE_H 171