<?php
require('fpdf.php');
class PDF_SmallCaps extends FPDF
{
/**
* Create the Small Caps effect in a Cell
* @author constantin teleman
*/
function CellSmallCaps($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
{
//Output a cell
$txt=(string)$txt;
$k=$this->k;
if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
{
//Automatic page break
$x=$this->x;
$ws=$this->ws;
if($ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$this->AddPage($this->CurOrientation,$this->CurPageSize);
$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 || $border==1)
{
if($fill)
$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!=='')
{
if(!isset($this->CurrentFont))
$this->Error('No font has been set');
if($align=='R')
$dx=$w-$this->cMargin-$this->GetStringWidth($txt);
elseif($align=='C')
$dx=($w-$this->GetStringWidth($txt))/2;
else
$dx=$this->cMargin;
if($this->ColorFlag)
$s.='q '.$this->TextColor.' ';
$txt2=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
$firstDx = $dx;
for ($i=0; $i<strlen($txt2); $i++) {
$letter = $txt2[$i];
$upper = strpos("ABCDEFGHIJKLMNOPQRSTUVWXZ0123456789`!@#$%^&*()_=+|'\";:{}[]?,.<>",$letter);
if ($upper === false) { // the letter is lowercase
if($i==0){ // for the first char
$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 { // if there is no first char
$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 { //case the letter is uppercase
if($i == 0){ //for the first char
$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 { // if there is no first char
$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)
{
//Go to next line
$this->y+=$h;
if($ln==1)
$this->x=$this->lMargin;
}
else
$this->x+=$w;
}
/**
* Create the Small Caps effect in a MultiCell
* @author constantin teleman
*/
function MultiCellSmallCaps($w, $h, $txt, $border=0, $align='', $fill=false)
{
//Output text with automatic or explicit line breaks
if(!isset($this->CurrentFont))
$this->Error('No font has been set');
$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",'',(string)$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)
{
//Get next character
$c=$s[$i];
if($c=="\n")
{
//Explicit line break
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)
{
//Automatic line break
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++;
}
//Last chunk
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;
}
}
?>