From e920a0f70fd117b83361ddaf4e3fa90d96ccdc96 Mon Sep 17 00:00:00 2001 From: Michael Billington Date: Sun, 6 Oct 2019 16:51:17 +1100 Subject: [PATCH] rename some methods --- README.md | 1 + composer.json | 1 + src/Mike42/Escpos/CodePage.php | 2 +- .../Escpos/PrintBuffers/EscposPrintBuffer.php | 26 ++++++++++++------- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b591cb2e..4a223ffb 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,7 @@ If you haven't used `composer` before, you can read about it at [getcomposer.org This project has few hard dependencies: - PHP 7.0 or newer. +- `json` extension, used to load bundled printer definitions (see [documentation](https://www.php.net/manual/en/book.json.php)) - `intl` extension, used for character encoding (see [documentation](https://www.php.net/manual/en/book.intl.php)) - `zlib` extension, used for de-compressing bundled resources (see [documentation](https://www.php.net/manual/en/book.zlib.php)). diff --git a/composer.json b/composer.json index a2dd66b0..217648f8 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ }, "require" : { "php" : ">=7.0.0", + "ext-json": "*", "ext-intl": "*", "ext-zlib": "*", "mike42/gfx-php" : "^0.6" diff --git a/src/Mike42/Escpos/CodePage.php b/src/Mike42/Escpos/CodePage.php index 4b4a0819..9ab6942b 100644 --- a/src/Mike42/Escpos/CodePage.php +++ b/src/Mike42/Escpos/CodePage.php @@ -178,7 +178,7 @@ protected static function generateEncodingMap($iconvName) } // Join into a 128-character string and return. $charMapStr = implode("", $charMap); - assert(EscposPrintBuffer::mb_strlen_substitute($charMapStr, self::INPUT_ENCODING) == 128); + assert(EscposPrintBuffer::mbStrlenSubtitute($charMapStr, self::INPUT_ENCODING) == 128); return $charMapStr; } } diff --git a/src/Mike42/Escpos/PrintBuffers/EscposPrintBuffer.php b/src/Mike42/Escpos/PrintBuffers/EscposPrintBuffer.php index 38bbc20b..f80634dc 100644 --- a/src/Mike42/Escpos/PrintBuffers/EscposPrintBuffer.php +++ b/src/Mike42/Escpos/PrintBuffers/EscposPrintBuffer.php @@ -96,24 +96,24 @@ public function writeText(string $text) } $i = 0; $j = 0; - $len = self::mb_strlen_substitute($text, self::INPUT_ENCODING); + $len = self::mbStrlenSubtitute($text, self::INPUT_ENCODING); while ($i < $len) { $matching = true; - if (($encoding = $this -> identifyText(self::mb_substr_substitute($text, $i, 1, self::INPUT_ENCODING))) === false) { + if (($encoding = $this -> identifyText(self::mbSubstrSubstitute($text, $i, 1, self::INPUT_ENCODING))) === false) { // Un-encodeable text $encoding = $this -> getPrinter() -> getCharacterTable(); } $i++; $j = 1; do { - $char = self::mb_substr_substitute($text, $i, 1, self::INPUT_ENCODING); + $char = self::mbSubstrSubstitute($text, $i, 1, self::INPUT_ENCODING); $matching = !isset($this -> available[$char]) || isset($this -> available[$char][$encoding]); if ($matching) { $i++; $j++; } } while ($matching && $i < $len); - $this -> writeTextUsingEncoding(self::mb_substr_substitute($text, $i - $j, $j, self::INPUT_ENCODING), $encoding); + $this -> writeTextUsingEncoding(self::mbSubstrSubstitute($text, $i - $j, $j, self::INPUT_ENCODING), $encoding); } } @@ -154,7 +154,7 @@ private function identifyText(string $text) { // TODO Replace this with an algorithm to choose the encoding which will // encode the farthest into the string, to minimise code page changes. - $char = self::mb_substr_substitute($text, 0, 1, self::INPUT_ENCODING); + $char = self::mbSubstrSubstitute($text, 0, 1, self::INPUT_ENCODING); if (!isset($this -> available[$char])) { /* Character not available anywhere */ return false; @@ -206,7 +206,7 @@ private function loadAvailableCharacters() } $map = $codePage -> getData(); for ($char = 128; $char <= 255; $char++) { - $utf8 = self::mb_substr_substitute($map, $char - 128, 1, self::INPUT_ENCODING); + $utf8 = self::mbSubstrSubstitute($map, $char - 128, 1, self::INPUT_ENCODING); if ($utf8 == " ") { // Skip placeholders continue; } @@ -239,11 +239,11 @@ private function loadAvailableCharacters() private function writeTextUsingEncoding(string $text, int $encodingNo) { $encodeMap = $this -> encode[$encodingNo]; - $len = self::mb_strlen_substitute($text, self::INPUT_ENCODING); + $len = self::mbStrlenSubtitute($text, self::INPUT_ENCODING); $rawText = str_repeat(self::REPLACEMENT_CHAR, $len); $j = 0; for ($i = 0; $i < $len; $i++) { - $char = self::mb_substr_substitute($text, $i, 1, self::INPUT_ENCODING); + $char = self::mbSubstrSubstitute($text, $i, 1, self::INPUT_ENCODING); if (isset($encodeMap[$char])) { $rawText[$j] = $encodeMap[$char]; } elseif (self::asciiCheck($char)) { @@ -296,12 +296,18 @@ private static function asciiCheck(string $char, bool $extended = false) return false; } - public static function mb_strlen_substitute(string $text, string $encoding = 'UTF-8') : int + /** + * Replacement for mb_strlen to help us transition away from the mbstring plugin. + */ + public static function mbStrlenSubtitute(string $text, string $encoding = 'UTF-8') : int { return count(preg_split('//u', $text, -1, PREG_SPLIT_NO_EMPTY)); } - public static function mb_substr_substitute(string $str, int $start, int $length = null, string $encoding = 'UTF-8') : string + /** + * Replacement for mb_substr to help us transition away from the mbstring plugin. + */ + public static function mbSubstrSubstitute(string $str, int $start, int $length = null, string $encoding = 'UTF-8') : string { $split = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY); $ret1 = implode(array_splice($split, $start, $length));