Drawing on canvas with custom font face does not work
See original GitHub issueUsing a custom font face to draw to a canvas does not work. Test case (based on pdf.js test):
var jsdom = require('jsdom');
var HTML = "<html><head><style id='test'>@font-face { font-family: 'plus'; src: url(data:font/opentype;base64,AAEAAAAOAIAAAwBgRkZUTWNJJVkAAAZEAAAAHEdERUYANQAkAAAGHAAAAChPUy8yVkDi7gAAAWgAAABgY21hcPAZ92QAAAHcAAABUmN2dCAAIQJ5AAADMAAAAARnYXNw//8AAwAABhQAAAAIZ2x5Zk7Cd0UAAANEAAAA8GhlYWT8fgSnAAAA7AAAADZoaGVhBuoD7QAAASQAAAAkaG10eAwCALUAAAHIAAAAFGxvY2EA5gCyAAADNAAAAA5tYXhwAEoAPQAAAUgAAAAgbmFtZWDR73sAAAQ0AAABnnBvc3RBBJyBAAAF1AAAAD4AAQAAAAEAAPbZ2E5fDzz1AB8D6AAAAADM3+BPAAAAAMzf4E8AIQAAA2sDJAAAAAgAAgAAAAAAAAABAAADJAAAAFoD6AAAAAADawABAAAAAAAAAAAAAAAAAAAABAABAAAABgAMAAIAAAAAAAIAAAABAAEAAABAAC4AAAAAAAQD6AH0AAUAAAKKArwAAACMAooCvAAAAeAAMQECAAACAAYJAAAAAAAAAAAAARAAAAAAAAAAAAAAAFBmRWQAwABg8DADIP84AFoDJAAAgAAAAQAAAAAAAAAAAAAAIAABA+gAIQAAAAAD6AAAA+gASgBKAEoAAAADAAAAAwAAABwAAQAAAAAATAADAAEAAAAcAAQAMAAAAAgACAACAAAAYPAA8DD//wAAAGDwAPAw////oxAED9UAAQAAAAAAAAAAAAABBgAAAQAAAAAAAAABAgAAAAIAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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QwECAQMHdW5pRjAwMAd1bmlGMDMwAAAAAAAB//8AAgABAAAADgAAABgAIAAAAAIAAQABAAUAAQAEAAAAAgAAAAEAAAABAAAAAAABAAAAAMmJbzEAAAAAzN/V8gAAAADM3+A1); }</style></head><body><div id='canvasHolder'></div></body></html>";
var document = jsdom.jsdom(HTML).createWindow().document;
console.log(checkCanvas('plus') ? "font-face supported" : "font-face NOT supported");
function checkCanvas(font) {
var canvas = document.createElement('canvas');
var canvasHolder = document.getElementById('canvasHolder');
canvasHolder.appendChild(canvas);
var ctx = canvas.getContext('2d');
ctx.font = '40px \'' + font + '\'';
ctx.fillText('\u0060', 0, 40);
var data = ctx.getImageData(0, 0, 40, 40).data;
canvasHolder.removeChild(canvas);
// detects plus figure
var minx = 40, maxx = 0, miny = 40, maxy = 0;
for (var y = 0; y < 40; y++) {
for (var x = 0; x < 40; x++) {
if (data[x * 4 + y * 160 + 3] == 0) continue; // no color
minx = Math.min(minx, x); miny = Math.min(miny, y);
maxx = Math.max(maxx, x); maxy = Math.max(maxy, y);
}
}
var colors = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
var counts = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
for (var y = miny; y <= maxy; y++) {
for (var x = minx; x <= maxx; x++) {
var i = Math.floor((x - minx) * 3 / (maxx - minx + 1));
var j = Math.floor((y - miny) * 3 / (maxy - miny + 1));
counts[i][j]++;
if (data[x * 4 + y * 160 + 3] != 0)
colors[i][j]++;
}
}
var isPlus =
colors[0][0] * 3 < counts[0][0] &&
colors[0][1] * 3 > counts[0][1] &&
colors[0][2] * 3 < counts[0][2] &&
colors[1][0] * 3 > counts[1][0] &&
colors[1][1] * 3 > counts[1][1] &&
colors[1][2] * 3 > counts[1][2] &&
colors[2][0] * 3 < counts[2][0] &&
colors[2][1] * 3 > counts[2][1] &&
colors[2][2] * 3 < counts[2][2];
return isPlus;
}
It seems there is no connection for passing custom font face to canvas?
Issue Analytics
- State:
- Created 10 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Drawing text to <canvas> with @font-face does not work at the ...
Drawing on canvas has to happen and return immediately when you call the fillText method. However, the browser has not yet loaded the...
Read more >How to add a custom font to an HTML canvas
So yesterday I was working on a canvas and I realized that the font I had tried to add using @font-face with CSS,...
Read more >Drawing text to HTML5 with @fontface does not work at the ...
Drawing text in a canvas with a typeface that is loaded via @font-face does not show text correctly at first. This is because...
Read more >CanvasRenderingContext2D.font - Web APIs | MDN
The CanvasRenderingContext2D.font property of the Canvas 2D API specifies the current text style to use when drawing text.
Read more >How to use custom font for HTML5 canvas? - Konva
How to draw external font on html5 canvas?If you want to use custom font for Konva.Text you just need to: Add font style...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Is this still an issue? i would like to do something to get it fixed.
Any updates on this?? I’m really stuck at the same issue, I’m using pdfkit and frontend canvas code (by jsdom) to generate a pdf of a book with images and text. I just cant get the fonts working because of this