Chris@441: # Copyright (c) 2006 4ssoM LLC Chris@441: # 1.12 contributed by Ed Moss. Chris@441: # Chris@441: # The MIT License Chris@441: # Chris@441: # Permission is hereby granted, free of charge, to any person obtaining a copy Chris@441: # of this software and associated documentation files (the "Software"), to deal Chris@441: # in the Software without restriction, including without limitation the rights Chris@441: # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@441: # copies of the Software, and to permit persons to whom the Software is Chris@441: # furnished to do so, subject to the following conditions: Chris@441: # Chris@441: # The above copyright notice and this permission notice shall be included in Chris@441: # all copies or substantial portions of the Software. Chris@441: # Chris@441: # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@441: # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@441: # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@441: # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@441: # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@441: # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@441: # THE SOFTWARE. Chris@441: # Chris@441: # This is direct port of korean.php Chris@441: # Chris@441: # Korean PDF support. Chris@441: # Chris@441: # Usage is as follows: Chris@441: # Chris@441: # require 'fpdf' Chris@441: # require 'chinese' Chris@441: # pdf = FPDF.new Chris@441: # pdf.extend(PDF_Korean) Chris@441: # Chris@441: # This allows it to be combined with other extensions, such as the bookmark Chris@441: # module. Chris@441: Chris@441: module PDF_Korean Chris@441: Chris@441: UHC_widths={' ' => 333, '!' => 416, '"' => 416, '#' => 833, '$' => 625, '%' => 916, '&' => 833, '\'' => 250, Chris@441: '(' => 500, ')' => 500, '*' => 500, '+' => 833, ',' => 291, '-' => 833, '.' => 291, '/' => 375, '0' => 625, '1' => 625, Chris@441: '2' => 625, '3' => 625, '4' => 625, '5' => 625, '6' => 625, '7' => 625, '8' => 625, '9' => 625, ':' => 333, ';' => 333, Chris@441: '<' => 833, '=' => 833, '>' => 916, '?' => 500, '@' => 1000, 'A' => 791, 'B' => 708, 'C' => 708, 'D' => 750, 'E' => 708, Chris@441: 'F' => 666, 'G' => 750, 'H' => 791, 'I' => 375, 'J' => 500, 'K' => 791, 'L' => 666, 'M' => 916, 'N' => 791, 'O' => 750, Chris@441: 'P' => 666, 'Q' => 750, 'R' => 708, 'S' => 666, 'T' => 791, 'U' => 791, 'V' => 750, 'W' => 1000, 'X' => 708, 'Y' => 708, Chris@441: 'Z' => 666, '[' => 500, '\\' => 375, ']' => 500, '^' => 500, '_' => 500, '`' => 333, 'a' => 541, 'b' => 583, 'c' => 541, Chris@441: 'd' => 583, 'e' => 583, 'f' => 375, 'g' => 583, 'h' => 583, 'i' => 291, 'j' => 333, 'k' => 583, 'l' => 291, 'm' => 875, Chris@441: 'n' => 583, 'o' => 583, 'p' => 583, 'q' => 583, 'r' => 458, 's' => 541, 't' => 375, 'u' => 583, 'v' => 583, 'w' => 833, Chris@441: 'x' => 625, 'y' => 625, 'z' => 500, '{' => 583, '|' => 583, '}' => 583, '~' => 750} Chris@441: Chris@441: def AddCIDFont(family,style,name,cw,cMap,registry) Chris@441: fontkey=family.downcase+style.upcase Chris@441: unless @fonts[fontkey].nil? Chris@441: Error("Font already added: family style") Chris@441: end Chris@441: i=@fonts.length+1 Chris@441: name=name.gsub(' ','') Chris@441: @fonts[fontkey]={'i'=>i,'type'=>'Type0','name'=>name,'up'=>-130,'ut'=>40,'cw'=>cw, Chris@441: 'CMap'=>cMap,'registry'=>registry} Chris@441: end Chris@441: Chris@441: def AddCIDFonts(family,name,cw,cMap,registry) Chris@441: AddCIDFont(family,'',name,cw,cMap,registry) Chris@441: AddCIDFont(family,'B',name+',Bold',cw,cMap,registry) Chris@441: AddCIDFont(family,'I',name+',Italic',cw,cMap,registry) Chris@441: AddCIDFont(family,'BI',name+',BoldItalic',cw,cMap,registry) Chris@441: end Chris@441: Chris@441: def AddUHCFont(family='UHC',name='HYSMyeongJoStd-Medium-Acro') Chris@441: #Add UHC font with proportional Latin Chris@441: cw=UHC_widths Chris@441: cMap='KSCms-UHC-H' Chris@441: registry={'ordering'=>'Korea1','supplement'=>1} Chris@441: AddCIDFonts(family,name,cw,cMap,registry) Chris@441: end Chris@441: Chris@441: def AddUHChwFont(family='UHC-hw',name='HYSMyeongJoStd-Medium-Acro') Chris@441: #Add UHC font with half-witdh Latin Chris@441: 32.upto(126) do |i| Chris@441: cw[i.chr]=500 Chris@441: end Chris@441: cMap='KSCms-UHC-HW-H' Chris@441: registry={'ordering'=>'Korea1','supplement'=>1} Chris@441: AddCIDFonts(family,name,cw,cMap,registry) Chris@441: end Chris@441: Chris@441: def GetStringWidth(s) Chris@441: if(@current_font['type']=='Type0') Chris@441: return GetMBStringWidth(s) Chris@441: else Chris@441: return super(s) Chris@441: end Chris@441: end Chris@441: Chris@441: def GetMBStringWidth(s) Chris@441: #Multi-byte version of GetStringWidth() Chris@441: l=0 Chris@441: cw=@current_font['cw'] Chris@441: nb=s.length Chris@441: i=0 Chris@441: while(i0 and s[nb-1]=="\n") Chris@441: nb-=1 Chris@441: end Chris@441: b=0 Chris@441: if(border) Chris@441: if(border==1) Chris@441: border='LTRB' Chris@441: b='LRT' Chris@441: b2='LR' Chris@441: else Chris@441: b2='' Chris@441: b2='L' unless border.to_s.index('L').nil? Chris@441: b2=b2+'R' unless border.to_s.index('R').nil? Chris@441: b=(border.to_s.index('T')) ? (b2+'T') : b2 Chris@441: end Chris@441: end Chris@441: sep=-1 Chris@441: i=0 Chris@441: j=0 Chris@441: l=0 Chris@441: nl=1 Chris@441: while(iwmax) Chris@441: #Automatic line break Chris@441: if(sep==-1 or i==j) Chris@441: if(i==j) Chris@441: i+=ascii ? 1 : 2 Chris@441: end Chris@441: Cell(w,h,s[j,i-j],b,2,align,fill) Chris@441: else Chris@441: Cell(w,h,s[j,sep-j],b,2,align,fill) Chris@441: i=(s[sep].chr==' ') ? sep+1 : sep Chris@441: end Chris@441: sep=-1 Chris@441: j=i Chris@441: l=0 Chris@441: nl+=1 Chris@441: if(border and nl==2) Chris@441: b=b2 Chris@441: end Chris@441: else Chris@441: i+=ascii ? 1 : 2 Chris@441: end Chris@441: end Chris@441: #Last chunk Chris@441: if(border and not border.to_s.index('B').nil?) Chris@441: b+='B' Chris@441: end Chris@441: Cell(w,h,s[j,i-j],b,2,align,fill) Chris@441: @x=@l_margin Chris@441: end Chris@441: Chris@507: def Write(h,txt,link='',fill=0) Chris@441: if(@current_font['type']=='Type0') Chris@441: MBWrite(h,txt,link) Chris@441: else Chris@441: super(h,txt,link) Chris@441: end Chris@441: end Chris@441: Chris@441: def MBWrite(h,txt,link) Chris@441: #Multi-byte version of Write() Chris@441: cw=@current_font['cw'] Chris@441: w=@w-@r_margin-@x Chris@441: wmax=(w-2*@c_margin)*1000/@font_size Chris@441: s=txt.gsub("\r",'') Chris@441: nb=s.length Chris@441: sep=-1 Chris@441: i=0 Chris@441: j=0 Chris@441: l=0 Chris@441: nl=1 Chris@441: while(iwmax) Chris@441: #Automatic line break Chris@441: if(sep==-1 or i==j) Chris@441: if(@x>@l_margin) Chris@441: #Move to next line Chris@441: @x=@l_margin Chris@441: @y+=h Chris@441: w=@w-@r_margin-@x Chris@441: wmax=(w-2*@c_margin)*1000/@font_size Chris@441: i+=1 Chris@441: nl+=1 Chris@441: next Chris@441: end Chris@441: if(i==j) Chris@441: i+=ascii ? 1 : 2 Chris@441: end Chris@441: Cell(w,h,s[j,i-j],0,2,'',0,link) Chris@441: else Chris@441: Cell(w,h,s[j,sep-j],0,2,'',0,link) Chris@441: i=(s[sep].chr==' ') ? sep+1 : sep Chris@441: end Chris@441: sep=-1 Chris@441: j=i Chris@441: l=0 Chris@441: if(nl==1) Chris@441: @x=@l_margin Chris@441: w=@w-@r_margin-@x Chris@441: wmax=(w-2*@c_margin)*1000/@font_size Chris@441: end Chris@441: nl+=1 Chris@441: else Chris@441: i+=ascii ? 1 : 2 Chris@441: end Chris@441: end Chris@441: #Last chunk Chris@441: if(i!=j) Chris@441: Cell(l/1000*@font_size,h,s[j,i-j],0,0,'',0,link) Chris@441: end Chris@441: end Chris@441: Chris@441: private Chris@441: Chris@441: def putfonts() Chris@441: nf=@n Chris@441: @diffs.each do |diff| Chris@441: #Encodings Chris@441: newobj() Chris@441: out('<>') Chris@441: out('endobj') Chris@441: end Chris@441: # mqr=get_magic_quotes_runtime() Chris@441: # set_magic_quotes_runtime(0) Chris@441: @font_files.each_pair do |file, info| Chris@441: #Font file embedding Chris@441: newobj() Chris@441: @font_files[file]['n']=@n Chris@441: if(defined('FPDF_FONTPATH')) Chris@441: file=FPDF_FONTPATH+file Chris@441: end Chris@441: size=filesize(file) Chris@441: if(!size) Chris@441: Error('Font file not found') Chris@441: end Chris@441: out('<>') Chris@441: f=fopen(file,'rb') Chris@441: putstream(fread(f,size)) Chris@441: fclose(f) Chris@441: out('endobj') Chris@441: end Chris@441: # set_magic_quotes_runtime(mqr) Chris@441: @fonts.each_pair do |k, font| Chris@441: #Font objects Chris@441: newobj() Chris@441: @fonts[k]['n']=@n Chris@441: out('<>') Chris@441: out('endobj') Chris@441: if(font['type']!='core') Chris@441: #Widths Chris@441: newobj() Chris@441: cw=font['cw'] Chris@441: s='[' Chris@441: 32.upto(255) do |i| Chris@441: s+=cw[i.chr]+' ' Chris@441: end Chris@441: out(s+']') Chris@441: out('endobj') Chris@441: #Descriptor Chris@441: newobj() Chris@441: s='<>') Chris@441: out('endobj') Chris@441: end Chris@441: end Chris@441: end Chris@441: end Chris@441: Chris@441: def putType0(font) Chris@441: #Type0 Chris@441: out('/Subtype /Type0') Chris@441: out('/BaseFont /'+font['name']+'-'+font['CMap']) Chris@441: out('/Encoding /'+font['CMap']) Chris@441: out('/DescendantFonts ['+(@n+1).to_s+' 0 R]') Chris@441: out('>>') Chris@441: out('endobj') Chris@441: #CIDFont Chris@441: newobj() Chris@441: out('<>') Chris@441: out('/FontDescriptor '+(@n+1).to_s+' 0 R') Chris@441: if(font['CMap']=='KSCms-UHC-HW-H') Chris@441: w='8094 8190 500' Chris@441: else Chris@441: w='1 [' Chris@441: font['cw'].keys.sort.each {|key| Chris@441: w+=font['cw'][key].to_s + " " Chris@441: # ActionController::Base::logger.debug key.to_s Chris@441: # ActionController::Base::logger.debug font['cw'][key].to_s Chris@441: } Chris@441: w +=']' Chris@441: end Chris@441: out('/W ['+w+']>>') Chris@441: out('endobj') Chris@441: #Font descriptor Chris@441: newobj() Chris@441: out('<>') Chris@441: out('endobj') Chris@441: end Chris@441: end