blob: b1feaa4d6078f4d7e62e66a97667854035ff80a9 [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"
Julia Lavrova916a9042019-08-08 16:51:27 -040011#include "include/core/SkScalar.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "modules/skparagraph/include/DartTypes.h"
13#include "modules/skparagraph/include/TextShadow.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -040014
15// TODO: Make it external so the other platforms (Android) could use it
16#define DEFAULT_FONT_FAMILY "sans-serif"
17
18namespace skia {
19namespace textlayout {
20
Julia Lavrovac0360582020-02-05 10:17:53 -050021static inline bool nearlyZero(SkScalar x, SkScalar tolerance = SK_ScalarNearlyZero) {
22 if (SkScalarIsFinite(x)) {
23 return SkScalarNearlyZero(x, tolerance);
24 }
25 return false;
26}
27
28static inline bool nearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance = SK_ScalarNearlyZero) {
29 if (SkScalarIsFinite(x) && SkScalarIsFinite(x)) {
30 return SkScalarNearlyEqual(x, y, tolerance);
31 }
32 // Inf == Inf, anything else is false
33 return x == y;
34}
35
Julia Lavrovaa3552c52019-05-30 16:12:56 -040036// Multiple decorations can be applied at once. Ex: Underline and overline is
37// (0x1 | 0x2)
38enum TextDecoration {
39 kNoDecoration = 0x0,
40 kUnderline = 0x1,
41 kOverline = 0x2,
42 kLineThrough = 0x4,
43};
Julia Lavrovab5c69a42019-07-11 09:34:39 -040044constexpr TextDecoration AllTextDecorations[] = {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040045 kNoDecoration,
46 kUnderline,
47 kOverline,
48 kLineThrough,
49};
50
51enum TextDecorationStyle { kSolid, kDouble, kDotted, kDashed, kWavy };
52
Julia Lavrova18db52f2020-05-04 15:03:18 -040053enum TextDecorationMode { kGaps, kThrough };
54
Julia Lavrovaa3552c52019-05-30 16:12:56 -040055enum StyleType {
Julia Lavrova76ae22e2020-02-26 12:14:18 -050056 kNone,
Julia Lavrovaa3552c52019-05-30 16:12:56 -040057 kAllAttributes,
Julia Lavrovaa3552c52019-05-30 16:12:56 -040058 kFont,
59 kForeground,
60 kBackground,
61 kShadow,
62 kDecorations,
63 kLetterSpacing,
64 kWordSpacing
65};
66
Julia Lavrova5207f352019-06-21 12:22:32 -040067struct Decoration {
68 TextDecoration fType;
Julia Lavrova18db52f2020-05-04 15:03:18 -040069 TextDecorationMode fMode;
Julia Lavrova5207f352019-06-21 12:22:32 -040070 SkColor fColor;
71 TextDecorationStyle fStyle;
72 SkScalar fThicknessMultiplier;
73
74 bool operator==(const Decoration& other) const {
75 return this->fType == other.fType &&
Julia Lavrova18db52f2020-05-04 15:03:18 -040076 this->fMode == other.fMode &&
Julia Lavrova5207f352019-06-21 12:22:32 -040077 this->fColor == other.fColor &&
78 this->fStyle == other.fStyle &&
79 this->fThicknessMultiplier == other.fThicknessMultiplier;
80 }
81};
82
Julia Lavrova916a9042019-08-08 16:51:27 -040083/// Where to vertically align the placeholder relative to the surrounding text.
84enum class PlaceholderAlignment {
85 /// Match the baseline of the placeholder with the baseline.
86 kBaseline,
87
88 /// Align the bottom edge of the placeholder with the baseline such that the
89 /// placeholder sits on top of the baseline.
90 kAboveBaseline,
91
92 /// Align the top edge of the placeholder with the baseline specified in
93 /// such that the placeholder hangs below the baseline.
94 kBelowBaseline,
95
96 /// Align the top edge of the placeholder with the top edge of the font.
97 /// When the placeholder is very tall, the extra space will hang from
98 /// the top and extend through the bottom of the line.
99 kTop,
100
101 /// Align the bottom edge of the placeholder with the top edge of the font.
102 /// When the placeholder is very tall, the extra space will rise from
103 /// the bottom and extend through the top of the line.
104 kBottom,
105
106 /// Align the middle of the placeholder with the middle of the text. When the
107 /// placeholder is very tall, the extra space will grow equally from
108 /// the top and bottom of the line.
109 kMiddle,
110};
111
Julia Lavrovac5313e62019-12-10 12:11:17 -0500112struct FontFeature {
113 FontFeature(const SkString name, int value) : fName(name), fValue(value) { }
Ben Wagner833313b2020-03-23 17:22:24 -0400114 bool operator==(const FontFeature& that) const {
115 return fName == that.fName && fValue == that.fValue;
Julia Lavrovac5313e62019-12-10 12:11:17 -0500116 }
117 SkString fName;
118 int fValue;
119};
120
Julia Lavrova916a9042019-08-08 16:51:27 -0400121struct PlaceholderStyle {
Julia Lavrovac0360582020-02-05 10:17:53 -0500122 PlaceholderStyle()
123 : fWidth(0)
124 , fHeight(0)
125 , fAlignment(PlaceholderAlignment::kBaseline)
126 , fBaseline(TextBaseline::kAlphabetic)
127 , fBaselineOffset(0) {}
128
Julia Lavrova916a9042019-08-08 16:51:27 -0400129 PlaceholderStyle(SkScalar width, SkScalar height, PlaceholderAlignment alignment,
130 TextBaseline baseline, SkScalar offset)
131 : fWidth(width)
132 , fHeight(height)
133 , fAlignment(alignment)
134 , fBaseline(baseline)
135 , fBaselineOffset(offset) {}
136
Ben Wagner833313b2020-03-23 17:22:24 -0400137 bool equals(const PlaceholderStyle&) const;
Julia Lavrova4cf18742020-01-14 13:24:45 -0500138
Julia Lavrovac0360582020-02-05 10:17:53 -0500139 SkScalar fWidth;
140 SkScalar fHeight;
Julia Lavrova916a9042019-08-08 16:51:27 -0400141 PlaceholderAlignment fAlignment;
Julia Lavrova916a9042019-08-08 16:51:27 -0400142 TextBaseline fBaseline;
Julia Lavrova916a9042019-08-08 16:51:27 -0400143 // Distance from the top edge of the rect to the baseline position. This
144 // baseline will be aligned against the alphabetic baseline of the surrounding
145 // text.
146 //
147 // Positive values drop the baseline lower (positions the rect higher) and
148 // small or negative values will cause the rect to be positioned underneath
149 // the line. When baseline == height, the bottom edge of the rect will rest on
150 // the alphabetic baseline.
Julia Lavrovac0360582020-02-05 10:17:53 -0500151 SkScalar fBaselineOffset;
Julia Lavrova916a9042019-08-08 16:51:27 -0400152};
153
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400154class TextStyle {
155public:
156 TextStyle();
Julia Lavrova916a9042019-08-08 16:51:27 -0400157 TextStyle(const TextStyle& other, bool placeholder);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400158 ~TextStyle() = default;
159
160 bool equals(const TextStyle& other) const;
Julia Lavrova4cf18742020-01-14 13:24:45 -0500161 bool equalsByFonts(const TextStyle& that) const;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400162 bool matchOneAttribute(StyleType styleType, const TextStyle& other) const;
163 bool operator==(const TextStyle& rhs) const { return this->equals(rhs); }
164
165 // Colors
166 SkColor getColor() const { return fColor; }
167 void setColor(SkColor color) { fColor = color; }
168
169 bool hasForeground() const { return fHasForeground; }
170 SkPaint getForeground() const { return fForeground; }
171 void setForegroundColor(SkPaint paint) {
172 fHasForeground = true;
173 fForeground = std::move(paint);
174 }
175 void clearForegroundColor() { fHasForeground = false; }
176
177 bool hasBackground() const { return fHasBackground; }
178 SkPaint getBackground() const { return fBackground; }
179 void setBackgroundColor(SkPaint paint) {
180 fHasBackground = true;
181 fBackground = std::move(paint);
182 }
183 void clearBackgroundColor() { fHasBackground = false; }
184
185 // Decorations
Julia Lavrova5207f352019-06-21 12:22:32 -0400186 Decoration getDecoration() const { return fDecoration; }
187 TextDecoration getDecorationType() const { return fDecoration.fType; }
Julia Lavrova18db52f2020-05-04 15:03:18 -0400188 TextDecorationMode getDecorationMode() const { return fDecoration.fMode; }
Julia Lavrova5207f352019-06-21 12:22:32 -0400189 SkColor getDecorationColor() const { return fDecoration.fColor; }
190 TextDecorationStyle getDecorationStyle() const { return fDecoration.fStyle; }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400191 SkScalar getDecorationThicknessMultiplier() const {
Julia Lavrova5207f352019-06-21 12:22:32 -0400192 return fDecoration.fThicknessMultiplier;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400193 }
Julia Lavrova5207f352019-06-21 12:22:32 -0400194 void setDecoration(TextDecoration decoration) { fDecoration.fType = decoration; }
Julia Lavrova18db52f2020-05-04 15:03:18 -0400195 void setDecorationMode(TextDecorationMode mode) { fDecoration.fMode = mode; }
Julia Lavrova5207f352019-06-21 12:22:32 -0400196 void setDecorationStyle(TextDecorationStyle style) { fDecoration.fStyle = style; }
197 void setDecorationColor(SkColor color) { fDecoration.fColor = color; }
198 void setDecorationThicknessMultiplier(SkScalar m) { fDecoration.fThicknessMultiplier = m; }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400199
200 // Weight/Width/Slant
201 SkFontStyle getFontStyle() const { return fFontStyle; }
202 void setFontStyle(SkFontStyle fontStyle) { fFontStyle = fontStyle; }
203
204 // Shadows
205 size_t getShadowNumber() const { return fTextShadows.size(); }
206 std::vector<TextShadow> getShadows() const { return fTextShadows; }
207 void addShadow(TextShadow shadow) { fTextShadows.emplace_back(shadow); }
208 void resetShadows() { fTextShadows.clear(); }
209
Julia Lavrovac5313e62019-12-10 12:11:17 -0500210 // Font features
211 size_t getFontFeatureNumber() const { return fFontFeatures.size(); }
212 std::vector<FontFeature> getFontFeatures() const { return fFontFeatures; }
213 void addFontFeature(const SkString& fontFeature, int value)
214 { fFontFeatures.emplace_back(fontFeature, value); }
215 void resetFontFeatures() { fFontFeatures.clear(); }
216
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400217 SkScalar getFontSize() const { return fFontSize; }
218 void setFontSize(SkScalar size) { fFontSize = size; }
219
220 const std::vector<SkString>& getFontFamilies() const { return fFontFamilies; }
221 void setFontFamilies(std::vector<SkString> families) {
222 fFontFamilies = std::move(families);
223 }
224
225 void setHeight(SkScalar height) { fHeight = height; }
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400226 SkScalar getHeight() const { return fHeightOverride ? fHeight : 0; }
227
228 void setHeightOverride(bool heightOverride) { fHeightOverride = heightOverride; }
229 bool getHeightOverride() const { return fHeightOverride; }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400230
231 void setLetterSpacing(SkScalar letterSpacing) { fLetterSpacing = letterSpacing; }
232 SkScalar getLetterSpacing() const { return fLetterSpacing; }
233
234 void setWordSpacing(SkScalar wordSpacing) { fWordSpacing = wordSpacing; }
235 SkScalar getWordSpacing() const { return fWordSpacing; }
236
237 SkTypeface* getTypeface() const { return fTypeface.get(); }
238 sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
239 void setTypeface(sk_sp<SkTypeface> typeface) { fTypeface = std::move(typeface); }
240
241 SkString getLocale() const { return fLocale; }
242 void setLocale(const SkString& locale) { fLocale = locale; }
243
244 TextBaseline getTextBaseline() const { return fTextBaseline; }
245 void setTextBaseline(TextBaseline baseline) { fTextBaseline = baseline; }
246
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400247 void getFontMetrics(SkFontMetrics* metrics) const;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400248
Julia Lavrova916a9042019-08-08 16:51:27 -0400249 bool isPlaceholder() const { return fIsPlaceholder; }
250 void setPlaceholder() { fIsPlaceholder = true; }
251
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400252private:
Julia Lavrova916a9042019-08-08 16:51:27 -0400253
Julia Lavrova5207f352019-06-21 12:22:32 -0400254 Decoration fDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400255
256 SkFontStyle fFontStyle;
257
258 std::vector<SkString> fFontFamilies;
259 SkScalar fFontSize;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400260 SkScalar fHeight;
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400261 bool fHeightOverride;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400262 SkString fLocale;
263 SkScalar fLetterSpacing;
264 SkScalar fWordSpacing;
265
266 TextBaseline fTextBaseline;
267
268 SkColor fColor;
269 bool fHasBackground;
270 SkPaint fBackground;
271 bool fHasForeground;
272 SkPaint fForeground;
273
274 std::vector<TextShadow> fTextShadows;
275
276 sk_sp<SkTypeface> fTypeface;
Julia Lavrova916a9042019-08-08 16:51:27 -0400277 bool fIsPlaceholder;
Julia Lavrovac5313e62019-12-10 12:11:17 -0500278
279 std::vector<FontFeature> fFontFeatures;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400280};
Julia Lavrova5207f352019-06-21 12:22:32 -0400281
282typedef size_t TextIndex;
283typedef SkRange<size_t> TextRange;
284const SkRange<size_t> EMPTY_TEXT = EMPTY_RANGE;
285
Julia Lavrova5207f352019-06-21 12:22:32 -0400286struct Block {
287 Block() : fRange(EMPTY_RANGE), fStyle() { }
Julia Lavrova916a9042019-08-08 16:51:27 -0400288 Block(size_t start, size_t end, const TextStyle& style) : fRange(start, end), fStyle(style) {}
289 Block(TextRange textRange, const TextStyle& style) : fRange(textRange), fStyle(style) {}
290
Julia Lavrova90bfd1c2019-12-04 11:43:32 -0500291 Block(const Block& other) : fRange(other.fRange), fStyle(other.fStyle) {}
Julia Lavrova5207f352019-06-21 12:22:32 -0400292
293 void add(TextRange tail) {
294 SkASSERT(fRange.end == tail.start);
295 fRange = TextRange(fRange.start, fRange.start + fRange.width() + tail.width());
296 }
Julia Lavrova90bfd1c2019-12-04 11:43:32 -0500297
Julia Lavrova5207f352019-06-21 12:22:32 -0400298 TextRange fRange;
299 TextStyle fStyle;
300};
301
Julia Lavrova916a9042019-08-08 16:51:27 -0400302
303typedef size_t BlockIndex;
304typedef SkRange<size_t> BlockRange;
305const size_t EMPTY_BLOCK = EMPTY_INDEX;
306const SkRange<size_t> EMPTY_BLOCKS = EMPTY_RANGE;
307
308struct Placeholder {
309 Placeholder() : fRange(EMPTY_RANGE), fStyle() {}
310
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400311 Placeholder(size_t start, size_t end, const PlaceholderStyle& style, const TextStyle& textStyle,
312 BlockRange blocksBefore, TextRange textBefore)
Julia Lavrova916a9042019-08-08 16:51:27 -0400313 : fRange(start, end)
314 , fStyle(style)
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400315 , fTextStyle(textStyle)
Julia Lavrova916a9042019-08-08 16:51:27 -0400316 , fBlocksBefore(blocksBefore)
317 , fTextBefore(textBefore) {}
318
Julia Lavrova90bfd1c2019-12-04 11:43:32 -0500319 Placeholder(const Placeholder& other)
320 : fRange(other.fRange)
321 , fStyle(other.fStyle)
322 , fTextStyle(other.fTextStyle)
323 , fBlocksBefore(other.fBlocksBefore)
324 , fTextBefore(other.fTextBefore) {}
Julia Lavrova916a9042019-08-08 16:51:27 -0400325
326 TextRange fRange;
327 PlaceholderStyle fStyle;
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400328 TextStyle fTextStyle;
Julia Lavrova916a9042019-08-08 16:51:27 -0400329 BlockRange fBlocksBefore;
330 TextRange fTextBefore;
331};
332
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400333} // namespace textlayout
334} // namespace skia
335
336#endif // TextStyle_DEFINED