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