Automattic / node-canvas

Node canvas is a Cairo backed Canvas implementation for NodeJS.
10.11k stars 1.16k forks source link

Right-aligned text with leading/trailing spaces rendered incorrectly #333

Open kangax opened 10 years ago

kangax commented 10 years ago

Using a simple test like this:

ctx.font = '30px Arial';
ctx.textAlign = 'right';
ctx.strokeRect(100, 100, 100, 100);
ctx.fillText('   foobar', 100, 100);

... I'm getting incorrect rendering in latest node-canvas:

leading_spaces_nodecanvas

compare to the same output in Chrome (and FF):

leading_spaces_chrome

There's a similar problem with trailing spaces.

node-canvas:

trailing_spaces_nodecanvas

Chrome (FF):

trailing_spaces_chrome

konkissner commented 9 years ago

same for i-text elements with textAlign: center.

this is the canvas rendered in chrome:

bildschirmfoto 2015-09-04 um 15 02 35

this one is generated with node-canvas:

bildschirmfoto 2015-09-04 um 15 02 44

Any ideas or workarounds?

Joesmi commented 8 years ago

Hello, has anything happened on this front? Thanks for letting us know.

StephanBijzitter commented 8 years ago

+1 We also need a fix for this. @LinusU

duydodevgithub commented 5 years ago

Version

2.3.6

Steps to reproduce

We seem to have same problem with text position after saving to PNG. Attached is the design and the final PNG. Here is my environment information:

fabricissue

@asturur Any thoughts about this issue? Thank you so much !

asturur commented 5 years ago

when i have time to update to node-canvas 2, i ll swarm over those old fabricjs bugs.

Now we are on 1.6 and that is unsupported. This means that we can't really invest time in research and fixes.

akinnee commented 5 years ago

I'm running into an issue like this. Using canvas in the browser my text is centered. Running the same text rendering code but using node-canvas results in seemingly random text alignment.

browser canvas: screen shot 2019-01-18 at 11 25 47 am screen shot 2019-01-18 at 11 26 20 am screen shot 2019-01-18 at 11 26 41 am

node-canvas: screen shot 2019-01-18 at 11 28 02 am screen shot 2019-01-18 at 11 28 09 am screen shot 2019-01-18 at 11 28 17 am screen shot 2019-01-18 at 11 28 21 am screen shot 2019-01-18 at 11 28 35 am

And here's my function for rendering my lines of text:

function renderLines({ canvasContext, lines, x, y }) {
  const lineHeight = canvasContext.measureText('M').width * 1.2;
  const startY = y - (lines.length - 1) * lineHeight / 2;
  lines.forEach((line, index) => {
    const lineY = startY + index * lineHeight;
    canvasContext.fillText(line.trim(), x, lineY);
  });
}

I'm using node-canvas 2.3.1.

andreafalzetti commented 4 years ago

Same issue here. Node.js tex-align is off compared to the browser. Anyone found a solution?

asturur commented 4 years ago

I think we have some text align test in node and they work. What kind of font are you using? If you have a simple index.js that can print the text not correctly aligned, that woulds help. Maybe with a vertical line for reference where the alignemnt should be

aman-webdev commented 8 months ago

I am facing an issue with top alignment, on browser its okay but on node its adding extra space at the top , happening with all fonts

browser

image

node

image

Some extra space is coming at the top in node