filters

msodimport.cpp
1/*
2 Copyright (C) 2000, S.R.Haque <shaheedhaque@hotmail.com>.
3 This file is part of the KDE project
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19
20DESCRIPTION
21*/
22
23#include <kdebug.h>
24#include <tdetempfile.h>
25#include <kmimetype.h>
26#include <kgenericfactory.h>
27#include <KoFilterChain.h>
28#include <tqfile.h>
29#include <msodimport.h>
30#include <tqpointarray.h>
31
32typedef KGenericFactory<MSODImport, KoFilter> MSODImportFactory;
33K_EXPORT_COMPONENT_FACTORY( libmsodimport, MSODImportFactory( "kofficefilters" ) )
34
35const int MSODImport::s_area = 30505;
36
37MSODImport::MSODImport(
38 KoFilter *,
39 const char *,
40 const TQStringList&) :
41 KoEmbeddingFilter(), Msod(100)
42{
43}
44
45MSODImport::~MSODImport()
46{
47}
48
49KoFilter::ConversionStatus MSODImport::convert( const TQCString& from, const TQCString& to )
50{
51 if (to != "application/x-karbon" || from != "image/x-msod")
52 return KoFilter::NotImplemented;
53
54 // Get configuration data: the shape id, and any delay stream that we were given.
55 unsigned shapeId;
56 emit commSignalShapeID( shapeId );
57 const char *delayStream = 0L;
58 emit commSignalDelayStream( delayStream );
59 kdDebug( s_area ) << "##################################################################" << endl;
60 kdDebug( s_area ) << "shape id: " << shapeId << endl;
61 kdDebug( s_area ) << "delay stream: " << delayStream << endl;
62 kdDebug( s_area ) << "##################################################################" << endl;
63/*
64 TQString config = ""; // ###### FIXME: We aren't able to pass config data right now
65 TQStringList args = TQStringList::split(";", config);
66 unsigned i;
67
68 kdDebug(s_area) << "MSODImport::filter: config: " << config << endl;
69 for (i = 0; i < args.count(); i++)
70 {
71 if (args[i].startsWith("shape-id="))
72 {
73 shapeId = args[i].mid(9).toUInt();
74 }
75 else
76 if (args[i].startsWith("delay-stream="))
77 {
78 delayStream = (const char *)args[i].mid(13).toULong();
79 }
80 else
81 {
82 kdError(s_area) << "Invalid argument: " << args[i] << endl;
83 return KoFilter::StupidError;
84 }
85 }
86*/
87 // doc header
88 m_text = "";
89 m_text += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
90 m_text += "<!DOCTYPE DOC>\n";
91 m_text += "<DOC mime=\"application/x-karbon\" syntaxVersion=\"0.1\" editor=\"WMF import filter\">\n";
92 m_text += " <LAYER name=\"Layer\" visible=\"1\">\n";
93
94 if (!parse(shapeId, m_chain->inputFile(), delayStream))
95 return KoFilter::WrongFormat;
96
97 // close doc
98 m_text += " </LAYER>\n";
99 m_text += "</DOC>\n";
100
101 emit sigProgress(100);
102
103 KoStoreDevice* dev = m_chain->storageFile( "root", KoStore::Write );
104 if (!dev)
105 {
106 kdError(s_area) << "Cannot open output file" << endl;
107 return KoFilter::StorageCreationError;
108 }
109 TQCString cstring ( m_text.utf8() );
110 dev->writeBlock(cstring.data(), cstring.size()-1);
111
112 return KoFilter::OK;
113}
114
115void MSODImport::gotEllipse(
116 const DrawContext &/*dc*/,
117 TQString /*type*/,
118 TQPoint /*topLeft*/,
119 TQSize /*halfAxes*/,
120 unsigned /*startAngle*/,
121 unsigned /*stopAngle*/)
122{
123// ### TODO
124#if 0
125 m_text += "<ellipse angle1=\"" + TQString::number(startAngle) +
126 "\" angle2=\"" + TQString::number(stopAngle) +
127 "\" x=\"" + TQString::number(topLeft.x()) +
128 "\" y=\"" + TQString::number(topLeft.y()) +
129 "\" kind=\"" + type +
130 "\" rx=\"" + TQString::number(halfAxes.width()) +
131 "\" ry=\"" + TQString::number(halfAxes.height()) +
132 "\">\n";
133 m_text += " <gobject fillcolor=\"#" + TQString::number(dc.m_brushColour, 16) +
134 "\" fillstyle=\"" + TQString::number(1 /*m_winding*/) +
135 "\" linewidth=\"" + TQString::number(dc.m_penWidth) +
136 "\" strokecolor=\"#" + TQString::number(dc.m_penColour, 16) +
137 "\" strokestyle=\"" + TQString::number(dc.m_penStyle) +
138 "\">\n";
139 m_text += " <matrix dx=\"0\" dy=\"0\" m21=\"0\" m22=\"1\" m11=\"1\" m12=\"0\"/>\n";
140 m_text += " </gobject>\n";
141 m_text += "</ellipse>\n";
142#endif
143}
144
145static void toRGB(int c, double &r, double &g, double &b)
146{
147 r = (c >> 16) / 255.0;
148 g = ((c >> 8) & 0xFF) / 255.0;
149 b = (c & 0xFF) / 255.0;
150}
151
152void MSODImport::gotPicture(
153 unsigned key,
154 TQString extension,
155 unsigned length,
156 const char *data)
157{
158// ### TODO
159#if 0
160 kdDebug() << "##########################################MSODImport::gotPicture" << endl;
161 kdDebug() << "MSODImport::gotPicture -- " << extension << endl;
162 if ((extension == "wmf") ||
163 (extension == "emf") ||
164 (extension == "pict"))
165 {
166 int partRef = internalPartReference( TQString::number( key ) );
167
168 if (partRef == -1)
169 {
170 m_embeddeeData = data;
171 m_embeddeeLength = length;
172
173 TQString srcMime( KoEmbeddingFilter::mimeTypeByExtension( extension ) );
174 if ( srcMime == KMimeType::defaultMimeType() )
175 kdWarning( s_area ) << "Couldn't determine the mimetype from the extension" << endl;
176
177 TQCString destMime; // intentionally empty, the filter manager will do the rest
178 KoFilter::ConversionStatus status;
179 partRef = embedPart( srcMime.latin1(), destMime, status, TQString::number( key ) );
180
181 m_embeddeeData = 0;
182 m_embeddeeLength = 0;
183
184 if ( status != KoFilter::OK ) {
185 kdWarning(s_area) << "Couldn't convert the image!" << endl;
186 return;
187 }
188 }
189 m_text += "<object url=\"" + TQString::number( partRef ) + "\" mime=\"";
190 m_text += internalPartMimeType( TQString::number( key ) );
191 m_text += "\" x=\"0\" y=\"0\" width=\"100\" height=\"200\"/>\n";
192 }
193 else
194 {
195 // We could not import it as a part. Try as an image.
196 KTempFile tempFile( TQString(), '.' + extension );
197 tempFile.file()->writeBlock( data, length );
198 tempFile.close();
199
200 m_text += "<pixmap src=\"" + tempFile.name() + "\">\n"
201 " <gobject fillstyle=\"0\" linewidth=\"1\" strokecolor=\"#000000\" strokestyle=\"1\">\n"
202 " <matrix dx=\"0\" dy=\"0\" m21=\"0\" m22=\"1\" m11=\"1\" m12=\"0\"/>\n"
203 " </gobject>\n"
204 "</pixmap>\n";
205
206 // Note that we cannot delete the file...
207 }
208#endif
209}
210
211void MSODImport::gotPolygon(
212 const DrawContext &dc,
213 const TQPointArray &points)
214{
215 kdDebug(s_area) << "MSODImport::gotPolygon" << endl;
216 kdDebug(s_area) << TQString::number(dc.m_penWidth, 16) << endl;
217 kdDebug(s_area) << dc.m_penStyle << endl;
218 m_text += "<COMPOSITE>\n";
219 if( dc.m_penWidth > 0 )
220 {
221 m_text += "<STROKE lineWidth=\"1\">\n";// + TQString::number(dc.m_penWidth, 16) + "\">\n";
222 double r, g, b;
223 toRGB(dc.m_penColour, r, g, b);
224 m_text += "<COLOR v1=\"" + TQString::number(r) + "\" v2=\"" + TQString::number(g) + "\" v3=\"" + TQString::number(b) + "\" opacity=\"1\" colorSpace=\"0\" />\n";
225 m_text += "</STROKE>\n";
226 }
227 else
228 m_text += "<STROKE lineWidth=\"1\" />\n";
229 m_text += "<FILL fillRule=\"" + TQString::number(dc.m_winding) + "\">\n";
230 double r, g, b;
231 toRGB(dc.m_brushColour, r, g, b);
232 m_text += "<COLOR v1=\"" + TQString::number(r) + "\" v2=\"" + TQString::number(g) + "\" v3=\"" + TQString::number(b) + "\" opacity=\"1\" colorSpace=\"0\" />\n";
233 m_text += "</FILL>\n";
234
235 m_text += "<PATH isClosed=\"1\" >\n";
236 pointArray(points);
237 m_text += "</PATH>\n";
238 m_text += "</COMPOSITE>\n";
239}
240
241
242void MSODImport::gotPolyline(
243 const DrawContext &dc,
244 const TQPointArray &points)
245{
246 kdDebug(s_area) << "MSODImport::gotPolyline" << endl;
247 return; // ### TODO
248 m_text += "<COMPOSITE>\n";
249 m_text += "<STROKE lineWidth=\"" + TQString::number(dc.m_penWidth) + "\">\n";
250 m_text += "</STROKE>\n";
251 m_text += "<PATH isClosed=\"1\" >\n";
252 pointArray(points);
253 m_text += "</PATH>\n";
254 m_text += "</COMPOSITE>\n";
255}
256
257void MSODImport::gotRectangle(
258 const DrawContext &dc,
259 const TQPointArray &points)
260{
261// ### TODO
262#if 0
263 TQRect bounds = points.boundingRect();
264
265 m_text += "<rectangle width=\"" + TQString::number(bounds.width()) +
266 "\" x=\"" + TQString::number(bounds.x()) +
267 "\" y=\"" + TQString::number(bounds.y()) +
268 "\" height=\"" + TQString::number(bounds.height()) +
269 "\" rounding=\"0\">\n";
270 m_text += "<polyline arrow1=\"0\" arrow2=\"0\">\n";
271 pointArray(points);
272 m_text += " <gobject fillcolor=\"#" + TQString::number(dc.m_brushColour, 16) +
273 "\" fillstyle=\"" + TQString::number(1 /*m_winding*/) +
274 "\" linewidth=\"" + TQString::number(dc.m_penWidth) +
275 "\" strokecolor=\"#" + TQString::number(dc.m_penColour, 16) +
276 "\" strokestyle=\"" + TQString::number(dc.m_penStyle) +
277 "\">\n";
278 m_text += " <matrix dx=\"0\" dy=\"0\" m21=\"0\" m22=\"1\" m11=\"1\" m12=\"0\"/>\n";
279 m_text += " </gobject>\n";
280 m_text += "</polyline>\n";
281 m_text += "</rectangle>\n";
282#endif
283}
284
285void MSODImport::savePartContents( TQIODevice* file )
286{
287 if ( m_embeddeeData != 0 && m_embeddeeLength != 0 )
288 file->writeBlock( m_embeddeeData, m_embeddeeLength );
289}
290
291void MSODImport::pointArray(
292 const TQPointArray &points)
293{
294
295 m_text += "<MOVE x=\"" + TQString::number(points.point(0).x()) +
296 "\" y=\"" + TQString::number(points.point(0).y()) +
297 "\" />\n";
298 kdDebug(s_area) << "\n<MOVE x=\"" + TQString::number(points.point(0).x()) +
299 "\" y=\"" + TQString::number(points.point(0).y()) +
300 "\" />" << endl;
301 for (unsigned int i = 1; i < points.count(); i++)
302 {
303 m_text += "<LINE x=\"" + TQString::number(points.point(i).x()) +
304 "\" y=\"" + TQString::number(points.point(i).y()) +
305 "\" />\n";
306 kdDebug(s_area) << "<LINE x=\"" + TQString::number(points.point(i).x()) +
307 "\" y=\"" + TQString::number(points.point(i).y()) +
308 "\" />" << endl;
309 }
310
311}
312
313#include <msodimport.moc>