diff --git a/CHANGELOG.md b/CHANGELOG.md index ea2dbe1..1b1a6e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/PdfBorder.php b/src/PdfBorder.php index 3ab12ff..b424ce1 100644 --- a/src/PdfBorder.php +++ b/src/PdfBorder.php @@ -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. */ diff --git a/tests/PdfBorderTest.php b/tests/PdfBorderTest.php index 3e12171..522ddb0 100644 --- a/tests/PdfBorderTest.php +++ b/tests/PdfBorderTest.php @@ -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(); @@ -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();