-
Notifications
You must be signed in to change notification settings - Fork 22
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
gfx.getFontMetrics.stringWidth gives different width than pdFont.getStringWidth / 1000 * fontSize #16
Comments
Can you provide a small test case (preferred as a pull request) which demonstrate this inaccurate measurement? Is this inaccuracy depending on the used font or does it happen with all fonts? A correct fix would need to extend the IPdfBoxGraphics2DFontTextDrawer interface to also provide metric data for the fonts, and then hook the FontMetrics into that. As I'm leaving for holiday tomorrow I won't have time the next two weeks to look into this. But a test case which clearly shows the problem would help me. |
I added a small test case :) |
I'll look into implementing some form of 3rd party FontMetrics inside FontTextDrawer. It kind of feels like the whole thing (FontTextDrawer) could be hidden away inside the PdfBoxGraphics2D-class, which would also remove one moving piece (creating the FontTextDrawer, registering the fonts and then adding it to the Graphics2D-context). It feels a bit clumsy. WDYT? |
@megri The FontTextDrawer is there for a reason. It needs to be customizable for different use cases:
You can get bidi to work with PDFont's, you just have to somehow bridge the HarfBuzz functionality or use something like ICU. To do so you could derive from the FontTextDrawer and "just" implement that stuff correctly... (No, this is not easy and out of scope for this project) Also if you have more then one Graphics2D in your PDF you can reuse the FontTextDrawer and therefor reuse the PDFont's. You may even want to bridge your own font management stuff (like https://github.com/danfickle/openhtmltopdf does). Beside this, it's still a bug that pdFont.getStringWidth() and the getFontMetrics().stringWidth() don't match. But when when implementing this you must have the three different use cases in mind. |
This is only possible if there is any PDFont. This leads to more exact string widths. Only the string widths get more accurate with this method. If you try to do any kind of layout beside advancing the text position you should really consider using TextLayout (which will *always* lead to vector glyphs embedded).
I've released a new version with a fix for this. I.e. if you have registered a font then the font metrics of the PDFont are used for the stringWidth(). If the font is not registered and the text is vectorized, then it behaves the old way, as that matches the resulting text graphic. |
To calculate the width of text using PDFBox you can use
pdFont.getStringWidth(theAlphabet) * 1000 / fontSize
. From my experiments this gives a precise measurement of the text's width.Using
pdfBoxGraphics2d.getFontMetrics(font).stringWidth(alphabet)
the width is different (and, from my experimentations, incorrect)The text was updated successfully, but these errors were encountered: