46 public const ESCAPE =
"\xc2\xa7";
47 public const EOL =
"\n";
49 public const BLACK = TextFormat::ESCAPE .
"0";
50 public const DARK_BLUE = TextFormat::ESCAPE .
"1";
51 public const DARK_GREEN = TextFormat::ESCAPE .
"2";
52 public const DARK_AQUA = TextFormat::ESCAPE .
"3";
53 public const DARK_RED = TextFormat::ESCAPE .
"4";
54 public const DARK_PURPLE = TextFormat::ESCAPE .
"5";
55 public const GOLD = TextFormat::ESCAPE .
"6";
56 public const GRAY = TextFormat::ESCAPE .
"7";
57 public const DARK_GRAY = TextFormat::ESCAPE .
"8";
58 public const BLUE = TextFormat::ESCAPE .
"9";
59 public const GREEN = TextFormat::ESCAPE .
"a";
60 public const AQUA = TextFormat::ESCAPE .
"b";
61 public const RED = TextFormat::ESCAPE .
"c";
62 public const LIGHT_PURPLE = TextFormat::ESCAPE .
"d";
63 public const YELLOW = TextFormat::ESCAPE .
"e";
64 public const WHITE = TextFormat::ESCAPE .
"f";
65 public const MINECOIN_GOLD = TextFormat::ESCAPE .
"g";
66 public const MATERIAL_QUARTZ = TextFormat::ESCAPE .
"h";
67 public const MATERIAL_IRON = TextFormat::ESCAPE .
"i";
68 public const MATERIAL_NETHERITE = TextFormat::ESCAPE .
"j";
69 public const MATERIAL_REDSTONE = TextFormat::ESCAPE .
"m";
70 public const MATERIAL_COPPER = TextFormat::ESCAPE .
"n";
71 public const MATERIAL_GOLD = TextFormat::ESCAPE .
"p";
72 public const MATERIAL_EMERALD = TextFormat::ESCAPE .
"q";
73 public const MATERIAL_DIAMOND = TextFormat::ESCAPE .
"s";
74 public const MATERIAL_LAPIS = TextFormat::ESCAPE .
"t";
75 public const MATERIAL_AMETHYST = TextFormat::ESCAPE .
"u";
77 public const COLORS = [
78 self::BLACK => self::BLACK,
79 self::DARK_BLUE => self::DARK_BLUE,
80 self::DARK_GREEN => self::DARK_GREEN,
81 self::DARK_AQUA => self::DARK_AQUA,
82 self::DARK_RED => self::DARK_RED,
83 self::DARK_PURPLE => self::DARK_PURPLE,
84 self::GOLD => self::GOLD,
85 self::GRAY => self::GRAY,
86 self::DARK_GRAY => self::DARK_GRAY,
87 self::BLUE => self::BLUE,
88 self::GREEN => self::GREEN,
89 self::AQUA => self::AQUA,
90 self::RED => self::RED,
91 self::LIGHT_PURPLE => self::LIGHT_PURPLE,
92 self::YELLOW => self::YELLOW,
93 self::WHITE => self::WHITE,
94 self::MINECOIN_GOLD => self::MINECOIN_GOLD,
95 self::MATERIAL_QUARTZ => self::MATERIAL_QUARTZ,
96 self::MATERIAL_IRON => self::MATERIAL_IRON,
97 self::MATERIAL_NETHERITE => self::MATERIAL_NETHERITE,
98 self::MATERIAL_REDSTONE => self::MATERIAL_REDSTONE,
99 self::MATERIAL_COPPER => self::MATERIAL_COPPER,
100 self::MATERIAL_GOLD => self::MATERIAL_GOLD,
101 self::MATERIAL_EMERALD => self::MATERIAL_EMERALD,
102 self::MATERIAL_DIAMOND => self::MATERIAL_DIAMOND,
103 self::MATERIAL_LAPIS => self::MATERIAL_LAPIS,
104 self::MATERIAL_AMETHYST => self::MATERIAL_AMETHYST,
107 public const OBFUSCATED = TextFormat::ESCAPE .
"k";
108 public const BOLD = TextFormat::ESCAPE .
"l";
109 public const ITALIC = TextFormat::ESCAPE .
"o";
111 public const FORMATS = [
112 self::OBFUSCATED => self::OBFUSCATED,
113 self::BOLD => self::BOLD,
114 self::ITALIC => self::ITALIC,
117 public const RESET = TextFormat::ESCAPE .
"r";
119 private static function makePcreError() : \InvalidArgumentException{
120 $errorCode = preg_last_error();
122 PREG_INTERNAL_ERROR =>
"Internal error",
123 PREG_BACKTRACK_LIMIT_ERROR =>
"Backtrack limit reached",
124 PREG_RECURSION_LIMIT_ERROR =>
"Recursion limit reached",
125 PREG_BAD_UTF8_ERROR =>
"Malformed UTF-8",
126 PREG_BAD_UTF8_OFFSET_ERROR =>
"Bad UTF-8 offset",
127 PREG_JIT_STACKLIMIT_ERROR =>
"PCRE JIT stack limit reached"
128 ][$errorCode] ??
"Unknown (code $errorCode)";
129 throw new \InvalidArgumentException(
"PCRE error: $message");
135 private static function preg_replace(
string $pattern,
string $replacement,
string $string) :
string{
136 $result = preg_replace($pattern, $replacement, $string);
137 if($result ===
null){
138 throw self::makePcreError();
148 public static function tokenize(
string $string) : array{
149 $result = preg_split(
"/(" .
TextFormat::ESCAPE .
"[0-9a-u])/u", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
150 if($result ===
false)
throw self::makePcreError();
159 public static function clean(
string $string,
bool $removeFormat =
true) : string{
160 $string = mb_scrub($string,
'UTF-8');
161 $string = self::preg_replace(
"/[\x{E000}-\x{F8FF}]/u",
"", $string);
163 $string = str_replace(TextFormat::ESCAPE,
"", self::preg_replace(
"/" . TextFormat::ESCAPE .
"[0-9a-u]/u",
"", $string));
165 return str_replace(
"\x1b",
"", self::preg_replace(
"/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/u",
"", $string));
173 public static function colorize(
string $string,
string $placeholder =
"&") : string{
174 return self::preg_replace(
'/' . preg_quote($placeholder,
"/") .
'([0-9a-u])/u',
TextFormat::ESCAPE .
'$1', $string);
190 public static function addBase(
string $baseFormat,
string $string) : string{
191 $baseFormatParts = self::tokenize($baseFormat);
192 foreach($baseFormatParts as $part){
193 if(!isset(self::FORMATS[$part]) && !isset(self::COLORS[$part])){
194 throw new \InvalidArgumentException(
"Unexpected base format token \"$part\", expected only color and format tokens");
197 $baseFormat = self::RESET . $baseFormat;
199 return $baseFormat . str_replace(TextFormat::RESET, $baseFormat, $string);
219 public static function toHTML(
string $string) : string{
222 foreach(self::tokenize($string) as $token){
223 $formatString = match($token){
224 TextFormat::BLACK =>
"color:#000",
225 TextFormat::DARK_BLUE =>
"color:#00A",
226 TextFormat::DARK_GREEN =>
"color:#0A0",
227 TextFormat::DARK_AQUA =>
"color:#0AA",
228 TextFormat::DARK_RED =>
"color:#A00",
229 TextFormat::DARK_PURPLE =>
"color:#A0A",
230 TextFormat::GOLD =>
"color:#FA0",
231 TextFormat::GRAY =>
"color:#AAA",
232 TextFormat::DARK_GRAY =>
"color:#555",
233 TextFormat::BLUE =>
"color:#55F",
234 TextFormat::GREEN =>
"color:#5F5",
235 TextFormat::AQUA =>
"color:#5FF",
236 TextFormat::RED =>
"color:#F55",
237 TextFormat::LIGHT_PURPLE =>
"color:#F5F",
238 TextFormat::YELLOW =>
"color:#FF5",
239 TextFormat::WHITE =>
"color:#FFF",
240 TextFormat::MINECOIN_GOLD =>
"color:#dd0",
241 TextFormat::MATERIAL_QUARTZ =>
"color:#e2d3d1",
242 TextFormat::MATERIAL_IRON =>
"color:#cec9c9",
243 TextFormat::MATERIAL_NETHERITE =>
"color:#44393a",
244 TextFormat::MATERIAL_REDSTONE =>
"color:#961506",
245 TextFormat::MATERIAL_COPPER =>
"color:#b4684d",
246 TextFormat::MATERIAL_GOLD =>
"color:#deb02c",
247 TextFormat::MATERIAL_EMERALD =>
"color:#119f36",
248 TextFormat::MATERIAL_DIAMOND =>
"color:#2cb9a8",
249 TextFormat::MATERIAL_LAPIS =>
"color:#20487a",
250 TextFormat::MATERIAL_AMETHYST =>
"color:#9a5cc5",
251 TextFormat::BOLD =>
"font-weight:bold",
252 TextFormat::ITALIC =>
"font-style:italic",
255 if($formatString !==
null){
256 $newString .=
"<span style=\"$formatString\">";
258 }elseif($token === TextFormat::RESET){
259 $newString .= str_repeat(
"</span>", $tokens);
262 $newString .= $token;
266 $newString .= str_repeat(
"</span>", $tokens);