OpenPDF icon indicating copy to clipboard operation
OpenPDF copied to clipboard

Accurate Width Measurement After Layout Processing in OpenPDF 1.3.43(or any java8 supported versions)

Open sriharsha9598 opened this issue 10 months ago • 6 comments

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: Image

Output when LayoutProcessor is enabled : Image 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 LayoutProcessor processing?
  • If not, are there any recommended alternatives or workarounds?
  • Any insights or guidance on optimizing this calculation would be greatly appreciated.

Thank you!

sriharsha9598 avatar Apr 02 '25 09:04 sriharsha9598

@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)

vk-github18 avatar Apr 03 '25 22:04 vk-github18

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!

sriharsha9598 avatar Apr 07 '25 05:04 sriharsha9598

Hi @sriharsha9598 , sorry I do not know special optimizations, and I don't know which methods are costly.

vk-github18 avatar Apr 07 '25 19:04 vk-github18

ok, thank you

sriharsha9598 avatar Apr 08 '25 05:04 sriharsha9598

The URL in the last comment leads to an ERR_SSL_PROTOCOL_ERROR, possibly SPAM -- this comment is now deleted

vk-github18 avatar Apr 13 '25 15:04 vk-github18

A solution has been described in https://github.com/LibrePDF/OpenPDF/issues/1281#issuecomment-2777102697

Please submit pull requests to improve this.

andreasrosdal avatar Jul 06 '25 20:07 andreasrosdal