blob: 630482724f6a385e5e0e64fcb85131471d69ca53 [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 Lavrovaa3552c52019-05-30 16:12:56 -040018 // Thickness is applied as a multiplier to the default thickness of the font.
Julia Lavrova5207f352019-06-21 12:22:32 -040019 fDecoration.fThicknessMultiplier = 1.0;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040020 fFontSize = 14.0;
21 fLetterSpacing = 0.0;
22 fWordSpacing = 0.0;
23 fHeight = 1.0;
24 fHasBackground = false;
25 fHasForeground = false;
26 fTextBaseline = TextBaseline::kAlphabetic;
Julia Lavrova35f88222019-06-21 12:22:32 -040027 fLocale = "";
Julia Lavrovaa3552c52019-05-30 16:12:56 -040028}
29
30bool TextStyle::equals(const TextStyle& other) const {
31 if (fColor != other.fColor) {
32 return false;
33 }
Julia Lavrova5207f352019-06-21 12:22:32 -040034 if (!(fDecoration == other.fDecoration)) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040035 return false;
36 }
37 if (!(fFontStyle == other.fFontStyle)) {
38 return false;
39 }
40 if (fFontFamilies != other.fFontFamilies) {
41 return false;
42 }
43 if (fLetterSpacing != other.fLetterSpacing) {
44 return false;
45 }
46 if (fWordSpacing != other.fWordSpacing) {
47 return false;
48 }
49 if (fHeight != other.fHeight) {
50 return false;
51 }
52 if (fFontSize != other.fFontSize) {
53 return false;
54 }
55 if (fLocale != other.fLocale) {
56 return false;
57 }
58 if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) {
59 return false;
60 }
61 if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) {
62 return false;
63 }
64 if (fTextShadows.size() != other.fTextShadows.size()) {
65 return false;
66 }
67
68 for (int32_t i = 0; i < (int32_t)fTextShadows.size(); ++i) {
69 if (fTextShadows[i] != other.fTextShadows[i]) {
70 return false;
71 }
72 }
73
74 return true;
75}
76
77bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
78 switch (styleType) {
79 case kForeground:
80 if (fHasForeground) {
81 return other.fHasForeground && fForeground == other.fForeground;
82 } else {
83 return !other.fHasForeground && fColor == other.fColor;
84 }
85
86 case kBackground:
87 return (fHasBackground == other.fHasBackground && fBackground == other.fBackground);
88
89 case kShadow:
90 if (fTextShadows.size() != other.fTextShadows.size()) {
91 return false;
92 }
93
94 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
95 if (fTextShadows[i] != other.fTextShadows[i]) {
96 return false;
97 }
98 }
99 return true;
100
101 case kDecorations:
Julia Lavrova5207f352019-06-21 12:22:32 -0400102 return this->fDecoration == other.fDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400103
104 case kLetterSpacing:
105 return fLetterSpacing == other.fLetterSpacing;
106
107 case kWordSpacing:
108 return fWordSpacing == other.fWordSpacing;
109
110 case kAllAttributes:
111 return this->equals(other);
112
113 case kFont:
Julia Lavrova5207f352019-06-21 12:22:32 -0400114 // TODO: should not we take typefaces in account?
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400115 return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies &&
116 fFontSize == other.fFontSize && fHeight == other.fHeight;
117
118 default:
119 SkASSERT(false);
120 return false;
121 }
122}
123
124} // namespace textlayout
125} // namespace skia