• 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 enum class WordBreakType {
23     NORMAL,     // to be done.
24     BREAK_ALL,  // break occur after any characters.
25     BREAK_WORD, // break only occur after word.
26 };
27 
28 enum class LineBreakStrategy {
29     GREEDY,        // faster and linear.
30     HIGH_QUALITY,  // breaking tries to split the lines as efficiently as possible
31     BALANCED,      // breaking tries to make the line lengths even
32 };
33 
34 struct StrutStyle {
35     StrutStyle();
36 
getFontFamiliesStrutStyle37     const std::vector<SkString>& getFontFamilies() const { return fFontFamilies; }
setFontFamiliesStrutStyle38     void setFontFamilies(std::vector<SkString> families) { fFontFamilies = std::move(families); }
39 
40 #ifndef USE_SKIA_TXT
getFontStyleStrutStyle41     SkFontStyle getFontStyle() const { return fFontStyle; }
setFontStyleStrutStyle42     void setFontStyle(SkFontStyle fontStyle) { fFontStyle = fontStyle; }
43 #else
getFontStyleStrutStyle44     RSFontStyle getFontStyle() const { return fFontStyle; }
setFontStyleStrutStyle45     void setFontStyle(RSFontStyle fontStyle) { fFontStyle = fontStyle; }
46 #endif
47 
getFontSizeStrutStyle48     SkScalar getFontSize() const { return fFontSize; }
setFontSizeStrutStyle49     void setFontSize(SkScalar size) { fFontSize = size; }
50 
setHeightStrutStyle51     void setHeight(SkScalar height) { fHeight = height; }
getHeightStrutStyle52     SkScalar getHeight() const { return fHeight; }
53 
setLeadingStrutStyle54     void setLeading(SkScalar Leading) { fLeading = Leading; }
getLeadingStrutStyle55     SkScalar getLeading() const { return fLeading; }
56 
getStrutEnabledStrutStyle57     bool getStrutEnabled() const { return fEnabled; }
setStrutEnabledStrutStyle58     void setStrutEnabled(bool v) { fEnabled = v; }
59 
getForceStrutHeightStrutStyle60     bool getForceStrutHeight() const { return fForceHeight; }
setForceStrutHeightStrutStyle61     void setForceStrutHeight(bool v) { fForceHeight = v; }
62 
getHeightOverrideStrutStyle63     bool getHeightOverride() const { return fHeightOverride; }
setHeightOverrideStrutStyle64     void setHeightOverride(bool v) { fHeightOverride = v; }
65 
setHalfLeadingStrutStyle66     void setHalfLeading(bool halfLeading) { fHalfLeading = halfLeading; }
getHalfLeadingStrutStyle67     bool getHalfLeading() const { return fHalfLeading; }
68 
setWordBreakTypeStrutStyle69     void setWordBreakType(const WordBreakType& wordBreakType) { fWordBreakType = wordBreakType; }
getWordBreakTypeStrutStyle70     WordBreakType getWordBreakType() const { return fWordBreakType; }
71 
setLineBreakStrategyStrutStyle72     void setLineBreakStrategy(const LineBreakStrategy& lineBreakStrategy) { fLineBreakStrategy = lineBreakStrategy; }
getLineBreakStrategyStrutStyle73     LineBreakStrategy getLineBreakStrategy() const { return fLineBreakStrategy; }
74 
75     bool operator==(const StrutStyle& rhs) const {
76         return this->fEnabled == rhs.fEnabled &&
77                this->fHeightOverride == rhs.fHeightOverride &&
78                this->fForceHeight == rhs.fForceHeight &&
79                this->fHalfLeading == rhs.fHalfLeading &&
80                nearlyEqual(this->fLeading, rhs.fLeading) &&
81                nearlyEqual(this->fHeight, rhs.fHeight) &&
82                nearlyEqual(this->fFontSize, rhs.fFontSize) &&
83                this->fFontStyle == rhs.fFontStyle &&
84                this->fFontFamilies == rhs.fFontFamilies &&
85                this->fWordBreakType == rhs.fWordBreakType &&
86                this->fLineBreakStrategy == rhs.fLineBreakStrategy;
87     }
88 
89 private:
90 
91     std::vector<SkString> fFontFamilies;
92 #ifndef USE_SKIA_TXT
93     SkFontStyle fFontStyle;
94 #else
95     RSFontStyle fFontStyle;
96 #endif
97     SkScalar fFontSize;
98     SkScalar fHeight;
99     SkScalar fLeading;
100     bool fForceHeight;
101     bool fEnabled;
102     bool fHeightOverride;
103     // true: half leading.
104     // false: scale ascent/descent with fHeight.
105     bool fHalfLeading;
106     WordBreakType fWordBreakType;
107     LineBreakStrategy fLineBreakStrategy { LineBreakStrategy::GREEDY };
108 };
109 
110 struct ParagraphStyle {
111     ParagraphStyle();
112 
113     bool operator==(const ParagraphStyle& rhs) const {
114         return this->fHeight == rhs.fHeight &&
115                this->fEllipsis == rhs.fEllipsis &&
116                this->fEllipsisUtf16 == rhs.fEllipsisUtf16 &&
117                this->fTextDirection == rhs.fTextDirection && this->fTextAlign == rhs.fTextAlign &&
118                this->fDefaultTextStyle == rhs.fDefaultTextStyle &&
119                this->fEllipsisModal == rhs.fEllipsisModal &&
120                this->fTextOverflower == rhs.fTextOverflower &&
121                this->fReplaceTabCharacters == rhs.fReplaceTabCharacters &&
122                nearlyEqual(this->fTextSplitRatio, rhs.fTextSplitRatio);
123     }
124 
getStrutStyleParagraphStyle125     const StrutStyle& getStrutStyle() const { return fStrutStyle; }
setStrutStyleParagraphStyle126     void setStrutStyle(StrutStyle strutStyle) { fStrutStyle = std::move(strutStyle); }
127 
getTextStyleParagraphStyle128     const TextStyle& getTextStyle() const { return fDefaultTextStyle; }
setTextStyleParagraphStyle129     void setTextStyle(const TextStyle& textStyle) { fDefaultTextStyle = textStyle; }
130 
getTextDirectionParagraphStyle131     TextDirection getTextDirection() const { return fTextDirection; }
setTextDirectionParagraphStyle132     void setTextDirection(TextDirection direction) { fTextDirection = direction; }
133 
getTextAlignParagraphStyle134     TextAlign getTextAlign() const { return fTextAlign; }
setTextAlignParagraphStyle135     void setTextAlign(TextAlign align) { fTextAlign = align; }
136 
getMaxLinesParagraphStyle137     size_t getMaxLines() const { return fLinesLimit; }
setMaxLinesParagraphStyle138     void setMaxLines(size_t maxLines) { fLinesLimit = maxLines; }
139 
getEllipsisParagraphStyle140     SkString getEllipsis() const { return fEllipsis; }
getEllipsisUtf16ParagraphStyle141     std::u16string getEllipsisUtf16() const { return fEllipsisUtf16; }
setEllipsisParagraphStyle142     void setEllipsis(const std::u16string& ellipsis) {  fEllipsisUtf16 = ellipsis; }
setEllipsisParagraphStyle143     void setEllipsis(const SkString& ellipsis) { fEllipsis = ellipsis; }
144 
getHeightParagraphStyle145     SkScalar getHeight() const { return fHeight; }
setHeightParagraphStyle146     void setHeight(SkScalar height) { fHeight = height; }
147 
getTextHeightBehaviorParagraphStyle148     TextHeightBehavior getTextHeightBehavior() const { return fTextHeightBehavior; }
setTextHeightBehaviorParagraphStyle149     void setTextHeightBehavior(TextHeightBehavior v) { fTextHeightBehavior = v; }
150 
unlimited_linesParagraphStyle151     bool unlimited_lines() const {
152         return fLinesLimit == std::numeric_limits<size_t>::max();
153     }
ellipsizedParagraphStyle154     bool ellipsized() const { return !fEllipsis.isEmpty() || !fEllipsisUtf16.empty(); }
155     TextAlign effective_align() const;
hintingIsOnParagraphStyle156     bool hintingIsOn() const { return fHintingIsOn; }
turnHintingOffParagraphStyle157     void turnHintingOff() { fHintingIsOn = false; }
158 
getReplaceTabCharactersParagraphStyle159     bool getReplaceTabCharacters() const { return fReplaceTabCharacters; }
setReplaceTabCharactersParagraphStyle160     void setReplaceTabCharacters(bool value) { fReplaceTabCharacters = value; }
161 
getEllipsisModParagraphStyle162     skia::textlayout::EllipsisModal getEllipsisMod() const { return fEllipsisModal; }
setEllipsisModParagraphStyle163     void setEllipsisMod(skia::textlayout::EllipsisModal ellipsisModel) { fEllipsisModal = ellipsisModel; }
getTextSplitRatioParagraphStyle164     SkScalar getTextSplitRatio() const { return fTextSplitRatio; }
setTextSplitRatioParagraphStyle165     void setTextSplitRatio(SkScalar textSplitRatio) { fTextSplitRatio = textSplitRatio; }
166 
getTextOverflowerParagraphStyle167     bool getTextOverflower() const { return fTextOverflower; }
setTextOverflowerParagraphStyle168     void setTextOverflower(bool textOverflowerFlag) { fTextOverflower = textOverflowerFlag; }
169 private:
170     StrutStyle fStrutStyle;
171     TextStyle fDefaultTextStyle;
172     TextAlign fTextAlign;
173     TextDirection fTextDirection;
174     size_t fLinesLimit;
175     std::u16string fEllipsisUtf16;
176     SkString fEllipsis;
177     SkScalar fHeight;
178     TextHeightBehavior fTextHeightBehavior;
179     bool fHintingIsOn;
180     bool fReplaceTabCharacters;
181     bool fTextOverflower;
182     skia::textlayout::EllipsisModal fEllipsisModal;
183     SkScalar fTextSplitRatio = 0.5f;
184 };
185 }  // namespace textlayout
186 }  // namespace skia
187 
188 #endif  // ParagraphStyle_DEFINED
189