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 .
20 #ifndef INCLUDED_EDITENG_SVXRTF_HXX
21 #define INCLUDED_EDITENG_SVXRTF_HXX
23 #include <svl/itemset.hxx>
24 #include <svtools/parrtf.hxx>
25 #include <rtl/ustring.hxx>
26 #include <tools/color.hxx>
28 #include <editeng/editengdllapi.h>
35 namespace vcl
{ class Font
; }
36 struct SvxRTFStyleType
;
37 class SvxRTFItemStackType
;
38 class SvxRTFItemStackList
: public std::vector
<std::unique_ptr
<SvxRTFItemStackType
>> {};
40 // Mapper-Classes for the various requirements on Document positions
41 // Swg - NodePosition is a SwIndex, which is used internally
42 // EditEngine - ULONG to list of paragraphs
49 EditNodeIdx(EditEngine
* pEE
, ContentNode
* pNd
);
50 sal_Int32
GetIdx() const;
51 ContentNode
* GetNode() { return mpNode
; }
53 EditEngine
* mpEditEngine
;
61 EditEngine
* mpEditEngine
;
62 EditSelection
* mpCurSel
;
65 EditPosition(EditEngine
* pIEE
, EditSelection
* pSel
);
67 sal_Int32
GetNodeIdx() const;
68 sal_Int32
GetCntIdx() const;
71 std::unique_ptr
<EditPosition
> Clone() const;
74 std::unique_ptr
<EditNodeIdx
> MakeNodeIdx() const;
77 typedef std::map
<short, std::unique_ptr
<vcl::Font
>> SvxRTFFontTbl
;
78 typedef std::map
<sal_uInt16
, std::unique_ptr
<SvxRTFStyleType
>> SvxRTFStyleTbl
;
80 // own helper classes for the RTF Parser
81 struct SvxRTFStyleType
83 SfxItemSet aAttrSet
; // the attributes of Style (+ derivated!)
88 SvxRTFStyleType( SfxItemPool
& rPool
, const sal_uInt16
* pWhichRange
);
92 // Here are the IDs for all character attributes, which can be detected by
93 // SvxParser and can be set in a SfxItemSet. The IDs are set correctly through
94 // the SlotIds from POOL.
95 struct RTFPlainAttrMapIds
131 RTFPlainAttrMapIds( const SfxItemPool
& rPool
);
134 // Here are the IDs for all paragraph attributes, which can be detected by
135 // SvxParser and can be set in a SfxItemSet. The IDs are set correctly through
136 // the SlotIds from POOL.
137 struct RTFPardAttrMapIds
139 sal_uInt16 nLinespacing
,
157 RTFPardAttrMapIds( const SfxItemPool
& rPool
);
161 class EDITENG_DLLPUBLIC SvxRTFParser
: public SvRTFParser
163 std::vector
<Color
> maColorTable
;
164 SvxRTFFontTbl m_FontTable
;
165 SvxRTFStyleTbl m_StyleTable
;
166 std::deque
< std::unique_ptr
<SvxRTFItemStackType
> > aAttrStack
;
167 SvxRTFItemStackList m_AttrSetList
;
169 RTFPlainAttrMapIds aPlainMap
;
170 RTFPardAttrMapIds aPardMap
;
171 std::vector
<sal_uInt16
> aWhichMap
;
173 std::unique_ptr
<EditPosition
> pInsPos
;
174 SfxItemPool
* pAttrPool
;
175 std::optional
<Color
> mxDefaultColor
;
176 std::unique_ptr
<vcl::Font
> pDfltFont
;
177 std::unique_ptr
<SfxItemSet
> pRTFDefaults
;
181 bool bNewDoc
: 1; // sal_False - Reading in an existing
182 bool bNewGroup
: 1; // sal_True - there was an opening parenthesis
183 bool bIsSetDfltTab
: 1; // sal_True - DefTab was loaded
184 bool bChkStyleAttr
: 1; // sal_True - StyleSheets are evaluated
185 bool bCalcValue
: 1; // sal_True - Twip values adapt to App
186 bool bIsLeftToRightDef
: 1; // sal_True - in LeftToRight char run def.
187 // sal_False - in RightToLeft char run def.
188 bool bIsInReadStyleTab
: 1; // sal_True - in ReadStyleTable
190 SvxRTFParser(SvxRTFParser
const&) = delete;
191 void operator=(SvxRTFParser
const&) = delete;
193 void ClearColorTbl();
194 void ClearAttrStack();
196 SvxRTFItemStackType
* GetAttrSet_(); // Create new ItemStackType:s
197 void ClearStyleAttr_( SvxRTFItemStackType
& rStkType
);
199 // Sets all the attributes that are different from the current
200 void SetAttrSet( SvxRTFItemStackType
&rSet
);
201 void SetDefault( int nToken
, int nValue
);
203 // Execute pard / plain
204 void RTFPardPlain( bool bPard
, SfxItemSet
** ppSet
);
206 void BuildWhichTable();
216 // set latin/asian/complex character attributes
218 RTF_CharTypeDef eType
, SfxItemSet
& rSet
, SfxPoolItem
& rItem
);
221 virtual void InsertPara() = 0;
223 static OUString
& DelCharAtEnd( OUString
& rStr
, const sal_Unicode cDel
);
225 // is called for each token that is recognized in CallParser
226 virtual void NextToken( int nToken
) override
;
228 void ReadStyleTable();
229 void ReadColorTable();
230 void ReadFontTable();
231 void ReadAttr( int nToken
, SfxItemSet
* pSet
);
232 void ReadTabAttr( int nToken
, SfxItemSet
& rSet
);
234 inline SfxItemSet
& GetAttrSet();
235 // no text yet inserted? (SttPos from the top stack entry!)
237 void AttrGroupEnd(); // edit the current, delete from stack
238 void SetAllAttrOfStk(); // end all Attr. and set it into doc
241 virtual void InsertText() = 0;
242 virtual void MovePos( bool bForward
= true ) = 0;
243 virtual void SetEndPrevPara( EditNodeIdx
*& rpNodePos
,
244 sal_Int32
& rCntPos
)=0;
245 virtual void SetAttrInDoc( SvxRTFItemStackType
&rSet
);
246 // for Tokens, which are not evaluated in ReadAttr
247 virtual void UnknownAttrToken( int nToken
);
249 // if no-one would like to have any twips
250 virtual void CalcValue();
252 SvxRTFParser( SfxItemPool
& rAttrPool
, SvStream
& rIn
);
253 virtual ~SvxRTFParser() override
;
255 void SetNewDoc( bool bFlag
) { bNewDoc
= bFlag
; }
256 bool IsChkStyleAttr() const { return bChkStyleAttr
; }
257 void SetChkStyleAttr( bool bFlag
) { bChkStyleAttr
= bFlag
; }
258 bool IsCalcValue() const { return bCalcValue
; }
259 void SetCalcValue( bool bFlag
) { bCalcValue
= bFlag
; }
261 // Query/Set the current insert position
262 void SetInsPos( const EditPosition
& rNew
);
263 SvxRTFStyleTbl
& GetStyleTbl() { return m_StyleTable
; }
267 virtual SvParserState
CallParser() override
;
269 inline const Color
& GetColor( size_t nId
) const;
270 const vcl::Font
& GetFont( sal_uInt16 nId
); // Changes the default Font
272 virtual bool IsEndPara( EditNodeIdx
* pNd
, sal_Int32 nCnt
) const = 0;
274 // to set a different attribute pool. May only be done prior to CallParser!
275 // The maps are not generated anew!
276 void SetAttrPool( SfxItemPool
* pNewPool
) { pAttrPool
= pNewPool
; }
277 // to set different WhichIds for a different pool.
278 RTFPardAttrMapIds
& GetPardMap() { return aPardMap
; }
279 // to be able to assign them from the outside as for example table cells
280 void ReadBorderAttr( int nToken
, SfxItemSet
& rSet
, bool bTableDef
=false );
281 void ReadBackgroundAttr( int nToken
, SfxItemSet
& rSet
, bool bTableDef
=false );
283 // for asynchronous read from the SvStream
284 virtual void Continue( int nToken
) override
;
286 // get RTF default ItemSets. Must be used by pard/plain tokens or in
287 // reset of Style-Items
288 const SfxItemSet
& GetRTFDefaults();
291 // The stack for the attributes:
292 // this class may only be used by SvxRTFParser!
293 class SvxRTFItemStackType
295 friend class SvxRTFParser
;
298 std::unique_ptr
<EditNodeIdx
> pSttNd
;
300 sal_Int32 nSttCnt
, nEndCnt
;
301 std::unique_ptr
<SvxRTFItemStackList
> m_pChildList
;
304 SvxRTFItemStackType(SvxRTFItemStackType
const&) = delete;
305 void operator=(SvxRTFItemStackType
const&) = delete;
307 SvxRTFItemStackType( SfxItemPool
&, const sal_uInt16
* pWhichRange
,
308 const EditPosition
& );
310 void Add(std::unique_ptr
<SvxRTFItemStackType
>);
311 void Compress( const SvxRTFParser
& );
312 void DropChildList();
315 SvxRTFItemStackType(const SvxRTFItemStackType
&, const EditPosition
&,
317 ~SvxRTFItemStackType();
318 //cmc, I'm very suspicious about SetStartPos, it doesn't change
319 //its children's starting position, and the implementation looks
320 //bad, consider this deprecated.
321 void SetStartPos( const EditPosition
& rPos
);
323 const EditNodeIdx
& GetSttNode() const { return *pSttNd
; }
324 const EditNodeIdx
& GetEndNode() const { return *pEndNd
; }
326 sal_Int32
GetSttCnt() const { return nSttCnt
; }
327 sal_Int32
GetEndCnt() const { return nEndCnt
; }
329 SfxItemSet
& GetAttrSet() { return aAttrSet
; }
330 const SfxItemSet
& GetAttrSet() const { return aAttrSet
; }
332 sal_uInt16
StyleNo() const { return nStyleNo
; }
334 void SetRTFDefaults( const SfxItemSet
& rDefaults
);
338 // ----------- Inline Implementations --------------
340 inline const Color
& SvxRTFParser::GetColor( size_t nId
) const
342 if( nId
< maColorTable
.size() )
343 return maColorTable
[ nId
];
344 return *mxDefaultColor
;
347 inline SfxItemSet
& SvxRTFParser::GetAttrSet()
349 SvxRTFItemStackType
* pTmp
;
350 if( bNewGroup
|| aAttrStack
.empty() )
351 pTmp
= GetAttrSet_();
354 pTmp
= aAttrStack
.back().get();
355 if ( pTmp
== nullptr )
356 pTmp
= GetAttrSet_();
358 return pTmp
->aAttrSet
;
362 #endif // INCLUDED_EDITENG_SVXRTF_HXX
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */