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

PdfBoxGraphics2DFontTextDrawer.getFontMetrics always floors the width #35

Closed
MarkusDLN opened this issue Apr 8, 2022 · 3 comments
Closed

Comments

@MarkusDLN
Copy link

I ran into an issue with the FontMetrics provided by the PdfBoxGraphics2DFontTextDrawer when using it for the method SwingUtilities.layoutCompoundLabel. The method tries to shorten the given text if necessary and add three dots at the end (...) to indicate that the original string was longer. To do this the method goes character by character until the available width is reached.
The issue now is that the FontMetrics always floors the width so that the sum of the width of the individual characters will always be (significantly) lower than the actual width of the string represented by these characters (for non monospaced fonts).
I think this should be rounded (like sun.font.FontDesignMetrics) by adding 0.5f before casting to an int.

@MarkusDLN MarkusDLN changed the title PdfBoxGraphics2DFontTextDrawer.getFontMetrics always floors the size PdfBoxGraphics2DFontTextDrawer.getFontMetrics always floors the width Apr 8, 2022
rototor added a commit that referenced this issue Apr 10, 2022
@rototor
Copy link
Owner

rototor commented Apr 12, 2022

I made a simple test case for this. As documented on the stringWidth() method, this is always off. I.e. the sum of all single char widths will never be the width of the complete string.

This is how it looks like without the rounding like FontDesignMetrics:
image

And this is how it looks like with the rounding:
image

You can see that the first case underestimates the string width, the second ("fixed") version overestimates the stringWidth.

So relying on this is broken in any way. But with this "fix" it is now broken the same way as in the JDK. So its now more consistent.

@rototor
Copy link
Owner

rototor commented Apr 12, 2022

I just release version 0.37 with this fix.

@rototor rototor closed this as completed Apr 12, 2022
@MarkusDLN
Copy link
Author

Thank you for your quick response and the new release. Much appreciated.

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

No branches or pull requests

2 participants