blob: 0487aa1c009af29a459d1bc7db093c75c03bc7e2 [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;
Julia Lavrovadb9f6692019-08-01 16:02:17 -040024 fHeightOverride = false;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040025 fHasBackground = false;
26 fHasForeground = false;
27 fTextBaseline = TextBaseline::kAlphabetic;
Julia Lavrova35f88222019-06-21 12:22:32 -040028 fLocale = "";
Julia Lavrova916a9042019-08-08 16:51:27 -040029 fIsPlaceholder = false;
30}
31
32TextStyle:: TextStyle(const TextStyle& other, bool placeholder) {
33 fColor = other.fColor;
34 fDecoration = other.fDecoration;
35 fHasBackground = other.fHasBackground;
36 fHasForeground = other.fHasForeground;
37 fBackground = other.fBackground;
38 fForeground = other.fForeground;
39 fIsPlaceholder = placeholder;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040040}
41
42bool TextStyle::equals(const TextStyle& other) const {
Julia Lavrova916a9042019-08-08 16:51:27 -040043
44 if (fIsPlaceholder || other.fIsPlaceholder) {
45 return false;
46 }
47
Julia Lavrovaa3552c52019-05-30 16:12:56 -040048 if (fColor != other.fColor) {
49 return false;
50 }
Julia Lavrova5207f352019-06-21 12:22:32 -040051 if (!(fDecoration == other.fDecoration)) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040052 return false;
53 }
54 if (!(fFontStyle == other.fFontStyle)) {
55 return false;
56 }
57 if (fFontFamilies != other.fFontFamilies) {
58 return false;
59 }
60 if (fLetterSpacing != other.fLetterSpacing) {
61 return false;
62 }
63 if (fWordSpacing != other.fWordSpacing) {
64 return false;
65 }
66 if (fHeight != other.fHeight) {
67 return false;
68 }
69 if (fFontSize != other.fFontSize) {
70 return false;
71 }
72 if (fLocale != other.fLocale) {
73 return false;
74 }
75 if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) {
76 return false;
77 }
78 if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) {
79 return false;
80 }
81 if (fTextShadows.size() != other.fTextShadows.size()) {
82 return false;
83 }
84
85 for (int32_t i = 0; i < (int32_t)fTextShadows.size(); ++i) {
86 if (fTextShadows[i] != other.fTextShadows[i]) {
87 return false;
88 }
89 }
90
91 return true;
92}
93
94bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
95 switch (styleType) {
96 case kForeground:
97 if (fHasForeground) {
98 return other.fHasForeground && fForeground == other.fForeground;
99 } else {
100 return !other.fHasForeground && fColor == other.fColor;
101 }
102
103 case kBackground:
104 return (fHasBackground == other.fHasBackground && fBackground == other.fBackground);
105
106 case kShadow:
107 if (fTextShadows.size() != other.fTextShadows.size()) {
108 return false;
109 }
110
111 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
112 if (fTextShadows[i] != other.fTextShadows[i]) {
113 return false;
114 }
115 }
116 return true;
117
118 case kDecorations:
Julia Lavrova5207f352019-06-21 12:22:32 -0400119 return this->fDecoration == other.fDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400120
121 case kLetterSpacing:
122 return fLetterSpacing == other.fLetterSpacing;
123
124 case kWordSpacing:
125 return fWordSpacing == other.fWordSpacing;
126
127 case kAllAttributes:
128 return this->equals(other);
129
130 case kFont:
Julia Lavrova5207f352019-06-21 12:22:32 -0400131 // TODO: should not we take typefaces in account?
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400132 return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies &&
133 fFontSize == other.fFontSize && fHeight == other.fHeight;
134
135 default:
136 SkASSERT(false);
137 return false;
138 }
139}
140
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400141void TextStyle::getFontMetrics(SkFontMetrics* metrics) const {
142 SkFont font(fTypeface, fFontSize);
Julia Lavrova916a9042019-08-08 16:51:27 -0400143 font.setEdging(SkFont::Edging::kAntiAlias);
144 font.setSubpixel(true);
145 font.setHinting(SkFontHinting::kSlight);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400146 font.getMetrics(metrics);
Julia Lavrovac2228562019-08-08 16:51:27 -0400147 if (fHeightOverride) {
148 auto multiplier = fHeight * fFontSize;
149 auto height = metrics->fDescent - metrics->fAscent + metrics->fLeading;
150 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2) * multiplier / height;
151 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2) * multiplier / height;
152
153 } else {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400154 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2);
155 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400156 }
157}
158
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400159} // namespace textlayout
160} // namespace skia