blob: d4d1b81da51c71fda70b8215185c8f405fbe39bb [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 &&
110 fLetterSpacing == that.fLetterSpacing &&
111 fWordSpacing == that.fWordSpacing &&
112 fHeight == that.fHeight &&
113 fFontSize == that.fFontSize &&
114 fLocale == that.fLocale;
115}
116
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400117bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
118 switch (styleType) {
119 case kForeground:
120 if (fHasForeground) {
121 return other.fHasForeground && fForeground == other.fForeground;
122 } else {
123 return !other.fHasForeground && fColor == other.fColor;
124 }
125
126 case kBackground:
127 return (fHasBackground == other.fHasBackground && fBackground == other.fBackground);
128
129 case kShadow:
130 if (fTextShadows.size() != other.fTextShadows.size()) {
131 return false;
132 }
133
134 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
135 if (fTextShadows[i] != other.fTextShadows[i]) {
136 return false;
137 }
138 }
139 return true;
140
141 case kDecorations:
Julia Lavrova5207f352019-06-21 12:22:32 -0400142 return this->fDecoration == other.fDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400143
144 case kLetterSpacing:
145 return fLetterSpacing == other.fLetterSpacing;
146
147 case kWordSpacing:
148 return fWordSpacing == other.fWordSpacing;
149
150 case kAllAttributes:
151 return this->equals(other);
152
153 case kFont:
Julia Lavrova5207f352019-06-21 12:22:32 -0400154 // TODO: should not we take typefaces in account?
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400155 return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies &&
156 fFontSize == other.fFontSize && fHeight == other.fHeight;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400157 default:
158 SkASSERT(false);
159 return false;
160 }
161}
162
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400163void TextStyle::getFontMetrics(SkFontMetrics* metrics) const {
164 SkFont font(fTypeface, fFontSize);
Julia Lavrova916a9042019-08-08 16:51:27 -0400165 font.setEdging(SkFont::Edging::kAntiAlias);
166 font.setSubpixel(true);
167 font.setHinting(SkFontHinting::kSlight);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400168 font.getMetrics(metrics);
Julia Lavrovac2228562019-08-08 16:51:27 -0400169 if (fHeightOverride) {
170 auto multiplier = fHeight * fFontSize;
171 auto height = metrics->fDescent - metrics->fAscent + metrics->fLeading;
172 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2) * multiplier / height;
173 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2) * multiplier / height;
174
175 } else {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400176 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2);
177 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400178 }
179}
180
Julia Lavrova4cf18742020-01-14 13:24:45 -0500181bool PlaceholderStyle::equals(const PlaceholderStyle& other) const {
182 return this->fWidth == other.fWidth &&
183 this->fHeight == other.fHeight &&
184 this->fAlignment == other.fAlignment &&
185 this->fBaseline == other.fBaseline &&
186 this->fBaselineOffset == other.fBaselineOffset;
187}
188
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400189} // namespace textlayout
190} // namespace skia