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 japanese.php Chris@441: # Chris@441: # Japanese 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_Japanese) 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_Japanese Chris@441: Chris@441: SJIS_widths={' ' => 278, '!' => 299, '"' => 353, '#' => 614, '$' => 614, '%' => 721, '&' => 735, '\'' => 216, Chris@441: '(' => 323, ')' => 323, '*' => 449, '+' => 529, ',' => 219, '-' => 306, '.' => 219, '/' => 453, '0' => 614, '1' => 614, Chris@441: '2' => 614, '3' => 614, '4' => 614, '5' => 614, '6' => 614, '7' => 614, '8' => 614, '9' => 614, ':' => 219, ';' => 219, Chris@441: '<' => 529, '=' => 529, '>' => 529, '?' => 486, '@' => 744, 'A' => 646, 'B' => 604, 'C' => 617, 'D' => 681, 'E' => 567, Chris@441: 'F' => 537, 'G' => 647, 'H' => 738, 'I' => 320, 'J' => 433, 'K' => 637, 'L' => 566, 'M' => 904, 'N' => 710, 'O' => 716, Chris@441: 'P' => 605, 'Q' => 716, 'R' => 623, 'S' => 517, 'T' => 601, 'U' => 690, 'V' => 668, 'W' => 990, 'X' => 681, 'Y' => 634, Chris@441: 'Z' => 578, '[' => 316, '\\' => 614, ']' => 316, '^' => 529, '_' => 500, '`' => 387, 'a' => 509, 'b' => 566, 'c' => 478, Chris@441: 'd' => 565, 'e' => 503, 'f' => 337, 'g' => 549, 'h' => 580, 'i' => 275, 'j' => 266, 'k' => 544, 'l' => 276, 'm' => 854, Chris@441: 'n' => 579, 'o' => 550, 'p' => 578, 'q' => 566, 'r' => 410, 's' => 444, 't' => 340, 'u' => 575, 'v' => 512, 'w' => 760, Chris@441: 'x' => 503, 'y' => 529, 'z' => 453, '{' => 326, '|' => 380, '}' => 326, '~' => 387} 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("CID font already added: family style") Chris@441: end Chris@441: i=@fonts.length+1 Chris@441: @fonts[fontkey]={'i'=>i,'type'=>'Type0','name'=>name,'up'=>-120,'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 AddSJISFont(family='SJIS') Chris@441: #Add SJIS font with proportional Latin Chris@441: name='KozMinPro-Regular-Acro' Chris@441: cw=SJIS_widths Chris@441: cMap='90msp-RKSJ-H' Chris@441: registry={'ordering'=>'Japan1','supplement'=>2} Chris@441: AddCIDFonts(family,name,cw,cMap,registry) Chris@441: end Chris@441: Chris@441: def AddSJIShwFont(family='SJIS-hw') Chris@441: #Add SJIS font with half-width Latin Chris@441: name='KozMinPro-Regular-Acro' Chris@441: 32.upto(126) do |i| Chris@441: cw[i.chr]=500 Chris@441: end Chris@441: cMap='90ms-RKSJ-H' Chris@441: registry={'ordering'=>'Japan1','supplement'=>2} 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 GetSJISStringWidth(s) Chris@441: else Chris@441: return super(s) Chris@441: end Chris@441: end Chris@441: Chris@441: def GetSJISStringWidth(s) Chris@441: #SJIS 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(i=161 and o<=223) Chris@441: #Half-width katakana Chris@441: l+=500 Chris@441: i+=1 Chris@441: else Chris@441: #Full-width character Chris@441: l+=1000 Chris@441: i+=2 Chris@441: end Chris@441: end Chris@441: return l*@font_size/1000 Chris@441: end Chris@441: Chris@507: def MultiCell(w,h,txt,border=0,align='L',fill=0,ln=1) Chris@441: if(@current_font['type']=='Type0') Chris@909: SJISMultiCell(w,h,txt,border,align,fill,ln) Chris@441: else Chris@909: super(w,h,txt,border,align,fill,ln) Chris@441: end Chris@441: end Chris@441: Chris@909: def SJISMultiCell(w,h,txt,border=0,align='L',fill=0,ln=1) Chris@909: Chris@909: # save current position Chris@909: prevx = @x; Chris@909: prevy = @y; Chris@909: Chris@441: #Output text with automatic or explicit line breaks Chris@441: cw=@current_font['cw'] Chris@441: if(w==0) Chris@441: w=@w-@r_margin-@x Chris@441: end Chris@441: wmax=(w-2*@c_margin)*1000/@font_size Chris@441: s=txt.gsub("\r",'') Chris@441: nb=s.length Chris@441: if(nb>0 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(i=161 and o<=223) Chris@441: #Half-width katakana Chris@441: l+=500 Chris@441: n=1 Chris@441: sep=i Chris@441: else Chris@441: #Full-width character Chris@441: l+=1000 Chris@441: n=2 Chris@441: sep=i Chris@441: end Chris@441: if(l>wmax) Chris@441: #Automatic line break Chris@441: if(sep==-1 or i==j) Chris@441: if(i==j) Chris@441: i+=n 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+=n Chris@441: if(o>=128) Chris@441: sep=i Chris@441: end 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@909: Chris@909: # move cursor to specified position Chris@909: if (ln == 1) Chris@909: # go to the beginning of the next line Chris@909: @x=@l_margin Chris@909: elsif (ln == 0) Chris@909: # go to the top-right of the cell Chris@909: @y = prevy; Chris@909: @x = prevx + w; Chris@909: elsif (ln == 2) Chris@909: # go to the bottom-left of the cell Chris@909: @x = prevx; Chris@909: end Chris@441: end Chris@441: Chris@507: def Write(h,txt,link='',fill=0) Chris@441: if(@current_font['type']=='Type0') Chris@909: SJISWrite(h,txt,link,fill) Chris@441: else Chris@909: super(h,txt,link,fill) Chris@441: end Chris@441: end Chris@441: Chris@909: def SJISWrite(h,txt,link,fill=0) Chris@441: #SJIS 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(i=161 and o<=223) Chris@441: #Half-width katakana Chris@441: l+=500 Chris@441: n=1 Chris@441: sep=i Chris@441: else Chris@441: #Full-width character Chris@441: l+=1000 Chris@441: n=2 Chris@441: sep=i Chris@441: end Chris@441: if(l>wmax) 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+=n Chris@441: nl+=1 Chris@441: next Chris@441: end Chris@441: if(i==j) Chris@441: i+=n Chris@441: end Chris@909: Cell(w,h,s[j,i-j],0,2,'',fill,link) Chris@441: else Chris@909: Cell(w,h,s[j,sep-j],0,2,'',fill,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+=n Chris@441: if(o>=128) Chris@441: sep=i Chris@441: end Chris@441: end Chris@441: end Chris@441: #Last chunk Chris@441: if(i!=j) Chris@909: Cell(l*@font_size/1000.0,h,s[j,i-j],0,0,'',fill,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: w='/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: out(w+'] 231 325 500 631 [500] 326 389 500]') Chris@441: out('>>') Chris@441: out('endobj') Chris@441: #Font descriptor Chris@441: newobj() Chris@441: out('<>') Chris@441: out('endobj') Chris@441: end Chris@441: end