imagescript 3.7.0 → 3.8.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.
@@ -90,9 +90,9 @@ function glyphOf(f, index, size) {
90
90
  f.glyphs.set(index, g);
91
91
  return g;
92
92
  }
93
- export async function render(fonts, size, text, layout, hinting = false, foreground = 0xffffffff) {
93
+ export async function render(fonts, size, text, layout, hinting = false, foreground = 0xffffffff, features = []) {
94
94
  try {
95
- return await layOut(fonts, size, text, layout, hinting, foreground);
95
+ return await layOut(fonts, size, text, layout, hinting, foreground, features);
96
96
  }
97
97
  catch (err) {
98
98
  if (err instanceof RangeError && !(err instanceof TooLarge))
@@ -103,7 +103,7 @@ export async function render(fonts, size, text, layout, hinting = false, foregro
103
103
  const MAX_PIXELS = 1 << 28;
104
104
  class TooLarge extends RangeError {
105
105
  }
106
- async function layOut(fonts, size, text, layout, hinting, foreground) {
106
+ async function layOut(fonts, size, text, layout, hinting, foreground, features) {
107
107
  const sized = [];
108
108
  const font = (i) => sized[i] ??= sizedFont(fonts[i], size, hinting);
109
109
  const primary = font(0);
@@ -112,7 +112,7 @@ async function layOut(fonts, size, text, layout, hinting, foreground) {
112
112
  const paragraphs = layout.wrapHardBreaks ? text.split(/\r\n|[\r\n\u0085\u2028\u2029]/) : [text.replace(/\r\n|[\r\n\u0085\u2028\u2029]/g, '')];
113
113
  const lines = [];
114
114
  for (const paragraph of paragraphs)
115
- lines.push(...layOutParagraph(analyze(paragraph, fonts.length, font), layout, font, size));
115
+ lines.push(...layOutParagraph(analyze(paragraph, fonts.length, font), layout, font, size, features));
116
116
  let visible = lines;
117
117
  if (isFinite(layout.maxHeight)) {
118
118
  const count = Math.max(1, Math.floor(layout.maxHeight / lineHeight));
@@ -212,7 +212,7 @@ function analyze(text, count, font) {
212
212
  };
213
213
  }
214
214
  const CONTEXT = 5;
215
- function shapeRange(p, start, end, levels, font, size) {
215
+ function shapeRange(p, start, end, levels, font, size, features) {
216
216
  const runs = [];
217
217
  let lastLevel = p.bidi.level;
218
218
  const levelAt = (i) => levels[i] >= 0 ? lastLevel = levels[i] : lastLevel;
@@ -250,7 +250,7 @@ function shapeRange(p, start, end, levels, font, size) {
250
250
  const f = font(p.fontOf[rs]);
251
251
  const before = p.codepoints.slice(Math.max(0, rs - CONTEXT), rs).reverse(), after = p.codepoints.slice(re, re + CONTEXT);
252
252
  const clusters = Array.from({ length: re - rs }, (_, k) => rs + k);
253
- const shaped = shapeRun(f.shaping, p.codepoints.slice(rs, re), p.scripts[rs], level & 1 ? 5 : 4, clusters, { before, after });
253
+ const shaped = shapeRun(f.shaping, p.codepoints.slice(rs, re), p.scripts[rs], level & 1 ? 5 : 4, clusters, { before, after }, features);
254
254
  for (const info of shaped) {
255
255
  const g = glyphOf(f, info.codepoint, size);
256
256
  let advance;
@@ -271,11 +271,11 @@ function shapeRange(p, start, end, levels, font, size) {
271
271
  return { glyphs, width: x, advances };
272
272
  }
273
273
  const isSpace = (codepoint) => codepoint === 0x20 || codepoint === 0x09 || codepoint === 0x3000 || codepoint >= 0x2000 && codepoint <= 0x200a;
274
- function layOutParagraph(p, layout, font, size) {
274
+ function layOutParagraph(p, layout, font, size, features) {
275
275
  const n = p.codepoints.length;
276
276
  if (!n)
277
277
  return [{ glyphs: [], extent: 0 }];
278
- const whole = shapeRange(p, 0, n, p.bidi.levels, font, size);
278
+ const whole = shapeRange(p, 0, n, p.bidi.levels, font, size, features);
279
279
  const width = new Float64Array(n), starts = new Uint8Array(n + 1);
280
280
  starts[n] = 1;
281
281
  for (const [cluster, advance] of whole.advances) {
@@ -321,7 +321,7 @@ function layOutParagraph(p, layout, font, size) {
321
321
  const levels = lineLevels(p.bidi, start, end);
322
322
  const shifted = new Int8Array(n);
323
323
  shifted.set(levels, start);
324
- const { glyphs } = ranges.length === 1 ? whole : shapeRange(p, start, end, shifted, font, size);
324
+ const { glyphs } = ranges.length === 1 ? whole : shapeRange(p, start, end, shifted, font, size, features);
325
325
  let extent = 0;
326
326
  for (const { x, dx, glyph } of glyphs)
327
327
  if (glyph.ink)
@@ -1,105 +1,184 @@
1
1
  const ALPHA = new Int32Array(new Uint8Array([0, 0, 0, 255]).buffer)[0];
2
2
  const ALPHA_COST = 4 / 3;
3
- export function optimize(frames, width, height, { replace, even, alpha, palette, cover }) {
4
- const size = width * height * 4;
5
- const canvas = new Uint8Array(size), canvas32 = new Int32Array(canvas.buffer);
6
- const shown = new Uint8Array(size), shown32 = new Int32Array(shown.buffer);
7
- const output = [];
8
- const state = { canvas32, shown32, width, replace };
9
- let opaque = true, denseTotal = 0, chosenTotal = 0;
10
- const add = (duration) => {
3
+ export class Optimizer {
4
+ width;
5
+ height;
6
+ options;
7
+ canvas;
8
+ shown;
9
+ state;
10
+ final = [];
11
+ last = null;
12
+ count = 0;
13
+ cleared;
14
+ opaque = true;
15
+ denseTotal = 0;
16
+ chosenTotal = 0;
17
+ constructor(width, height, options) {
18
+ this.width = width;
19
+ this.height = height;
20
+ this.options = options;
21
+ const size = width * height * 4;
22
+ this.canvas = new Uint8Array(size);
23
+ this.shown = new Uint8Array(size);
24
+ this.state = { canvas32: new Int32Array(this.canvas.buffer), shown32: new Int32Array(this.shown.buffer), width, replace: options.replace, tolerance: options.tolerance };
25
+ this.cleared = { x: 0, y: 0, width, height };
26
+ }
27
+ push(f) {
28
+ const { canvas, width, height } = this;
29
+ const saved = f.dispose === 2 ? canvas.slice() : null;
30
+ const area = clip(f, width, height);
31
+ if (this.cleared && contains(this.cleared, area))
32
+ copy(canvas, width, f, area);
33
+ else
34
+ draw(canvas, width, height, f);
35
+ this.cleared = null;
36
+ this.add(f.duration);
37
+ if (f.dispose === 1) {
38
+ clear(canvas, width, area);
39
+ this.cleared = area;
40
+ }
41
+ else if (saved)
42
+ canvas.set(saved);
43
+ }
44
+ take() {
45
+ return this.final.splice(0);
46
+ }
47
+ end() {
48
+ const rest = this.take();
49
+ if (this.last)
50
+ rest.push(this.last);
51
+ this.last = null;
52
+ return rest;
53
+ }
54
+ output(frame) {
55
+ if (this.last)
56
+ this.final.push(this.last);
57
+ this.last = frame;
58
+ this.count++;
59
+ }
60
+ add(duration) {
61
+ const { width, height, canvas, shown, state } = this;
62
+ const { replace, even, alpha, palette, tolerance } = this.options;
11
63
  let changed = bounds(state, { x: 0, y: 0, width, height });
12
64
  if (!changed) {
13
- if (output.length)
14
- output[output.length - 1].duration += duration;
65
+ if (this.last)
66
+ this.last.duration += duration;
15
67
  else {
16
- output.push(empty(duration));
17
- opaque = false;
68
+ this.output(empty(duration));
69
+ this.opaque = false;
18
70
  }
19
71
  return;
20
72
  }
21
73
  if (even)
22
74
  changed = evenArea(changed);
23
- let area = sparse(state, changed);
75
+ let area = tolerance === undefined ? sparse(state, changed) : sparseBlocks(state, changed);
24
76
  let blend = true;
25
77
  if (area.unblendable && replace)
26
78
  blend = false;
27
- else if (area.unblendable) {
28
- const last = output[output.length - 1];
79
+ else if (area.unblendable && this.last) {
80
+ const last = this.last;
29
81
  grow(last, union(last, area.unblendable));
30
82
  last.dispose = true;
31
83
  clear(shown, width, last);
32
84
  changed = bounds(state, { x: 0, y: 0, width, height });
33
85
  if (!changed)
34
- return void output.push(empty(duration));
86
+ return void this.output(empty(duration));
35
87
  if (even)
36
88
  changed = evenArea(changed);
37
89
  area = sparse(state, changed);
38
90
  }
39
91
  const { x, y, width: w, height: h } = changed;
40
92
  const pixels = area.pixels;
41
- if (!output.length && (w !== width || h !== height))
42
- opaque = false;
93
+ if (!this.count && (w !== width || h !== height))
94
+ this.opaque = false;
43
95
  if (!area.denseOpaque)
44
- opaque = false;
96
+ this.opaque = false;
45
97
  const sparseCost = alpha === 'frame' && area.denseOpaque ? area.sparseChanges * ALPHA_COST : area.sparseChanges;
46
- const dense = !blend || area.denseChanges < sparseCost
98
+ const dense = !blend || (tolerance !== undefined ? !area.unchangedBlocks : area.denseChanges < sparseCost
47
99
  || (palette !== undefined && colors(new Int32Array(pixels.buffer), w, { x: 0, y: 0, width: w, height: h }, palette + 1) > palette
48
- && colors(canvas32, width, changed, palette + 1) <= palette);
49
- denseTotal += area.denseChanges;
50
- chosenTotal += dense ? area.denseChanges : area.sparseChanges;
100
+ && colors(state.canvas32, width, changed, palette + 1) <= palette));
101
+ this.denseTotal += area.denseChanges;
102
+ this.chosenTotal += dense ? area.denseChanges : area.sparseChanges;
51
103
  if (dense) {
52
104
  for (let row = 0; row < h; row++)
53
105
  pixels.set(canvas.subarray(((y + row) * width + x) * 4, ((y + row) * width + x + w) * 4), row * w * 4);
54
106
  if (replace)
55
107
  blend = false;
56
108
  }
57
- for (let row = 0; row < h; row++) {
58
- const start = ((y + row) * width + x) * 4;
59
- shown.set(canvas.subarray(start, start + w * 4), start);
109
+ if (!dense && tolerance !== undefined) {
110
+ const pixels32 = new Int32Array(pixels.buffer);
111
+ for (let row = 0, k = 0; row < h; row++)
112
+ for (let p = (y + row) * width + x, end = p + w; p < end; p++, k++)
113
+ if (pixels32[k] & ALPHA)
114
+ state.shown32[p] = pixels32[k];
60
115
  }
61
- output.push({ x, y, width: w, height: h, duration, dispose: false, blend, pixels });
62
- };
63
- let cleared = { x: 0, y: 0, width, height };
64
- for (const f of frames) {
65
- const saved = f.dispose === 2 ? canvas.slice() : null;
66
- const area = clip(f, width, height);
67
- if (cleared && contains(cleared, area))
68
- copy(canvas, width, f, area);
69
116
  else
70
- draw(canvas, width, height, f);
71
- cleared = null;
72
- add(f.duration);
73
- if (f.dispose === 1) {
74
- clear(canvas, width, area);
75
- cleared = area;
76
- }
117
+ for (let row = 0; row < h; row++) {
118
+ const start = ((y + row) * width + x) * 4;
119
+ shown.set(canvas.subarray(start, start + w * 4), start);
120
+ }
121
+ this.output({ x, y, width: w, height: h, duration, dispose: false, blend, pixels });
122
+ }
123
+ }
124
+ export class Compositor {
125
+ width;
126
+ height;
127
+ canvas;
128
+ constructor(width, height) {
129
+ this.width = width;
130
+ this.height = height;
131
+ this.canvas = new Uint8Array(width * height * 4);
132
+ }
133
+ push(f) {
134
+ const { canvas, width, height } = this;
135
+ const saved = f.dispose === 2 ? canvas.slice() : null;
136
+ draw(canvas, width, height, f);
137
+ const shown = canvas.slice();
138
+ if (f.dispose === 1)
139
+ clear(canvas, width, clip(f, width, height));
77
140
  else if (saved)
78
141
  canvas.set(saved);
142
+ return shown;
79
143
  }
80
- if (typeof alpha === 'function' && opaque && output.some(f => f.blend) && denseTotal < chosenTotal * alpha(output)) {
81
- shown.fill(0);
82
- for (const f of output) {
83
- const pixels32 = new Int32Array(f.pixels.buffer, f.pixels.byteOffset, f.width * f.height);
84
- for (let row = 0, k = 0; row < f.height; row++, k += f.width) {
85
- const start = (f.y + row) * width + f.x;
86
- if (!f.blend)
87
- shown32.set(pixels32.subarray(k, k + f.width), start);
88
- else
89
- for (let i = k, p = start, end = k + f.width; i < end; i++, p++) {
90
- if ((pixels32[i] & ALPHA) === 0)
91
- pixels32[i] = shown32[p];
92
- else
93
- shown32[p] = pixels32[i];
94
- }
95
- }
96
- f.blend = false;
97
- }
144
+ }
145
+ export function optimize(frames, width, height, options) {
146
+ const optimizer = new Optimizer(width, height, options);
147
+ const output = [];
148
+ for (const f of frames) {
149
+ optimizer.push(f);
150
+ output.push(...optimizer.take());
151
+ }
152
+ output.push(...optimizer.end());
153
+ const { alpha, cover } = options;
154
+ if (typeof alpha === 'function' && optimizer.opaque && output.some(f => f.blend) && optimizer.denseTotal < optimizer.chosenTotal * alpha(output)) {
155
+ const shown32 = new Int32Array(width * height);
156
+ for (const f of output)
157
+ fillUnchanged(f, shown32, width);
98
158
  }
99
159
  if (cover && output.length)
100
- grow(output[0], { x: 0, y: 0, width, height });
160
+ coverCanvas(output[0], width, height);
101
161
  return output;
102
162
  }
163
+ export function fillUnchanged(f, shown32, width) {
164
+ const pixels32 = new Int32Array(f.pixels.buffer, f.pixels.byteOffset, f.width * f.height);
165
+ for (let row = 0, k = 0; row < f.height; row++, k += f.width) {
166
+ const start = (f.y + row) * width + f.x;
167
+ if (!f.blend)
168
+ shown32.set(pixels32.subarray(k, k + f.width), start);
169
+ else
170
+ for (let i = k, p = start, end = k + f.width; i < end; i++, p++) {
171
+ if ((pixels32[i] & ALPHA) === 0)
172
+ pixels32[i] = shown32[p];
173
+ else
174
+ shown32[p] = pixels32[i];
175
+ }
176
+ }
177
+ f.blend = false;
178
+ }
179
+ export function coverCanvas(frame, width, height) {
180
+ grow(frame, { x: 0, y: 0, width, height });
181
+ }
103
182
  function colors(image32, width, { x, y, width: w, height: h }, limit) {
104
183
  const seen = new Set();
105
184
  for (let row = y; row < y + h; row++) {
@@ -128,9 +207,75 @@ function union(a, b) {
128
207
  const x = Math.min(a.x, b.x), y = Math.min(a.y, b.y);
129
208
  return { x, y, width: Math.max(a.x + a.width, b.x + b.width) - x, height: Math.max(a.y + a.height, b.y + b.height) - y };
130
209
  }
210
+ function similar(a, i, b, j, tolerance) {
211
+ const alpha = a[i + 3];
212
+ if (alpha !== b[j + 3])
213
+ return false;
214
+ const limit = tolerance * 255;
215
+ return Math.abs(a[i] - b[j]) * alpha <= limit && Math.abs(a[i + 1] - b[j + 1]) * alpha <= limit && Math.abs(a[i + 2] - b[j + 2]) * alpha <= limit;
216
+ }
131
217
  function evenArea({ x, y, width, height }) {
132
218
  return { x: x & ~1, y: y & ~1, width: width + (x & 1), height: height + (y & 1) };
133
219
  }
220
+ const BLOCK = 8;
221
+ function sparseBlocks({ canvas32, shown32, width, tolerance = 0 }, { x, y, width: w, height: h }) {
222
+ const canvas = new Uint8Array(canvas32.buffer), shown = new Uint8Array(shown32.buffer);
223
+ const pixels = new Uint8Array(w * h * 4), pixels32 = new Int32Array(pixels.buffer);
224
+ let left = w, right = -1, top = h, bottom = -1, translucent = 0, unchangedBlocks = 0;
225
+ for (let by = 0; by < h; by += BLOCK)
226
+ for (let bx = 0; bx < w; bx += BLOCK) {
227
+ const bw = Math.min(BLOCK, w - bx), bh = Math.min(BLOCK, h - by);
228
+ let unchanged = true;
229
+ for (let row = by; row < by + bh && unchanged; row++)
230
+ for (let p = (y + row) * width + x + bx, end = p + bw; p < end; p++) {
231
+ const value = canvas32[p], before = shown32[p];
232
+ if (value !== before && ((value | before) & ALPHA) !== 0 && !similar(canvas, p * 4, shown, p * 4, tolerance)) {
233
+ unchanged = false;
234
+ break;
235
+ }
236
+ }
237
+ if (unchanged) {
238
+ unchangedBlocks++;
239
+ let r = 0, g = 0, b = 0;
240
+ for (let row = by; row < by + bh; row++)
241
+ for (let p = ((y + row) * width + x + bx) * 4, end = p + bw * 4; p < end; p += 4) {
242
+ r += shown[p];
243
+ g += shown[p + 1];
244
+ b += shown[p + 2];
245
+ }
246
+ const n = bw * bh;
247
+ r = r / n | 0;
248
+ g = g / n | 0;
249
+ b = b / n | 0;
250
+ for (let row = by; row < by + bh; row++)
251
+ for (let k = ((row * w) + bx) * 4, end = k + bw * 4; k < end; k += 4) {
252
+ pixels[k] = r;
253
+ pixels[k + 1] = g;
254
+ pixels[k + 2] = b;
255
+ }
256
+ }
257
+ for (let row = by; row < by + bh; row++)
258
+ for (let i = bx, k = row * w + bx, p = (y + row) * width + x + bx; i < bx + bw; i++, k++, p++) {
259
+ const value = canvas32[p];
260
+ translucent |= ~value & ALPHA;
261
+ if (unchanged)
262
+ continue;
263
+ pixels32[k] = value;
264
+ if ((value & ALPHA) !== ALPHA && ((value | shown32[p]) & ALPHA) !== 0) {
265
+ if (i < left)
266
+ left = i;
267
+ if (i > right)
268
+ right = i;
269
+ if (row < top)
270
+ top = row;
271
+ if (row > bottom)
272
+ bottom = row;
273
+ }
274
+ }
275
+ }
276
+ const unblendable = right < 0 ? null : { x: x + left, y: y + top, width: right - left + 1, height: bottom - top + 1 };
277
+ return { pixels, sparseChanges: 0, denseChanges: 0, unblendable, denseOpaque: translucent === 0, unchangedBlocks };
278
+ }
134
279
  function sparse({ canvas32, shown32, width, replace }, { x, y, width: w, height: h }) {
135
280
  const pixels = new Uint8Array(w * h * 4), pixels32 = new Int32Array(pixels.buffer);
136
281
  let sparseChanges = 0, denseChanges = 0, lastSparse = -1, lastDense = -1;
@@ -169,11 +314,12 @@ function sparse({ canvas32, shown32, width, replace }, { x, y, width: w, height:
169
314
  const unblendable = right < 0 ? null : { x: x + left, y: y + top, width: right - left + 1, height: bottom - top + 1 };
170
315
  return { pixels, sparseChanges, denseChanges, unblendable, denseOpaque: translucent === 0 };
171
316
  }
172
- function bounds({ canvas32, shown32, width }, area) {
317
+ function bounds({ canvas32, shown32, width, tolerance }, area) {
173
318
  const x0 = area.x, x1 = area.x + area.width, y0 = area.y, y1 = area.y + area.height;
319
+ const canvas = new Uint8Array(canvas32.buffer), shown = new Uint8Array(shown32.buffer);
174
320
  const test = (p) => {
175
321
  const c = canvas32[p], s = shown32[p];
176
- return c !== s && ((c | s) & ALPHA) !== 0;
322
+ return c !== s && ((c | s) & ALPHA) !== 0 && (tolerance === undefined || !similar(canvas, p * 4, shown, p * 4, tolerance));
177
323
  };
178
324
  const rowHas = (y, from, to) => {
179
325
  for (let x = from, p = y * width + from; x < to; x++, p++)
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.7.0';
1
+ export const VERSION = '3.8.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.7.0",
3
+ "version": "3.8.1",
4
4
  "description": "zero-dependency javascript image manipulation",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",