summaryrefslogtreecommitdiffstats
path: root/filters/karbon/msod/msodimport.cc
blob: bd272271efc4958d1acc68472c82e80e0b891223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
    Copyright (C) 2000, S.R.Haque <shaheedhaque@hotmail.com>.
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.

DESCRIPTION
*/

#include <kdebug.h>
#include <tdetempfile.h>
#include <kmimetype.h>
#include <kgenericfactory.h>
#include <KoFilterChain.h>
#include <tqfile.h>
#include <msodimport.h>
#include <tqpointarray.h>

typedef KGenericFactory<MSODImport, KoFilter> MSODImportFactory;
K_EXPORT_COMPONENT_FACTORY( libmsodimport, MSODImportFactory( "kofficefilters" ) )

const int MSODImport::s_area = 30505;

MSODImport::MSODImport(
    KoFilter *,
    const char *,
    const TQStringList&) :
        KoEmbeddingFilter(), Msod(100)
{
}

MSODImport::~MSODImport()
{
}

KoFilter::ConversionStatus MSODImport::convert( const TQCString& from, const TQCString& to )
{
    if (to != "application/x-karbon" || from != "image/x-msod")
        return KoFilter::NotImplemented;

    // Get configuration data: the shape id, and any delay stream that we were given.
    unsigned shapeId;
    emit commSignalShapeID( shapeId );
    const char *delayStream = 0L;
    emit commSignalDelayStream( delayStream );
    kdDebug( s_area ) << "##################################################################" << endl;
    kdDebug( s_area ) << "shape id: " << shapeId << endl;
    kdDebug( s_area ) << "delay stream: " << delayStream << endl;
    kdDebug( s_area ) << "##################################################################" << endl;
/*
    TQString config = ""; // ###### FIXME: We aren't able to pass config data right now
    TQStringList args = TQStringList::split(";", config);
    unsigned i;

    kdDebug(s_area) << "MSODImport::filter: config: " << config << endl;
    for (i = 0; i < args.count(); i++)
    {
        if (args[i].startsWith("shape-id="))
        {
            shapeId = args[i].mid(9).toUInt();
        }
        else
        if (args[i].startsWith("delay-stream="))
        {
            delayStream = (const char *)args[i].mid(13).toULong();
        }
        else
        {
            kdError(s_area) << "Invalid argument: " << args[i] << endl;
            return KoFilter::StupidError;
        }
    }
*/
    // doc header
    m_text = "";
    m_text += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    m_text += "<!DOCTYPE DOC>\n";
    m_text += "<DOC mime=\"application/x-karbon\" syntaxVersion=\"0.1\" editor=\"WMF import filter\">\n";
    m_text += "  <LAYER name=\"Layer\" visible=\"1\">\n";

    if (!parse(shapeId, m_chain->inputFile(), delayStream))
        return KoFilter::WrongFormat;

    // close doc
    m_text += "  </LAYER>\n";
    m_text += "</DOC>\n";

    emit sigProgress(100);

    KoStoreDevice* dev = m_chain->storageFile( "root", KoStore::Write );
    if (!dev)
    {
        kdError(s_area) << "Cannot open output file" << endl;
        return KoFilter::StorageCreationError;
    }
    TQCString cstring ( m_text.utf8() );
    dev->writeBlock(cstring.data(), cstring.size()-1);

    return KoFilter::OK;
}

void MSODImport::gotEllipse(
    const DrawContext &/*dc*/,
    TQString /*type*/,
    TQPoint /*topLeft*/,
    TQSize /*halfAxes*/,
    unsigned /*startAngle*/,
    unsigned /*stopAngle*/)
{
// ### TODO
#if 0
    m_text += "<ellipse angle1=\"" + TQString::number(startAngle) +
                "\" angle2=\"" + TQString::number(stopAngle) +
                "\" x=\"" + TQString::number(topLeft.x()) +
                "\" y=\"" + TQString::number(topLeft.y()) +
                "\" kind=\"" + type +
                "\" rx=\"" + TQString::number(halfAxes.width()) +
                "\" ry=\"" + TQString::number(halfAxes.height()) +
                "\">\n";
    m_text += " <gobject fillcolor=\"#" + TQString::number(dc.m_brushColour, 16) +
                "\" fillstyle=\"" + TQString::number(1 /*m_winding*/) +
                "\" linewidth=\"" + TQString::number(dc.m_penWidth) +
                "\" strokecolor=\"#" + TQString::number(dc.m_penColour, 16) +
                "\" strokestyle=\"" + TQString::number(dc.m_penStyle) +
                "\">\n";
    m_text += "  <matrix dx=\"0\" dy=\"0\" m21=\"0\" m22=\"1\" m11=\"1\" m12=\"0\"/>\n";
    m_text += " </gobject>\n";
    m_text += "</ellipse>\n";
#endif
}

static void toRGB(int c, double &r, double &g, double &b)
{
	r = (c >> 16) / 255.0;
	g = ((c >> 8) & 0xFF) / 255.0;
	b = (c & 0xFF) / 255.0;
}

void MSODImport::gotPicture(
    unsigned key,
    TQString extension,
    unsigned length,
    const char *data)
{
// ### TODO
#if 0
    kdDebug() << "##########################################MSODImport::gotPicture" << endl;
    kdDebug() << "MSODImport::gotPicture -- " << extension << endl;
    if ((extension == "wmf") ||
        (extension == "emf") ||
        (extension == "pict"))
    {
        int partRef = internalPartReference( TQString::number( key ) );

        if (partRef == -1)
        {
            m_embeddeeData = data;
            m_embeddeeLength = length;

            TQString srcMime( KoEmbeddingFilter::mimeTypeByExtension( extension ) );
            if ( srcMime == KMimeType::defaultMimeType() )
                kdWarning( s_area ) << "Couldn't determine the mimetype from the extension" << endl;

            TQCString destMime; // intentionally empty, the filter manager will do the rest
            KoFilter::ConversionStatus status;
            partRef = embedPart( srcMime.latin1(), destMime, status, TQString::number( key ) );

            m_embeddeeData = 0;
            m_embeddeeLength = 0;

            if ( status != KoFilter::OK ) {
                kdWarning(s_area) << "Couldn't convert the image!" << endl;
                return;
            }
        }
        m_text += "<object url=\"" + TQString::number( partRef ) + "\" mime=\"";
        m_text += internalPartMimeType( TQString::number( key ) );
        m_text += "\" x=\"0\" y=\"0\" width=\"100\" height=\"200\"/>\n";
    }
    else
    {
        // We could not import it as a part. Try as an image.
        KTempFile tempFile( TQString(), '.' + extension );
        tempFile.file()->writeBlock( data, length );
        tempFile.close();

        m_text += "<pixmap src=\"" + tempFile.name() + "\">\n"
                    " <gobject fillstyle=\"0\" linewidth=\"1\" strokecolor=\"#000000\" strokestyle=\"1\">\n"
                    "  <matrix dx=\"0\" dy=\"0\" m21=\"0\" m22=\"1\" m11=\"1\" m12=\"0\"/>\n"
                    " </gobject>\n"
                    "</pixmap>\n";

        // Note that we cannot delete the file...
    }
#endif
}

void MSODImport::gotPolygon(
    const DrawContext &dc,
    const TQPointArray &points)
{
    kdDebug(s_area) << "MSODImport::gotPolygon" << endl;
    kdDebug(s_area) << TQString::number(dc.m_penWidth, 16) << endl;
    kdDebug(s_area) << dc.m_penStyle << endl;
    m_text += "<COMPOSITE>\n";
    if( dc.m_penWidth > 0 )
    {
        m_text += "<STROKE lineWidth=\"1\">\n";// + TQString::number(dc.m_penWidth, 16) + "\">\n";
        double r, g, b;
        toRGB(dc.m_penColour, r, g, b);
        m_text += "<COLOR v1=\"" + TQString::number(r) + "\" v2=\"" + TQString::number(g) + "\"  v3=\"" + TQString::number(b) + "\" opacity=\"1\" colorSpace=\"0\"  />\n";
    m_text += "</STROKE>\n";
    }
    else
        m_text += "<STROKE lineWidth=\"1\" />\n";
    m_text += "<FILL fillRule=\"" + TQString::number(dc.m_winding) + "\">\n";
    double r, g, b;
    toRGB(dc.m_brushColour, r, g, b);
    m_text += "<COLOR v1=\"" + TQString::number(r) + "\" v2=\"" + TQString::number(g) + "\"  v3=\"" + TQString::number(b) + "\" opacity=\"1\" colorSpace=\"0\"  />\n";
    m_text += "</FILL>\n";

    m_text += "<PATH isClosed=\"1\" >\n";
    pointArray(points);
    m_text += "</PATH>\n";
    m_text += "</COMPOSITE>\n";
}


void MSODImport::gotPolyline(
    const DrawContext &dc,
    const TQPointArray &points)
{
	kdDebug(s_area) << "MSODImport::gotPolyline" << endl;
	return; // ### TODO
    m_text += "<COMPOSITE>\n";
    m_text += "<STROKE lineWidth=\"" + TQString::number(dc.m_penWidth) + "\">\n";
    m_text += "</STROKE>\n";
    m_text += "<PATH isClosed=\"1\" >\n";
    pointArray(points);
    m_text += "</PATH>\n";
    m_text += "</COMPOSITE>\n";
}

void MSODImport::gotRectangle(
    const DrawContext &dc,
    const TQPointArray &points)
{
// ### TODO
#if 0
    TQRect bounds = points.boundingRect();

    m_text += "<rectangle width=\"" + TQString::number(bounds.width()) +
                "\" x=\"" + TQString::number(bounds.x()) +
                "\" y=\"" + TQString::number(bounds.y()) +
                "\" height=\"" + TQString::number(bounds.height()) +
                "\" rounding=\"0\">\n";
    m_text += "<polyline arrow1=\"0\" arrow2=\"0\">\n";
    pointArray(points);
    m_text += " <gobject fillcolor=\"#" + TQString::number(dc.m_brushColour, 16) +
                "\" fillstyle=\"" + TQString::number(1 /*m_winding*/) +
                "\" linewidth=\"" + TQString::number(dc.m_penWidth) +
                "\" strokecolor=\"#" + TQString::number(dc.m_penColour, 16) +
                "\" strokestyle=\"" + TQString::number(dc.m_penStyle) +
                "\">\n";
    m_text += "  <matrix dx=\"0\" dy=\"0\" m21=\"0\" m22=\"1\" m11=\"1\" m12=\"0\"/>\n";
    m_text += " </gobject>\n";
    m_text += "</polyline>\n";
    m_text += "</rectangle>\n";
#endif
}

void MSODImport::savePartContents( TQIODevice* file )
{
    if ( m_embeddeeData != 0 && m_embeddeeLength != 0 )
        file->writeBlock( m_embeddeeData, m_embeddeeLength );
}

void MSODImport::pointArray(
    const TQPointArray &points)
{

    m_text += "<MOVE x=\"" + TQString::number(points.point(0).x()) +
                "\" y=\"" + TQString::number(points.point(0).y()) +
                "\" />\n";
    kdDebug(s_area) << "\n<MOVE x=\"" + TQString::number(points.point(0).x()) +
                            "\" y=\"" + TQString::number(points.point(0).y()) +
                                        "\" />" << endl;
    for (unsigned int i = 1; i < points.count(); i++)
    {
        m_text += "<LINE x=\"" + TQString::number(points.point(i).x()) +
                    "\" y=\"" + TQString::number(points.point(i).y()) +
                    "\" />\n";
        kdDebug(s_area) << "<LINE x=\"" + TQString::number(points.point(i).x()) +
                            "\" y=\"" + TQString::number(points.point(i).y()) +
                                            "\" />" << endl;
    }

}

#include <msodimport.moc>