• 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 
55     PARAGRAPH_STYLE_ATTRIBUTE_BUTT,
56 };
57 
58 struct TextTab {
59     TextTab() = default;
TextTabTextTab60     TextTab(TextAlign alignment, float location) : alignment(alignment), location(location) {};
TextTabTextTab61     TextTab(const TextTab& other) : alignment(other.alignment), location(other.location) {};
62     TextTab& operator=(const TextTab&) = default;
63 
64     bool operator==(const TextTab& rhs) const
65     {
66         return (this->alignment == rhs.alignment) &&
67             (skia::textlayout::nearlyEqual(this->location, rhs.location));
68     }
69     TextAlign alignment = TextAlign::LEFT;
70     static constexpr float INVALID_LOCATION = -1.0f;
71     float location = INVALID_LOCATION;
72 };
73 struct TypographyStyle {
74     const static inline std::u16string ELLIPSIS = u"\u2026";
75 
76     FontWeight fontWeight = FontWeight::W400;
77     FontWidth fontWidth = FontWidth::NORMAL;
78     FontStyle fontStyle = FontStyle::NORMAL;
79     std::string fontFamily = "";
80     double fontSize = 14.0; // default is libtxt text style fonst size
81     double heightScale = 1.0;
82     bool halfLeading = false;
83     bool heightOnly = false;
84     bool useLineStyle = false;
85 
86     FontWeight lineStyleFontWeight = FontWeight::W400;
87     FontWidth lineStyleFontWidth = FontWidth::NORMAL;
88     FontStyle lineStyleFontStyle = FontStyle::NORMAL;
89     std::vector<std::string> lineStyleFontFamilies;
90     double lineStyleFontSize = 14.0; // default is libtxt text style font size
91     double lineStyleHeightScale = 1.0;
92     bool lineStyleHeightOnlyInit = false;
93     bool lineStyleHeightOnly = false;
94     bool lineStyleHalfLeading = false;
95     double lineStyleSpacingScale = -1.0;
96     bool lineStyleOnly = false;
97 
98     TextAlign textAlign = TextAlign::START;
99     TextDirection textDirection = TextDirection::LTR;
100     size_t maxLines = 1e9;
101     std::u16string ellipsis;
102     std::string locale;
103 
104     BreakStrategy breakStrategy = BreakStrategy::GREEDY;
105     WordBreakType wordBreakType = WordBreakType::BREAK_WORD;
106     EllipsisModal ellipsisModal = EllipsisModal::TAIL;
107     float textSplitRatio = 0.5f;
108     float paragraphSpacing { 0.0f };
109     bool isEndAddParagraphSpacing { false };
110 
111     bool operator==(const TypographyStyle &rhs) const
112     {
113         return
114             this->ELLIPSIS == rhs.ELLIPSIS &&
115             this->fontWeight == rhs.fontWeight &&
116             this->fontStyle == rhs.fontStyle &&
117             this->fontFamily == rhs.fontFamily &&
118             skia::textlayout::nearlyEqual(this->fontSize, rhs.fontSize) &&
119             skia::textlayout::nearlyEqual(this->heightScale, rhs.heightScale) &&
120             this->halfLeading == rhs.halfLeading &&
121             this->heightOnly == rhs.heightOnly &&
122             this->useLineStyle == rhs.useLineStyle &&
123             this->lineStyleFontWidth == rhs.lineStyleFontWidth &&
124             this->lineStyleFontWeight == rhs.lineStyleFontWeight &&
125             this->lineStyleFontStyle == rhs.lineStyleFontStyle &&
126             this->lineStyleFontFamilies == rhs.lineStyleFontFamilies &&
127             skia::textlayout::nearlyEqual(this->lineStyleFontSize, rhs.lineStyleFontSize) &&
128             skia::textlayout::nearlyEqual(this->lineStyleHeightScale, rhs.lineStyleHeightScale) &&
129             this->lineStyleHeightOnlyInit == rhs.lineStyleHeightOnlyInit &&
130             this->lineStyleHeightOnly == rhs.lineStyleHeightOnly &&
131             this->lineStyleHalfLeading == rhs.lineStyleHalfLeading &&
132             skia::textlayout::nearlyEqual(this->lineStyleSpacingScale, rhs.lineStyleSpacingScale) &&
133             this->lineStyleOnly == rhs.lineStyleOnly &&
134             this->textAlign == rhs.textAlign &&
135             this->textDirection == rhs.textDirection &&
136             this->maxLines == rhs.maxLines &&
137             this->ellipsis == rhs.ellipsis &&
138             this->locale == rhs.locale &&
139             this->breakStrategy == rhs.breakStrategy &&
140             this->wordBreakType == rhs.wordBreakType &&
141             this->ellipsisModal == rhs.ellipsisModal &&
142             skia::textlayout::nearlyEqual(this->textSplitRatio, rhs.textSplitRatio) &&
143             this->defaultTextStyleUid == rhs.defaultTextStyleUid &&
144             this->tab == rhs.tab;
145     }
146     TextStyle GetTextStyle() const;
147     void SetTextStyle(TextStyle& textstyle);
148     TextAlign GetEffectiveAlign() const;
149     bool IsUnlimitedLines() const;
150     bool IsEllipsized() const;
EllipsizedTypographyStyle151     bool Ellipsized() const
152     {
153         return !ellipsis.empty();
154     }
155     TextStyle insideTextStyle;
156     bool customTextStyle = false;
157     TextHeightBehavior textHeightBehavior = TextHeightBehavior::ALL;
158     bool hintingIsOn = false;
159     TextTab tab;
160     std::bitset<static_cast<size_t>(RelayoutParagraphStyleAttribute::PARAGRAPH_STYLE_ATTRIBUTE_BUTT)>
161         relayoutChangeBitmap;
162     size_t defaultTextStyleUid { 0 };
163 };
164 } // namespace Rosen
165 } // namespace OHOS
166 #endif // ROSEN_TEXT_EXPORT_ROSEN_TEXT_TYPOGRAPHY_STYLE_H
167