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>
27 #include <vcl/font.hxx>
29 #include <editeng/editengdllapi.h>
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 SwContentIndex, 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 EditNodeIdx
MakeNodeIdx() const;
74 typedef std::map
<short, vcl::Font
> SvxRTFFontTbl
;
76 // own helper classes for the RTF Parser
77 struct SvxRTFStyleType
79 SfxItemSet aAttrSet
; // the attributes of Style (+ derivated!)
84 SvxRTFStyleType(SfxItemPool
& rPool
, const WhichRangesContainer
& pWhichRange
);
87 typedef std::map
<sal_uInt16
, SvxRTFStyleType
> SvxRTFStyleTbl
;
89 class EDITENG_DLLPUBLIC SvxRTFParser
: public SvRTFParser
91 std::vector
<Color
> maColorTable
;
92 SvxRTFFontTbl m_FontTable
;
93 SvxRTFStyleTbl m_StyleTable
;
94 std::deque
< std::unique_ptr
<SvxRTFItemStackType
> > aAttrStack
;
95 SvxRTFItemStackList m_AttrSetList
;
99 std::map
<sal_uInt16
, sal_uInt16
> data
;
101 TypedWhichId
<T
> operator[](TypedWhichId
<T
> in
) const { return TypedWhichId
<T
>(data
.at(in
)); }
103 void set(TypedWhichId
<T
> in
, TypedWhichId
<T
> out
) { data
[in
] = out
; }
105 PlainOrPardMap aPlainMap
;
106 PlainOrPardMap aPardMap
;
107 WhichRangesContainer aWhichMap
;
109 std::optional
<EditPosition
> mxInsertPosition
;
110 SfxItemPool
* pAttrPool
;
111 std::optional
<Color
> mxDefaultColor
;
112 std::optional
<vcl::Font
> pDfltFont
;
113 std::unique_ptr
<SfxItemSet
> pRTFDefaults
;
117 bool bNewDoc
: 1; // sal_False - Reading in an existing
118 bool bNewGroup
: 1; // sal_True - there was an opening parenthesis
119 bool bIsSetDfltTab
: 1; // sal_True - DefTab was loaded
120 bool bChkStyleAttr
: 1; // sal_True - StyleSheets are evaluated
121 bool bCalcValue
: 1; // sal_True - Twip values adapt to App
122 bool bIsLeftToRightDef
: 1; // sal_True - in LeftToRight char run def.
123 // sal_False - in RightToLeft char run def.
124 bool bIsInReadStyleTab
: 1; // sal_True - in ReadStyleTable
126 SvxRTFParser(SvxRTFParser
const&) = delete;
127 void operator=(SvxRTFParser
const&) = delete;
129 void ClearColorTbl();
130 void ClearAttrStack();
132 SvxRTFItemStackType
* GetAttrSet_(); // Create new ItemStackType:s
133 void ClearStyleAttr_( SvxRTFItemStackType
& rStkType
);
135 // Sets all the attributes that are different from the current
136 void SetAttrSet( SvxRTFItemStackType
&rSet
);
137 void SetDefault( int nToken
, int nValue
);
139 // Execute pard / plain
140 void RTFPardPlain( bool bPard
, SfxItemSet
** ppSet
);
142 void BuildWhichTable();
152 // set latin/asian/complex character attributes
154 RTF_CharTypeDef eType
, SfxItemSet
& rSet
, SfxPoolItem
& rItem
);
157 virtual void InsertPara() = 0;
159 static void DelCharAtEnd( OUStringBuffer
& rStr
, const sal_Unicode cDel
);
161 // is called for each token that is recognized in CallParser
162 virtual void NextToken( int nToken
) override
;
164 void ReadStyleTable();
165 void ReadColorTable();
166 void ReadFontTable();
167 void ReadAttr( int nToken
, SfxItemSet
* pSet
);
168 void ReadTabAttr( int nToken
, SfxItemSet
& rSet
);
170 inline SfxItemSet
& GetAttrSet();
171 // no text yet inserted? (SttPos from the top stack entry!)
173 void AttrGroupEnd(); // edit the current, delete from stack
174 void SetAllAttrOfStk(); // end all Attr. and set it into doc
177 virtual void InsertText() = 0;
178 virtual void MovePos( bool bForward
= true ) = 0;
179 virtual void SetEndPrevPara( std::optional
<EditNodeIdx
>& rpNodePos
,
180 sal_Int32
& rCntPos
)=0;
181 virtual void SetAttrInDoc( SvxRTFItemStackType
&rSet
);
182 // for Tokens, which are not evaluated in ReadAttr
183 virtual void UnknownAttrToken( int nToken
);
185 // if no-one would like to have any twips
186 virtual void CalcValue();
188 SvxRTFParser( SfxItemPool
& rAttrPool
, SvStream
& rIn
);
189 virtual ~SvxRTFParser() override
;
191 void SetNewDoc( bool bFlag
) { bNewDoc
= bFlag
; }
192 bool IsChkStyleAttr() const { return bChkStyleAttr
; }
193 void SetChkStyleAttr( bool bFlag
) { bChkStyleAttr
= bFlag
; }
194 bool IsCalcValue() const { return bCalcValue
; }
195 void SetCalcValue( bool bFlag
) { bCalcValue
= bFlag
; }
197 // Query/Set the current insert position
198 void SetInsPos( const EditPosition
& rNew
);
199 SvxRTFStyleTbl
& GetStyleTbl() { return m_StyleTable
; }
203 virtual SvParserState
CallParser() override
;
205 inline const Color
& GetColor( size_t nId
) const;
206 const vcl::Font
& GetFont( sal_uInt16 nId
); // Changes the default Font
208 virtual bool IsEndPara( EditNodeIdx
* pNd
, sal_Int32 nCnt
) const = 0;
210 // to set a different attribute pool. May only be done prior to CallParser!
211 // The maps are not generated anew!
212 void SetAttrPool( SfxItemPool
* pNewPool
) { pAttrPool
= pNewPool
; }
213 // to set different WhichIds for a different pool.
215 void SetPardMap(TypedWhichId
<T
> wid
, TypedWhichId
<T
> widTrue
) { aPardMap
.set(wid
, widTrue
); }
216 // to be able to assign them from the outside as for example table cells
217 void ReadBorderAttr( int nToken
, SfxItemSet
& rSet
, bool bTableDef
=false );
218 void ReadBackgroundAttr( int nToken
, SfxItemSet
& rSet
, bool bTableDef
=false );
220 // for asynchronous read from the SvStream
221 virtual void Continue( int nToken
) override
;
223 // get RTF default ItemSets. Must be used by pard/plain tokens or in
224 // reset of Style-Items
225 const SfxItemSet
& GetRTFDefaults();
228 // The stack for the attributes:
229 // this class may only be used by SvxRTFParser!
230 class SvxRTFItemStackType
232 friend class SvxRTFParser
;
235 std::optional
<EditNodeIdx
> mxStartNodeIdx
;
236 std::optional
<EditNodeIdx
> mxEndNodeIdx
;
237 sal_Int32 nSttCnt
, nEndCnt
;
238 SvxRTFItemStackList maChildList
;
241 SvxRTFItemStackType(SvxRTFItemStackType
const&) = delete;
242 void operator=(SvxRTFItemStackType
const&) = delete;
244 SvxRTFItemStackType( SfxItemPool
&, const WhichRangesContainer
& pWhichRange
,
245 const EditPosition
& );
247 static std::unique_ptr
<SvxRTFItemStackType
> createSvxRTFItemStackType(
248 SfxItemPool
&, const WhichRangesContainer
& pWhichRange
, const EditPosition
&);
250 void Add(std::unique_ptr
<SvxRTFItemStackType
>);
251 void Compress( const SvxRTFParser
& );
252 void DropChildList();
255 SvxRTFItemStackType(const SvxRTFItemStackType
&, const EditPosition
&,
257 ~SvxRTFItemStackType();
258 //cmc, I'm very suspicious about SetStartPos, it doesn't change
259 //its children's starting position, and the implementation looks
260 //bad, consider this deprecated.
261 void SetStartPos( const EditPosition
& rPos
);
263 const EditNodeIdx
& GetSttNode() const { return *mxStartNodeIdx
; }
264 const EditNodeIdx
& GetEndNode() const { return *mxEndNodeIdx
; }
266 sal_Int32
GetSttCnt() const { return nSttCnt
; }
267 sal_Int32
GetEndCnt() const { return nEndCnt
; }
269 SfxItemSet
& GetAttrSet() { return aAttrSet
; }
270 const SfxItemSet
& GetAttrSet() const { return aAttrSet
; }
272 sal_uInt16
StyleNo() const { return nStyleNo
; }
274 void SetRTFDefaults( const SfxItemSet
& rDefaults
);
278 // ----------- Inline Implementations --------------
280 inline const Color
& SvxRTFParser::GetColor( size_t nId
) const
282 if( nId
< maColorTable
.size() )
283 return maColorTable
[ nId
];
284 return *mxDefaultColor
;
287 inline SfxItemSet
& SvxRTFParser::GetAttrSet()
289 SvxRTFItemStackType
* pTmp
;
290 if( bNewGroup
|| aAttrStack
.empty() )
291 pTmp
= GetAttrSet_();
294 pTmp
= aAttrStack
.back().get();
295 if ( pTmp
== nullptr )
296 pTmp
= GetAttrSet_();
298 return pTmp
->aAttrSet
;
302 #endif // INCLUDED_EDITENG_SVXRTF_HXX
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */