Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / gfx / src / nsFont.cpp
blob9667be1cc36d9146706bec4a43174e24249ae0ea
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsFont.h"
8 #include "gfxFont.h" // for gfxFontStyle
9 #include "gfxFontFeatures.h" // for gfxFontFeature, etc
10 #include "gfxFontUtils.h" // for TRUETYPE_TAG
11 #include "mozilla/ServoStyleConstsInlines.h"
12 #include "nsCRT.h" // for nsCRT
13 #include "nsDebug.h" // for NS_ASSERTION
14 #include "nsISupports.h"
15 #include "nsUnicharUtils.h"
16 #include "nscore.h" // for char16_t
17 #include "mozilla/ArrayUtils.h"
18 #include "mozilla/gfx/2D.h"
20 using namespace mozilla;
22 nsFont::nsFont(const StyleFontFamily& aFamily, mozilla::Length aSize)
23 : family(aFamily), size(aSize) {}
25 nsFont::nsFont(StyleGenericFontFamily aGenericType, mozilla::Length aSize)
26 : family(*Servo_FontFamily_Generic(aGenericType)), size(aSize) {}
28 nsFont::nsFont(const nsFont& aOther) = default;
30 nsFont::~nsFont() = default;
32 nsFont& nsFont::operator=(const nsFont&) = default;
34 bool nsFont::Equals(const nsFont& aOther) const {
35 return CalcDifference(aOther) == MaxDifference::eNone;
38 nsFont::MaxDifference nsFont::CalcDifference(const nsFont& aOther) const {
39 if ((style != aOther.style) || (weight != aOther.weight) ||
40 (stretch != aOther.stretch) || (size != aOther.size) ||
41 (sizeAdjust != aOther.sizeAdjust) || (family != aOther.family) ||
42 (kerning != aOther.kerning) || (opticalSizing != aOther.opticalSizing) ||
43 (synthesisWeight != aOther.synthesisWeight) ||
44 (synthesisStyle != aOther.synthesisStyle) ||
45 (synthesisSmallCaps != aOther.synthesisSmallCaps) ||
46 (synthesisPosition != aOther.synthesisPosition) ||
47 (fontFeatureSettings != aOther.fontFeatureSettings) ||
48 (fontVariationSettings != aOther.fontVariationSettings) ||
49 (languageOverride != aOther.languageOverride) ||
50 (variantAlternates != aOther.variantAlternates) ||
51 (variantCaps != aOther.variantCaps) ||
52 (variantEastAsian != aOther.variantEastAsian) ||
53 (variantLigatures != aOther.variantLigatures) ||
54 (variantNumeric != aOther.variantNumeric) ||
55 (variantPosition != aOther.variantPosition) ||
56 (variantWidth != aOther.variantWidth) ||
57 (variantEmoji != aOther.variantEmoji)) {
58 return MaxDifference::eLayoutAffecting;
61 if ((smoothing != aOther.smoothing) ||
62 (fontSmoothingBackgroundColor != aOther.fontSmoothingBackgroundColor)) {
63 return MaxDifference::eVisual;
66 return MaxDifference::eNone;
69 // mapping from bitflag to font feature tag/value pair
71 // these need to be kept in sync with the constants listed
72 // in gfxFontConstants.h (e.g. NS_FONT_VARIANT_EAST_ASIAN_JIS78)
74 // NS_FONT_VARIANT_EAST_ASIAN_xxx values
75 const gfxFontFeature eastAsianDefaults[] = {
76 {TRUETYPE_TAG('j', 'p', '7', '8'), 1},
77 {TRUETYPE_TAG('j', 'p', '8', '3'), 1},
78 {TRUETYPE_TAG('j', 'p', '9', '0'), 1},
79 {TRUETYPE_TAG('j', 'p', '0', '4'), 1},
80 {TRUETYPE_TAG('s', 'm', 'p', 'l'), 1},
81 {TRUETYPE_TAG('t', 'r', 'a', 'd'), 1},
82 {TRUETYPE_TAG('f', 'w', 'i', 'd'), 1},
83 {TRUETYPE_TAG('p', 'w', 'i', 'd'), 1},
84 {TRUETYPE_TAG('r', 'u', 'b', 'y'), 1}};
86 static_assert(MOZ_ARRAY_LENGTH(eastAsianDefaults) ==
87 NS_FONT_VARIANT_EAST_ASIAN_COUNT,
88 "eastAsianDefaults[] should be correct");
90 // NS_FONT_VARIANT_LIGATURES_xxx values
91 const gfxFontFeature ligDefaults[] = {
92 {TRUETYPE_TAG('l', 'i', 'g', 'a'), 0}, // none value means all off
93 {TRUETYPE_TAG('l', 'i', 'g', 'a'), 1},
94 {TRUETYPE_TAG('l', 'i', 'g', 'a'), 0},
95 {TRUETYPE_TAG('d', 'l', 'i', 'g'), 1},
96 {TRUETYPE_TAG('d', 'l', 'i', 'g'), 0},
97 {TRUETYPE_TAG('h', 'l', 'i', 'g'), 1},
98 {TRUETYPE_TAG('h', 'l', 'i', 'g'), 0},
99 {TRUETYPE_TAG('c', 'a', 'l', 't'), 1},
100 {TRUETYPE_TAG('c', 'a', 'l', 't'), 0}};
102 static_assert(MOZ_ARRAY_LENGTH(ligDefaults) == NS_FONT_VARIANT_LIGATURES_COUNT,
103 "ligDefaults[] should be correct");
105 // NS_FONT_VARIANT_NUMERIC_xxx values
106 const gfxFontFeature numericDefaults[] = {
107 {TRUETYPE_TAG('l', 'n', 'u', 'm'), 1},
108 {TRUETYPE_TAG('o', 'n', 'u', 'm'), 1},
109 {TRUETYPE_TAG('p', 'n', 'u', 'm'), 1},
110 {TRUETYPE_TAG('t', 'n', 'u', 'm'), 1},
111 {TRUETYPE_TAG('f', 'r', 'a', 'c'), 1},
112 {TRUETYPE_TAG('a', 'f', 'r', 'c'), 1},
113 {TRUETYPE_TAG('z', 'e', 'r', 'o'), 1},
114 {TRUETYPE_TAG('o', 'r', 'd', 'n'), 1}};
116 static_assert(MOZ_ARRAY_LENGTH(numericDefaults) ==
117 NS_FONT_VARIANT_NUMERIC_COUNT,
118 "numericDefaults[] should be correct");
120 static void AddFontFeaturesBitmask(uint32_t aValue, uint32_t aMin,
121 uint32_t aMax,
122 const gfxFontFeature aFeatureDefaults[],
123 nsTArray<gfxFontFeature>& aFeaturesOut)
126 uint32_t i, m;
128 for (i = 0, m = aMin; m <= aMax; i++, m <<= 1) {
129 if (m & aValue) {
130 const gfxFontFeature& feature = aFeatureDefaults[i];
131 aFeaturesOut.AppendElement(feature);
136 static uint32_t FontFeatureTagForVariantWidth(uint32_t aVariantWidth) {
137 switch (aVariantWidth) {
138 case NS_FONT_VARIANT_WIDTH_FULL:
139 return TRUETYPE_TAG('f', 'w', 'i', 'd');
140 case NS_FONT_VARIANT_WIDTH_HALF:
141 return TRUETYPE_TAG('h', 'w', 'i', 'd');
142 case NS_FONT_VARIANT_WIDTH_THIRD:
143 return TRUETYPE_TAG('t', 'w', 'i', 'd');
144 case NS_FONT_VARIANT_WIDTH_QUARTER:
145 return TRUETYPE_TAG('q', 'w', 'i', 'd');
146 default:
147 return 0;
151 void nsFont::AddFontFeaturesToStyle(gfxFontStyle* aStyle,
152 bool aVertical) const {
153 // add in font-variant features
154 gfxFontFeature setting;
156 // -- kerning
157 setting.mTag = aVertical ? TRUETYPE_TAG('v', 'k', 'r', 'n')
158 : TRUETYPE_TAG('k', 'e', 'r', 'n');
159 switch (kerning) {
160 case NS_FONT_KERNING_NONE:
161 setting.mValue = 0;
162 aStyle->featureSettings.AppendElement(setting);
163 break;
164 case NS_FONT_KERNING_NORMAL:
165 setting.mValue = 1;
166 aStyle->featureSettings.AppendElement(setting);
167 break;
168 default:
169 // auto case implies use user agent default
170 break;
173 // -- alternates
175 // NOTE(emilio): We handle historical-forms here because it doesn't depend on
176 // other values set by @font-face and thus may be less expensive to do here
177 // than after font-matching.
178 for (auto& alternate : variantAlternates.AsSpan()) {
179 if (alternate.IsHistoricalForms()) {
180 setting.mValue = 1;
181 setting.mTag = TRUETYPE_TAG('h', 'i', 's', 't');
182 aStyle->featureSettings.AppendElement(setting);
183 break;
187 // -- copy font-specific alternate info into style
188 // (this will be resolved after font-matching occurs)
189 aStyle->variantAlternates = variantAlternates;
191 // -- caps
192 aStyle->variantCaps = variantCaps;
194 // -- east-asian
195 if (variantEastAsian) {
196 AddFontFeaturesBitmask(variantEastAsian, NS_FONT_VARIANT_EAST_ASIAN_JIS78,
197 NS_FONT_VARIANT_EAST_ASIAN_RUBY, eastAsianDefaults,
198 aStyle->featureSettings);
201 // -- ligatures
202 if (variantLigatures) {
203 AddFontFeaturesBitmask(variantLigatures, NS_FONT_VARIANT_LIGATURES_NONE,
204 NS_FONT_VARIANT_LIGATURES_NO_CONTEXTUAL, ligDefaults,
205 aStyle->featureSettings);
207 if (variantLigatures & NS_FONT_VARIANT_LIGATURES_COMMON) {
208 // liga already enabled, need to enable clig also
209 setting.mTag = TRUETYPE_TAG('c', 'l', 'i', 'g');
210 setting.mValue = 1;
211 aStyle->featureSettings.AppendElement(setting);
212 } else if (variantLigatures & NS_FONT_VARIANT_LIGATURES_NO_COMMON) {
213 // liga already disabled, need to disable clig also
214 setting.mTag = TRUETYPE_TAG('c', 'l', 'i', 'g');
215 setting.mValue = 0;
216 aStyle->featureSettings.AppendElement(setting);
217 } else if (variantLigatures & NS_FONT_VARIANT_LIGATURES_NONE) {
218 // liga already disabled, need to disable dlig, hlig, calt, clig
219 setting.mValue = 0;
220 setting.mTag = TRUETYPE_TAG('d', 'l', 'i', 'g');
221 aStyle->featureSettings.AppendElement(setting);
222 setting.mTag = TRUETYPE_TAG('h', 'l', 'i', 'g');
223 aStyle->featureSettings.AppendElement(setting);
224 setting.mTag = TRUETYPE_TAG('c', 'a', 'l', 't');
225 aStyle->featureSettings.AppendElement(setting);
226 setting.mTag = TRUETYPE_TAG('c', 'l', 'i', 'g');
227 aStyle->featureSettings.AppendElement(setting);
231 // -- numeric
232 if (variantNumeric) {
233 AddFontFeaturesBitmask(variantNumeric, NS_FONT_VARIANT_NUMERIC_LINING,
234 NS_FONT_VARIANT_NUMERIC_ORDINAL, numericDefaults,
235 aStyle->featureSettings);
238 // -- position
239 aStyle->variantSubSuper = variantPosition;
241 // -- width
242 setting.mTag = FontFeatureTagForVariantWidth(variantWidth);
243 if (setting.mTag) {
244 setting.mValue = 1;
245 aStyle->featureSettings.AppendElement(setting);
248 // indicate common-path case when neither variantCaps or variantSubSuper are
249 // set
250 aStyle->noFallbackVariantFeatures =
251 (aStyle->variantCaps == NS_FONT_VARIANT_CAPS_NORMAL) &&
252 (variantPosition == NS_FONT_VARIANT_POSITION_NORMAL);
254 // If the feature list is not empty, we insert a "fake" feature with tag=0
255 // as delimiter between the above "high-level" features from font-variant-*
256 // etc and those coming from the low-level font-feature-settings property.
257 // This will allow us to distinguish high- and low-level settings when it
258 // comes to potentially disabling ligatures because of letter-spacing.
259 if (!aStyle->featureSettings.IsEmpty() || !fontFeatureSettings.IsEmpty()) {
260 aStyle->featureSettings.AppendElement(gfxFontFeature{0, 0});
263 // add in features from font-feature-settings
264 aStyle->featureSettings.AppendElements(fontFeatureSettings);
266 // enable grayscale antialiasing for text
267 if (smoothing == NS_FONT_SMOOTHING_GRAYSCALE) {
268 aStyle->useGrayscaleAntialiasing = true;
271 aStyle->fontSmoothingBackgroundColor = fontSmoothingBackgroundColor.ToColor();
274 void nsFont::AddFontVariationsToStyle(gfxFontStyle* aStyle) const {
275 // If auto optical sizing is enabled, and if there's no 'opsz' axis in
276 // fontVariationSettings, then set the automatic value on the style.
277 class VariationTagComparator {
278 public:
279 bool Equals(const gfxFontVariation& aVariation, uint32_t aTag) const {
280 return aVariation.mTag == aTag;
283 const uint32_t kTagOpsz = TRUETYPE_TAG('o', 'p', 's', 'z');
284 if (opticalSizing == NS_FONT_OPTICAL_SIZING_AUTO &&
285 !fontVariationSettings.Contains(kTagOpsz, VariationTagComparator())) {
286 aStyle->autoOpticalSize = size.ToCSSPixels();
289 // Add in arbitrary values from font-variation-settings
290 aStyle->variationSettings.AppendElements(fontVariationSettings);