web/lib/Zend/Barcode/Object/ObjectAbstract.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Barcode
    16  * @package    Zend_Barcode
    17  * @subpackage Object
    17  * @subpackage Object
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: ObjectAbstract.php 24593 2012-01-05 20:35:02Z matthew $
    20  * @version    $Id$
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * Class for generate Barcode
    24  * Class for generate Barcode
    25  *
    25  *
    26  * @category   Zend
    26  * @category   Zend
    27  * @package    Zend_Barcode
    27  * @package    Zend_Barcode
    28  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    28  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    29  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    29  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    30  */
    30  */
    31 abstract class Zend_Barcode_Object_ObjectAbstract
    31 abstract class Zend_Barcode_Object_ObjectAbstract
    32 {
    32 {
    33     /**
    33     /**
    34      * Namespace of the barcode for autoloading
    34      * Namespace of the barcode for autoloading
       
    35      *
    35      * @var string
    36      * @var string
    36      */
    37      */
    37     protected $_barcodeNamespace = 'Zend_Barcode_Object';
    38     protected $_barcodeNamespace = 'Zend_Barcode_Object';
    38 
    39 
    39     /**
    40     /**
    40      * Set of drawing instructions
    41      * Set of drawing instructions
       
    42      *
    41      * @var array
    43      * @var array
    42      */
    44      */
    43     protected $_instructions = array();
    45     protected $_instructions = array();
    44 
    46 
    45     /**
    47     /**
    46      * Barcode type
    48      * Barcode type
       
    49      *
    47      * @var string
    50      * @var string
    48      */
    51      */
    49     protected $_type = null;
    52     protected $_type = null;
    50 
    53 
    51     /**
    54     /**
    52      * Height of the object
    55      * Height of the object
       
    56      *
    53      * @var integer
    57      * @var integer
    54      */
    58      */
    55     protected $_height = null;
    59     protected $_height = null;
    56 
    60 
    57     /**
    61     /**
    58      * Width of the object
    62      * Width of the object
       
    63      *
    59      * @var integer
    64      * @var integer
    60      */
    65      */
    61     protected $_width = null;
    66     protected $_width = null;
    62 
    67 
    63     /**
    68     /**
    64      * Height of the bar
    69      * Height of the bar
       
    70      *
    65      * @var integer
    71      * @var integer
    66      */
    72      */
    67     protected $_barHeight = 50;
    73     protected $_barHeight = 50;
    68 
    74 
    69     /**
    75     /**
    70      * Width of a thin bar
    76      * Width of a thin bar
       
    77      *
    71      * @var integer
    78      * @var integer
    72      */
    79      */
    73     protected $_barThinWidth = 1;
    80     protected $_barThinWidth = 1;
    74 
    81 
    75     /**
    82     /**
    76      * Width of a thick bar
    83      * Width of a thick bar
       
    84      *
    77      * @var integer
    85      * @var integer
    78      */
    86      */
    79     protected $_barThickWidth = 3;
    87     protected $_barThickWidth = 3;
    80 
    88 
    81     /**
    89     /**
    82      * Factor to multiply bar and font measure
    90      * Factor to multiply bar and font measure
    83      * (barHeight, barThinWidth, barThickWidth & fontSize)
    91      * (barHeight, barThinWidth, barThickWidth & fontSize)
       
    92      *
    84      * @var integer
    93      * @var integer
    85      */
    94      */
    86     protected $_factor = 1;
    95     protected $_factor = 1;
    87 
    96 
    88     /**
    97     /**
    89      * Font and bars color of the object
    98      * Font and bars color of the object
       
    99      *
    90      * @var integer
   100      * @var integer
    91      */
   101      */
    92     protected $_foreColor = 0x000000;
   102     protected $_foreColor = 0x000000;
    93 
   103 
    94     /**
   104     /**
    95      * Background color of the object
   105      * Background color of the object
       
   106      *
    96      * @var integer
   107      * @var integer
    97      */
   108      */
    98     protected $_backgroundColor = 0xFFFFFF;
   109     protected $_backgroundColor = 0xFFFFFF;
    99 
   110 
   100     /**
   111     /**
   101      * Activate/deactivate border of the object
   112      * Activate/deactivate border of the object
       
   113      *
   102      * @var boolean
   114      * @var boolean
   103      */
   115      */
   104     protected $_withBorder = false;
   116     protected $_withBorder = false;
   105 
   117 
   106     /**
   118     /**
   107      * Activate/deactivate drawing of quiet zones
   119      * Activate/deactivate drawing of quiet zones
       
   120      *
   108      * @var boolean
   121      * @var boolean
   109      */
   122      */
   110     protected $_withQuietZones = true;
   123     protected $_withQuietZones = true;
   111 
   124 
   112     /**
   125     /**
   113      * Force quiet zones even if
   126      * Force quiet zones even if
       
   127      *
   114      * @var boolean
   128      * @var boolean
   115      */
   129      */
   116     protected $_mandatoryQuietZones = false;
   130     protected $_mandatoryQuietZones = false;
   117 
   131 
   118     /**
   132     /**
   119      * Orientation of the barcode in degrees
   133      * Orientation of the barcode in degrees
       
   134      *
   120      * @var float
   135      * @var float
   121      */
   136      */
   122     protected $_orientation = 0;
   137     protected $_orientation = 0;
   123 
   138 
   124     /**
   139     /**
   125      * Offset from the top the object
   140      * Offset from the top the object
   126      * (calculated from the orientation)
   141      * (calculated from the orientation)
       
   142      *
   127      * @var integer
   143      * @var integer
   128      */
   144      */
   129     protected $_offsetTop = null;
   145     protected $_offsetTop = null;
   130 
   146 
   131     /**
   147     /**
   132      * Offset from the left the object
   148      * Offset from the left the object
   133      * (calculated from the orientation)
   149      * (calculated from the orientation)
       
   150      *
   134      * @var integer
   151      * @var integer
   135      */
   152      */
   136     protected $_offsetLeft = null;
   153     protected $_offsetLeft = null;
   137 
   154 
   138     /**
   155     /**
   139      * Text to display
   156      * Text to display
       
   157      *
   140      * @var string
   158      * @var string
   141      */
   159      */
   142     protected $_text = null;
   160     protected $_text = null;
   143 
   161 
   144     /**
   162     /**
   145      * Display (or not) human readable text
   163      * Display (or not) human readable text
       
   164      *
   146      * @var boolean
   165      * @var boolean
   147      */
   166      */
   148     protected $_drawText = true;
   167     protected $_drawText = true;
   149 
   168 
   150     /**
   169     /**
   151      * Adjust (or not) position of human readable characters with barcode
   170      * Adjust (or not) position of human readable characters with barcode
       
   171      *
   152      * @var boolean
   172      * @var boolean
   153      */
   173      */
   154     protected $_stretchText = false;
   174     protected $_stretchText = false;
   155 
   175 
   156     /**
   176     /**
   157      * Font resource
   177      * Font resource
   158      *  - integer (1 to 5): corresponds to GD included fonts
   178      *  - integer (1 to 5): corresponds to GD included fonts
   159      *  - string: corresponds to path of a TTF font
   179      *  - string: corresponds to path of a TTF font
       
   180      *
   160      * @var integer|string
   181      * @var integer|string
   161      */
   182      */
   162     protected $_font = null;
   183     protected $_font = null;
   163 
   184 
   164     /**
   185     /**
   165      * Font size
   186      * Font size
       
   187      *
   166      * @var float
   188      * @var float
   167      */
   189      */
   168     protected $_fontSize = 10;
   190     protected $_fontSize = 10;
   169 
   191 
   170     /**
   192     /**
   171      * Drawing of checksum
   193      * Drawing of checksum
       
   194      *
   172      * @var boolean
   195      * @var boolean
   173      */
   196      */
   174     protected $_withChecksum = false;
   197     protected $_withChecksum = false;
   175 
   198 
   176     /**
   199     /**
   177      * Drawing of checksum inside text
   200      * Drawing of checksum inside text
       
   201      *
   178      * @var boolean
   202      * @var boolean
   179      */
   203      */
   180     protected $_withChecksumInText = false;
   204     protected $_withChecksumInText = false;
   181 
   205 
   182     /**
   206     /**
   183      * Fix barcode length (numeric or string like 'even')
   207      * Fix barcode length (numeric or string like 'even')
       
   208      *
   184      * @var $_barcodeLength integer | string
   209      * @var $_barcodeLength integer | string
   185      */
   210      */
   186     protected $_barcodeLength = null;
   211     protected $_barcodeLength = null;
   187 
   212 
   188     /**
   213     /**
   189      * Activate automatic addition of leading zeros
   214      * Activate automatic addition of leading zeros
   190      * if barcode length is fixed
   215      * if barcode length is fixed
       
   216      *
   191      * @var $_addLeadingZeros boolean
   217      * @var $_addLeadingZeros boolean
   192      */
   218      */
   193     protected $_addLeadingZeros = true;
   219     protected $_addLeadingZeros = true;
   194 
   220 
   195     /**
   221     /**
   196      * Activation of mandatory checksum
   222      * Activation of mandatory checksum
   197      * to deactivate unauthorized modification
   223      * to deactivate unauthorized modification
       
   224      *
   198      * @var $_mandatoryChecksum boolean
   225      * @var $_mandatoryChecksum boolean
   199      */
   226      */
   200     protected $_mandatoryChecksum = false;
   227     protected $_mandatoryChecksum = false;
   201 
   228 
   202     /**
   229     /**
   203      * Character used to substitute checksum character for validation
   230      * Character used to substitute checksum character for validation
       
   231      *
   204      * @var $_substituteChecksumCharacter mixed
   232      * @var $_substituteChecksumCharacter mixed
   205      */
   233      */
   206     protected $_substituteChecksumCharacter = 0;
   234     protected $_substituteChecksumCharacter = 0;
   207 
   235 
   208     /**
   236     /**
   209      * TTF font name: can be set before instanciation of the object
   237      * TTF font name: can be set before instanciation of the object
       
   238      *
   210      * @var string
   239      * @var string
   211      */
   240      */
   212     protected static $_staticFont = null;
   241     protected static $_staticFont = null;
   213 
   242 
   214     /**
   243     /**
   215      * Constructor
   244      * Constructor
       
   245      *
   216      * @param array|Zend_Config $options
   246      * @param array|Zend_Config $options
   217      * @return void
       
   218      */
   247      */
   219     public function __construct($options = null)
   248     public function __construct($options = null)
   220     {
   249     {
   221         $this->_getDefaultOptions();
   250         $this->_getDefaultOptions();
   222         if (self::$_staticFont !== null) {
   251         if (self::$_staticFont !== null) {
   226             $options = $options->toArray();
   255             $options = $options->toArray();
   227         }
   256         }
   228         if (is_array($options)) {
   257         if (is_array($options)) {
   229             $this->setOptions($options);
   258             $this->setOptions($options);
   230         }
   259         }
   231         $this->_type = strtolower(substr(get_class($this), strlen($this->_barcodeNamespace) + 1));
   260         $this->_type = strtolower(
       
   261             substr(get_class($this), strlen($this->_barcodeNamespace) + 1)
       
   262         );
   232         if ($this->_mandatoryChecksum) {
   263         if ($this->_mandatoryChecksum) {
   233             $this->_withChecksum = true;
   264             $this->_withChecksum = true;
   234             $this->_withChecksumInText = true;
   265             $this->_withChecksumInText = true;
   235         }
   266         }
   236     }
   267     }
   237 
   268 
   238     /**
   269     /**
   239      * Set default options for particular object
   270      * Set default options for particular object
   240      * @return void
       
   241      */
   271      */
   242     protected function _getDefaultOptions()
   272     protected function _getDefaultOptions()
   243     {
   273     {
   244     }
   274     }
   245 
   275 
   246     /**
   276     /**
   247      * Set barcode state from options array
   277      * Set barcode state from options array
       
   278      *
   248      * @param  array $options
   279      * @param  array $options
   249      * @return Zend_Barcode_Object
   280      * @return $this
   250      */
   281      */
   251     public function setOptions($options)
   282     public function setOptions($options)
   252     {
   283     {
   253         foreach ($options as $key => $value) {
   284         foreach ($options as $key => $value) {
   254             $method = 'set' . $key;
   285             $method = 'set' . $key;
   259         return $this;
   290         return $this;
   260     }
   291     }
   261 
   292 
   262     /**
   293     /**
   263      * Set barcode state from config object
   294      * Set barcode state from config object
       
   295      *
   264      * @param Zend_Config $config
   296      * @param Zend_Config $config
   265      * @return Zend_Barcode_Object
   297      * @return $this
   266      */
   298      */
   267     public function setConfig(Zend_Config $config)
   299     public function setConfig(Zend_Config $config)
   268     {
   300     {
   269         return $this->setOptions($config->toArray());
   301         return $this->setOptions($config->toArray());
   270     }
   302     }
   271 
   303 
   272     /**
   304     /**
   273      * Set barcode namespace for autoloading
   305      * Set barcode namespace for autoloading
   274      *
   306      *
   275      * @param string $namespace
   307      * @param string $namespace
   276      * @return Zend_Barcode_Object
   308      * @return $this
   277      */
   309      */
   278     public function setBarcodeNamespace($namespace)
   310     public function setBarcodeNamespace($namespace)
   279     {
   311     {
   280         $this->_barcodeNamespace = $namespace;
   312         $this->_barcodeNamespace = $namespace;
   281         return $this;
   313         return $this;
   291         return $this->_barcodeNamespace;
   323         return $this->_barcodeNamespace;
   292     }
   324     }
   293 
   325 
   294     /**
   326     /**
   295      * Retrieve type of barcode
   327      * Retrieve type of barcode
       
   328      *
   296      * @return string
   329      * @return string
   297      */
   330      */
   298     public function getType()
   331     public function getType()
   299     {
   332     {
   300         return $this->_type;
   333         return $this->_type;
   301     }
   334     }
   302 
   335 
   303     /**
   336     /**
   304      * Set height of the barcode bar
   337      * Set height of the barcode bar
       
   338      *
   305      * @param integer $value
   339      * @param integer $value
   306      * @return Zend_Barcode_Object
   340      * @return $this
   307      * @throw Zend_Barcode_Object_Exception
   341      * @throws Zend_Barcode_Object_Exception
   308      */
   342      */
   309     public function setBarHeight($value)
   343     public function setBarHeight($value)
   310     {
   344     {
   311         if (intval($value) <= 0) {
   345         if (intval($value) <= 0) {
   312             require_once 'Zend/Barcode/Object/Exception.php';
   346             require_once 'Zend/Barcode/Object/Exception.php';
   318         return $this;
   352         return $this;
   319     }
   353     }
   320 
   354 
   321     /**
   355     /**
   322      * Get height of the barcode bar
   356      * Get height of the barcode bar
       
   357      *
   323      * @return integer
   358      * @return integer
   324      */
   359      */
   325     public function getBarHeight()
   360     public function getBarHeight()
   326     {
   361     {
   327         return $this->_barHeight;
   362         return $this->_barHeight;
   328     }
   363     }
   329 
   364 
   330     /**
   365     /**
   331      * Set thickness of thin bar
   366      * Set thickness of thin bar
       
   367      *
   332      * @param integer $value
   368      * @param integer $value
   333      * @return Zend_Barcode_Object
   369      * @return $this
   334      * @throw Zend_Barcode_Object_Exception
   370      * @throws Zend_Barcode_Object_Exception
   335      */
   371      */
   336     public function setBarThinWidth($value)
   372     public function setBarThinWidth($value)
   337     {
   373     {
   338         if (intval($value) <= 0) {
   374         if (intval($value) <= 0) {
   339             require_once 'Zend/Barcode/Object/Exception.php';
   375             require_once 'Zend/Barcode/Object/Exception.php';
   345         return $this;
   381         return $this;
   346     }
   382     }
   347 
   383 
   348     /**
   384     /**
   349      * Get thickness of thin bar
   385      * Get thickness of thin bar
       
   386      *
   350      * @return integer
   387      * @return integer
   351      */
   388      */
   352     public function getBarThinWidth()
   389     public function getBarThinWidth()
   353     {
   390     {
   354         return $this->_barThinWidth;
   391         return $this->_barThinWidth;
   355     }
   392     }
   356 
   393 
   357     /**
   394     /**
   358      * Set thickness of thick bar
   395      * Set thickness of thick bar
       
   396      *
   359      * @param integer $value
   397      * @param integer $value
   360      * @return Zend_Barcode_Object
   398      * @return $this
   361      * @throw Zend_Barcode_Object_Exception
   399      * @throws Zend_Barcode_Object_Exception
   362      */
   400      */
   363     public function setBarThickWidth($value)
   401     public function setBarThickWidth($value)
   364     {
   402     {
   365         if (intval($value) <= 0) {
   403         if (intval($value) <= 0) {
   366             require_once 'Zend/Barcode/Object/Exception.php';
   404             require_once 'Zend/Barcode/Object/Exception.php';
   372         return $this;
   410         return $this;
   373     }
   411     }
   374 
   412 
   375     /**
   413     /**
   376      * Get thickness of thick bar
   414      * Get thickness of thick bar
       
   415      *
   377      * @return integer
   416      * @return integer
   378      */
   417      */
   379     public function getBarThickWidth()
   418     public function getBarThickWidth()
   380     {
   419     {
   381         return $this->_barThickWidth;
   420         return $this->_barThickWidth;
   382     }
   421     }
   383 
   422 
   384     /**
   423     /**
   385      * Set factor applying to
   424      * Set factor applying to
   386      * thinBarWidth - thickBarWidth - barHeight - fontSize
   425      * thinBarWidth - thickBarWidth - barHeight - fontSize
   387      * @param float $value
   426      *
   388      * @return Zend_Barcode_Object
   427      * @param int|float|string|bool $value
   389      * @throw Zend_Barcode_Object_Exception
   428      * @return $this
       
   429      * @throws Zend_Barcode_Object_Exception
   390      */
   430      */
   391     public function setFactor($value)
   431     public function setFactor($value)
   392     {
   432     {
   393         if (floatval($value) <= 0) {
   433         if (floatval($value) <= 0) {
   394             require_once 'Zend/Barcode/Object/Exception.php';
   434             require_once 'Zend/Barcode/Object/Exception.php';
   401     }
   441     }
   402 
   442 
   403     /**
   443     /**
   404      * Get factor applying to
   444      * Get factor applying to
   405      * thinBarWidth - thickBarWidth - barHeight - fontSize
   445      * thinBarWidth - thickBarWidth - barHeight - fontSize
       
   446      *
   406      * @return integer
   447      * @return integer
   407      */
   448      */
   408     public function getFactor()
   449     public function getFactor()
   409     {
   450     {
   410         return $this->_factor;
   451         return $this->_factor;
   411     }
   452     }
   412 
   453 
   413     /**
   454     /**
   414      * Set color of the barcode and text
   455      * Set color of the barcode and text
       
   456      *
   415      * @param string $value
   457      * @param string $value
   416      * @return Zend_Barcode_Object
   458      * @return $this
   417      * @throw Zend_Barcode_Object_Exception
   459      * @throws Zend_Barcode_Object_Exception
   418      */
   460      */
   419     public function setForeColor($value)
   461     public function setForeColor($value)
   420     {
   462     {
   421         if (preg_match('`\#[0-9A-F]{6}`', $value)) {
   463         if (preg_match('`\#[0-9A-F]{6}`', $value)) {
   422             $this->_foreColor = hexdec($value);
   464             $this->_foreColor = hexdec($value);
   431         return $this;
   473         return $this;
   432     }
   474     }
   433 
   475 
   434     /**
   476     /**
   435      * Retrieve color of the barcode and text
   477      * Retrieve color of the barcode and text
       
   478      *
   436      * @return unknown
   479      * @return unknown
   437      */
   480      */
   438     public function getForeColor()
   481     public function getForeColor()
   439     {
   482     {
   440         return $this->_foreColor;
   483         return $this->_foreColor;
   441     }
   484     }
   442 
   485 
   443     /**
   486     /**
   444      * Set the color of the background
   487      * Set the color of the background
       
   488      *
   445      * @param integer $value
   489      * @param integer $value
   446      * @return Zend_Barcode_Object
   490      * @return $this
   447      * @throw Zend_Barcode_Object_Exception
   491      * @throws Zend_Barcode_Object_Exception
   448      */
   492      */
   449     public function setBackgroundColor($value)
   493     public function setBackgroundColor($value)
   450     {
   494     {
   451         if (preg_match('`\#[0-9A-F]{6}`', $value)) {
   495         if (preg_match('`\#[0-9A-F]{6}`', $value)) {
   452             $this->_backgroundColor = hexdec($value);
   496             $this->_backgroundColor = hexdec($value);
   461         return $this;
   505         return $this;
   462     }
   506     }
   463 
   507 
   464     /**
   508     /**
   465      * Retrieve background color of the image
   509      * Retrieve background color of the image
       
   510      *
   466      * @return integer
   511      * @return integer
   467      */
   512      */
   468     public function getBackgroundColor()
   513     public function getBackgroundColor()
   469     {
   514     {
   470         return $this->_backgroundColor;
   515         return $this->_backgroundColor;
   471     }
   516     }
   472 
   517 
   473     /**
   518     /**
   474      * Activate/deactivate drawing of the bar
   519      * Activate/deactivate drawing of the bar
       
   520      *
   475      * @param boolean $value
   521      * @param boolean $value
   476      * @return Zend_Barcode_Object
   522      * @return $this
   477      */
   523      */
   478     public function setWithBorder($value)
   524     public function setWithBorder($value)
   479     {
   525     {
   480         $this->_withBorder = (bool) $value;
   526         $this->_withBorder = (bool) $value;
   481         return $this;
   527         return $this;
   482     }
   528     }
   483 
   529 
   484     /**
   530     /**
   485      * Retrieve if border are draw or not
   531      * Retrieve if border are draw or not
       
   532      *
   486      * @return boolean
   533      * @return boolean
   487      */
   534      */
   488     public function getWithBorder()
   535     public function getWithBorder()
   489     {
   536     {
   490         return $this->_withBorder;
   537         return $this->_withBorder;
   491     }
   538     }
   492 
   539 
   493     /**
   540     /**
   494      * Activate/deactivate drawing of the quiet zones
   541      * Activate/deactivate drawing of the quiet zones
       
   542      *
   495      * @param boolean $value
   543      * @param boolean $value
   496      * @return Zend_Barcode_Object
   544      * @return $this
   497      */
   545      */
   498     public function setWithQuietZones($value)
   546     public function setWithQuietZones($value)
   499     {
   547     {
   500         $this->_withQuietZones = (bool) $value;
   548         $this->_withQuietZones = (bool) $value;
   501         return $this;
   549         return $this;
   502     }
   550     }
   503 
   551 
   504     /**
   552     /**
   505      * Retrieve if quiet zones are draw or not
   553      * Retrieve if quiet zones are draw or not
       
   554      *
   506      * @return boolean
   555      * @return boolean
   507      */
   556      */
   508     public function getWithQuietZones()
   557     public function getWithQuietZones()
   509     {
   558     {
   510         return $this->_withQuietZones;
   559         return $this->_withQuietZones;
   511     }
   560     }
   512 
   561 
   513     /**
   562     /**
   514      * Allow fast inversion of font/bars color and background color
   563      * Allow fast inversion of font/bars color and background color
   515      * @return Zend_Barcode_Object
   564      *
       
   565      * @return $this
   516      */
   566      */
   517     public function setReverseColor()
   567     public function setReverseColor()
   518     {
   568     {
   519         $tmp                    = $this->_foreColor;
   569         $tmp                    = $this->_foreColor;
   520         $this->_foreColor       = $this->_backgroundColor;
   570         $this->_foreColor       = $this->_backgroundColor;
   521         $this->_backgroundColor = $tmp;
   571         $this->_backgroundColor = $tmp;
       
   572 
   522         return $this;
   573         return $this;
   523     }
   574     }
   524 
   575 
   525     /**
   576     /**
   526      * Set orientation of barcode and text
   577      * Set orientation of barcode and text
   527      * @param float $value
   578      *
   528      * @return Zend_Barcode_Object
   579      * @param int|float|string|bool $value
   529      * @throw Zend_Barcode_Object_Exception
   580      * @return $this
       
   581      * @throws Zend_Barcode_Object_Exception
   530      */
   582      */
   531     public function setOrientation($value)
   583     public function setOrientation($value)
   532     {
   584     {
   533         $this->_orientation = floatval($value) - floor(floatval($value) / 360) * 360;
   585         $value              = floatval($value);
       
   586         $this->_orientation = $value - floor($value / 360) * 360;
   534         return $this;
   587         return $this;
   535     }
   588     }
   536 
   589 
   537     /**
   590     /**
   538      * Retrieve orientation of barcode and text
   591      * Retrieve orientation of barcode and text
       
   592      *
   539      * @return float
   593      * @return float
   540      */
   594      */
   541     public function getOrientation()
   595     public function getOrientation()
   542     {
   596     {
   543         return $this->_orientation;
   597         return $this->_orientation;
   544     }
   598     }
   545 
   599 
   546     /**
   600     /**
   547      * Set text to encode
   601      * Set text to encode
       
   602      *
   548      * @param string $value
   603      * @param string $value
   549      * @return Zend_Barcode_Object
   604      * @return $this
   550      */
   605      */
   551     public function setText($value)
   606     public function setText($value)
   552     {
   607     {
   553         $this->_text = trim($value);
   608         $this->_text = trim($value);
   554         return $this;
   609         return $this;
   555     }
   610     }
   556 
   611 
   557     /**
   612     /**
   558      * Retrieve text to encode
   613      * Retrieve text to encode
       
   614      *
   559      * @return string
   615      * @return string
   560      */
   616      */
   561     public function getText()
   617     public function getText()
   562     {
   618     {
   563         $text = $this->_text;
   619         $text = $this->_text;
   567         return $this->_addLeadingZeros($text);
   623         return $this->_addLeadingZeros($text);
   568     }
   624     }
   569 
   625 
   570     /**
   626     /**
   571      * Automatically add leading zeros if barcode length is fixed
   627      * Automatically add leading zeros if barcode length is fixed
   572      * @param string $text
   628      *
       
   629      * @param string  $text
   573      * @param boolean $withoutChecksum
   630      * @param boolean $withoutChecksum
       
   631      * @return string
   574      */
   632      */
   575     protected function _addLeadingZeros($text, $withoutChecksum = false)
   633     protected function _addLeadingZeros($text, $withoutChecksum = false)
   576     {
   634     {
   577         if ($this->_barcodeLength && $this->_addLeadingZeros) {
   635         if ($this->_barcodeLength && $this->_addLeadingZeros) {
   578             $omitChecksum = (int) ($this->_withChecksum && $withoutChecksum);
   636             $omitChecksum = (int) ($this->_withChecksum && $withoutChecksum);
   590         return $text;
   648         return $text;
   591     }
   649     }
   592 
   650 
   593     /**
   651     /**
   594      * Retrieve text to encode
   652      * Retrieve text to encode
       
   653      *
   595      * @return string
   654      * @return string
   596      */
   655      */
   597     public function getRawText()
   656     public function getRawText()
   598     {
   657     {
   599         return $this->_text;
   658         return $this->_text;
   600     }
   659     }
   601 
   660 
   602     /**
   661     /**
   603      * Retrieve text to display
   662      * Retrieve text to display
       
   663      *
   604      * @return string
   664      * @return string
   605      */
   665      */
   606     public function getTextToDisplay()
   666     public function getTextToDisplay()
   607     {
   667     {
   608         if ($this->_withChecksumInText) {
   668         if ($this->_withChecksumInText) {
   612         }
   672         }
   613     }
   673     }
   614 
   674 
   615     /**
   675     /**
   616      * Activate/deactivate drawing of text to encode
   676      * Activate/deactivate drawing of text to encode
       
   677      *
   617      * @param boolean $value
   678      * @param boolean $value
   618      * @return Zend_Barcode_Object
   679      * @return $this
   619      */
   680      */
   620     public function setDrawText($value)
   681     public function setDrawText($value)
   621     {
   682     {
   622         $this->_drawText = (bool) $value;
   683         $this->_drawText = (bool) $value;
   623         return $this;
   684         return $this;
   624     }
   685     }
   625 
   686 
   626     /**
   687     /**
   627      * Retrieve if drawing of text to encode is enabled
   688      * Retrieve if drawing of text to encode is enabled
       
   689      *
   628      * @return boolean
   690      * @return boolean
   629      */
   691      */
   630     public function getDrawText()
   692     public function getDrawText()
   631     {
   693     {
   632         return $this->_drawText;
   694         return $this->_drawText;
   633     }
   695     }
   634 
   696 
   635     /**
   697     /**
   636      * Activate/deactivate the adjustment of the position
   698      * Activate/deactivate the adjustment of the position
   637      * of the characters to the position of the bars
   699      * of the characters to the position of the bars
       
   700      *
   638      * @param boolean $value
   701      * @param boolean $value
   639      * @return Zend_Barcode_Object
   702      * @return $this
   640      * @throw Zend_Barcode_Object_Exception
   703      * @throws Zend_Barcode_Object_Exception
   641      */
   704      */
   642     public function setStretchText($value)
   705     public function setStretchText($value)
   643     {
   706     {
   644         $this->_stretchText = (bool) $value;
   707         $this->_stretchText = (bool) $value;
   645         return $this;
   708         return $this;
   646     }
   709     }
   647 
   710 
   648     /**
   711     /**
   649      * Retrieve if the adjustment of the position of the characters
   712      * Retrieve if the adjustment of the position of the characters
   650      * to the position of the bars is enabled
   713      * to the position of the bars is enabled
       
   714      *
   651      * @return boolean
   715      * @return boolean
   652      */
   716      */
   653     public function getStretchText()
   717     public function getStretchText()
   654     {
   718     {
   655         return $this->_stretchText;
   719         return $this->_stretchText;
   657 
   721 
   658     /**
   722     /**
   659      * Activate/deactivate the automatic generation
   723      * Activate/deactivate the automatic generation
   660      * of the checksum character
   724      * of the checksum character
   661      * added to the barcode text
   725      * added to the barcode text
       
   726      *
   662      * @param boolean $value
   727      * @param boolean $value
   663      * @return Zend_Barcode_Object
   728      * @return $this
   664      */
   729      */
   665     public function setWithChecksum($value)
   730     public function setWithChecksum($value)
   666     {
   731     {
   667         if (!$this->_mandatoryChecksum) {
   732         if (!$this->_mandatoryChecksum) {
   668             $this->_withChecksum = (bool) $value;
   733             $this->_withChecksum = (bool) $value;
   671     }
   736     }
   672 
   737 
   673     /**
   738     /**
   674      * Retrieve if the checksum character is automatically
   739      * Retrieve if the checksum character is automatically
   675      * added to the barcode text
   740      * added to the barcode text
       
   741      *
   676      * @return boolean
   742      * @return boolean
   677      */
   743      */
   678     public function getWithChecksum()
   744     public function getWithChecksum()
   679     {
   745     {
   680         return $this->_withChecksum;
   746         return $this->_withChecksum;
   682 
   748 
   683     /**
   749     /**
   684      * Activate/deactivate the automatic generation
   750      * Activate/deactivate the automatic generation
   685      * of the checksum character
   751      * of the checksum character
   686      * added to the barcode text
   752      * added to the barcode text
       
   753      *
   687      * @param boolean $value
   754      * @param boolean $value
   688      * @return Zend_Barcode_Object
   755      * @return $this
   689      * @throw Zend_Barcode_Object_Exception
   756      * @throws Zend_Barcode_Object_Exception
   690      */
   757      */
   691     public function setWithChecksumInText($value)
   758     public function setWithChecksumInText($value)
   692     {
   759     {
   693         if (!$this->_mandatoryChecksum) {
   760         if (!$this->_mandatoryChecksum) {
   694             $this->_withChecksumInText = (bool) $value;
   761             $this->_withChecksumInText = (bool) $value;
   695         }
   762         }
       
   763 
   696         return $this;
   764         return $this;
   697     }
   765     }
   698 
   766 
   699     /**
   767     /**
   700      * Retrieve if the checksum character is automatically
   768      * Retrieve if the checksum character is automatically
   701      * added to the barcode text
   769      * added to the barcode text
       
   770      *
   702      * @return boolean
   771      * @return boolean
   703      */
   772      */
   704     public function getWithChecksumInText()
   773     public function getWithChecksumInText()
   705     {
   774     {
   706         return $this->_withChecksumInText;
   775         return $this->_withChecksumInText;
   707     }
   776     }
   708 
   777 
   709     /**
   778     /**
   710      * Set the font for all instances of barcode
   779      * Set the font for all instances of barcode
       
   780      *
   711      * @param string $font
   781      * @param string $font
   712      * @return void
       
   713      */
   782      */
   714     public static function setBarcodeFont($font)
   783     public static function setBarcodeFont($font)
   715     {
   784     {
   716         if (is_string($font) || (is_int($font) && $font >= 1 && $font <= 5)) {
   785         if (is_string($font) || (is_int($font) && $font >= 1 && $font <= 5)) {
   717             self::$_staticFont = $font;
   786             self::$_staticFont = $font;
   720 
   789 
   721     /**
   790     /**
   722      * Set the font:
   791      * Set the font:
   723      *  - if integer between 1 and 5, use gd built-in fonts
   792      *  - if integer between 1 and 5, use gd built-in fonts
   724      *  - if string, $value is assumed to be the path to a TTF font
   793      *  - if string, $value is assumed to be the path to a TTF font
       
   794      *
   725      * @param integer|string $value
   795      * @param integer|string $value
   726      * @return Zend_Barcode_Object
   796      * @return $this
   727      * @throw Zend_Barcode_Object_Exception
   797      * @throws Zend_Barcode_Object_Exception
   728      */
   798      */
   729     public function setFont($value)
   799     public function setFont($value)
   730     {
   800     {
   731         if (is_int($value) && $value >= 1 && $value <= 5) {
   801         if (is_int($value) && $value >= 1 && $value <= 5) {
   732             if (!extension_loaded('gd')) {
   802             if (!extension_loaded('gd')) {
   743             $this->_fontSize = imagefontheight($value);
   813             $this->_fontSize = imagefontheight($value);
   744         } elseif (is_string($value)) {
   814         } elseif (is_string($value)) {
   745             $this->_font = $value;
   815             $this->_font = $value;
   746         } else {
   816         } else {
   747             require_once 'Zend/Barcode/Object/Exception.php';
   817             require_once 'Zend/Barcode/Object/Exception.php';
   748             throw new Zend_Barcode_Object_Exception(sprintf(
   818             throw new Zend_Barcode_Object_Exception(
   749                 'Invalid font "%s" provided to setFont()',
   819                 sprintf(
   750                 $value
   820                     'Invalid font "%s" provided to setFont()',
   751             ));
   821                     $value
       
   822                 )
       
   823             );
   752         }
   824         }
   753         return $this;
   825         return $this;
   754     }
   826     }
   755 
   827 
   756     /**
   828     /**
   757      * Retrieve the font
   829      * Retrieve the font
       
   830      *
   758      * @return integer|string
   831      * @return integer|string
   759      */
   832      */
   760     public function getFont()
   833     public function getFont()
   761     {
   834     {
   762         return $this->_font;
   835         return $this->_font;
   763     }
   836     }
   764 
   837 
   765     /**
   838     /**
   766      * Set the size of the font in case of TTF
   839      * Set the size of the font in case of TTF
       
   840      *
   767      * @param float $value
   841      * @param float $value
   768      * @return Zend_Barcode_Object
   842      * @return $this
   769      * @throw Zend_Barcode_Object_Exception
   843      * @throws Zend_Barcode_Object_Exception
   770      */
   844      */
   771     public function setFontSize($value)
   845     public function setFontSize($value)
   772     {
   846     {
   773         if (is_numeric($this->_font)) {
   847         if (is_numeric($this->_font)) {
   774             // Case of numeric font with GD
   848             // Case of numeric font with GD
   786         return $this;
   860         return $this;
   787     }
   861     }
   788 
   862 
   789     /**
   863     /**
   790      * Retrieve the size of the font in case of TTF
   864      * Retrieve the size of the font in case of TTF
       
   865      *
   791      * @return float
   866      * @return float
   792      */
   867      */
   793     public function getFontSize()
   868     public function getFontSize()
   794     {
   869     {
   795         return $this->_fontSize;
   870         return $this->_fontSize;
   796     }
   871     }
   797 
   872 
   798     /**
   873     /**
   799      * Quiet zone before first bar
   874      * Quiet zone before first bar
   800      * and after the last bar
   875      * and after the last bar
       
   876      *
   801      * @return integer
   877      * @return integer
   802      */
   878      */
   803     public function getQuietZone()
   879     public function getQuietZone()
   804     {
   880     {
   805         if ($this->_withQuietZones || $this->_mandatoryQuietZones) {
   881         if ($this->_withQuietZones || $this->_mandatoryQuietZones) {
   809         }
   885         }
   810     }
   886     }
   811 
   887 
   812     /**
   888     /**
   813      * Add an instruction in the array of instructions
   889      * Add an instruction in the array of instructions
       
   890      *
   814      * @param array $instruction
   891      * @param array $instruction
   815      */
   892      */
   816     protected function _addInstruction(array $instruction)
   893     protected function _addInstruction(array $instruction)
   817     {
   894     {
   818         $this->_instructions[] = $instruction;
   895         $this->_instructions[] = $instruction;
   819     }
   896     }
   820 
   897 
   821     /**
   898     /**
   822      * Retrieve the set of drawing instructions
   899      * Retrieve the set of drawing instructions
       
   900      *
   823      * @return array
   901      * @return array
   824      */
   902      */
   825     public function getInstructions()
   903     public function getInstructions()
   826     {
   904     {
   827         return $this->_instructions;
   905         return $this->_instructions;
   828     }
   906     }
   829 
   907 
   830     /**
   908     /**
   831      * Add a polygon drawing instruction in the set of instructions
   909      * Add a polygon drawing instruction in the set of instructions
   832      * @param array $points
   910      *
       
   911      * @param array   $points
   833      * @param integer $color
   912      * @param integer $color
   834      * @param boolean $filled
   913      * @param boolean $filled
   835      */
   914      */
   836     protected function _addPolygon(array $points, $color = null, $filled = true)
   915     protected function _addPolygon(array $points, $color = null, $filled = true)
   837     {
   916     {
   838         if ($color === null) {
   917         if ($color === null) {
   839             $color = $this->_foreColor;
   918             $color = $this->_foreColor;
   840         }
   919         }
   841         $this->_addInstruction(array(
   920         $this->_addInstruction(
   842             'type'   => 'polygon',
   921             array(
   843             'points' => $points,
   922                 'type'   => 'polygon',
   844             'color'  => $color,
   923                 'points' => $points,
   845             'filled' => $filled,
   924                 'color'  => $color,
   846         ));
   925                 'filled' => $filled,
       
   926             )
       
   927         );
   847     }
   928     }
   848 
   929 
   849     /**
   930     /**
   850      * Add a text drawing instruction in the set of instructions
   931      * Add a text drawing instruction in the set of instructions
   851      * @param string $text
   932      *
   852      * @param float $size
   933      * @param string    $text
   853      * @param array $position
   934      * @param float     $size
   854      * @param string $font
   935      * @param array     $position
   855      * @param integer $color
   936      * @param string    $font
   856      * @param string $alignment
   937      * @param integer   $color
   857      * @param float $orientation
   938      * @param string    $alignment
       
   939      * @param float|int $orientation
   858      */
   940      */
   859     protected function _addText(
   941     protected function _addText(
   860         $text,
   942         $text,
   861         $size,
   943         $size,
   862         $position,
   944         $position,
   866         $orientation = 0
   948         $orientation = 0
   867     ) {
   949     ) {
   868         if ($color === null) {
   950         if ($color === null) {
   869             $color = $this->_foreColor;
   951             $color = $this->_foreColor;
   870         }
   952         }
   871         $this->_addInstruction(array(
   953         $this->_addInstruction(
   872             'type'        => 'text',
   954             array(
   873             'text'        => $text,
   955                 'type'        => 'text',
   874             'size'        => $size,
   956                 'text'        => $text,
   875             'position'    => $position,
   957                 'size'        => $size,
   876             'font'        => $font,
   958                 'position'    => $position,
   877             'color'       => $color,
   959                 'font'        => $font,
   878             'alignment'   => $alignment,
   960                 'color'       => $color,
   879             'orientation' => $orientation,
   961                 'alignment'   => $alignment,
   880         ));
   962                 'orientation' => $orientation,
       
   963             )
       
   964         );
   881     }
   965     }
   882 
   966 
   883     /**
   967     /**
   884      * Checking of parameters after all settings
   968      * Checking of parameters after all settings
   885      * @return void
   969      *
       
   970      * @return bool
   886      */
   971      */
   887     public function checkParams()
   972     public function checkParams()
   888     {
   973     {
   889         $this->_checkText();
   974         $this->_checkText();
   890         $this->_checkFontAndOrientation();
   975         $this->_checkFontAndOrientation();
   892         return true;
   977         return true;
   893     }
   978     }
   894 
   979 
   895     /**
   980     /**
   896      * Check if a text is really provided to barcode
   981      * Check if a text is really provided to barcode
   897      * @return void
   982      *
   898      * @throw Zend_Barcode_Object_Exception
   983      * @param string|null $value
       
   984      * @throws Zend_Barcode_Object_Exception
   899      */
   985      */
   900     protected function _checkText($value = null)
   986     protected function _checkText($value = null)
   901     {
   987     {
   902         if ($value === null) {
   988         if ($value === null) {
   903             $value = $this->_text;
   989             $value = $this->_text;
   911         $this->validateText($value);
   997         $this->validateText($value);
   912     }
   998     }
   913 
   999 
   914     /**
  1000     /**
   915      * Check the ratio between the thick and the thin bar
  1001      * Check the ratio between the thick and the thin bar
   916      * @param integer $min
  1002      *
   917      * @param integer $max
  1003      * @param int $min
   918      * @return void
  1004      * @param int $max
   919      * @throw Zend_Barcode_Object_Exception
  1005      * @throws Zend_Barcode_Object_Exception
   920      */
  1006      */
   921     protected function _checkRatio($min = 2, $max = 3)
  1007     protected function _checkRatio($min = 2, $max = 3)
   922     {
  1008     {
   923         $ratio = $this->_barThickWidth / $this->_barThinWidth;
  1009         $ratio = $this->_barThickWidth / $this->_barThinWidth;
   924         if (!($ratio >= $min && $ratio <= $max)) {
  1010         if (!($ratio >= $min && $ratio <= $max)) {
   925             require_once 'Zend/Barcode/Object/Exception.php';
  1011             require_once 'Zend/Barcode/Object/Exception.php';
   926             throw new Zend_Barcode_Object_Exception(sprintf(
  1012             throw new Zend_Barcode_Object_Exception(
   927                 'Ratio thick/thin bar must be between %0.1f and %0.1f (actual %0.3f)',
  1013                 sprintf(
   928                 $min,
  1014                     'Ratio thick/thin bar must be between %0.1f and %0.1f (actual %0.3f)',
   929                 $max,
  1015                     $min,
   930                 $ratio
  1016                     $max,
   931             ));
  1017                     $ratio
       
  1018                 )
       
  1019             );
   932         }
  1020         }
   933     }
  1021     }
   934 
  1022 
   935     /**
  1023     /**
   936      * Drawing with an angle is just allow TTF font
  1024      * Drawing with an angle is just allow TTF font
   937      * @return void
  1025      *
   938      * @throw Zend_Barcode_Object_Exception
  1026      * @throws Zend_Barcode_Object_Exception
   939      */
  1027      */
   940     protected function _checkFontAndOrientation()
  1028     protected function _checkFontAndOrientation()
   941     {
  1029     {
   942         if (is_numeric($this->_font) && $this->_orientation != 0) {
  1030         if (is_numeric($this->_font) && $this->_orientation != 0) {
   943             require_once 'Zend/Barcode/Object/Exception.php';
  1031             require_once 'Zend/Barcode/Object/Exception.php';
   946             );
  1034             );
   947         }
  1035         }
   948     }
  1036     }
   949 
  1037 
   950     /**
  1038     /**
   951      * Width of the result image
  1039      * Width of the result image (before any rotation)
   952      * (before any rotation)
  1040      *
   953      * @return integer
  1041      * @return integer
   954      */
  1042      */
   955     protected function _calculateWidth()
  1043     protected function _calculateWidth()
   956     {
  1044     {
   957         return (int) $this->_withBorder
  1045         return (int) $this->_withBorder
   959             + (int) $this->_withBorder;
  1047             + (int) $this->_withBorder;
   960     }
  1048     }
   961 
  1049 
   962     /**
  1050     /**
   963      * Calculate the width of the barcode
  1051      * Calculate the width of the barcode
       
  1052      *
   964      * @return integer
  1053      * @return integer
   965      */
  1054      */
   966     abstract protected function _calculateBarcodeWidth();
  1055     abstract protected function _calculateBarcodeWidth();
   967 
  1056 
   968     /**
  1057     /**
   969      * Height of the result object
  1058      * Height of the result object
   970      * @return integer
  1059      *
       
  1060      * @return int
   971      */
  1061      */
   972     protected function _calculateHeight()
  1062     protected function _calculateHeight()
   973     {
  1063     {
   974         return (int) $this->_withBorder * 2
  1064         return (int) $this->_withBorder * 2
   975             + $this->_calculateBarcodeHeight()
  1065             + $this->_calculateBarcodeHeight()
   976             + (int) $this->_withBorder * 2;
  1066             + (int) $this->_withBorder * 2;
   977     }
  1067     }
   978 
  1068 
   979     /**
  1069     /**
   980      * Height of the barcode
  1070      * Height of the barcode
   981      * @return integer
  1071      *
       
  1072      * @return int
   982      */
  1073      */
   983     protected function _calculateBarcodeHeight()
  1074     protected function _calculateBarcodeHeight()
   984     {
  1075     {
   985         $textHeight = 0;
  1076         $textHeight = 0;
   986         $extraHeight = 0;
  1077         $extraHeight = 0;
   987         if ($this->_drawText) {
  1078         if ($this->_drawText) {
   988             $textHeight += $this->_fontSize;
  1079             $textHeight += $this->_fontSize;
   989             $extraHeight = 2;
  1080             $extraHeight = 2;
   990         }
  1081         }
   991         return ($this->_barHeight + $textHeight) * $this->_factor + $extraHeight;
  1082 
       
  1083         return ($this->_barHeight + $textHeight) * $this->_factor
       
  1084         + $extraHeight;
   992     }
  1085     }
   993 
  1086 
   994     /**
  1087     /**
   995      * Get height of the result object
  1088      * Get height of the result object
   996      * @return integer
  1089      *
       
  1090      * @param bool $recalculate
       
  1091      * @return int
   997      */
  1092      */
   998     public function getHeight($recalculate = false)
  1093     public function getHeight($recalculate = false)
   999     {
  1094     {
  1000         if ($this->_height === null || $recalculate) {
  1095         if ($this->_height === null || $recalculate) {
  1001             $this->_height =
  1096             $this->_height =
  1002                 abs($this->_calculateHeight() * cos($this->_orientation / 180 * pi()))
  1097                 abs(
  1003                 + abs($this->_calculateWidth() * sin($this->_orientation / 180 * pi()));
  1098                     $this->_calculateHeight() * cos(
       
  1099                         $this->_orientation / 180 * pi()
       
  1100                     )
       
  1101                 )
       
  1102                 + abs(
       
  1103                     $this->_calculateWidth() * sin(
       
  1104                         $this->_orientation / 180 * pi()
       
  1105                     )
       
  1106                 );
  1004         }
  1107         }
  1005         return $this->_height;
  1108         return $this->_height;
  1006     }
  1109     }
  1007 
  1110 
  1008     /**
  1111     /**
  1009      * Get width of the result object
  1112      * Get width of the result object
  1010      * @return integer
  1113      *
       
  1114      * @param bool $recalculate
       
  1115      * @return int
  1011      */
  1116      */
  1012     public function getWidth($recalculate = false)
  1117     public function getWidth($recalculate = false)
  1013     {
  1118     {
  1014         if ($this->_width === null || $recalculate) {
  1119         if ($this->_width === null || $recalculate) {
  1015             $this->_width =
  1120             $this->_width =
  1016                 abs($this->_calculateWidth() * cos($this->_orientation / 180 * pi()))
  1121                 abs(
  1017                 + abs($this->_calculateHeight() * sin($this->_orientation / 180 * pi()));
  1122                     $this->_calculateWidth() * cos(
       
  1123                         $this->_orientation / 180 * pi()
       
  1124                     )
       
  1125                 )
       
  1126                 + abs(
       
  1127                     $this->_calculateHeight() * sin(
       
  1128                         $this->_orientation / 180 * pi()
       
  1129                     )
       
  1130                 );
  1018         }
  1131         }
  1019         return $this->_width;
  1132         return $this->_width;
  1020     }
  1133     }
  1021 
  1134 
  1022     /**
  1135     /**
  1023      * Calculate the offset from the left of the object
  1136      * Calculate the offset from the left of the object
  1024      * if an orientation is activated
  1137      * if an orientation is activated
  1025      * @param boolean $recalculate
  1138      *
       
  1139      * @param bool $recalculate
  1026      * @return float
  1140      * @return float
  1027      */
  1141      */
  1028     public function getOffsetLeft($recalculate = false)
  1142     public function getOffsetLeft($recalculate = false)
  1029     {
  1143     {
  1030         if ($this->_offsetLeft === null || $recalculate) {
  1144         if ($this->_offsetLeft === null || $recalculate) {
  1047     }
  1161     }
  1048 
  1162 
  1049     /**
  1163     /**
  1050      * Calculate the offset from the top of the object
  1164      * Calculate the offset from the top of the object
  1051      * if an orientation is activated
  1165      * if an orientation is activated
  1052      * @param boolean $recalculate
  1166      *
       
  1167      * @param bool $recalculate
  1053      * @return float
  1168      * @return float
  1054      */
  1169      */
  1055     public function getOffsetTop($recalculate = false)
  1170     public function getOffsetTop($recalculate = false)
  1056     {
  1171     {
  1057         if ($this->_offsetTop === null || $recalculate) {
  1172         if ($this->_offsetTop === null || $recalculate) {
  1073         return $this->_offsetTop;
  1188         return $this->_offsetTop;
  1074     }
  1189     }
  1075 
  1190 
  1076     /**
  1191     /**
  1077      * Apply rotation on a point in X/Y dimensions
  1192      * Apply rotation on a point in X/Y dimensions
  1078      * @param float $x1     x-position before rotation
  1193      *
  1079      * @param float $y1     y-position before rotation
  1194      * @param  float $x1 x-position before rotation
  1080      * @return array        Array of two elements corresponding to the new XY point
  1195      * @param  float $y1 y-position before rotation
       
  1196      * @return array Array of two elements corresponding to the new XY point
  1081      */
  1197      */
  1082     protected function _rotate($x1, $y1)
  1198     protected function _rotate($x1, $y1)
  1083     {
  1199     {
  1084         $x2 = $x1 * cos($this->_orientation / 180 * pi())
  1200         $x2 = $x1 * cos($this->_orientation / 180 * pi())
  1085             - $y1 * sin($this->_orientation / 180 * pi())
  1201             - $y1 * sin($this->_orientation / 180 * pi())
  1086             + $this->getOffsetLeft();
  1202             + $this->getOffsetLeft();
  1087         $y2 = $y1 * cos($this->_orientation / 180 * pi())
  1203         $y2 = $y1 * cos($this->_orientation / 180 * pi())
  1088             + $x1 * sin($this->_orientation / 180 * pi())
  1204             + $x1 * sin($this->_orientation / 180 * pi())
  1089             + $this->getOffsetTop();
  1205             + $this->getOffsetTop();
  1090         return array(intval($x2) , intval($y2));
  1206 
       
  1207         return array(
       
  1208             intval($x2),
       
  1209             intval($y2)
       
  1210         );
  1091     }
  1211     }
  1092 
  1212 
  1093     /**
  1213     /**
  1094      * Complete drawing of the barcode
  1214      * Complete drawing of the barcode
       
  1215      *
  1095      * @return array Table of instructions
  1216      * @return array Table of instructions
  1096      */
  1217      */
  1097     public function draw()
  1218     public function draw()
  1098     {
  1219     {
  1099         $this->checkParams();
  1220         $this->checkParams();
  1103         return $this->getInstructions();
  1224         return $this->getInstructions();
  1104     }
  1225     }
  1105 
  1226 
  1106     /**
  1227     /**
  1107      * Draw the barcode
  1228      * Draw the barcode
  1108      * @return void
       
  1109      */
  1229      */
  1110     protected function _drawBarcode()
  1230     protected function _drawBarcode()
  1111     {
  1231     {
  1112         $barcodeTable = $this->_prepareBarcode();
  1232         $barcodeTable = $this->_prepareBarcode();
  1113 
  1233 
  1122             $this->_calculateWidth() - 1,
  1242             $this->_calculateWidth() - 1,
  1123             $this->_calculateHeight() - 1
  1243             $this->_calculateHeight() - 1
  1124         );
  1244         );
  1125         $point4 = $this->_rotate($this->_calculateWidth() - 1, 0);
  1245         $point4 = $this->_rotate($this->_calculateWidth() - 1, 0);
  1126 
  1246 
  1127         $this->_addPolygon(array(
  1247         $this->_addPolygon(
  1128             $point1,
  1248             array(
  1129             $point2,
  1249                 $point1,
  1130             $point3,
  1250                 $point2,
  1131             $point4
  1251                 $point3,
  1132         ), $this->_backgroundColor);
  1252                 $point4
       
  1253             ), $this->_backgroundColor
       
  1254         );
  1133 
  1255 
  1134         $xpos     += $this->getQuietZone();
  1256         $xpos     += $this->getQuietZone();
  1135         $barLength = $this->_barHeight * $this->_factor;
  1257         $barLength = $this->_barHeight * $this->_factor;
  1136 
  1258 
  1137         foreach ($barcodeTable as $bar) {
  1259         foreach ($barcodeTable as $bar) {
  1145                 );
  1267                 );
  1146                 $point4 = $this->_rotate(
  1268                 $point4 = $this->_rotate(
  1147                     $xpos + $width - 1,
  1269                     $xpos + $width - 1,
  1148                     $ypos + $bar[2] * $barLength
  1270                     $ypos + $bar[2] * $barLength
  1149                 );
  1271                 );
  1150                 $this->_addPolygon(array(
  1272                 $this->_addPolygon(
  1151                     $point1,
  1273                     array(
  1152                     $point2,
  1274                         $point1,
  1153                     $point3,
  1275                         $point2,
  1154                     $point4,
  1276                         $point3,
  1155                 ));
  1277                         $point4,
       
  1278                     )
       
  1279                 );
  1156             }
  1280             }
  1157             $xpos += $width;
  1281             $xpos += $width;
  1158         }
  1282         }
  1159 
  1283 
  1160         $this->_postDrawBarcode();
  1284         $this->_postDrawBarcode();
  1161     }
  1285     }
  1162 
  1286 
  1163     /**
  1287     /**
  1164      * Partial function to draw border
  1288      * Partial function to draw border
  1165      * @return void
       
  1166      */
  1289      */
  1167     protected function _drawBorder()
  1290     protected function _drawBorder()
  1168     {
  1291     {
  1169         if ($this->_withBorder) {
  1292         if ($this->_withBorder) {
  1170             $point1 = $this->_rotate(0, 0);
  1293             $point1 = $this->_rotate(0, 0);
  1172             $point3 = $this->_rotate(
  1295             $point3 = $this->_rotate(
  1173                 $this->_calculateWidth() - 1,
  1296                 $this->_calculateWidth() - 1,
  1174                 $this->_calculateHeight() - 1
  1297                 $this->_calculateHeight() - 1
  1175             );
  1298             );
  1176             $point4 = $this->_rotate(0, $this->_calculateHeight() - 1);
  1299             $point4 = $this->_rotate(0, $this->_calculateHeight() - 1);
  1177             $this->_addPolygon(array(
  1300             $this->_addPolygon(
  1178                 $point1,
  1301                 array(
  1179                 $point2,
  1302                     $point1,
  1180                 $point3,
  1303                     $point2,
  1181                 $point4,
  1304                     $point3,
  1182                 $point1,
  1305                     $point4,
  1183             ), $this->_foreColor, false);
  1306                     $point1,
       
  1307                 ), $this->_foreColor, false
       
  1308             );
  1184         }
  1309         }
  1185     }
  1310     }
  1186 
  1311 
  1187     /**
  1312     /**
  1188      * Partial function to draw text
  1313      * Partial function to draw text
  1189      * @return void
       
  1190      */
  1314      */
  1191     protected function _drawText()
  1315     protected function _drawText()
  1192     {
  1316     {
  1193         if ($this->_drawText) {
  1317         if ($this->_drawText) {
  1194             $text = $this->getTextToDisplay();
  1318             $text = $this->getTextToDisplay();
  1229         }
  1353         }
  1230     }
  1354     }
  1231 
  1355 
  1232     /**
  1356     /**
  1233      * Check for invalid characters
  1357      * Check for invalid characters
  1234      * @param   string $value    Text to be ckecked
  1358      *
  1235      * @return void
  1359      * @param string $value Text to be ckecked
  1236      */
  1360      */
  1237     public function validateText($value)
  1361     public function validateText($value)
  1238     {
  1362     {
  1239         $this->_validateText($value);
  1363         $this->_validateText($value);
  1240     }
  1364     }
  1241 
  1365 
  1242     /**
  1366     /**
  1243      * Standard validation for most of barcode objects
  1367      * Standard validation for most of barcode objects
       
  1368      *
  1244      * @param string $value
  1369      * @param string $value
  1245      * @param array  $options
  1370      * @param array  $options
       
  1371      * @throws Zend_Barcode_Object_Exception
  1246      */
  1372      */
  1247     protected function _validateText($value, $options = array())
  1373     protected function _validateText($value, $options = array())
  1248     {
  1374     {
  1249         $validatorName = (isset($options['validator'])) ? $options['validator'] : $this->getType();
  1375         $validatorName = (isset($options['validator'])) ? $options['validator'] : $this->getType();
  1250 
  1376 
  1251         $validator = new Zend_Validate_Barcode(array(
  1377         $validator = new Zend_Validate_Barcode(
  1252             'adapter'  => $validatorName,
  1378             array(
  1253             'checksum' => false,
  1379                 'adapter'  => $validatorName,
  1254         ));
  1380                 'checksum' => false,
       
  1381             )
       
  1382         );
  1255 
  1383 
  1256         $checksumCharacter = '';
  1384         $checksumCharacter = '';
  1257         $withChecksum = false;
  1385         $withChecksum = false;
  1258         if ($this->_mandatoryChecksum) {
  1386         if ($this->_mandatoryChecksum) {
  1259             $checksumCharacter = $this->_substituteChecksumCharacter;
  1387             $checksumCharacter = $this->_substituteChecksumCharacter;
  1260             $withChecksum = true;
  1388             $withChecksum = true;
  1261         }
  1389         }
  1262 
  1390 
  1263         $value = $this->_addLeadingZeros($value, $withChecksum) . $checksumCharacter;
  1391         $value = $this->_addLeadingZeros($value, $withChecksum)
       
  1392                . $checksumCharacter;
  1264 
  1393 
  1265         if (!$validator->isValid($value)) {
  1394         if (!$validator->isValid($value)) {
  1266             $message = implode("\n", $validator->getMessages());
  1395             $message = implode("\n", $validator->getMessages());
  1267 
  1396 
  1268             /**
  1397             /**
  1289      */
  1418      */
  1290     abstract protected function _prepareBarcode();
  1419     abstract protected function _prepareBarcode();
  1291 
  1420 
  1292     /**
  1421     /**
  1293      * Checking of parameters after all settings
  1422      * Checking of parameters after all settings
  1294      *
       
  1295      * @return void
       
  1296      */
  1423      */
  1297     abstract protected function _checkParams();
  1424     abstract protected function _checkParams();
  1298 
  1425 
  1299     /**
  1426     /**
  1300      * Allow each child to draw something else
  1427      * Allow each child to draw something else
  1301      *
       
  1302      * @return void
       
  1303      */
  1428      */
  1304     protected function _preDrawBarcode()
  1429     protected function _preDrawBarcode()
  1305     {
  1430     {
  1306     }
  1431     }
  1307 
  1432 
  1308     /**
  1433     /**
  1309      * Allow each child to draw something else
  1434      * Allow each child to draw something else
  1310      * (ex: bearer bars in interleaved 2 of 5 code)
  1435      * (ex: bearer bars in interleaved 2 of 5 code)
  1311      *
       
  1312      * @return void
       
  1313      */
  1436      */
  1314     protected function _postDrawBarcode()
  1437     protected function _postDrawBarcode()
  1315     {
  1438     {
  1316     }
  1439     }
  1317 }
  1440 }