xpict 0.3.1 → 0.3.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.
@@ -16,6 +16,8 @@ export type TextFunctionOptions<Data> = {
16
16
  index: number;
17
17
  };
18
18
  export type StringFunction<Data> = (options: TextFunctionOptions<Data>) => string;
19
+ export type TextAlign = "left" | "center" | "right";
20
+ export type TextBaseline = "top" | "hanging" | "middle" | "alphabetic" | "ideographic" | "bottom";
19
21
  export type TextLayerOptions<Data> = {
20
22
  text: StringFunction<Data> | string;
21
23
  font: FontOptions;
@@ -23,6 +25,8 @@ export type TextLayerOptions<Data> = {
23
25
  y: Axis<Data>;
24
26
  backgroundColor?: string;
25
27
  anchor?: TextAnchor;
28
+ align?: TextAlign;
29
+ baseline?: TextBaseline;
26
30
  stroke?: Stroke;
27
31
  rotation?: number;
28
32
  when?: WhenFunction<Data>;
@@ -25,7 +25,7 @@ class TextLayer extends layer_1.Layer {
25
25
  const imageMetadata = await ctx.image.metadata();
26
26
  const width = imageMetadata.width;
27
27
  const height = imageMetadata.height;
28
- const { text, font, x: initialX, y: initialY, backgroundColor = "transparent", anchor = "top-left", stroke, rotation = 0, } = this.options;
28
+ const { text, font, x: initialX, y: initialY, backgroundColor = "transparent", anchor = "top-left", stroke, rotation = 0, align, baseline, } = this.options;
29
29
  if (!font.name) {
30
30
  throw new Error("Font name is required");
31
31
  }
@@ -38,19 +38,18 @@ class TextLayer extends layer_1.Layer {
38
38
  context.fillStyle = backgroundColor;
39
39
  context.fillRect(0, 0, width, height);
40
40
  }
41
+ if (align) {
42
+ context.textAlign = align;
43
+ }
44
+ if (baseline) {
45
+ context.textBaseline = baseline;
46
+ }
41
47
  context.font = `${font.size}px ${(_a = font.name) !== null && _a !== void 0 ? _a : "Arial"}`;
42
48
  context.fillStyle = (_b = font.color) !== null && _b !== void 0 ? _b : "#000";
43
49
  const content = typeof text === "string" ? text : text({ data: data, index: index });
44
- const lines = content.split("\n");
45
- const sampleMetrics = context.measureText("M");
46
- const lineHeight = sampleMetrics.actualBoundingBoxAscent + sampleMetrics.actualBoundingBoxDescent;
47
- let maxWidth = 0;
48
- for (const line of lines) {
49
- const metrics = context.measureText(line);
50
- maxWidth = Math.max(maxWidth, metrics.width);
51
- }
52
- const textWidth = maxWidth;
53
- const textHeight = lines.length * lineHeight;
50
+ const metrics = context.measureText(content);
51
+ const textWidth = metrics.width;
52
+ const textHeight = font.size;
54
53
  const offset = anchorOffsets[anchor](textWidth, textHeight);
55
54
  const localX = (0, resolve_axis_1.resolveAxis)({
56
55
  axis: initialX,
@@ -71,16 +70,13 @@ class TextLayer extends layer_1.Layer {
71
70
  context.save();
72
71
  context.translate(adjustedX, adjustedY);
73
72
  context.rotate((rotation * Math.PI) / 180);
74
- lines.forEach((line, i) => {
75
- const y = i * lineHeight;
76
- if (stroke) {
77
- context.strokeStyle = stroke.fill;
78
- context.lineWidth = stroke.width;
79
- context.lineJoin = "round";
80
- context.strokeText(line, 0, y);
81
- }
82
- context.fillText(line, 0, y);
83
- });
73
+ if (stroke) {
74
+ context.strokeStyle = stroke.fill;
75
+ context.lineWidth = stroke.width;
76
+ context.lineJoin = "round";
77
+ context.strokeText(content, 0, 0);
78
+ }
79
+ context.fillText(content, 0, 0);
84
80
  context.restore();
85
81
  const buffer = canvas.toBuffer();
86
82
  ctx.image = ctx.image.composite([{ input: buffer, top: 0, left: 0 }]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xpict",
3
- "version": "0.3.1",
3
+ "version": "0.3.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",