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