blob: e3d645c917fc372dfbce4aa9ebcddc22f7e20914 [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 Lavrovaaec9c842020-01-24 10:58:56 -0500111 SkScalarNearlyEqual(fLetterSpacing, that.fLetterSpacing) &&
112 SkScalarNearlyEqual(fWordSpacing, that.fWordSpacing) &&
113 SkScalarNearlyEqual(fHeight, that.fHeight) &&
114 SkScalarNearlyEqual(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 Lavrovaa3552c52019-05-30 16:12:56 -0400156 return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies &&
157 fFontSize == other.fFontSize && fHeight == other.fHeight;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400158 default:
159 SkASSERT(false);
160 return false;
161 }
162}
163
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400164void TextStyle::getFontMetrics(SkFontMetrics* metrics) const {
165 SkFont font(fTypeface, fFontSize);
Julia Lavrova916a9042019-08-08 16:51:27 -0400166 font.setEdging(SkFont::Edging::kAntiAlias);
167 font.setSubpixel(true);
168 font.setHinting(SkFontHinting::kSlight);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400169 font.getMetrics(metrics);
Julia Lavrovac2228562019-08-08 16:51:27 -0400170 if (fHeightOverride) {
171 auto multiplier = fHeight * fFontSize;
172 auto height = metrics->fDescent - metrics->fAscent + metrics->fLeading;
173 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2) * multiplier / height;
174 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2) * multiplier / height;
175
176 } else {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400177 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2);
178 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400179 }
180}
181
Julia Lavrova4cf18742020-01-14 13:24:45 -0500182bool PlaceholderStyle::equals(const PlaceholderStyle& other) const {
Julia Lavrovaaec9c842020-01-24 10:58:56 -0500183 return SkScalarNearlyEqual(fWidth, other.fWidth) &&
184 SkScalarNearlyEqual(fHeight, other.fHeight) &&
185 fAlignment == other.fAlignment &&
186 fBaseline == other.fBaseline &&
Julia Lavrova7ad393e2020-01-30 09:48:51 -0500187 (fAlignment != PlaceholderAlignment::kBaseline ||
188 SkScalarNearlyEqual(fBaselineOffset, other.fBaselineOffset));
Julia Lavrova4cf18742020-01-14 13:24:45 -0500189}
190
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400191} // namespace textlayout
192} // namespace skia