Subversion Repositories Scribus

Rev

Rev 25070 | 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 <QTextCodec>
10002 fschmid 24
#include <QTextStream>
10334 jghali 25
#include <QXmlStreamReader>
11713 fschmid 26
#include <QDebug>
10334 jghali 27
 
3 paul 28
#include <cstdlib>
14170 jghali 29
 
30
#include "commonstrings.h"
456 fschmid 31
#include "pageitem.h"
10660 cbradney 32
#include "pageitem_latexframe.h"
13839 fschmid 33
#ifdef HAVE_OSG
34
        #include "pageitem_osgframe.h"
35
#endif
2834 cbradney 36
#include "prefsmanager.h"
14170 jghali 37
#include "scclocale.h"
18028 jghali 38
#include "scmimedata.h"
16736 jghali 39
#include "scpage.h"
21526 craig 40
#include "scpaths.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
 
22686 jghali 68
bool ScriXmlDoc::readElemHeader(const QString& file, bool isFile, double *x, double *y, double *w, double *h)
3 paul 69
{
25070 craig 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;
24834 jghali 85
        QStringView tName;
10334 jghali 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();
24834 jghali 94
                        if ((tName == QLatin1String("SCRIBUSELEM")) || (tName == QLatin1String("SCRIBUSELEMUTF8")))
10334 jghali 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
 
23604 jghali 112
bool ScriXmlDoc::readElem(const QString& fileNameOrData, ScribusDoc *doc, double xPos, double yPos, bool isDataFromFile, bool loc)
3 paul 113
{
14291 jghali 114
        // Do not suppose the existence of layer with id = 0
22686 jghali 115
        // return readElemToLayer(fileName, avail, doc, Xp, Yp, Fi, loc, FontSub, view, 0);
23604 jghali 116
        return readElemToLayer(fileNameOrData, doc, xPos, yPos, isDataFromFile, loc, doc->activeLayer());
12907 fschmid 117
}
118
 
23604 jghali 119
bool ScriXmlDoc::readElemToLayer(const QString& fileNameOrData, ScribusDoc *doc, double xPos, double yPos, bool isDataFromFile, bool loc, int toLayer)
12907 fschmid 120
{
19891 craig 121
        QString elementData;
21526 craig 122
        QString fileDir = ScPaths::applicationDataDir();
19891 craig 123
        if (isDataFromFile)
167 Franz 124
        {
9535 fschmid 125
                QByteArray f;
19891 craig 126
                if (!loadRawText(fileNameOrData, f))
3 paul 127
                        return false;
6062 jghali 128
                if (f.left(16) == "<SCRIBUSELEMUTF8")
19891 craig 129
                        elementData = QString::fromUtf8(f.data());
3 paul 130
                else
19891 craig 131
                        elementData = f;
132
                fileDir = QFileInfo(fileNameOrData).absolutePath();
167 Franz 133
        }
3 paul 134
        else
167 Franz 135
        {
19891 craig 136
                elementData = fileNameOrData;
167 Franz 137
        }
19891 craig 138
        // In case elementData contains some old broken scribus xml
139
        elementData.replace(QChar(5), SpecialChars::PARSEP);
140
        elementData.replace(QChar(4), SpecialChars::TAB);
141
        elementData.replace(QChar(0), QChar(32));
142
        elementData.replace("&#x5;", SpecialChars::PARSEP);
143
        elementData.replace("&#x4;", SpecialChars::TAB);
16105 fschmid 144
        const FileFormat *fmt = LoadSavePlugin::getFormatById(FORMATID_SLA150IMPORT);
145
        if (fmt)
167 Franz 146
        {
23060 craig 147
                fmt->setupTargets(doc, nullptr, doc->scMW(), nullptr, &(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts));
19891 craig 148
                fmt->loadElements(elementData, fileDir, toLayer, xPos, yPos, loc);
16105 fschmid 149
                return true;
167 Franz 150
        }
16105 fschmid 151
        return false;
3 paul 152
}
153
 
22686 jghali 154
QString ScriXmlDoc::writeElem(ScribusDoc *doc, Selection* selection)
3 paul 155
{
7546 cbradney 156
        if (selection->count()==0)
25158 craig 157
                return QString();
16162 fschmid 158
        double xp, yp, wp, hp;
159
        PageItem *item;
25070 craig 160
        QString documentStr;
16162 fschmid 161
        item = selection->itemAt(0);
22673 jghali 162
 
163
        auto items = getItemsFromSelection(doc, selection);
164
 
16162 fschmid 165
        double selectionWidth = 0;
166
        double selectionHeight = 0;
167
        if (selection->isMultipleSelection())
168
        {
169
                double gx, gy, gw, gh;
170
                selection->getGroupRect(&gx, &gy, &gw, &gh);
18398 jghali 171
                xp = gx;
172
                yp = gy;
16162 fschmid 173
                wp = gw;
174
                hp = gh;
175
                selection->getVisualGroupRect(&gx, &gy, &selectionWidth, &selectionHeight);
176
        }
177
        else
178
        {
18398 jghali 179
                double minx =  std::numeric_limits<double>::max();
180
                double miny =  std::numeric_limits<double>::max();
181
                double maxx = -std::numeric_limits<double>::max();
182
                double maxy = -std::numeric_limits<double>::max();
183
                double x1, x2, y1, y2;
184
                item->getVisualBoundingRect(&x1, &y1, &x2, &y2);
185
                xp = qMin(minx, x1);
186
                yp = qMin(miny, y1);
187
                selectionWidth  = wp = qMax(maxx, x2) - xp;
188
                selectionHeight = hp = qMax(maxy, y2) - yp;
16162 fschmid 189
        }
190
        double scaleI = 50.0 / qMax(selectionWidth, selectionHeight);
25064 jghali 191
        QImage retImg(50, 50, QImage::Format_ARGB32_Premultiplied);
16162 fschmid 192
        retImg.fill( qRgba(0, 0, 0, 0) );
193
        ScPainter *painter = new ScPainter(&retImg, retImg.width(), retImg.height(), 1, 0);
194
        painter->setZoomFactor(scaleI);
22673 jghali 195
        for (int em = 0; em < items.count(); ++em)
16162 fschmid 196
        {
22673 jghali 197
                PageItem* embedded = items.at(em);
16162 fschmid 198
                painter->save();
18398 jghali 199
                painter->translate(-xp, -yp);
16162 fschmid 200
                embedded->invalid = true;
201
                embedded->DrawObj(painter, QRectF());
202
                painter->restore();
203
        }
18471 fschmid 204
        int pg = doc->OnPage(xp + wp / 2.0, yp + hp / 2.0);
18477 fschmid 205
        if (pg > -1)
18471 fschmid 206
        {
207
                xp = xp - doc->getXOffsetForPage(pg);
208
                yp = yp - doc->getYOffsetForPage(pg);
209
        }
16162 fschmid 210
        delete painter;
211
        QBuffer buffer;
212
        buffer.open(QIODevice::WriteOnly);
213
        retImg.save(&buffer, "PNG");
214
        QByteArray ba = buffer.buffer().toBase64();
215
        buffer.close();
216
        const FileFormat *fmt = LoadSavePlugin::getFormatById(FORMATID_SLA150EXPORT);
217
        if (fmt)
218
        {
23060 craig 219
                fmt->setupTargets(doc, nullptr, doc->scMW(), nullptr, &(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts));
16162 fschmid 220
                documentStr = fmt->saveElements(xp, yp, wp, hp, selection, ba);
221
        }
222
        return documentStr;
223
}
18028 jghali 224
 
22686 jghali 225
ScElemMimeData* ScriXmlDoc::writeToMimeData(ScribusDoc *doc, Selection *selection)
18028 jghali 226
{
227
        ScElemMimeData* md = new ScElemMimeData();
22686 jghali 228
        md->setScribusElem(writeElem(doc, selection));
18028 jghali 229
        return md;
230
}
22673 jghali 231
 
232
QList<PageItem*> ScriXmlDoc::getItemsFromSelection(ScribusDoc *doc, Selection* selection)
233
{
234
        QMap<int, PageItem*> items;
235
 
236
        const QList<PageItem*> selectedItems = selection->items();
237
        for (auto item : selectedItems)
238
                items.insert(doc->Items->indexOf(item), item);
239
        return items.values();
24729 craig 240
}