PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
Loading...
Searching...
No Matches
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
22declare(strict_types=1);
23
24namespace pocketmine\utils;
25
26use function mb_scrub;
27use function preg_last_error;
28use function preg_quote;
29use function preg_replace;
30use function preg_split;
31use function str_repeat;
32use function str_replace;
33use const PREG_BACKTRACK_LIMIT_ERROR;
34use const PREG_BAD_UTF8_ERROR;
35use const PREG_BAD_UTF8_OFFSET_ERROR;
36use const PREG_INTERNAL_ERROR;
37use const PREG_JIT_STACKLIMIT_ERROR;
38use const PREG_RECURSION_LIMIT_ERROR;
39use const PREG_SPLIT_DELIM_CAPTURE;
40use const PREG_SPLIT_NO_EMPTY;
41
45abstract class TextFormat{
46 public const ESCAPE = "\xc2\xa7"; //ยง
47 public const EOL = "\n";
48
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";
76
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,
105 ];
106
107 public const OBFUSCATED = TextFormat::ESCAPE . "k";
108 public const BOLD = TextFormat::ESCAPE . "l";
109 public const ITALIC = TextFormat::ESCAPE . "o";
110
111 public const FORMATS = [
112 self::OBFUSCATED => self::OBFUSCATED,
113 self::BOLD => self::BOLD,
114 self::ITALIC => self::ITALIC,
115 ];
116
117 public const RESET = TextFormat::ESCAPE . "r";
118
119 private static function makePcreError() : \InvalidArgumentException{
120 $errorCode = preg_last_error();
121 $message = [
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");
130 }
131
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();
139 }
140 return $result;
141 }
142
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();
151 return $result;
152 }
153
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); //remove unicode private-use-area characters (they might break the console)
162 if($removeFormat){
163 $string = str_replace(TextFormat::ESCAPE, "", self::preg_replace("/" . TextFormat::ESCAPE . "[0-9a-u]/u", "", $string));
164 }
165 return str_replace("\x1b", "", self::preg_replace("/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/u", "", $string));
166 }
167
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);
175 }
176
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");
195 }
196 }
197 $baseFormat = self::RESET . $baseFormat;
198
199 return $baseFormat . str_replace(TextFormat::RESET, $baseFormat, $string);
200 }
201
212 public static function javaToBedrock(string $string) : string{
213 return str_replace([TextFormat::ESCAPE . "m", TextFormat::ESCAPE . "n"], "", $string);
214 }
215
219 public static function toHTML(string $string) : string{
220 $newString = "";
221 $tokens = 0;
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",
253 default => null
254 };
255 if($formatString !== null){
256 $newString .= "<span style=\"$formatString\">";
257 ++$tokens;
258 }elseif($token === TextFormat::RESET){
259 $newString .= str_repeat("</span>", $tokens);
260 $tokens = 0;
261 }else{
262 $newString .= $token;
263 }
264 }
265
266 $newString .= str_repeat("</span>", $tokens);
267
268 return $newString;
269 }
270}
static addBase(string $baseFormat, string $string)
static colorize(string $string, string $placeholder="&")
static tokenize(string $string)
static clean(string $string, bool $removeFormat=true)
static javaToBedrock(string $string)
static toHTML(string $string)