summaryrefslogtreecommitdiffstats
path: root/filters/kword/rtf/import/rtfimport.h
blob: d70cf8fafe8c438d74bafa5db8c56acb64924c00 (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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
   This file is part of the KDE project
   Copyright (C) 2001 Ewald Snel <ewald@rambo.its.tudelft.nl>
   Copyright (C) 2001 Tomasz Grobelny <grotk@poczta.onet.pl>
   Copyright (C) 2003, 2004 Nicolas GOUTTE <goutte@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.
*/

// ### FIXME: copyright holders/date

#ifndef __RTFIMPORT_H__
#define __RTFIMPORT_H__

#include <KoFilter.h>
#include <tqasciidict.h>
#include <tqcolor.h>
#include <tqcstring.h>
#include <tqfile.h>
#include <tqfont.h>
#include <tqmap.h>
#include <tqstring.h>
#include <tqvaluelist.h>
#include <tqvaluestack.h>
#include <tqtextcodec.h>
#include "rtfimport_dom.h"
#include "rtfimport_tokenizer.h"
#include <tqptrlist.h>


class DomNode;
class RTFImport;


/// RTF property (control word table entry)
struct RTFProperty
{
    const char *onlyValidIn;
    const char *name;
    void (RTFImport::*cwproc)( struct RTFProperty * );
    /// offset of formatting property
    int offset;
    /// default value
    int value;
};

/// RTF field description
struct RTFField
{
    const char *id;
    int		type;
    int		subtype;
    const char *value;
};

struct RTFTextState;

/// RTF destination
struct RTFDestination
{
    const char *group;
    const char *name;
    void (RTFImport::*destproc)( RTFProperty * );
    RTFTextState* target;
};

/// Paragraph border
struct RTFBorder
{
    enum BorderStyle { Solid, Dashes, Dots, DashDot, DashDotDot, None = 16 };

    BorderStyle style;
    int color;
    int width;
    int space;
};

/// Tabulator
struct RTFTab
{
    enum TabType { Left, Centered, FlushRight, Decimal };
    enum LeaderType { None, Dots, Hyphens, Underline, ThickLine };

    TabType type;
    LeaderType leader;
    int position;
};

/// Font table entry
struct RTFFont
{
    TQString name;
    TQFont::StyleHint styleHint;
    int fixedPitch;
    int number;
};

/// RTF embedded picture
struct RTFPicture
{
    enum PictureType { BMP, WMF, MacPict, EMF, PNG, JPEG };

    TQByteArray bits;
    PictureType type;
    int width, height;
    int cropLeft, cropTop, cropRight, cropBottom;
    int desiredWidth, desiredHeight;
    int scalex, scaley;
    int nibble;
    bool scaled;
    /// \\blipuid
    TQString identifier;
};

/// Paragraph-formatting properties
struct RTFLayout
{
    enum Alignment { Left, Right, Justified, Centered };

    TQValueStack<RTFTab> tablist;
    RTFTab tab;
    RTFBorder borders[4];
    RTFBorder *border;
    Alignment alignment;
    int style;
    int firstIndent, leftIndent, rightIndent;
    int spaceBefore, spaceAfter;
    int spaceBetween; ///< Line spacing
    bool spaceBetweenMultiple; ///< Is the linespacing defined as multiple of line height (true) or by a height in 1/20 pt (false)?
    bool inTable;
    bool keep, keepNext;
    bool pageBB, pageBA;
};

/// Character-formatting properties
struct RTFFormat
{
    enum VertAlign { Normal = 0, SubScript, SuperScript };
    enum Underline { UnderlineNone=0, UnderlineSimple, UnderlineThick, UnderlineDouble, UnderlineWordByWord,
        UnderlineWave, UnderlineDash, UnderlineDot, UnderlineDashDot, UnderlineDashDotDot };

    VertAlign vertAlign;
    Underline underline;
    int font, fontSize, baseline;
    int color, bgcolor, underlinecolor;
    int uc;
    bool bold, italic, strike, striked;
    bool hidden, caps, smallCaps;
};

/// Comparison operator \since 1.4
inline bool operator == ( const RTFFormat& f1, const RTFFormat& f2 )
{
    return f1.vertAlign == f2.vertAlign && f1.underline == f2.underline
        && f1.font == f2.font && f1.fontSize == f2.fontSize && f1.baseline == f2.baseline
        && f1.color == f2.color && f1.bgcolor == f2.color && f1.underlinecolor == f2.underlinecolor
        && f1.uc == f2.uc
        && f1.bold == f2.bold && f1.italic == f2.italic && f1.strike == f2.strike && f1.striked == f2.striked
        && f1.hidden == f2.hidden && f1.caps == f2.caps && f1.smallCaps == f2.smallCaps
        ;
}
/// Comparison operator \since 1.4
inline bool operator != ( const RTFFormat& f1, const RTFFormat& f2 )
{
    return ! ( f1 == f2 );
}

/// Style sheet entry
struct RTFStyle
{
    TQString name;
    RTFFormat format;
    RTFLayout layout;
    int next;
};

/// Section-formatting properties
struct RTFSectionLayout
{
    int headerMargin;
    int footerMargin;
    bool titlePage;
};

/// Table cell definition
struct RTFTableCell
{
    RTFBorder borders[4];
    int bgcolor;
    int x;
};

/// Table-formatting properties
struct RTFTableRow
{
    TQValueList<RTFTableCell> cells;
    TQStringList frameSets;
    RTFLayout::Alignment alignment;
    int height;
    int left;
};

/// KWord format
struct KWFormat
{
    RTFFormat fmt;
    TQString xmldata;
    uint id, pos, len;
};

/// RTF rich text state (text and tables)
struct RTFTextState
{
    /// paragraphs
    DomNode node;
    /// table cell(s)
    DomNode cell;
    /// plain text (for paragraph or table cell)
    DomNode text;
    TQValueList<KWFormat> formats;
    TQStringList frameSets;
    TQValueList<RTFTableRow> rows;
    uint table, length;
};

/// RTF group state (formatting properties)
struct RTFGroupState
{
    RTFTableRow tableRow;
    RTFTableCell tableCell;
    RTFFormat format;
    RTFLayout layout;
    RTFSectionLayout section;
    /// '}' will close the current destination
    bool brace0;
    /// Should the group be ignored?
    bool ignoreGroup;
};


class RTFImport : public KoFilter
{
    Q_OBJECT
  

public:
    RTFImport( KoFilter *parent, const char *name, const TQStringList& );

    /**
     * Convert document from RTF to KWord format.
     * @param from the mimetype for RTF
     * @param to the mimetype for KWord
     * @return true if the document was successfully converted
     */
    virtual KoFilter::ConversionStatus convert( const TQCString& from, const TQCString& to );

    /**
     * Skip the keyword, as we do not need to do anything with it
     * (either because it is supported anyway or because we cannot support it.)
     */
    void ignoreKeyword( RTFProperty * );
    /**
     * Set document codepage.
     * @note Mac's code pages > 10000 are not supported
     */
    void setCodepage( RTFProperty * );
    /**
     * Set document codepage to Mac (also known as MacRoman or as Apple Roman)
     */
    void setMacCodepage( RTFProperty * );
    /**
     * Set document codepage to CP1252
     * @note Old RTF files have a \ansi keyword but no \ansicpg keyword
     */
    void setAnsiCodepage( RTFProperty * );
    /**
     * Set document codepage to IBM 850
     */
    void setPcaCodepage( RTFProperty * );
    /**
     * Set document codepage to IBM 435.
     * @note As TQt does not support IBM 435, this is currently approximated as IBM 850
     */
    void setPcCodepage( RTFProperty * );
    /**
     * Sets the value of a boolean RTF property specified by token.
     * @param property the property to set
     * @deprecated not portable, as it needs an out-of-specification use of offsetof
     */
    void setToggleProperty( RTFProperty * );
    /**
     * Sets a boolean RTF property specified by token.
     * @param property the property to set
     * @deprecated not portable, as it needs an out-of-specification use of offsetof
     */
    void setFlagProperty( RTFProperty *property );
    /**
     * Sets the charset.
     * @param property the property to set
     * @deprecated not portable, as it needs an out-of-specification use of offsetof
     */
    void setCharset( RTFProperty *property );
    /**
     * Sets the value of a numeric RTF property specified by token.
     * @param property the property to set
     * @deprecated not portable, as it assumes that an enum is a char
     */
    void setNumericProperty( RTFProperty *property );
    /**
     * Sets an enumeration (flag) RTF property specified by token.
     * @param property the property to set
     * @deprecated not portable, as it assumes that an enum is a char
     */
    void setEnumProperty( RTFProperty *property );
    /**
     * Set font style hint
     * @since 1.4
     */
    void setFontStyleHint( RTFProperty* property );
    /**
     * Set the picture type
     * (BMP, PNG...)
     * @since 1.4
     */
    void setPictureType( RTFProperty* property );
    /**
     * Sets the enumaration value for \\ul-type keywords
     * \\ul switches on simple underline
     * \\ul0 switches off all underlines
     * @since 1.4 (renamed)
     */
    void setSimpleUnderlineProperty( RTFProperty* );
    /**
     * Set underline properties
     * @param property the property to set
     * @since 1.4 (changed behaviour)
     */
    void setUnderlineProperty( RTFProperty* property );
    /**
     * Sets the value of a border property specified by token.
     * @param property the property to set
     */
    void setBorderProperty( RTFProperty *property );
    /**
     * Sets the value of a border color specified by token.
     * @deprecated not portable, as it needs an out-of-specification use of offsetof
     */
    void setBorderColor( RTFProperty * );
    /**
     * Sets the value of a border property specified by token.
     * @param property the property to set
     */
    void setBorderStyle( RTFProperty *property );
    /**
     * Sets the value of the font baseline (superscript).
     */
    void setUpProperty( RTFProperty * );
    /**
     * Reset character-formatting properties.
     */
    void setPlainFormatting( RTFProperty * = 0L );
    /**
     * Reset paragraph-formatting properties
     */
    void setParagraphDefaults( RTFProperty * = 0L );
    /**
     * Reset section-formatting properties.
     */
    void setSectionDefaults( RTFProperty * = 0L );
    /**
     * Reset table-formatting properties.
     */
    void setTableRowDefaults( RTFProperty * = 0L );
    /**
     * Select which border is the current one.
     * @param property the property to set
     */
    void selectLayoutBorder( RTFProperty * property );
    /**
     * Select which border is the current one, in case of a cell
     * @param property the property to set
     */
    void selectLayoutBorderFromCell( RTFProperty * property );
    void insertParagraph( RTFProperty * = 0L );
    void insertPageBreak( RTFProperty * );
    void insertTableCell( RTFProperty * );
    /**
     * Finish table row and calculate cell borders.
     */
    void insertTableRow( RTFProperty * = 0L );
    /**
     * Inserts a table cell definition.
     */
    void insertCellDef( RTFProperty * );
    /**
     * Inserts a tabulator definition.
     */
    void insertTabDef( RTFProperty * );
    /**
     * Inserts a single (Unicode) character in UTF8 format.
     * @param ch the character to write to the current destination
     */
    void insertUTF8( int ch );
    /// Insert special character (as plain text).
    void insertSymbol( RTFProperty *property );
    /// Insert special character (hexadecimal escape value).
    void insertHexSymbol( RTFProperty * );
    /// Insert unicode character (keyword \\u).
    void insertUnicodeSymbol( RTFProperty * );
    /**
     * Insert a date or time field
     */
    void insertDateTime( RTFProperty *property );
    /**
     * Insert a page number field
     */
    void insertPageNumber( RTFProperty * );
    /**
     * Parse the picture identifier
     */
    void parseBlipUid( RTFProperty* );
    /**
     * Parse recursive fields.
     * @note The {\fldrslt ...} group will be used for
     * unsupported and embedded fields.
     */
    void parseField( RTFProperty* );
    void parseFldinst( RTFProperty* );
    void parseFldrslt( RTFProperty* );
    /**
     * Font table destination callback
     */
    void parseFontTable( RTFProperty * );
    /**
     * This function parses footnotes
     * \todo Endnotes
     */
    void parseFootNote( RTFProperty * );
    /**
     * Style sheet destination callback.
     */
    void parseStyleSheet( RTFProperty * );
    /**
     * Color table destination callback.
     */
    void parseColorTable( RTFProperty * );
    /**
     * Picture destination callback.
     */
    void parsePicture( RTFProperty * );
    /**
     * Rich text destination callback.
     */
    void parseRichText( RTFProperty * );
    /**
     * Plain text destination callback.
     */
    void parsePlainText( RTFProperty * );
    /**
     * Do nothing special for this group
     */
    void parseGroup( RTFProperty * );
    /**
     * Discard all tokens until the current group is closed.
     */
    void skipGroup( RTFProperty * );
    /**
     * Change the destination.
     */
    void changeDestination( RTFProperty *property );

    /**
     * Reset formatting properties to their default settings.
     */
    void resetState();
    /**
     * Add anchor to current destination (see KWord DTD).
     * @param instance the frameset number in the document
     */
    void addAnchor( const char *instance );
    /**
     * Add format information to document node.
     * @param node the document node (destination)
     * @param format the format information
     * @param baseFormat the format information is based on this format
     */
    void addFormat( DomNode &node, const KWFormat& format, const RTFFormat* baseFormat );
    /**
     * Add layout information to document node.
     * @param node the document node (destination)
     * @param name the name of the current style
     * @param layout the paragraph layout information
     * @param frameBreak paragraph is always the last in a frame if true
     */
    void addLayout( DomNode &node, const TQString &name, const RTFLayout &layout, bool frameBreak );
    /**
     * Add paragraph information to document node.
     * @param node the document node (destination)
     * @param frameBreak paragraph is always the last in a frame if true
     */
    void addParagraph( DomNode &node, bool frameBreak );
    void addVariable(const DomNode& spec, int type, const TQString& key, const RTFFormat* fmt=0);
    void addImportedPicture( const TQString& rawFileName );
    /**
     *  Add a date/time field and split it for KWord
     * @param format format of the date/time
     * @param isDate is it a date field? (For the default format, if needed)
     */
    void addDateTime( const TQString& format, const bool isDate, RTFFormat& fmt );
    /**
     * Finish table and recalculate cell borders.
     */
    void finishTable();
    /**
     * Write out part (file inside the store).
     * @param name the internal name of the part
     * @param node the data to write
     */
    void writeOutPart( const char *name, const DomNode &node );


    RTFTokenizer token;
    DomNode frameSets;
    DomNode pictures;
    DomNode author, company, title, doccomm;
    RTFTextState bodyText;
    TQPtrList<RTFTextState> footnotes; ///< list of footnotes
    int fnnum; ///< number of last footnote
    RTFTextState firstPageHeader, oddPagesHeader, evenPagesHeader;
    RTFTextState firstPageFooter, oddPagesFooter, evenPagesFooter;
    /**
     * Dummy text state for destinations without own RTFTextState
     * @note this is mainly to avoid dangling or NULL pointers
     */
    RTFTextState m_dummyTextState;
    TQMap<int,TQString> fontTable;
    TQValueList<RTFStyle> styleSheet;
    TQValueList<TQColor> colorTable;
    TQValueStack<RTFGroupState> stateStack;
    TQValueStack<RTFDestination> destinationStack;
    RTFGroupState state;
    RTFDestination destination;
    RTFTextState *textState;
    RTFFont font;
    RTFStyle style;
    RTFPicture picture;
    RTFTableCell emptyCell;
    KWFormat kwFormat;
    TQAsciiDict<RTFProperty> properties;
    TQAsciiDict<RTFProperty> destinationProperties;
    uint table;
    uint pictureNumber; ///< Picture number; increase *before* use!

    // Color table and document-formatting properties
    int red, green, blue;
    int paperWidth, paperHeight;
    int leftMargin, topMargin, rightMargin, bottomMargin;
    int defaultTab, defaultFont;
    bool landscape, facingPages;

    // Field support
    TQCString fldinst, fldrslt;
    RTFFormat fldfmt;
    int flddst; ///< support for recursive fields
    TQString inFileName; ///< File name of the source file.
protected:
    TQTextCodec* textCodec; ///< currently used TQTextCodec by the RTF file
    TQTextCodec* utf8TextCodec; ///< TQTextCodec for UTF-8 (used in \u)
    TQMap<TQString,int> debugUnknownKeywords;
    bool m_batch; ///< Should the filter system be in batch mode (i.e. non-interactive)
};

#endif