Logo Search packages:      
Sourcecode: acidbase version File versions  Download package

Cpdf::addTextWrap ( x,
y,
width,
size,
text,
justification = 'left',
angle = 0,
test = 0 
)

add text to the page, but ensure that it fits within a certain width if it does not fit then put in as much as possible, splitting at word boundaries and return the remainder. justification and angle can also be specified for the text

Definition at line 2331 of file class.pdf.php.

References addText(), PRVTadjustWrapText(), PRVTcheckTextDirective(), selectFont(), and setCurrentFont().

                                                                                     {
  // this will display the text, and if it goes beyond the width $width, will backtrack to the 
  // previous space or hyphen, and return the remainder of the text.

  // $justification can be set to 'left','right','center','centre','full'

  // need to store the initial text state, as this will change during the width calculation
  // but will need to be re-set before printing, so that the chars work out right
  $store_currentTextState = $this->currentTextState;

  if (!$this->numFonts){$this->selectFont('./fonts/Helvetica');}
  if ($width<=0){
    // error, pretend it printed ok, otherwise risking a loop
    return '';
  }
  $w=0;
  $break=0;
  $breakWidth=0;
  $len=strlen($text);
  $cf = $this->currentFont;
  $tw = $width/$size*1000;
  for ($i=0;$i<$len;$i++){
    $f=1;
    $directive = $this->PRVTcheckTextDirective($text,$i,$f);
    if ($directive){
      if ($f){
        $this->setCurrentFont();
        $cf = $this->currentFont;
      }
      $i=$i+$directive-1;
    } else {
      $cOrd = ord($text[$i]);
      if (isset($this->fonts[$cf]['differences'][$cOrd])){
        // then this character is being replaced by another
        $cOrd2 = $this->fonts[$cf]['differences'][$cOrd];
      } else {
        $cOrd2 = $cOrd;
      }
  
      if (isset($this->fonts[$cf]['C'][$cOrd2]['WX'])){
        $w+=$this->fonts[$cf]['C'][$cOrd2]['WX'];
      }
      if ($w>$tw){
        // then we need to truncate this line
        if ($break>0){
          // then we have somewhere that we can split :)
          if ($text[$break]==' '){
            $tmp = substr($text,0,$break);
          } else {
            $tmp = substr($text,0,$break+1);
          }
          $adjust=0;
          $this->PRVTadjustWrapText($tmp,$breakWidth,$width,$x,$adjust,$justification);

          // reset the text state
          $this->currentTextState = $store_currentTextState;
          $this->setCurrentFont();
          if (!$test){
            $this->addText($x,$y,$size,$tmp,$angle,$adjust);
          }
          return substr($text,$break+1);
        } else {
          // just split before the current character
          $tmp = substr($text,0,$i);
          $adjust=0;
          $ctmp=ord($text[$i]);
          if (isset($this->fonts[$cf]['differences'][$ctmp])){
            $ctmp=$this->fonts[$cf]['differences'][$ctmp];
          }
          $tmpw=($w-$this->fonts[$cf]['C'][$ctmp]['WX'])*$size/1000;
          $this->PRVTadjustWrapText($tmp,$tmpw,$width,$x,$adjust,$justification);
          // reset the text state
          $this->currentTextState = $store_currentTextState;
          $this->setCurrentFont();
          if (!$test){
            $this->addText($x,$y,$size,$tmp,$angle,$adjust);
          }
          return substr($text,$i);
        }
      }
      if ($text[$i]=='-'){
        $break=$i;
        $breakWidth = $w*$size/1000;
      }
      if ($text[$i]==' '){
        $break=$i;
        $ctmp=ord($text[$i]);
        if (isset($this->fonts[$cf]['differences'][$ctmp])){
          $ctmp=$this->fonts[$cf]['differences'][$ctmp];
        }
        $breakWidth = ($w-$this->fonts[$cf]['C'][$ctmp]['WX'])*$size/1000;
      }
    }
  }
  // then there was no need to break this line
  if ($justification=='full'){
    $justification='left';
  }
  $adjust=0;
  $tmpw=$w*$size/1000;
  $this->PRVTadjustWrapText($text,$tmpw,$width,$x,$adjust,$justification);
  // reset the text state
  $this->currentTextState = $store_currentTextState;
  $this->setCurrentFont();
  if (!$test){
    $this->addText($x,$y,$size,$text,$angle,$adjust,$angle);
  }
  return '';
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index