parallax / jsPDF

Client-side JavaScript PDF generation for everyone.
https://parall.ax/products/jspdf
MIT License
29.39k stars 4.68k forks source link

[bug] splitTextToSize() wrong calculations with spaceless string #3644

Open mmghv opened 1 year ago

mmghv commented 1 year ago

When splitting a long text that has no spaces using splitTextToSize() on a predefined width, it splits at the wrong length causing the text to get out of bounds.

image

The issue gets worse the more width the rectangle has (with longer text)

Exaggerated example :

image

jspdf v2.5.1 codepen : https://codepen.io/mmghv/pen/eYbjRdX?editors=0011

Jai-Marothiya commented 1 year ago

Hi Mohamed, I am really interested in solving this issue, could you please assign this to me?

Thanks

mmghv commented 1 year ago

Sorry guys, I'm not associated with jspdf, just reporting an issue.

github-actions[bot] commented 10 months ago

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

mmghv commented 10 months ago

Still relevant.

a6younes commented 10 months ago

Hello,

I want to resolve this number, please assign me

GiannyPontat commented 10 months ago

Hello, I've tried to work on this issue by adding a '-' character for word splitting in the splitLongWord() function :

var splitLongWord = function(word, widths_array, firstLineMaxLen, maxLen) {
    var answer = [];
    var hyphen = '-'; // Hyphen character for word splitting
    // Calculate the width of a hyphen using the current font and size
    var hyphenWidth = this.getStringUnitWidth(hyphen, { font: this.getFont().fontName, fontSize: this.internal.getFontSize() });

    // Chop the word to fit the remaining space on the line
    var i = 0,
        l = word.length,
        workingLen = 0;
    while (i < l && workingLen + widths_array[i] < firstLineMaxLen) {
      workingLen += widths_array[i];
      i++;
    }

    // If the word is being split and there's room, add a hyphen
    if (i < l && workingLen + hyphenWidth <= firstLineMaxLen) {
      answer.push(word.slice(0, i) + hyphen);
    } else {
      answer.push(word.slice(0, i));
    }

    // Split the rest of the word to fit the max line length
    var startOfLine = i;
    workingLen = 0;
    while (i < l) {
      // When exceeding max length, split and add to the answer
      if (workingLen + widths_array[i] > maxLen) {
        answer.push(word.slice(startOfLine, i));
        startOfLine = i;
        workingLen = 0;
      }
      workingLen += widths_array[i];
      i++;
    }

    // Add the last piece if there's any
    if (startOfLine < l) {
      answer.push(word.slice(startOfLine));
    }

    return answer;
  };

What do you think ?

github-actions[bot] commented 7 months ago

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

mmghv commented 7 months ago

Still relevant.

github-actions[bot] commented 4 months ago

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

mmghv commented 4 months ago

Still relevant.

sanjogbhalla16 commented 1 month ago

Hi is the issue is still open , I would love to contribute in this .