1 # Copyright (c) 2006 4ssoM LLC <www.4ssoM.com>
\r
2 # 1.12 contributed by Ed Moss.
\r
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
\r
7 # of this software and associated documentation files (the "Software"), to deal
\r
8 # in the Software without restriction, including without limitation the rights
\r
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
10 # copies of the Software, and to permit persons to whom the Software is
\r
11 # furnished to do so, subject to the following conditions:
\r
13 # The above copyright notice and this permission notice shall be included in
\r
14 # all copies or substantial portions of the Software.
\r
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
24 # This is direct port of korean.php
\r
26 # Korean PDF support.
\r
28 # Usage is as follows:
\r
33 # pdf.extend(PDF_Korean)
\r
35 # This allows it to be combined with other extensions, such as the bookmark
\r
40 UHC_widths={' ' => 333, '!' => 416, '"' => 416, '#' => 833, '$' => 625, '%' => 916, '&' => 833, '\'' => 250,
\r
41 '(' => 500, ')' => 500, '*' => 500, '+' => 833, ',' => 291, '-' => 833, '.' => 291, '/' => 375, '0' => 625, '1' => 625,
\r
42 '2' => 625, '3' => 625, '4' => 625, '5' => 625, '6' => 625, '7' => 625, '8' => 625, '9' => 625, ':' => 333, ';' => 333,
\r
43 '<' => 833, '=' => 833, '>' => 916, '?' => 500, '@' => 1000, 'A' => 791, 'B' => 708, 'C' => 708, 'D' => 750, 'E' => 708,
\r
44 'F' => 666, 'G' => 750, 'H' => 791, 'I' => 375, 'J' => 500, 'K' => 791, 'L' => 666, 'M' => 916, 'N' => 791, 'O' => 750,
\r
45 'P' => 666, 'Q' => 750, 'R' => 708, 'S' => 666, 'T' => 791, 'U' => 791, 'V' => 750, 'W' => 1000, 'X' => 708, 'Y' => 708,
\r
46 'Z' => 666, '[' => 500, '\\' => 375, ']' => 500, '^' => 500, '_' => 500, '`' => 333, 'a' => 541, 'b' => 583, 'c' => 541,
\r
47 'd' => 583, 'e' => 583, 'f' => 375, 'g' => 583, 'h' => 583, 'i' => 291, 'j' => 333, 'k' => 583, 'l' => 291, 'm' => 875,
\r
48 'n' => 583, 'o' => 583, 'p' => 583, 'q' => 583, 'r' => 458, 's' => 541, 't' => 375, 'u' => 583, 'v' => 583, 'w' => 833,
\r
49 'x' => 625, 'y' => 625, 'z' => 500, '{' => 583, '|' => 583, '}' => 583, '~' => 750}
\r
51 def AddCIDFont(family,style,name,cw,cMap,registry)
\r
52 fontkey=family.downcase+style.upcase
\r
53 unless @fonts[fontkey].nil?
\r
54 Error("Font already added: family style")
\r
57 name=name.gsub(' ','')
\r
58 @fonts[fontkey]={'i'=>i,'type'=>'Type0','name'=>name,'up'=>-130,'ut'=>40,'cw'=>cw,
\r
59 'CMap'=>cMap,'registry'=>registry}
\r
62 def AddCIDFonts(family,name,cw,cMap,registry)
\r
63 AddCIDFont(family,'',name,cw,cMap,registry)
\r
64 AddCIDFont(family,'B',name+',Bold',cw,cMap,registry)
\r
65 AddCIDFont(family,'I',name+',Italic',cw,cMap,registry)
\r
66 AddCIDFont(family,'BI',name+',BoldItalic',cw,cMap,registry)
\r
69 def AddUHCFont(family='UHC',name='HYSMyeongJoStd-Medium-Acro')
\r
70 #Add UHC font with proportional Latin
\r
73 registry={'ordering'=>'Korea1','supplement'=>1}
\r
74 AddCIDFonts(family,name,cw,cMap,registry)
\r
77 def AddUHChwFont(family='UHC-hw',name='HYSMyeongJoStd-Medium-Acro')
\r
78 #Add UHC font with half-witdh Latin
\r
82 cMap='KSCms-UHC-HW-H'
\r
83 registry={'ordering'=>'Korea1','supplement'=>1}
\r
84 AddCIDFonts(family,name,cw,cMap,registry)
\r
87 def GetStringWidth(s)
\r
88 if(@current_font['type']=='Type0')
\r
89 return GetMBStringWidth(s)
\r
95 def GetMBStringWidth(s)
\r
96 #Multi-byte version of GetStringWidth()
\r
98 cw=@current_font['cw']
\r
102 c = s[i].is_a?(String) ? s[i].ord : s[i]
\r
104 l+=cw[c.chr] if cw[c.chr]
\r
111 return l*@font_size/1000
\r
114 def MultiCell(w,h,txt,border=0,align='L',fill=0)
\r
115 if(@current_font['type']=='Type0')
\r
116 MBMultiCell(w,h,txt,border,align,fill)
\r
118 super(w,h,txt,border,align,fill)
\r
122 def MBMultiCell(w,h,txt,border=0,align='L',fill=0)
\r
123 #Multi-byte version of MultiCell()
\r
124 cw=@current_font['cw']
\r
128 wmax=(w-2*@c_margin)*1000/@font_size
\r
129 s=txt.gsub("\r",'')
\r
131 if(nb>0 and s[nb-1]=="\n")
\r
142 b2='L' unless border.to_s.index('L').nil?
\r
143 b2=b2+'R' unless border.to_s.index('R').nil?
\r
144 b=(border.to_s.index('T')) ? (b2+'T') : b2
\r
153 #Get next character
\r
154 c = s[i].is_a?(String) ? s[i].ord : s[i]
\r
155 #Check if ASCII or MB
\r
158 #Explicit line break
\r
159 Cell(w,h,s[j,i-j],b,2,align,fill)
\r
165 if(border and nl==2)
\r
177 l+=(ascii ? cw[c.chr] : 1000) || 0
\r
179 #Automatic line break
\r
180 if(sep==-1 or i==j)
\r
184 Cell(w,h,s[j,i-j],b,2,align,fill)
\r
186 Cell(w,h,s[j,sep-j],b,2,align,fill)
\r
187 i=(s[sep].chr==' ') ? sep+1 : sep
\r
193 if(border and nl==2)
\r
201 if(border and not border.to_s.index('B').nil?)
\r
204 Cell(w,h,s[j,i-j],b,2,align,fill)
\r
208 def Write(h,txt,link='')
\r
209 if(@current_font['type']=='Type0')
\r
210 MBWrite(h,txt,link)
\r
216 def MBWrite(h,txt,link)
\r
217 #Multi-byte version of Write()
\r
218 cw=@current_font['cw']
\r
220 wmax=(w-2*@c_margin)*1000/@font_size
\r
221 s=txt.gsub("\r",'')
\r
229 #Get next character
\r
230 c = s[i].is_a?(String) ? s[i].ord : s[i]
\r
231 #Check if ASCII or MB
\r
234 #Explicit line break
\r
235 Cell(w,h,s[j,i-j],0,2,'',0,link)
\r
243 wmax=(w-2*@c_margin)*1000/@font_size
\r
248 if(!ascii or c.chr==' ')
\r
251 l+=(ascii ? cw[c.chr] : 1000) || 0
\r
253 #Automatic line break
\r
254 if(sep==-1 or i==j)
\r
260 wmax=(w-2*@c_margin)*1000/@font_size
\r
268 Cell(w,h,s[j,i-j],0,2,'',0,link)
\r
270 Cell(w,h,s[j,sep-j],0,2,'',0,link)
\r
271 i=(s[sep].chr==' ') ? sep+1 : sep
\r
279 wmax=(w-2*@c_margin)*1000/@font_size
\r
288 Cell(l/1000*@font_size,h,s[j,i-j],0,0,'',0,link)
\r
296 @diffs.each do |diff|
\r
299 out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['+diff+']>>')
\r
302 # mqr=get_magic_quotes_runtime()
\r
303 # set_magic_quotes_runtime(0)
\r
304 @font_files.each_pair do |file, info|
\r
305 #Font file embedding
\r
307 @font_files[file]['n']=@n
\r
308 if(defined('FPDF_FONTPATH'))
\r
309 file=FPDF_FONTPATH+file
\r
311 size=filesize(file)
\r
313 Error('Font file not found')
\r
315 out('<</Length '+size)
\r
317 out('/Filter /FlateDecode')
\r
319 out('/Length1 '+info['length1'])
\r
320 if(not info['length2'].nil?)
\r
321 out('/Length2 '+info['length2']+' /Length3 0')
\r
325 putstream(fread(f,size))
\r
329 # set_magic_quotes_runtime(mqr)
\r
330 @fonts.each_pair do |k, font|
\r
334 out('<</Type /Font')
\r
335 if(font['type']=='Type0')
\r
339 out('/BaseFont /'+name)
\r
340 if(font['type']=='core')
\r
342 out('/Subtype /Type1')
\r
343 if(name!='Symbol' and name!='ZapfDingbats')
\r
344 out('/Encoding /WinAnsiEncoding')
\r
348 out('/Subtype /'+font['type'])
\r
349 out('/FirstChar 32')
\r
350 out('/LastChar 255')
\r
351 out('/Widths '+(@n+1)+' 0 R')
\r
352 out('/FontDescriptor '+(@n+2)+' 0 R')
\r
354 if(not font['diff'].nil?)
\r
355 out('/Encoding '+(nf+font['diff'])+' 0 R')
\r
357 out('/Encoding /WinAnsiEncoding')
\r
363 if(font['type']!='core')
\r
368 32.upto(255) do |i|
\r
375 s='<</Type /FontDescriptor /FontName /'+name
\r
376 font['desc'].each_pair do |k, v|
\r
381 s+=' /FontFile'+(font['type']=='Type1' ? '' : '2')+' '+@font_files[file]['n']+' 0 R'
\r
392 out('/Subtype /Type0')
\r
393 out('/BaseFont /'+font['name']+'-'+font['CMap'])
\r
394 out('/Encoding /'+font['CMap'])
\r
395 out('/DescendantFonts ['+(@n+1).to_s+' 0 R]')
\r
400 out('<</Type /Font')
\r
401 out('/Subtype /CIDFontType0')
\r
402 out('/BaseFont /'+font['name'])
\r
403 out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('+font['registry']['ordering']+') /Supplement '+font['registry']['supplement'].to_s+'>>')
\r
404 out('/FontDescriptor '+(@n+1).to_s+' 0 R')
\r
405 if(font['CMap']=='KSCms-UHC-HW-H')
\r
409 font['cw'].keys.sort.each {|key|
\r
410 w+=font['cw'][key].to_s + " "
\r
411 # ActionController::Base::logger.debug key.to_s
\r
412 # ActionController::Base::logger.debug font['cw'][key].to_s
\r
416 out('/W ['+w+']>>')
\r
420 out('<</Type /FontDescriptor')
\r
421 out('/FontName /'+font['name'])
\r
423 out('/FontBBox [0 -200 1000 900]')
\r
424 out('/ItalicAngle 0')
\r
426 out('/Descent -200')
\r
427 out('/CapHeight 800')
\r