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 #include "scitems.hxx"
21 #include <editeng/eeitem.hxx>
22 #include <editeng/editeng.hxx>
23 #include <editeng/fhgtitem.hxx>
24 #include <editeng/svxrtf.hxx>
25 #include <vcl/outdev.hxx>
26 #include <svtools/rtftoken.h>
28 #include "rtfparse.hxx"
30 #include "document.hxx"
31 #include "docpool.hxx"
33 #define SC_RTFTWIPTOL 10 // 10 Twips tolerance when determining columns
35 ScRTFParser::ScRTFParser( EditEngine
* pEditP
) :
38 pColTwips( new ScRTFColTwips
),
41 nStartAdjust( (sal_uLong
)~0 ),
45 // RTF default FontSize 12Pt
46 long nMM
= OutputDevice::LogicToLogic( 12, MAP_POINT
, MAP_100TH_MM
);
47 pPool
->SetPoolDefaultItem( SvxFontHeightItem( nMM
, 100, EE_CHAR_FONTHEIGHT
) );
48 // Free-flying pInsDefault
49 pInsDefault
= new ScRTFCellDefault( pPool
);
52 ScRTFParser::~ScRTFParser()
56 maDefaultList
.clear();
59 sal_uLong
ScRTFParser::Read( SvStream
& rStream
, const OUString
& rBaseURL
)
61 Link
<> aOldLink
= pEdit
->GetImportHdl();
62 pEdit
->SetImportHdl( LINK( this, ScRTFParser
, RTFImportHdl
) );
63 sal_uLong nErr
= pEdit
->Read( rStream
, rBaseURL
, EE_FORMAT_RTF
);
64 if ( nLastToken
== RTF_PAR
)
66 if ( !maList
.empty() )
68 ScEEParseEntry
* pE
= maList
.back();
69 if ( // Completely empty
70 ( ( pE
->aSel
.nStartPara
== pE
->aSel
.nEndPara
71 && pE
->aSel
.nStartPos
== pE
->aSel
.nEndPos
74 ( pE
->aSel
.nStartPara
+ 1 == pE
->aSel
.nEndPara
75 && pE
->aSel
.nStartPos
== pEdit
->GetTextLen( pE
->aSel
.nStartPara
)
76 && pE
->aSel
.nEndPos
== 0
80 { // Don't take over the last paragraph
86 pEdit
->SetImportHdl( aOldLink
);
90 void ScRTFParser::EntryEnd( ScEEParseEntry
* pE
, const ESelection
& aSel
)
92 // Paragraph -2 strips the attached empty paragraph
93 pE
->aSel
.nEndPara
= aSel
.nEndPara
- 2;
94 // Although it's called nEndPos, the last one is position + 1
95 pE
->aSel
.nEndPos
= pEdit
->GetTextLen( aSel
.nEndPara
- 1 );
98 inline void ScRTFParser::NextRow()
100 if ( nRowMax
< ++nRowCnt
)
104 bool ScRTFParser::SeekTwips( sal_uInt16 nTwips
, SCCOL
* pCol
)
106 ScRTFColTwips::const_iterator it
= pColTwips
->find( nTwips
);
107 bool bFound
= it
!= pColTwips
->end();
108 sal_uInt16 nPos
= it
- pColTwips
->begin();
109 *pCol
= static_cast<SCCOL
>(nPos
);
112 sal_uInt16 nCount
= pColTwips
->size();
116 // nCol is insertion position; the next one higher up is there (or not)
117 if ( nCol
< static_cast<SCCOL
>(nCount
) && (((*pColTwips
)[nCol
] - SC_RTFTWIPTOL
) <= nTwips
) )
119 // Not smaller than everything else? Then compare with the next lower one
120 else if ( nCol
!= 0 && (((*pColTwips
)[nCol
-1] + SC_RTFTWIPTOL
) >= nTwips
) )
128 void ScRTFParser::ColAdjust()
130 if ( nStartAdjust
!= (sal_uLong
)~0 )
134 for ( size_t i
= nStartAdjust
, nListSize
= maList
.size(); i
< nListSize
; ++ i
)
140 if ( pE
->nColOverlap
> 1 )
141 nCol
= nCol
+ pE
->nColOverlap
; // Merged cells with \clmrg
144 SeekTwips( pE
->nTwips
, &nCol
);
145 if ( ++nCol
<= pE
->nCol
)
146 nCol
= pE
->nCol
+ 1; // Moved cell X
147 pE
->nColOverlap
= nCol
- pE
->nCol
; // Merged cells without \clmrg
149 if ( nCol
> nColMax
)
152 nStartAdjust
= (sal_uLong
)~0;
157 IMPL_LINK( ScRTFParser
, RTFImportHdl
, ImportInfo
*, pInfo
)
159 switch ( pInfo
->eState
)
161 case RTFIMP_NEXTTOKEN
:
164 case RTFIMP_UNKNOWNATTR
:
169 SvxRTFParser
* pParser
= static_cast<SvxRTFParser
*>(pInfo
->pParser
);
170 pParser
->SetAttrPool( pPool
);
171 RTFPardAttrMapIds
& rMap
= pParser
->GetPardMap();
172 rMap
.nBrush
= ATTR_BACKGROUND
;
173 rMap
.nBox
= ATTR_BORDER
;
174 rMap
.nShadow
= ATTR_SHADOW
;
178 if ( pInfo
->aSelection
.nEndPos
)
179 { // If still text: create last paragraph
181 pInfo
->nToken
= RTF_PAR
;
182 // EditEngine did not attach an empty paragraph anymore
183 // which EntryEnd could strip
184 pInfo
->aSelection
.nEndPara
++;
190 case RTFIMP_INSERTTEXT
:
192 case RTFIMP_INSERTPARA
:
195 OSL_FAIL("unknown ImportInfo.eState");
201 // For RTF_INTBL or respectively at the start of the first RTF_CELL
202 // after RTF_CELLX if there was no RTF_INTBL
203 void ScRTFParser::NewCellRow( ImportInfo
* /*pInfo*/ )
208 // Not flush on the right? => new table
209 if ( nLastWidth
&& !maDefaultList
.empty() )
211 const ScRTFCellDefault
& rD
= maDefaultList
.back();
212 if (rD
.nTwips
!= nLastWidth
)
215 if ( !( SeekTwips( nLastWidth
, &n1
)
216 && SeekTwips( rD
.nTwips
, &n2
)
225 // Build up TwipCols only after nLastWidth comparison!
226 for ( size_t i
= 0, n
= maDefaultList
.size(); i
< n
; ++i
)
228 const ScRTFCellDefault
& rD
= maDefaultList
[i
];
230 if ( !SeekTwips(rD
.nTwips
, &nCol
) )
231 pColTwips
->insert( rD
.nTwips
);
235 pActDefault
= maDefaultList
.empty() ? NULL
: &maDefaultList
[0];
237 OSL_ENSURE( pActDefault
, "NewCellRow: pActDefault==0" );
244 \trowd \cellx \cellx ...
245 \intbl \cell \cell ...
248 [\trowd \cellx \cellx ...]
249 \intbl \cell \cell ...
256 \trowd \cellx \cellx ...
257 \intbl \cell \cell ...
260 [\trowd \cellx \cellx ...]
261 \intbl \cell \cell ...
267 void ScRTFParser::ProcToken( ImportInfo
* pInfo
)
269 switch ( pInfo
->nToken
)
271 case RTF_TROWD
: // denotes table row defauls, before RTF_CELLX
273 if (!maDefaultList
.empty())
274 nLastWidth
= maDefaultList
.back().nTwips
;
277 maDefaultList
.clear();
279 nLastToken
= pInfo
->nToken
;
283 case RTF_CLMGF
: // The first cell of cells to be merged
285 pDefMerge
= pInsDefault
;
286 nLastToken
= pInfo
->nToken
;
289 case RTF_CLMRG
: // A cell to be merged with the preceding cell
291 if (!pDefMerge
&& !maDefaultList
.empty())
293 pDefMerge
= &maDefaultList
.back();
294 mnCurPos
= maDefaultList
.size() - 1;
296 OSL_ENSURE( pDefMerge
, "RTF_CLMRG: pDefMerge==0" );
297 if ( pDefMerge
) // Else broken RTF
298 pDefMerge
->nColOverlap
++; // multiple successive ones possible
299 pInsDefault
->nColOverlap
= 0; // Flag: ignore these
300 nLastToken
= pInfo
->nToken
;
303 case RTF_CELLX
: // closes cell default
306 pInsDefault
->nCol
= nColCnt
;
307 pInsDefault
->nTwips
= pInfo
->nTokenValue
; // Right cell border
308 maDefaultList
.push_back( pInsDefault
);
309 // New free-flying pInsDefault
310 pInsDefault
= new ScRTFCellDefault( pPool
);
311 if ( ++nColCnt
> nColMax
)
313 nLastToken
= pInfo
->nToken
;
316 case RTF_INTBL
: // before the first RTF_CELL
318 // Once over NextToken and once over UnknownAttrToken
319 // or e.g. \intbl ... \cell \pard \intbl ... \cell
320 if ( nLastToken
!= RTF_INTBL
&& nLastToken
!= RTF_CELL
&& nLastToken
!= RTF_PAR
)
323 nLastToken
= pInfo
->nToken
;
327 case RTF_CELL
: // denotes the end of a cell.
329 OSL_ENSURE( pActDefault
, "RTF_CELL: pActDefault==0" );
330 if ( bNewDef
|| !pActDefault
)
331 NewCellRow( pInfo
); // davor war kein \intbl, bad behavior
332 // Broken RTF? Let's save what we can
334 pActDefault
= pInsDefault
;
335 if ( pActDefault
->nColOverlap
> 0 )
336 { // Not merged with preceding
337 pActEntry
->nCol
= pActDefault
->nCol
;
338 pActEntry
->nColOverlap
= pActDefault
->nColOverlap
;
339 pActEntry
->nTwips
= pActDefault
->nTwips
;
340 pActEntry
->nRow
= nRowCnt
;
341 pActEntry
->aItemSet
.Set( pActDefault
->aItemSet
);
342 EntryEnd( pActEntry
, pInfo
->aSelection
);
344 if ( nStartAdjust
== (sal_uLong
)~0 )
345 nStartAdjust
= maList
.size();
346 maList
.push_back( pActEntry
);
347 NewActEntry( pActEntry
); // New free-flying pActEntry
350 { // Assign current Twips to MergeCell
351 if ( !maList
.empty() )
353 ScEEParseEntry
* pE
= maList
.back();
354 pE
->nTwips
= pActDefault
->nTwips
;
356 // Adjust selection of free-flying pActEntry
357 // Paragraph -1 due to separated text in EditEngine during parsing
358 pActEntry
->aSel
.nStartPara
= pInfo
->aSelection
.nEndPara
- 1;
362 if (!maDefaultList
.empty() && (mnCurPos
+1) < maDefaultList
.size())
363 pActDefault
= &maDefaultList
[++mnCurPos
];
365 nLastToken
= pInfo
->nToken
;
368 case RTF_ROW
: // denotes the end of a row
371 nLastToken
= pInfo
->nToken
;
374 case RTF_PAR
: // Paragraph
377 { // text not in table
378 ColAdjust(); // close the processing table
380 pActEntry
->nRow
= nRowCnt
;
381 EntryEnd( pActEntry
, pInfo
->aSelection
);
382 maList
.push_back( pActEntry
);
383 NewActEntry( pActEntry
); // new pActEntry
386 nLastToken
= pInfo
->nToken
;
390 { // do not set nLastToken
391 switch ( pInfo
->nToken
& ~(0xff | RTF_TABLEDEF
) )
394 static_cast<SvxRTFParser
*>(pInfo
->pParser
)->ReadBackgroundAttr(
395 pInfo
->nToken
, pInsDefault
->aItemSet
, true );
398 static_cast<SvxRTFParser
*>(pInfo
->pParser
)->ReadBorderAttr(
399 pInfo
->nToken
, pInsDefault
->aItemSet
, true );
406 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */