Completed
Push — master ( 5cb337...1dfd52 )
by Roberto
03:40
created

Bematech::setExpanded()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
cc 3
eloc 8
nc 4
nop 1
crap 12
1
<?php
2
3
namespace Posprint\Printers;
4
5
/**
6
 * Bematech class for POS printer
7
 * Model: MP 4200TH
8
 *
9
 * @category   NFePHP
10
 * @package    Posprint
11
 * @copyright  Copyright (c) 2016
12
 * @license    http://www.gnu.org/licenses/lesser.html LGPL v3
13
 * @author     Roberto L. Machado <linux.rlm at gmail dot com>
14
 * @link       http://github.com/nfephp-org/posprint for the canonical source repository
15
 */
16
17
use Posprint\Printers\DefaultPrinter;
18
use Posprint\Printers\PrinterInterface;
19
20
final class Bematech extends DefaultPrinter implements PrinterInterface
21
{
22
    
23
    /**
24
     * List all available code pages.
25
     *
26
     * @var array
27
     */
28
    protected $aCodePage = array(
29
        'CP437' => array('conv' => '437', 'table' => '3', 'desc' => 'PC437: USA, Standard Europe'),
30
        'CP850' => array('conv' => '850', 'table' => '2', 'desc' => 'PC850: Multilingual'),
31
        'CP858' => array('conv' => '858', 'table' => '5', 'desc' => 'PC858: Multilingual'),
32
        'CP860' => array('conv' => '860', 'table' => '4', 'desc' => 'PC860: Portuguese'),
33
        'CP864' => array('conv' => '864', 'table' => '7', 'desc' => 'PC864: Arabic'),
34
        'CP866' => array('conv' => '866', 'table' => '6', 'desc' => 'PC866: Cyrillic'),
35
        'UTF8'  => array('conv' => 'UTF8', 'table' => '8', 'desc' => 'UTF-8: Unicode')
36
    );
37
    
38
    /**
39
     * List all avaiable fonts
40
     *
41
     * @var array
42
     */
43
    protected $aFont = array(0 => 'C', 1 => 'D');
44
45
    /**
46
     * initialize printer
47
     * Clears the data in the print buffer and resets the printer modes to
48
     * the modes that were in effect when the power was turned on.
49
     */
50
    public function initialize()
51
    {
52
        parent::initialize();
53
        $this->setPrintMode('ESCPOS');
54
        $this->setCodePage('CP850');
55
        $this->setRegionPage('LATIN');
56
        //clear Emphasized, Double height, Double width and select font C
57
        $this->defaultFont('C');
58
        $this->buffer->write(self::ESC . '!' . chr(1));
59
    }
60
    
61
    /**
62
     * Select printer mode
63
     *
64
     * @param string $mode
65
     */
66
    public function setPrintMode($mode = 'ESCPOS')
67
    {
68
        //default ESC/POS
69
        $nmode = 0;
70
        if ($mode == 'ESCBEMA') {
71
            $this->printerMode = 'ESCBEMA';
72
            $nmode = 1;
73
        }
74
        //select mode ESCPOS or ESCBEMA
75
        $this->buffer->write(self::GS . chr(249) . chr(32) . $nmode);
76
    }
77
    
78
    /**
79
     * Set expanded mode.
80
     *
81
     * @param int $size qualquer valor ativa e null desativa
82
     */
83
    public function setExpanded($size = null)
84
    {
85
        $mode = array_keys($this->aFont, $this->font, true);
86
        if ($this->boldMode) {
87
                $mode += 8;
88
        }
89
        if (! isnull($size)) {
90
            $mode = array_keys($this->aFont, $this->font, true);
91
            //double width and double height
92
            $mode += (16+32);
93
        }
94
        $this->buffer->write(self::ESC . '!' . $mode);
95
    }
96
    
97
    /**
98
     * Set condensed mode.
99
     * Will change Font do D 
100
     */
101
    public function setCondensed()
102
    {
103
        $this->setExpanded();
104
        $this->setFont('D');
105
    }
106
    
107
    /**
108
     * Imprime o QR Code
109
     *
110
     * @param string $data   Dados a serem inseridos no QRCode
111
     * @param string $level  Nivel de correção L,M,Q ou H
112
     * @param int    $modelo modelo de QRCode 0 QRCode ou 1 microQR
113
     * @param int    $wmod   largura da barra 3 ~ 16
114
     */
115
    public function barcodeQRCode($data = '', $level = 'M', $modelo = 0, $wmod = 4)
116
    {
117
        $aModels = array();
118
        //essa matriz especifica o numero máximo de caracteres alfanumericos que o
119
        //modelo de QRCode suporta dependendo no nivel de correção.
120
        //Cada matriz representa um nivel de correção e cada uma das 40 posições nessas
121
        //matrizes indicam o numero do modelo do QRCode e o numero máximo de caracteres
122
        //alfamunéricos suportados
123
        //Quanto maior o nivel de correção menor é a quantidade de caracteres suportada
124
        $aModels[0]=[25,47,77,114,154,195,224,279,335,395,468,535,619,667,
125
            758,854,938,1046,1153,1249,1352,1460,1588,1704,1853,1990,2132,
126
            2223,2369,2520,2677,2840,3009,3183,3351,3537,3729,3927,4087,4296];
127
        $aModels[1]=[20,38,61,90,122,154,178,221,262,311,366,419,483,528,600,
128
            656,734,816,909,970,1035,1134,1248,1326,1451,1542,1637,1732,1839,
129
            1994,2113,2238,2369,2506,2632,2780,2894,3054,3220,3391];
130
        $aModels[2]=[16,29,47,67,87,108,125,157,189,221,259,296,352,376,426,
131
            470,531,574,644,702,742,823,890,963,1041,1094,1172,1263,1322,1429,
132
            1499,1618,1700,1787,1867,1966,2071,2181,2298,2420];
133
        $aModels[3]=[10,20,35,50,64,84,93,122,143,174,200,227,259,283,321,365,
134
            408,452,493,557,587,640,672,744,779,864,910,958,1016,1080,1150,1226,
135
            1307,1394,1431,1530,1591,1658,1774,1852];
136
        //n1 Error correction level (data restoration)
137
        switch ($level) {
138
            case 'L':
139
                $n1 = 0;
140
                break;
141
            case "M":
142
                $n1 = 1;
143
                break;
144
            case "Q":
145
                $n1 = 2;
146
                break;
147
            case "H":
148
                $n1 = 3;
149
                break;
150
            default:
151
                $n1 = 0;
152
        }
153
        if ($modelo != 0 && $modelo != 1) {
154
            $modelo = 0;
155
        }
156
        //se for mucroQR sua capacidade é bem reduzida
157
        if ($modelo == 1) {
158
            $aModels[0] = [6,14,21];
159
            $aModels[1] = [5,11,18];
160
            $aModels[2] = [0,0,13];
161
            $aModels[3] = [0,0,0];
162
        }
163
        //n2 Module/cell size in pixels MSB 1 ≤ module size ≤ 127 LSB 0 QR or 1 MicroQR
164
        $n2 = $wmod << 1;//shift 1 é o mesmo que multiplicar por 2
165
        $n2 += $modelo;//seleciona QRCode ou microQR
166
        //comprimento da mensagem
167
        $length = strlen($data);
168
        //seleciona matriz de modelos aplicavel pelo nivel de correção
169
        $am = $aModels[$n1];
170
        $i = 0;
171
        $flag = false;
172
        foreach ($am as $size) {
173
            //verifica se o tamanho maximo é maior ou igual ao comprimento da mensagem
174
            if ($size >= $length) {
175
                $flag = true;
176
                break;
177
            }
178
            $i++;
179
        }
180
        if (! $flag) {
181
            throw new InvalidArgumentException(
182
                'O numero de caracteres da mensagem é maior que a capacidade do QRCode'
183
            );
184
        }
185
        //n3 Version QRCode
186
        //depende do comprimento dos dados e do nivel de correção
187
        $n3 = ($i + 1);
188
        //n4 Encoding modes
189
        //0 – Numeric only              Max. 7,089 characters
190
        //1 – Alphanumeric              Max. 4,296 characters
191
        //2 – Binary (8 bits)           Max. 2,953 bytes
192
        //3 – Kanji, full-width Kana    Max. 1,817 characters
193
        $n4 = 1;//sempre será 1 apenas caracteres alfanumericos nesse caso
194
        //n5 e n6 Indicate the number of bytes that will be coded, where total = n5 + n6 x 256,
195
        //and total must be less than 7089.
196
        $n6 = intval($length / 256);
197
        $n5 = ($length % 256);
198
        $this->buffer->write(self::GS."kQ" . chr($n1) . chr($n2) . chr($n3) . chr($n4) . chr($n5) . chr($n6) . $data);
199
    }
200
}
201