imagescript 3.17.0 → 3.18.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.
package/dist/image.js CHANGED
@@ -37,17 +37,17 @@ import { GainMap } from "./gainmap.js";
37
37
  import * as gain from "./util/gain.js";
38
38
  import * as gainMapCodec from "./codecs/gainmap.js";
39
39
  export class ResizeModes {
40
- static NEAREST_NEIGHBOR = 'RESIZE_NEAREST_NEIGHBOR';
41
- static BILINEAR = 'RESIZE_BILINEAR';
42
- static BICUBIC = 'RESIZE_BICUBIC';
43
- static AREA = 'RESIZE_AREA';
40
+ static NEAREST_NEIGHBOR = 'nearest-neighbor';
41
+ static BILINEAR = 'bilinear';
42
+ static BICUBIC = 'bicubic';
43
+ static AREA = 'area';
44
44
  static AUTO = -1;
45
45
  constructor() { }
46
46
  }
47
47
  export class SVGScaleModes {
48
- static SCALE = 1;
49
- static WIDTH = 2;
50
- static HEIGHT = 3;
48
+ static SCALE = 'scale';
49
+ static WIDTH = 'width';
50
+ static HEIGHT = 'height';
51
51
  constructor() { }
52
52
  }
53
53
  function featureList(features) {
@@ -115,18 +115,24 @@ export class Image {
115
115
  get height() {
116
116
  return this.__height__;
117
117
  }
118
- *[Symbol.iterator]() {
118
+ *coordinates() {
119
119
  for (let y = 0; y < this.height; y++)
120
120
  for (let x = 0; x < this.width; x++)
121
121
  yield [x, y];
122
122
  }
123
- *iterateWithColors() {
123
+ *pixels() {
124
124
  const view = new DataView(this.bitmap.buffer, this.bitmap.byteOffset, this.bitmap.byteLength);
125
125
  let offset = 0;
126
126
  for (let y = 0; y < this.height; y++)
127
127
  for (let x = 0; x < this.width; x++, offset += 4)
128
128
  yield [x, y, view.getUint32(offset, false)];
129
129
  }
130
+ *[Symbol.iterator]() {
131
+ yield* this.coordinates();
132
+ }
133
+ *iterateWithColors() {
134
+ yield* this.pixels();
135
+ }
130
136
  static rgbaToColor(r, g, b, a) {
131
137
  return Colors.fromRGBA(r, g, b, a);
132
138
  }
@@ -220,10 +226,10 @@ export class Image {
220
226
  image.__gain_map__ = this.__gain_map__ ? this.__gain_map__.clone() : null;
221
227
  return image;
222
228
  }
223
- static RESIZE_NEAREST_NEIGHBOR = ResizeModes.NEAREST_NEIGHBOR;
224
- static RESIZE_BILINEAR = ResizeModes.BILINEAR;
225
- static RESIZE_BICUBIC = ResizeModes.BICUBIC;
226
- static RESIZE_AREA = ResizeModes.AREA;
229
+ static RESIZE_NEAREST_NEIGHBOR = 'RESIZE_NEAREST_NEIGHBOR';
230
+ static RESIZE_BILINEAR = 'RESIZE_BILINEAR';
231
+ static RESIZE_BICUBIC = 'RESIZE_BICUBIC';
232
+ static RESIZE_AREA = 'RESIZE_AREA';
227
233
  static RESIZE_AUTO = ResizeModes.AUTO;
228
234
  scale(factor, modeOrOptions) {
229
235
  const { mode, fast } = resizeOptions(modeOrOptions);
@@ -236,7 +242,8 @@ export class Image {
236
242
  return this;
237
243
  return this.__resize__(this.width * factor, this.height * factor, mode, fast);
238
244
  }
239
- resize(width, height, modeOrOptions) {
245
+ resize(widthOrOptions, heightArgument, modeArgument) {
246
+ const [width, height, modeOrOptions] = resizeArguments(widthOrOptions, heightArgument, modeArgument);
240
247
  const { mode, fast } = resizeOptions(modeOrOptions);
241
248
  const image = this.__resize__(width, height, mode, fast);
242
249
  this.__gain_map__?.__rescale__(this.width, this.height, image.width, image.height);
@@ -341,7 +348,8 @@ export class Image {
341
348
  throw new RangeError('Crop region does not overlap the image');
342
349
  return [left, top, right - left, bottom - top];
343
350
  }
344
- trim(color, options = {}) {
351
+ trim(colorOrOptions, optionsArgument = {}) {
352
+ const { color, ...options } = isOptions(colorOrOptions) ? colorOrOptions : { ...optionsArgument, color: colorOrOptions ?? optionsArgument.color };
345
353
  const bounds = this.__trim_bounds__(color, options);
346
354
  if (!bounds)
347
355
  return this;
@@ -477,10 +485,12 @@ export class Image {
477
485
  b[i + 3] = a * 255;
478
486
  }
479
487
  cropCircle(maxOrOptions = false, featheringArgument = 0) {
480
- const { max = false, feathering = 0 } = typeof maxOrOptions === 'object' && maxOrOptions !== null
488
+ const { max = false, size = max ? 'cover' : 'contain', feathering = 0 } = typeof maxOrOptions === 'object' && maxOrOptions !== null
481
489
  ? maxOrOptions : { max: maxOrOptions, feathering: featheringArgument };
482
490
  finite(feathering, 'feathering');
483
- const radius = (max ? Math.max : Math.min)(this.width, this.height) / 2;
491
+ if (size !== 'contain' && size !== 'cover')
492
+ throw new TypeError(`Invalid circle size: ${String(size)}`);
493
+ const radius = (size === 'cover' ? Math.max : Math.min)(this.width, this.height) / 2;
484
494
  const cx = this.width / 2, cy = this.height / 2;
485
495
  const band = Math.min(1, Math.max(0, feathering)) * radius;
486
496
  for (let y = 0; y < this.height; y++) {
@@ -847,8 +857,9 @@ export class Image {
847
857
  }
848
858
  return this;
849
859
  }
850
- colorToAlpha(color, options = {}) {
851
- const { transparencyThreshold = 0, opacityThreshold = 1 } = options;
860
+ colorToAlpha(colorOrOptions, optionsArgument = {}) {
861
+ const { color: colorOption, transparencyThreshold = 0, opacityThreshold = 1 } = isOptions(colorOrOptions) ? colorOrOptions : { ...optionsArgument, color: colorOrOptions ?? optionsArgument.color };
862
+ let color = colorOption;
852
863
  if (!(transparencyThreshold >= 0 && opacityThreshold <= 1 && transparencyThreshold < opacityThreshold))
853
864
  throw new RangeError('Invalid thresholds, they have to satisfy 0 <= transparencyThreshold < opacityThreshold <= 1');
854
865
  const bitmap = this.bitmap;
@@ -1106,7 +1117,7 @@ export class Image {
1106
1117
  });
1107
1118
  return this.__apply__(r);
1108
1119
  }
1109
- async encode(compressionOrOptions = 4, metadataArgument = {}) {
1120
+ async encodePNG(compressionOrOptions = 4, metadataArgument = {}) {
1110
1121
  let compression = 4, metadata = metadataArgument;
1111
1122
  if (!isOptions(compressionOrOptions))
1112
1123
  compression = compressionOrOptions;
@@ -1116,8 +1127,8 @@ export class Image {
1116
1127
  metadata = compressionOrOptions;
1117
1128
  return png.encodeRGBA(this.__bytes__(), this.width, this.height, compressionLevel(compression), pngText(resolveMetadata(metadata)));
1118
1129
  }
1119
- encodePNG(compressionOrOptions, metadata) {
1120
- return this.encode(compressionOrOptions, metadata);
1130
+ encode(compressionOrOptions, metadata) {
1131
+ return this.encodePNG(compressionOrOptions, metadata);
1121
1132
  }
1122
1133
  async encodeJPEG(qualityOrOptions = 90) {
1123
1134
  const { quality = 90, metadata = {}, gainMap: gainMapOption = true } = isOptions(qualityOrOptions) ? qualityOrOptions : { quality: qualityOrOptions };
@@ -1326,12 +1337,13 @@ export class Image {
1326
1337
  }
1327
1338
  return this.__apply__(out);
1328
1339
  }
1329
- static SVG_MODE_SCALE = SVGScaleModes.SCALE;
1330
- static SVG_MODE_WIDTH = SVGScaleModes.WIDTH;
1331
- static SVG_MODE_HEIGHT = SVGScaleModes.HEIGHT;
1340
+ static SVG_MODE_SCALE = 1;
1341
+ static SVG_MODE_WIDTH = 2;
1342
+ static SVG_MODE_HEIGHT = 3;
1332
1343
  static async renderSVG(svg, sizeOrOptions = 1, modeArgument = SVGScaleModes.SCALE) {
1333
- const { size = 1, mode = SVGScaleModes.SCALE, fonts = [] } = isOptions(sizeOrOptions) ? sizeOrOptions : { size: sizeOrOptions, mode: modeArgument };
1334
- if (![1, 2, 3].includes(mode))
1344
+ const { size = 1, mode: givenMode = SVGScaleModes.SCALE, fonts = [] } = isOptions(sizeOrOptions) ? sizeOrOptions : { size: sizeOrOptions, mode: modeArgument };
1345
+ const mode = LEGACY_SVG_MODES.get(givenMode) ?? givenMode;
1346
+ if (mode !== 'scale' && mode !== 'width' && mode !== 'height')
1335
1347
  throw new Error('Invalid SVG scaling mode');
1336
1348
  finite(size, 'SVG size');
1337
1349
  if (mode === SVGScaleModes.SCALE && size <= 0)
@@ -1361,9 +1373,22 @@ export class Image {
1361
1373
  image.bitmap.set(pixels);
1362
1374
  return image;
1363
1375
  }
1364
- static async renderText(font, size, text, colorOrOptions = 0xffffffff, layoutArgument, styleArgument = {}) {
1365
- const options = isOptions(colorOrOptions) ? colorOrOptions : { ...styleArgument, color: colorOrOptions, layout: layoutArgument };
1366
- const { color = 0xffffffff, layout = new TextLayout(), strokeWidth = 0, strokeColor = 0x000000ff, hinting = true, features = {}, variations = {} } = options;
1376
+ static async renderText(fontOrText, sizeOrOptions, textArgument, colorOrOptions = 0xffffffff, layoutArgument, styleArgument = {}) {
1377
+ let font, size, text, options;
1378
+ if (typeof fontOrText === 'string') {
1379
+ if (!isOptions(sizeOrOptions) || sizeOrOptions.font === undefined)
1380
+ throw new TypeError('renderText(text, options) needs the font in the options');
1381
+ ({ font, size, ...options } = sizeOrOptions);
1382
+ text = fontOrText;
1383
+ }
1384
+ else {
1385
+ font = fontOrText;
1386
+ size = sizeOrOptions;
1387
+ text = textArgument;
1388
+ options = isOptions(colorOrOptions) ? colorOrOptions : { ...styleArgument, color: colorOrOptions, layout: layoutArgument };
1389
+ }
1390
+ const { color = 0xffffffff, strokeWidth = 0, strokeColor = 0x000000ff, hinting = true, features = {}, variations = {} } = options;
1391
+ const layout = options.layout instanceof TextLayout ? options.layout : new TextLayout(options.layout);
1367
1392
  const axes = textRenderer.axisValues(variations);
1368
1393
  size = finite(size, 'font size');
1369
1394
  if (size < 0)
@@ -1449,10 +1474,23 @@ function circleCoverage(px, py, cx, cy, radius) {
1449
1474
  return 0;
1450
1475
  return pixelCoverage(px, py, (x, y) => (x - cx) ** 2 + (y - cy) ** 2 <= radius * radius);
1451
1476
  }
1477
+ const LEGACY_RESIZE_MODES = new Map([
1478
+ ['RESIZE_NEAREST_NEIGHBOR', 'nearest-neighbor'], ['RESIZE_BILINEAR', 'bilinear'], ['RESIZE_BICUBIC', 'bicubic'], ['RESIZE_AREA', 'area']
1479
+ ]);
1480
+ export function resizeArguments(widthOrOptions, height, modeOrOptions) {
1481
+ if (!isOptions(widthOrOptions))
1482
+ return [widthOrOptions, height, modeOrOptions];
1483
+ const { width, height: optionsHeight, ...options } = widthOrOptions;
1484
+ if (width === undefined && optionsHeight === undefined)
1485
+ throw new TypeError('resize() needs a width, a height or both');
1486
+ return [width ?? ResizeModes.AUTO, optionsHeight ?? ResizeModes.AUTO, options];
1487
+ }
1452
1488
  function resizeOptions(modeOrOptions) {
1453
1489
  const options = isOptions(modeOrOptions) ? modeOrOptions : { mode: modeOrOptions };
1454
- return { mode: options.mode ?? ResizeModes.BILINEAR, fast: options.fast ?? true };
1490
+ const mode = options.mode ?? ResizeModes.BILINEAR;
1491
+ return { mode: LEGACY_RESIZE_MODES.get(mode) ?? mode, fast: options.fast ?? true };
1455
1492
  }
1493
+ const LEGACY_SVG_MODES = new Map([[1, 'scale'], [2, 'width'], [3, 'height']]);
1456
1494
  const hardLight = (b, s) => s <= 0.5 ? b * 2 * s : 1 - (1 - b) * (1 - (2 * s - 1));
1457
1495
  const BLEND_MODES = {
1458
1496
  'normal': (_b, s) => s,
package/dist/index.d.ts CHANGED
@@ -4,11 +4,11 @@ import { GIF } from './gif.js';
4
4
  import { Animation, AnimationDecodeOptions } from './animation.js';
5
5
  export { Image, ResizeModes, SVGScaleModes } from './image.js';
6
6
  export { Colors } from './colors.js';
7
- export type { Color, ColorFunction, ResizeMode, SVGScaleMode, PNGMetadata, TextStyle, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, AdjustOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, AVIFEncodeOptions, JXLEncodeOptions, EncodeOptions, SVGOptions, TextOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
7
+ export type { Color, ColorFunction, ResizeMode, SVGScaleMode, PNGMetadata, TextStyle, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, ResizeToOptions, AdjustOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, AVIFEncodeOptions, JXLEncodeOptions, EncodeOptions, SVGOptions, TextOptions, RenderTextOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
8
8
  export { Frame, FrameDisposalModes } from './frame.js';
9
9
  export type { FrameDisposalModeName, FrameDisposalModeId, FrameOptions } from './frame.js';
10
10
  export { GainMap } from './gainmap.js';
11
- export type { GainMapOptions } from './gainmap.js';
11
+ export type { GainMapOptions, GainMapEncodeOptions } from './gainmap.js';
12
12
  export { GIF } from './gif.js';
13
13
  export { Animation } from './animation.js';
14
14
  export type { AnimationOptions, AnimationDecodeOptions, AnimationEncodeOptions, WebPAnimationEncodeOptions, GIFEncodeOptions, APNGEncodeOptions } from './animation.js';
@@ -63,11 +63,11 @@ export function render(source, mode, size, images = new Map(), fonts = []) {
63
63
  if (!(width > 0) || !(height > 0))
64
64
  throw new Error('SVG: invalid dimensions');
65
65
  let outWidth, outHeight;
66
- if (mode === 2) {
66
+ if (mode === 'width') {
67
67
  outWidth = Math.round(size);
68
68
  outHeight = Math.round(height * size / width);
69
69
  }
70
- else if (mode === 3) {
70
+ else if (mode === 'height') {
71
71
  outHeight = Math.round(size);
72
72
  outWidth = Math.round(width * size / height);
73
73
  }
@@ -14,12 +14,14 @@ export interface TextLayoutOptions {
14
14
  wrapStyle?: WrapStyle;
15
15
  /**
16
16
  * Alignment of the lines relative to the widest line. Default: `'left'`
17
- * (before 3.0 this was called verticalAlign, values are accepted under either name)
17
+ * (before 3.0 this was called verticalAlign, the values of the other axis (`'top'`, `'middle'`, `'bottom'`) are
18
+ * accepted for compatibility, which will be removed in 4.0)
18
19
  */
19
20
  horizontalAlign?: HorizontalAlign | VerticalAlign;
20
21
  /**
21
22
  * Which lines to keep when cutting off at `maxHeight`. Default: `'top'`
22
- * (before 3.0 this was called horizontalAlign, values are accepted under either name)
23
+ * (before 3.0 this was called horizontalAlign, the values of the other axis (`'left'`, `'center'`, `'right'`) are
24
+ * accepted for compatibility, which will be removed in 4.0)
23
25
  */
24
26
  verticalAlign?: VerticalAlign | HorizontalAlign;
25
27
  /** Whether to break lines at newline characters. Default: `true` */
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.17.0';
1
+ export const VERSION = '3.18.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.17.0",
3
+ "version": "3.18.0",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",