Skip to content

Commit

Permalink
rename some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mike42 committed Oct 6, 2019
1 parent 6b4215b commit e920a0f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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)).

Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -23,6 +23,7 @@
},
"require" : {
"php" : ">=7.0.0",
"ext-json": "*",
"ext-intl": "*",
"ext-zlib": "*",
"mike42/gfx-php" : "^0.6"
Expand Down
2 changes: 1 addition & 1 deletion src/Mike42/Escpos/CodePage.php
Expand Up @@ -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;
}
}
26 changes: 16 additions & 10 deletions src/Mike42/Escpos/PrintBuffers/EscposPrintBuffer.php
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit e920a0f

Please sign in to comment.