Subversion Repositories Scribus

Rev

Rev 18477 | Rev 19891 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4430 cbradney 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
*/
3 paul 7
/***************************************************************************
8
                        scribusXml.cpp the document xml library for scribus
9
                             -------------------
10
    begin                : Sam Jul 14 10:00:00 CEST 2001
167 Franz 11
    copyright            : (C) 2001 by Christian T�p
3 paul 12
    email                : christian.toepp@mr-ct@gmx.de
13
 ***************************************************************************/
75 Franz 14
 
3 paul 15
#include "scribusXml.h"
10334 jghali 16
 
17
#include <QApplication>
18
#include <QByteArray>
12795 fschmid 19
#include <QBuffer>
10334 jghali 20
#include <QCursor>
21
#include <QDir>
10223 cbradney 22
#include <QFile>
23
#include <QRegExp>
24
#include <QTextCodec>
10002 fschmid 25
#include <QTextStream>
10334 jghali 26
#include <QXmlStreamReader>
11713 fschmid 27
#include <QDebug>
10334 jghali 28
 
3 paul 29
#include <cstdlib>
14170 jghali 30
 
31
#include "commonstrings.h"
456 fschmid 32
#include "pageitem.h"
10660 cbradney 33
#include "pageitem_latexframe.h"
13839 fschmid 34
#ifdef HAVE_OSG
35
        #include "pageitem_osgframe.h"
36
#endif
2834 cbradney 37
#include "prefsmanager.h"
14170 jghali 38
#include "scclocale.h"
18028 jghali 39
#include "scmimedata.h"
16736 jghali 40
#include "scpage.h"
13805 jghali 41
#include "scribusdoc.h"
14170 jghali 42
#include "selection.h"
43
#include "units.h"
128 Franz 44
 
1542 cbradney 45
#include <iostream>
46
 
2688 craig 47
#include "scconfig.h"
128 Franz 48
 
2531 craig 49
#include "util.h"
12277 cbradney 50
#include "util_text.h"
10203 cbradney 51
#include "util_math.h"
52
#include "util_color.h"
15023 jghali 53
#include "sclimits.h"
6402 fschmid 54
#include "scpattern.h"
13839 fschmid 55
#include "sctextstream.h"
12039 jghali 56
#include "scxmlstreamwriter.h"
12795 fschmid 57
#include "scpainter.h"
16105 fschmid 58
#include "fileloader.h"
59
#include "loadsaveplugin.h"
60
#include "plugins/formatidlist.h"
325 Franz 61
 
504 cbradney 62
using namespace std;
63
 
10334 jghali 64
ScriXmlDoc::ScriXmlDoc()
10009 jghali 65
{
10334 jghali 66
}
16163 fschmid 67
 
80 Franz 68
bool ScriXmlDoc::ReadElemHeader(QString file, bool isFile, double *x, double *y, double *w, double *h)
3 paul 69
{
6028 jghali 70
        QString ff = "";
3 paul 71
        if (isFile)
167 Franz 72
        {
9535 fschmid 73
                QByteArray f;
6013 jghali 74
                if (!loadRawText(file, f))
3 paul 75
                        return false;
6062 jghali 76
                if (f.left(16) == "<SCRIBUSELEMUTF8")
6013 jghali 77
                        ff = QString::fromUtf8(f.data());
3 paul 78
                else
79
                        ff = f;
167 Franz 80
        }
3 paul 81
        else
10334 jghali 82
                ff  = file;
83
 
84
        bool succeed = false;
85
        QStringRef tName;
86
        QXmlStreamReader sReader(ff);
87
        QXmlStreamReader::TokenType tType;
88
        while (!sReader.atEnd() && !sReader.hasError())
167 Franz 89
        {
10334 jghali 90
                tType = sReader.readNext();
91
                if (tType == QXmlStreamReader::StartElement)
92
                {
93
                        tName = sReader.name();
94
                        if ((tName == "SCRIBUSELEM") || (tName == "SCRIBUSELEMUTF8"))
95
                        {
14784 fschmid 96
                                QXmlStreamAttributes attrs = sReader.attributes();;
10334 jghali 97
                                QString attx = attrs.value("XP").toString();
98
                                QString atty = attrs.value("YP").toString();
99
                                QString attw = attrs.value("W").toString();
100
                                QString atth = attrs.value("H").toString();
13457 cbradney 101
                                *x = (attx.isEmpty()) ? 0.0 : ScCLocale::toDoubleC(attx);
102
                                *y = (atty.isEmpty()) ? 0.0 : ScCLocale::toDoubleC(atty);
103
                                *w = (attw.isEmpty()) ? 0.0 : ScCLocale::toDoubleC(attw);
104
                                *h = (atth.isEmpty()) ? 0.0 : ScCLocale::toDoubleC(atth);
10334 jghali 105
                                succeed = true;
106
                        }
107
                }
167 Franz 108
        }
10334 jghali 109
        return (succeed && !sReader.hasError());
3 paul 110
}
111
 
14744 fschmid 112
bool ScriXmlDoc::ReadElem(QString fileName, SCFonts &avail, ScribusDoc *doc, double Xp, double Yp, bool Fi, bool loc, QMap<QString,QString> &FontSub)
3 paul 113
{
14291 jghali 114
        // Do not suppose the existence of layer with id = 0
115
        // return ReadElemToLayer(fileName, avail, doc, Xp, Yp, Fi, loc, FontSub, view, 0);
14744 fschmid 116
        return ReadElemToLayer(fileName, avail, doc, Xp, Yp, Fi, loc, FontSub, doc->activeLayer());
12907 fschmid 117
}
118
 
14744 fschmid 119
bool ScriXmlDoc::ReadElemToLayer(QString fileName, SCFonts &avail, ScribusDoc *doc, double Xp, double Yp, bool Fi, bool loc, QMap<QString,QString> &FontSub, int toLayer)
12907 fschmid 120
{
10334 jghali 121
        QString ff;
10398 cbradney 122
        QString fileDir = QDir::homePath();
3 paul 123
        if (Fi)
167 Franz 124
        {
9535 fschmid 125
                QByteArray f;
6013 jghali 126
                if (!loadRawText(fileName, f))
3 paul 127
                        return false;
6062 jghali 128
                if (f.left(16) == "<SCRIBUSELEMUTF8")
6013 jghali 129
                        ff = QString::fromUtf8(f.data());
3 paul 130
                else
131
                        ff = f;
10508 cbradney 132
                fileDir = QFileInfo(fileName).absolutePath();
167 Franz 133
        }
3 paul 134
        else
167 Franz 135
        {
6062 jghali 136
                ff = fileName;
167 Franz 137
        }
10870 jghali 138
        // In case ff contains some old broken scribus xml
139
        ff.replace(QChar(5), SpecialChars::PARSEP);
140
        ff.replace(QChar(4), SpecialChars::TAB);
12185 fschmid 141
        ff.replace(QChar(0), QChar(32));
142
        ff.replace("&#x5;", SpecialChars::PARSEP);
143
        ff.replace("&#x4;", SpecialChars::TAB);
16105 fschmid 144
        const FileFormat *fmt = LoadSavePlugin::getFormatById(FORMATID_SLA150IMPORT);
145
        if (fmt)
167 Franz 146
        {
16105 fschmid 147
                fmt->setupTargets(doc, 0, doc->scMW(), 0, &(PrefsManager::instance()->appPrefs.fontPrefs.AvailFonts));
148
                fmt->loadElements(ff, fileDir, toLayer, Xp, Yp, loc);
149
                return true;
167 Franz 150
        }
16105 fschmid 151
        return false;
3 paul 152
}
153
 
14744 fschmid 154
QString ScriXmlDoc::WriteElem(ScribusDoc *doc, Selection* selection)
3 paul 155
{
7546 cbradney 156
        if (selection->count()==0)
157
                return "";
16162 fschmid 158
        double xp, yp, wp, hp;
159
        QString tmp, tmpy;
160
        PageItem *item;
161
        QString documentStr = "";
162
        item = selection->itemAt(0);
163
        QList<PageItem*> emG;
16163 fschmid 164
        QMap<int, PageItem*> emMap;
16162 fschmid 165
        emG.clear();
166
        for (int cor = 0; cor < selection->count(); ++cor)
167
        {
16943 fschmid 168
                emMap.insert(doc->Items->indexOf(selection->itemAt(cor)), selection->itemAt(cor));
16162 fschmid 169
        }
16163 fschmid 170
        emG = emMap.values();
16162 fschmid 171
        double selectionWidth = 0;
172
        double selectionHeight = 0;
173
        if (selection->isMultipleSelection())
174
        {
175
                double gx, gy, gw, gh;
176
                selection->getGroupRect(&gx, &gy, &gw, &gh);
18398 jghali 177
                xp = gx;
178
                yp = gy;
16162 fschmid 179
                wp = gw;
180
                hp = gh;
181
                selection->getVisualGroupRect(&gx, &gy, &selectionWidth, &selectionHeight);
182
        }
183
        else
184
        {
18398 jghali 185
                double minx =  std::numeric_limits<double>::max();
186
                double miny =  std::numeric_limits<double>::max();
187
                double maxx = -std::numeric_limits<double>::max();
188
                double maxy = -std::numeric_limits<double>::max();
189
                double x1, x2, y1, y2;
190
                item->getVisualBoundingRect(&x1, &y1, &x2, &y2);
191
                xp = qMin(minx, x1);
192
                yp = qMin(miny, y1);
193
                selectionWidth  = wp = qMax(maxx, x2) - xp;
194
                selectionHeight = hp = qMax(maxy, y2) - yp;
16162 fschmid 195
        }
196
        double scaleI = 50.0 / qMax(selectionWidth, selectionHeight);
197
        QImage retImg = QImage(50, 50, QImage::Format_ARGB32_Premultiplied);
198
        retImg.fill( qRgba(0, 0, 0, 0) );
199
        ScPainter *painter = new ScPainter(&retImg, retImg.width(), retImg.height(), 1, 0);
200
        painter->setZoomFactor(scaleI);
201
        for (int em = 0; em < emG.count(); ++em)
202
        {
203
                PageItem* embedded = emG.at(em);
204
                painter->save();
18398 jghali 205
                painter->translate(-xp, -yp);
16162 fschmid 206
                embedded->invalid = true;
207
                embedded->DrawObj(painter, QRectF());
208
                painter->restore();
209
        }
18471 fschmid 210
        int pg = doc->OnPage(xp + wp / 2.0, yp + hp / 2.0);
18477 fschmid 211
        if (pg > -1)
18471 fschmid 212
        {
213
                xp = xp - doc->getXOffsetForPage(pg);
214
                yp = yp - doc->getYOffsetForPage(pg);
215
        }
16162 fschmid 216
        delete painter;
217
        QBuffer buffer;
218
        buffer.open(QIODevice::WriteOnly);
219
        retImg.save(&buffer, "PNG");
220
        QByteArray ba = buffer.buffer().toBase64();
221
        buffer.close();
222
        const FileFormat *fmt = LoadSavePlugin::getFormatById(FORMATID_SLA150EXPORT);
223
        if (fmt)
224
        {
225
                fmt->setupTargets(doc, 0, doc->scMW(), 0, &(PrefsManager::instance()->appPrefs.fontPrefs.AvailFonts));
226
                documentStr = fmt->saveElements(xp, yp, wp, hp, selection, ba);
227
        }
228
        return documentStr;
229
}
18028 jghali 230
 
231
ScElemMimeData* ScriXmlDoc::WriteToMimeData(ScribusDoc *doc, Selection *selection)
232
{
233
        ScElemMimeData* md = new ScElemMimeData();
234
        md->setScribusElem(WriteElem(doc, selection));
235
        return md;
236
}