blob: 4fc619acfd7d5aa982444e0016eb8861219cf83f [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 }
Julia Lavrova90bfd1c2019-12-04 11:43:32 -050087 for (size_t i = 0; i < fTextShadows.size(); ++i) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040088 if (fTextShadows[i] != other.fTextShadows[i]) {
89 return false;
90 }
91 }
92
93 return true;
94}
95
96bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const {
97 switch (styleType) {
98 case kForeground:
99 if (fHasForeground) {
100 return other.fHasForeground && fForeground == other.fForeground;
101 } else {
102 return !other.fHasForeground && fColor == other.fColor;
103 }
104
105 case kBackground:
106 return (fHasBackground == other.fHasBackground && fBackground == other.fBackground);
107
108 case kShadow:
109 if (fTextShadows.size() != other.fTextShadows.size()) {
110 return false;
111 }
112
113 for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) {
114 if (fTextShadows[i] != other.fTextShadows[i]) {
115 return false;
116 }
117 }
118 return true;
119
120 case kDecorations:
Julia Lavrova5207f352019-06-21 12:22:32 -0400121 return this->fDecoration == other.fDecoration;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400122
123 case kLetterSpacing:
124 return fLetterSpacing == other.fLetterSpacing;
125
126 case kWordSpacing:
127 return fWordSpacing == other.fWordSpacing;
128
129 case kAllAttributes:
130 return this->equals(other);
131
132 case kFont:
Julia Lavrova5207f352019-06-21 12:22:32 -0400133 // TODO: should not we take typefaces in account?
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400134 return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies &&
135 fFontSize == other.fFontSize && fHeight == other.fHeight;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400136 default:
137 SkASSERT(false);
138 return false;
139 }
140}
141
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400142void TextStyle::getFontMetrics(SkFontMetrics* metrics) const {
143 SkFont font(fTypeface, fFontSize);
Julia Lavrova916a9042019-08-08 16:51:27 -0400144 font.setEdging(SkFont::Edging::kAntiAlias);
145 font.setSubpixel(true);
146 font.setHinting(SkFontHinting::kSlight);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400147 font.getMetrics(metrics);
Julia Lavrovac2228562019-08-08 16:51:27 -0400148 if (fHeightOverride) {
149 auto multiplier = fHeight * fFontSize;
150 auto height = metrics->fDescent - metrics->fAscent + metrics->fLeading;
151 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2) * multiplier / height;
152 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2) * multiplier / height;
153
154 } else {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400155 metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2);
156 metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400157 }
158}
159
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400160} // namespace textlayout
161} // namespace skia