@@ -78,5 +78,38 module Redmine | |||||
78 | end |
|
78 | end | |
79 | str |
|
79 | str | |
80 | end |
|
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 | end |
|
114 | end | |
82 | end |
|
115 | end |
@@ -36,9 +36,6 module Redmine | |||||
36 | def initialize(lang) |
|
36 | def initialize(lang) | |
37 | set_language_if_valid lang |
|
37 | set_language_if_valid lang | |
38 | pdf_encoding = l(:general_pdf_encoding).upcase |
|
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 | super('P', 'mm', 'A4', (pdf_encoding == 'UTF-8'), pdf_encoding) |
|
39 | super('P', 'mm', 'A4', (pdf_encoding == 'UTF-8'), pdf_encoding) | |
43 | case current_language.to_s.downcase |
|
40 | case current_language.to_s.downcase | |
44 | when 'vi' |
|
41 | when 'vi' | |
@@ -104,7 +101,7 module Redmine | |||||
104 | end |
|
101 | end | |
105 |
|
102 | |||
106 | def fix_text_encoding(txt) |
|
103 | def fix_text_encoding(txt) | |
107 |
RDMPdfEncoding::rdm_ |
|
104 | RDMPdfEncoding::rdm_from_utf8(txt, l(:general_pdf_encoding)) | |
108 | end |
|
105 | end | |
109 |
|
106 | |||
110 | def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='') |
|
107 | def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='') | |
@@ -505,37 +502,11 module Redmine | |||||
505 |
|
502 | |||
506 | class RDMPdfEncoding |
|
503 | class RDMPdfEncoding | |
507 | include Redmine::I18n |
|
504 | include Redmine::I18n | |
508 |
def self.rdm_ |
|
505 | def self.rdm_from_utf8(txt, encoding) | |
509 | txt ||= '' |
|
506 | txt ||= '' | |
|
507 | txt = Redmine::CodesetUtil.from_utf8(txt, encoding) | |||
510 | if txt.respond_to?(:force_encoding) |
|
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 | txt.force_encoding('ASCII-8BIT') |
|
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 | end |
|
510 | end | |
540 | txt |
|
511 | txt | |
541 | end |
|
512 | end |
@@ -19,38 +19,21 require File.expand_path('../../../../../test_helper', __FILE__) | |||||
19 | require 'iconv' |
|
19 | require 'iconv' | |
20 |
|
20 | |||
21 | class PdfTest < ActiveSupport::TestCase |
|
21 | class PdfTest < ActiveSupport::TestCase | |
22 | include Redmine::I18n |
|
|||
23 |
|
22 | |||
24 | def test_fix_text_encoding_nil |
|
23 | def test_fix_text_encoding_nil | |
25 | set_language_if_valid 'ja' |
|
24 | assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "UTF-8") | |
26 | assert_equal 'CP932', l(:general_pdf_encoding) |
|
25 | assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "ISO-8859-1") | |
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) |
|
|||
35 | end |
|
26 | end | |
36 |
|
27 | |||
37 | def test_rdm_pdf_iconv_cannot_convert_ja_cp932 |
|
28 | def test_rdm_pdf_iconv_cannot_convert_ja_cp932 | |
38 | set_language_if_valid 'ja' |
|
29 | encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" ) | |
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 |
|
|||
47 | utf8_txt_1 = "\xe7\x8b\x80\xe6\x85\x8b" |
|
30 | utf8_txt_1 = "\xe7\x8b\x80\xe6\x85\x8b" | |
48 | utf8_txt_2 = "\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80" |
|
31 | utf8_txt_2 = "\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80" | |
49 | utf8_txt_3 = "\xe7\x8b\x80\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80" |
|
32 | utf8_txt_3 = "\xe7\x8b\x80\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80" | |
50 | if utf8_txt_1.respond_to?(:force_encoding) |
|
33 | if utf8_txt_1.respond_to?(:force_encoding) | |
51 |
txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
34 | txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding) | |
52 |
txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
35 | txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding) | |
53 |
txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
36 | txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding) | |
54 | assert_equal "?\x91\xd4", txt_1 |
|
37 | assert_equal "?\x91\xd4", txt_1 | |
55 | assert_equal "?\x91\xd4?", txt_2 |
|
38 | assert_equal "?\x91\xd4?", txt_2 | |
56 | assert_equal "??\x91\xd4?", txt_3 |
|
39 | assert_equal "??\x91\xd4?", txt_3 | |
@@ -59,33 +42,28 class PdfTest < ActiveSupport::TestCase | |||||
59 | assert_equal "ASCII-8BIT", txt_3.encoding.to_s |
|
42 | assert_equal "ASCII-8BIT", txt_3.encoding.to_s | |
60 | elsif RUBY_PLATFORM == 'java' |
|
43 | elsif RUBY_PLATFORM == 'java' | |
61 | assert_equal "??", |
|
44 | assert_equal "??", | |
62 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
45 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding) | |
63 | assert_equal "???", |
|
46 | assert_equal "???", | |
64 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
47 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding) | |
65 | assert_equal "????", |
|
48 | assert_equal "????", | |
66 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
49 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding) | |
67 | else |
|
50 | else | |
68 | assert_equal "???\x91\xd4", |
|
51 | assert_equal "???\x91\xd4", | |
69 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
52 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding) | |
70 | assert_equal "???\x91\xd4???", |
|
53 | assert_equal "???\x91\xd4???", | |
71 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
54 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding) | |
72 | assert_equal "??????\x91\xd4???", |
|
55 | assert_equal "??????\x91\xd4???", | |
73 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_ |
|
56 | Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding) | |
74 | end |
|
57 | end | |
75 | end |
|
58 | end | |
76 |
|
59 | |||
77 | def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_en |
|
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 | str1 = "Texte encod\xe9 en ISO-8859-1" |
|
61 | str1 = "Texte encod\xe9 en ISO-8859-1" | |
81 | str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test" |
|
62 | str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test" | |
82 | str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding) |
|
63 | str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding) | |
83 | str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding) |
|
64 | str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding) | |
84 | if RUBY_VERSION < '1.9' |
|
65 | txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, 'UTF-8') | |
85 | ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8') |
|
66 | txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, '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) |
|
|||
89 | if txt_1.respond_to?(:force_encoding) |
|
67 | if txt_1.respond_to?(:force_encoding) | |
90 | assert_equal "ASCII-8BIT", txt_1.encoding.to_s |
|
68 | assert_equal "ASCII-8BIT", txt_1.encoding.to_s | |
91 | assert_equal "ASCII-8BIT", txt_2.encoding.to_s |
|
69 | assert_equal "ASCII-8BIT", txt_2.encoding.to_s | |
@@ -95,21 +73,13 class PdfTest < ActiveSupport::TestCase | |||||
95 | end |
|
73 | end | |
96 |
|
74 | |||
97 | def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_ja |
|
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 | str1 = "Texte encod\xe9 en ISO-8859-1" |
|
76 | str1 = "Texte encod\xe9 en ISO-8859-1" | |
101 | str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test" |
|
77 | str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test" | |
102 | str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding) |
|
78 | str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding) | |
103 | str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding) |
|
79 | str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding) | |
104 | if RUBY_VERSION < '1.9' |
|
80 | encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" ) | |
105 | if RUBY_PLATFORM == 'java' |
|
81 | txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, encoding) | |
106 | ic = Iconv.new("SJIS", 'UTF-8') |
|
82 | txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, encoding) | |
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) |
|
|||
113 | if txt_1.respond_to?(:force_encoding) |
|
83 | if txt_1.respond_to?(:force_encoding) | |
114 | assert_equal "ASCII-8BIT", txt_1.encoding.to_s |
|
84 | assert_equal "ASCII-8BIT", txt_1.encoding.to_s | |
115 | assert_equal "ASCII-8BIT", txt_2.encoding.to_s |
|
85 | assert_equal "ASCII-8BIT", txt_2.encoding.to_s |
General Comments 0
You need to be logged in to leave comments.
Login now