xpict 0.2.1 → 0.3.1
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 +6 -3
- package/dist/index.js +6 -3
- package/dist/layers/text-layer.js +20 -10
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +18 -0
- package/dist/utils/wrap-text.d.ts +1 -0
- package/dist/utils/wrap-text.js +20 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
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 { fontConfig } from "./utils
|
|
4
|
+
import { wrapText, fontConfig } from "./utils";
|
|
5
5
|
export * from "./layers";
|
|
6
6
|
export * from "./effects";
|
|
7
7
|
export * from "./template";
|
|
8
|
-
export * from "./utils
|
|
8
|
+
export * from "./utils";
|
|
9
9
|
declare const xpict: {
|
|
10
10
|
rectangle<Data>(options: RectangleLayerOptions<Data>): RectangleLayer<Data>;
|
|
11
11
|
circle<Data>(options: CircleLayerOptions<Data>): CircleLayer<Data>;
|
|
@@ -15,11 +15,14 @@ declare const xpict: {
|
|
|
15
15
|
group<Data>(options: GroupLayerOptions<Data>): GroupLayer<Data>;
|
|
16
16
|
line<Data>(options: LineLayerOptions<Data>): LineLayer<Data>;
|
|
17
17
|
template<Data>(options: InputTemplateOptions<Data>): Template<Data>;
|
|
18
|
-
fontConfig: typeof fontConfig;
|
|
19
18
|
effects: {
|
|
20
19
|
negative: typeof negativeEffect;
|
|
21
20
|
blur: typeof blurEffect;
|
|
22
21
|
grayscale: typeof grayscaleEffect;
|
|
23
22
|
};
|
|
23
|
+
utils: {
|
|
24
|
+
fontConfig: typeof fontConfig;
|
|
25
|
+
wrapText: typeof wrapText;
|
|
26
|
+
};
|
|
24
27
|
};
|
|
25
28
|
export default xpict;
|
package/dist/index.js
CHANGED
|
@@ -17,11 +17,11 @@ 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
|
|
20
|
+
const utils_1 = require("./utils");
|
|
21
21
|
__exportStar(require("./layers"), exports);
|
|
22
22
|
__exportStar(require("./effects"), exports);
|
|
23
23
|
__exportStar(require("./template"), exports);
|
|
24
|
-
__exportStar(require("./utils
|
|
24
|
+
__exportStar(require("./utils"), exports);
|
|
25
25
|
const xpict = {
|
|
26
26
|
rectangle(options) {
|
|
27
27
|
return new layers_1.RectangleLayer(options);
|
|
@@ -47,11 +47,14 @@ const xpict = {
|
|
|
47
47
|
template(options) {
|
|
48
48
|
return new template_1.Template(options);
|
|
49
49
|
},
|
|
50
|
-
fontConfig: font_config_1.fontConfig,
|
|
51
50
|
effects: {
|
|
52
51
|
negative: effects_1.negativeEffect,
|
|
53
52
|
blur: effects_1.blurEffect,
|
|
54
53
|
grayscale: effects_1.grayscaleEffect,
|
|
55
54
|
},
|
|
55
|
+
utils: {
|
|
56
|
+
fontConfig: utils_1.fontConfig,
|
|
57
|
+
wrapText: utils_1.wrapText,
|
|
58
|
+
},
|
|
56
59
|
};
|
|
57
60
|
exports.default = xpict;
|
|
@@ -41,9 +41,16 @@ class TextLayer extends layer_1.Layer {
|
|
|
41
41
|
context.font = `${font.size}px ${(_a = font.name) !== null && _a !== void 0 ? _a : "Arial"}`;
|
|
42
42
|
context.fillStyle = (_b = font.color) !== null && _b !== void 0 ? _b : "#000";
|
|
43
43
|
const content = typeof text === "string" ? text : text({ data: data, index: index });
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
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;
|
|
47
54
|
const offset = anchorOffsets[anchor](textWidth, textHeight);
|
|
48
55
|
const localX = (0, resolve_axis_1.resolveAxis)({
|
|
49
56
|
axis: initialX,
|
|
@@ -64,13 +71,16 @@ class TextLayer extends layer_1.Layer {
|
|
|
64
71
|
context.save();
|
|
65
72
|
context.translate(adjustedX, adjustedY);
|
|
66
73
|
context.rotate((rotation * Math.PI) / 180);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
+
});
|
|
74
84
|
context.restore();
|
|
75
85
|
const buffer = canvas.toBuffer();
|
|
76
86
|
ctx.image = ctx.image.composite([{ input: buffer, top: 0, left: 0 }]);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./wrap-text"), exports);
|
|
18
|
+
__exportStar(require("./font-config"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function wrapText(text: string, maxWidth: number): string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wrapText = wrapText;
|
|
4
|
+
function wrapText(text, maxWidth) {
|
|
5
|
+
const words = text.split(" ");
|
|
6
|
+
let line = "";
|
|
7
|
+
const lines = [];
|
|
8
|
+
for (let n = 0; n < words.length; n++) {
|
|
9
|
+
const testLine = line + words[n] + " ";
|
|
10
|
+
if (testLine.length > maxWidth && n > 0) {
|
|
11
|
+
lines.push(line);
|
|
12
|
+
line = words[n] + " ";
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
line = testLine;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
lines.push(line);
|
|
19
|
+
return lines.join("\n");
|
|
20
|
+
}
|
package/package.json
CHANGED