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 11 #include <stddef.h> 12 #include <algorithm> 13 #include <limits> 14 #include <string> 15 #include <utility> 16 #include <vector> 17 18 namespace skia { 19 namespace textlayout { 20 21 enum class WordBreakType { 22 NORMAL, // to be done. 23 BREAK_ALL, // break occur after any characters. 24 BREAK_WORD, // break only occur after word. 25 }; 26 27 struct StrutStyle { 28 StrutStyle(); 29 getFontFamiliesStrutStyle30 const std::vector<SkString>& getFontFamilies() const { return fFontFamilies; } setFontFamiliesStrutStyle31 void setFontFamilies(std::vector<SkString> families) { fFontFamilies = std::move(families); } 32 getFontStyleStrutStyle33 SkFontStyle getFontStyle() const { return fFontStyle; } setFontStyleStrutStyle34 void setFontStyle(SkFontStyle fontStyle) { fFontStyle = fontStyle; } 35 getFontSizeStrutStyle36 SkScalar getFontSize() const { return fFontSize; } setFontSizeStrutStyle37 void setFontSize(SkScalar size) { fFontSize = size; } 38 setHeightStrutStyle39 void setHeight(SkScalar height) { fHeight = height; } getHeightStrutStyle40 SkScalar getHeight() const { return fHeight; } 41 setLeadingStrutStyle42 void setLeading(SkScalar Leading) { fLeading = Leading; } getLeadingStrutStyle43 SkScalar getLeading() const { return fLeading; } 44 getStrutEnabledStrutStyle45 bool getStrutEnabled() const { return fEnabled; } setStrutEnabledStrutStyle46 void setStrutEnabled(bool v) { fEnabled = v; } 47 getForceStrutHeightStrutStyle48 bool getForceStrutHeight() const { return fForceHeight; } setForceStrutHeightStrutStyle49 void setForceStrutHeight(bool v) { fForceHeight = v; } 50 getHeightOverrideStrutStyle51 bool getHeightOverride() const { return fHeightOverride; } setHeightOverrideStrutStyle52 void setHeightOverride(bool v) { fHeightOverride = v; } 53 setHalfLeadingStrutStyle54 void setHalfLeading(bool halfLeading) { fHalfLeading = halfLeading; } getHalfLeadingStrutStyle55 bool getHalfLeading() const { return fHalfLeading; } 56 setWordBreakTypeStrutStyle57 void setWordBreakType(const WordBreakType& wordBreakType) { fWordBreakType = wordBreakType; } getWordBreakTypeStrutStyle58 WordBreakType getWordBreakType() const { return fWordBreakType; } 59 60 bool operator==(const StrutStyle& rhs) const { 61 return this->fEnabled == rhs.fEnabled && 62 this->fHeightOverride == rhs.fHeightOverride && 63 this->fForceHeight == rhs.fForceHeight && 64 this->fHalfLeading == rhs.fHalfLeading && 65 nearlyEqual(this->fLeading, rhs.fLeading) && 66 nearlyEqual(this->fHeight, rhs.fHeight) && 67 nearlyEqual(this->fFontSize, rhs.fFontSize) && 68 this->fFontStyle == rhs.fFontStyle && 69 this->fFontFamilies == rhs.fFontFamilies && 70 this->fWordBreakType == rhs.fWordBreakType; 71 } 72 73 private: 74 75 std::vector<SkString> fFontFamilies; 76 SkFontStyle fFontStyle; 77 SkScalar fFontSize; 78 SkScalar fHeight; 79 SkScalar fLeading; 80 bool fForceHeight; 81 bool fEnabled; 82 bool fHeightOverride; 83 // true: half leading. 84 // false: scale ascent/descent with fHeight. 85 bool fHalfLeading; 86 WordBreakType fWordBreakType; 87 }; 88 89 struct ParagraphStyle { 90 ParagraphStyle(); 91 92 bool operator==(const ParagraphStyle& rhs) const { 93 return this->fHeight == rhs.fHeight && 94 this->fEllipsis == rhs.fEllipsis && 95 this->fEllipsisUtf16 == rhs.fEllipsisUtf16 && 96 this->fTextDirection == rhs.fTextDirection && this->fTextAlign == rhs.fTextAlign && 97 this->fDefaultTextStyle == rhs.fDefaultTextStyle && 98 this->fEllipsisModal == rhs.fEllipsisModal && 99 this->fTextOverflower == rhs.fTextOverflower && 100 this->fReplaceTabCharacters == rhs.fReplaceTabCharacters && 101 nearlyEqual(this->fTextSplitRatio, rhs.fTextSplitRatio); 102 } 103 getStrutStyleParagraphStyle104 const StrutStyle& getStrutStyle() const { return fStrutStyle; } setStrutStyleParagraphStyle105 void setStrutStyle(StrutStyle strutStyle) { fStrutStyle = std::move(strutStyle); } 106 getTextStyleParagraphStyle107 const TextStyle& getTextStyle() const { return fDefaultTextStyle; } setTextStyleParagraphStyle108 void setTextStyle(const TextStyle& textStyle) { fDefaultTextStyle = textStyle; } 109 getTextDirectionParagraphStyle110 TextDirection getTextDirection() const { return fTextDirection; } setTextDirectionParagraphStyle111 void setTextDirection(TextDirection direction) { fTextDirection = direction; } 112 getTextAlignParagraphStyle113 TextAlign getTextAlign() const { return fTextAlign; } setTextAlignParagraphStyle114 void setTextAlign(TextAlign align) { fTextAlign = align; } 115 getMaxLinesParagraphStyle116 size_t getMaxLines() const { return fLinesLimit; } setMaxLinesParagraphStyle117 void setMaxLines(size_t maxLines) { fLinesLimit = maxLines; } 118 getEllipsisParagraphStyle119 SkString getEllipsis() const { return fEllipsis; } getEllipsisUtf16ParagraphStyle120 std::u16string getEllipsisUtf16() const { return fEllipsisUtf16; } setEllipsisParagraphStyle121 void setEllipsis(const std::u16string& ellipsis) { fEllipsisUtf16 = ellipsis; } setEllipsisParagraphStyle122 void setEllipsis(const SkString& ellipsis) { fEllipsis = ellipsis; } 123 getHeightParagraphStyle124 SkScalar getHeight() const { return fHeight; } setHeightParagraphStyle125 void setHeight(SkScalar height) { fHeight = height; } 126 getTextHeightBehaviorParagraphStyle127 TextHeightBehavior getTextHeightBehavior() const { return fTextHeightBehavior; } setTextHeightBehaviorParagraphStyle128 void setTextHeightBehavior(TextHeightBehavior v) { fTextHeightBehavior = v; } 129 unlimited_linesParagraphStyle130 bool unlimited_lines() const { 131 return fLinesLimit == std::numeric_limits<size_t>::max(); 132 } ellipsizedParagraphStyle133 bool ellipsized() const { return !fEllipsis.isEmpty() || !fEllipsisUtf16.empty(); } 134 TextAlign effective_align() const; hintingIsOnParagraphStyle135 bool hintingIsOn() const { return fHintingIsOn; } turnHintingOffParagraphStyle136 void turnHintingOff() { fHintingIsOn = false; } 137 getReplaceTabCharactersParagraphStyle138 bool getReplaceTabCharacters() const { return fReplaceTabCharacters; } setReplaceTabCharactersParagraphStyle139 void setReplaceTabCharacters(bool value) { fReplaceTabCharacters = value; } 140 getEllipsisModParagraphStyle141 skia::textlayout::EllipsisModal getEllipsisMod() const { return fEllipsisModal; } setEllipsisModParagraphStyle142 void setEllipsisMod(skia::textlayout::EllipsisModal ellipsisModel) { fEllipsisModal = ellipsisModel; } getTextSplitRatioParagraphStyle143 SkScalar getTextSplitRatio() const { return fTextSplitRatio; } setTextSplitRatioParagraphStyle144 void setTextSplitRatio(SkScalar textSplitRatio) { fTextSplitRatio = textSplitRatio; } 145 getTextOverflowerParagraphStyle146 bool getTextOverflower() const { return fTextOverflower; } setTextOverflowerParagraphStyle147 void setTextOverflower(bool textOverflowerFlag) { fTextOverflower = textOverflowerFlag; } 148 private: 149 StrutStyle fStrutStyle; 150 TextStyle fDefaultTextStyle; 151 TextAlign fTextAlign; 152 TextDirection fTextDirection; 153 size_t fLinesLimit; 154 std::u16string fEllipsisUtf16; 155 SkString fEllipsis; 156 SkScalar fHeight; 157 TextHeightBehavior fTextHeightBehavior; 158 bool fHintingIsOn; 159 bool fReplaceTabCharacters; 160 bool fTextOverflower; 161 skia::textlayout::EllipsisModal fEllipsisModal; 162 SkScalar fTextSplitRatio = 0.5f; 163 }; 164 } // namespace textlayout 165 } // namespace skia 166 167 #endif // ParagraphStyle_DEFINED 168