r17448 by fschmid - PDF Import: import layers defined by optional content groups.

scribus-commit scribus-commit at lists.scribus.net
Mon Apr 16 20:01:20 UTC 2012


Author: fschmid
Date: Mon Apr 16 20:01:20 2012
New Revision: 17448

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=17448
Log:
PDF Import: import layers defined by optional content groups.

Modified:
    trunk/Scribus/scribus/plugins/import/pdf/importpdf.cpp
    trunk/Scribus/scribus/plugins/import/pdf/slaoutput.cpp
    trunk/Scribus/scribus/plugins/import/pdf/slaoutput.h

Modified: trunk/Scribus/scribus/plugins/import/pdf/importpdf.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17448&path=/trunk/Scribus/scribus/plugins/import/pdf/importpdf.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/import/pdf/importpdf.cpp (original)
+++ trunk/Scribus/scribus/plugins/import/pdf/importpdf.cpp Mon Apr 16 20:01:20 2012
@@ -16,6 +16,7 @@
 #include <QDebug>
 #include "slaoutput.h"
 #include <GlobalParams.h>
+#include <poppler-config.h>
 
 #include "importpdf.h"
 
@@ -277,7 +278,9 @@
 
 bool PdfPlug::convert(QString fn)
 {
-	QString tmp;
+	bool firstLayer = true;
+	bool firstPg = true;
+	int currentLayer = m_Doc->activeLayer();
 	importedColors.clear();
 	if(progressDialog)
 	{
@@ -298,6 +301,8 @@
 	{
 		GooString *fname = new GooString(QFile::encodeName(fn).data());
 		globalParams->setErrQuiet(gTrue);
+		GBool hasOcg = gFalse;
+		QList<OptionalContentGroup*> ocgGroups;
 //		globalParams->setPrintCommands(gTrue);
 		PDFDoc *pdfDoc = new PDFDoc(fname, 0, 0, 0);
 		if (pdfDoc)
@@ -308,77 +313,94 @@
 				double vDPI = 72.0;
 				int firstPage = 1;
 				int lastPage = pdfDoc->getNumPages();
-				OCGs* ocg = pdfDoc->getOptContentConfig();
-				if (ocg)
-				{
-					GBool hasOcg = ocg->hasOCGs();
-					if (hasOcg)
-					{
-					//	qDebug() << "File has OCGs";
-						Array *order = ocg->getOrderArray();
-						if (order)
-						{
-							for (int i = 0; i < order->getLength (); ++i)
-							{
-								Object orderItem;
-								order->get(i, &orderItem);
-								if (orderItem.isDict())
-								{
-									Object ref;
-									order->getNF(i, &ref);
-									if (ref.isRef())
-									{
-										OptionalContentGroup *oc = ocg->findOcgByRef(ref.getRef());
-								//		qDebug() << "Name" << UnicodeParsedString(oc->getName()) << "State" << oc->getState() << "View" << oc->getViewState() << "Print" << oc->getPrintState();
-										oc->setState(OptionalContentGroup::On);
-									}
-									ref.free();
-								}
-							}
-						}
-						else
-						{
-							GooList *ocgs;
-							int i;
-							ocgs = ocg->getOCGs ();
-							for (i = 0; i < ocgs->getLength (); ++i)
-							{
-								OptionalContentGroup *oc = (OptionalContentGroup *)ocgs->get(i);
-							//	qDebug() << "Name" << UnicodeParsedString(oc->getName()) << "State" << oc->getState() << "View" << oc->getViewState() << "Print" << oc->getPrintState();
-								oc->setState(OptionalContentGroup::On);
-							}
-						}
-					}
-				}
-	//			qDebug() << "converting page" << firstPage;
 				SlaOutputDev *dev = new SlaOutputDev(m_Doc, &Elements, &importedColors, importerFlags);
 				if (dev->isOk())
 				{
+					OCGs* ocg = pdfDoc->getOptContentConfig();
+					if (ocg)
+					{
+						hasOcg = ocg->hasOCGs();
+						if (hasOcg)
+						{
+							Array *order = ocg->getOrderArray();
+							if (order)
+							{
+								for (int i = 0; i < order->getLength (); ++i)
+								{
+									Object orderItem;
+									order->get(i, &orderItem);
+									if (orderItem.isDict())
+									{
+										Object ref;
+										order->getNF(i, &ref);
+										if (ref.isRef())
+										{
+											OptionalContentGroup *oc = ocg->findOcgByRef(ref.getRef());
+											ocgGroups.prepend(oc);
+										}
+										ref.free();
+									}
+								}
+							}
+							else
+							{
+								GooList *ocgs;
+								int i;
+								ocgs = ocg->getOCGs ();
+								for (i = 0; i < ocgs->getLength (); ++i)
+								{
+									OptionalContentGroup *oc = (OptionalContentGroup *)ocgs->get(i);
+									ocgGroups.prepend(oc);
+								}
+							}
+						}
+					}
 					GBool useMediaBox = gTrue;
 					GBool crop = gFalse;
 					GBool printing = gFalse;
 					dev->startDoc(pdfDoc, pdfDoc->getXRef(), pdfDoc->getCatalog());
 					int rotate = pdfDoc->getPageRotate(firstPage);
-				/*	PDFRectangle *rect = pdfDoc->getPage(firstPage)->getMediaBox();
-					qDebug() << "Media Box" << rect->x1 << rect->y1 << rect->x2 << rect->y2;
-					rect = pdfDoc->getPage(firstPage)->getCropBox();
-					qDebug() << "Crop Box " << rect->x1 << rect->y1 << rect->x2 << rect->y2;
-					rect = pdfDoc->getPage(firstPage)->getBleedBox();
-					qDebug() << "Bleed Box" << rect->x1 << rect->y1 << rect->x2 << rect->y2;
-					rect = pdfDoc->getPage(firstPage)->getTrimBox();
-					qDebug() << "Trim Box " << rect->x1 << rect->y1 << rect->x2 << rect->y2;
-					rect = pdfDoc->getPage(firstPage)->getArtBox();
-					qDebug() << "Art Box  " << rect->x1 << rect->y1 << rect->x2 << rect->y2; */
 					if (importerFlags & LoadSavePlugin::lfCreateDoc)
 					{
-						m_Doc->currentPage()->setInitialHeight(pdfDoc->getPageMediaHeight(firstPage));
-						m_Doc->currentPage()->setInitialWidth(pdfDoc->getPageMediaWidth(firstPage));
-						m_Doc->currentPage()->setHeight(pdfDoc->getPageMediaHeight(firstPage));
-						m_Doc->currentPage()->setWidth(pdfDoc->getPageMediaWidth(firstPage));
-						m_Doc->currentPage()->MPageNam = CommonStrings::trMasterPageNormal;
-						m_Doc->currentPage()->m_pageSize = "Custom";
-						m_Doc->setPageSize("Custom");
-						m_Doc->reformPages(true);
+						if (hasOcg)
+						{
+							for (int a = 0; a < ocgGroups.count(); a++)
+							{
+								OptionalContentGroup *oc = ocgGroups[a];
+								if (firstLayer)
+								{
+									m_Doc->changeLayerName(m_Doc->activeLayer(), UnicodeParsedString(oc->getName()));
+									currentLayer = m_Doc->activeLayer();
+									firstLayer = false;
+								}
+								else
+									currentLayer = m_Doc->addLayer(UnicodeParsedString(oc->getName()), false);
+// POPPLER_VERSION appeared in 0.19.0 first
+#ifdef POPPLER_VERSION
+								if ((oc->getViewState() == OptionalContentGroup::ocUsageOn) || (oc->getViewState() == OptionalContentGroup::ocUsageUnset))
+									m_Doc->setLayerVisible(currentLayer, true);
+								else
+									m_Doc->setLayerVisible(currentLayer, false);
+								if ((oc->getPrintState() == OptionalContentGroup::ocUsageOn) || (oc->getPrintState() == OptionalContentGroup::ocUsageUnset))
+									m_Doc->setLayerPrintable(currentLayer, true);
+								else
+									m_Doc->setLayerPrintable(currentLayer, false);
+#else
+								if (oc->getState() == OptionalContentGroup::On)
+								{
+									m_Doc->setLayerVisible(currentLayer, true);
+									m_Doc->setLayerPrintable(currentLayer, true);
+								}
+								else
+								{
+									m_Doc->setLayerVisible(currentLayer, false);
+									m_Doc->setLayerPrintable(currentLayer, false);
+								}
+#endif
+								oc->setState(OptionalContentGroup::Off);
+							}
+							dev->layersSetByOCG = true;
+						}
 						Object info;
 						pdfDoc->getDocInfo(&info);
 						if (info.isDict())
@@ -412,10 +434,47 @@
 							}
 						}
 						info.free();
-						pdfDoc->displayPages(dev, firstPage, lastPage, hDPI, vDPI, rotate, useMediaBox, crop, printing);
+						for (int pp = 0; pp < lastPage; pp++)
+						{
+							if (firstPg)
+								firstPg = false;
+							else
+								m_Doc->addPage(pp);
+							m_Doc->currentPage()->setInitialHeight(pdfDoc->getPageMediaHeight(pp + 1));
+							m_Doc->currentPage()->setInitialWidth(pdfDoc->getPageMediaWidth(pp + 1));
+							m_Doc->currentPage()->setHeight(pdfDoc->getPageMediaHeight(pp + 1));
+							m_Doc->currentPage()->setWidth(pdfDoc->getPageMediaWidth(pp + 1));
+							m_Doc->currentPage()->MPageNam = CommonStrings::trMasterPageNormal;
+							m_Doc->currentPage()->m_pageSize = "Custom";
+							m_Doc->setPageSize("Custom");
+							m_Doc->reformPages(true);
+							if (hasOcg)
+							{
+								for (int a = 0; a < ocgGroups.count(); a++)
+								{
+									OptionalContentGroup *oc = ocgGroups[a];
+									m_Doc->setActiveLayer(UnicodeParsedString(oc->getName()));
+									currentLayer = m_Doc->activeLayer();
+									oc->setState(OptionalContentGroup::On);
+									pdfDoc->displayPage(dev, pp + 1, hDPI, vDPI, rotate, useMediaBox, crop, printing);
+									oc->setState(OptionalContentGroup::Off);
+								}
+							}
+							else
+								pdfDoc->displayPage(dev, pp + 1, hDPI, vDPI, rotate, useMediaBox, crop, printing);
+						}
 					}
 					else
+					{
+						if (hasOcg)
+						{
+							for (int a = 0; a < ocgGroups.count(); a++)
+							{
+								ocgGroups[a]->setState(OptionalContentGroup::On);
+							}
+						}
 						pdfDoc->displayPage(dev, firstPage, hDPI, vDPI, rotate, useMediaBox, crop, printing);
+					}
 				}
 				delete dev;
 			}

Modified: trunk/Scribus/scribus/plugins/import/pdf/slaoutput.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17448&path=/trunk/Scribus/scribus/plugins/import/pdf/slaoutput.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/import/pdf/slaoutput.cpp (original)
+++ trunk/Scribus/scribus/plugins/import/pdf/slaoutput.cpp Mon Apr 16 20:01:20 2012
@@ -32,9 +32,8 @@
 	xref = NULL;
 	m_fontEngine = 0;
 	m_font = 0;
-	firstPage = true;
-	pagecount = 1;
 	updateGUICounter = 0;
+	layersSetByOCG = false;
 }
 
 SlaOutputDev::~SlaOutputDev()
@@ -51,8 +50,6 @@
 	xref = xrefA;
 	catalog = catA;
 	pdfDoc = doc;
-	firstPage = true;
-	pagecount = 1;
 	updateGUICounter = 0;
 	m_fontEngine = new SplashFontEngine(
 #if HAVE_T1LIB_H
@@ -66,27 +63,8 @@
 	true);
 }
 
-void SlaOutputDev::startPage(int pageNum, GfxState *state)
-{
-//    qDebug() << "starting page" << pageNum;
-	if (firstPage)
-		firstPage = false;
-	else
-	{
-		if (importerFlags & LoadSavePlugin::lfCreateDoc)
-		{
-			m_doc->addPage(pagecount);
-			m_doc->currentPage()->setInitialHeight(state->getPageHeight());
-			m_doc->currentPage()->setInitialWidth(state->getPageWidth());
-			m_doc->currentPage()->setHeight(state->getPageHeight());
-			m_doc->currentPage()->setWidth(state->getPageWidth());
-			m_doc->currentPage()->MPageNam = CommonStrings::trMasterPageNormal;
-			m_doc->currentPage()->m_pageSize = "Custom";
-			m_doc->view()->addPage(pagecount, true);
-			pagecount++;
-		}
-	}
- //   qDebug() << "page size =" << pageSize;
+void SlaOutputDev::startPage(int, GfxState *)
+{
 }
 
 void SlaOutputDev::endPage()
@@ -1404,6 +1382,8 @@
 	{
 		if (QString(name) == "Layer")		// Handle Adobe Illustrator Layer command
 		{
+			if (layersSetByOCG)
+				return;
 			Object obj;
 			QString lName = QString("Layer_%1").arg(layerNum + 1);
 			if (properties->lookup((char*)"Title", &obj))

Modified: trunk/Scribus/scribus/plugins/import/pdf/slaoutput.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17448&path=/trunk/Scribus/scribus/plugins/import/pdf/slaoutput.h
==============================================================================
--- trunk/Scribus/scribus/plugins/import/pdf/slaoutput.h (original)
+++ trunk/Scribus/scribus/plugins/import/pdf/slaoutput.h Mon Apr 16 20:01:20 2012
@@ -83,7 +83,7 @@
 	virtual GBool useTilingPatternFill() { return gTrue; }
 	virtual GBool useShadedFills(int type) { return type <= 7; }
 	virtual GBool useFillColorStop() { return gTrue; }
-	virtual void startPage(int pageNum, GfxState *state);
+	virtual void startPage(int, GfxState *);
 	virtual void endPage();
 	// grapics state
 	virtual void saveState(GfxState *state);
@@ -165,6 +165,7 @@
 	virtual void endType3Char(GfxState * /*state*/);
 	virtual void type3D0(GfxState * /*state*/, double /*wx*/, double /*wy*/);
 	virtual void type3D1(GfxState * /*state*/, double /*wx*/, double /*wy*/, double /*llx*/, double /*lly*/, double /*urx*/, double /*ury*/);
+	bool layersSetByOCG;
 
 private:
 	void getPenState(GfxState *state);
@@ -174,7 +175,6 @@
 	void applyMask(PageItem *ite);
 	void pushGroup(QString maskName = "", GBool forSoftMask = gFalse, GBool alpha = gFalse, bool inverted = false);
 	bool pathIsClosed;
-	bool firstLayer;
 	QString CurrColorFill;
 	QString CurrColorStroke;
 	Qt::PenCapStyle PLineEnd;
@@ -220,9 +220,8 @@
 	int grStackDepth;
 	int layerNum;
 	int currentLayer;
+	bool firstLayer;
 	int importerFlags;
-	bool firstPage;
-	int pagecount;
 	int updateGUICounter;
 	XRef *xref;		// xref table for current document
 	PDFDoc *pdfDoc;




More information about the scribus-commit mailing list