From cdacfea6818b7389e5717b4bfbbf99abe01549d6 Mon Sep 17 00:00:00 2001 From: Anderson Date: Wed, 11 May 2022 15:22:25 -0300 Subject: [PATCH] Added new method drawCircle and support for the ^FR command. --- src/AbstractBuilder.php | 49 ++++++++++++++++++++++----- src/PdfBuilder.php | 38 +++++++++++++++++---- src/ZplBuilder.php | 73 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 140 insertions(+), 20 deletions(-) diff --git a/src/AbstractBuilder.php b/src/AbstractBuilder.php index eede4c2..78ab82a 100644 --- a/src/AbstractBuilder.php +++ b/src/AbstractBuilder.php @@ -68,18 +68,29 @@ abstract public function setFont(string $font, float $size) : void; * R = rotated 90 degrees * I = inverted 180 degrees * B = bottom-up 270 degrees, read from bottom up + * @param bool $invert Invert the color based on the background behind the text */ - abstract public function drawText(float $x, float $y, string $text, string $orientation = 'N') : void; + abstract public function drawText(float $x, float $y, string $text, string $orientation = 'N', bool $invert = false) : void; /** * - * @param float $x1 X1 position in user units - * @param float $y1 Y1 position in user units - * @param float $x2 X2 position in user units - * @param float $y2 Y2 position in user units - * @param float $thickness Thickness in user units + * @param float $x1 X1 position in user units + * @param float $y1 Y1 position in user units + * @param float $x2 X2 position in user units + * @param float $y2 Y2 position in user units + * @param float $thickness Thickness in user units + * @param string $color 'B' for black or 'W' for white + * @param bool $invert Invert the color based on the background behind the text */ - abstract public function drawLine(float $x1, float $y1, float $x2, float $y2, float $thickness = 0) : void; + abstract public function drawLine( + float $x1, + float $y1, + float $x2, + float $y2, + float $thickness = 0, + string $color = 'B', + bool $invert = false + ) : void; /** * @@ -90,6 +101,7 @@ abstract public function drawLine(float $x1, float $y1, float $x2, float $y2, fl * @param float $thickness Thickness in user units or 0 for the default thickness * @param string $color 'B' for black or 'W' for white * @param float $round 0 (no rounding) to 8 (heaviest rounding) + * @param bool $invert Invert the color based on the background behind the text */ abstract public function drawRect( float $x, @@ -98,7 +110,8 @@ abstract public function drawRect( float $height, float $thickness = 0, string $color = 'B', - float $round = 0 + float $round = 0, + bool $invert = false ) : void; /** @@ -118,7 +131,25 @@ abstract public function drawCell( bool $ln = false, string $align = '' ) : void; - + + /** + * + * @param float $x X position in user units + * @param float $y Y position in user units + * @param float $diameter diameter of the circle in user units + * @param float $thickness Thickness in user units or 0 for the default thickness + * @param string $color 'B' for black or 'W' for white + * @param bool $invert Invert the color based on the background behind the text + */ + abstract public function drawCircle( + float $x, + float $y, + float $diameter, + float $thickness = 0, + string $color = 'B', + bool $invert = false + ) : void; + /** * @param float $x X position in user units * @param float $y Y position in user units diff --git a/src/PdfBuilder.php b/src/PdfBuilder.php index 386d2cc..7fdd27f 100644 --- a/src/PdfBuilder.php +++ b/src/PdfBuilder.php @@ -7,18 +7,18 @@ class PdfBuilder extends AbstractBuilder /** * PDF driver - for example FPDF * - * @var \Sigep\Pdf\ImprovedFPDF + * @var mixed */ protected $pdfDriver; /** * - * @param string $unit + * @param string $unit - For example mm * @param mixed $pdfDriver PDF driver - for example FPDF * * @throws BuilderException */ - public function __construct($unit = 'mm', $pdfDriver) + public function __construct($unit, $pdfDriver) { parent::__construct($unit); $this->pdfDriver = $pdfDriver; @@ -40,7 +40,7 @@ public function setFont(string $font, float $size) : void * {@inheritDoc} * @see \Zpl\AbstractBuilder::drawText() */ - public function drawText(float $x, float $y, string $text, string $orientation = 'N') : void + public function drawText(float $x, float $y, string $text, string $orientation = 'N', bool $invert = false) : void { $this->pdfDriver->Text($x, $y, $this->_($text)); } @@ -50,7 +50,15 @@ public function drawText(float $x, float $y, string $text, string $orientation = * {@inheritDoc} * @see \Zpl\AbstractBuilder::drawLine() */ - public function drawLine(float $x1, float $y1, float $x2, float $y2, float $thickness = 0) : void + public function drawLine( + float $x1, + float $y1, + float $x2, + float $y2, + float $thickness = 0, + string $color = 'B', + bool $invert = false + ) : void { if ($thickness !== 0) { $this->pdfDriver->SetLineWidth($thickness); @@ -70,7 +78,8 @@ public function drawRect( float $height, float $thickness = 0, string $color = 'B', - float $round = 0 + float $round = 0, + bool $invert = false ) : void { if ($thickness !== 0) { $this->pdfDriver->SetLineWidth($thickness); @@ -222,4 +231,21 @@ public function drawGraphic(float $x, float $y, string $image, int $width) : voi { throw new BuilderException('Command not yet implemented'); } + + /** + * {@inheritDoc} + * @see \Zpl\AbstractBuilder::drawGraphic() + * + * @throws BuilderException + */ + public function drawCircle( + float $x, + float $y, + float $diameter, + float $thickness = 0, + string $color = 'B', + bool $invert = false + ) : void { + throw new BuilderException('Command not yet implemented'); + } } diff --git a/src/ZplBuilder.php b/src/ZplBuilder.php index c4ca308..7782f73 100644 --- a/src/ZplBuilder.php +++ b/src/ZplBuilder.php @@ -85,10 +85,13 @@ public function setEncoding(int $code) : void * {@inheritDoc} * @see \Zpl\AbstractBuilder::drawText() */ - public function drawText(float $x, float $y, string $text, string $orientation = 'N') : void + public function drawText(float $x, float $y, string $text, string $orientation = 'N', bool $invert = false) : void { $this->commands[] = '^FW' . $orientation; $this->commands[] = '^FO' . $this->toDots($x) . ',' . $this->toDots($y); + if ($invert === true) { + $this->commands[] = '^FR'; + } $this->commands[] = '^FD' . $text . '^FS'; $this->commands[] = '^FWN'; } @@ -98,9 +101,25 @@ public function drawText(float $x, float $y, string $text, string $orientation = * {@inheritDoc} * @see \Zpl\AbstractBuilder::drawLine() */ - public function drawLine(float $x1, float $y1, float $x2, float $y2, float $thickness = 0) : void - { - $this->drawRect($this->x, $this->y, $x2-$x1, $y2-$y1, $thickness); + public function drawLine( + float $x1, + float $y1, + float $x2, + float $y2, + float $thickness = 0, + string $color = 'B', + bool $invert = false + ) : void { + $this->drawRect( + $this->x, + $this->y, + $x2-$x1, + $y2-$y1, + $thickness, + $color, + 0, + $invert + ); } /** @@ -115,13 +134,35 @@ public function drawRect( float $height, float $thickness = 0, string $color = 'B', - float $round = 0 + float $round = 0, + bool $invert = false ) : void { $thickness = $thickness === 0 ? 3 : $this->toDots($thickness); $this->commands[] = '^FO' . $this->toDots($x) . ',' . $this->toDots($y) + . ($invert === true ? '^FR' : '') . '^GB' . $this->toDots($width) . ',' . $this->toDots($height) . ',' . $thickness . ',' . $color . ',' . $round . '^FS'; } + + /** + * + * {@inheritDoc} + * @see \Zpl\AbstractBuilder::drawCircle() + */ + public function drawCircle( + float $x, + float $y, + float $diameter, + float $thickness = 0, + string $color = 'B', + bool $invert = false + ) : void { + $thickness = $thickness === 0 ? 3 : $this->toDots($thickness); + $this->commands[] = '^FO' . $this->toDots($x) . ',' . $this->toDots($y) + . ($invert === true ? '^FR' : '') + . '^GC' . $this->toDots($diameter) . ',' . $thickness . ',' . $color + . '^FS'; + } /** * @@ -195,6 +236,16 @@ public function drawGraphic(float $x, float $y, string $image, int $width) : voi $this->commands[] = '^FS'; } + /** + * Adds an arbitrary command to the command queue + * + * @param string $command + */ + public function addCommand(string $command) : void + { + $this->commands[] = $command; + } + /** * * @param string $command @@ -297,4 +348,16 @@ public function __toString() : string { return $this->toZpl(); } + + /** + * Reset the command queue + * + * @return void + */ + public function reset() : void + { + $this->commands = []; + $this->preCommands = []; + $this->postCommands = []; + } }