##// END OF EJS Templates
move iconv from utf8 logic from pdf to lib/redmine/codeset_util.rb for common use (#8549)...
Toshi MARUYAMA -
r7698:8433bbab69c6
parent child
Show More
@@ -1,82 +1,115
1 1 require 'iconv'
2 2
3 3 module Redmine
4 4 module CodesetUtil
5 5
6 6 def self.replace_invalid_utf8(str)
7 7 return str if str.nil?
8 8 if str.respond_to?(:force_encoding)
9 9 str.force_encoding('UTF-8')
10 10 if ! str.valid_encoding?
11 11 str = str.encode("US-ASCII", :invalid => :replace,
12 12 :undef => :replace, :replace => '?').encode("UTF-8")
13 13 end
14 14 elsif RUBY_PLATFORM == 'java'
15 15 begin
16 16 ic = Iconv.new('UTF-8', 'UTF-8')
17 17 str = ic.iconv(str)
18 18 rescue
19 19 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
20 20 end
21 21 else
22 22 ic = Iconv.new('UTF-8', 'UTF-8')
23 23 txtar = ""
24 24 begin
25 25 txtar += ic.iconv(str)
26 26 rescue Iconv::IllegalSequence
27 27 txtar += $!.success
28 28 str = '?' + $!.failed[1,$!.failed.length]
29 29 retry
30 30 rescue
31 31 txtar += $!.success
32 32 end
33 33 str = txtar
34 34 end
35 35 str
36 36 end
37 37
38 38 def self.to_utf8(str, encoding)
39 39 return str if str.nil?
40 40 str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
41 41 if str.empty?
42 42 str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
43 43 return str
44 44 end
45 45 enc = encoding.blank? ? "UTF-8" : encoding
46 46 if str.respond_to?(:force_encoding)
47 47 if enc.upcase != "UTF-8"
48 48 str.force_encoding(enc)
49 49 str = str.encode("UTF-8", :invalid => :replace,
50 50 :undef => :replace, :replace => '?')
51 51 else
52 52 str.force_encoding("UTF-8")
53 53 if ! str.valid_encoding?
54 54 str = str.encode("US-ASCII", :invalid => :replace,
55 55 :undef => :replace, :replace => '?').encode("UTF-8")
56 56 end
57 57 end
58 58 elsif RUBY_PLATFORM == 'java'
59 59 begin
60 60 ic = Iconv.new('UTF-8', enc)
61 61 str = ic.iconv(str)
62 62 rescue
63 63 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
64 64 end
65 65 else
66 66 ic = Iconv.new('UTF-8', enc)
67 67 txtar = ""
68 68 begin
69 69 txtar += ic.iconv(str)
70 70 rescue Iconv::IllegalSequence
71 71 txtar += $!.success
72 72 str = '?' + $!.failed[1,$!.failed.length]
73 73 retry
74 74 rescue
75 75 txtar += $!.success
76 76 end
77 77 str = txtar
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
@@ -1,545 +1,516
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2011 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 require 'iconv'
21 21 require 'fpdf/chinese'
22 22 require 'fpdf/japanese'
23 23 require 'fpdf/korean'
24 24
25 25 module Redmine
26 26 module Export
27 27 module PDF
28 28 include ActionView::Helpers::TextHelper
29 29 include ActionView::Helpers::NumberHelper
30 30 include IssuesHelper
31 31
32 32 class ITCPDF < TCPDF
33 33 include Redmine::I18n
34 34 attr_accessor :footer_date
35 35
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'
45 42 @font_for_content = 'DejaVuSans'
46 43 @font_for_footer = 'DejaVuSans'
47 44 else
48 45 case pdf_encoding
49 46 when 'UTF-8'
50 47 @font_for_content = 'FreeSans'
51 48 @font_for_footer = 'FreeSans'
52 49 when 'CP949'
53 50 extend(PDF_Korean)
54 51 AddUHCFont()
55 52 @font_for_content = 'UHC'
56 53 @font_for_footer = 'UHC'
57 54 when 'CP932', 'SJIS', 'SHIFT_JIS'
58 55 extend(PDF_Japanese)
59 56 AddSJISFont()
60 57 @font_for_content = 'SJIS'
61 58 @font_for_footer = 'SJIS'
62 59 when 'GB18030'
63 60 extend(PDF_Chinese)
64 61 AddGBFont()
65 62 @font_for_content = 'GB'
66 63 @font_for_footer = 'GB'
67 64 when 'BIG5'
68 65 extend(PDF_Chinese)
69 66 AddBig5Font()
70 67 @font_for_content = 'Big5'
71 68 @font_for_footer = 'Big5'
72 69 else
73 70 @font_for_content = 'Arial'
74 71 @font_for_footer = 'Helvetica'
75 72 end
76 73 end
77 74 SetCreator(Redmine::Info.app_name)
78 75 SetFont(@font_for_content)
79 76 end
80 77
81 78 def SetFontStyle(style, size)
82 79 SetFont(@font_for_content, style, size)
83 80 end
84 81
85 82 def SetTitle(txt)
86 83 txt = begin
87 84 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
88 85 hextxt = "<FEFF" # FEFF is BOM
89 86 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
90 87 hextxt << ">"
91 88 rescue
92 89 txt
93 90 end || ''
94 91 super(txt)
95 92 end
96 93
97 94 def textstring(s)
98 95 # Format a text string
99 96 if s =~ /^</ # This means the string is hex-dumped.
100 97 return s
101 98 else
102 99 return '('+escape(s)+')'
103 100 end
104 101 end
105 102
106 103 def fix_text_encoding(txt)
107 RDMPdfEncoding::rdm_pdf_iconv(@ic, txt)
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='')
111 108 Cell(w, h, fix_text_encoding(txt), border, ln, align, fill, link)
112 109 end
113 110
114 111 def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1)
115 112 MultiCell(w, h, fix_text_encoding(txt), border, align, fill, ln)
116 113 end
117 114
118 115 def RDMwriteHTMLCell(w, h, x, y, html='', border=0, ln=1, fill=0)
119 116 writeHTMLCell(w, h, x, y, fix_text_encoding(html), border, ln, fill)
120 117 end
121 118
122 119 def Footer
123 120 SetFont(@font_for_footer, 'I', 8)
124 121 SetY(-15)
125 122 SetX(15)
126 123 RDMCell(0, 5, @footer_date, 0, 0, 'L')
127 124 SetY(-15)
128 125 SetX(-30)
129 126 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
130 127 end
131 128 end
132 129
133 130 # Returns a PDF string of a list of issues
134 131 def issues_to_pdf(issues, project, query)
135 132 pdf = ITCPDF.new(current_language)
136 133 title = query.new_record? ? l(:label_issue_plural) : query.name
137 134 title = "#{project} - #{title}" if project
138 135 pdf.SetTitle(title)
139 136 pdf.alias_nb_pages
140 137 pdf.footer_date = format_date(Date.today)
141 138 pdf.SetAutoPageBreak(false)
142 139 pdf.AddPage("L")
143 140
144 141 # Landscape A4 = 210 x 297 mm
145 142 page_height = 210
146 143 page_width = 297
147 144 right_margin = 10
148 145 bottom_margin = 20
149 146 col_id_width = 10
150 147 row_height = 5
151 148
152 149 # column widths
153 150 table_width = page_width - right_margin - 10 # fixed left margin
154 151 col_width = []
155 152 unless query.columns.empty?
156 153 col_width = query.columns.collect do |c|
157 154 (c.name == :subject || (c.is_a?(QueryCustomFieldColumn) &&
158 155 ['string', 'text'].include?(c.custom_field.field_format))) ? 4.0 : 1.0
159 156 end
160 157 ratio = (table_width - col_id_width) / col_width.inject(0) {|s,w| s += w}
161 158 col_width = col_width.collect {|w| w * ratio}
162 159 end
163 160
164 161 # title
165 162 pdf.SetFontStyle('B',11)
166 163 pdf.RDMCell(190,10, title)
167 164 pdf.Ln
168 165
169 166 # headers
170 167 pdf.SetFontStyle('B',8)
171 168 pdf.SetFillColor(230, 230, 230)
172 169
173 170 # render it background to find the max height used
174 171 base_x = pdf.GetX
175 172 base_y = pdf.GetY
176 173 max_height = issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
177 174 pdf.Rect(base_x, base_y, table_width, max_height, 'FD');
178 175 pdf.SetXY(base_x, base_y);
179 176
180 177 # write the cells on page
181 178 pdf.RDMCell(col_id_width, row_height, "#", "T", 0, 'C', 1)
182 179 issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
183 180 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
184 181 pdf.SetY(base_y + max_height);
185 182
186 183 # rows
187 184 pdf.SetFontStyle('',8)
188 185 pdf.SetFillColor(255, 255, 255)
189 186 previous_group = false
190 187 issue_list(issues) do |issue, level|
191 188 if query.grouped? &&
192 189 (group = query.group_by_column.value(issue)) != previous_group
193 190 pdf.SetFontStyle('B',9)
194 191 pdf.RDMCell(277, row_height,
195 192 (group.blank? ? 'None' : group.to_s) + " (#{query.issue_count_by_group[group]})",
196 193 1, 1, 'L')
197 194 pdf.SetFontStyle('',8)
198 195 previous_group = group
199 196 end
200 197 # fetch all the row values
201 198 col_values = query.columns.collect do |column|
202 199 s = if column.is_a?(QueryCustomFieldColumn)
203 200 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
204 201 show_value(cv)
205 202 else
206 203 value = issue.send(column.name)
207 204 if column.name == :subject
208 205 value = " " * level + value
209 206 end
210 207 if value.is_a?(Date)
211 208 format_date(value)
212 209 elsif value.is_a?(Time)
213 210 format_time(value)
214 211 else
215 212 value
216 213 end
217 214 end
218 215 s.to_s
219 216 end
220 217
221 218 # render it off-page to find the max height used
222 219 base_x = pdf.GetX
223 220 base_y = pdf.GetY
224 221 pdf.SetY(2 * page_height)
225 222 max_height = issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
226 223 pdf.SetXY(base_x, base_y)
227 224
228 225 # make new page if it doesn't fit on the current one
229 226 space_left = page_height - base_y - bottom_margin
230 227 if max_height > space_left
231 228 pdf.AddPage("L")
232 229 base_x = pdf.GetX
233 230 base_y = pdf.GetY
234 231 end
235 232
236 233 # write the cells on page
237 234 pdf.RDMCell(col_id_width, row_height, issue.id.to_s, "T", 0, 'C', 1)
238 235 issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
239 236 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
240 237 pdf.SetY(base_y + max_height);
241 238 end
242 239
243 240 if issues.size == Setting.issues_export_limit.to_i
244 241 pdf.SetFontStyle('B',10)
245 242 pdf.RDMCell(0, row_height, '...')
246 243 end
247 244 pdf.Output
248 245 end
249 246
250 247 # Renders MultiCells and returns the maximum height used
251 248 def issues_to_pdf_write_cells(pdf, col_values, col_widths,
252 249 row_height, head=false)
253 250 base_y = pdf.GetY
254 251 max_height = row_height
255 252 col_values.each_with_index do |column, i|
256 253 col_x = pdf.GetX
257 254 if head == true
258 255 pdf.RDMMultiCell(col_widths[i], row_height, column.caption, "T", 'L', 1)
259 256 else
260 257 pdf.RDMMultiCell(col_widths[i], row_height, column, "T", 'L', 1)
261 258 end
262 259 max_height = (pdf.GetY - base_y) if (pdf.GetY - base_y) > max_height
263 260 pdf.SetXY(col_x + col_widths[i], base_y);
264 261 end
265 262 return max_height
266 263 end
267 264
268 265 # Draw lines to close the row (MultiCell border drawing in not uniform)
269 266 def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y,
270 267 id_width, col_widths)
271 268 col_x = top_x + id_width
272 269 pdf.Line(col_x, top_y, col_x, lower_y) # id right border
273 270 col_widths.each do |width|
274 271 col_x += width
275 272 pdf.Line(col_x, top_y, col_x, lower_y) # columns right border
276 273 end
277 274 pdf.Line(top_x, top_y, top_x, lower_y) # left border
278 275 pdf.Line(top_x, lower_y, col_x, lower_y) # bottom border
279 276 end
280 277
281 278 # Returns a PDF string of a single issue
282 279 def issue_to_pdf(issue)
283 280 pdf = ITCPDF.new(current_language)
284 281 pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
285 282 pdf.alias_nb_pages
286 283 pdf.footer_date = format_date(Date.today)
287 284 pdf.AddPage
288 285 pdf.SetFontStyle('B',11)
289 286 buf = "#{issue.project} - #{issue.tracker} # #{issue.id}"
290 287 pdf.RDMMultiCell(190, 5, buf)
291 288 pdf.Ln
292 289 pdf.SetFontStyle('',8)
293 290 base_x = pdf.GetX
294 291 i = 1
295 292 issue.ancestors.each do |ancestor|
296 293 pdf.SetX(base_x + i)
297 294 buf = "#{ancestor.tracker} # #{ancestor.id} (#{ancestor.status.to_s}): #{ancestor.subject}"
298 295 pdf.RDMMultiCell(190 - i, 5, buf)
299 296 i += 1 if i < 35
300 297 end
301 298 pdf.Ln
302 299
303 300 pdf.SetFontStyle('B',9)
304 301 pdf.RDMCell(35,5, l(:field_status) + ":","LT")
305 302 pdf.SetFontStyle('',9)
306 303 pdf.RDMCell(60,5, issue.status.to_s,"RT")
307 304 pdf.SetFontStyle('B',9)
308 305 pdf.RDMCell(35,5, l(:field_priority) + ":","LT")
309 306 pdf.SetFontStyle('',9)
310 307 pdf.RDMCell(60,5, issue.priority.to_s,"RT")
311 308 pdf.Ln
312 309
313 310 pdf.SetFontStyle('B',9)
314 311 pdf.RDMCell(35,5, l(:field_author) + ":","L")
315 312 pdf.SetFontStyle('',9)
316 313 pdf.RDMCell(60,5, issue.author.to_s,"R")
317 314 pdf.SetFontStyle('B',9)
318 315 pdf.RDMCell(35,5, l(:field_category) + ":","L")
319 316 pdf.SetFontStyle('',9)
320 317 pdf.RDMCell(60,5, issue.category.to_s,"R")
321 318 pdf.Ln
322 319
323 320 pdf.SetFontStyle('B',9)
324 321 pdf.RDMCell(35,5, l(:field_created_on) + ":","L")
325 322 pdf.SetFontStyle('',9)
326 323 pdf.RDMCell(60,5, format_date(issue.created_on),"R")
327 324 pdf.SetFontStyle('B',9)
328 325 pdf.RDMCell(35,5, l(:field_assigned_to) + ":","L")
329 326 pdf.SetFontStyle('',9)
330 327 pdf.RDMCell(60,5, issue.assigned_to.to_s,"R")
331 328 pdf.Ln
332 329
333 330 pdf.SetFontStyle('B',9)
334 331 pdf.RDMCell(35,5, l(:field_updated_on) + ":","LB")
335 332 pdf.SetFontStyle('',9)
336 333 pdf.RDMCell(60,5, format_date(issue.updated_on),"RB")
337 334 pdf.SetFontStyle('B',9)
338 335 pdf.RDMCell(35,5, l(:field_due_date) + ":","LB")
339 336 pdf.SetFontStyle('',9)
340 337 pdf.RDMCell(60,5, format_date(issue.due_date),"RB")
341 338 pdf.Ln
342 339
343 340 for custom_value in issue.custom_field_values
344 341 pdf.SetFontStyle('B',9)
345 342 pdf.RDMCell(35,5, custom_value.custom_field.name + ":","L")
346 343 pdf.SetFontStyle('',9)
347 344 pdf.RDMMultiCell(155,5, (show_value custom_value),"R")
348 345 end
349 346
350 347 y0 = pdf.GetY
351 348
352 349 pdf.SetFontStyle('B',9)
353 350 pdf.RDMCell(35,5, l(:field_subject) + ":","LT")
354 351 pdf.SetFontStyle('',9)
355 352 pdf.RDMMultiCell(155,5, issue.subject,"RT")
356 353 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
357 354
358 355 pdf.SetFontStyle('B',9)
359 356 pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1)
360 357 pdf.SetFontStyle('',9)
361 358 pdf.RDMwriteHTMLCell(35+155, 5, 0, 0,
362 359 Redmine::WikiFormatting.to_html(
363 360 Setting.text_formatting, issue.description.to_s),"LRB")
364 361
365 362 # for CJK
366 363 truncate_length = ( l(:general_pdf_encoding).upcase == "UTF-8" ? 90 : 65 )
367 364
368 365 pdf.SetFontStyle('B',9)
369 366 pdf.RDMCell(35+155,5, l(:label_subtask_plural) + ":", "LTR")
370 367 pdf.Ln
371 368 issue_list(issue.descendants.sort_by(&:lft)) do |child, level|
372 369 buf = truncate("#{child.tracker} # #{child.id}: #{child.subject}",
373 370 :length => truncate_length)
374 371 level = 10 if level >= 10
375 372 pdf.SetFontStyle('',8)
376 373 pdf.RDMCell(35+135,5, (level >=1 ? " " * level : "") + buf, "L")
377 374 pdf.SetFontStyle('B',8)
378 375 pdf.RDMCell(20,5, child.status.to_s, "R")
379 376 pdf.Ln
380 377 end
381 378 pdf.SetFontStyle('B',9)
382 379 pdf.RDMCell(35+155,5, l(:label_related_issues) + ":", "LTR")
383 380 pdf.Ln
384 381
385 382 # for CJK
386 383 truncate_length = ( l(:general_pdf_encoding).upcase == "UTF-8" ? 80 : 60 )
387 384
388 385 issue.relations.select { |r| r.other_issue(issue).visible? }.each do |relation|
389 386 buf = ""
390 387 buf += "#{l(relation.label_for(issue))} "
391 388 if relation.delay && relation.delay != 0
392 389 buf += "(#{l('datetime.distance_in_words.x_days', :count => relation.delay)}) "
393 390 end
394 391 if Setting.cross_project_issue_relations?
395 392 buf += "#{relation.other_issue(issue).project} - "
396 393 end
397 394 buf += "#{relation.other_issue(issue).tracker}" +
398 395 " # #{relation.other_issue(issue).id}: #{relation.other_issue(issue).subject}"
399 396 buf = truncate(buf, :length => truncate_length)
400 397 pdf.SetFontStyle('', 8)
401 398 pdf.RDMCell(35+155-50,5, buf, "L")
402 399 pdf.SetFontStyle('B',8)
403 400 pdf.RDMCell(10,5, relation.other_issue(issue).status.to_s, "")
404 401 pdf.RDMCell(20,5, format_date(relation.other_issue(issue).start_date), "")
405 402 pdf.RDMCell(20,5, format_date(relation.other_issue(issue).due_date), "R")
406 403 pdf.Ln
407 404 end
408 405 pdf.RDMCell(190,5, "", "T")
409 406 pdf.Ln
410 407
411 408 if issue.changesets.any? &&
412 409 User.current.allowed_to?(:view_changesets, issue.project)
413 410 pdf.SetFontStyle('B',9)
414 411 pdf.RDMCell(190,5, l(:label_associated_revisions), "B")
415 412 pdf.Ln
416 413 for changeset in issue.changesets
417 414 pdf.SetFontStyle('B',8)
418 415 csstr = "#{l(:label_revision)} #{changeset.format_identifier} - "
419 416 csstr += format_time(changeset.committed_on) + " - " + changeset.author.to_s
420 417 pdf.RDMCell(190, 5, csstr)
421 418 pdf.Ln
422 419 unless changeset.comments.blank?
423 420 pdf.SetFontStyle('',8)
424 421 pdf.RDMwriteHTMLCell(190,5,0,0,
425 422 Redmine::WikiFormatting.to_html(
426 423 Setting.text_formatting, changeset.comments.to_s), "")
427 424 end
428 425 pdf.Ln
429 426 end
430 427 end
431 428
432 429 pdf.SetFontStyle('B',9)
433 430 pdf.RDMCell(190,5, l(:label_history), "B")
434 431 pdf.Ln
435 432 for journal in issue.journals.find(
436 433 :all, :include => [:user, :details],
437 434 :order => "#{Journal.table_name}.created_on ASC")
438 435 pdf.SetFontStyle('B',8)
439 436 pdf.RDMCell(190,5,
440 437 format_time(journal.created_on) + " - " + journal.user.name)
441 438 pdf.Ln
442 439 pdf.SetFontStyle('I',8)
443 440 for detail in journal.details
444 441 pdf.RDMMultiCell(190,5, "- " + show_detail(detail, true))
445 442 end
446 443 if journal.notes?
447 444 pdf.Ln unless journal.details.empty?
448 445 pdf.SetFontStyle('',8)
449 446 pdf.RDMwriteHTMLCell(190,5,0,0,
450 447 Redmine::WikiFormatting.to_html(
451 448 Setting.text_formatting, journal.notes.to_s), "")
452 449 end
453 450 pdf.Ln
454 451 end
455 452
456 453 if issue.attachments.any?
457 454 pdf.SetFontStyle('B',9)
458 455 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
459 456 pdf.Ln
460 457 for attachment in issue.attachments
461 458 pdf.SetFontStyle('',8)
462 459 pdf.RDMCell(80,5, attachment.filename)
463 460 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
464 461 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
465 462 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
466 463 pdf.Ln
467 464 end
468 465 end
469 466 pdf.Output
470 467 end
471 468
472 469 # Returns a PDF string of a single wiki page
473 470 def wiki_to_pdf(page, project)
474 471 pdf = ITCPDF.new(current_language)
475 472 pdf.SetTitle("#{project} - #{page.title}")
476 473 pdf.alias_nb_pages
477 474 pdf.footer_date = format_date(Date.today)
478 475 pdf.AddPage
479 476 pdf.SetFontStyle('B',11)
480 477 pdf.RDMMultiCell(190,5,
481 478 "#{project} - #{page.title} - # #{page.content.version}")
482 479 pdf.Ln
483 480 # Set resize image scale
484 481 pdf.SetImageScale(1.6)
485 482 pdf.SetFontStyle('',9)
486 483 pdf.RDMwriteHTMLCell(190,5,0,0,
487 484 Redmine::WikiFormatting.to_html(
488 485 Setting.text_formatting, page.content.text.to_s), "TLRB")
489 486 if page.attachments.any?
490 487 pdf.Ln
491 488 pdf.SetFontStyle('B',9)
492 489 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
493 490 pdf.Ln
494 491 for attachment in page.attachments
495 492 pdf.SetFontStyle('',8)
496 493 pdf.RDMCell(80,5, attachment.filename)
497 494 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
498 495 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
499 496 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
500 497 pdf.Ln
501 498 end
502 499 end
503 500 pdf.Output
504 501 end
505 502
506 503 class RDMPdfEncoding
507 504 include Redmine::I18n
508 def self.rdm_pdf_iconv(ic, txt)
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
542 513 end
543 514 end
544 515 end
545 516 end
@@ -1,120 +1,90
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 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_pdf_iconv(ic, utf8_txt_1)
52 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_2)
53 txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_3)
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
57 40 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
58 41 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
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_pdf_iconv(ic, utf8_txt_1)
45 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding)
63 46 assert_equal "???",
64 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_2)
47 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding)
65 48 assert_equal "????",
66 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_3)
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_pdf_iconv(ic, utf8_txt_1)
52 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding)
70 53 assert_equal "???\x91\xd4???",
71 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_2)
54 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding)
72 55 assert_equal "??????\x91\xd4???",
73 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_3)
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
92 70 end
93 71 assert_equal "Texte encod? en ISO-8859-1", txt_1
94 72 assert_equal "?a?b?c?d?e test", txt_2
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
116 86 end
117 87 assert_equal "Texte encod? en ISO-8859-1", txt_1
118 88 assert_equal "?a?b?c?d?e test", txt_2
119 89 end
120 90 end
General Comments 0
You need to be logged in to leave comments. Login now