r18004 by fschmid - PDF-Import: implemented cropping the imported page area to the different pdf boxes similar to what Inkscape does.

scribus-commit scribus-commit at lists.scribus.net
Sat Dec 29 11:18:52 UTC 2012


Author: fschmid
Date: Sat Dec 29 11:18:52 2012
New Revision: 18004

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=18004
Log:
PDF-Import: implemented cropping the imported page area to the different pdf boxes similar to what Inkscape does.

Modified:
    trunk/Scribus/scribus/plugins/import/pdf/importpdf.cpp
    trunk/Scribus/scribus/plugins/import/pdf/importpdf.h
    trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.cpp
    trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.h
    trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.ui
    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=18004&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 Sat Dec 29 11:18:52 2012
@@ -432,7 +432,23 @@
 				double vDPI = 72.0;
 				int firstPage = 1;
 				int lastPage = pdfDoc->getNumPages();
-				if (((interactive) || (importerFlags & LoadSavePlugin::lfCreateDoc)) && (lastPage > 1))
+				GBool useMediaBox = gTrue;
+				GBool crop = gFalse;
+				GBool printing = gFalse;
+				PDFRectangle *mediaBox = pdfDoc->getPage(1)->getMediaBox();
+				QRectF mediaRect = QRectF(QPointF(mediaBox->x1, mediaBox->y1), QPointF(mediaBox->x2, mediaBox->y2)).normalized();
+				bool boxesAreDifferent = false;
+				if (getCBox(Crop_Box, 1) != mediaRect)
+					boxesAreDifferent = true;
+				else if (getCBox(Trim_Box, 1) != mediaRect)
+					boxesAreDifferent = true;
+				else if (getCBox(Bleed_Box, 1) != mediaRect)
+					boxesAreDifferent = true;
+				else if (getCBox(Art_Box, 1) != mediaRect)
+					boxesAreDifferent = true;
+				bool cropped = false;
+				int contentRect = Media_Box;
+				if (((interactive) || (importerFlags & LoadSavePlugin::lfCreateDoc)) && ((lastPage > 1) || boxesAreDifferent))
 				{
 					if (progressDialog)
 						progressDialog->hide();
@@ -450,6 +466,12 @@
 						return false;
 					}
 					pageString = optImp->getPagesString();
+					int cb = optImp->getCropBox();
+					if (cb > Media_Box)
+					{
+						cropped = true;
+						contentRect = cb;
+					}
 					delete optImp;
 					qApp->changeOverrideCursor(QCursor(Qt::WaitCursor));
 					if (progressDialog)
@@ -527,9 +549,6 @@
 							}
 						}
 					}
-					GBool useMediaBox = gTrue;
-					GBool crop = gFalse;
-					GBool printing = gFalse;
 					bool rotated = false;
 					dev->startDoc(pdfDoc, pdfDoc->getXRef(), pdfDoc->getCatalog());
 					int rotate = pdfDoc->getPageRotate(firstPage);
@@ -610,17 +629,18 @@
 							}
 						}
 						info.free();
-						if (crop)
+						if (cropped)
 						{
+							QRectF crBox = getCBox(contentRect, pageNs[0]);
 							if (rotated)
 							{
-								m_Doc->setPageWidth(pdfDoc->getPageCropHeight(pageNs[0]));
-								m_Doc->setPageHeight(pdfDoc->getPageCropWidth(pageNs[0]));
+								m_Doc->setPageWidth(crBox.height());
+								m_Doc->setPageHeight(crBox.width());
 							}
 							else
 							{
-								m_Doc->setPageHeight(pdfDoc->getPageCropHeight(pageNs[0]));
-								m_Doc->setPageWidth(pdfDoc->getPageCropWidth(pageNs[0]));
+								m_Doc->setPageHeight(crBox.height());
+								m_Doc->setPageWidth(crBox.width());
 							}
 						}
 						else
@@ -637,21 +657,6 @@
 							}
 						}
 						m_Doc->setPageSize("Custom");
-					/*	PDFRectangle *mediaBox = pdfDoc->getPage(1)->getMediaBox();
-						PDFRectangle *cropBox = pdfDoc->getPage(1)->getCropBox();
-						PDFRectangle *trimBox = pdfDoc->getPage(1)->getTrimBox();
-						PDFRectangle *bleedBox = pdfDoc->getPage(1)->getBleedBox();
-						PDFRectangle *artBox = pdfDoc->getPage(1)->getArtBox();
-						QRectF mediaRect = QRectF(QPointF(mediaBox->x1, mediaBox->y1), QPointF(mediaBox->x2, mediaBox->y2)).normalized();
-						QRectF cropRect = QRectF(QPointF(cropBox->x1, cropBox->y1), QPointF(cropBox->x2, cropBox->y2)).normalized();
-						QRectF trimRect = QRectF(QPointF(trimBox->x1, trimBox->y1), QPointF(trimBox->x2, trimBox->y2)).normalized();
-						QRectF bleedRect = QRectF(QPointF(bleedBox->x1, bleedBox->y1), QPointF(bleedBox->x2, bleedBox->y2)).normalized();
-						QRectF artRect = QRectF(QPointF(artBox->x1, artBox->y1), QPointF(artBox->x2, artBox->y2)).normalized();
-						qDebug() << "Media Box" << mediaRect << "Area" << mediaRect.width() * mediaRect.height();
-						qDebug() << "Crop Box" << cropRect << "Area" << cropRect.width() * cropRect.height();
-						qDebug() << "Trim Box" << trimRect << "Area" << trimRect.width() * trimRect.height();
-						qDebug() << "Bleed Box" << bleedRect << "Area" << bleedRect.width() * bleedRect.height();
-						qDebug() << "Art Box" << artRect << "Area" << artRect.width() * artRect.height(); */
 						m_Doc->pdfOptions().PresentVals.clear();
 						for (uint ap = 0; ap < pageNs.size(); ++ap)
 						{
@@ -661,21 +666,26 @@
 								firstPg = false;
 							else
 								m_Doc->addPage(ap);
-							if (crop)
+							QRectF crBox = getCBox(contentRect, pp);
+							if (cropped)
 							{
 								if (rotated)
 								{
-									m_Doc->currentPage()->setInitialWidth(pdfDoc->getPageCropHeight(pp));
-									m_Doc->currentPage()->setInitialHeight(pdfDoc->getPageCropWidth(pp));
-									m_Doc->currentPage()->setWidth(pdfDoc->getPageCropHeight(pp));
-									m_Doc->currentPage()->setHeight(pdfDoc->getPageCropWidth(pp));
+									m_Doc->currentPage()->setInitialWidth(crBox.height());
+									m_Doc->currentPage()->setInitialHeight(crBox.width());
+									m_Doc->currentPage()->setWidth(crBox.height());
+									m_Doc->currentPage()->setHeight(crBox.width());
+									dev->cropOffsetX = crBox.y();
+									dev->cropOffsetY = crBox.x();
 								}
 								else
 								{
-									m_Doc->currentPage()->setInitialHeight(pdfDoc->getPageCropHeight(pp));
-									m_Doc->currentPage()->setInitialWidth(pdfDoc->getPageCropWidth(pp));
-									m_Doc->currentPage()->setHeight(pdfDoc->getPageCropHeight(pp));
-									m_Doc->currentPage()->setWidth(pdfDoc->getPageCropWidth(pp));
+									m_Doc->currentPage()->setInitialHeight(crBox.height());
+									m_Doc->currentPage()->setInitialWidth(crBox.width());
+									m_Doc->currentPage()->setHeight(crBox.height());
+									m_Doc->currentPage()->setWidth(crBox.width());
+									dev->cropOffsetX = crBox.x();
+									dev->cropOffsetY = crBox.y();
 								}
 							}
 							else
@@ -709,10 +719,18 @@
 								//	pdfDoc->displayPage(dev, pp + 1, hDPI, vDPI, rotate, useMediaBox, crop, printing);
 								//	oc->setState(OptionalContentGroup::Off);
 								}
-								pdfDoc->displayPage(dev, pp, hDPI, vDPI, rotate, useMediaBox, crop, printing, NULL, NULL, dev->annotations_callback, dev);
+								if (cropped)
+									pdfDoc->displayPageSlice(dev, pp, hDPI, vDPI, rotate, useMediaBox, crop, printing, crBox.x(), crBox.y(), crBox.width(), crBox.height(), NULL, NULL, dev->annotations_callback, dev);
+								else
+									pdfDoc->displayPage(dev, pp, hDPI, vDPI, rotate, useMediaBox, crop, printing, NULL, NULL, dev->annotations_callback, dev);
 							}
 							else
-								pdfDoc->displayPage(dev, pp, hDPI, vDPI, rotate, useMediaBox, crop, printing, NULL, NULL, dev->annotations_callback, dev);
+							{
+								if (cropped)
+									pdfDoc->displayPageSlice(dev, pp, hDPI, vDPI, rotate, useMediaBox, crop, printing, crBox.x(), crBox.y(), crBox.width(), crBox.height(), NULL, NULL, dev->annotations_callback, dev);
+								else
+									pdfDoc->displayPage(dev, pp, hDPI, vDPI, rotate, useMediaBox, crop, printing, NULL, NULL, dev->annotations_callback, dev);
+							}
 							PDFPresentationData ef;
 							Object trans;
 							Object *transi = pdfDoc->getPage(pp)->getTrans(&trans);
@@ -875,7 +893,7 @@
 	return true;
 }
 
-QImage PdfPlug::readPreview(int pgNum, int width, int height)
+QImage PdfPlug::readPreview(int pgNum, int width, int height, int box)
 {
 	if (!m_pdfDoc)
 		return QImage();
@@ -916,8 +934,35 @@
 	QImage image = tmpimg.copy();
 	image.setText("XSize", QString("%1").arg(w));
 	image.setText("YSize", QString("%1").arg(h));
+	if (box > Media_Box)
+	{
+		QRectF cRect = getCBox(box, pgNum);
+		QPainter pp;
+		pp.begin(&image);
+		pp.setBrush(Qt::NoBrush);
+		pp.setPen(QPen(Qt::red, 1.0));
+		pp.translate(0, bh);
+		pp.scale(scale, -scale);
+		pp.drawRect(cRect);
+		pp.end();
+	}
 	delete dev;
 	return image;
+}
+
+QRectF PdfPlug::getCBox(int box, int pgNum)
+{
+	PDFRectangle *cBox;
+	if (box == Bleed_Box)
+		cBox = m_pdfDoc->getPage(pgNum)->getBleedBox();
+	else if (box == Trim_Box)
+		cBox = m_pdfDoc->getPage(pgNum)->getTrimBox();
+	else if (box == Crop_Box)
+		cBox = m_pdfDoc->getPage(pgNum)->getCropBox();
+	else if (box == Art_Box)
+		cBox = m_pdfDoc->getPage(pgNum)->getArtBox();
+	QRectF cRect = QRectF(QPointF(cBox->x1, cBox->y1), QPointF(cBox->x2, cBox->y2)).normalized();
+	return cRect;
 }
 
 QString PdfPlug::UnicodeParsedString(GooString *s1)

Modified: trunk/Scribus/scribus/plugins/import/pdf/importpdf.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18004&path=/trunk/Scribus/scribus/plugins/import/pdf/importpdf.h
==============================================================================
--- trunk/Scribus/scribus/plugins/import/pdf/importpdf.h (original)
+++ trunk/Scribus/scribus/plugins/import/pdf/importpdf.h Sat Dec 29 11:18:52 2012
@@ -68,10 +68,19 @@
 	 */
 	bool import(QString fn, const TransactionSettings& trSettings, int flags, bool showProgress = true);
 	QImage readThumbnail(QString fn);
-	QImage readPreview(int pgNum, int width, int height);
+	QImage readPreview(int pgNum, int width, int height, int box);
+	enum PDF_Box_Type
+	{
+		Media_Box	= 0,
+		Bleed_Box	= 1,
+		Trim_Box	= 2,
+		Crop_Box	= 3,
+		Art_Box		= 4
+	};
 
 private:
 	bool convert(QString fn);
+	QRectF getCBox(int box, int pgNum);
 	QString UnicodeParsedString(GooString *s1);
 	
 	QList<PageItem*> Elements;

Modified: trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18004&path=/trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.cpp (original)
+++ trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.cpp Sat Dec 29 11:18:52 2012
@@ -18,7 +18,7 @@
 	m_plugin = NULL;
 	m_maxPage = 0;
 	m_resized = false;
-	ui->cropGroup->hide();	// for now as functionality is not implemented yet
+//	ui->cropGroup->hide();	// for now as functionality is not implemented yet
 }
 
 PdfImportOptions::~PdfImportOptions()
@@ -49,6 +49,14 @@
 	return ui->pageRangeString->text();
 }
 
+int PdfImportOptions::getCropBox()
+{
+	int ret = 0;
+	if (ui->cropGroup->isChecked())
+		ret = ui->cropBox->currentIndex();
+	return ret;
+}
+
 void PdfImportOptions::setUpOptions(QString fileName, int actPage, int numPages, bool interact, PdfPlug* plug)
 {
 	m_plugin = plug;
@@ -77,8 +85,14 @@
 	connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(updateFromSpinBox(int)));
 	connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(updatePreview(int)));
 	connect(ui->pageSelectButton, SIGNAL(clicked()), this, SLOT(createPageNumberRange()));
+	connect(ui->cropGroup, SIGNAL(clicked()), this, SLOT(updateFromCrop()));
+	connect(ui->cropBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateFromCrop()));
 }
 
+void PdfImportOptions::updateFromCrop()
+{
+	updatePreview(ui->pgSelect->getCurrentPage());
+}
 
 void PdfImportOptions::updateFromSpinBox(int pg)
 {
@@ -89,7 +103,10 @@
 {
 	if (m_plugin)
 	{
-		QImage img = m_plugin->readPreview(pg, ui->previewWidget->width(), ui->previewWidget->height());
+		int cb = 0;
+		if (ui->cropGroup->isChecked())
+			cb =  ui->cropBox->currentIndex();
+		QImage img = m_plugin->readPreview(pg, ui->previewWidget->width(), ui->previewWidget->height(), cb);
 		ui->previewWidget->setPixmap(QPixmap::fromImage(img));
 		disconnect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(updateFromSpinBox(int)));
 		disconnect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(updatePreview(int)));

Modified: trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18004&path=/trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.h
==============================================================================
--- trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.h (original)
+++ trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.h Sat Dec 29 11:18:52 2012
@@ -23,12 +23,14 @@
 	~PdfImportOptions();
 	void setUpOptions(QString fileName, int actPage, int numPages, bool interact, PdfPlug* plug);
 	QString getPagesString();
+	int getCropBox();
 	void paintEvent(QPaintEvent *e);
 
 protected:
 	void resizeEvent(QResizeEvent *e);
 
 public slots:
+	void updateFromCrop();
 	void updateFromSpinBox(int pg);
 	void updatePreview(int pg);
 	void createPageNumberRange();

Modified: trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.ui
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18004&path=/trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.ui
==============================================================================
--- trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.ui (original)
+++ trunk/Scribus/scribus/plugins/import/pdf/pdfimportoptions.ui Sat Dec 29 11:18:52 2012
@@ -144,7 +144,7 @@
          </property>
          <layout class="QVBoxLayout" name="verticalLayout_2">
           <item>
-           <widget class="QComboBox" name="comboBox">
+           <widget class="QComboBox" name="cropBox">
             <item>
              <property name="text">
               <string>Media Box</string>

Modified: trunk/Scribus/scribus/plugins/import/pdf/slaoutput.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18004&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 Sat Dec 29 11:18:52 2012
@@ -224,6 +224,8 @@
 	m_font = 0;
 	updateGUICounter = 0;
 	layersSetByOCG = false;
+	cropOffsetX = 0;
+	cropOffsetY = 0;
 }
 
 SlaOutputDev::~SlaOutputDev()
@@ -305,8 +307,8 @@
 {
 	SlaOutputDev *dev = (SlaOutputDev*)user_data;
 	PDFRectangle *box = annota->getRect();
-	double xCoor = dev->m_doc->currentPage()->xOffset() + box->x1;
-	double yCoor = dev->m_doc->currentPage()->yOffset() + dev->m_doc->currentPage()->height() - box->y2;
+	double xCoor = dev->m_doc->currentPage()->xOffset() + box->x1 - dev->cropOffsetX;
+	double yCoor = dev->m_doc->currentPage()->yOffset() + dev->m_doc->currentPage()->height() - box->y2 + dev->cropOffsetY;
 	double width = box->x2 - box->x1;
 	double height = box->y2 - box->y1;
 	bool retVal = true;

Modified: trunk/Scribus/scribus/plugins/import/pdf/slaoutput.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18004&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 Sat Dec 29 11:18:52 2012
@@ -250,6 +250,8 @@
 	//----- links
 	virtual void processLink(AnnotLink * /*link*/) { qDebug() << "Draw Link"; }
 	bool layersSetByOCG;
+	double cropOffsetX;
+	double cropOffsetY;
 
 private:
 	void getPenState(GfxState *state);




More information about the scribus-commit mailing list