Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #ifndef _RTFDOCUMENTIMPL_HXX_
11 : #define _RTFDOCUMENTIMPL_HXX_
12 :
13 : #include <stack>
14 : #include <queue>
15 : #include <boost/optional.hpp>
16 :
17 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
18 : #include <com/sun/star/beans/XPropertySet.hpp>
19 : #include <com/sun/star/document/XDocumentProperties.hpp>
20 : #include <oox/helper/graphichelper.hxx>
21 : #include <oox/mathml/importutils.hxx>
22 : #include <rtl/strbuf.hxx>
23 :
24 : #include <rtftok/RTFDocument.hxx>
25 : #include <rtfreferencetable.hxx>
26 : #include <rtfsprm.hxx>
27 : #include <rtflistener.hxx>
28 :
29 : class SvStream;
30 :
31 : namespace writerfilter {
32 : namespace rtftok {
33 : enum RTFBorderState
34 : {
35 : BORDER_NONE,
36 : BORDER_PARAGRAPH,
37 : BORDER_PARAGRAPH_BOX,
38 : BORDER_CELL,
39 : BORDER_PAGE
40 : };
41 :
42 : /// Minimalistic buffer of elements for nested cells.
43 : enum RTFBufferTypes
44 : {
45 : BUFFER_PROPS,
46 : BUFFER_CELLEND,
47 : BUFFER_STARTRUN,
48 : BUFFER_TEXT,
49 : BUFFER_UTEXT,
50 : BUFFER_ENDRUN,
51 : BUFFER_PAR,
52 : BUFFER_STARTSHAPE,
53 : BUFFER_ENDSHAPE
54 : };
55 :
56 : /// Form field types
57 : enum RTFFormFieldTypes
58 : {
59 : FORMFIELD_NONE,
60 : FORMFIELD_TEXT,
61 : FORMFIELD_CHECKBOX,
62 : FORMFIELD_LIST
63 : };
64 :
65 : enum RTFBmpStyles
66 : {
67 : BMPSTYLE_NONE,
68 : BMPSTYLE_PNG,
69 : BMPSTYLE_JPEG
70 : };
71 :
72 : enum RTFFieldStatus
73 : {
74 : FIELD_NONE,
75 : FIELD_INSTRUCTION,
76 : FIELD_RESULT
77 : };
78 :
79 : /// A buffer storing dmapper calls.
80 : typedef std::deque< std::pair<RTFBufferTypes, RTFValue::Pointer_t> > RTFBuffer_t;
81 :
82 : /// An entry in the color table.
83 : class RTFColorTableEntry
84 : {
85 : public:
86 : RTFColorTableEntry();
87 : sal_uInt8 nRed;
88 : sal_uInt8 nGreen;
89 : sal_uInt8 nBlue;
90 : };
91 :
92 : /// Stores the properties of a shape.
93 358828 : class RTFShape
94 : {
95 : public:
96 : RTFShape();
97 : std::vector< std::pair<OUString, OUString> > aProperties;
98 : sal_Int32 nLeft;
99 : sal_Int32 nTop;
100 : sal_Int32 nRight;
101 : sal_Int32 nBottom;
102 : boost::optional<sal_Int32> oZ; ///< Z-Order of the shape.
103 : sal_Int16 nHoriOrientRelation;
104 : sal_Int16 nVertOrientRelation;
105 : int nWrap;
106 : };
107 :
108 : /// Stores the properties of a drawing object.
109 29782 : class RTFDrawingObject : public RTFShape
110 : {
111 : public:
112 : RTFDrawingObject();
113 : uno::Reference<drawing::XShape> xShape;
114 : uno::Reference<beans::XPropertySet> xPropertySet;
115 : std::vector<beans::PropertyValue> aPendingProperties;
116 : sal_uInt8 nLineColorR, nLineColorG, nLineColorB;
117 : bool bHasLineColor;
118 : sal_uInt8 nFillColorR, nFillColorG, nFillColorB;
119 : bool bHasFillColor;
120 : sal_Int32 nDhgt;
121 : sal_Int32 nFLine;
122 : sal_Int32 nPolyLineCount;
123 : uno::Sequence<awt::Point> aPolyLinePoints;
124 : bool bHadShapeText;
125 : };
126 :
127 : /// Stores the properties of a picture.
128 : class RTFPicture
129 : {
130 : public:
131 : RTFPicture();
132 : sal_uInt16 nWidth, nHeight;
133 : sal_uInt16 nGoalWidth, nGoalHeight;
134 : sal_uInt16 nScaleX, nScaleY;
135 : short nCropT, nCropB, nCropL, nCropR;
136 : sal_uInt16 eWMetafile;
137 : RTFBmpStyles nStyle;
138 : };
139 :
140 : class RTFParserState;
141 :
142 : /// Stores the properties of a frame
143 30593 : class RTFFrame
144 : {
145 : private:
146 : RTFParserState* m_pParserState;
147 : sal_Int32 nX, nY, nW, nH;
148 : sal_Int32 nHoriPadding, nVertPadding;
149 : sal_Int32 nHoriAlign, nHoriAnchor, nVertAlign, nVertAnchor;
150 : Id nHRule;
151 : boost::optional<Id> oWrap;
152 : public:
153 : RTFFrame(RTFParserState* pParserState);
154 : sal_Int16 nAnchorType;
155 :
156 : /// Convert the stored properties to Sprms
157 : RTFSprms getSprms();
158 : /// Store a property
159 : void setSprm(Id nId, Id nValue);
160 : bool hasProperties();
161 : /// If we got tokens indicating we're in a frame.
162 : bool inFrame();
163 : };
164 :
165 : class RTFDocumentImpl;
166 :
167 : /// State of the parser, which gets saved / restored when changing groups.
168 29782 : class RTFParserState
169 : {
170 : public:
171 : RTFParserState(RTFDocumentImpl* pDocumentImpl);
172 : /// Resets aFrame.
173 : void resetFrame();
174 :
175 : RTFDocumentImpl* m_pDocumentImpl;
176 : RTFInternalState nInternalState;
177 : RTFDestinationState nDestinationState;
178 : RTFFieldStatus nFieldStatus;
179 : RTFBorderState nBorderState;
180 : // font table, stylesheet table
181 : RTFSprms aTableSprms;
182 : RTFSprms aTableAttributes;
183 : // reset by plain
184 : RTFSprms aCharacterSprms;
185 : RTFSprms aCharacterAttributes;
186 : // reset by pard
187 : RTFSprms aParagraphSprms;
188 : RTFSprms aParagraphAttributes;
189 : // reset by sectd
190 : RTFSprms aSectionSprms;
191 : RTFSprms aSectionAttributes;
192 : // reset by trowd
193 : RTFSprms aTableRowSprms;
194 : RTFSprms aTableRowAttributes;
195 : // reset by cellx
196 : RTFSprms aTableCellSprms;
197 : RTFSprms aTableCellAttributes;
198 : // reset by row/nestrow
199 : std::deque<RTFSprms> aTableCellsSprms;
200 : std::deque<RTFSprms> aTableCellsAttributes;
201 : // backup of the above two, to support inheriting cell props
202 : std::deque<RTFSprms> aTableInheritingCellsSprms;
203 : std::deque<RTFSprms> aTableInheritingCellsAttributes;
204 : // reset by tx
205 : RTFSprms aTabAttributes;
206 :
207 : RTFColorTableEntry aCurrentColor;
208 :
209 : rtl_TextEncoding nCurrentEncoding;
210 :
211 : /// Current \uc value.
212 : int nUc;
213 : /// Characters to skip, set to nUc by \u.
214 : int nCharsToSkip;
215 : /// Characters to read, once in binary mode.
216 : int nBinaryToRead;
217 :
218 : /// Next list level index to use when parsing list table.
219 : int nListLevelNum;
220 : /// List level entries, which will form a list entry later.
221 : RTFSprms aListLevelEntries;
222 :
223 : /// List of character positions in leveltext to replace.
224 : std::vector<sal_Int32> aLevelNumbers;
225 :
226 : RTFPicture aPicture;
227 : RTFShape aShape;
228 : RTFDrawingObject aDrawingObject;
229 : RTFFrame aFrame;
230 :
231 : /// Current cellx value.
232 : int nCellX;
233 : int nCells;
234 : int nInheritingCells;
235 :
236 : /// CJK or CTL?
237 : bool bIsCjk;
238 :
239 : // Info group.
240 : int nYear;
241 : int nMonth;
242 : int nDay;
243 : int nHour;
244 : int nMinute;
245 :
246 : /// Text from special destinations.
247 : OUStringBuffer aDestinationText;
248 :
249 : /// Same as the int value of NS_rtf::LN_ISTD in aParagraphAttributes, for performance reasons.
250 : int nCurrentStyleIndex;
251 :
252 : /// Points to the active buffer, if there is one.
253 : RTFBuffer_t* pCurrentBuffer;
254 :
255 : /// If a table style was requested to be used.
256 : bool bHasTableStyle;
257 :
258 : /// If we're inside a \listpicture group.
259 : bool bInListpicture;
260 :
261 : /// If we're inside a \background group.
262 : bool bInBackground;
263 :
264 : bool bHadShapeText;
265 : bool bInShapeGroup; ///< If we're inside a \shpgrp group.
266 : };
267 :
268 : class RTFTokenizer;
269 : class RTFSdrImport;
270 :
271 : /// Implementation of the RTFDocument interface.
272 : class RTFDocumentImpl
273 : : public RTFDocument, public RTFListener
274 : {
275 : public:
276 : typedef ::boost::shared_ptr<RTFDocumentImpl> Pointer_t;
277 : RTFDocumentImpl(uno::Reference<uno::XComponentContext> const& xContext,
278 : uno::Reference<io::XInputStream> const& xInputStream,
279 : uno::Reference<lang::XComponent> const& xDstDoc,
280 : uno::Reference<frame::XFrame> const& xFrame,
281 : uno::Reference<task::XStatusIndicator> const& xStatusIndicator);
282 : virtual ~RTFDocumentImpl();
283 :
284 : // RTFDocument
285 : virtual void resolve(Stream & rHandler);
286 : virtual std::string getType() const;
287 :
288 : // RTFListener
289 : virtual int dispatchDestination(RTFKeyword nKeyword);
290 : virtual int dispatchFlag(RTFKeyword nKeyword);
291 : virtual int dispatchSymbol(RTFKeyword nKeyword);
292 : virtual int dispatchToggle(RTFKeyword nKeyword, bool bParam, int nParam);
293 : virtual int dispatchValue(RTFKeyword nKeyword, int nParam);
294 : virtual int resolveChars(char ch);
295 : virtual int pushState();
296 : virtual int popState();
297 : virtual RTFDestinationState getDestinationState();
298 : virtual void setDestinationState(RTFDestinationState nDestinationState);
299 : virtual RTFInternalState getInternalState();
300 : virtual void setInternalState(RTFInternalState nInternalState);
301 : virtual bool getSkipUnknown();
302 : virtual void setSkipUnknown(bool bSkipUnknown);
303 : virtual void finishSubstream();
304 : virtual bool isSubstream() const;
305 :
306 : Stream& Mapper();
307 : void setSubstream(bool bIsSubtream);
308 : void setSuperstream(RTFDocumentImpl *pSuperstream);
309 : void setAuthor(OUString& rAuthor);
310 : void setAuthorInitials(OUString& rAuthorInitials);
311 : void setIgnoreFirst(OUString& rIgnoreFirst);
312 : void seek(sal_uInt32 nPos);
313 : uno::Reference<lang::XMultiServiceFactory> getModelFactory();
314 : bool isInBackground();
315 : void setDestinationText(OUString& rString);
316 : /// Resolve a picture: If not inline, then anchored.
317 : int resolvePict(bool bInline);
318 :
319 : /// If this is the first run of the document, starts the initial paragraph.
320 : void checkFirstRun();
321 : /// If the initial paragraph is started.
322 : bool getFirstRun();
323 : /// If we need to add a dummy paragraph before a section break.
324 : void setNeedPar(bool bNeedPar);
325 : /// Return the dmapper index of an RTF index for fonts.
326 : int getFontIndex(int nIndex);
327 : /// Return the encoding associated with a dmapper font index.
328 : rtl_TextEncoding getEncoding(sal_uInt32 nFontIndex);
329 : /// Get the default parser state.
330 : RTFParserState& getDefaultState();
331 : oox::GraphicHelper& getGraphicHelper();
332 :
333 : private:
334 : SvStream& Strm();
335 : sal_uInt32 getColorTable(sal_uInt32 nIndex);
336 : RTFSprms mergeSprms();
337 : RTFSprms mergeAttributes();
338 : void resetSprms();
339 : void resetAttributes();
340 : void resolveSubstream(sal_uInt32 nPos, Id nId);
341 : void resolveSubstream(sal_uInt32 nPos, Id nId, OUString& rIgnoreFirst);
342 :
343 : void text(OUString& rString);
344 : // Sends a single character to dmapper, taking care of buffering.
345 : void singleChar(sal_uInt8 nValue, bool bRunProps = false);
346 : // Sends run properties to dmapper, taking care of buffering.
347 : void runProps();
348 : void runBreak();
349 : void parBreak();
350 : void tableBreak();
351 : writerfilter::Reference<Properties>::Pointer_t getProperties(RTFSprms& rAttributes, RTFSprms& rSprms);
352 : void checkNeedPap();
353 : void sectBreak(bool bFinal);
354 : void replayBuffer(RTFBuffer_t& rBuffer);
355 : /// If we have some unicode or hex characters to send.
356 : void checkUnicode(bool bUnicode, bool bHex);
357 : /// If we need a final section break at the end of the document.
358 : void setNeedSect(bool bNeedSect = true);
359 :
360 : uno::Reference<uno::XComponentContext> const& m_xContext;
361 : uno::Reference<io::XInputStream> const& m_xInputStream;
362 : uno::Reference<lang::XComponent> const& m_xDstDoc;
363 : uno::Reference<frame::XFrame> const& m_xFrame;
364 : uno::Reference<task::XStatusIndicator> const& m_xStatusIndicator;
365 : uno::Reference<lang::XMultiServiceFactory> m_xModelFactory;
366 : uno::Reference<document::XDocumentProperties> m_xDocumentProperties;
367 : boost::shared_ptr<SvStream> m_pInStream;
368 : Stream* m_pMapperStream;
369 : boost::shared_ptr<RTFSdrImport> m_pSdrImport;
370 : boost::shared_ptr<RTFTokenizer> m_pTokenizer;
371 : std::stack<RTFParserState> m_aStates;
372 : /// Read by RTF_PARD.
373 : RTFParserState m_aDefaultState;
374 : bool m_bSkipUnknown;
375 : /// Font index <-> encoding map, *not* part of the parser state
376 : std::map<int, rtl_TextEncoding> m_aFontEncodings;
377 : /// Maps the non-continious font indexes to the continous dmapper indexes.
378 : std::vector<int> m_aFontIndexes;
379 : /// Color index <-> RGB color value map
380 : std::vector<sal_uInt32> m_aColorTable;
381 : bool m_bFirstRun;
382 : /// If paragraph properties should be emitted on next run.
383 : bool m_bNeedPap;
384 : /// If we need to emit a CR at the end of substream.
385 : bool m_bNeedCr;
386 : /// Original value of m_bNeedCr -- saved/restored before/after textframes.
387 : bool m_bNeedCrOrig;
388 : bool m_bNeedPar;
389 : /// If set, an empty paragraph will be added at the end of the document.
390 : bool m_bNeedFinalPar;
391 : /// The list table and list override table combined.
392 : RTFSprms m_aListTableSprms;
393 : /// The settings table attributes.
394 : RTFSprms m_aSettingsTableAttributes;
395 : /// The settings table sprms.
396 : RTFSprms m_aSettingsTableSprms;
397 :
398 : oox::StorageRef m_xStorage;
399 : boost::shared_ptr<oox::GraphicHelper> m_pGraphicHelper;
400 :
401 : /// Buffered table cells, till cell definitions are not reached.
402 : RTFBuffer_t m_aTableBuffer;
403 : /// Buffered superscript, till footnote is reached (or not).
404 : RTFBuffer_t m_aSuperBuffer;
405 :
406 : bool m_bHasFootnote;
407 : /// Superstream of this substream.
408 : RTFDocumentImpl *m_pSuperstream;
409 : std::queue< std::pair<Id, sal_uInt32> > m_nHeaderFooterPositions;
410 : sal_uInt32 m_nGroupStartPos;
411 : /// Ignore the first occurrence of this text.
412 : OUString m_aIgnoreFirst;
413 : /// Bookmark name <-> index map.
414 : std::map<OUString, int> m_aBookmarks;
415 : /// Revision index <-> author map.
416 : std::map<int, OUString> m_aAuthors;
417 : /// Annotation author of the next annotation.
418 : OUString m_aAuthor;
419 : /// Initials of author of the next annotation.
420 : OUString m_aAuthorInitials;
421 :
422 : RTFSprms m_aFormfieldSprms;
423 : RTFSprms m_aFormfieldAttributes;
424 : RTFFormFieldTypes m_nFormFieldType;
425 :
426 : RTFSprms m_aObjectSprms;
427 : RTFSprms m_aObjectAttributes;
428 : /** If we are in an object group and if the we use its
429 : * \objdata element.
430 : * (if we don't use the \objdata we use the \result element)*/
431 : bool m_bObject;
432 : /// Contents of the objdata group.
433 : boost::shared_ptr<SvStream> m_pObjectData;
434 : /// If the data for a picture is a binary one, it's stored here.
435 : boost::shared_ptr<SvStream> m_pBinaryData;
436 :
437 : RTFReferenceTable::Entries_t m_aFontTableEntries;
438 : int m_nCurrentFontIndex;
439 :
440 : RTFReferenceTable::Entries_t m_aStyleTableEntries;
441 : int m_nCurrentStyleIndex;
442 : bool m_bFormField;
443 : /// If a frame start token is already sent to dmapper (nesting them is not OK).
444 : bool m_bIsInFrame;
445 : // Unicode characters are collected here so we don't have to send them one by one.
446 : OUStringBuffer m_aUnicodeBuffer;
447 : /// Same for hex characters.
448 : OStringBuffer m_aHexBuffer;
449 : /// Formula import.
450 : oox::formulaimport::XmlStreamBuilder m_aMathBuffer;
451 : /// Normal text property, that is math italic and math spacing are not applied to the current run.
452 : bool m_bMathNor;
453 : /// If the next continous section break should be ignored.
454 : bool m_bIgnoreNextContSectBreak;
455 : /// If a section break is needed before the end of the doc (false right after a section break).
456 : bool m_bNeedSect;
457 : /// If aFrame.inFrame() was true in the previous state.
458 : bool m_bWasInFrame;
459 : /// A picture was seen in the current paragraph.
460 : bool m_bHadPicture;
461 : /// The document has multiple sections.
462 : bool m_bHadSect;
463 : /// Max width of the rows in the current table.
464 : int m_nCellxMax;
465 : /// ID of the next \listlevel picture.
466 : int m_nListPictureId;
467 : };
468 : } // namespace rtftok
469 : } // namespace writerfilter
470 :
471 : #endif // _RTFDOCUMENTIMPL_HXX_
472 :
473 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|