-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Open
Labels
Description
Hi, I'm following up on scaling issues revealed in this fiddle (text is strangely spaced when using default jsPDF units):
https://jsfiddle.net/eKoopmans/egm94jqh/
This comes from writing each word separately, and specifying the x/y coordinates of each. When using an HTML canvas, units are expected to always be in pixels (e.g. fillText). However the jsPDF Context2d gets pretty confused with units:
- (I've only focused on text methods so far)
.fontsets the PDF font size to the same numeric value, regardless of what units were specified (problem - should be scaled based on specified units (e.g.12px) and document units; related problem - the regex doesn't correctly identify the units).fillTextreceives x and y as pixels- it adjusts the y value using
getBaseline, which uses the current font size scaled based on the document units (problem - should be scaled a second time into pixels to match y) - x and y are passed on to
.putTextas still mostly pixels .putTextmodifies x and y based on canvas transformations (fine...?), then sends toAPI.text(problem - these should be scaled from pixels to document unit before sending)API.textexpects x and y in document units (not pixels), and scales everything topt- since
ptis the default unit (scaleFactor = 1), it's the only unit that ends up looking correct
So, there's 3-4 problems so far, but I think a larger audit is necessary of all the assumptions being made about units in Context2d. It seems to me the best approach is to keep everything internally as pixels within Context2d, and make sure to adjust correctly whenever interacting with methods external to Context2d.
benelliott