1 // Copyright 2019 Google LLC.
2 #include "include/core/SkColor.h"
3 #include "include/core/SkFontStyle.h"
4 #include "modules/skparagraph/include/TextStyle.h"
5 #include "modules/skparagraph/src/Run.h"
6
7 namespace skia {
8 namespace textlayout {
9
10 #ifdef OHOS_SUPPORT
11 struct SkStringHash {
operator ()skia::textlayout::SkStringHash12 size_t operator()(const SkString& s) const {
13 size_t hash = 0;
14 for (size_t i = 0; i < s.size(); ++i) {
15 hash ^= std::hash<char>()(s.c_str()[i]);
16 }
17 return hash;
18 }
19 };
20
21 const std::unordered_map<SkString, SkString, SkStringHash> GENERIC_FAMILY_NAME_MAP = {
22 { SkString{"HarmonyOS Sans"}, SkString{"HarmonyOS-Sans"} },
23 { SkString{"HarmonyOS Sans Condensed"}, SkString{"HarmonyOS-Sans-Condensed"} },
24 { SkString{"HarmonyOS Sans Digit"}, SkString{"HarmonyOS-Sans-Digit"} },
25 { SkString{"Noto Serif"}, SkString{"serif"} },
26 { SkString{"Noto Sans Mono"}, SkString{"monospace"} }
27 };
28 #endif
29
30 const std::vector<SkString>* TextStyle::kDefaultFontFamilies =
31 new std::vector<SkString>{SkString(DEFAULT_FONT_FAMILY)};
32
cloneForPlaceholder()33 TextStyle TextStyle::cloneForPlaceholder() {
34 TextStyle result;
35 result.fColor = fColor;
36 result.fFontSize = fFontSize;
37 result.fFontFamilies = fFontFamilies;
38 result.fDecoration = fDecoration;
39 result.fHasBackground = fHasBackground;
40 result.fHasForeground = fHasForeground;
41 result.fBackground = fBackground;
42 result.fForeground = fForeground;
43 result.fHeightOverride = fHeightOverride;
44 result.fIsPlaceholder = true;
45 result.fFontFeatures = fFontFeatures;
46 result.fHalfLeading = fHalfLeading;
47 result.fBaselineShift = fBaselineShift;
48 result.fFontArguments = fFontArguments;
49 result.fBackgroundRect = fBackgroundRect;
50 result.fStyleId = fStyleId;
51 result.fTextStyleUid = fTextStyleUid;
52 return result;
53 }
54
equals(const TextStyle & other) const55 bool TextStyle::equals(const TextStyle& other) const {
56
57 if (fIsPlaceholder || other.fIsPlaceholder) {
58 return false;
59 }
60
61 if (fColor != other.fColor) {
62 return false;
63 }
64 if (!(fDecoration == other.fDecoration)) {
65 return false;
66 }
67 if (!(fFontStyle == other.fFontStyle)) {
68 return false;
69 }
70 if (fFontFamilies != other.fFontFamilies) {
71 return false;
72 }
73 if (fLetterSpacing != other.fLetterSpacing) {
74 return false;
75 }
76 if (fWordSpacing != other.fWordSpacing) {
77 return false;
78 }
79 if (fHeight != other.fHeight) {
80 return false;
81 }
82 if (fHeightOverride != other.fHeightOverride) {
83 return false;
84 }
85 if (fHalfLeading != other.fHalfLeading) {
86 return false;
87 }
88 if (fBaselineShift != other.fBaselineShift) {
89 return false;
90 }
91 if (fFontSize != other.fFontSize) {
92 return false;
93 }
94 if (fLocale != other.fLocale) {
95 return false;
96 }
97 if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) {
98 return false;
99 }
100 if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) {
101 return false;
102 }
103 if (fTextShadows.size() != other.fTextShadows.size()) {
104 return false;
105 }
106 for (size_t i = 0; i < fTextShadows.size(); ++i) {
107 if (fTextShadows[i] != other.fTextShadows[i]) {
108 return false;
109 }
110 }
111 if (fFontFeatures.size() != other.fFontFeatures.size()) {
112 return false;
113 }
114 for (size_t i = 0; i < fFontFeatures.size(); ++i) {
115 if (!(fFontFeatures[i] == other.fFontFeatures[i])) {
116 return false;
117 }
118 }
119 if (fFontArguments != other.fFontArguments) {
120 return false;
121 }
122 if (fStyleId != other.fStyleId || fBackgroundRect != other.fBackgroundRect) {
123 return false;
124 }
125
126 return true;
127 }
128
equalsByFonts(const TextStyle & that) const129 bool TextStyle::equalsByFonts(const TextStyle& that) const {
130
131 return !fIsPlaceholder && !that.fIsPlaceholder &&
132 fFontStyle == that.fFontStyle &&
133 fFontFamilies == that.fFontFamilies &&
134 fFontFeatures == that.fFontFeatures &&
135 fFontArguments == that.getFontArguments() &&
136 nearlyEqual(fLetterSpacing, that.fLetterSpacing) &&
137 nearlyEqual(fWordSpacing, that.fWordSpacing) &&
138 nearlyEqual(fHeight, that.fHeight) &&
139 nearlyEqual(fBaselineShift, that.fBaselineShift) &&
140 nearlyEqual(fFontSize, that.fFontSize) &&
141 fLocale == that.fLocale &&
142 fStyleId == that.fStyleId &&
143 fBackgroundRect == that.fBackgroundRect;
144 }
145
matchOneAttribute(StyleType styleType,const TextStyle & other) const146 bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
147 switch (styleType) {
148 case kForeground:
149 return (!fHasForeground && !other.fHasForeground && fColor == other.fColor) ||
150 ( fHasForeground && other.fHasForeground && fForeground == other.fForeground);
151
152 case kBackground:
153 return (!fHasBackground && !other.fHasBackground) ||
154 ( fHasBackground && other.fHasBackground && fBackground == other.fBackground);
155
156 case kShadow:
157 if (fTextShadows.size() != other.fTextShadows.size()) {
158 return false;
159 }
160
161 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
162 if (fTextShadows[i] != other.fTextShadows[i]) {
163 return false;
164 }
165 }
166 return true;
167
168 case kDecorations:
169 return this->fDecoration == other.fDecoration;
170
171 case kLetterSpacing:
172 return fLetterSpacing == other.fLetterSpacing;
173
174 case kWordSpacing:
175 return fWordSpacing == other.fWordSpacing;
176
177 case kAllAttributes:
178 return this->equals(other);
179
180 case kFont:
181 // TODO: should not we take typefaces in account?
182 return fFontStyle == other.fFontStyle &&
183 fLocale == other.fLocale &&
184 fFontFamilies == other.fFontFamilies &&
185 #ifdef OHOS_SUPPORT
186 getCorrectFontSize() == other.getCorrectFontSize() &&
187 fHeight == other.fHeight &&
188 fHalfLeading == other.fHalfLeading &&
189 getTotalVerticalShift() == other.getTotalVerticalShift() &&
190 #else
191 fFontSize == other.fFontSize &&
192 fHeight == other.fHeight &&
193 fHalfLeading == other.fHalfLeading &&
194 fBaselineShift == other.fBaselineShift &&
195 #endif
196 fFontArguments == other.fFontArguments &&
197 fStyleId == other.fStyleId &&
198 fBackgroundRect == other.fBackgroundRect;
199 default:
200 SkASSERT(false);
201 return false;
202 }
203 }
204
205 #ifndef USE_SKIA_TXT
getFontMetrics(SkFontMetrics * metrics) const206 void TextStyle::getFontMetrics(SkFontMetrics* metrics) const {
207 #else
208 void TextStyle::getFontMetrics(RSFontMetrics* metrics) const {
209 #endif
210 #ifndef USE_SKIA_TXT
211 SkFont font(fTypeface, fFontSize);
212 font.setEdging(SkFont::Edging::kAntiAlias);
213 font.setSubpixel(true);
214 font.setHinting(SkFontHinting::kSlight);
215 #ifdef OHOS_SUPPORT
216 auto compressFont = font;
217 scaleFontWithCompressionConfig(compressFont, ScaleOP::COMPRESS);
218 compressFont.getMetrics(metrics);
219 metricsIncludeFontPadding(metrics, font);
220 #else
221 font.getMetrics(metrics);
222 #endif
223 #else
224 RSFont font(fTypeface, fFontSize, 1, 0);
225 font.SetEdging(RSDrawing::FontEdging::ANTI_ALIAS);
226 font.SetHinting(RSDrawing::FontHinting::SLIGHT);
227 font.SetSubpixel(true);
228 #ifdef OHOS_SUPPORT
229 auto compressFont = font;
230 scaleFontWithCompressionConfig(compressFont, ScaleOP::COMPRESS);
231 compressFont.GetMetrics(metrics);
232 metricsIncludeFontPadding(metrics, font);
233 #else
234 font.GetMetrics(metrics);
235 #endif
236 #endif
237 if (fHeightOverride) {
238 auto multiplier = fHeight * fFontSize;
239 auto height = metrics->fDescent - metrics->fAscent + metrics->fLeading;
240 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2) * multiplier / height;
241 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2) * multiplier / height;
242
243 } else {
244 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2);
245 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2);
246 }
247 // If we shift the baseline we need to make sure the shifted text fits the line
248 metrics->fAscent += fBaselineShift;
249 metrics->fDescent += fBaselineShift;
250 }
251
252 void TextStyle::setFontArguments(const std::optional<SkFontArguments>& args) {
253 if (!args) {
254 fFontArguments.reset();
255 return;
256 }
257
258 fFontArguments.emplace(*args);
259 }
260
261 bool PlaceholderStyle::equals(const PlaceholderStyle& other) const {
262 return nearlyEqual(fWidth, other.fWidth) &&
263 nearlyEqual(fHeight, other.fHeight) &&
264 fAlignment == other.fAlignment &&
265 fBaseline == other.fBaseline &&
266 (fAlignment != PlaceholderAlignment::kBaseline ||
267 nearlyEqual(fBaselineOffset, other.fBaselineOffset));
268 }
269
270 #ifdef OHOS_SUPPORT
271 void TextStyle::setFontFamilies(std::vector<SkString> families) {
272 std::for_each(families.begin(), families.end(), [](SkString& familyName) {
273 if (GENERIC_FAMILY_NAME_MAP.count(familyName)) {
274 familyName = GENERIC_FAMILY_NAME_MAP.at(familyName);
275 }
276 });
277 fFontFamilies = std::move(families);
278 }
279
280 SkScalar TextStyle::getBadgeBaseLineShift() const {
281 if (getTextBadgeType() == TextBadgeType::BADGE_NONE) {
282 return 0.0;
283 }
284
285 SkScalar actualFontSize = getFontSize() * TEXT_BADGE_FONT_SIZE_SCALE;
286 return getTextBadgeType() == TextBadgeType::SUPERSCRIPT ? actualFontSize * SUPERSCRIPT_BASELINE_SHIFT_SCALE :
287 actualFontSize * SUBSCRIPT_BASELINE_SHIFT_SCALE;
288 }
289
290 SkScalar TextStyle::getCorrectFontSize() const {
291 if (getTextBadgeType() == TextBadgeType::BADGE_NONE) {
292 return getFontSize();
293 }
294
295 return getFontSize() * TEXT_BADGE_FONT_SIZE_SCALE;
296 };
297 #endif
298
299 } // namespace textlayout
300 } // namespace skia
301