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> |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 6 | #include "include/core/SkColor.h" |
| 7 | #include "include/core/SkFont.h" |
| 8 | #include "include/core/SkFontMetrics.h" |
| 9 | #include "include/core/SkFontStyle.h" |
| 10 | #include "include/core/SkPaint.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 11 | #include "modules/skparagraph/include/DartTypes.h" |
| 12 | #include "modules/skparagraph/include/TextShadow.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 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 | }; |
Julia Lavrova | b5c69a4 | 2019-07-11 09:34:39 -0400 | [diff] [blame] | 28 | constexpr TextDecoration AllTextDecorations[] = { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 29 | kNoDecoration, |
| 30 | kUnderline, |
| 31 | kOverline, |
| 32 | kLineThrough, |
| 33 | }; |
| 34 | |
| 35 | enum TextDecorationStyle { kSolid, kDouble, kDotted, kDashed, kWavy }; |
| 36 | |
| 37 | enum StyleType { |
| 38 | kAllAttributes, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 39 | kFont, |
| 40 | kForeground, |
| 41 | kBackground, |
| 42 | kShadow, |
| 43 | kDecorations, |
| 44 | kLetterSpacing, |
| 45 | kWordSpacing |
| 46 | }; |
| 47 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 48 | struct Decoration { |
| 49 | TextDecoration fType; |
| 50 | SkColor fColor; |
| 51 | TextDecorationStyle fStyle; |
| 52 | SkScalar fThicknessMultiplier; |
| 53 | |
| 54 | bool operator==(const Decoration& other) const { |
| 55 | return this->fType == other.fType && |
| 56 | this->fColor == other.fColor && |
| 57 | this->fStyle == other.fStyle && |
| 58 | this->fThicknessMultiplier == other.fThicknessMultiplier; |
| 59 | } |
| 60 | }; |
| 61 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 62 | class TextStyle { |
| 63 | public: |
| 64 | TextStyle(); |
| 65 | ~TextStyle() = default; |
| 66 | |
| 67 | bool equals(const TextStyle& other) const; |
| 68 | bool matchOneAttribute(StyleType styleType, const TextStyle& other) const; |
| 69 | bool operator==(const TextStyle& rhs) const { return this->equals(rhs); } |
| 70 | |
| 71 | // Colors |
| 72 | SkColor getColor() const { return fColor; } |
| 73 | void setColor(SkColor color) { fColor = color; } |
| 74 | |
| 75 | bool hasForeground() const { return fHasForeground; } |
| 76 | SkPaint getForeground() const { return fForeground; } |
| 77 | void setForegroundColor(SkPaint paint) { |
| 78 | fHasForeground = true; |
| 79 | fForeground = std::move(paint); |
| 80 | } |
| 81 | void clearForegroundColor() { fHasForeground = false; } |
| 82 | |
| 83 | bool hasBackground() const { return fHasBackground; } |
| 84 | SkPaint getBackground() const { return fBackground; } |
| 85 | void setBackgroundColor(SkPaint paint) { |
| 86 | fHasBackground = true; |
| 87 | fBackground = std::move(paint); |
| 88 | } |
| 89 | void clearBackgroundColor() { fHasBackground = false; } |
| 90 | |
| 91 | // Decorations |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 92 | Decoration getDecoration() const { return fDecoration; } |
| 93 | TextDecoration getDecorationType() const { return fDecoration.fType; } |
| 94 | SkColor getDecorationColor() const { return fDecoration.fColor; } |
| 95 | TextDecorationStyle getDecorationStyle() const { return fDecoration.fStyle; } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 96 | SkScalar getDecorationThicknessMultiplier() const { |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 97 | return fDecoration.fThicknessMultiplier; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 98 | } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 99 | void setDecoration(TextDecoration decoration) { fDecoration.fType = decoration; } |
| 100 | void setDecorationStyle(TextDecorationStyle style) { fDecoration.fStyle = style; } |
| 101 | void setDecorationColor(SkColor color) { fDecoration.fColor = color; } |
| 102 | void setDecorationThicknessMultiplier(SkScalar m) { fDecoration.fThicknessMultiplier = m; } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 103 | |
| 104 | // Weight/Width/Slant |
| 105 | SkFontStyle getFontStyle() const { return fFontStyle; } |
| 106 | void setFontStyle(SkFontStyle fontStyle) { fFontStyle = fontStyle; } |
| 107 | |
| 108 | // Shadows |
| 109 | size_t getShadowNumber() const { return fTextShadows.size(); } |
| 110 | std::vector<TextShadow> getShadows() const { return fTextShadows; } |
| 111 | void addShadow(TextShadow shadow) { fTextShadows.emplace_back(shadow); } |
| 112 | void resetShadows() { fTextShadows.clear(); } |
| 113 | |
| 114 | SkScalar getFontSize() const { return fFontSize; } |
| 115 | void setFontSize(SkScalar size) { fFontSize = size; } |
| 116 | |
| 117 | const std::vector<SkString>& getFontFamilies() const { return fFontFamilies; } |
| 118 | void setFontFamilies(std::vector<SkString> families) { |
| 119 | fFontFamilies = std::move(families); |
| 120 | } |
| 121 | |
| 122 | void setHeight(SkScalar height) { fHeight = height; } |
| 123 | SkScalar getHeight() const { return fHeight; } |
| 124 | |
| 125 | void setLetterSpacing(SkScalar letterSpacing) { fLetterSpacing = letterSpacing; } |
| 126 | SkScalar getLetterSpacing() const { return fLetterSpacing; } |
| 127 | |
| 128 | void setWordSpacing(SkScalar wordSpacing) { fWordSpacing = wordSpacing; } |
| 129 | SkScalar getWordSpacing() const { return fWordSpacing; } |
| 130 | |
| 131 | SkTypeface* getTypeface() const { return fTypeface.get(); } |
| 132 | sk_sp<SkTypeface> refTypeface() const { return fTypeface; } |
| 133 | void setTypeface(sk_sp<SkTypeface> typeface) { fTypeface = std::move(typeface); } |
| 134 | |
| 135 | SkString getLocale() const { return fLocale; } |
| 136 | void setLocale(const SkString& locale) { fLocale = locale; } |
| 137 | |
| 138 | TextBaseline getTextBaseline() const { return fTextBaseline; } |
| 139 | void setTextBaseline(TextBaseline baseline) { fTextBaseline = baseline; } |
| 140 | |
| 141 | // TODO: Not to use SkFontMetrics class (it has different purpose and meaning) |
| 142 | void getFontMetrics(SkFontMetrics* metrics) const { |
| 143 | SkFont font(fTypeface, fFontSize); |
| 144 | font.getMetrics(metrics); |
| 145 | metrics->fAscent = |
| 146 | (metrics->fAscent - metrics->fLeading / 2) * (fHeight == 0 ? 1 : fHeight); |
| 147 | metrics->fDescent = |
| 148 | (metrics->fDescent + metrics->fLeading / 2) * (fHeight == 0 ? 1 : fHeight); |
| 149 | } |
| 150 | |
| 151 | private: |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 152 | Decoration fDecoration; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 153 | |
| 154 | SkFontStyle fFontStyle; |
| 155 | |
| 156 | std::vector<SkString> fFontFamilies; |
| 157 | SkScalar fFontSize; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 158 | SkScalar fHeight; |
| 159 | SkString fLocale; |
| 160 | SkScalar fLetterSpacing; |
| 161 | SkScalar fWordSpacing; |
| 162 | |
| 163 | TextBaseline fTextBaseline; |
| 164 | |
| 165 | SkColor fColor; |
| 166 | bool fHasBackground; |
| 167 | SkPaint fBackground; |
| 168 | bool fHasForeground; |
| 169 | SkPaint fForeground; |
| 170 | |
| 171 | std::vector<TextShadow> fTextShadows; |
| 172 | |
| 173 | sk_sp<SkTypeface> fTypeface; |
| 174 | }; |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 175 | |
| 176 | typedef size_t TextIndex; |
| 177 | typedef SkRange<size_t> TextRange; |
| 178 | const SkRange<size_t> EMPTY_TEXT = EMPTY_RANGE; |
| 179 | |
| 180 | |
| 181 | struct Block { |
| 182 | Block() : fRange(EMPTY_RANGE), fStyle() { } |
| 183 | Block(size_t start, size_t end, const TextStyle& style) |
| 184 | : fRange(start, end), fStyle(style) {} |
| 185 | Block(TextRange textRange, const TextStyle& style) |
| 186 | : fRange(textRange), fStyle(style) {} |
| 187 | |
| 188 | void add(TextRange tail) { |
| 189 | SkASSERT(fRange.end == tail.start); |
| 190 | fRange = TextRange(fRange.start, fRange.start + fRange.width() + tail.width()); |
| 191 | } |
| 192 | TextRange fRange; |
| 193 | TextStyle fStyle; |
| 194 | }; |
| 195 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 196 | } // namespace textlayout |
| 197 | } // namespace skia |
| 198 | |
| 199 | #endif // TextStyle_DEFINED |