]> source.dussan.org Git - redmine.git/commitdiff
PDF: move fix_text_encoding() logic to new method for common use in FPDF and TCPDF...
authorToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Mon, 9 May 2011 06:44:23 +0000 (06:44 +0000)
committerToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Mon, 9 May 2011 06:44:23 +0000 (06:44 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5713 e93f8b46-1217-0410-a6f0-8f06a7374b81

lib/redmine/export/pdf.rb

index a7a00cfa38c725746f396832566c6ff8aa6535a6..2ca6deb5cc5e61fca3311abb6d1406495da773e6 100644 (file)
@@ -87,6 +87,9 @@ module Redmine
 
         def initialize(lang)
           super()
+          if RUBY_VERSION < '1.9'
+            @ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
+          end
           set_language_if_valid lang
           case l(:general_pdf_encoding).upcase
           when 'CP949'
@@ -143,27 +146,7 @@ module Redmine
         end
 
         def fix_text_encoding(txt)
-          txt ||= ''
-          if txt.respond_to?(:force_encoding)
-            txt.force_encoding('UTF-8')
-            txt = txt.encode(l(:general_pdf_encoding), :invalid => :replace,
-                              :undef => :replace, :replace => '?')
-            txt.force_encoding('ASCII-8BIT')
-          else
-            @ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
-            txtar = ""
-            begin
-              txtar += @ic.iconv(txt)
-            rescue Iconv::IllegalSequence
-              txtar += $!.success
-              txt = '?' + $!.failed[1,$!.failed.length]
-              retry
-            rescue
-              txtar += $!.success
-            end
-            txt = txtar
-          end
-          txt
+          RDMPdfEncoding::rdm_pdf_iconv(@ic, txt)
         end
 
         def RDMCell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
@@ -465,6 +448,33 @@ module Redmine
         end
         pdf.Output
       end
+
+      class RDMPdfEncoding
+        include Redmine::I18n
+        def self.rdm_pdf_iconv(ic, txt)
+          txt ||= ''
+          if txt.respond_to?(:force_encoding)
+            txt.force_encoding('UTF-8')
+            txt = txt.encode(l(:general_pdf_encoding), :invalid => :replace,
+                              :undef => :replace, :replace => '?')
+            txt.force_encoding('ASCII-8BIT')
+          else
+            ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
+            txtar = ""
+            begin
+              txtar += ic.iconv(txt)
+            rescue Iconv::IllegalSequence
+              txtar += $!.success
+              txt = '?' + $!.failed[1,$!.failed.length]
+              retry
+            rescue
+              txtar += $!.success
+            end
+            txt = txtar
+          end
+          txt
+        end
+      end
     end
   end
 end