1
- /* eslint-disable max-classes-per-file */
2
-
3
- import isUrl from 'is-url' ;
4
- import * as fontkit from 'fontkit' ;
5
- import {
6
- FontDescriptor ,
7
- FontSourceOptions ,
8
- FontStyle ,
9
- FontWeight ,
10
- RemoteOptions ,
11
- SingleLoad ,
12
- } from './types' ;
1
+ import FontSource from './font-source' ;
2
+ import { FontDescriptor , FontWeight , SingleLoad } from './types' ;
13
3
14
4
const FONT_WEIGHTS = {
15
5
thin : 100 ,
@@ -28,88 +18,19 @@ const FONT_WEIGHTS = {
28
18
black : 900 ,
29
19
} ;
30
20
31
- const fetchFont = async ( src : string , options : RemoteOptions ) => {
32
- const response = await fetch ( src , options ) ;
33
- const data = await response . arrayBuffer ( ) ;
34
-
35
- return new Uint8Array ( data ) ;
36
- } ;
37
-
38
- const isDataUrl = ( dataUrl : string ) => {
39
- const header = dataUrl . split ( ',' ) [ 0 ] ;
40
- const hasDataPrefix = header . substring ( 0 , 5 ) === 'data:' ;
41
- const hasBase64Prefix = header . split ( ';' ) [ 1 ] === 'base64' ;
42
-
43
- return hasDataPrefix && hasBase64Prefix ;
44
- } ;
45
-
46
21
const resolveFontWeight = ( value : FontWeight ) => {
47
22
return typeof value === 'string' ? FONT_WEIGHTS [ value ] : value ;
48
23
} ;
49
24
50
25
const sortByFontWeight = ( a : FontSource , b : FontSource ) =>
51
26
a . fontWeight - b . fontWeight ;
52
27
53
- class FontSource {
54
- src : string ;
55
- fontFamily : string ;
56
- fontStyle : FontStyle ;
57
- fontWeight : number ;
58
- data : fontkit . Font | fontkit . FontCollection | null ;
59
- options : FontSourceOptions ;
60
- loadResultPromise : Promise < void > | null ;
61
-
62
- constructor (
63
- src : string ,
64
- fontFamily : string ,
65
- fontStyle ?: FontStyle ,
66
- fontWeight ?: number ,
67
- options ?: FontSourceOptions ,
68
- ) {
69
- this . src = src ;
70
- this . fontFamily = fontFamily ;
71
- this . fontStyle = fontStyle || 'normal' ;
72
- this . fontWeight = fontWeight || 400 ;
73
-
74
- this . data = null ;
75
- this . options = options || { } ;
76
- this . loadResultPromise = null ;
77
- }
78
-
79
- async _load ( ) : Promise < void > {
80
- const { postscriptName } = this . options ;
81
-
82
- if ( isDataUrl ( this . src ) ) {
83
- const raw = this . src . split ( ',' ) [ 1 ] ;
84
- const uint8Array = new Uint8Array (
85
- atob ( raw )
86
- . split ( '' )
87
- . map ( ( c ) => c . charCodeAt ( 0 ) ) ,
88
- ) ;
89
- this . data = fontkit . create ( uint8Array , postscriptName ) ;
90
- } else if ( BROWSER || isUrl ( this . src ) ) {
91
- const { headers, body, method = 'GET' } = this . options ;
92
- const data = await fetchFont ( this . src , { method, body, headers } ) ;
93
- this . data = fontkit . create ( data , postscriptName ) ;
94
- } else if ( ! BROWSER ) {
95
- this . data = await fontkit . open ( this . src , postscriptName ) ;
96
- }
97
- }
98
-
99
- async load ( ) {
100
- if ( this . loadResultPromise === null ) {
101
- this . loadResultPromise = this . _load ( ) ;
102
- }
103
- return this . loadResultPromise ;
104
- }
105
- }
106
-
107
- class Font {
28
+ class FontFamily {
108
29
family : string ;
109
30
sources : FontSource [ ] ;
110
31
111
32
static create ( family : string ) {
112
- return new Font ( family ) ;
33
+ return new FontFamily ( family ) ;
113
34
}
114
35
115
36
constructor ( family : string ) {
@@ -163,6 +84,7 @@ class Font {
163
84
const lt = styleSources
164
85
. filter ( ( s ) => s . fontWeight < numericFontWeight )
165
86
. sort ( sortByFontWeight ) ;
87
+
166
88
const gt = styleSources
167
89
. filter ( ( s ) => s . fontWeight > numericFontWeight )
168
90
. sort ( sortByFontWeight ) ;
@@ -185,4 +107,4 @@ class Font {
185
107
}
186
108
}
187
109
188
- export default Font ;
110
+ export default FontFamily ;
0 commit comments