@hussmartinez/image-sdk 0.4.0 → 0.5.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.
@@ -46,6 +46,12 @@ var imageLimitDefinitions = {
46
46
  code: "TOO_MANY_LAYERS",
47
47
  unit: "count",
48
48
  default: 256
49
+ },
50
+ maxDrawingPoints: {
51
+ code: "TOO_MANY_POINTS",
52
+ unit: "count",
53
+ default: 2e5,
54
+ warning: 1e5
49
55
  }
50
56
  };
51
57
  var imageLimits = /* @__PURE__ */ defineLimits(imageLimitDefinitions, { errorClass: ImageError });
@@ -168,8 +174,8 @@ function createImageEngine(options = {}) {
168
174
  type: "module",
169
175
  name: "hm-image-render"
170
176
  });
171
- } : async () => (await import("../lazy/inline-worker-DsBNkr8Q.js")).createInlineWorker,
172
- version: "0.4.0",
177
+ } : async () => (await import("../lazy/inline-worker-BGAoQbIR.js")).createInlineWorker,
178
+ version: "0.5.0",
173
179
  get asset() {
174
180
  return workerUrl ?? "inline";
175
181
  }
@@ -554,6 +560,9 @@ var REDACTION_MODES = ["solid", "pixelate"];
554
560
  var MAX_LAYER_TEXT_LENGTH = 1e3;
555
561
  var MAX_LAYER_NAME_LENGTH = 100;
556
562
  var MAX_STROKE_WIDTH = 1024;
563
+ var MAX_RUN_POINTS = 2e3;
564
+ var MAX_STROKE_RUNS = 64;
565
+ var MAX_DRAWING_STROKES = 1024;
557
566
  var LAYER_FONTS = [
558
567
  "sans-serif",
559
568
  "serif",
@@ -568,7 +577,9 @@ var LAYER_SHAPES = ["rectangle", "ellipse"];
568
577
  var LAYER_KINDS = [
569
578
  "text",
570
579
  "shape",
571
- "image"
580
+ "image",
581
+ "drawing",
582
+ "arrow"
572
583
  ];
573
584
  var UPRIGHT = Object.freeze({
574
585
  rotate: 0,
@@ -904,6 +915,7 @@ var BOX = [
904
915
  ];
905
916
  var OWN = {
906
917
  text: [
918
+ ...BOX,
907
919
  "text",
908
920
  "font",
909
921
  "bold",
@@ -911,13 +923,37 @@ var OWN = {
911
923
  "align"
912
924
  ],
913
925
  shape: [
926
+ ...BOX,
914
927
  "shape",
915
928
  "fill",
916
929
  "stroke"
917
930
  ],
918
- image: ["source", "crop"]
931
+ image: [
932
+ ...BOX,
933
+ "source",
934
+ "crop"
935
+ ],
936
+ drawing: [
937
+ "x",
938
+ "y",
939
+ "strokes"
940
+ ],
941
+ arrow: [
942
+ "from",
943
+ "to",
944
+ "colour",
945
+ "width"
946
+ ]
947
+ };
948
+ var NOT_UPDATED = { drawing: ["strokes"] };
949
+ var KINDS_BY_VERSION = {
950
+ 2: [
951
+ "text",
952
+ "shape",
953
+ "image"
954
+ ],
955
+ 3: LAYER_KINDS
919
956
  };
920
- var KINDS = LAYER_KINDS;
921
957
  var ANY_OWN = new Set(Object.values(OWN).flat());
922
958
  var FORBIDDEN_IN_TEXT = /[\u0000-\u0009\u000B-\u001F\u007F‪-‮⁦-⁩]/u;
923
959
  function checkText(value, max, what) {
@@ -934,42 +970,116 @@ function checkBoolean(value, what) {
934
970
  if (typeof value !== "boolean") invalid("malformed-command", `${what} is true or false`);
935
971
  return value;
936
972
  }
937
- function checkLayer(value, sourceSize) {
973
+ var isCoordinate = (value) => isWhole(value, -MAX_SCENE_COORDINATE, MAX_SCENE_COORDINATE);
974
+ function checkWidth(value, what) {
975
+ if (!isWhole(value, 1, 1024)) invalid("malformed-command", `${what}'s width is from 1 to ${MAX_STROKE_WIDTH} pixels`);
976
+ return value;
977
+ }
978
+ function checkStroke(value, origin) {
979
+ if (!isRecord(value)) invalid("malformed-command", "a stroke is an object");
980
+ allFields(value, [
981
+ "colour",
982
+ "width",
983
+ "runs"
984
+ ], "a stroke");
985
+ const colour = checkColour(value.colour);
986
+ const width = checkWidth(value.width, "a stroke");
987
+ const { runs } = value;
988
+ if (!Array.isArray(runs) || runs.length < 1 || runs.length > 64) invalid("malformed-command", `a stroke has 1 to 64 runs`);
989
+ return {
990
+ colour,
991
+ width,
992
+ runs: runs.map((run) => {
993
+ if (!Array.isArray(run) || run.length < 2 || run.length > 4e3) invalid("malformed-command", `a run has 1 to ${MAX_RUN_POINTS} points`);
994
+ const numbers = run;
995
+ if (!numbers.every((value) => isWhole(value, -131072, 131072))) invalid("malformed-command", "a run's coordinates are whole numbers of pixels");
996
+ if (run.length % 2 !== 0) invalid("invalid-points", "a run has an x and a y for each point");
997
+ for (let i = 0; i < run.length; i += 2) if (!isCoordinate(origin.x + numbers[i]) || !isCoordinate(origin.y + numbers[i + 1])) invalid("invalid-points", `a point is within ${MAX_SCENE_COORDINATE} pixels of the scene's origin`);
998
+ return [...numbers];
999
+ })
1000
+ };
1001
+ }
1002
+ function checkStrokes(value, origin) {
1003
+ if (!Array.isArray(value) || value.length > 1024) invalid("malformed-command", `a drawing has at most ${MAX_DRAWING_STROKES} strokes`);
1004
+ return value.map((stroke) => checkStroke(stroke, origin));
1005
+ }
1006
+ function checkPoint(value, what) {
1007
+ if (!isRecord(value)) invalid("malformed-command", `${what} is a point`);
1008
+ allFields(value, ["x", "y"], what);
1009
+ const { x, y } = value;
1010
+ if (!isCoordinate(x) || !isCoordinate(y)) invalid("malformed-command", `${what} is in whole pixels, within ${MAX_SCENE_COORDINATE} of the scene's origin`);
1011
+ return {
1012
+ x,
1013
+ y
1014
+ };
1015
+ }
1016
+ function checkLayer(value, sourceSize, version = 3) {
938
1017
  if (!isRecord(value)) invalid("malformed-command", "a layer is an object");
939
1018
  const { kind } = value;
940
1019
  if (typeof kind !== "string") invalid("malformed-command", "a layer's kind is text");
941
- if (!KINDS.includes(kind)) invalid("unknown-layer-kind", `a layer's kind is one of ${LAYER_KINDS.join(", ")}`);
1020
+ const kinds = KINDS_BY_VERSION[version];
1021
+ if (!kinds.includes(kind)) invalid("unknown-layer-kind", `a layer's kind is one of ${kinds.join(", ")}`);
942
1022
  const own = OWN[kind];
943
- allFields(value, [
944
- ...COMMON,
945
- ...BOX,
946
- ...own
947
- ], `a ${kind} layer`);
1023
+ allFields(value, [...COMMON, ...own], `a ${kind} layer`);
948
1024
  const key = checkKey(value["key"]);
949
1025
  const name = checkText(value["name"], 100, "a layer's name");
950
1026
  const visible = checkBoolean(value["visible"], "a layer's visible");
951
- const { opacity, x, y, width, height, rotation } = value;
1027
+ const { opacity } = value;
1028
+ if (!Number.isInteger(opacity)) invalid("malformed-command", "a layer's opacity is a whole number");
1029
+ if (!isWhole(opacity, 0, 100)) invalid("invalid-opacity", "a layer's opacity is from 0 to 100");
1030
+ const shared = {
1031
+ key,
1032
+ name,
1033
+ visible,
1034
+ opacity
1035
+ };
1036
+ if (kind === "drawing") {
1037
+ const { x, y } = value;
1038
+ if (!isCoordinate(x) || !isCoordinate(y)) invalid("malformed-command", `a drawing's origin is in whole pixels, within ${MAX_SCENE_COORDINATE} of the scene's origin`);
1039
+ const strokes = checkStrokes(value["strokes"], {
1040
+ x,
1041
+ y
1042
+ });
1043
+ if (strokes.length === 0) invalid("empty-drawing", "a drawing has at least one stroke");
1044
+ return {
1045
+ ...shared,
1046
+ kind,
1047
+ x,
1048
+ y,
1049
+ strokes
1050
+ };
1051
+ }
1052
+ if (kind === "arrow") {
1053
+ const from = checkPoint(value["from"], "an arrow's from");
1054
+ const to = checkPoint(value["to"], "an arrow's to");
1055
+ if (from.x === to.x && from.y === to.y) invalid("empty-arrow", "an arrow's ends differ");
1056
+ return {
1057
+ ...shared,
1058
+ kind,
1059
+ from,
1060
+ to,
1061
+ colour: checkColour(value["colour"]),
1062
+ width: checkWidth(value["width"], "an arrow")
1063
+ };
1064
+ }
1065
+ const { x, y, width, height, rotation } = value;
952
1066
  if (![
953
- opacity,
954
1067
  x,
955
1068
  y,
956
1069
  width,
957
1070
  height,
958
1071
  rotation
959
- ].every(Number.isInteger)) invalid("malformed-command", "a layer's opacity and box are whole numbers");
960
- if (!isWhole(opacity, 0, 100)) invalid("invalid-opacity", "a layer's opacity is from 0 to 100");
961
- if (!isWhole(x, -65536, 65536) || !isWhole(y, -65536, 65536) || !isWhole(width, 1, 65536) || !isWhole(height, 1, 65536) || !isWhole(rotation, 0, 3599)) invalid("invalid-box", `a layer's box is at least 1 × 1, within ${MAX_SCENE_COORDINATE} pixels of the scene's origin, turned by 0 to 3599 tenths of a degree`);
1072
+ ].every(Number.isInteger)) invalid("malformed-command", "a layer's box is in whole numbers");
1073
+ if (!isCoordinate(x) || !isCoordinate(y) || !isWhole(width, 1, 65536) || !isWhole(height, 1, 65536) || !isWhole(rotation, 0, 3599)) invalid("invalid-box", `a layer's box is at least 1 × 1, within ${MAX_SCENE_COORDINATE} pixels of the scene's origin, turned by 0 to 3599 tenths of a degree`);
1074
+ const flipX = checkBoolean(value["flipX"], "a layer's flipX");
962
1075
  const common = {
963
- key,
964
- name,
965
- visible,
966
- opacity,
1076
+ ...shared,
967
1077
  x,
968
1078
  y,
969
1079
  width,
970
1080
  height,
971
1081
  rotation,
972
- flipX: checkBoolean(value["flipX"], "a layer's flipX")
1082
+ flipX
973
1083
  };
974
1084
  if (kind === "text") return {
975
1085
  ...common,
@@ -988,10 +1098,9 @@ function checkLayer(value, sourceSize) {
988
1098
  const given = value["stroke"];
989
1099
  if (!isRecord(given)) invalid("malformed-command", "a stroke is an object or null");
990
1100
  allFields(given, ["colour", "width"], "a stroke");
991
- if (!isWhole(given.width, 1, 1024)) invalid("malformed-command", `a stroke's width is from 1 to ${MAX_STROKE_WIDTH} pixels`);
992
1101
  stroke = {
993
1102
  colour: checkColour(given.colour),
994
- width: given.width
1103
+ width: checkWidth(given.width, "a stroke")
995
1104
  };
996
1105
  }
997
1106
  if (fill === null && stroke === null) invalid("empty-shape", "a shape has a fill or a stroke");
@@ -1018,15 +1127,13 @@ function checkLayer(value, sourceSize) {
1018
1127
  }
1019
1128
  function patchLayer(item, props, sourceSize) {
1020
1129
  if (!isRecord(props)) invalid("malformed-command", "an update's props are an object");
1021
- const allowed = /* @__PURE__ */ new Set([
1022
- ...COMMON.slice(2),
1023
- ...BOX,
1024
- ...OWN[item.kind]
1025
- ]);
1130
+ const notUpdated = NOT_UPDATED[item.kind] ?? [];
1131
+ const allowed = new Set([...COMMON.slice(2), ...OWN[item.kind]].filter((name) => !notUpdated.includes(name)));
1026
1132
  const before = item;
1027
1133
  const merged = { ...item };
1028
1134
  const previous = {};
1029
1135
  for (const name of Object.keys(props)) {
1136
+ if (notUpdated.includes(name)) invalid("malformed-command", `a ${item.kind} layer's "${name}" isn't set by an update`);
1030
1137
  if (!allowed.has(name)) {
1031
1138
  if (ANY_OWN.has(name)) invalid("wrong-kind", `a ${item.kind} layer has no "${name}"`);
1032
1139
  invalid("malformed-command", `a layer's props have no field "${name}"`);
@@ -1039,6 +1146,90 @@ function patchLayer(item, props, sourceSize) {
1039
1146
  previous
1040
1147
  };
1041
1148
  }
1149
+ // src/core/operations/strokes.ts
1150
+ function drawingOf(plan, key) {
1151
+ if (typeof key !== "string") invalid("malformed-command", "a key is text");
1152
+ const at = plan.layers.findIndex((layer) => layer.key === key);
1153
+ const layer = plan.layers[at];
1154
+ if (layer === void 0) invalid("unknown-layer", `no layer has the key "${key}"`);
1155
+ if (layer.kind !== "drawing") invalid("wrong-kind", `a ${layer.kind} layer has no strokes`);
1156
+ return {
1157
+ at,
1158
+ drawing: layer
1159
+ };
1160
+ }
1161
+ function withStrokes(plan, at, strokes) {
1162
+ return {
1163
+ ...plan,
1164
+ layers: plan.layers.map((layer, index) => index === at && layer.kind === "drawing" ? {
1165
+ ...layer,
1166
+ strokes
1167
+ } : layer)
1168
+ };
1169
+ }
1170
+ function some(value, what) {
1171
+ if (!Array.isArray(value) || value.length === 0) invalid("malformed-command", `${what} is a list of at least one`);
1172
+ return value;
1173
+ }
1174
+ function addStrokes(plan, key, value) {
1175
+ const { at, drawing } = drawingOf(plan, key);
1176
+ const strokes = some(value, "strokes").map((stroke) => checkStroke(stroke, drawing));
1177
+ const before = drawing.strokes.length;
1178
+ if (before + strokes.length > 1024) invalid("too-many-strokes", `a drawing has at most ${MAX_DRAWING_STROKES} strokes`);
1179
+ return {
1180
+ state: withStrokes(plan, at, [...drawing.strokes, ...strokes]),
1181
+ inverse: {
1182
+ type: "image.removeStrokes",
1183
+ key: drawing.key,
1184
+ indexes: strokes.map((_, index) => before + index)
1185
+ }
1186
+ };
1187
+ }
1188
+ function removeStrokes(plan, key, value) {
1189
+ const { at, drawing } = drawingOf(plan, key);
1190
+ const indexes = some(value, "indexes");
1191
+ if (!indexes.every((index) => Number.isSafeInteger(index) && index >= 0)) invalid("malformed-command", "an index is a whole number from 0");
1192
+ const chosen = new Set(indexes);
1193
+ if (chosen.size !== indexes.length) invalid("invalid-index", "an index is listed once");
1194
+ for (const index of chosen) if (index >= drawing.strokes.length) invalid("invalid-index", `the drawing has no stroke ${index}`);
1195
+ if (chosen.size === drawing.strokes.length) invalid("empty-drawing", "a drawing keeps at least one stroke; remove the layer instead");
1196
+ return {
1197
+ state: withStrokes(plan, at, drawing.strokes.filter((_, index) => !chosen.has(index))),
1198
+ inverse: {
1199
+ type: "image.restoreStrokes",
1200
+ key: drawing.key,
1201
+ entries: [...chosen].sort((a, b) => a - b).map((index) => ({
1202
+ at: index,
1203
+ stroke: drawing.strokes[index]
1204
+ }))
1205
+ }
1206
+ };
1207
+ }
1208
+ function restoreStrokes(plan, key, value) {
1209
+ const { at, drawing } = drawingOf(plan, key);
1210
+ const strokes = [...drawing.strokes];
1211
+ let last = -1;
1212
+ const indexes = [];
1213
+ for (const entry of some(value, "entries")) {
1214
+ if (!isRecord(entry)) invalid("malformed-command", "an entry is an object");
1215
+ allFields(entry, ["at", "stroke"], "an entry");
1216
+ const index = entry.at;
1217
+ if (!Number.isSafeInteger(index) || index < 0) invalid("malformed-command", "an index is a whole number from 0");
1218
+ if (index <= last || index > strokes.length) invalid("invalid-index", "entries are by rising index, each within the drawing");
1219
+ last = index;
1220
+ strokes.splice(last, 0, checkStroke(entry.stroke, drawing));
1221
+ indexes.push(last);
1222
+ }
1223
+ if (strokes.length > 1024) invalid("too-many-strokes", `a drawing has at most ${MAX_DRAWING_STROKES} strokes`);
1224
+ return {
1225
+ state: withStrokes(plan, at, strokes),
1226
+ inverse: {
1227
+ type: "image.removeStrokes",
1228
+ key: drawing.key,
1229
+ indexes
1230
+ }
1231
+ };
1232
+ }
1042
1233
  // src/core/operations/commands.ts
1043
1234
  function checkOrientation(value) {
1044
1235
  if (!isRecord(value)) invalid("malformed-command", "an orientation is an object");
@@ -1157,6 +1348,21 @@ var COMMAND_FIELDS = {
1157
1348
  "image.updateLayers": ["type", "updates"],
1158
1349
  "image.restoreLayers": ["type", "entries"],
1159
1350
  "image.setLayerOrder": ["type", "keys"],
1351
+ "image.addStrokes": [
1352
+ "type",
1353
+ "key",
1354
+ "strokes"
1355
+ ],
1356
+ "image.removeStrokes": [
1357
+ "type",
1358
+ "key",
1359
+ "indexes"
1360
+ ],
1361
+ "image.restoreStrokes": [
1362
+ "type",
1363
+ "key",
1364
+ "entries"
1365
+ ],
1160
1366
  "image.insertRedactions": [
1161
1367
  "type",
1162
1368
  "at",
@@ -1412,6 +1618,9 @@ function apply(plan, input, sourceSize) {
1412
1618
  }
1413
1619
  };
1414
1620
  }
1621
+ case "image.addStrokes": return addStrokes(plan, command.key, command.strokes);
1622
+ case "image.removeStrokes": return removeStrokes(plan, command.key, command.indexes);
1623
+ case "image.restoreStrokes": return restoreStrokes(plan, command.key, command.entries);
1415
1624
  case "image.insertRedactions": {
1416
1625
  const redactions = checkRedactions(command.redactions);
1417
1626
  const { list, keys } = insertItems(plan.redactions, command.at, redactions, planKeys(plan));
@@ -1767,6 +1976,160 @@ function nudgeCropOnScreen(crop, key, step, resize, orientation, source, ratio)
1767
1976
  if (!resize) return moveCrop(crop, moved.dx, moved.dy, source);
1768
1977
  return dragHandle(crop, handleInSource(dx === 0 ? "s" : "e", orientation), moved.dx, moved.dy, source, ratio);
1769
1978
  }
1979
+ // src/core/model/drawing-geometry.ts
1980
+ function simplifyRun(xs, ys, tolerance) {
1981
+ const count = xs.length;
1982
+ const keep = new Uint8Array(count);
1983
+ keep[0] = 1;
1984
+ keep[count - 1] = 1;
1985
+ const limit = BigInt(tolerance) * BigInt(tolerance);
1986
+ const stack = [0, count - 1];
1987
+ while (stack.length > 0) {
1988
+ const last = stack.pop();
1989
+ const first = stack.pop();
1990
+ if (last - first < 2) continue;
1991
+ const ax = xs[first];
1992
+ const ay = ys[first];
1993
+ const dx = xs[last] - ax;
1994
+ const dy = ys[last] - ay;
1995
+ const length = dx * dx + dy * dy;
1996
+ let farthest = -1;
1997
+ let most = -1n;
1998
+ for (let index = first + 1; index < last; index += 1) {
1999
+ const px = xs[index] - ax;
2000
+ const py = ys[index] - ay;
2001
+ let measure;
2002
+ if (length === 0) measure = BigInt(px * px + py * py);
2003
+ else {
2004
+ const along = px * dx + py * dy;
2005
+ if (along <= 0) measure = BigInt(px * px + py * py) * BigInt(length);
2006
+ else if (along >= length) {
2007
+ const qx = px - dx;
2008
+ const qy = py - dy;
2009
+ measure = BigInt(qx * qx + qy * qy) * BigInt(length);
2010
+ } else {
2011
+ const cross = BigInt(px * dy - py * dx);
2012
+ measure = cross * cross;
2013
+ }
2014
+ }
2015
+ if (measure > most) {
2016
+ most = measure;
2017
+ farthest = index;
2018
+ }
2019
+ }
2020
+ if (most > (length === 0 ? limit : limit * BigInt(length))) {
2021
+ keep[farthest] = 1;
2022
+ stack.push(first, farthest, farthest, last);
2023
+ }
2024
+ }
2025
+ const points = [];
2026
+ for (let index = 0; index < count; index += 1) if (keep[index] === 1) points.push(xs[index], ys[index]);
2027
+ return points;
2028
+ }
2029
+ function simplifyStroke(points, tolerance) {
2030
+ if (points.length < 2 || points.length % 2 !== 0) throw new RangeError("A stroke's samples are an x and a y for each of at least one point.");
2031
+ if (!Number.isSafeInteger(tolerance) || tolerance < 0) throw new RangeError("A tolerance is a whole number of pixels, from 0.");
2032
+ const xs = [];
2033
+ const ys = [];
2034
+ for (let index = 0; index < points.length; index += 2) {
2035
+ const x = points[index];
2036
+ const y = points[index + 1];
2037
+ if (!Number.isSafeInteger(x) || !Number.isSafeInteger(y)) throw new RangeError("A stroke's samples are in whole pixels.");
2038
+ const previous = xs.length - 1;
2039
+ if (previous >= 0 && xs[previous] === x && ys[previous] === y) continue;
2040
+ xs.push(x);
2041
+ ys.push(y);
2042
+ }
2043
+ if (xs.length === 1) return [[xs[0], ys[0]]];
2044
+ const runs = [];
2045
+ for (let start = 0; start < xs.length - 1; start += MAX_RUN_POINTS - 1) {
2046
+ if (runs.length === 64) throw new RangeError(`A stroke holds at most 64 runs of samples.`);
2047
+ const end = Math.min(start + MAX_RUN_POINTS, xs.length);
2048
+ runs.push(simplifyRun(xs.slice(start, end), ys.slice(start, end), tolerance));
2049
+ }
2050
+ return runs;
2051
+ }
2052
+ function arrowOutline(from, to, width) {
2053
+ const dx = to.x - from.x;
2054
+ const dy = to.y - from.y;
2055
+ const length = Math.hypot(dx, dy);
2056
+ if (!(length > 0)) throw new RangeError("An arrow's ends differ.");
2057
+ const ux = dx / length;
2058
+ const uy = dy / length;
2059
+ const lx = uy;
2060
+ const ly = -ux;
2061
+ const head = Math.min(4 * width, length);
2062
+ const half = head / 2;
2063
+ const shaft = Math.min(width / 2, half);
2064
+ const base = {
2065
+ x: to.x - ux * head,
2066
+ y: to.y - uy * head
2067
+ };
2068
+ const at = (origin, left) => ({
2069
+ x: origin.x + lx * left || 0,
2070
+ y: origin.y + ly * left || 0
2071
+ });
2072
+ return [
2073
+ at(from, shaft),
2074
+ at(from, -shaft),
2075
+ at(base, -shaft),
2076
+ at(base, -half),
2077
+ {
2078
+ x: to.x,
2079
+ y: to.y
2080
+ },
2081
+ at(base, half),
2082
+ at(base, shaft)
2083
+ ];
2084
+ }
2085
+ var around = (left, top, right, bottom) => {
2086
+ const x = Math.floor(left);
2087
+ const y = Math.floor(top);
2088
+ return {
2089
+ x,
2090
+ y,
2091
+ width: Math.ceil(right) - x,
2092
+ height: Math.ceil(bottom) - y
2093
+ };
2094
+ };
2095
+ function drawingBounds(drawing) {
2096
+ let left = Number.POSITIVE_INFINITY;
2097
+ let top = Number.POSITIVE_INFINITY;
2098
+ let right = Number.NEGATIVE_INFINITY;
2099
+ let bottom = Number.NEGATIVE_INFINITY;
2100
+ for (const stroke of drawing.strokes) {
2101
+ const reach = stroke.width / 2;
2102
+ for (const run of stroke.runs) for (let index = 0; index + 1 < run.length; index += 2) {
2103
+ const x = drawing.x + run[index];
2104
+ const y = drawing.y + run[index + 1];
2105
+ left = Math.min(left, x - reach);
2106
+ top = Math.min(top, y - reach);
2107
+ right = Math.max(right, x + reach);
2108
+ bottom = Math.max(bottom, y + reach);
2109
+ }
2110
+ }
2111
+ if (left > right) return {
2112
+ x: drawing.x,
2113
+ y: drawing.y,
2114
+ width: 0,
2115
+ height: 0
2116
+ };
2117
+ return around(left, top, right, bottom);
2118
+ }
2119
+ function arrowBounds(arrow) {
2120
+ const outline = arrowOutline(arrow.from, arrow.to, arrow.width);
2121
+ const xs = outline.map((point) => point.x);
2122
+ const ys = outline.map((point) => point.y);
2123
+ return around(Math.min(...xs), Math.min(...ys), Math.max(...xs), Math.max(...ys));
2124
+ }
2125
+ function drawingPoints(plan) {
2126
+ let points = 0;
2127
+ for (const layer of plan.layers) {
2128
+ if (layer.kind !== "drawing") continue;
2129
+ for (const stroke of layer.strokes) for (const run of stroke.runs) points += run.length / 2;
2130
+ }
2131
+ return points;
2132
+ }
1770
2133
  // src/core/model/keys.ts
1771
2134
  function createImageKey(used) {
1772
2135
  for (;;) {
@@ -2098,6 +2461,18 @@ function sourcesIn(command) {
2098
2461
  default: return [];
2099
2462
  }
2100
2463
  }
2464
+ var MAX_HISTORY_POINTS = 1e6;
2465
+ function pointsIn(command) {
2466
+ const ofStrokes = (strokes) => strokes.reduce((points, stroke) => points + stroke.runs.reduce((sum, run) => sum + run.length / 2, 0), 0);
2467
+ const ofLayers = (layers) => layers.reduce((points, layer) => points + (layer.kind === "drawing" ? ofStrokes(layer.strokes) : 0), 0);
2468
+ switch (command.type) {
2469
+ case "image.addStrokes": return ofStrokes(command.strokes);
2470
+ case "image.restoreStrokes": return ofStrokes(command.entries.map((entry) => entry.stroke));
2471
+ case "image.insertLayers": return ofLayers(command.layers);
2472
+ case "image.restoreLayers": return ofLayers(command.entries.map((entry) => entry.layer));
2473
+ default: return 0;
2474
+ }
2475
+ }
2101
2476
  var sourcesOf = (plan) => plan.layers.flatMap((layer) => layer.kind === "image" ? [layer.source] : []);
2102
2477
  function defaultLayer(source, plan, base) {
2103
2478
  return {
@@ -2278,6 +2653,8 @@ function createImageSession(options) {
2278
2653
  if (source) checkOutputLimits(policy, outputSize(state.output, compositionSize(state, source)));
2279
2654
  const items = state.layers.length + state.redactions.length;
2280
2655
  if (items > from.layers.length + from.redactions.length) policy.check("maxLayers", items);
2656
+ const points = drawingPoints(state);
2657
+ if (points > drawingPoints(from)) policy.check("maxDrawingPoints", points);
2281
2658
  return {
2282
2659
  state,
2283
2660
  step: {
@@ -2286,6 +2663,21 @@ function createImageSession(options) {
2286
2663
  }
2287
2664
  };
2288
2665
  };
2666
+ const notePoints = () => {
2667
+ let near = false;
2668
+ try {
2669
+ near = plan !== null && policy.check("maxDrawingPoints", drawingPoints(plan)) === "warning";
2670
+ } catch {
2671
+ near = true;
2672
+ }
2673
+ const noted = warnings.includes("maxDrawingPoints");
2674
+ if (near && !noted) warnings = [...warnings, "maxDrawingPoints"];
2675
+ if (!near && noted) warnings = warnings.filter((name) => name !== "maxDrawingPoints");
2676
+ };
2677
+ const trimPoints = () => {
2678
+ const held = () => history.steps.reduce((points, step) => points + [...step.commands, ...step.inverses].reduce((sum, c) => sum + pointsIn(c), 0), 0);
2679
+ while (history.dropOrder.length > 1 && held() > MAX_HISTORY_POINTS) history.drop(1);
2680
+ };
2289
2681
  const replay = (direction) => {
2290
2682
  assertAlive();
2291
2683
  const step = direction === "undo" ? history.undo() : history.redo();
@@ -2304,6 +2696,7 @@ function createImageSession(options) {
2304
2696
  throw fail(reason);
2305
2697
  }
2306
2698
  error = null;
2699
+ notePoints();
2307
2700
  releaseUnreferenced();
2308
2701
  publish();
2309
2702
  emit({
@@ -2321,6 +2714,8 @@ function createImageSession(options) {
2321
2714
  const result = attempt(plan, commands);
2322
2715
  plan = result.state;
2323
2716
  history.push(result.step);
2717
+ trimPoints();
2718
+ notePoints();
2324
2719
  error = null;
2325
2720
  releaseUnreferenced();
2326
2721
  emit({
@@ -2595,6 +2990,7 @@ var imageMessages = /* @__PURE__ */ defineMessages({
2595
2990
  one: "An image can have {maximum} layer. Merge or delete layers, and try again.",
2596
2991
  other: "An image can have up to {maximum, number} layers. Merge or delete layers, and try again."
2597
2992
  }),
2993
+ "errors.TOO_MANY_POINTS": "This image's drawings would have {actual, number} points, which is over the limit of {maximum, number}. Remove some strokes, and try again.",
2598
2994
  "warnings.maxImagePixels": "This image is large, and may be slow to edit on some devices.",
2599
2995
  "warnings.maxImageDimension": "This image is very wide or tall, and may be slow to edit.",
2600
2996
  "warnings.maxFrames": /* @__PURE__ */ plural("actual", {
@@ -2605,8 +3001,9 @@ var imageMessages = /* @__PURE__ */ defineMessages({
2605
3001
  "warnings.maxLayers": /* @__PURE__ */ plural("actual", {
2606
3002
  one: "This image has {actual} layer.",
2607
3003
  other: "This image has {actual, number} layers, and may be slow to edit and export."
2608
- })
3004
+ }),
3005
+ "warnings.maxDrawingPoints": "This image has a lot of drawing, and may be slow to edit and export."
2609
3006
  });
2610
- export { NO_METADATA as $, compositionSize as A, imageLimitDefinitions as At, checkOutput as B, cropInComposition as C, createImageProcessor as Ct, moveCrop as D, PER_FILE_LIMITS as Dt, handleInSource as E, OUTPUT_LIMITS as Et, sourceToComposition as F, Invalid as G, FORBIDDEN_IN_TEXT as H, checkDevelop as I, invalid as J, checkCrop as K, checkFilter as L, cropRect as M, asImageError as Mt, orientedSize as N, ImageError as Nt, nudgeCropOnScreen as O, checkFileLimits as Ot, outputSize as P, isImageError as Pt, MAX_METADATA_LENGTH as Q, checkMetadata as R, compositionDeltaToSource as S, placement as St, fitAspect as T, engineInternals as Tt, checkLayer as U, createImageReducer as V, checkRedaction as W, onlyFields as X, isRecord as Y, EMPTY_KEEPABLE_METADATA as Z, sceneToLayerSource as _, createPlan as _t, fitRedactionRegion as a, LAYER_FONTS as at, ASPECT_PRESETS as b, backingSize as bt, redactionBlocksInScene as c, MAX_KEY_LENGTH as ct, applyMatrix as d, MAX_PLAN_ITEMS as dt, longerThan as et, fitTextLines as f, MAX_SCENE_COORDINATE as ft, layerToScene as g, UPRIGHT as gt, layerToOutput as h, REDACTION_MODES as ht, compositionRectToScene as i, LAYER_ALIGNMENTS as it, compositionToSource as j, imageLimits as jt, wholeCrop as k, checkOutputLimits as kt, redactionRectInOutput as l, MAX_LAYER_NAME_LENGTH as lt, isRightToLeft as m, NEUTRAL_DEVELOP as mt, createImageSession as n, FILTER_PRESETS as nt, redactionBlockSize as o, LAYER_KINDS as ot, invertMatrix as p, MAX_STROKE_WIDTH as pt, checkCropInSource as q, newLayerBox as r, FULL_SIZE as rt, redactionBlocksInOutput as s, LAYER_SHAPES as st, imageMessages as t, DEVELOP_ADJUSTMENTS as tt, sceneRectToComposition as u, MAX_LAYER_TEXT_LENGTH as ut, sceneToOutput as v, createImageRenderer as vt, dragHandle as w, createImageEngine as wt, aspectRatio as x, fittedView as xt, createImageKey as y, MAX_PIXEL_RATIO as yt, checkOrientation as z };
3007
+ export { invalid as $, fitAspect as A, placement as At, sourceToComposition as B, asImageError as Bt, drawingPoints as C, REDACTION_MODES as Ct, compositionDeltaToSource as D, MAX_PIXEL_RATIO as Dt, aspectRatio as E, createImageRenderer as Et, compositionSize as F, PER_FILE_LIMITS as Ft, checkOutput as G, checkFilter as H, isImageError as Ht, compositionToSource as I, checkFileLimits as It, checkLayer as J, createImageReducer as K, cropRect as L, checkOutputLimits as Lt, moveCrop as M, createImageEngine as Mt, nudgeCropOnScreen as N, engineInternals as Nt, cropInComposition as O, backingSize as Ot, wholeCrop as P, OUTPUT_LIMITS as Pt, checkCropInSource as Q, orientedSize as R, imageLimitDefinitions as Rt, drawingBounds as S, NEUTRAL_DEVELOP as St, ASPECT_PRESETS as T, createPlan as Tt, checkMetadata as U, checkDevelop as V, ImageError as Vt, checkOrientation as W, Invalid as X, checkRedaction as Y, checkCrop as Z, sceneToLayerSource as _, MAX_PLAN_ITEMS as _t, fitRedactionRegion as a, longerThan as at, arrowBounds as b, MAX_STROKE_RUNS as bt, redactionBlocksInScene as c, FULL_SIZE as ct, applyMatrix as d, LAYER_KINDS as dt, isRecord as et, fitTextLines as f, LAYER_SHAPES as ft, layerToScene as g, MAX_LAYER_TEXT_LENGTH as gt, layerToOutput as h, MAX_LAYER_NAME_LENGTH as ht, compositionRectToScene as i, NO_METADATA as it, handleInSource as j, createImageProcessor as jt, dragHandle as k, fittedView as kt, redactionRectInOutput as l, LAYER_ALIGNMENTS as lt, isRightToLeft as m, MAX_KEY_LENGTH as mt, createImageSession as n, EMPTY_KEEPABLE_METADATA as nt, redactionBlockSize as o, DEVELOP_ADJUSTMENTS as ot, invertMatrix as p, MAX_DRAWING_STROKES as pt, FORBIDDEN_IN_TEXT as q, newLayerBox as r, MAX_METADATA_LENGTH as rt, redactionBlocksInOutput as s, FILTER_PRESETS as st, imageMessages as t, onlyFields as tt, sceneRectToComposition as u, LAYER_FONTS as ut, sceneToOutput as v, MAX_RUN_POINTS as vt, simplifyStroke as w, UPRIGHT as wt, arrowOutline as x, MAX_STROKE_WIDTH as xt, createImageKey as y, MAX_SCENE_COORDINATE as yt, outputSize as z, imageLimits as zt };
2611
3008
 
2612
- //# sourceMappingURL=core-BlFeJWak.js.map
3009
+ //# sourceMappingURL=core-DKUm3jGZ.js.map