summaryrefslogtreecommitdiffstats
path: root/filters/kpresenter/kword/kprkword.cc
blob: fc0f50cd01846096f9e68e39924a8a542b61ef47 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* This file is part of the KDE project
   Copyright (C) 2001 David Faure <faure@kde.org>

   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.
*/

#include <config.h>

#include <kgenericfactory.h>
#include <KoStoreDevice.h>
#include <KoFilterChain.h>
#include <KoGlobal.h>
#include <kprkword.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tqsortedlist.h>
#include <tqcolor.h>

typedef KGenericFactory<KprKword, KoFilter> KprKwordFactory;
K_EXPORT_COMPONENT_FACTORY( libkprkword, KprKwordFactory( "kofficefilters" ) )

KprKword::KprKword(KoFilter *, const char *, const TQStringList&) :
    KoFilter(),
    outdoc( "DOC" )
{
}

// This filter can act as an import filter for KWord and as an export
// filter for KPresenter (isn't our architecture really nice ? :)
// This is why we use the file-to-file method, not a TQDomDoc one.
KoFilter::ConversionStatus KprKword::convert( const TQCString& from, const TQCString& to )
{
    if(to!="application/x-kword" || from!="application/x-kpresenter")
        return KoFilter::NotImplemented;

    KoStoreDevice* inpdev = m_chain->storageFile( "root", KoStore::Read );
    if ( !inpdev )
    {
        kdError(30502) << "Unable to open input stream" << endl;
        return KoFilter::StorageCreationError;
    }

    inpdoc.setContent( inpdev );


    outdoc.appendChild( outdoc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
    TQDomElement kwdoc = outdoc.createElement( "DOC" );
    kwdoc.setAttribute( "editor", "KprKword converter" );
    kwdoc.setAttribute( "mime", "application/x-kword" );
    kwdoc.setAttribute( "syntaxVersion", 1 );
    outdoc.appendChild( kwdoc );

    TQDomElement paper = outdoc.createElement( "PAPER" );
    kwdoc.appendChild( paper );
    paper.setAttribute( "format", 1 ); // A4. How on earth could I know what the user really wants ? :)
    paper.setAttribute( "width", 595 );
    paper.setAttribute( "height", 841 );
    TQDomElement borders = outdoc.createElement( "PAPERBORDERS" );
    paper.appendChild( borders );
    borders.setAttribute( "left", 28 );
    borders.setAttribute( "top", 42 );
    borders.setAttribute( "right", 28 );
    borders.setAttribute( "bottom", 42 );

    TQDomElement framesets = outdoc.createElement( "FRAMESETS" );
    kwdoc.appendChild( framesets );

    frameset = outdoc.createElement( "FRAMESET" );
    framesets.appendChild( frameset );
    frameset.setAttribute( "frameType", 1 ); // text
    frameset.setAttribute( "frameInfo", 0 ); // body
    TQDomElement frame = outdoc.createElement( "FRAME" );
    frameset.appendChild( frame );
    frame.setAttribute( "left", 28 );
    frame.setAttribute( "top", 42 );
    frame.setAttribute( "right", 566 );
    frame.setAttribute( "bottom", 798 );
    frame.setAttribute( "autoCreateNewFrame", 1 );
    frame.setAttribute( "newFrameBehaviour", 0 );

    titleStyleName = i18n("Slide Title");

    // Convert !

    convert();

    // Create a style for the slide titles

    TQDomElement styles = outdoc.createElement( "STYLES" );
    kwdoc.appendChild( styles );

    TQDomElement style = outdoc.createElement( "STYLE" );
    styles.appendChild( style );
    TQDomElement elem = outdoc.createElement( "NAME" );
    style.appendChild( elem );
    elem.setAttribute( "value", titleStyleName );
    elem = outdoc.createElement( "FOLLOWING" );
    style.appendChild( elem );
    elem.setAttribute( "name", "Standard" ); // no i18n here!

    TQDomElement counter = outdoc.createElement( "COUNTER" );
    style.appendChild( counter );
    counter.setAttribute( "type", 1 ); // numbered
    counter.setAttribute( "depth", 0 );
    counter.setAttribute( "start", 1 );
    counter.setAttribute( "numberingtype", 1 ); // chapter
    counter.setAttribute( "righttext", "." );

    TQDomElement format = outdoc.createElement( "FORMAT" );
    style.appendChild( format );
    TQDomElement font = outdoc.createElement( "FONT" );
    format.appendChild( font );
    font.setAttribute( "name", titleFont ); // found when reading the first title
    TQDomElement size = outdoc.createElement( "SIZE" );
    format.appendChild( size );
    size.setAttribute( "value", 24 );
    TQDomElement bold = outdoc.createElement( "WEIGHT" );
    format.appendChild( bold );
    bold.setAttribute( "value", 75 );

    // Create the standard style
    style = outdoc.createElement( "STYLE" );
    styles.appendChild( style );
    elem = outdoc.createElement( "NAME" );
    style.appendChild( elem );
    elem.setAttribute( "value", "Standard" ); // no i18n here!
    format = outdoc.createElement( "FORMAT" );
    style.appendChild( format ); // empty format == use defaults

    // Write output file

    KoStoreDevice* out = m_chain->storageFile( "root", KoStore::Write );
    if(!out) {
        kdError(30502) << "Unable to open output file!" << endl;
        return KoFilter::StorageCreationError;
    }
    TQCString cstring = outdoc.toCString(); // utf-8 already
    out->writeBlock( cstring.data(), cstring.length() );
    return KoFilter::OK;
}

// This class is used to sort the objects by y position
class KprObject {
 public:
    double y;
    TQDomElement elem;
    bool operator < ( const KprObject & c ) const
    {
        return y < c.y;
    }
    bool operator == ( const KprObject & c ) const
    {
        return y == c.y;
    }
};

void KprKword::convert()
{
    TQDomElement docElem = inpdoc.documentElement();
    TQDomElement paper = docElem.namedItem( "PAPER" ).toElement();
    int ptPageHeight = paper.attribute( "ptHeight" ).toInt();

    TQDomElement objects = docElem.namedItem( "OBJECTS" ).toElement();
    if ( objects.isNull() )
        return;

    TQSortedList< KprObject > objList;
    objList.setAutoDelete( true );

    TQDomNodeList lst = objects.elementsByTagName( "OBJECT" );
    uint lstcount = lst.count();
    for ( uint item = 0 ; item < lstcount ; ++item )
    {
        TQDomElement object = lst.item( item ).toElement();
        if ( object.attribute( "type" ).toInt() == 4 ) // we only care about text objs
        {
            TQDomElement orig = object.namedItem( "ORIG" ).toElement();
            if ( !orig.isNull() )
            {
                KprObject * obj = new KprObject;
                obj->y = orig.attribute( "y" ).toDouble();
                obj->elem = object;
                objList.inSort( obj );
            }
        }
    }

    int curPage = -1;
    //kdDebug() << "found " << objList.count() << " objects" << endl;

    for ( TQPtrListIterator<KprObject> it(objList); it.current(); ++it )
    {
        TQDomElement elem = it.current()->elem;
        // Detect the first object of each page
        int page = int( it.current()->y / ptPageHeight );
        bool isTitle = ( page > curPage );
        //kdDebug() << "KprKword::convert y=" << it.current()->y << " ptPageHeight=" << ptPageHeight
        //          << " isTitle=" << isTitle << endl;
        curPage = page;

        TQDomElement textObj = elem.namedItem( "TEXTOBJ" ).toElement();
        if (textObj.isNull())
            continue;
        // For each paragraph in this text object...
        TQDomNodeList lst = textObj.elementsByTagName( "P" );
        uint lstcount = lst.count();
        for ( uint item = 0; item < lstcount ; ++item )
        {
            TQDomElement p = lst.item( item ).toElement();

            // Create paragraph in KWord doc
            TQDomElement parag = outdoc.createElement( "PARAGRAPH" );
            frameset.appendChild( parag );

            TQDomElement outFormatsElem = outdoc.createElement( "FORMATS" );

            TQString text;
            // For each text element in the paragraph...
            TQDomElement textElem = p.firstChild().toElement();

            TQDomElement counter = p.namedItem( "COUNTER" ).toElement();
            TQDomElement indent=p.namedItem("INDENTS").toElement();
            TQDomElement lineSpacing=p.namedItem( "LINESPACING" ).toElement();
            TQDomElement offset=p.namedItem("OFFSETS").toElement();
            TQDomElement leftBorder = p.namedItem( "LEFTBORDER" ).toElement();
            TQDomElement rightBorder = p.namedItem( "RIGHTBORDER" ).toElement();
            TQDomElement topBorder = p.namedItem( "TOPBORDER" ).toElement();
            TQDomElement bottomBorder = p.namedItem( "BOTTOMBORDER" ).toElement();

            TQDomElement shadow=p.namedItem("SHADOW").toElement();

            for ( ; !textElem.isNull() ; textElem = textElem.nextSibling().toElement() )
            {
                int oldLen = text.length();
                text += textElem.text();
                //kdDebug() << "KprKword::convert text now " << text << endl;
                TQDomElement outFormatElem = outdoc.createElement( "FORMAT" );

                if ( textElem.attribute( "italic" ).toInt() )
                {
                    TQDomElement e = outdoc.createElement("ITALIC");
                    e.setAttribute( "value", 1 );
                    outFormatElem.appendChild( e );
                }
                TQColor underlineColor;
                if ( textElem.hasAttribute("underlinecolor" ))
                {
                    underlineColor =TQColor(textElem.attribute("underlinecolor" ));
                }
                TQString underlineStyleLine;
                if ( textElem.hasAttribute("underlinestyleline"))
                {
                    underlineStyleLine = textElem.attribute("underlinestyleline");
                }
                if ( textElem.hasAttribute("underline" ))
                {
                    TQDomElement e = outdoc.createElement("UNDERLINE");
                    TQString value = textElem.attribute( "underline" );
                     if ( value == "double" )
                     {
                         e.setAttribute( "value", "double" );
                     }
                     else if ( value == "single" )
                     {
                         e.setAttribute( "value", "double" );
                     }
                     else
                     {
                         e.setAttribute( "value", (bool)value.toInt() ? "1" :"0" );
                     }
                     if ( underlineColor.isValid())
                     {
                         e.setAttribute("underlinecolor", underlineColor.name());
                     }
                     if ( !underlineStyleLine.isEmpty() )
                         e.setAttribute("styleline", underlineStyleLine);
                     outFormatElem.appendChild( e );

                }

                TQString strikeOutStyleLine;
                if ( textElem.hasAttribute("strikeoutstyleline"))
                {
                    strikeOutStyleLine = textElem.attribute("strikeoutstyleline");
                }
                TQString strikeOutValue;
                if ( textElem.hasAttribute("strikeOut"))
                {
                    strikeOutValue = textElem.attribute("strikeOut");
                }

                if( !strikeOutValue.isEmpty())
                {
                    TQDomElement e = outdoc.createElement("STRIKEOUT");
                    e.setAttribute( "value", strikeOutValue );
                    if ( !strikeOutStyleLine.isEmpty())
                        e.setAttribute("styleline", strikeOutStyleLine);
                    outFormatElem.appendChild( e );
                }
                /*if ( textElem.attribute( "bold" ).toInt() )
                {
                    TQDomElement e = outdoc.createElement("WEIGHT");
                    e.setAttribute( "value", 75 );
                    outFormatElem.appendChild( e );
                }*/ // doesn't look good
                if ( titleFont.isEmpty() && isTitle )
                    titleFont = textElem.attribute( "family" );

                // Family and point size are voluntarily NOT passed over.
                if ( !textElem.attribute( "color" ).isEmpty())
                {
                    TQColor col;
                    col.setNamedColor(textElem.attribute( "color" ));
                    TQDomElement e = outdoc.createElement("COLOR");
                    e.setAttribute( "red", col.red() );
                    e.setAttribute( "green", col.green() );
                    e.setAttribute( "blue", col.blue() );
                    outFormatElem.appendChild( e );
                }
                if ( !textElem.attribute("textbackcolor").isEmpty())
                {
                    TQColor col;
                    col.setNamedColor(textElem.attribute( "textbackcolor" ));
                    TQDomElement e = outdoc.createElement("TEXTBACKGROUNDCOLOR");
                    e.setAttribute( "red", col.red() );
                    e.setAttribute( "green", col.green() );
                    e.setAttribute( "blue", col.blue() );
                    outFormatElem.appendChild( e );
                }

                //before VERTICAL align
                double relative = 0;
                if( textElem.attribute("relativetextsize").toDouble())
                {
                    relative = textElem.attribute("relativetextsize").toDouble();
                }


                if( textElem.attribute("VERTALIGN").toInt())
                {
                    TQDomElement e = outdoc.createElement("VERTALIGN");
                    e.setAttribute( "value", textElem.attribute("VERTALIGN").toInt() );
                    if ( relative != 0)
                        e.setAttribute( "relativetextsize", relative );
                    outFormatElem.appendChild( e );
                }

                if( textElem.hasAttribute("shadowtext"))
                {
                    TQDomElement e = outdoc.createElement("SHADOWTEXT");
                    e.setAttribute( "value", textElem.attribute("shadowtext").toInt() );
                    outFormatElem.appendChild( e );
                }

                if( textElem.hasAttribute("offsetfrombaseline"))
                {
                    TQDomElement e = outdoc.createElement("OFFSETFROMBASELINE");
                    e.setAttribute( "value", textElem.attribute("offsetfrombaseline").toInt() );
                    outFormatElem.appendChild( e );
                }

                if( textElem.hasAttribute("wordbyword"))
                {
                    TQDomElement e = outdoc.createElement("WORDBYWORD");
                    e.setAttribute( "value", textElem.attribute("wordbyword").toInt() );
                    outFormatElem.appendChild( e );
                }

                if( textElem.hasAttribute("fontattribute"))
                {
                    TQDomElement e = outdoc.createElement("FONTATTRIBUTE");
                    e.setAttribute( "value", textElem.attribute("fontattribute") );
                    outFormatElem.appendChild( e );
                }
                if( textElem.hasAttribute("language"))
                {
                    TQDomElement e = outdoc.createElement("LANGUAGE");
                    e.setAttribute( "value", textElem.attribute("language") );
                    outFormatElem.appendChild( e );
                }
                if ( !outFormatElem.firstChild().isNull() )
                {
                    outFormatElem.setAttribute( "id", 1 ); // normal exte
                    outFormatElem.setAttribute( "pos", oldLen );
                    outFormatElem.setAttribute( "len", text.length() - oldLen );
                    outFormatsElem.appendChild( outFormatElem );
                }

            } // end "for each text element"

            // KPresenter seems to save a trailing space (bug!)
            int len = text.length();
            if ( len > 0 && text[ len - 1 ] == ' ' )
                text.truncate( len - 1 );

            TQDomElement outTextElem = outdoc.createElement( "TEXT" );
            parag.appendChild( outTextElem );
            outTextElem.appendChild( outdoc.createTextNode( text ) );

            if ( !outFormatsElem.firstChild().isNull() ) // Do we have formats to save ?
                parag.appendChild( outFormatsElem );

            TQDomElement layoutElem = outdoc.createElement( "LAYOUT" );
            parag.appendChild( layoutElem );
            TQDomElement nameElem = outdoc.createElement( "NAME" );
            layoutElem.appendChild( nameElem );
            nameElem.setAttribute( "value", isTitle ? titleStyleName : TQString("Standard") );
            TQDomElement align=outdoc.createElement("FLOW");
            if(p.hasAttribute("align"))
            {
                switch(p.attribute( "align" ).toInt())
                {
                case 1:
                    align.setAttribute( "align","left");
                    break;
                case 2:
                    align.setAttribute( "align","right");
                    break;
                case 4:
                    align.setAttribute( "align","center");
                    break;
                case 8:
                    align.setAttribute( "align","justify");
                    break;
                }
            }
            if(!counter.isNull() )
                layoutElem.appendChild( counter );
            if(!indent.isNull())
                layoutElem.appendChild( indent );
            if(!lineSpacing.isNull())
                layoutElem.appendChild( lineSpacing );
            if(!offset.isNull())
                layoutElem.appendChild( offset);
            if(!leftBorder.isNull())
                layoutElem.appendChild(leftBorder);
            if(!rightBorder.isNull())
                layoutElem.appendChild(rightBorder);
            if(!topBorder.isNull())
                layoutElem.appendChild(topBorder);
            if(!bottomBorder.isNull())
                layoutElem.appendChild(bottomBorder);
            if(!align.isNull())
                layoutElem.appendChild(align);
            if(!shadow.isNull())
                layoutElem.appendChild(shadow);
            // Only the first parag of the top text object is set to the 'title' style
            isTitle = false;
        }
    }
}

#include <kprkword.moc>