Repository navigation
Expand file tree
/
Copy pathTextStyle.h
More file actions
392 lines (322 loc) · 13.9 KB
/
Copy pathTextStyle.h
File metadata and controls
392 lines (322 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright 2019 Google LLC
#ifndef TextStyle_DEFINED
#define TextStyle_DEFINED
#include <optional>
#include <vector>
#include "include/core/SkColor.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontMetrics.h"
#include "include/core/SkFontStyle.h"
#include "include/core/SkPaint.h"
#include "include/core/SkScalar.h"
#include "modules/skparagraph/include/DartTypes.h"
#include "modules/skparagraph/include/FontArguments.h"
#include "modules/skparagraph/include/ParagraphPainter.h"
#include "modules/skparagraph/include/TextShadow.h"
// TODO: Make it external so the other platforms (Android) could use it
#define DEFAULT_FONT_FAMILY "sans-serif"
namespace skia {
namespace textlayout {
static inline bool nearlyZero(SkScalar x, SkScalar tolerance = SK_ScalarNearlyZero) {
if (SkIsFinite(x)) {
return SkScalarNearlyZero(x, tolerance);
}
return false;
}
static inline bool nearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance = SK_ScalarNearlyZero) {
if (SkIsFinite(x, y)) {
return SkScalarNearlyEqual(x, y, tolerance);
}
// Inf == Inf, anything else is false
return x == y;
}
// Multiple decorations can be applied at once. Ex: Underline and overline is
// (0x1 | 0x2)
enum TextDecoration {
kNoDecoration = 0x0,
kUnderline = 0x1,
kOverline = 0x2,
kLineThrough = 0x4,
};
constexpr TextDecoration AllTextDecorations[] = {
kNoDecoration,
kUnderline,
kOverline,
kLineThrough,
};
enum TextDecorationStyle { kSolid, kDouble, kDotted, kDashed, kWavy };
enum TextDecorationMode { kGaps, kThrough };
enum StyleType {
kNone,
kAllAttributes,
kFont,
kForeground,
kBackground,
kShadow,
kDecorations,
kLetterSpacing,
kWordSpacing
};
struct Decoration {
TextDecoration fType;
TextDecorationMode fMode;
SkColor fColor;
TextDecorationStyle fStyle;
SkScalar fThicknessMultiplier;
bool operator==(const Decoration& other) const {
return this->fType == other.fType &&
this->fMode == other.fMode &&
this->fColor == other.fColor &&
this->fStyle == other.fStyle &&
this->fThicknessMultiplier == other.fThicknessMultiplier;
}
};
/// Where to vertically align the placeholder relative to the surrounding text.
enum class PlaceholderAlignment {
/// Match the baseline of the placeholder with the baseline.
kBaseline,
/// Align the bottom edge of the placeholder with the baseline such that the
/// placeholder sits on top of the baseline.
kAboveBaseline,
/// Align the top edge of the placeholder with the baseline specified in
/// such that the placeholder hangs below the baseline.
kBelowBaseline,
/// Align the top edge of the placeholder with the top edge of the font.
/// When the placeholder is very tall, the extra space will hang from
/// the top and extend through the bottom of the line.
kTop,
/// Align the bottom edge of the placeholder with the top edge of the font.
/// When the placeholder is very tall, the extra space will rise from
/// the bottom and extend through the top of the line.
kBottom,
/// Align the middle of the placeholder with the middle of the text. When the
/// placeholder is very tall, the extra space will grow equally from
/// the top and bottom of the line.
kMiddle,
};
struct FontFeature {
FontFeature(SkString name, int value) : fName(std::move(name)), fValue(value) {}
bool operator==(const FontFeature& that) const {
return fName == that.fName && fValue == that.fValue;
}
SkString fName;
int fValue;
};
struct PlaceholderStyle {
PlaceholderStyle() = default;
PlaceholderStyle(SkScalar width, SkScalar height, PlaceholderAlignment alignment,
TextBaseline baseline, SkScalar offset)
: fWidth(width)
, fHeight(height)
, fAlignment(alignment)
, fBaseline(baseline)
, fBaselineOffset(offset) {}
bool equals(const PlaceholderStyle&) const;
SkScalar fWidth = 0;
SkScalar fHeight = 0;
PlaceholderAlignment fAlignment = PlaceholderAlignment::kBaseline;
TextBaseline fBaseline = TextBaseline::kAlphabetic;
// Distance from the top edge of the rect to the baseline position. This
// baseline will be aligned against the alphabetic baseline of the surrounding
// text.
//
// Positive values drop the baseline lower (positions the rect higher) and
// small or negative values will cause the rect to be positioned underneath
// the line. When baseline == height, the bottom edge of the rect will rest on
// the alphabetic baseline.
SkScalar fBaselineOffset = 0;
};
class TextStyle {
public:
TextStyle() = default;
TextStyle(const TextStyle& other) = default;
TextStyle& operator=(const TextStyle& other) = default;
TextStyle cloneForPlaceholder();
bool equals(const TextStyle& other) const;
bool equalsByFonts(const TextStyle& that) const;
bool matchOneAttribute(StyleType styleType, const TextStyle& other) const;
bool operator==(const TextStyle& rhs) const { return this->equals(rhs); }
// Colors
SkColor getColor() const { return fColor; }
void setColor(SkColor color) { fColor = color; }
bool hasForeground() const { return fHasForeground; }
SkPaint getForeground() const {
const SkPaint* paint = std::get_if<SkPaint>(&fForeground);
return paint ? *paint : SkPaint();
}
ParagraphPainter::SkPaintOrID getForegroundPaintOrID() const {
return fForeground;
}
void setForegroundPaint(SkPaint paint) {
fHasForeground = true;
fForeground = std::move(paint);
}
// DEPRECATED: prefer `setForegroundPaint`.
void setForegroundColor(SkPaint paint) { setForegroundPaint(std::move(paint)); }
// Set the foreground to a paint ID. This is intended for use by clients
// that implement a custom ParagraphPainter that can not accept an SkPaint.
void setForegroundPaintID(ParagraphPainter::PaintID paintID) {
fHasForeground = true;
fForeground = paintID;
}
void clearForegroundColor() { fHasForeground = false; }
bool hasBackground() const { return fHasBackground; }
SkPaint getBackground() const {
const SkPaint* paint = std::get_if<SkPaint>(&fBackground);
return paint ? *paint : SkPaint();
}
ParagraphPainter::SkPaintOrID getBackgroundPaintOrID() const {
return fBackground;
}
void setBackgroundPaint(SkPaint paint) {
fHasBackground = true;
fBackground = std::move(paint);
}
// DEPRECATED: prefer `setBackgroundPaint`.
void setBackgroundColor(SkPaint paint) { setBackgroundPaint(std::move(paint)); }
void setBackgroundPaintID(ParagraphPainter::PaintID paintID) {
fHasBackground = true;
fBackground = paintID;
}
void clearBackgroundColor() { fHasBackground = false; }
// Decorations
Decoration getDecoration() const { return fDecoration; }
TextDecoration getDecorationType() const { return fDecoration.fType; }
TextDecorationMode getDecorationMode() const { return fDecoration.fMode; }
SkColor getDecorationColor() const { return fDecoration.fColor; }
TextDecorationStyle getDecorationStyle() const { return fDecoration.fStyle; }
SkScalar getDecorationThicknessMultiplier() const {
return fDecoration.fThicknessMultiplier;
}
void setDecoration(TextDecoration decoration) { fDecoration.fType = decoration; }
void setDecorationMode(TextDecorationMode mode) { fDecoration.fMode = mode; }
void setDecorationStyle(TextDecorationStyle style) { fDecoration.fStyle = style; }
void setDecorationColor(SkColor color) { fDecoration.fColor = color; }
void setDecorationThicknessMultiplier(SkScalar m) { fDecoration.fThicknessMultiplier = m; }
// Weight/Width/Slant
SkFontStyle getFontStyle() const { return fFontStyle; }
void setFontStyle(SkFontStyle fontStyle) { fFontStyle = fontStyle; }
// Shadows
size_t getShadowNumber() const { return fTextShadows.size(); }
std::vector<TextShadow> getShadows() const { return fTextShadows; }
void addShadow(TextShadow shadow) { fTextShadows.emplace_back(shadow); }
void resetShadows() { fTextShadows.clear(); }
// Font features
size_t getFontFeatureNumber() const { return fFontFeatures.size(); }
std::vector<FontFeature> getFontFeatures() const { return fFontFeatures; }
void addFontFeature(const SkString& fontFeature, int value)
{ fFontFeatures.emplace_back(fontFeature, value); }
void resetFontFeatures() { fFontFeatures.clear(); }
// Font arguments
const std::optional<FontArguments>& getFontArguments() const { return fFontArguments; }
// The contents of the SkFontArguments will be copied into the TextStyle,
// and the SkFontArguments can be safely deleted after setFontArguments returns.
void setFontArguments(const std::optional<SkFontArguments>& args);
SkScalar getFontSize() const { return fFontSize; }
void setFontSize(SkScalar size) { fFontSize = size; }
const std::vector<SkString>& getFontFamilies() const { return fFontFamilies; }
void setFontFamilies(std::vector<SkString> families) {
fFontFamilies = std::move(families);
}
SkScalar getBaselineShift() const { return fBaselineShift; }
void setBaselineShift(SkScalar baselineShift) { fBaselineShift = baselineShift; }
void setHeight(SkScalar height) { fHeight = height; }
SkScalar getHeight() const { return fHeightOverride ? fHeight : 0; }
void setHeightOverride(bool heightOverride) { fHeightOverride = heightOverride; }
bool getHeightOverride() const { return fHeightOverride; }
void setHalfLeading(bool halfLeading) { fHalfLeading = halfLeading; }
bool getHalfLeading() const { return fHalfLeading; }
void setLetterSpacing(SkScalar letterSpacing) { fLetterSpacing = letterSpacing; }
SkScalar getLetterSpacing() const { return fLetterSpacing; }
void setWordSpacing(SkScalar wordSpacing) { fWordSpacing = wordSpacing; }
SkScalar getWordSpacing() const { return fWordSpacing; }
SkTypeface* getTypeface() const { return fTypeface.get(); }
sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
void setTypeface(sk_sp<SkTypeface> typeface) { fTypeface = std::move(typeface); }
SkString getLocale() const { return fLocale; }
void setLocale(const SkString& locale) { fLocale = locale; }
TextBaseline getTextBaseline() const { return fTextBaseline; }
void setTextBaseline(TextBaseline baseline) { fTextBaseline = baseline; }
void getFontMetrics(SkFontMetrics* metrics) const;
bool isPlaceholder() const { return fIsPlaceholder; }
void setPlaceholder() { fIsPlaceholder = true; }
void setFontEdging(SkFont::Edging edging) { fEdging = edging; }
SkFont::Edging getFontEdging() const { return fEdging; }
void setSubpixel(bool subpixel) { fSubpixel = subpixel; }
bool getSubpixel() const { return fSubpixel; }
void setFontHinting(SkFontHinting hinting) { fHinting = hinting; }
SkFontHinting getFontHinting() const { return fHinting; }
private:
static const std::vector<SkString>* kDefaultFontFamilies;
Decoration fDecoration = {
TextDecoration::kNoDecoration,
// TODO: switch back to kGaps when (if) switching flutter to skparagraph
TextDecorationMode::kThrough,
// It does not make sense to draw a transparent object, so we use this as a default
// value to indicate no decoration color was set.
SK_ColorTRANSPARENT, TextDecorationStyle::kSolid,
// Thickness is applied as a multiplier to the default thickness of the font.
1.0f};
SkFontStyle fFontStyle;
std::vector<SkString> fFontFamilies = *kDefaultFontFamilies;
SkScalar fFontSize = 14.0;
SkFont::Edging fEdging = SkFont::Edging::kAntiAlias;
bool fSubpixel = true;
SkFontHinting fHinting = SkFontHinting::kSlight;
SkScalar fHeight = 1.0;
bool fHeightOverride = false;
SkScalar fBaselineShift = 0.0f;
// true: half leading.
// false: scale ascent/descent with fHeight.
bool fHalfLeading = false;
SkString fLocale = {};
SkScalar fLetterSpacing = 0.0;
SkScalar fWordSpacing = 0.0;
TextBaseline fTextBaseline = TextBaseline::kAlphabetic;
SkColor fColor = SK_ColorWHITE;
bool fHasBackground = false;
ParagraphPainter::SkPaintOrID fBackground;
bool fHasForeground = false;
ParagraphPainter::SkPaintOrID fForeground;
std::vector<TextShadow> fTextShadows;
sk_sp<SkTypeface> fTypeface;
bool fIsPlaceholder = false;
std::vector<FontFeature> fFontFeatures;
std::optional<FontArguments> fFontArguments;
};
typedef size_t TextIndex;
typedef SkRange<size_t> TextRange;
const SkRange<size_t> EMPTY_TEXT = EMPTY_RANGE;
struct Block {
Block() = default;
Block(size_t start, size_t end, const TextStyle& style) : fRange(start, end), fStyle(style) {}
Block(TextRange textRange, const TextStyle& style) : fRange(textRange), fStyle(style) {}
void add(TextRange tail) {
SkASSERT(fRange.end == tail.start);
fRange = TextRange(fRange.start, fRange.start + fRange.width() + tail.width());
}
TextRange fRange = EMPTY_RANGE;
TextStyle fStyle;
};
typedef size_t BlockIndex;
typedef SkRange<size_t> BlockRange;
const size_t EMPTY_BLOCK = EMPTY_INDEX;
const SkRange<size_t> EMPTY_BLOCKS = EMPTY_RANGE;
struct Placeholder {
Placeholder() = default;
Placeholder(size_t start, size_t end, const PlaceholderStyle& style, const TextStyle& textStyle,
BlockRange blocksBefore, TextRange textBefore)
: fRange(start, end)
, fStyle(style)
, fTextStyle(textStyle)
, fBlocksBefore(blocksBefore)
, fTextBefore(textBefore) {}
TextRange fRange = EMPTY_RANGE;
PlaceholderStyle fStyle;
TextStyle fTextStyle;
BlockRange fBlocksBefore;
TextRange fTextBefore;
};
} // namespace textlayout
} // namespace skia
#endif // TextStyle_DEFINED