chess-snap 1.0.0 → 1.0.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/LICENSE +21 -0
- package/dist/config.d.ts +1 -0
- package/dist/config.js +1 -0
- package/dist/index.js +17 -1
- package/dist/renderer.js +4 -3
- package/dist/types.d.ts +4 -0
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 カット Katt
|
|
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/dist/config.d.ts
CHANGED
|
@@ -8,4 +8,5 @@ export declare const defaultLight = "#f0d9b5";
|
|
|
8
8
|
export declare const defaultDark = "#b58863";
|
|
9
9
|
export declare const defaultHighlight = "rgba(235, 97, 80, 0.8)";
|
|
10
10
|
export declare const defaultStyle: PieceStyle;
|
|
11
|
+
export declare const defaultFontFamily = "sans-serif";
|
|
11
12
|
export declare const filePaths: Record<string, string>;
|
package/dist/config.js
CHANGED
|
@@ -7,6 +7,7 @@ export const defaultLight = '#f0d9b5';
|
|
|
7
7
|
export const defaultDark = '#b58863';
|
|
8
8
|
export const defaultHighlight = 'rgba(235, 97, 80, 0.8)';
|
|
9
9
|
export const defaultStyle = 'merida';
|
|
10
|
+
export const defaultFontFamily = 'sans-serif';
|
|
10
11
|
export const filePaths = {
|
|
11
12
|
wp: 'WhitePawn',
|
|
12
13
|
wn: 'WhiteKnight',
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Chess } from 'chess.js';
|
|
2
|
-
import {
|
|
2
|
+
import { GlobalFonts } from '@napi-rs/canvas';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import { defaultSize, defaultPadding, defaultLight, defaultDark, defaultHighlight, defaultStyle, defaultFontFamily, white, black, cols } from './config.js';
|
|
3
6
|
import { generateBoardBuffer, writePNG } from './renderer.js';
|
|
4
7
|
export default class ChessImageGenerator {
|
|
5
8
|
chess;
|
|
@@ -10,6 +13,17 @@ export default class ChessImageGenerator {
|
|
|
10
13
|
this.chess = new Chess();
|
|
11
14
|
this.highlightedSquares = [];
|
|
12
15
|
this.ready = false;
|
|
16
|
+
let fontFamilyName = options.fontFamily || defaultFontFamily;
|
|
17
|
+
if (options.fontPath && fs.existsSync(options.fontPath)) {
|
|
18
|
+
const familyFromFileName = path.basename(options.fontPath, path.extname(options.fontPath));
|
|
19
|
+
fontFamilyName = options.fontFamily || familyFromFileName;
|
|
20
|
+
try {
|
|
21
|
+
GlobalFonts.registerFromPath(options.fontPath, fontFamilyName);
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
console.error(`Failed to register font from path ${options.fontPath}:`, err);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
13
27
|
this.options = {
|
|
14
28
|
size: options.size || defaultSize,
|
|
15
29
|
padding: options.padding || defaultPadding,
|
|
@@ -21,6 +35,8 @@ export default class ChessImageGenerator {
|
|
|
21
35
|
notations: options.notations ?? false,
|
|
22
36
|
notationColor: options.notationColor || '',
|
|
23
37
|
notationStyle: options.notationStyle || 'inside',
|
|
38
|
+
fontFamily: fontFamilyName,
|
|
39
|
+
fontPath: options.fontPath || '',
|
|
24
40
|
format: options.format || 'png',
|
|
25
41
|
quality: options.quality ?? 80,
|
|
26
42
|
};
|
package/dist/renderer.js
CHANGED
|
@@ -30,14 +30,15 @@ async function getPieceImage(style, pieceKey) {
|
|
|
30
30
|
/**
|
|
31
31
|
* Renders the algebraic notations (A-H, 1-8) around the board.
|
|
32
32
|
*/
|
|
33
|
-
function drawNotations(ctx, size, flipped, light, dark, padding, notationStyle, notationColor) {
|
|
33
|
+
function drawNotations(ctx, size, flipped, light, dark, padding, notationStyle, notationColor, fontFamily) {
|
|
34
34
|
const paddingTop = padding[0];
|
|
35
35
|
const paddingLeft = padding[3];
|
|
36
36
|
const paddingBottom = padding[2];
|
|
37
37
|
const paddingRight = padding[1];
|
|
38
38
|
const squareSize = size / 8;
|
|
39
39
|
const fontSize = Math.max(16, Math.floor(squareSize / 3.5));
|
|
40
|
-
|
|
40
|
+
const fontSpec = fontFamily ? `"${fontFamily}", sans-serif` : 'sans-serif';
|
|
41
|
+
ctx.font = `bold ${fontSize}px ${fontSpec}`;
|
|
41
42
|
ctx.textBaseline = 'top';
|
|
42
43
|
const rows = flipped ? [1, 2, 3, 4, 5, 6, 7, 8] : [8, 7, 6, 5, 4, 3, 2, 1];
|
|
43
44
|
const columns = flipped ? [...cols].reverse() : cols;
|
|
@@ -124,7 +125,7 @@ export async function generateBoardBuffer(chess, options, highlightedSquares) {
|
|
|
124
125
|
ctx.strokeRect(paddingLeft, paddingTop, options.size, options.size);
|
|
125
126
|
}
|
|
126
127
|
if (options.notations) {
|
|
127
|
-
drawNotations(ctx, options.size, options.flipped, options.light, options.dark, options.padding, options.notationStyle, options.notationColor);
|
|
128
|
+
drawNotations(ctx, options.size, options.flipped, options.light, options.dark, options.padding, options.notationStyle, options.notationColor, options.fontFamily);
|
|
128
129
|
}
|
|
129
130
|
const format = options.format;
|
|
130
131
|
const quality = options.quality;
|
package/dist/types.d.ts
CHANGED
|
@@ -20,6 +20,10 @@ export interface ChessImageOptions {
|
|
|
20
20
|
notationColor?: string;
|
|
21
21
|
/** Placement of the notations */
|
|
22
22
|
notationStyle?: 'inside' | 'outside';
|
|
23
|
+
/** Custom font family for notations (e.g. 'Agency FB', 'Arial', 'sans-serif') */
|
|
24
|
+
fontFamily?: string;
|
|
25
|
+
/** Path to a local .ttf / .otf font file to automatically load & register */
|
|
26
|
+
fontPath?: string;
|
|
23
27
|
/** Image output format (png, jpeg, webp) */
|
|
24
28
|
format?: 'png' | 'jpeg' | 'webp';
|
|
25
29
|
/** Image quality (0-100) for jpeg and webp formats */
|
package/package.json
CHANGED