Example #1
0
 /**
  * Return width of text
  * Returns the width of text in an arbitrary font.
  *  
  * @param string $text Text
  *
  * @return float Width of text
  */
 public function stringwidth($text)
 {
     //Source: http://stackoverflow.com/a/8076461
     if ($this->_page instanceof Zend_Pdf_Page) {
         $font = $this->_page->getFont();
         $fontSize = $this->_page->getFontSize();
     } elseif ($this->_page instanceof Zend_Pdf_Resource_Font) {
         $font = $this->_page;
         if ($fontSize === null) {
             return false;
         }
     }
     if (!$font instanceof Zend_Pdf_Resource_Font) {
         $this->_errmsg = "Could not find font";
         return false;
     }
     $drawingText = $text;
     //iconv ( '', $encoding, $text );
     $characters = array();
     for ($i = 0; $i < strlen($drawingText); $i++) {
         $characters[] = ord($drawingText[$i]);
     }
     $glyphs = $font->glyphNumbersForCharacters($characters);
     $widths = $font->widthsForGlyphs($glyphs);
     $textWidth = array_sum($widths) / $font->getUnitsPerEm() * $fontSize;
     return $textWidth;
 }
Example #2
0
 public function drawHtml(Page $page, $html, $x1, $y1, $x2 = null, $y2 = null, $lineHeight = 1.3, $charEncoding = '', $debug = false)
 {
     if (is_null($x2)) {
         $x2 = $page->getWidth();
     }
     if (is_null($y2)) {
         $y2 = 0;
     }
     if ($debug === true) {
         var_dump(array('x1' => $x1, 'y1' => $y1, 'x2' => $x2, 'y2' => $y2));
     }
     $x = $x1;
     $y = $y1;
     if (is_null($page->getFont())) {
         $page->setFont(Font::fontWithName(Font::FONT_HELVETICA), 10);
     }
     $defaultFont = $page->getFont();
     $defaultfontSize = $page->getFontSize();
     $maxFontSize = $defaultfontSize;
     $words = array();
     $elements = $this->getParser()->parse($html);
     if ($debug === true) {
         var_dump($elements);
     }
     foreach ($elements as $element) {
         if ($element instanceof DataElement) {
             $font = $element->getFont($defaultFont);
             $fontSize = $element->getFontSize($defaultfontSize);
             $maxFontSize = $fontSize > $maxFontSize ? $fontSize : $maxFontSize;
             $rawWords = explode(' ', $element->getValue());
             foreach ($rawWords as $rawWord) {
                 $rawWord .= $rawWord ? ' ' : '';
                 $wordWith = $this->widthForStringUsingFontSize($rawWord, $font, $fontSize);
                 if ($x + $wordWith > $x2) {
                     $x = $x1;
                     if ($y1 < $y2) {
                         $y += $maxFontSize * $lineHeight;
                     } else {
                         $y -= $maxFontSize * $lineHeight;
                     }
                     $maxFontSize = $fontSize;
                 }
                 $words[] = new Word($rawWord, $x, $y, $font, $fontSize);
                 $x += $wordWith;
             }
         } elseif ($element instanceof ControllElement) {
             if ($element->isBlockElement()) {
                 if ($x != $x1) {
                     $x = $x1;
                     if ($y1 < $y2) {
                         $y += $maxFontSize * $lineHeight;
                     } else {
                         $y -= $maxFontSize * $lineHeight;
                     }
                     $maxFontSize = $defaultfontSize;
                 }
                 if ($element instanceof StartElement) {
                     if (!is_null($element->marginTop())) {
                         if ($y1 < $y2) {
                             $y += $element->marginTop();
                         } else {
                             $y -= $element->marginTop();
                         }
                     }
                     if (!is_null($element->marginLeft())) {
                         if (!is_null($element->getListSign())) {
                             $words[] = new Word($element->getListSign(), $x, $y, $element->getFont($defaultFont), $element->getFontSize($defaultfontSize));
                         }
                         $x1 += $element->marginLeft();
                         $x = $x1;
                     }
                 }
                 if ($element instanceof StopElement) {
                     if (!is_null($element->marginBottom())) {
                         if ($y1 < $y2) {
                             $y += $element->marginBottom();
                         } else {
                             $y -= $element->marginBottom();
                         }
                     }
                     if (!is_null($element->marginLeft())) {
                         $x1 -= $element->marginLeft();
                         $x = $x1;
                     }
                 }
             }
         }
     }
     if ($debug === true) {
         var_dump($words);
     }
     foreach ($words as $word) {
         /** @var Word $word */
         $page->setFont($word->getFont(), $word->getFontSize());
         $page->drawText($word->getText(), $word->getX(), $word->getY(), $charEncoding);
     }
     if ($debug === true) {
         die;
     }
     return array($x, $y);
 }