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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
|
/* -*- 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 "mozilla/dom/FontFaceImpl.h"
#include <algorithm>
#include "gfxFontUtils.h"
#include "gfxPlatformFontList.h"
#include "mozilla/dom/CSSFontFaceRule.h"
#include "mozilla/dom/FontFaceBinding.h"
#include "mozilla/dom/FontFaceSetImpl.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/dom/UnionTypes.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ServoCSSParser.h"
#include "mozilla/ServoUtils.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/dom/Document.h"
#include "nsStyleUtil.h"
namespace mozilla {
namespace dom {
// -- FontFaceBufferSource ---------------------------------------------------
/**
* An object that wraps a FontFace object and exposes its ArrayBuffer
* or ArrayBufferView data in a form the user font set can consume.
*/
class FontFaceBufferSource : public gfxFontFaceBufferSource {
public:
FontFaceBufferSource(uint8_t* aBuffer, uint32_t aLength)
: mBuffer(aBuffer), mLength(aLength) {}
void TakeBuffer(uint8_t*& aBuffer, uint32_t& aLength) override {
MOZ_ASSERT(mBuffer,
"only call TakeBuffer once on a given "
"FontFaceBufferSource object");
aBuffer = mBuffer;
aLength = mLength;
mBuffer = nullptr;
mLength = 0;
}
private:
~FontFaceBufferSource() override {
if (mBuffer) {
free(mBuffer);
}
}
uint8_t* mBuffer;
uint32_t mLength;
};
// -- FontFaceImpl -----------------------------------------------------------
FontFaceImpl::FontFaceImpl(FontFace* aOwner, FontFaceSetImpl* aFontFaceSet)
: mOwner(aOwner),
mStatus(FontFaceLoadStatus::Unloaded),
mSourceType(SourceType(0)),
mFontFaceSet(aFontFaceSet),
mUnicodeRangeDirty(true),
mInFontFaceSet(false) {}
FontFaceImpl::~FontFaceImpl() {
// Assert that we don't drop any FontFace objects during a Servo traversal,
// since PostTraversalTask objects can hold raw pointers to FontFaces.
MOZ_ASSERT(!gfxFontUtils::IsInServoTraversal());
SetUserFontEntry(nullptr);
}
#ifdef DEBUG
void FontFaceImpl::AssertIsOnOwningThread() const {
mFontFaceSet->AssertIsOnOwningThread();
}
#endif
void FontFaceImpl::Destroy() {
mInFontFaceSet = false;
SetUserFontEntry(nullptr);
mOwner = nullptr;
}
static FontFaceLoadStatus LoadStateToStatus(
gfxUserFontEntry::UserFontLoadState aLoadState) {
switch (aLoadState) {
case gfxUserFontEntry::UserFontLoadState::STATUS_NOT_LOADED:
return FontFaceLoadStatus::Unloaded;
case gfxUserFontEntry::UserFontLoadState::STATUS_LOAD_PENDING:
case gfxUserFontEntry::UserFontLoadState::STATUS_LOADING:
return FontFaceLoadStatus::Loading;
case gfxUserFontEntry::UserFontLoadState::STATUS_LOADED:
return FontFaceLoadStatus::Loaded;
case gfxUserFontEntry::UserFontLoadState::STATUS_FAILED:
return FontFaceLoadStatus::Error;
}
MOZ_ASSERT_UNREACHABLE("invalid aLoadState value");
return FontFaceLoadStatus::Error;
}
already_AddRefed<FontFaceImpl> FontFaceImpl::CreateForRule(
FontFace* aOwner, FontFaceSetImpl* aFontFaceSet,
RawServoFontFaceRule* aRule) {
RefPtr<FontFaceImpl> obj = new FontFaceImpl(aOwner, aFontFaceSet);
obj->mRule = aRule;
obj->mSourceType = eSourceType_FontFaceRule;
obj->mInFontFaceSet = true;
return obj.forget();
}
void FontFaceImpl::InitializeSourceURL(const nsACString& aURL) {
MOZ_ASSERT(mOwner);
mSourceType = eSourceType_URLs;
IgnoredErrorResult rv;
SetDescriptor(eCSSFontDesc_Src, aURL, rv);
if (rv.Failed()) {
mOwner->MaybeReject(NS_ERROR_DOM_SYNTAX_ERR);
SetStatus(FontFaceLoadStatus::Error);
}
}
void FontFaceImpl::InitializeSourceBuffer(uint8_t* aBuffer, uint32_t aLength) {
MOZ_ASSERT(mOwner);
MOZ_ASSERT(!mBufferSource);
mSourceType = FontFaceImpl::eSourceType_Buffer;
if (aBuffer) {
mBufferSource = new FontFaceBufferSource(aBuffer, aLength);
}
SetStatus(FontFaceLoadStatus::Loading);
DoLoad();
}
void FontFaceImpl::GetFamily(nsACString& aResult) {
GetDesc(eCSSFontDesc_Family, aResult);
}
void FontFaceImpl::SetFamily(const nsACString& aValue, ErrorResult& aRv) {
mFontFaceSet->FlushUserFontSet();
if (SetDescriptor(eCSSFontDesc_Family, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::GetStyle(nsACString& aResult) {
GetDesc(eCSSFontDesc_Style, aResult);
}
void FontFaceImpl::SetStyle(const nsACString& aValue, ErrorResult& aRv) {
if (SetDescriptor(eCSSFontDesc_Style, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::GetWeight(nsACString& aResult) {
GetDesc(eCSSFontDesc_Weight, aResult);
}
void FontFaceImpl::SetWeight(const nsACString& aValue, ErrorResult& aRv) {
mFontFaceSet->FlushUserFontSet();
if (SetDescriptor(eCSSFontDesc_Weight, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::GetStretch(nsACString& aResult) {
GetDesc(eCSSFontDesc_Stretch, aResult);
}
void FontFaceImpl::SetStretch(const nsACString& aValue, ErrorResult& aRv) {
mFontFaceSet->FlushUserFontSet();
if (SetDescriptor(eCSSFontDesc_Stretch, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::GetUnicodeRange(nsACString& aResult) {
GetDesc(eCSSFontDesc_UnicodeRange, aResult);
}
void FontFaceImpl::SetUnicodeRange(const nsACString& aValue, ErrorResult& aRv) {
mFontFaceSet->FlushUserFontSet();
if (SetDescriptor(eCSSFontDesc_UnicodeRange, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::GetVariant(nsACString& aResult) {
// XXX Just expose the font-variant descriptor as "normal" until we
// support it properly (bug 1055385).
aResult.AssignLiteral("normal");
}
void FontFaceImpl::SetVariant(const nsACString& aValue, ErrorResult& aRv) {
// XXX Ignore assignments to variant until we support font-variant
// descriptors (bug 1055385).
}
void FontFaceImpl::GetFeatureSettings(nsACString& aResult) {
GetDesc(eCSSFontDesc_FontFeatureSettings, aResult);
}
void FontFaceImpl::SetFeatureSettings(const nsACString& aValue,
ErrorResult& aRv) {
mFontFaceSet->FlushUserFontSet();
if (SetDescriptor(eCSSFontDesc_FontFeatureSettings, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::GetVariationSettings(nsACString& aResult) {
GetDesc(eCSSFontDesc_FontVariationSettings, aResult);
}
void FontFaceImpl::SetVariationSettings(const nsACString& aValue,
ErrorResult& aRv) {
mFontFaceSet->FlushUserFontSet();
if (SetDescriptor(eCSSFontDesc_FontVariationSettings, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::GetDisplay(nsACString& aResult) {
GetDesc(eCSSFontDesc_Display, aResult);
}
void FontFaceImpl::SetDisplay(const nsACString& aValue, ErrorResult& aRv) {
if (SetDescriptor(eCSSFontDesc_Display, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::GetAscentOverride(nsACString& aResult) {
GetDesc(eCSSFontDesc_AscentOverride, aResult);
}
void FontFaceImpl::SetAscentOverride(const nsACString& aValue,
ErrorResult& aRv) {
if (SetDescriptor(eCSSFontDesc_AscentOverride, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::GetDescentOverride(nsACString& aResult) {
GetDesc(eCSSFontDesc_DescentOverride, aResult);
}
void FontFaceImpl::SetDescentOverride(const nsACString& aValue,
ErrorResult& aRv) {
if (SetDescriptor(eCSSFontDesc_DescentOverride, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::GetLineGapOverride(nsACString& aResult) {
GetDesc(eCSSFontDesc_LineGapOverride, aResult);
}
void FontFaceImpl::SetLineGapOverride(const nsACString& aValue,
ErrorResult& aRv) {
if (SetDescriptor(eCSSFontDesc_LineGapOverride, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::GetSizeAdjust(nsACString& aResult) {
GetDesc(eCSSFontDesc_SizeAdjust, aResult);
}
void FontFaceImpl::SetSizeAdjust(const nsACString& aValue, ErrorResult& aRv) {
if (SetDescriptor(eCSSFontDesc_SizeAdjust, aValue, aRv)) {
DescriptorUpdated();
}
}
void FontFaceImpl::DescriptorUpdated() {
// If we haven't yet initialized mUserFontEntry, no need to do anything here;
// we'll respect the updated descriptor when the time comes to create it.
if (!mUserFontEntry) {
return;
}
gfxUserFontAttributes attr;
RefPtr<gfxUserFontEntry> newEntry;
if (GetAttributes(attr)) {
newEntry = mFontFaceSet->FindOrCreateUserFontEntryFromFontFace(
this, std::move(attr), StyleOrigin::Author);
}
SetUserFontEntry(newEntry);
// Behind the scenes, this will actually update the existing entry and return
// it, rather than create a new one.
if (mInFontFaceSet) {
mFontFaceSet->MarkUserFontSetDirty();
}
for (auto& set : mOtherFontFaceSets) {
set->MarkUserFontSetDirty();
}
}
FontFaceLoadStatus FontFaceImpl::Status() { return mStatus; }
void FontFaceImpl::Load(ErrorResult& aRv) {
mFontFaceSet->FlushUserFontSet();
// Calling Load on a FontFace constructed with an ArrayBuffer data source,
// or on one that is already loading (or has finished loading), has no
// effect.
if (mSourceType == eSourceType_Buffer ||
mStatus != FontFaceLoadStatus::Unloaded) {
return;
}
// Calling the user font entry's Load method will end up setting our
// status to Loading, but the spec requires us to set it to Loading
// here.
SetStatus(FontFaceLoadStatus::Loading);
DoLoad();
}
gfxUserFontEntry* FontFaceImpl::CreateUserFontEntry() {
if (!mUserFontEntry) {
MOZ_ASSERT(!HasRule(),
"Rule backed FontFace objects should already have a user font "
"entry by the time Load() can be called on them");
gfxUserFontAttributes attr;
if (GetAttributes(attr)) {
RefPtr<gfxUserFontEntry> newEntry =
mFontFaceSet->FindOrCreateUserFontEntryFromFontFace(
this, std::move(attr), StyleOrigin::Author);
if (newEntry) {
SetUserFontEntry(newEntry);
}
}
}
return mUserFontEntry;
}
void FontFaceImpl::DoLoad() {
if (!CreateUserFontEntry()) {
return;
}
if (!NS_IsMainThread()) {
NS_DispatchToMainThread(NS_NewRunnableFunction(
"FontFaceImpl::DoLoad",
[entry = RefPtr{mUserFontEntry}]() { entry->Load(); }));
return;
}
mUserFontEntry->Load();
}
void FontFaceImpl::SetStatus(FontFaceLoadStatus aStatus) {
gfxFontUtils::AssertSafeThreadOrServoFontMetricsLocked();
if (mStatus == aStatus) {
return;
}
if (aStatus < mStatus) {
// We're being asked to go backwards in status! Normally, this shouldn't
// happen. But it can if the FontFace had a user font entry that had
// loaded, but then was given a new one by FontFaceSet::InsertRuleFontFace
// if we used a local() rule. For now, just ignore the request to
// go backwards in status.
return;
}
mStatus = aStatus;
if (mInFontFaceSet) {
mFontFaceSet->OnFontFaceStatusChanged(this);
}
for (FontFaceSetImpl* otherSet : mOtherFontFaceSets) {
otherSet->OnFontFaceStatusChanged(this);
}
UpdateOwnerPromise();
}
void FontFaceImpl::UpdateOwnerPromise() {
if (!mFontFaceSet->IsOnOwningThread()) {
mFontFaceSet->DispatchToOwningThread(
"FontFaceImpl::UpdateOwnerPromise",
[self = RefPtr{this}] { self->UpdateOwnerPromise(); });
return;
}
if (NS_WARN_IF(!mOwner)) {
return;
}
if (mStatus == FontFaceLoadStatus::Loaded) {
mOwner->MaybeResolve();
} else if (mStatus == FontFaceLoadStatus::Error) {
if (mSourceType == eSourceType_Buffer) {
mOwner->MaybeReject(NS_ERROR_DOM_SYNTAX_ERR);
} else {
mOwner->MaybeReject(NS_ERROR_DOM_NETWORK_ERR);
}
}
}
// Boolean result indicates whether the value of the descriptor was actually
// changed.
bool FontFaceImpl::SetDescriptor(nsCSSFontDesc aFontDesc,
const nsACString& aValue, ErrorResult& aRv) {
// FIXME We probably don't need to distinguish between this anymore
// since we have common backend now.
NS_ASSERTION(!HasRule(), "we don't handle rule backed FontFace objects yet");
if (HasRule()) {
return false;
}
RefPtr<URLExtraData> url = mFontFaceSet->GetURLExtraData();
if (NS_WARN_IF(!url)) {
// This should only happen on worker threads, where we failed to initialize
// the worker before it was shutdown.
aRv.ThrowInvalidStateError("Missing URLExtraData");
return false;
}
// FIXME(heycam): Should not allow modification of FontFaces that are
// CSS-connected and whose rule is read only.
bool changed;
if (!Servo_FontFaceRule_SetDescriptor(GetData(), aFontDesc, &aValue, url,
&changed)) {
aRv.ThrowSyntaxError("Invalid font descriptor");
return false;
}
if (!changed) {
return false;
}
if (aFontDesc == eCSSFontDesc_UnicodeRange) {
mUnicodeRangeDirty = true;
}
return true;
}
bool FontFaceImpl::SetDescriptors(const nsACString& aFamily,
const FontFaceDescriptors& aDescriptors) {
MOZ_ASSERT(!HasRule());
MOZ_ASSERT(!mDescriptors);
mDescriptors = Servo_FontFaceRule_CreateEmpty().Consume();
// Helper to call SetDescriptor and return true on success, false on failure.
auto setDesc = [=](nsCSSFontDesc aDesc, const nsACString& aVal) -> bool {
IgnoredErrorResult rv;
SetDescriptor(aDesc, aVal, rv);
return !rv.Failed();
};
// Parse all of the mDescriptors in aInitializer, which are the values
// we got from the JS constructor.
if (!setDesc(eCSSFontDesc_Family, aFamily) ||
!setDesc(eCSSFontDesc_Style, aDescriptors.mStyle) ||
!setDesc(eCSSFontDesc_Weight, aDescriptors.mWeight) ||
!setDesc(eCSSFontDesc_Stretch, aDescriptors.mStretch) ||
!setDesc(eCSSFontDesc_UnicodeRange, aDescriptors.mUnicodeRange) ||
!setDesc(eCSSFontDesc_FontFeatureSettings,
aDescriptors.mFeatureSettings) ||
(StaticPrefs::layout_css_font_variations_enabled() &&
!setDesc(eCSSFontDesc_FontVariationSettings,
aDescriptors.mVariationSettings)) ||
!setDesc(eCSSFontDesc_Display, aDescriptors.mDisplay) ||
(StaticPrefs::layout_css_font_metrics_overrides_enabled() &&
(!setDesc(eCSSFontDesc_AscentOverride, aDescriptors.mAscentOverride) ||
!setDesc(eCSSFontDesc_DescentOverride, aDescriptors.mDescentOverride) ||
!setDesc(eCSSFontDesc_LineGapOverride,
aDescriptors.mLineGapOverride))) ||
(StaticPrefs::layout_css_size_adjust_enabled() &&
!setDesc(eCSSFontDesc_SizeAdjust, aDescriptors.mSizeAdjust))) {
// XXX Handle font-variant once we support it (bug 1055385).
// If any of the descriptors failed to parse, none of them should be set
// on the FontFace.
mDescriptors = Servo_FontFaceRule_CreateEmpty().Consume();
if (mOwner) {
mOwner->MaybeReject(NS_ERROR_DOM_SYNTAX_ERR);
}
SetStatus(FontFaceLoadStatus::Error);
return false;
}
return true;
}
void FontFaceImpl::GetDesc(nsCSSFontDesc aDescID, nsACString& aResult) const {
aResult.Truncate();
Servo_FontFaceRule_GetDescriptorCssText(GetData(), aDescID, &aResult);
// Fill in a default value for missing descriptors.
if (aResult.IsEmpty()) {
if (aDescID == eCSSFontDesc_UnicodeRange) {
aResult.AssignLiteral("U+0-10FFFF");
} else if (aDescID == eCSSFontDesc_Display) {
aResult.AssignLiteral("auto");
} else if (aDescID != eCSSFontDesc_Family && aDescID != eCSSFontDesc_Src) {
aResult.AssignLiteral("normal");
}
}
}
void FontFaceImpl::SetUserFontEntry(gfxUserFontEntry* aEntry) {
AssertIsOnOwningThread();
if (mUserFontEntry == aEntry) {
return;
}
if (mUserFontEntry) {
mUserFontEntry->RemoveFontFace(this);
}
auto* entry = static_cast<Entry*>(aEntry);
if (entry) {
entry->AddFontFace(this);
}
mUserFontEntry = entry;
if (!mUserFontEntry) {
return;
}
MOZ_ASSERT(mUserFontEntry->HasUserFontSet(mFontFaceSet),
"user font entry must be associated with the same user font set "
"as the FontFace");
// Our newly assigned user font entry might be in the process of or
// finished loading, so set our status accordingly. But only do so
// if we're not going "backwards" in status, which could otherwise
// happen in this case:
//
// new FontFace("ABC", "url(x)").load();
//
// where the SetUserFontEntry call (from the after-initialization
// DoLoad call) comes after the author's call to load(), which set mStatus
// to Loading.
FontFaceLoadStatus newStatus = LoadStateToStatus(mUserFontEntry->LoadState());
if (newStatus > mStatus) {
SetStatus(newStatus);
}
}
bool FontFaceImpl::GetAttributes(gfxUserFontAttributes& aAttr) {
RawServoFontFaceRule* data = GetData();
if (!data) {
return false;
}
nsAtom* fontFamily = Servo_FontFaceRule_GetFamilyName(data);
if (!fontFamily) {
return false;
}
aAttr.mFamilyName = nsAtomCString(fontFamily);
StyleComputedFontWeightRange weightRange;
if (Servo_FontFaceRule_GetFontWeight(data, &weightRange)) {
aAttr.mRangeFlags &= ~gfxFontEntry::RangeFlags::eAutoWeight;
aAttr.mWeight = WeightRange(FontWeight::FromFloat(weightRange._0),
FontWeight::FromFloat(weightRange._1));
}
StyleComputedFontStretchRange stretchRange;
if (Servo_FontFaceRule_GetFontStretch(data, &stretchRange)) {
aAttr.mRangeFlags &= ~gfxFontEntry::RangeFlags::eAutoStretch;
aAttr.mStretch = StretchRange(stretchRange._0, stretchRange._1);
}
auto styleDesc = StyleComputedFontStyleDescriptor::Normal();
if (Servo_FontFaceRule_GetFontStyle(data, &styleDesc)) {
aAttr.mRangeFlags &= ~gfxFontEntry::RangeFlags::eAutoSlantStyle;
switch (styleDesc.tag) {
case StyleComputedFontStyleDescriptor::Tag::Normal:
aAttr.mStyle = SlantStyleRange(FontSlantStyle::NORMAL);
break;
case StyleComputedFontStyleDescriptor::Tag::Italic:
aAttr.mStyle = SlantStyleRange(FontSlantStyle::ITALIC);
break;
case StyleComputedFontStyleDescriptor::Tag::Oblique:
aAttr.mStyle = SlantStyleRange(
FontSlantStyle::FromFloat(styleDesc.AsOblique()._0),
FontSlantStyle::FromFloat(styleDesc.AsOblique()._1));
break;
default:
MOZ_ASSERT_UNREACHABLE("Unhandled tag");
}
}
StylePercentage ascent{0};
if (Servo_FontFaceRule_GetAscentOverride(data, &ascent)) {
aAttr.mAscentOverride = ascent._0;
}
StylePercentage descent{0};
if (Servo_FontFaceRule_GetDescentOverride(data, &descent)) {
aAttr.mDescentOverride = descent._0;
}
StylePercentage lineGap{0};
if (Servo_FontFaceRule_GetLineGapOverride(data, &lineGap)) {
aAttr.mLineGapOverride = lineGap._0;
}
StylePercentage sizeAdjust;
if (Servo_FontFaceRule_GetSizeAdjust(data, &sizeAdjust)) {
aAttr.mSizeAdjust = sizeAdjust._0;
}
StyleFontLanguageOverride langOverride;
if (Servo_FontFaceRule_GetFontLanguageOverride(data, &langOverride)) {
aAttr.mLanguageOverride = langOverride._0;
}
Servo_FontFaceRule_GetFontDisplay(data, &aAttr.mFontDisplay);
Servo_FontFaceRule_GetFeatureSettings(data, &aAttr.mFeatureSettings);
Servo_FontFaceRule_GetVariationSettings(data, &aAttr.mVariationSettings);
Servo_FontFaceRule_GetSources(data, &aAttr.mSources);
aAttr.mUnicodeRanges = GetUnicodeRangeAsCharacterMap();
return true;
}
bool FontFaceImpl::HasLocalSrc() const {
AutoTArray<StyleFontFaceSourceListComponent, 8> components;
Servo_FontFaceRule_GetSources(GetData(), &components);
for (auto& component : components) {
if (component.tag == StyleFontFaceSourceListComponent::Tag::Local) {
return true;
}
}
return false;
}
nsAtom* FontFaceImpl::GetFamilyName() const {
return Servo_FontFaceRule_GetFamilyName(GetData());
}
void FontFaceImpl::DisconnectFromRule() {
MOZ_ASSERT(HasRule());
// Make a copy of the descriptors.
mDescriptors = Servo_FontFaceRule_Clone(mRule).Consume();
mRule = nullptr;
mInFontFaceSet = false;
}
bool FontFaceImpl::HasFontData() const {
return mSourceType == eSourceType_Buffer && mBufferSource;
}
already_AddRefed<gfxFontFaceBufferSource> FontFaceImpl::TakeBufferSource() {
MOZ_ASSERT(mBufferSource);
return mBufferSource.forget();
}
bool FontFaceImpl::IsInFontFaceSet(FontFaceSetImpl* aFontFaceSet) const {
if (mFontFaceSet == aFontFaceSet) {
return mInFontFaceSet;
}
return mOtherFontFaceSets.Contains(aFontFaceSet);
}
void FontFaceImpl::AddFontFaceSet(FontFaceSetImpl* aFontFaceSet) {
MOZ_ASSERT(!IsInFontFaceSet(aFontFaceSet));
if (mFontFaceSet == aFontFaceSet) {
mInFontFaceSet = true;
} else {
mOtherFontFaceSets.AppendElement(aFontFaceSet);
}
}
void FontFaceImpl::RemoveFontFaceSet(FontFaceSetImpl* aFontFaceSet) {
MOZ_ASSERT(IsInFontFaceSet(aFontFaceSet));
if (mFontFaceSet == aFontFaceSet) {
mInFontFaceSet = false;
} else {
mOtherFontFaceSets.RemoveElement(aFontFaceSet);
}
// The caller should be holding a strong reference to the FontFaceSetImpl.
if (mUserFontEntry) {
mUserFontEntry->CheckUserFontSet();
}
}
gfxCharacterMap* FontFaceImpl::GetUnicodeRangeAsCharacterMap() {
if (!mUnicodeRangeDirty) {
return mUnicodeRange;
}
size_t len;
const StyleUnicodeRange* rangesPtr =
Servo_FontFaceRule_GetUnicodeRanges(GetData(), &len);
Span<const StyleUnicodeRange> ranges(rangesPtr, len);
if (!ranges.IsEmpty()) {
RefPtr<gfxCharacterMap> charMap = new gfxCharacterMap();
for (auto& range : ranges) {
charMap->SetRange(range.start, range.end);
}
charMap->Compact();
// As it's common for multiple font resources to have the same
// unicode-range list, look for an existing copy of this map to share,
// or add this one to the sharing cache if not already present.
mUnicodeRange =
gfxPlatformFontList::PlatformFontList()->FindCharMap(charMap);
} else {
mUnicodeRange = nullptr;
}
mUnicodeRangeDirty = false;
return mUnicodeRange;
}
// -- FontFaceImpl::Entry
// --------------------------------------------------------
/* virtual */
void FontFaceImpl::Entry::SetLoadState(UserFontLoadState aLoadState) {
gfxUserFontEntry::SetLoadState(aLoadState);
FontFaceLoadStatus status = LoadStateToStatus(aLoadState);
nsTArray<RefPtr<FontFaceImpl>> fontFaces;
{
MutexAutoLock lock(mMutex);
fontFaces.SetCapacity(mFontFaces.Length());
for (FontFaceImpl* f : mFontFaces) {
fontFaces.AppendElement(f);
}
}
for (FontFaceImpl* impl : fontFaces) {
auto* setImpl = impl->GetPrimaryFontFaceSet();
if (setImpl->IsOnOwningThread()) {
impl->SetStatus(status);
} else {
setImpl->DispatchToOwningThread(
"FontFaceImpl::Entry::SetLoadState",
[self = RefPtr{impl}, status] { self->SetStatus(status); });
}
}
}
/* virtual */
void FontFaceImpl::Entry::GetUserFontSets(
nsTArray<RefPtr<gfxUserFontSet>>& aResult) {
MutexAutoLock lock(mMutex);
aResult.Clear();
if (mFontSet) {
aResult.AppendElement(mFontSet);
}
for (FontFaceImpl* f : mFontFaces) {
if (f->mInFontFaceSet) {
aResult.AppendElement(f->mFontFaceSet);
}
for (FontFaceSetImpl* s : f->mOtherFontFaceSets) {
aResult.AppendElement(s);
}
}
// Remove duplicates.
aResult.Sort();
auto it = std::unique(aResult.begin(), aResult.end());
aResult.TruncateLength(it - aResult.begin());
}
/* virtual */ already_AddRefed<gfxUserFontSet>
FontFaceImpl::Entry::GetUserFontSet() const {
MutexAutoLock lock(mMutex);
if (mFontSet) {
return do_AddRef(mFontSet);
}
if (NS_IsMainThread() && mLoadingFontSet) {
return do_AddRef(mLoadingFontSet);
}
return nullptr;
}
void FontFaceImpl::Entry::CheckUserFontSetLocked() {
// If this is the last font containing a strong reference to the set, we need
// to clear the reference as there is no longer anything guaranteeing the set
// will be kept alive.
if (mFontSet) {
auto* set = static_cast<FontFaceSetImpl*>(mFontSet);
for (FontFaceImpl* f : mFontFaces) {
if (f->mFontFaceSet == set || f->mOtherFontFaceSets.Contains(set)) {
return;
}
}
}
// If possible, promote the most recently added FontFace and its owning
// FontFaceSetImpl as the primary set.
if (!mFontFaces.IsEmpty()) {
mFontSet = mFontFaces.LastElement()->mFontFaceSet;
} else {
mFontSet = nullptr;
}
}
void FontFaceImpl::Entry::FindFontFaceOwners(nsTHashSet<FontFace*>& aOwners) {
MutexAutoLock lock(mMutex);
for (FontFaceImpl* f : mFontFaces) {
if (FontFace* owner = f->GetOwner()) {
aOwners.Insert(owner);
}
}
}
void FontFaceImpl::Entry::AddFontFace(FontFaceImpl* aFontFace) {
MutexAutoLock lock(mMutex);
mFontFaces.AppendElement(aFontFace);
CheckUserFontSetLocked();
}
void FontFaceImpl::Entry::RemoveFontFace(FontFaceImpl* aFontFace) {
MutexAutoLock lock(mMutex);
mFontFaces.RemoveElement(aFontFace);
CheckUserFontSetLocked();
}
} // namespace dom
} // namespace mozilla
|