summaryrefslogtreecommitdiff
path: root/writerfilter/source/rtftok/rtfdocumentimpl.hxx
blob: 90ecc98bd4ee87d400e2f469b16c2a1112f3ce72 (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
/*
 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Initial Developer of the Original Code is
 *       Miklos Vajna <vmiklos@frugalware.org>
 * Portions created by the Initial Developer are Copyright (C) 2011 the
 * Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
 * instead of those above.
 */

#ifndef _RTFDOCUMENTIMPL_HXX_
#define _RTFDOCUMENTIMPL_HXX_

#include <stack>
#include <queue>

#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/document/XDocumentProperties.hpp>
#include <oox/helper/graphichelper.hxx>
#include <rtl/strbuf.hxx>

#include <rtftok/RTFDocument.hxx>
#include <rtfreferencetable.hxx>
#include <rtfsprm.hxx>

class SvStream;

namespace writerfilter {
    namespace rtftok {
        enum RTFInternalState
        {
            INTERNAL_NORMAL,
            INTERNAL_BIN,
            INTERNAL_HEX
        };

        // Note that this is not a 1:1 mapping between destination control
        // words, e.g. RTF_PICT gets mapped to DESTINATION_PICT or
        // DESTINATION_SHAPEPROPERTYVALUEPICT, etc.
        enum RTFDesitnationState
        {
            DESTINATION_NORMAL,
            DESTINATION_SKIP,
            DESTINATION_FONTTABLE,
            DESTINATION_FONTENTRY,
            DESTINATION_COLORTABLE,
            DESTINATION_STYLESHEET,
            DESTINATION_STYLEENTRY,
            DESTINATION_EQINSTRUCTION,
            DESTINATION_FIELDINSTRUCTION,
            DESTINATION_FIELDRESULT,
            DESTINATION_LISTTABLE,
            DESTINATION_LISTENTRY,
            DESTINATION_LISTOVERRIDETABLE,
            DESTINATION_LISTOVERRIDEENTRY,
            DESTINATION_LISTLEVEL,
            DESTINATION_LEVELTEXT,
            DESTINATION_LEVELNUMBERS,
            DESTINATION_SHPPICT,
            DESTINATION_PICT,
            DESTINATION_PICPROP,
            DESTINATION_SHAPEPROPERTY,
            DESTINATION_SHAPEPROPERTYNAME,
            DESTINATION_SHAPEPROPERTYVALUE,
            DESTINATION_SHAPE,
            DESTINATION_SHAPEINSTRUCTION,
            DESTINATION_SHAPEPROPERTYVALUEPICT,
            DESTINATION_NESTEDTABLEPROPERTIES,
            DESTINATION_FOOTNOTE,
            DESTINATION_BOOKMARKSTART,
            DESTINATION_BOOKMARKEND,
            DESTINATION_REVISIONTABLE,
            DESTINATION_REVISIONENTRY,
            DESTINATION_SHAPETEXT,
            DESTINATION_FORMFIELD,
            DESTINATION_FORMFIELDNAME,
            DESTINATION_FORMFIELDLIST,
            DESTINATION_DATAFIELD,
            DESTINATION_INFO,
            DESTINATION_CREATIONTIME,
            DESTINATION_REVISIONTIME,
            DESTINATION_PRINTTIME,
            DESTINATION_AUTHOR,
            DESTINATION_OPERATOR,
            DESTINATION_COMPANY,
            DESTINATION_COMMENT,
            DESTINATION_OBJECT,
            DESTINATION_OBJDATA,
            DESTINATION_RESULT,
            DESTINATION_ANNOTATIONDATE,
            DESTINATION_ANNOTATIONAUTHOR,
            DESTINATION_FALT,
            DESTINATION_FLYMAINCONTENT
        };

        enum RTFBorderState
        {
            BORDER_NONE,
            BORDER_PARAGRAPH,
            BORDER_CELL,
            BORDER_PAGE
        };

        enum RTFErrors
        {
            ERROR_OK,
            ERROR_GROUP_UNDER,
            ERROR_GROUP_OVER,
            ERROR_EOF,
            ERROR_HEX_INVALID,
            ERROR_CHAR_OVER
        };

        /// Minimalistic buffer of elements for nested cells.
        enum RTFBufferTypes
        {
            BUFFER_PROPS,
            BUFFER_CELLEND,
            BUFFER_STARTRUN,
            BUFFER_TEXT,
            BUFFER_UTEXT,
            BUFFER_ENDRUN,
            BUFFER_PAR
        };

        /// Form field types
        enum RTFFormFieldTypes
        {
            FORMFIELD_NONE,
            FORMFIELD_TEXT,
            FORMFIELD_CHECKBOX,
            FORMFIELD_LIST
        };

        enum RTFBmpStyles
        {
            BMPSTYLE_NONE,
            BMPSTYLE_PNG
        };

        /// A buffer storing dmapper calls.
        typedef std::deque< std::pair<RTFBufferTypes, RTFValue::Pointer_t> > RTFBuffer_t;

        /// An entry in the color table.
        class RTFColorTableEntry
        {
            public:
                RTFColorTableEntry();
                sal_uInt8 nRed;
                sal_uInt8 nGreen;
                sal_uInt8 nBlue;
        };

        /// Stores the properties of a shape.
        class RTFShape
        {
            public:
                std::vector< std::pair<rtl::OUString, rtl::OUString> > aProperties;
                int nLeft;
                int nTop;
                int nRight;
                int nBottom;
        };

        /// Stores the properties of a picture.
        class RTFPicture
        {
            public:
                RTFPicture();
                sal_uInt16 nWidth, nHeight;
                sal_uInt16 nGoalWidth, nGoalHeight;
                sal_uInt16 nScaleX, nScaleY;
                short nCropT, nCropB, nCropL, nCropR;
                sal_uInt16 eWMetafile;
                RTFBmpStyles nStyle;
        };

        /// Stores the properties of a frame
        class RTFFrame
        {
            public:
                RTFFrame();
                sal_Int32 nX, nY, nW, nH;
                sal_Int32 nHoriPadding, nVertPadding;
                sal_Int32 nHoriAlign, nHoriAnchor, nVertAlign, nVertAnchor;
                sal_Int16 nAnchorType;

                /// Convert the stored properties to Sprms
                RTFSprms getSprms();
                bool hasProperties();
        };

        /// State of the parser, which gets saved / restored when changing groups.
        class RTFParserState
        {
            public:
                RTFParserState();
                RTFInternalState nInternalState;
                RTFDesitnationState nDestinationState;
                RTFBorderState nBorderState;
                // font table, stylesheet table
                RTFSprms aTableSprms;
                RTFSprms aTableAttributes;
                // reset by plain
                RTFSprms aCharacterSprms;
                RTFSprms aCharacterAttributes;
                // reset by pard
                RTFSprms aParagraphSprms;
                RTFSprms aParagraphAttributes;
                // reset by sectd
                RTFSprms aSectionSprms;
                RTFSprms aSectionAttributes;
                // reset by trowd
                RTFSprms aTableRowSprms;
                RTFSprms aTableRowAttributes;
                // reset by cellx
                RTFSprms aTableCellSprms;
                RTFSprms aTableCellAttributes;
                // reset by row/nestrow
                std::deque<RTFSprms> aTableCellsSprms;
                std::deque<RTFSprms> aTableCellsAttributes;
                // backup of the above two, to support inheriting cell props
                std::deque<RTFSprms> aTableInheritingCellsSprms;
                std::deque<RTFSprms> aTableInheritingCellsAttributes;
                // reset by tx
                RTFSprms aTabAttributes;

                RTFColorTableEntry aCurrentColor;

                rtl_TextEncoding nCurrentEncoding;

                /// Current \uc value.
                int nUc;
                /// Characters to skip, set to nUc by \u.
                int nCharsToSkip;

                /// Next list level index to use when parsing list table.
                int nListLevelNum;
                /// List level entries, which will form a list entry later.
                RTFSprms aListLevelEntries;

                /// List of character positions in leveltext to replace.
                std::vector<sal_Int32> aLevelNumbers;

                RTFPicture aPicture;
                RTFShape aShape;
                RTFFrame aFrame;

                /// Current cellx value.
                int nCellX;
                int nCells;
                int nInheritingCells;

                /// CJK or CTL?
                bool bIsCjk;

                // Info group.
                int nYear;
                int nMonth;
                int nDay;
                int nHour;
                int nMinute;

                /// Text from special destinations.
                rtl::OUStringBuffer aDestinationText;
        };

        class RTFTokenizer;
        class RTFSdrImport;

        /// Implementation of the RTFDocument interface.
        class RTFDocumentImpl
            : public RTFDocument
        {
            public:
                typedef ::boost::shared_ptr<RTFDocumentImpl> Pointer_t;
                RTFDocumentImpl(uno::Reference<uno::XComponentContext> const& xContext,
                                uno::Reference<io::XInputStream> const& xInputStream,
                                uno::Reference<lang::XComponent> const& xDstDoc,
                                uno::Reference<frame::XFrame> const& xFrame,
                                uno::Reference<task::XStatusIndicator> const& xStatusIndicator);
                virtual ~RTFDocumentImpl();
                virtual void resolve(Stream & rHandler);
                virtual std::string getType() const;

                Stream& Mapper();
                void setSubstream(bool bIsSubtream);
                void setAuthor(rtl::OUString& rAuthor);
                bool isSubstream() const;
                void finishSubstream();
                void setIgnoreFirst(rtl::OUString& rIgnoreFirst);
                void seek(sal_uInt32 nPos);
                uno::Reference<lang::XMultiServiceFactory> getModelFactory();
                RTFParserState& getState();
                /// If the stack of states is empty.
                bool isEmpty() const;
                int getGroup() const;
                void setDestinationText(rtl::OUString& rString);
                /// Resolve a picture: If not inline, then anchored.
                int resolvePict(bool bInline);
                void runBreak();
                void replayShapetext();
                bool getSkipUnknown();
                void setSkipUnknown(bool bSkipUnknown);

                // These callbacks are invoked by the tokenizer.
                int resolveChars(char ch);
                int pushState();
                int popState();
                int dispatchFlag(RTFKeyword nKeyword);
                int dispatchDestination(RTFKeyword nKeyword);
                int dispatchSymbol(RTFKeyword nKeyword);
                int dispatchToggle(RTFKeyword nKeyword, bool bParam, int nParam);
                int dispatchValue(RTFKeyword nKeyword, int nParam);

            private:
                SvStream& Strm();
                sal_uInt32 getColorTable(sal_uInt32 nIndex);
                sal_uInt32 getEncodingTable(sal_uInt32 nFontIndex);
                RTFSprms mergeSprms();
                RTFSprms mergeAttributes();
                void resetSprms();
                void resetAttributes();
                void resolveSubstream(sal_uInt32 nPos, Id nId);
                void resolveSubstream(sal_uInt32 nPos, Id nId, rtl::OUString& rIgnoreFirst);

                void text(rtl::OUString& rString);
                void parBreak();
                void tableBreak();
                /// If this is the first run of the document, starts the initial paragraph.
                void checkFirstRun();
                void checkNeedPap();
                void sectBreak(bool bFinal);
                void replayBuffer(RTFBuffer_t& rBuffer);
                /// If we got tokens indicating we're in a frame.
                bool inFrame();
                /// If we have some unicode characters to send.
                void checkUnicode();

                uno::Reference<uno::XComponentContext> const& m_xContext;
                uno::Reference<io::XInputStream> const& m_xInputStream;
                uno::Reference<lang::XComponent> const& m_xDstDoc;
                uno::Reference<frame::XFrame> const& m_xFrame;
                uno::Reference<task::XStatusIndicator> const& m_xStatusIndicator;
                uno::Reference<lang::XMultiServiceFactory> m_xModelFactory;
                uno::Reference<document::XDocumentProperties> m_xDocumentProperties;
                boost::shared_ptr<SvStream> m_pInStream;
                Stream* m_pMapperStream;
                boost::shared_ptr<RTFSdrImport> m_pSdrImport;
                boost::shared_ptr<RTFTokenizer> m_pTokenizer;
                /// Same as m_aStates.size(), except that this can be negative for invalid input.
                int m_nGroup;
                std::stack<RTFParserState> m_aStates;
                /// Read by RTF_PARD.
                RTFParserState m_aDefaultState;
                bool m_bSkipUnknown;
                /// Font index <-> encoding map, *not* part of the parser state
                std::map<int, rtl_TextEncoding> m_aFontEncodings;
                /// Maps the non-continious font indexes to the continous dmapper indexes.
                std::vector<int> m_aFontIndexes;
                /// Color index <-> RGB color value map
                std::vector<sal_uInt32> m_aColorTable;
                bool m_bFirstRun;
                /// If paragraph properties should be emitted on next run.
                bool m_bNeedPap;
                /// If we need to emit a CR at the end of substream.
                bool m_bNeedCr;
                /// If we need to add a dummy paragraph before a section break.
                bool m_bNeedPar;
                /// The list table and list override table combined.
                RTFSprms m_aListTableSprms;
                /// The settings table.
                RTFSprms m_aSettingsTableSprms;

                oox::StorageRef m_xStorage;
                boost::shared_ptr<oox::GraphicHelper> m_pGraphicHelper;

                /// Buffered table cells, till cell definitions are not reached.
                RTFBuffer_t m_aTableBuffer;
                /// Buffered superscript, till footnote is reached (or not).
                RTFBuffer_t m_aSuperBuffer;
                /// Buffered shape text.
                RTFBuffer_t m_aShapetextBuffer;
                /// Points to the active buffer, if there is one.
                RTFBuffer_t* m_pCurrentBuffer;

                bool m_bHasFootnote;
                /// If this is a substream.
                bool m_bIsSubstream;
                std::queue< std::pair<Id, sal_uInt32> > m_nHeaderFooterPositions;
                sal_uInt32 m_nGroupStartPos;
                /// Ignore the first occurrence of this text.
                rtl::OUString m_aIgnoreFirst;
                /// Bookmark name <-> index map.
                std::map<rtl::OUString, int> m_aBookmarks;
                /// Revision index <-> author map.
                std::map<int, rtl::OUString> m_aAuthors;
                /// Annotation author of the next annotation.
                rtl::OUString m_aAuthor;

                RTFSprms m_aFormfieldSprms;
                RTFSprms m_aFormfieldAttributes;
                RTFFormFieldTypes m_nFormFieldType;

                RTFSprms m_aObjectSprms;
                RTFSprms m_aObjectAttributes;
                /// If we are in an object group.
                bool m_bObject;
                /// Contents of the objdata group.
                boost::shared_ptr<SvStream> m_pObjectData;

                RTFReferenceTable::Entries_t m_aFontTableEntries;
                int m_nCurrentFontIndex;

                RTFReferenceTable::Entries_t m_aStyleTableEntries;
                int m_nCurrentStyleIndex;
                bool m_bEq;
                bool m_bFormField;
                /// If we were in a frame.
                bool m_bWasInFrame;
                /// If a frame start token is already sent to dmapper (nesting them is not OK).
                bool m_bIsInFrame;
                /// If we should reset the page break property when we start the next paragraph.
                bool m_bHasPage;
                // Unicode characters are collected here so we don't have to send them one by one.
                rtl::OUStringBuffer m_aUnicodeBuffer;
        };
    } // namespace rtftok
} // namespace writerfilter

#endif // _RTFDOCUMENTIMPL_HXX_

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */