Skip to content

Bug: Context2d issues with units #2294

Open
@eKoopmans

Description

@eKoopmans

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)
  • .font sets 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)
  • .fillText receives 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 .putText as still mostly pixels
  • .putText modifies x and y based on canvas transformations (fine...?), then sends to API.text (problem - these should be scaled from pixels to document unit before sending)
  • API.text expects x and y in document units (not pixels), and scales everything to pt
  • since pt is 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions