Hello everybody,
I am currently fighting with a problem that is quite common:
I have an application where users enter text. Then the entered text should appear in a PDF document. This seems easy enough, but becomes hairy when the entered text in the PDF must be limited in width.
Normally, such applications use a PDF library with appropriate APIs to generate the PDF, and to query the horizontal space a certain string will take in the generated PDF (before that PDF is generated at all).
In my case, the PDF generator is typst. This is the workflow:
The users enter their data in the application. The application dynamically generates a typst file by inserting the entered data into a typst file that acts as a template. The application then executes typst to compile the resulting file.
This works very well, except that I don’t have any idea how to reject a user’s input if it would take too much horizontal space in the final PDF. I have worked out two approaches to this problem which both are not appropriate. I describe them nevertheless to show where the hard problems are.
- My first idea was to simply test how many “W” characters could appear in the PDF within the given space, and limit the number of characters the user can enter in the application to that number.
However, the application must accept nearly every script, including CJK, Arabic, and so on. It didn’t take too long to find out that in foreign scripts there are single characters like the Arabic basmala that take 12 or 18 times the width of a normal character.
This means that it is impossible to estimate the horizontal extent of the entered text from the number of characters.
- The next idea was much better, but is still so far from being a correct solution that I barely can accept it. But here we go:
It is relatively easy possible to get the width of each glyph (character, code point) from a font file (e.g., .ttf). Since I know each used font, I could build a lookup table that maps code points (glyphs, characters) to that code point’s width. This table would be static (as long as the same fonts would be used), so I would need to build it only once.
The application could then get the width for each entered character, add these widths, and based on the result decide whether or not the entered text would take too much horizontal space in the final PDF.
However, there is one glitch and one massive problem:
The glitch is that no font file contains all possible glyphs. Rather, a font with a wide coverage, such as Google Noto families, is split over dozens of files, where each file usually contains the glyphs for a certain script (group). It’s up to the PDF engine (in this case, typst) to choose which font file to take a glyph from.
I know that I can tell typst in detail how to behave in this respect (without that feature, this approach would be impossible at all). But even then, a lot of work and a lot of discipline is required when constructing the lookup table. To get the width of a glyph for the table, the exact same font file must be used that typst takes that glyph from. Given the number of possible fallback mechanisms, this is error prone.
But it perhaps could work with a lot of effort and discipline applied (no font updates and no change in typst font order without re-creating the lookup table, etc.).
In contrast, the following problem seems impossible to solve for me:
When setting a PDF document, typst typically uses kerning and ligatures, and possibly there are some microtype-like features. Therefore, the actual width of a text fragment in a PDF that is generated by typst will almost never match the sum of the width of the single characters in that text fragment.
The only way to get around this would be to let typst actually set the text and to measure the text’s width afterwards. Of course, this would have to be done without setting the whole document (for performance reasons). I believe that this is currently not possible.
To be sure, I’d like to ask here:
Is it possible to get the width of a text in a typst document without rendering the whole document, and provide that width somehow to the outer world so that other applications can process it?
Many thanks in advance for any idea.