lib-font 2.6.0 → 3.0.1

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.
Files changed (27) hide show
  1. package/.github/workflows/ci.yml +29 -0
  2. package/.prettierrc +6 -0
  3. package/LICENSE +1 -1
  4. package/lib-font.browser.js +1 -1
  5. package/lib-font.js +176 -162
  6. package/package.json +8 -11
  7. package/src/opentype/tables/advanced/lookups/gpos/gpos-lookup.js +15 -2
  8. package/src/opentype/tables/advanced/lookups/gpos/lookup-type-1.js +1 -0
  9. package/src/opentype/tables/advanced/lookups/gpos/lookup-type-2.js +1 -0
  10. package/src/opentype/tables/advanced/lookups/gpos/lookup-type-3.js +1 -0
  11. package/src/opentype/tables/advanced/lookups/gpos/lookup-type-4.js +3 -1
  12. package/src/opentype/tables/advanced/lookups/gpos/lookup-type-5.js +3 -1
  13. package/src/opentype/tables/advanced/lookups/gpos/lookup-type-6.js +3 -1
  14. package/src/opentype/tables/advanced/lookups/gpos/lookup-type-7.js +1 -0
  15. package/src/opentype/tables/advanced/lookups/gpos/lookup-type-8.js +5 -1
  16. package/src/opentype/tables/advanced/lookups/gpos/lookup-type-9.js +3 -1
  17. package/src/opentype/tables/advanced/lookups/gsub/gsub-lookup.js +3 -1
  18. package/src/opentype/tables/advanced/lookups/gsub/lookup-type-1.js +12 -1
  19. package/src/opentype/tables/advanced/lookups/gsub/lookup-type-2.js +1 -0
  20. package/src/opentype/tables/advanced/lookups/gsub/lookup-type-3.js +1 -0
  21. package/src/opentype/tables/advanced/lookups/gsub/lookup-type-4.js +1 -0
  22. package/src/opentype/tables/advanced/lookups/gsub/lookup-type-5.js +10 -11
  23. package/src/opentype/tables/advanced/lookups/gsub/lookup-type-6.js +51 -24
  24. package/src/opentype/tables/advanced/lookups/gsub/lookup-type-7.js +46 -5
  25. package/src/opentype/tables/advanced/lookups/gsub/lookup-type-8.js +1 -0
  26. package/src/opentype/tables/advanced/shared/lookup.js +3 -1
  27. package/src/opentype/tables/simple/name.js +27 -1
package/lib-font.js CHANGED
@@ -3,7 +3,7 @@ import { Event, EventManager } from "./src/eventing.js";
3
3
  import { SFNT, WOFF, WOFF2 } from "./src/opentype/index.js";
4
4
  import { loadTableClasses } from "./src/opentype/tables/createTable.js";
5
5
  import { setupFontFace } from "./src/utils/fontface.js";
6
- import { validFontFormat } from "./src/utils/validator.js";
6
+ import { validFontFormat } from "./src/utils/validator.js";
7
7
 
8
8
  /**
9
9
  * Borderline trivial http response helper function
@@ -11,177 +11,191 @@ import { validFontFormat } from "./src/utils/validator.js";
11
11
  * @param {HttpResponse} response
12
12
  */
13
13
  function checkFetchResponseStatus(response) {
14
- if (!response.ok) {
15
- throw new Error(`HTTP ${response.status} - ${response.statusText}`);
16
- }
17
- return response;
14
+ if (!response.ok) {
15
+ throw new Error(`HTTP ${response.status} - ${response.statusText}`);
16
+ }
17
+ return response;
18
18
  }
19
19
 
20
20
  /**
21
21
  * The Font object, which the WebAPIs are still sorely missing.
22
22
  */
23
23
  class Font extends EventManager {
24
- constructor(name, options={}) {
25
- super();
26
- this.name = name;
27
- this.options = options;
28
- this.metrics = false;
29
- }
30
-
31
- get src() { return this.__src; }
32
-
33
- /**
34
- * Just like Image and Audio, we kick everything off when
35
- * our `src` gets assigned.
36
- *
37
- * @param {string} source url for this font ("real" or blob/base64)
38
- */
39
- set src(src) {
40
- this.__src = src;
41
- (async() => {
42
- if (globalThis.document && !this.options.skipStyleSheet) {
43
- await setupFontFace(this.name, src, this.options);
44
- }
45
- this.loadFont(src);
46
- })();
47
- }
48
-
49
- /**
50
- * This is a non-blocking operation.
51
- *
52
- * @param {String} url The URL for the font in question
53
- * @param {String} filename The filename to use when URL is a blob/base64 string
54
- */
55
- async loadFont(url, filename) {
56
- fetch(url)
57
- .then(response => checkFetchResponseStatus(response) && response.arrayBuffer())
58
- .then(buffer => this.fromDataBuffer(buffer, filename || url))
59
- .catch(err => {
60
- const evt = new Event(`error`, err, `Failed to load font at ${filename || url}`);
61
- this.dispatch(evt);
62
- if (this.onerror) this.onerror(evt);
63
- });
64
- }
65
-
66
- /**
67
- * This is a non-blocking operation.
68
- *
69
- * @param {Buffer} buffer The binary data associated with this font.
70
- */
71
- async fromDataBuffer(buffer, filenameOrUrL) {
72
- this.fontData = new DataView(buffer); // Big Endian
73
- let type = validFontFormat(this.fontData);
74
- if (!type) {
75
- // handled in loadFont's .catch()
76
- throw new Error(`${filenameOrUrL} is either an unsupported font format, or not a font at all.`);
77
- }
78
- await this.parseBasicData(type);
79
- const evt = new Event("load", { font: this });
24
+ constructor(name, options = {}) {
25
+ super();
26
+ this.name = name;
27
+ this.options = options;
28
+ this.metrics = false;
29
+ }
30
+
31
+ get src() {
32
+ return this.__src;
33
+ }
34
+
35
+ /**
36
+ * Just like Image and Audio, we kick everything off when
37
+ * our `src` gets assigned.
38
+ *
39
+ * @param {string} source url for this font ("real" or blob/base64)
40
+ */
41
+ set src(src) {
42
+ this.__src = src;
43
+ (async () => {
44
+ if (globalThis.document && !this.options.skipStyleSheet) {
45
+ await setupFontFace(this.name, src, this.options);
46
+ }
47
+ this.loadFont(src);
48
+ })();
49
+ }
50
+
51
+ /**
52
+ * This is a non-blocking operation.
53
+ *
54
+ * @param {String} url The URL for the font in question
55
+ * @param {String} filename The filename to use when URL is a blob/base64 string
56
+ */
57
+ async loadFont(url, filename) {
58
+ fetch(url)
59
+ .then(
60
+ (response) =>
61
+ checkFetchResponseStatus(response) && response.arrayBuffer()
62
+ )
63
+ .then((buffer) => this.fromDataBuffer(buffer, filename || url))
64
+ .catch((err) => {
65
+ const evt = new Event(
66
+ `error`,
67
+ err,
68
+ `Failed to load font at ${filename || url}`
69
+ );
80
70
  this.dispatch(evt);
81
- if (this.onload) this.onload(evt);
71
+ if (this.onerror) this.onerror(evt);
72
+ });
73
+ }
74
+
75
+ /**
76
+ * This is a non-blocking operation.
77
+ *
78
+ * @param {Buffer} buffer The binary data associated with this font.
79
+ */
80
+ async fromDataBuffer(buffer, filenameOrUrL) {
81
+ this.fontData = new DataView(buffer); // Big Endian
82
+ let type = validFontFormat(this.fontData);
83
+ if (!type) {
84
+ // handled in loadFont's .catch()
85
+ throw new Error(
86
+ `${filenameOrUrL} is either an unsupported font format, or not a font at all.`
87
+ );
82
88
  }
83
-
84
- /**
85
- * This is a non-blocking operation IF called from an async function
86
- */
87
- async parseBasicData(type) {
88
- return loadTableClasses().then(createTable => {
89
- if (type === `SFNT`) {
90
- this.opentype = new SFNT(this, this.fontData, createTable);
91
- }
92
- if (type === `WOFF`) {
93
- this.opentype = new WOFF(this, this.fontData, createTable);
94
- }
95
- if (type === `WOFF2`) {
96
- this.opentype = new WOFF2(this, this.fontData, createTable);
97
- }
98
- return this.opentype;
99
- });
89
+ await this.parseBasicData(type);
90
+ const evt = new CustomEvent("load", { detail: { font: this } });
91
+ this.dispatch(evt);
92
+ if (this.onload) this.onload(evt);
93
+ }
94
+
95
+ /**
96
+ * This is a non-blocking operation IF called from an async function
97
+ */
98
+ async parseBasicData(type) {
99
+ return loadTableClasses().then((createTable) => {
100
+ if (type === `SFNT`) {
101
+ this.opentype = new SFNT(this, this.fontData, createTable);
102
+ }
103
+ if (type === `WOFF`) {
104
+ this.opentype = new WOFF(this, this.fontData, createTable);
105
+ }
106
+ if (type === `WOFF2`) {
107
+ this.opentype = new WOFF2(this, this.fontData, createTable);
108
+ }
109
+ return this.opentype;
110
+ });
111
+ }
112
+
113
+ /**
114
+ * Does this font support the specified character?
115
+ * @param {*} char
116
+ */
117
+ getGlyphId(char) {
118
+ return this.opentype.tables.cmap.getGlyphId(char);
119
+ }
120
+
121
+ /**
122
+ * find the actual "letter" for a given glyphid
123
+ * @param {*} glyphid
124
+ */
125
+ reverse(glyphid) {
126
+ return this.opentype.tables.cmap.reverse(glyphid);
127
+ }
128
+
129
+ /**
130
+ * Does this font support the specified character?
131
+ * @param {*} char
132
+ */
133
+ supports(char) {
134
+ return this.getGlyphId(char) !== 0;
135
+ }
136
+
137
+ /**
138
+ * Does this font support the specified unicode variation?
139
+ * @param {*} variation
140
+ */
141
+ supportsVariation(variation) {
142
+ return this.opentype.tables.cmap.supportsVariation(variation) !== false;
143
+ }
144
+
145
+ /**
146
+ * Effectively, be https://html.spec.whatwg.org/multipage/canvas.html#textmetrics
147
+ * @param {*} text
148
+ * @param {*} size
149
+ */
150
+ measureText(text, size = 16) {
151
+ if (this.__unloaded)
152
+ throw new Error(
153
+ "Cannot measure text: font was unloaded. Please reload before calling measureText()"
154
+ );
155
+ let d = document.createElement("div");
156
+ d.textContent = text;
157
+ d.style.fontFamily = this.name;
158
+ d.style.fontSize = `${size}px`;
159
+ d.style.color = `transparent`;
160
+ d.style.background = `transparent`;
161
+ d.style.top = `0`;
162
+ d.style.left = `0`;
163
+ d.style.position = `absolute`;
164
+ document.body.appendChild(d);
165
+ let bbox = d.getBoundingClientRect();
166
+ document.body.removeChild(d);
167
+ const OS2 = this.opentype.tables["OS/2"];
168
+ bbox.fontSize = size;
169
+ bbox.ascender = OS2.sTypoAscender;
170
+ bbox.descender = OS2.sTypoDescender;
171
+ return bbox;
172
+ }
173
+
174
+ /**
175
+ * unload this font from the DOM context, making it no longer available for CSS purposes
176
+ */
177
+ unload() {
178
+ if (this.styleElement.parentNode) {
179
+ this.styleElement.parentNode.removeElement(this.styleElement);
180
+ const evt = new Event("unload", { font: this });
181
+ this.dispatch(evt);
182
+ if (this.onunload) this.onunload(evt);
100
183
  }
101
-
102
- /**
103
- * Does this font support the specified character?
104
- * @param {*} char
105
- */
106
- getGlyphId(char) {
107
- return this.opentype.tables.cmap.getGlyphId(char);
108
- }
109
-
110
- /**
111
- * find the actual "letter" for a given glyphid
112
- * @param {*} glyphid
113
- */
114
- reverse(glyphid) {
115
- return this.opentype.tables.cmap.reverse(glyphid);
116
- }
117
-
118
- /**
119
- * Does this font support the specified character?
120
- * @param {*} char
121
- */
122
- supports(char) {
123
- return this.getGlyphId(char) !== 0
124
- }
125
-
126
- /**
127
- * Does this font support the specified unicode variation?
128
- * @param {*} variation
129
- */
130
- supportsVariation(variation) {
131
- return this.opentype.tables.cmap.supportsVariation(variation) !== false;
132
- }
133
-
134
- /**
135
- * Effectively, be https://html.spec.whatwg.org/multipage/canvas.html#textmetrics
136
- * @param {*} text
137
- * @param {*} size
138
- */
139
- measureText(text, size=16) {
140
- if (this.__unloaded) throw new Error("Cannot measure text: font was unloaded. Please reload before calling measureText()");
141
- let d = document.createElement('div');
142
- d.textContent = text;
143
- d.style.fontFamily = this.name;
144
- d.style.fontSize = `${size}px`;
145
- d.style.color = `transparent`;
146
- d.style.background = `transparent`;
147
- d.style.top = `0`;
148
- d.style.left = `0`;
149
- d.style.position = `absolute`;
150
- document.body.appendChild(d);
151
- let bbox = d.getBoundingClientRect();
152
- document.body.removeChild(d);
153
- const OS2 = this.opentype.tables["OS/2"];
154
- bbox.fontSize = size;
155
- bbox.ascender = OS2.sTypoAscender;
156
- bbox.descender = OS2.sTypoDescender;
157
- return bbox;
158
- }
159
-
160
- /**
161
- * unload this font from the DOM context, making it no longer available for CSS purposes
162
- */
163
- unload() {
164
- if (this.styleElement.parentNode) {
165
- this.styleElement.parentNode.removeElement(this.styleElement);
166
- const evt = new Event("unload", { font: this });
167
- this.dispatch(evt);
168
- if (this.onunload) this.onunload(evt);
169
- }
170
- this._unloaded = true;
171
- }
172
-
173
- /**
174
- * load this font back into the DOM context after being unload()'d earlier.
175
- */
176
- load() {
177
- if (this.__unloaded) {
178
- delete this.__unloaded;
179
- document.head.appendChild(this.styleElement);
180
- const evt = new Event("load", { font: this });
181
- this.dispatch(evt);
182
- if (this.onload) this.onload(evt);
183
- }
184
+ this._unloaded = true;
185
+ }
186
+
187
+ /**
188
+ * load this font back into the DOM context after being unload()'d earlier.
189
+ */
190
+ load() {
191
+ if (this.__unloaded) {
192
+ delete this.__unloaded;
193
+ document.head.appendChild(this.styleElement);
194
+ const evt = new Event("load", { font: this });
195
+ this.dispatch(evt);
196
+ if (this.onload) this.onload(evt);
184
197
  }
198
+ }
185
199
  }
186
200
 
187
201
  globalThis.Font = Font;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lib-font",
3
- "version": "2.6.0",
3
+ "version": "3.0.1",
4
4
  "description": "A JS based OpenType font inspector",
5
5
  "main": "./lib-font.js",
6
6
  "exports": "./lib-font.js",
@@ -13,18 +13,17 @@
13
13
  },
14
14
  "scripts": {
15
15
  "bundle": "run-s rollup terser",
16
- "clean": "prettier --write \"src/**/*.js\"",
16
+ "clean": "prettier --write \"lib-font.js\" \"src/**/*.js\"",
17
17
  "rollup": "rollup -i lib-font.js -n Font -o lib-font.browser.js --inlineDynamicImports --external fs,zlib",
18
18
  "start": "npm test",
19
19
  "terser": "terser lib-font.browser.js -o lib-font.browser.js",
20
- "test:browser": "run-p test:server test:puppeteer",
21
- "test:node:jest": "npm run test:jest -- ./testing/node/",
22
- "test:node:native": "node --test \"./testing/native-node/**/*.js\"",
23
- "test:jest": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --verbose=false",
20
+ "test:browser": "run-p test:server test:playwright",
21
+ "test:cleanup": "rm -rf ./test-results",
22
+ "test:node": "node --test \"./testing/node/**/*.test.js\" \"./testing/native-node/**/*.js\"",
24
23
  "test:manual": "http-server -o testing/manual/index.html",
25
- "test:puppeteer": "node ./testing/browser/puppeteer.js",
24
+ "test:playwright": "playwright test ./testing/browser/playwright.test.js",
26
25
  "test:server": "node ./testing/browser/server.js",
27
- "test": "run-s clean test:node:jest test:node:native bundle test:browser"
26
+ "test": "run-s clean test:node bundle test:browser test:cleanup"
28
27
  },
29
28
  "repository": {
30
29
  "type": "git",
@@ -43,14 +42,12 @@
43
42
  },
44
43
  "homepage": "https://github.com/Pomax/lib-font#readme",
45
44
  "devDependencies": {
46
- "cross-env": "^7.0.2",
45
+ "@playwright/test": "^1.56.1",
47
46
  "express": "^4.17.1",
48
47
  "http-server": "^0.12.3",
49
- "jest": "^26.6.3",
50
48
  "npm-run-all": "^4.1.5",
51
49
  "open-cli": "^6.0.1",
52
50
  "prettier": "^2.1.1",
53
- "puppeteer": "^5.3.1",
54
51
  "rollup": "^2.53.0",
55
52
  "terser": "^5.7.1"
56
53
  }
@@ -1,10 +1,23 @@
1
1
  import { ParsedData } from "../../../../../parser.js";
2
2
 
3
- // Dummy placeholder
3
+ // Not every lookup subtable indicates a coverageOffset, so for
4
+ // those few that need it, we want to be able to undo parsing that.
5
+ function undoCoverageOffsetParsing(instance) {
6
+ instance.parser.currentPosition -= 2;
7
+
8
+ // remove the coverageOffset property binding
9
+ delete instance.coverageOffset;
10
+
11
+ // we can't delete a prototype function, but we can undefine it.
12
+ instance.getCoverageTable = undefined;
13
+ }
14
+
4
15
  class LookupType extends ParsedData {
5
16
  constructor(p) {
6
17
  super(p);
18
+ this.format = p.uint16;
19
+ this.coverageOffset = p.Offset16;
7
20
  }
8
21
  }
9
22
 
10
- export { LookupType };
23
+ export { LookupType, undoCoverageOffsetParsing };
@@ -1,6 +1,7 @@
1
1
  import { LookupType } from "./gpos-lookup.js";
2
2
 
3
3
  class LookupType1 extends LookupType {
4
+ type = 1;
4
5
  constructor(p) {
5
6
  super(p);
6
7
  console.log(`lookup type 1`);
@@ -1,6 +1,7 @@
1
1
  import { LookupType } from "./gpos-lookup.js";
2
2
 
3
3
  class LookupType2 extends LookupType {
4
+ type = 2;
4
5
  constructor(p) {
5
6
  super(p);
6
7
  console.log(`lookup type 2`);
@@ -1,6 +1,7 @@
1
1
  import { LookupType } from "./gpos-lookup.js";
2
2
 
3
3
  class LookupType3 extends LookupType {
4
+ type = 3;
4
5
  constructor(p) {
5
6
  super(p);
6
7
  console.log(`lookup type 3`);
@@ -1,8 +1,10 @@
1
- import { LookupType } from "./gpos-lookup.js";
1
+ import { LookupType, undoCoverageOffsetParsing } from "./gpos-lookup.js";
2
2
 
3
3
  class LookupType4 extends LookupType {
4
+ type = 4;
4
5
  constructor(p) {
5
6
  super(p);
7
+ undoCoverageOffsetParsing(this);
6
8
  console.log(`lookup type 4`);
7
9
  }
8
10
  }
@@ -1,8 +1,10 @@
1
- import { LookupType } from "./gpos-lookup.js";
1
+ import { LookupType, undoCoverageOffsetParsing } from "./gpos-lookup.js";
2
2
 
3
3
  class LookupType5 extends LookupType {
4
+ type = 5;
4
5
  constructor(p) {
5
6
  super(p);
7
+ undoCoverageOffsetParsing(this);
6
8
  console.log(`lookup type 5`);
7
9
  }
8
10
  }
@@ -1,8 +1,10 @@
1
- import { LookupType } from "./gpos-lookup.js";
1
+ import { LookupType, undoCoverageOffsetParsing } from "./gpos-lookup.js";
2
2
 
3
3
  class LookupType6 extends LookupType {
4
+ type = 6;
4
5
  constructor(p) {
5
6
  super(p);
7
+ undoCoverageOffsetParsing(this);
6
8
  console.log(`lookup type 6`);
7
9
  }
8
10
  }
@@ -1,6 +1,7 @@
1
1
  import { LookupType } from "./gpos-lookup.js";
2
2
 
3
3
  class LookupType7 extends LookupType {
4
+ type = 7;
4
5
  constructor(p) {
5
6
  super(p);
6
7
  console.log(`lookup type 7`);
@@ -1,8 +1,12 @@
1
- import { LookupType } from "./gpos-lookup.js";
1
+ import { LookupType, undoCoverageOffsetParsing } from "./gpos-lookup.js";
2
2
 
3
3
  class LookupType8 extends LookupType {
4
+ type = 8;
4
5
  constructor(p) {
5
6
  super(p);
7
+ if (this.format === 3) {
8
+ undoCoverageOffsetParsing(this);
9
+ }
6
10
  console.log(`lookup type 8`);
7
11
  }
8
12
  }
@@ -1,8 +1,10 @@
1
- import { LookupType } from "./gpos-lookup.js";
1
+ import { LookupType, undoCoverageOffsetParsing } from "./gpos-lookup.js";
2
2
 
3
3
  class LookupType9 extends LookupType {
4
+ type = 9;
4
5
  constructor(p) {
5
6
  super(p);
7
+ undoCoverageOffsetParsing(this);
6
8
  console.log(`lookup type 9`);
7
9
  }
8
10
  }
@@ -1,6 +1,8 @@
1
1
  import { ParsedData } from "../../../../../parser.js";
2
2
  import { CoverageTable } from "../../shared/coverage.js";
3
3
 
4
+ // Not every lookup subtable indicates a coverageOffset, so for
5
+ // those few that need it, we want to be able to undo parsing that.
4
6
  function undoCoverageOffsetParsing(instance) {
5
7
  instance.parser.currentPosition -= 2;
6
8
  delete instance.coverageOffset;
@@ -10,7 +12,7 @@ function undoCoverageOffsetParsing(instance) {
10
12
  class LookupType extends ParsedData {
11
13
  constructor(p) {
12
14
  super(p);
13
- this.substFormat = p.uint16;
15
+ this.format = p.uint16;
14
16
  this.coverageOffset = p.Offset16;
15
17
  }
16
18
  getCoverageTable() {
@@ -1,9 +1,20 @@
1
1
  import { LookupType } from "./gsub-lookup.js";
2
2
 
3
3
  class LookupType1 extends LookupType {
4
+ type = 1;
4
5
  constructor(p) {
5
6
  super(p);
6
- this.deltaGlyphID = p.int16;
7
+
8
+ if (this.format === 1) {
9
+ this.deltaGlyphID = p.uint16;
10
+ }
11
+
12
+ if (this.format === 2) {
13
+ this.glyphCount = p.Offset16;
14
+ this.substituteGlyphIDs = [...new Array(this.glyphCount)].map(
15
+ (_) => p.uint16
16
+ );
17
+ }
7
18
  }
8
19
  }
9
20
 
@@ -1,6 +1,7 @@
1
1
  import { LookupType } from "./gsub-lookup.js";
2
2
 
3
3
  class LookupType2 extends LookupType {
4
+ type = 2;
4
5
  constructor(p) {
5
6
  super(p);
6
7
  this.sequenceCount = p.uint16;
@@ -1,6 +1,7 @@
1
1
  import { LookupType } from "./gsub-lookup.js";
2
2
 
3
3
  class LookupType3 extends LookupType {
4
+ type = 3;
4
5
  constructor(p) {
5
6
  super(p);
6
7
  this.alternateSetCount = p.uint16;
@@ -2,6 +2,7 @@ import { ParsedData } from "../../../../../parser.js";
2
2
  import { LookupType } from "./gsub-lookup.js";
3
3
 
4
4
  class LookupType4 extends LookupType {
5
+ type = 4;
5
6
  constructor(p) {
6
7
  super(p);
7
8
  this.ligatureSetCount = p.uint16;