glyphhanger-core 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,12 +32,12 @@ let chars = new CharacterSet("Hello world").union(new CharacterSet("—"));
32
32
  let range = chars.toHexRangeString(); // "U+20,U+48,U+64,U+65,U+6C,U+6F,U+72,U+77,U+2014"
33
33
 
34
34
  let subset = new Subsetter();
35
- subset.setFontFiles(["fonts/LatoLatin-Regular.ttf", "fonts/LatoLatin-Italic.ttf"]);
36
- // or subset.setFontFilesGlob("fonts/*.ttf")
35
+ subset.setInput(["fonts/LatoLatin-Regular.ttf", "fonts/LatoLatin-Italic.ttf"]);
36
+ // or subset.setInputFilesGlob("fonts/*.ttf")
37
37
  subset.setFormats("woff2,woff");
38
38
  subset.setOutputDirectory("dist/");
39
39
  subset.setAxes("wght"); // keep only these variable font axes, or "none" for a static font
40
- subset.subsetAll(range);
40
+ subset.write(range);
41
41
  ```
42
42
 
43
43
  ### Subset a font from Google Fonts
@@ -55,13 +55,29 @@ let buffer = await Fetch("https://raw.githubusercontent.com/google/fonts/main/of
55
55
  });
56
56
 
57
57
  let subset = new Subsetter();
58
- subset.setFontFiles([{ name: "Lato-Regular", buffer }]);
58
+ subset.setInput([{ name: "Lato-Regular", buffer }]);
59
59
  subset.setFormats("woff2");
60
60
  subset.setOutputDirectory("fonts/");
61
- subset.subsetAll(new CharacterSet("Hello world").toHexRangeString());
61
+ subset.write(new CharacterSet("Hello world").toHexRangeString());
62
62
  // Subsetting Lato to Lato-subset.woff2 (was 656.57 kB, now 3.94 kB)
63
63
  ```
64
64
 
65
+ ### Subset without writing files
66
+
67
+ `toBuffer()` returns the files `write()` would create, without writing them.
68
+
69
+ ```js
70
+ import CharacterSet from "characterset";
71
+ import { Subsetter } from "glyphhanger-core";
72
+
73
+ let subsetter = new Subsetter();
74
+ subsetter.setInput(["fonts/Lato-Regular.ttf"]);
75
+ subsetter.setFormats("woff2");
76
+
77
+ subsetter.toBuffer(new CharacterSet("ABC").toHexRangeString());
78
+ // [{ name: "fonts/Lato-Regular.ttf", format: "woff2", filename: "Lato-Regular-subset.woff2", buffer: <Buffer …> }]
79
+ ```
80
+
65
81
  ### `@font-face` CSS toString
66
82
 
67
83
  ```js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glyphhanger-core",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Webfont subsetting, format conversion and @font-face output for glyphhanger (without a browser).",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/src/Subsetter.js CHANGED
@@ -60,23 +60,33 @@ export class Subsetter {
60
60
  return this.outputDirectory;
61
61
  }
62
62
 
63
- setFontFilesGlob( ttfFilesGlob ) {
63
+ setInputFilesGlob( ttfFilesGlob ) {
64
64
  this.fontPaths = fs.globSync( ttfFilesGlob );
65
65
  if( !this.fontPaths.length ) {
66
66
  console.log( styleText("yellow", `Warning: --subset=${ttfFilesGlob} matched no font files.` ) );
67
67
  }
68
68
  }
69
69
 
70
+ // Alias for the pre-split name.
71
+ setFontFilesGlob( ttfFilesGlob ) {
72
+ this.setInputFilesGlob( ttfFilesGlob );
73
+ }
74
+
70
75
  // Each entry is a file path or a `{ name, buffer }` object; `name` (a filename or family name like `Lato`) sets the output filenames.
71
- setFontFiles( ttfFontFiles ) {
76
+ setInput( ttfFontFiles ) {
72
77
  for( let font of ttfFontFiles ) {
73
78
  if( typeof font !== "string" && !( typeof font?.name === "string" && font.buffer instanceof Uint8Array ) ) {
74
- throw new Error( "setFontFiles entries must be file paths or { name, buffer } objects." );
79
+ throw new Error( "setInput entries must be file paths or { name, buffer } objects." );
75
80
  }
76
81
  }
77
82
  this.fontPaths = ttfFontFiles;
78
83
  }
79
84
 
85
+ // Alias for the pre-split name.
86
+ setFontFiles( ttfFontFiles ) {
87
+ this.setInput( ttfFontFiles );
88
+ }
89
+
80
90
  getFontName( font ) {
81
91
  return typeof font === "string" ? font : font.name;
82
92
  }
@@ -149,7 +159,7 @@ export class Subsetter {
149
159
  return outputFilename;
150
160
  }
151
161
 
152
- subsetAll( unicodes ) {
162
+ write( unicodes ) {
153
163
  for( let fontPath of this.fontPaths ) {
154
164
  let sfnt = this.subsetFont( fontPath, unicodes );
155
165
  for( let format of ["ttf", "woff", "woff2"] ) {
@@ -160,10 +170,45 @@ export class Subsetter {
160
170
  }
161
171
  }
162
172
 
173
+ // Returns the files write() would create, in memory: `[{ name, format, filename, buffer }]` for each input and format.
174
+ toBuffer( unicodes ) {
175
+ let results = [];
176
+ for( let font of this.fontPaths ) {
177
+ let sfnt = this.subsetFont( font, unicodes );
178
+ for( let format of ["ttf", "woff", "woff2"] ) {
179
+ if( this.formats.hasFormat( format ) ) {
180
+ results.push({
181
+ name: this.getFontName( font ),
182
+ format,
183
+ filename: this.getFilenameFromTTFPath( font, format === "ttf" ? undefined : format ),
184
+ buffer: this.encode( sfnt, format ),
185
+ });
186
+ }
187
+ }
188
+ }
189
+ return results;
190
+ }
191
+
192
+ // Alias for the pre-split name.
193
+ subsetAll( unicodes ) {
194
+ return this.write( unicodes );
195
+ }
196
+
197
+ // Subsets one font (a path or `{ name, buffer }`) to uncompressed TTF/OTF data.
163
198
  subsetFont( inputFile, unicodes ) {
164
199
  return this.limitAxes( inputFile, this.subsetWithEngine( inputFile, unicodes ) );
165
200
  }
166
201
 
202
+ encode( sfnt, format ) {
203
+ if( format === "woff" ) {
204
+ return toWoff( sfnt );
205
+ }
206
+ if( format === "woff2" ) {
207
+ return toWoff2( sfnt );
208
+ }
209
+ return sfnt;
210
+ }
211
+
167
212
  limitAxes( inputFile, sfnt ) {
168
213
  let name = this.getFontName( inputFile );
169
214
  let keep = this.getAxes();
@@ -243,12 +288,7 @@ export class Subsetter {
243
288
  fs.mkdirSync( outputDir, { recursive: true } );
244
289
  }
245
290
 
246
- let output = sfnt;
247
- if( format === "woff" ) {
248
- output = toWoff( sfnt );
249
- } else if( format === "woff2" ) {
250
- output = toWoff2( sfnt );
251
- }
291
+ let output = this.encode( sfnt, format );
252
292
  fs.writeFileSync( outputFullPath, output );
253
293
 
254
294
  if( !unicodes ) {