Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for two bugs related to Unicode translation support by Font objects #698

Merged
merged 20 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function loadTranslateTable(): array
// Support for multiple bfchar sections
if (preg_match_all('/beginbfchar(?P<sections>.*?)endbfchar/s', $content, $matches)) {
foreach ($matches['sections'] as $section) {
$regexp = '/<(?P<from>[0-9A-F]+)> +<(?P<to>[0-9A-F]+)>[ \r\n]+/is';
$regexp = '/<(?P<from>[0-9A-F]+)> *<(?P<to>[0-9A-F]+)>[ \r\n]+/is';

preg_match_all($regexp, $section, $matches);

Expand Down
7 changes: 7 additions & 0 deletions src/Smalot/PdfParser/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ class Page extends PDFObject
*/
protected $dataTm;

public function setFonts($fonts)
unixnut marked this conversation as resolved.
Show resolved Hide resolved
{
if (empty($this->fonts)) {
$this->fonts = $fonts;
k00ni marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* @return Font[]
*/
Expand Down
53 changes: 53 additions & 0 deletions src/Smalot/PdfParser/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
*/
class Pages extends PDFObject
{
/**
* @var Font[]
*/
protected $fonts;

/**
* @todo Objects other than Pages or Page might need to be treated specifically in order to get Page objects out of them,
*
Expand All @@ -57,17 +62,65 @@ public function getPages(bool $deep = false): array
return $kidsElement->getContent();
}

// Prepare to apply the Pages' object's fonts to each page
$fonts = $this->getFonts();

$kids = $kidsElement->getContent();
$pages = [];

foreach ($kids as $kid) {
if ($kid instanceof self) {
$pages = array_merge($pages, $kid->getPages(true));
} elseif ($kid instanceof Page) {
if (!empty($this->fonts)) {
$kid->setFonts($fonts);
}
$pages[] = $kid;
}
}

return $pages;
}

/**
* @return Font[]
*/
public function getFonts()
{
if (null !== $this->fonts) {
return $this->fonts;
}

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

if (method_exists($resources, 'has') && $resources->has('Font')) {
if ($resources->get('Font') instanceof Element\ElementMissing) {
return [];
}

if ($resources->get('Font') instanceof Header) {
$fonts = $resources->get('Font')->getElements();
} else {
$fonts = $resources->get('Font')->getHeader()->getElements();
}

$table = [];

foreach ($fonts as $id => $font) {
if ($font instanceof Font) {
$table[$id] = $font;

// Store too on cleaned id value (only numeric)
$id = preg_replace('/[^0-9\.\-_]/', '', $id);
if ('' != $id) {
$table[$id] = $font;
}
}
}

return $this->fonts = $table;
}
k00ni marked this conversation as resolved.
Show resolved Hide resolved

return [];
}
}
Loading