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 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_EDITENG_SVXRTF_HXX
21 : #define INCLUDED_EDITENG_SVXRTF_HXX
22 :
23 : #include <svl/itemset.hxx>
24 : #include <svtools/parrtf.hxx>
25 : #include <rtl/ustring.hxx>
26 :
27 : #include <editeng/editengdllapi.h>
28 :
29 : #include <deque>
30 : #include <utility>
31 : #include <vector>
32 : #include <boost/ptr_container/ptr_map.hpp>
33 : #include <boost/ptr_container/ptr_vector.hpp>
34 :
35 : namespace vcl { class Font; }
36 : class Color;
37 : class Graphic;
38 : class DateTime;
39 : struct SvxRTFStyleType;
40 : class SvxRTFItemStackType;
41 0 : class SvxRTFItemStackList : public boost::ptr_vector<SvxRTFItemStackType> {};
42 :
43 : namespace com { namespace sun { namespace star {
44 : namespace document {
45 : class XDocumentProperties;
46 : }
47 : namespace util {
48 : struct DateTime;
49 : }
50 : } } }
51 :
52 :
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
56 :
57 :
58 0 : class SvxNodeIdx
59 : {
60 : public:
61 0 : virtual ~SvxNodeIdx() {}
62 : virtual sal_Int32 GetIdx() const = 0;
63 : virtual SvxNodeIdx* Clone() const = 0; // Cloning itself
64 : };
65 :
66 0 : class SvxPosition
67 : {
68 : public:
69 0 : virtual ~SvxPosition() {}
70 :
71 : virtual sal_Int32 GetNodeIdx() const = 0;
72 : virtual sal_Int32 GetCntIdx() const = 0;
73 :
74 : virtual SvxPosition* Clone() const = 0; // Cloning itself
75 : virtual SvxNodeIdx* MakeNodeIdx() const = 0; // Cloning NodeIndex
76 : };
77 :
78 :
79 : typedef Color* ColorPtr;
80 : typedef std::deque< ColorPtr > SvxRTFColorTbl;
81 : typedef boost::ptr_map<short, vcl::Font> SvxRTFFontTbl;
82 : typedef boost::ptr_map<sal_uInt16, SvxRTFStyleType> SvxRTFStyleTbl;
83 :
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;
87 :
88 : // own helper classes for the RTF Parser
89 0 : struct SvxRTFStyleType
90 : {
91 : SfxItemSet aAttrSet; // the attributes of Style (+ derivate!)
92 : OUString sName;
93 : sal_uInt16 nBasedOn, nNext;
94 : bool bBasedOnIsSet;
95 : sal_uInt8 nOutlineNo;
96 : bool bIsCharFmt;
97 :
98 : SvxRTFStyleType( SfxItemPool& rPool, const sal_uInt16* pWhichRange );
99 : };
100 :
101 :
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 through
104 : // the SlotIds from POOL.
105 : struct RTFPlainAttrMapIds
106 : {
107 : sal_uInt16 nCaseMap,
108 : nBgColor,
109 : nColor,
110 : nContour,
111 : nCrossedOut,
112 : nEscapement,
113 : nFont,
114 : nFontHeight,
115 : nKering,
116 : nLanguage,
117 : nPosture,
118 : nShadowed,
119 : nUnderline,
120 : nOverline,
121 : nWeight,
122 : nWordlineMode,
123 : nAutoKerning,
124 : nCJKFont,
125 : nCJKFontHeight,
126 : nCJKLanguage,
127 : nCJKPosture,
128 : nCJKWeight,
129 : nCTLFont,
130 : nCTLFontHeight,
131 : nCTLLanguage,
132 : nCTLPosture,
133 : nCTLWeight,
134 : nEmphasis,
135 : nTwoLines,
136 : nCharScaleX,
137 : nHorzVert,
138 : nRuby,
139 : nRelief,
140 : nHidden
141 : ;
142 : RTFPlainAttrMapIds( const SfxItemPool& rPool );
143 : };
144 :
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 through
147 : // the SlotIds from POOL.
148 : struct RTFPardAttrMapIds
149 : {
150 : sal_uInt16 nLinespacing,
151 : nAdjust,
152 : nTabStop,
153 : nHyphenzone,
154 : nLRSpace,
155 : nULSpace,
156 : nBrush,
157 : nBox,
158 : nShadow,
159 : nOutlineLvl,
160 : nSplit,
161 : nKeep,
162 : nFontAlign,
163 : nScriptSpace,
164 : nHangPunct,
165 : nForbRule,
166 : nDirection
167 : ;
168 : RTFPardAttrMapIds( const SfxItemPool& rPool );
169 : };
170 :
171 :
172 :
173 :
174 :
175 :
176 : class EDITENG_DLLPUBLIC SvxRTFParser : public SvRTFParser
177 : {
178 : SvStream &rStrm;
179 : SvxRTFColorTbl aColorTbl;
180 : SvxRTFFontTbl aFontTbl;
181 : SvxRTFStyleTbl aStyleTbl;
182 : SvxRTFItemStack aAttrStack;
183 : SvxRTFItemStackList aAttrSetList;
184 :
185 : RTFPlainAttrMapIds aPlainMap;
186 : RTFPardAttrMapIds aPardMap;
187 : std::vector<sal_uInt16> aWhichMap;
188 : OUString sBaseURL;
189 :
190 : SvxPosition* pInsPos;
191 : SfxItemPool* pAttrPool;
192 : Color* pDfltColor;
193 : vcl::Font* pDfltFont;
194 : ::com::sun::star::uno::Reference<
195 : ::com::sun::star::document::XDocumentProperties> m_xDocProps;
196 : SfxItemSet *pRTFDefaults;
197 :
198 : long nVersionNo;
199 : int nDfltFont;
200 :
201 : bool bNewDoc : 1; // sal_False - Reading in an existing
202 : bool bNewGroup : 1; // sal_True - there was an opening parenthesis
203 : bool bIsSetDfltTab : 1; // sal_True - DefTab was loaded
204 : bool bChkStyleAttr : 1; // sal_True - StyleSheets are evaluated
205 : bool bCalcValue : 1; // sal_True - Twip values adapt to App
206 : bool bPardTokenRead : 1; // sal_True - Token \pard was detected
207 : bool bReadDocInfo : 1; // sal_True - DocInfo to read
208 : bool bIsLeftToRightDef : 1; // sal_True - in LeftToRight char run def.
209 : // sal_False - in RightToLeft char run def.
210 : bool bIsInReadStyleTab : 1; // sal_True - in ReadStyleTable
211 :
212 : void ClearColorTbl();
213 : void ClearFontTbl();
214 : void ClearStyleTbl();
215 : void ClearAttrStack();
216 :
217 : SvxRTFItemStackType* _GetAttrSet(bool bCopyAttr = false); // Create new ItemStackType:s
218 : void _ClearStyleAttr( SvxRTFItemStackType& rStkType );
219 :
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 );
224 :
225 : // Execute pard / plain
226 : void RTFPardPlain( bool bPard, SfxItemSet** ppSet );
227 :
228 : void BuildWhichTable();
229 :
230 : enum RTF_CharTypeDef
231 : {
232 : NOTDEF_CHARTYPE,
233 : LOW_CHARTYPE,
234 : HIGH_CHARTYPE,
235 : DOUBLEBYTE_CHARTYPE
236 : };
237 :
238 : // set latin/asian/complex character attributes
239 : void SetScriptAttr(
240 : RTF_CharTypeDef eType, SfxItemSet& rSet, SfxPoolItem& rItem );
241 :
242 : protected:
243 : virtual void InsertPara() = 0;
244 :
245 : static OUString& DelCharAtEnd( OUString& rStr, const sal_Unicode cDel );
246 :
247 : // is called for each token that is recognized in CallParser
248 : virtual void NextToken( int nToken ) SAL_OVERRIDE;
249 :
250 : virtual void ReadBitmapData() SAL_OVERRIDE;
251 : virtual void ReadOLEData() SAL_OVERRIDE;
252 :
253 : void ReadStyleTable();
254 : void ReadColorTable();
255 : void ReadFontTable();
256 : void ReadAttr( int nToken, SfxItemSet* pSet );
257 : void ReadTabAttr( int nToken, SfxItemSet& rSet );
258 :
259 : // Read Document-Info
260 : ::com::sun::star::util::DateTime GetDateTimeStamp( );
261 : OUString& GetTextToEndGroup( OUString& rStr );
262 : void ReadInfo( const sal_Char* pChkForVerNo = 0 );
263 :
264 : inline SfxItemSet& GetAttrSet();
265 : // no text yet inserted? (SttPos from the top stack entry!)
266 : bool IsAttrSttPos();
267 : void AttrGroupEnd(); // edit the current, delete from stack
268 : void SetAllAttrOfStk(); // end all Attr. and set it into doc
269 :
270 :
271 : virtual void InsertText() = 0;
272 : virtual void MovePos( bool bForward = true ) = 0;
273 : virtual void SetEndPrevPara( SvxNodeIdx*& rpNodePos,
274 : sal_Int32& rCntPos )=0;
275 : virtual void SetAttrInDoc( SvxRTFItemStackType &rSet );
276 : // for Tokens, which are not evaluated in ReadAttr
277 : virtual void UnknownAttrToken( int nToken, SfxItemSet* pSet );
278 :
279 : // if no-one would like to have any twips
280 : virtual void CalcValue();
281 :
282 : SvxRTFParser( SfxItemPool& rAttrPool,
283 : SvStream& rIn,
284 : ::com::sun::star::uno::Reference<
285 : ::com::sun::star::document::XDocumentProperties> i_xDocProps,
286 : bool bReadNewDoc = true );
287 : virtual ~SvxRTFParser();
288 :
289 : bool IsNewDoc() const { return bNewDoc; }
290 0 : void SetNewDoc( bool bFlag ) { bNewDoc = bFlag; }
291 : bool IsNewGroup() const { return bNewGroup; }
292 : void SetNewGroup( bool bFlag ) { bNewGroup = bFlag; }
293 0 : bool IsChkStyleAttr() const { return bChkStyleAttr; }
294 0 : void SetChkStyleAttr( bool bFlag ) { bChkStyleAttr = bFlag; }
295 0 : bool IsCalcValue() const { return bCalcValue; }
296 0 : void SetCalcValue( bool bFlag ) { bCalcValue = bFlag; }
297 : bool IsPardTokenRead() const { return bPardTokenRead; }
298 : void SetPardTokenRead( bool bFlag ) { bPardTokenRead = bFlag; }
299 : bool IsReadDocInfo() const { return bReadDocInfo; }
300 : void SetReadDocInfo( bool bFlag ) { bReadDocInfo = bFlag; }
301 :
302 : // Query/Set the current insert position
303 : SvxPosition& GetInsPos() const { return *pInsPos; }
304 : void SetInsPos( const SvxPosition& rNew );
305 :
306 : long GetVersionNo() const { return nVersionNo; }
307 :
308 0 : SvxRTFStyleTbl& GetStyleTbl() { return aStyleTbl; }
309 : SvxRTFItemStack& GetAttrStack() { return aAttrStack; }
310 : SvxRTFColorTbl& GetColorTbl() { return aColorTbl; }
311 : SvxRTFFontTbl& GetFontTbl() { return aFontTbl; }
312 :
313 : const OUString& GetBaseURL() const { return sBaseURL; }
314 :
315 : public:
316 :
317 : virtual SvParserState CallParser() SAL_OVERRIDE;
318 :
319 : inline const Color& GetColor( size_t nId ) const;
320 : const vcl::Font& GetFont( sal_uInt16 nId ); // Changes the default Font
321 :
322 : virtual bool IsEndPara( SvxNodeIdx* pNd, sal_Int32 nCnt ) const = 0;
323 :
324 : // to set a different attribute pool. May only be done prior to CallParser!
325 : // The maps are not generated anew!
326 0 : void SetAttrPool( SfxItemPool* pNewPool ) { pAttrPool = pNewPool; }
327 : // to set different WhichIds for a different pool.
328 0 : RTFPardAttrMapIds& GetPardMap() { return aPardMap; }
329 : RTFPlainAttrMapIds& GetPlainMap() { return aPlainMap; }
330 : // to be able to assign them from the outside as for example table cells
331 : void ReadBorderAttr( int nToken, SfxItemSet& rSet, bool bTableDef=false );
332 : void ReadBackgroundAttr( int nToken, SfxItemSet& rSet, bool bTableDef=false );
333 :
334 : // for asynchronous read from the SvStream
335 : virtual void Continue( int nToken ) SAL_OVERRIDE;
336 :
337 : // get RTF default ItemSets. Must be used by pard/plain tokens or in
338 : // reset of Style-Items
339 : const SfxItemSet& GetRTFDefaults();
340 : };
341 :
342 : // The stack for the attributes:
343 : // this class may only be used by SvxRTFParser!
344 : class EDITENG_DLLPUBLIC SvxRTFItemStackType
345 : {
346 : friend class SvxRTFParser;
347 :
348 : SfxItemSet aAttrSet;
349 : SvxNodeIdx *pSttNd, *pEndNd;
350 : sal_Int32 nSttCnt, nEndCnt;
351 : SvxRTFItemStackList* pChildList;
352 : sal_uInt16 nStyleNo;
353 :
354 : SvxRTFItemStackType( SfxItemPool&, const sal_uInt16* pWhichRange,
355 : const SvxPosition& );
356 :
357 : void Add( SvxRTFItemStackType* );
358 : void Compress( const SvxRTFParser& );
359 :
360 : public:
361 : SvxRTFItemStackType( const SvxRTFItemStackType&, const SvxPosition&,
362 : bool bCopyAttr = false );
363 : ~SvxRTFItemStackType();
364 : //cmc, I'm very suspicios about SetStartPos, it doesn't change
365 : //its children's starting position, and the implementation looks
366 : //bad, consider this deprecated.
367 : void SetStartPos( const SvxPosition& rPos );
368 :
369 : void MoveFullNode(const SvxNodeIdx &rOldNode,
370 : const SvxNodeIdx &rNewNode);
371 :
372 0 : sal_Int32 GetSttNodeIdx() const { return pSttNd->GetIdx(); }
373 0 : sal_Int32 GetEndNodeIdx() const { return pEndNd->GetIdx(); }
374 :
375 0 : const SvxNodeIdx& GetSttNode() const { return *pSttNd; }
376 0 : const SvxNodeIdx& GetEndNode() const { return *pEndNd; }
377 :
378 0 : sal_Int32 GetSttCnt() const { return nSttCnt; }
379 0 : sal_Int32 GetEndCnt() const { return nEndCnt; }
380 :
381 0 : SfxItemSet& GetAttrSet() { return aAttrSet; }
382 : const SfxItemSet& GetAttrSet() const { return aAttrSet; }
383 :
384 0 : sal_uInt16 StyleNo() const { return nStyleNo; }
385 :
386 : void SetRTFDefaults( const SfxItemSet& rDefaults );
387 : };
388 :
389 :
390 : // ----------- Inline Implementations --------------
391 :
392 0 : inline const Color& SvxRTFParser::GetColor( size_t nId ) const
393 : {
394 0 : ColorPtr pColor = pDfltColor;
395 0 : if( nId < aColorTbl.size() )
396 0 : pColor = aColorTbl[ nId ];
397 0 : return *pColor;
398 : }
399 :
400 0 : inline SfxItemSet& SvxRTFParser::GetAttrSet()
401 : {
402 : SvxRTFItemStackType* pTmp;
403 0 : if( bNewGroup || 0 == ( pTmp = aAttrStack.empty() ? 0 : aAttrStack.back()) )
404 0 : pTmp = _GetAttrSet();
405 0 : return pTmp->aAttrSet;
406 : }
407 :
408 :
409 : #endif
410 : // INCLUDED_EDITENG_SVXRTF_HXX
411 :
412 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|