harmonink 0.1.0 → 0.1.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.
package/README.md CHANGED
@@ -105,6 +105,8 @@ you are making and is not a property of the font file.
105
105
  size, width, slant) stays at its default. The `wght` axis range is reported so
106
106
  weights resolve exactly rather than snapping to 100s.
107
107
  - Requires Latin lowercase. Throws if `x` has no outline.
108
+ - A damaged or cut-short file throws `corrupt font file — ` and what was wrong
109
+ with it. Files built to attack a parser are not defended against.
108
110
  - Hand-drawn faces with regular letterforms (Comic Neue, Itim) measure as sans.
109
111
  Category is a geometric judgement, not an editorial one — pass `category`
110
112
  explicitly where that matters.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "harmonink",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Tiny font metric normalizer — makes any font render at a consistent, readable x-height",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -20,7 +20,7 @@
20
20
  "README.md"
21
21
  ],
22
22
  "engines": {
23
- "node": ">=18"
23
+ "node": ">=22"
24
24
  },
25
25
  "scripts": {
26
26
  "test": "node test/run.js",
package/src/glyf.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Reader } from './sfnt.js';
1
+ import { Reader, corrupt } from './sfnt.js';
2
2
 
3
3
  function sign(f, v) { return (f & 1) ? v : -v; }
4
4
  function f2dot14(r) { return r.i16() / 16384; }
@@ -142,6 +142,7 @@ export function decodeWoff2Glyf(glyfTable) {
142
142
  let total = 0;
143
143
  for (let i = 0; i < nc; i++) { counts[i] = npR.u255(); total += counts[i]; }
144
144
  const fslice = flags.subarray(flagPos, flagPos + total);
145
+ if (fslice.length < total) throw corrupt('a table ends before its data');
145
146
  flagPos += total;
146
147
  const pts = decodeTriplets(fslice, glR, total);
147
148
  glR.u255(); // instructionLength
@@ -171,9 +172,11 @@ export function parseSfntGlyf(glyf, loca, indexToLocFormat, numGlyphs) {
171
172
  out[g] = { composite: true, contours: [], bbox, comps };
172
173
  continue;
173
174
  }
175
+ // nothing follows the header of a glyph with no contours, so there is no instruction length to read
176
+ if (nc === 0) { out[g] = { composite: false, contours: [], bbox }; continue; }
174
177
  const ends = new Array(nc);
175
178
  for (let i = 0; i < nc; i++) ends[i] = r.u16();
176
- const total = nc ? ends[nc - 1] + 1 : 0;
179
+ const total = ends[nc - 1] + 1;
177
180
  const instrLen = r.u16(); r.p += instrLen; // read first: += would use the stale p
178
181
  const flags = new Array(total);
179
182
  for (let i = 0; i < total;) {
@@ -196,6 +199,8 @@ export function parseSfntGlyf(glyf, loca, indexToLocFormat, numGlyphs) {
196
199
  }
197
200
  const contours = []; let s = 0;
198
201
  for (const e of ends) {
202
+ // ends that go backwards would build the same points again for every contour, without limit
203
+ if (e + 1 < s) throw corrupt("a glyph's contours are out of order");
199
204
  const c = [];
200
205
  for (let i = s; i <= e; i++) c.push({ x: xs[i], y: ys[i], on: !!(flags[i] & 1) });
201
206
  contours.push(c); s = e + 1;
package/src/measure.js CHANGED
@@ -1,4 +1,4 @@
1
- import { readTables, Reader } from './sfnt.js';
1
+ import { readTables, Reader, corrupt } from './sfnt.js';
2
2
  import { decodeWoff2Glyf, parseSfntGlyf, resolveComposites } from './glyf.js';
3
3
  import { pathOf, bounds, crossingsAt } from './outline.js';
4
4
  import { instantiate } from './gvar.js';
@@ -34,7 +34,7 @@ function parseCmap(t) {
34
34
  if (rangeOff[i] === 0) g = (c + delta[i]) & 0xffff;
35
35
  else {
36
36
  const gi = rangeOffPos[i] + rangeOff[i] + (c - start[i]) * 2;
37
- g = (t[gi] << 8) | t[gi + 1];
37
+ g = new Reader(t, gi).u16();
38
38
  if (g) g = (g + delta[i]) & 0xffff;
39
39
  }
40
40
  if (g) map.set(c, g);
@@ -45,6 +45,7 @@ function parseCmap(t) {
45
45
  const groups = s.u32();
46
46
  for (let i = 0; i < groups; i++) {
47
47
  const a = s.u32(), b = s.u32(), g = s.u32();
48
+ if (b > 0x10ffff) throw corrupt('a cmap range runs past the end of Unicode');
48
49
  for (let c = a; c <= b; c++) map.set(c, g + (c - a));
49
50
  }
50
51
  } else if (fmt === 6) {
@@ -70,8 +71,10 @@ function parseName(t) {
70
71
  const pid = r.u16(); r.p += 4; // encoding and language ids
71
72
  const nid = r.u16(), len = r.u16(), off = r.u16();
72
73
  if (nid !== 1 && nid !== 16) continue;
74
+ const wide = pid === 3 || pid === 0;
75
+ if (storage + off + len > t.length || (wide && len % 2)) throw corrupt('a family name is damaged');
73
76
  const raw = t.subarray(storage + off, storage + off + len);
74
- const s = pid === 3 || pid === 0 ? Buffer.from(raw).swap16().toString('utf16le') : raw.toString('latin1');
77
+ const s = wide ? Buffer.from(raw).swap16().toString('utf16le') : raw.toString('latin1');
75
78
  if (nid === 16) pref = s; else if (!family) family = s;
76
79
  }
77
80
 
@@ -103,6 +106,8 @@ export function measure(buf, opts = {}) {
103
106
  if (tables['CFF '] || tables['CFF2']) throw new Error('CFF/OTF outlines are not supported yet — use a TrueType-flavoured file');
104
107
  if (!tables.glyf) throw new Error('no glyf table — bitmap or CFF font');
105
108
  if (tables.__t_hmtx) throw new Error('woff2 transformed hmtx is not supported');
109
+ for (const tag of ['head', 'maxp', 'hhea', 'hmtx', 'loca', 'cmap'])
110
+ if (!tables[tag]) throw corrupt(`the ${tag} table is missing`);
106
111
 
107
112
  const head = new Reader(tables.head, 18);
108
113
  const unitsPerEm = head.u16();
@@ -121,7 +126,7 @@ export function measure(buf, opts = {}) {
121
126
 
122
127
  const advance = g => {
123
128
  const i = Math.min(g, numHMetrics - 1);
124
- return ((tables.hmtx[i * 4] << 8) | tables.hmtx[i * 4 + 1]) + (vary.advDelta?.[g] ?? 0);
129
+ return new Reader(tables.hmtx, i * 4).u16() + (vary.advDelta?.[g] ?? 0);
125
130
  };
126
131
 
127
132
  const cmap = parseCmap(tables.cmap);
package/src/sfnt.js CHANGED
@@ -2,22 +2,27 @@ import { brotliDecompressSync, inflateSync } from 'node:zlib';
2
2
 
3
3
  const KNOWN = ["cmap","head","hhea","hmtx","maxp","name","OS/2","post","cvt ","fpgm","glyf","loca","prep","CFF ","VORG","EBDT","EBLC","gasp","hdmx","kern","LTSH","PCLT","VDMX","vhea","vmtx","BASE","GDEF","GPOS","GSUB","EBSC","JSTF","MATH","CBDT","CBLC","COLR","CPAL","SVG ","sbix","acnt","avar","bdat","bloc","bsln","cvar","fdsc","feat","fmtx","fvar","gvar","hsty","just","lcar","mort","morx","opbd","prop","trak","Zapf","Silf","Glat","Gloc","Feat","Sill"];
4
4
 
5
+ // what a file throws when its tables do not hold what they say they do
6
+ export const corrupt = what => new Error(`corrupt font file — ${what}`);
7
+
5
8
  export class Reader {
6
9
  constructor(b, p = 0) { this.b = b; this.p = p; }
7
- u8() { return this.b[this.p++]; }
10
+ // a typed array answers a read past its end with undefined, so every read is checked here
11
+ need(n) { if (!(this.p >= 0 && this.p + n <= this.b.length)) throw corrupt('a table ends before its data'); }
12
+ u8() { this.need(1); return this.b[this.p++]; }
8
13
  i8() { const v = this.u8(); return v > 127 ? v - 256 : v; }
9
- u16() { const v = (this.b[this.p] << 8) | this.b[this.p + 1]; this.p += 2; return v; }
14
+ u16() { this.need(2); const v = (this.b[this.p] << 8) | this.b[this.p + 1]; this.p += 2; return v; }
10
15
  i16() { const v = this.u16(); return v & 0x8000 ? v - 0x10000 : v; }
11
- u32() { const v = ((this.b[this.p] << 24) | (this.b[this.p+1] << 16) | (this.b[this.p+2] << 8) | this.b[this.p+3]) >>> 0; this.p += 4; return v; }
12
- tag() { const v = String.fromCharCode(this.b[this.p], this.b[this.p+1], this.b[this.p+2], this.b[this.p+3]); this.p += 4; return v; }
16
+ u32() { this.need(4); const v = ((this.b[this.p] << 24) | (this.b[this.p+1] << 16) | (this.b[this.p+2] << 8) | this.b[this.p+3]) >>> 0; this.p += 4; return v; }
17
+ tag() { this.need(4); const v = String.fromCharCode(this.b[this.p], this.b[this.p+1], this.b[this.p+2], this.b[this.p+3]); this.p += 4; return v; }
13
18
  base128() {
14
19
  let v = 0;
15
20
  for (let i = 0; i < 5; i++) {
16
- const x = this.b[this.p++];
21
+ const x = this.u8();
17
22
  v = (v << 7) | (x & 0x7f);
18
23
  if (!(x & 0x80)) return v;
19
24
  }
20
- throw new Error('bad base128');
25
+ throw corrupt('a table length is malformed');
21
26
  }
22
27
  u255() {
23
28
  const c = this.u8();
@@ -28,6 +33,9 @@ export class Reader {
28
33
  }
29
34
  }
30
35
 
36
+ // zlib's own messages say nothing about the font
37
+ const unpack = (fn, raw) => { try { return fn(raw); } catch { throw corrupt('its compressed data does not unpack'); } };
38
+
31
39
  function sfntTables(b) {
32
40
  const r = new Reader(b, 4);
33
41
  const n = r.u16();
@@ -50,7 +58,7 @@ function woff1Tables(b) {
50
58
  const tag = r.tag();
51
59
  const off = r.u32(), comp = r.u32(), orig = r.u32(); r.u32();
52
60
  const raw = b.subarray(off, off + comp);
53
- t[tag] = comp === orig ? raw : inflateSync(raw);
61
+ t[tag] = comp === orig ? raw : unpack(inflateSync, raw);
54
62
  }
55
63
  return t;
56
64
  }
@@ -74,7 +82,7 @@ function woff2Tables(b) {
74
82
  const transformLength = transformed ? r.base128() : origLength;
75
83
  dir.push({ tag, origLength, transformLength, transformed });
76
84
  }
77
- const data = brotliDecompressSync(b.subarray(r.p, r.p + totalCompressed));
85
+ const data = unpack(brotliDecompressSync, b.subarray(r.p, r.p + totalCompressed));
78
86
  const t = {};
79
87
  let off = 0;
80
88
  for (const d of dir) {
@@ -87,7 +95,7 @@ function woff2Tables(b) {
87
95
  }
88
96
 
89
97
  export function readTables(buf) {
90
- const magic = buf.readUInt32BE(0);
98
+ const magic = buf.length < 4 ? 0 : buf.readUInt32BE(0);
91
99
  if (magic === 0x774f4632) return { tables: woff2Tables(buf), container: 'woff2' };
92
100
  if (magic === 0x774f4646) return { tables: woff1Tables(buf), container: 'woff' };
93
101
  if (magic === 0x00010000 || magic === 0x74727565 || magic === 0x4f54544f)