From 8f6559bf63d3264fefdbb4e2a9750e211455e27a Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sun, 25 Dec 2022 16:19:30 +0100 Subject: [PATCH] Issue #33: ToUnicode for simple fonts not used --- Pdf4QtLib/sources/pdffont.cpp | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Pdf4QtLib/sources/pdffont.cpp b/Pdf4QtLib/sources/pdffont.cpp index 4173724..fdda6cf 100644 --- a/Pdf4QtLib/sources/pdffont.cpp +++ b/Pdf4QtLib/sources/pdffont.cpp @@ -1259,6 +1259,36 @@ PDFFontPointer PDFFont::createFont(const PDFObject& object, const PDFDocument* d throw PDFException(PDFTranslationContext::tr("Invalid encoding entry of the font.")); } } + else if (fontDictionary->hasKey("ToUnicode")) + { + PDFFontCMap toUnicodeCMap; + const PDFObject& toUnicode = document->getObject(fontDictionary->get("ToUnicode")); + if (toUnicode.isName()) + { + toUnicodeCMap = PDFFontCMap::createFromName(toUnicode.getString()); + } + else if (toUnicode.isStream()) + { + const PDFStream* stream = toUnicode.getStream(); + QByteArray decodedStream = document->getDecodedStream(stream); + toUnicodeCMap = PDFFontCMap::createFromData(decodedStream); + } + + const bool hasToUnicode = toUnicodeCMap.isValid(); + if (hasToUnicode) + { + for (size_t i = 0; i < differences.size(); ++i) + { + QChar character = toUnicodeCMap.getToUnicode(static_cast(i)); + + if (!character.isNull()) + { + differences[i] = character; + hasDifferences = true; + } + } + } + } if (encoding == PDFEncoding::Encoding::Invalid) { @@ -1296,7 +1326,7 @@ PDFFontPointer PDFFont::createFont(const PDFObject& object, const PDFDocument* d for (size_t i = 0; i < standardEncoding.size(); ++i) { if ((simpleFontEncodingTable[i].isNull() || simpleFontEncodingTable[i] == QChar(QChar::SpecialCharacter::ReplacementCharacter)) && - (!standardEncoding[i].isNull() && standardEncoding[i] != QChar(QChar::SpecialCharacter::ReplacementCharacter))) + (!standardEncoding[i].isNull() && standardEncoding[i] != QChar(QChar::SpecialCharacter::ReplacementCharacter))) { simpleFontEncodingTable[i] = standardEncoding[i]; }