Skip to content

Commit

Permalink
Refined Pages instance
Browse files Browse the repository at this point in the history
new getFonts method not only
returned stored fonts but also built related fonts
list. With this changes its easier to test and the
replacement (setupFonts) only builds the fonts list.
    
Also refined some phpdoc comments.
  • Loading branch information
k00ni authored Apr 19, 2024
1 parent 6c1c349 commit 8a8ea2c
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/Smalot/PdfParser/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
class Pages extends PDFObject
{
/**
* @var Font[]
* @var array<\Smalot\PdfParser\Font>|null
*/
protected $fonts;

Expand All @@ -63,7 +63,10 @@ public function getPages(bool $deep = false): array
}

// Prepare to apply the Pages' object's fonts to each page
$fonts = $this->getFonts();
if (false === \is_array($this->fonts)) {
$this->setupFonts();
}
$fontsAvailable = 0 < \count($this->fonts);

$kids = $kidsElement->getContent();
$pages = [];
Expand All @@ -72,8 +75,8 @@ public function getPages(bool $deep = false): array
if ($kid instanceof self) {
$pages = array_merge($pages, $kid->getPages(true));
} elseif ($kid instanceof Page) {
if (!empty($this->fonts)) {
$kid->setFonts($fonts);
if ($fontsAvailable) {
$kid->setFonts($this->fonts);
}
$pages[] = $kid;
}
Expand All @@ -83,19 +86,18 @@ public function getPages(bool $deep = false): array
}

/**
* @return Font[]
* Gathers information about fonts and collects them in a list.
*
* @return void
*/
protected function getFonts()
protected function setupFonts()
{
if (null !== $this->fonts) {
return $this->fonts;
}

$resources = $this->get('Resources');

if (method_exists($resources, 'has') && $resources->has('Font')) {
// no fonts available, therefore stop here
if ($resources->get('Font') instanceof Element\ElementMissing) {
return [];
return;
}

if ($resources->get('Font') instanceof Header) {
Expand All @@ -118,9 +120,9 @@ protected function getFonts()
}
}

return $this->fonts = $table;
$this->fonts = $table;
} else {
$this->fonts = [];
}

return [];
}
}

0 comments on commit 8a8ea2c

Please sign in to comment.