Accurate Width Measurement After Layout Processing in OpenPDF 1.3.43(or any java8 supported versions)
Description:
I am using OpenPDF 1.3.43 in my library and need to accurately measure the width of a phrase. However, when LayoutProcessor.enable() is used, certain glyphs in some languages are processed and merged into proper characters.
Methods such as ColumnText.getWidth() and Chunk.getWidthPoint() return the width of the unprocessed text, which includes separate glyphs before they are merged. After LayoutProcessor processes the text, multiple glyphs are combined, reducing the actual width. However, I have not found a way to retrieve the width of the processed text after these transformations.
Example Code to Reproduce:
FontSelector fs = new FontSelector();
fs.addFont(englishFont);
fs.addFont(hindiFont);
fs.addFont(...);
Phrase phrase = fs.process("श्रीहर्ष");
float widthBeforeProcessing = ColumnText.getWidth(phrase); // Approx. 35f for font size 10
System.out.println("Width before LayoutProcessor: " + widthBeforeProcessing);
Output without enabling LayoutProcessor:
Output when LayoutProcessor is enabled :
Glyphs are correctly merged, reducing the actual width
The challenge: Finding the actual width after glyph merging
Expected Behavior:
A method to accurately compute the width of text after LayoutProcessor has been applied, reflecting the actual rendered width.
Actual Behavior:
The available methods return the width of the unprocessed text, which does not account for glyph merging performed by LayoutProcessor.
Use Case:
I need to compute text widths in real-time for generating millions of PDFs efficiently. A highly optimized and accurate solution is crucial.
Request:
- Is there an existing method to retrieve the correct width after
LayoutProcessorprocessing? - If not, are there any recommended alternatives or workarounds?
- Any insights or guidance on optimizing this calculation would be greatly appreciated.
Thank you!
@sriharsha9598, you should be able to get the width, height etc. from
java.awt.font.GlyphVector that is returned from calling the static method
LayoutProcessor.computeGlyphVector(BaseFont baseFont, float fontSize, String text)
Hi @vk-github18 ,
Thank you for suggesting the solution. I’ve implemented it, but I’m noticing that it takes quite a bit of time to process — on average, it’s taking up to 1ms per string (approximately 20–25 characters).
When working with thousands of records in real-time, this adds up and impacts performance. Could you please suggest if there’s any way to optimize this further?
Appreciate your support!
Hi @sriharsha9598 , sorry I do not know special optimizations, and I don't know which methods are costly.
ok, thank you
The URL in the last comment leads to an ERR_SSL_PROTOCOL_ERROR, possibly SPAM -- this comment is now deleted
A solution has been described in https://github.com/LibrePDF/OpenPDF/issues/1281#issuecomment-2777102697
Please submit pull requests to improve this.