parallax / parallax/jsPDF

charSpace and maxWidth in doc.text() : charSpace > 0 and maxWidth is not respected

Open
#3,299 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug difficulty:medium hacktoberfest help wanted
Dominant language
JavaScript
Stars
31.3k
Forks
4.8k
PR merge metrics
No merged PRs in 30d

Description

I have read and understood the contribution guidelines.

Basically, charSpace and maxWidth don't play nicely together.

Code tested using your live demo at http://raw.githack.com/MrRio/jsPDF/master/

The following works fine ...

var doc = new jsPDF();
doc.setFontSize(22);
doc.text("This is a title", 20, 20);

const maxWidth = 100;
const charSpace = 0;

// Drawing a box to show maxWidth clearly
doc.rect(20, 20, maxWidth, 100, 'S')

// Using 'justify' to show things clearly but also happens with 'left'
const options = {
    maxWidth: maxWidth,
    charSpace: charSpace,
    align: 'justify'
}
const str = "Mary had a little lamb she also had a duck. She put them on the mantlepiece to see if they would fall off."

doc.setFontSize(16);
doc.text(str, 20, 30, options);

However, if you change charSpace to 1 then maxWidth is not correctly applied and then text extends beyond the maxWidth boundary.

Screen Shot 2021-10-16 at 07 00 24

I can then adjust maxWidth to try and compensate for this, but the formula is not leaping off the page at me!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example in the jsPDF live demo, focusing on doc.text() with maxWidth, charSpace, and justify alignment. Trace the doc.text() implementation and verify that text with charSpace greater than zero remains within the requested maxWidth; add or update coverage if the relevant test location is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.