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 invalid return type on unknown glyph #459

Merged
merged 6 commits into from
Oct 1, 2021
Merged

Fix invalid return type on unknown glyph #459

merged 6 commits into from
Oct 1, 2021

Conversation

PrinsFrank
Copy link
Contributor

@PrinsFrank PrinsFrank commented Sep 17, 2021

When an unknown glyph is encountered, the glyph itself is returned, which is a string:

// Encoding/PostScriptGlpyhs.php

/**
 * @return float|int|mixed
 */
public static function getCodePoint($glyph)
{
    $glyphsMap = static::getGlyphs();

    if (isset($glyphsMap[$glyph])) {
        return hexdec($glyphsMap[$glyph]);
    }

    return $glyph; // <======
}

The function 'translateChar' in Encoding.php directly returns the result of the above function, but has a return type of 'int'.

// Encoding.php

public function translateChar($dec): int // <=====
{
    if (isset($this->mapping[$dec])) {
        $dec = $this->mapping[$dec];
    }

    return PostScriptGlyphs::getCodePoint($dec);
}

This PR returns null on an unknown glyph instead, and passes the $dec_av to the encoding function instead to preserve the glyph representation in the final result as in the test for issue #359

The Newly added test throws a TypeError about strings when the return type should be an int for unknown glyphs without these changes.

@PrinsFrank
Copy link
Contributor Author

Sorry, forgot to run cs fixer before pushing. Fixed!

@PrinsFrank
Copy link
Contributor Author

@k00ni Is the 'Needs work' tag still relevant or was it added because of the failing cs pipeline? What can I do to get this merged?

@k00ni
Copy link
Collaborator

k00ni commented Sep 20, 2021

@k00ni Is the 'Needs work' tag still relevant or was it added because of the failing cs pipeline? What can I do to get this merged?

It was because of failing CS pipeline and some code parts I wanted to discuss before using them. I am currently super busy and not sure when I work on this. At a first glance it looks good.

@j0k3r can you take this one?

Copy link
Collaborator

@k00ni k00ni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your pull request @PrinsFrank. If I understand you correctly your fix makes PDFParser more resistant against unknown glyphs as well as prevents TypeError.

tests/Unit/EncodingTest.php Outdated Show resolved Hide resolved
tests/Unit/EncodingTest.php Outdated Show resolved Hide resolved
Copy link
Collaborator

@j0k3r j0k3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested it locally and it (indeed) fix the issue with the related PDF.

@k00ni k00ni merged commit 5ed3040 into smalot:master Oct 1, 2021
@PrinsFrank PrinsFrank deleted the fix-invalid-return-type-on-unknown-glyph branch October 1, 2021 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants