xpict 0.3.5 → 0.4.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.
@@ -0,0 +1,3 @@
1
+ export declare class XpictError extends Error {
2
+ constructor(message: string);
3
+ }
package/dist/error.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.XpictError = void 0;
4
+ class XpictError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = "XpictError";
8
+ }
9
+ }
10
+ exports.XpictError = XpictError;
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from "./layers";
5
5
  export * from "./effects";
6
6
  export * from "./template";
7
7
  export * from "./utils";
8
+ export * from "./error";
8
9
  declare const xpict: {
9
10
  rectangle<Data>(options: RectangleLayerOptions<Data>): RectangleLayer<Data>;
10
11
  circle<Data>(options: CircleLayerOptions<Data>): CircleLayer<Data>;
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ __exportStar(require("./layers"), exports);
21
21
  __exportStar(require("./effects"), exports);
22
22
  __exportStar(require("./template"), exports);
23
23
  __exportStar(require("./utils"), exports);
24
+ __exportStar(require("./error"), exports);
24
25
  const xpict = {
25
26
  rectangle(options) {
26
27
  return new layers_1.RectangleLayer(options);
@@ -2,8 +2,8 @@ import { Layer, RenderOptions, WhenOptions } from "./layer";
2
2
  import { Axis } from "../utils/resolve-axis";
3
3
  export type GroupLayerOptions<Data> = {
4
4
  layers: Layer<Data>[];
5
- x: Axis<Data>;
6
- y: Axis<Data>;
5
+ x?: Axis<Data>;
6
+ y?: Axis<Data>;
7
7
  when?: (options: WhenOptions<Data>) => boolean;
8
8
  };
9
9
  export declare class GroupLayer<Data> extends Layer<Data> {
@@ -10,14 +10,15 @@ class GroupLayer extends layer_1.Layer {
10
10
  this.options = options;
11
11
  }
12
12
  async render({ context: ctx, data, index = 0, templateConfig }) {
13
+ var _a, _b;
13
14
  const dx = (0, resolve_axis_1.resolveAxis)({
14
- axis: this.options.x,
15
+ axis: (_a = this.options.x) !== null && _a !== void 0 ? _a : 0,
15
16
  data: data,
16
17
  index: index,
17
18
  templateConfig: templateConfig,
18
19
  });
19
20
  const dy = (0, resolve_axis_1.resolveAxis)({
20
- axis: this.options.y,
21
+ axis: (_b = this.options.y) !== null && _b !== void 0 ? _b : 0,
21
22
  data: data,
22
23
  index: index,
23
24
  templateConfig: templateConfig,
@@ -7,6 +7,7 @@ exports.ImageLayer = void 0;
7
7
  const sharp_1 = __importDefault(require("sharp"));
8
8
  const layer_1 = require("./layer");
9
9
  const resolve_axis_1 = require("../utils/resolve-axis");
10
+ const error_1 = require("../error");
10
11
  class ImageLayer extends layer_1.Layer {
11
12
  constructor(options) {
12
13
  super(options.when);
@@ -29,29 +30,34 @@ class ImageLayer extends layer_1.Layer {
29
30
  const y = ctx.offsetY + localY;
30
31
  const src = this.options.src;
31
32
  const resolvedImageSource = typeof src === "string" ? src : src({ data: data, index: index });
32
- let img = (0, sharp_1.default)(resolvedImageSource).resize(this.options.width, this.options.height);
33
- if (this.options.flipX) {
34
- img = img.flop();
35
- }
36
- if (this.options.flipY) {
37
- img = img.flip();
38
- }
39
- if (this.options.rotate !== undefined) {
40
- img = img.rotate(this.options.rotate, {
41
- background: { r: 0, g: 0, b: 0, alpha: 0 },
42
- });
43
- }
44
- if (this.options.transform && this.options.transform.length > 0) {
45
- for (const transform of this.options.transform) {
46
- img = await transform({
47
- data: data,
48
- index: index,
49
- image: img,
33
+ try {
34
+ let img = (0, sharp_1.default)(resolvedImageSource).resize(this.options.width, this.options.height);
35
+ if (this.options.flipX) {
36
+ img = img.flop();
37
+ }
38
+ if (this.options.flipY) {
39
+ img = img.flip();
40
+ }
41
+ if (this.options.rotate !== undefined) {
42
+ img = img.rotate(this.options.rotate, {
43
+ background: { r: 0, g: 0, b: 0, alpha: 0 },
50
44
  });
51
45
  }
46
+ if (this.options.transform && this.options.transform.length > 0) {
47
+ for (const transform of this.options.transform) {
48
+ img = await transform({
49
+ data: data,
50
+ index: index,
51
+ image: img,
52
+ });
53
+ }
54
+ }
55
+ const buffer = await img.toBuffer();
56
+ ctx.image = ctx.image.composite([{ input: buffer, left: x, top: y }]);
57
+ }
58
+ catch (error) {
59
+ throw new error_1.XpictError(`Failed to render image layer (${resolvedImageSource}): ${error.message}`);
52
60
  }
53
- const buffer = await img.toBuffer();
54
- ctx.image = ctx.image.composite([{ input: buffer, left: x, top: y }]);
55
61
  }
56
62
  }
57
63
  exports.ImageLayer = ImageLayer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xpict",
3
- "version": "0.3.5",
3
+ "version": "0.4.0",
4
4
  "description": "Xpict é uma biblioteca para a geração de imagens padronizadas a partir de modelos declarativos.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",