• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (SkScalarIsFinite(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 (SkScalarIsFinite(x) && SkScalarIsFinite(x)) {
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 {
FontFeatureFontFeature116     FontFeature(const SkString name, int value) : fName(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;
PlaceholderStylePlaceholderStyle126     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
getColor()165     SkColor getColor() const { return fColor; }
setColor(SkColor color)166     void setColor(SkColor color) { fColor = color; }
167 
hasForeground()168     bool hasForeground() const { return fHasForeground; }
getForeground()169     SkPaint getForeground() const {
170         const SkPaint* paint = std::get_if<SkPaint>(&fForeground);
171         return paint ? *paint : SkPaint();
172     }
getForegroundPaintOrID()173     ParagraphPainter::SkPaintOrID getForegroundPaintOrID() const {
174         return fForeground;
175     }
setForegroundColor(SkPaint paint)176     void setForegroundColor(SkPaint paint) {
177         fHasForeground = true;
178         fForeground = std::move(paint);
179     }
180     // Set the foreground to a paint ID.  This is intended for use by clients
181     // that implement a custom ParagraphPainter that can not accept an SkPaint.
setForegroundPaintID(ParagraphPainter::PaintID paintID)182     void setForegroundPaintID(ParagraphPainter::PaintID paintID) {
183         fHasForeground = true;
184         fForeground = paintID;
185     }
clearForegroundColor()186     void clearForegroundColor() { fHasForeground = false; }
187 
hasBackground()188     bool hasBackground() const { return fHasBackground; }
getBackground()189     SkPaint getBackground() const {
190         const SkPaint* paint = std::get_if<SkPaint>(&fBackground);
191         return paint ? *paint : SkPaint();
192     }
getBackgroundPaintOrID()193     ParagraphPainter::SkPaintOrID getBackgroundPaintOrID() const {
194         return fBackground;
195     }
setBackgroundColor(SkPaint paint)196     void setBackgroundColor(SkPaint paint) {
197         fHasBackground = true;
198         fBackground = std::move(paint);
199     }
setBackgroundPaintID(ParagraphPainter::PaintID paintID)200     void setBackgroundPaintID(ParagraphPainter::PaintID paintID) {
201         fHasBackground = true;
202         fBackground = paintID;
203     }
clearBackgroundColor()204     void clearBackgroundColor() { fHasBackground = false; }
205 
206     // Decorations
getDecoration()207     Decoration getDecoration() const { return fDecoration; }
getDecorationType()208     TextDecoration getDecorationType() const { return fDecoration.fType; }
getDecorationMode()209     TextDecorationMode getDecorationMode() const { return fDecoration.fMode; }
getDecorationColor()210     SkColor getDecorationColor() const { return fDecoration.fColor; }
getDecorationStyle()211     TextDecorationStyle getDecorationStyle() const { return fDecoration.fStyle; }
getDecorationThicknessMultiplier()212     SkScalar getDecorationThicknessMultiplier() const {
213         return fDecoration.fThicknessMultiplier;
214     }
setDecoration(TextDecoration decoration)215     void setDecoration(TextDecoration decoration) { fDecoration.fType = decoration; }
setDecorationMode(TextDecorationMode mode)216     void setDecorationMode(TextDecorationMode mode) { fDecoration.fMode = mode; }
setDecorationStyle(TextDecorationStyle style)217     void setDecorationStyle(TextDecorationStyle style) { fDecoration.fStyle = style; }
setDecorationColor(SkColor color)218     void setDecorationColor(SkColor color) { fDecoration.fColor = color; }
setDecorationThicknessMultiplier(SkScalar m)219     void setDecorationThicknessMultiplier(SkScalar m) { fDecoration.fThicknessMultiplier = m; }
220 
221     // Weight/Width/Slant
getFontStyle()222     SkFontStyle getFontStyle() const { return fFontStyle; }
setFontStyle(SkFontStyle fontStyle)223     void setFontStyle(SkFontStyle fontStyle) { fFontStyle = fontStyle; }
224 
225     // Shadows
getShadowNumber()226     size_t getShadowNumber() const { return fTextShadows.size(); }
getShadows()227     std::vector<TextShadow> getShadows() const { return fTextShadows; }
addShadow(TextShadow shadow)228     void addShadow(TextShadow shadow) { fTextShadows.emplace_back(shadow); }
resetShadows()229     void resetShadows() { fTextShadows.clear(); }
230 
231     // Font features
getFontFeatureNumber()232     size_t getFontFeatureNumber() const { return fFontFeatures.size(); }
getFontFeatures()233     std::vector<FontFeature> getFontFeatures() const { return fFontFeatures; }
addFontFeature(const SkString & fontFeature,int value)234     void addFontFeature(const SkString& fontFeature, int value)
235         { fFontFeatures.emplace_back(fontFeature, value); }
resetFontFeatures()236     void resetFontFeatures() { fFontFeatures.clear(); }
237 
238     // Font arguments
getFontArguments()239     const std::optional<FontArguments>& getFontArguments() const { return fFontArguments; }
240     // The contents of the SkFontArguments will be copied into the TextStyle,
241     // and the SkFontArguments can be safely deleted after setFontArguments returns.
242     void setFontArguments(const std::optional<SkFontArguments>& args);
243 
getFontSize()244     SkScalar getFontSize() const { return fFontSize; }
setFontSize(SkScalar size)245     void setFontSize(SkScalar size) { fFontSize = size; }
246 
getFontFamilies()247     const std::vector<SkString>& getFontFamilies() const { return fFontFamilies; }
setFontFamilies(std::vector<SkString> families)248     void setFontFamilies(std::vector<SkString> families) {
249         fFontFamilies = std::move(families);
250     }
251 
getBaselineShift()252     SkScalar getBaselineShift() const { return fBaselineShift; }
setBaselineShift(SkScalar baselineShift)253     void setBaselineShift(SkScalar baselineShift) { fBaselineShift = baselineShift; }
254 
setHeight(SkScalar height)255     void setHeight(SkScalar height) { fHeight = height; }
getHeight()256     SkScalar getHeight() const { return fHeightOverride ? fHeight : 0; }
257 
setHeightOverride(bool heightOverride)258     void setHeightOverride(bool heightOverride) { fHeightOverride = heightOverride; }
getHeightOverride()259     bool getHeightOverride() const { return fHeightOverride; }
260 
setHalfLeading(bool halfLeading)261     void setHalfLeading(bool halfLeading) { fHalfLeading = halfLeading; }
getHalfLeading()262     bool getHalfLeading() const { return fHalfLeading; }
263 
setLetterSpacing(SkScalar letterSpacing)264     void setLetterSpacing(SkScalar letterSpacing) { fLetterSpacing = letterSpacing; }
getLetterSpacing()265     SkScalar getLetterSpacing() const { return fLetterSpacing; }
266 
setWordSpacing(SkScalar wordSpacing)267     void setWordSpacing(SkScalar wordSpacing) { fWordSpacing = wordSpacing; }
getWordSpacing()268     SkScalar getWordSpacing() const { return fWordSpacing; }
269 
getTypeface()270     SkTypeface* getTypeface() const { return fTypeface.get(); }
refTypeface()271     sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
setTypeface(sk_sp<SkTypeface> typeface)272     void setTypeface(sk_sp<SkTypeface> typeface) { fTypeface = std::move(typeface); }
273 
getLocale()274     SkString getLocale() const { return fLocale; }
setLocale(const SkString & locale)275     void setLocale(const SkString& locale) { fLocale = locale; }
276 
getTextBaseline()277     TextBaseline getTextBaseline() const { return fTextBaseline; }
setTextBaseline(TextBaseline baseline)278     void setTextBaseline(TextBaseline baseline) { fTextBaseline = baseline; }
279 
280     void getFontMetrics(SkFontMetrics* metrics) const;
281 
isPlaceholder()282     bool isPlaceholder() const { return fIsPlaceholder; }
setPlaceholder()283     void setPlaceholder() { fIsPlaceholder = true; }
284 
285 private:
286     static const std::vector<SkString>* kDefaultFontFamilies;
287 
288     Decoration fDecoration = {
289             TextDecoration::kNoDecoration,
290             // TODO: switch back to kGaps when (if) switching flutter to skparagraph
291             TextDecorationMode::kThrough,
292             // It does not make sense to draw a transparent object, so we use this as a default
293             // value to indicate no decoration color was set.
294             SK_ColorTRANSPARENT, TextDecorationStyle::kSolid,
295             // Thickness is applied as a multiplier to the default thickness of the font.
296             1.0f};
297 
298     SkFontStyle fFontStyle;
299 
300     std::vector<SkString> fFontFamilies = *kDefaultFontFamilies;
301 
302     SkScalar fFontSize = 14.0;
303     SkScalar fHeight = 1.0;
304     bool fHeightOverride = false;
305     SkScalar fBaselineShift = 0.0f;
306     // true: half leading.
307     // false: scale ascent/descent with fHeight.
308     bool fHalfLeading = false;
309     SkString fLocale = {};
310     SkScalar fLetterSpacing = 0.0;
311     SkScalar fWordSpacing = 0.0;
312 
313     TextBaseline fTextBaseline = TextBaseline::kAlphabetic;
314 
315     SkColor fColor = SK_ColorWHITE;
316     bool fHasBackground = false;
317     ParagraphPainter::SkPaintOrID fBackground;
318     bool fHasForeground = false;
319     ParagraphPainter::SkPaintOrID fForeground;
320 
321     std::vector<TextShadow> fTextShadows;
322 
323     sk_sp<SkTypeface> fTypeface;
324     bool fIsPlaceholder = false;
325 
326     std::vector<FontFeature> fFontFeatures;
327 
328     std::optional<FontArguments> fFontArguments;
329 };
330 
331 typedef size_t TextIndex;
332 typedef SkRange<size_t> TextRange;
333 const SkRange<size_t> EMPTY_TEXT = EMPTY_RANGE;
334 
335 struct Block {
336     Block() = default;
BlockBlock337     Block(size_t start, size_t end, const TextStyle& style) : fRange(start, end), fStyle(style) {}
BlockBlock338     Block(TextRange textRange, const TextStyle& style) : fRange(textRange), fStyle(style) {}
339 
addBlock340     void add(TextRange tail) {
341         SkASSERT(fRange.end == tail.start);
342         fRange = TextRange(fRange.start, fRange.start + fRange.width() + tail.width());
343     }
344 
345     TextRange fRange = EMPTY_RANGE;
346     TextStyle fStyle;
347 };
348 
349 
350 typedef size_t BlockIndex;
351 typedef SkRange<size_t> BlockRange;
352 const size_t EMPTY_BLOCK = EMPTY_INDEX;
353 const SkRange<size_t> EMPTY_BLOCKS = EMPTY_RANGE;
354 
355 struct Placeholder {
356     Placeholder() = default;
PlaceholderPlaceholder357     Placeholder(size_t start, size_t end, const PlaceholderStyle& style, const TextStyle& textStyle,
358                 BlockRange blocksBefore, TextRange textBefore)
359             : fRange(start, end)
360             , fStyle(style)
361             , fTextStyle(textStyle)
362             , fBlocksBefore(blocksBefore)
363             , fTextBefore(textBefore) {}
364 
365     TextRange fRange = EMPTY_RANGE;
366     PlaceholderStyle fStyle;
367     TextStyle fTextStyle;
368     BlockRange fBlocksBefore;
369     TextRange fTextBefore;
370 };
371 
372 }  // namespace textlayout
373 }  // namespace skia
374 
375 #endif  // TextStyle_DEFINED
376