/var/www/hkosl.com/oneMotorChunHing_delete20240201/html/html/bcclass/BCGcode39extended.barcode.php


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
<?php
/**
 * BCGcode39extended.barcode.php
 *--------------------------------------------------------------------
 *
 * Sub-Class - Code 39 Extended
 *
 *--------------------------------------------------------------------
 * Revision History
 * v2.0.0    23 apr    2008    Jean-Sébastien Goupil    New Version Update
 *--------------------------------------------------------------------
 * $Id: BCGcode39extended.barcode.php,v 1.3 2009/11/09 04:08:41 jsgoupil Exp $
 *--------------------------------------------------------------------
 * Copyright (C) Jean-Sebastien Goupil
 * http://www.barcodephp.com
 */
include_once('BCGcode39.barcode.php');

class 
BCGcode39extended extends BCGcode39 {
    const 
EXTENDED_1 39;
    const 
EXTENDED_2 40;
    const 
EXTENDED_3 41;
    const 
EXTENDED_4 42;

    protected 
$errorText;
    protected 
$indcheck$data;

    
/**
     * Constructor
     */
    
public function __construct() {
        
parent::__construct();

        
// We just put parenthesis around special characters.
        
$this->keys[self::EXTENDED_1] = '($)';
        
$this->keys[self::EXTENDED_2] = '(/)';
        
$this->keys[self::EXTENDED_3] = '(+)';
        
$this->keys[self::EXTENDED_4] = '(%)';

        
$this->errorText '';
    }

    
/**
     * Saves Text
     *
     * @param string $text
     */
    
public function parse($text) {
        
$this->text $text;

        
$data = array();
        
$indcheck = array();

        
$this->errorText ''// Reset Error
        
$c strlen($this->text);
        for(
$i 0$i $c$i++) {
            
$pos array_search($this->text[$i], $this->keys);
            if(
$pos === false) {
                
// Search in extended?
                
$extended $this->getExtendedVersion($this->text[$i]);
                if(
$extended === false) {
                    
$this->errorText .= 'Char \'' $this->text[$i] . '\' not allowed.'."\n";
                } 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));
    }

    
/**
     * Draws the barcode
     *
     * @param resource $im
     */
    
public function draw(&$im) {
        if(!empty(
$this->errorText)) {
            
$error explode("\n"trim($this->errorText));
            
$c count($error);
            for(
$i 0$i $c$i++) {
                
$this->drawError($im$error[$i]);
            }
        } else {
            
$c count($this->data);
            if(
$c === 0) {
                
$this->drawError($im'No text has been entered.');
            } else {
                
// Starting *
                
$this->drawChar($im$this->code[$this->starting], true);
                for(
$i 0$i $c$i++) {
                    
$this->drawChar($im$this->data[$i], true);
                }
                
// Checksum (rarely used)
                
if ($this->checksum === true) {
                    
$this->drawChar($im$this->code[$this->checksumValue 43], true);
                }
                
// Ending *
                
$this->drawChar($im$this->code[$this->ending], true);
                
$this->drawText($im);
            }
        }
    }

    
/**
     * Returns the maximal size of a barcode
     *
     * @return int[]
     */
    
public function getMaxSize() {
        
$p BCGBarcode1D::getMaxSize();

        
$textlength 13 count($this->data) * $this->scale;
        
$startlength 13 $this->scale;
        
$checksumlength 0;
        if (
$this->checksum === true) {
            
$checksumlength 13 $this->scale;
        }
        
$endlength 13 $this->scale;
        return array(
$p[0] + $startlength $textlength $checksumlength $endlength$p[1]);
    }

    
/**
     * Overloaded method to calculate checksum
     */
    
protected function calculateChecksum() {
        
$this->checksumValue 0;
        
$c count($this->indcheck);
        for (
$i 0$i $c$i++) {
            
$this->checksumValue += $this->indcheck[$i];
        }
        
$this->checksumValue $this->checksumValue 43;
    }

    
/**
     * Saves data into the classes.
     *
     * This method will save data, calculate real column number
     * (if -1 was selected), the real error level (if -1 was
     * selected)... It will add Padding to the end and generate
     * the error codes.
     *
     * @param array $data
     */
    
private function setData($data) {
        
$this->indcheck $data[0];
        
$this->data $data[1];
        
$this->calculateChecksum();
    }

    private function 
getExtendedVersion($char) {
        
$o ord($char);
        if(
$o === 0)
            return 
'%U';
        elseif(
$o >= && $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;
    }
};
?>