From 0413bebe7a8c08d75f6ea290d3ff54cb0df05a4e Mon Sep 17 00:00:00 2001 From: jghali Date: Mon, 2 Mar 2020 14:45:59 +0000 Subject: [PATCH] PDF import plugin: support poppler 0.86.x git-svn-id: svn://scribus.net/trunk/Scribus@23478 11d20701-8431-0410-a711-e3c959e3b870 --- scribus/plugins/import/pdf/importpdf.cpp | 53 ++++++- scribus/plugins/import/pdf/importpdf.h | 19 ++- scribus/plugins/import/pdf/slaoutput.cpp | 180 ++++++++++++++++++++--- scribus/plugins/import/pdf/slaoutput.h | 7 + 4 files changed, 226 insertions(+), 33 deletions(-) diff --git a/scribus/plugins/import/pdf/importpdf.cpp b/scribus/plugins/import/pdf/importpdf.cpp index 822617a..2c1539a 100644 --- a/scribus/plugins/import/pdf/importpdf.cpp +++ b/scribus/plugins/import/pdf/importpdf.cpp @@ -856,11 +856,20 @@ bool PdfPlug::convert(const QString& fn) names = catDict.dictLookup("OpenAction"); if (names.isDict()) { - LinkAction *linkAction = nullptr; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + std::unique_ptr linkAction; linkAction = LinkAction::parseAction(&names, pdfDoc->getCatalog()->getBaseURI()); +#else + LinkAction *linkAction = nullptr; + linkAction = LinkAction::parseAction(&names, pdfDoc->getCatalog()->getBaseURI()); +#endif if (linkAction) { - LinkJavaScript *jsa = (LinkJavaScript*)linkAction; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + LinkJavaScript *jsa = (LinkJavaScript*) linkAction.get(); +#else + LinkJavaScript *jsa = (LinkJavaScript*) linkAction; +#endif if (jsa->isOk()) { QString script = UnicodeParsedString(jsa->getScript()); @@ -1068,3 +1077,43 @@ QString PdfPlug::UnicodeParsedString(POPPLER_CONST GooString *s1) } return result; } + +QString PdfPlug::UnicodeParsedString(const std::string& s1) +{ + if (s1.length() == 0) + return QString(); + GBool isUnicode; + int i; + Unicode u; + QString result; + if ((s1.at(0) & 0xff) == 0xfe && (s1.length() > 1 && (s1.at(1) & 0xff) == 0xff)) + { + isUnicode = gTrue; + i = 2; + result.reserve((s1.length() - 2) / 2); + } + else + { + isUnicode = gFalse; + i = 0; + result.reserve(s1.length()); + } + while (i < s1.length()) + { + if (isUnicode) + { + u = ((s1.at(i) & 0xff) << 8) | (s1.at(i+1) & 0xff); + i += 2; + } + else + { + u = s1.at(i) & 0xff; + ++i; + } + // #15616: imagemagick may write unicode strings incorrectly in PDF + if (u == 0) + continue; + result += QChar( u ); + } + return result; +} diff --git a/scribus/plugins/import/pdf/importpdf.h b/scribus/plugins/import/pdf/importpdf.h index 9dbfecc..72cae6b 100644 --- a/scribus/plugins/import/pdf/importpdf.h +++ b/scribus/plugins/import/pdf/importpdf.h @@ -7,19 +7,21 @@ for which a new license (GPL+exception) is in place. #ifndef IMPORTPDF_H #define IMPORTPDF_H +#include +#include +#include +#include #include -#include #include -#include #include +#include +#include +#include #include #include -#include -#include -#include -#include -#include -#include +#include + +#include #include "fpointarray.h" #include "importpdfconfig.h" @@ -82,6 +84,7 @@ private: bool convert(const QString& fn); QRectF getCBox(int box, int pgNum); QString UnicodeParsedString(POPPLER_CONST GooString *s1); + QString UnicodeParsedString(const std::string& s1); QList Elements; double baseX, baseY; diff --git a/scribus/plugins/import/pdf/slaoutput.cpp b/scribus/plugins/import/pdf/slaoutput.cpp index fa35f51..8d8e9d4 100644 --- a/scribus/plugins/import/pdf/slaoutput.cpp +++ b/scribus/plugins/import/pdf/slaoutput.cpp @@ -308,9 +308,15 @@ LinkAction* SlaOutputDev::SC_getAction(AnnotWidget *ano) } /* Replacement for the crippled Poppler function LinkAction* AnnotWidget::getAdditionalAction(AdditionalActionsType type) */ +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) +std::unique_ptr SlaOutputDev::SC_getAdditionalAction(const char *key, AnnotWidget *ano) +{ + std::unique_ptr linkAction; +#else LinkAction* SlaOutputDev::SC_getAdditionalAction(const char *key, AnnotWidget *ano) { LinkAction *linkAction = nullptr; +#endif Object obj; Ref refa = ano->getRef(); @@ -455,7 +461,11 @@ bool SlaOutputDev::handleLinkAnnot(Annot* annota, double xCoor, double yCoor, do POPPLER_CONST GooString *ndst = gto->getNamedDest(); if (ndst) { - LinkDest *dstn = pdfDoc->findDest(ndst); +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + std::unique_ptr dstn = pdfDoc->findDest(ndst); +#else + LinkDest *dstn = pdfDoc->findDest(ndst); +#endif if (dstn) { if (dstn->getKind() == destXYZ) @@ -499,7 +509,11 @@ bool SlaOutputDev::handleLinkAnnot(Annot* annota, double xCoor, double yCoor, do POPPLER_CONST GooString *ndst = gto->getNamedDest(); if (ndst) { - LinkDest *dstn = pdfDoc->findDest(ndst); +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + std::unique_ptr dstn = pdfDoc->findDest(ndst); +#else + LinkDest *dstn = pdfDoc->findDest(ndst); +#endif if (dstn) { if (dstn->getKind() == destXYZ) @@ -967,7 +981,11 @@ void SlaOutputDev::handleActions(PageItem* ite, AnnotWidget *ano) POPPLER_CONST GooString *ndst = gto->getNamedDest(); if (ndst) { - LinkDest *dstn = pdfDoc->findDest(ndst); +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + std::unique_ptr dstn = pdfDoc->findDest(ndst); +#else + LinkDest *dstn = pdfDoc->findDest(ndst); +#endif if (dstn) { if (dstn->getKind() == destXYZ) @@ -1019,7 +1037,11 @@ void SlaOutputDev::handleActions(PageItem* ite, AnnotWidget *ano) POPPLER_CONST GooString *ndst = gto->getNamedDest(); if (ndst) { - LinkDest *dstn = pdfDoc->findDest(ndst); +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + std::unique_ptr dstn = pdfDoc->findDest(ndst); +#else + LinkDest *dstn = pdfDoc->findDest(ndst); +#endif if (dstn) { if (dstn->getKind() == destXYZ) @@ -1088,96 +1110,148 @@ void SlaOutputDev::handleActions(PageItem* ite, AnnotWidget *ano) else qDebug() << "Found unsupported Action of type" << Lact->getKind(); } - LinkAction *Aact = SC_getAdditionalAction("D", ano); + auto Aact = SC_getAdditionalAction("D", ano); if (Aact) { if (Aact->getKind() == actionJavaScript) { - LinkJavaScript *jsa = (LinkJavaScript*)Aact; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + LinkJavaScript *jsa = (LinkJavaScript*) Aact.get(); +#else + LinkJavaScript *jsa = (LinkJavaScript*) Aact; +#endif if (jsa->isOk()) { ite->annotation().setD_act(UnicodeParsedString(jsa->getScript())); ite->annotation().setAAact(true); } } - Aact = nullptr; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + Aact.reset(); +#else + Aact = nullptr; +#endif } Aact = SC_getAdditionalAction("E", ano); if (Aact) { if (Aact->getKind() == actionJavaScript) { - LinkJavaScript *jsa = (LinkJavaScript*)Aact; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + LinkJavaScript *jsa = (LinkJavaScript*) Aact.get(); +#else + LinkJavaScript *jsa = (LinkJavaScript*) Aact; +#endif if (jsa->isOk()) { ite->annotation().setE_act(UnicodeParsedString(jsa->getScript())); ite->annotation().setAAact(true); } } - Aact = nullptr; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + Aact.reset(); +#else + Aact = nullptr; +#endif } Aact = SC_getAdditionalAction("X", ano); if (Aact) { if (Aact->getKind() == actionJavaScript) { - LinkJavaScript *jsa = (LinkJavaScript*)Aact; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + LinkJavaScript *jsa = (LinkJavaScript*) Aact.get(); +#else + LinkJavaScript *jsa = (LinkJavaScript*) Aact; +#endif if (jsa->isOk()) { ite->annotation().setX_act(UnicodeParsedString(jsa->getScript())); ite->annotation().setAAact(true); } } - Aact = nullptr; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + Aact.reset(); +#else + Aact = nullptr; +#endif } Aact = SC_getAdditionalAction("Fo", ano); if (Aact) { if (Aact->getKind() == actionJavaScript) { - LinkJavaScript *jsa = (LinkJavaScript*)Aact; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + LinkJavaScript *jsa = (LinkJavaScript*) Aact.get(); +#else + LinkJavaScript *jsa = (LinkJavaScript*) Aact; +#endif if (jsa->isOk()) { ite->annotation().setFo_act(UnicodeParsedString(jsa->getScript())); ite->annotation().setAAact(true); } } - Aact = nullptr; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + Aact.reset(); +#else + Aact = nullptr; +#endif } Aact = SC_getAdditionalAction("Bl", ano); if (Aact) { if (Aact->getKind() == actionJavaScript) { - LinkJavaScript *jsa = (LinkJavaScript*)Aact; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + LinkJavaScript *jsa = (LinkJavaScript*) Aact.get(); +#else + LinkJavaScript *jsa = (LinkJavaScript*) Aact; +#endif if (jsa->isOk()) { ite->annotation().setBl_act(UnicodeParsedString(jsa->getScript())); ite->annotation().setAAact(true); } } - Aact = nullptr; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + Aact.reset(); +#else + Aact = nullptr; +#endif } Aact = SC_getAdditionalAction("C", ano); if (Aact) { if (Aact->getKind() == actionJavaScript) { - LinkJavaScript *jsa = (LinkJavaScript*)Aact; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + LinkJavaScript *jsa = (LinkJavaScript*) Aact.get(); +#else + LinkJavaScript *jsa = (LinkJavaScript*) Aact; +#endif if (jsa->isOk()) { ite->annotation().setC_act(UnicodeParsedString(jsa->getScript())); ite->annotation().setAAact(true); } } - Aact = nullptr; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + Aact.reset(); +#else + Aact = nullptr; +#endif } Aact = SC_getAdditionalAction("F", ano); if (Aact) { if (Aact->getKind() == actionJavaScript) { - LinkJavaScript *jsa = (LinkJavaScript*)Aact; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + LinkJavaScript *jsa = (LinkJavaScript*) Aact.get(); +#else + LinkJavaScript *jsa = (LinkJavaScript*) Aact; +#endif if (jsa->isOk()) { ite->annotation().setF_act(UnicodeParsedString(jsa->getScript())); @@ -1185,14 +1259,22 @@ void SlaOutputDev::handleActions(PageItem* ite, AnnotWidget *ano) ite->annotation().setFormat(5); } } - Aact = nullptr; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + Aact.reset(); +#else + Aact = nullptr; +#endif } Aact = SC_getAdditionalAction("K", ano); if (Aact) { if (Aact->getKind() == actionJavaScript) { - LinkJavaScript *jsa = (LinkJavaScript*)Aact; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + LinkJavaScript *jsa = (LinkJavaScript*) Aact.get(); +#else + LinkJavaScript *jsa = (LinkJavaScript*) Aact; +#endif if (jsa->isOk()) { ite->annotation().setK_act(UnicodeParsedString(jsa->getScript())); @@ -1200,21 +1282,33 @@ void SlaOutputDev::handleActions(PageItem* ite, AnnotWidget *ano) ite->annotation().setFormat(5); } } - Aact = nullptr; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + Aact.reset(); +#else + Aact = nullptr; +#endif } Aact = SC_getAdditionalAction("V", ano); if (Aact) { if (Aact->getKind() == actionJavaScript) { - LinkJavaScript *jsa = (LinkJavaScript*)Aact; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + LinkJavaScript *jsa = (LinkJavaScript*) Aact.get(); +#else + LinkJavaScript *jsa = (LinkJavaScript*) Aact; +#endif if (jsa->isOk()) { ite->annotation().setV_act(UnicodeParsedString(jsa->getScript())); ite->annotation().setAAact(true); } } - Aact = nullptr; +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + Aact.reset(); +#else + Aact = nullptr; +#endif } } @@ -3928,6 +4022,46 @@ QString SlaOutputDev::UnicodeParsedString(POPPLER_CONST GooString *s1) return result; } +QString SlaOutputDev::UnicodeParsedString(const std::string& s1) +{ + if (s1.length() == 0) + return QString(); + GBool isUnicode; + int i; + Unicode u; + QString result; + if ((s1.at(0) & 0xff) == 0xfe && (s1.length() > 1 && (s1.at(1) & 0xff) == 0xff)) + { + isUnicode = gTrue; + i = 2; + result.reserve((s1.length() - 2) / 2); + } +else + { + isUnicode = gFalse; + i = 0; + result.reserve(s1.length()); + } + while (i < s1.length()) + { + if (isUnicode) + { + u = ((s1.at(i) & 0xff) << 8) | (s1.at(i+1) & 0xff); + i += 2; + } + else + { + u = s1.at(i) & 0xff; + ++i; + } + // #15616: imagemagick may write unicode strings incorrectly in PDF + if (u == 0) + continue; + result += QChar( u ); + } + return result; +} + bool SlaOutputDev::checkClip() { bool ret = false; diff --git a/scribus/plugins/import/pdf/slaoutput.h b/scribus/plugins/import/pdf/slaoutput.h index ce73926..e42447c 100644 --- a/scribus/plugins/import/pdf/slaoutput.h +++ b/scribus/plugins/import/pdf/slaoutput.h @@ -20,6 +20,8 @@ for which a new license (GPL+exception) is in place. #include #include +#include + #include "fpointarray.h" #include "importpdfconfig.h" #include "pageitem.h" @@ -159,7 +161,11 @@ public: virtual ~SlaOutputDev(); LinkAction* SC_getAction(AnnotWidget *ano); +#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0) + std::unique_ptr SC_getAdditionalAction(const char *key, AnnotWidget *ano); +#else LinkAction* SC_getAdditionalAction(const char *key, AnnotWidget *ano); +#endif static GBool annotations_callback(Annot *annota, void *user_data); bool handleTextAnnot(Annot* annota, double xCoor, double yCoor, double width, double height); bool handleLinkAnnot(Annot* annota, double xCoor, double yCoor, double width, double height); @@ -287,6 +293,7 @@ private: void applyMask(PageItem *ite); void pushGroup(const QString& maskName = "", GBool forSoftMask = gFalse, GBool alpha = gFalse, bool inverted = false); QString UnicodeParsedString(POPPLER_CONST GooString *s1); + QString UnicodeParsedString(const std::string& s1); bool checkClip(); bool pathIsClosed; QString CurrColorFill; -- 2.26.0