diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-05-31 15:58:13 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-05-31 15:58:13 +0000 |
commit | ed1c86d6e19c1ec2255d0e76e914de035a961cae (patch) | |
tree | 30b927d70f3dbc865f8173128d3185b0ffdfc8cc /src/driver | |
parent | 0ac10a4984ccd0095d3fd45ce4d24cd7f45120eb (diff) | |
download | zetacomponents-graph-ed1c86d6e19c1ec2255d0e76e914de035a961cae.zip zetacomponents-graph-ed1c86d6e19c1ec2255d0e76e914de035a961cae.tar.gz |
- Completed GD driver
- Added font "Bitstream Vera Sans" owned by Bitstream for testing
Diffstat (limited to 'src/driver')
-rw-r--r-- | src/driver/gd.php | 95 | ||||
-rw-r--r-- | src/driver/svg.php | 2 |
2 files changed, 94 insertions, 3 deletions
diff --git a/src/driver/gd.php b/src/driver/gd.php index cec0a0d..43276b4 100644 --- a/src/driver/gd.php +++ b/src/driver/gd.php @@ -32,7 +32,9 @@ class ezcGraphGdDriver extends ezcGraphDriver if ( !isset( $this->image ) ) { $this->image = imagecreatetruecolor( $this->options->width, $this->options->height ); - imagecolorallocate( $this->image, 255, 255, 255 ); + $bgColor = imagecolorallocate( $this->image, 255, 255, 255 ); + // Default to a white background + imagefill( $this->image, 1, 1, $bgColor ); } return $this->image; @@ -142,6 +144,53 @@ class ezcGraphGdDriver extends ezcGraphDriver imageline( $image, $start->x, $start->y, $end->x, $end->y, $drawColor ); } + protected function testFitStringInTextBox( $string, ezcGraphCoordinate $position, $width, $height, $size ) + { + // Tokenize String + $tokens = preg_split( '/\s+/', $string ); + + $lines = array( array() ); + $line = 0; + foreach ( $tokens as $token ) + { + // Add token to tested line + $selectedLine = $lines[$line]; + $selectedLine[] = $token; + + $boundings = imagettfbbox( $size, 0, $this->options->font, implode( ' ', $selectedLine ) ); + + // Check if line is too long + if ( $boundings[2] > $width ) + { + if ( count( $selectedLine ) == 1 ) + { + // Return false if one single word does not fit into one line + return false; + } + else + { + // Put word in next line instead and reduce available height by used space + $lines[++$line][] = $token; + $height -= $size * ( 1 + $this->options->lineSpacing ); + } + } + else + { + // Everything is ok - put token in this line + $lines[$line][] = $token; + } + + // Return false if text exceeds vertical limit + if ( $size > $height ) + { + return false; + } + } + + // It seems to fit - return line array + return $lines; + } + /** * Wrties text in a box of desired size * @@ -154,7 +203,49 @@ class ezcGraphGdDriver extends ezcGraphDriver */ public function drawTextBox( $string, ezcGraphCoordinate $position, $width, $height, $align ) { - + $image = $this->getImage(); + $drawColor = $this->allocate( $this->options->fontColor ); + + // Try to get a font size for the text to fit into the box + $maxSize = min( $height, $this->options->maxFontSize ); + for ( $size = $maxSize; $size >= $this->options->minFontSize; --$size ) + { + $result = $this->testFitStringInTextBox( $string, $position, $width, $height, $size ); + if ( $result !== false ) + { + break; + } + } + + if ( is_array( $result ) ) + { + // Render text with evaluated font size + foreach ( $result as $line ) + { + $string = implode( ' ', $line ); + $boundings = imagettfbbox( $size, 0, $this->options->font, $string ); + $position->y += $size; + + switch ( $align ) + { + case ezcGraph::LEFT: + imagettftext( $image, $size, 0, $position->x, $position->y, $drawColor, $this->options->font, $string ); + break; + case ezcGraph::RIGHT: + imagettftext( $image, $size, 0, $position->x + ( $width - $boundings[2] ), $position->y, $drawColor, $this->options->font, $string ); + break; + case ezcGraph::CENTER: + imagettftext( $image, $size, 0, $position->x + ( ( $width - $boundings[2] ) / 2 ), $position->y, $drawColor, $this->options->font, $string ); + break; + } + + $position->y += $size * $this->options->lineSpacing; + } + } + else + { + // @TODO: Try to fit text in box with minimum font size + } } /** diff --git a/src/driver/svg.php b/src/driver/svg.php index 55c0ad5..446818c 100644 --- a/src/driver/svg.php +++ b/src/driver/svg.php @@ -18,7 +18,7 @@ class ezcGraphSVGDriver extends ezcGraphDriver public function __construct( array $options = array() ) { - + $this->options = new ezcGraphSvgDriverOptions( $options ); } /** |