##// END OF EJS Templates
Fixes Chinese pdf export when the issue description is too long (#1170)....
Jean-Philippe Lang -
r1483:f4aa04b96e4b
parent child
Show More
@@ -1,473 +1,473
1 1 # Copyright (c) 2006 4ssoM LLC <www.4ssoM.com>
2 2 # 1.12 contributed by Ed Moss.
3 3 #
4 4 # The MIT License
5 5 #
6 6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 7 # of this software and associated documentation files (the "Software"), to deal
8 8 # in the Software without restriction, including without limitation the rights
9 9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 10 # copies of the Software, and to permit persons to whom the Software is
11 11 # furnished to do so, subject to the following conditions:
12 12 #
13 13 # The above copyright notice and this permission notice shall be included in
14 14 # all copies or substantial portions of the Software.
15 15 #
16 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 22 # THE SOFTWARE.
23 23 #
24 24 # This is direct port of chinese.php
25 25 #
26 26 # Chinese PDF support.
27 27 #
28 28 # Usage is as follows:
29 29 #
30 30 # require 'fpdf'
31 31 # require 'chinese'
32 32 # pdf = FPDF.new
33 33 # pdf.extend(PDF_Chinese)
34 34 #
35 35 # This allows it to be combined with other extensions, such as the bookmark
36 36 # module.
37 37
38 38 module PDF_Chinese
39 39
40 40 Big5_widths={' '=>250,'!'=>250,'"'=>408,'#'=>668,''=>490,'%'=>875,'&'=>698,'\''=>250,
41 41 '('=>240,')'=>240,'*'=>417,'+'=>667,','=>250,'-'=>313,'.'=>250,'/'=>520,'0'=>500,'1'=>500,
42 42 '2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>250,''=>250,
43 43 '<'=>667,'='=>667,'>'=>667,'?'=>396,'@'=>921,'A'=>677,'B'=>615,'C'=>719,'D'=>760,'E'=>625,
44 44 'F'=>552,'G'=>771,'H'=>802,'I'=>354,'J'=>354,'K'=>781,'L'=>604,'M'=>927,'N'=>750,'O'=>823,
45 45 'P'=>563,'Q'=>823,'R'=>729,'S'=>542,'T'=>698,'U'=>771,'V'=>729,'W'=>948,'X'=>771,'Y'=>677,
46 46 'Z'=>635,'['=>344,'\\'=>520,']'=>344,'^'=>469,'_'=>500,'`'=>250,'a'=>469,'b'=>521,'c'=>427,
47 47 'd'=>521,'e'=>438,'f'=>271,'g'=>469,'h'=>531,'i'=>250,'j'=>250,'k'=>458,'l'=>240,'m'=>802,
48 48 'n'=>531,'o'=>500,'p'=>521,'q'=>521,'r'=>365,'s'=>333,'t'=>292,'u'=>521,'v'=>458,'w'=>677,
49 49 'x'=>479,'y'=>458,'z'=>427,'{'=>480,'|'=>496,'end'=>480,'~'=>667}
50 50
51 51 GB_widths={' '=>207,'!'=>270,'"'=>342,'#'=>467,''=>462,'%'=>797,'&'=>710,'\''=>239,
52 52 '('=>374,')'=>374,'*'=>423,'+'=>605,','=>238,'-'=>375,'.'=>238,'/'=>334,'0'=>462,'1'=>462,
53 53 '2'=>462,'3'=>462,'4'=>462,'5'=>462,'6'=>462,'7'=>462,'8'=>462,'9'=>462,':'=>238,''=>238,
54 54 '<'=>605,'='=>605,'>'=>605,'?'=>344,'@'=>748,'A'=>684,'B'=>560,'C'=>695,'D'=>739,'E'=>563,
55 55 'F'=>511,'G'=>729,'H'=>793,'I'=>318,'J'=>312,'K'=>666,'L'=>526,'M'=>896,'N'=>758,'O'=>772,
56 56 'P'=>544,'Q'=>772,'R'=>628,'S'=>465,'T'=>607,'U'=>753,'V'=>711,'W'=>972,'X'=>647,'Y'=>620,
57 57 'Z'=>607,'['=>374,'\\'=>333,']'=>374,'^'=>606,'_'=>500,'`'=>239,'a'=>417,'b'=>503,'c'=>427,
58 58 'd'=>529,'e'=>415,'f'=>264,'g'=>444,'h'=>518,'i'=>241,'j'=>230,'k'=>495,'l'=>228,'m'=>793,
59 59 'n'=>527,'o'=>524,'p'=>524,'q'=>504,'r'=>338,'s'=>336,'t'=>277,'u'=>517,'v'=>450,'w'=>652,
60 60 'x'=>466,'y'=>452,'z'=>407,'{'=>370,'|'=>258,'end'=>370,'~'=>605}
61 61
62 62 def AddCIDFont(family,style,name,cw,cMap,registry)
63 63 #ActionController::Base::logger.debug registry.to_a.join(":").to_s
64 64 fontkey=family.downcase+style.upcase
65 65 unless @fonts[fontkey].nil?
66 66 Error("Font already added: family style")
67 67 end
68 68 i=@fonts.length+1
69 69 name=name.gsub(' ','')
70 70 @fonts[fontkey]={'i'=>i,'type'=>'Type0','name'=>name,'up'=>-130,'ut'=>40,'cw'=>cw, 'CMap'=>cMap,'registry'=>registry}
71 71 end
72 72
73 73 def AddCIDFonts(family,name,cw,cMap,registry)
74 74 AddCIDFont(family,'',name,cw,cMap,registry)
75 75 AddCIDFont(family,'B',name+',Bold',cw,cMap,registry)
76 76 AddCIDFont(family,'I',name+',Italic',cw,cMap,registry)
77 77 AddCIDFont(family,'BI',name+',BoldItalic',cw,cMap,registry)
78 78 end
79 79
80 80 def AddBig5Font(family='Big5',name='MSungStd-Light-Acro')
81 81 #Add Big5 font with proportional Latin
82 82 cw=Big5_widths
83 83 cMap='ETenms-B5-H'
84 84 registry={'ordering'=>'CNS1','supplement'=>0}
85 85 #ActionController::Base::logger.debug registry.to_a.join(":").to_s
86 86 AddCIDFonts(family,name,cw,cMap,registry)
87 87 end
88 88
89 89 def AddBig5hwFont(family='Big5-hw',name='MSungStd-Light-Acro')
90 90 #Add Big5 font with half-witdh Latin
91 91 cw = {}
92 92 32.upto(126) do |i|
93 93 cw[i.chr]=500
94 94 end
95 95 cMap='ETen-B5-H'
96 96 registry={'ordering'=>'CNS1','supplement'=>0}
97 97 AddCIDFonts(family,name,cw,cMap,registry)
98 98 end
99 99
100 100 def AddGBFont(family='GB',name='STSongStd-Light-Acro')
101 101 #Add GB font with proportional Latin
102 102 cw=GB_widths
103 103 cMap='GBKp-EUC-H'
104 104 registry={'ordering'=>'GB1','supplement'=>2}
105 105 AddCIDFonts(family,name,cw,cMap,registry)
106 106 end
107 107
108 108 def AddGBhwFont(family='GB-hw',name='STSongStd-Light-Acro')
109 109 #Add GB font with half-width Latin
110 110 32.upto(126) do |i|
111 111 cw[i.chr]=500
112 112 end
113 113 cMap='GBK-EUC-H'
114 114 registry={'ordering'=>'GB1','supplement'=>2}
115 115 AddCIDFonts(family,name,cw,cMap,registry)
116 116 end
117 117
118 118 def GetStringWidth(s)
119 119 if(@CurrentFont['type']=='Type0')
120 120 return GetMBStringWidth(s)
121 121 else
122 122 return super(s)
123 123 end
124 124 end
125 125
126 126 def GetMBStringWidth(s)
127 127 #Multi-byte version of GetStringWidth()
128 128 l=0
129 129 cw=@CurrentFont['cw']
130 130 nb=s.length
131 131 i=0
132 132 while(i<nb)
133 133 c=s[i]
134 134 if(c<128)
135 135 l+=cw[c.chr] if cw[c.chr]
136 136 i+=1
137 137 else
138 138 l+=1000
139 139 i+=2
140 140 end
141 141 end
142 142 return l*@FontSize/1000
143 143 end
144 144
145 145 def MultiCell(w,h,txt,border=0,align='L',fill=0)
146 146 if(@CurrentFont['type']=='Type0')
147 147 MBMultiCell(w,h,txt,border,align,fill)
148 148 else
149 149 super(w,h,txt,border,align,fill)
150 150 end
151 151 end
152 152
153 153 def MBMultiCell(w,h,txt,border=0,align='L',fill=0)
154 154 #Multi-byte version of MultiCell()
155 155 cw=@CurrentFont['cw']
156 156 if(w==0)
157 157 w=@w-@rMargin-@x
158 158 end
159 159 wmax=(w-2*@cMargin)*1000/@FontSize
160 160 s=txt.gsub("\r",'')
161 161 nb=s.length
162 162 if(nb>0 and s[nb-1]=="\n")
163 163 nb-=1
164 164 end
165 165 b=0
166 166 if(border)
167 167 if(border==1)
168 168 border='LTRB'
169 169 b='LRT'
170 170 b2='LR'
171 171 else
172 172 b2=''
173 173 if(border.to_s.index('L'))
174 174 b2+='L'
175 175 end
176 176 if(border.to_s.index('R'))
177 177 b2+='R'
178 178 end
179 179 b=border.to_s.index('T') ? b2+'T' : b2
180 180 end
181 181 end
182 182 sep=-1
183 183 i=0
184 184 j=0
185 185 l=0
186 186 nl=1
187 187 while(i<nb)
188 188 #Get next character
189 189 c=s[i]
190 190 #Check if ASCII or MB
191 191 ascii=(c<128)
192 if(c=="\n")
192 if(c.chr=="\n")
193 193 #Explicit line break
194 194 Cell(w,h,s[j,i-j],b,2,align,fill)
195 195 i+=1
196 196 sep=-1
197 197 j=i
198 198 l=0
199 199 nl+=1
200 200 if(border and nl==2)
201 201 b=b2
202 202 end
203 203 next
204 204 end
205 205 if(!ascii)
206 206 sep=i
207 207 ls=l
208 208 elsif(c==' ')
209 209 sep=i
210 210 ls=l
211 211 end
212 l+=ascii ? (cw[c.chr] || 0) : 1000
212 l+=ascii ? (cw[c.chr] || 0) : 1100
213 213 if(l>wmax)
214 214 #Automatic line break
215 215 if(sep==-1 or i==j)
216 216 if(i==j)
217 i+=ascii ? 1 : 2
217 i+=ascii ? 1 : 3
218 218 end
219 219 Cell(w,h,s[j,i-j],b,2,align,fill)
220 220 else
221 221 Cell(w,h,s[j,sep-j],b,2,align,fill)
222 222 i=(s[sep]==' ') ? sep+1 : sep
223 223 end
224 224 sep=-1
225 225 j=i
226 226 l=0
227 227 # nl+=1
228 228 if(border and nl==2)
229 229 b=b2
230 230 end
231 231 else
232 i+=ascii ? 1 : 2
232 i+=ascii ? 1 : 3
233 233 end
234 234 end
235 235 #Last chunk
236 236 if(border and not border.to_s.index('B').nil?)
237 237 b+='B'
238 238 end
239 239 Cell(w,h,s[j,i-j],b,2,align,fill)
240 240 @x=@lMargin
241 241 end
242 242
243 243 def Write(h,txt,link='')
244 244 if(@CurrentFont['type']=='Type0')
245 245 MBWrite(h,txt,link)
246 246 else
247 247 super(h,txt,link)
248 248 end
249 249 end
250 250
251 251 def MBWrite(h,txt,link)
252 252 #Multi-byte version of Write()
253 253 cw=@CurrentFont['cw']
254 254 w=@w-@rMargin-@x
255 255 wmax=(w-2*@cMargin)*1000/@FontSize
256 256 s=txt.gsub("\r",'')
257 257 nb=s.length
258 258 sep=-1
259 259 i=0
260 260 j=0
261 261 l=0
262 262 nl=1
263 263 while(i<nb)
264 264 #Get next character
265 265 c=s[i]
266 266 #Check if ASCII or MB
267 267 ascii=(c<128)
268 if(c=="\n")
268 if(c.chr=="\n")
269 269 #Explicit line break
270 270 Cell(w,h,s[j,i-j],0,2,'',0,link)
271 271 i+=1
272 272 sep=-1
273 273 j=i
274 274 l=0
275 275 if(nl==1)
276 276 @x=@lMargin
277 277 w=@w-@rMargin-@x
278 278 wmax=(w-2*@cMargin)*1000/@FontSize
279 279 end
280 280 nl+=1
281 281 next
282 282 end
283 283 if(!ascii or c==' ')
284 284 sep=i
285 285 end
286 l+=ascii ? cw[c.chr] : 1000
286 l+=ascii ? cw[c.chr] : 1100
287 287 if(l>wmax)
288 288 #Automatic line break
289 289 if(sep==-1 or i==j)
290 290 if(@x>@lMargin)
291 291 #Move to next line
292 292 @x=@lMargin
293 293 @y+=h
294 294 w=@w-@rMargin-@x
295 295 wmax=(w-2*@cMargin)*1000/@FontSize
296 296 i+=1
297 297 nl+=1
298 298 next
299 299 end
300 300 if(i==j)
301 i+=ascii ? 1 : 2
301 i+=ascii ? 1 : 3
302 302 end
303 303 Cell(w,h,s[j,i-j],0,2,'',0,link)
304 304 else
305 305 Cell(w,h,s[j,sep-j],0,2,'',0,link)
306 306 i=(s[sep]==' ') ? sep+1 : sep
307 307 end
308 308 sep=-1
309 309 j=i
310 310 l=0
311 311 if(nl==1)
312 312 @x=@lMargin
313 313 w=@w-@rMargin-@x
314 314 wmax=(w-2*@cMargin)*1000/@FontSize
315 315 end
316 316 nl+=1
317 317 else
318 i+=ascii ? 1 : 2
318 i+=ascii ? 1 : 3
319 319 end
320 320 end
321 321 #Last chunk
322 322 if(i!=j)
323 323 Cell(l/1000*@FontSize,h,s[j,i-j],0,0,'',0,link)
324 324 end
325 325 end
326 326
327 327 private
328 328
329 329 def putfonts()
330 330 nf=@n
331 331 @diffs.each do |diff|
332 332 #Encodings
333 333 newobj()
334 334 out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['+diff+']>>')
335 335 out('endobj')
336 336 end
337 337 # mqr=get_magic_quotes_runtime()
338 338 # set_magic_quotes_runtime(0)
339 339 @FontFiles.each_pair do |file, info|
340 340 #Font file embedding
341 341 newobj()
342 342 @FontFiles[file]['n']=@n
343 343 if(defined('FPDF_FONTPATH'))
344 344 file=FPDF_FONTPATH+file
345 345 end
346 346 size=filesize(file)
347 347 if(!size)
348 348 Error('Font file not found')
349 349 end
350 350 out('<</Length '+size)
351 351 if(file[-2]=='.z')
352 352 out('/Filter /FlateDecode')
353 353 end
354 354 out('/Length1 '+info['length1'])
355 355 unless info['length2'].nil?
356 356 out('/Length2 '+info['length2']+' /Length3 0')
357 357 end
358 358 out('>>')
359 359 f=fopen(file,'rb')
360 360 putstream(fread(f,size))
361 361 fclose(f)
362 362 out('endobj')
363 363 end
364 364 #
365 365 # set_magic_quotes_runtime(mqr)
366 366 #
367 367 @fonts.each_pair do |k, font|
368 368 #Font objects
369 369 newobj()
370 370 @fonts[k]['n']=@n
371 371 out('<</Type /Font')
372 372 if(font['type']=='Type0')
373 373 putType0(font)
374 374 else
375 375 name=font['name']
376 376 out('/BaseFont /'+name)
377 377 if(font['type']=='core')
378 378 #Standard font
379 379 out('/Subtype /Type1')
380 380 if(name!='Symbol' and name!='ZapfDingbats')
381 381 out('/Encoding /WinAnsiEncoding')
382 382 end
383 383 else
384 384 #Additional font
385 385 out('/Subtype /'+font['type'])
386 386 out('/FirstChar 32')
387 387 out('/LastChar 255')
388 388 out('/Widths '+(@n+1)+' 0 R')
389 389 out('/FontDescriptor '+(@n+2)+' 0 R')
390 390 if(font['enc'])
391 391 if !font['diff'].nil?
392 392 out('/Encoding '+(nf+font['diff'])+' 0 R')
393 393 else
394 394 out('/Encoding /WinAnsiEncoding')
395 395 end
396 396 end
397 397 end
398 398 out('>>')
399 399 out('endobj')
400 400 if(font['type']!='core')
401 401 #Widths
402 402 newobj()
403 403 cw=font['cw']
404 404 s='['
405 405 32.upto(255) do |i|
406 406 s+=cw[i.chr]+' '
407 407 end
408 408 out(s+']')
409 409 out('endobj')
410 410 #Descriptor
411 411 newobj()
412 412 s='<</Type /FontDescriptor /FontName /'+name
413 413 font['desc'].each_pair do |k, v|
414 414 s+=' /'+k+' '+v
415 415 end
416 416 file=font['file']
417 417 if(file)
418 418 s+=' /FontFile'+(font['type']=='Type1' ? '' : '2')+' '+@FontFiles[file]['n']+' 0 R'
419 419 end
420 420 out(s+'>>')
421 421 out('endobj')
422 422 end
423 423 end
424 424 end
425 425 end
426 426
427 427 def putType0(font)
428 428 #Type0
429 429 out('/Subtype /Type0')
430 430 out('/BaseFont /'+font['name']+'-'+font['CMap'])
431 431 out('/Encoding /'+font['CMap'])
432 432 out('/DescendantFonts ['+(@n+1).to_s+' 0 R]')
433 433 out('>>')
434 434 out('endobj')
435 435 #CIDFont
436 436 newobj()
437 437 out('<</Type /Font')
438 438 out('/Subtype /CIDFontType0')
439 439 out('/BaseFont /'+font['name'])
440 440 out('/CIDSystemInfo <</Registry '+textstring('Adobe')+' /Ordering '+textstring(font['registry']['ordering'])+' /Supplement '+font['registry']['supplement'].to_s+'>>')
441 441 out('/FontDescriptor '+(@n+1).to_s+' 0 R')
442 442 if(font['CMap']=='ETen-B5-H')
443 443 w='13648 13742 500'
444 444 elsif(font['CMap']=='GBK-EUC-H')
445 445 w='814 907 500 7716 [500]'
446 446 else
447 447 # ActionController::Base::logger.debug font['cw'].keys.sort.join(' ').to_s
448 448 # ActionController::Base::logger.debug font['cw'].values.join(' ').to_s
449 449 w='1 ['
450 450 font['cw'].keys.sort.each {|key|
451 451 w+=font['cw'][key].to_s + " "
452 452 # ActionController::Base::logger.debug key.to_s
453 453 # ActionController::Base::logger.debug font['cw'][key].to_s
454 454 }
455 455 w +=']'
456 456 end
457 457 out('/W ['+w+']>>')
458 458 out('endobj')
459 459 #Font descriptor
460 460 newobj()
461 461 out('<</Type /FontDescriptor')
462 462 out('/FontName /'+font['name'])
463 463 out('/Flags 6')
464 464 out('/FontBBox [0 -200 1000 900]')
465 465 out('/ItalicAngle 0')
466 466 out('/Ascent 800')
467 467 out('/Descent -200')
468 468 out('/CapHeight 800')
469 469 out('/StemV 50')
470 470 out('>>')
471 471 out('endobj')
472 472 end
473 473 end
General Comments 0
You need to be logged in to leave comments. Login now