1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
33 //------------------------------------------------------------------------
35 #include "scitems.hxx"
36 #include <editeng/eeitem.hxx>
39 #include <editeng/editeng.hxx>
40 #include <editeng/fhgtitem.hxx>
41 #include <editeng/svxrtf.hxx>
42 #include <vcl/outdev.hxx>
43 #include <svtools/rtftoken.h>
45 #define SC_RTFPARSE_CXX
46 #include "rtfparse.hxx"
48 #include "document.hxx"
49 #include "docpool.hxx"
51 #define SC_RTFTWIPTOL 10 // 10 Twips Toleranz bei Spaltenbestimmung
55 SV_IMPL_VARARR_SORT( ScRTFColTwips
, ULONG
);
59 ScRTFParser::ScRTFParser( EditEngine
* pEditP
) :
61 pDefaultList( new ScRTFDefaultList
),
62 pColTwips( new ScRTFColTwips
),
65 nStartAdjust( (ULONG
)~0 ),
69 // RTF default FontSize 12Pt
70 long nMM
= OutputDevice::LogicToLogic( 12, MAP_POINT
, MAP_100TH_MM
);
71 pPool
->SetPoolDefaultItem( SvxFontHeightItem( nMM
, 100, EE_CHAR_FONTHEIGHT
) );
72 // freifliegender pInsDefault
73 pInsDefault
= new ScRTFCellDefault( pPool
);
77 ScRTFParser::~ScRTFParser()
81 for ( ScRTFCellDefault
* pD
= pDefaultList
->First(); pD
; pD
= pDefaultList
->Next() )
87 ULONG
ScRTFParser::Read( SvStream
& rStream
, const String
& rBaseURL
)
89 Link aOldLink
= pEdit
->GetImportHdl();
90 pEdit
->SetImportHdl( LINK( this, ScRTFParser
, RTFImportHdl
) );
91 ULONG nErr
= pEdit
->Read( rStream
, rBaseURL
, EE_FORMAT_RTF
);
92 if ( nLastToken
== RTF_PAR
)
94 ScEEParseEntry
* pE
= pList
->Last();
97 && (( pE
->aSel
.nStartPara
== pE
->aSel
.nEndPara
98 && pE
->aSel
.nStartPos
== pE
->aSel
.nEndPos
)
100 || ( pE
->aSel
.nStartPara
+ 1 == pE
->aSel
.nEndPara
101 && pE
->aSel
.nStartPos
== pEdit
->GetTextLen( pE
->aSel
.nStartPara
)
102 && pE
->aSel
.nEndPos
== 0 )) )
103 { // den letzten leeren Absatz nicht uebernehmen
109 pEdit
->SetImportHdl( aOldLink
);
114 void ScRTFParser::EntryEnd( ScEEParseEntry
* pE
, const ESelection
& aSel
)
116 // Paragraph -2 stript den angehaengten leeren Paragraph
117 pE
->aSel
.nEndPara
= aSel
.nEndPara
- 2;
118 // obwohl das nEndPos heisst, ist das letzte Position + 1
119 pE
->aSel
.nEndPos
= pEdit
->GetTextLen( aSel
.nEndPara
- 1 );
123 inline void ScRTFParser::NextRow()
125 if ( nRowMax
< ++nRowCnt
)
130 BOOL
ScRTFParser::SeekTwips( USHORT nTwips
, SCCOL
* pCol
)
133 BOOL bFound
= pColTwips
->Seek_Entry( nTwips
, &nPos
);
134 *pCol
= static_cast<SCCOL
>(nPos
);
137 USHORT nCount
= pColTwips
->Count();
141 // nCol ist Einfuegeposition, da liegt der Naechsthoehere (oder auch nicht)
142 if ( nCol
< static_cast<SCCOL
>(nCount
) && (((*pColTwips
)[nCol
] - SC_RTFTWIPTOL
) <= nTwips
) )
144 // nicht kleiner als alles andere? dann mit Naechstniedrigerem vergleichen
145 else if ( nCol
!= 0 && (((*pColTwips
)[nCol
-1] + SC_RTFTWIPTOL
) >= nTwips
) )
154 void ScRTFParser::ColAdjust()
156 if ( nStartAdjust
!= (ULONG
)~0 )
160 pE
= pList
->Seek( nStartAdjust
);
166 if ( pE
->nColOverlap
> 1 )
167 nCol
= nCol
+ pE
->nColOverlap
; // merged cells mit \clmrg
170 SeekTwips( pE
->nTwips
, &nCol
);
171 if ( ++nCol
<= pE
->nCol
)
172 nCol
= pE
->nCol
+ 1; // verschobene Zell-X
173 pE
->nColOverlap
= nCol
- pE
->nCol
; // merged cells ohne \clmrg
175 if ( nCol
> nColMax
)
179 nStartAdjust
= (ULONG
)~0;
180 pColTwips
->Remove( (USHORT
)0, pColTwips
->Count() );
185 IMPL_LINK( ScRTFParser
, RTFImportHdl
, ImportInfo
*, pInfo
)
187 switch ( pInfo
->eState
)
189 case RTFIMP_NEXTTOKEN
:
192 case RTFIMP_UNKNOWNATTR
:
197 SvxRTFParser
* pParser
= (SvxRTFParser
*) pInfo
->pParser
;
198 pParser
->SetAttrPool( pPool
);
199 RTFPardAttrMapIds
& rMap
= pParser
->GetPardMap();
200 rMap
.nBrush
= ATTR_BACKGROUND
;
201 rMap
.nBox
= ATTR_BORDER
;
202 rMap
.nShadow
= ATTR_SHADOW
;
206 if ( pInfo
->aSelection
.nEndPos
)
207 { // falls noch Text: letzten Absatz erzeugen
209 pInfo
->nToken
= RTF_PAR
;
210 // EditEngine hat keinen leeren Paragraph mehr angehaengt
211 // den EntryEnd strippen koennte
212 pInfo
->aSelection
.nEndPara
++;
218 case RTFIMP_INSERTTEXT
:
220 case RTFIMP_INSERTPARA
:
223 DBG_ERRORFILE("unknown ImportInfo.eState");
229 // bei RTF_INTBL bzw. am Anfang von erstem RTF_CELL nach RTF_CELLX wenn es
230 // kein RTF_INTBL gab, bad behavior
231 void ScRTFParser::NewCellRow( ImportInfo
* /*pInfo*/ )
235 ScRTFCellDefault
* pD
;
237 // rechts nicht buendig? => neue Tabelle
239 && ((pD
= pDefaultList
->Last()) != 0) && pD
->nTwips
!= nLastWidth
)
242 if ( !( SeekTwips( nLastWidth
, &n1
)
243 && SeekTwips( pD
->nTwips
, &n2
) && n1
== n2
) )
246 // TwipCols aufbauen, erst nach nLastWidth Vergleich!
247 for ( pD
= pDefaultList
->First(); pD
; pD
= pDefaultList
->Next() )
250 if ( !SeekTwips( pD
->nTwips
, &n
) )
251 pColTwips
->Insert( pD
->nTwips
);
255 pActDefault
= pDefaultList
->First();
256 DBG_ASSERT( pActDefault
, "NewCellRow: pActDefault==0" );
264 \trowd \cellx \cellx ...
265 \intbl \cell \cell ...
268 [\trowd \cellx \cellx ...]
269 \intbl \cell \cell ...
276 \trowd \cellx \cellx ...
277 \intbl \cell \cell ...
280 [\trowd \cellx \cellx ...]
281 \intbl \cell \cell ...
287 void ScRTFParser::ProcToken( ImportInfo
* pInfo
)
289 ScRTFCellDefault
* pD
;
291 switch ( pInfo
->nToken
)
293 case RTF_TROWD
: // denotes table row defauls, before RTF_CELLX
295 if ( (pD
= pDefaultList
->Last()) != 0 )
296 nLastWidth
= pD
->nTwips
;
298 for ( pD
= pDefaultList
->First(); pD
; pD
= pDefaultList
->Next() )
300 pDefaultList
->Clear();
302 nLastToken
= pInfo
->nToken
;
305 case RTF_CLMGF
: // The first cell of cells to be merged
307 pDefMerge
= pInsDefault
;
308 nLastToken
= pInfo
->nToken
;
311 case RTF_CLMRG
: // A cell to be merged with the preceding cell
314 pDefMerge
= pDefaultList
->Last();
315 DBG_ASSERT( pDefMerge
, "RTF_CLMRG: pDefMerge==0" );
316 if ( pDefMerge
) // sonst rottes RTF
317 pDefMerge
->nColOverlap
++; // mehrere nacheinander moeglich
318 pInsDefault
->nColOverlap
= 0; // Flag: ignoriere diese
319 nLastToken
= pInfo
->nToken
;
322 case RTF_CELLX
: // closes cell default
325 pInsDefault
->nCol
= nColCnt
;
326 pInsDefault
->nTwips
= pInfo
->nTokenValue
; // rechter Zellenrand
327 pDefaultList
->Insert( pInsDefault
, LIST_APPEND
);
328 // neuer freifliegender pInsDefault
329 pInsDefault
= new ScRTFCellDefault( pPool
);
330 if ( ++nColCnt
> nColMax
)
332 nLastToken
= pInfo
->nToken
;
335 case RTF_INTBL
: // before the first RTF_CELL
337 // einmal ueber NextToken und einmal ueber UnknownAttrToken
338 // oder z.B. \intbl ... \cell \pard \intbl ... \cell
339 if ( nLastToken
!= RTF_INTBL
&& nLastToken
!= RTF_CELL
&& nLastToken
!= RTF_PAR
)
342 nLastToken
= pInfo
->nToken
;
346 case RTF_CELL
: // denotes the end of a cell.
348 DBG_ASSERT( pActDefault
, "RTF_CELL: pActDefault==0" );
349 if ( bNewDef
|| !pActDefault
)
350 NewCellRow( pInfo
); // davor war kein \intbl, bad behavior
351 // rottes RTF? retten was zu retten ist
353 pActDefault
= pInsDefault
;
354 if ( pActDefault
->nColOverlap
> 0 )
355 { // nicht merged mit vorheriger
356 pActEntry
->nCol
= pActDefault
->nCol
;
357 pActEntry
->nColOverlap
= pActDefault
->nColOverlap
;
358 pActEntry
->nTwips
= pActDefault
->nTwips
;
359 pActEntry
->nRow
= nRowCnt
;
360 pActEntry
->aItemSet
.Set( pActDefault
->aItemSet
);
361 EntryEnd( pActEntry
, pInfo
->aSelection
);
363 if ( nStartAdjust
== (ULONG
)~0 )
364 nStartAdjust
= pList
->Count();
365 pList
->Insert( pActEntry
, LIST_APPEND
);
366 NewActEntry( pActEntry
); // neuer freifliegender pActEntry
369 { // aktuelle Twips der MergeCell zuweisen
370 if ( (pE
= pList
->Last()) != 0 )
371 pE
->nTwips
= pActDefault
->nTwips
;
372 // Selection des freifliegenden pActEntry anpassen
373 // Paragraph -1 wg. Textaufbruch in EditEngine waehrend Parse
374 pActEntry
->aSel
.nStartPara
= pInfo
->aSelection
.nEndPara
- 1;
376 pActDefault
= pDefaultList
->Next();
377 nLastToken
= pInfo
->nToken
;
380 case RTF_ROW
: // means the end of a row
383 nLastToken
= pInfo
->nToken
;
386 case RTF_PAR
: // Paragraph
389 { // text not in table
390 ColAdjust(); // close the processing table
392 pActEntry
->nRow
= nRowCnt
;
393 EntryEnd( pActEntry
, pInfo
->aSelection
);
394 pList
->Insert( pActEntry
, LIST_APPEND
);
395 NewActEntry( pActEntry
); // new pActEntry
398 nLastToken
= pInfo
->nToken
;
402 { // do not set nLastToken
403 switch ( pInfo
->nToken
& ~(0xff | RTF_TABLEDEF
) )
406 ((SvxRTFParser
*)pInfo
->pParser
)->ReadBackgroundAttr(
407 pInfo
->nToken
, pInsDefault
->aItemSet
, TRUE
);
410 ((SvxRTFParser
*)pInfo
->pParser
)->ReadBorderAttr(
411 pInfo
->nToken
, pInsDefault
->aItemSet
, TRUE
);