blob: 9186b93582c8b59857b918b2b932386d5fb7968f [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040011#include "modules/skparagraph/include/DartTypes.h"
12#include "modules/skparagraph/include/TextShadow.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -040013
14// TODO: Make it external so the other platforms (Android) could use it
15#define DEFAULT_FONT_FAMILY "sans-serif"
16
17namespace skia {
18namespace textlayout {
19
20// Multiple decorations can be applied at once. Ex: Underline and overline is
21// (0x1 | 0x2)
22enum TextDecoration {
23 kNoDecoration = 0x0,
24 kUnderline = 0x1,
25 kOverline = 0x2,
26 kLineThrough = 0x4,
27};
Julia Lavrovab5c69a42019-07-11 09:34:39 -040028constexpr TextDecoration AllTextDecorations[] = {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040029 kNoDecoration,
30 kUnderline,
31 kOverline,
32 kLineThrough,
33};
34
35enum TextDecorationStyle { kSolid, kDouble, kDotted, kDashed, kWavy };
36
37enum StyleType {
38 kAllAttributes,
39 kText,
40 kFont,
41 kForeground,
42 kBackground,
43 kShadow,
44 kDecorations,
45 kLetterSpacing,
46 kWordSpacing
47};
48
49class TextStyle {
50public:
51 TextStyle();
52 ~TextStyle() = default;
53
54 bool equals(const TextStyle& other) const;
55 bool matchOneAttribute(StyleType styleType, const TextStyle& other) const;
56 bool operator==(const TextStyle& rhs) const { return this->equals(rhs); }
57
58 // Colors
59 SkColor getColor() const { return fColor; }
60 void setColor(SkColor color) { fColor = color; }
61
62 bool hasForeground() const { return fHasForeground; }
63 SkPaint getForeground() const { return fForeground; }
64 void setForegroundColor(SkPaint paint) {
65 fHasForeground = true;
66 fForeground = std::move(paint);
67 }
68 void clearForegroundColor() { fHasForeground = false; }
69
70 bool hasBackground() const { return fHasBackground; }
71 SkPaint getBackground() const { return fBackground; }
72 void setBackgroundColor(SkPaint paint) {
73 fHasBackground = true;
74 fBackground = std::move(paint);
75 }
76 void clearBackgroundColor() { fHasBackground = false; }
77
78 // Decorations
79 TextDecoration getDecoration() const { return fDecoration; }
80 SkColor getDecorationColor() const { return fDecorationColor; }
81 TextDecorationStyle getDecorationStyle() const { return fDecorationStyle; }
82 SkScalar getDecorationThicknessMultiplier() const {
83 return fDecorationThicknessMultiplier;
84 }
85 void setDecoration(TextDecoration decoration) { fDecoration = decoration; }
86 void setDecorationStyle(TextDecorationStyle style) { fDecorationStyle = style; }
87 void setDecorationColor(SkColor color) { fDecorationColor = color; }
88 void setDecorationThicknessMultiplier(SkScalar m) { fDecorationThicknessMultiplier = m; }
89
90 // Weight/Width/Slant
91 SkFontStyle getFontStyle() const { return fFontStyle; }
92 void setFontStyle(SkFontStyle fontStyle) { fFontStyle = fontStyle; }
93
94 // Shadows
95 size_t getShadowNumber() const { return fTextShadows.size(); }
96 std::vector<TextShadow> getShadows() const { return fTextShadows; }
97 void addShadow(TextShadow shadow) { fTextShadows.emplace_back(shadow); }
98 void resetShadows() { fTextShadows.clear(); }
99
100 SkScalar getFontSize() const { return fFontSize; }
101 void setFontSize(SkScalar size) { fFontSize = size; }
102
103 const std::vector<SkString>& getFontFamilies() const { return fFontFamilies; }
104 void setFontFamilies(std::vector<SkString> families) {
105 fFontFamilies = std::move(families);
106 }
107
108 void setHeight(SkScalar height) { fHeight = height; }
109 SkScalar getHeight() const { return fHeight; }
110
111 void setLetterSpacing(SkScalar letterSpacing) { fLetterSpacing = letterSpacing; }
112 SkScalar getLetterSpacing() const { return fLetterSpacing; }
113
114 void setWordSpacing(SkScalar wordSpacing) { fWordSpacing = wordSpacing; }
115 SkScalar getWordSpacing() const { return fWordSpacing; }
116
117 SkTypeface* getTypeface() const { return fTypeface.get(); }
118 sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
119 void setTypeface(sk_sp<SkTypeface> typeface) { fTypeface = std::move(typeface); }
120
121 SkString getLocale() const { return fLocale; }
122 void setLocale(const SkString& locale) { fLocale = locale; }
123
124 TextBaseline getTextBaseline() const { return fTextBaseline; }
125 void setTextBaseline(TextBaseline baseline) { fTextBaseline = baseline; }
126
127 // TODO: Not to use SkFontMetrics class (it has different purpose and meaning)
128 void getFontMetrics(SkFontMetrics* metrics) const {
129 SkFont font(fTypeface, fFontSize);
130 font.getMetrics(metrics);
131 metrics->fAscent =
132 (metrics->fAscent - metrics->fLeading / 2) * (fHeight == 0 ? 1 : fHeight);
133 metrics->fDescent =
134 (metrics->fDescent + metrics->fLeading / 2) * (fHeight == 0 ? 1 : fHeight);
135 }
136
137private:
138 TextDecoration fDecoration;
139 SkColor fDecorationColor;
140 TextDecorationStyle fDecorationStyle;
141 SkScalar fDecorationThicknessMultiplier;
142
143 SkFontStyle fFontStyle;
144
145 std::vector<SkString> fFontFamilies;
146 SkScalar fFontSize;
147
148 SkScalar fHeight;
149 SkString fLocale;
150 SkScalar fLetterSpacing;
151 SkScalar fWordSpacing;
152
153 TextBaseline fTextBaseline;
154
155 SkColor fColor;
156 bool fHasBackground;
157 SkPaint fBackground;
158 bool fHasForeground;
159 SkPaint fForeground;
160
161 std::vector<TextShadow> fTextShadows;
162
163 sk_sp<SkTypeface> fTypeface;
164};
165} // namespace textlayout
166} // namespace skia
167
168#endif // TextStyle_DEFINED