xpict 1.0.0 → 1.0.2

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/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { GroupLayer, RepeatLayer, GroupLayerOptions, ImageLayer, ImageLayerOptions, LineLayer, LineLayerOptions, RectangleLayer, RectangleLayerOptions, RepeatLayerOptions, TextLayer, TextLayerOptions, CircleLayerOptions, CircleLayer } from "./layers";
2
2
  import { Template, InputTemplateOptions } from "./template";
3
- import * as utils from "./utils";
4
3
  import * as effects from "./effects";
4
+ import * as utils from "./utils";
5
5
  export * from "./effects";
6
6
  export * from "./layers";
7
7
  export * from "./template";
package/dist/index.js CHANGED
@@ -38,8 +38,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  const layers_1 = require("./layers");
40
40
  const template_1 = require("./template");
41
- const utils = __importStar(require("./utils"));
42
41
  const effects = __importStar(require("./effects"));
42
+ const utils = __importStar(require("./utils"));
43
43
  __exportStar(require("./effects"), exports);
44
44
  __exportStar(require("./layers"), exports);
45
45
  __exportStar(require("./template"), exports);
@@ -14,14 +14,14 @@ class CircleLayer extends layer_1.Layer {
14
14
  axis: this.options.x,
15
15
  data: data,
16
16
  index: index,
17
- templateConfig: templateConfig,
17
+ templateSize: templateConfig,
18
18
  });
19
19
  const y = renderContext.offsetY +
20
20
  (0, resolve_axis_1.resolveAxis)({
21
21
  axis: this.options.y,
22
22
  data: data,
23
23
  index: index,
24
- templateConfig: templateConfig,
24
+ templateSize: templateConfig,
25
25
  });
26
26
  const { radius, fill } = this.options;
27
27
  const context = renderContext.ctx;
@@ -14,13 +14,13 @@ class GroupLayer extends layer_1.Layer {
14
14
  axis: (_a = this.options.x) !== null && _a !== void 0 ? _a : 0,
15
15
  data: data,
16
16
  index: index,
17
- templateConfig: templateConfig,
17
+ templateSize: templateConfig,
18
18
  });
19
19
  const dy = (0, resolve_axis_1.resolveAxis)({
20
20
  axis: (_b = this.options.y) !== null && _b !== void 0 ? _b : 0,
21
21
  data: data,
22
22
  index: index,
23
- templateConfig: templateConfig,
23
+ templateSize: templateConfig,
24
24
  });
25
25
  const prevX = ctx.offsetX;
26
26
  const prevY = ctx.offsetY;
@@ -1,4 +1,5 @@
1
1
  import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
2
+ import { Dimension } from "../utils/resolve-dimension";
2
3
  import { Layer, RenderOptions, WhenFunction } from "./layer";
3
4
  import { Axis } from "../utils/resolve-axis";
4
5
  export type TransformOptions<Data> = {
@@ -17,8 +18,8 @@ export type ImageLayerOptions<Data> = {
17
18
  src: string | ImageSrcFunction<Data>;
18
19
  x: Axis<Data>;
19
20
  y: Axis<Data>;
20
- width?: number;
21
- height?: number;
21
+ width?: Dimension<Data>;
22
+ height?: Dimension<Data>;
22
23
  flipX?: boolean;
23
24
  flipY?: boolean;
24
25
  rotate?: number;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ImageLayer = void 0;
4
4
  const canvas_1 = require("@napi-rs/canvas");
5
+ const resolve_dimension_1 = require("../utils/resolve-dimension");
5
6
  const layer_1 = require("./layer");
6
7
  const resolve_axis_1 = require("../utils/resolve-axis");
7
8
  const error_1 = require("../error");
@@ -15,31 +16,68 @@ class ImageLayer extends layer_1.Layer {
15
16
  axis: this.options.x,
16
17
  data: data,
17
18
  index: index,
18
- templateConfig: templateConfig,
19
+ templateSize: templateConfig,
19
20
  });
20
21
  const localY = (0, resolve_axis_1.resolveAxis)({
21
22
  axis: this.options.y,
22
23
  data: data,
23
24
  index: index,
24
- templateConfig: templateConfig,
25
+ templateSize: templateConfig,
25
26
  });
27
+ const width = this.options.width
28
+ ? (0, resolve_dimension_1.resolveDimension)({
29
+ data: data,
30
+ dimension: this.options.width,
31
+ index: index,
32
+ templateSize: templateConfig,
33
+ })
34
+ : this.options.width;
35
+ const height = this.options.height
36
+ ? (0, resolve_dimension_1.resolveDimension)({
37
+ data: data,
38
+ dimension: this.options.height,
39
+ index: index,
40
+ templateSize: templateConfig,
41
+ })
42
+ : this.options.height;
26
43
  const x = renderContext.offsetX + localX;
27
44
  const y = renderContext.offsetY + localY;
28
45
  const src = this.options.src;
29
46
  const resolvedImageSource = typeof src === "string" ? src : src({ data: data, index: index });
30
47
  const image = await (0, canvas_1.loadImage)(resolvedImageSource);
31
- const localCanvas = (0, canvas_1.createCanvas)(image.width, image.height);
48
+ let finalWidth;
49
+ let finalHeight;
50
+ if (width && height) {
51
+ finalWidth = width;
52
+ finalHeight = height;
53
+ }
54
+ else if (width) {
55
+ finalWidth = width;
56
+ finalHeight = (width / image.width) * image.height;
57
+ }
58
+ else if (height) {
59
+ finalHeight = height;
60
+ finalWidth = (height / image.height) * image.width;
61
+ }
62
+ else {
63
+ finalWidth = image.width;
64
+ finalHeight = image.height;
65
+ }
66
+ const localCanvas = (0, canvas_1.createCanvas)(finalWidth, finalHeight);
32
67
  const localContext = localCanvas.getContext("2d");
33
68
  try {
34
- localContext.drawImage(image, 0, 0);
35
- if (this.options.flipX) {
36
- localContext.translate(image.width, 0);
37
- localContext.scale(-1, 1);
38
- }
39
- if (this.options.flipY) {
40
- localContext.translate(0, image.height);
41
- localContext.scale(1, -1);
69
+ const hasRotaion = this.options.flipX || this.options.flipY || this.options.rotate !== undefined;
70
+ if (hasRotaion) {
71
+ localContext.translate(finalWidth / 2, finalHeight / 2);
72
+ if (this.options.rotate !== undefined) {
73
+ localContext.rotate((this.options.rotate * Math.PI) / 180);
74
+ }
75
+ const scaleX = this.options.flipX ? -1 : 1;
76
+ const scaleY = this.options.flipY ? -1 : 1;
77
+ localContext.scale(scaleX, scaleY);
78
+ localContext.translate(-finalWidth / 2, -finalHeight / 2);
42
79
  }
80
+ localContext.drawImage(image, 0, 0, finalWidth, finalHeight);
43
81
  if (this.options.rotate !== undefined) {
44
82
  localContext.translate(image.width / 2, image.height / 2);
45
83
  localContext.rotate((this.options.rotate * Math.PI) / 180);
@@ -55,6 +93,7 @@ class ImageLayer extends layer_1.Layer {
55
93
  });
56
94
  }
57
95
  }
96
+ localContext.restore();
58
97
  const buffer = localCanvas.toBuffer("image/png");
59
98
  const finalImage = await (0, canvas_1.loadImage)(buffer);
60
99
  renderContext.ctx.drawImage(finalImage, x, y);
@@ -1,3 +1,4 @@
1
+ import { Dimension } from "../utils/resolve-dimension";
1
2
  import { Layer, RenderOptions, WhenFunction } from "./layer";
2
3
  import { Axis } from "../utils/resolve-axis";
3
4
  export type LineLayerOptions<Data> = {
@@ -10,7 +11,7 @@ export type LineLayerOptions<Data> = {
10
11
  y: Axis<Data>;
11
12
  };
12
13
  color: string;
13
- width: number;
14
+ width: Dimension<Data>;
14
15
  when?: WhenFunction<Data>;
15
16
  };
16
17
  export declare class LineLayer<Data> extends Layer<Data> {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LineLayer = void 0;
4
+ const resolve_dimension_1 = require("../utils/resolve-dimension");
4
5
  const layer_1 = require("./layer");
5
6
  const resolve_axis_1 = require("../utils/resolve-axis");
6
7
  class LineLayer extends layer_1.Layer {
@@ -14,32 +15,38 @@ class LineLayer extends layer_1.Layer {
14
15
  axis: this.options.from.x,
15
16
  data: data,
16
17
  index: index,
17
- templateConfig: templateConfig,
18
+ templateSize: templateConfig,
18
19
  });
19
20
  const y1 = renderContext.offsetY +
20
21
  (0, resolve_axis_1.resolveAxis)({
21
22
  axis: this.options.from.y,
22
23
  data: data,
23
24
  index: index,
24
- templateConfig: templateConfig,
25
+ templateSize: templateConfig,
25
26
  });
26
27
  const x2 = renderContext.offsetX +
27
28
  (0, resolve_axis_1.resolveAxis)({
28
29
  axis: this.options.to.x,
29
30
  data: data,
30
31
  index: index,
31
- templateConfig: templateConfig,
32
+ templateSize: templateConfig,
32
33
  });
33
34
  const y2 = renderContext.offsetY +
34
35
  (0, resolve_axis_1.resolveAxis)({
35
36
  axis: this.options.to.y,
36
37
  data: data,
37
38
  index: index,
38
- templateConfig: templateConfig,
39
+ templateSize: templateConfig,
39
40
  });
41
+ const width = (0, resolve_dimension_1.resolveDimension)({
42
+ data: data,
43
+ dimension: this.options.width,
44
+ index: index,
45
+ templateSize: templateConfig,
46
+ });
40
47
  const context = renderContext.ctx;
41
48
  context.strokeStyle = this.options.color;
42
- context.lineWidth = this.options.width;
49
+ context.lineWidth = width;
43
50
  context.lineCap = "round";
44
51
  context.beginPath();
45
52
  context.moveTo(x1, y1);
@@ -1,10 +1,11 @@
1
+ import { Dimension } from "../utils/resolve-dimension";
1
2
  import { Layer, RenderOptions, WhenFunction } from "./layer";
2
3
  import { Axis } from "../utils/resolve-axis";
3
4
  export type RectangleLayerOptions<Data> = {
4
5
  x: Axis<Data>;
5
6
  y: Axis<Data>;
6
- width: number;
7
- height: number;
7
+ width: Dimension<Data>;
8
+ height: Dimension<Data>;
8
9
  fill: string;
9
10
  borderRadius?: number;
10
11
  when?: WhenFunction<Data>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RectangleLayer = void 0;
4
+ const resolve_dimension_1 = require("../utils/resolve-dimension");
4
5
  const layer_1 = require("./layer");
5
6
  const resolve_axis_1 = require("../utils/resolve-axis");
6
7
  class RectangleLayer extends layer_1.Layer {
@@ -13,17 +14,29 @@ class RectangleLayer extends layer_1.Layer {
13
14
  axis: this.options.x,
14
15
  data: data,
15
16
  index: index,
16
- templateConfig: templateConfig,
17
+ templateSize: templateConfig,
17
18
  });
18
19
  const localY = (0, resolve_axis_1.resolveAxis)({
19
20
  axis: this.options.y,
20
21
  data: data,
21
22
  index: index,
22
- templateConfig: templateConfig,
23
+ templateSize: templateConfig,
23
24
  });
24
25
  const x = renderContext.offsetX + localX;
25
26
  const y = renderContext.offsetY + localY;
26
- const { width, height, fill, borderRadius } = this.options;
27
+ const { fill, borderRadius } = this.options;
28
+ const width = (0, resolve_dimension_1.resolveDimension)({
29
+ data: data,
30
+ dimension: this.options.width,
31
+ index: index,
32
+ templateSize: templateConfig,
33
+ });
34
+ const height = (0, resolve_dimension_1.resolveDimension)({
35
+ data: data,
36
+ dimension: this.options.height,
37
+ index: index,
38
+ templateSize: templateConfig,
39
+ });
27
40
  const context = renderContext.ctx;
28
41
  context.fillStyle = fill;
29
42
  if (borderRadius && borderRadius > 0) {
@@ -18,13 +18,13 @@ class RepeatLayer extends layer_1.Layer {
18
18
  axis: this.options.x,
19
19
  data: data,
20
20
  index: index,
21
- templateConfig: templateConfig,
21
+ templateSize: templateConfig,
22
22
  });
23
23
  const dy = (0, resolve_axis_1.resolveAxis)({
24
24
  axis: this.options.y,
25
25
  data: data,
26
26
  index: index,
27
- templateConfig: templateConfig,
27
+ templateSize: templateConfig,
28
28
  });
29
29
  const prevX = ctx.offsetX;
30
30
  const prevY = ctx.offsetY;
@@ -25,13 +25,13 @@ class TextLayer extends layer_1.Layer {
25
25
  axis: initialX,
26
26
  data: data,
27
27
  index: index,
28
- templateConfig: templateConfig,
28
+ templateSize: templateConfig,
29
29
  });
30
30
  const localY = (0, resolve_axis_1.resolveAxis)({
31
31
  axis: initialY,
32
32
  data: data,
33
33
  index: index,
34
- templateConfig: templateConfig,
34
+ templateSize: templateConfig,
35
35
  });
36
36
  const x = renderContext.offsetX + localX;
37
37
  const y = renderContext.offsetY + localY;
@@ -1,8 +1,10 @@
1
1
  import { Canvas } from "@napi-rs/canvas";
2
2
  import { Layer } from "./layers/layer";
3
- export type TemplateConfig = {
3
+ export type TemplateSize = {
4
4
  width: number;
5
5
  height: number;
6
+ };
7
+ export type TemplateConfig = TemplateSize & {
6
8
  fill?: string;
7
9
  };
8
10
  export type InputTemplateOptions<Data> = {
@@ -1,14 +1,12 @@
1
- import { TemplateConfig } from "../template";
1
+ import { TemplateSize } from "../template";
2
2
  export type ComputeAxisOptions<Data> = {
3
3
  data: Data;
4
4
  index: number;
5
+ templateSize: TemplateSize;
5
6
  };
6
7
  export type ComputeAxis<Data> = (options: ComputeAxisOptions<Data>) => number;
7
8
  export type Axis<Data> = ComputeAxis<Data> | number;
8
- export type ResolveAxisOptions<Data> = {
9
+ export type ResolveAxisOptions<Data> = ComputeAxisOptions<Data> & {
9
10
  axis: Axis<Data>;
10
- data: Data;
11
- index: number;
12
- templateConfig: TemplateConfig;
13
11
  };
14
12
  export declare function resolveAxis<Data>({ axis, ...params }: ResolveAxisOptions<Data>): number;
@@ -0,0 +1,12 @@
1
+ import { TemplateSize } from "../template";
2
+ export type ComputeDimensionOptions<Data> = {
3
+ data: Data;
4
+ index: number;
5
+ templateSize: TemplateSize;
6
+ };
7
+ export type ComputeDimension<Data> = (options: ComputeDimensionOptions<Data>) => number;
8
+ export type Dimension<Data> = ComputeDimension<Data> | number;
9
+ export type ResolveDimensionOptions<Data> = ComputeDimensionOptions<Data> & {
10
+ dimension: Dimension<Data>;
11
+ };
12
+ export declare function resolveDimension<Data>({ dimension, ...params }: ResolveDimensionOptions<Data>): number;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveDimension = resolveDimension;
4
+ function resolveDimension({ dimension, ...params }) {
5
+ if (typeof dimension === "function") {
6
+ return dimension(params);
7
+ }
8
+ return dimension;
9
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xpict",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
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",