Skip to content

Commit

Permalink
Version 1.9.0.8.
Browse files Browse the repository at this point in the history
- Added "draw()" function to "PdfBorder" class.
  • Loading branch information
laurentmuller committed Aug 2, 2024
1 parent 409522f commit 6fd2694
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Change log

## Unreleased
## 1.9.0.8 - 2024-08-02

- Added `draw()` function to `PdfBorder` class.
- Updated PHP doc.
- Added test for `PdfDocument::isLink()` function.
- Reworked `putFonts` function of `PdfDocument` class.
Expand Down
37 changes: 37 additions & 0 deletions src/PdfBorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,43 @@ public static function bottom(): self
return new self(false, false, false, true);
}

/**
* Draw borders, if applicable, to the given document using the current draw color and line width.
*
* @param PdfDocument $parent the parent document to draw borders to
* @param PdfRectangle $bounds the border bounds
*/
public function draw(PdfDocument $parent, PdfRectangle $bounds): void
{
if ($this->isNone()) {
return;
}

if ($this->isAll()) {
$parent->rectangle($bounds);

return;
}

// draw each applicable border side
$x = $bounds->x;
$y = $bounds->y;
$right = $bounds->right();
$bottom = $bounds->bottom();
if ($this->isLeft()) {
$parent->line($x, $y, $x, $bottom);
}
if ($this->isRight()) {
$parent->line($right, $y, $right, $bottom);
}
if ($this->isTop()) {
$parent->line($x, $y, $right, $y);
}
if ($this->isBottom()) {
$parent->line($x, $bottom, $right, $bottom);
}
}

/**
* Gets a value indicating if all borders are set.
*/
Expand Down
22 changes: 22 additions & 0 deletions tests/PdfBorderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@

namespace fpdf;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class PdfBorderTest extends TestCase
{
public static function getBorders(): \Generator
{
yield [PdfBorder::none()];
yield [PdfBorder::all()];
yield [PdfBorder::left()];
yield [PdfBorder::right()];
yield [PdfBorder::top()];
yield [PdfBorder::bottom()];
}

public function testAll(): void
{
$actual = PdfBorder::all();
Expand All @@ -36,6 +47,17 @@ public function testBottom(): void
self::assertTrue($actual->isBottom());
}

#[DataProvider('getBorders')]
public function testDraw(PdfBorder $border): void
{
$document = new PdfDocument();
$document->setFont(PdfFontName::ARIAL)
->addPage();
$bounds = new PdfRectangle(10, 10, 100, 100);
$border->draw($document, $bounds);
self::assertSame(1, $document->getPage());
}

public function testIsAll(): void
{
$actual = PdfBorder::none();
Expand Down

0 comments on commit 6fd2694

Please sign in to comment.