• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC.
2 #ifndef ParagraphStyle_DEFINED
3 #define ParagraphStyle_DEFINED
4 
5 #include "include/core/SkFontStyle.h"
6 #include "include/core/SkScalar.h"
7 #include "include/core/SkString.h"
8 #include "modules/skparagraph/include/DartTypes.h"
9 #include "modules/skparagraph/include/TextStyle.h"
10 #include "drawing.h"
11 
12 #include <stddef.h>
13 #include <algorithm>
14 #include <limits>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 namespace skia {
20 namespace textlayout {
21 
22 #ifdef OHOS_SUPPORT
23 struct TextTabs {
24     TextAlign alignment;
25     SkScalar location;
26     bool operator==(const TextTabs& other) const {
27         return (alignment == other.alignment) && nearlyEqual(location, other.location);
28     }
29 };
30 #endif
31 
32 enum class WordBreakType {
33     NORMAL,     // to be done.
34     BREAK_ALL,  // break occur after any characters.
35     BREAK_WORD, // break only occur after word.
36 #ifdef OHOS_SUPPORT
37     BREAK_HYPHEN,
38 #endif
39 };
40 
41 enum class LineBreakStrategy {
42     GREEDY,        // faster and linear.
43     HIGH_QUALITY,  // breaking tries to split the lines as efficiently as possible
44     BALANCED,      // breaking tries to make the line lengths even
45 };
46 
47 struct StrutStyle {
48     StrutStyle();
49 
getFontFamiliesStrutStyle50     const std::vector<SkString>& getFontFamilies() const { return fFontFamilies; }
setFontFamiliesStrutStyle51     void setFontFamilies(std::vector<SkString> families) { fFontFamilies = std::move(families); }
52 
53 #ifndef USE_SKIA_TXT
getFontStyleStrutStyle54     SkFontStyle getFontStyle() const { return fFontStyle; }
setFontStyleStrutStyle55     void setFontStyle(SkFontStyle fontStyle) { fFontStyle = fontStyle; }
56 #else
getFontStyleStrutStyle57     RSFontStyle getFontStyle() const { return fFontStyle; }
setFontStyleStrutStyle58     void setFontStyle(RSFontStyle fontStyle) { fFontStyle = fontStyle; }
59 #endif
60 
getFontSizeStrutStyle61     SkScalar getFontSize() const { return fFontSize; }
setFontSizeStrutStyle62     void setFontSize(SkScalar size) { fFontSize = size; }
63 
setHeightStrutStyle64     void setHeight(SkScalar height) { fHeight = height; }
getHeightStrutStyle65     SkScalar getHeight() const { return fHeight; }
66 
setLeadingStrutStyle67     void setLeading(SkScalar Leading) { fLeading = Leading; }
getLeadingStrutStyle68     SkScalar getLeading() const { return fLeading; }
69 
getStrutEnabledStrutStyle70     bool getStrutEnabled() const { return fEnabled; }
setStrutEnabledStrutStyle71     void setStrutEnabled(bool v) { fEnabled = v; }
72 
getForceStrutHeightStrutStyle73     bool getForceStrutHeight() const { return fForceHeight; }
setForceStrutHeightStrutStyle74     void setForceStrutHeight(bool v) { fForceHeight = v; }
75 
getHeightOverrideStrutStyle76     bool getHeightOverride() const { return fHeightOverride; }
setHeightOverrideStrutStyle77     void setHeightOverride(bool v) { fHeightOverride = v; }
78 
setHalfLeadingStrutStyle79     void setHalfLeading(bool halfLeading) { fHalfLeading = halfLeading; }
getHalfLeadingStrutStyle80     bool getHalfLeading() const { return fHalfLeading; }
81 
setWordBreakTypeStrutStyle82     void setWordBreakType(const WordBreakType& wordBreakType) { fWordBreakType = wordBreakType; }
getWordBreakTypeStrutStyle83     WordBreakType getWordBreakType() const { return fWordBreakType; }
84 
setLineBreakStrategyStrutStyle85     void setLineBreakStrategy(const LineBreakStrategy& lineBreakStrategy) { fLineBreakStrategy = lineBreakStrategy; }
getLineBreakStrategyStrutStyle86     LineBreakStrategy getLineBreakStrategy() const { return fLineBreakStrategy; }
87 
88     bool operator==(const StrutStyle& rhs) const {
89         return this->fEnabled == rhs.fEnabled &&
90                this->fHeightOverride == rhs.fHeightOverride &&
91                this->fForceHeight == rhs.fForceHeight &&
92                this->fHalfLeading == rhs.fHalfLeading &&
93                nearlyEqual(this->fLeading, rhs.fLeading) &&
94                nearlyEqual(this->fHeight, rhs.fHeight) &&
95                nearlyEqual(this->fFontSize, rhs.fFontSize) &&
96                this->fFontStyle == rhs.fFontStyle &&
97                this->fFontFamilies == rhs.fFontFamilies &&
98                this->fWordBreakType == rhs.fWordBreakType &&
99                this->fLineBreakStrategy == rhs.fLineBreakStrategy;
100     }
101 
102 private:
103 
104     std::vector<SkString> fFontFamilies;
105 #ifndef USE_SKIA_TXT
106     SkFontStyle fFontStyle;
107 #else
108     RSFontStyle fFontStyle;
109 #endif
110     SkScalar fFontSize;
111     SkScalar fHeight;
112     SkScalar fLeading;
113     bool fForceHeight;
114     bool fEnabled;
115     bool fHeightOverride;
116     // true: half leading.
117     // false: scale ascent/descent with fHeight.
118     bool fHalfLeading;
119     WordBreakType fWordBreakType;
120     LineBreakStrategy fLineBreakStrategy { LineBreakStrategy::GREEDY };
121 };
122 
123 struct ParagraphStyle {
124     ParagraphStyle();
125 
126     bool operator==(const ParagraphStyle& rhs) const {
127         return this->fHeight == rhs.fHeight &&
128                this->fEllipsis == rhs.fEllipsis &&
129                this->fEllipsisUtf16 == rhs.fEllipsisUtf16 &&
130                this->fTextDirection == rhs.fTextDirection && this->fTextAlign == rhs.fTextAlign &&
131                this->fDefaultTextStyle == rhs.fDefaultTextStyle &&
132                this->fEllipsisModal == rhs.fEllipsisModal &&
133                this->fTextOverflower == rhs.fTextOverflower &&
134                this->fReplaceTabCharacters == rhs.fReplaceTabCharacters &&
135 #ifdef OHOS_SUPPORT
136                this->fTextTab == rhs.fTextTab &&
137                this->fParagraphSpacing == rhs.fParagraphSpacing &&
138                this->fIsEndAddParagraphSpacing == rhs.fIsEndAddParagraphSpacing &&
139 #endif
140                nearlyEqual(this->fTextSplitRatio, rhs.fTextSplitRatio);
141     }
142 
143 #ifdef OHOS_SUPPORT
exportStrutStyleParagraphStyle144     StrutStyle& exportStrutStyle() { return fStrutStyle; }
exportTextStyleParagraphStyle145     TextStyle& exportTextStyle() { return fDefaultTextStyle; }
146 #endif
147 
getStrutStyleParagraphStyle148     const StrutStyle& getStrutStyle() const { return fStrutStyle; }
setStrutStyleParagraphStyle149     void setStrutStyle(StrutStyle strutStyle) { fStrutStyle = std::move(strutStyle); }
150 
getTextStyleParagraphStyle151     const TextStyle& getTextStyle() const { return fDefaultTextStyle; }
setTextStyleParagraphStyle152     void setTextStyle(const TextStyle& textStyle) { fDefaultTextStyle = textStyle; }
153 
getTextDirectionParagraphStyle154     TextDirection getTextDirection() const { return fTextDirection; }
setTextDirectionParagraphStyle155     void setTextDirection(TextDirection direction) { fTextDirection = direction; }
156 
getTextAlignParagraphStyle157     TextAlign getTextAlign() const { return fTextAlign; }
setTextAlignParagraphStyle158     void setTextAlign(TextAlign align) { fTextAlign = align; }
159 
getMaxLinesParagraphStyle160     size_t getMaxLines() const { return fLinesLimit; }
setMaxLinesParagraphStyle161     void setMaxLines(size_t maxLines) { fLinesLimit = maxLines; }
162 
getEllipsisParagraphStyle163     SkString getEllipsis() const { return fEllipsis; }
getEllipsisUtf16ParagraphStyle164     std::u16string getEllipsisUtf16() const { return fEllipsisUtf16; }
setEllipsisParagraphStyle165     void setEllipsis(const std::u16string& ellipsis) {  fEllipsisUtf16 = ellipsis; }
setEllipsisParagraphStyle166     void setEllipsis(const SkString& ellipsis) { fEllipsis = ellipsis; }
167 
getHeightParagraphStyle168     SkScalar getHeight() const { return fHeight; }
setHeightParagraphStyle169     void setHeight(SkScalar height) { fHeight = height; }
170 
getTextHeightBehaviorParagraphStyle171     TextHeightBehavior getTextHeightBehavior() const { return fTextHeightBehavior; }
setTextHeightBehaviorParagraphStyle172     void setTextHeightBehavior(TextHeightBehavior v) { fTextHeightBehavior = v; }
173 
unlimited_linesParagraphStyle174     bool unlimited_lines() const {
175         return fLinesLimit == std::numeric_limits<size_t>::max();
176     }
ellipsizedParagraphStyle177     bool ellipsized() const { return !fEllipsis.isEmpty() || !fEllipsisUtf16.empty(); }
178     TextAlign effective_align() const;
hintingIsOnParagraphStyle179     bool hintingIsOn() const { return fHintingIsOn; }
turnHintingOffParagraphStyle180     void turnHintingOff() { fHintingIsOn = false; }
181 
getReplaceTabCharactersParagraphStyle182     bool getReplaceTabCharacters() const { return fReplaceTabCharacters; }
setReplaceTabCharactersParagraphStyle183     void setReplaceTabCharacters(bool value) { fReplaceTabCharacters = value; }
184 
getEllipsisModParagraphStyle185     skia::textlayout::EllipsisModal getEllipsisMod() const { return fEllipsisModal; }
setEllipsisModParagraphStyle186     void setEllipsisMod(skia::textlayout::EllipsisModal ellipsisModel) { fEllipsisModal = ellipsisModel; }
getTextSplitRatioParagraphStyle187     SkScalar getTextSplitRatio() const { return fTextSplitRatio; }
setTextSplitRatioParagraphStyle188     void setTextSplitRatio(SkScalar textSplitRatio) { fTextSplitRatio = textSplitRatio; }
189 
getTextOverflowerParagraphStyle190     bool getTextOverflower() const { return fTextOverflower; }
setTextOverflowerParagraphStyle191     void setTextOverflower(bool textOverflowerFlag) { fTextOverflower = textOverflowerFlag; }
192 
193 #ifdef OHOS_SUPPORT
getTextTabParagraphStyle194     const TextTabs& getTextTab() const { return fTextTab; }
setTextTabParagraphStyle195     void setTextTab(const TextTabs& textTab) { fTextTab = textTab; }
getParagraphSpacingParagraphStyle196     SkScalar getParagraphSpacing() const { return fParagraphSpacing; }
setParagraphSpacingParagraphStyle197     void setParagraphSpacing(SkScalar paragraphSpacing)
198     {
199         fParagraphSpacing = paragraphSpacing;
200     }
getIsEndAddParagraphSpacingParagraphStyle201     bool getIsEndAddParagraphSpacing() const { return fIsEndAddParagraphSpacing; }
setIsEndAddParagraphSpacingParagraphStyle202     void setIsEndAddParagraphSpacing(bool isEndAddParagraphSpacing)
203     {
204         fIsEndAddParagraphSpacing = isEndAddParagraphSpacing;
205     }
206 #endif
207 private:
208     StrutStyle fStrutStyle;
209     TextStyle fDefaultTextStyle;
210     TextAlign fTextAlign;
211     TextDirection fTextDirection;
212     size_t fLinesLimit;
213     std::u16string fEllipsisUtf16;
214     SkString fEllipsis;
215     SkScalar fHeight;
216     TextHeightBehavior fTextHeightBehavior;
217     bool fHintingIsOn;
218     bool fReplaceTabCharacters;
219     bool fTextOverflower;
220     skia::textlayout::EllipsisModal fEllipsisModal;
221     SkScalar fTextSplitRatio = 0.5f;
222 #ifdef OHOS_SUPPORT
223     TextTabs fTextTab;
224     SkScalar fParagraphSpacing { 0.0f };
225     bool fIsEndAddParagraphSpacing { false };
226 #endif
227 };
228 }  // namespace textlayout
229 }  // namespace skia
230 
231 #endif  // ParagraphStyle_DEFINED
232