@igxjs/text2png 3.0.0 → 3.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.
Files changed (2) hide show
  1. package/index.js +40 -53
  2. package/package.json +3 -2
package/index.js CHANGED
@@ -2,30 +2,9 @@ const { registerFont, createCanvas } = require("canvas");
2
2
 
3
3
  /**
4
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
5
+ * @param {string} text Text to convert
6
+ * @param {import('./types').Text2PngOptions} options Options
7
+ * @returns {string|Buffer|import('node:stream').Readable|import('canvas').Canvas} Returns PNG data as specified by options.output
29
8
  */
30
9
  const text2png = (text, options = {}) => {
31
10
  // Options
@@ -33,7 +12,12 @@ const text2png = (text, options = {}) => {
33
12
 
34
13
  // Register a custom font
35
14
  if (options.localFontPath && options.localFontName) {
36
- registerFont(options.localFontPath, { family: options.localFontName });
15
+ try {
16
+ registerFont(options.localFontPath, { family: options.localFontName });
17
+ }
18
+ catch(error) {
19
+ throw new Error(`Failed to load local font from path: ${options.localFontPath}, error: ${error.message}`);
20
+ }
37
21
  }
38
22
 
39
23
  const canvas = createCanvas(0, 0);
@@ -117,7 +101,8 @@ const text2png = (text, options = {}) => {
117
101
 
118
102
  ctx.font = options.font;
119
103
  ctx.fillStyle = options.textColor;
120
- ctx.antialias = "gray";
104
+ ctx.antialias = 'gray';
105
+ ctx.imageSmoothingEnabled = options.imageSmoothingEnabled;
121
106
  ctx.textAlign = options.textAlign;
122
107
  ctx.lineWidth = options.strokeWidth;
123
108
  ctx.strokeStyle = options.strokeColor;
@@ -151,7 +136,7 @@ const text2png = (text, options = {}) => {
151
136
 
152
137
  ctx.fillText(lineProp.line, x, y);
153
138
 
154
- if ( options.strokeWidth > 0 ) {
139
+ if (options.strokeWidth > 0) {
155
140
  ctx.strokeText(lineProp.line, x, y);
156
141
  }
157
142
 
@@ -174,36 +159,38 @@ const text2png = (text, options = {}) => {
174
159
 
175
160
  function parseOptions(options) {
176
161
  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")
162
+ font: orOr(options.font, "30px sans-serif"),
163
+ textAlign: orOr(options.textAlign, "left"),
164
+ textColor: orOr(options.textColor, options.color, "black"),
165
+ backgroundColor: orOr(options.bgColor, options.backgroundColor, null),
166
+ lineSpacing: orOr(options.lineSpacing, 0),
167
+
168
+ strokeWidth: orOr(options.strokeWidth, 0),
169
+ strokeColor: orOr(options.strokeColor, "white"),
170
+
171
+ paddingLeft: orOr(options.paddingLeft, options.padding, 0),
172
+ paddingTop: orOr(options.paddingTop, options.padding, 0),
173
+ paddingRight: orOr(options.paddingRight, options.padding, 0),
174
+ paddingBottom: orOr(options.paddingBottom, options.padding, 0),
175
+
176
+ borderLeftWidth: orOr(options.borderLeftWidth, options.borderWidth, 0),
177
+ borderTopWidth: orOr(options.borderTopWidth, options.borderWidth, 0),
178
+ borderBottomWidth: orOr(options.borderBottomWidth, options.borderWidth, 0),
179
+ borderRightWidth: orOr(options.borderRightWidth, options.borderWidth, 0),
180
+ borderColor: orOr(options.borderColor, "black"),
181
+
182
+ localFontName: orOr(options.localFontName, null),
183
+ localFontPath: orOr(options.localFontPath, null),
184
+
185
+ output: orOr(options.output, "buffer"),
186
+
187
+ imageSmoothingEnabled: orOr(options.imageSmoothingEnabled, false)
201
188
  };
202
189
  }
203
190
 
204
- function or() {
191
+ function orOr() {
205
192
  for (const arg of arguments) {
206
- if (arg !== undefined && arg !== null && arg !== false && !(typeof arg === 'number' && (Number.isNaN(arg) || arg === 0))) {
193
+ if (arg !== undefined && arg !== null) {
207
194
  return arg;
208
195
  }
209
196
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@igxjs/text2png",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "description": "Convert text to png for node.js",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -45,5 +45,6 @@
45
45
  "eslint-config-prettier": "^10.1.8",
46
46
  "jasmine": "^6.0.0",
47
47
  "looks-same": "^10.0.1"
48
- }
48
+ },
49
+ "types": "./types.d.ts"
49
50
  }