123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
-
- include_once('BCGParseException.php');
- include_once('BCGBarcode1D.php');
-
- class BCGcode93 extends BCGBarcode1D {
- const EXTENDED_1 = 43;
- const EXTENDED_2 = 44;
- const EXTENDED_3 = 45;
- const EXTENDED_4 = 46;
-
- private $starting, $ending;
- private $indcheck, $data;
-
-
-
- public function __construct() {
- parent::__construct();
-
- $this->starting = $this->ending = 47;
- $this->keys = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%', '($)', '(%)', '(/)', '(+)', '(*)');
- $this->code = array(
- '020001',
- '000102',
- '000201',
- '000300',
- '010002',
- '010101',
- '010200',
- '000003',
- '020100',
- '030000',
- '100002',
- '100101',
- '100200',
- '110001',
- '110100',
- '120000',
- '001002',
- '001101',
- '001200',
- '011001',
- '021000',
- '000012',
- '000111',
- '000210',
- '010011',
- '020010',
- '101001',
- '101100',
- '100011',
- '100110',
- '110010',
- '111000',
- '001011',
- '001110',
- '011010',
- '012000',
- '010020',
- '200001',
- '200100',
- '210000',
- '001020',
- '002010',
- '100020',
- '010110',
- '201000',
- '200010',
- '011100',
- '000030'
- );
- }
-
-
-
- public function parse($text) {
- $this->text = $text;
-
- $data = array();
- $indcheck = array();
-
- $c = strlen($this->text);
- for ($i = 0; $i < $c; $i++) {
- $pos = array_search($this->text[$i], $this->keys);
- if ($pos === false) {
-
- $extended = self::getExtendedVersion($this->text[$i]);
- if ($extended === false) {
- throw new BCGParseException('code93', 'The character \'' . $this->text[$i] . '\' is not allowed.');
- } else {
- $extc = strlen($extended);
- for ($j = 0; $j < $extc; $j++) {
- $v = $extended[$j];
- if ($v === '$') {
- $indcheck[] = self::EXTENDED_1;
- $data[] = $this->code[self::EXTENDED_1];
- } elseif ($v === '%') {
- $indcheck[] = self::EXTENDED_2;
- $data[] = $this->code[self::EXTENDED_2];
- } elseif ($v === '/') {
- $indcheck[] = self::EXTENDED_3;
- $data[] = $this->code[self::EXTENDED_3];
- } elseif ($v === '+') {
- $indcheck[] = self::EXTENDED_4;
- $data[] = $this->code[self::EXTENDED_4];
- } else {
- $pos2 = array_search($v, $this->keys);
- $indcheck[] = $pos2;
- $data[] = $this->code[$pos2];
- }
- }
- }
- } else {
- $indcheck[] = $pos;
- $data[] = $this->code[$pos];
- }
- }
-
- $this->setData(array($indcheck, $data));
- $this->addDefaultLabel();
- }
-
-
-
- public function draw($im) {
-
- $this->drawChar($im, $this->code[$this->starting], true);
- $c = count($this->data);
- for ($i = 0; $i < $c; $i++) {
- $this->drawChar($im, $this->data[$i], true);
- }
-
-
- $c = count($this->checksumValue);
- for ($i = 0; $i < $c; $i++) {
- $this->drawChar($im, $this->code[$this->checksumValue[$i]], true);
- }
-
-
- $this->drawChar($im, $this->code[$this->ending], true);
-
-
- $this->drawChar($im, '0', true);
- $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
- }
-
-
-
- public function getDimension($w, $h) {
- $startlength = 9;
- $textlength = 9 * count($this->data);
- $checksumlength = 2 * 9;
- $endlength = 9 + 1;
-
- $w += $startlength + $textlength + $checksumlength + $endlength;
- $h += $this->thickness;
- return parent::getDimension($w, $h);
- }
-
-
-
- protected function validate() {
- $c = count($this->data);
- if ($c === 0) {
- throw new BCGParseException('code93', 'No data has been entered.');
- }
-
- parent::validate();
- }
-
-
-
- protected function calculateChecksum() {
-
-
-
-
-
-
-
-
-
-
- $sequence_multiplier = array(20, 15);
- $this->checksumValue = array();
- $indcheck = $this->indcheck;
- for ($z = 0; $z < 2; $z++) {
- $checksum = 0;
- for ($i = count($indcheck), $j = 0; $i > 0; $i--, $j++) {
- $multiplier = $i % $sequence_multiplier[$z];
- if ($multiplier === 0) {
- $multiplier = $sequence_multiplier[$z];
- }
-
- $checksum += $indcheck[$j] * $multiplier;
- }
-
- $this->checksumValue[$z] = $checksum % 47;
- $indcheck[] = $this->checksumValue[$z];
- }
- }
-
-
-
- protected function processChecksum() {
- if ($this->checksumValue === false) {
- $this->calculateChecksum();
- }
-
- if ($this->checksumValue !== false) {
- $ret = '';
- $c = count($this->checksumValue);
- for ($i = 0; $i < $c; $i++) {
- $ret .= $this->keys[$this->checksumValue[$i]];
- }
-
- return $ret;
- }
-
- return false;
- }
-
-
-
- private function setData($data) {
- $this->indcheck = $data[0];
- $this->data = $data[1];
- $this->calculateChecksum();
- }
-
-
-
- private static function getExtendedVersion($char) {
- $o = ord($char);
- if ($o === 0) {
- return '%U';
- } elseif ($o >= 1 && $o <= 26) {
- return '$' . chr($o + 64);
- } elseif (($o >= 33 && $o <= 44) || $o === 47 || $o === 48) {
- return '/' . chr($o + 32);
- } elseif ($o >= 97 && $o <= 122) {
- return '+' . chr($o - 32);
- } elseif ($o >= 27 && $o <= 31) {
- return '%' . chr($o + 38);
- } elseif ($o >= 59 && $o <= 63) {
- return '%' . chr($o + 11);
- } elseif ($o >= 91 && $o <= 95) {
- return '%' . chr($o - 16);
- } elseif ($o >= 123 && $o <= 127) {
- return '%' . chr($o - 43);
- } elseif ($o === 64) {
- return '%V';
- } elseif ($o === 96) {
- return '%W';
- } elseif ($o > 127) {
- return false;
- } else {
- return $char;
- }
- }
- }
- ?>
|