blob: 85a387b1506f7c74f86d514904629d2dbcbf6b41 [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"
Greg Danielf91aeb22019-06-18 09:58:02 -04004#include "modules/skparagraph/include/TextStyle.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -04005
6namespace skia {
7namespace textlayout {
8
9TextStyle::TextStyle() : fFontStyle() {
10 fFontFamilies.emplace_back(DEFAULT_FONT_FAMILY);
11 fColor = SK_ColorWHITE;
12 fDecoration = TextDecoration::kNoDecoration;
13 // Does not make sense to draw a transparent object, so we use it as a default
14 // value to indicate no decoration color was set.
15 fDecorationColor = SK_ColorTRANSPARENT;
16 fDecorationStyle = TextDecorationStyle::kSolid;
17 // Thickness is applied as a multiplier to the default thickness of the font.
18 fDecorationThicknessMultiplier = 1.0;
19 fFontSize = 14.0;
20 fLetterSpacing = 0.0;
21 fWordSpacing = 0.0;
22 fHeight = 1.0;
23 fHasBackground = false;
24 fHasForeground = false;
25 fTextBaseline = TextBaseline::kAlphabetic;
26}
27
28bool TextStyle::equals(const TextStyle& other) const {
29 if (fColor != other.fColor) {
30 return false;
31 }
32 if (fDecoration != other.fDecoration) {
33 return false;
34 }
35 if (fDecorationColor != other.fDecorationColor) {
36 return false;
37 }
38 if (fDecorationStyle != other.fDecorationStyle) {
39 return false;
40 }
41 if (fDecorationThicknessMultiplier != other.fDecorationThicknessMultiplier) {
42 return false;
43 }
44 if (!(fFontStyle == other.fFontStyle)) {
45 return false;
46 }
47 if (fFontFamilies != other.fFontFamilies) {
48 return false;
49 }
50 if (fLetterSpacing != other.fLetterSpacing) {
51 return false;
52 }
53 if (fWordSpacing != other.fWordSpacing) {
54 return false;
55 }
56 if (fHeight != other.fHeight) {
57 return false;
58 }
59 if (fFontSize != other.fFontSize) {
60 return false;
61 }
62 if (fLocale != other.fLocale) {
63 return false;
64 }
65 if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) {
66 return false;
67 }
68 if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) {
69 return false;
70 }
71 if (fTextShadows.size() != other.fTextShadows.size()) {
72 return false;
73 }
74
75 for (int32_t i = 0; i < (int32_t)fTextShadows.size(); ++i) {
76 if (fTextShadows[i] != other.fTextShadows[i]) {
77 return false;
78 }
79 }
80
81 return true;
82}
83
84bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
85 switch (styleType) {
86 case kForeground:
87 if (fHasForeground) {
88 return other.fHasForeground && fForeground == other.fForeground;
89 } else {
90 return !other.fHasForeground && fColor == other.fColor;
91 }
92
93 case kBackground:
94 return (fHasBackground == other.fHasBackground && fBackground == other.fBackground);
95
96 case kShadow:
97 if (fTextShadows.size() != other.fTextShadows.size()) {
98 return false;
99 }
100
101 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
102 if (fTextShadows[i] != other.fTextShadows[i]) {
103 return false;
104 }
105 }
106 return true;
107
108 case kDecorations:
109 return fDecoration == other.fDecoration && fDecorationColor == other.fDecorationColor &&
110 fDecorationStyle == other.fDecorationStyle &&
111 fDecorationThicknessMultiplier == other.fDecorationThicknessMultiplier;
112
113 case kLetterSpacing:
114 return fLetterSpacing == other.fLetterSpacing;
115
116 case kWordSpacing:
117 return fWordSpacing == other.fWordSpacing;
118
119 case kAllAttributes:
120 return this->equals(other);
121
122 case kFont:
123 return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies &&
124 fFontSize == other.fFontSize && fHeight == other.fHeight;
125
126 default:
127 SkASSERT(false);
128 return false;
129 }
130}
131
132} // namespace textlayout
133} // namespace skia