gofpdf icon indicating copy to clipboard operation
gofpdf copied to clipboard

Support for font kerning

Open beta opened this issue 5 years ago • 4 comments

It seems gofpdf does not support font kerning at the moment. For example, when outputting "Tourist" with Arial font:

Result of gofpdf: image

Expected: image

Note the difference of the spacing between "T" and "o". Is there any way to apply font kerning?

beta avatar Sep 04 '19 09:09 beta

Unfortunately, gofpdf (like FPDF from which it is derived) does not currently support font kerning. This feature would be valuable so I will tag this as an enhancement. I imagine a solution would involve a font-specific table that associates letter pairs with spacing amount. Thanks for your post.

jung-kurt avatar Sep 04 '19 15:09 jung-kurt

I had some experiments and below is what I've got with gofpdf and my simple implementation of font kerning.

image

It's not completely implemented (only applied when using AddUTF8Font() and Text()), but it can meet my need now. I will continue to work on this when I have time.

beta avatar Sep 05 '19 12:09 beta

Nice work, @beta! If your solution if generally applicable, please consider sharing.

jung-kurt avatar Sep 05 '19 12:09 jung-kurt

I have added this feature to another PDF generator library in the past, so I may be able to provide some insight. Basically, you just need to parse the kern move amount for each letter pair from the font file. Then, when writing text, if there is a kern move amount defined for a letter pair, you embed that amount in between parentheses.

For example, within a TJ operation (show one or more text strings), if the kern move amount is 120 between the letters "T" and "o", then the PDF content would be: [(T)120(ourist)] TJ

More information is in the PDF specification under "Text-Showing Operators".

phpdave11 avatar Sep 17 '19 19:09 phpdave11