blob: ce0cc40ec2e41ba0f30fe6f8bb46b78d7de1dc84 [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;
Julia Lavrova526df262019-08-21 17:49:44 -040034 fFontSize = other.fFontSize;
35 fFontFamilies = other.fFontFamilies;
Julia Lavrova916a9042019-08-08 16:51:27 -040036 fDecoration = other.fDecoration;
37 fHasBackground = other.fHasBackground;
38 fHasForeground = other.fHasForeground;
39 fBackground = other.fBackground;
40 fForeground = other.fForeground;
Julia Lavrova526df262019-08-21 17:49:44 -040041 fHeightOverride = other.fHeightOverride;
Julia Lavrova916a9042019-08-08 16:51:27 -040042 fIsPlaceholder = placeholder;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040043}
44
45bool TextStyle::equals(const TextStyle& other) const {
Julia Lavrova916a9042019-08-08 16:51:27 -040046
47 if (fIsPlaceholder || other.fIsPlaceholder) {
48 return false;
49 }
50
Julia Lavrovaa3552c52019-05-30 16:12:56 -040051 if (fColor != other.fColor) {
52 return false;
53 }
Julia Lavrova5207f352019-06-21 12:22:32 -040054 if (!(fDecoration == other.fDecoration)) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040055 return false;
56 }
57 if (!(fFontStyle == other.fFontStyle)) {
58 return false;
59 }
60 if (fFontFamilies != other.fFontFamilies) {
61 return false;
62 }
63 if (fLetterSpacing != other.fLetterSpacing) {
64 return false;
65 }
66 if (fWordSpacing != other.fWordSpacing) {
67 return false;
68 }
69 if (fHeight != other.fHeight) {
70 return false;
71 }
72 if (fFontSize != other.fFontSize) {
73 return false;
74 }
75 if (fLocale != other.fLocale) {
76 return false;
77 }
78 if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) {
79 return false;
80 }
81 if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) {
82 return false;
83 }
84 if (fTextShadows.size() != other.fTextShadows.size()) {
85 return false;
86 }
87
88 for (int32_t i = 0; i < (int32_t)fTextShadows.size(); ++i) {
89 if (fTextShadows[i] != other.fTextShadows[i]) {
90 return false;
91 }
92 }
93
94 return true;
95}
96
97bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
98 switch (styleType) {
99 case kForeground:
100 if (fHasForeground) {
101 return other.fHasForeground && fForeground == other.fForeground;
102 } else {
103 return !other.fHasForeground && fColor == other.fColor;
104 }
105
106 case kBackground:
107 return (fHasBackground == other.fHasBackground && fBackground == other.fBackground);
108
109 case kShadow:
110 if (fTextShadows.size() != other.fTextShadows.size()) {
111 return false;
112 }
113
114 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
115 if (fTextShadows[i] != other.fTextShadows[i]) {
116 return false;
117 }
118 }
119 return true;
120
121 case kDecorations:
Julia Lavrova5207f352019-06-21 12:22:32 -0400122 return this->fDecoration == other.fDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400123
124 case kLetterSpacing:
125 return fLetterSpacing == other.fLetterSpacing;
126
127 case kWordSpacing:
128 return fWordSpacing == other.fWordSpacing;
129
130 case kAllAttributes:
131 return this->equals(other);
132
133 case kFont:
Julia Lavrova5207f352019-06-21 12:22:32 -0400134 // TODO: should not we take typefaces in account?
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400135 return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies &&
136 fFontSize == other.fFontSize && fHeight == other.fHeight;
137
138 default:
139 SkASSERT(false);
140 return false;
141 }
142}
143
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400144void TextStyle::getFontMetrics(SkFontMetrics* metrics) const {
145 SkFont font(fTypeface, fFontSize);
Julia Lavrova916a9042019-08-08 16:51:27 -0400146 font.setEdging(SkFont::Edging::kAntiAlias);
147 font.setSubpixel(true);
148 font.setHinting(SkFontHinting::kSlight);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400149 font.getMetrics(metrics);
Julia Lavrovac2228562019-08-08 16:51:27 -0400150 if (fHeightOverride) {
151 auto multiplier = fHeight * fFontSize;
152 auto height = metrics->fDescent - metrics->fAscent + metrics->fLeading;
153 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2) * multiplier / height;
154 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2) * multiplier / height;
155
156 } else {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400157 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2);
158 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400159 }
160}
161
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400162} // namespace textlayout
163} // namespace skia