imagescript 3.12.1 → 3.13.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.
@@ -25,191 +25,6 @@ const token = (v) => {
25
25
  const n = 31 - Math.clz32(v), m = v - (1 << n);
26
26
  return SPLIT + ((n - SPLIT_EXPONENT) << (MSB + LSB)) + ((m >>> (n - MSB)) << LSB) + (m & ((1 << LSB) - 1));
27
27
  };
28
- const MAX_PREFIX_BITS = 15;
29
- function huffmanLengths(counts, limit) {
30
- const n = counts.length, lengths = new Uint8Array(n);
31
- const used = [];
32
- for (let i = 0; i < n; i++)
33
- if (counts[i] > 0)
34
- used.push(i);
35
- if (used.length <= 1) {
36
- if (used.length)
37
- lengths[used[0]] = 1;
38
- return lengths;
39
- }
40
- for (let floor = 1;; floor *= 2) {
41
- const m = used.length, parent = new Int32Array(2 * m), weights = [];
42
- for (let i = 0; i < m; i++)
43
- weights.push([Math.max(counts[used[i]], floor), i]);
44
- weights.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
45
- const internal = [];
46
- let li = 0, ii = 0, next = m;
47
- const pop = () => {
48
- if (ii >= internal.length || (li < weights.length && weights[li][0] <= internal[ii][0]))
49
- return weights[li++];
50
- return internal[ii++];
51
- };
52
- for (let k = 0; k < m - 1; k++) {
53
- const a = pop(), b = pop();
54
- parent[a[1]] = parent[b[1]] = next;
55
- internal.push([a[0] + b[0], next++]);
56
- }
57
- const depth = new Int32Array(2 * m);
58
- let max = 0;
59
- for (let node = next - 2; node >= 0; node--) {
60
- depth[node] = depth[parent[node]] + 1;
61
- if (node < m && depth[node] > max)
62
- max = depth[node];
63
- }
64
- if (max > limit)
65
- continue;
66
- for (let i = 0; i < m; i++)
67
- lengths[used[i]] = depth[i];
68
- return lengths;
69
- }
70
- }
71
- function canonicalCodes(lengths) {
72
- let maxLen = 0;
73
- for (const l of lengths)
74
- if (l > maxLen)
75
- maxLen = l;
76
- const count = new Int32Array(maxLen + 1), next = new Int32Array(maxLen + 2);
77
- for (const l of lengths)
78
- if (l)
79
- count[l]++;
80
- for (let len = 1, code = 0; len <= maxLen; len++) {
81
- code = (code + count[len - 1]) << 1;
82
- next[len] = code;
83
- }
84
- const codes = new Uint16Array(lengths.length);
85
- for (let s = 0; s < lengths.length; s++) {
86
- const len = lengths[s];
87
- if (!len)
88
- continue;
89
- const code = next[len]++;
90
- let rev = 0;
91
- for (let i = 0; i < len; i++)
92
- rev |= (code >> i & 1) << (len - 1 - i);
93
- codes[s] = rev;
94
- }
95
- return codes;
96
- }
97
- class PrefixCode {
98
- counts;
99
- lengths;
100
- codes;
101
- used;
102
- constructor(counts) {
103
- this.counts = counts;
104
- this.used = [];
105
- for (let i = 0; i < counts.length; i++)
106
- if (counts[i] > 0)
107
- this.used.push(i);
108
- if (this.used.length > 1 && this.used.length <= 4) {
109
- const syms = this.used.slice().sort((a, b) => counts[b] - counts[a] || a - b);
110
- const lens = syms.length === 2 ? [1, 1] : syms.length === 3 ? [1, 2, 2] : this.fourLengths(syms);
111
- this.used = syms;
112
- this.lengths = new Uint8Array(counts.length);
113
- syms.forEach((s, i) => this.lengths[s] = lens[i]);
114
- }
115
- else
116
- this.lengths = this.used.length > 1 ? huffmanLengths(counts, MAX_PREFIX_BITS) : new Uint8Array(counts.length);
117
- this.codes = canonicalCodes(this.lengths);
118
- }
119
- fourLengths(syms) {
120
- const c = syms.map(s => this.counts[s]);
121
- return c[0] + 2 * c[1] + 3 * (c[2] + c[3]) < 2 * (c[0] + c[1] + c[2] + c[3]) ? [1, 2, 3, 3] : [2, 2, 2, 2];
122
- }
123
- write(w) {
124
- const size = this.counts.length;
125
- if (size === 1)
126
- w.write(1, 0);
127
- else {
128
- const n = 31 - Math.clz32(size - 1);
129
- w.write(1, 1);
130
- w.write(4, n);
131
- w.write(n, size - 1 - (1 << n));
132
- }
133
- }
134
- writeCode(w) {
135
- const size = this.counts.length;
136
- if (size === 1)
137
- return;
138
- const used = this.used;
139
- if (used.length <= 4) {
140
- const bits = 32 - Math.clz32(size - 1);
141
- w.write(2, 1);
142
- w.write(2, Math.max(used.length, 1) - 1);
143
- for (const s of used.length ? used : [0])
144
- w.write(bits, s);
145
- if (used.length === 4)
146
- w.write(1, this.lengths[used[3]] === 3 ? 1 : 0);
147
- return;
148
- }
149
- let last = size;
150
- while (this.lengths[last - 1] === 0)
151
- last--;
152
- const syms = [], extra = [];
153
- let prevNonZero = 8;
154
- for (let i = 0; i < last;) {
155
- const l = this.lengths[i];
156
- let run = 1;
157
- while (i + run < last && this.lengths[i + run] === l)
158
- run++;
159
- i += run;
160
- if (l !== 0 && l !== prevNonZero) {
161
- syms.push(l);
162
- extra.push(0);
163
- run--;
164
- prevNonZero = l;
165
- }
166
- const rep = l === 0 ? 17 : 16, max = l === 0 ? 10 : 6;
167
- while (run > 0) {
168
- if (run < 3 || syms[syms.length - 1] === rep) {
169
- syms.push(l);
170
- extra.push(0);
171
- run--;
172
- continue;
173
- }
174
- const k = Math.min(run, max);
175
- syms.push(rep);
176
- extra.push(k - 3);
177
- run -= k;
178
- }
179
- }
180
- const clCounts = new Float64Array(18);
181
- for (const s of syms)
182
- clCounts[s]++;
183
- const clLengths = huffmanLengths(clCounts, 5);
184
- let distinct = 0;
185
- for (const l of clLengths)
186
- if (l)
187
- distinct++;
188
- const clCodes = canonicalCodes(clLengths);
189
- w.write(2, 0);
190
- const ORDER = [1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15];
191
- const FIXED_NBITS = [2, 4, 3, 2, 2, 4], FIXED_BITS = [0, 7, 3, 2, 1, 15];
192
- let space = 0;
193
- for (let i = 0; i < 18; i++) {
194
- const l = clLengths[ORDER[i]];
195
- w.write(FIXED_NBITS[l], FIXED_BITS[l]);
196
- if (l) {
197
- space += 32 >> l;
198
- if (distinct > 1 && space === 32)
199
- break;
200
- }
201
- }
202
- for (let i = 0; i < syms.length; i++) {
203
- const s = syms[i];
204
- if (distinct > 1)
205
- w.write(clLengths[s], clCodes[s]);
206
- if (s === 16)
207
- w.write(2, extra[i]);
208
- else if (s === 17)
209
- w.write(3, extra[i]);
210
- }
211
- }
212
- }
213
28
  const ANS_LOG_TAB_SIZE = 12, ANS_TAB_SIZE = 1 << ANS_LOG_TAB_SIZE, LOG_ALPHABET_SIZE = 8;
214
29
  const floorLog2 = (v) => 31 - Math.clz32(v);
215
30
  const precision = (logcount, shift) => Math.max(0, Math.min(logcount, shift - ((ANS_LOG_TAB_SIZE - logcount) >> 1)));
@@ -406,9 +221,8 @@ function cost(h, total) {
406
221
  export class EntropyCode {
407
222
  numContexts;
408
223
  ctxMap;
409
- prefix = null;
410
- ans = null;
411
- constructor(streams, numContexts, { maxClusters = 64, prefix = false } = {}) {
224
+ ans;
225
+ constructor(streams, numContexts, { maxClusters = 64 } = {}) {
412
226
  this.numContexts = numContexts;
413
227
  let alphabet = 1;
414
228
  for (const t of streams) {
@@ -432,49 +246,24 @@ export class EntropyCode {
432
246
  size--;
433
247
  return h.subarray(0, size);
434
248
  });
435
- if (prefix)
436
- this.prefix = clusters.map(h => new PrefixCode(h));
437
- else
438
- this.ans = clusters.map(h => new AnsCode(h));
249
+ this.ans = clusters.map(h => new AnsCode(h));
439
250
  }
440
251
  writeHeader(w) {
441
252
  w.write(1, 0);
442
- const n = (this.prefix ?? this.ans).length;
253
+ const n = this.ans.length;
443
254
  writeContextMap(w, this.ctxMap, n);
444
- w.write(1, this.prefix ? 1 : 0);
445
- if (!this.prefix)
446
- w.write(2, LOG_ALPHABET_SIZE - 5);
255
+ w.write(1, 0);
256
+ w.write(2, LOG_ALPHABET_SIZE - 5);
447
257
  for (let i = 0; i < n; i++) {
448
258
  w.write(4, SPLIT_EXPONENT);
449
259
  w.write(3, MSB);
450
260
  w.write(32 - Math.clz32(SPLIT_EXPONENT - MSB), LSB);
451
261
  }
452
- if (this.prefix) {
453
- for (const c of this.prefix)
454
- c.write(w);
455
- for (const c of this.prefix)
456
- c.writeCode(w);
457
- }
458
- else
459
- for (const c of this.ans)
460
- c.write(w);
262
+ for (const c of this.ans)
263
+ c.write(w);
461
264
  }
462
265
  writeTokens(w, t) {
463
266
  const map = this.ctxMap;
464
- if (this.prefix) {
465
- const codes = this.prefix;
466
- for (let i = 0; i < t.length; i++) {
467
- const code = codes[map[t.ctx[i]]], v = t.value[i];
468
- if (v < SPLIT) {
469
- w.write(code.lengths[v], code.codes[v]);
470
- continue;
471
- }
472
- const n = 31 - Math.clz32(v), nbits = n - MSB - LSB, tk = token(v);
473
- w.write(code.lengths[tk], code.codes[tk]);
474
- w.write(nbits, (v >>> LSB) & ((1 << nbits) - 1));
475
- }
476
- return;
477
- }
478
267
  const codes = this.ans, n = t.length, chunks = new Int32Array(n);
479
268
  let state = 0x130000;
480
269
  for (let i = n - 1; i >= 0; i--) {
@@ -58,7 +58,7 @@ class BoolDecoder {
58
58
  let i = 0;
59
59
  while ((i = tree[i + this.read(probs[offset + (i >> 1)])]) > 0)
60
60
  ;
61
- return -i;
61
+ return -i | 0;
62
62
  }
63
63
  get exhausted() {
64
64
  return this.pos > this.end + 2;
@@ -217,6 +217,7 @@ export function decode(data, maxPixels = MAX_PIXELS) {
217
217
  const an = mbx * 9;
218
218
  coeffs.fill(0);
219
219
  let nonZero = 0;
220
+ let ac = 0;
220
221
  if (!skip) {
221
222
  const q = h.quant[segment];
222
223
  if (hasY2) {
@@ -233,6 +234,8 @@ export function decode(data, maxPixels = MAX_PIXELS) {
233
234
  aboveNz[an + x] = leftNz[y] = n > first ? 1 : 0;
234
235
  if (n > first || coeffs[b * 16] !== 0)
235
236
  nonZero |= 1 << b;
237
+ if (n > 1)
238
+ ac |= 1 << b;
236
239
  }
237
240
  for (let b = 16; b < 24; b++) {
238
241
  const plane = b < 20 ? 4 : 6, x = b & 1, y = (b >> 1) & 1;
@@ -241,6 +244,8 @@ export function decode(data, maxPixels = MAX_PIXELS) {
241
244
  aboveNz[an + plane + x] = leftNz[plane + y] = n > 0 ? 1 : 0;
242
245
  if (n > 0)
243
246
  nonZero |= 1 << b;
247
+ if (n > 1)
248
+ ac |= 1 << b;
244
249
  }
245
250
  }
246
251
  else {
@@ -270,14 +275,14 @@ export function decode(data, maxPixels = MAX_PIXELS) {
270
275
  left[i] = Y[base + i * ys - 1];
271
276
  predictSubblock(bmodes[b], edge, left, Y, base, ys);
272
277
  if (nonZero >> b & 1)
273
- inverseDCT(coeffs, b * 16, Y, base, ys);
278
+ inverseDCT(coeffs, b * 16, Y, base, ys, ac >> b & 1);
274
279
  }
275
280
  }
276
281
  else {
277
282
  predictBlock(ymode, Y, yBase, ys, 16, mbx, mby);
278
283
  for (let b = 0; b < 16; b++)
279
284
  if (nonZero >> b & 1)
280
- inverseDCT(coeffs, b * 16, Y, yBase + (b >> 2) * 4 * ys + (b & 3) * 4, ys);
285
+ inverseDCT(coeffs, b * 16, Y, yBase + (b >> 2) * 4 * ys + (b & 3) * 4, ys, ac >> b & 1);
281
286
  }
282
287
  const cBase = (mby * 8 + 1) * cs + mbx * 8 + 1;
283
288
  predictBlock(uvmode, U, cBase, cs, 8, mbx, mby);
@@ -286,7 +291,7 @@ export function decode(data, maxPixels = MAX_PIXELS) {
286
291
  if (!(nonZero >> b & 1))
287
292
  continue;
288
293
  const plane = b < 20 ? U : V, i = b & 3;
289
- inverseDCT(coeffs, b * 16, plane, cBase + (i >> 1) * 4 * cs + (i & 1) * 4, cs);
294
+ inverseDCT(coeffs, b * 16, plane, cBase + (i >> 1) * 4 * cs + (i & 1) * 4, cs, ac >> b & 1);
290
295
  }
291
296
  }
292
297
  if (tokens.exhausted && d.exhausted)
@@ -366,7 +371,17 @@ function inverseWHT(c, offset, count) {
366
371
  c[b * 16] = out[b];
367
372
  }
368
373
  const tmp = new Int32Array(16);
369
- function inverseDCT(c, offset, dst, base, stride) {
374
+ function inverseDCT(c, offset, dst, base, stride, ac) {
375
+ if (!ac) {
376
+ const dc = c[offset] + 4 >> 3;
377
+ for (let o = base, end = base + 4 * stride; o < end; o += stride) {
378
+ dst[o] = clamp255(dst[o] + dc);
379
+ dst[o + 1] = clamp255(dst[o + 1] + dc);
380
+ dst[o + 2] = clamp255(dst[o + 2] + dc);
381
+ dst[o + 3] = clamp255(dst[o + 3] + dc);
382
+ }
383
+ return;
384
+ }
370
385
  for (let i = 0; i < 4; i++) {
371
386
  const i0 = c[offset + i], i4 = c[offset + 4 + i], i8 = c[offset + 8 + i], i12 = c[offset + 12 + i];
372
387
  const a1 = i0 + i8, b1 = i0 - i8;
@@ -413,16 +428,19 @@ function predictBlock(mode, p, base, stride, size, mbx, mby) {
413
428
  }
414
429
  const v = count === 0 ? 128 : sum + (1 << shift + count - 1) >> shift + count;
415
430
  for (let y = 0; y < size; y++)
416
- p.fill(v, base + y * stride, base + y * stride + size);
431
+ for (let row = base + y * stride, x = 0; x < size; x++)
432
+ p[row + x] = v;
417
433
  }
418
434
  else if (mode === V_PRED) {
419
435
  for (let y = 0; y < size; y++)
420
- p.copyWithin(base + y * stride, above, above + size);
436
+ for (let row = base + y * stride, x = 0; x < size; x++)
437
+ p[row + x] = p[above + x];
421
438
  }
422
439
  else if (mode === H_PRED) {
423
440
  for (let y = 0; y < size; y++) {
424
- const row = base + y * stride;
425
- p.fill(p[row - 1], row, row + size);
441
+ const row = base + y * stride, v = p[row - 1];
442
+ for (let x = 0; x < size; x++)
443
+ p[row + x] = v;
426
444
  }
427
445
  }
428
446
  else {
@@ -679,12 +697,17 @@ function toRGBA(Y, U, V, ys, cs, width, height) {
679
697
  }
680
698
  if (!(height & 1))
681
699
  upsamplePair(Y, U, V, yRow(height - 1), -1, cRow(lastChroma), cRow(lastChroma), out, (height - 1) * width, -1, width);
682
- if (!littleEndian)
683
- for (let i = 0; i < out.length; i++) {
684
- const w = out[i];
685
- out[i] = (w << 24 | (w & 0xff00) << 8 | w >>> 8 & 0xff00 | w >>> 24) >>> 0;
686
- }
687
- return new Uint8Array(out.buffer);
700
+ if (littleEndian)
701
+ return new Uint8Array(out.buffer);
702
+ const bytes = new Uint8Array(out.length * 4);
703
+ for (let i = 0, o = 0; i < out.length; i++, o += 4) {
704
+ const w = out[i];
705
+ bytes[o] = w & 0xff;
706
+ bytes[o + 1] = w >> 8 & 0xff;
707
+ bytes[o + 2] = w >> 16 & 0xff;
708
+ bytes[o + 3] = w >>> 24;
709
+ }
710
+ return bytes;
688
711
  }
689
712
  function upsamplePair(Y, U, V, topY, bottomY, topC, curC, out, topOut, bottomOut, width) {
690
713
  let tlu = U[topC], tlv = V[topC], lu = U[curC], lv = V[curC];
package/dist/image.d.ts CHANGED
@@ -54,7 +54,7 @@ export interface SharpenOptions {
54
54
  }
55
55
  /** Options of {@link Image.encode} */
56
56
  export interface PNGEncodeOptions {
57
- /** The compression level (0-9). Default: `1` */
57
+ /** The compression level (0-9), higher is smaller but slower. Default: `4` */
58
58
  compression?: number;
59
59
  /** Metadata to include, see {@link Metadata} */
60
60
  metadata?: Metadata;
@@ -131,8 +131,8 @@ export interface SVGOptions {
131
131
  * and `font-stretch` match best among them, by the names, weights and styles in the fonts (like browsers match
132
132
  * installed fonts). Generic families (`serif`, `sans-serif`, `monospace`, ...) are the first font that fits them,
133
133
  * other families and text without one get the first font's family; characters a face doesn't have come from the
134
- * other fonts. Fonts the SVG embeds with `@font-face` (in data: URIs, also WOFF) are used too, before these for
135
- * their family names. Without any fonts text isn't rendered. Default: `[]`
134
+ * other fonts (TrueType, OpenType or WOFF). Fonts the SVG embeds with `@font-face` (in data: URIs) are used too,
135
+ * before these for their family names. Without any fonts text isn't rendered. Default: `[]`
136
136
  */
137
137
  fonts?: BinaryData[];
138
138
  }
@@ -811,13 +811,13 @@ export declare class Image {
811
811
  encode(options?: PNGEncodeOptions): Promise<Uint8Array>;
812
812
  /**
813
813
  * Encodes the image into a PNG
814
- * @param compression The compression level (0-9). Default: `1`
814
+ * @param compression The compression level (0-9). Default: `4`
815
815
  * @param metadata Metadata to include, see {@link Metadata}
816
816
  * @return The encoded data
817
817
  */
818
818
  encode(compression?: number, metadata?: Metadata): Promise<Uint8Array>;
819
819
  /**
820
- * Encodes the image into a PNG with compression level 1
820
+ * Encodes the image into a PNG with compression level 4
821
821
  * @param metadata Metadata to include, see {@link Metadata}
822
822
  * @return The encoded data
823
823
  */
@@ -828,7 +828,7 @@ export declare class Image {
828
828
  encodePNG(options?: PNGEncodeOptions): Promise<Uint8Array>;
829
829
  /**
830
830
  * Encodes the image into a PNG, an alias of {@link Image.encode}
831
- * @param compression The compression level (0-9). Default: `1`
831
+ * @param compression The compression level (0-9). Default: `4`
832
832
  * @param metadata Metadata to include, see {@link Metadata}
833
833
  */
834
834
  encodePNG(compression?: number, metadata?: Metadata): Promise<Uint8Array>;
@@ -929,10 +929,11 @@ export declare class Image {
929
929
  static renderSVG(svg: string | BinaryData, size?: number, mode?: SVGScaleMode): Promise<Image>;
930
930
  /**
931
931
  * Creates a new image containing the rendered text.
932
- * @param font TrueType (ttf/ttc) or OpenType (otf) font buffer to use, or several by priority: every character
933
- * comes from the first font that has it (e.g. `[text, emoji]`), the first one sets the line height. Color
934
- * glyphs (COLR layers and paints, CBDT and sbix bitmaps, as in emoji fonts) keep their colors. Text in any script
935
- * is shaped like browsers do (right to left and bidirectional text, Arabic joining, Indic conjuncts, marks)
932
+ * @param font TrueType (ttf/ttc), OpenType (otf) or WOFF font buffer to use, or several by priority: every
933
+ * character comes from the first font that has it (e.g. `[text, emoji]`), the first one sets the line height.
934
+ * Color glyphs (COLR layers and paints, CBDT and sbix bitmaps, as in emoji fonts) keep their colors. Text in any
935
+ * script is shaped like browsers do (right to left and bidirectional text, Arabic joining, Indic conjuncts,
936
+ * marks)
936
937
  * @param size Font size in pixels (per em)
937
938
  * @param text Text to render
938
939
  * @param options Color, layout and style, see {@link TextOptions}; the image grows by the outline width on every
@@ -942,10 +943,11 @@ export declare class Image {
942
943
  static renderText(font: BinaryData | BinaryData[], size: number, text: string, options?: TextOptions): Promise<Image>;
943
944
  /**
944
945
  * Creates a new image containing the rendered text.
945
- * @param font TrueType (ttf/ttc) or OpenType (otf) font buffer to use, or several by priority: every character
946
- * comes from the first font that has it (e.g. `[text, emoji]`), the first one sets the line height. Color
947
- * glyphs (COLR layers and paints, CBDT and sbix bitmaps, as in emoji fonts) keep their colors. Text in any script
948
- * is shaped like browsers do (right to left and bidirectional text, Arabic joining, Indic conjuncts, marks)
946
+ * @param font TrueType (ttf/ttc), OpenType (otf) or WOFF font buffer to use, or several by priority: every
947
+ * character comes from the first font that has it (e.g. `[text, emoji]`), the first one sets the line height.
948
+ * Color glyphs (COLR layers and paints, CBDT and sbix bitmaps, as in emoji fonts) keep their colors. Text in any
949
+ * script is shaped like browsers do (right to left and bidirectional text, Arabic joining, Indic conjuncts,
950
+ * marks)
949
951
  * @param size Font size in pixels (per em)
950
952
  * @param text Text to render
951
953
  * @param [color=0xffffffff] Text color to use
package/dist/image.js CHANGED
@@ -1013,12 +1013,12 @@ export class Image {
1013
1013
  }
1014
1014
  return this.__apply__(r);
1015
1015
  }
1016
- async encode(compressionOrOptions = 1, metadataArgument = {}) {
1017
- let compression = 1, metadata = metadataArgument;
1016
+ async encode(compressionOrOptions = 4, metadataArgument = {}) {
1017
+ let compression = 4, metadata = metadataArgument;
1018
1018
  if (!isOptions(compressionOrOptions))
1019
1019
  compression = compressionOrOptions;
1020
1020
  else if ('compression' in compressionOrOptions || 'metadata' in compressionOrOptions)
1021
- ({ compression = 1, metadata = {} } = compressionOrOptions);
1021
+ ({ compression = 4, metadata = {} } = compressionOrOptions);
1022
1022
  else
1023
1023
  metadata = compressionOrOptions;
1024
1024
  const level = Math.min(9, Math.max(0, ~~finite(compression, 'compression level')));
@@ -1,3 +1,4 @@
1
+ import { decompress } from "../codecs/zlib.js";
1
2
  import { Gvar, ItemVariationStore, cvarDeltas, deltaSetIndexMap, normalize, parseAvar, parseFvar } from "./variations.js";
2
3
  const MAX_COMMANDS = 100000;
3
4
  export class Font {
@@ -22,14 +23,17 @@ export class Font {
22
23
  variationTables = null;
23
24
  instances = null;
24
25
  constructor(data) {
26
+ const woff = data.length >= 4 ? (data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]) >>> 0 : 0;
27
+ if (woff === 0x774f4646)
28
+ data = fromWOFF(data);
29
+ else if (woff === 0x774f4632)
30
+ throw new Error('Font: WOFF2 fonts are not supported, only WOFF');
25
31
  this.data = data;
26
32
  this.view = new DataView(data.buffer, data.byteOffset, data.byteLength);
27
33
  let offset = 0;
28
34
  const signature = this.u32(0);
29
35
  if (signature === 0x74746366)
30
36
  offset = this.u32(12);
31
- else if (signature === 0x774f4646 || signature === 0x774f4632)
32
- throw new Error('Font: WOFF fonts are not supported');
33
37
  const version = this.u32(offset);
34
38
  if (version !== 0x00010000 && version !== 0x4f54544f && version !== 0x74727565)
35
39
  throw new Error('Font: invalid font file');
@@ -989,3 +993,30 @@ function cffCharString(cff, glyph) {
989
993
  path.push(['Z']);
990
994
  return path;
991
995
  }
996
+ function fromWOFF(data) {
997
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
998
+ const count = view.getUint16(12);
999
+ const tables = [];
1000
+ for (let i = 0; i < count; i++) {
1001
+ const e = 44 + i * 20, offset = view.getUint32(e + 4), compressed = view.getUint32(e + 8), length = view.getUint32(e + 12);
1002
+ const raw = data.subarray(offset, offset + compressed);
1003
+ tables.push({ tag: view.getUint32(e), checksum: view.getUint32(e + 16), data: compressed < length ? decompress(raw, length).subarray(0, length) : raw });
1004
+ }
1005
+ const header = 12 + count * 16;
1006
+ let size = header;
1007
+ for (const t of tables)
1008
+ size += t.data.length + 3 & ~3;
1009
+ const out = new Uint8Array(size), o = new DataView(out.buffer);
1010
+ o.setUint32(0, view.getUint32(4));
1011
+ o.setUint16(4, count);
1012
+ let position = header;
1013
+ tables.forEach((t, i) => {
1014
+ o.setUint32(12 + i * 16, t.tag);
1015
+ o.setUint32(16 + i * 16, t.checksum);
1016
+ o.setUint32(20 + i * 16, position);
1017
+ o.setUint32(24 + i * 16, t.data.length);
1018
+ out.set(t.data, position);
1019
+ position += t.data.length + 3 & ~3;
1020
+ });
1021
+ return out;
1022
+ }
@@ -108,13 +108,13 @@ export class Hinter {
108
108
  this.fpgm = table('fpgm');
109
109
  this.prep = table('prep');
110
110
  const cvt = table('cvt ');
111
- this.cvt = new Int16Array(cvt.length >> 1);
111
+ this.cvt = new Int32Array(cvt.length >> 1);
112
112
  for (let i = 0; i < this.cvt.length; i++)
113
- this.cvt[i] = font.i16(font.tables['cvt '].offset + i * 2);
113
+ this.cvt[i] = font.i16(font.tables['cvt '].offset + i * 2) * 64;
114
114
  const deltas = font.cvtDeltas(this.cvt.length);
115
115
  if (deltas)
116
116
  for (let i = 0; i < this.cvt.length; i++)
117
- this.cvt[i] = Math.round(this.cvt[i] + deltas[i]);
117
+ this.cvt[i] += Math.floor(deltas[i] * 64 + 0.5);
118
118
  const maxp = font.tables.maxp.offset;
119
119
  const version = font.u32(maxp);
120
120
  this.maxTwilight = version >= 0x10000 ? font.u16(maxp + 16) : 0;
@@ -132,7 +132,7 @@ export class Hinter {
132
132
  const scale = scaleFixed / 65536;
133
133
  size = {
134
134
  ppem, scale, scaleFixed,
135
- cvt: Float64Array.from(this.cvt, v => mulFix(v, scaleFixed)),
135
+ cvt: Float64Array.from(this.cvt, v => mulFix(v, scaleFixed >> 6)),
136
136
  storage: new Int32Array(Math.max(this.maxStorage, 64)),
137
137
  state: defaultState(),
138
138
  twilight: makeZone(Math.max(this.maxTwilight, 1)),
@@ -19,7 +19,6 @@ import { fontFace, layOutText, parseFontFamilies, parseVariationSettings, pathLe
19
19
  import { rasterizeGlyph } from "./text.js";
20
20
  import { renderPaintGlyph } from "./colr.js";
21
21
  import { decodeSync as decodePNG } from "../codecs/png.js";
22
- import { decompress } from "../codecs/zlib.js";
23
22
  const UNSUPPORTED = new Set(['filter', 'pattern', 'marker', 'foreignObject', 'script', 'style', 'title', 'desc', 'metadata',
24
23
  'defs', 'clipPath', 'mask', 'linearGradient', 'radialGradient', 'stop', 'symbol', 'font', 'font-face', 'animate', 'animateTransform', 'animateMotion', 'set']);
25
24
  const INHERITED = new Set(['fill', 'fill-opacity', 'fill-rule', 'stroke', 'stroke-width', 'stroke-opacity', 'stroke-linecap', 'stroke-linejoin',
@@ -3444,15 +3443,7 @@ function embeddedFonts(css) {
3444
3443
  const uri = dataURI(url);
3445
3444
  if (!uri)
3446
3445
  continue;
3447
- let data = uri.data;
3448
- try {
3449
- if (data[0] === 0x77 && data[1] === 0x4f && data[2] === 0x46 && data[3] === 0x46)
3450
- data = fromWOFF(data);
3451
- }
3452
- catch {
3453
- data = null;
3454
- }
3455
- const face = data && fontFace(data, { family, weight, style });
3446
+ const face = fontFace(uri.data, { family, weight, style });
3456
3447
  if (face) {
3457
3448
  faces.push(face);
3458
3449
  break;
@@ -3463,33 +3454,6 @@ function embeddedFonts(css) {
3463
3454
  }
3464
3455
  return faces;
3465
3456
  }
3466
- function fromWOFF(data) {
3467
- const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
3468
- const count = view.getUint16(12);
3469
- const tables = [];
3470
- for (let i = 0; i < count; i++) {
3471
- const e = 44 + i * 20, offset = view.getUint32(e + 4), compressed = view.getUint32(e + 8), length = view.getUint32(e + 12);
3472
- const raw = data.subarray(offset, offset + compressed);
3473
- tables.push({ tag: view.getUint32(e), checksum: view.getUint32(e + 16), data: compressed < length ? decompress(raw, length).subarray(0, length) : raw });
3474
- }
3475
- const header = 12 + count * 16;
3476
- let size = header;
3477
- for (const t of tables)
3478
- size += t.data.length + 3 & ~3;
3479
- const out = new Uint8Array(size), o = new DataView(out.buffer);
3480
- o.setUint32(0, view.getUint32(4));
3481
- o.setUint16(4, count);
3482
- let position = header;
3483
- tables.forEach((t, i) => {
3484
- o.setUint32(12 + i * 16, t.tag);
3485
- o.setUint32(16 + i * 16, t.checksum);
3486
- o.setUint32(20 + i * 16, position);
3487
- o.setUint32(24 + i * 16, t.data.length);
3488
- out.set(t.data, position);
3489
- position += t.data.length + 3 & ~3;
3490
- });
3491
- return out;
3492
- }
3493
3457
  function transformOf(style, viewport, fallback) {
3494
3458
  return originTransform(style.transform, style['transform-origin'] ?? fallback, viewport, parseNumber(style['font-size'], 16));
3495
3459
  }
@@ -1,4 +1,7 @@
1
- export const littleEndian = new Uint8Array(new Uint32Array([1]).buffer)[0] === 1;
1
+ export let littleEndian = new Uint8Array(new Uint32Array([1]).buffer)[0] === 1;
2
+ export function useLittleEndian(value) {
3
+ littleEndian = value;
4
+ }
2
5
  const words = (bitmap) => new Int32Array(bitmap.buffer, bitmap.byteOffset, bitmap.length >> 2);
3
6
  export function isOpaque(bitmap) {
4
7
  const pixels = words(bitmap);
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.12.1';
1
+ export const VERSION = '3.13.0';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.12.1",
4
- "description": "zero-dependency javascript image manipulation",
3
+ "version": "3.13.0",
4
+ "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "exports": {
@@ -39,6 +39,14 @@
39
39
  "image",
40
40
  "image processing",
41
41
  "image manipulation",
42
+ "typescript",
43
+ "zero-dependency",
44
+ "browser",
45
+ "deno",
46
+ "bun",
47
+ "edge",
48
+ "cloudflare-workers",
49
+ "serverless",
42
50
  "png",
43
51
  "jpeg",
44
52
  "jpg",