public
Description: Fast, Nimble PDF Writer for Ruby
Homepage: http://prawn.majesticseacreature.com
Clone URL: git://github.com/sandal/prawn.git
Move the text encoding checks to a seperate private function
- Document#text() is getting long and scary.
James Healy (author)
Wed Jul 23 20:59:03 -0700 2008
sandal (committer)
Thu Jul 24 09:05:40 -0700 2008
commit  6c4bdf74dc677df5f88a77f4dc214feb744af7e2
tree    e3e2c6ba3fb5f1497a2030db3480438f7beb36fa
parent  d335b2a86ebefb68eb7effe6401491fd894e33f8
...
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
...
277
278
279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
281
282
...
33
34
35
 
 
36
37
38
39
40
41
 
42
43
 
 
 
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
46
47
48
49
...
254
255
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
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
@@ -277,6 +254,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