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