PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
TextFormat.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\utils;
23 
27 abstract class TextFormat{
28  const BLACK = "§0";
29  const DARK_BLUE = "§1";
30  const DARK_GREEN = "§2";
31  const DARK_AQUA = "§3";
32  const DARK_RED = "§4";
33  const DARK_PURPLE = "§5";
34  const GOLD = "§6";
35  const GRAY = "§7";
36  const DARK_GRAY = "§8";
37  const BLUE = "§9";
38  const GREEN = "§a";
39  const AQUA = "§b";
40  const RED = "§c";
41  const LIGHT_PURPLE = "§d";
42  const YELLOW = "§e";
43  const WHITE = "§f";
44 
45  const OBFUSCATED = "§k";
46  const BOLD = "§l";
47  const STRIKETHROUGH = "§m";
48  const UNDERLINE = "§n";
49  const ITALIC = "§o";
50  const RESET = "§r";
51 
59  public static function tokenize($string){
60  return preg_split("/(§[0123456789abcdefklmnor])/", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
61  }
62 
70  public static function clean($string){
71  return preg_replace(["/§[0123456789abcdefklmnor]/", "/\\x1b*/"], "", $string);
72  }
73 
81  public static function toJSON($string){
82  if(!is_array($string)){
83  $string = self::tokenize($string);
84  }
85  $newString = [];
86  $pointer =& $newString;
87  $color = "white";
88  $bold = false;
89  $italic = false;
90  $underlined = false;
91  $strikethrough = false;
92  $obfuscated = false;
93  $index = 0;
94 
95  foreach($string as $token){
96  if(isset($pointer["text"])){
97  if(!isset($newString["extra"])){
98  $newString["extra"] = [];
99  }
100  $newString["extra"][$index] = [];
101  $pointer =& $newString["extra"][$index];
102  if($color !== "white"){
103  $pointer["color"] = $color;
104  }
105  if($bold !== false){
106  $pointer["bold"] = true;
107  }
108  if($italic !== false){
109  $pointer["italic"] = true;
110  }
111  if($underlined !== false){
112  $pointer["underlined"] = true;
113  }
114  if($strikethrough !== false){
115  $pointer["strikethrough"] = true;
116  }
117  if($obfuscated !== false){
118  $pointer["obfuscated"] = true;
119  }
120  ++$index;
121  }
122  switch($token){
123  case TextFormat::BOLD:
124  if($bold === false){
125  $pointer["bold"] = true;
126  $bold = true;
127  }
128  break;
129  case TextFormat::OBFUSCATED:
130  if($obfuscated === false){
131  $pointer["obfuscated"] = true;
132  $obfuscated = true;
133  }
134  break;
135  case TextFormat::ITALIC:
136  if($italic === false){
137  $pointer["italic"] = true;
138  $italic = true;
139  }
140  break;
141  case TextFormat::UNDERLINE:
142  if($underlined === false){
143  $pointer["underlined"] = true;
144  $underlined = true;
145  }
146  break;
147  case TextFormat::STRIKETHROUGH:
148  if($strikethrough === false){
149  $pointer["strikethrough"] = true;
150  $strikethrough = true;
151  }
152  break;
153  case TextFormat::RESET:
154  if($color !== "white"){
155  $pointer["color"] = "white";
156  $color = "white";
157  }
158  if($bold !== false){
159  $pointer["bold"] = false;
160  $bold = false;
161  }
162  if($italic !== false){
163  $pointer["italic"] = false;
164  $italic = false;
165  }
166  if($underlined !== false){
167  $pointer["underlined"] = false;
168  $underlined = false;
169  }
170  if($strikethrough !== false){
171  $pointer["strikethrough"] = false;
172  $strikethrough = false;
173  }
174  if($obfuscated !== false){
175  $pointer["obfuscated"] = false;
176  $obfuscated = false;
177  }
178  break;
179 
180  //Colors
181  case TextFormat::BLACK:
182  $pointer["color"] = "black";
183  $color = "black";
184  break;
185  case TextFormat::DARK_BLUE:
186  $pointer["color"] = "dark_blue";
187  $color = "dark_blue";
188  break;
189  case TextFormat::DARK_GREEN:
190  $pointer["color"] = "dark_green";
191  $color = "dark_green";
192  break;
193  case TextFormat::DARK_AQUA:
194  $pointer["color"] = "dark_aqua";
195  $color = "dark_aqua";
196  break;
197  case TextFormat::DARK_RED:
198  $pointer["color"] = "dark_red";
199  $color = "dark_red";
200  break;
201  case TextFormat::DARK_PURPLE:
202  $pointer["color"] = "dark_purple";
203  $color = "dark_purple";
204  break;
205  case TextFormat::GOLD:
206  $pointer["color"] = "gold";
207  $color = "gold";
208  break;
209  case TextFormat::GRAY:
210  $pointer["color"] = "gray";
211  $color = "gray";
212  break;
213  case TextFormat::DARK_GRAY:
214  $pointer["color"] = "dark_gray";
215  $color = "dark_gray";
216  break;
217  case TextFormat::BLUE:
218  $pointer["color"] = "blue";
219  $color = "blue";
220  break;
221  case TextFormat::GREEN:
222  $pointer["color"] = "green";
223  $color = "green";
224  break;
225  case TextFormat::AQUA:
226  $pointer["color"] = "aqua";
227  $color = "aqua";
228  break;
229  case TextFormat::RED:
230  $pointer["color"] = "red";
231  $color = "red";
232  break;
233  case TextFormat::LIGHT_PURPLE:
234  $pointer["color"] = "light_purple";
235  $color = "light_purple";
236  break;
237  case TextFormat::YELLOW:
238  $pointer["color"] = "yellow";
239  $color = "yellow";
240  break;
241  case TextFormat::WHITE:
242  $pointer["color"] = "white";
243  $color = "white";
244  break;
245  default:
246  $pointer["text"] = $token;
247  break;
248  }
249  }
250 
251  if(isset($newString["extra"])){
252  foreach($newString["extra"] as $k => $d){
253  if(!isset($d["text"])){
254  unset($newString["extra"][$k]);
255  }
256  }
257  }
258 
259  return json_encode($newString, JSON_UNESCAPED_SLASHES);
260  }
261 
269  public static function toHTML($string){
270  if(!is_array($string)){
271  $string = self::tokenize($string);
272  }
273  $newString = "";
274  $tokens = 0;
275  foreach($string as $token){
276  switch($token){
277  case TextFormat::BOLD:
278  $newString .= "<span style=font-weight:bold>";
279  ++$tokens;
280  break;
281  case TextFormat::OBFUSCATED:
282  //$newString .= "<span style=text-decoration:line-through>";
283  //++$tokens;
284  break;
285  case TextFormat::ITALIC:
286  $newString .= "<span style=font-style:italic>";
287  ++$tokens;
288  break;
289  case TextFormat::UNDERLINE:
290  $newString .= "<span style=text-decoration:underline>";
291  ++$tokens;
292  break;
293  case TextFormat::STRIKETHROUGH:
294  $newString .= "<span style=text-decoration:line-through>";
295  ++$tokens;
296  break;
297  case TextFormat::RESET:
298  $newString .= str_repeat("</span>", $tokens);
299  $tokens = 0;
300  break;
301 
302  //Colors
303  case TextFormat::BLACK:
304  $newString .= "<span style=color:#000>";
305  ++$tokens;
306  break;
307  case TextFormat::DARK_BLUE:
308  $newString .= "<span style=color:#00A>";
309  ++$tokens;
310  break;
311  case TextFormat::DARK_GREEN:
312  $newString .= "<span style=color:#0A0>";
313  ++$tokens;
314  break;
315  case TextFormat::DARK_AQUA:
316  $newString .= "<span style=color:#0AA>";
317  ++$tokens;
318  break;
319  case TextFormat::DARK_RED:
320  $newString .= "<span style=color:#A00>";
321  ++$tokens;
322  break;
323  case TextFormat::DARK_PURPLE:
324  $newString .= "<span style=color:#A0A>";
325  ++$tokens;
326  break;
327  case TextFormat::GOLD:
328  $newString .= "<span style=color:#FA0>";
329  ++$tokens;
330  break;
331  case TextFormat::GRAY:
332  $newString .= "<span style=color:#AAA>";
333  ++$tokens;
334  break;
335  case TextFormat::DARK_GRAY:
336  $newString .= "<span style=color:#555>";
337  ++$tokens;
338  break;
339  case TextFormat::BLUE:
340  $newString .= "<span style=color:#55F>";
341  ++$tokens;
342  break;
343  case TextFormat::GREEN:
344  $newString .= "<span style=color:#5F5>";
345  ++$tokens;
346  break;
347  case TextFormat::AQUA:
348  $newString .= "<span style=color:#5FF>";
349  ++$tokens;
350  break;
351  case TextFormat::RED:
352  $newString .= "<span style=color:#F55>";
353  ++$tokens;
354  break;
355  case TextFormat::LIGHT_PURPLE:
356  $newString .= "<span style=color:#F5F>";
357  ++$tokens;
358  break;
359  case TextFormat::YELLOW:
360  $newString .= "<span style=color:#FF5>";
361  ++$tokens;
362  break;
363  case TextFormat::WHITE:
364  $newString .= "<span style=color:#FFF>";
365  ++$tokens;
366  break;
367  default:
368  $newString .= $token;
369  break;
370  }
371  }
372 
373  $newString .= str_repeat("</span>", $tokens);
374 
375  return $newString;
376  }
377 
385  public static function toANSI($string){
386  if(!is_array($string)){
387  $string = self::tokenize($string);
388  }
389  $newString = "";
390  foreach($string as $token){
391  switch($token){
392  case TextFormat::BOLD:
393  break;
394  case TextFormat::OBFUSCATED:
395  $newString .= "\x1b[8m";
396  break;
397  case TextFormat::ITALIC:
398  $newString .= "\x1b[3m";
399  break;
400  case TextFormat::UNDERLINE:
401  $newString .= "\x1b[4m";
402  break;
403  case TextFormat::STRIKETHROUGH:
404  $newString .= "\x1b[9m";
405  break;
406  case TextFormat::RESET:
407  $newString .= "\x1b[0m";
408  break;
409 
410  //Colors
411  case TextFormat::BLACK:
412  $newString .= "\x1b[0;30m";
413  break;
414  case TextFormat::DARK_BLUE:
415  $newString .= "\x1b[0;34m";
416  break;
417  case TextFormat::DARK_GREEN:
418  $newString .= "\x1b[0;32m";
419  break;
420  case TextFormat::DARK_AQUA:
421  $newString .= "\x1b[0;36m";
422  break;
423  case TextFormat::DARK_RED:
424  $newString .= "\x1b[0;31m";
425  break;
426  case TextFormat::DARK_PURPLE:
427  $newString .= "\x1b[0;35m";
428  break;
429  case TextFormat::GOLD:
430  $newString .= "\x1b[0;33m";
431  break;
432  case TextFormat::GRAY:
433  $newString .= "\x1b[0;37m";
434  break;
435  case TextFormat::DARK_GRAY:
436  $newString .= "\x1b[30;1m";
437  break;
438  case TextFormat::BLUE:
439  $newString .= "\x1b[34;1m";
440  break;
441  case TextFormat::GREEN:
442  $newString .= "\x1b[32;1m";
443  break;
444  case TextFormat::AQUA:
445  $newString .= "\x1b[36;1m";
446  break;
447  case TextFormat::RED:
448  $newString .= "\x1b[31;1m";
449  break;
450  case TextFormat::LIGHT_PURPLE:
451  $newString .= "\x1b[35;1m";
452  break;
453  case TextFormat::YELLOW:
454  $newString .= "\x1b[33;1m";
455  break;
456  case TextFormat::WHITE:
457  $newString .= "\x1b[37;1m";
458  break;
459  default:
460  $newString .= $token;
461  break;
462  }
463  }
464 
465  return $newString;
466  }
467 
468 }
static toJSON($string)
Definition: TextFormat.php:81
static tokenize($string)
Definition: TextFormat.php:59