Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
| 2 | #ifndef TextStyle_DEFINED |
| 3 | #define TextStyle_DEFINED |
| 4 | |
| 5 | #include <vector> |
| 6 | #include "modules/skparagraph/include/DartTypes.h" |
| 7 | #include "modules/skparagraph/include/TextShadow.h" |
| 8 | #include "include/core/SkColor.h" |
| 9 | #include "include/core/SkFont.h" |
| 10 | #include "include/core/SkFontMetrics.h" |
| 11 | #include "include/core/SkFontStyle.h" |
| 12 | #include "include/core/SkPaint.h" |
| 13 | |
| 14 | // TODO: Make it external so the other platforms (Android) could use it |
| 15 | #define DEFAULT_FONT_FAMILY "sans-serif" |
| 16 | |
| 17 | namespace skia { |
| 18 | namespace textlayout { |
| 19 | |
| 20 | // Multiple decorations can be applied at once. Ex: Underline and overline is |
| 21 | // (0x1 | 0x2) |
| 22 | enum TextDecoration { |
| 23 | kNoDecoration = 0x0, |
| 24 | kUnderline = 0x1, |
| 25 | kOverline = 0x2, |
| 26 | kLineThrough = 0x4, |
| 27 | }; |
| 28 | constexpr std::initializer_list<TextDecoration> AllTextDecorations = { |
| 29 | kNoDecoration, |
| 30 | kUnderline, |
| 31 | kOverline, |
| 32 | kLineThrough, |
| 33 | }; |
| 34 | |
| 35 | enum TextDecorationStyle { kSolid, kDouble, kDotted, kDashed, kWavy }; |
| 36 | |
| 37 | enum StyleType { |
| 38 | kAllAttributes, |
| 39 | kText, |
| 40 | kFont, |
| 41 | kForeground, |
| 42 | kBackground, |
| 43 | kShadow, |
| 44 | kDecorations, |
| 45 | kLetterSpacing, |
| 46 | kWordSpacing |
| 47 | }; |
| 48 | |
| 49 | class TextStyle { |
| 50 | public: |
| 51 | TextStyle(); |
| 52 | ~TextStyle() = default; |
| 53 | |
| 54 | bool equals(const TextStyle& other) const; |
| 55 | bool matchOneAttribute(StyleType styleType, const TextStyle& other) const; |
| 56 | bool operator==(const TextStyle& rhs) const { return this->equals(rhs); } |
| 57 | |
| 58 | // Colors |
| 59 | SkColor getColor() const { return fColor; } |
| 60 | void setColor(SkColor color) { fColor = color; } |
| 61 | |
| 62 | bool hasForeground() const { return fHasForeground; } |
| 63 | SkPaint getForeground() const { return fForeground; } |
| 64 | void setForegroundColor(SkPaint paint) { |
| 65 | fHasForeground = true; |
| 66 | fForeground = std::move(paint); |
| 67 | } |
| 68 | void clearForegroundColor() { fHasForeground = false; } |
| 69 | |
| 70 | bool hasBackground() const { return fHasBackground; } |
| 71 | SkPaint getBackground() const { return fBackground; } |
| 72 | void setBackgroundColor(SkPaint paint) { |
| 73 | fHasBackground = true; |
| 74 | fBackground = std::move(paint); |
| 75 | } |
| 76 | void clearBackgroundColor() { fHasBackground = false; } |
| 77 | |
| 78 | // Decorations |
| 79 | TextDecoration getDecoration() const { return fDecoration; } |
| 80 | SkColor getDecorationColor() const { return fDecorationColor; } |
| 81 | TextDecorationStyle getDecorationStyle() const { return fDecorationStyle; } |
| 82 | SkScalar getDecorationThicknessMultiplier() const { |
| 83 | return fDecorationThicknessMultiplier; |
| 84 | } |
| 85 | void setDecoration(TextDecoration decoration) { fDecoration = decoration; } |
| 86 | void setDecorationStyle(TextDecorationStyle style) { fDecorationStyle = style; } |
| 87 | void setDecorationColor(SkColor color) { fDecorationColor = color; } |
| 88 | void setDecorationThicknessMultiplier(SkScalar m) { fDecorationThicknessMultiplier = m; } |
| 89 | |
| 90 | // Weight/Width/Slant |
| 91 | SkFontStyle getFontStyle() const { return fFontStyle; } |
| 92 | void setFontStyle(SkFontStyle fontStyle) { fFontStyle = fontStyle; } |
| 93 | |
| 94 | // Shadows |
| 95 | size_t getShadowNumber() const { return fTextShadows.size(); } |
| 96 | std::vector<TextShadow> getShadows() const { return fTextShadows; } |
| 97 | void addShadow(TextShadow shadow) { fTextShadows.emplace_back(shadow); } |
| 98 | void resetShadows() { fTextShadows.clear(); } |
| 99 | |
| 100 | SkScalar getFontSize() const { return fFontSize; } |
| 101 | void setFontSize(SkScalar size) { fFontSize = size; } |
| 102 | |
| 103 | const std::vector<SkString>& getFontFamilies() const { return fFontFamilies; } |
| 104 | void setFontFamilies(std::vector<SkString> families) { |
| 105 | fFontFamilies = std::move(families); |
| 106 | } |
| 107 | |
| 108 | void setHeight(SkScalar height) { fHeight = height; } |
| 109 | SkScalar getHeight() const { return fHeight; } |
| 110 | |
| 111 | void setLetterSpacing(SkScalar letterSpacing) { fLetterSpacing = letterSpacing; } |
| 112 | SkScalar getLetterSpacing() const { return fLetterSpacing; } |
| 113 | |
| 114 | void setWordSpacing(SkScalar wordSpacing) { fWordSpacing = wordSpacing; } |
| 115 | SkScalar getWordSpacing() const { return fWordSpacing; } |
| 116 | |
| 117 | SkTypeface* getTypeface() const { return fTypeface.get(); } |
| 118 | sk_sp<SkTypeface> refTypeface() const { return fTypeface; } |
| 119 | void setTypeface(sk_sp<SkTypeface> typeface) { fTypeface = std::move(typeface); } |
| 120 | |
| 121 | SkString getLocale() const { return fLocale; } |
| 122 | void setLocale(const SkString& locale) { fLocale = locale; } |
| 123 | |
| 124 | TextBaseline getTextBaseline() const { return fTextBaseline; } |
| 125 | void setTextBaseline(TextBaseline baseline) { fTextBaseline = baseline; } |
| 126 | |
| 127 | // TODO: Not to use SkFontMetrics class (it has different purpose and meaning) |
| 128 | void getFontMetrics(SkFontMetrics* metrics) const { |
| 129 | SkFont font(fTypeface, fFontSize); |
| 130 | font.getMetrics(metrics); |
| 131 | metrics->fAscent = |
| 132 | (metrics->fAscent - metrics->fLeading / 2) * (fHeight == 0 ? 1 : fHeight); |
| 133 | metrics->fDescent = |
| 134 | (metrics->fDescent + metrics->fLeading / 2) * (fHeight == 0 ? 1 : fHeight); |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | TextDecoration fDecoration; |
| 139 | SkColor fDecorationColor; |
| 140 | TextDecorationStyle fDecorationStyle; |
| 141 | SkScalar fDecorationThicknessMultiplier; |
| 142 | |
| 143 | SkFontStyle fFontStyle; |
| 144 | |
| 145 | std::vector<SkString> fFontFamilies; |
| 146 | SkScalar fFontSize; |
| 147 | |
| 148 | SkScalar fHeight; |
| 149 | SkString fLocale; |
| 150 | SkScalar fLetterSpacing; |
| 151 | SkScalar fWordSpacing; |
| 152 | |
| 153 | TextBaseline fTextBaseline; |
| 154 | |
| 155 | SkColor fColor; |
| 156 | bool fHasBackground; |
| 157 | SkPaint fBackground; |
| 158 | bool fHasForeground; |
| 159 | SkPaint fForeground; |
| 160 | |
| 161 | std::vector<TextShadow> fTextShadows; |
| 162 | |
| 163 | sk_sp<SkTypeface> fTypeface; |
| 164 | }; |
| 165 | } // namespace textlayout |
| 166 | } // namespace skia |
| 167 | |
| 168 | #endif // TextStyle_DEFINED |