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/.
10 #include "rtfdocumentimpl.hxx"
12 #include <com/sun/star/io/WrongFormatException.hpp>
13 #include <svl/lngmisc.hxx>
15 #include <ooxml/resourceids.hxx>
17 #include <sal/log.hxx>
19 #include "rtfreferenceproperties.hxx"
20 #include "rtfskipdestination.hxx"
22 using namespace com::sun::star
;
24 namespace writerfilter::rtftok
26 RTFError
RTFDocumentImpl::dispatchSymbol(RTFKeyword nKeyword
)
29 if (nKeyword
!= RTFKeyword::HEXCHAR
)
30 checkUnicode(/*bUnicode =*/true, /*bHex =*/true);
32 checkUnicode(/*bUnicode =*/true, /*bHex =*/false);
33 RTFSkipDestination
aSkip(*this);
35 if (RTFKeyword::LINE
== nKeyword
)
37 // very special handling since text() will eat lone '\n'
38 singleChar('\n', /*bRunProps=*/true);
48 case RTFKeyword::BACKSLASH
:
51 case RTFKeyword::LBRACE
:
54 case RTFKeyword::RBRACE
:
57 case RTFKeyword::EMDASH
:
60 case RTFKeyword::ENDASH
:
63 case RTFKeyword::BULLET
:
66 case RTFKeyword::LQUOTE
:
69 case RTFKeyword::RQUOTE
:
72 case RTFKeyword::LDBLQUOTE
:
75 case RTFKeyword::RDBLQUOTE
:
83 OUString
aStr(OStringToOUString(OStringChar(char(cCh
)), RTL_TEXTENCODING_MS_1252
));
90 case RTFKeyword::IGNORE
:
92 m_bSkipUnknown
= true;
93 aSkip
.setReset(false);
99 if (m_aStates
.top().getDestination() == Destination::FOOTNOTESEPARATOR
)
100 break; // just ignore it - only thing we read in here is CHFTNSEP
102 bool bNeedPap
= m_bNeedPap
;
106 if (!m_aStates
.top().getCurrentBuffer())
109 // Not in table? Reset max width.
112 // Was in table, but not anymore -> tblEnd.
113 RTFSprms aAttributes
;
115 aSprms
.set(NS_ooxml::LN_tblEnd
, new RTFValue(1));
116 writerfilter::Reference
<Properties
>::Pointer_t pProperties
117 = new RTFReferenceProperties(aAttributes
, aSprms
);
118 Mapper().props(pProperties
);
122 else if (m_aStates
.top().getDestination() != Destination::SHAPETEXT
)
124 RTFValue::Pointer_t pValue
;
125 m_aStates
.top().getCurrentBuffer()->push_back(Buf_t(BUFFER_PAR
, pValue
, nullptr));
127 // but don't emit properties yet, since they may change till the first text token arrives
129 if (!m_aStates
.top().getFrame().inFrame())
131 m_bNeedFinalPar
= false;
134 case RTFKeyword::SECT
:
137 dispatchSymbol(RTFKeyword::PAR
);
140 if (m_bIgnoreNextContSectBreak
)
141 m_bIgnoreNextContSectBreak
= false;
145 if (m_nResetBreakOnSectBreak
!= RTFKeyword::invalid
)
147 // this should run on _second_ \sect after \page
148 dispatchSymbol(m_nResetBreakOnSectBreak
); // lazy reset
149 m_nResetBreakOnSectBreak
= RTFKeyword::invalid
;
150 m_bNeedSect
= false; // dispatchSymbol set it
155 case RTFKeyword::NOBREAK
:
157 OUString
aStr(SVT_HARD_SPACE
);
161 case RTFKeyword::NOBRKHYPH
:
163 OUString
aStr(SVT_HARD_HYPHEN
);
167 case RTFKeyword::OPTHYPH
:
169 OUString
aStr(SVT_SOFT_HYPHEN
);
173 case RTFKeyword::HEXCHAR
:
174 m_aStates
.top().setInternalState(RTFInternalState::HEX
);
176 case RTFKeyword::CELL
:
177 case RTFKeyword::NESTCELL
:
179 if (nKeyword
== RTFKeyword::CELL
)
180 m_bAfterCellBeforeRow
= true;
185 // There were no runs in the cell, so we need to send paragraph and character properties here.
186 auto pPValue
= new RTFValue(m_aStates
.top().getParagraphAttributes(),
187 m_aStates
.top().getParagraphSprms());
188 bufferProperties(m_aTableBufferStack
.back(), pPValue
, nullptr);
189 auto pCValue
= new RTFValue(m_aStates
.top().getCharacterAttributes(),
190 m_aStates
.top().getCharacterSprms());
191 bufferProperties(m_aTableBufferStack
.back(), pCValue
, nullptr);
194 RTFValue::Pointer_t pValue
;
195 m_aTableBufferStack
.back().emplace_back(Buf_t(BUFFER_CELLEND
, pValue
, nullptr));
199 case RTFKeyword::NESTROW
:
201 tools::SvRef
<TableRowBuffer
> const pBuffer(
202 new TableRowBuffer(m_aTableBufferStack
.back(), m_aNestedTableCellsSprms
,
203 m_aNestedTableCellsAttributes
, m_nNestedCells
));
204 prepareProperties(m_aStates
.top(), pBuffer
->GetParaProperties(),
205 pBuffer
->GetFrameProperties(), pBuffer
->GetRowProperties(),
206 m_nNestedCells
, m_nNestedCurrentCellX
- m_nNestedTRLeft
);
208 if (m_aTableBufferStack
.size() == 1 || !m_aStates
.top().getCurrentBuffer())
210 throw io::WrongFormatException("mismatch between \\itap and number of \\nestrow",
213 assert(m_aStates
.top().getCurrentBuffer() == &m_aTableBufferStack
.back());
214 // note: there may be several states pointing to table buffer!
215 for (std::size_t i
= 0; i
< m_aStates
.size(); ++i
)
217 if (m_aStates
[i
].getCurrentBuffer() == &m_aTableBufferStack
.back())
219 m_aStates
[i
].setCurrentBuffer(
220 &m_aTableBufferStack
[m_aTableBufferStack
.size() - 2]);
223 m_aTableBufferStack
.pop_back();
224 m_aTableBufferStack
.back().emplace_back(
225 Buf_t(BUFFER_NESTROW
, RTFValue::Pointer_t(), pBuffer
));
227 m_aNestedTableCellsSprms
.clear();
228 m_aNestedTableCellsAttributes
.clear();
233 case RTFKeyword::ROW
:
235 m_bAfterCellBeforeRow
= false;
236 if (m_aStates
.top().getTableRowWidthAfter() > 0)
238 // Add fake cellx / cell, RTF equivalent of
239 // OOXMLFastContextHandlerTextTableRow::handleGridAfter().
240 auto pXValue
= new RTFValue(m_aStates
.top().getTableRowWidthAfter());
241 m_aStates
.top().getTableRowSprms().set(NS_ooxml::LN_CT_TblGridBase_gridCol
, pXValue
,
242 RTFOverwrite::NO_APPEND
);
243 dispatchSymbol(RTFKeyword::CELL
);
245 // Adjust total width, which is done in the \cellx handler for normal cells.
246 m_nTopLevelCurrentCellX
+= m_aStates
.top().getTableRowWidthAfter();
248 m_aStates
.top().setTableRowWidthAfter(0);
251 bool bRestored
= false;
252 // Ending a row, but no cells defined?
253 // See if there was an invalid table row reset, so we can restore cell infos to help invalid documents.
254 if (!m_nTopLevelCurrentCellX
&& m_nBackupTopLevelCurrentCellX
)
256 restoreTableRowProperties();
260 // If the right edge of the last cell (row width) is smaller than the width of some other row, mimic WW8TabDesc::CalcDefaults(): resize the last cell
261 const int MINLAY
= 23; // sw/inc/swtypes.hxx, minimal possible size of frames.
262 if ((m_nCellxMax
- m_nTopLevelCurrentCellX
) >= MINLAY
)
264 auto pXValueLast
= m_aStates
.top().getTableRowSprms().find(
265 NS_ooxml::LN_CT_TblGridBase_gridCol
, false);
266 const int nXValueLast
= pXValueLast
? pXValueLast
->getInt() : 0;
267 auto pXValue
= new RTFValue(nXValueLast
+ m_nCellxMax
- m_nTopLevelCurrentCellX
);
268 m_aStates
.top().getTableRowSprms().eraseLast(NS_ooxml::LN_CT_TblGridBase_gridCol
);
269 m_aStates
.top().getTableRowSprms().set(NS_ooxml::LN_CT_TblGridBase_gridCol
, pXValue
,
270 RTFOverwrite::NO_APPEND
);
271 m_nTopLevelCurrentCellX
= m_nCellxMax
;
274 if (m_nTopLevelCells
)
276 // Make a backup before we start popping elements
277 m_aTableInheritingCellsSprms
= m_aTopLevelTableCellsSprms
;
278 m_aTableInheritingCellsAttributes
= m_aTopLevelTableCellsAttributes
;
279 m_nInheritingCells
= m_nTopLevelCells
;
283 // No table definition? Then inherit from the previous row
284 m_aTopLevelTableCellsSprms
= m_aTableInheritingCellsSprms
;
285 m_aTopLevelTableCellsAttributes
= m_aTableInheritingCellsAttributes
;
286 m_nTopLevelCells
= m_nInheritingCells
;
289 while (m_aTableBufferStack
.size() > 1)
291 SAL_WARN("writerfilter.rtf", "dropping extra table buffer");
292 // note: there may be several states pointing to table buffer!
293 for (std::size_t i
= 0; i
< m_aStates
.size(); ++i
)
295 if (m_aStates
[i
].getCurrentBuffer() == &m_aTableBufferStack
.back())
297 m_aStates
[i
].setCurrentBuffer(&m_aTableBufferStack
.front());
300 m_aTableBufferStack
.pop_back();
303 replayRowBuffer(m_aTableBufferStack
.back(), m_aTopLevelTableCellsSprms
,
304 m_aTopLevelTableCellsAttributes
, m_nTopLevelCells
);
306 // The scope of the table cell defaults is one row.
307 m_aDefaultState
.getTableCellSprms().clear();
308 m_aStates
.top().getTableCellSprms() = m_aDefaultState
.getTableCellSprms();
309 m_aStates
.top().getTableCellAttributes() = m_aDefaultState
.getTableCellAttributes();
311 writerfilter::Reference
<Properties
>::Pointer_t paraProperties
;
312 writerfilter::Reference
<Properties
>::Pointer_t frameProperties
;
313 writerfilter::Reference
<Properties
>::Pointer_t rowProperties
;
314 prepareProperties(m_aStates
.top(), paraProperties
, frameProperties
, rowProperties
,
315 m_nTopLevelCells
, m_nTopLevelCurrentCellX
- m_nTopLevelTRLeft
);
316 sendProperties(paraProperties
, frameProperties
, rowProperties
);
319 m_bNeedFinalPar
= true;
320 m_aTableBufferStack
.back().clear();
321 m_nTopLevelCells
= 0;
324 // We restored cell definitions, clear these now.
325 // This is necessary, as later cell definitions want to overwrite the restored ones.
326 resetTableRowProperties();
329 case RTFKeyword::COLUMN
:
331 bool bColumns
= false; // If we have multiple columns
332 RTFValue::Pointer_t pCols
333 = m_aStates
.top().getSectionSprms().find(NS_ooxml::LN_EG_SectPrContents_cols
);
336 RTFValue::Pointer_t pNum
= pCols
->getAttributes().find(NS_ooxml::LN_CT_Columns_num
);
337 if (pNum
&& pNum
->getInt() > 1)
343 sal_uInt8
const sBreak
[] = { 0xe };
344 Mapper().startCharacterGroup();
345 Mapper().text(sBreak
, 1);
346 Mapper().endCharacterGroup();
349 dispatchSymbol(RTFKeyword::PAGE
);
352 case RTFKeyword::CHFTN
:
354 if (m_aStates
.top().getCurrentBuffer() == &m_aSuperBuffer
)
355 // Stop buffering, there will be no custom mark for this footnote or endnote.
356 m_aStates
.top().setCurrentBuffer(nullptr);
359 case RTFKeyword::PAGE
:
361 // Ignore page breaks inside tables.
362 if (m_aStates
.top().getCurrentBuffer() == &m_aTableBufferStack
.back())
365 // If we're inside a continuous section, we should send a section break, not a page one.
366 RTFValue::Pointer_t pBreak
367 = m_aStates
.top().getSectionSprms().find(NS_ooxml::LN_EG_SectPrContents_type
);
368 // Unless we're on a title page.
369 RTFValue::Pointer_t pTitlePg
370 = m_aStates
.top().getSectionSprms().find(NS_ooxml::LN_EG_SectPrContents_titlePg
);
373 == static_cast<sal_Int32
>(NS_ooxml::LN_Value_ST_SectionMark_continuous
))
374 || m_nResetBreakOnSectBreak
== RTFKeyword::SBKNONE
)
375 && !(pTitlePg
&& pTitlePg
->getInt()))
379 dispatchSymbol(RTFKeyword::PAR
);
380 m_bWasInFrame
= false;
383 // note: this will not affect the following section break
384 // but the one just pushed
385 dispatchFlag(RTFKeyword::SBKPAGE
);
387 dispatchSymbol(RTFKeyword::PAR
);
388 m_bIgnoreNextContSectBreak
= true;
389 // arrange to clean up the synthetic RTFKeyword::SBKPAGE
390 m_nResetBreakOnSectBreak
= RTFKeyword::SBKNONE
;
396 sal_uInt8
const sBreak
[] = { 0xc };
397 Mapper().text(sBreak
, 1);
407 case RTFKeyword::CHPGN
:
409 OUString
aStr("PAGE");
410 singleChar(cFieldStart
);
412 singleChar(cFieldSep
, true);
413 singleChar(cFieldEnd
);
416 case RTFKeyword::CHFTNSEP
:
418 static const sal_Unicode uFtnEdnSep
= 0x3;
419 Mapper().utext(reinterpret_cast<const sal_uInt8
*>(&uFtnEdnSep
), 1);
424 SAL_INFO("writerfilter.rtf",
425 "TODO handle symbol '" << keywordToString(nKeyword
) << "'");
426 aSkip
.setParsed(false);
433 } // namespace writerfilter::rtftok
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */