7
7
FontSourceOptions ,
8
8
FontStyle ,
9
9
FontWeight ,
10
+ RemoteOptions ,
11
+ SingleLoad ,
10
12
} from './types' ;
11
13
12
14
const FONT_WEIGHTS = {
@@ -26,7 +28,7 @@ const FONT_WEIGHTS = {
26
28
black : 900 ,
27
29
} ;
28
30
29
- const fetchFont = async ( src : string , options ) => {
31
+ const fetchFont = async ( src : string , options : RemoteOptions ) => {
30
32
const response = await fetch ( src , options ) ;
31
33
const data = await response . arrayBuffer ( ) ;
32
34
@@ -60,17 +62,17 @@ class FontSource {
60
62
constructor (
61
63
src : string ,
62
64
fontFamily : string ,
63
- fontStyle : FontStyle ,
64
- fontWeight : number ,
65
- options : FontSourceOptions ,
65
+ fontStyle ? : FontStyle ,
66
+ fontWeight ? : number ,
67
+ options ? : FontSourceOptions ,
66
68
) {
67
69
this . src = src ;
68
70
this . fontFamily = fontFamily ;
69
71
this . fontStyle = fontStyle || 'normal' ;
70
72
this . fontWeight = fontWeight || 400 ;
71
73
72
74
this . data = null ;
73
- this . options = options ;
75
+ this . options = options || { } ;
74
76
this . loadResultPromise = null ;
75
77
}
76
78
@@ -115,8 +117,15 @@ class Font {
115
117
this . sources = [ ] ;
116
118
}
117
119
118
- register ( { src, fontWeight, fontStyle, ...options } ) {
119
- const numericFontWeight = resolveFontWeight ( fontWeight ) ;
120
+ register ( {
121
+ src,
122
+ fontWeight,
123
+ fontStyle,
124
+ ...options
125
+ } : Omit < SingleLoad , 'family' > ) {
126
+ const numericFontWeight = fontWeight
127
+ ? resolveFontWeight ( fontWeight )
128
+ : undefined ;
120
129
121
130
this . sources . push (
122
131
new FontSource ( src , this . family , fontStyle , numericFontWeight , options ) ,
@@ -133,7 +142,7 @@ class Font {
133
142
134
143
// Weight resolution. https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Fallback_weights
135
144
136
- let res : FontSource ;
145
+ let font : FontSource | null = null ;
137
146
138
147
const numericFontWeight = resolveFontWeight ( fontWeight ) ;
139
148
@@ -148,7 +157,7 @@ class Font {
148
157
( s ) => s . fontWeight >= numericFontWeight && s . fontWeight < 500 ,
149
158
) ;
150
159
151
- res = fit [ 0 ] || leftOffset [ leftOffset . length - 1 ] || rightOffset [ 0 ] ;
160
+ font = fit [ 0 ] || leftOffset [ leftOffset . length - 1 ] || rightOffset [ 0 ] ;
152
161
}
153
162
154
163
const lt = styleSources
@@ -159,20 +168,20 @@ class Font {
159
168
. sort ( sortByFontWeight ) ;
160
169
161
170
if ( numericFontWeight < 400 ) {
162
- res = lt [ lt . length - 1 ] || gt [ 0 ] ;
171
+ font = lt [ lt . length - 1 ] || gt [ 0 ] ;
163
172
}
164
173
165
174
if ( numericFontWeight > 500 ) {
166
- res = gt [ 0 ] || lt [ lt . length - 1 ] ;
175
+ font = gt [ 0 ] || lt [ lt . length - 1 ] ;
167
176
}
168
177
169
- if ( ! res ) {
178
+ if ( ! font ) {
170
179
throw new Error (
171
180
`Could not resolve font for ${ this . family } , fontWeight ${ fontWeight } , fontStyle ${ fontStyle } ` ,
172
181
) ;
173
182
}
174
183
175
- return res ;
184
+ return font ;
176
185
}
177
186
}
178
187
0 commit comments