|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kasifi\Gitaski; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
class AsciiHelper |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @param array $symbol |
|
11
|
|
|
* |
|
12
|
|
|
* @return string |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function renderSymbol($symbol) |
|
15
|
|
|
{ |
|
16
|
|
|
$output = ''; |
|
17
|
|
|
foreach ($symbol as $row) { |
|
18
|
|
|
foreach ($row as $cell) { |
|
19
|
|
|
switch ($cell) { |
|
20
|
|
|
case 0: |
|
21
|
|
|
$output .= " "; |
|
22
|
|
|
break; |
|
23
|
|
|
case 1: |
|
24
|
|
|
$output .= "░"; |
|
25
|
|
|
break; |
|
26
|
|
|
case 2: |
|
27
|
|
|
$output .= "▒"; |
|
28
|
|
|
break; |
|
29
|
|
|
case 3: |
|
30
|
|
|
$output .= "▓"; |
|
31
|
|
|
break; |
|
32
|
|
|
case 4: |
|
33
|
|
|
$output .= "█"; |
|
34
|
|
|
break; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
$output .= "\n"; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $output; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $ascii |
|
45
|
|
|
* @param array $charMapping |
|
46
|
|
|
* |
|
47
|
|
|
* @return array |
|
48
|
|
|
* @throws Exception |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function generateSymbolFromAsciiString($ascii, $charMapping) |
|
51
|
|
|
{ |
|
52
|
|
|
$symbol = []; |
|
53
|
|
|
$lines = explode("\n", $ascii); |
|
54
|
|
|
$width = strlen($lines[0]); |
|
55
|
|
|
if ($width > 52) { |
|
56
|
|
|
throw new Exception('Text width should be <= 52 chars, now it\'s ' . $width . '.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
foreach ($lines as $cols) { |
|
60
|
|
|
$newCols = []; |
|
61
|
|
|
for ($i = 0; $i < strlen($cols); $i++) { |
|
62
|
|
|
$col = $cols[$i]; |
|
63
|
|
|
$newCols[] = self::getMappedValue($col, $charMapping); |
|
64
|
|
|
} |
|
65
|
|
|
$symbol[] = $newCols; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $symbol; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param $string |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function generateAsciiFromText($string) |
|
77
|
|
|
{ |
|
78
|
|
|
$lineBreaks = 0; |
|
79
|
|
|
$font = 'banner3'; |
|
80
|
|
|
|
|
81
|
|
|
$ascii = file_get_contents('http://artii.herokuapp.com/make?text=' . urlencode($string) . '&font=' . $font); |
|
82
|
|
|
$lines = explode("\n", $ascii); |
|
83
|
|
|
$width = count($lines[0]); |
|
84
|
|
|
|
|
85
|
|
|
if ($lineBreaks > 0) { |
|
86
|
|
|
$more = ''; |
|
87
|
|
|
for ($i = 0; $i < $lineBreaks; $i++) { |
|
88
|
|
|
$more .= str_pad($more, $width) . "\n"; |
|
89
|
|
|
} |
|
90
|
|
|
$ascii = $more . $ascii; |
|
91
|
|
|
} |
|
92
|
|
|
$lines = explode("\n", $ascii); |
|
93
|
|
|
|
|
94
|
|
|
$ret = []; |
|
95
|
|
|
foreach ($lines as $key => $line) { |
|
96
|
|
|
if ($key < 7) { |
|
97
|
|
|
$ret[] = $line; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $ascii; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param array $parsedJson |
|
106
|
|
|
* @param array $charMapping |
|
107
|
|
|
* |
|
108
|
|
|
* @return array |
|
109
|
|
|
* @throws Exception |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function generateSymbolFromJson($parsedJson, $charMapping) |
|
112
|
|
|
{ |
|
113
|
|
|
$lines = $parsedJson['layers'][0]['rows']; |
|
114
|
|
|
|
|
115
|
|
|
$symbol = []; |
|
116
|
|
|
$width = count($lines[0]['cells']); |
|
117
|
|
|
if ($width > 52) { |
|
118
|
|
|
throw new Exception('Text width should be <= 52 chars, now it\'s ' . $width . '.'); |
|
119
|
|
|
} |
|
120
|
|
|
if (count($lines) < 5) { |
|
121
|
|
|
throw new Exception('Text height should be >= 5 lines, now it\'s ' . count($lines) . '.'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
foreach ($lines as $key => $line) { |
|
125
|
|
|
if ($key < 7) { |
|
126
|
|
|
$newCols = []; |
|
127
|
|
|
foreach ($line['cells'] as $cell) { |
|
128
|
|
|
$col = $cell[2]; |
|
129
|
|
|
$newCols[] = self::getMappedValue($col, $charMapping); |
|
130
|
|
|
} |
|
131
|
|
|
$symbol[] = $newCols; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (count($symbol) != 7) { |
|
136
|
|
|
$newCols = []; |
|
137
|
|
|
for ($i = 0; $i < (7 - count($symbol)); $i++) { |
|
138
|
|
|
for ($j = 0; $j < $width; $j++) { |
|
139
|
|
|
$newCols[] = 0; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
$symbol[] = $newCols; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $symbol; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $char |
|
150
|
|
|
* @param array $charMapping |
|
151
|
|
|
* |
|
152
|
|
|
* @return int |
|
153
|
|
|
*/ |
|
154
|
|
|
public static function getMappedValue($char, $charMapping) |
|
155
|
|
|
{ |
|
156
|
|
|
$value = 0; |
|
157
|
|
|
if (isset($charMapping[$char])) { |
|
158
|
|
|
$value = $charMapping[$char]; |
|
159
|
|
|
} else { |
|
160
|
|
|
if ($char == ' ') { |
|
161
|
|
|
$value = 0; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $value; |
|
166
|
|
|
} |
|
167
|
|
|
} |