blob: 4dfa070b3653326348030a0318181e93b6807377 [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 Lavrova18db52f2020-05-04 15:03:18 -040018 fDecoration.fMode = TextDecorationMode::kGaps;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040019 // Thickness is applied as a multiplier to the default thickness of the font.
Julia Lavrova5207f352019-06-21 12:22:32 -040020 fDecoration.fThicknessMultiplier = 1.0;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040021 fFontSize = 14.0;
22 fLetterSpacing = 0.0;
23 fWordSpacing = 0.0;
24 fHeight = 1.0;
Julia Lavrovadb9f6692019-08-01 16:02:17 -040025 fHeightOverride = false;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040026 fHasBackground = false;
27 fHasForeground = false;
28 fTextBaseline = TextBaseline::kAlphabetic;
Julia Lavrova35f88222019-06-21 12:22:32 -040029 fLocale = "";
Julia Lavrova916a9042019-08-08 16:51:27 -040030 fIsPlaceholder = false;
31}
32
33TextStyle:: TextStyle(const TextStyle& other, bool placeholder) {
34 fColor = other.fColor;
Julia Lavrova526df262019-08-21 17:49:44 -040035 fFontSize = other.fFontSize;
36 fFontFamilies = other.fFontFamilies;
Julia Lavrova916a9042019-08-08 16:51:27 -040037 fDecoration = other.fDecoration;
38 fHasBackground = other.fHasBackground;
39 fHasForeground = other.fHasForeground;
40 fBackground = other.fBackground;
41 fForeground = other.fForeground;
Julia Lavrova526df262019-08-21 17:49:44 -040042 fHeightOverride = other.fHeightOverride;
Julia Lavrova916a9042019-08-08 16:51:27 -040043 fIsPlaceholder = placeholder;
Julia Lavrovac5313e62019-12-10 12:11:17 -050044 fFontFeatures = other.fFontFeatures;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040045}
46
47bool TextStyle::equals(const TextStyle& other) const {
Julia Lavrova916a9042019-08-08 16:51:27 -040048
49 if (fIsPlaceholder || other.fIsPlaceholder) {
50 return false;
51 }
52
Julia Lavrovaa3552c52019-05-30 16:12:56 -040053 if (fColor != other.fColor) {
54 return false;
55 }
Julia Lavrova5207f352019-06-21 12:22:32 -040056 if (!(fDecoration == other.fDecoration)) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040057 return false;
58 }
59 if (!(fFontStyle == other.fFontStyle)) {
60 return false;
61 }
62 if (fFontFamilies != other.fFontFamilies) {
63 return false;
64 }
65 if (fLetterSpacing != other.fLetterSpacing) {
66 return false;
67 }
68 if (fWordSpacing != other.fWordSpacing) {
69 return false;
70 }
71 if (fHeight != other.fHeight) {
72 return false;
73 }
74 if (fFontSize != other.fFontSize) {
75 return false;
76 }
77 if (fLocale != other.fLocale) {
78 return false;
79 }
80 if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) {
81 return false;
82 }
83 if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) {
84 return false;
85 }
86 if (fTextShadows.size() != other.fTextShadows.size()) {
87 return false;
88 }
Julia Lavrova90bfd1c2019-12-04 11:43:32 -050089 for (size_t i = 0; i < fTextShadows.size(); ++i) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040090 if (fTextShadows[i] != other.fTextShadows[i]) {
91 return false;
92 }
93 }
Julia Lavrovac5313e62019-12-10 12:11:17 -050094 if (fFontFeatures.size() != other.fFontFeatures.size()) {
95 return false;
96 }
97 for (size_t i = 0; i < fFontFeatures.size(); ++i) {
98 if (!(fFontFeatures[i] == other.fFontFeatures[i])) {
99 return false;
100 }
101 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400102
103 return true;
104}
105
Julia Lavrova4cf18742020-01-14 13:24:45 -0500106bool TextStyle::equalsByFonts(const TextStyle& that) const {
107
108 return !fIsPlaceholder && !that.fIsPlaceholder &&
109 fFontStyle == that.fFontStyle &&
110 fFontFamilies == that.fFontFamilies &&
Julia Lavrovad3a32c52020-02-03 09:43:52 -0500111 fFontFeatures == that.fFontFeatures &&
Julia Lavrovac0360582020-02-05 10:17:53 -0500112 nearlyEqual(fLetterSpacing, that.fLetterSpacing) &&
113 nearlyEqual(fWordSpacing, that.fWordSpacing) &&
114 nearlyEqual(fHeight, that.fHeight) &&
115 nearlyEqual(fFontSize, that.fFontSize) &&
Julia Lavrova4cf18742020-01-14 13:24:45 -0500116 fLocale == that.fLocale;
117}
118
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400119bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
120 switch (styleType) {
121 case kForeground:
122 if (fHasForeground) {
123 return other.fHasForeground && fForeground == other.fForeground;
124 } else {
125 return !other.fHasForeground && fColor == other.fColor;
126 }
127
128 case kBackground:
129 return (fHasBackground == other.fHasBackground && fBackground == other.fBackground);
130
131 case kShadow:
132 if (fTextShadows.size() != other.fTextShadows.size()) {
133 return false;
134 }
135
136 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
137 if (fTextShadows[i] != other.fTextShadows[i]) {
138 return false;
139 }
140 }
141 return true;
142
143 case kDecorations:
Julia Lavrova5207f352019-06-21 12:22:32 -0400144 return this->fDecoration == other.fDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400145
146 case kLetterSpacing:
147 return fLetterSpacing == other.fLetterSpacing;
148
149 case kWordSpacing:
150 return fWordSpacing == other.fWordSpacing;
151
152 case kAllAttributes:
153 return this->equals(other);
154
155 case kFont:
Julia Lavrova5207f352019-06-21 12:22:32 -0400156 // TODO: should not we take typefaces in account?
Julia Lavrova76ae22e2020-02-26 12:14:18 -0500157 return fFontStyle == other.fFontStyle &&
158 fLocale == other.fLocale &&
159 fFontFamilies == other.fFontFamilies &&
160 fFontSize == other.fFontSize &&
161 fHeight == other.fHeight;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400162 default:
163 SkASSERT(false);
164 return false;
165 }
166}
167
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400168void TextStyle::getFontMetrics(SkFontMetrics* metrics) const {
169 SkFont font(fTypeface, fFontSize);
Julia Lavrova916a9042019-08-08 16:51:27 -0400170 font.setEdging(SkFont::Edging::kAntiAlias);
171 font.setSubpixel(true);
172 font.setHinting(SkFontHinting::kSlight);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400173 font.getMetrics(metrics);
Julia Lavrovac2228562019-08-08 16:51:27 -0400174 if (fHeightOverride) {
175 auto multiplier = fHeight * fFontSize;
176 auto height = metrics->fDescent - metrics->fAscent + metrics->fLeading;
177 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2) * multiplier / height;
178 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2) * multiplier / height;
179
180 } else {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400181 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2);
182 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400183 }
184}
185
Julia Lavrova4cf18742020-01-14 13:24:45 -0500186bool PlaceholderStyle::equals(const PlaceholderStyle& other) const {
Julia Lavrovac0360582020-02-05 10:17:53 -0500187 return nearlyEqual(fWidth, other.fWidth) &&
188 nearlyEqual(fHeight, other.fHeight) &&
Julia Lavrovaaec9c842020-01-24 10:58:56 -0500189 fAlignment == other.fAlignment &&
190 fBaseline == other.fBaseline &&
Julia Lavrova7ad393e2020-01-30 09:48:51 -0500191 (fAlignment != PlaceholderAlignment::kBaseline ||
Julia Lavrovac0360582020-02-05 10:17:53 -0500192 nearlyEqual(fBaselineOffset, other.fBaselineOffset));
Julia Lavrova4cf18742020-01-14 13:24:45 -0500193}
194
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400195} // namespace textlayout
196} // namespace skia