lib-font 3.0.4 → 3.0.6

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.
@@ -30,6 +30,7 @@ class sbix extends SimpleTable {
30
30
 
31
31
  class Strike extends ParsedData {
32
32
  constructor(p, numGlyphs) {
33
+ super(p);
33
34
  this.ppem = p.uint16;
34
35
  this.ppi = p.uint16;
35
36
  lazy(this, `glyphDataOffsets`, () =>
@@ -53,14 +53,14 @@ class IndexSubHeader {
53
53
 
54
54
  class BigGlyphMetrics {
55
55
  constructor(p) {
56
- this.height = uint8;
57
- this.width = uint8;
58
- this.horiBearingX = int8;
59
- this.horiBearingY = int8;
60
- this.horiAdvance = uint8;
61
- this.vertBearingX = int8;
62
- this.vertBearingY = int8;
63
- this.vertAdvance = uint8;
56
+ this.height = p.uint8;
57
+ this.width = p.uint8;
58
+ this.horiBearingX = p.int8;
59
+ this.horiBearingY = p.int8;
60
+ this.horiAdvance = p.up.int8;
61
+ this.vertBearingX = p.int8;
62
+ this.vertBearingY = p.int8;
63
+ this.vertAdvance = p.uint8;
64
64
  }
65
65
  }
66
66
 
@@ -1,9 +1,10 @@
1
+ import { ParsedData } from "../../../../../parser.js";
1
2
  import { IndexSubHeader } from "../shared.js";
2
3
 
3
4
  /**
4
5
  * IndexSubTable1: variable-metrics glyphs with 4-byte offsets
5
6
  */
6
- class Subtable1 {
7
+ class Subtable1 extends ParsedData {
7
8
  constructor(p) {
8
9
  super(p);
9
10
  this.header = new IndexSubHeader(p);
@@ -1,9 +1,10 @@
1
+ import { ParsedData } from "../../../../../parser.js";
1
2
  import { IndexSubHeader, BigGlyphMetrics } from "../shared.js";
2
3
 
3
4
  /**
4
5
  * IndexSubTable2: all glyphs have identical metrics
5
6
  */
6
- class Subtable2 {
7
+ class Subtable2 extends ParsedData {
7
8
  constructor(p) {
8
9
  super(p);
9
10
  this.header = new IndexSubHeader(p);
@@ -1,9 +1,10 @@
1
+ import { ParsedData } from "../../../../../parser.js";
1
2
  import { IndexSubHeader } from "../shared.js";
2
3
 
3
4
  /**
4
5
  * IndexSubTable3: variable-metrics glyphs with 2-byte offsets
5
6
  */
6
- class Subtable3 {
7
+ class Subtable3 extends ParsedData {
7
8
  constructor(p) {
8
9
  super(p);
9
10
  this.header = new IndexSubHeader(p);
@@ -1,10 +1,11 @@
1
1
  import { IndexSubHeader } from "../shared.js";
2
2
  import lazy from "../../../../../lazy.js";
3
+ import { ParsedData } from "../../../../../parser.js";
3
4
 
4
5
  /**
5
6
  * IndexSubTable4: variable-metrics glyphs with sparse glyph codes
6
7
  */
7
- class Subtable4 {
8
+ class Subtable4 extends ParsedData {
8
9
  constructor(p) {
9
10
  super(p);
10
11
 
@@ -1,10 +1,11 @@
1
1
  import { IndexSubHeader, BigGlyphMetrics } from "../shared.js";
2
2
  import lazy from "../../../../../lazy.js";
3
+ import { ParsedData } from "../../../../../parser.js";
3
4
 
4
5
  /**
5
6
  * IndexSubTable5: constant-metrics glyphs with sparse glyph codes
6
7
  */
7
- class Subtable5 {
8
+ class Subtable5 extends ParsedData {
8
9
  constructor(p) {
9
10
  super(p);
10
11
 
@@ -12,8 +12,6 @@ import { Format14 } from "./format14.js";
12
12
 
13
13
  /**
14
14
  * Cmap Subtable factory
15
- * @param {int} format the subtable format number (see https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-0-byte-encoding-table onward)
16
- * @param {parser} parser a parser already pointing at the subtable's data location, right after reading the `format` uint16.
17
15
  */
18
16
  export default function createSubTable(parser, platformID, encodingID) {
19
17
  const format = parser.uint16;
@@ -2,6 +2,9 @@ import lazy from "../../../../lazy.js";
2
2
  import { Subtable } from "./subtable.js";
3
3
 
4
4
  class Format2 extends Subtable {
5
+ subHeaders;
6
+ glyphIndexArray;
7
+
5
8
  constructor(p, platformID, encodingID) {
6
9
  super(p, platformID, encodingID);
7
10
  this.format = 2;
@@ -53,6 +53,7 @@ class COLR extends SimpleTable {
53
53
  }
54
54
 
55
55
  getLayers(glyphID) {
56
+ const { p } = this;
56
57
  let record = this.getBaseGlyphRecord(glyphID);
57
58
  this.parser.currentPosition =
58
59
  this.tableStart + this.layerRecordsOffset + 4 * record.firstLayerIndex;
@@ -41,7 +41,7 @@ class name extends SimpleTable {
41
41
 
42
42
  /**
43
43
  * Resolve a string by ID
44
- * @param {uint16} nameID the id used to find the name record to resolve.
44
+ * @param {Number} nameID the id used to find the name record to resolve.
45
45
  */
46
46
  get(nameID) {
47
47
  let record = this.nameRecords.find((record) => record.nameID === nameID);
@@ -10,7 +10,7 @@ class hdmx extends SimpleTable {
10
10
  const { p } = super(dict, dataview);
11
11
  const numGlyphs = tables.hmtx.numGlyphs;
12
12
  this.version = p.uint16;
13
- this.numRecords = p.int16;
13
+ const numRecords = (this.numRecords = p.int16);
14
14
  this.sizeDeviceRecord = p.int32;
15
15
  this.records = [...new Array(numRecords)].map(
16
16
  (_) => new DeviceRecord(p, numGlyphs)
@@ -21,8 +21,8 @@ class loca extends SimpleTable {
21
21
  }
22
22
 
23
23
  getGlyphDataOffsetAndLength(glyphID) {
24
- let offset = this.offsets[glyphID] * this.x2 ? 2 : 1;
25
- let nextOffset = this.offsets[glyphID + 1] * this.x2 ? 2 : 1;
24
+ let offset = this.offsets[glyphID] * (this.x2 ? 2 : 1);
25
+ let nextOffset = this.offsets[glyphID + 1] * (this.x2 ? 2 : 1);
26
26
  return { offset, length: nextOffset - offset };
27
27
  }
28
28
  }
@@ -9,13 +9,13 @@ import lazy from "../../../../lazy.js";
9
9
  class cvar extends SimpleTable {
10
10
  constructor(dict, dataview) {
11
11
  const { p } = super(dict, dataview);
12
- this.majorVersion = uint16;
13
- this.minorVersion = uint16;
14
- this.tupleVariationCount = uint16;
12
+ this.majorVersion = p.uint16;
13
+ this.minorVersion = p.uint16;
14
+ this.tupleVariationCount = p.uint16;
15
15
  this.dataOffset = p.Offset32; // from the start of the table
16
16
 
17
17
  // FIXME: this is only correct if we can properly read full Tuple Variation Header...
18
- lazy(this`tupleVariationHeaders`, () =>
18
+ lazy(this, `tupleVariationHeaders`, () =>
19
19
  [...new Array(this.tupleVariationCount)].map(
20
20
  (_) => new TupleVariationHeader(p)
21
21
  )
@@ -43,13 +43,13 @@ class TupleVariationHeader {
43
43
  }
44
44
 
45
45
  get EMBEDDED_PEAK_TUPLE() {
46
- return this.tupleIndex & (0x8000 === 0x8000);
46
+ return (this.tupleIndex & 0x8000) === 0x8000;
47
47
  }
48
48
  get INTERMEDIATE_REGION() {
49
- return this.tupleIndex & (0x4000 === 0x4000);
49
+ return (this.tupleIndex & 0x4000) === 0x4000;
50
50
  }
51
51
  get PRIVATE_POINT_NUMBERS() {
52
- return this.tupleIndex & (0x2000 === 0x2000);
52
+ return (this.tupleIndex & 0x2000) === 0x2000;
53
53
  }
54
54
  get realTupleIndex() {
55
55
  return this.tupleIndex & 0xfff;
@@ -5,7 +5,8 @@ const gzipDecode = globalThis.pako ? globalThis.pako.inflate : undefined;
5
5
  let nativeGzipDecode = undefined;
6
6
 
7
7
  if (!gzipDecode) {
8
- import("zlib").then((zlib) => {
8
+ // @ts-ignore
9
+ import(`node:zlib`).then((zlib) => {
9
10
  nativeGzipDecode = (buffer) => zlib.unzipSync(buffer);
10
11
  });
11
12
  }
@@ -20,6 +21,7 @@ class WOFF extends SimpleTable {
20
21
  constructor(font, dataview, createTable) {
21
22
  const { p } = super({ offset: 0, length: 44 }, dataview, `woff`);
22
23
 
24
+ this.font = font;
23
25
  this.signature = p.tag;
24
26
  this.flavor = p.uint32;
25
27
  this.length = p.uint32;
@@ -61,12 +63,9 @@ class WoffTableDirectoryEntry {
61
63
  * woff/woff2 font, so that accessing a table via the
62
64
  * woff.tables.tableName or woff2.tables.tableName
63
65
  * property kicks off a table parse on first access.
64
- *
65
- * @param {*} woff the woff or woff2 font object
66
- * @param {DataView} dataview passed when dealing with woff
67
- * @param {buffer} decoded passed when dealing with woff2
68
66
  */
69
67
  function buildWoffLazyLookups(woff, dataview, createTable) {
68
+ const { font } = woff;
70
69
  woff.tables = {};
71
70
  woff.directory.forEach((entry) => {
72
71
  lazy(woff.tables, entry.tag.trim(), () => {
@@ -86,7 +85,7 @@ function buildWoffLazyLookups(woff, dataview, createTable) {
86
85
  unpacked = nativeGzipDecode(new Uint8Array(data));
87
86
  } else {
88
87
  const msg = `no brotli decoder available to decode WOFF2 font`;
89
- if (font.onerror) font.onerror(msg);
88
+ if (font?.onerror) font.onerror(msg);
90
89
  throw new Error(msg);
91
90
  }
92
91
 
@@ -5,7 +5,8 @@ const brotliDecode = globalThis.unbrotli;
5
5
  let nativeBrotliDecode = undefined;
6
6
 
7
7
  if (!brotliDecode) {
8
- import("zlib").then((zlib) => {
8
+ // @ts-ignore
9
+ import(`node:zlib`).then((zlib) => {
9
10
  nativeBrotliDecode = (buffer) => zlib.brotliDecompressSync(buffer);
10
11
  });
11
12
  }
@@ -111,10 +112,6 @@ class Woff2TableDirectoryEntry {
111
112
  * woff2 font, so that accessing a table via the
112
113
  * font.opentype.tables.tableName property kicks off
113
114
  * a table parse on first access.
114
- *
115
- * @param {*} woff2 the woff2 font object
116
- * @param {decoded} the original (decompressed) SFNT data
117
- * @param {createTable} the opentype table builder function
118
115
  */
119
116
  function buildWoff2LazyLookups(woff2, decoded, createTable) {
120
117
  woff2.tables = {};
package/src/parser.js CHANGED
@@ -2,8 +2,6 @@ const startDate = new Date(`1904-01-01T00:00:00+0000`).getTime();
2
2
 
3
3
  /**
4
4
  * Convert an array of uint8 char into a proper string.
5
- *
6
- * @param {uint8[]} data
7
5
  */
8
6
  function asText(data) {
9
7
  return Array.from(data)
@@ -21,23 +19,6 @@ class Parser {
21
19
  this.start = dict.offset;
22
20
  this.offset = 0;
23
21
  this.data = dataview;
24
-
25
- [
26
- `getInt8`,
27
- `getUint8`,
28
- `getInt16`,
29
- `getUint16`,
30
- `getInt32`,
31
- `getUint32`,
32
- `getBigInt64`,
33
- `getBigUint64`,
34
- ].forEach((name) => {
35
- let fn = name.replace(/get(Big)?/, "").toLowerCase();
36
- let increment = parseInt(name.replace(/[^\d]/g, "")) / 8;
37
- Object.defineProperty(this, fn, {
38
- get: () => this.getValue(name, increment),
39
- });
40
- });
41
22
  }
42
23
 
43
24
  get currentPosition() {
@@ -86,7 +67,7 @@ class Parser {
86
67
 
87
68
  get fixed() {
88
69
  let major = this.int16;
89
- let minor = Math.round((1000 * this.uint16) / 65356);
70
+ let minor = Math.round((1000 * this.uint16) / 2 ** 16);
90
71
  return major + minor / 1000;
91
72
  }
92
73
 
@@ -97,11 +78,43 @@ class Parser {
97
78
  return parseFloat(`${major}.${minor}`);
98
79
  }
99
80
 
81
+ get int8() {
82
+ return this.getValue(`getInt8`, 1);
83
+ }
84
+
85
+ get uint8() {
86
+ return this.getValue(`getUint8`, 1);
87
+ }
88
+
89
+ get int16() {
90
+ return this.getValue(`getInt16`, 2);
91
+ }
92
+
93
+ get uint16() {
94
+ return this.getValue(`getUint16`, 2);
95
+ }
96
+
100
97
  get uint24() {
101
98
  // Why does DataView not have a 24 bit value getters?
102
99
  return (this.uint8 << 16) + (this.uint8 << 8) + this.uint8;
103
100
  }
104
101
 
102
+ get int32() {
103
+ return this.getValue(`getInt32`, 4);
104
+ }
105
+
106
+ get uint32() {
107
+ return this.getValue(`getUint32`, 4);
108
+ }
109
+
110
+ get int64() {
111
+ return this.getValue(`getBigInt64`, 8);
112
+ }
113
+
114
+ get uint64() {
115
+ return this.getValue(`getBigUint64`, 8);
116
+ }
117
+
105
118
  get uint128() {
106
119
  // I have no idea why the variable uint128 was chosen over a
107
120
  // fixed-width uint32, but it was, and so we need to decode it.
@@ -135,7 +148,7 @@ class Parser {
135
148
 
136
149
  // "that weird datatype"
137
150
  get F2DOT14() {
138
- const bits = p.uint16;
151
+ const bits = this.uint16;
139
152
  const integer = [0, 1, -2, -1][bits >> 14];
140
153
  const fraction = bits & 0x3fff;
141
154
  return integer + fraction / 16384;
@@ -180,7 +193,7 @@ class ParsedData {
180
193
 
181
194
  load(struct) {
182
195
  Object.keys(struct).forEach((p) => {
183
- let props = Object.getOwnPropertyDescriptor(struct, p);
196
+ let props = Object.getOwnPropertyDescriptor(struct, p) ?? {};
184
197
  if (props.get) {
185
198
  this[p] = props.get.bind(this);
186
199
  } else if (props.value !== undefined) {
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "compilerOptions": {
3
+ "checkJs": true,
4
+ "noEmit": true,
5
+ "module": "ESNext",
6
+ "target": "ESNext"
7
+ },
8
+ "include": ["src/**/*.js"]
9
+ }
@@ -1,46 +0,0 @@
1
- /**
2
- * A shim for the Fetch API. If not defined, we assume we're running
3
- * in Node.js and shim the fetch function using the `fs` module.
4
- */
5
-
6
- let fetchFunction = globalThis.fetch;
7
-
8
- if (!fetchFunction) {
9
- let backlog = [];
10
-
11
- fetchFunction = globalThis.fetch = (...args) => {
12
- return new Promise((resolve, reject) => {
13
- backlog.push({ args, resolve, reject });
14
- });
15
- };
16
-
17
- import("fs")
18
- .then((fs) => {
19
- fetchFunction = globalThis.fetch = async function (path) {
20
- return new Promise((resolve, reject) => {
21
- fs.readFile(path, (err, data) => {
22
- if (err) return reject(err);
23
- resolve({
24
- ok: true,
25
- arrayBuffer: () => data.buffer,
26
- });
27
- });
28
- });
29
- };
30
-
31
- while (backlog.length) {
32
- let instruction = backlog.shift();
33
- fetchFunction(...instruction.args)
34
- .then((data) => instruction.resolve(data))
35
- .catch((err) => instruction.reject(err));
36
- }
37
- })
38
- .catch((err) => {
39
- console.error(err);
40
- throw new Error(
41
- `lib-font cannot run unless either the Fetch API or Node's filesystem module is available.`
42
- );
43
- });
44
- }
45
-
46
- export default "shim activated";