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

Add support for Reversed Chars instruction in BMC blocs #402

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file added samples/bugs/Issue398.pdf
Binary file not shown.
21 changes: 19 additions & 2 deletions src/Smalot/PdfParser/PDFObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private function getDefaultFont(Page $page = null)
*/
public function getText(Page $page = null)
{
$text = '';
$result = '';
$sections = $this->getSectionsText($this->content);
$current_font = $this->getDefaultFont($page);

Expand All @@ -278,9 +278,17 @@ public function getText(Page $page = null)

foreach ($sections as $section) {
$commands = $this->getCommandsText($section);
$reverse_text = false;
$text = '';

foreach ($commands as $command) {
switch ($command[self::OPERATOR]) {
case 'BMC':
if ('ReversedChars' == $command[self::COMMAND]) {
$reverse_text = true;
}
break;

// set character spacing
case 'Tc':
break;
Expand Down Expand Up @@ -438,11 +446,20 @@ public function getText(Page $page = null)
default:
}
}

// Fix Hebrew and other reverse text oriented languages.
// @see: https://github.com/smalot/pdfparser/issues/398
if ($reverse_text) {
$chars = mb_str_split($text, 1, mb_internal_encoding());
$text = implode('', array_reverse($chars));
}

$result .= $text;
}

array_pop(self::$recursionStack);

return $text.' ';
return $result.' ';
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/PDFObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,20 @@ public function testGetSectionText()
$sections
);
}

/**
* Tests behavior with reversed chars instruction.
*
* @see: https://github.com/smalot/pdfparser/issues/398
*/
public function testReversedChars()
{
$filename = $this->rootDir.'/samples/bugs/Issue398.pdf';

$parser = $this->getParserInstance();
$document = $parser->parseFile($filename);
$pages = $document->getPages();

$this->assertStringContainsString('שלומי טסט', $pages[0]->getText());
}
}