Feature #3261 » pdf-image-r7788.diff
vendor/plugins/rfpdf/lib/tcpdf.rb | ||
---|---|---|
2049 | 2049 |
if (@images[file].nil?) |
2050 | 2050 |
#First use of image, get info |
2051 | 2051 |
if (type == '') |
2052 |
pos = file.rindex('.');
|
|
2053 |
if (pos == 0) |
|
2052 |
pos = File::basename(file).rindex('.');
|
|
2053 |
if (pos.nil? or pos == 0)
|
|
2054 | 2054 |
Error('Image file has no extension and no type was specified: ' + file); |
2055 | 2055 |
end |
2056 |
pos = file.rindex('.'); |
|
2056 | 2057 |
type = file[pos+1..-1]; |
2057 | 2058 |
end |
2058 | 2059 |
type.downcase! |
2059 | 2060 |
if (type == 'jpg' or type == 'jpeg') |
2060 | 2061 |
info=parsejpg(file); |
2061 |
elsif (type == 'png') |
|
2062 |
info=parsepng(file); |
|
2062 |
elsif (type == 'png' or type == 'gif') |
|
2063 |
img = Magick::ImageList.new(file) |
|
2064 |
img.format = "PNG" # convert to PNG from gif |
|
2065 |
img.opacity = 0 # PNG alpha channel delete |
|
2066 |
File.open( @@k_path_cache + File::basename(file), 'w'){|f| |
|
2067 |
f.binmode |
|
2068 |
f.print img.to_blob |
|
2069 |
f.close |
|
2070 |
} |
|
2071 |
info=parsepng( @@k_path_cache + File::basename(file)); |
|
2072 |
File.delete( @@k_path_cache + File::basename(file)) |
|
2063 | 2073 |
else |
2064 | 2074 |
#Allow for additional formats |
2065 | 2075 |
mtd='parse' + type; |
... | ... | |
2075 | 2085 |
end |
2076 | 2086 |
#Automatic width and height calculation if needed |
2077 | 2087 |
if ((w == 0) and (h == 0)) |
2088 |
rescale_x = (@w - @r_margin - x) / (info['w'] / (@img_scale * @k)) |
|
2089 |
rescale_x = 1 if rescale_x >= 1 |
|
2090 |
if (y + info['h'] * rescale_x / (@img_scale * @k) > @page_break_trigger and !@in_footer and AcceptPageBreak()) |
|
2091 |
#Automatic page break |
|
2092 |
if @pages[@page+1].nil? |
|
2093 |
ws = @ws; |
|
2094 |
if (ws > 0) |
|
2095 |
@ws = 0; |
|
2096 |
out('0 Tw'); |
|
2097 |
end |
|
2098 |
AddPage(@cur_orientation); |
|
2099 |
if (ws > 0) |
|
2100 |
@ws = ws; |
|
2101 |
out(sprintf('%.3f Tw', ws * @k)); |
|
2102 |
end |
|
2103 |
else |
|
2104 |
@page += 1; |
|
2105 |
end |
|
2106 |
y=@t_margin; |
|
2107 |
end |
|
2108 |
rescale_y = (@page_break_trigger - y) / (info['h'] / (@img_scale * @k)) |
|
2109 |
rescale_y = 1 if rescale_y >= 1 |
|
2110 |
rescale = rescale_y >= rescale_x ? rescale_x : rescale_y |
|
2111 | ||
2078 | 2112 |
#Put image at 72 dpi |
2079 | 2113 |
# 2004-06-14 :: Nicola Asuni, scale factor where added |
2080 |
w = info['w'] / (@img_scale * @k); |
|
2081 |
h = info['h'] / (@img_scale * @k); |
|
2082 |
end |
|
2083 |
if (w == 0) |
|
2114 |
w = info['w'] * rescale / (@img_scale * @k); |
|
2115 |
h = info['h'] * rescale / (@img_scale * @k); |
|
2116 |
elsif (w == 0) |
|
2084 | 2117 |
w = h * info['w'] / info['h']; |
2085 |
end |
|
2086 |
if (h == 0) |
|
2118 |
elsif (h == 0) |
|
2087 | 2119 |
h = w * info['h'] / info['w']; |
2088 | 2120 |
end |
2089 | 2121 |
out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q', w*@k, h*@k, x*@k, (@h-(y+h))*@k, info['i'])); |
... | ... | |
2846 | 2878 |
if (a.empty?) |
2847 | 2879 |
Error('Missing or incorrect image file: ' + file); |
2848 | 2880 |
end |
2849 |
if (a[2]!='JPEG') |
|
2881 |
if (!a[2].nil? and a[2]!='JPEG')
|
|
2850 | 2882 |
Error('Not a JPEG file: ' + file); |
2851 | 2883 |
end |
2852 | 2884 |
if (a['channels'].nil? or a['channels']==3) |
... | ... | |
2859 | 2891 |
bpc=!a['bits'].nil? ? a['bits'] : 8; |
2860 | 2892 |
#Read whole file |
2861 | 2893 |
data=''; |
2862 |
open(file,'rb') do |f| |
|
2894 | ||
2895 |
open( @@k_path_cache + File::basename(file),'rb') do |f| |
|
2863 | 2896 |
data<<f.read(); |
2864 | 2897 |
end |
2898 |
File.delete( @@k_path_cache + File::basename(file)) |
|
2899 | ||
2865 | 2900 |
return {'w' => a[0],'h' => a[1],'cs' => colspace,'bpc' => bpc,'f'=>'DCTDecode','data' => data} |
2866 | 2901 |
end |
2867 | 2902 | |
... | ... | |
2882 | 2917 |
end |
2883 | 2918 |
w=freadint(f); |
2884 | 2919 |
h=freadint(f); |
2885 |
bpc=f.read(1)[0]; |
|
2920 |
bpc=f.read(1).unpack('C')[0];
|
|
2886 | 2921 |
if (bpc>8) |
2887 | 2922 |
Error('16-bit depth not supported: ' + file); |
2888 | 2923 |
end |
2889 |
ct=f.read(1)[0]; |
|
2924 |
ct=f.read(1).unpack('C')[0];
|
|
2890 | 2925 |
if (ct==0) |
2891 | 2926 |
colspace='DeviceGray'; |
2892 | 2927 |
elsif (ct==2) |
... | ... | |
2896 | 2931 |
else |
2897 | 2932 |
Error('Alpha channel not supported: ' + file); |
2898 | 2933 |
end |
2899 |
if (f.read(1)[0] != 0) |
|
2934 |
if (f.read(1).unpack('C')[0] != 0)
|
|
2900 | 2935 |
Error('Unknown compression method: ' + file); |
2901 | 2936 |
end |
2902 |
if (f.read(1)[0]!=0)
|
|
2937 |
if (f.read(1).unpack('C')[0] != 0)
|
|
2903 | 2938 |
Error('Unknown filter method: ' + file); |
2904 | 2939 |
end |
2905 |
if (f.read(1)[0]!=0)
|
|
2940 |
if (f.read(1).unpack('C')[0] != 0)
|
|
2906 | 2941 |
Error('Interlacing not supported: ' + file); |
2907 | 2942 |
end |
2908 | 2943 |
f.read(4); |
... | ... | |
2922 | 2957 |
#Read transparency info |
2923 | 2958 |
t=f.read( n); |
2924 | 2959 |
if (ct==0) |
2925 |
trns = t[1][0] |
|
2960 |
trns = t[1].unpack('C')[0]
|
|
2926 | 2961 |
elsif (ct==2) |
2927 |
trns = t[[1][0], t[3][0], t[5][0]]
|
|
2962 |
trns = t[[1].unpack('C')[0], t[3].unpack('C')[0], t[5].unpack('C')[0]]
|
|
2928 | 2963 |
else |
2929 | 2964 |
pos=t.include?(0.chr); |
2930 | 2965 |
if (pos!=false) |
... | ... | |
3766 | 3801 |
end |
3767 | 3802 | |
3768 | 3803 |
# |
3804 |
# Convert to accessible file path |
|
3805 |
# @param string :attrname image file name |
|
3806 |
# @access private |
|
3807 |
# |
|
3808 |
def getImageFilename( attrname ) |
|
3809 |
# check of unsafe URI |
|
3810 |
if img.to_s =~ /[^-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]%]/n |
|
3811 |
URI.encode(attrname) |
|
3812 |
else |
|
3813 |
attrname |
|
3814 |
end |
|
3815 |
end |
|
3816 | ||
3817 |
# |
|
3769 | 3818 |
# Process opening tags. |
3770 | 3819 |
# @param string :tag tag name (in upcase) |
3771 | 3820 |
# @param string :attr tag attribute (in upcase) |
... | ... | |
3878 | 3927 |
|
3879 | 3928 |
when 'img' |
3880 | 3929 |
if (!attrs['src'].nil?) |
3881 |
Write(@lasth, '!' + attrs['src'] + '!', '', fill); |
|
3882 |
=begin Comment out. Because not implement image output yet. |
|
3883 |
# replace relative path with real server path |
|
3884 |
attrs['src'] = attrs['src'].gsub(@@k_path_url_cache, @@k_path_cache); |
|
3930 |
# Only generates image include a pdf if RMagick is avalaible |
|
3931 |
unless Object.const_defined?(:Magick) |
|
3932 |
Write(@lasth, attrs['src'], '', fill); |
|
3933 |
return |
|
3934 |
end |
|
3935 |
file = getImageFilename(attrs['src']) |
|
3936 |
if (file.nil?) |
|
3937 |
Write(@lasth, attrs['src'], '', fill); |
|
3938 |
return |
|
3939 |
end |
|
3940 | ||
3885 | 3941 |
if (attrs['width'].nil?) |
3886 | 3942 |
attrs['width'] = 0; |
3887 | 3943 |
end |
... | ... | |
3889 | 3945 |
attrs['height'] = 0; |
3890 | 3946 |
end |
3891 | 3947 |
|
3892 |
Image(attrs['src'], GetX(),GetY(), pixelsToMillimeters(attrs['width']), pixelsToMillimeters(attrs['height'])); |
|
3893 |
#SetX(@img_rb_x); |
|
3894 |
SetY(@img_rb_y); |
|
3895 |
=end |
|
3948 |
begin |
|
3949 |
Image(file, GetX(),GetY(), pixelsToMillimeters(attrs['width']), pixelsToMillimeters(attrs['height'])); |
|
3950 |
#SetX(@img_rb_x); |
|
3951 |
SetY(@img_rb_y); |
|
3952 |
rescue => err |
|
3953 |
logger.error "pdf: Image: error: #{err.message}" |
|
3954 |
Write(@lasth, attrs['src'], '', fill); |
|
3955 |
if File.file?( @@k_path_cache + File::basename(file)) |
|
3956 |
File.delete( @@k_path_cache + File::basename(file)) |
|
3957 |
end |
|
3958 |
end |
|
3896 | 3959 |
end |
3897 | 3960 |
|
3898 | 3961 |
when 'ul', 'ol' |
vendor/plugins/rfpdf/lib/core/rmagick.rb | ||
---|---|---|
57 | 57 |
end |
58 | 58 | |
59 | 59 |
out['bits'] = image.channel_depth |
60 |
File.open( TCPDF.k_path_cache + File::basename(filename), 'w'){|f| |
|
61 |
f.binmode |
|
62 |
f.print image.to_blob |
|
63 |
f.close |
|
64 |
} |
|
60 | 65 |
|
61 | 66 |
out |
62 | 67 |
end |
lib/redmine/export/pdf.rb | ||
---|---|---|
21 | 21 |
require 'fpdf/chinese' |
22 | 22 |
require 'fpdf/japanese' |
23 | 23 |
require 'fpdf/korean' |
24 |
require 'core/rmagick' |
|
24 | 25 | |
25 | 26 |
module Redmine |
26 | 27 |
module Export |
... | ... | |
34 | 35 |
attr_accessor :footer_date |
35 | 36 | |
36 | 37 |
def initialize(lang) |
38 |
@@k_path_cache = Rails.root.join('tmp', 'pdf') |
|
39 |
FileUtils.mkdir_p @@k_path_cache unless File::exist?(@@k_path_cache) |
|
37 | 40 |
set_language_if_valid lang |
38 | 41 |
pdf_encoding = l(:general_pdf_encoding).upcase |
39 | 42 |
if RUBY_VERSION < '1.9' |
40 |
@ic = Iconv.new(pdf_encoding, 'UTF-8') |
|
43 |
@ic_encode = Iconv.new(pdf_encoding, 'UTF-8') |
|
44 |
@ic_decode = Iconv.new('UTF-8', pdf_encoding) |
|
41 | 45 |
end |
42 | 46 |
super('P', 'mm', 'A4', (pdf_encoding == 'UTF-8'), pdf_encoding) |
43 | 47 |
case current_language.to_s.downcase |
... | ... | |
104 | 108 |
end |
105 | 109 | |
106 | 110 |
def fix_text_encoding(txt) |
107 |
RDMPdfEncoding::rdm_pdf_iconv(@ic, txt) |
|
111 |
RDMPdfEncoding::rdm_pdf_iconv(@ic_encode, txt) |
|
112 |
end |
|
113 | ||
114 |
def fix_text_decoding(txt) |
|
115 |
RDMPdfEncoding::rdm_pdf_iconv(@ic_decode, txt, true) |
|
108 | 116 |
end |
109 | 117 | |
110 | 118 |
def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='') |
... | ... | |
115 | 123 |
MultiCell(w, h, fix_text_encoding(txt), border, align, fill, ln) |
116 | 124 |
end |
117 | 125 | |
118 |
def RDMwriteHTMLCell(w, h, x, y, html='', border=0, ln=1, fill=0) |
|
119 |
writeHTMLCell(w, h, x, y, fix_text_encoding(html), border, ln, fill) |
|
126 |
def RDMwriteHTMLCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0) |
|
127 |
@attachments = attachments.sort_by(&:created_on).reverse |
|
128 | ||
129 |
writeHTMLCell(w, h, x, y, |
|
130 |
fix_text_encoding(Redmine::WikiFormatting.to_html(Setting.text_formatting, txt)), |
|
131 |
border, ln, fill) |
|
132 |
end |
|
133 | ||
134 |
def getImageFilename(attrname) |
|
135 |
# attrname : general_pdf_encoding string file/uri name |
|
136 |
if /^https?:\/\//i =~ attrname |
|
137 |
uri = fix_text_decoding(attrname) # get original URI |
|
138 | ||
139 |
# check of unsafe URI |
|
140 |
if uri =~ /[^-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]%]/n |
|
141 |
return URI.encode(uri) |
|
142 |
else |
|
143 |
return uri |
|
144 |
end |
|
145 |
else |
|
146 |
for attachment in @attachments |
|
147 |
# attachment.filename : UTF-8 string name |
|
148 |
# attachment.diskfile : real server path file name |
|
149 |
if fix_text_encoding(attachment.filename) == attrname && File.file?(attachment.diskfile) |
|
150 |
return attachment.diskfile |
|
151 |
end |
|
152 |
end |
|
153 |
end |
|
154 |
return nil |
|
120 | 155 |
end |
121 | 156 | |
122 | 157 |
def Footer |
... | ... | |
357 | 392 |
pdf.SetFontStyle('B',9) |
358 | 393 |
pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1) |
359 | 394 |
pdf.SetFontStyle('',9) |
395 | ||
396 |
# Set resize image scale |
|
397 |
pdf.SetImageScale(1.6) |
|
360 | 398 |
pdf.RDMwriteHTMLCell(35+155, 5, 0, 0, |
361 |
Redmine::WikiFormatting.to_html( |
|
362 |
Setting.text_formatting, issue.description.to_s),"LRB") |
|
399 |
issue.description.to_s, issue.attachments, "LRB") |
|
363 | 400 | |
364 | 401 |
# for CJK |
365 | 402 |
truncate_length = ( l(:general_pdf_encoding).upcase == "UTF-8" ? 90 : 65 ) |
... | ... | |
421 | 458 |
unless changeset.comments.blank? |
422 | 459 |
pdf.SetFontStyle('',8) |
423 | 460 |
pdf.RDMwriteHTMLCell(190,5,0,0, |
424 |
Redmine::WikiFormatting.to_html( |
|
425 |
Setting.text_formatting, changeset.comments.to_s), "") |
|
461 |
changeset.comments.to_s, issue.attachments, "") |
|
426 | 462 |
end |
427 | 463 |
pdf.Ln |
428 | 464 |
end |
... | ... | |
446 | 482 |
pdf.Ln unless journal.details.empty? |
447 | 483 |
pdf.SetFontStyle('',8) |
448 | 484 |
pdf.RDMwriteHTMLCell(190,5,0,0, |
449 |
Redmine::WikiFormatting.to_html( |
|
450 |
Setting.text_formatting, journal.notes.to_s), "") |
|
485 |
journal.notes.to_s, issue.attachments, "") |
|
451 | 486 |
end |
452 | 487 |
pdf.Ln |
453 | 488 |
end |
... | ... | |
483 | 518 |
pdf.SetImageScale(1.6) |
484 | 519 |
pdf.SetFontStyle('',9) |
485 | 520 |
pdf.RDMwriteHTMLCell(190,5,0,0, |
486 |
Redmine::WikiFormatting.to_html( |
|
487 |
Setting.text_formatting, page.content.text.to_s), "TLRB") |
|
521 |
page.content.text.to_s, page.attachments, "TLRB") |
|
488 | 522 |
if page.attachments.any? |
489 | 523 |
pdf.Ln |
490 | 524 |
pdf.SetFontStyle('B',9) |
... | ... | |
504 | 538 | |
505 | 539 |
class RDMPdfEncoding |
506 | 540 |
include Redmine::I18n |
507 |
def self.rdm_pdf_iconv(ic, txt) |
|
541 |
def self.rdm_pdf_iconv(ic, txt, toUTF8 = false)
|
|
508 | 542 |
txt ||= '' |
509 | 543 |
if txt.respond_to?(:force_encoding) |
510 |
txt.force_encoding('UTF-8') |
|
511 | 544 |
if l(:general_pdf_encoding).upcase != 'UTF-8' |
512 |
txt = txt.encode(l(:general_pdf_encoding), :invalid => :replace, |
|
513 |
:undef => :replace, :replace => '?') |
|
545 |
if toUTF8 == false |
|
546 |
txt.force_encoding('UTF-8') |
|
547 |
txtar = txt.encode(l(:general_pdf_encoding), :invalid => :replace, |
|
548 |
:undef => :replace, :replace => '?') |
|
549 |
txtar.force_encoding('ASCII-8BIT') |
|
550 |
else |
|
551 |
txt.force_encoding(l(:general_pdf_encoding)) |
|
552 |
txtar = txt.encode('UTF-8', :invalid => :replace, |
|
553 |
:undef => :replace, :replace => '?') |
|
554 |
txt.force_encoding('ASCII-8BIT') |
|
555 |
end |
|
514 | 556 |
else |
515 |
txt = Redmine::CodesetUtil.replace_invalid_utf8(txt) |
|
557 |
txt.force_encoding('UTF-8') |
|
558 |
txtar = Redmine::CodesetUtil.replace_invalid_utf8(txt) |
|
559 |
txtar.force_encoding('ASCII-8BIT') |
|
516 | 560 |
end |
517 |
txt.force_encoding('ASCII-8BIT')
|
|
561 |
txt = txtar
|
|
518 | 562 |
elsif RUBY_PLATFORM == 'java' |
519 | 563 |
begin |
520 |
ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8') |
|
564 |
if toUTF8 == false |
|
565 |
ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8') |
|
566 |
else |
|
567 |
ic ||= Iconv.new('UTF-8', l(:general_pdf_encoding)) |
|
568 |
end |
|
521 | 569 |
txt = ic.iconv(txt) |
522 | 570 |
rescue |
523 | 571 |
txt = txt.gsub(%r{[^\r\n\t\x20-\x7e]}, '?') |
524 | 572 |
end |
525 | 573 |
else |
526 |
ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8') |
|
574 |
if toUTF8 == false |
|
575 |
ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8') |
|
576 |
else |
|
577 |
ic ||= Iconv.new('UTF-8', l(:general_pdf_encoding)) |
|
578 |
end |
|
527 | 579 |
txtar = "" |
528 | 580 |
begin |
529 | 581 |
txtar += ic.iconv(txt) |
test/unit/lib/redmine/export/pdf_test.rb | ||
---|---|---|
34 | 34 |
assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, nil) |
35 | 35 |
end |
36 | 36 | |
37 |
def test_rdm_pdf_iconv_cannot_convert_ja_cp932 |
|
37 |
def test_fix_text_decoding_nil |
|
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("UTF-8", 'SJIS') |
|
43 |
else |
|
44 |
ic = Iconv.new('UTF-8', l(:general_pdf_encoding)) |
|
45 |
end |
|
46 |
end |
|
47 |
assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, nil, true) |
|
48 |
end |
|
49 | ||
50 |
def test_rdm_pdf_iconv_cannot_convert_ja_to_cp932 |
|
38 | 51 |
set_language_if_valid 'ja' |
39 | 52 |
assert_equal 'CP932', l(:general_pdf_encoding) |
40 | 53 |
if RUBY_VERSION < '1.9' |
... | ... | |
44 | 57 |
ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8') |
45 | 58 |
end |
46 | 59 |
end |
60 |
# \xe7\x8b\x80 : invalid UTF-8 character |
|
61 |
# \xe6\x85\x8b : valid UTF-8 character => \x91\xd4 : valid CP932 character |
|
47 | 62 |
utf8_txt_1 = "\xe7\x8b\x80\xe6\x85\x8b" |
48 | 63 |
utf8_txt_2 = "\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80" |
49 | 64 |
utf8_txt_3 = "\xe7\x8b\x80\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80" |
... | ... | |
54 | 69 |
assert_equal "?\x91\xd4", txt_1 |
55 | 70 |
assert_equal "?\x91\xd4?", txt_2 |
56 | 71 |
assert_equal "??\x91\xd4?", txt_3 |
72 |
assert_equal "UTF-8", utf8_txt_1.encoding.to_s |
|
73 |
assert_equal "UTF-8", utf8_txt_2.encoding.to_s |
|
74 |
assert_equal "UTF-8", utf8_txt_3.encoding.to_s |
|
57 | 75 |
assert_equal "ASCII-8BIT", txt_1.encoding.to_s |
58 | 76 |
assert_equal "ASCII-8BIT", txt_2.encoding.to_s |
59 | 77 |
assert_equal "ASCII-8BIT", txt_3.encoding.to_s |
... | ... | |
74 | 92 |
end |
75 | 93 |
end |
76 | 94 | |
95 |
def test_rdm_pdf_iconv_cannot_convert_from_cp932 |
|
96 |
set_language_if_valid 'ja' |
|
97 |
assert_equal 'CP932', l(:general_pdf_encoding) |
|
98 |
if RUBY_VERSION < '1.9' |
|
99 |
if RUBY_PLATFORM == 'java' |
|
100 |
ic = Iconv.new("UTF-8", 'SJIS') |
|
101 |
else |
|
102 |
ic = Iconv.new('UTF-8', l(:general_pdf_encoding)) |
|
103 |
end |
|
104 |
end |
|
105 |
# \x90\xff : invalid CP932 character |
|
106 |
# \x91\xd4 : valid CP932 character => \xe6\x85\x8b : valid UTF-8 character |
|
107 |
cp932_txt_1 = "\x90\xff\x91\xd4" |
|
108 |
cp932_txt_2 = "\x90\xff\x91\xd4\x90\xff" |
|
109 |
cp932_txt_3 = "\x90\xff\x90\xff\x91\xd4\x90\xff" |
|
110 |
if cp932_txt_1.respond_to?(:force_encoding) |
|
111 |
txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_1, true) |
|
112 |
txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_2, true) |
|
113 |
txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_3, true) |
|
114 |
assert_equal "ASCII-8BIT", cp932_txt_1.encoding.to_s |
|
115 |
assert_equal "ASCII-8BIT", cp932_txt_2.encoding.to_s |
|
116 |
assert_equal "ASCII-8BIT", cp932_txt_3.encoding.to_s |
|
117 |
assert_equal "UTF-8", txt_1.encoding.to_s |
|
118 |
assert_equal "UTF-8", txt_2.encoding.to_s |
|
119 |
assert_equal "UTF-8", txt_3.encoding.to_s |
|
120 |
assert_equal "??\xe6\x85\x8b", txt_1.force_encoding('ASCII-8BIT') |
|
121 |
assert_equal "??\xe6\x85\x8b??", txt_2.force_encoding('ASCII-8BIT') |
|
122 |
assert_equal "????\xe6\x85\x8b??", txt_3.force_encoding('ASCII-8BIT') |
|
123 |
elsif RUBY_PLATFORM == 'java' |
|
124 |
assert_equal "????", |
|
125 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_1, true) |
|
126 |
assert_equal "??????", |
|
127 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_2, true) |
|
128 |
assert_equal "????????", |
|
129 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_3, true) |
|
130 |
else |
|
131 |
assert_equal "??\xe6\x85\x8b", |
|
132 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_1, true) |
|
133 |
assert_equal "??\xe6\x85\x8b??", |
|
134 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_2, true) |
|
135 |
assert_equal "????\xe6\x85\x8b??", |
|
136 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_3, true) |
|
137 |
end |
|
138 |
end |
|
139 | ||
77 | 140 |
def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_en |
78 | 141 |
set_language_if_valid 'en' |
79 | 142 |
assert_equal 'UTF-8', l(:general_pdf_encoding) |
... | ... | |
86 | 149 |
end |
87 | 150 |
txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str1) |
88 | 151 |
txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str2) |
152 |
txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str1, true) |
|
153 |
txt_4 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str2, true) |
|
89 | 154 |
if txt_1.respond_to?(:force_encoding) |
155 |
assert_equal "UTF-8", str1.encoding.to_s |
|
156 |
assert_equal "UTF-8", str2.encoding.to_s |
|
90 | 157 |
assert_equal "ASCII-8BIT", txt_1.encoding.to_s |
91 | 158 |
assert_equal "ASCII-8BIT", txt_2.encoding.to_s |
159 |
assert_equal "ASCII-8BIT", txt_3.encoding.to_s |
|
160 |
assert_equal "ASCII-8BIT", txt_4.encoding.to_s |
|
92 | 161 |
end |
93 | 162 |
assert_equal "Texte encod? en ISO-8859-1", txt_1 |
94 | 163 |
assert_equal "?a?b?c?d?e test", txt_2 |
164 |
assert_equal "Texte encod? en ISO-8859-1", txt_3 |
|
165 |
assert_equal "?a?b?c?d?e test", txt_4 |
|
95 | 166 |
end |
96 | 167 | |
97 | 168 |
def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_ja |
... | ... | |
111 | 182 |
txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str1) |
112 | 183 |
txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str2) |
113 | 184 |
if txt_1.respond_to?(:force_encoding) |
185 |
assert_equal "UTF-8", str1.encoding.to_s |
|
186 |
assert_equal "UTF-8", str2.encoding.to_s |
|
114 | 187 |
assert_equal "ASCII-8BIT", txt_1.encoding.to_s |
115 | 188 |
assert_equal "ASCII-8BIT", txt_2.encoding.to_s |
116 | 189 |
end |
test/unit/lib/redmine/export/pdf_test.rb | ||
---|---|---|
103 | 103 |
end |
104 | 104 |
end |
105 | 105 |
# \x90\xff : invalid CP932 character |
106 |
# \x91\xd4 : valid CP932 character => \xe6\x85\x8b : valid UTF-8 character |
|
107 |
cp932_txt_1 = "\x90\xff\x91\xd4" |
|
108 |
cp932_txt_2 = "\x90\xff\x91\xd4\x90\xff" |
|
109 |
cp932_txt_3 = "\x90\xff\x90\xff\x91\xd4\x90\xff" |
|
106 |
# \x91\xe4 : valid CP932 character => \xe5\x8f\xb0 : valid UTF-8 character |
|
107 | ||
108 |
cp932_txt_1 = "\x90\xff\x91\xe4" |
|
109 |
cp932_txt_2 = "\x90\xff\x91\xe4\x90\xff" |
|
110 |
cp932_txt_3 = "\x90\xff\x90\xff\x91\xe4\x90\xff" |
|
110 | 111 |
if cp932_txt_1.respond_to?(:force_encoding) |
111 | 112 |
txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_1, true) |
112 | 113 |
txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_2, true) |
... | ... | |
117 | 118 |
assert_equal "UTF-8", txt_1.encoding.to_s |
118 | 119 |
assert_equal "UTF-8", txt_2.encoding.to_s |
119 | 120 |
assert_equal "UTF-8", txt_3.encoding.to_s |
120 |
assert_equal "??\xe6\x85\x8b", txt_1.force_encoding('ASCII-8BIT')
|
|
121 |
assert_equal "??\xe6\x85\x8b??", txt_2.force_encoding('ASCII-8BIT')
|
|
122 |
assert_equal "????\xe6\x85\x8b??", txt_3.force_encoding('ASCII-8BIT')
|
|
121 |
assert_equal "??\xe5\x8f\xb0", txt_1.force_encoding('ASCII-8BIT')
|
|
122 |
assert_equal "??\xe5\x8f\xb0??", txt_2.force_encoding('ASCII-8BIT')
|
|
123 |
assert_equal "????\xe5\x8f\xb0??", txt_3.force_encoding('ASCII-8BIT')
|
|
123 | 124 |
elsif RUBY_PLATFORM == 'java' |
124 | 125 |
assert_equal "????", |
125 | 126 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_1, true) |
... | ... | |
128 | 129 |
assert_equal "????????", |
129 | 130 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_3, true) |
130 | 131 |
else |
131 |
assert_equal "??\xe6\x85\x8b",
|
|
132 |
assert_equal "??\xe5\x8f\xb0",
|
|
132 | 133 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_1, true) |
133 |
assert_equal "??\xe6\x85\x8b??",
|
|
134 |
assert_equal "??\xe5\x8f\xb0??",
|
|
134 | 135 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_2, true) |
135 |
assert_equal "????\xe6\x85\x8b??",
|
|
136 |
assert_equal "????\xe5\x8f\xb0??",
|
|
136 | 137 |
Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, cp932_txt_3, true) |
137 | 138 |
end |
138 | 139 |
end |