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