Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for printing (already stored) NV graphics data #1157

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
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
82 changes: 64 additions & 18 deletions src/Mike42/Escpos/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -373,7 +373,7 @@ public function __construct(PrintConnector $connector, CapabilityProfile $profil
$this -> setPrintBuffer($buffer);
$this -> initialize();
}

/**
* Print a barcode.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -501,7 +501,7 @@ public function close()
{
$this -> connector -> finalize();
}

/**
* Cut the paper.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -564,7 +564,7 @@ public function getCharacterTable()
{
return $this -> characterTable;
}

/**
* @return PrintBuffer
*/
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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).
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down