xpict 0.3.1 → 0.3.3
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 +0 -5
- package/dist/index.js +0 -5
- package/dist/layers/text-layer.d.ts +4 -0
- package/dist/layers/text-layer.js +17 -21
- package/dist/utils/wrap-text.d.ts +1 -1
- package/dist/utils/wrap-text.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { GroupLayer, RepeatLayer, GroupLayerOptions, ImageLayer, ImageLayerOptions, LineLayer, LineLayerOptions, RectangleLayer, RectangleLayerOptions, RepeatLayerOptions, TextLayer, TextLayerOptions, CircleLayerOptions, CircleLayer } from "./layers";
|
|
2
2
|
import { negativeEffect, blurEffect, grayscaleEffect } from "./effects";
|
|
3
3
|
import { Template, InputTemplateOptions } from "./template";
|
|
4
|
-
import { wrapText, fontConfig } from "./utils";
|
|
5
4
|
export * from "./layers";
|
|
6
5
|
export * from "./effects";
|
|
7
6
|
export * from "./template";
|
|
@@ -20,9 +19,5 @@ declare const xpict: {
|
|
|
20
19
|
blur: typeof blurEffect;
|
|
21
20
|
grayscale: typeof grayscaleEffect;
|
|
22
21
|
};
|
|
23
|
-
utils: {
|
|
24
|
-
fontConfig: typeof fontConfig;
|
|
25
|
-
wrapText: typeof wrapText;
|
|
26
|
-
};
|
|
27
22
|
};
|
|
28
23
|
export default xpict;
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
const layers_1 = require("./layers");
|
|
18
18
|
const effects_1 = require("./effects");
|
|
19
19
|
const template_1 = require("./template");
|
|
20
|
-
const utils_1 = require("./utils");
|
|
21
20
|
__exportStar(require("./layers"), exports);
|
|
22
21
|
__exportStar(require("./effects"), exports);
|
|
23
22
|
__exportStar(require("./template"), exports);
|
|
@@ -52,9 +51,5 @@ const xpict = {
|
|
|
52
51
|
blur: effects_1.blurEffect,
|
|
53
52
|
grayscale: effects_1.grayscaleEffect,
|
|
54
53
|
},
|
|
55
|
-
utils: {
|
|
56
|
-
fontConfig: utils_1.fontConfig,
|
|
57
|
-
wrapText: utils_1.wrapText,
|
|
58
|
-
},
|
|
59
54
|
};
|
|
60
55
|
exports.default = xpict;
|
|
@@ -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
|
|
45
|
-
const
|
|
46
|
-
const
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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 }]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function wrapText(text: string, maxWidth: number): string;
|
|
1
|
+
export declare function wrapText(text: string, maxWidth: number): string[];
|
package/dist/utils/wrap-text.js
CHANGED
package/package.json
CHANGED