blob: 204df8fd3717d4a503a64b1c829057b31f878dea [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;
Julia Lavrova35f88222019-06-21 12:22:32 -040026 fLocale = "";
Julia Lavrovaa3552c52019-05-30 16:12:56 -040027}
28
29bool TextStyle::equals(const TextStyle& other) const {
30 if (fColor != other.fColor) {
31 return false;
32 }
33 if (fDecoration != other.fDecoration) {
34 return false;
35 }
36 if (fDecorationColor != other.fDecorationColor) {
37 return false;
38 }
39 if (fDecorationStyle != other.fDecorationStyle) {
40 return false;
41 }
42 if (fDecorationThicknessMultiplier != other.fDecorationThicknessMultiplier) {
43 return false;
44 }
45 if (!(fFontStyle == other.fFontStyle)) {
46 return false;
47 }
48 if (fFontFamilies != other.fFontFamilies) {
49 return false;
50 }
51 if (fLetterSpacing != other.fLetterSpacing) {
52 return false;
53 }
54 if (fWordSpacing != other.fWordSpacing) {
55 return false;
56 }
57 if (fHeight != other.fHeight) {
58 return false;
59 }
60 if (fFontSize != other.fFontSize) {
61 return false;
62 }
63 if (fLocale != other.fLocale) {
64 return false;
65 }
66 if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) {
67 return false;
68 }
69 if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) {
70 return false;
71 }
72 if (fTextShadows.size() != other.fTextShadows.size()) {
73 return false;
74 }
75
76 for (int32_t i = 0; i < (int32_t)fTextShadows.size(); ++i) {
77 if (fTextShadows[i] != other.fTextShadows[i]) {
78 return false;
79 }
80 }
81
82 return true;
83}
84
85bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
86 switch (styleType) {
87 case kForeground:
88 if (fHasForeground) {
89 return other.fHasForeground && fForeground == other.fForeground;
90 } else {
91 return !other.fHasForeground && fColor == other.fColor;
92 }
93
94 case kBackground:
95 return (fHasBackground == other.fHasBackground && fBackground == other.fBackground);
96
97 case kShadow:
98 if (fTextShadows.size() != other.fTextShadows.size()) {
99 return false;
100 }
101
102 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
103 if (fTextShadows[i] != other.fTextShadows[i]) {
104 return false;
105 }
106 }
107 return true;
108
109 case kDecorations:
110 return fDecoration == other.fDecoration && fDecorationColor == other.fDecorationColor &&
111 fDecorationStyle == other.fDecorationStyle &&
112 fDecorationThicknessMultiplier == other.fDecorationThicknessMultiplier;
113
114 case kLetterSpacing:
115 return fLetterSpacing == other.fLetterSpacing;
116
117 case kWordSpacing:
118 return fWordSpacing == other.fWordSpacing;
119
120 case kAllAttributes:
121 return this->equals(other);
122
123 case kFont:
124 return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies &&
125 fFontSize == other.fFontSize && fHeight == other.fHeight;
126
127 default:
128 SkASSERT(false);
129 return false;
130 }
131}
132
133} // namespace textlayout
134} // namespace skia