pdfme / pdfme

A TypeScript based PDF generator library, made with React.
https://pdfme.com
MIT License
2.19k stars 194 forks source link

Dynamic font sizing needs to consider the height of the containing box #209

Closed peteward closed 9 months ago

peteward commented 10 months ago

Describe the bug

The current implementation scales font based on the length of the longest line. This means that if you create a large text box and expect the content to span multiple lines, it will shrink the text down to a minimum to fit on the top line only.

To Reproduce

Expected behavior

It needs to consider scaling based on the full available height of potentially multiple lines from the defined text box.

A good example of how it should behave is shown in the google docs gif from issue #169.

Your Environment

- pdfme package(@pdfme/generator or @pdfme/ui): both
- pdfme version: 2.0.0

Your Error Log

-

Additional context

We are working on a fix/improvement to this.

peteward commented 10 months ago

NOTE: We will retain the overflow behaviour as it currently is for when you exceed the available box space even after shrinking text to the minimum size as per #169

hand-dot commented 10 months ago

Hey @peteward , is this issue same with https://github.com/pdfme/pdfme/issues/169?

if you already handle same problem, i will close https://github.com/pdfme/pdfme/issues/169.

peteward commented 10 months ago

Hi @hand-dot,

No, this is not the same issue.

Currently when you apply dynamic font sizing it starts to reduce the font size based on the first/longest line, it does not consider the full height of the box. In the scenario below it should only start reducing the font size once it starts to over flow the box: Screenshot 2023-08-02 at 09 10 47

Similarly, it should grow to fill the box up to the maximum font size.

Dynamic font size, if used, will reduce the chance of overflow but it can still happen and I'm not sure what the best course of action is when it does. There are arguments for both truncating/clipping and allowing the overflow in this scenario. I would probably keep the existing behaviour of overflow.

hand-dot commented 10 months ago

Hey @peteward ah, got it. this is not same with https://github.com/pdfme/pdfme/issues/169

I want to ensure that an overflow does not occur in the above issue.

The method I'm planning to use is to reduce the font size even if the text overflows vertically from the bounding box. By controlling the height of the font, we can definitely prevent overflow by reducing the font size until it becomes 0.

If you have any concerns, please let me know.

peteward commented 10 months ago

Yes, I think this is a good approach 👍

peteward commented 9 months ago

Update on progress:

https://github.com/pdfme/pdfme/assets/7068515/3cb898f6-b0d7-45e0-b76a-b4435d8c2443

A few static examples.

Current pdfme playground, where dynamic font sizing does not 'grow' to fit the available box space because it does not consider height: Screenshot 2023-08-09 at 09 29 11

New version where both height and line length are considered:

Screenshot 2023-08-09 at 09 30 02

Rendered PDF:

Screenshot 2023-08-09 at 09 30 23

hand-dot commented 9 months ago

Closed by https://github.com/pdfme/pdfme/pull/222