imagescript 3.20.4 → 3.20.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.
@@ -997,10 +997,22 @@ function fromWOFF(data) {
997
997
  const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
998
998
  const count = view.getUint16(12);
999
999
  const tables = [];
1000
+ let budget = 1 << 27;
1000
1001
  for (let i = 0; i < count; i++) {
1001
1002
  const e = 44 + i * 20, offset = view.getUint32(e + 4), compressed = view.getUint32(e + 8), length = view.getUint32(e + 12);
1003
+ if (offset + compressed > data.length)
1004
+ throw new Error('Font: damaged or truncated font data');
1005
+ if (length > budget)
1006
+ throw new Error('Font: WOFF tables are too large');
1007
+ budget -= length;
1002
1008
  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 });
1009
+ let table = raw;
1010
+ if (compressed < length) {
1011
+ table = decompress(raw, length).subarray(0, length);
1012
+ if (table.length !== length)
1013
+ throw new Error('Font: damaged WOFF table');
1014
+ }
1015
+ tables.push({ tag: view.getUint32(e), checksum: view.getUint32(e + 16), data: table });
1004
1016
  }
1005
1017
  const header = 12 + count * 16;
1006
1018
  let size = header;
@@ -78,9 +78,18 @@ export function render(source, mode, size, images = new Map(), fonts = []) {
78
78
  }
79
79
  outWidth = Math.max(1, outWidth);
80
80
  outHeight = Math.max(1, outHeight);
81
- const canvas = new Layer(outWidth, outHeight);
82
- doc.renderRoot([outWidth / width, 0, 0, outHeight / height, 0, 0], canvas, 0);
83
- return { width: outWidth, height: outHeight, pixels: canvas.toRGBA() };
81
+ const outermost = pointsLeft === Infinity;
82
+ if (outermost)
83
+ pointsLeft = MAX_POINTS + POINTS_PER_PIXEL * outWidth * outHeight;
84
+ try {
85
+ const canvas = new Layer(outWidth, outHeight);
86
+ doc.renderRoot([outWidth / width, 0, 0, outHeight / height, 0, 0], canvas, 0);
87
+ return { width: outWidth, height: outHeight, pixels: canvas.toRGBA() };
88
+ }
89
+ finally {
90
+ if (outermost)
91
+ pointsLeft = Infinity;
92
+ }
84
93
  }
85
94
  function documentSize(root) {
86
95
  const viewBox = parseViewBox(root.attrs.viewBox);
@@ -872,6 +881,9 @@ class Document {
872
881
  throw err;
873
882
  }
874
883
  }
884
+ if (layout)
885
+ for (const span of layout.spans)
886
+ glyphPaths.add(span.glyphs);
875
887
  this.texts.set(node, { style, width: viewport.width, height: viewport.height, scale, layout });
876
888
  return layout;
877
889
  }
@@ -1241,6 +1253,7 @@ class Document {
1241
1253
  if (span.visible)
1242
1254
  for (const part of [...span.before, { path: span.glyphs }, ...span.after])
1243
1255
  path.push(...part.path);
1256
+ glyphPaths.add(path);
1244
1257
  return path.length ? path : null;
1245
1258
  }
1246
1259
  renderImage(node, ctx) {
@@ -2923,6 +2936,13 @@ function shapePath(node, viewport, fontSize) {
2923
2936
  }
2924
2937
  return null;
2925
2938
  }
2939
+ const MAX_POINTS = 2 ** 25, POINTS_PER_PIXEL = 4, MAX_PATH_POINTS = 2 ** 21;
2940
+ let pointsLeft = Infinity;
2941
+ const glyphPaths = new WeakSet();
2942
+ function chargePoints(points) {
2943
+ if ((pointsLeft -= points) < 0)
2944
+ throw new RangeError(`SVG too complex: its paths are made of more points than a render of this size allows (${MAX_POINTS} and ${POINTS_PER_PIXEL} per output pixel)`);
2945
+ }
2926
2946
  function parsePath(d) {
2927
2947
  const path = [];
2928
2948
  const items = [];
@@ -2933,6 +2953,7 @@ function parsePath(d) {
2933
2953
  end = m.index + m[0].length;
2934
2954
  items.push(m[1] ? { command: m[1] } : m[2]);
2935
2955
  }
2956
+ chargePoints(2 * items.length);
2936
2957
  let i = 0;
2937
2958
  let command = null;
2938
2959
  let x = 0, y = 0, sx = 0, sy = 0;
@@ -3094,8 +3115,9 @@ function arcToCubics(path, x1, y1, rx, ry, rotation, large, sweep, x2, y2) {
3094
3115
  delta -= 2 * Math.PI;
3095
3116
  else if (sweep && delta < 0)
3096
3117
  delta += 2 * Math.PI;
3097
- const perTurn = Math.max(Math.pow(1.1163 * Math.max(rx, ry) / 0.1, 1 / 6), 3.999999);
3118
+ const perTurn = Math.min(Math.max(Math.pow(1.1163 * Math.max(rx, ry) / 0.1, 1 / 6), 3.999999), 64);
3098
3119
  const segments = Math.max(1, Math.ceil(perTurn * Math.abs(delta) / (2 * Math.PI)));
3120
+ chargePoints(4 * segments);
3099
3121
  const step = delta / segments;
3100
3122
  const k = 4 / 3 * Math.tan(step / 4);
3101
3123
  const point = (t) => [cx + rx * Math.cos(t) * cos - ry * Math.sin(t) * sin, cy + rx * Math.cos(t) * sin + ry * Math.sin(t) * cos];
@@ -3110,6 +3132,13 @@ function arcToCubics(path, x1, y1, rx, ry, rotation, large, sweep, x2, y2) {
3110
3132
  }
3111
3133
  }
3112
3134
  function flatten(path, tolerance) {
3135
+ let pathPoints = 0;
3136
+ const limited = !glyphPaths.has(path);
3137
+ const count = (points) => {
3138
+ chargePoints(points);
3139
+ if (limited && (pathPoints += points) > MAX_PATH_POINTS)
3140
+ throw new RangeError(`SVG too complex: a path is made of more than ${MAX_PATH_POINTS} points`);
3141
+ };
3113
3142
  const subpaths = [];
3114
3143
  let current = null;
3115
3144
  let x = 0, y = 0, sx = 0, sy = 0;
@@ -3123,9 +3152,11 @@ function flatten(path, tolerance) {
3123
3152
  case 'M':
3124
3153
  x = sx = command[1];
3125
3154
  y = sy = command[2];
3155
+ count(1);
3126
3156
  current = start(x, y);
3127
3157
  break;
3128
3158
  case 'L':
3159
+ count(1);
3129
3160
  if (!current)
3130
3161
  current = start(x, y);
3131
3162
  x = command[1];
@@ -3139,6 +3170,7 @@ function flatten(path, tolerance) {
3139
3170
  const ddx = Math.max(Math.abs(x - 2 * c1x + c2x), Math.abs(c1x - 2 * c2x + ex));
3140
3171
  const ddy = Math.max(Math.abs(y - 2 * c1y + c2y), Math.abs(c1y - 2 * c2y + ey));
3141
3172
  const n = Math.min(512, Math.max(1, Math.ceil(Math.sqrt(0.75 * Math.hypot(ddx, ddy) / tolerance))));
3173
+ count(n);
3142
3174
  for (let i = 1; i <= n; i++) {
3143
3175
  const t = i / n, mt = 1 - t;
3144
3176
  current.points.push(mt * mt * mt * x + 3 * mt * mt * t * c1x + 3 * mt * t * t * c2x + t * t * t * ex, mt * mt * mt * y + 3 * mt * mt * t * c1y + 3 * mt * t * t * c2y + t * t * t * ey);
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.20.4';
1
+ export const VERSION = '3.20.6';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.20.4",
3
+ "version": "3.20.6",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",