Пример #1
0
 /**
  * Get wrapped text
  *
  * @param string $string String
  * @param Zend_Pdf_Style $style Style(font, fontsize, line width)
  * @param bool $force Use force wrap ( break words )
  *
  * @return string Wrapped string
  */
 private function getWrappedText($string, Zend_Pdf_Style $style, $force = false)
 {
     $wrappedText = '';
     $max_width = $style->getLineWidth();
     $lines = explode("\n", $string);
     foreach ($lines as $line) {
         $words = explode(' ', $line);
         // Force wrap like:
         // Fo
         // rc
         // e
         //
         if ($force) {
             $array = array('');
             foreach ($words as $word) {
                 $width = $this->widthForStringUsingFontsize($word, $style->getFont(), $style->getFontsize());
                 if ($width > $max_width) {
                     $width = intval($width / $max_width + 1);
                     $w = str_split($word, 2 * intval(strlen($word) / (2 * $width)));
                     foreach ($w as $wd) {
                         if ($this->widthForStringUsingFontsize($array[count($array) - 1] . $wd, $style->getFont(), $style->getFontsize()) < $max_width) {
                             $array[count($array) - 1] .= $wd;
                         } else {
                             $array[] = $wd;
                         }
                     }
                 } else {
                     $array[] = $word;
                 }
             }
             $words = $array;
         }
         $word_count = count($words);
         $i = 0;
         $wrappedLine = '';
         while ($i < $word_count) {
             if ($this->widthForStringUsingFontsize($wrappedLine . ' ' . $words[$i], $style->getFont(), $style->getFontsize()) < $max_width) {
                 if (!empty($wrappedLine)) {
                     $wrappedLine .= ' ';
                 }
                 $wrappedLine .= $words[$i];
             } else {
                 $wrappedText .= $wrappedLine . "\n";
                 $wrappedLine = $words[$i];
             }
             $i++;
         }
         $wrappedText .= $wrappedLine . "\n";
     }
     return ltrim($wrappedText);
 }