Skripty : Malé velké písmena

Popis

Tento skript umožňuje psát text s malým velkým písmem. K dispozici jsou dva způsoby: CellSmallCaps() a MultiCellSmallCaps(), se stejnými parametry jako standardní verze.

Zdrojový kód


<?php
require('fpdf.php');


class PDF_SmallCaps extends FPDF
{

/**
* Vytvoří malé velké písmo v Cell

* @author constantin teleman
*/
function CellSmallCaps($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')

{
    // Výstup buňky
    $k=$this->k;
    if($this->y+$h>$this->PageBreakTrigger && !$this->InFooter && $this->AcceptPageBreak())

    {
        // Automatické odstránkování
        $x=$this->x;
        $ws=$this->ws;

        if($ws>0)
        {
            $this->ws=0;
            $this->_out('0 Tw');

        }
        $this->AddPage($this->CurOrientation);
        $this->x=$x;

        if($ws>0)
        {
            $this->ws=$ws;
            $this->_out(sprintf('%.3f Tw',$ws*$k));

        }
    }
    if($w==0)
        $w=$this->w-$this->rMargin-$this->x;

    $s='';
    if($fill==1 || $border==1)
    {

        if($fill==1)
            $op=($border==1) ? 'B' : 'f';

        else
            $op='S';
        $s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);

    }
    if(is_string($border))
    {
        $x=$this->x;

        $y=$this->y;
        if(strpos($border,'L')!==false)

            $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);

        if(strpos($border,'T')!==false)
            $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);

        if(strpos($border,'R')!==false)
            $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);

        if(strpos($border,'B')!==false)
            $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);

    }
    if($txt!=='')
    {
        // část pro výpočet polohy textu

        if($align=='R') // tarovnání doprava
            $dx=$w-$this->cMargin-$this->GetStringWidth($txt);

        elseif($align=='C') // zarovnání na střed
            $dx=($w-$this->GetStringWidth($txt))/2;

        else  // zarovnání doleva
            $dx=$this->cMargin;
            
            
        if($this->ColorFlag)

            $s.='q '.$this->TextColor.' ';
        
        $txt2=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));

        // První písmeno ve slově
        //$txtFirst = strtoupper(substr($txt2,0,1));
        //$txtRest = strtoupper(substr($txt2,1));
        $firstDx = $dx;

        for ($i=0; $i<strlen($txt2); $i++) {
            $letter = substr($txt2,$i,1);

            $upper = strpos("ABCDEFGHIJKLMNOPQRSTUVWXZ0123456789`!@#$%^&*()_=+|'\";:{}[]?,.<>",$letter);
            if ($upper === false) { // v případě, že je písmeno malé 

                if($i==0){ // pro první znak
                    $s.=sprintf(' BT /F%d %.2f Tf',$this->CurrentFont['i'],$this->FontSizePt*0.8);

                    $s.=sprintf(' %.2f %.2f Td (%s) Tj',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,strtoupper($letter));

                    $s.=sprintf(' /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt);

                } else {  // není-li první znak
                    $s.=sprintf(' BT /F%d %.2f Tf',$this->CurrentFont['i'],$this->FontSizePt*0.8);

                    $dx+=($this->GetStringWidth(substr($txt2,$i-1,1)))*1.02;

                    $s.=sprintf(' %.2f %.2f Td (%s) Tj',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,strtoupper($letter));

                    $s.=sprintf(' /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt);

                }
            } else { // v případě, že je písmeno velké
                if($i == 0){ // pro první znak

                    $s.=sprintf(' BT %.2f %.2f Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$letter);

                } else {  // není-li první znak
                    $dx+=($this->GetStringWidth(substr($txt2,$i-1,1)))*1.02;

                    $s.=sprintf(' BT %.2f %.2f Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$letter);

                }
            }
        }
        
        if($this->underline)
            $s.=' '.$this->_dounderline($this->x+$firstDx,$this->y+.5*$h+.3*$this->FontSize,$txt);

        if($this->ColorFlag)
            $s.=' Q';
        if($link)
            $this->Link($this->x+$firstDx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);

    }
    if($s)
        $this->_out($s);
    $this->lasth=$h;

    if($ln>0)
    {
        // Přejít na další řádek
        $this->y+=$h;

        if($ln==1)
            $this->x=$this->lMargin;
    }

    else
        $this->x+=$w;
}

/**
* Vytvoří malé velké písmo v MultiCell 

* @author constantin teleman
*/
function MultiCellSmallCaps($w,$h,$txt,$border=0,$align='',$fill=0)

{
    // Výstupní text s automatickým nebo s explicitním odřádkováním
    $cw=&$this->CurrentFont['cw'];

    if($w==0)
        $w=$this->w-$this->rMargin-$this->x;

    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;

    $s=str_replace("\r",'',$txt);
    $nb=strlen($s);

    if($nb>0 && $s[$nb-1]=="\n")
        $nb--;
    $b=0;

    if($border)
    {
        if($border==1)
        {
            $border='LTRB';

            $b='LRT';
            $b2='LR';
        }
        else
        {
            $b2='';

            if(strpos($border,'L')!==false)
                $b2.='L';
            if(strpos($border,'R')!==false)

                $b2.='R';
            $b=(strpos($border,'T')!==false) ? $b2.'T' : $b2;

        }
    }
    $sep=-1;
    $i=0;
    $j=0;

    $l=0;
    $ns=0;
    $nl=1;
    while($i<$nb)

    {
        // Získejte další znak
        $c=$s{$i};
        if($c=="\n")

        {
            // Explicitní ořádkování
            if($this->ws>0)
            {
                $this->ws=0;

                $this->_out('0 Tw');
            }
            $this->CellSmallCaps($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);

            $i++;
            $sep=-1;
            $j=$i;
            $l=0;

            $ns=0;
            $nl++;
            if($border && $nl==2)
                $b=$b2;

            continue;
        }
        if($c==' ')
        {
            $sep=$i;

            $ls=$l;
            $ns++;
        }
        $l+=$cw[$c];

        if($l>$wmax)
        {
            // Automatické odřádkování
            if($sep==-1)

            {
                if($i==$j)
                    $i++;
                if($this->ws>0)

                {
                    $this->ws=0;
                    $this->_out('0 Tw');

                }
                $this->CellSmallCaps($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);

            }
            else
            {
                if($align=='J')
                {
                    $this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;

                    $this->_out(sprintf('%.3f Tw',$this->ws*$this->k));

                }
                $this->CellSmallCaps($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);

                $i=$sep+1;
            }
            $sep=-1;
            $j=$i;

            $l=0;
            $ns=0;
            $nl++;
            if($border && $nl==2)

                $b=$b2;
        }
        else
            $i++;
    }
    // 

    if($this->ws>0)
    {
        $this->ws=0;
        $this->_out('0 Tw');

    }
    if($border && strpos($border,'B')!==false)
        $b.='B';

    $this->CellSmallCaps($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);

    $this->x=$this->lMargin;
}

}

?>


Příklad


<?php
require('smallcaps.php');

$pdf=new PDF_SmallCaps();

$pdf->AddPage();
$pdf->SetFont('Courier','',30);
for ($i = 30; $i>4; $i--) {

    $pdf->SetFontSize($i);
    $pdf->CellSmallCaps(180,8,"The Code and Text Editor",0,1);

}
$pdf->SetFontSize(10);
$pdf->MultiCellSmallCaps(180,6,'The Code and Text Editor is a word processing utility where you enter, display, and edit code or text.'.

    ' It is referred to as either the Text Editor or the Code Editor, based on its content.'.

    ' If it contains only text without an associated language, it is referred to as the Text Editor.'.

    ' If it contains source code associated with a language, it is referred to as the Code Editor.'.

    ' Because it is most often used for editing code, though, we will refer to it as the Code Editor.');

$pdf->Output();
?>


Ukázka

fpdf-male-velke-pismena.pdf

Download

fpdf-male-velke-pismena.zip

Informace

Author: Constantin Teleman
License: FPDF