Chris@909: # Copyright (c) 2006 4ssoM LLC Chris@909: # 1.12 contributed by Ed Moss. Chris@909: # Chris@909: # The MIT License Chris@909: # Chris@909: # Permission is hereby granted, free of charge, to any person obtaining a copy Chris@909: # of this software and associated documentation files (the "Software"), to deal Chris@909: # in the Software without restriction, including without limitation the rights Chris@909: # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@909: # copies of the Software, and to permit persons to whom the Software is Chris@909: # furnished to do so, subject to the following conditions: Chris@909: # Chris@909: # The above copyright notice and this permission notice shall be included in Chris@909: # all copies or substantial portions of the Software. Chris@909: # Chris@909: # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@909: # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@909: # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@909: # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@909: # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@909: # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@909: # THE SOFTWARE. Chris@909: # Chris@909: # This is direct port of japanese.php Chris@909: # Chris@909: # Japanese PDF support. Chris@909: # Chris@909: # Usage is as follows: Chris@909: # Chris@909: # require 'fpdf' Chris@909: # require 'chinese' Chris@909: # pdf = FPDF.new Chris@909: # pdf.extend(PDF_Japanese) Chris@909: # Chris@909: # This allows it to be combined with other extensions, such as the bookmark Chris@909: # module. Chris@909: Chris@909: module PDF_Japanese Chris@909: Chris@909: SJIS_widths={' ' => 278, '!' => 299, '"' => 353, '#' => 614, '$' => 614, '%' => 721, '&' => 735, '\'' => 216, Chris@909: '(' => 323, ')' => 323, '*' => 449, '+' => 529, ',' => 219, '-' => 306, '.' => 219, '/' => 453, '0' => 614, '1' => 614, Chris@909: '2' => 614, '3' => 614, '4' => 614, '5' => 614, '6' => 614, '7' => 614, '8' => 614, '9' => 614, ':' => 219, ';' => 219, Chris@909: '<' => 529, '=' => 529, '>' => 529, '?' => 486, '@' => 744, 'A' => 646, 'B' => 604, 'C' => 617, 'D' => 681, 'E' => 567, Chris@909: 'F' => 537, 'G' => 647, 'H' => 738, 'I' => 320, 'J' => 433, 'K' => 637, 'L' => 566, 'M' => 904, 'N' => 710, 'O' => 716, Chris@909: 'P' => 605, 'Q' => 716, 'R' => 623, 'S' => 517, 'T' => 601, 'U' => 690, 'V' => 668, 'W' => 990, 'X' => 681, 'Y' => 634, Chris@909: 'Z' => 578, '[' => 316, '\\' => 614, ']' => 316, '^' => 529, '_' => 500, '`' => 387, 'a' => 509, 'b' => 566, 'c' => 478, Chris@909: 'd' => 565, 'e' => 503, 'f' => 337, 'g' => 549, 'h' => 580, 'i' => 275, 'j' => 266, 'k' => 544, 'l' => 276, 'm' => 854, Chris@909: 'n' => 579, 'o' => 550, 'p' => 578, 'q' => 566, 'r' => 410, 's' => 444, 't' => 340, 'u' => 575, 'v' => 512, 'w' => 760, Chris@909: 'x' => 503, 'y' => 529, 'z' => 453, '{' => 326, '|' => 380, '}' => 326, '~' => 387} Chris@909: Chris@909: def AddCIDFont(family,style,name,cw,cMap,registry) Chris@909: fontkey=family.downcase+style.upcase Chris@909: unless @fonts[fontkey].nil? Chris@909: Error("CID font already added: family style") Chris@909: end Chris@909: i=@fonts.length+1 Chris@909: @fonts[fontkey]={'i'=>i,'type'=>'Type0','name'=>name,'up'=>-120,'ut'=>40,'cw'=>cw, Chris@909: 'CMap'=>cMap,'registry'=>registry} Chris@909: end Chris@909: Chris@909: def AddCIDFonts(family,name,cw,cMap,registry) Chris@909: AddCIDFont(family,'',name,cw,cMap,registry) Chris@909: AddCIDFont(family,'B',name+',Bold',cw,cMap,registry) Chris@909: AddCIDFont(family,'I',name+',Italic',cw,cMap,registry) Chris@909: AddCIDFont(family,'BI',name+',BoldItalic',cw,cMap,registry) Chris@909: end Chris@909: Chris@909: def AddSJISFont(family='SJIS') Chris@909: #Add SJIS font with proportional Latin Chris@909: name='KozMinPro-Regular-Acro' Chris@909: cw=SJIS_widths Chris@909: cMap='90msp-RKSJ-H' Chris@909: registry={'ordering'=>'Japan1','supplement'=>2} Chris@909: AddCIDFonts(family,name,cw,cMap,registry) Chris@909: end Chris@909: Chris@909: def AddSJIShwFont(family='SJIS-hw') Chris@909: #Add SJIS font with half-width Latin Chris@909: name='KozMinPro-Regular-Acro' Chris@909: 32.upto(126) do |i| Chris@909: cw[i.chr]=500 Chris@909: end Chris@909: cMap='90ms-RKSJ-H' Chris@909: registry={'ordering'=>'Japan1','supplement'=>2} Chris@909: AddCIDFonts(family,name,cw,cMap,registry) Chris@909: end Chris@909: Chris@909: def GetStringWidth(s) Chris@909: if(@current_font['type']=='Type0') Chris@909: return GetSJISStringWidth(s) Chris@909: else Chris@909: return super(s) Chris@909: end Chris@909: end Chris@909: Chris@909: def GetSJISStringWidth(s) Chris@909: #SJIS version of GetStringWidth() Chris@909: l=0 Chris@909: cw=@current_font['cw'] Chris@909: nb=s.length Chris@909: i=0 Chris@909: while(i=161 and o<=223) Chris@909: #Half-width katakana Chris@909: l+=500 Chris@909: i+=1 Chris@909: else Chris@909: #Full-width character Chris@909: l+=1000 Chris@909: i+=2 Chris@909: end Chris@909: end Chris@909: return l*@font_size/1000 Chris@909: end Chris@909: Chris@909: def MultiCell(w,h,txt,border=0,align='L',fill=0,ln=1) Chris@909: if(@current_font['type']=='Type0') Chris@909: SJISMultiCell(w,h,txt,border,align,fill,ln) Chris@909: else Chris@909: super(w,h,txt,border,align,fill,ln) Chris@909: end Chris@909: end Chris@909: 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@909: #Output text with automatic or explicit line breaks Chris@909: cw=@current_font['cw'] Chris@909: if(w==0) Chris@909: w=@w-@r_margin-@x Chris@909: end Chris@909: wmax=(w-2*@c_margin)*1000/@font_size Chris@909: s=txt.gsub("\r",'') Chris@909: nb=s.length Chris@909: if(nb>0 and s[nb-1]=="\n") Chris@909: nb-=1 Chris@909: end Chris@909: b=0 Chris@909: if(border) Chris@909: if(border==1) Chris@909: border='LTRB' Chris@909: b='LRT' Chris@909: b2='LR' Chris@909: else Chris@909: b2='' Chris@909: b2='L' unless border.to_s.index('L').nil? Chris@909: b2=b2+'R' unless border.to_s.index('R').nil? Chris@909: b=(border.to_s.index('T')) ? (b2+'T') : b2 Chris@909: end Chris@909: end Chris@909: sep=-1 Chris@909: i=0 Chris@909: j=0 Chris@909: l=0 Chris@909: nl=1 Chris@909: while(i=161 and o<=223) Chris@909: #Half-width katakana Chris@909: l+=500 Chris@909: n=1 Chris@909: sep=i Chris@909: else Chris@909: #Full-width character Chris@909: l+=1000 Chris@909: n=2 Chris@909: sep=i Chris@909: end Chris@909: if(l>wmax) Chris@909: #Automatic line break Chris@909: if(sep==-1 or i==j) Chris@909: if(i==j) Chris@909: i+=n Chris@909: end Chris@909: Cell(w,h,s[j,i-j],b,2,align,fill) Chris@909: else Chris@909: Cell(w,h,s[j,sep-j],b,2,align,fill) Chris@909: i=(s[sep].chr==' ') ? sep+1 : sep Chris@909: end Chris@909: sep=-1 Chris@909: j=i Chris@909: l=0 Chris@909: nl+=1 Chris@909: if(border and nl==2) Chris@909: b=b2 Chris@909: end Chris@909: else Chris@909: i+=n Chris@909: if(o>=128) Chris@909: sep=i Chris@909: end Chris@909: end Chris@909: end Chris@909: #Last chunk Chris@909: if(border and not border.to_s.index('B').nil?) Chris@909: b+='B' Chris@909: end Chris@909: 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@909: end Chris@909: Chris@909: def Write(h,txt,link='',fill=0) Chris@909: if(@current_font['type']=='Type0') Chris@909: SJISWrite(h,txt,link,fill) Chris@909: else Chris@909: super(h,txt,link,fill) Chris@909: end Chris@909: end Chris@909: Chris@909: def SJISWrite(h,txt,link,fill=0) Chris@909: #SJIS version of Write() Chris@909: cw=@current_font['cw'] Chris@909: w=@w-@r_margin-@x Chris@909: wmax=(w-2*@c_margin)*1000/@font_size Chris@909: s=txt.gsub("\r",'') Chris@909: nb=s.length Chris@909: sep=-1 Chris@909: i=0 Chris@909: j=0 Chris@909: l=0 Chris@909: nl=1 Chris@909: while(i=161 and o<=223) Chris@909: #Half-width katakana Chris@909: l+=500 Chris@909: n=1 Chris@909: sep=i Chris@909: else Chris@909: #Full-width character Chris@909: l+=1000 Chris@909: n=2 Chris@909: sep=i Chris@909: end Chris@909: if(l>wmax) Chris@909: #Automatic line break Chris@909: if(sep==-1 or i==j) Chris@909: if(@x>@l_margin) Chris@909: #Move to next line Chris@909: @x=@l_margin Chris@909: @y+=h Chris@909: w=@w-@r_margin-@x Chris@909: wmax=(w-2*@c_margin)*1000/@font_size Chris@909: i+=n Chris@909: nl+=1 Chris@909: next Chris@909: end Chris@909: if(i==j) Chris@909: i+=n Chris@909: end Chris@909: Cell(w,h,s[j,i-j],0,2,'',fill,link) Chris@909: else Chris@909: Cell(w,h,s[j,sep-j],0,2,'',fill,link) Chris@909: i=(s[sep].chr==' ') ? sep+1 : sep Chris@909: end Chris@909: sep=-1 Chris@909: j=i Chris@909: l=0 Chris@909: if(nl==1) Chris@909: @x=@l_margin Chris@909: w=@w-@r_margin-@x Chris@909: wmax=(w-2*@c_margin)*1000/@font_size Chris@909: end Chris@909: nl+=1 Chris@909: else Chris@909: i+=n Chris@909: if(o>=128) Chris@909: sep=i Chris@909: end Chris@909: end Chris@909: end Chris@909: #Last chunk Chris@909: if(i!=j) Chris@909: Cell(l*@font_size/1000.0,h,s[j,i-j],0,0,'',fill,link) Chris@909: end Chris@909: end Chris@909: Chris@909: private Chris@909: Chris@909: def putfonts() Chris@909: nf=@n Chris@909: @diffs.each do |diff| Chris@909: #Encodings Chris@909: newobj() Chris@909: out('<>') Chris@909: out('endobj') Chris@909: end Chris@909: # mqr=get_magic_quotes_runtime() Chris@909: # set_magic_quotes_runtime(0) Chris@909: @font_files.each_pair do |file, info| Chris@909: #Font file embedding Chris@909: newobj() Chris@909: @font_files[file]['n']=@n Chris@909: if(defined('FPDF_FONTPATH')) Chris@909: file=FPDF_FONTPATH+file Chris@909: end Chris@909: size=filesize(file) Chris@909: if(!size) Chris@909: Error('Font file not found') Chris@909: end Chris@909: out('<>') Chris@909: f=fopen(file,'rb') Chris@909: putstream(fread(f,size)) Chris@909: fclose(f) Chris@909: out('endobj') Chris@909: end Chris@909: # set_magic_quotes_runtime(mqr) Chris@909: @fonts.each_pair do |k, font| Chris@909: #Font objects Chris@909: newobj() Chris@909: @fonts[k]['n']=@n Chris@909: out('<>') Chris@909: out('endobj') Chris@909: if(font['type']!='core') Chris@909: #Widths Chris@909: newobj() Chris@909: cw=font['cw'] Chris@909: s='[' Chris@909: 32.upto(255) do |i| Chris@909: s+=cw[i.chr]+' ' Chris@909: end Chris@909: out(s+']') Chris@909: out('endobj') Chris@909: #Descriptor Chris@909: newobj() Chris@909: s='<>') Chris@909: out('endobj') Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def putType0(font) Chris@909: #Type0 Chris@909: out('/Subtype /Type0') Chris@909: out('/BaseFont /'+font['name']+'-'+font['CMap']) Chris@909: out('/Encoding /'+font['CMap']) Chris@909: out('/DescendantFonts ['+(@n+1).to_s+' 0 R]') Chris@909: out('>>') Chris@909: out('endobj') Chris@909: #CIDFont Chris@909: newobj() Chris@909: out('<>') Chris@909: out('/FontDescriptor '+(@n+1).to_s+' 0 R') Chris@909: w='/W [1 [' Chris@909: font['cw'].keys.sort.each {|key| Chris@909: w+=font['cw'][key].to_s + " " Chris@909: # ActionController::Base::logger.debug key.to_s Chris@909: # ActionController::Base::logger.debug font['cw'][key].to_s Chris@909: } Chris@909: out(w+'] 231 325 500 631 [500] 326 389 500]') Chris@909: out('>>') Chris@909: out('endobj') Chris@909: #Font descriptor Chris@909: newobj() Chris@909: out('<>') Chris@909: out('endobj') Chris@909: end Chris@909: end