xref: /aoo4110/main/editeng/inc/editeng/svxrtf.hxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SVXRTF_HXX
25 #define _SVXRTF_HXX
26 
27 #include <tools/table.hxx>
28 #include <tools/string.hxx>
29 #include <svl/itemset.hxx>
30 #include <svtools/parrtf.hxx>
31 
32 #define _SVSTDARR_sal_uInt16S
33 #include <svl/svstdarr.hxx>
34 #include <editeng/editengdllapi.h>
35 
36 #include <deque>
37 #include <utility>
38 #include <vector>
39 class Font;
40 class Color;
41 class Graphic;
42 class DateTime;
43 struct SvxRTFStyleType;
44 class SvxRTFItemStackType;
45 class SvxRTFItemStackList;
46 
47 namespace com { namespace sun { namespace star {
48     namespace document {
49         class XDocumentProperties;
50     }
51     namespace util {
52         struct DateTime;
53     }
54 } } }
55 
56 
57 // Mapper-Klassen fuer die verschiedenen Anforderungen an Doc-Positionen
58 //		  Swg - NodePosition ist ein SwIndex, der intern veraendert wird
59 // EditEngine - sal_uInt32 auf Liste von Absaetzen
60 // .....
61 
62 class SvxNodeIdx
63 {
64 public:
~SvxNodeIdx()65 	virtual ~SvxNodeIdx() {}
66 	virtual sal_uLong	GetIdx() const = 0;
67 	virtual SvxNodeIdx* Clone() const = 0;	// erzeuge von sich eine Kopie
68 };
69 
70 class SvxPosition
71 {
72 public:
~SvxPosition()73 	virtual ~SvxPosition() {}
74 
75 	virtual sal_uLong	GetNodeIdx() const = 0;
76 	virtual xub_StrLen	GetCntIdx() const = 0;
77 
78 	virtual SvxPosition* Clone() const = 0; // erzeuge von sich eine Kopie
79 	virtual SvxNodeIdx* MakeNodeIdx() const = 0; // erzeuge vom NodeIndex eine Kopie
80 };
81 
82 
83 typedef Color* ColorPtr;
84 typedef std::deque< ColorPtr > SvxRTFColorTbl;
85 DECLARE_TABLE( SvxRTFFontTbl, Font* )
86 DECLARE_TABLE( SvxRTFStyleTbl, SvxRTFStyleType* )
87 typedef SvxRTFItemStackType* SvxRTFItemStackTypePtr;
88 SV_DECL_PTRARR_DEL( SvxRTFItemStackList, SvxRTFItemStackTypePtr, 1, 1 )
89 
90 // SvxRTFItemStack can't be "std::stack< SvxRTFItemStackTypePtr >" type, because
91 // the methods are using operator[] in sw/source/filter/rtf/rtftbl.cxx file
92 typedef std::deque< SvxRTFItemStackTypePtr > SvxRTFItemStack;
93 
94 // einige Hilfsklassen fuer den RTF-Parser
95 struct SvxRTFStyleType
96 {
97 	SfxItemSet aAttrSet;		// die Attribute vom Style (+Ableitung!)
98 	String sName;
99 	sal_uInt16 nBasedOn, nNext;
100 	sal_Bool bBasedOnIsSet; //$flr #117411#
101 	sal_uInt8 nOutlineNo;
102 	sal_Bool bIsCharFmt;
103 
104 	SvxRTFStyleType( SfxItemPool& rPool, const sal_uInt16* pWhichRange );
105 };
106 
107 
108 // Bitmap - Mode
109 typedef ::std::vector< ::std::pair< ::rtl::OUString, ::rtl::OUString > > PictPropertyNameValuePairs;
110 struct EDITENG_DLLPUBLIC SvxRTFPictureType
111 {
112 	// Format der Bitmap
113 	enum RTF_BMPSTYLE
114 	{
115 		RTF_BITMAP, 		// Bitmap, die Planes sind in den Pict.Daten
116 		WIN_METAFILE,		// in den Pict.Daten steht ein Window-Metafile
117 		MAC_QUICKDRAW,		// in den Pict.Daten steht ein Mac-QuickDraw
118 		OS2_METAFILE,		// in den Pict.Daten steht ein OS2-Metafile
119 		RTF_DI_BMP,			// Device Independent Bitmap
120 		ENHANCED_MF,		// in den Pict.Daten steht ein Enhanced-Metafile
121 		RTF_PNG,			// in den Pict.Daten steht ein PNG file
122 		RTF_JPG				// in den Pict.Daten steht ein JPG file
123 	} eStyle;
124 
125 	enum RTF_BMPMODE
126 	{
127 		BINARY_MODE,
128 		HEX_MODE
129 	} nMode;
130 
131 	sal_uInt16	nType;
132 	sal_uInt32 uPicLen;
133 	sal_uInt16	nWidth, nHeight;
134 	sal_uInt16	nGoalWidth, nGoalHeight;
135 	sal_uInt16	nBitsPerPixel;
136 	sal_uInt16	nPlanes;
137 	sal_uInt16	nWidthBytes;
138 	sal_uInt16	nScalX, nScalY;
139 	short 	nCropT, nCropB, nCropL, nCropR;
140     PictPropertyNameValuePairs aPropertyPairs;
SvxRTFPictureTypeSvxRTFPictureType141 	SvxRTFPictureType() { ResetValues(); }
142 	// alle Werte auf default; wird nach einlesen der Bitmap aufgerufen !
143 	void ResetValues();
144 };
145 
146 // Hier sind die Ids fuer alle Charakter-Attribute, die vom SvxParser
147 // erkannt und in einem SfxItemSet gesetzt werden koennen.
148 // Die Ids werden ueber die SlotIds vom POOL richtig gesetzt.
149 struct RTFPlainAttrMapIds
150 {
151 	sal_uInt16 	nCaseMap,
152 			nBgColor,
153 			nColor,
154 			nContour,
155 			nCrossedOut,
156 			nEscapement,
157 			nFont,
158 			nFontHeight,
159 			nKering,
160 			nLanguage,
161 			nPosture,
162 			nShadowed,
163 			nUnderline,
164 			nOverline,
165 			nWeight,
166 			nWordlineMode,
167 			nAutoKerning,
168 			nCJKFont,
169 			nCJKFontHeight,
170 			nCJKLanguage,
171 			nCJKPosture,
172 			nCJKWeight,
173 			nCTLFont,
174 			nCTLFontHeight,
175 			nCTLLanguage,
176 			nCTLPosture,
177 			nCTLWeight,
178 			nEmphasis,
179 			nTwoLines,
180 			nCharScaleX,
181 			nHorzVert,
182 			nRuby,
183 			nRelief,
184 			nHidden
185 			;
186 	RTFPlainAttrMapIds( const SfxItemPool& rPool );
187 };
188 
189 // Hier sind die Ids fuer alle Paragraph-Attribute, die vom SvxParser
190 // erkannt und in einem SfxItemSet gesetzt werden koennen.
191 // Die Ids werden ueber die SlotIds vom POOL richtig gesetzt.
192 struct RTFPardAttrMapIds
193 {
194 	sal_uInt16	nLinespacing,
195 			nAdjust,
196 			nTabStop,
197 			nHyphenzone,
198 			nLRSpace,
199 			nULSpace,
200 			nBrush,
201 			nBox,
202 			nShadow,
203 			nOutlineLvl,
204 			nSplit,
205 			nKeep,
206 			nFontAlign,
207 			nScriptSpace,
208 			nHangPunct,
209 			nForbRule,
210             nDirection
211 			;
212 	RTFPardAttrMapIds( const SfxItemPool& rPool );
213 };
214 
215 
216 
217 // -----------------------------------------------------------------------
218 
219 
220 class EDITENG_DLLPUBLIC SvxRTFParser : public SvRTFParser
221 {
222 	SvStream &rStrm;
223 	SvxRTFColorTbl	aColorTbl;
224 	SvxRTFFontTbl	aFontTbl;
225 	SvxRTFStyleTbl	aStyleTbl;
226 	SvxRTFItemStack aAttrStack;
227 	SvxRTFItemStackList aAttrSetList;
228 
229 	SvUShorts aPlainMap;
230 	SvUShorts aPardMap;
231 	SvUShorts aWhichMap;
232 	String	sBaseURL;
233 
234 	SvxPosition* pInsPos;
235 	SfxItemPool* pAttrPool;
236 	Color*  pDfltColor;
237 	Font*	pDfltFont;
238     ::com::sun::star::uno::Reference<
239         ::com::sun::star::document::XDocumentProperties> m_xDocProps;
240 	SfxItemSet *pRTFDefaults;
241 
242 	long	nVersionNo;
243 	int 	nDfltFont;
244 
245 	sal_Bool	bNewDoc : 1;			// sal_False - in ein bestehendes lesen
246 	sal_Bool	bNewGroup : 1;			// sal_True - es gab eine oeffnende Klammer
247 	sal_Bool	bIsSetDfltTab : 1;		// sal_True - DefTab wurde eingelesen
248 	sal_Bool	bChkStyleAttr : 1;		// sal_True - StyleSheets werden ausgewertet
249 	sal_Bool	bCalcValue : 1; 		// sal_True - Twipwerte an APP anpassen
250 	sal_Bool	bPardTokenRead : 1; 	// sal_True - Token \pard wurde erkannt
251 	sal_Bool	bReadDocInfo : 1;		// sal_True - DocInfo mit einlesen
252 	sal_Bool	bIsLeftToRightDef : 1; 	// sal_True - in LeftToRight char run def.
253 									// sal_False - in RightToLeft char run def.
254 	sal_Bool 	bIsInReadStyleTab : 1;	// sal_True - in ReadStyleTable
255 
256 	void ClearColorTbl();
257 	void ClearFontTbl();
258 	void ClearStyleTbl();
259 	void ClearAttrStack();
260 
261 	SvxRTFItemStackTypePtr _GetAttrSet( int bCopyAttr=sal_False );	// neue ItemStackType anlegen
262 	void _ClearStyleAttr( SvxRTFItemStackType& rStkType );
263 
264 	// setzt alle Attribute, die unterschiedlich zum aktuellen sind
265 	void SetAttrSet( SfxItemSet& rAttrSet, SvxPosition& rSttPos );
266 	void SetAttrSet( SvxRTFItemStackType &rSet );
267 	void SetDefault( int nToken, int nValue );
268 
269 	// pard / plain abarbeiten
270 	void RTFPardPlain( int bPard, SfxItemSet** ppSet );
271 
272 	void BuildWhichTbl();
273 
274     enum RTF_CharTypeDef
275     {
276         NOTDEF_CHARTYPE,
277         LOW_CHARTYPE,
278         HIGH_CHARTYPE,
279         DOUBLEBYTE_CHARTYPE
280     };
281 
282 		// set latin/asian/complex character attributes
283 	void SetScriptAttr(
284         RTF_CharTypeDef eType, SfxItemSet& rSet, SfxPoolItem& rItem );
285 
286 protected:
287 	virtual void EnterEnvironment();
288 	virtual void LeaveEnvironment();
289 	virtual void ResetPard();
290 	virtual void InsertPara() = 0;
291 
292 
293 	String& DelCharAtEnd( String& rStr, const sal_Unicode cDel );
294 
295 	// wird fuer jedes Token gerufen, das in CallParser erkannt wird
296 	virtual void NextToken( int nToken );
297 
298 	virtual void ReadBitmapData();
299 	virtual void ReadOLEData();
300 
301 	void ReadStyleTable();
302 	void ReadColorTable();
303 	void ReadFontTable();
304 	void ReadAttr( int nToken, SfxItemSet* pSet );
305 	void ReadTabAttr( int nToken, SfxItemSet& rSet );
306 
307 	// Dokument-Info lesen
308 	::com::sun::star::util::DateTime GetDateTimeStamp( );
309 	String& GetTextToEndGroup( String& rStr );
310 	virtual void ReadInfo( const sal_Char* pChkForVerNo = 0 );
311 
312 	inline SfxItemSet& GetAttrSet();
313 	// wurde noch kein Text eingefuegt ? (SttPos vom obersten StackEintrag!)
314 	int IsAttrSttPos();
315 	void AttrGroupEnd();		// den akt. Bearbeiten, vom Stack loeschen
316 	void SetAllAttrOfStk();		// end all Attr. and set it into doc
317 
318 
319 	virtual void InsertText() = 0;
320 	virtual void MovePos( int bForward = sal_True ) = 0;
321 	virtual void SetEndPrevPara( SvxNodeIdx*& rpNodePos,
322 								 xub_StrLen& rCntPos )=0;
323 	virtual void SetAttrInDoc( SvxRTFItemStackType &rSet );
324 	// fuer Tokens, die im ReadAttr nicht ausgewertet werden
325 	virtual void UnknownAttrToken( int nToken, SfxItemSet* pSet );
326 
327 	// falls jemand keine Twips haben moechte
328     virtual void CalcValue();
329 
330 	SvxRTFParser( SfxItemPool& rAttrPool,
331 					SvStream& rIn,
332                     ::com::sun::star::uno::Reference<
333                         ::com::sun::star::document::XDocumentProperties> i_xDocProps,
334 					int bReadNewDoc = sal_True );
335 	virtual ~SvxRTFParser();
336 
IsNewDoc() const337 	int IsNewDoc() const				{ return bNewDoc; }
SetNewDoc(int bFlag)338 	void SetNewDoc( int bFlag ) 		{ bNewDoc = bFlag; }
IsNewGroup() const339 	int IsNewGroup() const				{ return bNewGroup; }
SetNewGroup(int bFlag)340 	void SetNewGroup( int bFlag )		{ bNewGroup = bFlag; }
IsChkStyleAttr() const341 	int IsChkStyleAttr() const			{ return bChkStyleAttr; }
SetChkStyleAttr(int bFlag)342 	void SetChkStyleAttr( int bFlag )	{ bChkStyleAttr = bFlag; }
IsCalcValue() const343 	int IsCalcValue() const 			{ return bCalcValue; }
SetCalcValue(int bFlag)344 	void SetCalcValue( int bFlag )		{ bCalcValue = bFlag; }
IsPardTokenRead() const345 	int IsPardTokenRead() const 		{ return bPardTokenRead; }
SetPardTokenRead(int bFlag)346 	void SetPardTokenRead( int bFlag )	{ bPardTokenRead = bFlag; }
IsReadDocInfo() const347 	int IsReadDocInfo() const			{ return bReadDocInfo; }
SetReadDocInfo(int bFlag)348 	void SetReadDocInfo( int bFlag )	{ bReadDocInfo = bFlag; }
349 
350 	// erfrage/setze die aktuelle Einfuegeposition
GetInsPos() const351 	SvxPosition& GetInsPos() const		{ return *pInsPos; }
352 	void SetInsPos( const SvxPosition& rNew );
353 
GetVersionNo() const354 	long GetVersionNo() const 			{ return nVersionNo; }
355 
356 	// erfrage/setze die Mapping-Ids fuer die Pard/Plain Attribute
357 	// (Set: es werden sich die Pointer gemerkt, also keine Kopie erzeugt!!! )
AddPardAttr(sal_uInt16 nWhich)358 	void AddPardAttr( sal_uInt16 nWhich ) { aPardMap.Insert( nWhich, aPardMap.Count() ); }
AddPlainAttr(sal_uInt16 nWhich)359 	void AddPlainAttr( sal_uInt16 nWhich ) { aPlainMap.Insert( nWhich, aPlainMap.Count() ); }
360 
GetStyleTbl()361 	SvxRTFStyleTbl& GetStyleTbl()				{ return aStyleTbl; }
GetAttrStack()362 	SvxRTFItemStack& GetAttrStack() 			{ return aAttrStack; }
GetColorTbl()363 	SvxRTFColorTbl&	GetColorTbl()				{ return aColorTbl; }
GetFontTbl()364 	SvxRTFFontTbl& GetFontTbl()					{ return aFontTbl; }
365 
GetBaseURL() const366 	const String& GetBaseURL() const			{ return sBaseURL; }
367 
368 		// lesen die GrafikDaten und fuelle damit die Grafik und die
369 		// die PicDaten.
370 		// Return - sal_True: die Grafik ist gueltig
371 	sal_Bool ReadBmpData( Graphic& rGrf, SvxRTFPictureType& rPicType );
372 		// wandel die ASCII-HexCodes in binaere Zeichen um. Werden
373 		// ungueltige Daten gefunden (Zeichen ausser 0-9|a-f|A-F, so
374 		// wird USHRT_MAX returnt, ansonsten die Anzahl der umgewandelten Ze.
375 	xub_StrLen HexToBin( String& rToken );
376 
377 public:
378 
379 	virtual SvParserState CallParser(); // Aufruf des Parsers
380 
381 	inline const Color& GetColor( size_t nId ) const;
382 	const Font& GetFont( sal_uInt16 nId );		// aendert den dflt Font
383 
384 	virtual int IsEndPara( SvxNodeIdx* pNd, xub_StrLen nCnt ) const = 0;
385 
386 	// um einen anderen Attribut-Pool setzen zukoennen. Darf nur vorm
387 	// CallParser erfolgen!! Die Maps werden nicht neu erzeugt!
SetAttrPool(SfxItemPool * pNewPool)388 	void SetAttrPool( SfxItemPool* pNewPool )	{ pAttrPool = pNewPool; }
389 	// um andere WhichIds fuer einen anderen Pool zusetzen.
GetPardMap()390 	RTFPardAttrMapIds& GetPardMap()
391 						{ return (RTFPardAttrMapIds&)*aPardMap.GetData(); }
GetPlainMap()392 	RTFPlainAttrMapIds& GetPlainMap()
393 						{ return (RTFPlainAttrMapIds&)*aPlainMap.GetData(); }
394 	// um diese von aussen z.B. Tabellenzellen zuordnen zu koennen
395 	void ReadBorderAttr( int nToken, SfxItemSet& rSet, int bTableDef=sal_False );
396 	void ReadBackgroundAttr( int nToken, SfxItemSet& rSet, int bTableDef=sal_False	);
397 
398 	// fuers asynchrone lesen aus dem SvStream
399 	virtual void Continue( int nToken );
400 
401 	// get RTF default ItemSets. Must be used by pard/plain tokens or in
402 	// reset of Style-Items
403 	const SfxItemSet& GetRTFDefaults();
404 	virtual bool UncompressableStackEntry(const SvxRTFItemStackType &rSet) const;
405 };
406 
407 // der Stack fuer die Attribute:
408 // diese Klasse darf nur vom SvxRTFParser benutzt werden!
409 class EDITENG_DLLPUBLIC SvxRTFItemStackType
410 {
411 	friend class SvxRTFParser;
412 	friend class SvxRTFItemStackList;
413 
414 	SfxItemSet	aAttrSet;
415 	SvxNodeIdx	*pSttNd, *pEndNd;
416 	xub_StrLen nSttCnt, nEndCnt;
417 	SvxRTFItemStackList* pChildList;
418 	sal_uInt16 nStyleNo;
419 
420 	SvxRTFItemStackType( SfxItemPool&, const sal_uInt16* pWhichRange,
421 							const SvxPosition& );
422 	~SvxRTFItemStackType();
423 
424 	void Add( SvxRTFItemStackTypePtr );
425 	void Compress( const SvxRTFParser& );
426 
427 public:
428 	SvxRTFItemStackType( const SvxRTFItemStackType&, const SvxPosition&,
429 						int bCopyAttr = sal_False );
430 	//cmc, I'm very suspicios about SetStartPos, it doesn't change
431 	//its children's starting position, and the implementation looks
432 	//bad, consider this deprecated.
433 	void SetStartPos( const SvxPosition& rPos );
434 
435 	void MoveFullNode(const SvxNodeIdx &rOldNode,
436 		const SvxNodeIdx &rNewNode);
437 
GetSttNodeIdx() const438 	sal_uLong GetSttNodeIdx() const { return pSttNd->GetIdx(); }
GetEndNodeIdx() const439 	sal_uLong GetEndNodeIdx() const { return pEndNd->GetIdx(); }
440 
GetSttNode() const441 	const SvxNodeIdx& GetSttNode() const { return *pSttNd; }
GetEndNode() const442 	const SvxNodeIdx& GetEndNode() const { return *pEndNd; }
443 
GetSttCnt() const444 	xub_StrLen GetSttCnt() const { return nSttCnt; }
GetEndCnt() const445 	xub_StrLen GetEndCnt() const { return nEndCnt; }
446 
GetAttrSet()447 		  SfxItemSet& GetAttrSet()			{ return aAttrSet; }
GetAttrSet() const448 	const SfxItemSet& GetAttrSet() const	{ return aAttrSet; }
449 
StyleNo() const450 	sal_uInt16 StyleNo() const	{ return nStyleNo; }
451 
452 	void SetRTFDefaults( const SfxItemSet& rDefaults );
453 };
454 
455 
456 // ----------- Inline Implementations --------------
457 
GetColor(size_t nId) const458 inline const Color& SvxRTFParser::GetColor( size_t nId ) const
459 {
460 	ColorPtr pColor = (ColorPtr)pDfltColor;
461 	if( nId < aColorTbl.size() )
462 		pColor = aColorTbl[ nId ];
463 	return *pColor;
464 }
465 
GetAttrSet()466 inline SfxItemSet& SvxRTFParser::GetAttrSet()
467 {
468 	SvxRTFItemStackTypePtr pTmp;
469     if( bNewGroup || 0 == ( pTmp = aAttrStack.empty() ? 0 : aAttrStack.back()) )
470 		pTmp = _GetAttrSet();
471 	return pTmp->aAttrSet;
472 }
473 
474 
475 #endif
476 	//_SVXRTF_HXX
477 
478