Exemplo n.º 1
0
 /**
  * Calculate text width
  * 
  * @param string $text
  * @param Zend_Pdf_Page|Zend_Pdf_Resource_Font $resource
  * @param int $fontSize
  * @param string $encoding
  * @return double
  */
 public function getTextWidth($text, $resource, $fontSize = null, $encoding = 'UTF-8')
 {
     if ($resource instanceof Zend_Pdf_Page) {
         $font = $resource->getFont();
         $fontSize = $resource->getFontSize();
     } elseif ($resource instanceof Zend_Pdf_Resource_Font) {
         $font = $resource;
         if ($fontSize === null) {
             throw new Exception('The fontsize is unknown');
         }
     }
     if (!$font instanceof Zend_Pdf_Resource_Font) {
         throw new Exception('Invalid resource passed');
     }
     //$drawingText = iconv ( '', $encoding, $text );
     $drawingText = $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;
 }