$OpenBSD: patch-scribus_plugins_import_pdf_importpdf_cpp,v 1.2 2020/03/21 19:48:55 kili Exp $

Fix build with newer poppler, from archlinux

Fix for poppler-0.86.x from svn r23478.

Index: scribus/plugins/import/pdf/importpdf.cpp
--- scribus/plugins/import/pdf/importpdf.cpp.orig
+++ scribus/plugins/import/pdf/importpdf.cpp
@@ -75,7 +75,11 @@ PdfPlug::PdfPlug(ScribusDoc* doc, int flags)
 QImage PdfPlug::readThumbnail(const QString& fName)
 {
 	QString pdfFile = QDir::toNativeSeparators(fName);
+#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 83, 0)
+	globalParams.reset(new GlobalParams());
+#else
 	globalParams = new GlobalParams();
+#endif
 	if (globalParams)
 	{
 #if defined(Q_OS_WIN32) && POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 62, 0)
@@ -90,7 +94,9 @@ QImage PdfPlug::readThumbnail(const QString& fName)
 			if (pdfDoc->getErrorCode() == errEncrypted)
 			{
 				delete pdfDoc;
+#if POPPLER_ENCODED_VERSION < POPPLER_VERSION_ENCODE(0, 83, 0)
 				delete globalParams;
+#endif
 				return QImage();
 			}
 			if (pdfDoc->isOk())
@@ -134,11 +140,15 @@ QImage PdfPlug::readThumbnail(const QString& fName)
 				image.setText("YSize", QString("%1").arg(h));
 				delete dev;
 				delete pdfDoc;
+#if POPPLER_ENCODED_VERSION < POPPLER_VERSION_ENCODE(0, 83, 0)
 				delete globalParams;
+#endif
 				return image;
 			}
 			delete pdfDoc;
+#if POPPLER_ENCODED_VERSION < POPPLER_VERSION_ENCODE(0, 83, 0)
 			delete globalParams;
+#endif
 		}
 	}
 	return QImage();
@@ -387,7 +397,11 @@ bool PdfPlug::convert(const QString& fn)
 		qApp->processEvents();
 	}
 
+#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 83, 0)
+	globalParams.reset(new GlobalParams());
+#else
 	globalParams = new GlobalParams();
+#endif
 	GooString *userPW = nullptr;
 	if (globalParams)
 	{
@@ -429,7 +443,9 @@ bool PdfPlug::convert(const QString& fn)
 					if (progressDialog)
 						progressDialog->close();
 					delete pdfDoc;
+#if POPPLER_ENCODED_VERSION < POPPLER_VERSION_ENCODE(0, 83, 0)
 					delete globalParams;
+#endif
 					return false;
 				}
 				if (progressDialog)
@@ -474,7 +490,9 @@ bool PdfPlug::convert(const QString& fn)
 							progressDialog->close();
 						delete optImp;
 						delete pdfDoc;
+#if POPPLER_ENCODED_VERSION < POPPLER_VERSION_ENCODE(0, 83, 0)
 						delete globalParams;
+#endif
 						return false;
 					}
 					pageString = optImp->getPagesString();
@@ -838,11 +856,20 @@ bool PdfPlug::convert(const QString& fn)
 								names = catDict.dictLookup("OpenAction");
 								if (names.isDict())
 								{
+#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 86, 0)
+									std::unique_ptr<LinkAction> 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());
@@ -908,8 +935,12 @@ bool PdfPlug::convert(const QString& fn)
 		}
 		delete pdfDoc;
 	}
+#if POPPLER_ENCODED_VERSION >= POPPLER_VERSION_ENCODE(0, 83, 0)
+	globalParams.release();
+#else
 	delete globalParams;
 	globalParams = nullptr;
+#endif
 
 //	qDebug() << "converting finished";
 //	qDebug() << "Imported" << Elements.count() << "Elements";
@@ -1037,6 +1068,46 @@ QString PdfPlug::UnicodeParsedString(POPPLER_CONST Goo
 		else
 		{
 			u = s1->getChar(i) & 0xff;
+			++i;
+		}
+		// #15616: imagemagick may write unicode strings incorrectly in PDF
+		if (u == 0)
+			continue;
+		result += QChar( u );
+	}
+	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
