Method: FPDF#MultiCell

Defined in:
lib/fpdf.rb

#MultiCell(w, h, txt, border = 0, align = 'J', fill = 0) ⇒ Object



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/fpdf.rb', line 655

def MultiCell(w,h,txt,border=0,align='J',fill=0)
    # Output text with automatic or explicit line breaks
    cw=@CurrentFont['cw']
    w=@w-@rMargin-@x if w==0
    wmax=(w-2*@cMargin)*1000/@FontSize
    s=txt.gsub('\r','')
    nb=s.length
    nb=nb-1 if nb>0 and s[nb-1].chr=='\n'
    b=0
    if border!=0
        if border==1
            border='LTRB'
            b='LRT'
            b2='LR'
        else
            b2=''
            b2='L' unless border.index('L').nil?
            b2=b2+'R' unless border.index('R').nil?
            b=(not border.index('T').nil?) ? (b2+'T') : b2
        end
    end
    sep=-1
    i=0
    j=0
    l=0
    ns=0
    nl=1
    while i<nb
        # Get next character
        c=s[i].chr
        if c=="\n"
            # Explicit line break
            if @ws>0
                @ws=0
                out('0 Tw')
            end
            
            # Changed from s[j..i] to fix bug reported by Hans Allis.
            self.Cell(w,h,s[j..i-1],b,2,align,fill) 
            
            i=i+1
            sep=-1
            j=i
            l=0
            ns=0
            nl=nl+1
            b=b2 if border and nl==2
        else
            if c==' '
                sep=i
                ls=l
                ns=ns+1
            end
            l=l+cw[c[0]]
            if l>wmax
                # Automatic line break
                if sep==-1
                    i=i+1 if i==j
                    if @ws>0
                        @ws=0
                        out('0 Tw')
                    end
                    self.Cell(w,h,s[j..i],b,2,align,fill)
                else
                    if align=='J'
                        @ws=(ns>1) ? (wmax-ls)/1000.0*@FontSize/(ns-1) : 0
                        out(sprintf('%.3f Tw',@ws*@k))
                    end
                    self.Cell(w,h,s[j..sep],b,2,align,fill)
                    i=sep+1
                end
                sep=-1
                j=i
                l=0
                ns=0
                nl=nl+1
                b=b2 if border and nl==2
            else
                i=i+1
            end
        end
    end

    # Last chunk
    if @ws>0
        @ws=0
        out('0 Tw')
    end
    b=b+'B' if border!=0 and not border.index('B').nil?
    self.Cell(w,h,s[j..i],b,2,align,fill)
    @x=@lMargin
end