linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file Driver.class.php

Documentation is available at Driver.class.php

  1. <?php
  2. /*
  3.  * This work is hereby released into the Public Domain.
  4.  * To view a copy of the public domain dedication,
  5.  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
  6.  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  7.  *
  8.  */
  9.  
  10. require_once dirname(__FILE__)."/../Graph.class.php";
  11.  
  12. /**
  13.  * Draw your objects
  14.  *
  15.  * @package linea21.externals
  16.  * @subpackage artichow
  17.  */
  18. abstract class awDriver {
  19.  
  20.   /**
  21.    * Image width
  22.    *
  23.    * @var int 
  24.    */
  25.   public $imageWidth;
  26.  
  27.   /**
  28.    * Image height
  29.    *
  30.    * @var int 
  31.    */
  32.   public $imageHeight;
  33.  
  34.   /**
  35.    * Driver X position
  36.    *
  37.    * @var int 
  38.    */
  39.   public $x;
  40.  
  41.   /**
  42.    * Driver Y position
  43.    *
  44.    * @var int 
  45.    */
  46.   public $y;
  47.  
  48.   /**
  49.    * Use anti-aliasing ?
  50.    *
  51.    * @var bool 
  52.    */
  53.   protected $antiAliasing = FALSE;
  54.  
  55.   /**
  56.    * The FontDriver object that will be used to draw text
  57.    * with PHP fonts.
  58.    *
  59.    * @var awPHPFontDriver 
  60.    */
  61.   protected $phpFontDriver;
  62.  
  63.   /**
  64.    * The FontDriver object that will be used to draw text
  65.    * with TTF or FDB fonts.
  66.    *
  67.    * @var awFileFontDriver 
  68.    */
  69.   protected $fileFontDriver;
  70.  
  71.   /**
  72.    * A string representing the type of the driver
  73.    *
  74.    * @var string 
  75.    */
  76.   protected $driverString;
  77.  
  78.   private $w;
  79.   private $h;
  80.  
  81.   public function __construct({
  82.     $this->phpFontDriver = new awPHPFontDriver();
  83.     $this->fileFontDriver = new awFileFontDriver();
  84.   }
  85.  
  86.   /**
  87.    * Initialize the driver for a particular awImage object
  88.    *
  89.    * @param awImage $image 
  90.    */
  91.   abstract public function init(awImage $image);
  92.  
  93.   /**
  94.    * Initialize the Driver for a particular FileImage object
  95.    *
  96.    * @param awFileImage $fileImage The FileImage object to work on
  97.    * @param string $file Image filename
  98.    */
  99.   abstract public function initFromFile(awFileImage $fileImage$file);
  100.  
  101.   /**
  102.    * Change the image size
  103.    *
  104.    * @param int $width Image width
  105.    * @param int $height Image height
  106.    */
  107.   abstract public function setImageSize($width$height);
  108.  
  109.   /**
  110.    * Inform the driver of the position of your image
  111.    *
  112.    * @param float $x Position on X axis of the center of the component
  113.    * @param float $y Position on Y axis of the center of the component
  114.    */
  115.   abstract public function setPosition($x$y);
  116.  
  117.   /**
  118.    * Inform the driver of the position of your image
  119.    * This method need absolutes values
  120.    *
  121.    * @param int $x Left-top corner X position
  122.    * @param int $y Left-top corner Y position
  123.    */
  124.   abstract public function setAbsPosition($x$y);
  125.  
  126.   /**
  127.    * Move the position of the image
  128.    *
  129.    * @param int $x Add this value to X axis
  130.    * @param int $y Add this value to Y axis
  131.    */
  132.   abstract public function movePosition($x$y);
  133.  
  134.   /**
  135.    * Inform the driver of the size of your image
  136.    * Height and width must be between 0 and 1.
  137.    *
  138.    * @param int $w Image width
  139.    * @param int $h Image height
  140.    * @return array Absolute width and height of the image
  141.    */
  142.   abstract public function setSize($w$h);
  143.  
  144.   /**
  145.    * Inform the driver of the size of your image
  146.    * You can set absolute size with this method.
  147.    *
  148.    * @param int $w Image width
  149.    * @param int $h Image height
  150.    */
  151.   abstract public function setAbsSize($w$h);
  152.  
  153.   /**
  154.    * Get the size of the component handled by the driver
  155.    *
  156.    * @return array Absolute width and height of the component
  157.    */
  158.   abstract public function getSize();
  159.  
  160.   /**
  161.    * Turn antialiasing on or off
  162.    *
  163.    * @var bool $bool 
  164.    */
  165.   abstract public function setAntiAliasing($bool);
  166.  
  167.   /**
  168.    * When passed a Color object, returns the corresponding
  169.    * color identifier (driver dependant).
  170.    *
  171.    * @param awColor $color A Color object
  172.    * @return int $rgb A color identifier representing the color composed of the given RGB components
  173.    */
  174.   abstract public function getColor(awColor $color);
  175.  
  176.   /**
  177.    * Draw an image here
  178.    *
  179.    * @param awImage $image Image
  180.    * @param int $p1 Image top-left point
  181.    * @param int $p2 Image bottom-right point
  182.    */
  183.   abstract public function copyImage(awImage $imageawPoint $p1awPoint $p2);
  184.  
  185.   /**
  186.    * Draw an image here
  187.    *
  188.    * @param awImage $image Image
  189.    * @param int $d1 Destination top-left position
  190.    * @param int $d2 Destination bottom-right position
  191.    * @param int $s1 Source top-left position
  192.    * @param int $s2 Source bottom-right position
  193.    * @param bool $resample Resample image ? (default to TRUE)
  194.    */
  195.   abstract public function copyResizeImage(awImage $imageawPoint $d1awPoint $d2awPoint $s1awPoint $s2$resample TRUE);
  196.  
  197.   /**
  198.    * Draw a string
  199.    *
  200.    * @var awText $text Text to print
  201.    * @param awPoint $point Draw the text at this point
  202.    * @param int $width Text max width
  203.    */
  204.   abstract public function string(awText $textawPoint $point$width NULL);
  205.  
  206.   /**
  207.    * Draw a pixel
  208.    *
  209.    * @param awColor $color Pixel color
  210.    * @param awPoint $p 
  211.    */
  212.   abstract public function point(awColor $colorawPoint $p);
  213.  
  214.   /**
  215.    * Draw a colored line
  216.    *
  217.    * @param awColor $color Line color
  218.    * @param awLine $line 
  219.    * @param int $thickness Line tickness
  220.    */
  221.   abstract public function line(awColor $colorawLine $line);
  222.  
  223.   /**
  224.    * Draw a color arc
  225.  
  226.    * @param awColor $color Arc color
  227.    * @param awPoint $center Point center
  228.    * @param int $width Ellipse width
  229.    * @param int $height Ellipse height
  230.    * @param int $from Start angle
  231.    * @param int $to End angle
  232.    */
  233.   abstract public function arc(awColor $colorawPoint $center$width$height$from$to);
  234.  
  235.   /**
  236.    * Draw an arc with a background color
  237.    *
  238.    * @param awColor $color Arc background color
  239.    * @param awPoint $center Point center
  240.    * @param int $width Ellipse width
  241.    * @param int $height Ellipse height
  242.    * @param int $from Start angle
  243.    * @param int $to End angle
  244.    */
  245.   abstract public function filledArc(awColor $colorawPoint $center$width$height$from$to);
  246.  
  247.   /**
  248.    * Draw a colored ellipse
  249.    *
  250.    * @param awColor $color Ellipse color
  251.    * @param awPoint $center Ellipse center
  252.    * @param int $width Ellipse width
  253.    * @param int $height Ellipse height
  254.    */
  255.   abstract public function ellipse(awColor $colorawPoint $center$width$height);
  256.  
  257.   /**
  258.    * Draw an ellipse with a background
  259.    *
  260.    * @param mixed $background Background (can be a color or a gradient)
  261.    * @param awPoint $center Ellipse center
  262.    * @param int $width Ellipse width
  263.    * @param int $height Ellipse height
  264.    */
  265.   abstract public function filledEllipse($backgroundawPoint $center$width$height);
  266.  
  267.   /**
  268.    * Draw a colored rectangle
  269.    *
  270.    * @param awColor $color Rectangle color
  271.    * @param awLine $line Rectangle diagonale
  272.    * @param awPoint $p2 
  273.    */
  274.   abstract public function rectangle(awColor $colorawLine $line);
  275.  
  276.   /**
  277.    * Draw a rectangle with a background
  278.    *
  279.    * @param mixed $background Background (can be a color or a gradient)
  280.    * @param awLine $line Rectangle diagonale
  281.    */
  282.   abstract public function filledRectangle($backgroundawLine $line);
  283.  
  284.   /**
  285.    * Draw a polygon
  286.    *
  287.    * @param awColor $color Polygon color
  288.    * @param Polygon A polygon
  289.    */
  290.   abstract public function polygon(awColor $colorawPolygon $polygon);
  291.  
  292.   /**
  293.    * Draw a polygon with a background
  294.    *
  295.    * @param mixed $background Background (can be a color or a gradient)
  296.    * @param Polygon A polygon
  297.    */
  298.   abstract public function filledPolygon($backgroundawPolygon $polygon);
  299.  
  300.   /**
  301.    * Sends the image, as well as the correct HTTP headers, to the browser
  302.    *
  303.    * @param awImage $image The Image object to send
  304.    */
  305.   abstract public function send(awImage $image);
  306.  
  307.   /**
  308.    * Get the image as binary data
  309.    *
  310.    * @param awImage $image 
  311.    */
  312.   abstract public function get(awImage $image);
  313.  
  314.   /**
  315.    * Return the width of some text
  316.    *
  317.    * @param awText $text 
  318.    */
  319.   abstract public function getTextWidth(awText $text);
  320.  
  321.   /**
  322.    * Return the height of some text
  323.    *
  324.    * @param awText $text 
  325.    */
  326.   abstract public function getTextHeight(awText $text);
  327.  
  328.   /**
  329.    * Return the string representing the type of driver
  330.    *
  331.    * @return string 
  332.    */
  333.   public function getDriverString({
  334.     return $this->driverString;
  335.   }
  336.  
  337.   /**
  338.    * Returns whether or not the driver is compatible with the given font type
  339.    *
  340.    * @param awFont $font 
  341.    * @return bool 
  342.    */
  343.   abstract protected function isCompatibleWithFont(awFont $font);
  344.  
  345.   //    abstract private function drawImage(awImage $image, $return = FALSE, $header = TRUE);
  346.  
  347. }
  348.  
  349. registerClass('Driver'TRUE);
  350.  
  351. /**
  352.  * Abstract class for font drivers.
  353.  * Those are used to do all the grunt work on fonts.
  354.  *
  355.  * @package linea21.externals
  356.  * @subpackage artichow
  357.  */
  358.  
  359. abstract class awFontDriver {
  360.  
  361.   public function __construct({
  362.  
  363.   }
  364.  
  365.   /**
  366.    * Draw the actual text.
  367.    *
  368.    * @param awDriver $driver The Driver object to draw upon
  369.    * @param awText $text The Text object
  370.    * @param awPoint $point Where to draw the text
  371.    * @param float $width The width of the area containing the text
  372.    */
  373.   abstract public function string(awDriver $driverawText $textawPoint $point$width NULL);
  374.  
  375.   /**
  376.    * Calculate the width of a given Text.
  377.    *
  378.    * @param awText $text The Text object
  379.    * @param awDriver $driver The awDriver object used to draw the graph
  380.    */
  381.   abstract public function getTextWidth(awText $textawDriver $driver);
  382.  
  383.   /**
  384.    * Calculate the height of a given Text.
  385.    *
  386.    * @param awText $text The Text object
  387.    * @param awDriver $driver The awDriver object used to draw the graph
  388.    */
  389.   abstract public function getTextHeight(awText $textawDriver $driver);
  390.  
  391. }
  392.  
  393. registerClass('FontDriver'TRUE);
  394.  
  395. /**
  396.  * Class to handle calculations on PHPFont objects
  397.  *
  398.  * @package linea21.externals
  399.  * @subpackage artichow
  400.  */
  401. class awPHPFontDriver extends awFontDriver {
  402.  
  403.   public function __construct({
  404.     parent::__construct();
  405.   }
  406.  
  407.   public function string(awDriver $driverawText $textawPoint $point$width NULL{
  408.  
  409.     switch ($driver->getDriverString()) {
  410.       case 'gd':
  411.         $this->gdString($driver$text$point$width);
  412.         break;
  413.  
  414.       default:
  415.         awImage::drawError('Class PHPFontDriver: Incompatibility between driver and font - You should never see this error message: have you called awDriver::isCompatibleWithFont() properly?');
  416.         break;
  417.             
  418.     }
  419.   }
  420.  
  421.   /**
  422.    * Draw a string onto a GDDriver object
  423.    *
  424.    * @param awGDDriver $driver The GDDriver to draw the text upon
  425.    * @param awText $text The awText object containing the string to draw
  426.    * @param awPoint $point Where to draw the text
  427.    * @param float $width The width of the text
  428.    */
  429.   private function gdString(awGDDriver $driverawText $textawPoint $point$width NULL{
  430.  
  431.     $angle $text->getAngle();
  432.     if($angle !== 90 and $angle !== 0{
  433.       awImage::drawError("Class PHPFontDriver: You can only use 0° and 90° angles.");
  434.     }
  435.  
  436.     if($angle === 90{
  437.       $function 'imagestringup';
  438.       $addAngle $this->getGDTextHeight($text);
  439.     else {
  440.       $function 'imagestring';
  441.       $addAngle 0;
  442.     }
  443.  
  444.     $color $text->getColor();
  445.     $rgb $driver->getColor($color);
  446.  
  447.     $textString $text->getText();
  448.     $textString str_replace("\r"""$textString);
  449.  
  450.     $textHeight $this->getGDTextHeight($text);
  451.  
  452.     // Split text if needed
  453.     if($width !== NULL{
  454.  
  455.       $characters floor($width ($this->getGDTextWidth($textstrlen($textString)));
  456.  
  457.       if($characters 0{
  458.         $textString wordwrap($textString$characters"\n"TRUE);
  459.       }
  460.  
  461.     }
  462.  
  463.     $font $text->getFont();
  464.     $lines explode("\n"$textString);
  465.  
  466.     foreach($lines as $i => $line{
  467.  
  468.       // Line position handling
  469.       if($angle === 90{
  470.         $addX $i $textHeight;
  471.         $addY 0;
  472.       else {
  473.         $addX 0;
  474.         $addY $i $textHeight;
  475.       }
  476.  
  477.       $function(
  478.       $driver->resource,
  479.       $font->font,
  480.       $driver->$point->$addX,
  481.       $driver->$point->$addY $addAngle,
  482.       $line,
  483.       $rgb
  484.       );
  485.  
  486.     }
  487.   }
  488.  
  489.   public function getTextWidth(awText $textawDriver $driver{
  490.  
  491.     switch ($driver->getDriverString()) {
  492.       case 'gd':
  493.         return $this->getGDTextWidth($text);
  494.  
  495.       default:
  496.         awImage::drawError('Class PHPFontDriver: Cannot get text width - incompatibility between driver and font');
  497.         break;
  498.     }
  499.  
  500.   }
  501.  
  502.   public function getTextHeight(awText $textawDriver $driver{
  503.  
  504.     switch ($driver->getDriverString()) {
  505.       case 'gd':
  506.         return $this->getGDTextHeight($text);
  507.  
  508.       default:
  509.         awImage::drawError('Class PHPFontDriver: Cannot get text height - incompatibility between driver and font');
  510.         break;
  511.     }
  512.  
  513.   }
  514.  
  515.   /**
  516.    * Return the width of a text for a GDDriver
  517.    *
  518.    * @param awText $text 
  519.    * @return int $fontWidth
  520.    */
  521.   private function getGDTextWidth(awText $text{
  522.     $font $text->getFont();
  523.  
  524.     if($text->getAngle(=== 90{
  525.       $text->setAngle(45);
  526.       return $this->getGDTextHeight($text);
  527.     else if($text->getAngle(=== 45{
  528.       $text->setAngle(90);
  529.     }
  530.  
  531.     $fontWidth imagefontwidth($font->font);
  532.  
  533.     if($fontWidth === FALSE{
  534.       awImage::drawError("Class PHPFontDriver: Unable to get font size.");
  535.     }
  536.  
  537.     return (int)$fontWidth strlen($text->getText());
  538.   }
  539.  
  540.   /**
  541.    * Return the height of a text for a GDDriver
  542.    *
  543.    * @param awText $text 
  544.    * @return int $fontHeight
  545.    */
  546.   private function getGDTextHeight(awText $text{
  547.     $font $text->getFont();
  548.  
  549.     if($text->getAngle(=== 90{
  550.       $text->setAngle(45);
  551.       return $this->getGDTextWidth($text);
  552.     else if($text->getAngle(=== 45{
  553.       $text->setAngle(90);
  554.     }
  555.  
  556.     $fontHeight imagefontheight($font->font);
  557.  
  558.     if($fontHeight === FALSE{
  559.       awImage::drawError("Class PHPFontDriver: Unable to get font size.");
  560.     }
  561.  
  562.     return (int)$fontHeight;
  563.   }
  564. }
  565.  
  566. registerClass('PHPFontDriver');
  567.  
  568. /**
  569.  * Class to handle calculations on FileFont objects
  570.  *
  571.  * @package linea21.externals
  572.  * @subpackage artichow
  573.  */
  574. class awFileFontDriver extends awFontDriver {
  575.  
  576.   public function __construct({
  577.     parent::__construct();
  578.   }
  579.  
  580.   public function string(awDriver $driverawText $textawPoint $point$width NULL{
  581.  
  582.     switch ($driver->getDriverString()) {
  583.       case 'gd':
  584.         $this->gdString($driver$text$point$width);
  585.         break;
  586.             
  587.       default:
  588.         awImage::drawError('Class fileFontDriver: Incompatibility between driver and font - You should never see this error message: have you called awDriver::isCompatibleWithFont() properly?');
  589.         break;
  590.     }
  591.   }
  592.  
  593.   /**
  594.    * Draw an awFileFont object on a GD ressource
  595.    *
  596.    * @param awGDDriver $driver The awGDDriver object containing the ressource to draw upon
  597.    * @param awText $text The awText object containing the string to draw
  598.    * @param awPoint $point Where to draw the string from
  599.    * @param float $width The width of the area containing the text
  600.    */
  601.   private function gdString(awGDDriver $driverawText $textawPoint $point$width NULL{
  602.     // Make easier font positionment
  603.     $text->setText($text->getText()." ");
  604.  
  605.     $font $text->getFont();
  606.     if($font instanceof awTTFFont === FALSE and $font->getExtension(=== NULL{
  607.       $font->setExtension('ttf');
  608.     }
  609.  
  610.     $filePath $font->getName().'.'.$font->getExtension();
  611.  
  612.     $box imagettfbbox($font->getSize()$text->getAngle()$filePath$text->getText());
  613.     $textHeight = - $box[5];
  614.  
  615.     $box imagettfbbox($font->getSize()90$filePath$text->getText());
  616.     $textWidth abs($box[6$box[2]);
  617.  
  618.     // Restore old text
  619.     $text->setText(substr($text->getText()0strlen($text->getText()) 1));
  620.  
  621.     $textString $text->getText();
  622.  
  623.     // Split text if needed
  624.     if($width !== NULL{
  625.  
  626.       $characters floor($width $this->getGDAverageWidth($font));
  627.       $textString wordwrap($textString$characters"\n"TRUE);
  628.  
  629.     }
  630.  
  631.     $color $text->getColor();
  632.     $rgb $driver->getColor($color);
  633.  
  634.     imagettftext(
  635.     $driver->resource,
  636.     $font->getSize(),
  637.     $text->getAngle(),
  638.     $driver->$point->$textWidth sin($text->getAngle(180 M_PI),
  639.     $driver->$point->$textHeight,
  640.     $rgb,
  641.     $filePath,
  642.     $textString
  643.     );
  644.   }
  645.  
  646.   public function getTextWidth(awText $textawDriver $driver{
  647.     switch ($driver->getDriverString()) {
  648.       case 'gd':
  649.         return $this->getGDTextWidth($text);
  650.             
  651.       default:
  652.         awImage::drawError('Class FileFontDriver: Cannot get text width - incompatibility between driver and font');
  653.         break;
  654.     }
  655.   }
  656.  
  657.   public function getTextHeight(awText $textawDriver $driver{
  658.     switch ($driver->getDriverString()) {
  659.       case 'gd':
  660.         return $this->getGDTextHeight($text);
  661.             
  662.       default:
  663.         awImage::drawError('Class FileFontDriver: Cannot get text height - incompatibility between driver and font');
  664.         break;
  665.     }
  666.   }
  667.  
  668.   private function getGDTextWidth(awText $text{
  669.     $font $text->getFont();
  670.     if($font->getExtension(=== NULL{
  671.       $font->setExtension('ttf');
  672.     }
  673.  
  674.     $filePath $font->getName().'.'.$font->getExtension();
  675.  
  676.     $box imagettfbbox($font->getSize()$text->getAngle()$filePath$text->getText());
  677.  
  678.     if($box === FALSE{
  679.       awImage::drawError("Class FileFontDriver: Unable to get font width (GD).");
  680.     }
  681.  
  682.     list(, , $x2, , , , $x1$box;
  683.  
  684.     return abs($x2 $x1);
  685.   }
  686.  
  687.   private function getGDTextHeight(awText $text{
  688.     $font $text->getFont();
  689.     if($font->getExtension(=== NULL{
  690.       $font->setExtension('ttf');
  691.     }
  692.  
  693.     $filePath $font->getName().'.'.$font->getExtension();
  694.  
  695.     $box imagettfbbox($font->getSize()$text->getAngle()$filePath$text->getText());
  696.  
  697.     if($box === FALSE{
  698.       awImage::drawError("Class FileFontDriver: Unable to get font height (GD).");
  699.     }
  700.  
  701.     list(, , , $y2, , , , $y1$box;
  702.  
  703.     return abs($y2 $y1);
  704.   }
  705.  
  706.   private function getGDAverageWidth(awFileFont $font{
  707.  
  708.     $text "azertyuiopqsdfghjklmmmmmmmwxcvbbbn,;:!?.";
  709.  
  710.     $box imagettfbbox($font->getSize()0$font->getName().'.'.$font->getExtension()$text);
  711.  
  712.     if($box === FALSE{
  713.       awImage::drawError("Class FileFontDriver: Unable to get font average width.");
  714.     }
  715.  
  716.     list(, , $x2$y2, , , $x1$y1$box;
  717.  
  718.     return abs($x2 $x1strlen($text);
  719.  
  720.   }
  721.  
  722. }
  723.  
  724. registerClass('FileFontDriver');
  725.  
  726. // Include ARTICHOW_DRIVER by default to preserve backward compatibility.
  727. require_once dirname(__FILE__).'/drivers/'.ARTICHOW_DRIVER.'.class.php';
  728.  
  729. ?>

Documentation generated on Thu, 03 May 2012 15:04:29 +0200 by phpDocumentor 1.4.1