##// END OF EJS Templates
PDF: add dummy 'fill' parameter at rfpdf CJK Write method (#8737)....
Toshi MARUYAMA -
r6036:edc4f6d54b99
parent child
Show More
@@ -1,469 +1,469
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,'}'=>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,'}'=>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(@current_font['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=@current_font['cw']
130 130 nb=s.length
131 131 i=0
132 132 while(i<nb)
133 133 c = s[i].is_a?(String) ? s[i].ord : 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*@font_size/1000
143 143 end
144 144
145 145 def MultiCell(w,h,txt,border=0,align='L',fill=0,ln=1)
146 146 if(@current_font['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=@current_font['cw']
156 156 if(w==0)
157 157 w=@w-@r_margin-@x
158 158 end
159 159 wmax=(w-2*@c_margin)*1000/@font_size
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 b2='L' unless border.to_s.index('L').nil?
174 174 b2=b2+'R' unless border.to_s.index('R').nil?
175 175 b=(border.to_s.index('T')) ? (b2+'T') : b2
176 176 end
177 177 end
178 178 sep=-1
179 179 i=0
180 180 j=0
181 181 l=0
182 182 nl=1
183 183 while(i<nb)
184 184 #Get next character
185 185 c = s[i].is_a?(String) ? s[i].ord : s[i]
186 186 #Check if ASCII or MB
187 187 ascii=(c<128)
188 188 if(c.chr=="\n")
189 189 #Explicit line break
190 190 Cell(w,h,s[j,i-j],b,2,align,fill)
191 191 i+=1
192 192 sep=-1
193 193 j=i
194 194 l=0
195 195 nl+=1
196 196 if(border and nl==2)
197 197 b=b2
198 198 end
199 199 next
200 200 end
201 201 if(!ascii)
202 202 sep=i
203 203 ls=l
204 204 elsif(c.chr==' ')
205 205 sep=i
206 206 ls=l
207 207 end
208 208 l+=(ascii ? cw[c.chr] : 1000) || 0
209 209 if(l>wmax)
210 210 #Automatic line break
211 211 if(sep==-1 or i==j)
212 212 if(i==j)
213 213 i+=ascii ? 1 : 2
214 214 end
215 215 Cell(w,h,s[j,i-j],b,2,align,fill)
216 216 else
217 217 Cell(w,h,s[j,sep-j],b,2,align,fill)
218 218 i=(s[sep].chr==' ') ? sep+1 : sep
219 219 end
220 220 sep=-1
221 221 j=i
222 222 l=0
223 223 nl+=1
224 224 if(border and nl==2)
225 225 b=b2
226 226 end
227 227 else
228 228 i+=ascii ? 1 : 2
229 229 end
230 230 end
231 231 #Last chunk
232 232 if(border and not border.to_s.index('B').nil?)
233 233 b+='B'
234 234 end
235 235 Cell(w,h,s[j,i-j],b,2,align,fill)
236 236 @x=@l_margin
237 237 end
238 238
239 def Write(h,txt,link='')
239 def Write(h,txt,link='',fill=0)
240 240 if(@current_font['type']=='Type0')
241 241 MBWrite(h,txt,link)
242 242 else
243 243 super(h,txt,link)
244 244 end
245 245 end
246 246
247 247 def MBWrite(h,txt,link)
248 248 #Multi-byte version of Write()
249 249 cw=@current_font['cw']
250 250 w=@w-@r_margin-@x
251 251 wmax=(w-2*@c_margin)*1000/@font_size
252 252 s=txt.gsub("\r",'')
253 253 nb=s.length
254 254 sep=-1
255 255 i=0
256 256 j=0
257 257 l=0
258 258 nl=1
259 259 while(i<nb)
260 260 #Get next character
261 261 c = s[i].is_a?(String) ? s[i].ord : s[i]
262 262 #Check if ASCII or MB
263 263 ascii=(c<128)
264 264 if(c.chr=="\n")
265 265 #Explicit line break
266 266 Cell(w,h,s[j,i-j],0,2,'',0,link)
267 267 i+=1
268 268 sep=-1
269 269 j=i
270 270 l=0
271 271 if(nl==1)
272 272 @x=@l_margin
273 273 w=@w-@r_margin-@x
274 274 wmax=(w-2*@c_margin)*1000/@font_size
275 275 end
276 276 nl+=1
277 277 next
278 278 end
279 279 if(!ascii or c.chr==' ')
280 280 sep=i
281 281 end
282 282 l+=(ascii ? cw[c.chr] : 1000) || 0
283 283 if(l>wmax)
284 284 #Automatic line break
285 285 if(sep==-1 or i==j)
286 286 if(@x>@l_margin)
287 287 #Move to next line
288 288 @x=@l_margin
289 289 @y+=h
290 290 w=@w-@r_margin-@x
291 291 wmax=(w-2*@c_margin)*1000/@font_size
292 292 i+=1
293 293 nl+=1
294 294 next
295 295 end
296 296 if(i==j)
297 297 i+=ascii ? 1 : 2
298 298 end
299 299 Cell(w,h,s[j,i-j],0,2,'',0,link)
300 300 else
301 301 Cell(w,h,s[j,sep-j],0,2,'',0,link)
302 302 i=(s[sep].chr==' ') ? sep+1 : sep
303 303 end
304 304 sep=-1
305 305 j=i
306 306 l=0
307 307 if(nl==1)
308 308 @x=@l_margin
309 309 w=@w-@r_margin-@x
310 310 wmax=(w-2*@c_margin)*1000/@font_size
311 311 end
312 312 nl+=1
313 313 else
314 314 i+=ascii ? 1 : 2
315 315 end
316 316 end
317 317 #Last chunk
318 318 if(i!=j)
319 319 Cell(l/1000*@font_size,h,s[j,i-j],0,0,'',0,link)
320 320 end
321 321 end
322 322
323 323 private
324 324
325 325 def putfonts()
326 326 nf=@n
327 327 @diffs.each do |diff|
328 328 #Encodings
329 329 newobj()
330 330 out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['+diff+']>>')
331 331 out('endobj')
332 332 end
333 333 # mqr=get_magic_quotes_runtime()
334 334 # set_magic_quotes_runtime(0)
335 335 @font_files.each_pair do |file, info|
336 336 #Font file embedding
337 337 newobj()
338 338 @font_files[file]['n']=@n
339 339 if(defined('FPDF_FONTPATH'))
340 340 file=FPDF_FONTPATH+file
341 341 end
342 342 size=filesize(file)
343 343 if(!size)
344 344 Error('Font file not found')
345 345 end
346 346 out('<</Length '+size)
347 347 if(file[-2]=='.z')
348 348 out('/Filter /FlateDecode')
349 349 end
350 350 out('/Length1 '+info['length1'])
351 351 unless info['length2'].nil?
352 352 out('/Length2 '+info['length2']+' /Length3 0')
353 353 end
354 354 out('>>')
355 355 f=fopen(file,'rb')
356 356 putstream(fread(f,size))
357 357 fclose(f)
358 358 out('endobj')
359 359 end
360 360 #
361 361 # set_magic_quotes_runtime(mqr)
362 362 #
363 363 @fonts.each_pair do |k, font|
364 364 #Font objects
365 365 newobj()
366 366 @fonts[k]['n']=@n
367 367 out('<</Type /Font')
368 368 if(font['type']=='Type0')
369 369 putType0(font)
370 370 else
371 371 name=font['name']
372 372 out('/BaseFont /'+name)
373 373 if(font['type']=='core')
374 374 #Standard font
375 375 out('/Subtype /Type1')
376 376 if(name!='Symbol' and name!='ZapfDingbats')
377 377 out('/Encoding /WinAnsiEncoding')
378 378 end
379 379 else
380 380 #Additional font
381 381 out('/Subtype /'+font['type'])
382 382 out('/FirstChar 32')
383 383 out('/LastChar 255')
384 384 out('/Widths '+(@n+1)+' 0 R')
385 385 out('/FontDescriptor '+(@n+2)+' 0 R')
386 386 if(font['enc'])
387 387 if !font['diff'].nil?
388 388 out('/Encoding '+(nf+font['diff'])+' 0 R')
389 389 else
390 390 out('/Encoding /WinAnsiEncoding')
391 391 end
392 392 end
393 393 end
394 394 out('>>')
395 395 out('endobj')
396 396 if(font['type']!='core')
397 397 #Widths
398 398 newobj()
399 399 cw=font['cw']
400 400 s='['
401 401 32.upto(255) do |i|
402 402 s+=cw[i.chr]+' '
403 403 end
404 404 out(s+']')
405 405 out('endobj')
406 406 #Descriptor
407 407 newobj()
408 408 s='<</Type /FontDescriptor /FontName /'+name
409 409 font['desc'].each_pair do |k, v|
410 410 s+=' /'+k+' '+v
411 411 end
412 412 file=font['file']
413 413 if(file)
414 414 s+=' /FontFile'+(font['type']=='Type1' ? '' : '2')+' '+@font_files[file]['n']+' 0 R'
415 415 end
416 416 out(s+'>>')
417 417 out('endobj')
418 418 end
419 419 end
420 420 end
421 421 end
422 422
423 423 def putType0(font)
424 424 #Type0
425 425 out('/Subtype /Type0')
426 426 out('/BaseFont /'+font['name']+'-'+font['CMap'])
427 427 out('/Encoding /'+font['CMap'])
428 428 out('/DescendantFonts ['+(@n+1).to_s+' 0 R]')
429 429 out('>>')
430 430 out('endobj')
431 431 #CIDFont
432 432 newobj()
433 433 out('<</Type /Font')
434 434 out('/Subtype /CIDFontType0')
435 435 out('/BaseFont /'+font['name'])
436 436 out('/CIDSystemInfo <</Registry '+textstring('Adobe')+' /Ordering '+textstring(font['registry']['ordering'])+' /Supplement '+font['registry']['supplement'].to_s+'>>')
437 437 out('/FontDescriptor '+(@n+1).to_s+' 0 R')
438 438 if(font['CMap']=='ETen-B5-H')
439 439 w='13648 13742 500'
440 440 elsif(font['CMap']=='GBK-EUC-H')
441 441 w='814 907 500 7716 [500]'
442 442 else
443 443 # ActionController::Base::logger.debug font['cw'].keys.sort.join(' ').to_s
444 444 # ActionController::Base::logger.debug font['cw'].values.join(' ').to_s
445 445 w='1 ['
446 446 font['cw'].keys.sort.each {|key|
447 447 w+=font['cw'][key].to_s + " "
448 448 # ActionController::Base::logger.debug key.to_s
449 449 # ActionController::Base::logger.debug font['cw'][key].to_s
450 450 }
451 451 w +=']'
452 452 end
453 453 out('/W ['+w+']>>')
454 454 out('endobj')
455 455 #Font descriptor
456 456 newobj()
457 457 out('<</Type /FontDescriptor')
458 458 out('/FontName /'+font['name'])
459 459 out('/Flags 6')
460 460 out('/FontBBox [0 -200 1000 900]')
461 461 out('/ItalicAngle 0')
462 462 out('/Ascent 800')
463 463 out('/Descent -200')
464 464 out('/CapHeight 800')
465 465 out('/StemV 50')
466 466 out('>>')
467 467 out('endobj')
468 468 end
469 469 end
@@ -1,464 +1,464
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 japanese.php
25 25 #
26 26 # Japanese 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_Japanese)
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_Japanese
39 39
40 40 SJIS_widths={' ' => 278, '!' => 299, '"' => 353, '#' => 614, '$' => 614, '%' => 721, '&' => 735, '\'' => 216,
41 41 '(' => 323, ')' => 323, '*' => 449, '+' => 529, ',' => 219, '-' => 306, '.' => 219, '/' => 453, '0' => 614, '1' => 614,
42 42 '2' => 614, '3' => 614, '4' => 614, '5' => 614, '6' => 614, '7' => 614, '8' => 614, '9' => 614, ':' => 219, ';' => 219,
43 43 '<' => 529, '=' => 529, '>' => 529, '?' => 486, '@' => 744, 'A' => 646, 'B' => 604, 'C' => 617, 'D' => 681, 'E' => 567,
44 44 'F' => 537, 'G' => 647, 'H' => 738, 'I' => 320, 'J' => 433, 'K' => 637, 'L' => 566, 'M' => 904, 'N' => 710, 'O' => 716,
45 45 'P' => 605, 'Q' => 716, 'R' => 623, 'S' => 517, 'T' => 601, 'U' => 690, 'V' => 668, 'W' => 990, 'X' => 681, 'Y' => 634,
46 46 'Z' => 578, '[' => 316, '\\' => 614, ']' => 316, '^' => 529, '_' => 500, '`' => 387, 'a' => 509, 'b' => 566, 'c' => 478,
47 47 'd' => 565, 'e' => 503, 'f' => 337, 'g' => 549, 'h' => 580, 'i' => 275, 'j' => 266, 'k' => 544, 'l' => 276, 'm' => 854,
48 48 'n' => 579, 'o' => 550, 'p' => 578, 'q' => 566, 'r' => 410, 's' => 444, 't' => 340, 'u' => 575, 'v' => 512, 'w' => 760,
49 49 'x' => 503, 'y' => 529, 'z' => 453, '{' => 326, '|' => 380, '}' => 326, '~' => 387}
50 50
51 51 def AddCIDFont(family,style,name,cw,cMap,registry)
52 52 fontkey=family.downcase+style.upcase
53 53 unless @fonts[fontkey].nil?
54 54 Error("CID font already added: family style")
55 55 end
56 56 i=@fonts.length+1
57 57 @fonts[fontkey]={'i'=>i,'type'=>'Type0','name'=>name,'up'=>-120,'ut'=>40,'cw'=>cw,
58 58 'CMap'=>cMap,'registry'=>registry}
59 59 end
60 60
61 61 def AddCIDFonts(family,name,cw,cMap,registry)
62 62 AddCIDFont(family,'',name,cw,cMap,registry)
63 63 AddCIDFont(family,'B',name+',Bold',cw,cMap,registry)
64 64 AddCIDFont(family,'I',name+',Italic',cw,cMap,registry)
65 65 AddCIDFont(family,'BI',name+',BoldItalic',cw,cMap,registry)
66 66 end
67 67
68 68 def AddSJISFont(family='SJIS')
69 69 #Add SJIS font with proportional Latin
70 70 name='KozMinPro-Regular-Acro'
71 71 cw=SJIS_widths
72 72 cMap='90msp-RKSJ-H'
73 73 registry={'ordering'=>'Japan1','supplement'=>2}
74 74 AddCIDFonts(family,name,cw,cMap,registry)
75 75 end
76 76
77 77 def AddSJIShwFont(family='SJIS-hw')
78 78 #Add SJIS font with half-width Latin
79 79 name='KozMinPro-Regular-Acro'
80 80 32.upto(126) do |i|
81 81 cw[i.chr]=500
82 82 end
83 83 cMap='90ms-RKSJ-H'
84 84 registry={'ordering'=>'Japan1','supplement'=>2}
85 85 AddCIDFonts(family,name,cw,cMap,registry)
86 86 end
87 87
88 88 def GetStringWidth(s)
89 89 if(@current_font['type']=='Type0')
90 90 return GetSJISStringWidth(s)
91 91 else
92 92 return super(s)
93 93 end
94 94 end
95 95
96 96 def GetSJISStringWidth(s)
97 97 #SJIS version of GetStringWidth()
98 98 l=0
99 99 cw=@current_font['cw']
100 100 nb=s.length
101 101 i=0
102 102 while(i<nb)
103 103 o = s[i].is_a?(String) ? s[i].ord : s[i]
104 104 if(o<128)
105 105 #ASCII
106 106 l+=cw[o.chr] if cw[o.chr]
107 107 i+=1
108 108 elsif(o>=161 and o<=223)
109 109 #Half-width katakana
110 110 l+=500
111 111 i+=1
112 112 else
113 113 #Full-width character
114 114 l+=1000
115 115 i+=2
116 116 end
117 117 end
118 118 return l*@font_size/1000
119 119 end
120 120
121 121 def MultiCell(w,h,txt,border=0,align='L',fill=0,ln=1)
122 122 if(@current_font['type']=='Type0')
123 123 SJISMultiCell(w,h,txt,border,align,fill)
124 124 else
125 125 super(w,h,txt,border,align,fill)
126 126 end
127 127 end
128 128
129 129 def SJISMultiCell(w,h,txt,border=0,align='L',fill=0)
130 130 #Output text with automatic or explicit line breaks
131 131 cw=@current_font['cw']
132 132 if(w==0)
133 133 w=@w-@r_margin-@x
134 134 end
135 135 wmax=(w-2*@c_margin)*1000/@font_size
136 136 s=txt.gsub("\r",'')
137 137 nb=s.length
138 138 if(nb>0 and s[nb-1]=="\n")
139 139 nb-=1
140 140 end
141 141 b=0
142 142 if(border)
143 143 if(border==1)
144 144 border='LTRB'
145 145 b='LRT'
146 146 b2='LR'
147 147 else
148 148 b2=''
149 149 b2='L' unless border.to_s.index('L').nil?
150 150 b2=b2+'R' unless border.to_s.index('R').nil?
151 151 b=(border.to_s.index('T')) ? (b2+'T') : b2
152 152 end
153 153 end
154 154 sep=-1
155 155 i=0
156 156 j=0
157 157 l=0
158 158 nl=1
159 159 while(i<nb)
160 160 #Get next character
161 161 c = s[i].is_a?(String) ? s[i].ord : s[i]
162 162 o=c #o=ord(c)
163 163 if(o==10)
164 164 #Explicit line break
165 165 Cell(w,h,s[j,i-j],b,2,align,fill)
166 166 i+=1
167 167 sep=-1
168 168 j=i
169 169 l=0
170 170 nl+=1
171 171 if(border and nl==2)
172 172 b=b2
173 173 end
174 174 next
175 175 end
176 176 if(o<128)
177 177 #ASCII
178 178 l+=cw[c.chr] || 0
179 179 n=1
180 180 if(o==32)
181 181 sep=i
182 182 end
183 183 elsif(o>=161 and o<=223)
184 184 #Half-width katakana
185 185 l+=500
186 186 n=1
187 187 sep=i
188 188 else
189 189 #Full-width character
190 190 l+=1000
191 191 n=2
192 192 sep=i
193 193 end
194 194 if(l>wmax)
195 195 #Automatic line break
196 196 if(sep==-1 or i==j)
197 197 if(i==j)
198 198 i+=n
199 199 end
200 200 Cell(w,h,s[j,i-j],b,2,align,fill)
201 201 else
202 202 Cell(w,h,s[j,sep-j],b,2,align,fill)
203 203 i=(s[sep].chr==' ') ? sep+1 : sep
204 204 end
205 205 sep=-1
206 206 j=i
207 207 l=0
208 208 nl+=1
209 209 if(border and nl==2)
210 210 b=b2
211 211 end
212 212 else
213 213 i+=n
214 214 if(o>=128)
215 215 sep=i
216 216 end
217 217 end
218 218 end
219 219 #Last chunk
220 220 if(border and not border.to_s.index('B').nil?)
221 221 b+='B'
222 222 end
223 223 Cell(w,h,s[j,i-j],b,2,align,fill)
224 224 @x=@l_margin
225 225 end
226 226
227 def Write(h,txt,link='')
227 def Write(h,txt,link='',fill=0)
228 228 if(@current_font['type']=='Type0')
229 229 SJISWrite(h,txt,link)
230 230 else
231 231 super(h,txt,link)
232 232 end
233 233 end
234 234
235 235 def SJISWrite(h,txt,link)
236 236 #SJIS version of Write()
237 237 cw=@current_font['cw']
238 238 w=@w-@r_margin-@x
239 239 wmax=(w-2*@c_margin)*1000/@font_size
240 240 s=txt.gsub("\r",'')
241 241 nb=s.length
242 242 sep=-1
243 243 i=0
244 244 j=0
245 245 l=0
246 246 nl=1
247 247 while(i<nb)
248 248 #Get next character
249 249 c = s[i].is_a?(String) ? s[i].ord : s[i]
250 250 o=c
251 251 if(o==10)
252 252 #Explicit line break
253 253 Cell(w,h,s[j,i-j],0,2,'',0,link)
254 254 i+=1
255 255 sep=-1
256 256 j=i
257 257 l=0
258 258 if(nl==1)
259 259 #Go to left margin
260 260 @x=@l_margin
261 261 w=@w-@r_margin-@x
262 262 wmax=(w-2*@c_margin)*1000/@font_size
263 263 end
264 264 nl+=1
265 265 next
266 266 end
267 267 if(o<128)
268 268 #ASCII
269 269 l+=cw[c.chr] || 0
270 270 n=1
271 271 if(o==32)
272 272 sep=i
273 273 end
274 274 elsif(o>=161 and o<=223)
275 275 #Half-width katakana
276 276 l+=500
277 277 n=1
278 278 sep=i
279 279 else
280 280 #Full-width character
281 281 l+=1000
282 282 n=2
283 283 sep=i
284 284 end
285 285 if(l>wmax)
286 286 #Automatic line break
287 287 if(sep==-1 or i==j)
288 288 if(@x>@l_margin)
289 289 #Move to next line
290 290 @x=@l_margin
291 291 @y+=h
292 292 w=@w-@r_margin-@x
293 293 wmax=(w-2*@c_margin)*1000/@font_size
294 294 i+=n
295 295 nl+=1
296 296 next
297 297 end
298 298 if(i==j)
299 299 i+=n
300 300 end
301 301 Cell(w,h,s[j,i-j],0,2,'',0,link)
302 302 else
303 303 Cell(w,h,s[j,sep-j],0,2,'',0,link)
304 304 i=(s[sep].chr==' ') ? sep+1 : sep
305 305 end
306 306 sep=-1
307 307 j=i
308 308 l=0
309 309 if(nl==1)
310 310 @x=@l_margin
311 311 w=@w-@r_margin-@x
312 312 wmax=(w-2*@c_margin)*1000/@font_size
313 313 end
314 314 nl+=1
315 315 else
316 316 i+=n
317 317 if(o>=128)
318 318 sep=i
319 319 end
320 320 end
321 321 end
322 322 #Last chunk
323 323 if(i!=j)
324 324 Cell(l/1000*@font_size,h,s[j,i-j],0,0,'',0,link)
325 325 end
326 326 end
327 327
328 328 private
329 329
330 330 def putfonts()
331 331 nf=@n
332 332 @diffs.each do |diff|
333 333 #Encodings
334 334 newobj()
335 335 out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['+diff+']>>')
336 336 out('endobj')
337 337 end
338 338 # mqr=get_magic_quotes_runtime()
339 339 # set_magic_quotes_runtime(0)
340 340 @font_files.each_pair do |file, info|
341 341 #Font file embedding
342 342 newobj()
343 343 @font_files[file]['n']=@n
344 344 if(defined('FPDF_FONTPATH'))
345 345 file=FPDF_FONTPATH+file
346 346 end
347 347 size=filesize(file)
348 348 if(!size)
349 349 Error('Font file not found')
350 350 end
351 351 out('<</Length '+size)
352 352 if(file[-2]=='.z')
353 353 out('/Filter /FlateDecode')
354 354 end
355 355 out('/Length1 '+info['length1'])
356 356 unless info['length2'].nil?
357 357 out('/Length2 '+info['length2']+' /Length3 0')
358 358 end
359 359 out('>>')
360 360 f=fopen(file,'rb')
361 361 putstream(fread(f,size))
362 362 fclose(f)
363 363 out('endobj')
364 364 end
365 365 # set_magic_quotes_runtime(mqr)
366 366 @fonts.each_pair do |k, font|
367 367 #Font objects
368 368 newobj()
369 369 @fonts[k]['n']=@n
370 370 out('<</Type /Font')
371 371 if(font['type']=='Type0')
372 372 putType0(font)
373 373 else
374 374 name=font['name']
375 375 out('/BaseFont /'+name)
376 376 if(font['type']=='core')
377 377 #Standard font
378 378 out('/Subtype /Type1')
379 379 if(name!='Symbol' and name!='ZapfDingbats')
380 380 out('/Encoding /WinAnsiEncoding')
381 381 end
382 382 else
383 383 #Additional font
384 384 out('/Subtype /'+font['type'])
385 385 out('/FirstChar 32')
386 386 out('/LastChar 255')
387 387 out('/Widths '+(@n+1)+' 0 R')
388 388 out('/FontDescriptor '+(@n+2)+' 0 R')
389 389 if(font['enc'])
390 390 if !font['diff'].nil?
391 391 out('/Encoding '+(nf+font['diff'])+' 0 R')
392 392 else
393 393 out('/Encoding /WinAnsiEncoding')
394 394 end
395 395 end
396 396 end
397 397 out('>>')
398 398 out('endobj')
399 399 if(font['type']!='core')
400 400 #Widths
401 401 newobj()
402 402 cw=font['cw']
403 403 s='['
404 404 32.upto(255) do |i|
405 405 s+=cw[i.chr]+' '
406 406 end
407 407 out(s+']')
408 408 out('endobj')
409 409 #Descriptor
410 410 newobj()
411 411 s='<</Type /FontDescriptor /FontName /'+name
412 412 font['desc'].each_pair do |k, v|
413 413 s+=' /'+k+' '+v
414 414 end
415 415 file=font['file']
416 416 if(file)
417 417 s+=' /FontFile'+(font['type']=='Type1' ? '' : '2')+' '+@font_files[file]['n']+' 0 R'
418 418 end
419 419 out(s+'>>')
420 420 out('endobj')
421 421 end
422 422 end
423 423 end
424 424 end
425 425
426 426 def putType0(font)
427 427 #Type0
428 428 out('/Subtype /Type0')
429 429 out('/BaseFont /'+font['name']+'-'+font['CMap'])
430 430 out('/Encoding /'+font['CMap'])
431 431 out('/DescendantFonts ['+(@n+1).to_s+' 0 R]')
432 432 out('>>')
433 433 out('endobj')
434 434 #CIDFont
435 435 newobj()
436 436 out('<</Type /Font')
437 437 out('/Subtype /CIDFontType0')
438 438 out('/BaseFont /'+font['name'])
439 439 out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('+font['registry']['ordering']+') /Supplement '+font['registry']['supplement'].to_s+'>>')
440 440 out('/FontDescriptor '+(@n+1).to_s+' 0 R')
441 441 w='/W [1 ['
442 442 font['cw'].keys.sort.each {|key|
443 443 w+=font['cw'][key].to_s + " "
444 444 # ActionController::Base::logger.debug key.to_s
445 445 # ActionController::Base::logger.debug font['cw'][key].to_s
446 446 }
447 447 out(w+'] 231 325 500 631 [500] 326 389 500]')
448 448 out('>>')
449 449 out('endobj')
450 450 #Font descriptor
451 451 newobj()
452 452 out('<</Type /FontDescriptor')
453 453 out('/FontName /'+font['name'])
454 454 out('/Flags 6')
455 455 out('/FontBBox [0 -200 1000 900]')
456 456 out('/ItalicAngle 0')
457 457 out('/Ascent 800')
458 458 out('/Descent -200')
459 459 out('/CapHeight 800')
460 460 out('/StemV 60')
461 461 out('>>')
462 462 out('endobj')
463 463 end
464 464 end
@@ -1,432 +1,432
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 korean.php
25 25 #
26 26 # Korean 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_Korean)
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_Korean
39 39
40 40 UHC_widths={' ' => 333, '!' => 416, '"' => 416, '#' => 833, '$' => 625, '%' => 916, '&' => 833, '\'' => 250,
41 41 '(' => 500, ')' => 500, '*' => 500, '+' => 833, ',' => 291, '-' => 833, '.' => 291, '/' => 375, '0' => 625, '1' => 625,
42 42 '2' => 625, '3' => 625, '4' => 625, '5' => 625, '6' => 625, '7' => 625, '8' => 625, '9' => 625, ':' => 333, ';' => 333,
43 43 '<' => 833, '=' => 833, '>' => 916, '?' => 500, '@' => 1000, 'A' => 791, 'B' => 708, 'C' => 708, 'D' => 750, 'E' => 708,
44 44 'F' => 666, 'G' => 750, 'H' => 791, 'I' => 375, 'J' => 500, 'K' => 791, 'L' => 666, 'M' => 916, 'N' => 791, 'O' => 750,
45 45 'P' => 666, 'Q' => 750, 'R' => 708, 'S' => 666, 'T' => 791, 'U' => 791, 'V' => 750, 'W' => 1000, 'X' => 708, 'Y' => 708,
46 46 'Z' => 666, '[' => 500, '\\' => 375, ']' => 500, '^' => 500, '_' => 500, '`' => 333, 'a' => 541, 'b' => 583, 'c' => 541,
47 47 'd' => 583, 'e' => 583, 'f' => 375, 'g' => 583, 'h' => 583, 'i' => 291, 'j' => 333, 'k' => 583, 'l' => 291, 'm' => 875,
48 48 'n' => 583, 'o' => 583, 'p' => 583, 'q' => 583, 'r' => 458, 's' => 541, 't' => 375, 'u' => 583, 'v' => 583, 'w' => 833,
49 49 'x' => 625, 'y' => 625, 'z' => 500, '{' => 583, '|' => 583, '}' => 583, '~' => 750}
50 50
51 51 def AddCIDFont(family,style,name,cw,cMap,registry)
52 52 fontkey=family.downcase+style.upcase
53 53 unless @fonts[fontkey].nil?
54 54 Error("Font already added: family style")
55 55 end
56 56 i=@fonts.length+1
57 57 name=name.gsub(' ','')
58 58 @fonts[fontkey]={'i'=>i,'type'=>'Type0','name'=>name,'up'=>-130,'ut'=>40,'cw'=>cw,
59 59 'CMap'=>cMap,'registry'=>registry}
60 60 end
61 61
62 62 def AddCIDFonts(family,name,cw,cMap,registry)
63 63 AddCIDFont(family,'',name,cw,cMap,registry)
64 64 AddCIDFont(family,'B',name+',Bold',cw,cMap,registry)
65 65 AddCIDFont(family,'I',name+',Italic',cw,cMap,registry)
66 66 AddCIDFont(family,'BI',name+',BoldItalic',cw,cMap,registry)
67 67 end
68 68
69 69 def AddUHCFont(family='UHC',name='HYSMyeongJoStd-Medium-Acro')
70 70 #Add UHC font with proportional Latin
71 71 cw=UHC_widths
72 72 cMap='KSCms-UHC-H'
73 73 registry={'ordering'=>'Korea1','supplement'=>1}
74 74 AddCIDFonts(family,name,cw,cMap,registry)
75 75 end
76 76
77 77 def AddUHChwFont(family='UHC-hw',name='HYSMyeongJoStd-Medium-Acro')
78 78 #Add UHC font with half-witdh Latin
79 79 32.upto(126) do |i|
80 80 cw[i.chr]=500
81 81 end
82 82 cMap='KSCms-UHC-HW-H'
83 83 registry={'ordering'=>'Korea1','supplement'=>1}
84 84 AddCIDFonts(family,name,cw,cMap,registry)
85 85 end
86 86
87 87 def GetStringWidth(s)
88 88 if(@current_font['type']=='Type0')
89 89 return GetMBStringWidth(s)
90 90 else
91 91 return super(s)
92 92 end
93 93 end
94 94
95 95 def GetMBStringWidth(s)
96 96 #Multi-byte version of GetStringWidth()
97 97 l=0
98 98 cw=@current_font['cw']
99 99 nb=s.length
100 100 i=0
101 101 while(i<nb)
102 102 c = s[i].is_a?(String) ? s[i].ord : s[i]
103 103 if(c<128)
104 104 l+=cw[c.chr] if cw[c.chr]
105 105 i+=1
106 106 else
107 107 l+=1000
108 108 i+=2
109 109 end
110 110 end
111 111 return l*@font_size/1000
112 112 end
113 113
114 114 def MultiCell(w,h,txt,border=0,align='L',fill=0,ln=1)
115 115 if(@current_font['type']=='Type0')
116 116 MBMultiCell(w,h,txt,border,align,fill)
117 117 else
118 118 super(w,h,txt,border,align,fill)
119 119 end
120 120 end
121 121
122 122 def MBMultiCell(w,h,txt,border=0,align='L',fill=0)
123 123 #Multi-byte version of MultiCell()
124 124 cw=@current_font['cw']
125 125 if(w==0)
126 126 w=@w-@r_margin-@x
127 127 end
128 128 wmax=(w-2*@c_margin)*1000/@font_size
129 129 s=txt.gsub("\r",'')
130 130 nb=s.length
131 131 if(nb>0 and s[nb-1]=="\n")
132 132 nb-=1
133 133 end
134 134 b=0
135 135 if(border)
136 136 if(border==1)
137 137 border='LTRB'
138 138 b='LRT'
139 139 b2='LR'
140 140 else
141 141 b2=''
142 142 b2='L' unless border.to_s.index('L').nil?
143 143 b2=b2+'R' unless border.to_s.index('R').nil?
144 144 b=(border.to_s.index('T')) ? (b2+'T') : b2
145 145 end
146 146 end
147 147 sep=-1
148 148 i=0
149 149 j=0
150 150 l=0
151 151 nl=1
152 152 while(i<nb)
153 153 #Get next character
154 154 c = s[i].is_a?(String) ? s[i].ord : s[i]
155 155 #Check if ASCII or MB
156 156 ascii=(c<128)
157 157 if(c.chr=="\n")
158 158 #Explicit line break
159 159 Cell(w,h,s[j,i-j],b,2,align,fill)
160 160 i+=1
161 161 sep=-1
162 162 j=i
163 163 l=0
164 164 nl+=1
165 165 if(border and nl==2)
166 166 b=b2
167 167 end
168 168 next
169 169 end
170 170 if(!ascii)
171 171 sep=i
172 172 ls=l
173 173 elsif(c.chr==' ')
174 174 sep=i
175 175 ls=l
176 176 end
177 177 l+=(ascii ? cw[c.chr] : 1000) || 0
178 178 if(l>wmax)
179 179 #Automatic line break
180 180 if(sep==-1 or i==j)
181 181 if(i==j)
182 182 i+=ascii ? 1 : 2
183 183 end
184 184 Cell(w,h,s[j,i-j],b,2,align,fill)
185 185 else
186 186 Cell(w,h,s[j,sep-j],b,2,align,fill)
187 187 i=(s[sep].chr==' ') ? sep+1 : sep
188 188 end
189 189 sep=-1
190 190 j=i
191 191 l=0
192 192 nl+=1
193 193 if(border and nl==2)
194 194 b=b2
195 195 end
196 196 else
197 197 i+=ascii ? 1 : 2
198 198 end
199 199 end
200 200 #Last chunk
201 201 if(border and not border.to_s.index('B').nil?)
202 202 b+='B'
203 203 end
204 204 Cell(w,h,s[j,i-j],b,2,align,fill)
205 205 @x=@l_margin
206 206 end
207 207
208 def Write(h,txt,link='')
208 def Write(h,txt,link='',fill=0)
209 209 if(@current_font['type']=='Type0')
210 210 MBWrite(h,txt,link)
211 211 else
212 212 super(h,txt,link)
213 213 end
214 214 end
215 215
216 216 def MBWrite(h,txt,link)
217 217 #Multi-byte version of Write()
218 218 cw=@current_font['cw']
219 219 w=@w-@r_margin-@x
220 220 wmax=(w-2*@c_margin)*1000/@font_size
221 221 s=txt.gsub("\r",'')
222 222 nb=s.length
223 223 sep=-1
224 224 i=0
225 225 j=0
226 226 l=0
227 227 nl=1
228 228 while(i<nb)
229 229 #Get next character
230 230 c = s[i].is_a?(String) ? s[i].ord : s[i]
231 231 #Check if ASCII or MB
232 232 ascii=(c<128)
233 233 if(c.chr=="\n")
234 234 #Explicit line break
235 235 Cell(w,h,s[j,i-j],0,2,'',0,link)
236 236 i+=1
237 237 sep=-1
238 238 j=i
239 239 l=0
240 240 if(nl==1)
241 241 @x=@l_margin
242 242 w=@w-@r_margin-@x
243 243 wmax=(w-2*@c_margin)*1000/@font_size
244 244 end
245 245 nl+=1
246 246 next
247 247 end
248 248 if(!ascii or c.chr==' ')
249 249 sep=i
250 250 end
251 251 l+=(ascii ? cw[c.chr] : 1000) || 0
252 252 if(l>wmax)
253 253 #Automatic line break
254 254 if(sep==-1 or i==j)
255 255 if(@x>@l_margin)
256 256 #Move to next line
257 257 @x=@l_margin
258 258 @y+=h
259 259 w=@w-@r_margin-@x
260 260 wmax=(w-2*@c_margin)*1000/@font_size
261 261 i+=1
262 262 nl+=1
263 263 next
264 264 end
265 265 if(i==j)
266 266 i+=ascii ? 1 : 2
267 267 end
268 268 Cell(w,h,s[j,i-j],0,2,'',0,link)
269 269 else
270 270 Cell(w,h,s[j,sep-j],0,2,'',0,link)
271 271 i=(s[sep].chr==' ') ? sep+1 : sep
272 272 end
273 273 sep=-1
274 274 j=i
275 275 l=0
276 276 if(nl==1)
277 277 @x=@l_margin
278 278 w=@w-@r_margin-@x
279 279 wmax=(w-2*@c_margin)*1000/@font_size
280 280 end
281 281 nl+=1
282 282 else
283 283 i+=ascii ? 1 : 2
284 284 end
285 285 end
286 286 #Last chunk
287 287 if(i!=j)
288 288 Cell(l/1000*@font_size,h,s[j,i-j],0,0,'',0,link)
289 289 end
290 290 end
291 291
292 292 private
293 293
294 294 def putfonts()
295 295 nf=@n
296 296 @diffs.each do |diff|
297 297 #Encodings
298 298 newobj()
299 299 out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['+diff+']>>')
300 300 out('endobj')
301 301 end
302 302 # mqr=get_magic_quotes_runtime()
303 303 # set_magic_quotes_runtime(0)
304 304 @font_files.each_pair do |file, info|
305 305 #Font file embedding
306 306 newobj()
307 307 @font_files[file]['n']=@n
308 308 if(defined('FPDF_FONTPATH'))
309 309 file=FPDF_FONTPATH+file
310 310 end
311 311 size=filesize(file)
312 312 if(!size)
313 313 Error('Font file not found')
314 314 end
315 315 out('<</Length '+size)
316 316 if(file[-2]=='.z')
317 317 out('/Filter /FlateDecode')
318 318 end
319 319 out('/Length1 '+info['length1'])
320 320 if(not info['length2'].nil?)
321 321 out('/Length2 '+info['length2']+' /Length3 0')
322 322 end
323 323 out('>>')
324 324 f=fopen(file,'rb')
325 325 putstream(fread(f,size))
326 326 fclose(f)
327 327 out('endobj')
328 328 end
329 329 # set_magic_quotes_runtime(mqr)
330 330 @fonts.each_pair do |k, font|
331 331 #Font objects
332 332 newobj()
333 333 @fonts[k]['n']=@n
334 334 out('<</Type /Font')
335 335 if(font['type']=='Type0')
336 336 putType0(font)
337 337 else
338 338 name=font['name']
339 339 out('/BaseFont /'+name)
340 340 if(font['type']=='core')
341 341 #Standard font
342 342 out('/Subtype /Type1')
343 343 if(name!='Symbol' and name!='ZapfDingbats')
344 344 out('/Encoding /WinAnsiEncoding')
345 345 end
346 346 else
347 347 #Additional font
348 348 out('/Subtype /'+font['type'])
349 349 out('/FirstChar 32')
350 350 out('/LastChar 255')
351 351 out('/Widths '+(@n+1)+' 0 R')
352 352 out('/FontDescriptor '+(@n+2)+' 0 R')
353 353 if(font['enc'])
354 354 if(not font['diff'].nil?)
355 355 out('/Encoding '+(nf+font['diff'])+' 0 R')
356 356 else
357 357 out('/Encoding /WinAnsiEncoding')
358 358 end
359 359 end
360 360 end
361 361 out('>>')
362 362 out('endobj')
363 363 if(font['type']!='core')
364 364 #Widths
365 365 newobj()
366 366 cw=font['cw']
367 367 s='['
368 368 32.upto(255) do |i|
369 369 s+=cw[i.chr]+' '
370 370 end
371 371 out(s+']')
372 372 out('endobj')
373 373 #Descriptor
374 374 newobj()
375 375 s='<</Type /FontDescriptor /FontName /'+name
376 376 font['desc'].each_pair do |k, v|
377 377 s+=' /'+k+' '+v
378 378 end
379 379 file=font['file']
380 380 if(file)
381 381 s+=' /FontFile'+(font['type']=='Type1' ? '' : '2')+' '+@font_files[file]['n']+' 0 R'
382 382 end
383 383 out(s+'>>')
384 384 out('endobj')
385 385 end
386 386 end
387 387 end
388 388 end
389 389
390 390 def putType0(font)
391 391 #Type0
392 392 out('/Subtype /Type0')
393 393 out('/BaseFont /'+font['name']+'-'+font['CMap'])
394 394 out('/Encoding /'+font['CMap'])
395 395 out('/DescendantFonts ['+(@n+1).to_s+' 0 R]')
396 396 out('>>')
397 397 out('endobj')
398 398 #CIDFont
399 399 newobj()
400 400 out('<</Type /Font')
401 401 out('/Subtype /CIDFontType0')
402 402 out('/BaseFont /'+font['name'])
403 403 out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('+font['registry']['ordering']+') /Supplement '+font['registry']['supplement'].to_s+'>>')
404 404 out('/FontDescriptor '+(@n+1).to_s+' 0 R')
405 405 if(font['CMap']=='KSCms-UHC-HW-H')
406 406 w='8094 8190 500'
407 407 else
408 408 w='1 ['
409 409 font['cw'].keys.sort.each {|key|
410 410 w+=font['cw'][key].to_s + " "
411 411 # ActionController::Base::logger.debug key.to_s
412 412 # ActionController::Base::logger.debug font['cw'][key].to_s
413 413 }
414 414 w +=']'
415 415 end
416 416 out('/W ['+w+']>>')
417 417 out('endobj')
418 418 #Font descriptor
419 419 newobj()
420 420 out('<</Type /FontDescriptor')
421 421 out('/FontName /'+font['name'])
422 422 out('/Flags 6')
423 423 out('/FontBBox [0 -200 1000 900]')
424 424 out('/ItalicAngle 0')
425 425 out('/Ascent 800')
426 426 out('/Descent -200')
427 427 out('/CapHeight 800')
428 428 out('/StemV 50')
429 429 out('>>')
430 430 out('endobj')
431 431 end
432 432 end
General Comments 0
You need to be logged in to leave comments. Login now