1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 #include <QDebug>
8 #include <QDir>
9 #include <QFile>
10 #include <QFileInfo>
11 
12 #include "util_ghostscript.h"
13 #include "scpaths.h"
14 #include "scribuscore.h"
15 #include "scimgdataloader_pdf.h"
16 
17 #ifdef HAVE_PODOFO
18 #include <podofo/podofo.h>
19 #endif
20 
21 
ScImgDataLoader_PDF()22 ScImgDataLoader_PDF::ScImgDataLoader_PDF()
23 {
24 	initSupportedFormatList();
25 }
26 
initSupportedFormatList()27 void ScImgDataLoader_PDF::initSupportedFormatList()
28 {
29 	m_supportedFormats.clear();
30 	m_supportedFormats.append( "pdf" );
31 }
32 
loadEmbeddedProfile(const QString & fn,int)33 void ScImgDataLoader_PDF::loadEmbeddedProfile(const QString& fn, int /* page */)
34 {
35 	m_embeddedProfile.resize(0);
36 	m_profileComponents = 0;
37 }
38 
loadPicture(const QString & fn,int page,int gsRes,bool)39 bool ScImgDataLoader_PDF::loadPicture(const QString& fn, int page, int gsRes, bool /*thumbnail*/)
40 {
41 	QStringList args;
42 	if (!QFile::exists(fn))
43 		return false;
44 	QString tmpFile = QDir::toNativeSeparators(ScPaths::tempFileDir() + "sc.png");
45 	QString picFile = QDir::toNativeSeparators(fn);
46 	float xres = gsRes;
47 	float yres = gsRes;
48 
49 	initialize();
50 	m_imageInfoRecord.actualPageNumber = page;
51 
52 	m_imageInfoRecord.type = ImageTypePDF;
53 	m_imageInfoRecord.exifDataValid = false;
54 	m_imageInfoRecord.numberOfPages = 99; // FIXME
55 #ifdef HAVE_PODOFO
56 	try
57 	{
58 		PoDoFo::PdfError::EnableDebug( false );
59 		PoDoFo::PdfError::EnableLogging( false );
60 		PoDoFo::PdfMemDocument doc( fn.toLocal8Bit().data() );
61 		m_imageInfoRecord.numberOfPages = doc.GetPageCount();
62 		if (page > m_imageInfoRecord.numberOfPages)
63 		{
64 			qDebug() << "Incorrect page number specified!";
65 			m_imageInfoRecord.actualPageNumber = page = 0;
66 		}
67 	}
68 	catch(PoDoFo::PdfError& e)
69 	{
70 		qDebug() << "PoDoFo error while reading page count!";
71 		e.PrintErrorMsg();
72 	}
73 #endif
74 	args.append("-r"+QString::number(gsRes));
75 	args.append("-sOutputFile="+tmpFile);
76 	args.append("-dFirstPage=" + QString::number(qMax(1, page)));
77 	args.append("-dLastPage=" + QString::number(qMax(1, page)));
78 	args.append("-dUseArtBox");
79 	args.append(picFile);
80 //	qDebug() << "scimgdataloader_pdf:" << args;
81 	int retg = callGS(args);
82 	if (retg == 0)
83 	{
84 		m_image.load(tmpFile);
85 		QFile::remove(tmpFile);
86 		if (!ScCore->havePNGAlpha())
87 		{
88 			for (int yi = 0; yi < m_image.height(); ++yi)
89 			{
90 				QRgb *s = (QRgb*)(m_image.scanLine( yi ));
91 				for (int xi = 0; xi < m_image.width(); ++xi )
92 				{
93 					if ((*s) == 0xffffffff)
94 						(*s) &= 0x00ffffff;
95 					s++;
96 				}
97 			}
98 		}
99 		m_imageInfoRecord.BBoxX = 0;
100 		m_imageInfoRecord.BBoxH = m_image.height();
101 		m_imageInfoRecord.xres = gsRes;
102 		m_imageInfoRecord.yres = gsRes;
103 		m_imageInfoRecord.colorspace = ColorSpaceRGB;
104 		m_image.setDotsPerMeterX ((int) (xres / 0.0254));
105 		m_image.setDotsPerMeterY ((int) (yres / 0.0254));
106 		m_pixelFormat = Format_BGRA_8;
107 		return true;
108 	}
109 	return false;
110 }
111 
preloadAlphaChannel(const QString & fn,int page,int gsRes,bool & hasAlpha)112 bool ScImgDataLoader_PDF::preloadAlphaChannel(const QString& fn, int page, int gsRes, bool& hasAlpha)
113 {
114 //	short resolutionunit = 0;
115 
116 	initialize();
117 	m_imageInfoRecord.actualPageNumber = page;
118 
119 	hasAlpha = false;
120 	QFileInfo fi = QFileInfo(fn);
121 	if (!fi.exists())
122 		return false;
123 	QString tmpFile = QDir::toNativeSeparators(ScPaths::tempFileDir() + "sc.png");
124 	QString picFile = QDir::toNativeSeparators(fn);
125 	QStringList args;
126 	args.append("-r"+QString::number(gsRes));
127 //	args.append("-sOutputFile=\""+tmpFile + "\"");
128 	args.append("-sOutputFile="+tmpFile);
129 	args.append("-dFirstPage=" + QString::number(qMax(1, page)));
130 	args.append("-dLastPage=" + QString::number(qMax(1, page)));
131 	args.append("-dUseArtBox");
132 //	args.append("\""+picFile+"\"");
133 	args.append(picFile);
134 //	qDebug() << "scimgdataloader_pdf(alpha):" << args;
135 	int retg = callGS(args);
136 	if (retg == 0)
137 	{
138 		m_image.load(tmpFile);
139 		QFile::remove(tmpFile);
140 		if (!ScCore->havePNGAlpha())
141 		{
142 			QRgb *s;
143 			for (int yi=0; yi < m_image.height(); ++yi)
144 			{
145 				s = (QRgb*)(m_image.scanLine( yi ));
146 				for (int xi=0; xi < m_image.width(); ++xi)
147 				{
148 					if ((*s) == 0xffffffff)
149 						(*s) &= 0x00ffffff;
150 					s++;
151 				}
152 			}
153 		}
154 		hasAlpha = true;
155 		return true;
156 	}
157 	return false;
158 }
159