1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This is the part of Povils open-source library. |
5
|
|
|
* |
6
|
|
|
* @author Povilas Susinskas |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Povils\Figlet; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Figlet |
13
|
|
|
* |
14
|
|
|
* @package Povils\Figlet |
15
|
|
|
*/ |
16
|
|
|
class Figlet implements FigletInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Defines first ASCII character code (blank/space). |
20
|
|
|
*/ |
21
|
|
|
const FIRST_ASCII_CHARACTER = 32; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ColorManager |
25
|
|
|
*/ |
26
|
|
|
private $colorManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var FontManager |
30
|
|
|
*/ |
31
|
|
|
private $fontManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Font |
35
|
|
|
*/ |
36
|
|
|
private $font; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $backgroundColor; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $fontColor; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $fontName; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $fontDir; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var int |
60
|
|
|
*/ |
61
|
|
|
private $stretching; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* This array will hold used Figlet characters. |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
private $characters = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Figlet constructor. |
72
|
|
|
*/ |
73
|
|
|
public function __construct() |
74
|
|
|
{ |
75
|
|
|
$this->fontDir = __DIR__ . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR; |
76
|
|
|
$this->fontName = 'big'; |
77
|
|
|
$this->stretching = 0; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function write($text) |
84
|
|
|
{ |
85
|
|
|
echo $this->render($text); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function render($text) |
94
|
|
|
{ |
95
|
|
|
$this->font = $this->getFontManager()->loadFont($this->fontName, $this->fontDir); |
96
|
|
|
|
97
|
|
|
$figletText = $this->generateFigletText($text); |
98
|
|
|
|
99
|
|
|
if ($this->fontColor || $this->backgroundColor) { |
100
|
|
|
$figletText = $this->colorize($figletText); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $figletText; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function setBackgroundColor($color) |
110
|
|
|
{ |
111
|
|
|
$this->backgroundColor = $color; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function setFontColor($color) |
120
|
|
|
{ |
121
|
|
|
$this->fontColor = $color; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function setFont($fontName) |
130
|
|
|
{ |
131
|
|
|
$this->fontName = $fontName; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function setFontDir($fontDir) |
140
|
|
|
{ |
141
|
|
|
$this->fontDir = $fontDir; |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function setFontStretching($stretching) |
150
|
|
|
{ |
151
|
|
|
$this->stretching = $stretching; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Unset some arrays and objects. |
158
|
|
|
*/ |
159
|
|
|
public function clear() |
160
|
|
|
{ |
161
|
|
|
unset($this->characters); |
162
|
|
|
unset($this->font); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Generates Figlet text. |
167
|
|
|
* |
168
|
|
|
* @param string $text |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
private function generateFigletText($text) |
173
|
|
|
{ |
174
|
|
|
$figletCharacters = $this->getFigletCharacters($text); |
175
|
|
|
|
176
|
|
|
return $this->combineFigletCharacters($figletCharacters); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $text |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
|
|
private function getFigletCharacters($text) |
185
|
|
|
{ |
186
|
|
|
$figletCharacters = []; |
187
|
|
|
foreach (str_split($text) as $character) { |
188
|
|
|
$figletCharacters[] = $this->getFigletCharacter($character); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $figletCharacters; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $character |
196
|
|
|
* |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
private function getFigletCharacter($character) |
200
|
|
|
{ |
201
|
|
|
if (isset($this->characters[$this->fontName][$character])) { |
202
|
|
|
return $this->characters[$this->fontName][$character]; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$figletCharacter = []; |
206
|
|
|
|
207
|
|
|
$lines = $this->getFigletCharacterLines($character); |
208
|
|
|
|
209
|
|
|
foreach ($lines as $line) { |
210
|
|
|
$figletCharacter[] = str_replace( |
211
|
|
|
['@', $this->font->getHardBlank()], |
212
|
|
|
['', ' '], |
213
|
|
|
$line |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$this->characters[$this->fontName][$character] = $figletCharacter; |
218
|
|
|
|
219
|
|
|
return $figletCharacter; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param string $character |
224
|
|
|
* |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
private function getFigletCharacterLines($character) |
228
|
|
|
{ |
229
|
|
|
$letterStartPosition = $this->getLetterStartPosition($character); |
230
|
|
|
|
231
|
|
|
$lines = array_slice($this->font->getFileCollection(), $letterStartPosition, $this->font->getHeight()); |
232
|
|
|
|
233
|
|
|
return $lines; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Combines Figlet characters to one. |
238
|
|
|
* |
239
|
|
|
* @param array $figletCharacters |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
private function combineFigletCharacters($figletCharacters) |
244
|
|
|
{ |
245
|
|
|
$figletText = ''; |
246
|
|
|
|
247
|
|
|
$height = $this->font->getHeight(); |
248
|
|
|
for ($line = 0; $line < $height; $line++) { |
249
|
|
|
$singleLine = ''; |
250
|
|
|
foreach ($figletCharacters as $charactersLines) { |
251
|
|
|
$singleLine .= $charactersLines[$line] . $this->addStretching(); |
252
|
|
|
} |
253
|
|
|
$singleLine = $this->removeNewlines($singleLine); |
254
|
|
|
$figletText .= $singleLine . "\n"; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $figletText; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Colorize text. |
262
|
|
|
* |
263
|
|
|
* @param string $figletText |
264
|
|
|
* |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
|
|
private function colorize($figletText) |
268
|
|
|
{ |
269
|
|
|
$figletText = $this |
270
|
|
|
->getColorManager() |
271
|
|
|
->colorize( |
272
|
|
|
$figletText, |
273
|
|
|
$this->fontColor, |
274
|
|
|
$this->backgroundColor |
275
|
|
|
); |
276
|
|
|
|
277
|
|
|
return $figletText; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return ColorManager |
282
|
|
|
*/ |
283
|
|
|
private function getColorManager() |
284
|
|
|
{ |
285
|
|
|
if (null === $this->colorManager) { |
286
|
|
|
return $this->colorManager = new ColorManager(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $this->colorManager; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return FontManager |
294
|
|
|
*/ |
295
|
|
|
private function getFontManager() |
296
|
|
|
{ |
297
|
|
|
if (null === $this->fontManager) { |
298
|
|
|
return $this->fontManager = new FontManager(); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $this->fontManager; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Remove newlines characters. |
306
|
|
|
* |
307
|
|
|
* @param string $singleLine |
308
|
|
|
* |
309
|
|
|
* @return string |
310
|
|
|
*/ |
311
|
|
|
private function removeNewlines($singleLine) |
312
|
|
|
{ |
313
|
|
|
$singleLine = preg_replace('/[\\r\\n]*/', '', $singleLine); |
314
|
|
|
|
315
|
|
|
return $singleLine; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Adds space(s) in the end to Figlet character. |
320
|
|
|
* |
321
|
|
|
* @return string |
322
|
|
|
*/ |
323
|
|
|
private function addStretching() |
324
|
|
|
{ |
325
|
|
|
if (is_int($this->stretching) && 0 < $this->stretching) { |
326
|
|
|
$stretchingSpace = ' '; |
327
|
|
|
} else { |
328
|
|
|
$stretchingSpace = ''; |
329
|
|
|
$this->stretching = 0; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
return str_repeat($stretchingSpace, $this->stretching); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param string $character |
337
|
|
|
* |
338
|
|
|
* @return int |
339
|
|
|
*/ |
340
|
|
|
private function getLetterStartPosition($character) |
341
|
|
|
{ |
342
|
|
|
return ((ord($character) - self::FIRST_ASCII_CHARACTER) * $this->font->getHeight()) + 1 + $this->font->getCommentLines(); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|