Skip to content

Commit 8f67f30

Browse files
authored
Merge pull request w3c#5521 from xiaochengh/font-metrics-override
Add @font-face descriptors for overriding font metrics
2 parents 8eaca51 + 439f4d2 commit 8f67f30

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

css-fonts-4/Overview.bs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,94 @@ Initial: normal
29082908

29092909
This descriptor defines initial settings that apply when the font defined by an @font-face rule is rendered. It does not affect font selection. Values are identical to those defined for the 'font-language-override!!property' property defined below except that the value inherit is omitted. When multiple font feature descriptors, properties, or variations are used, the cumulative effect on text rendering is detailed in the section [[#font-feature-variation-resolution]] below.
29102910

2911+
<h3 id="font-metrics-override-desc">
2912+
Default font metrics overriding:
2913+
the 'ascent-override', 'descent-override' and 'line-gap-override' descriptors</h3>
2914+
2915+
<pre class='descdef'>
2916+
Name: ascent-override
2917+
Value: normal | <<percentage>>
2918+
For: @font-face
2919+
Initial: normal
2920+
</pre>
2921+
2922+
<pre class='descdef'>
2923+
Name: descent-override
2924+
Value: normal | <<percentage>>
2925+
For: @font-face
2926+
Initial: normal
2927+
</pre>
2928+
2929+
<pre class='descdef'>
2930+
Name: line-gap-override
2931+
Value: normal | <<percentage>>
2932+
For: @font-face
2933+
Initial: normal
2934+
</pre>
2935+
2936+
The 'ascent-override', 'descent-override' and 'line-gap-override' descriptors define the
2937+
<a spec="CSS-INLINE-3">ascent metric</a>, <a spec="CSS-INLINE-3">descent metric</a> and
2938+
<a spec="CSS-INLINE-3">line gap metric</a> of the font, respectively.
2939+
2940+
When the descriptor value is 'normal', the corresponding metric value is obtained from the
2941+
font file directly.
2942+
2943+
Note: User agents may draw data from different places from the font file as the metric values,
2944+
which results in different text layouts.
2945+
2946+
When the descriptor value is a percentage, the corresponding metric value is resolved as the
2947+
given percentage multiplied by the used font size. Negative values are invalid at parse time.
2948+
2949+
<div class="example">
2950+
The percentage is resolved against different font sizes for different elements.
2951+
2952+
<pre>
2953+
@font-face {
2954+
font-family: overridden-font;
2955+
ascent-override: 50%;
2956+
...
2957+
}
2958+
2959+
&lt;span style="font-family: overridden-font; font-size: 20px;"&gt;
2960+
Outer span content
2961+
&lt;span style="font-size: 150%;"&gt;Inner span content&lt;/span&gt;
2962+
&lt;/span&gt;
2963+
</pre>
2964+
2965+
The outer span uses an <a spec="CSS-INLINE-3" lt="ascent metric">ascent</a> value of
2966+
10px, whereas the inner span uses 15px.
2967+
2968+
</div>
2969+
2970+
<div class="example">
2971+
We may override the metrics of a local fallback font to match the primary font, which
2972+
is a web font. This reduces layout shifting when switching from fallback to the
2973+
primary font.
2974+
2975+
<pre>
2976+
@font-face {
2977+
font-family: cool-web-font;
2978+
src: url("https://example.com/font.woff");
2979+
}
2980+
2981+
@font-face {
2982+
font-family: fallback-to-local;
2983+
src: local(Some Local Font);
2984+
/* Override metric values to match cool-web-font */
2985+
ascent-override: 125%;
2986+
descent-override: 25%;
2987+
line-gap-override: 0%;
2988+
}
2989+
2990+
&lt;div style="font-family: cool-web-font, fallback-to-local"&gt;Title goes here&lt;/div&gt;
2991+
&lt;img src="https://example.com/largeimage" alt="A large image that you don't want to shift"&gt;
2992+
</pre>
2993+
2994+
The image will not be vertically shifted when the user agent finishes loading and
2995+
switches to use the web font.
2996+
2997+
</div>
2998+
29112999
<h2 id="font-matching-algorithm">Font Matching Algorithm</h2>
29123000

29133001
The algorithm below describes how fonts are associated with individual runs of text.

0 commit comments

Comments
 (0)