Repository navigation
parse a TrueType font from a ByteBuffer without copying - #538
peteraisher wants to merge 1 commit into
Conversation
… don't copy glyf twice TTFParser.parse(ByteBuffer) reads the font in place, so many fonts can be parsed from one buffer, each with its own position. GlyphTable now keeps a view of its part of the font data instead of copying it twice. Generated-by: Claude Code (Claude Opus 5.5)
|
I'm happy to open a separate PR against 3.0 if that's helpful. The patch was tested against trunk and 3.0. |
|
How does this affect / improve the rest of pdfbox / is there a change that we should do in the main pdfbox? Or is this something that you need for your own project? |
|
In my opinion it's both. We will use this patch anyway — sharing here since it seems likely to benefit others. TL;DR: Previous options:
New option:
Right now, people using pdfbox across multiple threads (us included) have to choose between resource duplication or access serialisation if they want a strong guarantee against data corruption when multiple threads use a single font. To get an independent seek state currently requires copying the underlying Putting this change on main makes that choice between safety guarantee and resource sharing go away. |
parse a TrueType font from a
ByteBufferwithout copying itWhat
Adds
TTFParser.parse(ByteBuffer), which reads the font in place. Each font receives its own slice of the buffer, so multiple fonts can be parsed from one buffer and used in different threads.GlyphTablenow has a view of its part of the font data. Before, it copiedglyftwice. Streams that aren't in memory (e.g. a TTC over a file) still copy, but only once.RandomAccessReadDataStreamholds aByteBufferinstead of abyte[]and anintposition. Behaviour is otherwise unchanged.Arial Unicode MS (23.3 MB, glyf 22.4 MB), memory allocated per call:
parse(RandomAccessRead)parse(ByteBuffer)PDType0Font.load+ one page savedWhy
Reduce memory usage while enabling safe resource sharing across threads.
With a cheap parse, each document can have its own font instead of sharing one (PDFBOX-4220, PDFBOX-5779).
Safety
Tests:
TestTTFParser.testParseByteBufferandRandomAccessReadBufferDataStreamTest.testCreateSubStream. The fontbox and pdfbox module tests pass on 3.0 and trunk.LLM usage disclaimer
Claude Opus 5.5 was used during development. The code submitted represents standard usage of the
ByteBufferinterface. I would expect (and hope) that any code making use of this interface, proprietary or otherwise, would look similar, and am confident that this doesn't present a copyright risk.