blob: fd8d5ce52d8245fd08e9668dcb1253d3f420cc01 [file] [log] [blame]
Julia Lavrovaa3552c52019-05-30 16:12:56 -04001// Copyright 2019 Google LLC.
Julia Lavrova5207f352019-06-21 12:22:32 -04002#include "modules/skparagraph/include/TextStyle.h"
3#include <TextStyle.h>
Julia Lavrovaa3552c52019-05-30 16:12:56 -04004#include "include/core/SkColor.h"
5#include "include/core/SkFontStyle.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -04006
7namespace skia {
8namespace textlayout {
9
10TextStyle::TextStyle() : fFontStyle() {
Julia Lavrova5207f352019-06-21 12:22:32 -040011 fFontFamilies.reserve(1);
Julia Lavrovaa3552c52019-05-30 16:12:56 -040012 fFontFamilies.emplace_back(DEFAULT_FONT_FAMILY);
13 fColor = SK_ColorWHITE;
Julia Lavrova5207f352019-06-21 12:22:32 -040014 fDecoration.fType = TextDecoration::kNoDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040015 // Does not make sense to draw a transparent object, so we use it as a default
16 // value to indicate no decoration color was set.
Julia Lavrova5207f352019-06-21 12:22:32 -040017 fDecoration.fColor = SK_ColorTRANSPARENT;
18 fDecoration.fStyle = TextDecorationStyle::kSolid;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040019 // Thickness is applied as a multiplier to the default thickness of the font.
Julia Lavrova5207f352019-06-21 12:22:32 -040020 fDecoration.fThicknessMultiplier = 1.0;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040021 fFontSize = 14.0;
22 fLetterSpacing = 0.0;
23 fWordSpacing = 0.0;
24 fHeight = 1.0;
25 fHasBackground = false;
26 fHasForeground = false;
27 fTextBaseline = TextBaseline::kAlphabetic;
Julia Lavrova35f88222019-06-21 12:22:32 -040028 fLocale = "";
Julia Lavrovaa3552c52019-05-30 16:12:56 -040029}
30
31bool TextStyle::equals(const TextStyle& other) const {
32 if (fColor != other.fColor) {
33 return false;
34 }
Julia Lavrova5207f352019-06-21 12:22:32 -040035 if (!(fDecoration == other.fDecoration)) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040036 return false;
37 }
38 if (!(fFontStyle == other.fFontStyle)) {
39 return false;
40 }
41 if (fFontFamilies != other.fFontFamilies) {
42 return false;
43 }
44 if (fLetterSpacing != other.fLetterSpacing) {
45 return false;
46 }
47 if (fWordSpacing != other.fWordSpacing) {
48 return false;
49 }
50 if (fHeight != other.fHeight) {
51 return false;
52 }
53 if (fFontSize != other.fFontSize) {
54 return false;
55 }
56 if (fLocale != other.fLocale) {
57 return false;
58 }
59 if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) {
60 return false;
61 }
62 if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) {
63 return false;
64 }
65 if (fTextShadows.size() != other.fTextShadows.size()) {
66 return false;
67 }
68
69 for (int32_t i = 0; i < (int32_t)fTextShadows.size(); ++i) {
70 if (fTextShadows[i] != other.fTextShadows[i]) {
71 return false;
72 }
73 }
74
75 return true;
76}
77
78bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
79 switch (styleType) {
80 case kForeground:
81 if (fHasForeground) {
82 return other.fHasForeground && fForeground == other.fForeground;
83 } else {
84 return !other.fHasForeground && fColor == other.fColor;
85 }
86
87 case kBackground:
88 return (fHasBackground == other.fHasBackground && fBackground == other.fBackground);
89
90 case kShadow:
91 if (fTextShadows.size() != other.fTextShadows.size()) {
92 return false;
93 }
94
95 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
96 if (fTextShadows[i] != other.fTextShadows[i]) {
97 return false;
98 }
99 }
100 return true;
101
102 case kDecorations:
Julia Lavrova5207f352019-06-21 12:22:32 -0400103 return this->fDecoration == other.fDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400104
105 case kLetterSpacing:
106 return fLetterSpacing == other.fLetterSpacing;
107
108 case kWordSpacing:
109 return fWordSpacing == other.fWordSpacing;
110
111 case kAllAttributes:
112 return this->equals(other);
113
114 case kFont:
Julia Lavrova5207f352019-06-21 12:22:32 -0400115 // TODO: should not we take typefaces in account?
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400116 return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies &&
117 fFontSize == other.fFontSize && fHeight == other.fHeight;
118
119 default:
120 SkASSERT(false);
121 return false;
122 }
123}
124
125} // namespace textlayout
126} // namespace skia