1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
23 #include <tools/string.hxx>
24 #include <svl/itemset.hxx>
25 #include <svtools/parrtf.hxx>
27 #include <editeng/editengdllapi.h>
32 #include "boost/ptr_container/ptr_map.hpp"
33 #include "boost/ptr_container/ptr_vector.hpp"
39 struct SvxRTFStyleType
;
40 class SvxRTFItemStackType
;
41 class SvxRTFItemStackList
: public boost::ptr_vector
<SvxRTFItemStackType
> {};
43 namespace com
{ namespace sun
{ namespace star
{
45 class XDocumentProperties
;
53 // Mapper-Classes for the various requirements on Document positions
54 // Swg - NodePosition is a SwIndex, which is used internally
55 // EditEngine - ULONG to list of paragraphs
61 virtual ~SvxNodeIdx() {}
62 virtual sal_Int32
GetIdx() const = 0;
63 virtual SvxNodeIdx
* Clone() const = 0; // Cloning itself
69 virtual ~SvxPosition() {}
71 virtual sal_Int32
GetNodeIdx() const = 0;
72 virtual xub_StrLen
GetCntIdx() const = 0;
74 virtual SvxPosition
* Clone() const = 0; // Cloning itself
75 virtual SvxNodeIdx
* MakeNodeIdx() const = 0; // Cloning NodeIndex
79 typedef Color
* ColorPtr
;
80 typedef std::deque
< ColorPtr
> SvxRTFColorTbl
;
81 typedef boost::ptr_map
<short, Font
> SvxRTFFontTbl
;
82 typedef boost::ptr_map
<sal_uInt16
, SvxRTFStyleType
> SvxRTFStyleTbl
;
84 // SvxRTFItemStack can't be "std::stack< SvxRTFItemStackType* >" type, because
85 // the methods are using operator[] in sw/source/filter/rtf/rtftbl.cxx file
86 typedef std::deque
< SvxRTFItemStackType
* > SvxRTFItemStack
;
88 // own helper classes for the RTF Parser
89 struct SvxRTFStyleType
91 SfxItemSet aAttrSet
; // the attributes of Style (+ derivate!)
93 sal_uInt16 nBasedOn
, nNext
;
94 sal_Bool bBasedOnIsSet
;
98 SvxRTFStyleType( SfxItemPool
& rPool
, const sal_uInt16
* pWhichRange
);
102 // Here are the IDs for all character attributes, which can be detected by
103 // SvxParser and can be set in a SfxItemSet. The IDs are set correctly throught
104 // the SlotIds from POOL.
105 struct RTFPlainAttrMapIds
142 RTFPlainAttrMapIds( const SfxItemPool
& rPool
);
145 // Here are the IDs for all paragraph attributes, which can be detected by
146 // SvxParser and can be set in a SfxItemSet. The IDs are set correctly throught
147 // the SlotIds from POOL.
148 struct RTFPardAttrMapIds
150 sal_uInt16 nLinespacing
,
168 RTFPardAttrMapIds( const SfxItemPool
& rPool
);
173 // -----------------------------------------------------------------------
176 class EDITENG_DLLPUBLIC SvxRTFParser
: public SvRTFParser
179 SvxRTFColorTbl aColorTbl
;
180 SvxRTFFontTbl aFontTbl
;
181 SvxRTFStyleTbl aStyleTbl
;
182 SvxRTFItemStack aAttrStack
;
183 SvxRTFItemStackList aAttrSetList
;
185 std::vector
<sal_uInt16
> aPlainMap
;
186 std::vector
<sal_uInt16
> aPardMap
;
187 std::vector
<sal_uInt16
> aWhichMap
;
190 SvxPosition
* pInsPos
;
191 SfxItemPool
* pAttrPool
;
194 ::com::sun::star::uno::Reference
<
195 ::com::sun::star::document::XDocumentProperties
> m_xDocProps
;
196 SfxItemSet
*pRTFDefaults
;
201 sal_Bool bNewDoc
: 1; // sal_False - Reading in an existing
202 sal_Bool bNewGroup
: 1; // sal_True - there was an opening parenthesis
203 sal_Bool bIsSetDfltTab
: 1; // sal_True - DefTab was loaded
204 sal_Bool bChkStyleAttr
: 1; // sal_True - StyleSheets are evaluated
205 sal_Bool bCalcValue
: 1; // sal_True - Twip values adapt to App
206 sal_Bool bPardTokenRead
: 1; // sal_True - Token \pard was detected
207 sal_Bool bReadDocInfo
: 1; // sal_True - DocInfo to read
208 sal_Bool bIsLeftToRightDef
: 1; // sal_True - in LeftToRight char run def.
209 // sal_False - in RightToLeft char run def.
210 sal_Bool bIsInReadStyleTab
: 1; // sal_True - in ReadStyleTable
212 void ClearColorTbl();
214 void ClearStyleTbl();
215 void ClearAttrStack();
217 SvxRTFItemStackType
* _GetAttrSet( int bCopyAttr
=sal_False
); // Create new ItemStackType:s
218 void _ClearStyleAttr( SvxRTFItemStackType
& rStkType
);
220 // Sets all the attributes that are different from the current
221 void SetAttrSet( SfxItemSet
& rAttrSet
, SvxPosition
& rSttPos
);
222 void SetAttrSet( SvxRTFItemStackType
&rSet
);
223 void SetDefault( int nToken
, int nValue
);
225 // Excecute pard / plain
226 void RTFPardPlain( int bPard
, SfxItemSet
** ppSet
);
228 void BuildWhichTbl();
238 // set latin/asian/complex character attributes
240 RTF_CharTypeDef eType
, SfxItemSet
& rSet
, SfxPoolItem
& rItem
);
243 virtual void EnterEnvironment();
244 virtual void LeaveEnvironment();
245 virtual void ResetPard();
246 virtual void InsertPara() = 0;
249 String
& DelCharAtEnd( String
& rStr
, const sal_Unicode cDel
);
251 // is called for each token that is recognized in CallParser
252 virtual void NextToken( int nToken
);
254 virtual void ReadBitmapData();
255 virtual void ReadOLEData();
257 void ReadStyleTable();
258 void ReadColorTable();
259 void ReadFontTable();
260 void ReadAttr( int nToken
, SfxItemSet
* pSet
);
261 void ReadTabAttr( int nToken
, SfxItemSet
& rSet
);
263 // Read Document-Info
264 ::com::sun::star::util::DateTime
GetDateTimeStamp( );
265 String
& GetTextToEndGroup( String
& rStr
);
266 virtual void ReadInfo( const sal_Char
* pChkForVerNo
= 0 );
268 inline SfxItemSet
& GetAttrSet();
269 // no text yet inserted? (SttPos from the top stack entry!)
271 void AttrGroupEnd(); // edit the current, delete from stack
272 void SetAllAttrOfStk(); // end all Attr. and set it into doc
275 virtual void InsertText() = 0;
276 virtual void MovePos( int bForward
= sal_True
) = 0;
277 virtual void SetEndPrevPara( SvxNodeIdx
*& rpNodePos
,
278 xub_StrLen
& rCntPos
)=0;
279 virtual void SetAttrInDoc( SvxRTFItemStackType
&rSet
);
280 // for Tokens, which are not evaluated in ReadAttr
281 virtual void UnknownAttrToken( int nToken
, SfxItemSet
* pSet
);
283 // if no-one would like to have any twips
284 virtual void CalcValue();
286 SvxRTFParser( SfxItemPool
& rAttrPool
,
288 ::com::sun::star::uno::Reference
<
289 ::com::sun::star::document::XDocumentProperties
> i_xDocProps
,
290 int bReadNewDoc
= sal_True
);
291 virtual ~SvxRTFParser();
293 int IsNewDoc() const { return bNewDoc
; }
294 void SetNewDoc( int bFlag
) { bNewDoc
= bFlag
; }
295 int IsNewGroup() const { return bNewGroup
; }
296 void SetNewGroup( int bFlag
) { bNewGroup
= bFlag
; }
297 int IsChkStyleAttr() const { return bChkStyleAttr
; }
298 void SetChkStyleAttr( int bFlag
) { bChkStyleAttr
= bFlag
; }
299 int IsCalcValue() const { return bCalcValue
; }
300 void SetCalcValue( int bFlag
) { bCalcValue
= bFlag
; }
301 int IsPardTokenRead() const { return bPardTokenRead
; }
302 void SetPardTokenRead( int bFlag
) { bPardTokenRead
= bFlag
; }
303 int IsReadDocInfo() const { return bReadDocInfo
; }
304 void SetReadDocInfo( int bFlag
) { bReadDocInfo
= bFlag
; }
306 // Query/Set the current insert position
307 SvxPosition
& GetInsPos() const { return *pInsPos
; }
308 void SetInsPos( const SvxPosition
& rNew
);
310 long GetVersionNo() const { return nVersionNo
; }
312 // Query/Set the mapping IDs for the Pard/Plain attributes
313 //(Set: It is noted in the pointers, which thus does not create a copy)
314 void AddPardAttr( sal_uInt16 nWhich
) { aPardMap
.push_back( nWhich
); }
315 void AddPlainAttr( sal_uInt16 nWhich
) { aPlainMap
.push_back( nWhich
); }
317 SvxRTFStyleTbl
& GetStyleTbl() { return aStyleTbl
; }
318 SvxRTFItemStack
& GetAttrStack() { return aAttrStack
; }
319 SvxRTFColorTbl
& GetColorTbl() { return aColorTbl
; }
320 SvxRTFFontTbl
& GetFontTbl() { return aFontTbl
; }
322 const String
& GetBaseURL() const { return sBaseURL
; }
326 virtual SvParserState
CallParser();
328 inline const Color
& GetColor( size_t nId
) const;
329 const Font
& GetFont( sal_uInt16 nId
); // Changes the dflt Font
331 virtual int IsEndPara( SvxNodeIdx
* pNd
, xub_StrLen nCnt
) const = 0;
333 // to det a different attribute pool. May only be done prior to CallParser!
334 // The maps are not generated anew!
335 void SetAttrPool( SfxItemPool
* pNewPool
) { pAttrPool
= pNewPool
; }
336 // to set different WhichIds for a different pool.
337 RTFPardAttrMapIds
& GetPardMap()
338 { return (RTFPardAttrMapIds
&)*aPardMap
.begin(); }
339 RTFPlainAttrMapIds
& GetPlainMap()
340 { return (RTFPlainAttrMapIds
&)*aPlainMap
.begin(); }
341 // to be able to assign them from the outside as for example table cells
342 void ReadBorderAttr( int nToken
, SfxItemSet
& rSet
, int bTableDef
=sal_False
);
343 void ReadBackgroundAttr( int nToken
, SfxItemSet
& rSet
, int bTableDef
=sal_False
);
345 // for asynchronous read from the SvStream
346 virtual void Continue( int nToken
);
348 // get RTF default ItemSets. Must be used by pard/plain tokens or in
349 // reset of Style-Items
350 const SfxItemSet
& GetRTFDefaults();
351 virtual bool UncompressableStackEntry(const SvxRTFItemStackType
&rSet
) const;
354 // The stack for the attributes:
355 // this class may only be used by SvxRTFParser!
356 class EDITENG_DLLPUBLIC SvxRTFItemStackType
358 friend class SvxRTFParser
;
361 SvxNodeIdx
*pSttNd
, *pEndNd
;
362 xub_StrLen nSttCnt
, nEndCnt
;
363 SvxRTFItemStackList
* pChildList
;
366 SvxRTFItemStackType( SfxItemPool
&, const sal_uInt16
* pWhichRange
,
367 const SvxPosition
& );
369 void Add( SvxRTFItemStackType
* );
370 void Compress( const SvxRTFParser
& );
373 SvxRTFItemStackType( const SvxRTFItemStackType
&, const SvxPosition
&,
374 int bCopyAttr
= sal_False
);
375 ~SvxRTFItemStackType();
376 //cmc, I'm very suspicios about SetStartPos, it doesn't change
377 //its children's starting position, and the implementation looks
378 //bad, consider this deprecated.
379 void SetStartPos( const SvxPosition
& rPos
);
381 void MoveFullNode(const SvxNodeIdx
&rOldNode
,
382 const SvxNodeIdx
&rNewNode
);
384 sal_Int32
GetSttNodeIdx() const { return pSttNd
->GetIdx(); }
385 sal_Int32
GetEndNodeIdx() const { return pEndNd
->GetIdx(); }
387 const SvxNodeIdx
& GetSttNode() const { return *pSttNd
; }
388 const SvxNodeIdx
& GetEndNode() const { return *pEndNd
; }
390 xub_StrLen
GetSttCnt() const { return nSttCnt
; }
391 xub_StrLen
GetEndCnt() const { return nEndCnt
; }
393 SfxItemSet
& GetAttrSet() { return aAttrSet
; }
394 const SfxItemSet
& GetAttrSet() const { return aAttrSet
; }
396 sal_uInt16
StyleNo() const { return nStyleNo
; }
398 void SetRTFDefaults( const SfxItemSet
& rDefaults
);
402 // ----------- Inline Implementations --------------
404 inline const Color
& SvxRTFParser::GetColor( size_t nId
) const
406 ColorPtr pColor
= (ColorPtr
)pDfltColor
;
407 if( nId
< aColorTbl
.size() )
408 pColor
= aColorTbl
[ nId
];
412 inline SfxItemSet
& SvxRTFParser::GetAttrSet()
414 SvxRTFItemStackType
* pTmp
;
415 if( bNewGroup
|| 0 == ( pTmp
= aAttrStack
.empty() ? 0 : aAttrStack
.back()) )
416 pTmp
= _GetAttrSet();
417 return pTmp
->aAttrSet
;
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */