Ruby PDF::Writer doing text-transformations and font kerning
Some time ago I wrote about the PDF::Writer changes to be able to use font embedding properly. I got one inquiry about this, which is cool. I haven’t yet had time to test the PDFs in Mac OSX (I don’t have Mac myself, only two of my colleagues).
However shortly after writing about the font problem and solution for it I needed some text-transformations and they appeared to be really troublesome. Ie. rotate, stretch, skew text or all at the same time. Especially with the in-place transformations got from SVG files. Same for embedded images.
This led me again back to the PDF specifications and I found solution about transformations for the text block itself. PDF::Writer did not support this except for the fixed angle parameter. What I did I extended the angle parameter also to accept 6 element transformation array. Similar transformation array is accepted by the add_image methods.
After I had “fixed” that the text still did not look correct. Adobe Illustrator was still showing the texts to be have less width. Just by accident I noticed the kerning information in Illustrator and I figured PDF::Writer is not really adding kerning to the text for some reason. And it turned out atleast Acrobat does not view text automatically with kerning even if the kerning data is embedded in the font data.
Once again the PDF specifications were the saviour. Solution was to pre-process the text strings into array strings with the kerning data in them prior writing the PDF data block (so modifications on the add_text were needed). Rather simple string processing (although I admit the code doing it might be a bit poorly written as-is) after fixing bug from fontmetrics.rb to get proper kerning data.
That’s about it. Here’s are the changed and added methods:
PDF::Writer.add_text(x, y, text, size = nil, angle = 0, word_space_adjust = 0, char_space_adjust = 0, text_kerning = 1)
I added support for char_space_adjust and text_kerning. Angle can also be transformation matrix which is in-place relative transformation only to the text’s x, y and own size. No more need to “manually” correct x, y for add_text if you just want to rotate the text.
PDF::Writer.kern(text)
Returns PDF string array with text kerning information of the current font. Mostly useful only internally by the add_text. Note that this requires fixes in the fontmetrics.rb (fixes in the package). In short pdf-writer 1.1.3 just has few bugs in the fontmetrics.rb preventing kerning information being loaded properly.
PDF::Writer.add_image(image, x, y, width = nil, height = nil, image_info = nil, link = nil, matrix = nil)
PDF::Writer.add_image_from_file(image, x, y, width = nil, height = nil, link = nil, matrix = nil)
Basically I added same functionality as with add_text in-place transformation matrix. However there’s small catch in this implementation about the corner of the x, y position when adding image. In 1.1.3 the x, y is the position of the bottom, left corner, but in SVG transformations the corner is top, left. It was easier for me to do it this way for the purpose I needed this change.
This time I provide full package of the PDF::Writer. It’s basically 1.1.3 with the changes I made. Most likely the demos won’t work, but I don’t want to bother myself with that
Download: PDF::Writer 1.1.4
February 22nd, 2007 at 1:50
Does 1.1.4 also include your previous patch pdf-writer 1.1.3 for Font Embedding?
February 22nd, 2007 at 1:55
Yes it does.
December 19th, 2008 at 4:02
[…] BulkDump » Blog Archive » Ruby PDF::Writer doing text-transformations and font kerning (tags: ruby pdf text) […]