glyphhanger-core 0.0.0-stage → 1.0.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/LICENSE +22 -0
- package/README.md +97 -2
- package/index.js +3 -0
- package/package.json +33 -4
- package/src/Allowlist.js +61 -0
- package/src/FontFaceCss.js +120 -0
- package/src/Format.js +14 -0
- package/src/HarfBuzzSubset.js +93 -0
- package/src/Subsetter.js +260 -0
- package/src/Whitelist.js +2 -0
- package/src/webfont/Sfnt.js +230 -0
- package/src/webfont/WebFontWrapper.js +17 -0
- package/src/webfont/Woff.js +60 -0
- package/src/webfont/Woff2.js +125 -0
- package/src/webfont/Woff2Transforms.js +372 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zach Leatherman
|
|
4
|
+
Copyright (c) 2017 Filament Group
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,98 @@
|
|
|
1
|
-
#
|
|
1
|
+
# glyphhanger-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The browser-free pieces of [glyphhanger](https://www.npmjs.com/package/glyphhanger): web font subsetting, TTF/OTF/WOFF/WOFF2 conversion and `@font-face` output.
|
|
4
|
+
|
|
5
|
+
To find the glyphs used on a web page, use the [`glyphhanger`](https://www.npmjs.com/package/glyphhanger) CLI.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install glyphhanger-core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Optional: `pyftsubset`
|
|
14
|
+
|
|
15
|
+
Subsetting works out of the box using [HarfBuzz](https://github.com/harfbuzz/harfbuzzjs) (WebAssembly). If [`pyftsubset`](https://github.com/fonttools/fonttools) is installed, it’s used instead (use `setEngine("harfbuzz")` or `setEngine("pyftsubset")` to choose one).
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
brew install fonttools
|
|
19
|
+
# or
|
|
20
|
+
pip3 install fonttools
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
Build the characters to keep with [`characterset`](https://www.npmjs.com/package/characterset) (`npm install characterset`).
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import CharacterSet from "characterset";
|
|
29
|
+
import { Subsetter } from "glyphhanger-core";
|
|
30
|
+
|
|
31
|
+
let chars = new CharacterSet("Hello world").union(new CharacterSet("—"));
|
|
32
|
+
let range = chars.toHexRangeString(); // "U+20,U+48,U+64,U+65,U+6C,U+6F,U+72,U+77,U+2014"
|
|
33
|
+
|
|
34
|
+
let subset = new Subsetter();
|
|
35
|
+
subset.setFontFiles(["fonts/LatoLatin-Regular.ttf", "fonts/LatoLatin-Italic.ttf"]);
|
|
36
|
+
// or subset.setFontFilesGlob("fonts/*.ttf")
|
|
37
|
+
subset.setFormats("woff2,woff");
|
|
38
|
+
subset.setOutputDirectory("dist/");
|
|
39
|
+
subset.setAxes("wght"); // keep only these variable font axes, or "none" for a static font
|
|
40
|
+
subset.subsetAll(range);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Subset a font from Google Fonts
|
|
44
|
+
|
|
45
|
+
Download a raw source font from [`google/fonts`](https://github.com/google/fonts) with [`@11ty/eleventy-fetch`](https://www.11ty.dev/docs/plugins/fetch/) (cached for a day).
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import Fetch from "@11ty/eleventy-fetch";
|
|
49
|
+
import CharacterSet from "characterset";
|
|
50
|
+
import { Subsetter } from "glyphhanger-core";
|
|
51
|
+
|
|
52
|
+
let buffer = await Fetch("https://raw.githubusercontent.com/google/fonts/main/ofl/lato/Lato-Regular.ttf", {
|
|
53
|
+
duration: "1d",
|
|
54
|
+
type: "buffer",
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
let subset = new Subsetter();
|
|
58
|
+
subset.setFontFiles([{ name: "Lato-Regular", buffer }]);
|
|
59
|
+
subset.setFormats("woff2");
|
|
60
|
+
subset.setOutputDirectory("fonts/");
|
|
61
|
+
subset.subsetAll(new CharacterSet("Hello world").toHexRangeString());
|
|
62
|
+
// Subsetting Lato to Lato-subset.woff2 (was 656.57 kB, now 3.94 kB)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### `@font-face` CSS toString
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
import CharacterSet from "characterset";
|
|
69
|
+
import { FontFaceCss, Subsetter } from "glyphhanger-core";
|
|
70
|
+
|
|
71
|
+
let fontface = new FontFaceCss();
|
|
72
|
+
fontface.setFamilies("My Family");
|
|
73
|
+
fontface.setUnicodeRange(new CharacterSet("ABCD").toHexRangeString());
|
|
74
|
+
fontface.toString();
|
|
75
|
+
// @font-face {
|
|
76
|
+
// font-family: My Family;
|
|
77
|
+
// unicode-range: U+41-44;
|
|
78
|
+
// }
|
|
79
|
+
|
|
80
|
+
// Pass a font path to add `src` and variable font axis descriptors (the font is read, not written).
|
|
81
|
+
let subset = new Subsetter();
|
|
82
|
+
subset.setFormats("woff2,woff");
|
|
83
|
+
subset.setAxes("wght");
|
|
84
|
+
fontface.setSubset(subset);
|
|
85
|
+
fontface.toString("fonts/Fredoka-Variable.ttf");
|
|
86
|
+
// @font-face {
|
|
87
|
+
// font-family: My Family;
|
|
88
|
+
// src: url(Fredoka-Variable-subset.woff2) format("woff2"), url(Fredoka-Variable-subset.woff) format("woff");
|
|
89
|
+
// font-weight: 300 700;
|
|
90
|
+
// unicode-range: U+41-44;
|
|
91
|
+
// }
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Debug Mode
|
|
95
|
+
|
|
96
|
+
```sh
|
|
97
|
+
NODE_DEBUG=glyphhanger* node my-script.js
|
|
98
|
+
```
|
package/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "glyphhanger-core",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Webfont subsetting, format conversion and @font-face output for glyphhanger (without a browser).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"src/"
|
|
10
|
+
],
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": "^22.22.2 || ^24.15.0 || >=26.0.0"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "node --test test/*.js"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/zachleat/glyphhanger.git",
|
|
20
|
+
"directory": "packages/core"
|
|
21
|
+
},
|
|
22
|
+
"author": "Zach Leatherman (https://zachleat.com/)",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/zachleat/glyphhanger/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/zachleat/glyphhanger#readme",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"characterset": "^2.0.0",
|
|
30
|
+
"harfbuzzjs": "^1.6.4"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"wawoff2": "^2.0.1"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/Allowlist.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import CharacterSet from "characterset";
|
|
2
|
+
|
|
3
|
+
export class Allowlist {
|
|
4
|
+
|
|
5
|
+
constructor( chars, presets ) {
|
|
6
|
+
presets = presets || {};
|
|
7
|
+
|
|
8
|
+
var cs = new CharacterSet();
|
|
9
|
+
|
|
10
|
+
if( presets.US_ASCII ) {
|
|
11
|
+
cs = cs.union( CharacterSet.parseUnicodeRange("U+20-7E") );
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if( presets.LATIN ) {
|
|
15
|
+
cs = cs.union( CharacterSet.parseUnicodeRange("U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+2000-206F,U+2074,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD" ));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if( typeof chars === "string" ) {
|
|
19
|
+
if( chars && chars.match( Allowlist.unicodeCodePointsRegex ) ) {
|
|
20
|
+
cs = cs.union( CharacterSet.parseUnicodeRange( chars ) );
|
|
21
|
+
} else if( chars ) {
|
|
22
|
+
cs = cs.union( new CharacterSet( chars ) );
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
this.allowlist = cs;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static get unicodeCodePointsRegex() {
|
|
30
|
+
return /(U\+[\dABCDEF]+\-?[\dABCDEF]*),?/gi
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
isEmpty() {
|
|
34
|
+
return this.allowlist.isEmpty();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getUniversalRangeAsUnicodes() {
|
|
38
|
+
return CharacterSet.parseUnicodeRange( "U+0-10FFFF" ).toHexRangeString();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
getCharacterSet() {
|
|
42
|
+
return this.allowlist;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getAllowlist() {
|
|
46
|
+
return this.allowlist.toString();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Aliases for the pre-7.1 names.
|
|
50
|
+
getWhitelist() {
|
|
51
|
+
return this.getAllowlist();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getWhitelistAsUnicodes() {
|
|
55
|
+
return this.getAllowlistAsUnicodes();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
getAllowlistAsUnicodes() {
|
|
59
|
+
return this.allowlist.toHexRangeString();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import pkg from "../package.json" with { type: "json" };
|
|
4
|
+
import { readVariationAxes, toSfnt } from "./webfont/WebFontWrapper.js";
|
|
5
|
+
|
|
6
|
+
// Variable font axes that map to @font-face range descriptors.
|
|
7
|
+
const AXIS_DESCRIPTORS = {
|
|
8
|
+
wght: axis => `font-weight: ${axis.min} ${axis.max}`,
|
|
9
|
+
wdth: axis => `font-stretch: ${axis.min}% ${axis.max}%`,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export class FontFaceCss {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.cssOutput = false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
setUnicodeRange(range) {
|
|
18
|
+
this.unicodeRange = range;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
setSubset(glyphhangerSubset) {
|
|
22
|
+
this.subset = glyphhangerSubset;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
setCSSOutput(cssOutput) {
|
|
26
|
+
this.cssOutput = cssOutput;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
setFamilies( families ) {
|
|
30
|
+
if( families && typeof families === "string" ) {
|
|
31
|
+
let split = families.split(",").map(family => family.trim());
|
|
32
|
+
if( split.length ) {
|
|
33
|
+
this.family = split[0];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
this.families = families;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Subset fonts are written next to the CSS file, so src URLs are bare filenames.
|
|
41
|
+
getSrcDescriptor( ttfPath ) {
|
|
42
|
+
var srcs = this.subset.getSrcsObject(ttfPath);
|
|
43
|
+
|
|
44
|
+
var src = [];
|
|
45
|
+
if( srcs.woff2 ) {
|
|
46
|
+
src.push(`url(${srcs.woff2}) format("woff2")`);
|
|
47
|
+
}
|
|
48
|
+
if( srcs.woff ) {
|
|
49
|
+
src.push(`url(${srcs.woff}) format("woff")`);
|
|
50
|
+
}
|
|
51
|
+
if( srcs.truetype ) {
|
|
52
|
+
src.push(`url(${srcs.truetype}) format("truetype")`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return src.join(", ");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
toString(ttfPath) {
|
|
59
|
+
let family = this.family;
|
|
60
|
+
let content = [];
|
|
61
|
+
|
|
62
|
+
if(family) {
|
|
63
|
+
content.push(` font-family: ${family};`);
|
|
64
|
+
}
|
|
65
|
+
if(ttfPath && this.subset) {
|
|
66
|
+
content.push(` src: ${this.getSrcDescriptor(ttfPath)};`);
|
|
67
|
+
|
|
68
|
+
let keep = this.subset.getAxes();
|
|
69
|
+
let axes = readVariationAxes( toSfnt( this.subset.readFont( ttfPath ) ) ).filter( axis => !keep || keep.includes( axis.tag ) );
|
|
70
|
+
for( let axis of axes ) {
|
|
71
|
+
if( AXIS_DESCRIPTORS[axis.tag] ) {
|
|
72
|
+
content.push(` ${AXIS_DESCRIPTORS[axis.tag](axis)};`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if( this.unicodeRange && this.unicodeRange.trim() ) {
|
|
78
|
+
content.push(` unicode-range: ${this.unicodeRange};`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return `
|
|
82
|
+
@font-face {
|
|
83
|
+
${content.join("\n")}
|
|
84
|
+
}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
writeCSSFiles() {
|
|
88
|
+
if(!this.subset || !this.cssOutput) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let paths = this.subset.getPaths();
|
|
93
|
+
let outputDir = this.subset.getOutputDirectory();
|
|
94
|
+
for( let filePath of paths ) {
|
|
95
|
+
let dir = outputDir || path.dirname(this.subset.getFontName(filePath));
|
|
96
|
+
let outputFile = path.join( dir, this.subset.getFontBaseName(filePath) + ".css" );
|
|
97
|
+
console.log("Writing CSS file:", outputFile);
|
|
98
|
+
if(dir) {
|
|
99
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
100
|
+
}
|
|
101
|
+
fs.writeFileSync(outputFile, `/* This file was automatically generated by GlyphHanger ${pkg.version} */
|
|
102
|
+
${this.toString(filePath)}`, {"encoding": "utf8"});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
output() {
|
|
107
|
+
if( !this.cssOutput ) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if( this.subset ) {
|
|
112
|
+
let paths = this.subset.getPaths();
|
|
113
|
+
for( let path of paths ) {
|
|
114
|
+
console.log(this.toString(path));
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
console.log(this.toString());
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
package/src/Format.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function Format( formatsString ) {
|
|
2
|
+
this.setFormats( formatsString || "woff2" );
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
Format.prototype.setFormats = function( formatsString ) {
|
|
6
|
+
var lookup = {};
|
|
7
|
+
formatsString.split( "," ).forEach(function( format ) {
|
|
8
|
+
lookup[ format.trim().toLowerCase() ] = true;
|
|
9
|
+
})
|
|
10
|
+
this.formats = lookup;
|
|
11
|
+
};
|
|
12
|
+
Format.prototype.hasFormat = function( format ) {
|
|
13
|
+
return this.formats[ format ] === true;
|
|
14
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { tag } from "./webfont/Sfnt.js";
|
|
4
|
+
|
|
5
|
+
const HB_MEMORY_MODE_WRITABLE = 2;
|
|
6
|
+
const HB_SUBSET_SETS_LAYOUT_FEATURE_TAG = 6;
|
|
7
|
+
|
|
8
|
+
let exports;
|
|
9
|
+
|
|
10
|
+
function getExports() {
|
|
11
|
+
if( !exports ) {
|
|
12
|
+
let wasmPath = createRequire(import.meta.url).resolve("harfbuzzjs/dist/harfbuzz-subset.wasm");
|
|
13
|
+
let instance = new WebAssembly.Instance(new WebAssembly.Module(fs.readFileSync(wasmPath)));
|
|
14
|
+
exports = instance.exports;
|
|
15
|
+
exports._initialize();
|
|
16
|
+
}
|
|
17
|
+
return exports;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Accepts the `U+20-7E,U+A0` strings that characterset’s toHexRangeString produces.
|
|
21
|
+
function* getCodePoints(unicodes) {
|
|
22
|
+
for( let range of unicodes.split(",") ) {
|
|
23
|
+
let [start, end = start] = range.trim().replace(/^U\+/i, "").split("-").map(hex => parseInt(hex, 16));
|
|
24
|
+
for( let codePoint = start; codePoint <= end; codePoint++ ) {
|
|
25
|
+
yield codePoint;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Runs one hb_subset pass; configure sets up the subset input.
|
|
31
|
+
function runSubset( fontBuffer, configure ) {
|
|
32
|
+
let hb = getExports();
|
|
33
|
+
|
|
34
|
+
let fontPointer = hb.malloc(fontBuffer.byteLength);
|
|
35
|
+
new Uint8Array(hb.memory.buffer, fontPointer, fontBuffer.byteLength).set(fontBuffer);
|
|
36
|
+
let blob = hb.hb_blob_create(fontPointer, fontBuffer.byteLength, HB_MEMORY_MODE_WRITABLE, 0, 0);
|
|
37
|
+
let face = hb.hb_face_create(blob, 0);
|
|
38
|
+
hb.hb_blob_destroy(blob);
|
|
39
|
+
|
|
40
|
+
let input = hb.hb_subset_input_create_or_fail();
|
|
41
|
+
let subsetFace;
|
|
42
|
+
try {
|
|
43
|
+
configure(hb, input, face);
|
|
44
|
+
subsetFace = hb.hb_subset_or_fail(face, input);
|
|
45
|
+
} finally {
|
|
46
|
+
hb.hb_subset_input_destroy(input);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
if( !subsetFace ) {
|
|
51
|
+
throw new Error("HarfBuzz failed to subset the font.");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let resultBlob = hb.hb_face_reference_blob(subsetFace);
|
|
55
|
+
let dataPointer = hb.hb_blob_get_data(resultBlob, 0);
|
|
56
|
+
let length = hb.hb_blob_get_length(resultBlob);
|
|
57
|
+
let sfnt = Buffer.from(new Uint8Array(hb.memory.buffer, dataPointer, length));
|
|
58
|
+
hb.hb_blob_destroy(resultBlob);
|
|
59
|
+
return sfnt;
|
|
60
|
+
} finally {
|
|
61
|
+
if( subsetFace ) {
|
|
62
|
+
hb.hb_face_destroy(subsetFace);
|
|
63
|
+
}
|
|
64
|
+
hb.hb_face_destroy(face);
|
|
65
|
+
hb.free(fontPointer);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function subsetWithHarfBuzz( fontBuffer, unicodes ) {
|
|
70
|
+
return runSubset(fontBuffer, (hb, input) => {
|
|
71
|
+
let unicodeSet = hb.hb_subset_input_unicode_set(input);
|
|
72
|
+
for( let codePoint of getCodePoints(unicodes || "") ) {
|
|
73
|
+
hb.hb_set_add(unicodeSet, codePoint);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Matches pyftsubset’s `--layout-features=*`.
|
|
77
|
+
let featureSet = hb.hb_subset_input_set(input, HB_SUBSET_SETS_LAYOUT_FEATURE_TAG);
|
|
78
|
+
hb.hb_set_clear(featureSet);
|
|
79
|
+
hb.hb_set_invert(featureSet);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Keeps every glyph and table and removes the given variation axes, fixed at their defaults.
|
|
84
|
+
export function pinAxesWithHarfBuzz( fontBuffer, axisTags ) {
|
|
85
|
+
return runSubset(fontBuffer, (hb, input, face) => {
|
|
86
|
+
hb.hb_subset_input_keep_everything(input);
|
|
87
|
+
for( let axisTag of axisTags ) {
|
|
88
|
+
if( !hb.hb_subset_input_pin_axis_to_default(input, face, tag(axisTag)) ) {
|
|
89
|
+
throw new Error(`HarfBuzz couldn’t remove the ${axisTag} axis.`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|