1 /*
2  * Copyright (c) 2024 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_MODULES_SPTEXT_TEXT_STYLE_H
17 #define ROSEN_MODULES_SPTEXT_TEXT_STYLE_H
18 
19 #include <map>
20 #include <optional>
21 #include <string>
22 #include <vector>
23 
24 #include "include/core/SkColor.h"
25 #include "include/core/SkPoint.h"
26 #include "paint_record.h"
27 #include "text_types.h"
28 #include "symbol_engine/hm_symbol_txt.h"
29 
30 namespace OHOS {
31 namespace Rosen {
32 namespace SPText {
33 // FontFeatures is the container that stores the
34 // feature used to control how a font selects glyphs.
35 class FontFeatures {
36 public:
37     void SetFeature(std::string tag, int value);
38 
39     std::string GetFeatureSettings() const;
40 
41     const std::vector<std::pair<std::string, int>>& GetFontFeatures() const;
42 
43 private:
44     std::vector<std::pair<std::string, int>> features_;
45 };
46 
47 // Axis tags and values that can be applied in a text style to control the
48 // attributes of variable fonts.
49 class FontVariations {
50 public:
51     void SetAxisValue(std::string tag, float value);
52 
53     const std::map<std::string, float>& GetAxisValues() const;
54 
55 private:
56     std::map<std::string, float> axis_;
57 };
58 
59 // TextShadow contains parameters that control
60 // how the text shadow is displayed.
61 class TextShadow {
62 public:
63     TextShadow();
64 
65     TextShadow(SkColor color, SkPoint offset, double blurSigma);
66 
67     bool operator==(const TextShadow& other) const;
68 
69     bool operator!=(const TextShadow& other) const;
70 
71     bool HasShadow() const;
72 
73     SkColor color = SK_ColorBLACK;
74     SkPoint offset{0, 0};
75     double blurSigma = 0.0;
76 };
77 
78 struct RectStyle {
79     SkColor color = 0;
80     SkScalar leftTopRadius = 0.0f;
81     SkScalar rightTopRadius = 0.0f;
82     SkScalar rightBottomRadius = 0.0f;
83     SkScalar leftBottomRadius = 0.0f;
84 
85     bool operator ==(const RectStyle& rhs) const
86     {
87         return color == rhs.color &&
88             leftTopRadius == rhs.leftTopRadius &&
89             rightTopRadius == rhs.rightTopRadius &&
90             rightBottomRadius == rhs.rightBottomRadius &&
91             leftBottomRadius == rhs.leftBottomRadius;
92     }
93 
94     bool operator !=(const RectStyle& rhs) const
95     {
96         return color != rhs.color ||
97             leftTopRadius != rhs.leftTopRadius ||
98             rightTopRadius != rhs.rightTopRadius ||
99             rightBottomRadius != rhs.rightBottomRadius ||
100             leftBottomRadius != rhs.leftBottomRadius;
101     }
102 };
103 
104 // TextStyle is a collection of parameters that control how text is displayed,
105 // including parameters for fonts, decorations, and text.
106 class TextStyle {
107 public:
108     TextStyle();
109 
110     bool operator==(TextStyle const& other) const;
111 
112     SkColor color = SK_ColorWHITE;
113 
114     TextDecoration decoration = TextDecoration::NONE;
115     // transparent for no decoration color.
116     SkColor decorationColor = SK_ColorTRANSPARENT;
117     TextDecorationStyle decorationStyle = TextDecorationStyle::SOLID;
118     double decorationThicknessMultiplier = 1.0;
119 
120     FontWeight fontWeight = FontWeight::W400;
121     FontWidth fontWidth = FontWidth::NORMAL;
122     FontStyle fontStyle = FontStyle::NORMAL;
123     TextBaseline baseline = TextBaseline::ALPHABETIC;
124     bool halfLeading = false;
125     std::vector<std::string> fontFamilies;
126     double fontSize = 14.0;
127     double letterSpacing = 0.0;
128     double wordSpacing = 0.0;
129     double height = 1.0;
130     bool heightOverride = false;
131     std::string locale;
132     RectStyle backgroundRect = {0, 0.0f, 0.0f, 0.0f, 0.0f};
133     int styleId = 0;
134 
135     std::optional<PaintRecord> background;
136     std::optional<PaintRecord> foreground;
137 
138     std::vector<TextShadow> textShadows;
139     FontFeatures fontFeatures;
140     FontVariations fontVariations;
141 
142     // symbol glyph
143     bool isSymbolGlyph = false;
144     HMSymbolTxt symbol;
145     double baseLineShift = 0.0;
146     bool isPlaceholder = false;
147 };
148 
149 } // namespace SPText
150 } // namespace Rosen
151 } // namespace OHOS
152 
153 #endif // ROSEN_MODULES_SPTEXT_TEXT_STYLE_H
154