canvas icon indicating copy to clipboard operation
canvas copied to clipboard

How do I register custom fonts and use image links rather than buffer?

Open 0zul opened this issue 3 years ago • 4 comments

I was curious to know how can I register fonts from a local directory without installing them.

0zul avatar May 26 '21 18:05 0zul

See https://github.com/Brooooooklyn/canvas/blob/main/index.d.ts#L280

Brooooooklyn avatar May 27 '21 03:05 Brooooooklyn

Also is it possible for me to use the image's as links because as I see, only accepts buffer feed? https://github.com/Brooooooklyn/canvas/blob/main/index.d.ts#L191

0zul avatar May 27 '21 04:05 0zul

Also is it possible for me to use the image's as links because as I see, only accepts buffer feed? https://github.com/Brooooooklyn/canvas/blob/main/index.d.ts#L191

You can use fs to convert local files to buffer and for links, you can use native https module or third party modules like node-fetch

fetch("remote_image")
  .then(res => res.buffer())
  .then(data => /* load this data as image */)

Also #123, I believe there will be a function similar to https://www.npmjs.com/package/canvas#loadimage

twlite avatar May 27 '21 06:05 twlite

@0zul I guess this issue is closable since this lib supports registering custom fonts as well as a utility function to create Image?

Fonts can be registered with the help of GlobalFonts as mentioned in https://github.com/Brooooooklyn/canvas#emoji-text

const { GlobalFonts } = require('@napi-rs/canvas')

GlobalFonts.registerFromPath('./somefont.ttf', 'Name Alias')

and images can be loaded via loadImage utility function (very similar to node-canvas)

const { loadImage } = require('@napi-rs/canvas')

const source = someSource() // source can be file path, url, buffer, etc.
const image = await loadImage(source)

twlite avatar Jul 30 '22 12:07 twlite