Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _SVXRTF_HXX
30 : : #define _SVXRTF_HXX
31 : :
32 : : #include <tools/string.hxx>
33 : : #include <svl/itemset.hxx>
34 : : #include <svtools/parrtf.hxx>
35 : :
36 : : #include <editeng/editengdllapi.h>
37 : :
38 : : #include <deque>
39 : : #include <utility>
40 : : #include <vector>
41 : : #include "boost/ptr_container/ptr_map.hpp"
42 : : #include "boost/ptr_container/ptr_vector.hpp"
43 : :
44 : : class Font;
45 : : class Color;
46 : : class Graphic;
47 : : class DateTime;
48 : : struct SvxRTFStyleType;
49 : : class SvxRTFItemStackType;
50 : 0 : class SvxRTFItemStackList : public boost::ptr_vector<SvxRTFItemStackType> {};
51 : :
52 : : namespace com { namespace sun { namespace star {
53 : : namespace document {
54 : : class XDocumentProperties;
55 : : }
56 : : namespace util {
57 : : struct DateTime;
58 : : }
59 : : } } }
60 : :
61 : :
62 : : // Mapper-Classes for the various requirements on Document positions
63 : : // Swg - NodePosition is a SwIndex, which is used internally
64 : : // EditEngine - ULONG to list of paragraphs
65 : : // .....
66 : :
67 : 0 : class SvxNodeIdx
68 : : {
69 : : public:
70 [ # # ]: 0 : virtual ~SvxNodeIdx() {}
71 : : virtual sal_uLong GetIdx() const = 0;
72 : : virtual SvxNodeIdx* Clone() const = 0; // Cloning itself
73 : : };
74 : :
75 : 0 : class SvxPosition
76 : : {
77 : : public:
78 [ # # ]: 0 : virtual ~SvxPosition() {}
79 : :
80 : : virtual sal_uLong GetNodeIdx() const = 0;
81 : : virtual xub_StrLen GetCntIdx() const = 0;
82 : :
83 : : virtual SvxPosition* Clone() const = 0; // Cloning itself
84 : : virtual SvxNodeIdx* MakeNodeIdx() const = 0; // Cloning NodeIndex
85 : : };
86 : :
87 : :
88 : : typedef Color* ColorPtr;
89 : : typedef std::deque< ColorPtr > SvxRTFColorTbl;
90 : : typedef boost::ptr_map<short, Font> SvxRTFFontTbl;
91 : : typedef boost::ptr_map<sal_uInt16, SvxRTFStyleType> SvxRTFStyleTbl;
92 : :
93 : : // SvxRTFItemStack can't be "std::stack< SvxRTFItemStackType* >" type, because
94 : : // the methods are using operator[] in sw/source/filter/rtf/rtftbl.cxx file
95 : : typedef std::deque< SvxRTFItemStackType* > SvxRTFItemStack;
96 : :
97 : : // own helper classes for the RTF Parser
98 [ # # ]: 0 : struct SvxRTFStyleType
99 : : {
100 : : SfxItemSet aAttrSet; // the attributes of Style (+ derivate!)
101 : : String sName;
102 : : sal_uInt16 nBasedOn, nNext;
103 : : sal_Bool bBasedOnIsSet;
104 : : sal_uInt8 nOutlineNo;
105 : : sal_Bool bIsCharFmt;
106 : :
107 : : SvxRTFStyleType( SfxItemPool& rPool, const sal_uInt16* pWhichRange );
108 : : };
109 : :
110 : :
111 : : // Bitmap - Mode
112 : : typedef ::std::vector< ::std::pair< ::rtl::OUString, ::rtl::OUString > > PictPropertyNameValuePairs;
113 : 0 : struct EDITENG_DLLPUBLIC SvxRTFPictureType
114 : : {
115 : : // Bitmap Format
116 : : enum RTF_BMPSTYLE
117 : : {
118 : : RTF_BITMAP, // Meta data: Bitmap
119 : : WIN_METAFILE, // Meta data: Window-Metafile
120 : : MAC_QUICKDRAW, // Meta data: Mac-QuickDraw
121 : : OS2_METAFILE, // Meta data: OS2-Metafile
122 : : RTF_DI_BMP, // Meta data: Device Independent Bitmap
123 : : ENHANCED_MF, // Meta data: Enhanced-Metafile
124 : : RTF_PNG, // Meta data: PNG file
125 : : RTF_JPG // Meta data: JPG file
126 : : } eStyle;
127 : :
128 : : enum RTF_BMPMODE
129 : : {
130 : : BINARY_MODE,
131 : : HEX_MODE
132 : : } nMode;
133 : :
134 : : sal_uInt16 nType;
135 : : sal_uInt32 uPicLen;
136 : : sal_uInt16 nWidth, nHeight;
137 : : sal_uInt16 nGoalWidth, nGoalHeight;
138 : : sal_uInt16 nBitsPerPixel;
139 : : sal_uInt16 nPlanes;
140 : : sal_uInt16 nWidthBytes;
141 : : sal_uInt16 nScalX, nScalY;
142 : : short nCropT, nCropB, nCropL, nCropR;
143 : : PictPropertyNameValuePairs aPropertyPairs;
144 [ # # ]: 0 : SvxRTFPictureType() { ResetValues(); }
145 : : // Reset all values to default; is called after loading the Bitmap
146 : : void ResetValues();
147 : : };
148 : :
149 : : // Here are the IDs for all character attributes, which can be detected by
150 : : // SvxParser and can be set in a SfxItemSet. The IDs are set correctly throught
151 : : // the SlotIds from POOL.
152 : : struct RTFPlainAttrMapIds
153 : : {
154 : : sal_uInt16 nCaseMap,
155 : : nBgColor,
156 : : nColor,
157 : : nContour,
158 : : nCrossedOut,
159 : : nEscapement,
160 : : nFont,
161 : : nFontHeight,
162 : : nKering,
163 : : nLanguage,
164 : : nPosture,
165 : : nShadowed,
166 : : nUnderline,
167 : : nOverline,
168 : : nWeight,
169 : : nWordlineMode,
170 : : nAutoKerning,
171 : : nCJKFont,
172 : : nCJKFontHeight,
173 : : nCJKLanguage,
174 : : nCJKPosture,
175 : : nCJKWeight,
176 : : nCTLFont,
177 : : nCTLFontHeight,
178 : : nCTLLanguage,
179 : : nCTLPosture,
180 : : nCTLWeight,
181 : : nEmphasis,
182 : : nTwoLines,
183 : : nCharScaleX,
184 : : nHorzVert,
185 : : nRuby,
186 : : nRelief,
187 : : nHidden
188 : : ;
189 : : RTFPlainAttrMapIds( const SfxItemPool& rPool );
190 : : };
191 : :
192 : : // Here are the IDs for all paragraph attributes, which can be detected by
193 : : // SvxParser and can be set in a SfxItemSet. The IDs are set correctly throught
194 : : // the SlotIds from POOL.
195 : : struct RTFPardAttrMapIds
196 : : {
197 : : sal_uInt16 nLinespacing,
198 : : nAdjust,
199 : : nTabStop,
200 : : nHyphenzone,
201 : : nLRSpace,
202 : : nULSpace,
203 : : nBrush,
204 : : nBox,
205 : : nShadow,
206 : : nOutlineLvl,
207 : : nSplit,
208 : : nKeep,
209 : : nFontAlign,
210 : : nScriptSpace,
211 : : nHangPunct,
212 : : nForbRule,
213 : : nDirection
214 : : ;
215 : : RTFPardAttrMapIds( const SfxItemPool& rPool );
216 : : };
217 : :
218 : :
219 : :
220 : : // -----------------------------------------------------------------------
221 : :
222 : :
223 : : class EDITENG_DLLPUBLIC SvxRTFParser : public SvRTFParser
224 : : {
225 : : SvStream &rStrm;
226 : : SvxRTFColorTbl aColorTbl;
227 : : SvxRTFFontTbl aFontTbl;
228 : : SvxRTFStyleTbl aStyleTbl;
229 : : SvxRTFItemStack aAttrStack;
230 : : SvxRTFItemStackList aAttrSetList;
231 : :
232 : : std::vector<sal_uInt16> aPlainMap;
233 : : std::vector<sal_uInt16> aPardMap;
234 : : std::vector<sal_uInt16> aWhichMap;
235 : : String sBaseURL;
236 : :
237 : : SvxPosition* pInsPos;
238 : : SfxItemPool* pAttrPool;
239 : : Color* pDfltColor;
240 : : Font* pDfltFont;
241 : : ::com::sun::star::uno::Reference<
242 : : ::com::sun::star::document::XDocumentProperties> m_xDocProps;
243 : : SfxItemSet *pRTFDefaults;
244 : :
245 : : long nVersionNo;
246 : : int nDfltFont;
247 : :
248 : : sal_Bool bNewDoc : 1; // FALSE - Reading in an existing
249 : : sal_Bool bNewGroup : 1; // TRUE - there was an opening parenthesis
250 : : sal_Bool bIsSetDfltTab : 1; // TRUE - DefTab was loaded
251 : : sal_Bool bChkStyleAttr : 1; // TRUE - StyleSheets are evaluated
252 : : sal_Bool bCalcValue : 1; // TRUE - Twip values adapt to App
253 : : sal_Bool bPardTokenRead : 1; // TRUE - Token \pard was detected
254 : : sal_Bool bReadDocInfo : 1; // TRUE - DocInfo to read
255 : : sal_Bool bIsLeftToRightDef : 1; // TRUE - in LeftToRight char run def.
256 : : // FALSE - in RightToLeft char run def.
257 : : sal_Bool bIsInReadStyleTab : 1; // TRUE - in ReadStyleTable
258 : :
259 : : void ClearColorTbl();
260 : : void ClearFontTbl();
261 : : void ClearStyleTbl();
262 : : void ClearAttrStack();
263 : :
264 : : SvxRTFItemStackType* _GetAttrSet( int bCopyAttr=sal_False ); // Create new ItemStackType:s
265 : : void _ClearStyleAttr( SvxRTFItemStackType& rStkType );
266 : :
267 : : // Sets all the attributes that are different from the current
268 : : void SetAttrSet( SfxItemSet& rAttrSet, SvxPosition& rSttPos );
269 : : void SetAttrSet( SvxRTFItemStackType &rSet );
270 : : void SetDefault( int nToken, int nValue );
271 : :
272 : : // Excecute pard / plain
273 : : void RTFPardPlain( int bPard, SfxItemSet** ppSet );
274 : :
275 : : void BuildWhichTbl();
276 : :
277 : : enum RTF_CharTypeDef
278 : : {
279 : : NOTDEF_CHARTYPE,
280 : : LOW_CHARTYPE,
281 : : HIGH_CHARTYPE,
282 : : DOUBLEBYTE_CHARTYPE
283 : : };
284 : :
285 : : // set latin/asian/complex character attributes
286 : : void SetScriptAttr(
287 : : RTF_CharTypeDef eType, SfxItemSet& rSet, SfxPoolItem& rItem );
288 : :
289 : : protected:
290 : : virtual void EnterEnvironment();
291 : : virtual void LeaveEnvironment();
292 : : virtual void ResetPard();
293 : : virtual void InsertPara() = 0;
294 : :
295 : :
296 : : String& DelCharAtEnd( String& rStr, const sal_Unicode cDel );
297 : :
298 : : // is called for each token that is recognized in CallParser
299 : : virtual void NextToken( int nToken );
300 : :
301 : : virtual void ReadBitmapData();
302 : : virtual void ReadOLEData();
303 : :
304 : : void ReadStyleTable();
305 : : void ReadColorTable();
306 : : void ReadFontTable();
307 : : void ReadAttr( int nToken, SfxItemSet* pSet );
308 : : void ReadTabAttr( int nToken, SfxItemSet& rSet );
309 : :
310 : : // Read Document-Info
311 : : ::com::sun::star::util::DateTime GetDateTimeStamp( );
312 : : String& GetTextToEndGroup( String& rStr );
313 : : virtual void ReadInfo( const sal_Char* pChkForVerNo = 0 );
314 : :
315 : : inline SfxItemSet& GetAttrSet();
316 : : // no text yet inserted? (SttPos from the top stack entry!)
317 : : int IsAttrSttPos();
318 : : void AttrGroupEnd(); // edit the current, delete from stack
319 : : void SetAllAttrOfStk(); // end all Attr. and set it into doc
320 : :
321 : :
322 : : virtual void InsertText() = 0;
323 : : virtual void MovePos( int bForward = sal_True ) = 0;
324 : : virtual void SetEndPrevPara( SvxNodeIdx*& rpNodePos,
325 : : xub_StrLen& rCntPos )=0;
326 : : virtual void SetAttrInDoc( SvxRTFItemStackType &rSet );
327 : : // for Tokens, which are not evaluated in ReadAttr
328 : : virtual void UnknownAttrToken( int nToken, SfxItemSet* pSet );
329 : :
330 : : // if no-one would like to have any twips
331 : : virtual void CalcValue();
332 : :
333 : : SvxRTFParser( SfxItemPool& rAttrPool,
334 : : SvStream& rIn,
335 : : ::com::sun::star::uno::Reference<
336 : : ::com::sun::star::document::XDocumentProperties> i_xDocProps,
337 : : int bReadNewDoc = sal_True );
338 : : virtual ~SvxRTFParser();
339 : :
340 : 0 : int IsNewDoc() const { return bNewDoc; }
341 : 0 : void SetNewDoc( int bFlag ) { bNewDoc = bFlag; }
342 : 0 : int IsNewGroup() const { return bNewGroup; }
343 : 0 : void SetNewGroup( int bFlag ) { bNewGroup = bFlag; }
344 : 0 : int IsChkStyleAttr() const { return bChkStyleAttr; }
345 : 0 : void SetChkStyleAttr( int bFlag ) { bChkStyleAttr = bFlag; }
346 : 0 : int IsCalcValue() const { return bCalcValue; }
347 : 0 : void SetCalcValue( int bFlag ) { bCalcValue = bFlag; }
348 : 0 : int IsPardTokenRead() const { return bPardTokenRead; }
349 : 0 : void SetPardTokenRead( int bFlag ) { bPardTokenRead = bFlag; }
350 : : int IsReadDocInfo() const { return bReadDocInfo; }
351 : 0 : void SetReadDocInfo( int bFlag ) { bReadDocInfo = bFlag; }
352 : :
353 : : // Query/Set the current insert position
354 : : SvxPosition& GetInsPos() const { return *pInsPos; }
355 : : void SetInsPos( const SvxPosition& rNew );
356 : :
357 : 0 : long GetVersionNo() const { return nVersionNo; }
358 : :
359 : : // Query/Set the mapping IDs for the Pard/Plain attributes
360 : : //(Set: It is noted in the pointers, which thus does not create a copy)
361 : 0 : void AddPardAttr( sal_uInt16 nWhich ) { aPardMap.push_back( nWhich ); }
362 : 0 : void AddPlainAttr( sal_uInt16 nWhich ) { aPlainMap.push_back( nWhich ); }
363 : :
364 : 0 : SvxRTFStyleTbl& GetStyleTbl() { return aStyleTbl; }
365 : 0 : SvxRTFItemStack& GetAttrStack() { return aAttrStack; }
366 : : SvxRTFColorTbl& GetColorTbl() { return aColorTbl; }
367 : 0 : SvxRTFFontTbl& GetFontTbl() { return aFontTbl; }
368 : :
369 : : const String& GetBaseURL() const { return sBaseURL; }
370 : :
371 : : // Read the graphics data and make up for the graphics and the picture
372 : : // meta data.
373 : : // Return - TRUE: the graphic is valid
374 : : sal_Bool ReadBmpData( Graphic& rGrf, SvxRTFPictureType& rPicType );
375 : : // Change the ASCII-HexCodes into binary characters. If invalid data is
376 : : // found (strings not 0-9 | a-f | A-F, then USHRT_MAX is returned,
377 : : // otherwise the number of the converted character.
378 : : xub_StrLen HexToBin( String& rToken );
379 : :
380 : : public:
381 : :
382 : : virtual SvParserState CallParser();
383 : :
384 : : inline const Color& GetColor( size_t nId ) const;
385 : : const Font& GetFont( sal_uInt16 nId ); // Changes the dflt Font
386 : :
387 : : virtual int IsEndPara( SvxNodeIdx* pNd, xub_StrLen nCnt ) const = 0;
388 : :
389 : : // to det a different attribute pool. May only be done prior to CallParser!
390 : : // The maps are not generated anew!
391 : 0 : void SetAttrPool( SfxItemPool* pNewPool ) { pAttrPool = pNewPool; }
392 : : // to set different WhichIds for a different pool.
393 : 0 : RTFPardAttrMapIds& GetPardMap()
394 [ # # ]: 0 : { return (RTFPardAttrMapIds&)*aPardMap.begin(); }
395 : : RTFPlainAttrMapIds& GetPlainMap()
396 : : { return (RTFPlainAttrMapIds&)*aPlainMap.begin(); }
397 : : // to be able to assign them from the outside as for example table cells
398 : : void ReadBorderAttr( int nToken, SfxItemSet& rSet, int bTableDef=sal_False );
399 : : void ReadBackgroundAttr( int nToken, SfxItemSet& rSet, int bTableDef=sal_False );
400 : :
401 : : // for asynchronous read from the SvStream
402 : : virtual void Continue( int nToken );
403 : :
404 : : // get RTF default ItemSets. Must be used by pard/plain tokens or in
405 : : // reset of Style-Items
406 : : const SfxItemSet& GetRTFDefaults();
407 : : virtual bool UncompressableStackEntry(const SvxRTFItemStackType &rSet) const;
408 : : };
409 : :
410 : : // The stack for the attributes:
411 : : // this class may only be used by SvxRTFParser!
412 : : class EDITENG_DLLPUBLIC SvxRTFItemStackType
413 : : {
414 : : friend class SvxRTFParser;
415 : :
416 : : SfxItemSet aAttrSet;
417 : : SvxNodeIdx *pSttNd, *pEndNd;
418 : : xub_StrLen nSttCnt, nEndCnt;
419 : : SvxRTFItemStackList* pChildList;
420 : : sal_uInt16 nStyleNo;
421 : :
422 : : SvxRTFItemStackType( SfxItemPool&, const sal_uInt16* pWhichRange,
423 : : const SvxPosition& );
424 : :
425 : : void Add( SvxRTFItemStackType* );
426 : : void Compress( const SvxRTFParser& );
427 : :
428 : : public:
429 : : SvxRTFItemStackType( const SvxRTFItemStackType&, const SvxPosition&,
430 : : int bCopyAttr = sal_False );
431 : : ~SvxRTFItemStackType();
432 : : //cmc, I'm very suspicios about SetStartPos, it doesn't change
433 : : //its children's starting position, and the implementation looks
434 : : //bad, consider this deprecated.
435 : : void SetStartPos( const SvxPosition& rPos );
436 : :
437 : : void MoveFullNode(const SvxNodeIdx &rOldNode,
438 : : const SvxNodeIdx &rNewNode);
439 : :
440 : 0 : sal_uLong GetSttNodeIdx() const { return pSttNd->GetIdx(); }
441 : 0 : sal_uLong GetEndNodeIdx() const { return pEndNd->GetIdx(); }
442 : :
443 : 0 : const SvxNodeIdx& GetSttNode() const { return *pSttNd; }
444 : 0 : const SvxNodeIdx& GetEndNode() const { return *pEndNd; }
445 : :
446 : 0 : xub_StrLen GetSttCnt() const { return nSttCnt; }
447 : 0 : xub_StrLen GetEndCnt() const { return nEndCnt; }
448 : :
449 : 0 : SfxItemSet& GetAttrSet() { return aAttrSet; }
450 : 0 : const SfxItemSet& GetAttrSet() const { return aAttrSet; }
451 : :
452 : 0 : sal_uInt16 StyleNo() const { return nStyleNo; }
453 : :
454 : : void SetRTFDefaults( const SfxItemSet& rDefaults );
455 : : };
456 : :
457 : :
458 : : // ----------- Inline Implementations --------------
459 : :
460 : 0 : inline const Color& SvxRTFParser::GetColor( size_t nId ) const
461 : : {
462 : 0 : ColorPtr pColor = (ColorPtr)pDfltColor;
463 [ # # ]: 0 : if( nId < aColorTbl.size() )
464 : 0 : pColor = aColorTbl[ nId ];
465 : 0 : return *pColor;
466 : : }
467 : :
468 : 0 : inline SfxItemSet& SvxRTFParser::GetAttrSet()
469 : : {
470 : : SvxRTFItemStackType* pTmp;
471 [ # # ][ # # ]: 0 : if( bNewGroup || 0 == ( pTmp = aAttrStack.empty() ? 0 : aAttrStack.back()) )
[ # # ][ # # ]
472 : 0 : pTmp = _GetAttrSet();
473 : 0 : return pTmp->aAttrSet;
474 : : }
475 : :
476 : :
477 : : #endif
478 : : //_SVXRTF_HXX
479 : :
480 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|