diff --git a/src/Mike42/Escpos/Printer.php b/src/Mike42/Escpos/Printer.php index 05c8a6f0..50da3041 100644 --- a/src/Mike42/Escpos/Printer.php +++ b/src/Mike42/Escpos/Printer.php @@ -361,7 +361,7 @@ public function __construct(PrintConnector $connector, CapabilityProfile $profil { /* Set connector */ $this -> connector = $connector; - + /* Set capability profile */ if ($profile === null) { $profile = CapabilityProfile::load('default'); @@ -373,7 +373,7 @@ public function __construct(PrintConnector $connector, CapabilityProfile $profil $this -> setPrintBuffer($buffer); $this -> initialize(); } - + /** * Print a barcode. * @@ -440,7 +440,7 @@ public function barcode(string $content, int $type = Printer::BARCODE_CODE39) // More advanced function B, used in preference $this -> connector -> write(self::GS . "k" . chr($type) . chr(strlen($content)) . $content); } - + /** * Print an image, using the older "bit image" command. This creates padding on the right of the image, * if its width is not divisible by 8. @@ -501,7 +501,7 @@ public function close() { $this -> connector -> finalize(); } - + /** * Cut the paper. * @@ -513,7 +513,7 @@ public function cut(int $mode = Printer::CUT_FULL, int $lines = 3) // TODO validation on cut() inputs $this -> connector -> write(self::GS . "V" . chr($mode) . chr($lines)); } - + /** * Print and feed line / Print and feed n lines. * @@ -564,7 +564,7 @@ public function getCharacterTable() { return $this -> characterTable; } - + /** * @return PrintBuffer */ @@ -621,7 +621,53 @@ public function graphics(EscposImage $img, int $size = Printer::IMG_DEFAULT) $this -> wrapperSendGraphicsData('0', 'p', $header . $rasterData); $this -> wrapperSendGraphicsData('0', '2'); } - + + /** + * Print NV graphics data which is stored on the printer + * + * Valid keycodes range from 32 to 126 + * Valid scale factors: 1 or 2 + * + * @param int $keyCode1 + * @param int $keyCode2 + * @param int $scaleWidth Width scale factor + * @param int $scaleHeight Height scale factor + * @return bool + */ + public function printNvImage(int $keyCode1, int $keyCode2, int $scaleWidth = 1, int $scaleHeight = 1): bool + { + if (! ( + ($keyCode1 > 31 && $keyCode1 < 127) + && ($keyCode2 > 31 && $keyCode2 < 127) + ) // valid keycode range + ) { + return false; + } + + if (! ( + ($scaleWidth > 0 && $scaleWidth < 3) + && ($scaleHeight > 0 && $scaleHeight < 3) + ) // valid scale factors + ) { + return false; + } + + $this->connector->write( + self::GS + . "(L" + . chr(6) + . chr(0) + . chr(48) + . chr(69) + . chr($keyCode1) + . chr($keyCode2) + . chr($scaleWidth) + . chr($scaleHeight) + ); + + return true; + } + /** * Initialize printer. This resets formatting back to the defaults. */ @@ -805,7 +851,7 @@ public function setBarcodeWidth(int $width = 3) self::validateInteger($width, 1, 255, __FUNCTION__); $this -> connector -> write(self::GS . "w" . chr($width)); } - + /** * Set the position for the Human Readable Interpretation (HRI) of barcode characters. * @@ -818,7 +864,7 @@ public function setBarcodeTextPosition(int $position = Printer::BARCODE_TEXT_NON self::validateInteger($position, 0, 3, __FUNCTION__, "Barcode text position"); $this -> connector -> write(self::GS . "H" . chr($position)); } - + /** * Turn double-strike mode on/off. * @@ -851,7 +897,7 @@ public function setEmphasis(bool $on = true) self::validateBoolean($on, __FUNCTION__); $this -> connector -> write(self::ESC . "E". ($on ? chr(1) : chr(0))); } - + /** * Select font. Most printers have two fonts (Fonts A and B), and some have a third (Font C). * @@ -862,7 +908,7 @@ public function setFont(int $font = Printer::FONT_A) self::validateInteger($font, 0, 2, __FUNCTION__); $this -> connector -> write(self::ESC . "M" . chr($font)); } - + /** * Select justification. * @@ -936,7 +982,7 @@ public function setPrintBuffer(PrintBuffer $buffer) $this -> buffer = $buffer; $this -> buffer -> setPrinter($this); } - + /** * Set black/white reverse mode on or off. In this mode, text is printed white on a black background. * @@ -1025,7 +1071,7 @@ public function textRaw(string $str = "") { $this -> buffer -> writeTextRaw((string)$str); } - + /** * Wrapper for GS ( k, to calculate and send correct data length. * @@ -1043,7 +1089,7 @@ protected function wrapperSend2dCodeData(string $fn, string $cn, string$data = ' $header = $this -> intLowHigh(strlen($data) + strlen($m) + 2, 2); $this -> connector -> write(self::GS . "(k" . $header . $cn . $fn . $m . $data); } - + /** * Wrapper for GS ( L, to calculate and send correct data length. * @@ -1060,7 +1106,7 @@ protected function wrapperSendGraphicsData(string $m, string $fn, string$data = $header = $this -> intLowHigh(strlen($data) + 2, 2); $this -> connector -> write(self::GS . "(L" . $header . $m . $fn . $data); } - + /** * Convert widths and heights to characters. Used before sending graphics to set the size. * @@ -1081,7 +1127,7 @@ protected static function dataHeader(array $inputs, bool $long = true) } return implode("", $outp); } - + /** * Generate two characters for a number: In lower and higher parts, or more parts as needed. * @@ -1100,7 +1146,7 @@ protected static function intLowHigh(int $input, int $length) } return $outp; } - + /** * Throw an exception if the argument given is not a boolean * @@ -1146,7 +1192,7 @@ protected static function validateInteger(int $test, int $min, int $max, string { self::validateIntegerMulti($test, [[$min, $max]], $source, $argument); } - + /** * Throw an exception if the argument given is not an integer within one of the specified ranges *