• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TYPOGRAPHY_STYLE_H
17 #define ROSEN_TEXT_EXPORT_ROSEN_TEXT_TYPOGRAPHY_STYLE_H
18 
19 #include <bitset>
20 #include <string>
21 #include <vector>
22 
23 #include "modules/skparagraph/include/TextStyle.h"
24 #include "text_style.h"
25 #include "typography_types.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 enum class RelayoutParagraphStyleAttribute {
30     FONT_SIZE = 0,
31     DIRECTION = 1,
32     MAXLINES = 2,
33     FONT_WEIGHT = 3,
34     FONT_STYLE = 4,
35     FONT_FAMILY = 5,
36     HEIGHT_SCALE = 6,
37     HEIGHT_ONLY = 7,
38     HALF_LEADING = 8,
39     USE_LINE_STYLE = 9,
40     LINE_STYLE_FONT_WEIGHT = 10,
41     LINE_STYLE_FONT_WIDTH = 11,
42     LINE_STYLE_FONT_STYLE = 12,
43     LINE_STYLE_FONT_FAMILY = 13,
44     LINE_STYLE_FONT_SIZE = 14,
45     LINE_STYLE_HEIGHT_SCALE = 15,
46     LINE_STYLE_HEIGHT_ONLY = 16,
47     LINE_STYLE_HALF_LEADING = 17,
48     LINE_STYLE_ONLY = 18,
49     BREAKSTRAGY = 19,
50     WORD_BREAKTYPE = 20,
51     ELLIPSIS = 21,
52     ELLIPSIS_MODAL = 22,
53     TEXT_ALIGN = 23,
54     SPACING = 24,
55     SPACING_IS_END = 25,
56     TEXT_HEIGHT_BEHAVIOR = 26,
57 
58     PARAGRAPH_STYLE_ATTRIBUTE_BUTT,
59 };
60 
61 struct TextTab {
62     TextTab() = default;
TextTabTextTab63     TextTab(TextAlign alignment, float location) : alignment(alignment), location(location) {};
TextTabTextTab64     TextTab(const TextTab& other) : alignment(other.alignment), location(other.location) {};
65     TextTab& operator=(const TextTab&) = default;
66 
67     bool operator==(const TextTab& rhs) const
68     {
69         return (this->alignment == rhs.alignment) &&
70             (skia::textlayout::nearlyEqual(this->location, rhs.location));
71     }
72     TextAlign alignment = TextAlign::LEFT;
73     float location = -1.0f;
74 };
75 struct RS_EXPORT TypographyStyle {
76     const static inline std::u16string ELLIPSIS = u"\u2026";
77 
78     FontWeight fontWeight = FontWeight::W400;
79     FontWidth fontWidth = FontWidth::NORMAL;
80     FontStyle fontStyle = FontStyle::NORMAL;
81     std::string fontFamily = "";
82     double fontSize = 14.0; // default is libtxt text style fonst size
83     double heightScale = 1.0;
84     bool halfLeading = false;
85     bool heightOnly = false;
86     bool useLineStyle = false;
87 
88     FontWeight lineStyleFontWeight = FontWeight::W400;
89     FontWidth lineStyleFontWidth = FontWidth::NORMAL;
90     FontStyle lineStyleFontStyle = FontStyle::NORMAL;
91     std::vector<std::string> lineStyleFontFamilies;
92     double lineStyleFontSize = 14.0; // default is libtxt text style font size
93     double lineStyleHeightScale = 1.0;
94     bool lineStyleHeightOnlyInit = false;
95     bool lineStyleHeightOnly = false;
96     bool lineStyleHalfLeading = false;
97     double lineStyleSpacingScale = -1.0;
98     bool lineStyleOnly = false;
99 
100     TextAlign textAlign = TextAlign::START;
101     TextDirection textDirection = TextDirection::LTR;
102     size_t maxLines = 1e9;
103     std::u16string ellipsis;
104     std::string locale;
105 
106     BreakStrategy breakStrategy = BreakStrategy::GREEDY;
107     WordBreakType wordBreakType = WordBreakType::BREAK_WORD;
108     EllipsisModal ellipsisModal = EllipsisModal::TAIL;
109     float textSplitRatio = 0.5f;
110     float paragraphSpacing{0.0f};
111     bool isEndAddParagraphSpacing{false};
112     bool isTrailingSpaceOptimized{false};
113     bool enableAutoSpace{false};
114     TextVerticalAlign verticalAlignment{TextVerticalAlign::BASELINE};
115 
116     TypographyStyle() = default;
117     TypographyStyle(const TypographyStyle& other) = default;
118     TypographyStyle& operator=(const TypographyStyle&) = default;
119     bool operator==(const TypographyStyle &rhs) const
120     {
121         return
122             this->verticalAlignment == rhs.verticalAlignment &&
123             this->ELLIPSIS == rhs.ELLIPSIS &&
124             this->fontWeight == rhs.fontWeight &&
125             this->fontStyle == rhs.fontStyle &&
126             this->fontFamily == rhs.fontFamily &&
127             skia::textlayout::nearlyEqual(this->fontSize, rhs.fontSize) &&
128             skia::textlayout::nearlyEqual(this->heightScale, rhs.heightScale) &&
129             this->halfLeading == rhs.halfLeading &&
130             this->heightOnly == rhs.heightOnly &&
131             this->useLineStyle == rhs.useLineStyle &&
132             this->lineStyleFontWidth == rhs.lineStyleFontWidth &&
133             this->lineStyleFontWeight == rhs.lineStyleFontWeight &&
134             this->lineStyleFontStyle == rhs.lineStyleFontStyle &&
135             this->lineStyleFontFamilies == rhs.lineStyleFontFamilies &&
136             skia::textlayout::nearlyEqual(this->lineStyleFontSize, rhs.lineStyleFontSize) &&
137             skia::textlayout::nearlyEqual(this->lineStyleHeightScale, rhs.lineStyleHeightScale) &&
138             this->lineStyleHeightOnlyInit == rhs.lineStyleHeightOnlyInit &&
139             this->lineStyleHeightOnly == rhs.lineStyleHeightOnly &&
140             this->lineStyleHalfLeading == rhs.lineStyleHalfLeading &&
141             skia::textlayout::nearlyEqual(this->lineStyleSpacingScale, rhs.lineStyleSpacingScale) &&
142             this->lineStyleOnly == rhs.lineStyleOnly &&
143             this->textAlign == rhs.textAlign &&
144             this->textDirection == rhs.textDirection &&
145             this->maxLines == rhs.maxLines &&
146             this->ellipsis == rhs.ellipsis &&
147             this->locale == rhs.locale &&
148             this->breakStrategy == rhs.breakStrategy &&
149             this->wordBreakType == rhs.wordBreakType &&
150             this->ellipsisModal == rhs.ellipsisModal &&
151             skia::textlayout::nearlyEqual(this->textSplitRatio, rhs.textSplitRatio) &&
152             this->defaultTextStyleUid == rhs.defaultTextStyleUid &&
153             this->tab == rhs.tab &&
154             this->paragraphSpacing == rhs.paragraphSpacing &&
155             this->isEndAddParagraphSpacing == rhs.isEndAddParagraphSpacing &&
156             this->isTrailingSpaceOptimized == rhs.isTrailingSpaceOptimized &&
157             this->enableAutoSpace == rhs.enableAutoSpace;
158     }
159     TextStyle GetTextStyle() const;
160     void SetTextStyle(TextStyle& textstyle);
161     TextAlign GetEffectiveAlign() const;
162     bool IsUnlimitedLines() const;
163     bool IsEllipsized() const;
EllipsizedTypographyStyle164     bool Ellipsized() const
165     {
166         return !ellipsis.empty();
167     }
168     TextStyle insideTextStyle;
169     bool customTextStyle = false;
170     TextHeightBehavior textHeightBehavior = TextHeightBehavior::ALL;
171     bool hintingIsOn = false;
172     TextTab tab;
173     std::bitset<static_cast<size_t>(RelayoutParagraphStyleAttribute::PARAGRAPH_STYLE_ATTRIBUTE_BUTT)>
174         relayoutChangeBitmap;
175     size_t defaultTextStyleUid { 0 };
176 };
177 } // namespace Rosen
178 } // namespace OHOS
179 #endif // ROSEN_TEXT_EXPORT_ROSEN_TEXT_TYPOGRAPHY_STYLE_H
180