1 # Copyright (c) 2006 4ssoM LLC <www.4ssoM.com>
2 # 1.12 contributed by Ed Moss.
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:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
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
24 # This is direct port of japanese.php
26 # Japanese PDF support.
28 # Usage is as follows:
33 # pdf.extend(PDF_Japanese)
35 # This allows it to be combined with other extensions, such as the bookmark
40 SJIS_widths={' ' => 278, '!' => 299, '"' => 353, '#' => 614, '$' => 614, '%' => 721, '&' => 735, '\'' => 216,
41 '(' => 323, ')' => 323, '*' => 449, '+' => 529, ',' => 219, '-' => 306, '.' => 219, '/' => 453, '0' => 614, '1' => 614,
42 '2' => 614, '3' => 614, '4' => 614, '5' => 614, '6' => 614, '7' => 614, '8' => 614, '9' => 614, ':' => 219, ';' => 219,
43 '<' => 529, '=' => 529, '>' => 529, '?' => 486, '@' => 744, 'A' => 646, 'B' => 604, 'C' => 617, 'D' => 681, 'E' => 567,
44 'F' => 537, 'G' => 647, 'H' => 738, 'I' => 320, 'J' => 433, 'K' => 637, 'L' => 566, 'M' => 904, 'N' => 710, 'O' => 716,
45 'P' => 605, 'Q' => 716, 'R' => 623, 'S' => 517, 'T' => 601, 'U' => 690, 'V' => 668, 'W' => 990, 'X' => 681, 'Y' => 634,
46 'Z' => 578, '[' => 316, '\\' => 614, ']' => 316, '^' => 529, '_' => 500, '`' => 387, 'a' => 509, 'b' => 566, 'c' => 478,
47 'd' => 565, 'e' => 503, 'f' => 337, 'g' => 549, 'h' => 580, 'i' => 275, 'j' => 266, 'k' => 544, 'l' => 276, 'm' => 854,
48 'n' => 579, 'o' => 550, 'p' => 578, 'q' => 566, 'r' => 410, 's' => 444, 't' => 340, 'u' => 575, 'v' => 512, 'w' => 760,
49 'x' => 503, 'y' => 529, 'z' => 453, '{' => 326, '|' => 380, '}' => 326, '~' => 387}
51 def AddCIDFont(family,style,name,cw,cMap,registry)
52 fontkey=family.downcase+style.upcase
53 unless @fonts[fontkey].nil?
54 Error("CID font already added: family style")
57 @fonts[fontkey]={'i'=>i,'type'=>'Type0','name'=>name,'up'=>-120,'ut'=>40,'cw'=>cw,
58 'CMap'=>cMap,'registry'=>registry}
61 def AddCIDFonts(family,name,cw,cMap,registry)
62 AddCIDFont(family,'',name,cw,cMap,registry)
63 AddCIDFont(family,'B',name+',Bold',cw,cMap,registry)
64 AddCIDFont(family,'I',name+',Italic',cw,cMap,registry)
65 AddCIDFont(family,'BI',name+',BoldItalic',cw,cMap,registry)
68 def AddSJISFont(family='SJIS')
69 #Add SJIS font with proportional Latin
70 name='KozMinPro-Regular-Acro'
73 registry={'ordering'=>'Japan1','supplement'=>2}
74 AddCIDFonts(family,name,cw,cMap,registry)
77 def AddSJIShwFont(family='SJIS-hw')
78 #Add SJIS font with half-width Latin
79 name='KozMinPro-Regular-Acro'
84 registry={'ordering'=>'Japan1','supplement'=>2}
85 AddCIDFonts(family,name,cw,cMap,registry)
89 if(@CurrentFont['type']=='Type0')
90 return GetSJISStringWidth(s)
96 def GetSJISStringWidth(s)
97 #SJIS version of GetStringWidth()
108 elsif(o>=161 and o<=223)
113 #Full-width character
118 return l*@FontSize/1000
121 def MultiCell(w,h,txt,border=0,align='L',fill=0)
122 if(@CurrentFont['type']=='Type0')
123 SJISMultiCell(w,h,txt,border,align,fill)
125 super(w,h,txt,border,align,fill)
129 def SJISMultiCell(w,h,txt,border=0,align='L',fill=0)
130 #Output text with automatic or explicit line breaks
131 cw=@CurrentFont['cw']
135 wmax=(w-2*@cMargin)*1000/@FontSize
138 if(nb>0 and s[nb-1]=="\n")
149 if(border.to_s.index('L'))
152 if(border.to_s.index('R'))
155 b=border.to_s.index('T') ? b2+'T' : b2
169 Cell(w,h,s[j,i-j],b,2,align,fill)
187 elsif(o>=161 and o<=223)
193 #Full-width character
199 #Automatic line break
204 Cell(w,h,s[j,i-j],b,2,align,fill)
206 Cell(w,h,s[j,sep-j],b,2,align,fill)
207 i=(s[sep]==' ') ? sep+1 : sep
224 if(border and not border.to_s.index('B').nil?)
227 Cell(w,h,s[j,i-j],b,2,align,fill)
231 def Write(h,txt,link='')
232 if(@CurrentFont['type']=='Type0')
233 SJISWrite(h,txt,link)
239 def SJISWrite(h,txt,link)
240 #SJIS version of Write()
241 cw=@CurrentFont['cw']
243 wmax=(w-2*@cMargin)*1000/@FontSize
257 Cell(w,h,s[j,i-j],0,2,'',0,link)
266 wmax=(w-2*@cMargin)*1000/@FontSize
278 elsif(o>=161 and o<=223)
284 #Full-width character
290 #Automatic line break
297 wmax=(w-2*@cMargin)*1000/@FontSize
305 Cell(w,h,s[j,i-j],0,2,'',0,link)
307 Cell(w,h,s[j,sep-j],0,2,'',0,link)
308 i=(s[sep]==' ') ? sep+1 : sep
316 wmax=(w-2*@cMargin)*1000/@FontSize
328 Cell(l/1000*@FontSize,h,s[j,i-j],0,0,'',0,link)
336 @diffs.each do |diff|
339 out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['+diff+']>>')
342 # mqr=get_magic_quotes_runtime()
343 # set_magic_quotes_runtime(0)
344 @FontFiles.each_pair do |file, info|
347 @FontFiles[file]['n']=@n
348 if(defined('FPDF_FONTPATH'))
349 file=FPDF_FONTPATH+file
353 Error('Font file not found')
355 out('<</Length '+size)
357 out('/Filter /FlateDecode')
359 out('/Length1 '+info['length1'])
360 unless info['length2'].nil?
361 out('/Length2 '+info['length2']+' /Length3 0')
365 putstream(fread(f,size))
369 # set_magic_quotes_runtime(mqr)
370 @fonts.each_pair do |k, font|
375 if(font['type']=='Type0')
379 out('/BaseFont /'+name)
380 if(font['type']=='core')
382 out('/Subtype /Type1')
383 if(name!='Symbol' and name!='ZapfDingbats')
384 out('/Encoding /WinAnsiEncoding')
388 out('/Subtype /'+font['type'])
391 out('/Widths '+(@n+1)+' 0 R')
392 out('/FontDescriptor '+(@n+2)+' 0 R')
394 if !font['diff'].nil?
395 out('/Encoding '+(nf+font['diff'])+' 0 R')
397 out('/Encoding /WinAnsiEncoding')
403 if(font['type']!='core')
415 s='<</Type /FontDescriptor /FontName /'+name
416 font['desc'].each_pair do |k, v|
421 s+=' /FontFile'+(font['type']=='Type1' ? '' : '2')+' '+@FontFiles[file]['n']+' 0 R'
432 out('/Subtype /Type0')
433 out('/BaseFont /'+font['name']+'-'+font['CMap'])
434 out('/Encoding /'+font['CMap'])
435 out('/DescendantFonts ['+(@n+1).to_s+' 0 R]')
441 out('/Subtype /CIDFontType0')
442 out('/BaseFont /'+font['name'])
443 out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('+font['registry']['ordering']+') /Supplement '+font['registry']['supplement'].to_s+'>>')
444 out('/FontDescriptor '+(@n+1).to_s+' 0 R')
446 font['cw'].keys.sort.each {|key|
447 w+=font['cw'][key].to_s + " "
448 # ActionController::Base::logger.debug key.to_s
449 # ActionController::Base::logger.debug font['cw'][key].to_s
451 out(w+'] 231 325 500 631 [500] 326 389 500]')
456 out('<</Type /FontDescriptor')
457 out('/FontName /'+font['name'])
459 out('/FontBBox [0 -200 1000 900]')
460 out('/ItalicAngle 0')
463 out('/CapHeight 800')