1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "InspectorFontFace.h"
#include "gfxPlatformFontList.h"
#include "gfxTextRun.h"
#include "gfxUserFontSet.h"
#include "nsFontFaceLoader.h"
#include "mozilla/gfx/2D.h"
#include "brotli/decode.h"
#include "zlib.h"
#include "mozilla/dom/CSSFontFaceRule.h"
#include "mozilla/dom/FontFaceSet.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/Unused.h"
namespace mozilla {
namespace dom {
InspectorFontFace::InspectorFontFace(gfxFontEntry* aFontEntry,
gfxFontGroup* aFontGroup,
FontMatchType aMatchType)
: mFontEntry(aFontEntry), mFontGroup(aFontGroup), mMatchType(aMatchType) {
MOZ_COUNT_CTOR(InspectorFontFace);
}
InspectorFontFace::~InspectorFontFace() { MOZ_COUNT_DTOR(InspectorFontFace); }
bool InspectorFontFace::FromFontGroup() {
return bool(mMatchType.kind & FontMatchType::Kind::kFontGroup);
}
bool InspectorFontFace::FromLanguagePrefs() {
return bool(mMatchType.kind & FontMatchType::Kind::kPrefsFallback);
}
bool InspectorFontFace::FromSystemFallback() {
return bool(mMatchType.kind & FontMatchType::Kind::kSystemFallback);
}
void InspectorFontFace::GetName(nsAString& aName) {
if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
aName.Append(NS_ConvertUTF8toUTF16(mFontEntry->mUserFontData->mRealName));
} else {
aName.Append(NS_ConvertUTF8toUTF16(mFontEntry->RealFaceName()));
}
}
void InspectorFontFace::GetCSSFamilyName(nsAString& aCSSFamilyName) {
aCSSFamilyName.Append(NS_ConvertUTF8toUTF16(mFontEntry->FamilyName()));
}
void InspectorFontFace::GetCSSGeneric(nsAString& aName) {
if (mMatchType.generic != StyleGenericFontFamily::None) {
aName.AssignASCII(gfxPlatformFontList::GetGenericName(mMatchType.generic));
} else {
aName.Truncate(0);
}
}
CSSFontFaceRule* InspectorFontFace::GetRule() {
if (!mRule) {
// check whether this font entry is associated with an @font-face rule
// in the relevant font group's user font set
RawServoFontFaceRule* rule = nullptr;
if (mFontEntry->IsUserFont()) {
auto* fontFaceSet =
static_cast<FontFaceSetImpl*>(mFontGroup->GetUserFontSet());
if (fontFaceSet) {
rule = fontFaceSet->FindRuleForEntry(mFontEntry);
}
}
if (rule) {
// XXX It would be better if we can share this with CSSOM tree,
// but that may require us to create another map, which is not
// great either. As far as they would use the same backend, and
// we don't really support mutating @font-face rule via CSSOM,
// it's probably fine for now.
uint32_t line, column;
Servo_FontFaceRule_GetSourceLocation(rule, &line, &column);
mRule =
new CSSFontFaceRule(do_AddRef(rule), nullptr, nullptr, line, column);
}
}
return mRule;
}
int32_t InspectorFontFace::SrcIndex() {
if (mFontEntry->IsUserFont()) {
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
return mFontEntry->mUserFontData->mSrcIndex;
}
return -1;
}
void InspectorFontFace::GetURI(nsAString& aURI) {
aURI.Truncate();
if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
if (mFontEntry->mUserFontData->mURI) {
nsAutoCString spec;
mFontEntry->mUserFontData->mURI->GetSpec(spec);
AppendUTF8toUTF16(spec, aURI);
}
}
}
void InspectorFontFace::GetLocalName(nsAString& aLocalName) {
aLocalName.Truncate();
if (mFontEntry->IsLocalUserFont()) {
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
aLocalName.Append(
NS_ConvertUTF8toUTF16(mFontEntry->mUserFontData->mLocalName));
}
}
void InspectorFontFace::GetFormat(nsAString& aFormat) {
aFormat.Truncate();
if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
switch (mFontEntry->mUserFontData->mFormatHint) {
case StyleFontFaceSourceFormatKeyword::None:
break;
case StyleFontFaceSourceFormatKeyword::Collection:
aFormat.AssignLiteral("collection");
break;
case StyleFontFaceSourceFormatKeyword::Opentype:
aFormat.AssignLiteral("opentype");
break;
case StyleFontFaceSourceFormatKeyword::Truetype:
aFormat.AssignLiteral("truetype");
break;
case StyleFontFaceSourceFormatKeyword::EmbeddedOpentype:
aFormat.AssignLiteral("embedded-opentype");
break;
case StyleFontFaceSourceFormatKeyword::Svg:
aFormat.AssignLiteral("svg");
break;
case StyleFontFaceSourceFormatKeyword::Woff:
aFormat.AssignLiteral("woff");
break;
case StyleFontFaceSourceFormatKeyword::Woff2:
aFormat.AssignLiteral("woff2");
break;
case StyleFontFaceSourceFormatKeyword::Unknown:
aFormat.AssignLiteral("unknown!");
break;
}
}
}
void InspectorFontFace::GetMetadata(nsAString& aMetadata) {
aMetadata.Truncate();
if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
const gfxUserFontData* userFontData = mFontEntry->mUserFontData.get();
if (userFontData->mMetadata.Length() && userFontData->mMetaOrigLen) {
nsAutoCString str;
str.SetLength(userFontData->mMetaOrigLen);
if (str.Length() == userFontData->mMetaOrigLen) {
switch (userFontData->mCompression) {
case gfxUserFontData::kZlibCompression: {
uLongf destLen = userFontData->mMetaOrigLen;
if (uncompress((Bytef*)(str.BeginWriting()), &destLen,
(const Bytef*)(userFontData->mMetadata.Elements()),
userFontData->mMetadata.Length()) == Z_OK &&
destLen == userFontData->mMetaOrigLen) {
AppendUTF8toUTF16(str, aMetadata);
}
} break;
case gfxUserFontData::kBrotliCompression: {
size_t decodedSize = userFontData->mMetaOrigLen;
if (BrotliDecoderDecompress(userFontData->mMetadata.Length(),
userFontData->mMetadata.Elements(),
&decodedSize,
(uint8_t*)str.BeginWriting()) == 1 &&
decodedSize == userFontData->mMetaOrigLen) {
AppendUTF8toUTF16(str, aMetadata);
}
} break;
}
}
}
}
}
// Append an OpenType tag to a string as a 4-ASCII-character code.
static void AppendTagAsASCII(nsAString& aString, uint32_t aTag) {
aString.AppendPrintf("%c%c%c%c", (aTag >> 24) & 0xff, (aTag >> 16) & 0xff,
(aTag >> 8) & 0xff, aTag & 0xff);
}
void InspectorFontFace::GetVariationAxes(
nsTArray<InspectorVariationAxis>& aResult, ErrorResult& aRV) {
if (!mFontEntry->HasVariations()) {
return;
}
AutoTArray<gfxFontVariationAxis, 4> axes;
mFontEntry->GetVariationAxes(axes);
MOZ_ASSERT(!axes.IsEmpty());
if (!aResult.SetCapacity(axes.Length(), mozilla::fallible)) {
aRV.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
for (auto a : axes) {
InspectorVariationAxis& axis = *aResult.AppendElement();
AppendTagAsASCII(axis.mTag, a.mTag);
axis.mName.Append(NS_ConvertUTF8toUTF16(a.mName));
axis.mMinValue = a.mMinValue;
axis.mMaxValue = a.mMaxValue;
axis.mDefaultValue = a.mDefaultValue;
}
}
void InspectorFontFace::GetVariationInstances(
nsTArray<InspectorVariationInstance>& aResult, ErrorResult& aRV) {
if (!mFontEntry->HasVariations()) {
return;
}
AutoTArray<gfxFontVariationInstance, 16> instances;
mFontEntry->GetVariationInstances(instances);
if (!aResult.SetCapacity(instances.Length(), mozilla::fallible)) {
aRV.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
for (const auto& i : instances) {
InspectorVariationInstance& inst = *aResult.AppendElement();
inst.mName.Append(NS_ConvertUTF8toUTF16(i.mName));
// inst.mValues is a webidl sequence<>, which is a fallible array,
// so we are required to use fallible SetCapacity and AppendElement calls,
// and check the result. In practice we don't expect failure here; the
// list of values cannot get huge because of limits in the font format.
if (!inst.mValues.SetCapacity(i.mValues.Length(), mozilla::fallible)) {
aRV.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
for (const auto& v : i.mValues) {
InspectorVariationValue value;
AppendTagAsASCII(value.mAxis, v.mAxis);
value.mValue = v.mValue;
// This won't fail, because of SetCapacity above.
Unused << inst.mValues.AppendElement(value, mozilla::fallible);
}
}
}
void InspectorFontFace::GetFeatures(nsTArray<InspectorFontFeature>& aResult,
ErrorResult& aRV) {
AutoTArray<gfxFontFeatureInfo, 64> features;
mFontEntry->GetFeatureInfo(features);
if (features.IsEmpty()) {
return;
}
if (!aResult.SetCapacity(features.Length(), mozilla::fallible)) {
aRV.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
for (auto& f : features) {
InspectorFontFeature& feat = *aResult.AppendElement();
AppendTagAsASCII(feat.mTag, f.mTag);
AppendTagAsASCII(feat.mScript, f.mScript);
AppendTagAsASCII(feat.mLanguageSystem, f.mLangSys);
}
}
void InspectorFontFace::GetRanges(nsTArray<RefPtr<nsRange>>& aResult) {
aResult = mRanges.Clone();
}
void InspectorFontFace::AddRange(nsRange* aRange) {
mRanges.AppendElement(aRange);
}
} // namespace dom
} // namespace mozilla
|