chess2img 0.3.1 → 0.4.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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  <div align="center">
4
4
  <img src="./assets/themes/cburnett/wK.png" alt="chess2img king icon" width="88" />
5
- <p>Generate PNG chessboard images from FEN, PGN, or board arrays in Node.js.</p>
5
+ <p>Generate PNG, SVG, and JPEG chessboard images from FEN, PGN, or board arrays in Node.js.</p>
6
6
  <p>
7
7
  <a href="https://www.npmjs.com/package/chess2img"><img src="https://img.shields.io/npm/v/chess2img" alt="npm version" /></a>
8
8
  <a href="https://www.npmjs.com/package/chess2img"><img src="https://img.shields.io/npm/dm/chess2img" alt="npm downloads" /></a>
@@ -13,11 +13,17 @@
13
13
 
14
14
  ## Overview
15
15
 
16
- `chess2img` renders chessboard PNGs from FEN, PGN, or board-array inputs with a small Promise-based API for JavaScript and TypeScript users on Node.js.
16
+ `chess2img` renders chessboard PNG, SVG, and JPEG images from FEN, PGN, or board-array inputs with a small Promise-based API for JavaScript and TypeScript users on Node.js.
17
+
18
+ Supported output formats:
19
+
20
+ - PNG
21
+ - SVG
22
+ - JPEG
17
23
 
18
24
  ## Features
19
25
 
20
- - Render PNG chessboard images from `fen`, `pgn`, or `board` input.
26
+ - Render PNG, SVG, or JPEG chessboard images from `fen`, `pgn`, or `board` input.
21
27
  - Use five bundled built-in themes: `merida`, `alpha`, `cburnett`, `cheq`, and `leipzig`.
22
28
  - Highlight squares such as the last move or key tactical ideas.
23
29
  - Customize board colors, size, padding, border sizing, coordinates, and board orientation.
@@ -58,6 +64,36 @@ await writeFile("board.png", png);
58
64
 
59
65
  `renderChess(...)` returns a `Promise<Buffer>` containing PNG data.
60
66
 
67
+ ### SVG Output
68
+
69
+ ```ts
70
+ import { writeFile } from "node:fs/promises";
71
+ import { renderSvg } from "chess2img";
72
+
73
+ const svg = await renderSvg({
74
+ fen: "4k3/8/8/8/8/8/8/4K3 w - - 0 1",
75
+ size: 480,
76
+ style: "merida",
77
+ });
78
+
79
+ await writeFile("board.svg", svg, "utf8");
80
+ ```
81
+
82
+ ### JPEG Output
83
+
84
+ ```ts
85
+ import { writeFile } from "node:fs/promises";
86
+ import { renderJpeg } from "chess2img";
87
+
88
+ const jpeg = await renderJpeg({
89
+ fen: "4k3/8/8/8/8/8/8/4K3 w - - 0 1",
90
+ size: 480,
91
+ style: "merida",
92
+ });
93
+
94
+ await writeFile("board.jpg", jpeg);
95
+ ```
96
+
61
97
  ## Installation
62
98
 
63
99
  ```bash
@@ -77,16 +113,63 @@ If `canvas` fails to install, verify your system packages first. The library shi
77
113
  ### Functional API
78
114
 
79
115
  ```ts
80
- import { writeFile } from "node:fs/promises";
81
- import { renderChess } from "chess2img";
116
+ import { renderFile } from "chess2img";
82
117
 
83
- const png = await renderChess({
118
+ await renderFile("board.png", {
84
119
  fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
85
120
  size: 480,
86
121
  style: "merida",
87
122
  });
123
+ ```
88
124
 
89
- await writeFile("board.png", png);
125
+ `renderChess(...)` and `renderFile(...)` are the explicit PNG APIs.
126
+
127
+ ### Direct PNG, SVG, And JPEG Buffers
128
+
129
+ ```ts
130
+ import { renderChess, renderJpeg, renderSvg } from "chess2img";
131
+
132
+ const png = await renderChess({
133
+ fen: "4k3/8/8/8/8/8/8/4K3 w - - 0 1",
134
+ size: 480,
135
+ style: "merida",
136
+ });
137
+
138
+ const svg = await renderSvg({
139
+ fen: "4k3/8/8/8/8/8/8/4K3 w - - 0 1",
140
+ size: 480,
141
+ style: "merida",
142
+ });
143
+
144
+ const jpeg = await renderJpeg({
145
+ fen: "4k3/8/8/8/8/8/8/4K3 w - - 0 1",
146
+ size: 480,
147
+ style: "merida",
148
+ });
149
+ ```
150
+
151
+ ### SVG And JPEG File Helpers
152
+
153
+ ```ts
154
+ import { renderFile, renderJpegFile, renderSvgFile } from "chess2img";
155
+
156
+ await renderFile("board.png", {
157
+ fen: "4k3/8/8/8/8/8/8/4K3 w - - 0 1",
158
+ size: 480,
159
+ style: "merida",
160
+ });
161
+
162
+ await renderSvgFile("board.svg", {
163
+ fen: "4k3/8/8/8/8/8/8/4K3 w - - 0 1",
164
+ size: 480,
165
+ style: "merida",
166
+ });
167
+
168
+ await renderJpegFile("board.jpg", {
169
+ fen: "4k3/8/8/8/8/8/8/4K3 w - - 0 1",
170
+ size: 480,
171
+ style: "merida",
172
+ });
90
173
  ```
91
174
 
92
175
  ### Automatic Coordinates
@@ -189,9 +272,24 @@ const generator = new ChessImageGenerator({
189
272
  await generator.loadPGN("1. e4 e5 2. Nf3 Nc6 3. Bb5 a6");
190
273
  generator.setHighlights(["e4", "e5"]);
191
274
 
275
+ const png = await generator.toBuffer();
276
+ const svg = await generator.toSvg();
277
+ const jpeg = await generator.toJpeg();
278
+
192
279
  await generator.toFile("board.png");
280
+ await generator.toSvgFile("board.svg");
281
+ await generator.toJpegFile("board.jpg");
193
282
  ```
194
283
 
284
+ Class method summary:
285
+
286
+ - `toBuffer()` -> PNG `Buffer`
287
+ - `toFile(path)` -> writes PNG
288
+ - `toSvg()` -> SVG `string`
289
+ - `toSvgFile(path)` -> writes SVG
290
+ - `toJpeg()` -> JPEG `Buffer`
291
+ - `toJpegFile(path)` -> writes JPEG
292
+
195
293
  ### JavaScript Usage
196
294
 
197
295
  ```js
@@ -222,7 +320,7 @@ Bundled built-in themes:
222
320
 
223
321
  Built-in themes are vendored in-package and render through the same theme pipeline as custom themes.
224
322
 
225
- ## Custom Themes
323
+ ## Custom Piece Packs
226
324
 
227
325
  Register a reusable theme globally:
228
326
 
@@ -245,27 +343,70 @@ Or pass either:
245
343
  - a registered custom theme name through `theme: "custom-theme"`
246
344
  - an inline `ThemeDefinition` object through `theme: { ... }`
247
345
 
248
- Custom themes may use either:
346
+ Custom piece packs may use either:
249
347
 
250
348
  - `svg` assets
251
349
  - `png` assets
252
350
 
351
+ Expected piece-pack structure:
352
+
353
+ ```text
354
+ my-pack/
355
+ wK.svg
356
+ wQ.svg
357
+ wR.svg
358
+ wB.svg
359
+ wN.svg
360
+ wP.svg
361
+ bK.svg
362
+ bQ.svg
363
+ bR.svg
364
+ bB.svg
365
+ bN.svg
366
+ bP.svg
367
+ ```
368
+
369
+ You can point the theme definition at either SVG or PNG files. Mixing formats is also supported as long as all 12 canonical pieces are present.
370
+
371
+ ### Example Third-Party Packs
372
+
373
+ - `chess.com-boards-and-pieces`: https://github.com/GiorgioMegrelli/chess.com-boards-and-pieces
374
+
375
+ These are third-party repositories. They are not bundled with `chess2img`, and you should verify each pack's license terms before redistribution or repackaging.
376
+
253
377
  ## API
254
378
 
255
379
  ### Public Exports
256
380
 
257
381
  - `new ChessImageGenerator(options?)`
258
382
  - `renderChess(options)`
383
+ - `renderSvg(options)`
384
+ - `renderJpeg(options)`
385
+ - `renderFile(path, options)`
386
+ - `renderSvgFile(path, options)`
387
+ - `renderJpegFile(path, options)`
259
388
  - `registerTheme(theme)`
260
389
 
261
390
  ### Functional API
262
391
 
263
- `renderChess(...)` accepts exactly one position source:
392
+ `renderChess(...)`, `renderSvg(...)`, and `renderJpeg(...)` accept exactly one position source:
264
393
 
265
394
  - `fen`
266
395
  - `pgn`
267
396
  - `board`
268
397
 
398
+ Return values:
399
+
400
+ - `renderChess(...)` -> `Promise<Buffer>` containing PNG data
401
+ - `renderSvg(...)` -> `Promise<string>` containing SVG markup
402
+ - `renderJpeg(...)` -> `Promise<Buffer>` containing JPEG data
403
+
404
+ File helpers:
405
+
406
+ - `renderFile(path, options)` -> writes PNG only
407
+ - `renderSvgFile(path, options)` -> writes SVG only
408
+ - `renderJpegFile(path, options)` -> writes JPEG only
409
+
269
410
  ### Class API
270
411
 
271
412
  Methods:
@@ -277,6 +418,10 @@ Methods:
277
418
  - `clearHighlights(): void`
278
419
  - `toBuffer(): Promise<Buffer>`
279
420
  - `toFile(filePath: string): Promise<void>`
421
+ - `toSvg(): Promise<string>`
422
+ - `toSvgFile(filePath: string): Promise<void>`
423
+ - `toJpeg(): Promise<Buffer>`
424
+ - `toJpegFile(filePath: string): Promise<void>`
280
425
 
281
426
  Semantics:
282
427
 
@@ -302,7 +447,7 @@ Semantics:
302
447
 
303
448
  `coordinates: false` or omitting the option disables labels. `coordinates: true` enables labels and chooses `border` mode when `borderSize > 0`, otherwise `inside` mode. Explicit `coordinates: "inside"` is always valid. Explicit `coordinates: "border"` requires `borderSize > 0` and throws `ValidationError` otherwise.
304
449
 
305
- `highlights` is the preferred API. Each entry may be a square string for a filled highlight, or an object with `square`, `style`, `color`, `opacity`, and `lineWidth`. `highlightSquares` remains available for backward compatibility, but should not be used together with `highlights` in the same call.
450
+ `highlights` is the preferred API. Each entry may be a square string for a filled highlight, or an object with `square`, `style`, `color`, `opacity`, `lineWidth`, and `radius`. `highlightSquares` remains available for backward compatibility, but should not be used together with `highlights` in the same call.
306
451
 
307
452
  Inside coordinates use automatic light/dark contrast by default, similar to chess.com. If `coordinates.color` is provided, that exact color is used instead. Border coordinates keep a single-color label style with `#333` as the default.
308
453