blob: cb12c7f5b935bba13f2a84bf3a11cf96dfd45998 [file] [log] [blame]
Julia Lavrovaa3552c52019-05-30 16:12:56 -04001// Copyright 2019 Google LLC.
Julia Lavrovaa3552c52019-05-30 16:12:56 -04002#include "include/core/SkColor.h"
3#include "include/core/SkFontStyle.h"
Mike Klein52337de2019-07-25 09:00:52 -05004#include "modules/skparagraph/include/TextStyle.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -04005
6namespace skia {
7namespace textlayout {
8
9TextStyle::TextStyle() : fFontStyle() {
Julia Lavrova5207f352019-06-21 12:22:32 -040010 fFontFamilies.reserve(1);
Julia Lavrovaa3552c52019-05-30 16:12:56 -040011 fFontFamilies.emplace_back(DEFAULT_FONT_FAMILY);
12 fColor = SK_ColorWHITE;
Julia Lavrova5207f352019-06-21 12:22:32 -040013 fDecoration.fType = TextDecoration::kNoDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040014 // Does not make sense to draw a transparent object, so we use it as a default
15 // value to indicate no decoration color was set.
Julia Lavrova5207f352019-06-21 12:22:32 -040016 fDecoration.fColor = SK_ColorTRANSPARENT;
17 fDecoration.fStyle = TextDecorationStyle::kSolid;
Julia Lavrova568f35f2020-05-14 14:23:19 -040018 // TODO: switch back to kGaps when (if) switching flutter to skparagraph
19 fDecoration.fMode = TextDecorationMode::kThrough;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040020 // Thickness is applied as a multiplier to the default thickness of the font.
Julia Lavrova5207f352019-06-21 12:22:32 -040021 fDecoration.fThicknessMultiplier = 1.0;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040022 fFontSize = 14.0;
23 fLetterSpacing = 0.0;
24 fWordSpacing = 0.0;
25 fHeight = 1.0;
Julia Lavrovadb9f6692019-08-01 16:02:17 -040026 fHeightOverride = false;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040027 fHasBackground = false;
28 fHasForeground = false;
29 fTextBaseline = TextBaseline::kAlphabetic;
Julia Lavrova35f88222019-06-21 12:22:32 -040030 fLocale = "";
Julia Lavrova916a9042019-08-08 16:51:27 -040031 fIsPlaceholder = false;
32}
33
34TextStyle:: TextStyle(const TextStyle& other, bool placeholder) {
35 fColor = other.fColor;
Julia Lavrova526df262019-08-21 17:49:44 -040036 fFontSize = other.fFontSize;
37 fFontFamilies = other.fFontFamilies;
Julia Lavrova916a9042019-08-08 16:51:27 -040038 fDecoration = other.fDecoration;
39 fHasBackground = other.fHasBackground;
40 fHasForeground = other.fHasForeground;
41 fBackground = other.fBackground;
42 fForeground = other.fForeground;
Julia Lavrova526df262019-08-21 17:49:44 -040043 fHeightOverride = other.fHeightOverride;
Julia Lavrova916a9042019-08-08 16:51:27 -040044 fIsPlaceholder = placeholder;
Julia Lavrovac5313e62019-12-10 12:11:17 -050045 fFontFeatures = other.fFontFeatures;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040046}
47
48bool TextStyle::equals(const TextStyle& other) const {
Julia Lavrova916a9042019-08-08 16:51:27 -040049
50 if (fIsPlaceholder || other.fIsPlaceholder) {
51 return false;
52 }
53
Julia Lavrovaa3552c52019-05-30 16:12:56 -040054 if (fColor != other.fColor) {
55 return false;
56 }
Julia Lavrova5207f352019-06-21 12:22:32 -040057 if (!(fDecoration == other.fDecoration)) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040058 return false;
59 }
60 if (!(fFontStyle == other.fFontStyle)) {
61 return false;
62 }
63 if (fFontFamilies != other.fFontFamilies) {
64 return false;
65 }
66 if (fLetterSpacing != other.fLetterSpacing) {
67 return false;
68 }
69 if (fWordSpacing != other.fWordSpacing) {
70 return false;
71 }
72 if (fHeight != other.fHeight) {
73 return false;
74 }
75 if (fFontSize != other.fFontSize) {
76 return false;
77 }
78 if (fLocale != other.fLocale) {
79 return false;
80 }
81 if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) {
82 return false;
83 }
84 if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) {
85 return false;
86 }
87 if (fTextShadows.size() != other.fTextShadows.size()) {
88 return false;
89 }
Julia Lavrova90bfd1c2019-12-04 11:43:32 -050090 for (size_t i = 0; i < fTextShadows.size(); ++i) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040091 if (fTextShadows[i] != other.fTextShadows[i]) {
92 return false;
93 }
94 }
Julia Lavrovac5313e62019-12-10 12:11:17 -050095 if (fFontFeatures.size() != other.fFontFeatures.size()) {
96 return false;
97 }
98 for (size_t i = 0; i < fFontFeatures.size(); ++i) {
99 if (!(fFontFeatures[i] == other.fFontFeatures[i])) {
100 return false;
101 }
102 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400103
104 return true;
105}
106
Julia Lavrova4cf18742020-01-14 13:24:45 -0500107bool TextStyle::equalsByFonts(const TextStyle& that) const {
108
109 return !fIsPlaceholder && !that.fIsPlaceholder &&
110 fFontStyle == that.fFontStyle &&
111 fFontFamilies == that.fFontFamilies &&
Julia Lavrovad3a32c52020-02-03 09:43:52 -0500112 fFontFeatures == that.fFontFeatures &&
Julia Lavrovac0360582020-02-05 10:17:53 -0500113 nearlyEqual(fLetterSpacing, that.fLetterSpacing) &&
114 nearlyEqual(fWordSpacing, that.fWordSpacing) &&
115 nearlyEqual(fHeight, that.fHeight) &&
116 nearlyEqual(fFontSize, that.fFontSize) &&
Julia Lavrova4cf18742020-01-14 13:24:45 -0500117 fLocale == that.fLocale;
118}
119
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400120bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
121 switch (styleType) {
122 case kForeground:
Julia Lavrova1d784c72020-07-08 14:08:29 -0400123 return (!fHasForeground && !other.fHasForeground && fColor == other.fColor) ||
124 ( fHasForeground && other.fHasForeground && fForeground == other.fForeground);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400125
126 case kBackground:
Julia Lavrova1d784c72020-07-08 14:08:29 -0400127 return (!fHasBackground && !other.fHasBackground) ||
128 ( fHasBackground && other.fHasBackground && fBackground == other.fBackground);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400129
130 case kShadow:
131 if (fTextShadows.size() != other.fTextShadows.size()) {
132 return false;
133 }
134
135 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
136 if (fTextShadows[i] != other.fTextShadows[i]) {
137 return false;
138 }
139 }
140 return true;
141
142 case kDecorations:
Julia Lavrova5207f352019-06-21 12:22:32 -0400143 return this->fDecoration == other.fDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400144
145 case kLetterSpacing:
146 return fLetterSpacing == other.fLetterSpacing;
147
148 case kWordSpacing:
149 return fWordSpacing == other.fWordSpacing;
150
151 case kAllAttributes:
152 return this->equals(other);
153
154 case kFont:
Julia Lavrova5207f352019-06-21 12:22:32 -0400155 // TODO: should not we take typefaces in account?
Julia Lavrova76ae22e2020-02-26 12:14:18 -0500156 return fFontStyle == other.fFontStyle &&
157 fLocale == other.fLocale &&
158 fFontFamilies == other.fFontFamilies &&
159 fFontSize == other.fFontSize &&
160 fHeight == other.fHeight;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400161 default:
162 SkASSERT(false);
163 return false;
164 }
165}
166
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400167void TextStyle::getFontMetrics(SkFontMetrics* metrics) const {
168 SkFont font(fTypeface, fFontSize);
Julia Lavrova916a9042019-08-08 16:51:27 -0400169 font.setEdging(SkFont::Edging::kAntiAlias);
170 font.setSubpixel(true);
171 font.setHinting(SkFontHinting::kSlight);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400172 font.getMetrics(metrics);
Julia Lavrovac2228562019-08-08 16:51:27 -0400173 if (fHeightOverride) {
174 auto multiplier = fHeight * fFontSize;
175 auto height = metrics->fDescent - metrics->fAscent + metrics->fLeading;
176 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2) * multiplier / height;
177 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2) * multiplier / height;
178
179 } else {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400180 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2);
181 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400182 }
183}
184
Julia Lavrova4cf18742020-01-14 13:24:45 -0500185bool PlaceholderStyle::equals(const PlaceholderStyle& other) const {
Julia Lavrovac0360582020-02-05 10:17:53 -0500186 return nearlyEqual(fWidth, other.fWidth) &&
187 nearlyEqual(fHeight, other.fHeight) &&
Julia Lavrovaaec9c842020-01-24 10:58:56 -0500188 fAlignment == other.fAlignment &&
189 fBaseline == other.fBaseline &&
Julia Lavrova7ad393e2020-01-30 09:48:51 -0500190 (fAlignment != PlaceholderAlignment::kBaseline ||
Julia Lavrovac0360582020-02-05 10:17:53 -0500191 nearlyEqual(fBaselineOffset, other.fBaselineOffset));
Julia Lavrova4cf18742020-01-14 13:24:45 -0500192}
193
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400194} // namespace textlayout
195} // namespace skia