public
Fork of sandal/prawn
Description: Fast, Nimble PDF Writer for Ruby
Homepage: http://prawn.lighthouseapp.com/projects/9398/home
Clone URL: git://github.com/yob/prawn.git
Search Repo:
Move the text encoding checks to a seperate private function
- Document#text() is getting long and scary.
yob (author)
Wed Jul 23 20:59:03 -0700 2008
commit  9cfd4d1488d592f4fd97ac9d1e438fc7466c0c22
tree    2c3b0838719f3e917b6d15371366e8e9c53d9972
parent  c01eab94b60033009207d63fbc7700b0584d8206
...
33
34
35
36
37
 
 
38
39
40
41
42
 
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
 
70
71
72
...
279
280
281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
283
284
...
33
34
35
 
 
36
37
38
39
40
41
 
42
43
 
 
 
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
46
47
48
49
...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
0
@@ -33,40 +33,17 @@ module Prawn
0
       # pdf.text "Goodbye World", :at => [50,50], :size => 16
0
       # pdf.text "Will be wrapped when it hits the edge of your bounding box"
0
       #
0
- # All strings passed to this function should be encoded as UTF-8.
0
- # If you gets unexpected characters appearing in your rendered
0
+ # All strings passed to this function should be encoded as UTF-8.
0
+ # If you get unexpected characters appearing in your rendered
0
       # document, check this.
0
       #
0
       # If an empty box is rendered to your PDF instead of the character you
0
       # wanted it usually means the current font doesn't include that character.
0
- #
0
+ #
0
       def text(text,options={})
0
- # TODO: if the current font is a built in one, we can't use the utf-8
0
- # string provided by the user. We should convert it to WinAnsi or
0
- # MacRoman or some such.
0
 
0
- if text.respond_to?(:"encode!")
0
- # if we're running under a M17n aware VM, ensure the string provided is
0
- # UTF-8 (by converting it if necessary)
0
- begin
0
- text.encode!("UTF-8")
0
- rescue
0
- raise Prawn::Errors::IncompatibleStringEncoding, "Encoding " +
0
- "#{text.encoding} can not be transparently converted to UTF-8. " +
0
- "Please ensure the encoding of the string you are attempting " +
0
- "to use is set correctly"
0
- end
0
- else
0
- # on a non M17N aware VM, use unpack as a hackish way to verify the
0
- # string is valid utf-8. I thought it was better than loading iconv
0
- # though.
0
- begin
0
- text.unpack("U*")
0
- rescue
0
- raise Prawn::Errors::IncompatibleStringEncoding, "The string you " +
0
- "are attempting to render is not encoded in valid UTF-8."
0
- end
0
- end
0
+ # check the string is encoded sanely
0
+ normalize_encoding(text)
0
 
0
         if options.key?(:kerning)
0
           options[:kerning] = false unless font_metrics.has_kerning_data?
0
@@ -279,6 +256,34 @@ module Prawn
0
         return basename
0
       end
0
 
0
+ def normalise_encoding(text)
0
+ # TODO: if the current font is a built in one, we can't use the utf-8
0
+ # string provided by the user. We should convert it to WinAnsi or
0
+ # MacRoman or some such.
0
+ if text.respond_to?(:"encode!")
0
+ # if we're running under a M17n aware VM, ensure the string provided is
0
+ # UTF-8 (by converting it if necessary)
0
+ begin
0
+ text.encode!("UTF-8")
0
+ rescue
0
+ raise Prawn::Errors::IncompatibleStringEncoding, "Encoding " +
0
+ "#{text.encoding} can not be transparently converted to UTF-8. " +
0
+ "Please ensure the encoding of the string you are attempting " +
0
+ "to use is set correctly"
0
+ end
0
+ else
0
+ # on a non M17N aware VM, use unpack as a hackish way to verify the
0
+ # string is valid utf-8. I thought it was better than loading iconv
0
+ # though.
0
+ begin
0
+ text.unpack("U*")
0
+ rescue
0
+ raise Prawn::Errors::IncompatibleStringEncoding, "The string you " +
0
+ "are attempting to render is not encoded in valid UTF-8."
0
+ end
0
+ end
0
+ end
0
+
0
       def register_builtin_font(name) #:nodoc:
0
         unless BUILT_INS.include?(name)
0
           raise Prawn::Errors::UnknownFont, "#{name} is not a known font."

Comments

  • sandal Thu Jul 24 09:05:07 -0700 2008

    Good call. I was planning on doing this. I’ll pull it up.