-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
createPNGStream and Font() support in version 2.0.0 rc1 #4541
Comments
i opened this issue in JSDOM to get access to the underlying canvas. I just added this thing of accessing the Font that i completely forgot about. I'll help you with createPNGStream asap, i m overloaded those days |
Hi, is there any chance we can get a new release candidate with Font added back in? It would be much appreciated. Thanks for all of your hard work. |
Can you point me at were Font was before? I admit my node+fabric proficency suck to be the mantainer. I'll re release the RC asap. as i said, i am a bit busy. |
In the fabric 1.6 code, in createCanvasForNode It simply passed through the Canvas Font:
|
So the old code was binding on a per instance basis, and was passing trough a class from the library, not from the instance. maybe we should just expose the nodeCanvas import under fabric namespace? |
Yeah, that sounds like it would work. |
It looks like i can't make it to work on my setup, whatever i do the font is not used. const { fabric } = require('fabric');
const { writeFileSync } = require('fs');
const path = require('path')
function fontFile (name) {
return path.join(__dirname, name)
}
const canvas = new fabric.StaticCanvas(null, { width: 1000, height: 1000 });
var font = new fabric.nodeCanvas.Font('Yataghan', fontFile('WESTTEST.TTF'));
const text = new fabric.Text('Test custom font', { fontFamily: 'Yataghan', fontSize: 500, objectCaching: false });
canvas.contextContainer.addFont(font);
canvas.backgroundColor = 'white';
canvas.add(text);
console.log(text._getFontDeclaration())
writeFileSync('./testFont.png', canvas.toDataURL().replace('data:image/png;base64,', ""), 'base64'); |
anyway i'm merging those changes since they expose back the font method. if you have it working please share your setup, i would like this font registering to be as simple as possible. |
I'll see what I can do tomorrow. |
I tracked this down to an issue in node-canvas, which it appears has never worked. This code will only render as Arial/12pt.
I tried the similar code in the node-canvas 2.0 alpha, and it renders the font properly:
There is another thing that may be causing a misunderstanding on the node-canvas side. If you have the font installed on your system, the first example works fine. Unfortunately, this is not an option for us as we use Lambda as our node.js container. I'm going to raise this issue with node-canvas as well and see if they are willing to fix it. I'm also going to try to fork fabric to use the node-canvas 2.0 alpha and see if I can get the issue fixed that way. |
It appears that this has been a known issue for a while: They didn't want to fix it in the 1.0 code, so they implemented the fixes in the 2.0 branch. |
you can build node 1.x to use pango instead of cairo. i do not remember how. |
Problem of switching to node 2 is that jsdom does not support it yet, so you have to fork jsdom too and pull in the proposed patch. |
I built node-canvas with pango support but nothing changed. |
I forked jsdom today to support node-canvas 2, and I have fonts drawing on the canvas. The change to jsdom was trivial. Tomorrow I'll fork fabric using my jsdom fork and verify that everything works as expected. Then I'll submit my pull request to jsdom and see how they respond. |
They have a pull request for it already fron node-canvas themselves. They prefer to wait for node-canvas to be out of alpha/beta. Also they do not seem really happy to expose the canvas proprietary methods like registerFont addFont or simply createPngStream. I asked them to expose at least the utility to get the node-canvas implementation of a HTMLCanvas instance but they prefer not |
jsdom/jsdom#2067 try to leave a comment here about using the font registering as i did. |
I spent quite a bit of time on this yesterday, and uncovered a very strange behavior. I forked jsdom to use node-canvas 2.0 alpha and forked fabric to use my jsdom fork. If I create a canvas (using fabric.util.createCanvas) and simply import registerFont from node canvas, the font renders correctly:
However, if I simply create a fabric StaticCanvas, passing the canvas I created in the constructor, then run the exact same code, the font doesn't render properly:
I'm not sure what it means... |
i agree it should not happen.
is not that important but we should make it possible to do so
…On 13 Dec 2017 6:42 pm, "dennisrjohn" ***@***.***> wrote:
I spent quite a bit of time on this yesterday, and uncovered a very
strange behavior.
I forked jsdom to use node-canvas 2.0 alpha and forked fabric to use my
jsdom fork.
If I create a canvas (using fabric.util.createCanvas) and simply import
registerFont from node canvas, the font renders correctly:
const { fabric } = require('./dist/fabric');
const { writeFileSync } = require('fs');
const path = require('path');
let {registerFont} = require('canvas');
let canvasElement = fabric.util.createCanvasElement();
canvasElement.width = 600;
canvasElement.height = 200;
function fontFile (name) {
return path.join(__dirname, name)
}
registerFont(fontFile('daniel.woff'), {
family:"Daniel"
})
let context = canvasElement.getContext('2d');
context.font = "90px \"Daniel\"";
context.fillText("Hello World",50,100);
writeFileSync('./testFont.png', canvasElement.toDataURL().replace('data:image/png;base64,', ""), 'base64');
Output:
[image: testfont]
<https://user-images.githubusercontent.com/8462346/33953425-23a4829a-dff2-11e7-86d5-02c648d3ba0a.png>
However, if I simply create a fabric StaticCanvas, passing the canvas I
created in the constructor, then run the exact same code, the font doesn't
render properly:
const { fabric } = require('./dist/fabric');
const { writeFileSync } = require('fs');
const path = require('path');
let {registerFont} = require('canvas');
let canvasElement = fabric.util.createCanvasElement();
canvasElement.width = 600;
canvasElement.height = 200;
const canvas = new fabric.StaticCanvas(canvasElement, { width: 600, height: 200 });
function fontFile (name) {
return path.join(__dirname, name)
}
registerFont(fontFile('daniel.woff'), {
family:"Daniel"
})
let context = canvasElement.getContext('2d');
context.font = "90px \"Daniel\"";
context.fillText("Hello World",50,100);
writeFileSync('./testFont.png', canvasElement.toDataURL().replace('data:image/png;base64,', ""), 'base64');
Output:
[image: testfont]
<https://user-images.githubusercontent.com/8462346/33953451-38dca89a-dff2-11e7-880a-2339c8458ced.png>
I'm not sure what it means...
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#4541 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABI4QNqbFGEL3v0JTGO6jbiMqxajSmLdks5tAAyegaJpZM4Q5sCN>
.
|
In my jsdom/node-canvas2/fabric branches, the registered font doesn't work if I used an iText. I also tried passing registerFont from jsdom to fabric and it also didn't work. I think the above issue could be the cause of registerFont not working at all with node-canvas2 in fabric2.0. |
verify that for some reason you have objectCaching enabled. set it to false
on the Object.prototype.
2017-12-13 20:20 GMT+01:00 dennisrjohn <[email protected]>:
… In my jsdom/node-canvas2/fabric branches, the registered font doesn't work
if I used an iText. I also tried passing registerFont from jsdom to fabric
and it also didn't work. I think the above issue could be the cause of
registerFont not working at all with node-canvas2 in fabric2.0.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#4541 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABI4QG8XXAGSDsuAwCXkOOtN22In246oks5tACObgaJpZM4Q5sCN>
.
|
I set objectCaching to false on both the Canvas and the Text object:
|
Hi,
It seems like
createCanvasForNode
is deprecated. I'm using new fabric.Canvas instead. However, I can't figure out how to add a custom font and create a PNGStream in this setup. I searched the docs but wasn't able to find a solution.TypeError: canvas.Font is not a constructor
TypeError: canvas.createPNGStream is undefined
Would you mind helping me out?
(Note that my code worked with a previous version of fabricjs, but I need to use the new version for this project)
The text was updated successfully, but these errors were encountered: