Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Mike42/Escpos/ChineseEncoding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Mike42\Escpos;

/**
* Enum representing supported Chinese character encodings for ESC/POS printers
* These encodings are used to convert between Unicode text and printer-compatible character sets
*/
enum ChineseEncoding: string
{
case GBK = 'GBK'; // Extended GB2312 encoding, widely used in mainland China
case BIG5 = 'BIG-5'; // Traditional Chinese encoding, common in Taiwan and Hong Kong
case GB18030 = 'GB18030'; // Modern Unicode-compatible Chinese encoding that covers all CJK characters
}
7 changes: 4 additions & 3 deletions src/Mike42/Escpos/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1002,16 +1002,17 @@ public function text(string $str): void

/**
* 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.
* The printer will be switched to a two-byte mode and sent GBK/BIG5-encoded text.
*
* Support for this will be merged into a print buffer.
*
* @param string $str Text to print, as UTF-8
* @param ChineseEncoding $encoding Encoding of Chinese text
*/
public function textChinese(string $str = ""): void
public function textChinese(string $str = "", ChineseEncoding $encoding = ChineseEncoding::GBK): void
{
$this -> connector -> write(self::FS . "&");
$str = \UConverter::transcode($str, "GBK", "UTF-8");
$str = \UConverter::transcode($str, $encoding -> value, "UTF-8");
$this -> buffer -> writeTextRaw((string)$str);
$this -> connector -> write(self::FS . ".");
}
Expand Down