Skip to content

Registering fonts after drawing on canvas seems to work fine? #1921

@jhuckaby

Description

@jhuckaby

Documentation Issue?

The docs state (note emphasis):

To use a font file that is not installed as a system font, use registerFont() to register the font with Canvas. This must be done before the Canvas is created.

However, going against the docs and loading the font after creating and drawing into the canvas seems to work just fine for me. My code:

var fs = require('fs');
var Canvas = require('canvas');
var canvas = Canvas.createCanvas(640, 100);
var ctx = canvas.getContext('2d');

ctx.strokeStyle = 'gray';
ctx.fillStyle = 'yellow';
ctx.beginPath();
ctx.rect( 50, 10, 300, 80 );
ctx.fill();
ctx.stroke();

Canvas.registerFont(('futurastd-m.otf'), { family: 'FuturaStdMedium' });

ctx.font = 'normal normal 36px FuturaStdMedium';
ctx.fillStyle = 'black';
ctx.fillText('Now is the time for all good men.', 10, 35);

canvas.createPNGStream().pipe(fs.createWriteStream('output.png'));

Output:

output-joe

This renders correctly. I guess I was expecting it to crash or something? My question is, is this just a documentation issue? Was the requirement a previous limitation with node-canvas v1 perhaps, now gone with v2?

Your Environment

  • Version: node-canvas HEAD revision, compiled from source 2021/11/03 (reported version: 2.8)
  • Environment: CentOS Linux release 7.7.1908 (Core)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions