| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace App\Servers\printer;
- /**
- * 打印机格式化服务
- */
- class NoteFormatterServer
- {
- /**
- * 58mm 系列打印机模板
- */
- public const ROW_MAX_CHAR_LEN = 32;//行最大字符长度
- private const MAX_NAME_CHAR_LEN = 20;//最大名称字符长度
- private const LAST_ROW_MAX_NAME_CHAR_LEN = 16;//最后一行最大名称字符长度
- private const MAX_QUANTITY_CHAR_LEN = 6;//数量最大长度
- private const MAX_PRICE_CHAR_LEN = 6;//价格最大长度
- /**
- * 错误信息
- * @var string
- */
- private $errorMsg = '';
- static private $server = '';
- private function __construct()
- {
- }
- /**
- * 创建对象
- * @return NoteFormatterServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) {
- self::$server = new NoteFormatterServer();
- }
- return self::$server;
- }
- /**
- * 格式化菜品列表(用于58mm打印机)
- * 注意:默认字体排版,若是字体宽度倍大后不适用
- * 58mm打印机一行可打印32个字符 汉子按照2个字符算
- * 分3列: 名称20字符一般用16字符4空格填充 数量6字符 单价6字符,不足用英文空格填充 名称过长换行
- *
- */
- public static function formatPrintOrderItem($foodName, $quantity, $price,$is_yly=0)
- {
- $orderNameEmpty = str_repeat(" ", self::MAX_NAME_CHAR_LEN);
- $foodNameLen = self::CalcGbkLenForPrint($foodName);
- // print("foodNameLen=".$foodNameLen."\n");
- $quantityStr = '*' . $quantity;
- $quantityLen = self::CalcAsciiLenForPrint($quantityStr);
- // print("quantityLen=".$quantityLen."\n");
- $priceStr = '' . round($price, 2);
- $priceLen = self::CalcAsciiLenForPrint($priceStr);
- // print("priceLen=".$priceLen);
- $result = $foodName;
- $mod = $foodNameLen % self::ROW_MAX_CHAR_LEN;
- // print("mod=".$mod."\n");
- if ($mod <= self::LAST_ROW_MAX_NAME_CHAR_LEN) {
- // 保证各个列的宽度固定,不足部分,利用空格填充
- //make sure all the column length fixed, fill with space if not enough
- $result = $result . str_repeat(" ", self::MAX_NAME_CHAR_LEN - $mod);
- } else {
- // 另起新行
- // new line
- if ($is_yly){
- $result = $result . "\n";
- }else{
- $result = $result . "<BR>";
- }
- $result = $result . $orderNameEmpty;
- }
- $result = $result . $quantityStr . str_repeat(" ", self::MAX_QUANTITY_CHAR_LEN - $quantityLen);
- $result = $result . $priceStr . str_repeat(" ", self::MAX_PRICE_CHAR_LEN - $priceLen);
- // $result = '<L><HB>'.$result . "</L></HB><BR>";
- return $result;
- }
- /**
- * 大趋智能格式化菜品列表(用于58mm打印机)
- * @param $foodName
- * @param $quantity
- * @param $price
- * @return string
- */
- public static function trenditFormatOrderItem($foodName, $quantity, $price)
- {
- $orderNameEmpty = str_repeat(" ", self::MAX_NAME_CHAR_LEN);
- $foodNameLen = self::CalcGbkLenForPrint($foodName);
- // print("foodNameLen=".$foodNameLen."\n");
- $quantityStr = '*' . $quantity;
- $quantityLen = self::CalcAsciiLenForPrint($quantityStr);
- // print("quantityLen=".$quantityLen."\n");
- $priceStr = '' . round($price, 2);
- $priceLen = self::CalcAsciiLenForPrint($priceStr);
- // print("priceLen=".$priceLen);
- $result = $foodName;
- $mod = $foodNameLen % self::ROW_MAX_CHAR_LEN;
- // print("mod=".$mod."\n");
- if ($mod <= self::LAST_ROW_MAX_NAME_CHAR_LEN) {
- // 保证各个列的宽度固定,不足部分,利用空格填充
- //make sure all the column length fixed, fill with space if not enough
- $result = $result . str_repeat(" ", self::MAX_NAME_CHAR_LEN - $mod);
- } else {
- // 另起新行
- // new line
- // $result = $result . '<BR>';
- $result = $result . $orderNameEmpty;
- }
- $result = $result . $quantityStr . str_repeat(" ", self::MAX_QUANTITY_CHAR_LEN - $quantityLen);
- $result = $result . $priceStr . str_repeat(" ", self::MAX_PRICE_CHAR_LEN - $priceLen);
- $result = '<LEFT><font# height=2>'.$result . '</font#></LEFT><BR>';
- return $result;
- }
- public static function CalcGbkLenForPrint($data)
- {
- return (strlen($data) + mb_strlen($data, "UTF8")) / 2;
- }
- public static function CalcAsciiLenForPrint($data)
- {
- return strlen($data);
- }
- /**
- * @return string
- */
- public function getErrorMsg(): string
- {
- return $this->errorMsg;
- }
- }
|