Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: breakTextIntoLines removes empty lines in multiline text #1590

Open
2 tasks done
AmilKey opened this issue Jan 22, 2024 · 0 comments
Open
2 tasks done

BUG: breakTextIntoLines removes empty lines in multiline text #1590

AmilKey opened this issue Jan 22, 2024 · 0 comments

Comments

@AmilKey
Copy link

AmilKey commented Jan 22, 2024

What were you trying to do?

draw multiline text with empty line and maxWidth param

How did you attempt to do it?

function page.drawText have different result when pass maxWidth param and text with empty line for example:

  page.drawText(
    `first\n` +
    `\n` +
    `second\n`
    ,
    {
      x: 25,
      y: 100,
      font,
      size: 24,
      color: rgb(1, 0, 0),
      lineHeight: 24,
      opacity: 0.75,
      maxWidth: 500,
    },
  )

result :

image

call page.drawText without maxWidth

  page.drawText(
    `first\n` +
    `\n` +
    `second\n`
    ,
    {
      x: 25,
      y: 100,
      font,
      size: 24,
      color: rgb(1, 0, 0),
      lineHeight: 24,
      opacity: 0.75,
    },
  )

result:

image

as you can see, without maxWidth parameter the empty line is also present

What actually happened?

empty line not rendered

What did you expect to happen?

few tests that should passed

https://github.com/Hopding/pdf-lib/blob/master/tests/utils/strings.spec.ts#L23

  it(`handles trailing newlines`, () => {
    const input = 'foo\n';
    const expected = ['foo', ''];
    const actual = breakTextIntoLines(input, [], 21, computeTextWidth);
    expect(actual).toEqual(expected);
  });
  
  it(`handles empty lines`, () => {
    const input = 'foo\n\nbar';
    const expected = ['foo', '', 'bar'];
    const actual = breakTextIntoLines(input, [], 21, computeTextWidth);
    expect(actual).toEqual(expected);
  });

How can we reproduce the issue?

see above

Version

1.17.1

What environment are you running pdf-lib in?

Browser

Checklist

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant