@nexterm/canvas 0.0.0-stage → 1.1.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.
@@ -0,0 +1,221 @@
1
+ /**
2
+ * node-canvas compatibility layer for @napi-rs/canvas.
3
+ *
4
+ * Provides a drop-in replacement API matching the `canvas` (node-canvas) npm package.
5
+ * Quality values for JPEG/WebP use the node-canvas 0-1 scale and are automatically
6
+ * converted to @napi-rs/canvas's 0-100 scale internally.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // Drop-in replacement: change this import
11
+ * // import { createCanvas, registerFont, loadImage } from 'canvas'
12
+ * // to this:
13
+ * import { createCanvas, registerFont, loadImage } from '@napi-rs/canvas/node-canvas'
14
+ * ```
15
+ */
16
+
17
+ import { Readable } from 'node:stream'
18
+
19
+ // Re-export types from @napi-rs/canvas that are API-compatible
20
+ import {
21
+ Image as NapiImage,
22
+ ImageData as NapiImageData,
23
+ Path2D as NapiPath2D,
24
+ DOMPoint as NapiDOMPoint,
25
+ DOMMatrix as NapiDOMMatrix,
26
+ DOMRect as NapiDOMRect,
27
+ SKRSContext2D,
28
+ Canvas as NapiCanvas,
29
+ SvgCanvas,
30
+ SvgExportFlag,
31
+ IGlobalFonts,
32
+ PDFDocument,
33
+ } from './index'
34
+
35
+ // ---------------------------------------------------------------------------
36
+ // Config interfaces (node-canvas conventions)
37
+ // ---------------------------------------------------------------------------
38
+
39
+ export interface PngConfig {
40
+ /** ZLIB compression level (0-9). Accepted for compatibility; Skia uses its own encoder. */
41
+ compressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
42
+ /** PNG filter flags. Accepted for compatibility; Skia uses its own encoder. */
43
+ filters?: number
44
+ /** Palette for indexed PNGs. Not supported by Skia backend. */
45
+ palette?: Uint8ClampedArray
46
+ /** Background color index for indexed PNGs. Not supported by Skia backend. */
47
+ backgroundIndex?: number
48
+ /**
49
+ * Pixels per inch, written as a PNG `pHYs` chunk with the bytes node-canvas
50
+ * writes, up to 54546055 ppi; a larger value writes 2147483647 px/m, the
51
+ * PNG maximum. The fraction is dropped. 0, a value below 1, NaN, the
52
+ * infinities and a non-number write none.
53
+ */
54
+ resolution?: number
55
+ }
56
+
57
+ export interface JpegConfig {
58
+ /** Quality between 0 and 1. Defaults to 0.75. Converted to 0-100 for Skia. */
59
+ quality?: number
60
+ /** Progressive encoding. Accepted for compatibility; not supported by Skia backend. */
61
+ progressive?: boolean
62
+ /** 2x2 chroma subsampling. Accepted for compatibility; not supported by Skia backend. */
63
+ chromaSubsampling?: boolean
64
+ }
65
+
66
+ // ---------------------------------------------------------------------------
67
+ // Canvas class (with node-canvas compatible methods)
68
+ // ---------------------------------------------------------------------------
69
+
70
+ export interface Canvas extends Omit<NapiCanvas, 'toBuffer' | 'toDataURL'> {
71
+ /**
72
+ * Encode the canvas as a PNG and return a Node.js Readable stream.
73
+ * @param config PNG encoding options (accepted for compatibility)
74
+ */
75
+ createPNGStream(config?: PngConfig): PNGStream
76
+
77
+ /**
78
+ * Encode the canvas as a JPEG and return a Node.js Readable stream.
79
+ * @param config JPEG encoding options. Quality is 0-1 (default 0.75).
80
+ */
81
+ createJPEGStream(config?: JpegConfig): JPEGStream
82
+
83
+ // --- toBuffer overloads (node-canvas conventions) ---
84
+
85
+ /** Encode as PNG (default). */
86
+ toBuffer(): Buffer
87
+ toBuffer(mimeType: 'image/png', config?: PngConfig): Buffer
88
+ /** Encode as JPEG. Quality in config is 0-1 (default 0.75). */
89
+ toBuffer(mimeType: 'image/jpeg', config?: JpegConfig): Buffer
90
+ /** Get raw unencoded pixel data. */
91
+ toBuffer(mimeType: 'raw'): Buffer
92
+
93
+ /** Async: encode as PNG (default) via callback. */
94
+ toBuffer(cb: (err: Error | null, result: Buffer) => void): void
95
+ toBuffer(cb: (err: Error | null, result: Buffer) => void, mimeType: 'image/png', config?: PngConfig): void
96
+ /** Async: encode as JPEG via callback. Quality in config is 0-1 (default 0.75). */
97
+ toBuffer(cb: (err: Error | null, result: Buffer) => void, mimeType: 'image/jpeg', config?: JpegConfig): void
98
+
99
+ // --- toDataURL overloads (node-canvas conventions) ---
100
+
101
+ /** Encode as PNG data URL (default). */
102
+ toDataURL(): string
103
+ toDataURL(mimeType: 'image/png'): string
104
+ /** Encode as JPEG data URL. Quality is 0-1. */
105
+ toDataURL(mimeType: 'image/jpeg', quality?: number): string
106
+ /** Async: encode as data URL via callback. */
107
+ toDataURL(cb: (err: Error | null, result: string) => void): void
108
+ toDataURL(mimeType: 'image/png', cb: (err: Error | null, result: string) => void): void
109
+ toDataURL(mimeType: 'image/jpeg', cb: (err: Error | null, result: string) => void): void
110
+ toDataURL(mimeType: 'image/jpeg', quality: number, cb: (err: Error | null, result: string) => void): void
111
+ }
112
+
113
+ // ---------------------------------------------------------------------------
114
+ // Stream classes
115
+ // ---------------------------------------------------------------------------
116
+
117
+ /** Readable stream that emits PNG-encoded data. */
118
+ export class PNGStream extends Readable {}
119
+
120
+ /** Readable stream that emits JPEG-encoded data. */
121
+ export class JPEGStream extends Readable {}
122
+
123
+ // ---------------------------------------------------------------------------
124
+ // Factory functions
125
+ // ---------------------------------------------------------------------------
126
+
127
+ /**
128
+ * Create a new canvas with node-canvas compatible API.
129
+ * Canvas instances created through this function have `createPNGStream()`,
130
+ * `createJPEGStream()`, and node-canvas compatible `toBuffer()` overloads.
131
+ *
132
+ * When `type` is `'svg'`, returns a `SvgCanvas` which produces vector output
133
+ * via `getContent()` instead of raster encoding methods.
134
+ *
135
+ * @param width Canvas width in pixels
136
+ * @param height Canvas height in pixels
137
+ * @param type Canvas type: 'image' (default) or 'svg'
138
+ */
139
+ export function createCanvas(width: number, height: number, type: 'svg'): SvgCanvas
140
+ export function createCanvas(width: number, height: number, type?: 'image'): Canvas
141
+
142
+ /**
143
+ * Create an ImageData instance.
144
+ * @param data Pixel data array
145
+ * @param width Width in pixels
146
+ * @param height Height in pixels (calculated from data length if omitted)
147
+ */
148
+ export function createImageData(data: Uint8ClampedArray, width: number, height?: number): NapiImageData
149
+ export function createImageData(width: number, height: number): NapiImageData
150
+
151
+ /**
152
+ * Load an image from a file path, URL, Buffer, or other source.
153
+ * Returns a Promise that resolves to an Image instance.
154
+ */
155
+ export function loadImage(
156
+ source: string | URL | Buffer | ArrayBufferLike | Uint8Array | NapiImage | import('stream').Readable,
157
+ options?: { alt?: string; maxRedirects?: number; requestOptions?: import('http').RequestOptions },
158
+ ): Promise<NapiImage>
159
+
160
+ // ---------------------------------------------------------------------------
161
+ // Font registration (node-canvas convention)
162
+ // ---------------------------------------------------------------------------
163
+
164
+ /**
165
+ * Register a font file for use in canvas text rendering.
166
+ * Compatible with node-canvas's `registerFont()`.
167
+ *
168
+ * Note: @napi-rs/canvas auto-detects weight and style from font file metadata
169
+ * (matching browser behavior). The `weight` and `style` properties in fontFace
170
+ * are accepted for API compatibility.
171
+ *
172
+ * @param path Absolute path to the font file (.ttf, .otf, etc.)
173
+ * @param fontFace Font face properties matching CSS @font-face descriptors
174
+ */
175
+ export function registerFont(path: string, fontFace: { family: string; weight?: string; style?: string }): void
176
+
177
+ /**
178
+ * Deregister all previously registered fonts.
179
+ * Compatible with node-canvas's `deregisterAllFonts()`.
180
+ */
181
+ export function deregisterAllFonts(): void
182
+
183
+ // ---------------------------------------------------------------------------
184
+ // Re-exported classes & values
185
+ // ---------------------------------------------------------------------------
186
+
187
+ // Canvas is exported both as a type (the compat interface above) and as a
188
+ // value (constructor that returns compat Canvas instances at runtime).
189
+ export declare const Canvas: {
190
+ new (width: number, height: number): Canvas
191
+ new (width: number, height: number, flag: SvgExportFlag): SvgCanvas
192
+ prototype: NapiCanvas
193
+ }
194
+
195
+ export { NapiImage as Image }
196
+ export { NapiImageData as ImageData }
197
+ export { NapiPath2D as Path2D }
198
+ export { NapiDOMPoint as DOMPoint }
199
+ export { NapiDOMMatrix as DOMMatrix }
200
+ export { NapiDOMRect as DOMRect }
201
+
202
+ // SKRSContext2D is an interface in index.d.ts but a class value at runtime.
203
+ // Declare as const to export the value for instanceof checks and re-export the type.
204
+ export type CanvasRenderingContext2D = SKRSContext2D
205
+ export declare const CanvasRenderingContext2D: { prototype: SKRSContext2D }
206
+ /** Legacy alias for CanvasRenderingContext2D. */
207
+ export type Context2d = SKRSContext2D
208
+ export declare const Context2d: { prototype: SKRSContext2D }
209
+
210
+ // ---------------------------------------------------------------------------
211
+ // @napi-rs/canvas extras (not part of node-canvas, but useful)
212
+ // ---------------------------------------------------------------------------
213
+
214
+ export { IGlobalFonts }
215
+ export declare const GlobalFonts: IGlobalFonts & {
216
+ /** Reload system fonts. Available at runtime but not declared in IGlobalFonts. */
217
+ loadSystemFonts(): number
218
+ }
219
+ export { PDFDocument }
220
+ export { NapiCanvas as CanvasElement }
221
+ export { SvgCanvas as SVGCanvas }
package/node-canvas.js ADDED
@@ -0,0 +1,398 @@
1
+ 'use strict'
2
+
3
+ const { Readable } = require('node:stream')
4
+
5
+ const {
6
+ createCanvas: _createCanvas,
7
+ Canvas,
8
+ CanvasElement,
9
+ SVGCanvas,
10
+ GlobalFonts,
11
+ Image,
12
+ ImageData,
13
+ Path2D,
14
+ DOMPoint,
15
+ DOMMatrix,
16
+ DOMRect,
17
+ loadImage,
18
+ PDFDocument,
19
+ SvgExportFlag,
20
+ } = require('./index.js')
21
+
22
+ // CanvasRenderingContext2D is not re-exported by index.js, grab from native bindings
23
+ const { CanvasRenderingContext2D } = require('./js-binding')
24
+
25
+ // node-canvas defaults JPEG quality to 0.75 across all paths.
26
+ // @napi-rs/canvas native default is 0.92 (matching Blink/Chrome).
27
+ const NODE_CANVAS_DEFAULT_QUALITY = 0.75
28
+
29
+ // ---------------------------------------------------------------------------
30
+ // Stream classes (node-canvas returns Node.js Readable streams)
31
+ // ---------------------------------------------------------------------------
32
+
33
+ class PNGStream extends Readable {
34
+ constructor(canvas, options) {
35
+ super()
36
+ this._canvas = canvas
37
+ this._options = options || {}
38
+ this._done = false
39
+ }
40
+
41
+ _read() {
42
+ if (this._done) return
43
+ this._done = true
44
+ this._canvas
45
+ .encode('png', _pngOptions(this._options))
46
+ .then((buf) => {
47
+ this.push(buf)
48
+ this.push(null)
49
+ })
50
+ .catch((err) => {
51
+ this.destroy(err)
52
+ })
53
+ }
54
+ }
55
+
56
+ class JPEGStream extends Readable {
57
+ constructor(canvas, options) {
58
+ super()
59
+ this._canvas = canvas
60
+ const opts = options || {}
61
+ // node-canvas quality: 0-1 (default 0.75), encode() expects 0-100
62
+ this._quality = Math.round((opts.quality != null ? opts.quality : NODE_CANVAS_DEFAULT_QUALITY) * 100)
63
+ this._done = false
64
+ }
65
+
66
+ _read() {
67
+ if (this._done) return
68
+ this._done = true
69
+ this._canvas
70
+ .encode('jpeg', this._quality)
71
+ .then((buf) => {
72
+ this.push(buf)
73
+ this.push(null)
74
+ })
75
+ .catch((err) => {
76
+ this.destroy(err)
77
+ })
78
+ }
79
+ }
80
+
81
+ // ---------------------------------------------------------------------------
82
+ // Quality normalization helpers
83
+ // ---------------------------------------------------------------------------
84
+
85
+ const MIME_FORMAT_MAP = {
86
+ 'image/png': 'png',
87
+ 'image/jpeg': 'jpeg',
88
+ 'image/webp': 'webp',
89
+ 'image/avif': 'avif',
90
+ 'image/gif': 'gif',
91
+ }
92
+
93
+ /**
94
+ * Extract quality from a node-canvas config object or number.
95
+ * Returns the raw 0-1 quality value, defaulting to NODE_CANVAS_DEFAULT_QUALITY
96
+ * for JPEG/WebP when not specified.
97
+ *
98
+ * @param {string} mime
99
+ * @param {number|object|undefined} configOrQuality
100
+ * @returns {number|undefined} 0-1 scale for JPEG/WebP, undefined for other mimes
101
+ */
102
+ function _extractQuality(mime, configOrQuality) {
103
+ if (mime !== 'image/jpeg' && mime !== 'image/webp') return undefined
104
+ if (configOrQuality == null) return NODE_CANVAS_DEFAULT_QUALITY
105
+ if (typeof configOrQuality === 'number') return configOrQuality
106
+ if (typeof configOrQuality === 'object' && configOrQuality.quality != null) {
107
+ return configOrQuality.quality
108
+ }
109
+ return NODE_CANVAS_DEFAULT_QUALITY
110
+ }
111
+
112
+ /**
113
+ * The native PNG options for a node-canvas PngConfig: only `resolution`, the
114
+ * one member node-canvas writes (a pHYs chunk); undefined without a numeric
115
+ * one, so the native call stays a plain PNG encode.
116
+ *
117
+ * @param {object|undefined} config
118
+ * @returns {{ resolution: number }|undefined}
119
+ */
120
+ function _pngOptions(config) {
121
+ if (config === null || typeof config !== 'object') return undefined
122
+ const resolution = config.resolution
123
+ return typeof resolution === 'number' ? { resolution: resolution } : undefined
124
+ }
125
+
126
+ // ---------------------------------------------------------------------------
127
+ // Compat methods added to each canvas instance created via this module
128
+ // ---------------------------------------------------------------------------
129
+
130
+ // Keep references to the original prototype methods
131
+ const _origToBuffer = CanvasElement.prototype.toBuffer
132
+ const _origToDataURL = CanvasElement.prototype.toDataURL
133
+
134
+ function _compatCreatePNGStream(options) {
135
+ return new PNGStream(this, options)
136
+ }
137
+
138
+ function _compatCreateJPEGStream(options) {
139
+ return new JPEGStream(this, options)
140
+ }
141
+
142
+ /**
143
+ * node-canvas compatible toBuffer:
144
+ * - toBuffer() → PNG (sync)
145
+ * - toBuffer('image/png', config?) → PNG (sync)
146
+ * - toBuffer('image/jpeg', config?) → JPEG (sync, quality 0-1)
147
+ * - toBuffer('raw') → raw pixel data (sync)
148
+ * - toBuffer(callback) → PNG (async)
149
+ * - toBuffer(callback, mime, config?) → specified format (async)
150
+ */
151
+ function _compatToBuffer(mimeOrCallback, configOrQuality) {
152
+ // Callback form: toBuffer(callback) or toBuffer(callback, mime, config)
153
+ if (typeof mimeOrCallback === 'function') {
154
+ const callback = mimeOrCallback
155
+ const mime = typeof configOrQuality === 'string' ? configOrQuality : 'image/png'
156
+ const config = arguments[2]
157
+
158
+ if (mime === 'raw') {
159
+ let buf
160
+ try {
161
+ buf = this.data()
162
+ } catch (err) {
163
+ callback(err)
164
+ return
165
+ }
166
+ callback(null, buf)
167
+ return
168
+ }
169
+
170
+ const format = MIME_FORMAT_MAP[mime]
171
+ if (!format) {
172
+ callback(new TypeError(`Unsupported MIME type "${mime}". Supported: ${Object.keys(MIME_FORMAT_MAP).join(', ')}`))
173
+ return
174
+ }
175
+ const q = _extractQuality(mime, config)
176
+ const options = format === 'png' ? _pngOptions(config) : q != null ? Math.round(q * 100) : undefined
177
+ this.encode(format, options).then(
178
+ (buf) => callback(null, buf),
179
+ (err) => callback(err),
180
+ )
181
+ return
182
+ }
183
+
184
+ // Sync: no args → PNG
185
+ if (mimeOrCallback === undefined) {
186
+ return _origToBuffer.call(this, 'image/png')
187
+ }
188
+
189
+ // Sync: raw pixel data
190
+ if (mimeOrCallback === 'raw') {
191
+ return this.data()
192
+ }
193
+
194
+ // Sync: PNG takes the resolution; JPEG / WebP a quality (0-1) scaled to
195
+ // 0-100 for native toBuffer
196
+ if (mimeOrCallback === 'image/png') {
197
+ return _origToBuffer.call(this, mimeOrCallback, _pngOptions(configOrQuality))
198
+ }
199
+ const q = _extractQuality(mimeOrCallback, configOrQuality)
200
+ return _origToBuffer.call(this, mimeOrCallback, q != null ? Math.round(q * 100) : undefined)
201
+ }
202
+
203
+ /**
204
+ * node-canvas compatible toDataURL:
205
+ * - toDataURL() → PNG data URL (sync)
206
+ * - toDataURL('image/jpeg', quality) → JPEG data URL, quality 0-1 (sync)
207
+ * - toDataURL(callback) → PNG data URL (async)
208
+ * - toDataURL(mime, callback) → data URL (async)
209
+ * - toDataURL(mime, quality, callback) → data URL with quality (async)
210
+ */
211
+ function _compatToDataURL(mimeOrCallback, qualityOrCallback) {
212
+ // toDataURL(callback)
213
+ if (typeof mimeOrCallback === 'function') {
214
+ this.toDataURLAsync('image/png').then(
215
+ (url) => mimeOrCallback(null, url),
216
+ (err) => mimeOrCallback(err),
217
+ )
218
+ return
219
+ }
220
+ // toDataURL(mime, callback)
221
+ if (typeof qualityOrCallback === 'function') {
222
+ this.toDataURLAsync(mimeOrCallback, _extractQuality(mimeOrCallback, undefined)).then(
223
+ (url) => qualityOrCallback(null, url),
224
+ (err) => qualityOrCallback(err),
225
+ )
226
+ return
227
+ }
228
+ // toDataURL(mime, quality, callback)
229
+ const cb = arguments[2]
230
+ if (typeof cb === 'function') {
231
+ // Native toDataURLAsync expects 0-1 (f64) — pass quality through as-is
232
+ const quality = _extractQuality(mimeOrCallback, qualityOrCallback)
233
+ this.toDataURLAsync(mimeOrCallback, quality).then(
234
+ (url) => cb(null, url),
235
+ (err) => cb(err),
236
+ )
237
+ return
238
+ }
239
+
240
+ // Sync form: native toDataURL expects 0-1 (f64) — pass quality through as-is
241
+ const quality = _extractQuality(mimeOrCallback, qualityOrCallback)
242
+ return _origToDataURL.call(this, mimeOrCallback, quality)
243
+ }
244
+
245
+ /**
246
+ * Attach node-canvas compatible methods to a CanvasElement instance.
247
+ */
248
+ function _addCompatMethods(canvas) {
249
+ canvas.createPNGStream = _compatCreatePNGStream
250
+ canvas.createJPEGStream = _compatCreateJPEGStream
251
+ canvas.toBuffer = _compatToBuffer
252
+ canvas.toDataURL = _compatToDataURL
253
+ return canvas
254
+ }
255
+
256
+ // ---------------------------------------------------------------------------
257
+ // Public API: registerFont / deregisterAllFonts
258
+ // ---------------------------------------------------------------------------
259
+
260
+ // Track FontKeys from registerFont() so deregisterAllFonts() can remove
261
+ // only user-registered fonts (matching node-canvas behavior which leaves
262
+ // system fonts untouched).
263
+ const _registeredFontKeys = []
264
+
265
+ /**
266
+ * Register a font file with the specified font face properties.
267
+ * Compatible with node-canvas's registerFont(path, { family, weight?, style? }).
268
+ *
269
+ * Note: @napi-rs/canvas auto-detects weight and style from font file metadata
270
+ * (matching browser behavior). The weight and style properties in fontFace are
271
+ * accepted for API compatibility but the actual values are read from the font.
272
+ *
273
+ * @param {string} path Absolute path to the font file (.ttf, .otf, etc.)
274
+ * @param {{ family: string, weight?: string, style?: string }} fontFace
275
+ */
276
+ function registerFont(path, fontFace) {
277
+ if (!fontFace || typeof fontFace.family !== 'string') {
278
+ throw new TypeError('registerFont requires a fontFace with a "family" property')
279
+ }
280
+ const key = GlobalFonts.registerFromPath(path, fontFace.family)
281
+ if (!key) {
282
+ throw new Error(`Failed to register font from "${path}" with family "${fontFace.family}"`)
283
+ }
284
+ _registeredFontKeys.push(key)
285
+ }
286
+
287
+ /**
288
+ * Deregister all fonts previously registered via registerFont().
289
+ * Compatible with node-canvas's deregisterAllFonts() which only removes
290
+ * user-registered fonts and leaves system fonts untouched.
291
+ */
292
+ function deregisterAllFonts() {
293
+ if (_registeredFontKeys.length > 0) {
294
+ GlobalFonts.removeBatch(_registeredFontKeys)
295
+ _registeredFontKeys.length = 0
296
+ }
297
+ }
298
+
299
+ // ---------------------------------------------------------------------------
300
+ // Public API: createCanvas / createImageData
301
+ // ---------------------------------------------------------------------------
302
+
303
+ /**
304
+ * Create a new canvas instance with node-canvas compatible API.
305
+ *
306
+ * @param {number} width
307
+ * @param {number} height
308
+ * @param {'image'|'svg'} [type='image']
309
+ * @returns {Canvas}
310
+ */
311
+ function createCanvas(width, height, type) {
312
+ if (type === 'svg') {
313
+ // SvgExportFlag enum requires a valid variant; NoPrettyXML (0x02) is the
314
+ // least impactful on rendering behavior (only affects XML whitespace).
315
+ return _createCanvas(width, height, SvgExportFlag.NoPrettyXML)
316
+ }
317
+ if (type === 'pdf') {
318
+ throw new Error(
319
+ 'createCanvas with type "pdf" is not supported. Use the PDFDocument class from @napi-rs/canvas directly.',
320
+ )
321
+ }
322
+ if (type != null && type !== 'image') {
323
+ throw new TypeError(`createCanvas: unknown type "${type}". Supported types: "image", "svg".`)
324
+ }
325
+ return _addCompatMethods(_createCanvas(width, height))
326
+ }
327
+
328
+ /**
329
+ * Create an ImageData instance.
330
+ * Compatible with node-canvas's createImageData().
331
+ *
332
+ * @param {Uint8ClampedArray|number} dataOrWidth
333
+ * @param {number} widthOrHeight
334
+ * @param {number} [height]
335
+ * @returns {ImageData}
336
+ */
337
+ function createImageData(dataOrWidth, widthOrHeight, height) {
338
+ if (typeof dataOrWidth === 'number') {
339
+ return new ImageData(dataOrWidth, widthOrHeight)
340
+ }
341
+ if (height != null) {
342
+ return new ImageData(dataOrWidth, widthOrHeight, height)
343
+ }
344
+ return new ImageData(dataOrWidth, widthOrHeight)
345
+ }
346
+
347
+ // ---------------------------------------------------------------------------
348
+ // Canvas constructor wrapper
349
+ // ---------------------------------------------------------------------------
350
+
351
+ // In node-canvas, `new Canvas(w, h)` and `createCanvas(w, h)` produce
352
+ // equivalent canvases. Wrap the native Canvas constructor so that
353
+ // `new Canvas(w, h)` also gets compat methods, without polluting the
354
+ // prototype (which would affect @napi-rs/canvas users who don't use
355
+ // this compat layer).
356
+ const CompatCanvas = new Proxy(Canvas, {
357
+ construct(target, args) {
358
+ const canvas = new target(...args)
359
+ if (canvas instanceof CanvasElement) {
360
+ return _addCompatMethods(canvas)
361
+ }
362
+ return canvas
363
+ },
364
+ })
365
+
366
+ // ---------------------------------------------------------------------------
367
+ // Exports (matches node-canvas export shape)
368
+ // ---------------------------------------------------------------------------
369
+
370
+ module.exports = {
371
+ // Factory functions
372
+ Canvas: CompatCanvas,
373
+ createCanvas,
374
+ createImageData,
375
+ loadImage,
376
+ registerFont,
377
+ deregisterAllFonts,
378
+
379
+ // Classes
380
+ Image,
381
+ ImageData,
382
+ CanvasRenderingContext2D,
383
+ Context2d: CanvasRenderingContext2D,
384
+ PNGStream,
385
+ JPEGStream,
386
+ Path2D,
387
+
388
+ // Geometry
389
+ DOMPoint,
390
+ DOMMatrix,
391
+ DOMRect,
392
+
393
+ // @napi-rs/canvas extras (available but not part of node-canvas)
394
+ GlobalFonts,
395
+ PDFDocument,
396
+ CanvasElement,
397
+ SVGCanvas,
398
+ }
package/package.json CHANGED
@@ -1,6 +1,58 @@
1
1
  {
2
2
  "name": "@nexterm/canvas",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.1.1",
4
+ "description": "@napi-rs/canvas 1.1.1 with musl binaries",
5
+ "main": "index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Brooooooklyn/canvas.git"
9
+ },
10
+ "license": "MIT",
11
+ "keywords": [
12
+ "napi-rs",
13
+ "NAPI",
14
+ "N-API",
15
+ "Rust",
16
+ "node-addon",
17
+ "node-addon-api",
18
+ "canvas",
19
+ "image",
20
+ "pdf",
21
+ "svg",
22
+ "skia"
23
+ ],
24
+ "files": [
25
+ "index.d.ts",
26
+ "index.js",
27
+ "geometry.js",
28
+ "js-binding.js",
29
+ "load-image.js",
30
+ "node-canvas.js",
31
+ "node-canvas.d.ts"
32
+ ],
33
+ "engines": {
34
+ "node": ">= 10"
35
+ },
36
+ "publishConfig": {
37
+ "registry": "https://registry.npmjs.org/",
38
+ "access": "public"
39
+ },
40
+ "dependenciesMeta": {
41
+ "canvas": {
42
+ "built": true
43
+ }
44
+ },
45
+ "optionalDependencies": {
46
+ "@napi-rs/canvas-linux-x64-gnu": "1.1.1",
47
+ "@napi-rs/canvas-darwin-x64": "1.1.1",
48
+ "@napi-rs/canvas-win32-x64-msvc": "1.1.1",
49
+ "@napi-rs/canvas-win32-arm64-msvc": "1.1.1",
50
+ "@napi-rs/canvas-linux-arm-gnueabihf": "1.1.1",
51
+ "@napi-rs/canvas-linux-x64-musl": "npm:@nexterm/canvas-linux-x64-musl@1.1.1",
52
+ "@napi-rs/canvas-linux-arm64-gnu": "1.1.1",
53
+ "@napi-rs/canvas-linux-arm64-musl": "npm:@nexterm/canvas-linux-arm64-musl@1.1.1",
54
+ "@napi-rs/canvas-darwin-arm64": "1.1.1",
55
+ "@napi-rs/canvas-android-arm64": "1.1.1",
56
+ "@napi-rs/canvas-linux-riscv64-gnu": "1.1.1"
57
+ }
58
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.