blob: 64f12ff37b9462ce8d5c7a491f920f3c7d7e8dfd [file] [log] [blame]
Julia Lavrovaa3552c52019-05-30 16:12:56 -04001// Copyright 2019 Google LLC.
2#ifndef TextStyle_DEFINED
3#define TextStyle_DEFINED
4
5#include <vector>
Julia Lavrovaa3552c52019-05-30 16:12:56 -04006#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 Danielf91aeb22019-06-18 09:58:02 -040011#include "modules/skparagraph/include/DartTypes.h"
12#include "modules/skparagraph/include/TextShadow.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -040013
14// TODO: Make it external so the other platforms (Android) could use it
15#define DEFAULT_FONT_FAMILY "sans-serif"
16
17namespace skia {
18namespace textlayout {
19
20// Multiple decorations can be applied at once. Ex: Underline and overline is
21// (0x1 | 0x2)
22enum TextDecoration {
23 kNoDecoration = 0x0,
24 kUnderline = 0x1,
25 kOverline = 0x2,
26 kLineThrough = 0x4,
27};
Julia Lavrovab5c69a42019-07-11 09:34:39 -040028constexpr TextDecoration AllTextDecorations[] = {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040029 kNoDecoration,
30 kUnderline,
31 kOverline,
32 kLineThrough,
33};
34
35enum TextDecorationStyle { kSolid, kDouble, kDotted, kDashed, kWavy };
36
37enum StyleType {
38 kAllAttributes,
Julia Lavrovaa3552c52019-05-30 16:12:56 -040039 kFont,
40 kForeground,
41 kBackground,
42 kShadow,
43 kDecorations,
44 kLetterSpacing,
45 kWordSpacing
46};
47
Julia Lavrova5207f352019-06-21 12:22:32 -040048struct 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 Lavrovaa3552c52019-05-30 16:12:56 -040062class TextStyle {
63public:
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 Lavrova5207f352019-06-21 12:22:32 -040092 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 Lavrovaa3552c52019-05-30 16:12:56 -040096 SkScalar getDecorationThicknessMultiplier() const {
Julia Lavrova5207f352019-06-21 12:22:32 -040097 return fDecoration.fThicknessMultiplier;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040098 }
Julia Lavrova5207f352019-06-21 12:22:32 -040099 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 Lavrovaa3552c52019-05-30 16:12:56 -0400103
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
151private:
Julia Lavrova5207f352019-06-21 12:22:32 -0400152 Decoration fDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400153
154 SkFontStyle fFontStyle;
155
156 std::vector<SkString> fFontFamilies;
157 SkScalar fFontSize;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400158 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 Lavrova5207f352019-06-21 12:22:32 -0400175
176typedef size_t TextIndex;
177typedef SkRange<size_t> TextRange;
178const SkRange<size_t> EMPTY_TEXT = EMPTY_RANGE;
179
180
181struct 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 Lavrovaa3552c52019-05-30 16:12:56 -0400196} // namespace textlayout
197} // namespace skia
198
199#endif // TextStyle_DEFINED