From a1768f652ac7b694cc9faa58b6c352e20fcc0f28 Mon Sep 17 00:00:00 2001
From: Jussi Pakkanen <jpakkane@gmail.com>
Date: Sun, 30 Sep 2012 17:04:58 +0300
Subject: [PATCH] Export properly formatted overprint images.

---
 Scribus/scribus/pdflib_core.cpp |   48 +++++++++++++++++++++++++++++----------
 1 file changed, 36 insertions(+), 12 deletions(-)

diff --git a/Scribus/scribus/pdflib_core.cpp b/Scribus/scribus/pdflib_core.cpp
index ce4a448..74f419f 100644
--- a/Scribus/scribus/pdflib_core.cpp
+++ b/Scribus/scribus/pdflib_core.cpp
@@ -9347,6 +9347,17 @@ bool PDFLibCore::PDF_Image(PageItem* c, const QString& fn, double sx, double sy,
 	double ax, ay, a2, a1;
 	int    origWidth = 1;
 	int    origHeight = 1;
+	QString cacheID;
+
+	/*
+	 * Overprinted images need different entries in their
+	 * dictionaries. Deal with the case where an image is
+	 * used both as overprint and as a regular image.
+	 */
+	if(c->overprint())
+		cacheID = fn + "OVERPRINT";
+	else
+		cacheID = fn;
 	ShIm   ImInfo;
 	ImInfo.ResNum = 0;
 	ImInfo.sxa = 0;
@@ -9365,9 +9376,9 @@ bool PDFLibCore::PDF_Image(PageItem* c, const QString& fn, double sx, double sy,
 	ShIm   ImInfo2;
 	ImInfo2.origXsc = c->imageXScale();
 	ImInfo2.origYsc = c->imageYScale();
-	if (SharedImages.contains(fn))
-		ImInfo2 = SharedImages[fn];
-	if ((!SharedImages.contains(fn))
+	if (SharedImages.contains(cacheID))
+		ImInfo2 = SharedImages[cacheID];
+	if ((!SharedImages.contains(cacheID))
 		 || (fromAN)
 		 || (c->asLatexFrame())
 		 || (c->effectsInUse.count() != 0)
@@ -9855,10 +9866,18 @@ bool PDFLibCore::PDF_Image(PageItem* c, const QString& fn, double sx, double sy,
 			int bytesWritten = 0;
 			// Fixme: outType variable should be set directly in the if/else maze above.
 			ColorSpaceEnum outType;
+			// Overprint does not work for images in general, but it does work
+			// for 1 bit images if they are drawn as image masks.
+			// PDF 1.3 spec, pages 197 and 257
+			bool overprint1bitImage = false;
 			if (img.imgInfo.colorspace == ColorSpaceMonochrome && c->effectsInUse.count() == 0)
 				outType = ColorSpaceMonochrome;
 			else
 				outType = getOutputType(exportToGrayscale, exportToCMYK);
+			if(outType == ColorSpaceMonochrome
+				&& img.imgInfo.colorspace == ColorSpaceMonochrome &&
+				c->overprint())
+				overprint1bitImage = true;
 			if ((outType != ColorSpaceMonochrome) && (doc.HasCMS) && (Options.UseProfiles2))
 			{
 				PutDoc("/ColorSpace "+ICCProfiles[profInUse].ICCArray+"\n");
@@ -9874,6 +9893,11 @@ bool PDFLibCore::PDF_Image(PageItem* c, const QString& fn, double sx, double sy,
 				switch (outType)
 				{
 					case ColorSpaceMonochrome :
+						if (overprint1bitImage)
+							PutDoc("/ImageMask true\n");
+						else
+							PutDoc("/ColorSpace /DeviceGray\n");
+						break;
 					case ColorSpaceGray : PutDoc("/ColorSpace /DeviceGray\n"); break;
 					case ColorSpaceCMYK : PutDoc("/ColorSpace /DeviceCMYK\n"); break;
 					default : PutDoc("/ColorSpace /DeviceRGB\n"); break;
@@ -9927,22 +9951,22 @@ bool PDFLibCore::PDF_Image(PageItem* c, const QString& fn, double sx, double sy,
 			ImInfo.ya = sy;
 			ImInfo.RequestProps = c->pixm.imgInfo.RequestProps;
 		} // not embedded PDF
-		if ((c->effectsInUse.count() == 0) && (!SharedImages.contains(fn)))
-			SharedImages.insert(fn, ImInfo);
+		if ((c->effectsInUse.count() == 0) && (!SharedImages.contains(cacheID)))
+			SharedImages.insert(cacheID, ImInfo);
 		ResCount++;
 	}
 	else
 	{
-		ImInfo = SharedImages[fn];
+		ImInfo = SharedImages[cacheID];
 		ImInfo.sxa *= sx / ImInfo.xa;
 		ImInfo.sya *= sy / ImInfo.ya;
 		/*
-		ImRes = SharedImages[fn].ResNum;
-		ImWid = SharedImages[fn].Width;
-		ImHei = SharedImages[fn].Height;
-		aufl = SharedImages[fn].reso;
-		sxn = SharedImages[fn].sxa * sx / SharedImages[fn].xa;
-		syn = SharedImages[fn].sya * sy / SharedImages[fn].ya;
+		ImRes = SharedImages[cacheID].ResNum;
+		ImWid = SharedImages[cacheID].Width;
+		ImHei = SharedImages[cacheID].Height;
+		aufl = SharedImages[cacheID].reso;
+		sxn = SharedImages[cacheID].sxa * sx / SharedImages[cacheID].xa;
+		syn = SharedImages[cacheID].sya * sy / SharedImages[cacheID].ya;
 		*/
 	}
 	QString embedPre = "";
-- 
1.7.10.4

