blob: c211ab9f2ca867cf9f1c28fc4599368bcc196d23 [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 Lavrovaa3552c52019-05-30 16:12:56 -040018 // Thickness is applied as a multiplier to the default thickness of the font.
Julia Lavrova5207f352019-06-21 12:22:32 -040019 fDecoration.fThicknessMultiplier = 1.0;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040020 fFontSize = 14.0;
21 fLetterSpacing = 0.0;
22 fWordSpacing = 0.0;
23 fHeight = 1.0;
Julia Lavrovadb9f6692019-08-01 16:02:17 -040024 fHeightOverride = false;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040025 fHasBackground = false;
26 fHasForeground = false;
27 fTextBaseline = TextBaseline::kAlphabetic;
Julia Lavrova35f88222019-06-21 12:22:32 -040028 fLocale = "";
Julia Lavrova916a9042019-08-08 16:51:27 -040029 fIsPlaceholder = false;
30}
31
32TextStyle:: TextStyle(const TextStyle& other, bool placeholder) {
33 fColor = other.fColor;
Julia Lavrova526df262019-08-21 17:49:44 -040034 fFontSize = other.fFontSize;
35 fFontFamilies = other.fFontFamilies;
Julia Lavrova916a9042019-08-08 16:51:27 -040036 fDecoration = other.fDecoration;
37 fHasBackground = other.fHasBackground;
38 fHasForeground = other.fHasForeground;
39 fBackground = other.fBackground;
40 fForeground = other.fForeground;
Julia Lavrova526df262019-08-21 17:49:44 -040041 fHeightOverride = other.fHeightOverride;
Julia Lavrova916a9042019-08-08 16:51:27 -040042 fIsPlaceholder = placeholder;
Julia Lavrovac5313e62019-12-10 12:11:17 -050043 fFontFeatures = other.fFontFeatures;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040044}
45
46bool TextStyle::equals(const TextStyle& other) const {
Julia Lavrova916a9042019-08-08 16:51:27 -040047
48 if (fIsPlaceholder || other.fIsPlaceholder) {
49 return false;
50 }
51
Julia Lavrovaa3552c52019-05-30 16:12:56 -040052 if (fColor != other.fColor) {
53 return false;
54 }
Julia Lavrova5207f352019-06-21 12:22:32 -040055 if (!(fDecoration == other.fDecoration)) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040056 return false;
57 }
58 if (!(fFontStyle == other.fFontStyle)) {
59 return false;
60 }
61 if (fFontFamilies != other.fFontFamilies) {
62 return false;
63 }
64 if (fLetterSpacing != other.fLetterSpacing) {
65 return false;
66 }
67 if (fWordSpacing != other.fWordSpacing) {
68 return false;
69 }
70 if (fHeight != other.fHeight) {
71 return false;
72 }
73 if (fFontSize != other.fFontSize) {
74 return false;
75 }
76 if (fLocale != other.fLocale) {
77 return false;
78 }
79 if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) {
80 return false;
81 }
82 if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) {
83 return false;
84 }
85 if (fTextShadows.size() != other.fTextShadows.size()) {
86 return false;
87 }
Julia Lavrova90bfd1c2019-12-04 11:43:32 -050088 for (size_t i = 0; i < fTextShadows.size(); ++i) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040089 if (fTextShadows[i] != other.fTextShadows[i]) {
90 return false;
91 }
92 }
Julia Lavrovac5313e62019-12-10 12:11:17 -050093 if (fFontFeatures.size() != other.fFontFeatures.size()) {
94 return false;
95 }
96 for (size_t i = 0; i < fFontFeatures.size(); ++i) {
97 if (!(fFontFeatures[i] == other.fFontFeatures[i])) {
98 return false;
99 }
100 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400101
102 return true;
103}
104
Julia Lavrova4cf18742020-01-14 13:24:45 -0500105bool TextStyle::equalsByFonts(const TextStyle& that) const {
106
107 return !fIsPlaceholder && !that.fIsPlaceholder &&
108 fFontStyle == that.fFontStyle &&
109 fFontFamilies == that.fFontFamilies &&
Julia Lavrovad3a32c52020-02-03 09:43:52 -0500110 fFontFeatures == that.fFontFeatures &&
Julia Lavrovac0360582020-02-05 10:17:53 -0500111 nearlyEqual(fLetterSpacing, that.fLetterSpacing) &&
112 nearlyEqual(fWordSpacing, that.fWordSpacing) &&
113 nearlyEqual(fHeight, that.fHeight) &&
114 nearlyEqual(fFontSize, that.fFontSize) &&
Julia Lavrova4cf18742020-01-14 13:24:45 -0500115 fLocale == that.fLocale;
116}
117
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400118bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
119 switch (styleType) {
120 case kForeground:
121 if (fHasForeground) {
122 return other.fHasForeground && fForeground == other.fForeground;
123 } else {
124 return !other.fHasForeground && fColor == other.fColor;
125 }
126
127 case kBackground:
128 return (fHasBackground == other.fHasBackground && fBackground == other.fBackground);
129
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