| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Barcode
- * @subpackage Object
- * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * Class for generate Barcode
- *
- * @category Zend
- * @package Zend_Barcode
- * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- abstract class Zend_Barcode_Object_ObjectAbstract
- {
- /**
- * Namespace of the barcode for autoloading
- * @var string
- */
- protected $_barcodeNamespace = 'Zend_Barcode_Object';
- /**
- * Set of drawing instructions
- * @var array
- */
- protected $_instructions = array();
- /**
- * Barcode type
- * @var string
- */
- protected $_type = null;
- /**
- * Height of the object
- * @var integer
- */
- protected $_height = null;
- /**
- * Width of the object
- * @var integer
- */
- protected $_width = null;
- /**
- * Height of the bar
- * @var integer
- */
- protected $_barHeight = 50;
- /**
- * Width of a thin bar
- * @var integer
- */
- protected $_barThinWidth = 1;
- /**
- * Width of a thick bar
- * @var integer
- */
- protected $_barThickWidth = 3;
- /**
- * Factor to multiply bar and font measure
- * (barHeight, barThinWidth, barThickWidth & fontSize)
- * @var integer
- */
- protected $_factor = 1;
- /**
- * Font and bars color of the object
- * @var integer
- */
- protected $_foreColor = 0x000000;
- /**
- * Background color of the object
- * @var integer
- */
- protected $_backgroundColor = 0xFFFFFF;
- /**
- * Activate/deactivate border of the object
- * @var boolean
- */
- protected $_withBorder = false;
- /**
- * Orientation of the barcode in degrees
- * @var float
- */
- protected $_orientation = 0;
- /**
- * Offset from the top the object
- * (calculated from the orientation)
- * @var integer
- */
- protected $_offsetTop = null;
- /**
- * Offset from the left the object
- * (calculated from the orientation)
- * @var integer
- */
- protected $_offsetLeft = null;
- /**
- * Text to display
- * @var string
- */
- protected $_text = null;
- /**
- * Display (or not) human readable text
- * @var boolean
- */
- protected $_drawText = true;
- /**
- * Adjust (or not) position of human readable characters with barcode
- * @var boolean
- */
- protected $_stretchText = false;
- /**
- * Font resource
- * - integer (1 to 5): corresponds to GD included fonts
- * - string: corresponds to path of a TTF font
- * @var integer|string
- */
- protected $_font = null;
- /**
- * Font size
- * @var float
- */
- protected $_fontSize = 10;
- /**
- * Drawing of checksum
- * @var boolean
- */
- protected $_withChecksum = false;
- /**
- * Drawing of checksum inside text
- * @var boolean
- */
- protected $_withChecksumInText = false;
- /**
- * @var $_barcodeLength integer | string
- */
- protected $_barcodeLength = null;
- /**
- * TTF font name: can be set before instanciation of the object
- * @var string
- */
- protected static $_staticFont = null;
- /**
- * Constructor
- * @param array|Zend_Config $options
- * @return void
- */
- public function __construct($options = null)
- {
- if (self::$_staticFont !== null) {
- $this->_font = self::$_staticFont;
- }
- if ($options instanceof Zend_Config) {
- $options = $options->toArray();
- }
- if (is_array($options)) {
- $this->setOptions($options);
- }
- $this->_type = strtolower(substr(get_class($this), strlen($this->_barcodeNamespace) + 1));
- }
- /**
- * Set barcode state from options array
- * @param array $options
- * @return Zend_Barcode_Object
- */
- public function setOptions($options)
- {
- foreach ($options as $key => $value) {
- $method = 'set' . $key;
- if (method_exists($this, $method)) {
- $this->$method($value);
- }
- }
- return $this;
- }
- /**
- * Set barcode state from config object
- * @param Zend_Config $config
- * @return Zend_Barcode_Object
- */
- public function setConfig(Zend_Config $config)
- {
- return $this->setOptions($config->toArray());
- }
- /**
- * Set barcode namespace for autoloading
- *
- * @param string $namespace
- * @return Zend_Barcode_Object
- */
- public function setBarcodeNamespace($namespace)
- {
- $this->_barcodeNamespace = $namespace;
- return $this;
- }
- /**
- * Retrieve barcode namespace
- *
- * @return string
- */
- public function getBarcodeNamespace()
- {
- return $this->_barcodeNamespace;
- }
- /**
- * Retrieve type of barcode
- * @return string
- */
- public function getType()
- {
- return $this->_type;
- }
- /**
- * Set height of the barcode bar
- * @param integer $value
- * @return Zend_Barcode_Object
- * @throw Zend_Barcode_Object_Exception
- */
- public function setBarHeight($value)
- {
- if (intval($value) <= 0) {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(
- 'Bar height must be greater than 0'
- );
- }
- $this->_barHeight = intval($value);
- return $this;
- }
- /**
- * Get height of the barcode bar
- * @return integer
- */
- public function getBarHeight()
- {
- return $this->_barHeight;
- }
- /**
- * Set thickness of thin bar
- * @param integer $value
- * @return Zend_Barcode_Object
- * @throw Zend_Barcode_Object_Exception
- */
- public function setBarThinWidth($value)
- {
- if (intval($value) <= 0) {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(
- 'Bar width must be greater than 0'
- );
- }
- $this->_barThinWidth = intval($value);
- return $this;
- }
- /**
- * Get thickness of thin bar
- * @return integer
- */
- public function getBarThinWidth()
- {
- return $this->_barThinWidth;
- }
- /**
- * Set thickness of thick bar
- * @param integer $value
- * @return Zend_Barcode_Object
- * @throw Zend_Barcode_Object_Exception
- */
- public function setBarThickWidth($value)
- {
- if (intval($value) <= 0) {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(
- 'Bar width must be greater than 0'
- );
- }
- $this->_barThickWidth = intval($value);
- return $this;
- }
- /**
- * Get thickness of thick bar
- * @return integer
- */
- public function getBarThickWidth()
- {
- return $this->_barThickWidth;
- }
- /**
- * Set factor applying to
- * thinBarWidth - thickBarWidth - barHeight - fontSize
- * @param integer $value
- * @return Zend_Barcode_Object
- * @throw Zend_Barcode_Object_Exception
- */
- public function setFactor($value)
- {
- if (floatval($value) <= 0) {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(
- 'Factor must be greater than 0'
- );
- }
- $this->_factor = floatval($value);
- return $this;
- }
- /**
- * Get factor applying to
- * thinBarWidth - thickBarWidth - barHeight - fontSize
- * @return integer
- */
- public function getFactor()
- {
- return $this->_factor;
- }
- /**
- * Set color of the barcode and text
- * @param string $value
- * @return Zend_Barcode_Object
- * @throw Zend_Barcode_Object_Exception
- */
- public function setForeColor($value)
- {
- if (preg_match('`\#[0-9A-F]{6}`', $value)) {
- $this->_foreColor = hexdec($value);
- } elseif (is_numeric($value) && $value >= 0 && $value <= 16777125) {
- $this->_foreColor = intval($value);
- } else {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(
- 'Text color must be set as #[0-9A-F]{6}'
- );
- }
- return $this;
- }
- /**
- * Retrieve color of the barcode and text
- * @return unknown
- */
- public function getForeColor()
- {
- return $this->_foreColor;
- }
- /**
- * Set the color of the background
- * @param integer $value
- * @return Zend_Barcode_Object
- * @throw Zend_Barcode_Object_Exception
- */
- public function setBackgroundColor($value)
- {
- if (preg_match('`\#[0-9A-F]{6}`', $value)) {
- $this->_backgroundColor = hexdec($value);
- } elseif (is_numeric($value) && $value >= 0 && $value <= 16777125) {
- $this->_backgroundColor = intval($value);
- } else {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(
- 'Background color must be set as #[0-9A-F]{6}'
- );
- }
- return $this;
- }
- /**
- * Retrieve background color of the image
- * @return integer
- */
- public function getBackgroundColor()
- {
- return $this->_backgroundColor;
- }
- /**
- * Activate/deactivate drawing of the bar
- * @param boolean $value
- * @return Zend_Barcode_Object
- */
- public function setWithBorder($value)
- {
- $this->_withBorder = (bool) $value;
- return $this;
- }
- /**
- * Retrieve if border are draw or not
- * @return boolean
- */
- public function getWithBorder()
- {
- return $this->_withBorder;
- }
- /**
- * Allow fast inversion of font/bars color and background color
- * @return Zend_Barcode_Object
- */
- public function setReverseColor()
- {
- $tmp = $this->_foreColor;
- $this->_foreColor = $this->_backgroundColor;
- $this->_backgroundColor = $tmp;
- return $this;
- }
- /**
- * Set orientation of barcode and text
- * @param float $value
- * @return Zend_Barcode_Object
- * @throw Zend_Barcode_Object_Exception
- */
- public function setOrientation($value)
- {
- $this->_orientation = floatval($value) - floor(floatval($value) / 360) * 360;
- return $this;
- }
- /**
- * Retrieve orientation of barcode and text
- * @return float
- */
- public function getOrientation()
- {
- return $this->_orientation;
- }
- /**
- * Set text to encode
- * @param string $value
- * @return Zend_Barcode_Object
- */
- public function setText($value)
- {
- $this->_text = trim($value);
- return $this;
- }
- /**
- * Retrieve text to encode
- * @return string
- */
- public function getText()
- {
- if ($this->_withChecksum) {
- return $this->_getTextWithChecksum();
- }
- return $this->_text;
- }
- /**
- * Retrieve text to encode
- * @return string
- */
- public function getRawText()
- {
- return $this->_text;
- }
- /**
- * Retrieve text to display
- * @return string
- */
- public function getTextToDisplay()
- {
- if ($this->_withChecksumInText) {
- return $this->_getTextWithChecksum();
- }
- return $this->_text;
- }
- /**
- * Activate/deactivate drawing of text to encode
- * @param boolean $value
- * @return Zend_Barcode_Object
- */
- public function setDrawText($value)
- {
- $this->_drawText = (bool) $value;
- return $this;
- }
- /**
- * Retrieve if drawing of text to encode is enabled
- * @return boolean
- */
- public function getDrawText()
- {
- return $this->_drawText;
- }
- /**
- * Activate/deactivate the adjustment of the position
- * of the characters to the position of the bars
- * @param boolean $value
- * @return Zend_Barcode_Object
- * @throw Zend_Barcode_Object_Exception
- */
- public function setStretchText($value)
- {
- $this->_stretchText = (bool) $value;
- return $this;
- }
- /**
- * Retrieve if the adjustment of the position of the characters
- * to the position of the bars is enabled
- * @return boolean
- */
- public function getStretchText()
- {
- return $this->_stretchText;
- }
- /**
- * Activate/deactivate the automatic generation
- * of the checksum character
- * added to the barcode text
- * @param boolean $value
- * @return Zend_Barcode_Object
- */
- public function setWithChecksum($value)
- {
- $this->_withChecksum = (bool) $value;
- return $this;
- }
- /**
- * Retrieve if the checksum character is automatically
- * added to the barcode text
- * @return boolean
- */
- public function getWithChecksum()
- {
- return $this->_withChecksum;
- }
- /**
- * Activate/deactivate the automatic generation
- * of the checksum character
- * added to the barcode text
- * @param boolean $value
- * @return Zend_Barcode_Object
- * @throw Zend_Barcode_Object_Exception
- */
- public function setWithChecksumInText($value)
- {
- $this->_withChecksumInText = (bool) $value;
- return $this;
- }
- /**
- * Retrieve if the checksum character is automatically
- * added to the barcode text
- * @return boolean
- */
- public function getWithChecksumInText()
- {
- return $this->_withChecksumInText;
- }
- /**
- * Set the font for all instances of barcode
- * @param string $font
- * @return void
- */
- public static function setBarcodeFont($font)
- {
- if (is_string($font) || (is_int($font) && $font >= 1 && $font <= 5)) {
- self::$_staticFont = $font;
- }
- }
- /**
- * Set the font:
- * - if integer between 1 and 5, use gd built-in fonts
- * - if string, $value is assumed to be the path to a TTF font
- * @param integer|string $value
- * @return Zend_Barcode_Object
- * @throw Zend_Barcode_Object_Exception
- */
- public function setFont($value)
- {
- if (is_int($value) && $value >= 1 && $value <= 5) {
- if (!extension_loaded('gd')) {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(
- 'GD extension is required to use numeric font'
- );
- }
- // Case of numeric font with GD
- $this->_font = $value;
- // In this case font size is given by:
- $this->_fontSize = imagefontheight($value);
- } elseif (is_string($value)) {
- $this->_font = $value;
- } else {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(sprintf(
- 'Invalid font "%s" provided to setFont()',
- $value
- ));
- }
- return $this;
- }
- /**
- * Retrieve the font
- * @return integer|string
- */
- public function getFont()
- {
- return $this->_font;
- }
- /**
- * Set the size of the font in case of TTF
- * @param float $value
- * @return Zend_Barcode_Object
- * @throw Zend_Barcode_Object_Exception
- */
- public function setFontSize($value)
- {
- if (is_numeric($this->_font)) {
- // Case of numeric font with GD
- return $this;
- }
- if (!is_numeric($value)) {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(
- 'Font size must be a numeric value'
- );
- }
- $this->_fontSize = $value;
- return $this;
- }
- /**
- * Retrieve the size of the font in case of TTF
- * @return float
- */
- public function getFontSize()
- {
- return $this->_fontSize;
- }
- /**
- * Quiet zone before first bar
- * and after the last bar
- * @return integer
- */
- public function getQuietZone()
- {
- return 10 * $this->_barThinWidth * $this->_factor;
- }
- /**
- * Add an instruction in the array of instructions
- * @param array $instruction
- */
- protected function _addInstruction(array $instruction)
- {
- $this->_instructions[] = $instruction;
- }
- /**
- * Retrieve the set of drawing instructions
- * @return array
- */
- public function getInstructions()
- {
- return $this->_instructions;
- }
- /**
- * Add a polygon drawing instruction in the set of instructions
- * @param array $points
- * @param integer $color
- * @param boolean $filled
- */
- protected function _addPolygon(array $points, $color = null, $filled = true)
- {
- if ($color === null) {
- $color = $this->_foreColor;
- }
- $this->_addInstruction(array(
- 'type' => 'polygon',
- 'points' => $points,
- 'color' => $color,
- 'filled' => $filled,
- ));
- }
- /**
- * Add a text drawing instruction in the set of instructions
- * @param string $text
- * @param float $size
- * @param array $position
- * @param string $font
- * @param integer $color
- * @param string $alignment
- * @param float $orientation
- */
- protected function _addText(
- $text,
- $size,
- $position,
- $font,
- $color,
- $alignment = 'center',
- $orientation = 0
- ) {
- if ($color === null) {
- $color = $this->_foreColor;
- }
- $this->_addInstruction(array(
- 'type' => 'text',
- 'text' => $text,
- 'size' => $size,
- 'position' => $position,
- 'font' => $font,
- 'color' => $color,
- 'alignment' => $alignment,
- 'orientation' => $orientation,
- ));
- }
- /**
- * Checking of parameters after all settings
- * @return void
- */
- public function checkParams()
- {
- $this->_checkText();
- $this->_checkFontAndOrientation();
- $this->_checkParams();
- return true;
- }
- /**
- * Check if a text is really provided to barcode
- * @return void
- * @throw Zend_Barcode_Object_Exception
- */
- protected function _checkText($value = null)
- {
- if ($value === null) {
- $value = $this->_text;
- }
- if (!strlen($value)) {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(
- 'A text must be provide to Barcode before drawing'
- );
- }
- $this->validateText($value);
- }
- /**
- * Check the ratio between the thick and the thin bar
- * @param integer $min
- * @param integer $max
- * @return void
- * @throw Zend_Barcode_Object_Exception
- */
- protected function _checkRatio($min = 2, $max = 3)
- {
- $ratio = $this->_barThickWidth / $this->_barThinWidth;
- if (!($ratio >= $min && $ratio <= $max)) {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(sprintf(
- 'Ratio thick/thin bar must be between %0.1f and %0.1f (actual %0.3f)',
- $min,
- $max,
- $ratio
- ));
- }
- }
- /**
- * Drawing with an angle is just allow TTF font
- * @return void
- * @throw Zend_Barcode_Object_Exception
- */
- protected function _checkFontAndOrientation()
- {
- if (is_numeric($this->_font) && $this->_orientation != 0) {
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception(
- 'Only drawing with TTF font allow orientation of the barcode.'
- );
- }
- }
- /**
- * Width of the result image
- * (before any rotation)
- * @return integer
- */
- protected function _calculateWidth()
- {
- return (int) $this->_withBorder
- + $this->_calculateBarcodeWidth()
- + (int) $this->_withBorder;
- }
- /**
- * Calculate the width of the barcode
- * @return integer
- */
- abstract protected function _calculateBarcodeWidth();
- /**
- * Height of the result object
- * @return integer
- */
- protected function _calculateHeight()
- {
- return (int) $this->_withBorder * 2
- + $this->_calculateBarcodeHeight()
- + (int) $this->_withBorder * 2;
- }
- /**
- * Height of the barcode
- * @return integer
- */
- protected function _calculateBarcodeHeight()
- {
- $textHeight = 0;
- $extraHeight = 0;
- if ($this->_drawText) {
- $textHeight += $this->_fontSize;
- $extraHeight = 2;
- }
- return ($this->_barHeight + $textHeight) * $this->_factor + $extraHeight;
- }
- /**
- * Get height of the result object
- * @return integer
- */
- public function getHeight($recalculate = false)
- {
- if ($this->_height === null || $recalculate) {
- $this->_height =
- abs($this->_calculateHeight() * cos($this->_orientation / 180 * pi()))
- + abs($this->_calculateWidth() * sin($this->_orientation / 180 * pi()));
- }
- return $this->_height;
- }
- /**
- * Get width of the result object
- * @return integer
- */
- public function getWidth($recalculate = false)
- {
- if ($this->_width === null || $recalculate) {
- $this->_width =
- abs($this->_calculateWidth() * cos($this->_orientation / 180 * pi()))
- + abs($this->_calculateHeight() * sin($this->_orientation / 180 * pi()));
- }
- return $this->_width;
- }
- /**
- * Calculate the offset from the left of the object
- * if an orientation is activated
- * @param boolean $recalculate
- * @return float
- */
- public function getOffsetLeft($recalculate = false)
- {
- if ($this->_offsetLeft === null || $recalculate) {
- $this->_offsetLeft = - min(array(
- 0 * cos(
- $this->_orientation / 180 * pi()) - 0 * sin(
- $this->_orientation / 180 * pi()),
- 0 * cos(
- $this->_orientation / 180 * pi()) - $this->_calculateBarcodeHeight() * sin(
- $this->_orientation / 180 * pi()),
- $this->_calculateBarcodeWidth() * cos(
- $this->_orientation / 180 * pi()) - $this->_calculateBarcodeHeight() * sin(
- $this->_orientation / 180 * pi()),
- $this->_calculateBarcodeWidth() * cos(
- $this->_orientation / 180 * pi()) - 0 * sin(
- $this->_orientation / 180 * pi()),
- ));
- }
- return $this->_offsetLeft;
- }
- /**
- * Calculate the offset from the top of the object
- * if an orientation is activated
- * @param boolean $recalculate
- * @return float
- */
- public function getOffsetTop($recalculate = false)
- {
- if ($this->_offsetTop === null || $recalculate) {
- $this->_offsetTop = - min(array(
- 0 * cos(
- $this->_orientation / 180 * pi()) + 0 * sin(
- $this->_orientation / 180 * pi()),
- $this->_calculateBarcodeHeight() * cos(
- $this->_orientation / 180 * pi()) + 0 * sin(
- $this->_orientation / 180 * pi()),
- $this->_calculateBarcodeHeight() * cos(
- $this->_orientation / 180 * pi()) + $this->_calculateBarcodeWidth() * sin(
- $this->_orientation / 180 * pi()),
- 0 * cos(
- $this->_orientation / 180 * pi()) + $this->_calculateBarcodeWidth() * sin(
- $this->_orientation / 180 * pi()),
- ));
- }
- return $this->_offsetTop;
- }
- /**
- * Apply rotation on a point in X/Y dimensions
- * @param float $x1 x-position before rotation
- * @param float $y1 y-position before rotation
- * @return array Array of two elements corresponding to the new XY point
- */
- protected function _rotate($x1, $y1)
- {
- $x2 = $x1 * cos($this->_orientation / 180 * pi())
- - $y1 * sin($this->_orientation / 180 * pi())
- + $this->getOffsetLeft();
- $y2 = $y1 * cos($this->_orientation / 180 * pi())
- + $x1 * sin($this->_orientation / 180 * pi())
- + $this->getOffsetTop();
- return array(intval($x2) , intval($y2));
- }
- /**
- * Complete drawing of the barcode
- * @return array Table of instructions
- */
- public function draw()
- {
- $this->checkParams();
- $this->_drawBarcode();
- $this->_drawBorder();
- $this->_drawText();
- return $this->getInstructions();
- }
- /**
- * Draw the barcode
- * @return void
- */
- protected function _drawBarcode()
- {
- $barcodeTable = $this->_prepareBarcode();
- $this->_preDrawBarcode();
- $xpos = (int) $this->_withBorder;
- $ypos = (int) $this->_withBorder;
- $point1 = $this->_rotate(0, 0);
- $point2 = $this->_rotate(0, $this->_calculateHeight() - 1);
- $point3 = $this->_rotate(
- $this->_calculateWidth() - 1,
- $this->_calculateHeight() - 1
- );
- $point4 = $this->_rotate($this->_calculateWidth() - 1, 0);
- $this->_addPolygon(array(
- $point1,
- $point2,
- $point3,
- $point4
- ), $this->_backgroundColor);
- $xpos += $this->getQuietZone();
- $barLength = $this->_barHeight * $this->_factor;
- foreach ($barcodeTable as $bar) {
- $width = $bar[1] * $this->_factor;
- if ($bar[0]) {
- $point1 = $this->_rotate($xpos, $ypos + $bar[2] * $barLength);
- $point2 = $this->_rotate($xpos, $ypos + $bar[3] * $barLength);
- $point3 = $this->_rotate(
- $xpos + $width - 1,
- $ypos + $bar[3] * $barLength
- );
- $point4 = $this->_rotate(
- $xpos + $width - 1,
- $ypos + $bar[2] * $barLength
- );
- $this->_addPolygon(array(
- $point1,
- $point2,
- $point3,
- $point4,
- ));
- }
- $xpos += $width;
- }
- $this->_postDrawBarcode();
- }
- /**
- * Partial function to draw border
- * @return void
- */
- protected function _drawBorder()
- {
- if ($this->_withBorder) {
- $point1 = $this->_rotate(0, 0);
- $point2 = $this->_rotate($this->_calculateWidth() - 1, 0);
- $point3 = $this->_rotate(
- $this->_calculateWidth() - 1,
- $this->_calculateHeight() - 1
- );
- $point4 = $this->_rotate(0, $this->_calculateHeight() - 1);
- $this->_addPolygon(array(
- $point1,
- $point2,
- $point3,
- $point4,
- $point1,
- ), $this->_foreColor, false);
- }
- }
- /**
- * Partial function to draw text
- * @return void
- */
- protected function _drawText()
- {
- if ($this->_drawText) {
- $text = $this->getTextToDisplay();
- if ($this->_stretchText) {
- $textLength = strlen($text);
- $space = ($this->_calculateWidth() - 2 * $this->getQuietZone()) / $textLength;
- for ($i = 0; $i < $textLength; $i ++) {
- $leftPosition = $this->getQuietZone() + $space * ($i + 0.5);
- $this->_addText(
- $text{$i},
- $this->_fontSize * $this->_factor,
- $this->_rotate(
- $leftPosition,
- (int) $this->_withBorder * 2
- + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
- ),
- $this->_font,
- $this->_foreColor,
- 'center',
- - $this->_orientation
- );
- }
- } else {
- $this->_addText(
- $text,
- $this->_fontSize * $this->_factor,
- $this->_rotate(
- $this->_calculateWidth() / 2,
- (int) $this->_withBorder * 2
- + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
- ),
- $this->_font,
- $this->_foreColor,
- 'center',
- - $this->_orientation
- );
- }
- }
- }
- /**
- * Each child must verify allowed characters
- *
- * @return void
- */
- abstract public function validateText($value);
- protected function _validateText($value, $options = array())
- {
- $validatorName = (isset($options['validator'])) ? $options['validator'] : $this->getType();
- $validator = new Zend_Validate_Barcode(array(
- 'adapter' => $validatorName,
- 'checksum' => false,
- ));
- $checksumCharacter = (isset($options['substituteChecksumCharacter'])) ? $options['substituteChecksumCharacter'] : '';
- if (isset($options['automaticPrepend'])) {
- if (is_int($this->_barcodeLength)) {
- if (strlen($value) < ($this->_barcodeLength)) {
- $length = $this->_barcodeLength - strlen($checksumCharacter);
- $value = str_repeat($options['automaticPrepend'], $length - strlen($value)) . $value;
- }
- } else {
- if ($this->_barcodeLength == 'even') {
- $value = (strlen($value) % 2 ? $options['automaticPrepend'] . $value : $value);
- }
- }
- }
- if (!$validator->isValid($value . $checksumCharacter)) {
- $message = implode("\n", $validator->getMessages());
- /**
- * @see Zend_Barcode_Object_Exception
- */
- require_once 'Zend/Barcode/Object/Exception.php';
- throw new Zend_Barcode_Object_Exception($message);
- }
- }
- /**
- * Each child must prepare the barcode and return
- * a table like array(
- * 0 => array(
- * 0 => int (visible(black) or not(white))
- * 1 => int (width of the bar)
- * 2 => float (0->1 position from the top of the beginning of the bar in %)
- * 3 => float (0->1 position from the top of the end of the bar in %)
- * ),
- * 1 => ...
- * )
- *
- * @return array
- */
- abstract protected function _prepareBarcode();
- /**
- * Checking of parameters after all settings
- *
- * @return void
- */
- abstract protected function _checkParams();
- /**
- * Allow each child to draw something else
- *
- * @return void
- */
- protected function _preDrawBarcode()
- {
- }
- /**
- * Allow each child to draw something else
- * (ex: bearer bars in interleaved 2 of 5 code)
- *
- * @return void
- */
- protected function _postDrawBarcode()
- {
- }
- }
|