@@ -359,6 +359,9 @@ <h2 class="no-num no-toc" id=contents>Table of contents</h2>
359359 < li > < a href ="#font-feature-resolution "> < span class =secno > 7 </ span > Font
360360 feature resolution </ a >
361361
362+ < li > < a href ="#font-load-events "> < span class =secno > 8 </ span > Font load
363+ events</ a >
364+
362365 < li class =no-num > < a href ="#platform-props-to-css "> Appendix A: Mapping
363366 platform font properties to CSS properties</ a >
364367
@@ -689,7 +692,7 @@ <h3 id=font-family-prop><span class=secno>3.1 </span>Font family: the <a
689692 depending upon the platform font management API's. The Windows GDI API
690693 only allows four faces to be grouped into a family while the DirectWrite
691694 API and API's on OSX and other platforms support font families with a
692- variety of weights, widths and styles (see < a
695+ variety of weights, widths and slopes (see < a
693696 href ="#platform-props-to-css "> Appendix A</ a > for more details).
694697
695698 < p > Some font formats allow fonts to carry multiple localizations of the
@@ -4805,6 +4808,148 @@ <h2 id=font-feature-resolution><span class=secno>7 </span>Font feature
48054808 discretionary ligatures.</ p >
48064809 </ div >
48074810
4811+ < h2 id =font-load-events > < span class =secno > 8 </ span > Font load events</ h2 >
4812+
4813+ < p > Since fonts defined via @font-face rules are loaded on demand, pages may
4814+ need to know precisely when fonts have completed downloading before
4815+ measuring text elements on the page or to show some form of interim user
4816+ interface state.
4817+
4818+ < p > To allow font loading to be tracked explicitly within content the
4819+ following event target is added to the document of the page:
4820+
4821+ < dl >
4822+ < dt > < b > IDL Definition</ b >
4823+
4824+ < dd >
4825+ < div class =idl-code >
4826+ < pre >
4827+ partial interface Document {
4828+ readonly attribute FontLoader fontloader;
4829+ };
4830+
4831+ [Constructor]
4832+ interface CSSFontFaceLoadEvent : Event {
4833+ readonly attribute CSSFontFaceRule fontface;
4834+ readonly attribute DOMString error;
4835+ }
4836+
4837+ enum FontLoaderReadyState {
4838+ "idle",
4839+ "loading"
4840+ };
4841+
4842+ [Constructor]
4843+ interface FontLoader : EventTarget {
4844+
4845+ // -- events for one or more fonts loading and completing
4846+ [TreatNonCallableAsNull] attribute Function? onloading;
4847+ [TreatNonCallableAsNull] attribute Function? onloadingdone;
4848+
4849+ // -- events for each individual font load
4850+ [TreatNonCallableAsNull] attribute Function? onload;
4851+ [TreatNonCallableAsNull] attribute Function? onerror;
4852+
4853+ // async load
4854+ void loadFont(DOMString font, optional DOMString text);
4855+
4856+ // notify after font loads complete or no loads needed
4857+ callback FontsReadyCallback = void ();
4858+ void notifyWhenFontsReady(FontsReadyCallback fontsReady);
4859+
4860+ // "idle" or "loading"
4861+ readonly attribute FontLoaderReadyState readyState;
4862+ };</ pre >
4863+ </ div >
4864+ </ dd >
4865+ <!-- IDL -->
4866+ </ dl >
4867+
4868+ < p > When layout determines that a font or a set of fonts defined via
4869+ @font-face rules need to be loaded, the readyState switches from the
4870+ initial state of "idle" to the "loading" state and the "loading" event
4871+ fires. As each font load completes the "load" event fires (or an "error"
4872+ event fires if none of the resources listed for the ‘< a
4873+ href ="#descdef-src "> < code class =property > src</ code > </ a > ’ descriptor
4874+ contain valid data). After the final font load completes, the readyState
4875+ is switched to "idle" and the "load" (or "error") event fires. Then the
4876+ "loadingdone" event fires. For example, if three fonts are loaded at the
4877+ same time, a "loading" event followed by three "load" or "error" events
4878+ will fire, followed by a "loadingdone" event.
4879+
4880+ < p > The ‘< code class =property > loadFont</ code > ’ method is used to
4881+ explicitly load a font or set of fonts, for use with API's like canvas
4882+ where text drawing operations need to happen after a specific set of fonts
4883+ have been loaded. The ‘< a href ="#propdef-font "> < code
4884+ class =property > font</ code > </ a > ’ parameter indicates the exact set of
4885+ families and style to use and the optional ‘< code
4886+ class =property > text</ code > ’ parameter provides the exact text for which
4887+ fonts are to be selected, since the unicode-range descriptor of @font-face
4888+ rules indicates which codepoints a font may support.
4889+
4890+ < p > Because the number of fonts loaded depends on the how many fonts are
4891+ used for a given piece of text, in some cases whether fonts need to be
4892+ loaded or not may not be known. The ‘< code
4893+ class =property > notifyWhenFontsReady</ code > ’ method indicates a callback
4894+ function to be called the next time layout completes and the user agent
4895+ can determine whether fonts need to be loaded or not based on the current
4896+ state of layout. If no fonts need to be loaded, the callback is called
4897+ immediately upon layout completion. If fonts need to be loaded, the
4898+ callback is called after the user agent completes after all font loads. If
4899+ fonts are loaded, the callback is called after the "loadingdone" event
4900+ fires. The callback is only called once, authors need to explicitly call
4901+ this method again to have the callback called again.
4902+
4903+ < div class =example >
4904+ < p > To show content only after all font loads complete:</ p >
4905+
4906+ < pre > document.fontloader.onloadingdone = function() {
4907+ var content = document.getElementById("content");
4908+ content.style.visibility = "visible";
4909+ }
4910+ </ pre >
4911+ </ div >
4912+
4913+ < div class =example >
4914+ < p > Drawing text in a canvas with a downloadable font, explicitly
4915+ initiating the font download and drawing upon completion:</ p >
4916+
4917+ < pre > function drawStuff() {
4918+ var ctx = document.getElementById("c").getContext("2d");
4919+
4920+ ctx.fillStyle = "red";
4921+ ctx.font = "50px MyDownloadableFont";
4922+ ctx.fillText("Hello!", 100, 100);
4923+ }
4924+
4925+ window.onload = function() {
4926+ document.fontloader.loadFont("50px MyDownloadableFont");
4927+ }
4928+
4929+ document.fontloader.onloadingdone = drawStuff;</ pre >
4930+ </ div >
4931+
4932+ < div class =example >
4933+ < p > A rich text editing application may need to measure text elements after
4934+ editing operations have taken place. Since style changes may or may not
4935+ require additional fonts to be downloaded, or the fonts may already have
4936+ been downloaded, the measurement procedures need to occur after those
4937+ font loads complete:</ p >
4938+
4939+ < pre > function measureTextElements() {
4940+ // contents can now be measured using the metrics of
4941+ // the downloadable font(s)
4942+ }
4943+
4944+ function doEditing() {
4945+
4946+ // content/layout operations that may cause additional font loads
4947+
4948+ document.fontloader.notifyWhenFontsReady(measureTextElements);
4949+ }
4950+ </ pre >
4951+ </ div >
4952+
48084953 < h2 class =no-num id =platform-props-to-css > Appendix A: Mapping platform font
48094954 properties to CSS properties</ h2 >
48104955
0 commit comments