Skip to content

Commit

Permalink
Updated addPageIndex() function signature for PdfBookmarkTrait trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Aug 12, 2024
1 parent b3570eb commit c602b28
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

## Unreleased

- Updated `addPageIndex()` function signature for `PdfBookmarkTrait` trait.

## 2.0.2 - 2024-08-12

- Added `PdfBookmarkTrait` trait.
Expand Down
4 changes: 2 additions & 2 deletions doc/tuto_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ $pdf->addBookmark('Child Bookmark', level: 1);
The given trait can also output a new index page with all bookmarks. The index
page contains a centered title ('Index') and for each bookmark:

- The bookmark text
- The bookmark text on the left
- A character separator ('.') between the bookmark text and the page number
- The page number
- The page number on the right

Call the following function before closing or outputting the document:

Expand Down
32 changes: 18 additions & 14 deletions src/Traits/PdfBookmarkTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,22 @@ public function addBookmark(
*
* <b>Remark:</b> Do nothing if no bookmark is set.
*
* @param ?string $title the index title or <code>null</code> to use the default title ('Index')
* @param PdfFontName $fontName the title and content font name
* @param float $titleFontSize the title font size in points
* @param float $contentFontSize the content font size in points
* @param bool $addBookmark true to add the index page itself in the list of the bookmarks
* @param string $separator the separator character used between the bookmark text and the page number
* @param ?string $title the index title or <code>null</code> to use the default
* title ('Index')
* @param PdfFontName|string|null $fontName the title and content font name. It can be either a font name
* enumeration or a name defined by <code>addFont()</code>.
* If <code>null</code>, the current font name is kept.
* @param float $titleSize the title font size in points
* @param float $contentSize the content font size in points
* @param bool $addBookmark true to add the index page itself in the list of the bookmarks
* @param string $separator the separator character used between the bookmark text and
* the page number
*/
public function addPageIndex(
?string $title = null,
PdfFontName $fontName = PdfFontName::ARIAL,
float $titleFontSize = 9.0,
float $contentFontSize = 9.0,
PdfFontName|string|null $fontName = null,
float $titleSize = 9.0,
float $contentSize = 9.0,
bool $addBookmark = true,
string $separator = '.'
): static {
Expand All @@ -144,19 +148,19 @@ public function addPageIndex(
return $this;
}

// save style
// save font
$oldFamily = $this->fontFamily;
$oldStyle = $this->fontStyle;
$oldSize = $this->fontSizeInPoint;

// title
$this->addPage();
$space = PdfUnit::MILLIMETER->convert(self::SPACE, $this->unit);
$this->setFont($fontName, PdfFontStyle::BOLD, $titleFontSize);
$this->setFont($fontName, PdfFontStyle::BOLD, $titleSize);
$titleBookmark = $this->outputIndexTitle($title, $addBookmark, $space);

// content style
$this->setFont($fontName, PdfFontStyle::REGULAR, $contentFontSize);
$this->setFont($fontName, PdfFontStyle::REGULAR, $contentSize);
$height = $this->getFontSize() + $space;
$printable_width = $this->getPrintableWidth();
if ('' === $separator) {
Expand Down Expand Up @@ -186,7 +190,7 @@ public function addPageIndex(
$this->outputIndexPage($page_text, $page_size, $height, $link);
}

// restore style
// restore font
$this->setFont($oldFamily, $oldStyle, $oldSize);

return $this;
Expand Down Expand Up @@ -284,7 +288,7 @@ private function outputIndexText(
float $space
): float {
$text_width = $this->getStringWidth($text);
while ($text_width > $width) {
while ($text_width > $width && '' !== $text) {
$text = \substr($text, 0, -1);
$text_width = $this->getStringWidth($text);
}
Expand Down

0 comments on commit c602b28

Please sign in to comment.