~ubuntu-branches/ubuntu/maverick/scribus-ng/maverick-backports

« back to all changes in this revision

Viewing changes to scribus/plugins/import/ai/importai.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Oleksandr Moskalenko
  • Date: 2010-07-15 12:43:00 UTC
  • mfrom: (0.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100715124300-2u304r2rvy55vpkv
Tags: 1.3.7.dfsg~svn20100715-1
* Upstream svn. update.
* debian/scribus-ng.lintian: Updated overrides.
* debian/control: Updated standards version to 3.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include <QByteArray>
 
9
#include <QCursor>
 
10
#include <QDrag>
 
11
#include <QFile>
 
12
#include <QList>
 
13
#include <QMimeData>
 
14
#include <QRegExp>
 
15
#include <QStack>
 
16
#include <QStack>
 
17
#include <QTemporaryFile>
 
18
#include <QDebug>
 
19
 
 
20
#include <cmath>
 
21
#include <cstdlib>
 
22
#include <tiffio.h>
 
23
#include <zlib.h>
 
24
 
 
25
#include "commonstrings.h"
 
26
#include "customfdialog.h"
 
27
#include "importai.h"
 
28
#include "loadsaveplugin.h"
 
29
#include "missing.h"
 
30
#include "multiprogressdialog.h"
 
31
#include "prefscontext.h"
 
32
#include "prefsfile.h"
 
33
#include "prefsmanager.h"
 
34
#include "prefsmanager.h"
 
35
#include "prefstable.h"
 
36
#include "propertiespalette.h"
 
37
#include "rawimage.h"
 
38
#include "sccolorengine.h"
 
39
#include "scconfig.h"
 
40
#include "scclocale.h"
 
41
#include "scmimedata.h"
 
42
#include "scpaths.h"
 
43
#include "scpattern.h"
 
44
#include "scribus.h"
 
45
#include "scribusXml.h"
 
46
#include "scribuscore.h"
 
47
#include "scribusdoc.h"
 
48
#include "sctextstream.h"
 
49
#include "selection.h"
 
50
#include "text/specialchars.h"
 
51
#include "undomanager.h"
 
52
#include "util.h"
 
53
#include "util_color.h"
 
54
#include "util_file.h"
 
55
#include "util_formats.h"
 
56
#include "util_icon.h"
 
57
#include "util_math.h"
 
58
 
 
59
 
 
60
#ifdef HAVE_PODOFO
 
61
        #include <podofo/podofo.h>
 
62
#endif
 
63
 
 
64
extern SCRIBUS_API ScribusQApp * ScQApp;
 
65
 
 
66
AIPlug::AIPlug(ScribusDoc* doc, int flags)
 
67
{
 
68
        tmpSel=new Selection(this, false);
 
69
        m_Doc=doc;
 
70
        interactive = (flags & LoadSavePlugin::lfInteractive);
 
71
        progressDialog = NULL;
 
72
}
 
73
 
 
74
bool AIPlug::import(QString fNameIn, const TransactionSettings& trSettings, int flags, bool showProgress)
 
75
{
 
76
        QString fName = fNameIn;
 
77
        bool success = false;
 
78
        interactive = (flags & LoadSavePlugin::lfInteractive);
 
79
        importerFlags = flags;
 
80
        cancel = false;
 
81
        double x, y, b, h;
 
82
        bool ret = false;
 
83
        convertedPDF = false;
 
84
        CustColors.clear();
 
85
        QFileInfo fi = QFileInfo(fName);
 
86
//      QString ext = fi.suffix().toLower();
 
87
        if ( !ScCore->usingGUI() )
 
88
        {
 
89
                interactive = false;
 
90
                showProgress = false;
 
91
        }
 
92
        
 
93
/* Check if the file is an old style AI or one of the newer PDF wrapped ones */
 
94
        QFile fT(fName);
 
95
        if (fT.open(QIODevice::ReadOnly))
 
96
        {
 
97
                QByteArray tempBuf(9, ' ');
 
98
                fT.read(tempBuf.data(), 8);
 
99
                fT.close();
 
100
                if (tempBuf.startsWith("%PDF"))
 
101
                {
 
102
                        QFileInfo bF2(fName);
 
103
                        QString tmpFile = ScPaths::getTempFileDir()+ "/"+bF2.baseName()+"_tmp.ai";
 
104
                        if (!extractFromPDF(fName, tmpFile))
 
105
                                return false;
 
106
                        convertedPDF = true;
 
107
                        fName = tmpFile;
 
108
                }
 
109
        }
 
110
        QFile fT2(fName);
 
111
        if (fT2.open(QIODevice::ReadOnly))
 
112
        {
 
113
                QByteArray tempBuf(25, ' ');
 
114
                fT2.read(tempBuf.data(), 20);
 
115
                fT2.close();
 
116
                /* Illustrator CS files might be compressed
 
117
                        the compressed Data starts right after the "%AI12_CompressedData" comment
 
118
                        Compression is a simple zlib compression */
 
119
                if (tempBuf.startsWith("%AI12_CompressedData"))
 
120
                        decompressAIData(fName);
 
121
        }
 
122
        if ( showProgress )
 
123
        {
 
124
                ScribusMainWindow* mw=(m_Doc==0) ? ScCore->primaryMainWindow() : m_Doc->scMW();
 
125
                progressDialog = new MultiProgressDialog( tr("Importing: %1").arg(fi.fileName()), CommonStrings::tr_Cancel, mw );
 
126
                QStringList barNames, barTexts;
 
127
                barNames << "GI";
 
128
                barTexts << tr("Analyzing File:");
 
129
                QList<bool> barsNumeric;
 
130
                barsNumeric << false;
 
131
                progressDialog->addExtraProgressBars(barNames, barTexts, barsNumeric);
 
132
                progressDialog->setOverallTotalSteps(3);
 
133
                progressDialog->setOverallProgress(0);
 
134
                progressDialog->setProgress("GI", 0);
 
135
                progressDialog->show();
 
136
                connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelRequested()));
 
137
                qApp->processEvents();
 
138
        }
 
139
        else
 
140
                progressDialog = NULL;
 
141
/* Set default Page to size defined in Preferences */
 
142
        x = 0.0;
 
143
        y = 0.0;
 
144
        b = PrefsManager::instance()->appPrefs.PageWidth;
 
145
        h = PrefsManager::instance()->appPrefs.PageHeight;
 
146
        if (progressDialog)
 
147
        {
 
148
                progressDialog->setOverallProgress(1);
 
149
                qApp->processEvents();
 
150
        }
 
151
        parseHeader(fName, x, y, b, h);
 
152
        docX = x;
 
153
        docY = y;
 
154
        docWidth = b - x;
 
155
        docHeight = h - y;
 
156
        baseX = 0;
 
157
        baseY = 0;
 
158
        if (!interactive || (flags & LoadSavePlugin::lfInsertPage))
 
159
        {
 
160
                m_Doc->setPage(b-x, h-y, 0, 0, 0, 0, 0, 0, false, false);
 
161
                m_Doc->addPage(0);
 
162
                m_Doc->view()->addPage(0, true);
 
163
                baseX = 0;
 
164
                baseY = 0;
 
165
        }
 
166
        else
 
167
        {
 
168
                if (!m_Doc || (flags & LoadSavePlugin::lfCreateDoc))
 
169
                {
 
170
                        m_Doc=ScCore->primaryMainWindow()->doFileNew(b-x, h-y, 0, 0, 0, 0, 0, 0, false, false, 0, false, 0, 1, "Custom", true);
 
171
                        ScCore->primaryMainWindow()->HaveNewDoc();
 
172
                        ret = true;
 
173
                        baseX = 0;
 
174
                        baseY = 0;
 
175
                }
 
176
        }
 
177
        if (flags & LoadSavePlugin::lfCreateDoc)
 
178
        {
 
179
                m_Doc->documentInfo.setAuthor(docCreator);
 
180
                m_Doc->documentInfo.setPublisher(docOrganisation);
 
181
                m_Doc->documentInfo.setTitle(docTitle);
 
182
                m_Doc->documentInfo.setDate(docDate+" "+docTime);
 
183
        }
 
184
        if ((!ret) && (interactive))
 
185
        {
 
186
                baseX = m_Doc->currentPage()->xOffset();
 
187
                baseY = m_Doc->currentPage()->yOffset();
 
188
        }
 
189
        if ((ret) || (!interactive))
 
190
        {
 
191
                if (b-x > h-y)
 
192
                        m_Doc->PageOri = 1;
 
193
                else
 
194
                        m_Doc->PageOri = 0;
 
195
                m_Doc->m_pageSize = "Custom";
 
196
        }
 
197
        ColorList::Iterator it;
 
198
        for (it = CustColors.begin(); it != CustColors.end(); ++it)
 
199
        {
 
200
                if (!m_Doc->PageColors.contains(it.key()))
 
201
                        m_Doc->PageColors.insert(it.key(), it.value());
 
202
        }
 
203
        Elements.clear();
 
204
        FPoint minSize = m_Doc->minCanvasCoordinate;
 
205
        FPoint maxSize = m_Doc->maxCanvasCoordinate;
 
206
        FPoint cOrigin = m_Doc->view()->canvasOrigin();
 
207
        m_Doc->setLoading(true);
 
208
        m_Doc->DoDrawing = false;
 
209
        m_Doc->view()->updatesOn(false);
 
210
        m_Doc->scMW()->setScriptRunning(true);
 
211
        qApp->changeOverrideCursor(QCursor(Qt::WaitCursor));
 
212
        QString CurDirP = QDir::currentPath();
 
213
        QDir::setCurrent(fi.path());
 
214
        if (convert(fName))
 
215
        {
 
216
                tmpSel->clear();
 
217
                QDir::setCurrent(CurDirP);
 
218
                if ((Elements.count() > 1) && (!(importerFlags & LoadSavePlugin::lfCreateDoc)))
 
219
                {
 
220
                        bool isGroup = true;
 
221
                        int firstElem = -1;
 
222
                        if (Elements.at(0)->Groups.count() != 0)
 
223
                                firstElem = Elements.at(0)->Groups.top();
 
224
                        for (int bx = 0; bx < Elements.count(); ++bx)
 
225
                        {
 
226
                                PageItem* bxi = Elements.at(bx);
 
227
                                if (bxi->Groups.count() != 0)
 
228
                                {
 
229
                                        if (bxi->Groups.top() != firstElem)
 
230
                                                isGroup = false;
 
231
                                }
 
232
                                else
 
233
                                        isGroup = false;
 
234
                        }
 
235
                        if (!isGroup)
 
236
                        {
 
237
                                double minx = 99999.9;
 
238
                                double miny = 99999.9;
 
239
                                double maxx = -99999.9;
 
240
                                double maxy = -99999.9;
 
241
                                uint lowestItem = 999999;
 
242
                                uint highestItem = 0;
 
243
                                for (int a = 0; a < Elements.count(); ++a)
 
244
                                {
 
245
                                        Elements.at(a)->Groups.push(m_Doc->GroupCounter);
 
246
                                        PageItem* currItem = Elements.at(a);
 
247
                                        lowestItem = qMin(lowestItem, currItem->ItemNr);
 
248
                                        highestItem = qMax(highestItem, currItem->ItemNr);
 
249
                                        double x1, x2, y1, y2;
 
250
                                        currItem->getVisualBoundingRect(&x1, &y1, &x2, &y2);
 
251
                                        minx = qMin(minx, x1);
 
252
                                        miny = qMin(miny, y1);
 
253
                                        maxx = qMax(maxx, x2);
 
254
                                        maxy = qMax(maxy, y2);
 
255
                                }
 
256
                                double gx = minx;
 
257
                                double gy = miny;
 
258
                                double gw = maxx - minx;
 
259
                                double gh = maxy - miny;
 
260
                                PageItem *high = m_Doc->Items->at(highestItem);
 
261
                                int z = m_Doc->itemAdd(PageItem::Polygon, PageItem::Rectangle, gx, gy, gw, gh, 0, m_Doc->toolSettings.dBrush, m_Doc->toolSettings.dPen, true);
 
262
                                PageItem *neu = m_Doc->Items->takeAt(z);
 
263
                                m_Doc->Items->insert(lowestItem, neu);
 
264
                                neu->Groups.push(m_Doc->GroupCounter);
 
265
                                neu->setItemName( tr("Group%1").arg(neu->Groups.top()));
 
266
                                neu->isGroupControl = true;
 
267
                                neu->groupsLastItem = high;
 
268
                                neu->setTextFlowMode(PageItem::TextFlowDisabled);
 
269
                                for (int a = 0; a < m_Doc->Items->count(); ++a)
 
270
                                {
 
271
                                        m_Doc->Items->at(a)->ItemNr = a;
 
272
                                }
 
273
                                Elements.prepend(neu);
 
274
                                m_Doc->GroupCounter++;
 
275
                        }
 
276
                }
 
277
                m_Doc->DoDrawing = true;
 
278
                m_Doc->scMW()->setScriptRunning(false);
 
279
                m_Doc->setLoading(false);
 
280
                qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor));
 
281
                if ((Elements.count() > 0) && (!ret) && (interactive))
 
282
                {
 
283
                        if (flags & LoadSavePlugin::lfScripted)
 
284
                        {
 
285
                                bool loadF = m_Doc->isLoading();
 
286
                                m_Doc->setLoading(false);
 
287
                                m_Doc->changed();
 
288
                                m_Doc->setLoading(loadF);
 
289
                                m_Doc->m_Selection->delaySignalsOn();
 
290
                                for (int dre=0; dre<Elements.count(); ++dre)
 
291
                                {
 
292
                                        m_Doc->m_Selection->addItem(Elements.at(dre), true);
 
293
                                }
 
294
                                m_Doc->m_Selection->delaySignalsOff();
 
295
                                m_Doc->m_Selection->setGroupRect();
 
296
                                m_Doc->view()->updatesOn(true);
 
297
                        }
 
298
                        else
 
299
                        {
 
300
                                m_Doc->DragP = true;
 
301
                                m_Doc->DraggedElem = 0;
 
302
                                m_Doc->DragElements.clear();
 
303
                                m_Doc->m_Selection->delaySignalsOn();
 
304
                                for (int dre=0; dre<Elements.count(); ++dre)
 
305
                                {
 
306
                                        m_Doc->DragElements.append(Elements.at(dre)->ItemNr);
 
307
                                        tmpSel->addItem(Elements.at(dre), true);
 
308
                                }
 
309
                                tmpSel->setGroupRect();
 
310
                                ScriXmlDoc *ss = new ScriXmlDoc();
 
311
                                ScElemMimeData* md = new ScElemMimeData();
 
312
                                md->setScribusElem(ss->WriteElem(m_Doc, m_Doc->view(), tmpSel));
 
313
                                delete ss;
 
314
/*#ifndef Q_WS_MAC*/
 
315
// see #2196
 
316
                                m_Doc->itemSelection_DeleteItem(tmpSel);
 
317
/*#else
 
318
                                qDebug("aiimport: leaving items on page");
 
319
#endif*/
 
320
                                m_Doc->view()->updatesOn(true);
 
321
                                m_Doc->m_Selection->delaySignalsOff();
 
322
                                // We must copy the TransationSettings object as it is owned
 
323
                                // by handleObjectImport method afterwards
 
324
                                TransactionSettings* transacSettings = new TransactionSettings(trSettings);
 
325
                                m_Doc->view()->handleObjectImport(md, transacSettings);
 
326
                                m_Doc->DragP = false;
 
327
                                m_Doc->DraggedElem = 0;
 
328
                                m_Doc->DragElements.clear();
 
329
                        }
 
330
                }
 
331
                else
 
332
                {
 
333
                        m_Doc->changed();
 
334
                        m_Doc->reformPages();
 
335
                        m_Doc->view()->updatesOn(true);
 
336
                }
 
337
                success = true;
 
338
        }
 
339
        else
 
340
        {
 
341
                QDir::setCurrent(CurDirP);
 
342
                m_Doc->DoDrawing = true;
 
343
                m_Doc->scMW()->setScriptRunning(false);
 
344
                m_Doc->view()->updatesOn(true);
 
345
                qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor));
 
346
        }
 
347
        if (interactive)
 
348
                m_Doc->setLoading(false);
 
349
        //CB If we have a gui we must refresh it if we have used the progressbar
 
350
        if ((showProgress) && (!interactive))
 
351
                m_Doc->view()->DrawNew();
 
352
        if (convertedPDF)
 
353
                QFile::remove(fName);
 
354
        return success;
 
355
}
 
356
 
 
357
AIPlug::~AIPlug()
 
358
{
 
359
        if (progressDialog)
 
360
                delete progressDialog;
 
361
        delete tmpSel;
 
362
}
 
363
 
 
364
bool AIPlug::extractFromPDF(QString infile, QString outfile)
 
365
{
 
366
        bool ret = false;
 
367
#ifdef HAVE_PODOFO
 
368
        QFile outf(outfile);
 
369
        outf.open(QIODevice::WriteOnly);
 
370
        try
 
371
        {
 
372
                PoDoFo::PdfError::EnableDebug( false );
 
373
#if (PODOFO_VERSION == 0 && PODOFO_MINOR > 6)
 
374
                PoDoFo::PdfError::EnableLogging( false );
 
375
#endif
 
376
#if (PODOFO_VERSION == 0 && PODOFO_MINOR == 5 && PODOFO_REVISION == 99) || PODOFO_MINOR > 5
 
377
                PoDoFo::PdfMemDocument doc( infile.toLocal8Bit().data() );
 
378
#else
 
379
                PoDoFo::PdfDocument doc( infile.toLocal8Bit().data() );
 
380
#endif
 
381
                PoDoFo::PdfPage *curPage = doc.GetPage(0);
 
382
                if (curPage != NULL)
 
383
                {
 
384
                        PoDoFo::PdfObject *piece = curPage->GetObject()->GetIndirectKey("PieceInfo");
 
385
                        if (piece != NULL)
 
386
                        {
 
387
                                PoDoFo::PdfObject *illy = piece->GetIndirectKey("Illustrator");
 
388
                                if (illy != NULL)
 
389
                                {
 
390
                                        PoDoFo::PdfObject *priv = illy->GetIndirectKey("Private");
 
391
                                        if (priv == NULL)
 
392
                                                priv = illy;
 
393
                                        int num = 0;
 
394
                                        PoDoFo::PdfObject *numBl = priv->GetIndirectKey("NumBlock");
 
395
                                        if (numBl != NULL)
 
396
                                                num = numBl->GetNumber() + 1;
 
397
                                        if (num == 0)
 
398
                                                num = 99999;
 
399
                                        QString name = "AIPrivateData%1";
 
400
                                        QString Key = name.arg(1);
 
401
                                        PoDoFo::PdfObject *data = priv->GetIndirectKey(PoDoFo::PdfName(Key.toUtf8().data()));
 
402
                                        if (data == NULL)
 
403
                                        {
 
404
                                                name = "AIPDFPrivateData%1";
 
405
                                                Key = name.arg(1);
 
406
                                                data = priv->GetIndirectKey(PoDoFo::PdfName(Key.toUtf8().data()));
 
407
                                        }
 
408
                                        if (data != NULL)
 
409
                                        {
 
410
                                                if (num == 2)
 
411
                                                {
 
412
                                                        Key = name.arg(1);
 
413
                                                        data = priv->GetIndirectKey(PoDoFo::PdfName(Key.toUtf8().data()));
 
414
                                                        PoDoFo::PdfStream const *stream = data->GetStream();
 
415
                                                        char *Buffer;
 
416
#if (PODOFO_MAJOR == 0 && PODOFO_MINOR >= 8)
 
417
                                                        PoDoFo::pdf_long bLen = 0;
 
418
#elif defined(pdf_long)
 
419
                                                        pdf_long bLen = 0;
 
420
#else
 
421
                                                        long bLen = 0;
 
422
#endif
 
423
                                                        stream->GetFilteredCopy(&Buffer, &bLen);
 
424
                                                        outf.write(Buffer, bLen);
 
425
                                                        free( Buffer );
 
426
                                                }
 
427
                                                else
 
428
                                                {
 
429
                                                        for (int a = 2; a < num; a++)
 
430
                                                        {
 
431
                                                                Key = name.arg(a);
 
432
                                                                data = priv->GetIndirectKey(PoDoFo::PdfName(Key.toUtf8().data()));
 
433
                                                                if (data == NULL)
 
434
                                                                        break;
 
435
                                                                PoDoFo::PdfStream const *stream = data->GetStream();
 
436
                                                                char *Buffer;
 
437
#if (PODOFO_MAJOR == 0 && PODOFO_MINOR >= 8)
 
438
                                                                PoDoFo::pdf_long bLen = 0;
 
439
#elif defined(pdf_long)
 
440
                                                                pdf_long bLen = 0;
 
441
#else
 
442
                                                                long bLen = 0;
 
443
#endif
 
444
                                                                stream->GetFilteredCopy(&Buffer, &bLen);
 
445
                                                                outf.write(Buffer, bLen);
 
446
                                                                free( Buffer );
 
447
                                                        }
 
448
                                                }
 
449
                                        }
 
450
                                        ret = true;
 
451
                                }
 
452
                        }
 
453
                }
 
454
                outf.close();
 
455
        }
 
456
        catch (PoDoFo::PdfError& e)
 
457
        {
 
458
                outf.close();
 
459
                qDebug("Scribus caught and handled the following exception from PoDoFo while processing a PDF format ai file:\n----\n");
 
460
                e.PrintErrorMsg();
 
461
                qDebug("----\nThe ai file could not be imported.\n");
 
462
                QFile::remove(outfile);
 
463
                return false;
 
464
        }
 
465
#endif
 
466
        return ret;
 
467
}
 
468
 
 
469
bool AIPlug::decompressAIData(QString &fName)
 
470
{
 
471
        QString f2 = fName+"_decom.ai";
 
472
        FILE *source = fopen(fName.toLocal8Bit().constData(), "rb");
 
473
        fseek(source, 20, SEEK_SET);
 
474
        FILE *dest = fopen(f2.toLocal8Bit().constData(), "wb");
 
475
        int ret;
 
476
        unsigned have;
 
477
        z_stream strm;
 
478
        char in[4096];
 
479
        char out[4096];
 
480
        strm.zalloc = Z_NULL;
 
481
        strm.zfree = Z_NULL;
 
482
        strm.opaque = Z_NULL;
 
483
        strm.avail_in = 0;
 
484
        strm.next_in = Z_NULL;
 
485
        ret = inflateInit(&strm);
 
486
        if (ret != Z_OK)
 
487
                return false;
 
488
        do
 
489
        {
 
490
                strm.avail_in = fread(in, 1, 4096, source);
 
491
                if (ferror(source))
 
492
                {
 
493
                        (void)inflateEnd(&strm);
 
494
                        return false;
 
495
                }
 
496
                if (strm.avail_in == 0)
 
497
                        break;
 
498
                strm.next_in = (Bytef*)in;
 
499
                do
 
500
                {
 
501
                        strm.avail_out = 4096;
 
502
                        strm.next_out = (Bytef*)out;
 
503
                        ret = inflate(&strm, Z_NO_FLUSH);
 
504
                        assert(ret != Z_STREAM_ERROR);
 
505
                        switch (ret)
 
506
                        {
 
507
                                case Z_NEED_DICT:
 
508
                                        ret = Z_DATA_ERROR;
 
509
                                case Z_DATA_ERROR:
 
510
                                case Z_MEM_ERROR:
 
511
                                        (void)inflateEnd(&strm);
 
512
                                        return false;
 
513
                        }
 
514
                        have = 4096 - strm.avail_out;
 
515
                        if (fwrite(out, 1, have, dest) != have || ferror(dest))
 
516
                        {
 
517
                                (void)inflateEnd(&strm);
 
518
                                return false;
 
519
                        }
 
520
                }
 
521
                while (strm.avail_out == 0);
 
522
        }
 
523
        while (ret != Z_STREAM_END);
 
524
        (void)inflateEnd(&strm);
 
525
        fclose(source);
 
526
        fclose(dest);
 
527
        if (!convertedPDF)
 
528
        {
 
529
                QFileInfo bF2(fName);
 
530
                QString tmpFile = ScPaths::getTempFileDir()+ "/"+bF2.baseName()+"_tmp.ai";
 
531
                moveFile(f2, tmpFile);
 
532
                fName = tmpFile;
 
533
                convertedPDF = true;
 
534
        }
 
535
        else
 
536
        {
 
537
                QFile::remove(fName);
 
538
                fName = f2;
 
539
        }
 
540
        return true;
 
541
}
 
542
 
 
543
bool AIPlug::parseHeader(QString fName, double &x, double &y, double &b, double &h)
 
544
{
 
545
        QString tmp, BBox, tmp2, FarNam;
 
546
        ScColor cc;
 
547
//      double c, m, yc, k;
 
548
        bool found = false;
 
549
        QFile f(fName);
 
550
        if (f.open(QIODevice::ReadOnly))
 
551
        {
 
552
/* Try to find Bounding Box */
 
553
                QDataStream ts(&f);
 
554
                while (!ts.atEnd())
 
555
                {
 
556
                        tmp = readLinefromDataStream(ts);
 
557
                        if (tmp.startsWith("%%BoundingBox:"))
 
558
                        {
 
559
                                found = true;
 
560
                                BBox = tmp.remove("%%BoundingBox:");
 
561
                        }
 
562
                        if (!found)
 
563
                        {
 
564
                                if (tmp.startsWith("%%BoundingBox"))
 
565
                                {
 
566
                                        found = true;
 
567
                                        BBox = tmp.remove("%%BoundingBox");
 
568
                                }
 
569
                        }
 
570
                        if (tmp.startsWith("%%HiResBoundingBox:"))
 
571
                        {
 
572
                                found = true;
 
573
                                BBox = tmp.remove("%%HiResBoundingBox:");
 
574
                        }
 
575
                        if (tmp.startsWith("%%For"))
 
576
                        {
 
577
                                QStringList res = getStrings(tmp);
 
578
                                if (res.count() > 1)
 
579
                                {
 
580
                                        docCreator = res[0];
 
581
                                        docOrganisation = res[1];
 
582
                                }
 
583
                        }
 
584
                        if (tmp.startsWith("%%CreationDate:"))
 
585
                        {
 
586
                                QStringList res = getStrings(tmp);
 
587
                                if (res.count() > 1)
 
588
                                {
 
589
                                        docDate = res[0];
 
590
                                        docTime = res[1];
 
591
                                }
 
592
                                else
 
593
                                {
 
594
                                        docDate = tmp.remove("%%CreationDate: ");
 
595
                                        docTime = "";
 
596
                                }
 
597
                        }
 
598
                        if (tmp.startsWith("%%Title"))
 
599
                        {
 
600
                                QStringList res = getStrings(tmp);
 
601
                                if (res.count() > 0)
 
602
                                        docTitle = res[0];
 
603
                        }
 
604
                        if (tmp.startsWith("%%EndComments"))
 
605
                                break;
 
606
                }
 
607
                f.close();
 
608
                if (found)
 
609
                {
 
610
                        QStringList bb = BBox.split(" ", QString::SkipEmptyParts);
 
611
                        if (bb.count() == 4)
 
612
                        {
 
613
                                x = ScCLocale::toDoubleC(bb[0]);
 
614
                                y = ScCLocale::toDoubleC(bb[1]);
 
615
                                b = ScCLocale::toDoubleC(bb[2]);
 
616
                                h = ScCLocale::toDoubleC(bb[3]);
 
617
                        }
 
618
                }
 
619
                importColorsFromFile(fName, CustColors);
 
620
        }
 
621
        return found;
 
622
}
 
623
 
 
624
QString AIPlug::removeAIPrefix(QString comment)
 
625
{
 
626
        QString tmp;
 
627
        if (comment.startsWith("%AI"))
 
628
        {
 
629
                int an = comment.indexOf("_");
 
630
                tmp = comment.remove(0, an+1);
 
631
        }
 
632
        else
 
633
                tmp = comment;
 
634
        return tmp;
 
635
}
 
636
 
 
637
QString AIPlug::parseColor(QString data)
 
638
{
 
639
        QString ret = CommonStrings::None;
 
640
        if (data.isEmpty())
 
641
                return ret;
 
642
        double c, m, y, k;
 
643
        ScColor tmp;
 
644
        ColorList::Iterator it;
 
645
        ScTextStream Code(&data, QIODevice::ReadOnly);
 
646
        bool found = false;
 
647
        Code >> c;
 
648
        Code >> m;
 
649
        Code >> y;
 
650
        Code >> k;
 
651
        int Cc = qRound(c * 255);
 
652
        int Mc = qRound(m * 255);
 
653
        int Yc = qRound(y * 255);
 
654
        int Kc = qRound(k * 255);
 
655
        int hC, hM, hY, hK;
 
656
        tmp.setColor(Cc, Mc, Yc, Kc);
 
657
        for (it = m_Doc->PageColors.begin(); it != m_Doc->PageColors.end(); ++it)
 
658
        {
 
659
                if (it.value().getColorModel() == colorModelCMYK)
 
660
                {
 
661
                        it.value().getCMYK(&hC, &hM, &hY, &hK);
 
662
                        if ((Cc == hC) && (Mc == hM) && (Yc == hY) && (Kc == hK))
 
663
                        {
 
664
                                ret = it.key();
 
665
                                found = true;
 
666
                                break;
 
667
                        }
 
668
                }
 
669
        }
 
670
        if (!found)
 
671
        {
 
672
                tmp.setSpotColor(false);
 
673
                tmp.setRegistrationColor(false);
 
674
                QString namPrefix = "FromAI";
 
675
                m_Doc->PageColors.insert(namPrefix+tmp.name(), tmp);
 
676
//              importedColors.append(namPrefix+tmp.name());
 
677
                ret = namPrefix+tmp.name();
 
678
        }
 
679
        return ret;
 
680
}
 
681
 
 
682
QString AIPlug::parseColorGray(QString data)
 
683
{
 
684
        QString ret = CommonStrings::None;
 
685
        if (data.isEmpty())
 
686
                return ret;
 
687
        double k;
 
688
        ScColor tmp;
 
689
        ColorList::Iterator it;
 
690
        ScTextStream Code(&data, QIODevice::ReadOnly);
 
691
        bool found = false;
 
692
        Code >> k;
 
693
        int Kc = 255 - qRound(k * 255);
 
694
        int hC, hM, hY, hK;
 
695
        tmp.setColor(0, 0, 0, Kc);
 
696
        for (it = m_Doc->PageColors.begin(); it != m_Doc->PageColors.end(); ++it)
 
697
        {
 
698
                if (it.value().getColorModel() == colorModelCMYK)
 
699
                {
 
700
                        it.value().getCMYK(&hC, &hM, &hY, &hK);
 
701
                        if ((hC == 0) && (hM == 0) && (hY == 0) && (Kc == hK))
 
702
                        {
 
703
                                ret = it.key();
 
704
                                found = true;
 
705
                                break;
 
706
                        }
 
707
                }
 
708
        }
 
709
        if (!found)
 
710
        {
 
711
                tmp.setSpotColor(false);
 
712
                tmp.setRegistrationColor(false);
 
713
                QString namPrefix = "FromAI";
 
714
                m_Doc->PageColors.insert(namPrefix+tmp.name(), tmp);
 
715
//              importedColors.append(namPrefix+tmp.name());
 
716
                ret = namPrefix+tmp.name();
 
717
        }
 
718
        return ret;
 
719
}
 
720
 
 
721
QString AIPlug::parseColorRGB(QString data)
 
722
{
 
723
        QString ret = CommonStrings::None;
 
724
        if (data.isEmpty())
 
725
                return ret;
 
726
        double r, g, b;
 
727
        ScColor tmp;
 
728
        ColorList::Iterator it;
 
729
        ScTextStream Code(&data, QIODevice::ReadOnly);
 
730
        bool found = false;
 
731
        Code >> r;
 
732
        Code >> g;
 
733
        Code >> b;
 
734
        int Rc = qRound(r * 255);
 
735
        int Gc = qRound(g * 255);
 
736
        int Bc = qRound(b * 255);
 
737
        int hR, hG, hB;
 
738
        tmp.setColorRGB(Rc, Gc, Bc);
 
739
        for (it = m_Doc->PageColors.begin(); it != m_Doc->PageColors.end(); ++it)
 
740
        {
 
741
                if (it.value().getColorModel() == colorModelRGB)
 
742
                {
 
743
                        it.value().getRGB(&hR, &hG, &hB);
 
744
                        if ((Rc == hR) && (Gc == hG) && (Bc == hB))
 
745
                        {
 
746
                                ret = it.key();
 
747
                                found = true;
 
748
                                break;
 
749
                        }
 
750
                }
 
751
        }
 
752
        if (!found)
 
753
        {
 
754
                tmp.setSpotColor(false);
 
755
                tmp.setRegistrationColor(false);
 
756
                QString namPrefix = "FromAI";
 
757
                m_Doc->PageColors.insert(namPrefix+tmp.name(), tmp);
 
758
//              importedColors.append(namPrefix+tmp.name());
 
759
                ret = namPrefix+tmp.name();
 
760
        }
 
761
        return ret;
 
762
}
 
763
 
 
764
QString AIPlug::parseCustomColor(QString data, double &shade)
 
765
{
 
766
        QString ret = CommonStrings::None;
 
767
        if (data.isEmpty())
 
768
                return ret;
 
769
        double c, m, y, k, sh;
 
770
        ScColor tmp;
 
771
        ColorList::Iterator it;
 
772
        ScTextStream Code(&data, QIODevice::ReadOnly);
 
773
        bool found = false;
 
774
        Code >> c;
 
775
        Code >> m;
 
776
        Code >> y;
 
777
        Code >> k;
 
778
        QString tmpS = data;
 
779
        int an = data.indexOf("(");
 
780
        int en = data.lastIndexOf(")");
 
781
        QString FarNam = data.mid(an+1, en-an-1);
 
782
        FarNam.remove("\\");
 
783
        QString FarSha = data.mid(en+1, data.size() - en);
 
784
        ScTextStream Val(&FarSha, QIODevice::ReadOnly);
 
785
        Val >> sh;
 
786
        shade = (1.0 - sh) * 100.0;
 
787
        int Cc = qRound(c * 255);
 
788
        int Mc = qRound(m * 255);
 
789
        int Yc = qRound(y * 255);
 
790
        int Kc = qRound(k * 255);
 
791
        int hC, hM, hY, hK;
 
792
        tmp.setColor(Cc, Mc, Yc, Kc);
 
793
        for (it = m_Doc->PageColors.begin(); it != m_Doc->PageColors.end(); ++it)
 
794
        {
 
795
                if (it.value().getColorModel() == colorModelCMYK)
 
796
                {
 
797
                        it.value().getCMYK(&hC, &hM, &hY, &hK);
 
798
                        if ((Cc == hC) && (Mc == hM) && (Yc == hY) && (Kc == hK))
 
799
                        {
 
800
                                ret = it.key();
 
801
                                found = true;
 
802
                                break;
 
803
                        }
 
804
                }
 
805
        }
 
806
        if (!found)
 
807
        {
 
808
                tmp.setSpotColor(true);
 
809
                tmp.setRegistrationColor(false);
 
810
                m_Doc->PageColors.insert(FarNam, tmp);
 
811
//              importedColors.append(FarNam);
 
812
                ret = FarNam;
 
813
        }
 
814
        return ret;
 
815
}
 
816
 
 
817
QString AIPlug::parseCustomColorX(QString data, double &shade, QString type)
 
818
{
 
819
        QString ret = CommonStrings::None;
 
820
        if (data.isEmpty())
 
821
                return ret;
 
822
        double c, m, y, k, sh, r, g, b;
 
823
        ScColor tmp;
 
824
        ColorList::Iterator it;
 
825
        ScTextStream Code(&data, QIODevice::ReadOnly);
 
826
        bool found = false;
 
827
        if (type == "1")
 
828
        {
 
829
                Code >> r;
 
830
                Code >> g;
 
831
                Code >> b;
 
832
                int Rc = qRound(r * 255);
 
833
                int Gc = qRound(g * 255);
 
834
                int Bc = qRound(b * 255);
 
835
                int hR, hG, hB;
 
836
                tmp.setColorRGB(Rc, Gc, Bc);
 
837
                for (it = m_Doc->PageColors.begin(); it != m_Doc->PageColors.end(); ++it)
 
838
                {
 
839
                        if (it.value().getColorModel() == colorModelRGB)
 
840
                        {
 
841
                                it.value().getRGB(&hR, &hG, &hB);
 
842
                                if ((Rc == hR) && (Gc == hG) && (Bc == hB))
 
843
                                {
 
844
                                        ret = it.key();
 
845
                                        found = true;
 
846
                                        break;
 
847
                                }
 
848
                        }
 
849
                }
 
850
        }
 
851
        else
 
852
        {
 
853
                Code >> c;
 
854
                Code >> m;
 
855
                Code >> y;
 
856
                Code >> k;
 
857
                int Cc = qRound(c * 255);
 
858
                int Mc = qRound(m * 255);
 
859
                int Yc = qRound(y * 255);
 
860
                int Kc = qRound(k * 255);
 
861
                int hC, hM, hY, hK;
 
862
                tmp.setColor(Cc, Mc, Yc, Kc);
 
863
                for (it = m_Doc->PageColors.begin(); it != m_Doc->PageColors.end(); ++it)
 
864
                {
 
865
                        if (it.value().getColorModel() == colorModelCMYK)
 
866
                        {
 
867
                                it.value().getCMYK(&hC, &hM, &hY, &hK);
 
868
                                if ((Cc == hC) && (Mc == hM) && (Yc == hY) && (Kc == hK))
 
869
                                {
 
870
                                        ret = it.key();
 
871
                                        found = true;
 
872
                                        break;
 
873
                                }
 
874
                        }
 
875
                }
 
876
        }
 
877
        QString tmpS = data;
 
878
        int an = data.indexOf("(");
 
879
        int en = data.lastIndexOf(")");
 
880
        QString FarNam = data.mid(an+1, en-an-1);
 
881
        FarNam.remove("\\");
 
882
        QString FarSha = data.mid(en+1, data.size() - en);
 
883
        ScTextStream Val(&FarSha, QIODevice::ReadOnly);
 
884
        Val >> sh;
 
885
        shade = (1.0 - sh) * 100.0;
 
886
        if (!found)
 
887
        {
 
888
                if (type == "0")
 
889
                        tmp.setSpotColor(true);
 
890
                tmp.setRegistrationColor(false);
 
891
                m_Doc->PageColors.insert(FarNam, tmp);
 
892
//              importedColors.append(FarNam);
 
893
                ret = FarNam;
 
894
        }
 
895
        return ret;
 
896
}
 
897
 
 
898
QStringList AIPlug::getStrings(QString data)
 
899
{
 
900
        QStringList result;
 
901
        result.clear();
 
902
        QChar tmp;
 
903
        QString tmp2 = "";
 
904
        QString tmp3 = "";
 
905
        bool paran = false;
 
906
        bool skip = false;
 
907
        int digitCount = 0;
 
908
        for (int a = 0; a < data.count(); a++)
 
909
        {
 
910
                tmp = data[a];
 
911
                if (skip)
 
912
                {
 
913
                        if (paran)
 
914
                        {
 
915
                                if (tmp.isDigit())
 
916
                                {
 
917
                                        tmp3 += tmp;
 
918
                                        digitCount++;
 
919
                                        if (digitCount == 3)
 
920
                                        {
 
921
                                                bool ok = false;
 
922
                                                int code = tmp3.toInt(&ok, 8);
 
923
                                                if (ok)
 
924
                                                        tmp2 += QChar(code);
 
925
                                                digitCount = 0;
 
926
                                                tmp3 = "";
 
927
                                                skip = false;
 
928
                                        }
 
929
                                }
 
930
                                else
 
931
                                {
 
932
                                        if (tmp == 'r')
 
933
                                                tmp = SpecialChars::PARSEP;
 
934
                                        tmp2 += tmp;
 
935
                                        skip = false;
 
936
                                }
 
937
                        }
 
938
                        continue;
 
939
                }
 
940
                if (tmp == '(')
 
941
                {
 
942
                        paran = true;
 
943
                        continue;
 
944
                }
 
945
                if (tmp == ')')
 
946
                {
 
947
                        paran = false;
 
948
                        result.append(tmp2);
 
949
                        tmp2 = "";
 
950
                        continue;
 
951
                }
 
952
                if (tmp == '\\')
 
953
                {
 
954
                        skip = true;
 
955
                        continue;
 
956
                }
 
957
                if (paran)
 
958
                        tmp2 += tmp;
 
959
        }
 
960
        return result;
 
961
}
 
962
 
 
963
void AIPlug::getCommands(QString data, QStringList &commands)
 
964
{
 
965
        QString tmp;
 
966
        QString tmp2;
 
967
        QString tmp3;
 
968
        bool paran = false;
 
969
        bool arra = false;
 
970
        bool skip = false;
 
971
        for (int a = 0; a < data.count(); a++)
 
972
        {
 
973
                tmp = data[a];
 
974
                if (skip)
 
975
                {
 
976
                        tmp2 += tmp;
 
977
                        skip = false;
 
978
                        continue;
 
979
                }
 
980
                if (tmp == "(")
 
981
                {
 
982
                        paran = true;
 
983
                        tmp2 += tmp;
 
984
                        continue;
 
985
                }
 
986
                if (tmp == ")")
 
987
                {
 
988
                        paran = false;
 
989
                        tmp2 += tmp;
 
990
                        continue;
 
991
                }
 
992
                if (tmp == "[")
 
993
                {
 
994
                        arra = true;
 
995
                        tmp2 += tmp;
 
996
                        continue;
 
997
                }
 
998
                if (tmp == "]")
 
999
                {
 
1000
                        arra = false;
 
1001
                        tmp2 += tmp;
 
1002
                        continue;
 
1003
                }
 
1004
//              if (tmp == "\\")
 
1005
//              {
 
1006
//                      skip = true;
 
1007
//                      continue;
 
1008
//              }
 
1009
                if (!paran)
 
1010
                {
 
1011
                        if (tmp == " ")
 
1012
                        {
 
1013
                                tmp3 += " " + tmp2;
 
1014
                                if (commandList.contains(tmp2))
 
1015
                                {
 
1016
                                        commands.append(tmp3);
 
1017
                                        tmp3 = "";
 
1018
                                }
 
1019
                                tmp2 = "";
 
1020
                                continue;
 
1021
                        }
 
1022
                }
 
1023
                tmp2 += tmp;
 
1024
        }
 
1025
        if (!tmp2.isEmpty())
 
1026
        {
 
1027
                tmp3 += " " + tmp2;
 
1028
                commands.append(tmp3);
 
1029
        }
 
1030
}
 
1031
 
 
1032
void AIPlug::processData(QString data)
 
1033
{
 
1034
        double x, y, x1, y1, x2, y2;
 
1035
        int z, tmpInt;
 
1036
        PageItem *ite;
 
1037
        QString command = "";
 
1038
        QString Cdata = "";
 
1039
        QStringList da;
 
1040
        getCommands(data, da);
 
1041
        for (int a = 0; a < da.count(); a++)
 
1042
        {
 
1043
                Cdata = da[a];
 
1044
                if (((Cdata.startsWith("%")) || (Cdata.startsWith(" %"))) && (!meshMode))
 
1045
                        continue;
 
1046
                QStringList da2 = Cdata.split(" ", QString::SkipEmptyParts);
 
1047
                if (da2.count() == 0)
 
1048
                        return;
 
1049
                command = da2[da2.count()-1];
 
1050
/* Start Path construction commands */
 
1051
                if (command == "m")
 
1052
                {
 
1053
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1054
                        ts2 >> x >> y;
 
1055
                        Coords.svgMoveTo(x - docX, docHeight - (y - docY));
 
1056
                        currentPoint = FPoint(x - docX, docHeight - (y - docY));
 
1057
                }
 
1058
                else if ((command == "L") || (command == "l"))
 
1059
                {
 
1060
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1061
                        ts2 >> x >> y;
 
1062
                        Coords.svgLineTo(x - docX, docHeight - (y - docY));
 
1063
                        currentPoint = FPoint(x - docX, docHeight - (y - docY));
 
1064
                }
 
1065
                else if ((command == "C") || (command == "c"))
 
1066
                {
 
1067
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1068
                        ts2 >> x >> y >> x1 >> y1 >> x2 >> y2;
 
1069
                        Coords.svgCurveToCubic(x - docX, docHeight - (y - docY),
 
1070
                                                                   x1 - docX, docHeight - (y1 - docY),
 
1071
                                                                   x2 - docX, docHeight - (y2 - docY));
 
1072
                        currentPoint = FPoint(x2 - docX, docHeight - (y2 - docY));
 
1073
                }
 
1074
                else if ((command == "Y") || (command == "y"))
 
1075
                {
 
1076
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1077
                        ts2 >> x1 >> y1 >> x2 >> y2;
 
1078
                        Coords.svgCurveToCubic(currentPoint.x(), currentPoint.y(),
 
1079
                                                                   x1 - docX, docHeight - (y1 - docY),
 
1080
                                                                   x2 - docX, docHeight - (y2 - docY));
 
1081
                        currentPoint = FPoint(x2 - docX, docHeight - (y2 - docY));
 
1082
                }
 
1083
                else if ((command == "V") || (command == "v"))
 
1084
                {
 
1085
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1086
                        ts2 >> x >> y >> x2 >> y2;
 
1087
                        Coords.svgCurveToCubic(x - docX, docHeight - (y - docY),
 
1088
                                                                   currentPoint.x(), currentPoint.y(),
 
1089
                                                                   x2 - docX, docHeight - (y2 - docY));
 
1090
                        currentPoint = FPoint(x2 - docX, docHeight - (y2 - docY));
 
1091
                }
 
1092
/* End Path construction commands */
 
1093
/* Start Object creation commands */
 
1094
                else if ((command == "b") || (command == "B") || (command == "f") || (command == "F") || (command == "s") || (command == "S"))
 
1095
                {
 
1096
                        if (Coords.size() > 3)
 
1097
                        {
 
1098
                                if ((!WasU) || ((WasU) && (FirstU)))
 
1099
                                {
 
1100
                                        if ((command == "B") || (command == "F") || (command == "S"))
 
1101
                                        {
 
1102
                                                if (command == "F")
 
1103
                                                        z = m_Doc->itemAdd(PageItem::PolyLine, PageItem::Unspecified, baseX, baseY, 10, 10, LineW, CurrColorFill, CommonStrings::None, true);
 
1104
                                                else if (command == "B")
 
1105
                                                        z = m_Doc->itemAdd(PageItem::PolyLine, PageItem::Unspecified, baseX, baseY, 10, 10, LineW, CurrColorFill, CurrColorStroke, true);
 
1106
                                                else
 
1107
                                                        z = m_Doc->itemAdd(PageItem::PolyLine, PageItem::Unspecified, baseX, baseY, 10, 10, LineW, CommonStrings::None, CurrColorStroke, true);
 
1108
                                        }
 
1109
                                        else
 
1110
                                        {
 
1111
                                                if (command == "f")
 
1112
                                                        z = m_Doc->itemAdd(PageItem::Polygon, PageItem::Unspecified, baseX, baseY, 10, 10, LineW, CurrColorFill, CommonStrings::None, true);
 
1113
                                                else if (command == "b")
 
1114
                                                        z = m_Doc->itemAdd(PageItem::Polygon, PageItem::Unspecified, baseX, baseY, 10, 10, LineW, CurrColorFill, CurrColorStroke, true);
 
1115
                                                else
 
1116
                                                        z = m_Doc->itemAdd(PageItem::Polygon, PageItem::Unspecified, baseX, baseY, 10, 10, LineW, CommonStrings::None, CurrColorStroke, true);
 
1117
                                        }
 
1118
                                        ite = m_Doc->Items->at(z);
 
1119
                                        ite->PoLine = Coords.copy();
 
1120
                                        ite->PoLine.translate(m_Doc->currentPage()->xOffset(), m_Doc->currentPage()->yOffset());
 
1121
                                        ite->ClipEdited = true;
 
1122
                                        ite->FrameType = 3;
 
1123
                                        ite->setFillShade(CurrFillShade);
 
1124
                                        ite->setLineShade(CurrStrokeShade);
 
1125
                                        ite->setFillEvenOdd(fillRule);
 
1126
                                        ite->setFillTransparency(1.0 - Opacity);
 
1127
                                        ite->setLineTransparency(1.0 - Opacity);
 
1128
        //                              ite->setFillBlendmode(blendMode);
 
1129
        //                              ite->setLineBlendmode(blendMode);
 
1130
                                        if (!currentPatternName.isEmpty())
 
1131
                                        {
 
1132
                                                ite->setPattern(currentPatternName);
 
1133
                                                ite->setPatternTransform(currentPatternXScale * 100, currentPatternYScale * 100, currentPatternX, currentPatternY, currentPatternRotation);
 
1134
                                                ite->GrType = 8;
 
1135
                                                currentPatternName = "";
 
1136
                                        }
 
1137
                                        ite->setLineEnd(CapStyle);
 
1138
                                        ite->setLineJoin(JoinStyle);
 
1139
                                        if (importerFlags & LoadSavePlugin::lfCreateDoc)
 
1140
                                                ite->setLocked(itemLocked);
 
1141
                                        if (!WasU)
 
1142
                                        {
 
1143
                                                FPoint wh = getMaxClipF(&ite->PoLine);
 
1144
                                                ite->setWidthHeight(wh.x(),wh.y());
 
1145
                                                ite->setTextFlowMode(PageItem::TextFlowDisabled);
 
1146
                                                m_Doc->AdjustItemSize(ite);
 
1147
                                        }
 
1148
                                        if (patternMode)
 
1149
                                                PatternElements.append(ite);
 
1150
                                        else
 
1151
                                                Elements.append(ite);
 
1152
                                        if (groupStack.count() != 0)
 
1153
                                                groupStack.top().append(ite);
 
1154
                                        
 
1155
                                }
 
1156
                                else
 
1157
                                {
 
1158
                                        ite = m_Doc->Items->at(m_Doc->Items->count()-1);
 
1159
                                        ite->PoLine.setMarker();
 
1160
                                        Coords.translate(m_Doc->currentPage()->xOffset(), m_Doc->currentPage()->yOffset());
 
1161
                                        ite->PoLine.putPoints(ite->PoLine.size(), Coords.size(), Coords);
 
1162
                                }
 
1163
                                Coords.resize(0);
 
1164
                                Coords.svgInit();
 
1165
                                FirstU = false;
 
1166
                                itemRendered = true;
 
1167
                        }
 
1168
                }
 
1169
                else if (command == "*u")
 
1170
                {
 
1171
                        FirstU = true;
 
1172
                        WasU = true;
 
1173
                }
 
1174
                else if (command == "*U")
 
1175
                {
 
1176
                        WasU = false;
 
1177
                        ite = m_Doc->Items->at(m_Doc->Items->count()-1);
 
1178
                        FPoint wh = getMaxClipF(&ite->PoLine);
 
1179
                        ite->setWidthHeight(wh.x(),wh.y());
 
1180
                        m_Doc->AdjustItemSize(ite);
 
1181
                }
 
1182
                else if ((command == "u") || (command == "q"))
 
1183
                {
 
1184
                        QList<PageItem*> gElements;
 
1185
                        groupStack.push(gElements);
 
1186
                }
 
1187
                else if ((command == "U") || (command == "Q"))
 
1188
                {
 
1189
                        if (groupStack.count() != 0)
 
1190
                        {
 
1191
                                QList<PageItem*> gElements = groupStack.pop();
 
1192
                                tmpSel->clear();
 
1193
                                if (gElements.count() > 0)
 
1194
                                {
 
1195
                                        for (int dre = 0; dre < gElements.count(); ++dre)
 
1196
                                        {
 
1197
                                                tmpSel->addItem(gElements.at(dre), true);
 
1198
                                        }
 
1199
                                        m_Doc->itemSelection_GroupObjects(false, false, tmpSel);
 
1200
                                        ite = tmpSel->itemAt(0);
 
1201
                                        if ((clipCoords.size() > 4) && (command == "Q"))
 
1202
                                        {
 
1203
                                                ite = tmpSel->itemAt(0);
 
1204
                                                clipCoords.translate(m_Doc->currentPage()->xOffset()-ite->xPos(), m_Doc->currentPage()->yOffset()-ite->yPos());
 
1205
                                                ite->PoLine = clipCoords.copy();
 
1206
                                                ite->PoLine.translate(baseX, baseY);
 
1207
                                        }
 
1208
                                if (patternMode)
 
1209
                                        PatternElements.append(ite);
 
1210
                                else
 
1211
                                        Elements.append(ite);
 
1212
                                }
 
1213
                                if (groupStack.count() != 0)
 
1214
                                {
 
1215
                                        for (int as = 0; as < tmpSel->count(); ++as)
 
1216
                                        {
 
1217
                                                groupStack.top().append(tmpSel->itemAt(as));
 
1218
                                        }
 
1219
                                }
 
1220
                                tmpSel->clear();
 
1221
                        }
 
1222
                        if (command == "Q")
 
1223
                        {
 
1224
                                clipCoords.resize(0);
 
1225
                                clipCoords.svgInit();
 
1226
                        }
 
1227
                }
 
1228
                else if (command == "W")
 
1229
                {
 
1230
                        if (clipCoords.size() > 0)
 
1231
                                clipCoords.setMarker();
 
1232
                        clipCoords.putPoints(clipCoords.size(), Coords.size(), Coords);
 
1233
                }
 
1234
/* End Object construction commands */
 
1235
/* Start Graphics state commands */
 
1236
                else if (command == "A")
 
1237
                {
 
1238
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1239
                        ts2 >> tmpInt;
 
1240
                        if (tmpInt == 1)
 
1241
                                itemLocked = true;
 
1242
                        else
 
1243
                                itemLocked = false;
 
1244
                }
 
1245
                else if (command == "w")
 
1246
                {
 
1247
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1248
                        ts2 >> LineW;
 
1249
                }
 
1250
                else if (command == "j")
 
1251
                {
 
1252
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1253
                        ts2 >> tmpInt;
 
1254
                        if (tmpInt == 0)
 
1255
                                JoinStyle = Qt::MiterJoin;
 
1256
                        else if (tmpInt == 1)
 
1257
                                JoinStyle = Qt::RoundJoin;
 
1258
                        else if (tmpInt == 1)
 
1259
                                JoinStyle = Qt::BevelJoin;
 
1260
                }
 
1261
                else if (command == "J")
 
1262
                {
 
1263
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1264
                        ts2 >> tmpInt;
 
1265
                        if (tmpInt == 0)
 
1266
                                CapStyle = Qt::FlatCap;
 
1267
                        else if (tmpInt == 1)
 
1268
                                CapStyle = Qt::RoundCap;
 
1269
                        else if (tmpInt == 1)
 
1270
                                CapStyle = Qt::SquareCap;
 
1271
                }
 
1272
                /* undocumented Command Xy
 
1273
                        - has up to 5 Parameters
 
1274
                        - first Parameter might be the Blendmode
 
1275
                        - second Parameter is the Opacity
 
1276
                */
 
1277
                else if (command == "Xy")
 
1278
                {
 
1279
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1280
                        ts2 >> blendMode >> Opacity;
 
1281
                }
 
1282
                else if (command == "XR")
 
1283
                {
 
1284
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1285
                        ts2 >> tmpInt;
 
1286
                        if (tmpInt == 1)
 
1287
                                fillRule = true;
 
1288
                        else
 
1289
                                fillRule = false;
 
1290
                }
 
1291
                else if (command == "Bb")
 
1292
                {
 
1293
                        gradientMode = true;
 
1294
                        wasBC = false;
 
1295
                        itemRendered = false;
 
1296
                        startMatrix = QMatrix();
 
1297
                        endMatrix = QMatrix();
 
1298
                }
 
1299
                else if (command == "Xm")
 
1300
                {
 
1301
                        ScTextStream gVals(&Cdata, QIODevice::ReadOnly);
 
1302
                        double m1, m2, m3, m4, m5, m6;
 
1303
                        gVals >> m1 >> m2 >> m3 >> m4 >> m5 >> m6;
 
1304
                        startMatrix.translate(m5, -m6);
 
1305
                        endMatrix.scale(m1, m4);
 
1306
                        wasBC = true;
 
1307
                }
 
1308
                else if (command == "Bm")
 
1309
                {
 
1310
                        if (m_gradients[currentGradientName].type() == 1)
 
1311
                        {
 
1312
                                ScTextStream gVals(&Cdata, QIODevice::ReadOnly);
 
1313
                                double m1, m2, m3, m4, m5, m6;
 
1314
                                gVals >> m1 >> m2 >> m3 >> m4 >> m5 >> m6;
 
1315
                                startMatrix.translate(m5, -m6);
 
1316
//                              endMatrix.scale(m1, m4);
 
1317
                                endMatrix *= QMatrix(m1, m2, m3, m4, 0, 0);
 
1318
//                              endMatrix = QMatrix(m1, m2, m3, m4, m5, m6);
 
1319
                                wasBC = true;
 
1320
                        }
 
1321
                }
 
1322
                else if (command == "BB")
 
1323
                {
 
1324
                        if (itemRendered)
 
1325
                        {
 
1326
                                gradientMode = false;
 
1327
                                ite = m_Doc->Items->at(m_Doc->Items->count()-1);
 
1328
                                ite->fill_gradient = m_gradients[currentGradientName];
 
1329
                                if (ite->fill_gradient.type() == 0)
 
1330
                                        ite->GrType = 6;
 
1331
                                else
 
1332
                                        ite->GrType = 7;
 
1333
                                QMatrix m1;
 
1334
                                m1.translate(currentGradientOrigin.x() - ite->xPos(), currentGradientOrigin.y() - ite->yPos());
 
1335
                                m1.translate(m_Doc->currentPage()->xOffset(), m_Doc->currentPage()->yOffset());
 
1336
                                m1.rotate(-currentGradientAngle);
 
1337
                                ite->GrStartX = currentGradientOrigin.x() - ite->xPos() + m_Doc->currentPage()->xOffset();
 
1338
                                ite->GrStartY = currentGradientOrigin.y() - ite->yPos() + m_Doc->currentPage()->yOffset();
 
1339
                                QPointF target = m1.map(QPointF(currentGradientLenght, 0.0));
 
1340
                                ite->GrEndX = target.x();
 
1341
                                ite->GrEndY = target.y();
 
1342
                                if (wasBC)
 
1343
                                {
 
1344
                                        QPointF newS = startMatrix.map(QPointF(ite->GrStartX, ite->GrStartY));
 
1345
                                        ite->GrStartX = newS.x();
 
1346
                                        ite->GrStartY = newS.y();
 
1347
                                        QMatrix m2;
 
1348
//                                      m2.translate(ite->GrStartX, ite->GrStartY);
 
1349
                                        m2.rotate(-currentGradientAngle);
 
1350
                                        m2 *= endMatrix;
 
1351
                                        QPointF target = m2.map(QPointF(currentGradientLenght, 0.0));
 
1352
                                        ite->GrEndX = target.x();
 
1353
                                        ite->GrEndY = target.y();
 
1354
                                }
 
1355
                        }
 
1356
                        wasBC = false;
 
1357
                        currentGradientMatrix = QMatrix();
 
1358
                        currentGradientOrigin = QPointF(0.0, 0.0);
 
1359
                        currentGradientAngle = 0.0;
 
1360
                        currentGradientLenght = 1.0;
 
1361
                        itemRendered = false;
 
1362
                }
 
1363
                else if (command == "Bg")
 
1364
                {
 
1365
                        int an = Cdata.indexOf("(");
 
1366
                        int en = Cdata.lastIndexOf(")");
 
1367
                        currentGradientName = Cdata.mid(an+1, en-an-1);
 
1368
                        currentGradientName.remove("\\");
 
1369
                        QString tmpS = Cdata.mid(en+1, Cdata.size() - en);
 
1370
                        ScTextStream gVals(&tmpS, QIODevice::ReadOnly);
 
1371
                        double xOrig, yOrig, m1, m2, m3, m4, m5, m6;
 
1372
                        gVals >> xOrig >> yOrig >> currentGradientAngle >> currentGradientLenght >> m1 >> m2 >> m3 >> m4 >> m5 >> m6;
 
1373
                        currentGradientOrigin = QPointF(xOrig - docX, docHeight - (yOrig - docY));
 
1374
                        currentGradientMatrix = QMatrix(m1, m2, m3, m4, m5, m6);
 
1375
                }
 
1376
/* End Graphics state commands */
 
1377
/* Start Color commands */
 
1378
                else if ((command == "G") || (command == "g"))
 
1379
                {
 
1380
                        if (command == "G")
 
1381
                                CurrColorStroke = parseColorGray(Cdata);
 
1382
                        else
 
1383
                                CurrColorFill = parseColorGray(Cdata);
 
1384
                }
 
1385
                else if ((command == "K") || (command == "k"))
 
1386
                {
 
1387
                        if (command == "K")
 
1388
                                CurrColorStroke = parseColor(Cdata);
 
1389
                        else
 
1390
                                CurrColorFill = parseColor(Cdata);
 
1391
                }
 
1392
                else if ((command == "XA") || (command == "Xa"))
 
1393
                {
 
1394
                        QString Xdata = da2[da2.count()-4] + " " + da2[da2.count()-3] + " " + da2[da2.count()-2];
 
1395
                        if (command == "XA")
 
1396
                                CurrColorStroke = parseColorRGB(Xdata);
 
1397
                        else
 
1398
                                CurrColorFill = parseColorRGB(Xdata);
 
1399
                }
 
1400
                else if ((command == "XX") || (command == "Xx") || (command == "Xk"))
 
1401
                {
 
1402
                        if (command == "XX")
 
1403
                                CurrColorStroke = parseCustomColorX(Cdata, CurrStrokeShade, da2[da2.count()-2]);
 
1404
                        else
 
1405
                                CurrColorFill = parseCustomColorX(Cdata, CurrFillShade, da2[da2.count()-2]);
 
1406
                }
 
1407
                else if ((command == "X") || (command == "x"))
 
1408
                {
 
1409
                        if (command == "X")
 
1410
                                CurrColorStroke = parseCustomColor(Cdata, CurrStrokeShade);
 
1411
                        else
 
1412
                                CurrColorFill = parseCustomColor(Cdata, CurrFillShade);
 
1413
                }
 
1414
                else if (command == "p")
 
1415
                {
 
1416
                        int an = Cdata.indexOf("(");
 
1417
                        int en = Cdata.lastIndexOf(")");
 
1418
                        currentPatternName = Cdata.mid(an+1, en-an-1);
 
1419
                        currentPatternName.remove("\\");
 
1420
                        currentPatternName = currentPatternName.trimmed().simplified().replace(" ", "_");
 
1421
                        QString tmpS = Cdata.mid(en+1, Cdata.size() - en);
 
1422
                        ScTextStream gVals(&tmpS, QIODevice::ReadOnly);
 
1423
                        gVals >> currentPatternX >> currentPatternY >> currentPatternXScale >> currentPatternYScale >> currentPatternRotation;
 
1424
                }
 
1425
                else if (command == "X!")
 
1426
                {
 
1427
                        if (Cdata.contains("/Mesh"))
 
1428
                        {
 
1429
                                QList<PageItem*> gElements;
 
1430
                                groupStack.push(gElements);
 
1431
                                meshMode = true;
 
1432
                                meshNodeCounter = 0;
 
1433
                                accumColorC = 0.0;
 
1434
                                accumColorM = 0.0;
 
1435
                                accumColorY = 0.0;
 
1436
                                accumColorK = 0.0;
 
1437
                                meshColorMode = 0;
 
1438
                        //      meshBoundingPath = QPainterPath();
 
1439
                        }
 
1440
                        if (Cdata.contains("/End"))
 
1441
                        {
 
1442
                                meshMode = false;
 
1443
                                if (groupStack.count() != 0)
 
1444
                                {
 
1445
                                        QList<PageItem*> gElements = groupStack.pop();
 
1446
                                        tmpSel->clear();
 
1447
                                        if (gElements.count() > 0)
 
1448
                                        {
 
1449
                                                for (int dre = 0; dre < gElements.count(); ++dre)
 
1450
                                                {
 
1451
                                                        tmpSel->addItem(gElements.at(dre), true);
 
1452
                                                }
 
1453
                                                m_Doc->itemSelection_GroupObjects(false, false, tmpSel);
 
1454
                                                ite = tmpSel->itemAt(0);
 
1455
                                        /*      if (!meshBoundingPath.isEmpty())
 
1456
                                                {
 
1457
                                                        ite = tmpSel->itemAt(0);
 
1458
                                                        clipCoords.fromQPainterPath(meshBoundingPath);
 
1459
                                                        clipCoords.translate(m_Doc->currentPage()->xOffset()-ite->xPos(), m_Doc->currentPage()->yOffset()-ite->yPos());
 
1460
                                                        ite->PoLine = clipCoords.copy();
 
1461
                                                        ite->PoLine.translate(baseX, baseY);
 
1462
                                                } */
 
1463
                                                if (patternMode)
 
1464
                                                        PatternElements.append(ite);
 
1465
                                                else
 
1466
                                                        Elements.append(ite);
 
1467
                                        }
 
1468
                                        if (groupStack.count() != 0)
 
1469
                                        {
 
1470
                                                for (int as = 0; as < tmpSel->count(); ++as)
 
1471
                                                {
 
1472
                                                        groupStack.top().append(tmpSel->itemAt(as));
 
1473
                                                }
 
1474
                                        }
 
1475
                                        tmpSel->clear();
 
1476
                                }
 
1477
                        }
 
1478
                }
 
1479
                else if (command == "X#")
 
1480
                {
 
1481
                        int an = Cdata.indexOf("_");
 
1482
                        QString cmdLine = Cdata.remove(0, an+1);
 
1483
                        an = cmdLine.lastIndexOf("/");
 
1484
                        QString tmpS = cmdLine.mid(an+1, Cdata.size());
 
1485
                        ScTextStream mVals(&tmpS, QIODevice::ReadOnly);
 
1486
                        QString mKey;
 
1487
                        mVals >> mKey;
 
1488
                        if (mKey == "Size")
 
1489
                        {
 
1490
                                int ans = cmdLine.indexOf("[");
 
1491
                                int ens = cmdLine.lastIndexOf("]");
 
1492
                                QString sizeVals = cmdLine.mid(ans+1, ens-ans-1);
 
1493
                                ScTextStream mVals2(&sizeVals, QIODevice::ReadOnly);
 
1494
                                mVals2 >> meshXSize >> meshYSize;
 
1495
                        }
 
1496
                        if (mKey == "P")
 
1497
                        {
 
1498
                                int ans = cmdLine.indexOf("[");
 
1499
                                int ens = cmdLine.lastIndexOf("]");
 
1500
                                QString posVals = cmdLine.mid(ans+1, ens-ans-1);
 
1501
                                ScTextStream mVals3(&posVals, QIODevice::ReadOnly);
 
1502
                                mVals3 >> currentMeshXPos >> currentMeshYPos;
 
1503
                                accumColorC = 0.0;
 
1504
                                accumColorM = 0.0;
 
1505
                                accumColorY = 0.0;
 
1506
                                accumColorK = 0.0;
 
1507
                        }
 
1508
                        if (mKey == "CS")
 
1509
                        {
 
1510
                                if (Cdata.contains("CMYK"))
 
1511
                                        meshColorMode = 0;
 
1512
                                else if (Cdata.contains("RGB"))
 
1513
                                        meshColorMode = 1;
 
1514
                        }
 
1515
                        if (mKey == "E")
 
1516
                        {
 
1517
                                ScColor tmpColor;
 
1518
                                ColorList::Iterator it;
 
1519
                                int Cc = qRound((accumColorC / 4.0) * 255);
 
1520
                                int Mc = qRound((accumColorM / 4.0) * 255);
 
1521
                                int Yc = qRound((accumColorY / 4.0) * 255);
 
1522
                                int Kc = qRound((accumColorK / 4.0) * 255);
 
1523
                                int hC, hM, hY, hK;
 
1524
                                bool found = false;
 
1525
                                if (meshColorMode == 0)
 
1526
                                {
 
1527
                                        tmpColor.setColor(Cc, Mc, Yc, Kc);
 
1528
                                        for (it = m_Doc->PageColors.begin(); it != m_Doc->PageColors.end(); ++it)
 
1529
                                        {
 
1530
                                                if (it.value().getColorModel() == colorModelCMYK)
 
1531
                                                {
 
1532
                                                        it.value().getCMYK(&hC, &hM, &hY, &hK);
 
1533
                                                        if ((Cc == hC) && (Mc == hM) && (Yc == hY) && (Kc == hK))
 
1534
                                                        {
 
1535
                                                                CurrColorFill = it.key();
 
1536
                                                                found = true;
 
1537
                                                                break;
 
1538
                                                        }
 
1539
                                                }
 
1540
                                        }
 
1541
                                }
 
1542
                                else
 
1543
                                {
 
1544
                                        tmpColor.setColorRGB(Cc, Mc, Yc);
 
1545
                                        for (it = m_Doc->PageColors.begin(); it != m_Doc->PageColors.end(); ++it)
 
1546
                                        {
 
1547
                                                if (it.value().getColorModel() == colorModelRGB)
 
1548
                                                {
 
1549
                                                        it.value().getRGB(&hC, &hM, &hY);
 
1550
                                                        if ((Cc == hC) && (Mc == hM) && (Yc == hY))
 
1551
                                                        {
 
1552
                                                                CurrColorFill = it.key();
 
1553
                                                                found = true;
 
1554
                                                                break;
 
1555
                                                        }
 
1556
                                                }
 
1557
                                        }
 
1558
                                }
 
1559
                                if (!found)
 
1560
                                {
 
1561
                                        tmpColor.setSpotColor(false);
 
1562
                                        tmpColor.setRegistrationColor(false);
 
1563
                                        QString namPrefix = "FromAI";
 
1564
                                        m_Doc->PageColors.insert(namPrefix+tmpColor.name(), tmpColor);
 
1565
                                        CurrColorFill = namPrefix+tmpColor.name();
 
1566
                                }
 
1567
                                meshNodeCounter = 0;
 
1568
                                Coords.resize(0);
 
1569
                                Coords.svgInit();
 
1570
                                Coords.svgMoveTo(meshNode1PointX, meshNode1PointY);
 
1571
                                Coords.svgCurveToCubic(meshNode1Control2X, meshNode1Control2Y, meshNode2Control1X, meshNode2Control1Y, meshNode2PointX, meshNode2PointY);
 
1572
                                Coords.svgCurveToCubic(meshNode2Control2X, meshNode2Control2Y, meshNode3Control1X, meshNode3Control1Y, meshNode3PointX, meshNode3PointY);
 
1573
                                Coords.svgCurveToCubic(meshNode3Control2X, meshNode3Control2Y, meshNode4Control1X, meshNode4Control1Y, meshNode4PointX, meshNode4PointY);
 
1574
                                Coords.svgCurveToCubic(meshNode4Control2X, meshNode4Control2Y, meshNode1Control1X, meshNode1Control1Y, meshNode1PointX, meshNode1PointY);
 
1575
                                z = m_Doc->itemAdd(PageItem::Polygon, PageItem::Unspecified, baseX, baseY, 10, 10, 0, CurrColorFill, CurrColorFill, true);
 
1576
                                ite = m_Doc->Items->at(z);
 
1577
                                ite->PoLine = Coords.copy();
 
1578
                        //      QPainterPath tmpPath = Coords.toQPainterPath(true);
 
1579
                        //      meshBoundingPath = meshBoundingPath.united(tmpPath);
 
1580
                                ite->PoLine.translate(m_Doc->currentPage()->xOffset(), m_Doc->currentPage()->yOffset());
 
1581
                                ite->ClipEdited = true;
 
1582
                                ite->FrameType = 3;
 
1583
                                ite->setFillShade(CurrFillShade);
 
1584
                                ite->setLineShade(CurrFillShade);
 
1585
                                ite->setFillEvenOdd(fillRule);
 
1586
                                ite->setFillTransparency(1.0 - Opacity);
 
1587
                                ite->setLineTransparency(1.0 - Opacity);
 
1588
                                ite->setLineEnd(CapStyle);
 
1589
                                ite->setLineJoin(JoinStyle);
 
1590
                                if (importerFlags & LoadSavePlugin::lfCreateDoc)
 
1591
                                        ite->setLocked(itemLocked);
 
1592
                                FPoint wh = getMaxClipF(&ite->PoLine);
 
1593
                                ite->setWidthHeight(wh.x(),wh.y());
 
1594
                                ite->setTextFlowMode(PageItem::TextFlowDisabled);
 
1595
                                m_Doc->AdjustItemSize(ite);
 
1596
                                if (patternMode)
 
1597
                                        PatternElements.append(ite);
 
1598
                                else
 
1599
                                        Elements.append(ite);
 
1600
                                if (groupStack.count() != 0)
 
1601
                                        groupStack.top().append(ite);
 
1602
                                Coords.resize(0);
 
1603
                                Coords.svgInit();
 
1604
                        }
 
1605
                        if (mKey == "N")
 
1606
                        {
 
1607
                                double cVal, mVal, yVal, kVal, coorX1, coorY1, coorX2, coorY2, coorX3, coorY3;
 
1608
                                int dummy;
 
1609
                                meshNodeCounter++;
 
1610
                                int ans = cmdLine.indexOf("[");
 
1611
                                int ens = cmdLine.lastIndexOf("]");
 
1612
                                QString nodeVals = cmdLine.mid(ans+1, ens-ans-1);
 
1613
                                ScTextStream mVals4(&nodeVals, QIODevice::ReadOnly);
 
1614
                                kVal = 0.0;
 
1615
                                if (meshColorMode == 0)
 
1616
                                        mVals4 >> cVal >> mVal >> yVal >> kVal >> coorX1 >> coorY1 >> coorX2 >> coorY2 >> dummy >> coorX3 >> coorY3;
 
1617
                                else
 
1618
                                        mVals4 >> cVal >> mVal >> yVal >> coorX1 >> coorY1 >> coorX2 >> coorY2 >> dummy >> coorX3 >> coorY3;
 
1619
                                accumColorC += cVal;
 
1620
                                accumColorM += mVal;
 
1621
                                accumColorY += yVal;
 
1622
                                accumColorK += kVal;
 
1623
                                if (meshNodeCounter == 1)
 
1624
                                {
 
1625
                                        meshNode1PointX = coorX1 - docX;
 
1626
                                        meshNode1PointY = docHeight - (coorY1 - docY);
 
1627
                                        meshNode1Control2X = coorX2 - docX;
 
1628
                                        meshNode1Control2Y = docHeight - (coorY2 - docY);
 
1629
                                        meshNode1Control1X = coorX3 - docX;
 
1630
                                        meshNode1Control1Y = docHeight - (coorY3 - docY);
 
1631
                                }
 
1632
                                if (meshNodeCounter == 2)
 
1633
                                {
 
1634
                                        meshNode2PointX = coorX1 - docX;
 
1635
                                        meshNode2PointY = docHeight - (coorY1 - docY);
 
1636
                                        meshNode2Control2X = coorX2 - docX;
 
1637
                                        meshNode2Control2Y = docHeight - (coorY2 - docY);
 
1638
                                        meshNode2Control1X = coorX3 - docX;
 
1639
                                        meshNode2Control1Y = docHeight - (coorY3 - docY);
 
1640
                                }
 
1641
                                if (meshNodeCounter == 3)
 
1642
                                {
 
1643
                                        meshNode3PointX = coorX1 - docX;
 
1644
                                        meshNode3PointY = docHeight - (coorY1 - docY);
 
1645
                                        meshNode3Control2X = coorX2 - docX;
 
1646
                                        meshNode3Control2Y = docHeight - (coorY2 - docY);
 
1647
                                        meshNode3Control1X = coorX3 - docX;
 
1648
                                        meshNode3Control1Y = docHeight - (coorY3 - docY);
 
1649
                                }
 
1650
                                if (meshNodeCounter == 4)
 
1651
                                {
 
1652
                                        meshNode4PointX = coorX1 - docX;
 
1653
                                        meshNode4PointY = docHeight - (coorY1 - docY);
 
1654
                                        meshNode4Control2X = coorX2 - docX;
 
1655
                                        meshNode4Control2Y = docHeight - (coorY2 - docY);
 
1656
                                        meshNode4Control1X = coorX3 - docX;
 
1657
                                        meshNode4Control1Y = docHeight - (coorY3 - docY);
 
1658
                                }
 
1659
                        }
 
1660
                }
 
1661
/* End Color commands */
 
1662
/* Start Layer commands */
 
1663
                else if (command == "Lb")
 
1664
                {
 
1665
                        if (importerFlags & LoadSavePlugin::lfCreateDoc)
 
1666
                        {
 
1667
                                int visible, preview, enabled, printing, dummy, rc, gc, bc;
 
1668
                                ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1669
                                ts2 >> visible >> preview >> enabled >> printing >> dummy >> dummy >> dummy >> rc >> gc >> bc;
 
1670
                                if (!firstLayer)
 
1671
                                        currentLayer = m_Doc->addLayer("Layer", true);
 
1672
                                m_Doc->setLayerVisible(currentLayer, static_cast<bool>(visible));
 
1673
                                m_Doc->setLayerOutline(currentLayer, static_cast<bool>(!preview));
 
1674
                                m_Doc->setLayerLocked(currentLayer, static_cast<bool>(!enabled));
 
1675
                                m_Doc->setLayerPrintable(currentLayer, static_cast<bool>(printing));
 
1676
                                m_Doc->setLayerMarker(currentLayer, QColor(rc, gc, bc));
 
1677
                                firstLayer = false;
 
1678
                        }
 
1679
                        Coords.resize(0);
 
1680
                        Coords.svgInit();
 
1681
                }
 
1682
                else if (command == "LB")
 
1683
                {
 
1684
                        Coords.resize(0);
 
1685
                        Coords.svgInit();
 
1686
                }
 
1687
                else if (command == "Ln")
 
1688
                {
 
1689
                        if (importerFlags & LoadSavePlugin::lfCreateDoc)
 
1690
                        {
 
1691
                                int an = Cdata.indexOf("(");
 
1692
                                int en = Cdata.lastIndexOf(")");
 
1693
                                QString LayerNam = Cdata.mid(an+1, en-an-1);
 
1694
                                LayerNam.remove("\\");
 
1695
                                m_Doc->changeLayerName(currentLayer, LayerNam);
 
1696
                        }
 
1697
                }
 
1698
/* End Layer commands */
 
1699
/* Start Text commands */
 
1700
                else if (command == "To")
 
1701
                {
 
1702
                        ScTextStream ts2(&Cdata, QIODevice::ReadOnly);
 
1703
                        ts2 >> textMode;
 
1704
                        textData.clear();
 
1705
                        textMatrix = QMatrix();
 
1706
                        maxWidth = 0;
 
1707
                        tempW = 0;
 
1708
                        maxHeight = 0;
 
1709
                        textKern = 0;
 
1710
                        startCurrentTextRange = 0;
 
1711
                        endCurrentTextRange = 0;
 
1712
                        textScaleH = 1000;
 
1713
                        textScaleV = 1000;
 
1714
                }
 
1715
                else if (command == "Tp")
 
1716
                {
 
1717
                        ScTextStream gVals(&Cdata, QIODevice::ReadOnly);
 
1718
                        double m1, m2, m3, m4, m5, m6;
 
1719
                        gVals >> m1 >> m2 >> m3 >> m4 >> m5 >> m6;
 
1720
                        textMatrix = QMatrix(m1, m2, m3, m4, m5, m6);
 
1721
                }
 
1722
                else if (command == "Tx") // || (command == "TX"))
 
1723
                {
 
1724
                        QStringList res = getStrings(Cdata);
 
1725
                        if (res.count() > 0)
 
1726
                        {
 
1727
                                QString tex = res[0];
 
1728
                                double tempH = 0;
 
1729
                                startCurrentTextRange = textData.length();
 
1730
                                for (int tt = 0; tt < tex.length(); ++tt)
 
1731
                                {
 
1732
                                        CharStyle nstyle;
 
1733
                                        QString ch = tex.mid(tt,1);
 
1734
                                        nstyle.setFont((*m_Doc->AllFonts)[textFont]);
 
1735
                                        nstyle.setFontSize(textSize);
 
1736
                                        nstyle.setFillColor(CurrColorFill);
 
1737
                                        nstyle.setTracking(textKern);
 
1738
                                        nstyle.setFillShade(100);
 
1739
                                        nstyle.setStrokeColor(CurrColorStroke);
 
1740
                                        nstyle.setStrokeShade(100);
 
1741
                                        nstyle.setScaleH(textScaleH);
 
1742
                                        nstyle.setScaleV(textScaleV);
 
1743
                                        nstyle.setBaselineOffset(0);
 
1744
                                        nstyle.setShadowXOffset(50);
 
1745
                                        nstyle.setShadowYOffset(-50);
 
1746
                                        nstyle.setOutlineWidth(10);
 
1747
                                        nstyle.setUnderlineOffset(-1);
 
1748
                                        nstyle.setUnderlineWidth(-1);
 
1749
                                        nstyle.setStrikethruOffset(-1);
 
1750
                                        nstyle.setStrikethruWidth(-1);
 
1751
                                        nstyle.setFeatures(StyleFlag(ScStyle_Default).featureList());
 
1752
                                        int pot = textData.length();
 
1753
                                        textData.insertChars(pot, ch);
 
1754
                                        textData.applyCharStyle(pot, 1, nstyle);
 
1755
                                        tempW += nstyle.font().realCharWidth(ch[0], nstyle.fontSize() / 10.0)+1;
 
1756
                                        tempH  = qMax(tempH, nstyle.font().height(nstyle.fontSize() / 10.0) + 2.0);
 
1757
                                        maxWidth  = qMax(tempW, maxWidth);
 
1758
                                        maxHeight = qMax(tempH, maxHeight);
 
1759
                                        if ((ch == SpecialChars::PARSEP) || (ch == SpecialChars::LINEBREAK))
 
1760
                                        {
 
1761
                                                maxHeight += nstyle.font().height(nstyle.fontSize() / 10.0);
 
1762
                                                tempW = 0;
 
1763
                                        }
 
1764
                                        endCurrentTextRange = pot;
 
1765
                                }
 
1766
                        }
 
1767
                }
 
1768
                else if (command == "Tk")
 
1769
                {
 
1770
                        int flag;
 
1771
                        double val;
 
1772
                        ScTextStream gVals(&Cdata, QIODevice::ReadOnly);
 
1773
                        gVals >> flag >> val;
 
1774
                        if (flag == 1)
 
1775
                                val = 0;
 
1776
                        double oldval = textData.charStyle(startCurrentTextRange).tracking();
 
1777
                        CharStyle nstyle = textData.charStyle(startCurrentTextRange);
 
1778
                        nstyle.setTracking(oldval + val);
 
1779
                        textData.applyCharStyle(startCurrentTextRange, 1, nstyle);
 
1780
                }
 
1781
                else if (command == "Tc")
 
1782
                {
 
1783
                        ScTextStream gVals(&Cdata, QIODevice::ReadOnly);
 
1784
                        gVals >> textKern;
 
1785
                        textKern *= 100.0;
 
1786
                }
 
1787
                else if (command == "Tz")
 
1788
                {
 
1789
                        ScTextStream gVals(&Cdata, QIODevice::ReadOnly);
 
1790
                        gVals >> textScaleH >> textScaleV;
 
1791
                        textScaleH *= 10.0;
 
1792
                        textScaleV *= 10.0;
 
1793
                }
 
1794
                else if (command == "T*")
 
1795
                {
 
1796
                        CharStyle nstyle;
 
1797
                        QString ch = SpecialChars::LINEBREAK;
 
1798
                        nstyle.setFont((*m_Doc->AllFonts)[textFont]);
 
1799
                        nstyle.setFontSize(textSize);
 
1800
                        nstyle.setFillColor(CurrColorFill);
 
1801
                        nstyle.setTracking(textKern);
 
1802
                        nstyle.setFillShade(100);
 
1803
                        nstyle.setStrokeColor(CurrColorStroke);
 
1804
                        nstyle.setStrokeShade(100);
 
1805
                        nstyle.setScaleH(textScaleH);
 
1806
                        nstyle.setScaleV(textScaleV);
 
1807
                        nstyle.setBaselineOffset(0);
 
1808
                        nstyle.setShadowXOffset(50);
 
1809
                        nstyle.setShadowYOffset(-50);
 
1810
                        nstyle.setOutlineWidth(10);
 
1811
                        nstyle.setUnderlineOffset(-1);
 
1812
                        nstyle.setUnderlineWidth(-1);
 
1813
                        nstyle.setStrikethruOffset(-1);
 
1814
                        nstyle.setStrikethruWidth(-1);
 
1815
                        nstyle.setFeatures(StyleFlag(ScStyle_Default).featureList());
 
1816
                        int pot = textData.length();
 
1817
                        textData.insertChars(pot, ch);
 
1818
                        textData.applyCharStyle(pot, 1, nstyle);
 
1819
                        maxHeight += nstyle.font().height(nstyle.fontSize() / 10.0) + 2.0;
 
1820
                        tempW = 0;
 
1821
                }
 
1822
                else if (command == "Tf")
 
1823
                {
 
1824
                        ScTextStream gVals(&Cdata, QIODevice::ReadOnly);
 
1825
                        gVals >> textFont >> textSize;
 
1826
                        textFont.remove(0, 2);
 
1827
                        QString family = textFont;
 
1828
                        QString ret = "";
 
1829
                        family.replace( QRegExp( "'" ) , QChar( ' ' ) );
 
1830
                        textFont = m_Doc->toolSettings.defFont;
 
1831
                        bool found = false;
 
1832
                        SCFontsIterator it(PrefsManager::instance()->appPrefs.AvailFonts);
 
1833
                        for ( ; it.hasNext(); it.next())
 
1834
                        {
 
1835
                                QString fam;
 
1836
                                QString fn = it.current().scName();
 
1837
                                int pos = fn.indexOf(" ");
 
1838
                                fam = fn.left(pos);
 
1839
                                if (fam == family)
 
1840
                                {
 
1841
                                        found = true;
 
1842
                                        ret = fn;
 
1843
                                }
 
1844
                        }
 
1845
                        if (found)
 
1846
                                textFont = family;
 
1847
                        else
 
1848
                        {
 
1849
                                if (!PrefsManager::instance()->appPrefs.GFontSub.contains(family))
 
1850
                                {
 
1851
                                        qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor));
 
1852
                                        MissingFont *dia = new MissingFont(0, family, m_Doc);
 
1853
                                        dia->exec();
 
1854
                                        QString tmpf = dia->getReplacementFont();
 
1855
                                        delete dia;
 
1856
                                        qApp->changeOverrideCursor(QCursor(Qt::WaitCursor));
 
1857
                                        PrefsManager::instance()->appPrefs.GFontSub[family] = tmpf;
 
1858
                                }
 
1859
                                else
 
1860
                                        textFont = PrefsManager::instance()->appPrefs.GFontSub[family];
 
1861
                        }
 
1862
                        textSize *= 10.0;
 
1863
                }
 
1864
                else if (command == "TO")
 
1865
                {
 
1866
                        if (textData.length() > 0)
 
1867
                        {
 
1868
                                if (!((textData.length() == 1) && (textData.text(0) == SpecialChars::PARSEP)))
 
1869
                                {
 
1870
                                        QPointF pos = QPointF(textMatrix.dx(), textMatrix.dy());
 
1871
                                        pos += QPointF(m_Doc->currentPage()->xOffset(), -m_Doc->currentPage()->yOffset());
 
1872
                                        pos += QPointF(0.0, textSize / 10.0 + 2.0);
 
1873
                                        z = m_Doc->itemAdd(PageItem::TextFrame, PageItem::Unspecified, pos.x() - docX, docHeight - (pos.y() - docY), 10, 10, 0, CommonStrings::None, CommonStrings::None, true);
 
1874
                                        ite = m_Doc->Items->at(z);
 
1875
                                        ite->setTextToFrameDist(0.0, 0.0, 0.0, 0.0);
 
1876
                                        ite->itemText.append(textData);
 
1877
                                        double xpos = ite->xPos();
 
1878
                                        double ypos = ite->yPos();
 
1879
                                        ite->setWidthHeight(qMax(ite->width(), maxWidth), qMax(ite->height(), maxHeight));
 
1880
                                        double xoffset = 0.0, yoffset = 0.0;
 
1881
                                        double rotation = getRotationFromMatrix(textMatrix, 0.0);
 
1882
                                        if (rotation != 0.0)
 
1883
                                        {
 
1884
                                                double temp = textSize / 10.0 + 2.0;
 
1885
                                                xoffset = sin(rotation) * temp;
 
1886
                                                yoffset = cos(rotation) * temp;
 
1887
                                        }
 
1888
                                        ite->setXPos(xpos + xoffset);
 
1889
                                        ite->setYPos(ypos + yoffset);
 
1890
                                        ite->setRotation(rotation * 180 / M_PI);
 
1891
                                        ite->SetRectFrame();
 
1892
                                        m_Doc->setRedrawBounding(ite);
 
1893
                                        ite->Clip = FlattenPath(ite->PoLine, ite->Segments);
 
1894
                                        ite->setTextFlowMode(PageItem::TextFlowDisabled);
 
1895
                                        ite->setFillShade(CurrFillShade);
 
1896
                                        ite->setLineShade(CurrStrokeShade);
 
1897
                                        ite->setFillEvenOdd(fillRule);
 
1898
                                        ite->setFillTransparency(1.0 - Opacity);
 
1899
                                        ite->setLineTransparency(1.0 - Opacity);
 
1900
                                        ite->setLineEnd(CapStyle);
 
1901
                                        ite->setLineJoin(JoinStyle);
 
1902
                                        if (importerFlags & LoadSavePlugin::lfCreateDoc)
 
1903
                                                ite->setLocked(itemLocked);
 
1904
                                        if (patternMode)
 
1905
                                                PatternElements.append(ite);
 
1906
                                        else
 
1907
                                                Elements.append(ite);
 
1908
                                        if (groupStack.count() != 0)
 
1909
                                                groupStack.top().append(ite);
 
1910
                                }
 
1911
                        }
 
1912
                }
 
1913
/* End Text commands */
 
1914
/* Start special Commands */
 
1915
                else if (command == "*")
 
1916
                {
 
1917
                        Coords.resize(0);
 
1918
                        Coords.svgInit();
 
1919
                }
 
1920
                else if ((command == "N") || (command == "n"))
 
1921
                {
 
1922
                        Coords.resize(0);
 
1923
                        Coords.svgInit();
 
1924
                }
 
1925
                else if (command == "[")
 
1926
                {
 
1927
                        Coords.resize(0);
 
1928
                        Coords.svgInit();
 
1929
                        int an = Cdata.indexOf("(");
 
1930
                        int en = Cdata.lastIndexOf(")");
 
1931
                        if ((an != -1) && (en != -1))
 
1932
                        {
 
1933
                                patternMode = true;
 
1934
                                currentPatternDefName = Cdata.mid(an+1, en-an-1);
 
1935
                                currentPatternDefName.remove("\\");
 
1936
                                currentPatternDefName = currentPatternDefName.trimmed().simplified().replace(" ", "_");
 
1937
                                QString tmpS = Cdata.mid(en+1, Cdata.size() - en);
 
1938
                                ScTextStream gVals(&tmpS, QIODevice::ReadOnly);
 
1939
                                gVals >> patternX1 >> patternY1 >> patternX2 >> patternY2;
 
1940
                        }
 
1941
                }
 
1942
/* End special Commands */
 
1943
/* Skip everything else */
 
1944
//              else
 
1945
//                      qDebug(command);
 
1946
        }
 
1947
}
 
1948
 
 
1949
void AIPlug::processGradientData(QString data)
 
1950
{
 
1951
        QString command = "";
 
1952
        QString Cdata = "";
 
1953
        QStringList da;
 
1954
        getCommands(data, da);
 
1955
        for (int a = 0; a < da.count(); a++)
 
1956
        {
 
1957
                Cdata = da[a];
 
1958
                QStringList da2 = Cdata.split(" ", QString::SkipEmptyParts);
 
1959
                command = da2[da2.count()-1];
 
1960
                if (command == "Bd")
 
1961
                {
 
1962
                        QString tmpS = Cdata;
 
1963
                        int an = Cdata.indexOf("(");
 
1964
                        int en = Cdata.lastIndexOf(")");
 
1965
                        currentGradientName = Cdata.mid(an+1, en-an-1);
 
1966
                        currentGradientName.remove("\\");
 
1967
                        if (da2[da2.count()-3] == "0")
 
1968
                                currentGradient = VGradient(VGradient::linear);
 
1969
                        else
 
1970
                                currentGradient = VGradient(VGradient::radial);
 
1971
                        currentGradient.clearStops();
 
1972
                }
 
1973
                else if ((command == "%_Bs") || (command == "%_BS"))
 
1974
                {
 
1975
                        QString stopName = "";
 
1976
                        double stop = ScCLocale::toDoubleC(da2[da2.count()-2]) / 100.0;
 
1977
                        double colorShade = 100.0;
 
1978
                        int colortype = da2[da2.count()-4].toInt();
 
1979
                        if (colortype == 0)
 
1980
                        {
 
1981
                                stopName = parseColorGray(Cdata);
 
1982
                                const ScColor& gradC = m_Doc->PageColors[stopName];
 
1983
                                currentGradient.addStop( ScColorEngine::getRGBColor(gradC, m_Doc), stop, 0.5, 1.0, stopName, 100 );
 
1984
                        }
 
1985
                        else if (colortype == 1)
 
1986
                        {
 
1987
                                stopName = parseColor(Cdata);
 
1988
                                const ScColor& gradC = m_Doc->PageColors[stopName];
 
1989
                                currentGradient.addStop( ScColorEngine::getRGBColor(gradC, m_Doc), stop, 0.5, 1.0, stopName, 100 );
 
1990
                        }
 
1991
                        else if (colortype == 2)
 
1992
                        {
 
1993
                                stopName = parseColor(Cdata);
 
1994
                                const ScColor& gradC = m_Doc->PageColors[stopName];
 
1995
                                currentGradient.addStop( ScColorEngine::getRGBColor(gradC, m_Doc), stop, 0.5, 1.0, stopName, 100 );
 
1996
                        }
 
1997
                        else if (colortype == 3)
 
1998
                        {
 
1999
                                stopName = parseCustomColor(Cdata, colorShade);
 
2000
                                const ScColor& gradC = m_Doc->PageColors[stopName];
 
2001
                                currentGradient.addStop( ScColorEngine::getRGBColor(gradC, m_Doc), stop, 0.5, 1.0, stopName, qRound(colorShade));
 
2002
                        }
 
2003
                        else if (colortype == 4)
 
2004
                        {
 
2005
                                stopName = parseCustomColorX(Cdata, colorShade, "0");
 
2006
                                const ScColor& gradC = m_Doc->PageColors[stopName];
 
2007
                                currentGradient.addStop( ScColorEngine::getRGBColor(gradC, m_Doc), stop, 0.5, 1.0, stopName, qRound(colorShade));
 
2008
                        }
 
2009
                }
 
2010
                else if (command == "BD")
 
2011
                {
 
2012
                        m_gradients.insert(currentGradientName, currentGradient);
 
2013
                        currentGradient = VGradient(VGradient::linear);
 
2014
                        currentGradient.clearStops();
 
2015
                        currentGradient.setRepeatMethod( VGradient::none );
 
2016
                        currentGradientName = "";
 
2017
                }
 
2018
        }
 
2019
}
 
2020
 
 
2021
void AIPlug::processPattern(QDataStream &ts)
 
2022
{
 
2023
        QString tmp = "";
 
2024
        QString tmpData = "";
 
2025
        while (!ts.atEnd())
 
2026
        {
 
2027
                tmp = removeAIPrefix(readLinefromDataStream(ts));
 
2028
                if (patternMode)
 
2029
                {
 
2030
                        if (tmp == "EndPattern")
 
2031
                        {
 
2032
                                tmpSel->clear();
 
2033
                                if (PatternElements.count() > 0)
 
2034
                                {
 
2035
                                        for (int dre = 0; dre < PatternElements.count(); ++dre)
 
2036
                                        {
 
2037
                                                tmpSel->addItem(PatternElements.at(dre), true);
 
2038
                                        }
 
2039
                                        if (PatternElements.count() > 1)
 
2040
                                                m_Doc->itemSelection_GroupObjects(false, false, tmpSel);
 
2041
                                        ScPattern pat = ScPattern();
 
2042
                                        pat.setDoc(m_Doc);
 
2043
                                        PageItem* currItem = tmpSel->itemAt(0);
 
2044
                                        currItem->setItemName(currentPatternDefName);
 
2045
                                        currItem->AutoName = false;
 
2046
                                        m_Doc->DoDrawing = true;
 
2047
                                        QImage tmpImg = currItem->DrawObj_toImage();
 
2048
                                        QImage retImg = QImage(qRound(patternX2 - patternX1), qRound(patternY2 - patternY1), QImage::Format_ARGB32);
 
2049
                                        retImg.fill( qRgba(255, 255, 255, 0) );
 
2050
                                        QPainter p;
 
2051
                                        p.begin(&retImg);
 
2052
                                        if (PatternElements.count() > 1)
 
2053
                                                p.drawImage(qRound(-patternX1), qRound(-patternY1), tmpImg);
 
2054
                                        else
 
2055
                                                p.drawImage(0, 0, tmpImg);
 
2056
                                        p.end();
 
2057
                                        pat.pattern = retImg;
 
2058
//                                      pat.pattern = currItem->DrawObj_toImage();
 
2059
                                        m_Doc->DoDrawing = false;
 
2060
                                        //                      pat.width = currItem->gWidth;
 
2061
                                        //                      pat.height = currItem->gHeight;
 
2062
                                        pat.width = patternX2 - patternX1;
 
2063
                                        pat.height = patternY2 - patternY1;
 
2064
                                        pat.xoffset = -patternX1;
 
2065
                                        pat.yoffset = -patternY1;
 
2066
                                        for (int as = 0; as < tmpSel->count(); ++as)
 
2067
                                        {
 
2068
                                                PageItem* Neu = tmpSel->itemAt(as);
 
2069
                                                Neu->moveBy(-patternX1, -patternY1, true);
 
2070
                                                Neu->gXpos -= patternX1;
 
2071
                                                Neu->gYpos -= patternY1;
 
2072
                                                pat.items.append(Neu);
 
2073
                                        }
 
2074
                                        m_Doc->itemSelection_DeleteItem(tmpSel);
 
2075
                                        m_Doc->addPattern(currentPatternDefName, pat);
 
2076
                                }
 
2077
                                PatternElements.clear();
 
2078
                                currentPatternDefName = "";
 
2079
                                break;
 
2080
                        }
 
2081
                        else if (tmp.startsWith("Tile"))
 
2082
                                continue;
 
2083
                        else if (tmp.contains(") @"))
 
2084
                        {
 
2085
                                tmpData += tmp;
 
2086
                                tmpData.remove(") @");
 
2087
                                processData(tmpData);
 
2088
                                tmpData = "";
 
2089
                        }
 
2090
                        else if (tmp.contains(") &"))
 
2091
                        {
 
2092
                                tmpData += tmp;
 
2093
                                tmpData.remove(") &");
 
2094
                                processData(tmpData);
 
2095
                                tmpData = "";
 
2096
                        }
 
2097
                        else
 
2098
                        {
 
2099
                                if (tmp.startsWith("("))
 
2100
                                        tmp.remove(0, 1);
 
2101
                                tmpData += " "+tmp;
 
2102
                        }
 
2103
                }
 
2104
                else if (tmp == "EndPattern")
 
2105
                {
 
2106
                        PatternElements.clear();
 
2107
                        currentPatternDefName = "";
 
2108
                        break;
 
2109
                }
 
2110
                else
 
2111
                        processData(tmp);
 
2112
        }
 
2113
        patternMode = false;
 
2114
}
 
2115
 
 
2116
void AIPlug::processRaster(QDataStream &ts)
 
2117
{
 
2118
        double m1, m2, m3, m4, m5, m6, x1, y1, x2, y2, dummy;
 
2119
        int w, h, type, alpha, bin, bits;
 
2120
        QString tmp = "";
 
2121
        QString cumulated = "";
 
2122
        while (!ts.atEnd())
 
2123
        {
 
2124
                tmp = readLinefromDataStream(ts);
 
2125
                if (tmp.startsWith("%"))
 
2126
                        break;
 
2127
                tmp.remove("[");
 
2128
                tmp.remove("]");
 
2129
                if (!tmp.isEmpty())
 
2130
                        cumulated += " " + tmp;
 
2131
        }
 
2132
        QString Cdata = "";
 
2133
        QStringList da;
 
2134
        getCommands(cumulated, da);
 
2135
        Cdata = da[da.count()-1];
 
2136
        ScTextStream gVals(&Cdata, QIODevice::ReadOnly);
 
2137
        gVals >> m1 >> m2 >> m3 >> m4 >> m5 >> m6 >> x1 >> y1 >> x2 >> y2 >> w >> h >> bits >> type >> alpha >> dummy >> bin;
 
2138
//      qDebug() << QString("Matrix: %1 %2 %3 %4 %5 %6").arg(m1).arg(m2).arg(m3).arg(m4).arg(m5).arg(m6);
 
2139
//      qDebug() << QString("Bounds: %1 %2 %3 %4").arg(x1).arg(y1).arg(x2).arg(y2);
 
2140
//      qDebug() << QString("Size: %1 %2").arg(w).arg(h);
 
2141
//      qDebug() << QString("Bits: %1").arg(bits);
 
2142
//      qDebug() << QString("Typ: %1").arg(type);
 
2143
//      qDebug() << QString("Alpha-Channels: %1").arg(alpha);
 
2144
//      qDebug() << QString("Encoding: %1").arg(bin);
 
2145
        uint dataSize = w * h * (type + alpha);
 
2146
        uint alphaData = w * h * type;
 
2147
        bool cmyk = false;
 
2148
        if (type == 4)
 
2149
                cmyk = true;
 
2150
        if (tmp.startsWith("%%BeginData"))
 
2151
        {
 
2152
                QString dummyS;
 
2153
                ScTextStream gVals2(&tmp, QIODevice::ReadOnly);
 
2154
                tmp = readLinefromDataStream(ts);
 
2155
        }
 
2156
        QByteArray psdata;
 
2157
        psdata.resize(dataSize);
 
2158
        RawImage m_image;
 
2159
        if (type == 4)
 
2160
        {
 
2161
                if (alpha > 0)
 
2162
                        m_image.create(w, 1, 5);
 
2163
                else
 
2164
                        m_image.create(w, 1, 4);
 
2165
        }
 
2166
        else
 
2167
        {
 
2168
                if (alpha > 0)
 
2169
                        m_image.create(w, 1, 4);
 
2170
                else
 
2171
                        m_image.create(w, 1, 3);
 
2172
        }
 
2173
        bool first = false;
 
2174
        if (bin == 0) // 0 = ASCII encoded data
 
2175
        {
 
2176
                uint dataPointer = 0;
 
2177
                while (!ts.atEnd())
 
2178
                {
 
2179
                        if (first)
 
2180
                                tmp = readLinefromDataStream(ts);
 
2181
                        first = true;
 
2182
                        if (tmp.startsWith("%AI5_EndRaster"))
 
2183
                                break;
 
2184
                        for (int a = 1; a < tmp.length(); a += 2)
 
2185
                        {
 
2186
                                bool ok;
 
2187
                                ushort data = tmp.mid(a, 2).toUShort(&ok, 16);
 
2188
                                psdata[dataPointer++] = data;
 
2189
                        }
 
2190
                }
 
2191
        }
 
2192
        else
 
2193
        {
 
2194
                psdata.resize(dataSize);
 
2195
                ts.readRawData(psdata.data(), dataSize);
 
2196
        }
 
2197
        QMatrix imgMatrix = QMatrix(m1, m2, m3, m4, m5, m6);
 
2198
        QPointF pos = QPointF(imgMatrix.dx(), imgMatrix.dy());
 
2199
        pos += QPointF(m_Doc->currentPage()->xOffset(), -m_Doc->currentPage()->yOffset());
 
2200
        pos += QPointF(baseX, -baseY);
 
2201
        int z = m_Doc->itemAdd(PageItem::ImageFrame, PageItem::Unspecified, pos.x() - docX, docHeight - (pos.y() - docY), 10, 10, 0, CurrColorFill, CurrColorStroke, true);
 
2202
        PageItem* ite = m_Doc->Items->at(z);
 
2203
        ite->setWidthHeight(w * m1, h * m4);
 
2204
        double rotation = getRotationFromMatrix(imgMatrix, 0.0);
 
2205
        ite->setRotation(rotation * 180 / M_PI);
 
2206
        ite->SetRectFrame();
 
2207
        m_Doc->setRedrawBounding(ite);
 
2208
        ite->Clip = FlattenPath(ite->PoLine, ite->Segments);
 
2209
        ite->setTextFlowMode(PageItem::TextFlowDisabled);
 
2210
        ite->setFillShade(CurrFillShade);
 
2211
        ite->setLineShade(CurrStrokeShade);
 
2212
        ite->setFillEvenOdd(fillRule);
 
2213
        ite->setFillTransparency(1.0 - Opacity);
 
2214
        ite->setLineTransparency(1.0 - Opacity);
 
2215
        ite->setLineEnd(CapStyle);
 
2216
        ite->setLineJoin(JoinStyle);
 
2217
        uchar *p;
 
2218
        uint yCount = 0;
 
2219
        ite->tempImageFile = new QTemporaryFile(QDir::tempPath() + "/scribus_temp_ai_XXXXXX.tif");
 
2220
        ite->tempImageFile->open();
 
2221
        QString imgName = getLongPathName(ite->tempImageFile->fileName());
 
2222
        ite->tempImageFile->close();
 
2223
        ite->isInlineImage = true;
 
2224
        TIFF* tif = TIFFOpen(imgName.toLocal8Bit().data(), "w");
 
2225
        if (tif)
 
2226
        {
 
2227
                TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w);
 
2228
                TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h);
 
2229
                TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
 
2230
                TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, m_image.channels());
 
2231
                TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
 
2232
                if (type == 4)
 
2233
                        TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
 
2234
                else
 
2235
                        TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
 
2236
                TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
 
2237
                for (int y = 0; y < h; ++y)
 
2238
                {
 
2239
                        p = m_image.scanLine( 0 );
 
2240
                        for (int xh = 0; xh < m_image.width(); ++xh )
 
2241
                        {
 
2242
                                p[0] = psdata[yCount++];
 
2243
                                if (type > 1)
 
2244
                                {
 
2245
                                        p[1] = psdata[yCount++];
 
2246
                                        p[2] = psdata[yCount++];
 
2247
                                        if (type == 4)
 
2248
                                                p[3] = psdata[yCount++];
 
2249
                                }
 
2250
                                else
 
2251
                                {
 
2252
                                        p[1] = p[0];
 
2253
                                        p[2] = p[0];
 
2254
                                }
 
2255
                                if (alpha == 1)
 
2256
                                {
 
2257
                                        if (type == 4)
 
2258
                                                p[4] = psdata[alphaData++];
 
2259
                                        else
 
2260
                                                p[3] = psdata[alphaData++];
 
2261
                                }
 
2262
                                p += m_image.channels();
 
2263
                        }
 
2264
                        TIFFWriteScanline(tif, m_image.scanLine(0), y);
 
2265
                }
 
2266
                TIFFClose(tif);
 
2267
        }
 
2268
        m_Doc->LoadPict(imgName, z);
 
2269
        if (ite->PictureIsAvailable)
 
2270
                ite->setImageXYScale(ite->width() / ite->pixm.width(), ite->height() / ite->pixm.height());
 
2271
        if (importerFlags & LoadSavePlugin::lfCreateDoc)
 
2272
                ite->setLocked(itemLocked);
 
2273
        if (patternMode)
 
2274
                PatternElements.append(ite);
 
2275
        else
 
2276
                Elements.append(ite);
 
2277
        if (groupStack.count() != 0)
 
2278
                groupStack.top().append(ite);
 
2279
}
 
2280
 
 
2281
void AIPlug::processComment(QDataStream &ts, QString comment)
 
2282
{
 
2283
        QString tmp = removeAIPrefix(comment);
 
2284
        if (tmp.startsWith("Begin_NonPrinting"))
 
2285
        {
 
2286
                while (!ts.atEnd())
 
2287
                {
 
2288
                        tmp = removeAIPrefix(readLinefromDataStream(ts));
 
2289
                        if (tmp.startsWith("BeginGradient"))
 
2290
                        {
 
2291
                                while (!ts.atEnd())
 
2292
                                {
 
2293
                                        tmp = removeAIPrefix(readLinefromDataStream(ts));
 
2294
                                        if (tmp.startsWith("EndGradient"))
 
2295
                                                break;
 
2296
                                        else
 
2297
                                                processGradientData(tmp);
 
2298
                                }
 
2299
                        }
 
2300
                        if (tmp.startsWith("BeginPattern:"))
 
2301
                                processPattern(ts);
 
2302
                        if (tmp.startsWith("End_NonPrinting"))
 
2303
                                break;
 
2304
                        progressDialog->setProgress("GI", ts.device()->pos());
 
2305
                        qApp->processEvents();
 
2306
                }
 
2307
        }
 
2308
        else if (tmp.startsWith("BeginPattern:"))
 
2309
                processPattern(ts);
 
2310
        else if (tmp.startsWith("BeginGradient"))
 
2311
        {
 
2312
                while (!ts.atEnd())
 
2313
                {
 
2314
                        tmp = removeAIPrefix(readLinefromDataStream(ts));
 
2315
                        if (tmp.startsWith("EndGradient"))
 
2316
                                break;
 
2317
                        else
 
2318
                                processGradientData(tmp);
 
2319
                        progressDialog->setProgress("GI", ts.device()->pos());
 
2320
                        qApp->processEvents();
 
2321
                }
 
2322
        }
 
2323
        else if (tmp.startsWith("BeginPalette"))
 
2324
        {
 
2325
                while (!ts.atEnd())
 
2326
                {
 
2327
                        tmp = removeAIPrefix(readLinefromDataStream(ts));
 
2328
                        if (tmp.startsWith("EndPalette"))
 
2329
                                break;
 
2330
                        progressDialog->setProgress("GI", ts.device()->pos());
 
2331
                        qApp->processEvents();
 
2332
                }
 
2333
        }
 
2334
        else if (tmp.startsWith("BeginSymbol"))
 
2335
        {
 
2336
                while (!ts.atEnd())
 
2337
                {
 
2338
                        tmp = removeAIPrefix(readLinefromDataStream(ts));
 
2339
                        if (tmp.startsWith("EndSymbol"))
 
2340
                                break;
 
2341
                        progressDialog->setProgress("GI", ts.device()->pos());
 
2342
                        qApp->processEvents();
 
2343
                }
 
2344
        }
 
2345
        else if (tmp.startsWith("BeginDocumentData"))
 
2346
        {
 
2347
                while (!ts.atEnd())
 
2348
                {
 
2349
                        tmp = removeAIPrefix(readLinefromDataStream(ts));
 
2350
                        if (tmp.startsWith("EndDocumentData"))
 
2351
                                break;
 
2352
                        progressDialog->setProgress("GI", ts.device()->pos());
 
2353
                        qApp->processEvents();
 
2354
                }
 
2355
        }
 
2356
        else if (tmp.startsWith("%%BeginProlog"))
 
2357
        {
 
2358
                while (!ts.atEnd())
 
2359
                {
 
2360
                        tmp = removeAIPrefix(readLinefromDataStream(ts));
 
2361
                        if (tmp.startsWith("%%EndProlog"))
 
2362
                                break;
 
2363
                        progressDialog->setProgress("GI", ts.device()->pos());
 
2364
                        qApp->processEvents();
 
2365
                }
 
2366
        }
 
2367
        else if (tmp.startsWith("%%BeginData"))
 
2368
        {
 
2369
                while (!ts.atEnd())
 
2370
                {
 
2371
                        tmp = removeAIPrefix(readLinefromDataStream(ts));
 
2372
                        if (tmp.startsWith("%%EndData"))
 
2373
                                break;
 
2374
                        progressDialog->setProgress("GI", ts.device()->pos());
 
2375
                        qApp->processEvents();
 
2376
                }
 
2377
        }
 
2378
        else if (tmp.startsWith("%%BeginCrops"))
 
2379
        {
 
2380
                while (!ts.atEnd())
 
2381
                {
 
2382
                        tmp = removeAIPrefix(readLinefromDataStream(ts));
 
2383
                        if (tmp.startsWith("%%EndCrops"))
 
2384
                                break;
 
2385
                        progressDialog->setProgress("GI", ts.device()->pos());
 
2386
                        qApp->processEvents();
 
2387
                }
 
2388
        }
 
2389
        else if (tmp.startsWith("BeginRaster"))
 
2390
        {
 
2391
                processRaster(ts);
 
2392
                progressDialog->setProgress("GI", ts.device()->pos());
 
2393
                qApp->processEvents();
 
2394
        }
 
2395
        else if (tmp.startsWith("BeginLayer"))
 
2396
        {
 
2397
                while (!ts.atEnd())
 
2398
                {
 
2399
                        tmp = removeAIPrefix(readLinefromDataStream(ts));
 
2400
                        if (tmp.startsWith("BeginRaster"))
 
2401
                        {
 
2402
                                processRaster(ts);
 
2403
                                continue;
 
2404
                        }
 
2405
                        if (tmp.startsWith("EndLayer"))
 
2406
                                break;
 
2407
                        else
 
2408
                                processData(tmp);
 
2409
                        progressDialog->setProgress("GI", ts.device()->pos());
 
2410
                        qApp->processEvents();
 
2411
                }
 
2412
        }
 
2413
}
 
2414
 
 
2415
bool AIPlug::convert(QString fn)
 
2416
{
 
2417
        QString tmp;
 
2418
        LineW = 1.0;
 
2419
        Opacity = 1.0;
 
2420
        blendMode = 0;
 
2421
        CurrColorFill = "White";
 
2422
        CurrFillShade = 100.0;
 
2423
        CurrColorStroke = "Black";
 
2424
        CurrStrokeShade = 100.0;
 
2425
        JoinStyle = Qt::MiterJoin;
 
2426
        CapStyle = Qt::FlatCap;
 
2427
        DashPattern.clear();
 
2428
        DashOffset = 0.0;
 
2429
        fillRule = false;
 
2430
        FirstU = false;
 
2431
        WasU = false;
 
2432
        firstLayer = true;
 
2433
        patternMode = false;
 
2434
        meshMode = false;
 
2435
        itemLocked = false;
 
2436
        patternX1 = 0.0;
 
2437
        patternY1 = 0.0;
 
2438
        patternX2 = 0.0;
 
2439
        patternY2 = 0.0;
 
2440
        Coords.resize(0);
 
2441
        Coords.svgInit();
 
2442
        clipCoords.resize(0);
 
2443
        clipCoords.svgInit();
 
2444
        currentPoint = FPoint(0.0, 0.0);
 
2445
        currentLayer = 0;
 
2446
        currentGradient = VGradient(VGradient::linear);
 
2447
        currentGradient.clearStops();
 
2448
        currentGradient.setRepeatMethod( VGradient::none );
 
2449
        currentGradientName = "";
 
2450
        currentGradientMatrix = QMatrix();
 
2451
        currentGradientOrigin = QPointF(0.0, 0.0);
 
2452
        currentGradientAngle = 0.0;
 
2453
        currentGradientLenght = 1.0;
 
2454
        currentPatternName = "";
 
2455
        currentPatternX = 0.0;
 
2456
        currentPatternY = 0.0;
 
2457
        currentPatternXScale = 1.0;
 
2458
        currentPatternYScale = 1.0;
 
2459
        currentPatternRotation = 0.0;
 
2460
        QList<PageItem*> gElements;
 
2461
        groupStack.push(gElements);
 
2462
        commandList << "m" << "l" << "L" << "c" << "C" << "v" << "V" << "y" << "Y";             // Path construction
 
2463
        commandList << "b" << "B" << "f" << "F" << "s" << "S" << "*u" << "*U";                  // Object creation
 
2464
        commandList << "u" << "U" << "W" << "q" << "Q";                                                                 // Object creation
 
2465
        commandList << "A" << "w" << "j" << "J" << "Xy" << "XR";                                                // Graphic state
 
2466
        commandList << "k" << "K" << "Xa" << "XA" << "x" << "X" << "XX" << "Xx";                // Color commands
 
2467
        commandList << "Xk" << "g" << "G" << "p";                                                                               // Color commands
 
2468
        commandList << "Ln" << "Lb" << "LB";                                                                                    // Layer commands
 
2469
        commandList << "Bd" << "BD" << "%_Bs" << "Bg" << "Bb" << "BB" << "Bm" << "Xm";  // Gradient commands
 
2470
        commandList << "To" << "TO" << "Tf" << "Tp" << "Tx" << "TX" << "T*" << "Tk";    // Text commands
 
2471
        commandList << "Tc" << "Tz";                                                                                                    // Text commands
 
2472
        commandList << "XI" << "XG" << "Xh";                                                                                    // Image commands
 
2473
        commandList << "n" << "N" << "*" << "[";                                                                                // Special commands
 
2474
        commandList << "X!" << "X#";                                                                                                    // Mesh commands
 
2475
        commandList << "M" << "d" << "D" << "E";                                                                                // unimplemented
 
2476
        commandList << "h" << "H" << "i" << "I" << "Np" << "O";                                                 // unimplemented
 
2477
        commandList << "P" << "R";                                                                                                              // unimplemented
 
2478
        commandList << "XI" << "XF" << "XG" << "XT" << "Z" << "`" << "~" << "_" << "@"; // unimplemented
 
2479
        commandList << "&" << "*w" << "*W" << "Ap" << "Ar";                                                             // unimplemented
 
2480
        if(progressDialog)
 
2481
        {
 
2482
                progressDialog->setOverallProgress(2);
 
2483
                progressDialog->setLabel("GI", tr("Generating Items"));
 
2484
                qApp->processEvents();
 
2485
        }
 
2486
        QFile f(fn);
 
2487
        if (f.open(QIODevice::ReadOnly))
 
2488
        {
 
2489
                int fSize = (int) f.size();
 
2490
                if (progressDialog)
 
2491
                {
 
2492
                        progressDialog->setTotalSteps("GI", fSize);
 
2493
                        qApp->processEvents();
 
2494
                }
 
2495
                QDataStream ts(&f);
 
2496
                while (!ts.atEnd())
 
2497
                {
 
2498
                        tmp = readLinefromDataStream(ts);
 
2499
                        if (tmp.startsWith("%"))
 
2500
                                processComment(ts, tmp);
 
2501
                        else
 
2502
                                processData(tmp);
 
2503
                        progressDialog->setProgress("GI", ts.device()->pos());
 
2504
                        qApp->processEvents();
 
2505
                }
 
2506
        }
 
2507
        if (progressDialog)
 
2508
                progressDialog->close();
 
2509
        return true;
 
2510
}