prawnpdf/prawn — pull request #390 by alexdowad (2012-08-19 11:08:55+00:00)
Title: Encoding compatibility cleanup (including removal of unnecessary String packing/unpacking on Ruby 1.9)

This all started when I was studying Prawn's text wrapping code and found some seemingly-redundant code for iterating over a String one UTF-8 character at a time. It turned out to be something which was done purely for compatibility with Ruby 1.8. Investigation revealed several similar places in the codebase.

I've tried to concentrate this String encoding-related compatibility stuff in compatibility.rb. With this patch, packing and unpacking of Strings to get a sequence of UTF-8 characters is only done under Ruby 1.8; under 1.9, it will just use the built-in methods for dealing with encodings.

The specs pass under 1.8.7, 1.9.2, and 1.9.3. I couldn't run the specs under 1.8.6, because the Rakefile requires Bundler, and Bundler requires Ruby >= 1.8.7. Upon discovering this, I took the liberty of removing a little code which was there only for < 1.8.6 compatibility. Now that compatibility with Ruby 1.8.6 has been dropped, the header comment for prawn/compatibility.rb no longer made sense, so I rewrote it.

When running the specs under different implementations, I discovered one test was failing under Ruby 1.9.2 (but not 1.9.3). What it came down to was: Font::TTF#encode_text was returning text encoded as ASCII-8BIT under 1.9.3, but as US-ASCII under 1.9.2. I think that US-ASCII strings are supposed to be upgraded to ASCII-8BIT if non-ASCII chars (ie. chars with MSB set) are added to the string, but Ruby 1.9.2 isn't doing that for some reason. It may be a platform bug.

I modified the spec in question so it passes regardless of the String encoding which is returned by Font#encode_text.