Source for file svgGrapher.internal.getComputedTextLength.php

Documentation is available at svgGrapher.internal.getComputedTextLength.php

  1. <?php
  2. /**
  3.  * Plugin internal getComputedTextLength.
  4.  *
  5.  * @version $Id: svgGrapher.internal.getComputedTextLength.php,v 1.5 2007/10/23 08:59:08 geelweb Exp $
  6.  * @author Guillaume L. <guillaume@geelweb.org>
  7.  * @copyright Copyright © 2006, Guillaume Luchet.
  8.  * @license http://opensource.org/licenses/bsd-license.php BSD License
  9.  * @package SvgGrapher
  10.  * @subpackage Internal
  11.  */
  12.  
  13. /**
  14.  *
  15.  */
  16. require_once SVGGRAPHER_ELEMENTS_PATH . '/TextElement.php';
  17.  
  18. /**
  19.  * svgGrapher_internal_getComputedTextLength()
  20.  *
  21.  * Get the text size for a givan font.
  22.  * Need GD.
  23.  *
  24.  * It's ok just with standard fonts :
  25.  * - arial
  26.  * - times new roman
  27.  * - verdana
  28.  * - sans-serif
  29.  * - serif
  30.  * - garamond
  31.  * - georgia
  32.  * - trebuchet
  33.  * - courier new
  34.  *
  35.  * If $textElm is a string a TextElement will be create with style $st and
  36.  * transform $tr.
  37.  *
  38.  * @param mixed $textElm TextElement or svg string style
  39.  * @param mixed $st svg Style element
  40.  * @param mixed $tr svg Transform element
  41.  * @return integer 
  42.  */
  43. function svgGrapher_internal_getComputedTextLength($textElm, $st=NULL, $tr=NULL) {
  44.     $fontFileExt = '.ttf';
  45.     $fontFileName = '';
  46.     $fontBold = 'b';
  47.     $fontItalic = 'i';
  48.     $sizeEquiv = 0;
  49.     $svgFonts = array(
  50.         'arial' => 'arial',
  51.         'times new roman' => 'times',
  52.         'verdana' => 'verdana',
  53.         'sans-serif' => 'arial',
  54.         'serif' => 'times',
  55.         'garamond' => 'gara',
  56.         'georgia' => 'georgia',
  57.         'trebuchet' => 'trebuc',
  58.         'courier new' => 'cour' );
  59.  
  60.     // Convertie $textElm en objet de type SvgText si il s'agit d'un "string".
  61.     // Lui affecte $st en style et $tr en element de transformation.
  62.     if(!is_object($textElm)) {
  63.         $textElm = new TextElement($textElm);
  64.         $textElm->setAttribute('style', $st);
  65.         $textElm->setAttribute('transform', $tr);
  66.     }
  67.  
  68.     $text = $textElm->content;
  69.     $style = $textElm->getAttribute('style');
  70.     $transform = $textElm->getAttribute('transform');
  71.  
  72.     $fontFamily = ($style->getAttribute('font-family') !== false) ? 
  73.         strtolower( $style->getAttribute('font-family') ) : 'arial';
  74.     $fontSize = ($style->getAttribute('font-size') !== false) ? 
  75.         $style->getAttribute('font-size') : 12;
  76.     $fontAngle = ($transform->get('rotate') !== false) ? 
  77.         trim( substr($transform->get('rotate'), 0, 3) ) : 0;
  78.  
  79.     if(array_key_exists($fontFamily, $svgFonts)) {
  80.         // Fonction svgGrapher_internal_fontSizeEquivPt inclus par SvgGrapher.
  81.         if(($sizeEquiv = svgGrapher_internal_fontSizeEquivPt($style)) === false) {
  82.             throw new Exception('FontSize in relative units, cannot determine FontSize');
  83.             exit(-1);
  84.         }
  85.  
  86.         $fontFileName = $svgFonts[$fontFamily];
  87.  
  88.         if($style->getAttribute('font-weight') == 'bold') { 
  89.             $fontFileName .= $fontBold; 
  90.         }
  91.         if($style->getAttribute('font-style') == 'italic') { 
  92.             $fontFileName .= $fontItalic; 
  93.         }
  94.  
  95.         $fontFileName = TTF_FONTS_PATH . $fontFileName . $fontFileExt;
  96.  
  97.         $bbox = imagettfbbox($sizeEquiv, $fontAngle, $fontFileName, $text);
  98.         $tmp_bbox['left'] = 0 - min($bbox[0], $bbox[2], $bbox[4], $bbox[6]);
  99.         $tmp_bbox['top'] = 0 - min($bbox[1], $bbox[3], $bbox[5], $bbox[7]);
  100.         $tmp_bbox['width'] = max($bbox[0], $bbox[2], $bbox[4], $bbox[6]) - 
  101.             min($bbox[0], $bbox[2], $bbox[4], $bbox[6]) + 1;
  102.         $tmp_bbox['height'] = max($bbox[1], $bbox[3], $bbox[5], $bbox[7]) - 
  103.             min($bbox[1], $bbox[3], $bbox[5], $bbox[7]);
  104.  
  105.         return $tmp_bbox;
  106.     }
  107.  
  108.     return false;
  109. }
  110.  
  111. ?>

Documentation generated on Tue, 23 Oct 2007 11:32:11 +0200 by phpDocumentor 1.4.0