Copyright © 2014 W3C® (MIT, ERCIM, Keio, Beihang), All Rights Reserved. W3C liability, trademark and document use rules apply.
This CSS module describes events and interfaces used for dynamically loading font resources. CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
This document is a First Public Working Draft.
Publication as a First Public Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css-font-loading” in the subject, preferably like this: “[css-font-loading] …summary of comment…”
This document was produced by the CSS Working Group (part of the Style Activity).
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
CSS allows authors to load custom fonts from the web via the @font-face rule. While this is easy to use when authoring a stylesheet, it’s much more difficult to use dynamically via scripting.
Further, CSS allows the user agent to choose when to actually load a font; if a font face isn’t currently used by anything on a page, most user agents will not download its associated file. This means that later use of the font face will incur a delay as the user agent finally notices a usage and begins downloading and parsing the font file.
This specification defines a scripting interface to font faces in CSS, allowing font faces to be easily created and loaded from script. It also provides methods to track the loading status of an individual font, or of all the fonts on an entire page.
FontFace
InterfaceThe FontFace 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.
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(); };
For example, a FontFace with a style of "italic"
represents an italic font face;
it does not make the font face italic.
They turn on or off specific features in fonts that support them. Unlike the previous attributes, these attributes actually affect the font face.
It can change due to an author explicitly requesting a font face to load, such as through the load() method on FontFace, or implicitly by the user agent, due to it detecting that the font face is needed to draw some text on the screen.
All FontFace objects contain an internal [FontStatusPromise] 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 FontFace objects also contain
internal [Urls] and [Data] attributes,
of which one is not null
and the rest are null
.
A FontFace 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 FontFace(DOMString family, (DOMString or BinaryData) source, FontFaceDescriptors descriptors) method is called, execute these steps:
"unloaded"
.
Set its internal [FontStatusPromise] attribute to a newly-created promise object.
Return font face,
and complete the rest of these steps asynchronously.
Note: Note that this means that passing a naked url as the source argument,
like "http://example.com/myFont.woff"
,
won’t work - it needs to be at least wrapped in a url() function,
like "url(http://example.com/myFont.woff)"
.
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 source argument was a BinaryData, set font face’s internal [Data] attribute to the passed argument.
null
,
set font face’s status attribute to "loading",
and attempt to parse the data in it as a font.
If this is successful,
font face now represents the parsed font;
fulfill font face’s [FontStatusPromise] with font face,
and set its status attribute to "loaded".
If it is unsuccessful,
reject font face’s [FontStatusPromise] with a SyntaxError
and set font face’s status attribute to "error".
load()
methodThe load() method of FontFace 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 load() method is called, execute these steps:
null
,
or its status attribute is anything other than "unloaded"
,
abort this algorithm immediately.
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 FontFace’s load() method described here.
ready()
methodThe ready() method of FontFace 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 ready() method is called, execute these steps:
A CSS @font-face rule automatically defines a corresponding FontFace object, which is automatically placed in the document’s font source. This FontFace object is CSS-connected.
The FontFace object corresponding to a @font-face rule has its family, style, weight, stretch, unicodeRange, variant, and featureSettings 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 FontFace attribute, and vice versa.
The internal [Urls] attribute of the FontFace 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 FontFace 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 FontFace object is no longer CSS-connected. The connection is not restorable by any means (but adding the @font-face back to the stylesheet will create a brand new FontFace object which is CSS-connected).
FontFaceSet
Interfacedictionary 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; // 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, "loading" while one or more fonts loading, "loaded" otherwise readonly attribute FontFaceLoadStatus status; }; FontFaceSet implements EventTarget;
The set entries for a FontFaceSet is initially an empty list. A FontFaceSet attached to a document may have some initial FontFace objects prefilled in it; see the section on Interactions with CSS’s @font-face Rule 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 font load indicates when the loading of content for a given FontFace object completes. A FontFace object may list multiple alternate resources within its [Urls] attribute, including references to local fonts, but the term font load only refers to the loading of the finally selected resource for a given FontFace, not to the loading of each individual resource.
There are no pending font loads whenever all of the following are true:
If any of the above conditions are false, there are possibly pending font loads.
"loading"
.
Otherwise, it must have the value "loaded"
.
FontFaceSet objects also have internal [LoadingFonts], [LoadedFonts], [FailedFonts], and [PendingReadyPromises] attributes, all of which are initialized to the empty list.
Document the FontFaceSet behavior for Workers. (They start out empty, but can construct fonts or be sent them via postMessage.)
The FontFaceSet methods add()
and delete()
must throw an InvalidModificationError exception
if their argument is a CSS-connected FontFace object.
The FontFaceSet method clear()
must only remove the manually-added FontFace objects;
the CSS-connected FontFace objects are unaffected.
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 loading event fires when the document begins loading fonts, while the loadingdone and loadingerror 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 FontFaceSet
objects as IDL attributes:
Event handler | Event handler event type |
---|---|
onloading | loading |
onloadingdone | loadingdone |
onloadingerror | loadingerror |
To fire a font load event named e at a FontFaceSet target with optional font faces means to fire a simple event named e using the CSSFontFaceLoadEvent interface that also meets these conditions:
Whenever a FontFace in a given FontFaceSet font face set changes its status attribute to
Whenever one or more available font faces for a given FontFaceSet change their status attribute to "loading", the user agent must run the following steps:
Whenever one or more available font faces for a given FontFaceSet change their status attribute to "loaded" or "error", the user agent must run the following steps:
If there are ever no pending font loads and either of font face set’s [LoadedFonts] or [FailedFonts] attributes are not empty, user agents must run these steps:
If asked to find the matching font faces from a FontFaceSet source, for a given font string font and optionally some sample text text, run the following steps:
load()
methodThe load() method of FontFaceSet 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 load(font, text) method is called, execute these steps:
check()
methodThe check() method of FontFaceSet 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 check(font, text) method is called, execute these steps:
true
.
true
.
Otherwise, return false
.
ready()
methodBecause 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 ready() 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 ready() method is called, execute these steps:
Note: Authors should note that a given ready promise is only fulfilled once, but further fonts may be loaded after it fulfills. This is similar to listening for a loadingdone event to fire, but the callbacks passed to the ready() promise will always 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 ready promise 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 ready promise is only fulfilled after layout operations complete and no additional font loads are necessary.
Note: Note that the Promise returned by this ready() method is only ever fulfilled, never rejected, unlike the Promise returned by the FontFace ready() method.
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 font source 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 load() method of the corresponding FontFace object.
(This means it must run the same algorithm,
not literally call the value currently stored in the load
property of the object.)
FontFaceSource
interfaceinterface FontFaceSource { readonly attribute FontFaceSet fonts; }; Document implements FontFaceSource; WorkerGlobalScope implements FontFaceSource;
Any document, workers, or other context which can use fonts in some manner must implement the FontFaceSource interface. The value of the context’s fonts attribute is its font source, 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 font source 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 FontFace objects within the font source are its available font faces.
The set entries for a document’s font source must be initially populated with all the CSS-connected FontFace 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 CSS-connected FontFace objects must be added or removed from the document’s font source, and maintain this ordering.
All non-CSS-connected FontFace objects must be sorted after the CSS-connected ones, in insertion order.
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.
document.FontFaceSet.ready().then(function() { var content = document.getElementById("content"); content.style.visibility = "visible"; });
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);
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); }
@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>納豆はいかがでしょうか
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 loadingdone event fire.
Major changes include:
Several members of the Google Fonts team provided helpful feedback on font load events, as did Boris Zbarsky, Jonas Sicking and ms2ger.
Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.
All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]
Examples in this specification are introduced with the words "for example"
or are set apart from the normative text with class="example"
,
like this:
This is an example of an informative example.
Informative notes begin with the word "Note" and are set apart from the
normative text with class="note"
, like this:
Note, this is an informative note.
Conformance to this specification is defined for three conformance classes:
A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.
A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)
An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.
So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.
To avoid clashes with future CSS features, the CSS2.1 specification reserves a prefixed syntax for proprietary and experimental extensions to CSS.
Prior to a specification reaching the Candidate Recommendation stage in the W3C process, all implementations of a CSS feature are considered experimental. The CSS Working Group recommends that implementations use a vendor-prefixed syntax for such features, including those in W3C Working Drafts. This avoids incompatibilities with future changes in the draft.
Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.
To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.
Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.
No properties defined.