123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
-
- include_once('BCGArgumentException.php');
- include_once('BCGBarcode.php');
- include_once('BCGFontPhp.php');
- include_once('BCGFontFile.php');
- include_once('BCGLabel.php');
-
- abstract class BCGBarcode1D extends BCGBarcode {
- const SIZE_SPACING_FONT = 5;
-
- const AUTO_LABEL = '##!!AUTO_LABEL!!##';
-
- protected $thickness;
- protected $keys, $code;
- protected $positionX;
- protected $font;
- protected $text;
- protected $checksumValue;
- protected $displayChecksum;
- protected $label;
- protected $defaultLabel;
-
-
-
- protected function __construct() {
- parent::__construct();
-
- $this->setThickness(30);
-
- $this->defaultLabel = new BCGLabel();
- $this->defaultLabel->setPosition(BCGLabel::POSITION_BOTTOM);
- $this->setLabel(self::AUTO_LABEL);
- $this->setFont(new BCGFontPhp(5));
-
- $this->text = '';
- $this->checksumValue = false;
- $this->positionX = 0;
- }
-
-
-
- public function getThickness() {
- return $this->thickness;
- }
-
-
-
- public function setThickness($thickness) {
- $thickness = intval($thickness);
- if ($thickness <= 0) {
- throw new BCGArgumentException('The thickness must be larger than 0.', 'thickness');
- }
-
- $this->thickness = $thickness;
- }
-
-
-
- public function getLabel() {
- $label = $this->label;
- if ($this->label === self::AUTO_LABEL) {
- $label = $this->text;
- if ($this->displayChecksum === true && ($checksum = $this->processChecksum()) !== false) {
- $label .= $checksum;
- }
- }
-
- return $label;
- }
-
-
-
- public function setLabel($label) {
- $this->label = $label;
- }
-
-
-
- public function getFont() {
- return $this->font;
- }
-
-
-
- public function setFont($font) {
- if (is_int($font)) {
- if ($font === 0) {
- $font = null;
- } else {
- $font = new BCGFontPhp($font);
- }
- }
-
- $this->font = $font;
- }
-
-
-
- public function parse($text) {
- $this->text = $text;
- $this->checksumValue = false;
- $this->validate();
-
- parent::parse($text);
-
- $this->addDefaultLabel();
- }
-
-
-
- public function getChecksum() {
- return $this->processChecksum();
- }
-
-
-
- public function setDisplayChecksum($displayChecksum) {
- $this->displayChecksum = (bool)$displayChecksum;
- }
-
-
-
- protected function addDefaultLabel() {
- $label = $this->getLabel();
- $font = $this->font;
- if ($label !== null && $label !== '' && $font !== null && $this->defaultLabel !== null) {
- $this->defaultLabel->setText($label);
- $this->defaultLabel->setFont($font);
- $this->addLabel($this->defaultLabel);
- }
- }
-
-
-
- protected function validate() {
-
- }
-
-
-
- protected function findIndex($var) {
- return array_search($var, $this->keys);
- }
-
-
-
- protected function findCode($var) {
- return $this->code[$this->findIndex($var)];
- }
-
-
-
- protected function drawChar($im, $code, $startBar = true) {
- $colors = array(BCGBarcode::COLOR_FG, BCGBarcode::COLOR_BG);
- $currentColor = $startBar ? 0 : 1;
- $c = strlen($code);
- for ($i = 0; $i < $c; $i++) {
- for ($j = 0; $j < intval($code[$i]) + 1; $j++) {
- $this->drawSingleBar($im, $colors[$currentColor]);
- $this->nextX();
- }
-
- $currentColor = ($currentColor + 1) % 2;
- }
- }
-
-
-
- protected function drawSingleBar($im, $color) {
- $this->drawFilledRectangle($im, $this->positionX, 0, $this->positionX, $this->thickness - 1, $color);
- }
-
-
-
- protected function nextX() {
- $this->positionX++;
- }
-
-
-
- protected function calculateChecksum() {
- $this->checksumValue = false;
- }
-
-
-
- protected function processChecksum() {
- return false;
- }
- }
- ?>
|