@@ -78,5 +78,38 module Redmine | |||
|
78 | 78 | end |
|
79 | 79 | str |
|
80 | 80 | end |
|
81 | ||
|
82 | def self.from_utf8(str, encoding) | |
|
83 | str ||= '' | |
|
84 | if str.respond_to?(:force_encoding) | |
|
85 | str.force_encoding('UTF-8') | |
|
86 | if encoding.upcase != 'UTF-8' | |
|
87 | str = str.encode(encoding, :invalid => :replace, | |
|
88 | :undef => :replace, :replace => '?') | |
|
89 | else | |
|
90 | str = self.replace_invalid_utf8(str) | |
|
91 | end | |
|
92 | elsif RUBY_PLATFORM == 'java' | |
|
93 | begin | |
|
94 | ic = Iconv.new(encoding, 'UTF-8') | |
|
95 | str = ic.iconv(str) | |
|
96 | rescue | |
|
97 | str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?') | |
|
98 | end | |
|
99 | else | |
|
100 | ic = Iconv.new(encoding, 'UTF-8') | |
|
101 | txtar = "" | |
|
102 | begin | |
|
103 | txtar += ic.iconv(str) | |
|
104 | rescue Iconv::IllegalSequence | |
|
105 | txtar += $!.success | |
|
106 | str = '?' + $!.failed[1, $!.failed.length] | |
|
107 | retry | |
|
108 | rescue | |
|
109 | txtar += $!.success | |
|
110 | end | |
|
111 | str = txtar | |
|
112 | end | |
|
113 | end | |
|
81 | 114 | end |
|
82 | 115 | end |
@@ -36,9 +36,6 module Redmine | |||
|
36 | 36 | def initialize(lang) |
|
37 | 37 | set_language_if_valid lang |
|
38 | 38 | pdf_encoding = l(:general_pdf_encoding).upcase |
|
39 | if RUBY_VERSION < '1.9' | |
|
40 | @ic = Iconv.new(pdf_encoding, 'UTF-8') | |
|
41 | end | |
|
42 | 39 | super('P', 'mm', 'A4', (pdf_encoding == 'UTF-8'), pdf_encoding) |
|
43 | 40 | case current_language.to_s.downcase |
|
44 | 41 | when 'vi' |
@@ -104,7 +101,7 module Redmine | |||
|
104 | 101 | end |
|
105 | 102 | |
|
106 | 103 | def fix_text_encoding(txt) |
|
107 |
RDMPdfEncoding::rdm_ |
|
|
104 | RDMPdfEncoding::rdm_from_utf8(txt, l(:general_pdf_encoding)) | |
|
108 | 105 | end |
|
109 | 106 | |
|
110 | 107 | def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='') |
@@ -505,37 +502,11 module Redmine | |||
|
505 | 502 | |
|
506 | 503 | class RDMPdfEncoding |
|
507 | 504 | include Redmine::I18n |
|
508 |
def self.rdm_ |
|
|
505 | def self.rdm_from_utf8(txt, encoding) | |
|
509 | 506 | txt ||= '' |
|
507 | txt = Redmine::CodesetUtil.from_utf8(txt, encoding) | |
|
510 | 508 | if txt.respond_to?(:force_encoding) |
|
511 | txt.force_encoding('UTF-8') | |
|
512 | if l(:general_pdf_encoding).upcase != 'UTF-8' | |
|
513 | txt = txt.encode(l(:general_pdf_encoding), :invalid => :replace, | |
|
514 | :undef => :replace, :replace => '?') | |
|
515 | else | |
|
516 | txt = Redmine::CodesetUtil.replace_invalid_utf8(txt) | |
|
517 | end | |
|
518 | 509 | txt.force_encoding('ASCII-8BIT') |
|
519 | elsif RUBY_PLATFORM == 'java' | |
|
520 | begin | |
|
521 | ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8') | |
|
522 | txt = ic.iconv(txt) | |
|
523 | rescue | |
|
524 | txt = txt.gsub(%r{[^\r\n\t\x20-\x7e]}, '?') | |
|
525 | end | |
|
526 | else | |
|
527 | ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8') | |
|
528 | txtar = "" | |
|
529 | begin | |
|
530 | txtar += ic.iconv(txt) | |
|
531 | rescue Iconv::IllegalSequence | |
|
532 | txtar += $!.success | |
|
533 | txt = '?' + $!.failed[1,$!.failed.length] | |
|
534 | retry | |
|
535 | rescue | |
|
536 | txtar += $!.success | |
|
537 | end | |
|
538 | txt = txtar | |
|
539 | 510 | end |
|
540 | 511 | txt |
|
541 | 512 | end |
@@ -19,38 +19,21 require File.expand_path('../../../../../test_helper', __FILE__) | |||
|
19 | 19 | require 'iconv' |
|
20 | 20 | |
|
21 | 21 | class PdfTest < ActiveSupport::TestCase |
|
22 | include Redmine::I18n | |
|
23 | 22 | |
|
24 | 23 | def test_fix_text_encoding_nil |
|
25 | set_language_if_valid 'ja' | |
|
26 | assert_equal 'CP932', l(:general_pdf_encoding) | |
|
27 | if RUBY_VERSION < '1.9' | |
|
28 | if RUBY_PLATFORM == 'java' | |
|
29 | ic = Iconv.new("SJIS", 'UTF-8') | |
|
30 | else | |
|
31 | ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8') | |
|
32 | end | |
|
33 | end | |
|
34 | assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, nil) | |
|
24 | assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "UTF-8") | |
|
25 | assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "ISO-8859-1") | |
|
35 | 26 | end |
|
36 | 27 | |
|
37 | 28 | def test_rdm_pdf_iconv_cannot_convert_ja_cp932 |
|
38 | set_language_if_valid 'ja' | |
|
39 | assert_equal 'CP932', l(:general_pdf_encoding) | |
|
40 | if RUBY_VERSION < '1.9' | |
|
41 | if RUBY_PLATFORM == 'java' | |
|
42 | ic = Iconv.new("SJIS", 'UTF-8') | |
|
43 | else | |
|
44 | ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8') | |
|
45 | end | |
|
46 | end | |
|
29 | encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" ) | |
|
47 | 30 | utf8_txt_1 = "\xe7\x8b\x80\xe6\x85\x8b" |
|
48 | 31 | utf8_txt_2 = "\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80" |
|
49 | 32 | utf8_txt_3 = "\xe7\x8b\x80\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80" |
|
50 | 33 | if utf8_txt_1.respond_to?(:force_encoding) |
|
51 |
txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
|
52 |
txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
|
53 |
txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
|
34 | txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding) | |
|
35 | txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding) | |
|
36 | txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding) | |
|
54 | 37 | assert_equal "?\x91\xd4", txt_1 |
|
55 | 38 | assert_equal "?\x91\xd4?", txt_2 |
|
56 | 39 | assert_equal "??\x91\xd4?", txt_3 |
@@ -59,33 +42,28 class PdfTest < ActiveSupport::TestCase | |||
|
59 | 42 | assert_equal "ASCII-8BIT", txt_3.encoding.to_s |
|
60 | 43 | elsif RUBY_PLATFORM == 'java' |
|
61 | 44 | assert_equal "??", |
|
62 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
|
45 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding) | |
|
63 | 46 | assert_equal "???", |
|
64 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
|
47 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding) | |
|
65 | 48 | assert_equal "????", |
|
66 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
|
49 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding) | |
|
67 | 50 | else |
|
68 | 51 | assert_equal "???\x91\xd4", |
|
69 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
|
52 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding) | |
|
70 | 53 | assert_equal "???\x91\xd4???", |
|
71 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
|
54 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding) | |
|
72 | 55 | assert_equal "??????\x91\xd4???", |
|
73 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
|
56 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding) | |
|
74 | 57 | end |
|
75 | 58 | end |
|
76 | 59 | |
|
77 | 60 | def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_en |
|
78 | set_language_if_valid 'en' | |
|
79 | assert_equal 'UTF-8', l(:general_pdf_encoding) | |
|
80 | 61 | str1 = "Texte encod\xe9 en ISO-8859-1" |
|
81 | 62 | str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test" |
|
82 | 63 | str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding) |
|
83 | 64 | str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding) |
|
84 | if RUBY_VERSION < '1.9' | |
|
85 | ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8') | |
|
86 | end | |
|
87 | txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str1) | |
|
88 | txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str2) | |
|
65 | txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, 'UTF-8') | |
|
66 | txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, 'UTF-8') | |
|
89 | 67 | if txt_1.respond_to?(:force_encoding) |
|
90 | 68 | assert_equal "ASCII-8BIT", txt_1.encoding.to_s |
|
91 | 69 | assert_equal "ASCII-8BIT", txt_2.encoding.to_s |
@@ -95,21 +73,13 class PdfTest < ActiveSupport::TestCase | |||
|
95 | 73 | end |
|
96 | 74 | |
|
97 | 75 | def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_ja |
|
98 | set_language_if_valid 'ja' | |
|
99 | assert_equal 'CP932', l(:general_pdf_encoding) | |
|
100 | 76 | str1 = "Texte encod\xe9 en ISO-8859-1" |
|
101 | 77 | str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test" |
|
102 | 78 | str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding) |
|
103 | 79 | str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding) |
|
104 | if RUBY_VERSION < '1.9' | |
|
105 | if RUBY_PLATFORM == 'java' | |
|
106 | ic = Iconv.new("SJIS", 'UTF-8') | |
|
107 | else | |
|
108 | ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8') | |
|
109 | end | |
|
110 | end | |
|
111 | txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str1) | |
|
112 | txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str2) | |
|
80 | encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" ) | |
|
81 | txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, encoding) | |
|
82 | txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, encoding) | |
|
113 | 83 | if txt_1.respond_to?(:force_encoding) |
|
114 | 84 | assert_equal "ASCII-8BIT", txt_1.encoding.to_s |
|
115 | 85 | assert_equal "ASCII-8BIT", txt_2.encoding.to_s |
General Comments 0
You need to be logged in to leave comments.
Login now