1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
|
<?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-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: ObjectAbstract.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* Class for generate Barcode
*
* @category Zend
* @package Zend_Barcode
* @copyright Copyright (c) 2005-2010 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;
/**
* Fix barcode length (numeric or string like 'even')
* @var $_barcodeLength integer | string
*/
protected $_barcodeLength = null;
/**
* Activate automatic addition of leading zeros
* if barcode length is fixed
* @var $_addLeadingZeros boolean
*/
protected $_addLeadingZeros = true;
/**
* Activation of mandatory checksum
* to deactivate unauthorized modification
* @var $_mandatoryChecksum boolean
*/
protected $_mandatoryChecksum = false;
/**
* Character used to substitute checksum character for validation
* @var $_substituteChecksumCharacter mixed
*/
protected $_substituteChecksumCharacter = 0;
/**
* 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)
{
$this->_getDefaultOptions();
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));
if ($this->_mandatoryChecksum) {
$this->_withChecksum = true;
$this->_withChecksumInText = true;
}
}
/**
* Set default options for particular object
* @return void
*/
protected function _getDefaultOptions()
{
}
/**
* 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()
{
$text = $this->_text;
if ($this->_withChecksum) {
$text .= $this->getChecksum($this->_text);
}
return $this->_addLeadingZeros($text);
}
/**
* Automatically add leading zeros if barcode length is fixed
* @param string $text
* @param boolean $withoutChecksum
*/
protected function _addLeadingZeros($text, $withoutChecksum = false)
{
if ($this->_barcodeLength && $this->_addLeadingZeros) {
$omitChecksum = (int) ($this->_withChecksum && $withoutChecksum);
if (is_int($this->_barcodeLength)) {
$length = $this->_barcodeLength - $omitChecksum;
if (strlen($text) < $length) {
$text = str_repeat('0', $length - strlen($text)) . $text;
}
} else {
if ($this->_barcodeLength == 'even') {
$text = ((strlen($text) - $omitChecksum) % 2 ? '0' . $text : $text);
}
}
}
return $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->getText();
} else {
return $this->_addLeadingZeros($this->_text, true);
}
}
/**
* 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)
{
if (!$this->_mandatoryChecksum) {
$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)
{
if (!$this->_mandatoryChecksum) {
$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
);
}
}
}
/**
* Check for invalid characters
* @param string $value Text to be ckecked
* @return void
*/
public function validateText($value)
{
$this->_validateText($value);
}
/**
* Standard validation for most of barcode objects
* @param string $value
* @param array $options
*/
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 = '';
$withChecksum = false;
if ($this->_mandatoryChecksum) {
$checksumCharacter = $this->_substituteChecksumCharacter;
$withChecksum = true;
}
$value = $this->_addLeadingZeros($value, $withChecksum) . $checksumCharacter;
if (!$validator->isValid($value)) {
$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()
{
}
}
|