@@ -45,22 +45,29 @@ export default class DynamicFonts {
45
45
private readonly fontDownloader : InstanceType < typeof FontDownloader > ;
46
46
47
47
constructor ( props : DynamicFontsProps ) {
48
- const { debug} = props ;
48
+ const { debug = __DEV__ } = props ;
49
49
this . props = { fontLoadErrorMessage : DEFAULT_FONT_LOAD_ERROR_MESSAGE , ...props } ;
50
50
this . permissionsAcquirer = new PermissionsAcquirer ( this . props . permissionsAcquirerProps ?? { } ) ;
51
51
this . fontLoader = new FontLoader ( { debug} ) ;
52
52
const fontDownloadingProps = this . props . fontDownloadingProps ?? { } ;
53
53
this . fontDownloader = new FontDownloader ( { ...fontDownloadingProps , debug} ) ;
54
54
}
55
55
56
+ private log ( message ?: any , ...optionalParams : any [ ] ) {
57
+ const { debug} = this . props ;
58
+ if ( debug ) {
59
+ console . log ( message , optionalParams ) ;
60
+ }
61
+ }
62
+
56
63
private async loadFont ( input : LoadFontInput ) {
57
64
const { fontLoadErrorMessage} = this . props ;
58
65
try {
59
66
return await this . fontLoader . loadFont ( input ) ;
60
67
} catch ( err ) {
61
68
return Promise . reject ( {
62
69
source : 'uilib:FontDownloader:loadFont' ,
63
- message : `${ fontLoadErrorMessage } fontName: ${ input . fontName } `
70
+ message : `${ fontLoadErrorMessage } fontName: ${ input . fontName } error: ${ JSON . stringify ( err ) } `
64
71
} ) ;
65
72
}
66
73
}
@@ -74,32 +81,19 @@ export default class DynamicFonts {
74
81
* @param timeout milliseconds for the download to complete in (defaults to 5000)
75
82
*/
76
83
public async getFont ( { fontUri, fontName, fontExtension, timeout = 5000 } : GetFontInput ) : Promise < string > {
77
- const { debug} = this . props ;
78
84
const { fontLoadErrorMessage} = this . props ;
79
85
await this . permissionsAcquirer . getPermissions ( ) ;
80
86
if ( await this . fontDownloader . isFontDownloaded ( fontName , fontExtension ) ) {
81
- if ( debug ) {
82
- console . log ( fontName , 'Already downloaded' ) ;
83
- }
87
+ this . log ( fontName , 'Already downloaded' ) ;
84
88
} else {
85
- if ( debug ) {
86
- console . log ( fontName , 'Starting to download' ) ;
87
- }
88
89
await this . fontDownloader . downloadFont ( fontUri , fontName , fontExtension , timeout ) ;
89
- if ( debug ) {
90
- console . log ( fontName , 'Finished downloading' ) ;
91
- }
92
90
}
93
91
94
92
const base64FontString = await this . fontDownloader . readFontFromDisk ( fontName , fontExtension ) ;
95
93
if ( base64FontString ) {
96
- if ( debug ) {
97
- console . log ( fontName , 'Loading' ) ;
98
- }
94
+ this . log ( fontName , 'Loading' ) ;
99
95
const _fontName = await this . loadFont ( { fontName, base64FontString, fontExtension} ) ;
100
- if ( debug ) {
101
- console . log ( _fontName , 'Finished loading' ) ;
102
- }
96
+ this . log ( _fontName , 'Finished loading' ) ;
103
97
return Promise . resolve ( _fontName ) ;
104
98
} else {
105
99
return Promise . reject ( {
@@ -118,13 +112,13 @@ export default class DynamicFonts {
118
112
}
119
113
}
120
114
121
- private buildFontName ( rootUri : string ,
115
+ private buildFontData ( rootUri : string ,
122
116
fontName : string ,
123
117
fontExtension : FontExtension ,
124
- fontNamePrefix ?: string ) : GetFontInput {
118
+ fontNamePrefix ?: string ) : GetFontInput & { fullFontName : string } {
125
119
const _fontName = `${ fontNamePrefix ?? '' } ${ fontName } ` ;
126
120
const fullFontName = `${ _fontName } .${ fontExtension } ` ;
127
- return { fontUri : `${ rootUri } ${ fullFontName } ` , fontName : _fontName , fontExtension} ;
121
+ return { fontUri : `${ rootUri } ${ fullFontName } ` , fontName : _fontName , fontExtension, fullFontName } ;
128
122
}
129
123
130
124
// eslint-disable-next-line max-params
@@ -133,9 +127,8 @@ export default class DynamicFonts {
133
127
fontExtension : FontExtension ,
134
128
fontNamePrefix ?: string ,
135
129
retries = 1 ) : Promise < string [ ] > {
136
- const { debug} = this . props ;
137
130
const fonts : GetFontInput [ ] = fontNames . map ( fontName =>
138
- this . buildFontName ( rootUri , fontName , fontExtension , fontNamePrefix ) ) ;
131
+ this . buildFontData ( rootUri , fontName , fontExtension , fontNamePrefix ) ) ;
139
132
let fontsLoaded : string [ ] = [ ] ;
140
133
let tryCounter = 0 ;
141
134
while ( fontsLoaded . length < fontNames . length && tryCounter < retries ) {
@@ -144,12 +137,29 @@ export default class DynamicFonts {
144
137
// TODO: we should return successful loaded fonts and not fail all of them
145
138
fontsLoaded = await this . getFonts ( fonts ) ;
146
139
} catch ( error ) {
147
- if ( debug ) {
148
- console . log ( `getFontFamily failed (try #${ tryCounter } ) error: ${ error } ` ) ;
149
- }
140
+ this . log ( `getFontFamily failed (try #${ tryCounter } ) error:` , error ) ;
150
141
}
151
142
}
152
143
153
144
return Promise . resolve ( fontsLoaded ) ;
154
145
}
146
+
147
+ private async deleteFontFromDisk ( fontName : string , fontExtension : FontExtension , fontNamePrefix ?: string ) {
148
+ const fontInput = this . buildFontData ( '' , fontName , fontExtension , fontNamePrefix ) ;
149
+ await this . fontDownloader . deleteFontFromDisk ( fontInput . fullFontName ) ;
150
+ }
151
+
152
+ public async deleteFont ( fontName : string , fontExtension : FontExtension ) : Promise < void > {
153
+ await this . permissionsAcquirer . getPermissions ( ) ;
154
+ await this . deleteFontFromDisk ( fontName , fontExtension ) ;
155
+ }
156
+
157
+ public async deleteFontFamily ( fontNames : string [ ] ,
158
+ fontExtension : FontExtension ,
159
+ fontNamePrefix ?: string ) : Promise < void > {
160
+ await this . permissionsAcquirer . getPermissions ( ) ;
161
+ fontNames . forEach ( async fontName => {
162
+ await this . deleteFontFromDisk ( fontName , fontExtension , fontNamePrefix ) ;
163
+ } ) ;
164
+ }
155
165
}
0 commit comments