Source for file Driver.class.php
Documentation is available at Driver.class.php
* This work is hereby released into the Public Domain.
* To view a copy of the public domain dedication,
* visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
* Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
require_once dirname(__FILE__ ). "/../Graph.class.php";
* @package linea21.externals
* The FontDriver object that will be used to draw text
* The FontDriver object that will be used to draw text
* A string representing the type of the driver
* Initialize the driver for a particular awImage object
* Initialize the Driver for a particular FileImage object
* @param &$fileImage The FileImage object to work on
* @param string $file Image filename
* @param int $width Image width
* @param int $height Image height
* Inform the driver of the position of your image
* @param float $x Position on X axis of the center of the component
* @param float $y Position on Y axis of the center of the component
* Inform the driver of the position of your image
* This method need absolutes values
* @param int $x Left-top corner X position
* @param int $y Left-top corner Y position
* Move the position of the image
* @param int $x Add this value to X axis
* @param int $y Add this value to Y axis
* Inform the driver of the size of your image
* Height and width must be between 0 and 1.
* @param int $w Image width
* @param int $h Image height
* @return array Absolute width and height of the image
* Inform the driver of the size of your image
* You can set absolute size with this method.
* @param int $w Image width
* @param int $h Image height
* Get the size of the component handled by the driver
* @return array Absolute width and height of the component
* Turn antialiasing on or off
* When passed a Color object, returns the corresponding
* color identifier (driver dependant).
* @param $color A Color object
* @return int $rgb A color identifier representing the color composed of the given RGB components
* @param int $p1 Image top-left point
* @param int $p2 Image bottom-right point
* @param int $d1 Destination top-left position
* @param int $d2 Destination bottom-right position
* @param int $s1 Source top-left position
* @param int $s2 Source bottom-right position
* @param bool $resample Resample image ? (default to TRUE)
* @var &$text Text to print
* @param $point Draw the text at this point
* @param int $width Text max width
* @param $color Pixel color
* @param $color Line color
* @param int $thickness Line tickness
* @param $color Arc color
* @param $center Point center
* @param int $width Ellipse width
* @param int $height Ellipse height
* @param int $from Start angle
* @param int $to End angle
* Draw an arc with a background color
* @param $color Arc background color
* @param $center Point center
* @param int $width Ellipse width
* @param int $height Ellipse height
* @param int $from Start angle
* @param int $to End angle
* @param $color Ellipse color
* @param $center Ellipse center
* @param int $width Ellipse width
* @param int $height Ellipse height
* Draw an ellipse with a background
* @param mixed $background Background (can be a color or a gradient)
* @param $center Ellipse center
* @param int $width Ellipse width
* @param int $height Ellipse height
* Draw a colored rectangle
* @param $color Rectangle color
* @param $line Rectangle diagonale
* Draw a rectangle with a background
* @param mixed $background Background (can be a color or a gradient)
* @param $line Rectangle diagonale
* @param $color Polygon color
* @param Polygon A polygon
* Draw a polygon with a background
* @param mixed $background Background (can be a color or a gradient)
* @param Polygon A polygon
* Sends the image, as well as the correct HTTP headers, to the browser
* @param &$image The Image object to send
* Get the image as binary data
* Return the width of some text
* Return the height of some text
* Return the string representing the type of driver
* Returns whether or not the driver is compatible with the given font type
* Abstract class for font drivers.
* Those are used to do all the grunt work on fonts.
* @package linea21.externals
function awFontDriver() {
* @param $driver The Driver object to draw upon
* @param &$text The Text object
* @param $point Where to draw the text
* @param float $width The width of the area containing the text
* Calculate the width of a given Text.
* @param &$text The Text object
* @param $driver The awDriver object used to draw the graph
* Calculate the height of a given Text.
* @param &$text The Text object
* @param $driver The awDriver object used to draw the graph
* Class to handle calculations on PHPFont objects
* @package linea21.externals
function awPHPFontDriver() {
function string($driver, &$text, $point, $width = NULL) {
switch ($driver->getDriverString()) {
$this->gdString($driver, $text, $point, $width);
awImage::drawError('Class PHPFontDriver: Incompatibility between driver and font - You should never see this error message: have you called awDriver::isCompatibleWithFont() properly?');
* Draw a string onto a GDDriver object
* @param $driver The GDDriver to draw the text upon
* @param &$text The awText object containing the string to draw
* @param $point Where to draw the text
* @param float $width The width of the text
function gdString($driver, &$text, $point, $width = NULL) {
$angle = $text->getAngle();
if($angle !== 90 and $angle !== 0) {
awImage::drawError("Class PHPFontDriver: You can only use 0° and 90° angles.");
$function = 'imagestringup';
$addAngle = $this->getGDTextHeight($text);
$function = 'imagestring';
$color = $text->getColor();
$rgb = $driver->getColor($color);
$textString = $text->getText();
$textHeight = $this->getGDTextHeight($text);
$characters = floor($width / ($this->getGDTextWidth($text) / strlen($textString)));
$textString = wordwrap($textString, $characters, "\n", TRUE);
$font = $text->getFont();
$lines = explode("\n", $textString);
foreach($lines as $i => $line) {
// Line position handling
$addX = $i * $textHeight;
$addY = $i * $textHeight;
$driver->x + $point->x + $addX,
$driver->y + $point->y + $addY + $addAngle,
switch ($driver->getDriverString()) {
return $this->getGDTextWidth($text);
awImage::drawError('Class PHPFontDriver: Cannot get text width - incompatibility between driver and font');
switch ($driver->getDriverString()) {
return $this->getGDTextHeight($text);
awImage::drawError('Class PHPFontDriver: Cannot get text height - incompatibility between driver and font');
* Return the width of a text for a GDDriver
function getGDTextWidth(&$text) {
$font = $text->getFont();
if($text->getAngle() === 90) {
return $this->getGDTextHeight($text);
} else if($text->getAngle() === 45) {
if($fontWidth === FALSE) {
return (int) $fontWidth * strlen($text->getText());
* Return the height of a text for a GDDriver
* @return int $fontHeight
function getGDTextHeight(&$text) {
$font = $text->getFont();
if($text->getAngle() === 90) {
return $this->getGDTextWidth($text);
} else if($text->getAngle() === 45) {
if($fontHeight === FALSE) {
* Class to handle calculations on FileFont objects
* @package linea21.externals
function awFileFontDriver() {
function string($driver, &$text, $point, $width = NULL) {
switch ($driver->getDriverString()) {
$this->gdString($driver, $text, $point, $width);
awImage::drawError('Class fileFontDriver: Incompatibility between driver and font - You should never see this error message: have you called awDriver::isCompatibleWithFont() properly?');
* Draw an awFileFont object on a GD ressource
* @param $driver The awGDDriver object containing the ressource to draw upon
* @param &$text The awText object containing the string to draw
* @param $point Where to draw the string from
* @param float $width The width of the area containing the text
function gdString($driver, &$text, $point, $width = NULL) {
// Make easier font positionment
$text->setText($text->getText(). " ");
$font = $text->getFont();
if(is_a($font, 'awTTFFont') === FALSE and $font->getExtension() === NULL) {
$font->setExtension('ttf');
$filePath = $font->getName(). '.'. $font->getExtension();
$box = imagettfbbox($font->getSize(), $text->getAngle(), $filePath, $text->getText());
$box = imagettfbbox($font->getSize(), 90, $filePath, $text->getText());
$textWidth = abs($box[6] - $box[2]);
$text->setText(substr($text->getText(), 0, strlen($text->getText()) - 1));
$textString = $text->getText();
$characters = floor($width / $this->getGDAverageWidth($font));
$textString = wordwrap($textString, $characters, "\n", TRUE);
$color = $text->getColor();
$rgb = $driver->getColor($color);
$driver->x + $point->x + $textWidth * sin($text->getAngle() / 180 * M_PI),
$driver->y + $point->y + $textHeight,
switch ($driver->getDriverString()) {
return $this->getGDTextWidth($text);
awImage::drawError('Class FileFontDriver: Cannot get text width - incompatibility between driver and font');
switch ($driver->getDriverString()) {
return $this->getGDTextHeight($text);
awImage::drawError('Class FileFontDriver: Cannot get text height - incompatibility between driver and font');
function getGDTextWidth(&$text) {
$font = $text->getFont();
if($font->getExtension() === NULL) {
$font->setExtension('ttf');
$filePath = $font->getName(). '.'. $font->getExtension();
$box = imagettfbbox($font->getSize(), $text->getAngle(), $filePath, $text->getText());
list (, , $x2, , , , $x1, ) = $box;
function getGDTextHeight(&$text) {
$font = $text->getFont();
if($font->getExtension() === NULL) {
$font->setExtension('ttf');
$filePath = $font->getName(). '.'. $font->getExtension();
$box = imagettfbbox($font->getSize(), $text->getAngle(), $filePath, $text->getText());
list (, , , $y2, , , , $y1) = $box;
function getGDAverageWidth(&$font) {
$text = "azertyuiopqsdfghjklmmmmmmmwxcvbbbn,;:!?.";
$box = imagettfbbox($font->getSize(), 0, $font->getName(). '.'. $font->getExtension(), $text);
list (, , $x2, $y2, , , $x1, $y1) = $box;
// Include ARTICHOW_DRIVER by default to preserve backward compatibility.
require_once dirname(__FILE__ ). '/drivers/'. ARTICHOW_DRIVER. '.class.php';
|