index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const Canvas = require('./lib/canvas')
  2. const Image = require('./lib/image')
  3. const CanvasRenderingContext2D = require('./lib/context2d')
  4. const CanvasPattern = require('./lib/pattern')
  5. const parseFont = require('./lib/parse-font')
  6. const packageJson = require('./package.json')
  7. const bindings = require('./lib/bindings')
  8. const fs = require('fs')
  9. const PNGStream = require('./lib/pngstream')
  10. const PDFStream = require('./lib/pdfstream')
  11. const JPEGStream = require('./lib/jpegstream')
  12. const { DOMPoint, DOMMatrix } = require('./lib/DOMMatrix')
  13. function createCanvas (width, height, type) {
  14. return new Canvas(width, height, type)
  15. }
  16. function createImageData (array, width, height) {
  17. return new bindings.ImageData(array, width, height)
  18. }
  19. function loadImage (src) {
  20. return new Promise((resolve, reject) => {
  21. const image = new Image()
  22. function cleanup () {
  23. image.onload = null
  24. image.onerror = null
  25. }
  26. image.onload = () => { cleanup(); resolve(image) }
  27. image.onerror = (err) => { cleanup(); reject(err) }
  28. image.src = src
  29. })
  30. }
  31. /**
  32. * Resolve paths for registerFont. Must be called *before* creating a Canvas
  33. * instance.
  34. * @param src {string} Path to font file.
  35. * @param fontFace {{family: string, weight?: string, style?: string}} Object
  36. * specifying font information. `weight` and `style` default to `"normal"`.
  37. */
  38. function registerFont (src, fontFace) {
  39. // TODO this doesn't need to be on Canvas; it should just be a static method
  40. // of `bindings`.
  41. return Canvas._registerFont(fs.realpathSync(src), fontFace)
  42. }
  43. /**
  44. * Unload all fonts from pango to free up memory
  45. */
  46. function deregisterAllFonts () {
  47. return Canvas._deregisterAllFonts()
  48. }
  49. exports.Canvas = Canvas
  50. exports.Context2d = CanvasRenderingContext2D // Legacy/compat export
  51. exports.CanvasRenderingContext2D = CanvasRenderingContext2D
  52. exports.CanvasGradient = bindings.CanvasGradient
  53. exports.CanvasPattern = CanvasPattern
  54. exports.Image = Image
  55. exports.ImageData = bindings.ImageData
  56. exports.PNGStream = PNGStream
  57. exports.PDFStream = PDFStream
  58. exports.JPEGStream = JPEGStream
  59. exports.DOMMatrix = DOMMatrix
  60. exports.DOMPoint = DOMPoint
  61. exports.registerFont = registerFont
  62. exports.deregisterAllFonts = deregisterAllFonts
  63. exports.parseFont = parseFont
  64. exports.createCanvas = createCanvas
  65. exports.createImageData = createImageData
  66. exports.loadImage = loadImage
  67. exports.backends = bindings.Backends
  68. /** Library version. */
  69. exports.version = packageJson.version
  70. /** Cairo version. */
  71. exports.cairoVersion = bindings.cairoVersion
  72. /** jpeglib version. */
  73. exports.jpegVersion = bindings.jpegVersion
  74. /** gif_lib version. */
  75. exports.gifVersion = bindings.gifVersion ? bindings.gifVersion.replace(/[^.\d]/g, '') : undefined
  76. /** freetype version. */
  77. exports.freetypeVersion = bindings.freetypeVersion
  78. /** rsvg version. */
  79. exports.rsvgVersion = bindings.rsvgVersion
  80. /** pango version. */
  81. exports.pangoVersion = bindings.pangoVersion