Debian Codegraph @

modules/skparagraph/include/TextStyle.h

raw
1 // Copyright 2019 Google LLC.
2 #ifndef TextStyle_DEFINED
3 #define TextStyle_DEFINED
4
5 #include <optional>
6 #include <vector>
7 #include "include/core/SkColor.h"
8 #include "include/core/SkFont.h"
9 #include "include/core/SkFontMetrics.h"
10 #include "include/core/SkFontStyle.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkScalar.h"
13 #include "modules/skparagraph/include/DartTypes.h"
14 #include "modules/skparagraph/include/FontArguments.h"
15 #include "modules/skparagraph/include/ParagraphPainter.h"
16 #include "modules/skparagraph/include/TextShadow.h"
17
18 // TODO: Make it external so the other platforms (Android) could use it
19 #define DEFAULT_FONT_FAMILY "sans-serif"
20
21 namespace skia {
22 namespace textlayout {
23
24 static inline bool nearlyZero(SkScalar x, SkScalar tolerance = SK_ScalarNearlyZero) {
25 if (SkIsFinite(x)) {
26 return SkScalarNearlyZero(x, tolerance);
27 }
28 return false;
29 }
30
31 static 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)
41 enum TextDecoration {
42 kNoDecoration = 0x0,
43 kUnderline = 0x1,
44 kOverline = 0x2,
45 kLineThrough = 0x4,
46 };
47 constexpr TextDecoration AllTextDecorations[] = {
48 kNoDecoration,
49 kUnderline,
50 kOverline,
51 kLineThrough,
52 };
53
54 enum TextDecorationStyle { kSolid, kDouble, kDotted, kDashed, kWavy };
55
56 enum TextDecorationMode { kGaps, kThrough };
57
58 enum StyleType {
59 kNone,
60 kAllAttributes,
61 kFont,
62 kForeground,
63 kBackground,
64 kShadow,
65 kDecorations,
66 kLetterSpacing,
67 kWordSpacing
68 };
69
70 struct Decoration {
71 TextDecoration fType;
72 TextDecorationMode fMode;
73 SkColor fColor;
74 TextDecorationStyle fStyle;
75 SkScalar fThicknessMultiplier;
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.
87 enum class PlaceholderAlignment {
88 /// Match the baseline of the placeholder with the baseline.
89 kBaseline,
90
91 /// Align the bottom edge of the placeholder with the baseline such that the
92 /// placeholder sits on top of the baseline.
93 kAboveBaseline,
94
95 /// Align the top edge of the placeholder with the baseline specified in
96 /// such that the placeholder hangs below the baseline.
97 kBelowBaseline,
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
115 struct FontFeature {
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 }
120 SkString fName;
121 int fValue;
122 };
123
124 struct PlaceholderStyle {
125 PlaceholderStyle() = default;
126 PlaceholderStyle(SkScalar width, SkScalar height, PlaceholderAlignment alignment,
127 TextBaseline baseline, SkScalar offset)
128 : fWidth(width)
129 , fHeight(height)
130 , fAlignment(alignment)
131 , fBaseline(baseline)
132 , fBaselineOffset(offset) {}
133
134 bool equals(const PlaceholderStyle&) const;
135
136 SkScalar fWidth = 0;
137 SkScalar fHeight = 0;
138 PlaceholderAlignment fAlignment = PlaceholderAlignment::kBaseline;
139 TextBaseline fBaseline = TextBaseline::kAlphabetic;
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.
148 SkScalar fBaselineOffset = 0;
149 };
150
151 class TextStyle {
152 public:
153 TextStyle() = default;
154 TextStyle(const TextStyle& other) = default;
155 TextStyle& operator=(const TextStyle& other) = default;
156
157 TextStyle cloneForPlaceholder();
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; }
169 SkPaint getForeground() const {
170 const SkPaint* paint = std::get_if<SkPaint>(&fForeground);
171 return paint ? *paint : SkPaint();
172 }
173 ParagraphPainter::SkPaintOrID getForegroundPaintOrID() const {
174 return fForeground;
175 }
176 void setForegroundPaint(SkPaint paint) {
177 fHasForeground = true;
178 fForeground = std::move(paint);
179 }
180 // DEPRECATED: prefer `setForegroundPaint`.
181 void setForegroundColor(SkPaint paint) { setForegroundPaint(std::move(paint)); }
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.
185 void setForegroundPaintID(ParagraphPainter::PaintID paintID) {
186 fHasForeground = true;
187 fForeground = paintID;
188 }
189 void clearForegroundColor() { fHasForeground = false; }
190
191 bool hasBackground() const { return fHasBackground; }
192 SkPaint getBackground() const {
193 const SkPaint* paint = std::get_if<SkPaint>(&fBackground);
194 return paint ? *paint : SkPaint();
195 }
196 ParagraphPainter::SkPaintOrID getBackgroundPaintOrID() const {
197 return fBackground;
198 }
199 void setBackgroundPaint(SkPaint paint) {
200 fHasBackground = true;
201 fBackground = std::move(paint);
202 }
203 // DEPRECATED: prefer `setBackgroundPaint`.
204 void setBackgroundColor(SkPaint paint) { setBackgroundPaint(std::move(paint)); }
205 void setBackgroundPaintID(ParagraphPainter::PaintID paintID) {
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; }
217 SkScalar getDecorationThicknessMultiplier() const {
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; }
224 void setDecorationThicknessMultiplier(SkScalar m) { fDecoration.fThicknessMultiplier = m; }
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
290 void setFontEdging(SkFont::Edging edging) { fEdging = edging; }
291 SkFont::Edging getFontEdging() const { return fEdging; }
292
293 void setSubpixel(bool subpixel) { fSubpixel = subpixel; }
294 bool getSubpixel() const { return fSubpixel; }
295
296 void setFontHinting(SkFontHinting hinting) { fHinting = hinting; }
297 SkFontHinting getFontHinting() const { return fHinting; }
298
299 private:
300 static const std::vector<SkString>* kDefaultFontFamilies;
301
302 Decoration fDecoration = {
303 TextDecoration::kNoDecoration,
304 // TODO: switch back to kGaps when (if) switching flutter to skparagraph
305 TextDecorationMode::kThrough,
306 // It does not make sense to draw a transparent object, so we use this as a default
307 // value to indicate no decoration color was set.
308 SK_ColorTRANSPARENT, TextDecorationStyle::kSolid,
309 // Thickness is applied as a multiplier to the default thickness of the font.
310 1.0f};
311
312 SkFontStyle fFontStyle;
313
314 std::vector<SkString> fFontFamilies = *kDefaultFontFamilies;
315
316 SkScalar fFontSize = 14.0;
317 SkFont::Edging fEdging = SkFont::Edging::kAntiAlias;
318 bool fSubpixel = true;
319 SkFontHinting fHinting = SkFontHinting::kSlight;
320 SkScalar fHeight = 1.0;
321 bool fHeightOverride = false;
322 SkScalar fBaselineShift = 0.0f;
323 // true: half leading.
324 // false: scale ascent/descent with fHeight.
325 bool fHalfLeading = false;
326 SkString fLocale = {};
327 SkScalar fLetterSpacing = 0.0;
328 SkScalar fWordSpacing = 0.0;
329
330 TextBaseline fTextBaseline = TextBaseline::kAlphabetic;
331
332 SkColor fColor = SK_ColorWHITE;
333 bool fHasBackground = false;
334 ParagraphPainter::SkPaintOrID fBackground;
335 bool fHasForeground = false;
336 ParagraphPainter::SkPaintOrID fForeground;
337
338 std::vector<TextShadow> fTextShadows;
339
340 sk_sp<SkTypeface> fTypeface;
341 bool fIsPlaceholder = false;
342
343 std::vector<FontFeature> fFontFeatures;
344
345 std::optional<FontArguments> fFontArguments;
346 };
347
348 typedef size_t TextIndex;
349 typedef SkRange<size_t> TextRange;
350 const SkRange<size_t> EMPTY_TEXT = EMPTY_RANGE;
351
352 struct Block {
353 Block() = default;
354 Block(size_t start, size_t end, const TextStyle& style) : fRange(start, end), fStyle(style) {}
355 Block(TextRange textRange, const TextStyle& style) : fRange(textRange), fStyle(style) {}
356
357 void add(TextRange tail) {
358 SkASSERT(fRange.end == tail.start);
359 fRange = TextRange(fRange.start, fRange.start + fRange.width() + tail.width());
360 }
361
362 TextRange fRange = EMPTY_RANGE;
363 TextStyle fStyle;
364 };
365
366
367 typedef size_t BlockIndex;
368 typedef SkRange<size_t> BlockRange;
369 const size_t EMPTY_BLOCK = EMPTY_INDEX;
370 const SkRange<size_t> EMPTY_BLOCKS = EMPTY_RANGE;
371
372 struct Placeholder {
373 Placeholder() = default;
374 Placeholder(size_t start, size_t end, const PlaceholderStyle& style, const TextStyle& textStyle,
375 BlockRange blocksBefore, TextRange textBefore)
376 : fRange(start, end)
377 , fStyle(style)
378 , fTextStyle(textStyle)
379 , fBlocksBefore(blocksBefore)
380 , fTextBefore(textBefore) {}
381
382 TextRange fRange = EMPTY_RANGE;
383 PlaceholderStyle fStyle;
384 TextStyle fTextStyle;
385 BlockRange fBlocksBefore;
386 TextRange fTextBefore;
387 };
388
389 } // namespace textlayout
390 } // namespace skia
391
392 #endif // TextStyle_DEFINED