@igxjs/text2png 3.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Text to PNG
2
+
3
+ We upgraded the `node-canvas` to latest in order to support Node.js v24+.
4
+
5
+ ```js
6
+ text2png('Create png image\nfrom multi-line text!');
7
+ ```
8
+
9
+ ![text2png](./img/text2png.png)
10
+
11
+ ## Quick start
12
+
13
+ text2png depends on [node-canvas](https://github.com/Automattic/node-canvas).
14
+ See [node-canvas wiki](https://github.com/Automattic/node-canvas/wiki) on installing node-canvas.
15
+
16
+ ```
17
+ $ npm install @igxjs/text2png
18
+ ```
19
+
20
+ ```js
21
+ const fs = require('fs');
22
+ const text2png = require('text2png');
23
+ fs.writeFileSync('out.png', text2png('Hello!', { color: 'blue' }));
24
+ ```
25
+
26
+ ## Option
27
+
28
+ ``text2png(text, option)``
29
+
30
+ |param|default|
31
+ |---|---|
32
+ |text|(required)|
33
+ |option.font|'30px sans-serif'|
34
+ |option.textAlign|'left'|
35
+ |option.color (or option.textColor)|'black'|
36
+ |option.backgroundColor (or option.bgColor)|'transparent'|
37
+ |option.lineSpacing|0|
38
+ |option.strokeWidth|0|
39
+ |option.strokeColor|'white'|
40
+ |option.padding|0|
41
+ |option.padding(Left\|Top\|Right\|Bottom)|0|
42
+ |option.borderWidth|0|
43
+ |option.border(Left\|Top\|Right\|Bottom)Width|0|
44
+ |option.borderColor|'black'|
45
+ |option.localFontPath||
46
+ |option.localFontName||
47
+ |option.output|'buffer'|
48
+
49
+ ``option.color = '#000' | 'rgb(0, 0, 0)' | 'black' | ...``
50
+
51
+ ``option.output = 'buffer' | 'stream' | 'dataURL' | 'canvas'``
52
+
53
+ ``option.strokeWidth = 1 | 2 | ... `` A padding may have to be set to avoid cutoff of stroke
54
+
55
+ ``'canvas'`` returns [node-canvas](https://github.com/Automattic/node-canvas) object.
56
+
57
+ If you want to use any custom fonts without installing, use `localFontPath` and `localFontName` property.
58
+
59
+ ```js
60
+ text2png('with custom fonts', {
61
+ font: '50px Lobster',
62
+ localFontPath: 'fonts/Lobstar-Regular.ttf',
63
+ localFontName: 'Lobster'
64
+ });
65
+ ```
66
+
67
+ ## Command line interface
68
+
69
+ ```
70
+ $ npm install -g text2png
71
+ $ text2png --help
72
+ $ text2png -t "Hello!" -o "output.png"
73
+ ```
74
+
75
+ ## Example
76
+
77
+ ```js
78
+ text2png('Example\nText', {
79
+ font: '80px Futura',
80
+ color: 'teal',
81
+ backgroundColor: 'linen',
82
+ lineSpacing: 10,
83
+ padding: 20
84
+ });
85
+ ```
86
+
87
+ ![ExampleText](./img/exampleText.png)
88
+
89
+ Enjoy!
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const commander = require("commander");
6
+ const version = require("../package.json").version;
7
+ const text2png = require("../index.js");
8
+
9
+ commander
10
+ .version(version)
11
+ .description("Create png image from text.")
12
+ .option("-t, --text <message>", "text")
13
+ .option("-o, --output <path>", "output file path")
14
+ .option("-f, --font <string>", 'css font option (e.g. "30px Lobster")')
15
+ .option("-a, --textAlign <textAlign>", "text alignment")
16
+ .option("-c, --color <color>", "text color")
17
+ .option("-b, --backgroundColor <color>", "background color")
18
+ .option("-s, --lineSpacing <number>", "line spacing")
19
+
20
+ .option("--strokeWidth <number>", "stroke width")
21
+ .option("--strokeColor <number>", "stroke color")
22
+
23
+ .option(
24
+ "-p, --padding <number>",
25
+ "width of the padding area (left, top, right, bottom)"
26
+ )
27
+ .option("--paddingLeft <number>")
28
+ .option("--paddingTop <number>")
29
+ .option("--paddingRight <number>")
30
+ .option("--paddingBottom <number>")
31
+
32
+ .option(
33
+ "--borderWidth <number>",
34
+ "width of border (left, top, right, bottom)"
35
+ )
36
+ .option("--borderLeftWidth <number>")
37
+ .option("--borderTopWidth <number>")
38
+ .option("--borderRightWidth <number>")
39
+ .option("--borderBottomWidth <number>")
40
+ .option("--borderColor <color>", "border color")
41
+
42
+ .option(
43
+ "--localFontPath <path>",
44
+ "path to local font (e.g. fonts/Lobster-Regular.ttf)"
45
+ )
46
+ .option("--localFontName <name>", "name of local font (e.g. Lobster)")
47
+
48
+ .parse(process.argv);
49
+
50
+ const exec = text => {
51
+ if ((commander.text || text) && commander.output) {
52
+ const stream = text2png(commander.text || text, {
53
+ font: commander.font,
54
+ textAlign: commander.textAlign,
55
+ color: commander.color,
56
+ backgroundColor: commander.backgroundColor,
57
+ lineSpacing: commander.lineSpacing && +commander.lineSpacing,
58
+
59
+ padding: commander.padding && +commander.padding,
60
+ paddingLeft: commander.paddingLeft && +commander.paddingLeft,
61
+ paddingTop: commander.paddingTop && +commander.paddingTop,
62
+ paddingRight: commander.paddingRight && +commander.paddingRight,
63
+ paddingBottom: commander.paddingBottom && +commander.paddingBottom,
64
+
65
+ borderWidth: commander.borderWidth && +commander.borderWidth,
66
+ borderLeftWidth: commander.borderLeftWidth && +commander.borderLeftWidth,
67
+ borderTopWidth: commander.borderTopWidth && +commander.borderTopWidth,
68
+ borderRightWidth:
69
+ commander.borderRightWidth && +commander.borderRightWidth,
70
+ borderBottomWidth:
71
+ commander.borderBottomWidth && +commander.borderBottomWidth,
72
+ borderColor: commander.borderColor,
73
+
74
+ localFontPath: commander.localFontPath,
75
+ localFontName: commander.localFontName,
76
+
77
+ output: "stream"
78
+ });
79
+ const outputPath = path.resolve(process.cwd(), commander.output);
80
+ stream.pipe(fs.createWriteStream(outputPath));
81
+ } else {
82
+ commander.outputHelp();
83
+ }
84
+ };
85
+
86
+ if (process.stdin.isTTY) {
87
+ exec();
88
+ } else {
89
+ let input = "";
90
+ process.stdin.resume();
91
+ process.stdin.setEncoding("utf8");
92
+ process.stdin.on("data", function(chunk) {
93
+ input += chunk;
94
+ });
95
+ process.stdin.on("end", function() {
96
+ exec(input);
97
+ });
98
+ }
package/index.js ADDED
@@ -0,0 +1,213 @@
1
+ const { registerFont, createCanvas } = require("canvas");
2
+
3
+ /**
4
+ * Convert text to PNG image.
5
+ * @param text
6
+ * @param [options]
7
+ * @param [options.font="30px sans-serif"] css style font
8
+ * @param [options.textAlign="left"] text alignment (left, center, right)
9
+ * @param [options.color="black"] (or options.textColor) text color
10
+ * @param [options.backgroundColor] (or options.bgColor) background color
11
+ * @param [options.lineSpacing=0]
12
+ * @param [options.strokeWidth=0]
13
+ * @param [options.strokeColor='white']
14
+ * @param [options.padding=0] width of the padding area (left, top, right, bottom)
15
+ * @param [options.paddingLeft]
16
+ * @param [options.paddingTop]
17
+ * @param [options.paddingRight]
18
+ * @param [options.paddingBottom]
19
+ * @param [options.borderWidth=0] width of border (left, top, right, bottom)
20
+ * @param [options.borderLeftWidth=0]
21
+ * @param [options.borderTopWidth=0]
22
+ * @param [options.borderRightWidth=0]
23
+ * @param [options.borderBottomWidth=0]
24
+ * @param [options.borderColor="black"] border color
25
+ * @param [options.localFontPath] path to local font (e.g. fonts/Lobster-Regular.ttf)
26
+ * @param [options.localFontName] name of local font (e.g. Lobster)
27
+ * @param [options.output="buffer"] 'buffer', 'stream', 'dataURL', 'canvas's
28
+ * @returns {string} png image buffer
29
+ */
30
+ const text2png = (text, options = {}) => {
31
+ // Options
32
+ options = parseOptions(options);
33
+
34
+ // Register a custom font
35
+ if (options.localFontPath && options.localFontName) {
36
+ registerFont(options.localFontPath, { family: options.localFontName });
37
+ }
38
+
39
+ const canvas = createCanvas(0, 0);
40
+ const ctx = canvas.getContext("2d");
41
+
42
+ const max = {
43
+ left: 0,
44
+ right: 0,
45
+ ascent: 0,
46
+ descent: 0
47
+ };
48
+
49
+ let lastDescent;
50
+ const lineProps = text.split("\n").map(line => {
51
+ ctx.font = options.font;
52
+ const metrics = ctx.measureText(line);
53
+
54
+ const left = -1 * metrics.actualBoundingBoxLeft;
55
+ const right = metrics.actualBoundingBoxRight;
56
+ const ascent = metrics.actualBoundingBoxAscent;
57
+ const descent = metrics.actualBoundingBoxDescent;
58
+
59
+ max.left = Math.max(max.left, left);
60
+ max.right = Math.max(max.right, right);
61
+ max.ascent = Math.max(max.ascent, ascent);
62
+ max.descent = Math.max(max.descent, descent);
63
+ lastDescent = descent;
64
+
65
+ return { line, left, right, ascent, descent };
66
+ });
67
+
68
+ const lineHeight = max.ascent + max.descent + options.lineSpacing;
69
+
70
+ const contentWidth = max.left + max.right;
71
+ const contentHeight =
72
+ lineHeight * lineProps.length -
73
+ options.lineSpacing -
74
+ (max.descent - lastDescent);
75
+
76
+ canvas.width =
77
+ contentWidth +
78
+ options.borderLeftWidth +
79
+ options.borderRightWidth +
80
+ options.paddingLeft +
81
+ options.paddingRight;
82
+
83
+ canvas.height =
84
+ contentHeight +
85
+ options.borderTopWidth +
86
+ options.borderBottomWidth +
87
+ options.paddingTop +
88
+ options.paddingBottom;
89
+
90
+ const hasBorder =
91
+ options.borderLeftWidth ||
92
+ options.borderTopWidth ||
93
+ options.borderRightWidth ||
94
+ options.borderBottomWidth || false;
95
+
96
+ if (hasBorder) {
97
+ ctx.fillStyle = options.borderColor;
98
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
99
+ }
100
+
101
+ if (options.backgroundColor) {
102
+ ctx.fillStyle = options.backgroundColor;
103
+ ctx.fillRect(
104
+ options.borderLeftWidth,
105
+ options.borderTopWidth,
106
+ canvas.width - (options.borderLeftWidth + options.borderRightWidth),
107
+ canvas.height - (options.borderTopWidth + options.borderBottomWidth)
108
+ );
109
+ } else if (hasBorder) {
110
+ ctx.clearRect(
111
+ options.borderLeftWidth,
112
+ options.borderTopWidth,
113
+ canvas.width - (options.borderLeftWidth + options.borderRightWidth),
114
+ canvas.height - (options.borderTopWidth + options.borderBottomWidth)
115
+ );
116
+ }
117
+
118
+ ctx.font = options.font;
119
+ ctx.fillStyle = options.textColor;
120
+ ctx.antialias = "gray";
121
+ ctx.textAlign = options.textAlign;
122
+ ctx.lineWidth = options.strokeWidth;
123
+ ctx.strokeStyle = options.strokeColor;
124
+
125
+ let offsetY = options.borderTopWidth + options.paddingTop;
126
+ lineProps.forEach(lineProp => {
127
+ // Calculate Y
128
+ let x = 0;
129
+ const y = max.ascent + offsetY;
130
+
131
+ // Calculate X
132
+ switch (options.textAlign) {
133
+ case "start":
134
+ case "left":
135
+ x = lineProp.left + options.borderLeftWidth + options.paddingLeft;
136
+ break;
137
+
138
+ case "end":
139
+ case "right":
140
+ x =
141
+ canvas.width -
142
+ lineProp.left -
143
+ options.borderRightWidth -
144
+ options.paddingRight;
145
+ break;
146
+
147
+ case "center":
148
+ x = contentWidth / 2 + options.borderLeftWidth + options.paddingLeft;
149
+ break;
150
+ }
151
+
152
+ ctx.fillText(lineProp.line, x, y);
153
+
154
+ if ( options.strokeWidth > 0 ) {
155
+ ctx.strokeText(lineProp.line, x, y);
156
+ }
157
+
158
+ offsetY += lineHeight;
159
+ });
160
+
161
+ switch (options.output) {
162
+ case "buffer":
163
+ return canvas.toBuffer();
164
+ case "stream":
165
+ return canvas.createPNGStream();
166
+ case "dataURL":
167
+ return canvas.toDataURL("image/png");
168
+ case "canvas":
169
+ return canvas;
170
+ default:
171
+ throw new Error(`output type:${options.output} is not supported.`);
172
+ }
173
+ };
174
+
175
+ function parseOptions(options) {
176
+ return {
177
+ font: or(options.font, "30px sans-serif"),
178
+ textAlign: or(options.textAlign, "left"),
179
+ textColor: or(options.textColor, options.color, "black"),
180
+ backgroundColor: or(options.bgColor, options.backgroundColor, null),
181
+ lineSpacing: or(options.lineSpacing, 0),
182
+
183
+ strokeWidth: or(options.strokeWidth, 0),
184
+ strokeColor: or(options.strokeColor, "white"),
185
+
186
+ paddingLeft: or(options.paddingLeft, options.padding, 0),
187
+ paddingTop: or(options.paddingTop, options.padding, 0),
188
+ paddingRight: or(options.paddingRight, options.padding, 0),
189
+ paddingBottom: or(options.paddingBottom, options.padding, 0),
190
+
191
+ borderLeftWidth: or(options.borderLeftWidth, options.borderWidth, 0),
192
+ borderTopWidth: or(options.borderTopWidth, options.borderWidth, 0),
193
+ borderBottomWidth: or(options.borderBottomWidth, options.borderWidth, 0),
194
+ borderRightWidth: or(options.borderRightWidth, options.borderWidth, 0),
195
+ borderColor: or(options.borderColor, "black"),
196
+
197
+ localFontName: or(options.localFontName, null),
198
+ localFontPath: or(options.localFontPath, null),
199
+
200
+ output: or(options.output, "buffer")
201
+ };
202
+ }
203
+
204
+ function or() {
205
+ for (const arg of arguments) {
206
+ if (arg !== undefined && arg !== null && arg !== false && !(typeof arg === 'number' && (Number.isNaN(arg) || arg === 0))) {
207
+ return arg;
208
+ }
209
+ }
210
+ return arguments[arguments.length - 1];
211
+ }
212
+
213
+ module.exports = text2png;
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@igxjs/text2png",
3
+ "version": "3.0.0",
4
+ "description": "Convert text to png for node.js",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "text2png": "./bin/text2png.js"
8
+ },
9
+ "scripts": {
10
+ "lint": "eslint **/*.js *.js",
11
+ "fix": "eslint **/*.js *.js --fix",
12
+ "test": "npm run lint && jasmine"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/ibm-garage-experience/text2png.git"
17
+ },
18
+ "keywords": [
19
+ "text",
20
+ "png"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "author": "tkrkts <tkrkt2773@gmail.com> (http://github.com/tkrkt)",
26
+ "license": "MIT",
27
+ "bugs": {
28
+ "url": "https://github.com/ibm-garage-experience/text2png/issues"
29
+ },
30
+ "homepage": "https://github.com/ibm-garage-experience/text2png#readme",
31
+ "files": [
32
+ "bin",
33
+ "README.md",
34
+ "index.js",
35
+ "package.json",
36
+ "LICENSE"
37
+ ],
38
+ "dependencies": {
39
+ "canvas": "^3.2.1",
40
+ "commander": "^14.0.3"
41
+ },
42
+ "devDependencies": {
43
+ "@eslint/js": "^10.0.1",
44
+ "eslint": "^10.0.0",
45
+ "eslint-config-prettier": "^10.1.8",
46
+ "jasmine": "^6.0.0",
47
+ "looks-same": "^10.0.1"
48
+ }
49
+ }