1
1
import type { AtRule } from 'csstype' ;
2
2
import { round } from './round' ;
3
- import type { FontMetrics } from './types' ;
3
+ import type { FontMetrics , SupportedSubset } from './types' ;
4
4
5
5
const toPercentString = ( value : number ) => `${ round ( value * 100 ) } %` ;
6
6
@@ -9,21 +9,48 @@ export const toCssProperty = (property: string) =>
9
9
10
10
type FontStackMetrics = Pick <
11
11
FontMetrics ,
12
- 'familyName' | 'ascent' | 'descent' | 'lineGap' | 'unitsPerEm' | 'xWidthAvg'
12
+ | 'familyName'
13
+ | 'ascent'
14
+ | 'descent'
15
+ | 'lineGap'
16
+ | 'unitsPerEm'
17
+ | 'xWidthAvg'
18
+ | 'subsets'
13
19
> ;
14
20
21
+ // Support old metrics pre-`subsets` alongside the newer core package with `subset` support.
22
+ const resolveXWidthAvg = (
23
+ metrics : FontStackMetrics ,
24
+ subset : SupportedSubset ,
25
+ ) => {
26
+ if ( metrics ?. subsets ?. [ subset ] ) {
27
+ return metrics . subsets [ subset ] . xWidthAvg ;
28
+ }
29
+
30
+ if ( subset !== 'latin' ) {
31
+ throw new Error (
32
+ `The subset "${ subset } " is not available in the metrics provided for "${ metrics . familyName } "` ,
33
+ ) ;
34
+ }
35
+
36
+ return metrics . xWidthAvg ;
37
+ } ;
38
+
15
39
interface OverrideValuesParams {
16
40
metrics : FontStackMetrics ;
17
41
fallbackMetrics : FontStackMetrics ;
42
+ subset : SupportedSubset ;
18
43
}
19
44
const calculateOverrideValues = ( {
20
45
metrics,
21
46
fallbackMetrics,
47
+ subset,
22
48
} : OverrideValuesParams ) : AtRule . FontFace => {
23
49
// Calculate size adjust
24
- const preferredFontXAvgRatio = metrics . xWidthAvg / metrics . unitsPerEm ;
50
+ const preferredFontXAvgRatio =
51
+ resolveXWidthAvg ( metrics , subset ) / metrics . unitsPerEm ;
25
52
const fallbackFontXAvgRatio =
26
- fallbackMetrics . xWidthAvg / fallbackMetrics . unitsPerEm ;
53
+ resolveXWidthAvg ( fallbackMetrics , subset ) / fallbackMetrics . unitsPerEm ;
27
54
28
55
const sizeAdjust =
29
56
preferredFontXAvgRatio && fallbackFontXAvgRatio
@@ -131,6 +158,12 @@ type CreateFontStackOptions = {
131
158
* support explicit overrides.
132
159
*/
133
160
fontFaceProperties ?: AdditionalFontFaceProperties ;
161
+ /**
162
+ * The unicode subset to use for calculating the `size-adjust` property.
163
+ *
164
+ * Default: `latin`
165
+ */
166
+ subset ?: SupportedSubset ;
134
167
} ;
135
168
type FontFaceFormatString = {
136
169
/**
@@ -145,6 +178,18 @@ type FontFaceFormatObject = {
145
178
fontFaceFormat ?: 'styleObject' ;
146
179
} ;
147
180
181
+ const resolveOptions = ( options : Parameters < typeof createFontStack > [ 1 ] ) => {
182
+ const fontFaceFormat = options ?. fontFaceFormat ?? 'styleString' ;
183
+ const subset = options ?. subset ?? 'latin' ;
184
+ const fontFaceProperties = options ?. fontFaceProperties ?? { } ;
185
+
186
+ return {
187
+ fontFaceFormat,
188
+ subset,
189
+ fontFaceProperties,
190
+ } as const ;
191
+ } ;
192
+
148
193
export function createFontStack (
149
194
fontStackMetrics : FontStackMetrics [ ] ,
150
195
options ?: CreateFontStackOptions & FontFaceFormatString ,
@@ -157,10 +202,8 @@ export function createFontStack(
157
202
[ metrics , ...fallbackMetrics ] : FontStackMetrics [ ] ,
158
203
optionsArg : CreateFontStackOptions = { } ,
159
204
) {
160
- const { fontFaceFormat, fontFaceProperties } = {
161
- fontFaceFormat : 'styleString' ,
162
- ...optionsArg ,
163
- } ;
205
+ const { fontFaceFormat, fontFaceProperties, subset } =
206
+ resolveOptions ( optionsArg ) ;
164
207
const { familyName } = metrics ;
165
208
166
209
const fontFamilies : string [ ] = [ quoteIfNeeded ( familyName ) ] ;
@@ -182,6 +225,7 @@ export function createFontStack(
182
225
...calculateOverrideValues ( {
183
226
metrics,
184
227
fallbackMetrics : fallback ,
228
+ subset,
185
229
} ) ,
186
230
...( fontFaceProperties ?. sizeAdjust
187
231
? { sizeAdjust : fontFaceProperties . sizeAdjust }
0 commit comments