--- /dev/null
+From 67f8771aaff2f55d61b8246f420e762f4b526944 Mon Sep 17 00:00:00 2001
+From: Jean Ghali <jghali@libertysurf.fr>
+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 | 51 +++++++-
+ scribus/plugins/import/pdf/importpdf.h | 19 +--
+ scribus/plugins/import/pdf/slaoutput.cpp | 154 +++++++++++++++++++++--
+ scribus/plugins/import/pdf/slaoutput.h | 13 +-
+ 4 files changed, 215 insertions(+), 22 deletions(-)
+
+diff --git a/scribus/plugins/import/pdf/importpdf.cpp b/scribus/plugins/import/pdf/importpdf.cpp
+index 427cd66ef2..4679674a4d 100644
+--- a/scribus/plugins/import/pdf/importpdf.cpp
++++ b/scribus/plugins/import/pdf/importpdf.cpp
+@@ -791,11 +791,20 @@ bool PdfPlug::convert(const QString& fn)
+ names = catDict.dictLookup("OpenAction");\r
+ if (names.isDict())\r
+ {\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ std::unique_ptr<LinkAction> linkAction;\r
++ linkAction = LinkAction::parseAction(&names, pdfDoc->getCatalog()->getBaseURI());\r
++#else\r
+ LinkAction *linkAction = nullptr;\r
+ linkAction = LinkAction::parseAction(&names, pdfDoc->getCatalog()->getBaseURI());\r
++#endif\r
+ if (linkAction)\r
+ {\r
+- LinkJavaScript *jsa = (LinkJavaScript*)linkAction;\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ LinkJavaScript *jsa = (LinkJavaScript*) linkAction.get();\r
++#else\r
++ LinkJavaScript *jsa = (LinkJavaScript*) linkAction;\r
++#endif\r
+ if (jsa->isOk())\r
+ {\r
+ QString script = UnicodeParsedString(jsa->getScript());\r
+@@ -1003,3 +1012,43 @@ QString PdfPlug::UnicodeParsedString(POPPLER_CONST GooString *s1)
+ }\r
+ return result;\r
+ }\r
++\r
++QString PdfPlug::UnicodeParsedString(const std::string& s1)\r
++{\r
++ if (s1.length() == 0)\r
++ return QString();\r
++ GBool isUnicode;\r
++ int i;\r
++ Unicode u;\r
++ QString result;\r
++ if ((s1.at(0) & 0xff) == 0xfe && (s1.length() > 1 && (s1.at(1) & 0xff) == 0xff))\r
++ {\r
++ isUnicode = gTrue;\r
++ i = 2;\r
++ result.reserve((s1.length() - 2) / 2);\r
++ }\r
++ else\r
++ {\r
++ isUnicode = gFalse;\r
++ i = 0;\r
++ result.reserve(s1.length());\r
++ }\r
++ while (i < s1.length())\r
++ {\r
++ if (isUnicode)\r
++ {\r
++ u = ((s1.at(i) & 0xff) << 8) | (s1.at(i+1) & 0xff);\r
++ i += 2;\r
++ }\r
++ else\r
++ {\r
++ u = s1.at(i) & 0xff;\r
++ ++i;\r
++ }\r
++ // #15616: imagemagick may write unicode strings incorrectly in PDF\r
++ if (u == 0)\r
++ continue;\r
++ result += QChar( u );\r
++ }\r
++ return result;\r
++}\r
+diff --git a/scribus/plugins/import/pdf/importpdf.h b/scribus/plugins/import/pdf/importpdf.h
+index bb58fd208f..bc55819618 100644
+--- a/scribus/plugins/import/pdf/importpdf.h
++++ b/scribus/plugins/import/pdf/importpdf.h
+@@ -82,6 +84,7 @@ class PdfPlug : public QObject
+ bool convert(const QString& fn);
+ QRectF getCBox(int box, int pgNum);
+ QString UnicodeParsedString(POPPLER_CONST GooString *s1);
++ QString UnicodeParsedString(const std::string& s1);
+
+ QList<PageItem*> Elements;
+ double baseX, baseY;\r
+diff --git a/scribus/plugins/import/pdf/slaoutput.cpp b/scribus/plugins/import/pdf/slaoutput.cpp
+index 93ceb1e305..6e73049ef7 100644
+--- a/scribus/plugins/import/pdf/slaoutput.cpp
++++ b/scribus/plugins/import/pdf/slaoutput.cpp
+@@ -273,9 +273,15 @@ LinkAction* SlaOutputDev::SC_getAction(AnnotWidget *ano)
+ }\r
+ \r
+ /* Replacement for the crippled Poppler function LinkAction* AnnotWidget::getAdditionalAction(AdditionalActionsType type) */\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++std::unique_ptr<LinkAction> SlaOutputDev::SC_getAdditionalAction(const char *key, AnnotWidget *ano)\r
++{\r
++ std::unique_ptr<LinkAction> linkAction;\r
++#else\r
+ LinkAction* SlaOutputDev::SC_getAdditionalAction(const char *key, AnnotWidget *ano)\r
+ {\r
+ LinkAction *linkAction = nullptr;\r
++#endif\r
+ Object obj;\r
+ Ref refa = ano->getRef();\r
+ \r
+@@ -420,7 +426,11 @@ bool SlaOutputDev::handleLinkAnnot(Annot* annota, double xCoor, double yCoor, do
+ POPPLER_CONST GooString *ndst = gto->getNamedDest();\r
+ if (ndst)\r
+ {\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ std::unique_ptr<LinkDest> dstn = pdfDoc->findDest(ndst);\r
++#else\r
+ LinkDest *dstn = pdfDoc->findDest(ndst);\r
++#endif\r
+ if (dstn)\r
+ {\r
+ if (dstn->getKind() == destXYZ)\r
+@@ -464,7 +474,11 @@ bool SlaOutputDev::handleLinkAnnot(Annot* annota, double xCoor, double yCoor, do
+ POPPLER_CONST GooString *ndst = gto->getNamedDest();\r
+ if (ndst)\r
+ {\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ std::unique_ptr<LinkDest> dstn = pdfDoc->findDest(ndst);\r
++#else\r
+ LinkDest *dstn = pdfDoc->findDest(ndst);\r
++#endif\r
+ if (dstn)\r
+ {\r
+ if (dstn->getKind() == destXYZ)\r
+@@ -932,7 +946,11 @@ void SlaOutputDev::handleActions(PageItem* ite, AnnotWidget *ano)
+ POPPLER_CONST GooString *ndst = gto->getNamedDest();\r
+ if (ndst)\r
+ {\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ std::unique_ptr<LinkDest> dstn = pdfDoc->findDest(ndst);\r
++#else\r
+ LinkDest *dstn = pdfDoc->findDest(ndst);\r
++#endif\r
+ if (dstn)\r
+ {\r
+ if (dstn->getKind() == destXYZ)\r
+@@ -984,7 +1002,11 @@ void SlaOutputDev::handleActions(PageItem* ite, AnnotWidget *ano)
+ POPPLER_CONST GooString *ndst = gto->getNamedDest();\r
+ if (ndst)\r
+ {\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ std::unique_ptr<LinkDest> dstn = pdfDoc->findDest(ndst);\r
++#else\r
+ LinkDest *dstn = pdfDoc->findDest(ndst);\r
++#endif\r
+ if (dstn)\r
+ {\r
+ if (dstn->getKind() == destXYZ)\r
+@@ -1053,96 +1075,148 @@ void SlaOutputDev::handleActions(PageItem* ite, AnnotWidget *ano)
+ else\r
+ qDebug() << "Found unsupported Action of type" << Lact->getKind();\r
+ }\r
+- LinkAction *Aact = SC_getAdditionalAction("D", ano);\r
++ auto Aact = SC_getAdditionalAction("D", ano);\r
+ if (Aact)\r
+ {\r
+ if (Aact->getKind() == actionJavaScript)\r
+ {\r
+- LinkJavaScript *jsa = (LinkJavaScript*)Aact;\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact.get();\r
++#else\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact;\r
++#endif\r
+ if (jsa->isOk())\r
+ {\r
+ ite->annotation().setD_act(UnicodeParsedString(jsa->getScript()));\r
+ ite->annotation().setAAact(true);\r
+ }\r
+ }\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ Aact.reset();\r
++#else\r
+ Aact = nullptr;\r
++#endif\r
+ }\r
+ Aact = SC_getAdditionalAction("E", ano);\r
+ if (Aact)\r
+ {\r
+ if (Aact->getKind() == actionJavaScript)\r
+ {\r
+- LinkJavaScript *jsa = (LinkJavaScript*)Aact;\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact.get();\r
++#else\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact;\r
++#endif\r
+ if (jsa->isOk())\r
+ {\r
+ ite->annotation().setE_act(UnicodeParsedString(jsa->getScript()));\r
+ ite->annotation().setAAact(true);\r
+ }\r
+ }\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ Aact.reset();\r
++#else\r
+ Aact = nullptr;\r
++#endif\r
+ }\r
+ Aact = SC_getAdditionalAction("X", ano);\r
+ if (Aact)\r
+ {\r
+ if (Aact->getKind() == actionJavaScript)\r
+ {\r
+- LinkJavaScript *jsa = (LinkJavaScript*)Aact;\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact.get();\r
++#else\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact;\r
++#endif\r
+ if (jsa->isOk())\r
+ {\r
+ ite->annotation().setX_act(UnicodeParsedString(jsa->getScript()));\r
+ ite->annotation().setAAact(true);\r
+ }\r
+ }\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ Aact.reset();\r
++#else\r
+ Aact = nullptr;\r
++#endif\r
+ }\r
+ Aact = SC_getAdditionalAction("Fo", ano);\r
+ if (Aact)\r
+ {\r
+ if (Aact->getKind() == actionJavaScript)\r
+ {\r
+- LinkJavaScript *jsa = (LinkJavaScript*)Aact;\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact.get();\r
++#else\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact;\r
++#endif\r
+ if (jsa->isOk())\r
+ {\r
+ ite->annotation().setFo_act(UnicodeParsedString(jsa->getScript()));\r
+ ite->annotation().setAAact(true);\r
+ }\r
+ }\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ Aact.reset();\r
++#else\r
+ Aact = nullptr;\r
++#endif\r
+ }\r
+ Aact = SC_getAdditionalAction("Bl", ano);\r
+ if (Aact)\r
+ {\r
+ if (Aact->getKind() == actionJavaScript)\r
+ {\r
+- LinkJavaScript *jsa = (LinkJavaScript*)Aact;\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact.get();\r
++#else\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact;\r
++#endif\r
+ if (jsa->isOk())\r
+ {\r
+ ite->annotation().setBl_act(UnicodeParsedString(jsa->getScript()));\r
+ ite->annotation().setAAact(true);\r
+ }\r
+ }\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ Aact.reset();\r
++#else\r
+ Aact = nullptr;\r
++#endif\r
+ }\r
+ Aact = SC_getAdditionalAction("C", ano);\r
+ if (Aact)\r
+ {\r
+ if (Aact->getKind() == actionJavaScript)\r
+ {\r
+- LinkJavaScript *jsa = (LinkJavaScript*)Aact;\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact.get();\r
++#else\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact;\r
++#endif\r
+ if (jsa->isOk())\r
+ {\r
+ ite->annotation().setC_act(UnicodeParsedString(jsa->getScript()));\r
+ ite->annotation().setAAact(true);\r
+ }\r
+ }\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ Aact.reset();\r
++#else\r
+ Aact = nullptr;\r
++#endif\r
+ }\r
+ Aact = SC_getAdditionalAction("F", ano);\r
+ if (Aact)\r
+ {\r
+ if (Aact->getKind() == actionJavaScript)\r
+ {\r
+- LinkJavaScript *jsa = (LinkJavaScript*)Aact;\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact.get();\r
++#else\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact;\r
++#endif\r
+ if (jsa->isOk())\r
+ {\r
+ ite->annotation().setF_act(UnicodeParsedString(jsa->getScript()));\r
+@@ -1150,14 +1224,22 @@ void SlaOutputDev::handleActions(PageItem* ite, AnnotWidget *ano)
+ ite->annotation().setFormat(5);\r
+ }\r
+ }\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ Aact.reset();\r
++#else\r
+ Aact = nullptr;\r
++#endif\r
+ }\r
+ Aact = SC_getAdditionalAction("K", ano);\r
+ if (Aact)\r
+ {\r
+ if (Aact->getKind() == actionJavaScript)\r
+ {\r
+- LinkJavaScript *jsa = (LinkJavaScript*)Aact;\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact.get();\r
++#else\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact;\r
++#endif\r
+ if (jsa->isOk())\r
+ {\r
+ ite->annotation().setK_act(UnicodeParsedString(jsa->getScript()));\r
+@@ -1165,21 +1247,33 @@ void SlaOutputDev::handleActions(PageItem* ite, AnnotWidget *ano)
+ ite->annotation().setFormat(5);\r
+ }\r
+ }\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ Aact.reset();\r
++#else\r
+ Aact = nullptr;\r
++#endif\r
+ }\r
+ Aact = SC_getAdditionalAction("V", ano);\r
+ if (Aact)\r
+ {\r
+ if (Aact->getKind() == actionJavaScript)\r
+ {\r
+- LinkJavaScript *jsa = (LinkJavaScript*)Aact;\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact.get();\r
++#else\r
++ LinkJavaScript *jsa = (LinkJavaScript*) Aact;\r
++#endif\r
+ if (jsa->isOk())\r
+ {\r
+ ite->annotation().setV_act(UnicodeParsedString(jsa->getScript()));\r
+ ite->annotation().setAAact(true);\r
+ }\r
+ }\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ Aact.reset();\r
++#else\r
+ Aact = nullptr;\r
++#endif\r
+ }\r
+ }\r
+ \r
+@@ -3901,6 +3995,46 @@ QString SlaOutputDev::UnicodeParsedString(POPPLER_CONST GooString *s1)
+ return result;\r
+ }\r
+ \r
++QString SlaOutputDev::UnicodeParsedString(const std::string& s1)\r
++{\r
++ if (s1.length() == 0)\r
++ return QString();\r
++ GBool isUnicode;\r
++ int i;\r
++ Unicode u;\r
++ QString result;\r
++ if ((s1.at(0) & 0xff) == 0xfe && (s1.length() > 1 && (s1.at(1) & 0xff) == 0xff))\r
++ {\r
++ isUnicode = gTrue;\r
++ i = 2;\r
++ result.reserve((s1.length() - 2) / 2);\r
++ }\r
++ else\r
++ {\r
++ isUnicode = gFalse;\r
++ i = 0;\r
++ result.reserve(s1.length());\r
++ }\r
++ while (i < s1.length())\r
++ {\r
++ if (isUnicode)\r
++ {\r
++ u = ((s1.at(i) & 0xff) << 8) | (s1.at(i+1) & 0xff);\r
++ i += 2;\r
++ }\r
++ else\r
++ {\r
++ u = s1.at(i) & 0xff;\r
++ ++i;\r
++ }\r
++ // #15616: imagemagick may write unicode strings incorrectly in PDF\r
++ if (u == 0)\r
++ continue;\r
++ result += QChar( u );\r
++ }\r
++ return result;\r
++}\r
++\r
+ bool SlaOutputDev::checkClip()\r
+ {\r
+ bool ret = false;\r
+diff --git a/scribus/plugins/import/pdf/slaoutput.h b/scribus/plugins/import/pdf/slaoutput.h
+index d928fada81..67b5a51937 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 <QTextStream>\r
+ #include <QTransform>\r
+ \r
++#include <memory>\r
++\r
+ #include "fpointarray.h"\r
+ #include "importpdfconfig.h"\r
+ #include "pageitem.h"\r
+@@ -159,7 +161,11 @@ class SlaOutputDev : public OutputDev
+ virtual ~SlaOutputDev();\r
+ \r
+ LinkAction* SC_getAction(AnnotWidget *ano);\r
++#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)\r
++ std::unique_ptr<LinkAction> SC_getAdditionalAction(const char *key, AnnotWidget *ano);\r
++#else\r
+ LinkAction* SC_getAdditionalAction(const char *key, AnnotWidget *ano);\r
++#endif\r
+ static GBool annotations_callback(Annot *annota, void *user_data);\r
+ bool handleTextAnnot(Annot* annota, double xCoor, double yCoor, double width, double height);\r
+ bool handleLinkAnnot(Annot* annota, double xCoor, double yCoor, double width, double height);\r
+@@ -287,6 +293,7 @@ class SlaOutputDev : public OutputDev
+ void applyMask(PageItem *ite);\r
+ void pushGroup(const QString& maskName = "", GBool forSoftMask = gFalse, GBool alpha = gFalse, bool inverted = false);\r
+ QString UnicodeParsedString(POPPLER_CONST GooString *s1);\r
++ QString UnicodeParsedString(const std::string& s1);\r
+ bool checkClip();\r
+ bool pathIsClosed {false};\r
+ QString CurrColorFill;\r