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 .
21 #include <scitems.hxx>
22 #include <editeng/eeitem.hxx>
23 #include <editeng/editeng.hxx>
24 #include <editeng/fhgtitem.hxx>
25 #include <editeng/svxrtf.hxx>
26 #include <vcl/outdev.hxx>
27 #include <svtools/rtftoken.h>
28 #include <osl/diagnose.h>
29 #include <svl/itempool.hxx>
31 #include <rtfparse.hxx>
33 #define SC_RTFTWIPTOL 10 // 10 Twips tolerance when determining columns
35 ScRTFParser::ScRTFParser( EditEngine
* pEditP
) :
38 pActDefault( nullptr ),
40 nStartAdjust( sal_uLong(~0) ),
44 // RTF default FontSize 12Pt
45 tools::Long nMM
= OutputDevice::LogicToLogic( 12, MapUnit::MapPoint
, MapUnit::Map100thMM
);
46 pPool
->SetPoolDefaultItem( SvxFontHeightItem( nMM
, 100, EE_CHAR_FONTHEIGHT
) );
47 // Free-flying pInsDefault
48 pInsDefault
.reset( new ScRTFCellDefault( pPool
) );
51 ScRTFParser::~ScRTFParser()
54 maDefaultList
.clear();
57 ErrCode
ScRTFParser::Read( SvStream
& rStream
, const OUString
& rBaseURL
)
59 Link
<RtfImportInfo
&,void> aOldLink
= pEdit
->GetRtfImportHdl();
60 pEdit
->SetRtfImportHdl( LINK( this, ScRTFParser
, RTFImportHdl
) );
61 ErrCode nErr
= pEdit
->Read( rStream
, rBaseURL
, EETextFormat::Rtf
);
62 if ( nRtfLastToken
== RTF_PAR
)
64 if ( !maList
.empty() )
66 auto& pE
= maList
.back();
67 if ( // Completely empty
68 ( pE
->aSel
.nStartPara
== pE
->aSel
.nEndPara
69 && pE
->aSel
.nStartPos
== pE
->aSel
.nEndPos
72 ( pE
->aSel
.nStartPara
+ 1 == pE
->aSel
.nEndPara
73 && pE
->aSel
.nStartPos
== pEdit
->GetTextLen( pE
->aSel
.nStartPara
)
74 && pE
->aSel
.nEndPos
== 0
77 { // Don't take over the last paragraph
83 pEdit
->SetRtfImportHdl( aOldLink
);
87 void ScRTFParser::EntryEnd( ScEEParseEntry
* pE
, const ESelection
& aSel
)
89 // Paragraph -2 strips the attached empty paragraph
90 pE
->aSel
.nEndPara
= aSel
.nEndPara
- 2;
91 // Although it's called nEndPos, the last one is position + 1
92 pE
->aSel
.nEndPos
= pEdit
->GetTextLen( aSel
.nEndPara
- 1 );
95 inline void ScRTFParser::NextRow()
97 if ( nRowMax
< ++nRowCnt
)
101 bool ScRTFParser::SeekTwips( sal_uInt16 nTwips
, SCCOL
* pCol
)
103 ScRTFColTwips::const_iterator it
= aColTwips
.find( nTwips
);
104 bool bFound
= it
!= aColTwips
.end();
105 sal_uInt16 nPos
= it
- aColTwips
.begin();
106 *pCol
= static_cast<SCCOL
>(nPos
);
109 sal_uInt16 nCount
= aColTwips
.size();
113 // nCol is insertion position; the next one higher up is there (or not)
114 if ( nCol
< static_cast<SCCOL
>(nCount
) && ((aColTwips
[nCol
] - SC_RTFTWIPTOL
) <= nTwips
) )
116 // Not smaller than everything else? Then compare with the next lower one
117 else if ( nCol
!= 0 && ((aColTwips
[nCol
-1] + SC_RTFTWIPTOL
) >= nTwips
) )
125 void ScRTFParser::ColAdjust()
127 if ( nStartAdjust
== sal_uLong(~0) )
131 for (size_t i
= nStartAdjust
, nListSize
= maList
.size(); i
< nListSize
; ++i
)
133 auto& pE
= maList
[i
];
137 if ( pE
->nColOverlap
> 1 )
138 nCol
= nCol
+ pE
->nColOverlap
; // Merged cells with \clmrg
141 SeekTwips( pE
->nTwips
, &nCol
);
142 if ( ++nCol
<= pE
->nCol
)
143 nCol
= pE
->nCol
+ 1; // Moved cell X
144 pE
->nColOverlap
= nCol
- pE
->nCol
; // Merged cells without \clmrg
146 if ( nCol
> nColMax
)
149 nStartAdjust
= sal_uLong(~0);
153 IMPL_LINK( ScRTFParser
, RTFImportHdl
, RtfImportInfo
&, rInfo
, void )
155 switch ( rInfo
.eState
)
157 case RtfImportState::NextToken
:
160 case RtfImportState::UnknownAttr
:
163 case RtfImportState::Start
:
165 SvxRTFParser
* pParser
= static_cast<SvxRTFParser
*>(rInfo
.pParser
);
166 pParser
->SetAttrPool( pPool
);
167 RTFPardAttrMapIds
& rMap
= pParser
->GetPardMap();
168 rMap
.nBrush
= ATTR_BACKGROUND
;
169 rMap
.nBox
= ATTR_BORDER
;
170 rMap
.nShadow
= ATTR_SHADOW
;
173 case RtfImportState::End
:
174 if ( rInfo
.aSelection
.nEndPos
)
175 { // If still text: create last paragraph
176 pActDefault
= nullptr;
177 rInfo
.nToken
= RTF_PAR
;
178 // EditEngine did not attach an empty paragraph anymore
179 // which EntryEnd could strip
180 rInfo
.aSelection
.nEndPara
++;
184 case RtfImportState::SetAttr
:
186 case RtfImportState::InsertText
:
188 case RtfImportState::InsertPara
:
191 OSL_FAIL("unknown ImportInfo.eState");
196 // For RTF_INTBL or respectively at the start of the first RTF_CELL
197 // after RTF_CELLX if there was no RTF_INTBL
198 void ScRTFParser::NewCellRow()
203 // Not flush on the right? => new table
204 if ( nLastWidth
&& !maDefaultList
.empty() )
206 const ScRTFCellDefault
& rD
= *maDefaultList
.back();
207 if (rD
.nTwips
!= nLastWidth
)
210 if ( !( SeekTwips( nLastWidth
, &n1
)
211 && SeekTwips( rD
.nTwips
, &n2
)
220 // Build up TwipCols only after nLastWidth comparison!
221 for (const std::unique_ptr
<ScRTFCellDefault
> & pCellDefault
: maDefaultList
)
223 const ScRTFCellDefault
& rD
= *pCellDefault
;
225 if ( !SeekTwips(rD
.nTwips
, &nCol
) )
226 aColTwips
.insert( rD
.nTwips
);
230 pActDefault
= maDefaultList
.empty() ? nullptr : maDefaultList
[0].get();
232 OSL_ENSURE( pActDefault
, "NewCellRow: pActDefault==0" );
239 \trowd \cellx \cellx ...
240 \intbl \cell \cell ...
243 [\trowd \cellx \cellx ...]
244 \intbl \cell \cell ...
251 \trowd \cellx \cellx ...
252 \intbl \cell \cell ...
255 [\trowd \cellx \cellx ...]
256 \intbl \cell \cell ...
262 void ScRTFParser::ProcToken( RtfImportInfo
* pInfo
)
264 switch ( pInfo
->nToken
)
266 case RTF_TROWD
: // denotes table row default, before RTF_CELLX
268 if (!maDefaultList
.empty())
269 nLastWidth
= maDefaultList
.back()->nTwips
;
272 if (pActDefault
!= pInsDefault
.get())
273 pActDefault
= nullptr;
274 maDefaultList
.clear();
276 nRtfLastToken
= pInfo
->nToken
;
280 case RTF_CLMGF
: // The first cell of cells to be merged
282 pDefMerge
= pInsDefault
.get();
283 nRtfLastToken
= pInfo
->nToken
;
286 case RTF_CLMRG
: // A cell to be merged with the preceding cell
288 if (!pDefMerge
&& !maDefaultList
.empty())
290 pDefMerge
= maDefaultList
.back().get();
291 mnCurPos
= maDefaultList
.size() - 1;
293 OSL_ENSURE( pDefMerge
, "RTF_CLMRG: pDefMerge==0" );
294 if ( pDefMerge
) // Else broken RTF
295 pDefMerge
->nColOverlap
++; // multiple successive ones possible
296 pInsDefault
->nColOverlap
= 0; // Flag: ignore these
297 nRtfLastToken
= pInfo
->nToken
;
300 case RTF_CELLX
: // closes cell default
303 pInsDefault
->nCol
= nColCnt
;
304 pInsDefault
->nTwips
= pInfo
->nTokenValue
; // Right cell border
305 maDefaultList
.push_back( std::move(pInsDefault
) );
306 // New free-flying pInsDefault
307 pInsDefault
.reset( new ScRTFCellDefault( pPool
) );
308 if ( ++nColCnt
> nColMax
)
310 nRtfLastToken
= pInfo
->nToken
;
313 case RTF_INTBL
: // before the first RTF_CELL
315 // Once over NextToken and once over UnknownAttrToken
316 // or e.g. \intbl ... \cell \pard \intbl ... \cell
317 if ( nRtfLastToken
!= RTF_INTBL
&& nRtfLastToken
!= RTF_CELL
&& nRtfLastToken
!= RTF_PAR
)
320 nRtfLastToken
= pInfo
->nToken
;
324 case RTF_CELL
: // denotes the end of a cell.
326 OSL_ENSURE( pActDefault
, "RTF_CELL: pActDefault==0" );
327 if ( bNewDef
|| !pActDefault
)
328 NewCellRow(); // before was no \intbl, bad behavior
329 // Broken RTF? Let's save what we can
331 pActDefault
= pInsDefault
.get();
332 if ( pActDefault
->nColOverlap
> 0 )
333 { // Not merged with preceding
334 mxActEntry
->nCol
= pActDefault
->nCol
;
335 mxActEntry
->nColOverlap
= pActDefault
->nColOverlap
;
336 mxActEntry
->nTwips
= pActDefault
->nTwips
;
337 mxActEntry
->nRow
= nRowCnt
;
338 mxActEntry
->aItemSet
.Set(pActDefault
->aItemSet
);
339 EntryEnd(mxActEntry
.get(), pInfo
->aSelection
);
341 if ( nStartAdjust
== sal_uLong(~0) )
342 nStartAdjust
= maList
.size();
343 maList
.push_back(mxActEntry
);
344 NewActEntry(mxActEntry
.get()); // New free-flying mxActEntry
347 { // Assign current Twips to MergeCell
348 if ( !maList
.empty() )
350 auto& pE
= maList
.back();
351 pE
->nTwips
= pActDefault
->nTwips
;
353 // Adjust selection of free-flying mxActEntry
354 // Paragraph -1 due to separated text in EditEngine during parsing
355 mxActEntry
->aSel
.nStartPara
= pInfo
->aSelection
.nEndPara
- 1;
358 pActDefault
= nullptr;
359 if (!maDefaultList
.empty() && (mnCurPos
+1) < maDefaultList
.size())
360 pActDefault
= maDefaultList
[++mnCurPos
].get();
362 nRtfLastToken
= pInfo
->nToken
;
365 case RTF_ROW
: // denotes the end of a row
368 nRtfLastToken
= pInfo
->nToken
;
371 case RTF_PAR
: // Paragraph
374 { // text not in table
375 ColAdjust(); // close the processing table
376 mxActEntry
->nCol
= 0;
377 mxActEntry
->nRow
= nRowCnt
;
378 EntryEnd(mxActEntry
.get(), pInfo
->aSelection
);
379 maList
.push_back(mxActEntry
);
380 NewActEntry(mxActEntry
.get()); // new mxActEntry
383 nRtfLastToken
= pInfo
->nToken
;
387 { // do not set nRtfLastToken
388 switch ( pInfo
->nToken
& ~(0xff | RTF_TABLEDEF
) )
391 static_cast<SvxRTFParser
*>(pInfo
->pParser
)->ReadBackgroundAttr(
392 pInfo
->nToken
, pInsDefault
->aItemSet
, true );
395 static_cast<SvxRTFParser
*>(pInfo
->pParser
)->ReadBorderAttr(
396 pInfo
->nToken
, pInsDefault
->aItemSet
, true );
403 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */