Skip to content

Commit

Permalink
replace iconv with UConverter for ZJ printer textChinese() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mike42 committed Oct 6, 2019
1 parent 51e1c9e commit 6b4215b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/Mike42/Escpos/PrintBuffers/EscposPrintBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function writeText(string $text)
}
// Normalize text - this replaces combining characters with composed glyphs, and also helps us eliminated bad UTF-8 early
$text = \Normalizer::normalize($text);
if($text === false) {
if ($text === false) {
throw new \Exception("Input must be UTF-8");
}
$i = 0;
Expand Down Expand Up @@ -296,11 +296,13 @@ private static function asciiCheck(string $char, bool $extended = false)
return false;
}

public static function mb_strlen_substitute(string $text, string $encoding = 'UTF-8') : int {
public static function mb_strlen_substitute(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 {
public static function mb_substr_substitute(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));
return $ret1;
Expand Down
7 changes: 4 additions & 3 deletions src/Mike42/Escpos/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,15 +977,16 @@ public function setUpsideDown(bool $on = true)
* Text should either be followed by a line-break, or feed() should be called
* after this to clear the print buffer.
*
* @param string $str Text to print
* @param string $str Text to print, as UTF-8
*/
public function text(string $str)
{
$this -> buffer -> writeText((string)$str);
}

/**
* Add Chinese text to the buffer. This is a specific workaround for the common Zijang printer- The printer will be switched to a two-byte mode and sent GBK-encoded text.
* Add Chinese text to the buffer. This is a specific workaround for Zijang printers-
* The printer will be switched to a two-byte mode and sent GBK-encoded text.
*
* Support for this will be merged into a print buffer.
*
Expand All @@ -994,7 +995,7 @@ public function text(string $str)
public function textChinese(string $str = "")
{
$this -> connector -> write(self::FS . "&");
$str = iconv("UTF-8", "GBK//IGNORE", $str);
$str = \UConverter::transcode($str, "GBK", "UTF-8");
$this -> buffer -> writeTextRaw((string)$str);
$this -> connector -> write(self::FS . ".");
}
Expand Down
10 changes: 0 additions & 10 deletions test/unit/CodePageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@

class CodePageTest extends PHPUnit\Framework\TestCase
{

protected function requiresIconv()
{
if (! extension_loaded('iconv')) {
$this->markTestSkipped("Requires iconv");
}
}

public function testDataIconv()
{
// Set up CP437
$this->requiresIconv();
$cp = new CodePage("CP437", array(
"name" => "CP437",
"iconv" => "CP437"
Expand All @@ -33,7 +24,6 @@ public function testDataIconv()
public function testDataIconvBogus()
{
// No errors raised, you just get an empty list of supported characters if you try to compute a fake code page
$this->requiresIconv();
$cp = new CodePage("foo", array(
"name" => "foo",
"iconv" => "foo"
Expand Down

0 comments on commit 6b4215b

Please sign in to comment.