jung-kurt / gofpdf

A PDF document generator with high level support for text, drawing and images
http://godoc.org/github.com/jung-kurt/gofpdf
MIT License
4.29k stars 772 forks source link

Support for font kerning #297

Open beta opened 4 years ago

beta commented 4 years ago

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?

jung-kurt commented 4 years ago

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.

beta commented 4 years ago

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.

jung-kurt commented 4 years ago

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

phpdave11 commented 4 years ago

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".