Skip to content

Commit

Permalink
Merge pull request #146 from mike42/testing
Browse files Browse the repository at this point in the history
Update master w/ margins, spacing, Vietnamese text support, bug fix
  • Loading branch information
mike42 authored Jul 19, 2016
2 parents 688c9df + 10148b7 commit f6bb510
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 5 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,22 @@ Parameters:

- `int $justification`: One of `Printer::JUSTIFY_LEFT`, `Printer::JUSTIFY_CENTER`, or `Printer::JUSTIFY_RIGHT`.

### setPrintLeftMargin($margin)

Set print area left margin. Reset to default with `Printer::initialize()`.

Parameters:

- `int $margin`: The left margin to set on to the print area, in dots.

### setPrintWidth($width)

Set print area width. This can be used to add a right margin to the print area. Reset to default with `Printer::initialize()`.

Parameters:

- `int $width`: The width of the page print area, in dots.

### setReverseColors($on)
Set black/white reverse mode on or off. In this mode, text is printed white on a black background.

Expand Down
48 changes: 48 additions & 0 deletions example/margins-and-spacing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/* Left margin & page width demo. */
require __DIR__ . '/../autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;

$connector = new FilePrintConnector("php://stdout"); // Add connector for your printer here.
$printer = new Printer($connector);

/* Line spacing */
/*
$printer -> setEmphasis(true);
$printer -> text("Line spacing\n");
$printer -> setEmphasis(false);
foreach(array(16, 32, 64, 128, 255) as $spacing) {
$printer -> setLineSpacing($spacing);
$printer -> text("Spacing $spacing: The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.\n");
}
$printer -> setLineSpacing(); // Back to default
*/

/* Stuff around with left margin */
$printer -> setEmphasis(true);
$printer -> text("Left margin\n");
$printer -> setEmphasis(false);
$printer -> text("Default left\n");
foreach(array(1, 2, 4, 8, 16, 32, 64, 128, 256, 512) as $margin) {
$printer -> setPrintLeftMargin($margin);
$printer -> text("left margin $margin\n");
}
/* Reset left */
$printer -> setPrintLeftMargin(0);

/* Stuff around with page width */
$printer -> setEmphasis(true);
$printer -> text("Page width\n");
$printer -> setEmphasis(false);
$printer -> setJustification(Printer::JUSTIFY_RIGHT);
$printer -> text("Default width\n");
foreach(array(512, 256, 128, 64) as $width) {
$printer -> setPrintWidth($width);
$printer -> text("page width $width\n");
}

/* Printer shutdown */
$printer -> cut();
$printer -> close();

3 changes: 2 additions & 1 deletion example/resources/character-encoding-test-strings.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ $inputsOk = array(
"Polish" => "Pchnąć w tę łódź jeża lub ośm skrzyń fig.\n",
"Russian" => "В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!\n",
"Turkish" => "Pijamalı hasta, yağız şoföre çabucak güvendi.\n",
"Japanese (Katakana half-width)" => implode("\n", array("イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム", "ウイノオクヤマ ケフコエテ アサキユメミシ エヒモセスン")) . "\n"
"Japanese (Katakana half-width)" => implode("\n", array("イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム", "ウイノオクヤマ ケフコエテ アサキユメミシ エヒモセスン")) . "\n",
"Vietnamese" => "Tiếng Việt, còn gọi tiếng Việt Nam hay Việt ngữ, là ngôn ngữ của người Việt (người Kinh) và là ngôn ngữ chính thức tại Việt Nam.\n"
);

/*
Expand Down
23 changes: 20 additions & 3 deletions src/Mike42/Escpos/CapabilityProfiles/DefaultCapabilityProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,24 @@ class DefaultCapabilityProfile extends AbstractCapabilityProfile
*/
public function getCustomCodePages()
{
return array();
return array(
'TCVN-3-1' => " ".
" ".
" ăâêôơưđ ".
" àảãáạ ằẳẵắ ".
" ặầẩẫấậè ẻẽ".
"éẹềểễếệìỉ ĩíịò".
" ỏõóọồổỗốộờởỡớợù".
" ủũúụừửữứựỳỷỹýỵ ",
'TCVN-3-2' => " ".
" ".
" ĂÂ Ð ÊÔƠƯ ".
" ÀẢÃÁẠ ẰẲẴẮ ".
" ẶẦẨẪẤẬÈ ẺẼ".
"ÉẸỀỂỄẾỆÌỈ ĨÍỊÒ".
" ỎÕÓỌỒỔỖỐỘỜỞỠỚỢÙ".
" ỦŨÚỤỪỬỮỨỰỲỶỸÝỴ "
);
}

/**
Expand Down Expand Up @@ -66,8 +83,8 @@ public function getSupportedCodePages()
24 => false, // Thai Character Code 16
25 => false, // Thai Character Code 17
26 => false, // Thai Character Code 18
30 => false, // TCVN-3: Vietnamese
31 => false, // TCVN-3: Vietnamese
30 => 'custom:TCVN-3-1', // TCVN-3: Vietnamese
31 => 'custom:TCVN-3-2', // TCVN-3: Vietnamese
32 => CodePage::CP720,
33 => CodePage::CP775,
34 => CodePage::CP855,
Expand Down
1 change: 1 addition & 0 deletions src/Mike42/Escpos/EscposImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Mike42\Escpos;

use Exception;
use InvalidArgumentException;

/**
* This class deals with images in raster formats, and converts them into formats
Expand Down
Binary file not shown.
Binary file not shown.
25 changes: 24 additions & 1 deletion src/Mike42/Escpos/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,30 @@ public function setJustification($justification = Printer::JUSTIFY_LEFT)
self::validateInteger($justification, 0, 2, __FUNCTION__);
$this -> connector -> write(self::ESC . "a" . chr($justification));
}


/**
* Set print area left margin. Reset to default with Printer::initialize()
*
* @param int $margin The left margin to set on to the print area, in dots.
*/
public function setPrintLeftMargin($margin = 0)
{
self::validateInteger($margin, 0, 65535, __FUNCTION__);
$this -> connector -> write(Printer::GS . 'L' . self::intLowHigh($margin, 2));
}

/**
* Set print area width. This can be used to add a right margin to the print area.
* Reset to default with Printer::initialize()
*
* @param int $width The width of the page print area, in dots.
*/
public function setPrintWidth($width = 512)
{
self::validateInteger($width, 1, 65535, __FUNCTION__);
$this -> connector -> write(Printer::GS . 'W' . self::intLowHigh($width, 2));
}

/**
* Attach a different print buffer to the printer. Buffers are responsible for handling text output to the printer.
*
Expand Down
9 changes: 9 additions & 0 deletions test/integration/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ public function testTextSize()
$this -> outpTest($outp, "text-size.bin");
}

/**
* @medium
*/
public function testMarginsAndSpacing()
{
$outp = $this -> runExample("margins-and-spacing.php");
$this -> outpTest($outp, "margins-and-spacing.bin");
}

public function testInterfaceCups()
{
$outp = $this -> runSyntaxCheck("interface/cups.php");
Expand Down
Binary file modified test/integration/resources/output/character-encodings.bin
Binary file not shown.
Binary file modified test/integration/resources/output/character-tables.bin
Binary file not shown.
Binary file not shown.
16 changes: 16 additions & 0 deletions test/unit/EscposImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
use Mike42\Escpos\EscposImage;

class EscposImageTest extends PHPUnit_Framework_TestCase
{
public function testImageMissingException()
{
$this -> setExpectedException('Exception');
$img = EscposImage::load('not-a-real-file.png');
}
public function testImageNotSupportedException()
{
$this -> setExpectedException('InvalidArgumentException');
$img = EscposImage::load('/dev/null', false, array());
}
}
5 changes: 5 additions & 0 deletions test/unit/EscposPrintBufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,9 @@ public function testHebrew()
$this -> buffer -> writeText("דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקליטה" . "\n");
$this -> checkOutput();
}

public function testVietnamese() {
$this -> buffer -> writeText("Tiếng Việt, còn gọi tiếng Việt Nam hay Việt ngữ, là ngôn ngữ của người Việt (người Kinh) và là ngôn ngữ chính thức tại Việt Nam.\n");
$this -> checkOutput("\x1b@Ti\x1bt\x1e\xd5ng Vi\xd6t, c\xdfn g\xe4i ti\xd5ng Vi\xd6t Nam hay Vi\xd6t ng\xf7, l\xb5 ng\xabn ng\xf7 c\xf1a ng\xad\xeai Vi\xd6t (ng\xad\xeai Kinh) v\xb5 l\xb5 ng\xabn ng\xf7 ch\xddnh th\xf8c t\xb9i Vi\xd6t Nam.\x0a");
}
}
39 changes: 39 additions & 0 deletions test/unit/EscposTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,45 @@ public function testSetColorInvalid()
$this -> setExpectedException('InvalidArgumentException');
$this -> printer -> setColor(3);
}

/* Set print width */
public function testSetPrintWidthDefault()
{
$this -> printer -> setPrintWidth();
$this -> checkOutput("\x1b@\x1dW\x00\x02");
}

public function testSetPrintWidthNarrow()
{
$this -> printer -> setPrintWidth(400);
$this -> checkOutput("\x1b@\x1dW\x90\x01");
}

public function testSetPrintWidthInvalid()
{
$this -> setExpectedException('InvalidArgumentException');
$this -> printer -> setPrintWidth(0);
}

/* Set print left margin */
public function testSetPrintLeftMarginDefault()
{
$this -> printer -> setPrintLeftMargin();
$this -> checkOutput("\x1b@\x1dL\x00\x00");
}

public function testSetPrintLeftMarginWide()
{
$this -> printer -> setPrintLeftMargin(32);
$this -> checkOutput("\x1b@\x1dL\x20\x00");
}

public function testPrintLeftMarginInvalid()
{
$this -> setExpectedException('InvalidArgumentException');
$this -> printer -> setPrintLeftMargin(70000);
$this -> checkOutput();
}
}

/*
Expand Down

0 comments on commit f6bb510

Please sign in to comment.