@@ -24,12 +24,15 @@ interface Options {
2424 */
2525 inputDir : string | string [ ] ;
2626 /**
27- * Path to the output directory. All fonts awill be placed into this directory,
27+ * Path to the output directory. All fonts will be placed into this directory,
2828 * even if multiple directories are specified as inputs. Subdirectories will
29- * be retained from the input. If you want to output to multiple directories,
30- * you can use a second plugin instance with a different output directory.
29+ * be retained from the input.
3130 */
3231 outDir : string ;
32+ /**
33+ * If true, copies the original font files to the output directory. Defaults to false
34+ */
35+ copyOriginalToOutDir ?: boolean ;
3336 /**
3437 * Type of font to generate. Defaults to ['msdf']
3538 */
@@ -45,11 +48,16 @@ interface Options {
4548 */
4649 force ?: boolean ;
4750 /**
48- * Charset to include in the generated font. If there is a charset.config.json file in
49- * the input directory, that will be used instead. Defaults to
51+ * Charset to include in the generated font. If there is a charset.config.json
52+ * file in the input directory, or if the charsetFile option is set, they will
53+ * be used instead. Defaults to
5054 * ' !\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~’“”'
5155 */
5256 charset ?: string ;
57+ /**
58+ * Path to a charset file. If this is set, it will be used instead of the charset option.
59+ */
60+ charsetFile ?: string ;
5361
5462 /**
5563 * Overrides for font generation. This allows you to specify custom font sizes
@@ -59,56 +67,77 @@ interface Options {
5967 overrides ?: Override ;
6068}
6169
62- export default function msdfFontGen ( {
63- inputDir,
64- outDir,
65- force,
66- types = [ 'msdf' ] ,
67- extensions = [ 'ttf' , 'otf' , 'woff' , 'woff2' ] ,
68- charset = ' !\\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~’“”' ,
69- overrides,
70- } : Options ) : PluginOption {
70+ export default function msdfFontGen (
71+ options : Options | Options [ ] ,
72+ ) : PluginOption {
7173 return {
7274 name : 'msdf-fontgen' ,
7375
7476 async buildStart ( ) {
7577 console . log ( 'Building fonts...' ) ;
7678
7779 const cleanup : string [ ] = [ ] ;
80+ const optionsArray = Array . isArray ( options ) ? options : [ options ] ;
81+
82+ for ( const option of optionsArray ) {
83+ const {
84+ inputDir,
85+ outDir,
86+ force,
87+ types = [ 'msdf' ] ,
88+ extensions = [ 'ttf' , 'otf' , 'woff' , 'woff2' ] ,
89+ charset = ' !\\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~’“”' ,
90+ charsetFile,
91+ overrides,
92+ copyOriginalToOutDir,
93+ } = option ;
94+
95+ if ( types . length === 0 ) {
96+ console . log ( 'No font types specified' ) ;
97+ return ;
98+ }
7899
79- if ( types . length === 0 ) {
80- console . log ( 'No font types specified' ) ;
81- return ;
82- }
100+ console . log ( 'Looking for fonts...' ) ;
83101
84- console . log ( 'Looking for fonts...' ) ;
102+ const inputDirs = Array . isArray ( inputDir ) ? inputDir : [ inputDir ] ;
85103
86- if ( ! Array . isArray ( inputDir ) ) {
87- inputDir = [ inputDir ] ;
88- }
104+ const fontFolders = getFolders ( inputDirs , outDir , extensions ) ;
89105
90- const fontFolders = getFolders ( inputDir , outDir , extensions ) ;
106+ for ( const { input, output, files } of fontFolders ) {
107+ console . log ( `Generating fonts in folder ${ input } ...` ) ;
91108
92- for ( const { input, output, files } of fontFolders ) {
93- console . log ( `Generating fonts in folder ${ input } ...` ) ;
109+ setGeneratePaths ( input , output , charsetFile ) ;
94110
95- setGeneratePaths ( input , output ) ;
111+ if ( ! charsetFile || ! fs . existsSync ( charsetFile ) ) {
112+ const charsetPath = `${ input } /charset.config.json` ;
96113
97- const charsetPath = `${ input } /charset.config.json` ;
98- if ( ! fs . existsSync ( charsetPath ) && charset ) {
99- fs . writeFileSync ( charsetPath , JSON . stringify ( { charset } ) , 'utf8' ) ;
100- cleanup . push ( charsetPath ) ;
101- }
114+ if ( ! fs . existsSync ( charsetPath ) && charset ) {
115+ fs . writeFileSync (
116+ charsetPath ,
117+ JSON . stringify ( { charset } ) ,
118+ 'utf8' ,
119+ ) ;
120+ cleanup . push ( charsetPath ) ;
121+ }
122+ }
102123
103- const overridePath = `${ input } /overrides.txt` ;
104- if ( ! fs . existsSync ( overridePath ) && overrides ) {
105- fs . writeFileSync ( overridePath , JSON . stringify ( overrides , null , 2 ) ) ;
106- cleanup . push ( overridePath ) ;
107- }
124+ const overridePath = `${ input } /overrides.txt` ;
125+ if ( ! fs . existsSync ( overridePath ) && overrides ) {
126+ fs . writeFileSync ( overridePath , JSON . stringify ( overrides , null , 2 ) ) ;
127+ cleanup . push ( overridePath ) ;
128+ }
108129
109- for ( const file of files ) {
110- for ( const type of new Set ( types ) ) {
111- await generateFont ( output , file , type , force ) ;
130+ for ( const file of files ) {
131+ for ( const type of new Set ( types ) ) {
132+ await generateFont ( output , file , type , force ) ;
133+
134+ if ( copyOriginalToOutDir ) {
135+ fs . copyFileSync (
136+ path . join ( input , file ) ,
137+ path . join ( output , file ) ,
138+ ) ;
139+ }
140+ }
112141 }
113142 }
114143 }
0 commit comments