node-canvas.d.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. import { Readable } from 'node:stream'
  17. // Re-export types from @napi-rs/canvas that are API-compatible
  18. import {
  19. Image as NapiImage,
  20. ImageData as NapiImageData,
  21. Path2D as NapiPath2D,
  22. DOMPoint as NapiDOMPoint,
  23. DOMMatrix as NapiDOMMatrix,
  24. DOMRect as NapiDOMRect,
  25. SKRSContext2D,
  26. Canvas as NapiCanvas,
  27. SvgCanvas,
  28. SvgExportFlag,
  29. IGlobalFonts,
  30. PDFDocument,
  31. } from './index'
  32. // ---------------------------------------------------------------------------
  33. // Config interfaces (node-canvas conventions)
  34. // ---------------------------------------------------------------------------
  35. export interface PngConfig {
  36. /** ZLIB compression level (0-9). Accepted for compatibility; Skia uses its own encoder. */
  37. compressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
  38. /** PNG filter flags. Accepted for compatibility; Skia uses its own encoder. */
  39. filters?: number
  40. /** Palette for indexed PNGs. Not supported by Skia backend. */
  41. palette?: Uint8ClampedArray
  42. /** Background color index for indexed PNGs. Not supported by Skia backend. */
  43. backgroundIndex?: number
  44. /** Pixels per inch. Accepted for compatibility. */
  45. resolution?: number
  46. }
  47. export interface JpegConfig {
  48. /** Quality between 0 and 1. Defaults to 0.75. Converted to 0-100 for Skia. */
  49. quality?: number
  50. /** Progressive encoding. Accepted for compatibility; not supported by Skia backend. */
  51. progressive?: boolean
  52. /** 2x2 chroma subsampling. Accepted for compatibility; not supported by Skia backend. */
  53. chromaSubsampling?: boolean
  54. }
  55. // ---------------------------------------------------------------------------
  56. // Canvas class (with node-canvas compatible methods)
  57. // ---------------------------------------------------------------------------
  58. export interface Canvas extends Omit<NapiCanvas, 'toBuffer' | 'toDataURL'> {
  59. /**
  60. * Encode the canvas as a PNG and return a Node.js Readable stream.
  61. * @param config PNG encoding options (accepted for compatibility)
  62. */
  63. createPNGStream(config?: PngConfig): PNGStream
  64. /**
  65. * Encode the canvas as a JPEG and return a Node.js Readable stream.
  66. * @param config JPEG encoding options. Quality is 0-1 (default 0.75).
  67. */
  68. createJPEGStream(config?: JpegConfig): JPEGStream
  69. // --- toBuffer overloads (node-canvas conventions) ---
  70. /** Encode as PNG (default). */
  71. toBuffer(): Buffer
  72. toBuffer(mimeType: 'image/png', config?: PngConfig): Buffer
  73. /** Encode as JPEG. Quality in config is 0-1 (default 0.75). */
  74. toBuffer(mimeType: 'image/jpeg', config?: JpegConfig): Buffer
  75. /** Get raw unencoded pixel data. */
  76. toBuffer(mimeType: 'raw'): Buffer
  77. /** Async: encode as PNG (default) via callback. */
  78. toBuffer(cb: (err: Error | null, result: Buffer) => void): void
  79. toBuffer(cb: (err: Error | null, result: Buffer) => void, mimeType: 'image/png', config?: PngConfig): void
  80. /** Async: encode as JPEG via callback. Quality in config is 0-1 (default 0.75). */
  81. toBuffer(cb: (err: Error | null, result: Buffer) => void, mimeType: 'image/jpeg', config?: JpegConfig): void
  82. // --- toDataURL overloads (node-canvas conventions) ---
  83. /** Encode as PNG data URL (default). */
  84. toDataURL(): string
  85. toDataURL(mimeType: 'image/png'): string
  86. /** Encode as JPEG data URL. Quality is 0-1. */
  87. toDataURL(mimeType: 'image/jpeg', quality?: number): string
  88. /** Async: encode as data URL via callback. */
  89. toDataURL(cb: (err: Error | null, result: string) => void): void
  90. toDataURL(mimeType: 'image/png', cb: (err: Error | null, result: string) => void): void
  91. toDataURL(mimeType: 'image/jpeg', cb: (err: Error | null, result: string) => void): void
  92. toDataURL(mimeType: 'image/jpeg', quality: number, cb: (err: Error | null, result: string) => void): void
  93. }
  94. // ---------------------------------------------------------------------------
  95. // Stream classes
  96. // ---------------------------------------------------------------------------
  97. /** Readable stream that emits PNG-encoded data. */
  98. export class PNGStream extends Readable {}
  99. /** Readable stream that emits JPEG-encoded data. */
  100. export class JPEGStream extends Readable {}
  101. // ---------------------------------------------------------------------------
  102. // Factory functions
  103. // ---------------------------------------------------------------------------
  104. /**
  105. * Create a new canvas with node-canvas compatible API.
  106. * Canvas instances created through this function have `createPNGStream()`,
  107. * `createJPEGStream()`, and node-canvas compatible `toBuffer()` overloads.
  108. *
  109. * When `type` is `'svg'`, returns a `SvgCanvas` which produces vector output
  110. * via `getContent()` instead of raster encoding methods.
  111. *
  112. * @param width Canvas width in pixels
  113. * @param height Canvas height in pixels
  114. * @param type Canvas type: 'image' (default) or 'svg'
  115. */
  116. export function createCanvas(width: number, height: number, type: 'svg'): SvgCanvas
  117. export function createCanvas(width: number, height: number, type?: 'image'): Canvas
  118. /**
  119. * Create an ImageData instance.
  120. * @param data Pixel data array
  121. * @param width Width in pixels
  122. * @param height Height in pixels (calculated from data length if omitted)
  123. */
  124. export function createImageData(data: Uint8ClampedArray, width: number, height?: number): NapiImageData
  125. export function createImageData(width: number, height: number): NapiImageData
  126. /**
  127. * Load an image from a file path, URL, Buffer, or other source.
  128. * Returns a Promise that resolves to an Image instance.
  129. */
  130. export function loadImage(
  131. source: string | URL | Buffer | ArrayBufferLike | Uint8Array | NapiImage | import('stream').Readable,
  132. options?: { alt?: string; maxRedirects?: number; requestOptions?: import('http').RequestOptions },
  133. ): Promise<NapiImage>
  134. // ---------------------------------------------------------------------------
  135. // Font registration (node-canvas convention)
  136. // ---------------------------------------------------------------------------
  137. /**
  138. * Register a font file for use in canvas text rendering.
  139. * Compatible with node-canvas's `registerFont()`.
  140. *
  141. * Note: @napi-rs/canvas auto-detects weight and style from font file metadata
  142. * (matching browser behavior). The `weight` and `style` properties in fontFace
  143. * are accepted for API compatibility.
  144. *
  145. * @param path Absolute path to the font file (.ttf, .otf, etc.)
  146. * @param fontFace Font face properties matching CSS @font-face descriptors
  147. */
  148. export function registerFont(path: string, fontFace: { family: string; weight?: string; style?: string }): void
  149. /**
  150. * Deregister all previously registered fonts.
  151. * Compatible with node-canvas's `deregisterAllFonts()`.
  152. */
  153. export function deregisterAllFonts(): void
  154. // ---------------------------------------------------------------------------
  155. // Re-exported classes & values
  156. // ---------------------------------------------------------------------------
  157. // Canvas is exported both as a type (the compat interface above) and as a
  158. // value (constructor that returns compat Canvas instances at runtime).
  159. export declare const Canvas: {
  160. new (width: number, height: number): Canvas
  161. new (width: number, height: number, flag: SvgExportFlag): SvgCanvas
  162. prototype: NapiCanvas
  163. }
  164. export { NapiImage as Image }
  165. export { NapiImageData as ImageData }
  166. export { NapiPath2D as Path2D }
  167. export { NapiDOMPoint as DOMPoint }
  168. export { NapiDOMMatrix as DOMMatrix }
  169. export { NapiDOMRect as DOMRect }
  170. // SKRSContext2D is an interface in index.d.ts but a class value at runtime.
  171. // Declare as const to export the value for instanceof checks and re-export the type.
  172. export type CanvasRenderingContext2D = SKRSContext2D
  173. export declare const CanvasRenderingContext2D: { prototype: SKRSContext2D }
  174. /** Legacy alias for CanvasRenderingContext2D. */
  175. export type Context2d = SKRSContext2D
  176. export declare const Context2d: { prototype: SKRSContext2D }
  177. // ---------------------------------------------------------------------------
  178. // @napi-rs/canvas extras (not part of node-canvas, but useful)
  179. // ---------------------------------------------------------------------------
  180. export { IGlobalFonts }
  181. export declare const GlobalFonts: IGlobalFonts & {
  182. /** Reload system fonts. Available at runtime but not declared in IGlobalFonts. */
  183. loadSystemFonts(): number
  184. }
  185. export { PDFDocument }
  186. export { NapiCanvas as CanvasElement }
  187. export { SvgCanvas as SVGCanvas }