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 /***************************************************************************
8 							 -------------------
9 	begin                : Sun Jan 11 2015
10 	copyright            : (C) 2015 by Franz Schmid
11 	email                : Franz.Schmid@altmuehlnet.de
12  ***************************************************************************/
13 #ifndef IMPORTEMF_H
14 #define IMPORTEMF_H
15 
16 
17 #include "pluginapi.h"
18 #include "pageitem.h"
19 #include "sccolor.h"
20 #include "fpointarray.h"
21 #include "commonstrings.h"
22 #include <QList>
23 #include <QTransform>
24 #include <QMultiMap>
25 #include <QtGlobal>
26 #include <QObject>
27 #include <QStack>
28 #include <QString>
29 #include <QRect>
30 
31 class MultiProgressDialog;
32 class ScribusDoc;
33 class Selection;
34 class TransactionSettings;
35 
36 class emfStyle
37 {
38 public:
emfStyle()39 	emfStyle() :
40 		styType(0),
41 		penStyle(Qt::SolidLine),
42 		penCap(Qt::RoundCap),
43 		penJoin(Qt::RoundJoin),
44 		penWidth(0.0),
45 		dashArray(),
46 		dashOffset(0.0),
47 		brushStyle(0),
48 		hatchStyle(0),
49 		gradientStart(),
50 		gradientEnd(),
51 		gradientAngle(0),
52 		gradient(VGradient::linear),
53 		gradientPath(),
54 		hAlign(0),
55 		vAlign(0),
56 		fontUnit(0),
57 		verticalText(false),
58 		fontRotation(0.0),
59 		fontSize(12.0),
60 		fontName(""),
61 		penColor(CommonStrings::None),
62 		brushColor(CommonStrings::None),
63 		patternName(""),
64 		patternMode(0),
65 		fillTrans(0.0),
66 		penTrans(0.0),
67 		Coords(),
68 		MetaFile(false),
69 		imageType(0),
70 		imageWidth(0),
71 		imageHeight(0),
72 		imagePixelFormat(0),
73 		imageData()
74 	{
75 		gradientPath.resize(0);
76 		gradientPath.svgInit();
77 		imageData.resize(0);
78 		Coords.resize(0);
79 		Coords.svgInit();
80 	}
81 	quint32          styType;
82 	Qt::PenStyle     penStyle;
83 	Qt::PenCapStyle  penCap;
84 	Qt::PenJoinStyle penJoin;
85 	double           penWidth;
86 	QVector<double>  dashArray;
87 	double           dashOffset;
88 	quint32          brushStyle;
89 	quint32          hatchStyle;
90 	QPointF          gradientStart;
91 	QPointF          gradientEnd;
92 	double           gradientAngle;
93 	VGradient        gradient;
94 	FPointArray      gradientPath;
95 	quint32          hAlign;
96 	quint32          vAlign;
97 	quint32          fontUnit;
98 	bool             verticalText;
99 	double           fontRotation;
100 	double           fontSize;
101 	QString          fontName;
102 	QString          penColor;
103 	QString          brushColor;
104 	QString          patternName;
105 	quint32          patternMode;
106 	double           fillTrans;
107 	double           penTrans;
108 	FPointArray      Coords;
109 	bool             MetaFile;
110 	quint32          imageType;
111 	qint32           imageWidth;
112 	qint32           imageHeight;
113 	quint32          imagePixelFormat;
114 	QByteArray       imageData;
115 };
116 
117 class EmfPlug : public QObject
118 {
119 	Q_OBJECT
120 
121 public:
122 	/*!
123 	\author Franz Schmid
124 	\date
125 	\brief Create the EMF importer window.
126 	\param fName QString
127 	\param flags combination of loadFlags
128 	\param showProgress if progress must be displayed
129 	\retval EmfPlug plugin
130 	*/
131 	EmfPlug( ScribusDoc* doc, int flags );
132 	~EmfPlug();
133 
134 	/*!
135 	\author Franz Schmid
136 	\date
137 	\brief Perform import.
138 	\param fn QString
139 	\param trSettings undo transaction settings
140 	\param flags combination of loadFlags
141 	\param showProgress if progress must be displayed
142 	\retval bool true if import was ok
143 	 */
144 	bool        import(const QString& fn, const TransactionSettings& trSettings, int flags, bool showProgress = true);
145 	QImage      readThumbnail(const QString& fn);
146 
147 private:
148 	void        parseHeader(const QString& fName, double &x, double &y, double &b, double &h);
149 	bool        convert(const QString& fn);
150 	// Common functions
151 	bool        checkClip(FPointArray &clip);
152 	void        aligntoQuadWord(QDataStream &ds);
153 	double      convertDevice2Pts(double in);
154 	QPointF     convertDevice2Pts(QPointF in);
155 	double      convertLogical2Pts(double in);
156 	QPointF     convertLogical2Pts(QPointF in);
157 	void        createPatternFromDIB(const QImage& img, quint32 brID);
158 	void        getPolyInfo(QDataStream &ds, QRectF &bounds, quint32 &count);
159 	FPointArray getPolyPoints(QDataStream &ds, quint32 count, bool length, bool closed);
160 	QPointF     getPoint(QDataStream &ds, bool size);
161 	quint32     getColor(QDataStream &ds);
162 	void        setWTransform(const QTransform& mm, quint32 how);
163 	QPointF     intersectBoundingRect(PageItem *item, QLineF gradientVector);
164 	void        finishItem(PageItem* ite, bool fill = true);
165 	void        invalidateClipGroup();
166 	void        createClipGroup();
167 	// Functions for normal EMF Files
168 	void        handleComment(QDataStream &ds);
169 	void        handleEllipse(QDataStream &ds);
170 	void        handleRectangle(QDataStream &ds);
171 	void        handleRoundRect(QDataStream &ds);
172 	void        handlePolyBezierTo(QDataStream &ds, bool size);
173 	void        handlePolylineTo(QDataStream &ds, bool size);
174 	void        handleLineTo(QDataStream &ds);
175 	void        handleArc(QDataStream &ds);
176 	void        handleArcTo(QDataStream &ds);
177 	void        handleChord(QDataStream &ds);
178 	void        handlePie(QDataStream &ds);
179 	void        handleSmallText(QDataStream &ds);
180 	void        handleText(QDataStream &ds, qint64 posi, bool size);
181 	void        handleImage(qint32 dstX, qint32 dstY, qint32 dstW, qint32 dstH, const QImage& img);
182 	void        handlePatternFill(qint32 dstX, qint32 dstY, qint32 dstW, qint32 dstH);
183 	QImage      handleDIB(QDataStream &ds, qint64 filePos, quint32 offBitH, quint32 sizeBitH, quint32 offBits, quint32 sizeBits);
184 	void        handleBezier(QDataStream &ds, bool size);
185 	void        handlePolygon(QDataStream &ds, bool size, bool fill);
186 	void        handlePolyPolygon(QDataStream &ds, bool size, bool fill);
187 	void        handlePenDef(quint32 penID, quint32 penStyle, quint32 penWidth, quint32 penColor);
188 	QString     handleColor(const QColor& col);
189 	void        handleFillRegion(QDataStream &ds);
190 	void        handleFrameRegion(QDataStream &ds);
191 	void        handleSetClipRegion(QDataStream &ds);
192 	// Functions for EMF+ Files
193 	void        handleEMFPlus(QDataStream &ds, quint32 dtaSize);
194 	void        handleEMPObject(QDataStream &ds, quint8 flagsH, quint8 flagsL, quint32 dataSize);
195 	quint32     handleEMPBrush(QDataStream &ds, quint16 id, bool first, bool cont, quint32 dataSize);
196 	void        handleEMPPen(QDataStream &ds, quint16 id);
197 	void        handleEMPPath(QDataStream &ds, quint16 id);
198 	void        handleEMPRegion(QDataStream &ds, quint16 id);
199 	quint32     handleEMPImage(QDataStream &ds, quint16 id, bool first, bool cont, quint32 dataSize);
200 	quint32     getImageData(QDataStream &ds, quint16 id, bool first, bool cont, quint32 dataSize, emfStyle &sty);
201 	void        handleEMPFont(QDataStream &ds, quint16 id);
202 	void        handleEMPSFormat(QDataStream &ds, quint16 id);
203 	void        handleEMPLineCap(QDataStream &ds, quint16 id);
204 	void        handleEMFPFillClosedCurve(QDataStream &ds, quint8 flagsL);
205 	void        handleEMFPFillEllipse(QDataStream &ds, quint8 flagsL);
206 	void        handleEMFPFillPath(QDataStream &ds, quint8 flagsL, quint8 flagsH);
207 	void        handleEMFPFillPie(QDataStream &ds, quint8 flagsL);
208 	void        handleEMFPFillPolygon(QDataStream &ds, quint8 flagsL);
209 	void        handleEMFPFillRects(QDataStream &ds, quint8 flagsL);
210 	void        handleEMFPFillRegion(QDataStream &ds, quint8 flagsL, quint8 flagsH);
211 	void        handleEMFPDrawArc(QDataStream &ds, quint8 flagsL, quint8 flagsH);
212 	void        handleEMFPDrawBezier(QDataStream &ds, quint8 flagsL, quint8 flagsH);
213 	void        handleEMFPDrawClosedCurve(QDataStream &ds, quint8 flagsL, quint8 flagsH);
214 	void        handleEMFPDrawCurve(QDataStream &ds, quint8 flagsL, quint8 flagsH);
215 	void        handleEMFPDrawEllipse(QDataStream &ds, quint8 flagsL, quint8 flagsH);
216 	void        handleEMFPDrawImage(QDataStream &ds, quint8 flagsL, quint8 flagsH);
217 	void        handleEMFPDrawImagePoints(QDataStream &ds, quint8 flagsL, quint8 flagsH);
218 	void        handleEMFPDrawLines(QDataStream &ds, quint8 flagsL, quint8 flagsH);
219 	void        handleEMFPDrawPath(QDataStream &ds, quint8 flagsH);
220 	void        handleEMFPDrawPie(QDataStream &ds, quint8 flagsL, quint8 flagsH);
221 	void        handleEMFPDrawRects(QDataStream &ds, quint8 flagsL, quint8 flagsH);
222 	void        handleEMFPDrawDriverString(QDataStream &ds, quint8 flagsL, quint8 flagsH);
223 	void        handleEMFPDrawString(QDataStream &ds, quint8 flagsL, quint8 flagsH);
224 	void        handleEMFPSetClipRect(QDataStream &ds, quint8 flagsL);
225 	void        handleEMFPSetClipRegion(QDataStream &ds, quint8 flagsL, quint8 flagsH);
226 	void        handleEMFPSetClipPath(QDataStream &ds, quint8 flagsL, quint8 flagsH);
227 	void		handleEMFPSerializableObject(QDataStream &ds);
228 	void        getEMFPBrush(quint32 brushID, bool directBrush);
229 	void        getEMFPPen(quint32 penID);
230 	void        getEMFPFont(quint32 fontID);
231 	void        getEMFPStringFormat(quint32 fontID);
232 	FPointArray getEMPPathData(QDataStream &ds);
233 	QPolygonF   getEMFPCurvePoints(QDataStream &ds, quint8 flagsL, quint32 count);
234 	QPolygonF   getEMFPRect(QDataStream &ds, bool size);
235 	QPointF     getEMFPPoint(QDataStream &ds, bool size);
236 	double      getEMFPDistance(QDataStream &ds, bool size);
237 	QPointF     convertEMFPLogical2Pts(QPointF in, quint16 unit);
238 	double      convertEMFPLogical2Pts(double in, quint16 unit);
239 	QPolygonF   gdip_open_curve_tangents(QPolygonF &points, double tension);
240 	QPolygonF   gdip_closed_curve_tangents(QPolygonF &points, double tension);
241 	void        append_curve(QPainterPath &path, QPolygonF &points, QPolygonF &tangents, bool type);
242 	void        GdipAddPathCurve(QPainterPath &path, QPolygonF &points, float tension);
243 	void        GdipAddPathClosedCurve(QPainterPath &path, QPolygonF &points, float tension);
244 	void        handleEMFPDrawImageData(QPointF p1, QPointF p2, QPointF p3, quint8 flagsH);
245 	QImage      getImageDataFromStyle(quint8 flagsH);
246 	struct dcState
247 	{
248 		quint32          m_mapMode;
249 		quint16          emfPlusUnit;
250 		quint32          textAlignment;
251 		quint32          hatchStyle;
252 		quint32          brushStyle;
253 		QPointF          gradientStart;
254 		QPointF          gradientEnd;
255 		double           gradientAngle;
256 		VGradient        gradient;
257 		QTransform       m_WorldMap;
258 		QTransform       m_WorldMapEMFP;
259 		bool             fillRule;
260 		bool             backgroundMode;
261 		bool		     arcDirection;
262 		bool             alphaOn;
263 		double           LineW;
264 		QVector<double>  dashArray;
265 		double           dashOffset;
266 		Qt::PenStyle     penStyle;
267 		Qt::PenCapStyle  penCap;
268 		Qt::PenJoinStyle penJoin;
269 		QString          CurrColorFill;
270 		QString          CurrColorStroke;
271 		QString          CurrColorText;
272 		QString          backColor;
273 		QString          fontName;
274 		QString          patternName;
275 		quint32          patternMode;
276 		double           fontSize;
277 		double           fontRotation;
278 		double           CurrStrokeTrans;
279 		double           CurrFillTrans;
280 		FPointArray      Coords;
281 		FPointArray      clipPath;
282 		FPointArray      gradientPath;
283 		QPointF          viewOrigin;
284 		QPointF          winOrigin;
285 		QPointF          currentPoint;
286 		QPointF          originEMFP;
287 		quint32          hAlign;
288 		quint32          vAlign;
289 		quint32          fontUnit;
290 		bool             verticalText;
291 		bool             clipValid;
292 	};
293 	QStack<dcState> dcStack;
294 	QHash<quint32, dcState> dcStackEMP;
295 	dcState currentDC;
296 	QHash<quint32, emfStyle> emfStyleMap;
297 	QHash<quint32, emfStyle> emfStyleMapEMP;
298 	QList<PageItem*> Elements;
299 	PageItem* clipGroup;
300 	double docWidth;
301 	double docHeight;
302 	double baseX, baseY;
303 	double docX;
304 	double docY;
305 	double dpiX;
306 	double dpiY;
307 	quint32 EmfPdpiX, EmfPdpiY;
308 	quint32 m_records;
309 	int recordCount;
310 	qint32 viewPextendX, viewPextendY;
311 	qint32 winPextendX, winPextendY;
312 	qint32 winOrigX, winOrigY;
313 	QRectF bBoxDev;
314 	QRectF bBoxMM;
315 	QStringList importedColors;
316 	QStringList importedPatterns;
317 	bool interactive;
318 	MultiProgressDialog * progressDialog;
319 	bool cancel;
320 	ScribusDoc* m_Doc;
321 	Selection* tmpSel;
322 	int importerFlags;
323 	QString baseFile;
324 	bool inPath;
325 	bool inEMFPlus;
326 	bool emfPlusDual;
327 	bool emfMixed;
328 	float emfPlusScale;
329 	bool SerializableObject_Valid;
330 	ScImageEffectList m_Effects;
331 	quint32 m_ObjSize;
332 	quint32 m_currObjSize;
333 	quint16 m_objID;
334 public slots:
cancelRequested()335 	void cancelRequested() { cancel = true; }
336 };
337 
338 #endif
339