Test Failed
Push — master ( 0ba858...da6932 )
by Francimar
05:42
created

Profile::getBitmapCmd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Thermal\Profile;
4
5
use Thermal\Printer;
6
use Thermal\Buffer\Encoding;
7
use Thermal\Connection\Connection;
8
9
abstract class Profile
10
{
11
    private $columns;
12
    protected $capabilities;
13
    private $connection;
14
    private $font;
15
    private $default_font;
16
    private $encoding;
17
18 40
    public function __construct($capabilities)
19
    {
20 40
        $this->capabilities = $capabilities;
21 40
        $this->columns = $this->getDefaultColumns();
22 40
        $this->default_font = $this->findDefaultFont();
23 39
        $this->font = $this->getDefaultFont();
24 39
        $this->connection = null;
25 39
        $this->encoding = new Encoding($this->getDefaultCodePage());
26 39
    }
27
28 13
    public function getName()
29
    {
30 13
        $name = isset($this->capabilities['name']) ? $this->capabilities['name'] : $this->capabilities['model'];
31 13
        return $this->capabilities['brand'] . ' ' . $name;
32
    }
33
34 17
    public function getEncoding()
35
    {
36 17
        return $this->encoding;
37
    }
38
39 40
    public function getDefaultColumns()
40
    {
41 40
        return $this->capabilities['columns'];
42
    }
43
44 4
    public function getColumns()
45
    {
46 4
        return $this->columns;
47
    }
48
49 6
    public function setColumns($columns)
50
    {
51 6
        $font = ['name' => 'Unknow'];
52
        // search for font with more columns
53
        // font list must be sorted by their number of columns
54 6
        foreach ($this->capabilities['fonts'] as $font) {
55 6
            if ($columns <= $font['columns']) {
56 6
                break;
57
            }
58
        }
59 6
        $this->setFont($font);
60 6
        $this->columns = $columns;
61 6
        return $this;
62
    }
63
64 39
    public function getDefaultFont()
65
    {
66 39
        return $this->default_font;
67
    }
68
69 40
    private function findDefaultFont()
70
    {
71 40
        foreach ($this->capabilities['fonts'] as $font) {
72 40
            if ($this->getDefaultColumns() == $font['columns']) {
73 40
                return $font;
74
            }
75
        }
76 2
        throw new \Exception(
77 2
            sprintf(
78 2
                'Default font with %d columns not found for printer "%s"',
79 2
                $this->getDefaultColumns(),
80 2
                $this->getName()
81
            ),
82 2
            404
83
        );
84
    }
85
86 3
    public function getCodePages()
87
    {
88 3
        return array_keys($this->capabilities['codepages']);
89
    }
90
91 39
    public function getDefaultCodePage()
92
    {
93 39
        return $this->capabilities['codepage'];
94
    }
95
96 13
    protected function checkCodePage($codepage)
97
    {
98 13
        if (!isset($this->capabilities['codepages'][$codepage])) {
99 1
            throw new \Exception(
100 1
                sprintf(
101 1
                    'Codepage "%s" not supported for printer "%s"',
102 1
                    $codepage,
103 1
                    $this->getName()
104
                ),
105 1
                401
106
            );
107
        }
108 13
        return $this;
109
    }
110
111 13
    public function setCodePage($codepage)
112
    {
113 13
        $this->getEncoding()->setCodePage($codepage);
114 13
        $this->checkCodePage($codepage);
115 13
        $this->getConnection()->write($this->capabilities['codepages'][$codepage]);
116 13
    }
117
118 5
    public function getFont()
119
    {
120 5
        return $this->font;
121
    }
122
123 8
    public function setFont($font)
124
    {
125 8
        $found = false;
126 8
        $_font = ['name' => 'Unknow'];
127 8
        foreach ($this->capabilities['fonts'] as $_font) {
128 8
            if ($_font['name'] == $font['name']) {
129 6
                $found = true;
130 8
                break;
131
            }
132
        }
133 8
        if (!$found) {
134 2
            throw new \Exception(
135 2
                sprintf(
136 2
                    'Font "%s" not found for printer "%s"',
137 2
                    $font['name'],
138 2
                    $this->getName()
139
                ),
140 2
                404
141
            );
142
        }
143 6
        if ($this->font['name'] != $_font['name']) {
144 6
            $this->fontChanged($_font, $this->font['name']);
145 6
            $this->refresh();
146
        }
147 6
        $this->font = $_font;
148 6
        return $this;
149
    }
150
151 13
    protected function refresh()
152
    {
153
        // ensure current codepage
154 13
        $this->setCodePage($this->getEncoding()->getCodePage());
155 13
        return $this;
156
    }
157
158 5
    protected function fontChanged($new_font, $old_font)
159
    {
160 5
    }
161
162 27
    public function getConnection()
163
    {
164 27
        if ($this->connection instanceof Connection) {
165 25
            return $this->connection;
166
        }
167 2
        throw new \Exception('Connection must be set before priting', 500);
168
    }
169
170 30
    public function setConnection($connection)
171
    {
172 30
        $this->connection = $connection;
173 30
        return $this;
174
    }
175
176 11
    public function initialize()
177
    {
178 11
        if (isset($this->capabilities['initialize'])) {
179
            // ensure defaults
180 11
            $this->getConnection()->write($this->capabilities['initialize']);
181
        }
182 11
        $this->refresh();
183 11
    }
184
185 1
    public function finalize()
186
    {
187 1
    }
188
189 6
    public function write($text, $styles, $align)
190
    {
191 6
        if ($align !== null) {
192 3
            $this->setAlignment($align);
193
        }
194 6
        $this->setMode($styles, true);
195 6
        $this->setStyle(Printer::STYLE_CONDENSED & $styles, true);
196 6
        $this->setStyle(Printer::STYLE_BOLD & $styles, true);
197 6
        $this->setStyle(Printer::STYLE_ITALIC & $styles, true);
198 6
        $this->setStyle(Printer::STYLE_UNDERLINE & $styles, true);
199 6
        $this->getConnection()->write($this->getEncoding()->encode($text, 'UTF-8'));
200 6
        $this->setStyle(Printer::STYLE_UNDERLINE & $styles, false);
201 6
        $this->setStyle(Printer::STYLE_ITALIC & $styles, false);
202 6
        $this->setStyle(Printer::STYLE_BOLD & $styles, false);
203 6
        $this->setStyle(Printer::STYLE_CONDENSED & $styles, false);
204 6
        $this->setMode($styles, false);
205
        // reset align to left
206 6
        if ($align !== null && $align != Printer::ALIGN_LEFT) {
207 2
            $this->setAlignment(Printer::ALIGN_LEFT);
208
        }
209 6
        return $this;
210
    }
211
212 1
    protected function getBitmapCmd()
213
    {
214 1
        return "\e*!";
215
    }
216
217 2
    public function draw($image, $align)
218
    {
219 2
        if ($align !== null) {
220 2
            $this->setAlignment($align);
221
        }
222 2
        $width = $image->getWidth();
223 2
        $low = $width & 0xFF;
224 2
        $high = ($width >> 8) & 0xFF;
225 2
        $this->getConnection()->write("\e3\x10");
226 2
        for ($i=0; $i < $image->getLines(); $i++) {
227 2
            $data = $image->getLineData($i);
228 2
            $this->getConnection()->write($this->getBitmapCmd() . chr($low) . chr($high) . $data . "\eJ\x00");
229
        }
230 2
        $this->getConnection()->write("\e2");
231
        // reset align to left
232 2
        if ($align !== null && $align != Printer::ALIGN_LEFT) {
233 2
            $this->setAlignment(Printer::ALIGN_LEFT);
234
        }
235 2
        return $this;
236
    }
237
238
    abstract public function feed($lines);
239
    abstract public function cutter($mode);
240
    /**
241
     * @param int $number drawer id
242
     * @param int $on_time time in milliseconds that activate the drawer
243
     * @param int $off_time time in milliseconds that deactivate the drawer
244
     */
245
    abstract public function drawer($number, $on_time, $off_time);
246
247
    abstract protected function setAlignment($align);
248
    abstract protected function setMode($mode, $enable);
249
    abstract protected function setStyle($style, $enable);
250
}
251