imagescript 3.8.1 → 3.10.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.
@@ -18,10 +18,62 @@ const mulDiv = (a, b, c) => {
18
18
  const d = Math.floor((Math.abs(a * b) + Math.abs(c) / 2) / Math.abs(c));
19
19
  return a * b * c < 0 ? -d : d;
20
20
  };
21
- const divFix = (a, b) => b === 0 ? 0 : roundHalfAway(a * 65536 / b);
21
+ const divFix = (a, b) => {
22
+ if (b === 0)
23
+ return a < 0 ? -0x7fffffff : 0x7fffffff;
24
+ const n = Math.abs(a) * 65536 + Math.floor(Math.abs(b) / 2), d = Math.abs(b);
25
+ let q = Math.floor(n / d);
26
+ if (q * d > n)
27
+ q--;
28
+ else if ((q + 1) * d <= n)
29
+ q++;
30
+ return (a < 0) !== (b < 0) ? -q : q;
31
+ };
32
+ const mulFix14 = (a, b) => {
33
+ const ab = a * b;
34
+ return Math.floor((ab + 0x2000 + (ab < 0 ? -1 : 0)) / 0x4000);
35
+ };
36
+ const dotFix14 = (ax, ay, bx, by) => {
37
+ const c = ax * bx + ay * by;
38
+ return Math.floor((c + 0x2000 + (c < 0 ? -1 : 0)) / 0x4000);
39
+ };
40
+ const X_AXIS = { x: 0x4000, y: 0 }, Y_AXIS = { x: 0, y: 0x4000 };
41
+ function normalize(vx, vy) {
42
+ if (vx === 0 && vy === 0)
43
+ return null;
44
+ const sx = vx < 0 ? -1 : 1, sy = vy < 0 ? -1 : 1;
45
+ let x = Math.abs(vx) >>> 0, y = Math.abs(vy) >>> 0;
46
+ if (x === 0)
47
+ return { x: 0, y: sy * 0x4000 };
48
+ if (y === 0)
49
+ return { x: sx * 0x4000, y: 0 };
50
+ let l = (x > y ? x + (y >>> 1) : y + (x >>> 1)) >>> 0;
51
+ let shift = 31 - (31 - Math.clz32(l));
52
+ shift -= 15 + (l >= 0xaaaaaaaa >>> shift ? 1 : 0);
53
+ if (shift > 0) {
54
+ x = x << shift >>> 0;
55
+ y = y << shift >>> 0;
56
+ l = (x > y ? x + (y >>> 1) : y + (x >>> 1)) >>> 0;
57
+ }
58
+ else {
59
+ x >>>= -shift;
60
+ y >>>= -shift;
61
+ l >>>= -shift;
62
+ }
63
+ let b = 0x10000 - l | 0, u, v, z;
64
+ const x_ = x | 0, y_ = y | 0;
65
+ do {
66
+ u = x_ + (Math.imul(x_, b) >> 16) >>> 0;
67
+ v = y_ + (Math.imul(y_, b) >> 16) >>> 0;
68
+ z = Math.trunc(-(Math.imul(u, u) + Math.imul(v, v) | 0) / 0x200);
69
+ z = Math.trunc(Math.imul(z, 0x10000 + b >> 8) / 0x10000);
70
+ b = b + z | 0;
71
+ } while (z > 0);
72
+ return { x: Math.trunc(sx * u / 4), y: Math.trunc(sy * v / 4) };
73
+ }
22
74
  const ROUND_GRID = { period: 64, phase: 0, threshold: 32 };
23
75
  const defaultState = () => ({
24
- pv: { x: 1, y: 0 }, fv: { x: 1, y: 0 }, dv: { x: 1, y: 0 },
76
+ pv: X_AXIS, fv: X_AXIS, dv: X_AXIS,
25
77
  rp0: 0, rp1: 0, rp2: 0, zp0: 1, zp1: 1, zp2: 1, loop: 1,
26
78
  minimumDistance: 64, cvtCutIn: 68, singleWidthCutIn: 0, singleWidth: 0,
27
79
  deltaBase: 9, deltaShift: 3, autoFlip: true, round: ROUND_GRID, instructControl: 0
@@ -145,7 +197,7 @@ export class Hinter {
145
197
  this.points += data.xs.length;
146
198
  if (this.points > MAX_POINTS)
147
199
  throw new HintError('too many points');
148
- const scale = size.scale, scaleFixed = this.scaleFixed = size.scaleFixed;
200
+ const scaleFixed = this.scaleFixed = size.scaleFixed;
149
201
  const lsb = font.leftSideBearing(glyph), advance = font.advance(glyph);
150
202
  let count, zone;
151
203
  if (data.components) {
@@ -196,14 +248,12 @@ export class Hinter {
196
248
  zone.curY.set(ysAll);
197
249
  zone.orgX.set(xsAll);
198
250
  zone.orgY.set(ysAll);
199
- for (let i = 0; i < count; i++) {
200
- zone.orusX[i] = xsAll[i] / scale;
201
- zone.orusY[i] = ysAll[i] / scale;
202
- }
251
+ zone.orusX.set(xsAll);
252
+ zone.orusY.set(ysAll);
203
253
  zone.on.set(on);
204
- this.phantoms(zone, count, data.xMin - lsb, advance, useMetrics);
254
+ this.phantoms(zone, count, data.xMin - lsb, advance, useMetrics, data.instructions.length > 0);
205
255
  if (data.instructions.length && !(size.state.instructControl & 1)) {
206
- new Execution(this, size, size.twilight, zone).run(data.instructions, true);
256
+ new Execution(this, size, size.twilight, zone, true).run(data.instructions, true);
207
257
  }
208
258
  return { zone, count };
209
259
  }
@@ -224,7 +274,7 @@ export class Hinter {
224
274
  new Execution(this, size, size.twilight, zone).run(data.instructions, true);
225
275
  return { zone, count };
226
276
  }
227
- phantoms(zone, count, origin, advance, metrics) {
277
+ phantoms(zone, count, origin, advance, metrics, round = true) {
228
278
  const font = this.font;
229
279
  const units = [origin, origin + advance, font.ascender, font.descender];
230
280
  const org = units.map(v => mulFix(v, this.scaleFixed));
@@ -232,7 +282,7 @@ export class Hinter {
232
282
  zone.orusX[count + 1] = units[1];
233
283
  zone.orusY[count + 2] = units[2];
234
284
  zone.orusY[count + 3] = units[3];
235
- const grid = (v) => Math.round(v / 64) * 64;
285
+ const grid = (v) => round ? Math.round(v / 64) * 64 : v;
236
286
  zone.orgX[count] = org[0];
237
287
  zone.orgX[count + 1] = org[1];
238
288
  zone.curX[count] = grid(org[0]);
@@ -259,20 +309,20 @@ class Execution {
259
309
  callDepth = 0;
260
310
  steps = 0;
261
311
  isGlyph = false;
262
- constructor(hinter, size, twilight, glyph) {
312
+ unitsScale;
313
+ constructor(hinter, size, twilight, glyph, composite = false) {
263
314
  this.hinter = hinter;
264
315
  this.size = size;
265
316
  const s = size.state;
266
317
  this.gs = { ...s, pv: { ...s.pv }, fv: { ...s.fv }, dv: { ...s.dv } };
267
318
  this.zones = [twilight, glyph];
319
+ this.unitsScale = composite ? 0x10000 : size.scaleFixed;
268
320
  }
269
321
  run(code, glyph = false) {
270
322
  this.isGlyph = glyph;
271
323
  if (glyph) {
272
324
  const gs = this.gs;
273
- gs.pv = { x: 1, y: 0 };
274
- gs.fv = { x: 1, y: 0 };
275
- gs.dv = { x: 1, y: 0 };
325
+ gs.pv = gs.fv = gs.dv = X_AXIS;
276
326
  gs.rp0 = gs.rp1 = gs.rp2 = 0;
277
327
  gs.zp0 = gs.zp1 = gs.zp2 = 1;
278
328
  gs.loop = 1;
@@ -306,34 +356,38 @@ class Execution {
306
356
  return p;
307
357
  }
308
358
  project(dx, dy) {
309
- return dx * this.gs.pv.x + dy * this.gs.pv.y;
359
+ return dotFix14(dx, dy, this.gs.pv.x, this.gs.pv.y);
310
360
  }
311
361
  dualProject(dx, dy) {
312
- return dx * this.gs.dv.x + dy * this.gs.dv.y;
362
+ return dotFix14(dx, dy, this.gs.dv.x, this.gs.dv.y);
313
363
  }
314
364
  moveVector() {
315
365
  const { fv, pv } = this.gs;
316
- const dot = fv.x * pv.x + fv.y * pv.y;
317
- if (dot >= 0x3ffe / 0x4000)
318
- return fv;
319
- if (Math.abs(dot) < 0x1555 / 0x4000)
366
+ const dot = Math.floor((pv.x * fv.x + pv.y * fv.y + 0x2000) / 0x4000);
367
+ if (dot >= 0x3ffe)
368
+ return { x: fv.x * 4, y: fv.y * 4 };
369
+ if (dot > -0x1555 && dot < 0x1555)
320
370
  return { x: 0, y: 0 };
321
- return { x: fv.x / dot, y: fv.y / dot };
371
+ return { x: Math.trunc(fv.x * 0x10000 / dot), y: Math.trunc(fv.y * 0x10000 / dot) };
322
372
  }
323
- move(zone, p, distance, touch = true) {
324
- const { fv } = this.gs;
373
+ move(zone, p, distance) {
325
374
  const mv = this.moveVector();
326
- if (fv.x !== 0) {
327
- zone.curX[p] += distance * mv.x;
328
- if (touch)
329
- zone.touchX[p] = 1;
375
+ if (mv.x !== 0) {
376
+ zone.curX[p] += mulFix(distance, mv.x);
377
+ zone.touchX[p] = 1;
330
378
  }
331
- if (fv.y !== 0) {
332
- zone.curY[p] += distance * mv.y;
333
- if (touch)
334
- zone.touchY[p] = 1;
379
+ if (mv.y !== 0) {
380
+ zone.curY[p] += mulFix(distance, mv.y);
381
+ zone.touchY[p] = 1;
335
382
  }
336
383
  }
384
+ moveOriginal(zone, p, distance) {
385
+ const mv = this.moveVector();
386
+ if (mv.x !== 0)
387
+ zone.orgX[p] += mulFix(distance, mv.x);
388
+ if (mv.y !== 0)
389
+ zone.orgY[p] += mulFix(distance, mv.y);
390
+ }
337
391
  round(d) {
338
392
  const r = this.gs.round;
339
393
  if (!r)
@@ -345,34 +399,27 @@ class Execution {
345
399
  const v = -(Math.floor((-d - r.phase + r.threshold) / r.period) * r.period + r.phase);
346
400
  return v > 0 ? -r.phase : v;
347
401
  }
348
- setVector(target, x, y) {
349
- const length = Math.hypot(x, y);
350
- if (length === 0)
351
- return;
352
- this.gs[target] = { x: x / length, y: y / length };
353
- }
354
- lineVector(p1, p2, perpendicular, original) {
402
+ lineVector(p1, p2, perpendicular, original, current) {
355
403
  const z1 = this.zone(this.gs.zp1), z2 = this.zone(this.gs.zp2);
356
404
  this.point(z1, p1);
357
405
  this.point(z2, p2);
358
406
  let x = (original ? z1.orgX[p1] - z2.orgX[p2] : z1.curX[p1] - z2.curX[p2]);
359
407
  let y = (original ? z1.orgY[p1] - z2.orgY[p2] : z1.curY[p1] - z2.curY[p2]);
360
408
  if (x === 0 && y === 0) {
361
- x = 1;
409
+ x = 0x4000;
362
410
  y = 0;
363
411
  perpendicular = false;
364
412
  }
365
413
  if (perpendicular)
366
414
  [x, y] = [-y, x];
367
- const length = Math.hypot(x, y);
368
- return { x: x / length, y: y / length };
415
+ return normalize(x, y) ?? current;
369
416
  }
370
417
  originalDistance(za, a, zb, b, twilight = za === 0 || zb === 0, round = true) {
371
418
  const A = this.zones[za], B = this.zones[zb];
372
419
  if (twilight)
373
420
  return this.dualProject(A.orgX[a] - B.orgX[b], A.orgY[a] - B.orgY[b]);
374
421
  const d = this.dualProject(A.orusX[a] - B.orusX[b], A.orusY[a] - B.orusY[b]);
375
- return round ? mulFix(d, this.size.scaleFixed) : d * this.size.scale;
422
+ return round ? mulFix(d, this.unitsScale) : d;
376
423
  }
377
424
  loopCount() {
378
425
  const n = this.gs.loop;
@@ -419,61 +466,54 @@ class Execution {
419
466
  switch (op) {
420
467
  case 0x00:
421
468
  case 0x01: {
422
- const v = op & 1 ? { x: 1, y: 0 } : { x: 0, y: 1 };
423
- gs.pv = v;
424
- gs.fv = { ...v };
425
- gs.dv = { ...v };
469
+ gs.pv = gs.fv = gs.dv = op & 1 ? X_AXIS : Y_AXIS;
426
470
  break;
427
471
  }
428
472
  case 0x02:
429
473
  case 0x03:
430
- gs.pv = op & 1 ? { x: 1, y: 0 } : { x: 0, y: 1 };
431
- gs.dv = { ...gs.pv };
474
+ gs.pv = gs.dv = op & 1 ? X_AXIS : Y_AXIS;
432
475
  break;
433
476
  case 0x04:
434
477
  case 0x05:
435
- gs.fv = op & 1 ? { x: 1, y: 0 } : { x: 0, y: 1 };
478
+ gs.fv = op & 1 ? X_AXIS : Y_AXIS;
436
479
  break;
437
480
  case 0x06:
438
481
  case 0x07: {
439
482
  const p2 = this.pop(), p1 = this.pop();
440
- gs.pv = this.lineVector(p1, p2, (op & 1) === 1, false);
441
- gs.dv = { ...gs.pv };
483
+ gs.pv = gs.dv = this.lineVector(p1, p2, (op & 1) === 1, false, gs.pv);
442
484
  break;
443
485
  }
444
486
  case 0x08:
445
487
  case 0x09: {
446
488
  const p2 = this.pop(), p1 = this.pop();
447
- gs.fv = this.lineVector(p1, p2, (op & 1) === 1, false);
489
+ gs.fv = this.lineVector(p1, p2, (op & 1) === 1, false, gs.fv);
448
490
  break;
449
491
  }
450
492
  case 0x86:
451
493
  case 0x87: {
452
494
  const p2 = this.pop(), p1 = this.pop();
453
- gs.pv = this.lineVector(p1, p2, (op & 1) === 1, false);
454
- gs.dv = this.lineVector(p1, p2, (op & 1) === 1, true);
495
+ gs.dv = this.lineVector(p1, p2, (op & 1) === 1, true, gs.dv);
496
+ gs.pv = this.lineVector(p1, p2, (op & 1) === 1, false, gs.pv);
455
497
  break;
456
498
  }
457
499
  case 0x0a:
458
500
  case 0x0b: {
459
501
  const y = this.pop() << 16 >> 16, x = this.pop() << 16 >> 16;
460
- if (op === 0x0a) {
461
- this.setVector('pv', x, y);
462
- gs.dv = { ...gs.pv };
463
- }
502
+ if (op === 0x0a)
503
+ gs.pv = gs.dv = normalize(x, y) ?? gs.pv;
464
504
  else
465
- this.setVector('fv', x, y);
505
+ gs.fv = normalize(x, y) ?? gs.fv;
466
506
  break;
467
507
  }
468
508
  case 0x0c:
469
509
  case 0x0d: {
470
510
  const v = op === 0x0c ? gs.pv : gs.fv;
471
- this.push(Math.round(v.x * 16384));
472
- this.push(Math.round(v.y * 16384));
511
+ this.push(v.x);
512
+ this.push(v.y);
473
513
  break;
474
514
  }
475
515
  case 0x0e:
476
- gs.fv = { ...gs.pv };
516
+ gs.fv = gs.pv;
477
517
  break;
478
518
  case 0x0f: {
479
519
  const b1 = this.pop(), b0 = this.pop(), a1 = this.pop(), a0 = this.pop(), p = this.pop();
@@ -523,7 +563,7 @@ class Execution {
523
563
  const n = this.pop();
524
564
  if (n < 0)
525
565
  throw new HintError('invalid loop');
526
- gs.loop = Math.min(n, 65535) || 1;
566
+ gs.loop = Math.min(n, 65535);
527
567
  break;
528
568
  }
529
569
  case 0x18:
@@ -910,8 +950,8 @@ class Execution {
910
950
  this.cvtIndex(n);
911
951
  let distance = size.cvt[n];
912
952
  if (gs.zp0 === 0) {
913
- z.orgX[p] = z.curX[p] = distance * gs.fv.x;
914
- z.orgY[p] = z.curY[p] = distance * gs.fv.y;
953
+ z.orgX[p] = z.curX[p] = mulFix14(distance, gs.fv.x);
954
+ z.orgY[p] = z.curY[p] = mulFix14(distance, gs.fv.y);
915
955
  }
916
956
  const current = this.project(z.curX[p], z.curY[p]);
917
957
  if (op & 1) {
@@ -932,6 +972,7 @@ class Execution {
932
972
  if (gs.zp1 === 0) {
933
973
  z1.orgX[p] = z0.orgX[gs.rp0];
934
974
  z1.orgY[p] = z0.orgY[gs.rp0];
975
+ this.moveOriginal(z1, p, d);
935
976
  z1.curX[p] = z1.orgX[p];
936
977
  z1.curY[p] = z1.orgY[p];
937
978
  }
@@ -954,7 +995,7 @@ class Execution {
954
995
  this.point(refZone, ref);
955
996
  const d = this.project(refZone.curX[ref] - refZone.orgX[ref], refZone.curY[ref] - refZone.orgY[ref]);
956
997
  const mv = this.moveVector();
957
- const dx = d * mv.x, dy = d * mv.y;
998
+ const dx = mulFix(d, mv.x), dy = mulFix(d, mv.y);
958
999
  const shift = (z, p, touch) => {
959
1000
  z.curX[p] += dx;
960
1001
  z.curY[p] += dy;
@@ -992,12 +1033,14 @@ class Execution {
992
1033
  const d = this.pop(), z = this.zone(gs.zp2);
993
1034
  for (let n = this.loopCount(); n > 0; n--) {
994
1035
  const p = this.point(z, this.pop());
995
- z.curX[p] += d * gs.fv.x;
996
- z.curY[p] += d * gs.fv.y;
997
- if (gs.fv.x !== 0)
1036
+ if (gs.fv.x !== 0) {
1037
+ z.curX[p] += mulFix14(d, gs.fv.x);
998
1038
  z.touchX[p] = 1;
999
- if (gs.fv.y !== 0)
1039
+ }
1040
+ if (gs.fv.y !== 0) {
1041
+ z.curY[p] += mulFix14(d, gs.fv.y);
1000
1042
  z.touchY[p] = 1;
1043
+ }
1001
1044
  }
1002
1045
  break;
1003
1046
  }
@@ -1013,7 +1056,7 @@ class Execution {
1013
1056
  const p = this.point(z2, this.pop());
1014
1057
  const orgDistance = this.originalDistance(gs.zp2, p, gs.zp0, gs.rp1, twilight, false);
1015
1058
  const curDistance = this.project(z2.curX[p] - curBaseX, z2.curY[p] - curBaseY);
1016
- const newDistance = orgDistance === 0 ? 0 : oldRange !== 0 ? roundHalfAway(orgDistance * curRange / oldRange) : roundHalfAway(orgDistance);
1059
+ const newDistance = orgDistance === 0 ? 0 : oldRange !== 0 ? mulDiv(orgDistance, curRange, oldRange) : orgDistance;
1017
1060
  this.move(z2, p, newDistance - curDistance);
1018
1061
  }
1019
1062
  break;
@@ -1073,7 +1116,7 @@ class Execution {
1073
1116
  case 0x73:
1074
1117
  case 0x74:
1075
1118
  case 0x75: {
1076
- const n = this.pop();
1119
+ const n = Math.max(0, Math.min(this.pop(), this.stack.length >> 1));
1077
1120
  const range = op === 0x5d || op === 0x73 ? 0 : op === 0x71 || op === 0x74 ? 16 : 32;
1078
1121
  for (let k = 0; k < n; k++) {
1079
1122
  const target = this.pop(), arg = this.pop();
@@ -1147,8 +1190,8 @@ class Execution {
1147
1190
  if (Math.abs(cvt - gs.singleWidth) < gs.singleWidthCutIn)
1148
1191
  cvt = cvt >= 0 ? gs.singleWidth : -gs.singleWidth;
1149
1192
  if (gs.zp1 === 0) {
1150
- z1.orgX[p] = z0.orgX[gs.rp0] + cvt * gs.fv.x;
1151
- z1.orgY[p] = z0.orgY[gs.rp0] + cvt * gs.fv.y;
1193
+ z1.orgX[p] = z0.orgX[gs.rp0] + mulFix14(cvt, gs.fv.x);
1194
+ z1.orgY[p] = z0.orgY[gs.rp0] + mulFix14(cvt, gs.fv.y);
1152
1195
  z1.curX[p] = z1.orgX[p];
1153
1196
  z1.curY[p] = z1.orgY[p];
1154
1197
  }