blob: 0299f465ece365e92e442df65735b5048055e46d [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
John Stiles059ed672020-07-27 15:53:28 -04009TextStyle::TextStyle(const TextStyle& other, bool placeholder) {
Julia Lavrova916a9042019-08-08 16:51:27 -040010 fColor = other.fColor;
Julia Lavrova526df262019-08-21 17:49:44 -040011 fFontSize = other.fFontSize;
12 fFontFamilies = other.fFontFamilies;
Julia Lavrova916a9042019-08-08 16:51:27 -040013 fDecoration = other.fDecoration;
14 fHasBackground = other.fHasBackground;
15 fHasForeground = other.fHasForeground;
16 fBackground = other.fBackground;
17 fForeground = other.fForeground;
Julia Lavrova526df262019-08-21 17:49:44 -040018 fHeightOverride = other.fHeightOverride;
Julia Lavrova916a9042019-08-08 16:51:27 -040019 fIsPlaceholder = placeholder;
Julia Lavrovac5313e62019-12-10 12:11:17 -050020 fFontFeatures = other.fFontFeatures;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040021}
22
23bool TextStyle::equals(const TextStyle& other) const {
Julia Lavrova916a9042019-08-08 16:51:27 -040024
25 if (fIsPlaceholder || other.fIsPlaceholder) {
26 return false;
27 }
28
Julia Lavrovaa3552c52019-05-30 16:12:56 -040029 if (fColor != other.fColor) {
30 return false;
31 }
Julia Lavrova5207f352019-06-21 12:22:32 -040032 if (!(fDecoration == other.fDecoration)) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040033 return false;
34 }
35 if (!(fFontStyle == other.fFontStyle)) {
36 return false;
37 }
38 if (fFontFamilies != other.fFontFamilies) {
39 return false;
40 }
41 if (fLetterSpacing != other.fLetterSpacing) {
42 return false;
43 }
44 if (fWordSpacing != other.fWordSpacing) {
45 return false;
46 }
47 if (fHeight != other.fHeight) {
48 return false;
49 }
50 if (fFontSize != other.fFontSize) {
51 return false;
52 }
53 if (fLocale != other.fLocale) {
54 return false;
55 }
56 if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) {
57 return false;
58 }
59 if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) {
60 return false;
61 }
62 if (fTextShadows.size() != other.fTextShadows.size()) {
63 return false;
64 }
Julia Lavrova90bfd1c2019-12-04 11:43:32 -050065 for (size_t i = 0; i < fTextShadows.size(); ++i) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040066 if (fTextShadows[i] != other.fTextShadows[i]) {
67 return false;
68 }
69 }
Julia Lavrovac5313e62019-12-10 12:11:17 -050070 if (fFontFeatures.size() != other.fFontFeatures.size()) {
71 return false;
72 }
73 for (size_t i = 0; i < fFontFeatures.size(); ++i) {
74 if (!(fFontFeatures[i] == other.fFontFeatures[i])) {
75 return false;
76 }
77 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -040078
79 return true;
80}
81
Julia Lavrova4cf18742020-01-14 13:24:45 -050082bool TextStyle::equalsByFonts(const TextStyle& that) const {
83
84 return !fIsPlaceholder && !that.fIsPlaceholder &&
85 fFontStyle == that.fFontStyle &&
86 fFontFamilies == that.fFontFamilies &&
Julia Lavrovad3a32c52020-02-03 09:43:52 -050087 fFontFeatures == that.fFontFeatures &&
Julia Lavrovac0360582020-02-05 10:17:53 -050088 nearlyEqual(fLetterSpacing, that.fLetterSpacing) &&
89 nearlyEqual(fWordSpacing, that.fWordSpacing) &&
90 nearlyEqual(fHeight, that.fHeight) &&
91 nearlyEqual(fFontSize, that.fFontSize) &&
Julia Lavrova4cf18742020-01-14 13:24:45 -050092 fLocale == that.fLocale;
93}
94
Julia Lavrovaa3552c52019-05-30 16:12:56 -040095bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
96 switch (styleType) {
97 case kForeground:
Julia Lavrova1d784c72020-07-08 14:08:29 -040098 return (!fHasForeground && !other.fHasForeground && fColor == other.fColor) ||
99 ( fHasForeground && other.fHasForeground && fForeground == other.fForeground);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400100
101 case kBackground:
Julia Lavrova1d784c72020-07-08 14:08:29 -0400102 return (!fHasBackground && !other.fHasBackground) ||
103 ( fHasBackground && other.fHasBackground && fBackground == other.fBackground);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400104
105 case kShadow:
106 if (fTextShadows.size() != other.fTextShadows.size()) {
107 return false;
108 }
109
110 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
111 if (fTextShadows[i] != other.fTextShadows[i]) {
112 return false;
113 }
114 }
115 return true;
116
117 case kDecorations:
Julia Lavrova5207f352019-06-21 12:22:32 -0400118 return this->fDecoration == other.fDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400119
120 case kLetterSpacing:
121 return fLetterSpacing == other.fLetterSpacing;
122
123 case kWordSpacing:
124 return fWordSpacing == other.fWordSpacing;
125
126 case kAllAttributes:
127 return this->equals(other);
128
129 case kFont:
Julia Lavrova5207f352019-06-21 12:22:32 -0400130 // TODO: should not we take typefaces in account?
Julia Lavrova76ae22e2020-02-26 12:14:18 -0500131 return fFontStyle == other.fFontStyle &&
132 fLocale == other.fLocale &&
133 fFontFamilies == other.fFontFamilies &&
134 fFontSize == other.fFontSize &&
135 fHeight == other.fHeight;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400136 default:
137 SkASSERT(false);
138 return false;
139 }
140}
141
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400142void TextStyle::getFontMetrics(SkFontMetrics* metrics) const {
143 SkFont font(fTypeface, fFontSize);
Julia Lavrova916a9042019-08-08 16:51:27 -0400144 font.setEdging(SkFont::Edging::kAntiAlias);
145 font.setSubpixel(true);
146 font.setHinting(SkFontHinting::kSlight);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400147 font.getMetrics(metrics);
Julia Lavrovac2228562019-08-08 16:51:27 -0400148 if (fHeightOverride) {
149 auto multiplier = fHeight * fFontSize;
150 auto height = metrics->fDescent - metrics->fAscent + metrics->fLeading;
151 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2) * multiplier / height;
152 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2) * multiplier / height;
153
154 } else {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400155 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2);
156 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400157 }
158}
159
Julia Lavrova4cf18742020-01-14 13:24:45 -0500160bool PlaceholderStyle::equals(const PlaceholderStyle& other) const {
Julia Lavrovac0360582020-02-05 10:17:53 -0500161 return nearlyEqual(fWidth, other.fWidth) &&
162 nearlyEqual(fHeight, other.fHeight) &&
Julia Lavrovaaec9c842020-01-24 10:58:56 -0500163 fAlignment == other.fAlignment &&
164 fBaseline == other.fBaseline &&
Julia Lavrova7ad393e2020-01-30 09:48:51 -0500165 (fAlignment != PlaceholderAlignment::kBaseline ||
Julia Lavrovac0360582020-02-05 10:17:53 -0500166 nearlyEqual(fBaselineOffset, other.fBaselineOffset));
Julia Lavrova4cf18742020-01-14 13:24:45 -0500167}
168
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400169} // namespace textlayout
170} // namespace skia