1 /* 2 * Copyright (c) 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 ROSEN_MODULES_TEXGINE_EXPORT_TEXGINE_TEXT_STYLE_H 17 #define ROSEN_MODULES_TEXGINE_EXPORT_TEXGINE_TEXT_STYLE_H 18 19 #include <map> 20 #include <optional> 21 #include <string> 22 #include <vector> 23 24 #include "texgine_paint.h" 25 #include "texgine/typography_types.h" 26 #include "symbol_engine/hm_symbol_txt.h" 27 28 #include "draw/pen.h" 29 namespace OHOS { 30 namespace Rosen { 31 namespace TextEngine { 32 /* 33 * @brief FontFeatures is the container that stores the 34 * feature used to control how a font selects glyphs. 35 */ 36 class FontFeatures { 37 public: 38 /* 39 * @brief Set a feature by integer value. 40 * @param ftag The tag of feature. 41 * @param fvalue The integer value of feature. 42 */ 43 void SetFeature(const std::string& ftag, int fvalue); 44 45 /* 46 * @brief Returns the feature map that user set. 47 */ 48 const std::map<std::string, int>& GetFeatures() const; 49 50 /* 51 * @brief Implements the equality operator. 52 */ 53 bool operator ==(const FontFeatures& rhs) const; 54 55 private: 56 friend void ReportMemoryUsage(const std::string& member, const FontFeatures& that, const bool needThis); 57 58 std::map<std::string, int> features_; 59 }; 60 61 /* 62 * @brief TextShadow contains parameters that control 63 * how the text shadow is displayed. 64 */ 65 struct TextShadow { 66 // The offset between the shaded text and the main text 67 double offsetX = 0.0; 68 double offsetY = 0.0; 69 uint32_t color = 0xffcccccc; 70 double blurLeave = 0; 71 72 bool HasShadow() const; 73 // Implements the equality operator. 74 bool operator ==(TextShadow const& rhs) const; 75 }; 76 77 /* 78 * @brief RectStyle contains parameters that control 79 * how the text background rect is displayed. 80 */ 81 struct RectStyle { 82 uint32_t color = 0; 83 double leftTopRadius = 0.0; 84 double rightTopRadius = 0.0; 85 double rightBottomRadius = 0.0; 86 double leftBottomRadius = 0.0; 87 88 bool operator ==(const RectStyle& rhs) const 89 { 90 return color == rhs.color && 91 leftTopRadius == rhs.leftTopRadius && 92 rightTopRadius == rhs.rightTopRadius && 93 rightBottomRadius == rhs.rightBottomRadius && 94 leftBottomRadius == rhs.leftBottomRadius; 95 } 96 97 bool operator !=(const RectStyle& rhs) const 98 { 99 return color != rhs.color || 100 leftTopRadius != rhs.leftTopRadius || 101 rightTopRadius != rhs.rightTopRadius || 102 rightBottomRadius != rhs.rightBottomRadius || 103 leftBottomRadius != rhs.leftBottomRadius; 104 } 105 }; 106 107 enum class RoundRectType { 108 NONE, 109 LEFT_ONLY, 110 RIGHT_ONLY, 111 ALL, 112 }; 113 114 /* 115 * @brief TextStyle is a collection of parameters that control how text is displayed, 116 * including parameters for fonts, decorations, and text. 117 */ 118 struct TextStyle { 119 // font style 120 FontWeight fontWeight = FontWeight::W400; 121 FontStyle fontStyle = FontStyle::NORMAL; 122 std::vector<std::string> fontFamilies = {}; 123 double fontSize = 14.0; 124 FontFeatures fontFeature; 125 126 // Decoration style 127 TextDecoration decoration = TextDecoration::NONE; 128 std::optional<uint32_t> decorationColor = std::nullopt; 129 TextDecorationStyle decorationStyle = TextDecorationStyle::SOLID; 130 double decorationThicknessScale = 1.0; 131 132 // text style 133 uint32_t color = 0xff000000; // black 134 TextBaseline baseline = TextBaseline::ALPHABETIC; 135 std::string locale = ""; 136 bool halfLeading = false; 137 bool heightOnly = false; // true means text height is heightScale_ * fontSize_ 138 double heightScale = 1.0; 139 double letterSpacing = 0.0; 140 double wordSpacing = 0.0; 141 std::optional<TexginePaint> foreground = std::nullopt; 142 std::optional<TexginePaint> background = std::nullopt; 143 std::vector<TextShadow> shadows; 144 RectStyle backgroundRect = {0, 0.0, 0.0, 0.0, 0.0}; 145 int styleId = 0; 146 // Implements the equality operator. 147 bool operator ==(TextStyle const& rhs) const; 148 149 // symbol glyph 150 bool isSymbolGlyph = false; 151 HMSymbolTxt symbol; 152 }; 153 } // namespace TextEngine 154 } // namespace Rosen 155 } // namespace OHOS 156 157 #endif // ROSEN_MODULES_TEXGINE_EXPORT_TEXGINE_TEXT_STYLE_H 158