PDF: move fix_text_encoding() logic to new method for common use in FPDF and TCPDF (#61, #8312).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5713 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2011-05-09 06:44:23 +00:00
parent 0681d086e2
commit 32a52a6540
1 changed files with 31 additions and 21 deletions

View File

@ -87,6 +87,9 @@ module Redmine
def initialize(lang) def initialize(lang)
super() super()
if RUBY_VERSION < '1.9'
@ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
end
set_language_if_valid lang set_language_if_valid lang
case l(:general_pdf_encoding).upcase case l(:general_pdf_encoding).upcase
when 'CP949' when 'CP949'
@ -143,27 +146,7 @@ module Redmine
end end
def fix_text_encoding(txt) def fix_text_encoding(txt)
txt ||= '' RDMPdfEncoding::rdm_pdf_iconv(@ic, 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
def RDMCell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='') def RDMCell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
@ -465,6 +448,33 @@ module Redmine
end end
pdf.Output pdf.Output
end 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 end
end end