You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Registering fonts after drawing on canvas seems to work fine? #1921
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:
varfs=require('fs');varCanvas=require('canvas');varcanvas=Canvas.createCanvas(640,100);varctx=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:
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)
Update: This has now started to fail (font renders as some kind of generic default) with canvas v2.9.1, when the font is registered after the canvas is created.
Examples:
canvas v2.9.0 (correct font):
canvas v2.9.1 (incorrect font):
So I guess now the order really matters... at least on Linux. macOS doesn't seem to care.
It's been a while since you asked, but the answer is that Pango caches all fonts present on the OS in a FontMap. When you register a custom font, we change the defaultFontMap, but we don't update the PangoContext stored on the canvas to use that new FontMap. (I'm not sure how it could have worked before. Maybe Pango used to do something differently?).
Activity
jhuckaby commentedon May 25, 2022
Update: This has now started to fail (font renders as some kind of generic default) with canvas v2.9.1, when the font is registered after the canvas is created.
Examples:
canvas v2.9.0 (correct font):
canvas v2.9.1 (incorrect font):
So I guess now the order really matters... at least on Linux. macOS doesn't seem to care.
allow registerFont after a canvas has been created
chearon commentedon Jan 15, 2025
It's been a while since you asked, but the answer is that Pango caches all fonts present on the OS in a
FontMap. When you register a custom font, we change the defaultFontMap, but we don't update thePangoContextstored on the canvas to use that newFontMap. (I'm not sure how it could have worked before. Maybe Pango used to do something differently?).allow registerFont after a canvas has been created
allow registerFont after a canvas has been created
allow registerFont after a canvas has been created
allow registerFont after a canvas has been created (#2483)