-
Notifications
You must be signed in to change notification settings - Fork 790
Expand file tree
/
Copy pathOverview.src.html
More file actions
813 lines (635 loc) · 32.3 KB
/
Overview.src.html
File metadata and controls
813 lines (635 loc) · 32.3 KB
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
<h1>CSS Font Load Events Module Level 3</h1>
<pre class='metadata'>
Shortname: css-font-load-events
Level: 3
Group: csswg
Status: ED
ED: http://dev.w3.org/csswg/css-font-load-events/
TR: http://www.w3.org/TR/css-font-load-events-3/
Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/
Former Editor: John Daggett, Mozilla, jdaggett@mozilla.com
Abstract: This CSS module describes events and interfaces used for dynamically loading font resources.
Link Defaults: css-fonts-3 (descriptor) src
Ignored Terms: promise, domstring, eventinit, cssfontfacerule, event, eventtarget, workerglobalscope
</pre>
<h2 id="introduction">
Introduction</h2>
<p class='issue'>
Fill in intro.
<h2 id="fontface-interface">
The <code>FontFace</code> Interface</h2>
The <a interface>FontFace</a> interface represents a single usable font face.
CSS ''@font-face'' rules implicitly define FontFace objects,
or they can be constructed manually from a url or binary data.
<pre class="idl">
typedef (ArrayBuffer or ArrayBufferView) BinaryData;
dictionary FontFaceDescriptors {
DOMString style = "normal";
DOMString weight = "normal";
DOMString stretch = "normal";
DOMString unicodeRange = "U+0-10FFFF";
DOMString variant = "normal";
DOMString featureSettings = "normal";
};
enum FontFaceLoadStatus { "unloaded", "loading", "loaded", "error" };
[Constructor(DOMString family, (DOMString or BinaryData) source,
FontFaceDescriptors descriptors)]
interface FontFace {
attribute DOMString family;
attribute DOMString style;
attribute DOMString weight;
attribute DOMString stretch;
attribute DOMString unicodeRange;
attribute DOMString variant;
attribute DOMString featureSettings;
readonly attribute FontFaceLoadStatus status;
// If the font face's source is url-based and its status isn't
// "loading" or "loaded", this kicks off the load.
void load();
// Returns a promise that fulfills or rejects when the font face
// reached "loaded" or "error" status.
Promise ready();
};
</pre>
<dl dfn-type=attribute dfn-for=FontFace>
<dt><dfn>family</dfn>, of type <a interface>DOMString</a>
<dt><dfn>style</dfn>, of type <a interface>DOMString</a>
<dt><dfn>weight</dfn>, of type <a interface>DOMString</a>
<dt><dfn>stretch</dfn>, of type <a interface>DOMString</a>
<dt><dfn>unicodeRange</dfn>, of type <a interface>DOMString</a>
<dd>
These attributes all represent the corresponding aspects of a font face,
as defined by the descriptors defined in the CSS ''@font-face'' rule.
They are parsed the same as the corresponding ''@font-face'' descriptors.
They are used by the font matching algorithm,
but otherwise have no effect.
For example, a <a interface>FontFace</a> with a <a attribute>style</a> of <code>"italic"</code>
<em>represents</em> an italic font face;
it does not <strong>make</strong> the font face italic.
<dt><dfn>variant</dfn>, of type <a interface>DOMString</a>
<dt><dfn>featureSettings</dfn>, of type <a interface>DOMString</a>
<dd>
These attributes have the same meaning,
and are parsed the same as,
the corresponding descriptors in the CSS ''@font-face'' rules.
They turn on or off specific features in fonts that support them.
Unlike the previous attributes,
these attributes actually affect the font face.
<dt><dfn>status</dfn>, of type <a enum>FontFaceLoadStatus</a>
<dd>
This attribute reflects the current status of the font face.
It must be "unloaded" for a newly-created <a interface>FontFace</a>.
It can change due to an author explicitly requesting a font face to load,
such as through the <a method for=FontFace>load()</a> method on <a interface>FontFace</a>,
or implicitly by the user agent,
due to it detecting that the font face is needed to draw some text on the screen.
</dl>
All <a interface>FontFace</a> objects contain an internal <dfn attribute for=FontFace>[FontStatusPromise]</dfn> attribute,
which tracks the status of the font.
It starts out pending,
and fulfills or rejects when the font is successfully loaded and parsed, or hits an error.
All <a interface>FontFace</a> objects also contain
internal <dfn attribute for=FontFace>[Urls]</dfn> and <dfn attribute for=FontFace>[Data]</dfn> attributes,
of which one is not <code>null</code> and the rest are <code>null</code>.
<h3 id='font-face-constructor'>
The Constructor</h3>
A <a interface>FontFace</a> can be constructed either
from a URL pointing to a font face file,
or from an ArrayBuffer (or ArrayBufferView) containing the binary representation of a font face.
When the <dfn method title='FontFace()' link-type=idl for=FontFace>Constructor(DOMString <dfn argument>family</dfn>, (DOMString or <a>BinaryData</a>) <dfn argument>source</dfn>, <a>FontFaceDescriptors</a> <dfn argument>descriptors</dfn>)</dfn> method is called,
execute these steps:
<ol link-type=idl>
<li>
Let <var>font face</var> be a fresh <a>FontFace</a> object.
Set <var>font face's</var> <a for=FontFace>status</a> argument to <code>"unloaded"</code>.
Set its internal <a>[FontStatusPromise]</a> attribute to a newly-created promise object.
Return <var>font face</var>,
and complete the rest of these steps asynchronously.
<li>
Parse the <a argument>family</a> argument,
and the members of the <a argument for='FontFace()'>descriptors</a> argument,
according to the grammars of the corresponding descriptors of the CSS ''@font-face'' rule.
If any of them fail to parse correctly,
reject <var>font face's</var> <a>[FontStatusPromise]</a> with a SyntaxError exception and abort these steps.
Otherwise, set <var>font face's</var> corresponding attributes to the serialization of the parsed values.
<li>
If the <a>source</a> argument was a <a>DOMString</a>,
parse it according to the grammar of the 'src' descriptor of the CSS ''@font-face'' rule.
If it fails to parse correctly,
reject <var>font face's</var> <a>[FontStatusPromise]</a> with a SyntaxError exception and abort these steps;
otherwise, set <var>font face's</var> internal <a>[Urls]</a> attribute to the string.
Note: Note that this means that passing a naked url as the source argument,
like <code>"http://example.com/myFont.woff"</code>,
won't work - it needs to be at least wrapped in a ''url()'' function,
like <code>"url(http://example.com/myFont.woff)"</code>.
In return for this inconvenience,
you get to specify multiple fallbacks,
specify the type of font each fallback is,
and refer to local fonts easily.
If the <a>source</a> argument was a <a>BinaryData</a>,
set <var>font face's</var> internal <a>[Data]</a> attribute to the passed argument.
<li>
If <var>font face's</var> <a>[Data]</a> attribute is not <code>null</code>,
set <var>font face's</var> <a for=FontFace>status</a> attribute to "loading",
and attempt to parse the data in it as a font.
If this is successful,
<var>font face</var> now represents the parsed font;
fulfill <var>font face's</var> <a>[FontStatusPromise]</a> with <var>font face</var>,
and set its <a for=FontFace>status</a> attribute to "loaded".
If it is unsuccessful,
reject <var>font face's</var> <a>[FontStatusPromise]</a> with a SyntaxError
and set <var>font face's</var> <a for=FontFace>status</a> attribute to "error".
</ol>
<h3 id='font-face-load'>
The <code>load()</code> method</h3>
The <a method for=FontFace>load()</a> method of <a interface>FontFace</a>
forces a url-based font face to request its font data and load.
For fonts constructed from binary data,
or fonts that are already loading or loaded,
it does nothing.
When the <dfn method for=FontFace>load()</dfn> method is called,
execute these steps:
<ol link-type=idl>
<li>
Let <var>font face</var> be the <a>FontFace</a> object on which this method was called.
<li>
If <var>font face's</var> <a>[Urls]</a> attribute is <code>null</code>,
or its <a for=FontFace>status</a> attribute is anything other than <code>"unloaded"</code>,
abort this algorithm immediately.
<li>
Otherwise,
set <var>font face's</var> <a for=FontFace>status</a> attribute to "loading",
return immediately,
and follow these steps asynchronously:
<li>
Using the value of <var>font face's</a> <a>[Urls]</a> attribute,
attempt to load a font as defined in [[!CSS3-FONTS]],
as if it was the value of a ''@font-face'' rule's 'src' descriptor.
<li>
If the attempt to load fails,
reject <var>font face's</var> <a>[FontStatusPromise]</a> with a NetworkError
and set <var>font face's</var> <a for=FontFace>status</a> attribute to "error".
<li>
Otherwise,
<var>font face</var> now represents the loaded font;
fulfill <var>font face's</var> <a>[FontStatusPromise]</a> with <var>font face</var>
and set <var>font face's</var> <a for=FontFace>status</a> attribute to "loaded".
</ol>
User agents can initiate font loads on their own,
whenever they determine that a given font face is necessary to render something on the page.
When this happens,
they must act as if they had called the corresponding <a interface>FontFace</a>’s <a method for=FontFace>load()</a> method described here.
<h3 id='font-face-ready'>
The <code>ready()</code> method</h3>
The <a method for=FontFace>ready()</a> method of <a interface>FontFace</a>
returns a Promise
which is fulfilled when the font face is successfully loaded,
or rejected when the font face fails to load or parse successfully.
When the <dfn method for="FontFace">ready()</dfn> method is called,
execute these steps:
<ol link-type=idl>
<li>
Let <var>font face</var> be the <a>FontFace</a> object on which this method was called.
<li>
Let <var>ready promise</var> be a newly-created promise object.
<li>
Resolve <var>ready promise</var> with <var>font face's</var> <a attribute>[FontStatusPromise]</a>.
<li>
Return <var>ready promise</var>.
</ol>
<h3 id='font-face-css-connection'>
Interaction with CSS’s ''@font-face'' Rule</h3>
A CSS ''@font-face'' rule automatically defines a corresponding <a interface>FontFace</a> object,
which is automatically placed in the document's <a>font source</a>.
This <a interface>FontFace</a> object is <dfn>CSS-connected</dfn>.
The <a interface>FontFace</a> object corresponding to a ''@font-face'' rule
has its <a attribute>family</a>, <a attribute>style</a>, <a attribute>weight</a>, <a attribute>stretch</a>, <a attribute>unicodeRange</a>, <a attribute>variant</a>, and <a attribute>featureSettings</a> attributes set to the same value as the corresponding descriptors in the ''@font-face'' rule.
There is a two-way connection between the two:
any change made to a ''@font-face'' descriptor is immediately reflected in the corresponding <a interface>FontFace</a> attribute,
and vice versa.
The internal <a attribute>[Urls]</a> attribute of the <a interface>FontFace</a> object is set to the value of the ''@font-face'' rule's 'src' descriptor,
and reflects any changes made to the 'src' descriptor.
Otherwise, a <a interface>FontFace</a> object created by a CSS ''@font-face'' rule is identical to one created manually.
If a ''@font-face'' rule is removed from the document, its corresponding <a interface>FontFace</a> object is no longer <a>CSS-connected</a>. The connection is not restorable by any means (but adding the ''@font-face'' back to the stylesheet will create a brand new <a interface>FontFace</a> object which <em>is</em> <a>CSS-connected</a>).
<h2 id="FontFaceSet-interface">
The <code>FontFaceSet</code> Interface</h2>
<pre class="idl">
dictionary CSSFontFaceLoadEventInit : EventInit {
sequence<CSSFontFaceRule> fontfaces = null;
};
[Constructor(DOMString type, optional CSSFontFaceLoadEventInit eventInitDict)]
interface CSSFontFaceLoadEvent : Event {
readonly attribute sequence<CSSFontFaceRule> fontfaces;
};
enum FontFaceSetLoadStatus { "loading", "loaded" };
[SetClass(FontFace)]
interface FontFaceSet {
// -- events for when loading state changes
attribute EventHandler onloading;
attribute EventHandler onloadingdone;
attribute EventHandler onloadingerror;
// Return the fonts that match the given "font" argument
// and can render the given characters.
sequence<FontFace> match(DOMString font, optional DOMString text = " ");
// check and start loads if appropriate
// and fulfill promise when all loads complete
Promise load(DOMString font, optional DOMString text = " ");
// return whether all fonts in the fontlist are loaded
// (does not initiate load if not available)
boolean check(DOMString font, optional DOMString text = " ");
// async notification that font loading and layout operations are done
Promise ready();
// loading state, true while one or more fonts loading, false otherwise
readonly attribute FontFaceLoadStatus status;
};
FontFaceSet implements EventTarget;
</pre>
The set entries for a <a interface>FontFaceSet</a> is initially an empty list.
A <a interface>FontFaceSet</a> attached to a document may have some initial <a interface>FontFace</a> objects prefilled in it;
see the section on <a href="#document-font-face-set">Interactions with CSS's @font-face Rule</a> for details.
Because font families are loaded only when they are used,
content sometimes needs to understand when the loading of fonts occurs.
Authors can use the events and methods defined here to allow greater control over actions
that are dependent upon the availability of specific fonts.
The term <dfn>font load</dfn> indicates when the
loading of content for a given <a interface>FontFace</a> object completes.
A <a interface>FontFace</a> object may list multiple alternate resources
within its <a attribute>[Urls]</a> attribute,
including references to local fonts,
but the term font load only refers to the loading of the finally selected resource for a given <a interface>FontFace</a>,
not to the loading of each individual resource.
There are <dfn>no pending font loads</dfn> whenever all of the following are true:
<ul>
<li>
The document has finished loading.
<li>
There are no pending stylesheet requests.
<li>
There are no <a>available font faces</a>
with a <a attribute for=FontFace>status</a> of "loading".
<li>
There are no pending layout operations which might cause the user agent to request a font.
</ul>
If any of the above conditions are false,
there are <dfn>possibly pending font loads</dfn>.
<a interface>FontFaceSet</a> objects also have internal
<dfn attribute for=FontFaceSet>[LoadingFonts]</dfn>,
<dfn attribute for=FontFaceSet>[LoadedFonts]</dfn>,
<dfn attribute for=FontFaceSet>[FailedFonts]</dfn>,
and <dfn attribute for=FontFaceSet>[PendingReadyPromises]</dfn> attributes,
all of which are initialized to the empty list.
<p class="issue">
Document the FontFaceSet behavior for Workers.
(They start out empty, but can construct fonts or be sent them via postMessage.)
<h3 id='set-modifications'>
Modifications of normal Set methods</h3>
The <a interface>FontFaceSet</a> methods <code>add()</code> and <code>delete()</code>
must throw an InvalidModificationError exception
if their argument is a <a>CSS-connected</a> <a interface>FontFace</a> object.
The <a interface>FontFaceSet</a> method <code>clear()</code>
must only remove the manually-added <a interface>FontFace</a> objects;
the <a>CSS-connected</a> <a interface>FontFace</a> objects are unaffected.
<h3 id='FontFaceSet-events'>
Events</h3>
Font load events make it easy to respond to the font-loading behavior of the entire document,
rather than having to listen to each font specifically.
The <dfn event for="FontFaceSet">loading</dfn> event
fires when the document begins loading fonts,
while the <dfn event for="FontFaceSet">loadingdone</dfn>
and <dfn event for="FontFaceSet">loadingerror</dfn> events
fire when the document is done loading fonts,
containing the fonts that successfully loaded
or failed to load,
respectively.
The following are the event handlers (and their corresponding event handler event types)
that must be supported by <code>FontFaceSet</code> objects as IDL attributes:
<table class="data" id="eventhandlers">
<thead>
<tr>
<th>Event handler
<th>Event handler event type
<tbody>
<tr>
<th><a attribute>onloading</a>
<td><a event>loading</a>
<tr>
<th><a attribute>onloadingdone</a>
<td><a event>loadingdone</a>
<tr>
<th><a attribute>onloadingerror</a>
<td><a event>loadingerror</a>
</table>
To <dfn>fire a font load event</dfn> named <var>e</var>
at a <a interface>FontFaceSet</a> <var>target</var>
with optional <var>font faces</var>
means to
<a href="http://www.w3.org/TR/html5/webappapis.html#event-firing">fire a simple event</a> named <var>e</var>
using the <a interface>CSSFontFaceLoadEvent</a> interface that also meets these conditions:
<ol>
<li>
The <a attribute>fontfaces</a> attribute is initialized to the given list of <a interface>FontFace</a> objects.
</ol>
Whenever a <a interface>FontFace</a> in a given <a interface>FontFaceSet</a> <var>font face set</var>
changes its <a attribute for=FontFace>status</a> attribute to
Whenever one or more <a>available font faces</a> for a given <a interface>FontFaceSet</a>
change their <a attribute for=FontFace>status</a> attribute to "loading",
the user agent must run the following steps:
<ol>
<li>
Let <var>font face set</var> be the given <a interface>FontFaceSet</a>,
and <var>loading fonts</var> be the <a interface>FontFace</a> objects
that have newly switched to "loading" status,
in the same order as they appear in <var>font face set</var>.
<li>
Set the <a attribute for=FontFaceSet>status</a> attribute of <var>font face set</var> to "loading".
<li>
If <var>font face set's</var> <a attribute>[LoadingFonts]</a> attribute is currently empty,
<a>fire a font load event</a> named <a event>loading</a> at <var>font face set</var>.
<li>
Append the <var>loading fonts</var> to <var>font face set's</var> <a attribute>[LoadingFonts]</a> attribute.
</ol>
Whenever one or more <a>available font faces</a> for a given <a interface>FontFaceSet</a>
change their <a attribute for=FontFace>status</a> attribute to "loaded" or "error",
the user agent must run the following steps:
<ol>
<li>
Let <var>font face set</var> be the given <a interface>FontFaceSet</a>,
and <var>loaded fonts</var> be the <a interface>FontFace</a> objects
that have newly switched to "loaded" or "error" status,
in the same order as they appear in <var>font face set</var>.
<li>
For each font in the <var>loaded fonts</var>,
if their <a attribute for=FontFace>status</a> attribute is "loaded",
append them to <var>font face set's</var> <a attribute>[LoadedFonts]</a> attribute;
if it's "error",
append them to <var>font face set's</var> <a attribute>[FailedFonts]</a> attribute.
</ol>
If there are ever <a>no pending font loads</a>
and either of <var>font face set's</var> <a attribute>[LoadedFonts]</a> or <a attribute>[FailedFonts]</a> attributes are not empty,
user agents must run these steps:
<ol>
<li>
Set <var>font face set's</var> <a attribute for=FontFaceSet>status</a> attribute to "loaded".
<li>
<a>Fire a font load event</a> named <a event>loadingdone</a> at <var>font face set</var>
with the contents of <var>font face set's</var> <a attribute>[LoadedFonts]</a> attribute, if any.
<li>
If <var>font face set's</var> <a attribute>[FailedFonts]</a> attribute is non-empty,
<a>fire a font load event</a> named <a event>loadingerror</a> at <var>font face set</var>
with the contents of <var>font face set's</var> <a attribute>[FailedFonts]</a> attribute.
<li>
If <var>font face set's</var> <a attribute>[PendingReadyPromises]</a> attribute is non-empty,
fulfill each promise in the attribute's value with <var>font face set</var>.
<li>
Empty <var>font face set's</var> <a attribute>[LoadingFonts]</a>, <a attribute>[LoadedFonts]</a>, <a attribute>[FailedFonts]</a>, and <a attribute>[PendingReadyPromises]</a> attributes.
</ol>
<h3 id='font-face-set-match'>
The <code>match()</code> method</h3>
The <a for=FontFaceSet>match()</a> method of <a interface>FontFaceSet</a> returns
the subset of <a>available font faces</a>
that match a given 'font' specifier
and can render the given characters.
When the <dfn method for="FontFaceSet">match(<dfn argument>font</dfn>, <dfn argument>text</dfn>)</dfn> method is called,
execute these steps:
<ol>
<li>
Parse the value of the <a argument for='load()'>font</a> argument,
using the CSS value syntax of the 'font' property.
<li>
If a syntax error occurs,
throw a SyntaxError exception.
<li>
Otherwise, let <var>font family list</var> be the set of families and
<var>font style</var> be the other font style attributes.
<li>
For each family in <var>font family list</var>,
use the font matching rules to select the <a>available font faces</a> that match the <var>font style</var>.
The use of the <a attribute>unicodeRange</a> attribute means that this may be more than just a single font face.
<li>
Remove from the set of font faces all faces that have defined 'unicode-range'
values that don't intersect the range of character values in the <a argument for='load()'>text</a>
argument and set this to be the <var>font load list</var>.
<li>
Return the <var>font load list</var>.
</ol>
<h3 id='font-face-set-load'>
The <code>load()</code> method</h3>
The <a for=FontFaceSet>load()</a> method of <a interface>FontFaceSet</a> will determine whether all fonts in the given font list
have been loaded and are available.
If any fonts are downloadable fonts and have not already been loaded,
the user agent will initiate the load of each of these fonts.
It returns a Promise,
which is fulfilled when all of the fonts are loaded and ready to be used,
or rejected if any font failed to load properly.
When the <dfn method for="FontFaceSet">load(<dfn argument>font</dfn>, <dfn argument>text</dfn>)</dfn> method is called,
execute these steps:
<ol link-type=idl>
<li>
Let <var>font face set</var> be the <a interface>FontFaceSet</a> object this method was called on.
Let <var>promise</var> be a newly-created promise object.
<li>
Return <var>promise</var>.
Complete the rest of these steps asynchronously.
<li>
Call <var>font face set's</var> <a method for=FontFaceSet>match()</a> method
with the same arguments as this method,
and let <var>font face list</var> be the return value.
If <a method for=FontFaceSet>match()</a> threw an exception,
reject <var>promise</var> with that same exception and terminate these steps.
<li>
For all of the font faces in the <var>font face list</var>,
call their <a method for=FontFace>load()</a> method.
<li>
Resolve <var>promise</var> with the result of
waiting for all of the <a>[FontStatusPromise]</a>s of each font face in the <var>font face list</var>, in order.
</ol>
<h3 id='font-face-set-check'>
The <code>check()</code> method</h3>
The <a>check()</a> method of <a interface>FontFaceSet</a> will determine whether all fonts in the given font list
have been loaded and are available.
If all fonts are available,
it returns true;
otherwise, it returns false.
When the <dfn method for="FontFaceSet">check(<dfn argument>font</dfn>, <dfn argument>text</dfn>)</dfn> method is called,
execute these steps:
<ol>
<li>
Let <var>font face set</var> be the <a interface>FontFaceSet</a> object this method was called on.
<li>
Call <var>font face set's</var> <a method for=FontFaceSet>match()</a> method
with the same arguments as this method,
and let <var>font face list</var> be the return value.
If <a method for=FontFaceSet>match()</a> threw an exception,
throw that same exception and terminate these steps.
<li>
If the <var>font face list</var> contains no font faces,
return <code class="idl-code">false</code>.
<li>
If all fonts in the <var>font face list</var> have a <a attribute for=FontFace>status</a> attribute of "loaded",
return <code>true</code>.
Otherwise, return <code>false</code>.
</ol>
<h3 id='font-face-set-ready'>
The <code>ready()</code> method</h3>
Because the number of fonts loaded depends on the how many fonts are used for a given piece of text,
in some cases whether fonts need to be loaded or not may not be known.
The <a for=FontFaceSet>ready()</a> method returns a Promise which is resolved when the document is done loading fonts,
which provides a way for authors to avoid having to keep track of which fonts have or haven't been loaded
before examining content which may be affected by loading fonts.
When the <dfn method for=FontFaceSet>ready()</dfn> method is called,
execute these steps:
<ol>
<li>
Let <var>font face set</var> be the <a interface>FontFaceSet</a> object this method was called on.
Let <var>ready promise</var> be a fresh Promise which is initially pending.
<li>
If <var>font face set's</var> <a attribute for=FontFaceSet>status</a> attribute is "loaded",
fulfill <var>ready promise</var> with <var>font face set</var>.
Otherwise, append <var>ready promise</var>
to <var>font face set's</var> <a attribute>[PendingReadyPromises]</a> attribute.
<li>
Return <var>ready promise</var>.
</ol>
Note: Authors should note that a given <var>ready promise</var> is only fulfilled once,
but further fonts may be loaded after it fulfills.
This is similar to listening for a <a event>loadingdone</a> event to fire,
but the callbacks passed to the <a method for=FontFaceSet>ready()</a> promise will <strong>always</strong> get called,
even when no font loads occur because the fonts in question are already loaded.
It's a simple, easy way to synchronize code to font loads
without the need to keep track of what fonts are needed and precisely when they load.
Note: Note that the user agent may need to iterate over multiple font loads before the <var>ready promise</var> is fulfilled.
This can occur with font fallback situations,
where one font in the fontlist is loaded
but doesn't contain a particular glyph
and other fonts in the fontlist need to be loaded.
The <var>ready promise</var> is only fulfilled after layout operations complete
and no additional font loads are necessary.
Note: Note that the Promise returned by this <a method for=FontFaceSet>ready()</a> method is only ever fulfilled,
never rejected,
unlike the Promise returned by the <a interface>FontFace</a> <a method for=FontFace>ready()</a> method.
<h3 id='font-face-set-css'>
Interaction with CSS Font Loading and Matching</h3>
When the font matching algorithm in [[CSS3-FONTS]] is run automatically by the user-agent,
the set of font faces it matches over must be precisely the set of fonts in the <a>font source</a> for the document,
plus any local font faces.
When a user-agent needs to load a font face,
it must do so by calling the <a method for=FontFace>load()</a> method
of the corresponding <a interface>FontFace</a> object.
(This means it must run the same algorithm,
not literally call the value currently stored in the <code>load</code> property of the object.)
<h2 id='font-face-source'>
The <code>FontFaceSource</code> interface</h2>
<pre class='idl'>
interface <dfn interface>FontFaceSource</dfn> {
readonly attribute <a interface>FontFaceSet</a> <dfn attribute for=FontFaceSource>fonts</dfn>;
};
Document implements <a interface>FontFaceSource</a>;
WorkerGlobalScope implements <a interface>FontFaceSource</a>;
</pre>
Any document, workers, or other context which can use fonts in some manner must implement the <a interface>FontFaceSource</a> interface.
The value of the context’s <a attribute>fonts</a> attribute is its <dfn>font source</dfn>,
which provides all of the fonts used in font-related operations,
unless defined otherwise.
Operations referring to “the font source” must be interpreted as referring to the <a>font source</a> of the relevant context in which the operation is taking place.
For any font-related operation that takes place within one of these contexts,
the <a interface>FontFace</a> objects within the <a>font source</a> are its <dfn>available font faces</dfn>.
<h3 id="document-font-face-set">
Interaction with CSS’s ''@font-face'' Rule</h3>
The set entries for a document's <a>font source</a>
must be initially populated with all the <a>CSS-connected</a> <a interface>FontFace</a> objects
from all of the CSS ''@font-face'' rules in the document's stylesheets,
in document order.
As ''@font-face'' rules are added or removed from a stylesheet,
or stylesheets containing ''@font-face'' rules are added or removed,
the corresponding <a>CSS-connected</a> <a interface>FontFace</a> objects
must be added or removed from the document's <a>font source</a>,
and maintain this ordering.
All non-<a>CSS-connected</a> <a interface>FontFace</a> objects must be sorted after the <a>CSS-connected</a> ones,
in insertion order.
<p class='issue'>
Can/should we include the local system fonts,
or is that a fingerprinting problem?
I think the set of local fonts is trivially discoverable anyway.
Alternately, define a specialized variant of FontFace which represents all the local fonts collectively,
and perhaps can be queried against?
This would make FontFaceSet fully explain the current behavior of fonts.
<h2 id="font-load-event-examples">
API Examples</h2>
<div class="example">
To show content only after all font loads complete:
<pre>
document.FontFaceSet.ready().then(function() {
var content = document.getElementById("content");
content.style.visibility = "visible";
});
</pre>
</div>
<div class="example">
Drawing text in a canvas with a downloadable font, explicitly
initiating the font download and drawing upon completion:
<pre>
function drawStuff() {
var ctx = document.getElementById("c").getContext("2d");
ctx.fillStyle = "red";
ctx.font = "50px MyDownloadableFont";
ctx.fillText("Hello!", 100, 100);
}
document.fonts.load("50px MyDownloadableFont")
.then(drawStuff, handleError);
</pre>
</div>
<div class="example">
A rich text editing application may need to measure text elements
after editing operations have taken place. Since style changes may
or may not require additional fonts to be downloaded, or the fonts
may already have been downloaded, the measurement procedures need to
occur after those font loads complete:
<pre>
function measureTextElements() {
// contents can now be measured using the metrics of
// the downloadable font(s)
}
function doEditing() {
// content/layout operations that may cause additional font loads
document.fonts.ready().then(measureTextElements);
}
</pre>
</div>
<div class="example">
The <a event>loadingdone</a> event only fires after all font related loads have completed
<strong>and</strong> text has been laid out without causing additional font loads:
<pre>
@font-face {
font-family: latin-serif;
src: url(latinserif.woff) format("woff"); /* contains no kanji/kana */
}
@font-face {
font-family: jpn-mincho;
src: url(mincho.woff) format("woff");
}
body { font-family: latin-serif, jpn-mincho; }
<p>納豆はいかがでしょうか
</pre>
In this situation, the user agent first downloads “latinserif.woff”
and then tries to use this to draw the Japanese text. But because no
Japanese glyphs are present in that font, fallback occurs and the font
“mincho.woff” is downloaded. Only after the second font is downloaded
and the Japanese text laid out does the <a event>loadingdone</a>
event fire.
</div>
<h2 class="no-num" id="changes">Changes</h2>
<h3 class="no-num" id="changes-since-20130212">
Changes from the <a href="http://www.w3.org/TR/2013/WD-css3-fonts-20130212/">February 2013 CSS3 Fonts Working Draft</a></h3>
Major changes include:
<ul>
<li>Add details of font load errors.
<li>Switched to a promise-based design for load() and ready().
<li>Renamed notifyWhenFontsReady() to ready().
<li>Added a match() method, to directly expose the font matching algorithm.
<li>Added a FontFace interface, to directly represent the individual font faces, and allow manual construction of font faces without having to indirect through a @font-face rule.
<li>Dropped the per-font events, in favor of promise-based methods on FontFace.
</ul>
<h2 class=no-num id=acknowledgments>
Acknowledgments</h2>
Several members of the Google Fonts team provided helpful feedback on font load events,
as did Boris Zbarsky, Jonas Sicking and ms2ger.