Skip to content

Commit 420cd65

Browse files
committed
Edits for non-goals, detectability and use-cases
1 parent 59e0eec commit 420cd65

File tree

1 file changed

+113
-24
lines changed

1 file changed

+113
-24
lines changed

css-fonts-4/src-explainer.md

Lines changed: 113 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
# The @font-face src descriptor
22

3+
## Authors
4+
5+
* Chris Liley, [@svgeesus](chris@w3.org)
6+
* Dominik Röttsches, [@drott](drott@chromium.org)
7+
38
## Introduction
49

5-
The @font-face rule is used
10+
The `@font-face` rule is used
611
to automatically download Web Fonts on demand,
712
and use them for rendering
813
without the user having to install them.
914
The fonts are discarded after use,
1015
and do not become installed fonts.
1116

12-
The src descriptor contains the url of the font to be downloaded, plus additional metadata so that unsuitable or unusable fonts are not wastefully downloaded.
17+
The `src` descriptor contains the url of the font to be downloaded,
18+
plus additional metadata so that unsuitable or
19+
unusable fonts are not wastefully downloaded.
1320

1421
Other descriptors include the font family name,
1522
the range of font weights supported,
@@ -150,6 +157,12 @@ and that combinatorial explosion would result
150157
but "opentype-variation-SVG-Graphite" would become
151158
unweildy and error-prone).
152159

160+
With the upcoming [COLRv1 color font format](https://github.com/googlefonts/colr-gradients-spec/),
161+
selection of the right font resource and detectability are once again
162+
important use cases which have been requested by early adopters of COLRv1,
163+
similar to the need to detect availability and prefer such font resources at the time
164+
when variable fonts where introduced.
165+
153166
## Use cases
154167

155168
1. I want to use color fonts,
@@ -170,8 +183,23 @@ For Chrome and Safari, I want to fall back to OpenType features.
170183
Both fonts are in "opentype" format,
171184
so again the font format by itself cannot be used to make the choice.
172185

186+
3. I want to know programmatically in my script code what level of font support is available.
187+
188+
Where 3. is in line in line with the TAG design principles, which recommend
189+
[detectability of a feature](https://w3ctag.github.io/design-principles/#feature-detect).
190+
191+
## Non-Goals
192+
193+
This proposal is not-intended as a server-side content negotiation solution. In
194+
many cases, third-party font providers currently choose based on User Agent
195+
which resources they deliver to clients at the time of the request to the
196+
included CSS. This is a different content negotiation mechanism from what is
197+
discussed in this proposal.
198+
173199
## Design constraints & alternative solutions
174200

201+
### Backwards compatibility
202+
175203
The largest constraint is that older browsers must continue
176204
to sucessfully parse the src descriptor,
177205
and to end up ignoring links to fonts containg unsupported features,
@@ -199,33 +227,94 @@ and also a desire to avoid microsyntaxes which:
199227
200228
Thus, it was [resolved](https://github.com/w3c/csswg-drafts/issues/633#issuecomment-380469287) in 2018 to put the extra "supports" requirements inside the parentheses of the format hint, but without string concatenation.
201229

230+
### Strings vs. Keywords
231+
202232
There was also Issue 6340 [Drop bracket matching step from @font-face src: line parsing](https://github.com/w3c/csswg-drafts/issues/6340), resolved in June 2021,
203233
about whether to do parenthesis matching before splitting the value of the src descriptor on commas.
204234

205-
The use cases mentioned above are then solved
206-
using [the current syntax](https://drafts.csswg.org/css-fonts-4/#src-desc)
207-
as follows:
208-
209-
/* 1. prefer COLRv1, then SVG-in-OpenType, then COLRv0 */
210-
@font-face {
211-
font-family: jewel;
212-
src: url(jewel-v1.woff2) format("woff2" supports COLRv1),
213-
url(jewel-svg.woff2) format("woff2" supports SVG),
214-
url(jewel-v0.woff2) format("woff2" supports COLRv0),
215-
url(boring.ttf) format("woff2");
216-
}
217-
218-
/* 2. prefer Graphite over OpenType layout, if supported */
219-
@font-face {
220-
font-family: rare;
221-
src: url(rare-graphite.otf) format("opentype" supports features(graphite)),
222-
url(rare.otf) format("opentype" supports features(opentype)),
223-
url(fallback.ttf) format("truetype");
224-
}
225-
226235
The original format hints were strings, although limited to a defined set.
227236
In CSS Fonts 4, either strings or keywords may be used for the format.
228237
The new font technology specifiers are keywords.
229238
There is an open Issue 6328 [@font-face src: url() format() keywords vs. strings ambiguous in spec](https://github.com/w3c/csswg-drafts/issues/6328) regarding how to serialize this.
230239

231-
An alternative solution to putting supports inside format is proposed in Issue 6520 [Nesting of @supports inside @font-face](https://github.com/w3c/csswg-drafts/issues/6520).
240+
An alternative solution to putting supports inside format is proposed in Issue 6520 [Nesting of @supports inside @font-face](https://github.com/w3c/csswg-drafts/issues/6520).
241+
242+
### Detectability
243+
244+
Detetecting font capabilities of the UA programmatically proves difficult due to
245+
no direct mapping of font technologies to `CSS.supports()`.
246+
247+
Testing of font capabilities is possible through probing for rendered pixels on
248+
a 2D canvas and testing for RGB color values, as done in @RoelN's
249+
[Chromacheck](https://pixelambacht.nl/chromacheck/) tool. However, this is an
250+
extremely wasteful approach, requiring canvas resources for something that can
251+
be returned by the UA and detected more efficienlty.
252+
253+
## Proposed Syntax
254+
255+
See
256+
[4.3.1 Parsing the src descriptor of the CSS Fonts Level 4 spec](https://drafts.csswg.org/css-fonts-4/#font-face-src-parsing)
257+
258+
## Examples
259+
260+
With the introduction of the `supports` part, the use cases 1 and 2 are then solved
261+
using [the current syntax](https://drafts.csswg.org/css-fonts-4/#src-desc)
262+
as follows:
263+
264+
### Use Case 1: Color Fonts
265+
266+
267+
```CSS
268+
/* 1. prefer COLRv1, then SVG-in-OpenType, then COLRv0 */
269+
@font-face {
270+
font-family: jewel;
271+
src: url(jewel-v1.woff2) format("woff2" supports COLRv1),
272+
url(jewel-svg.woff2) format("woff2" supports SVG),
273+
url(jewel-v0.woff2) format("woff2" supports COLRv0),
274+
url(boring.ttf) format("woff2");
275+
}
276+
```
277+
278+
### Use Case 2: Graphite Preference
279+
280+
```CSS
281+
/* 2. prefer Graphite over OpenType layout, if supported */
282+
@font-face {
283+
font-family: rare;
284+
src: url(rare-graphite.otf) format("opentype" supports features(graphite)),
285+
url(rare.otf) format("opentype" supports features(opentype)),
286+
url(fallback.ttf) format("truetype");
287+
}
288+
```
289+
290+
### Use Case 3: Detectability
291+
292+
The wording of the current specification currently reads:
293+
294+
> If a component value is parsed correctly and is of a format and font
295+
> technology that the UA supports, add it to the list of supported sources. If
296+
> parsing a component value results in a parsing error or its format or
297+
> technology are unsupported, do not add it to the list of supported sources.
298+
299+
Using this rule, availability of a particular font technology
300+
can be programmatically tested for
301+
by evaluating a `@font-face` rule and
302+
accessing its result `src:` descriptor value.
303+
Without downloading any font resources,
304+
knowledge about technology support can be retrieved.
305+
306+
```HTML
307+
<style>
308+
@font-face {
309+
font-family: a;
310+
src: url(/FEATURETESTVAR) format(woff2 supports variations);
311+
}
312+
</style>
313+
<script>
314+
window.onload = () => {
315+
var variations_supported = document.styleSheets[0].cssRules[0].cssText.includes("FEATURETESTVAR")
316+
? "Variations supported." : "Variations NOT supported.")
317+
console.log("Variations supported: " + variations);
318+
}
319+
</script>
320+
```

0 commit comments

Comments
 (0)