LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / writerfilter / source / rtftok / rtfdispatchsymbol.cxx
blob9167c5479317315a91aaa4bb17c11b902cf375c8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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)
28 setNeedSect(true);
29 if (nKeyword != RTFKeyword::HEXCHAR)
30 checkUnicode(/*bUnicode =*/true, /*bHex =*/true);
31 else
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);
39 return RTFError::OK;
41 // Trivial symbols
42 sal_uInt8 cCh = 0;
43 switch (nKeyword)
45 case RTFKeyword::TAB:
46 cCh = '\t';
47 break;
48 case RTFKeyword::BACKSLASH:
49 cCh = '\\';
50 break;
51 case RTFKeyword::LBRACE:
52 cCh = '{';
53 break;
54 case RTFKeyword::RBRACE:
55 cCh = '}';
56 break;
57 case RTFKeyword::EMDASH:
58 cCh = 151;
59 break;
60 case RTFKeyword::ENDASH:
61 cCh = 150;
62 break;
63 case RTFKeyword::BULLET:
64 cCh = 149;
65 break;
66 case RTFKeyword::LQUOTE:
67 cCh = 145;
68 break;
69 case RTFKeyword::RQUOTE:
70 cCh = 146;
71 break;
72 case RTFKeyword::LDBLQUOTE:
73 cCh = 147;
74 break;
75 case RTFKeyword::RDBLQUOTE:
76 cCh = 148;
77 break;
78 default:
79 break;
81 if (cCh > 0)
83 OUString aStr(OStringToOUString(OStringChar(char(cCh)), RTL_TEXTENCODING_MS_1252));
84 text(aStr);
85 return RTFError::OK;
88 switch (nKeyword)
90 case RTFKeyword::IGNORE:
92 m_bSkipUnknown = true;
93 aSkip.setReset(false);
94 return RTFError::OK;
96 break;
97 case RTFKeyword::PAR:
99 if (m_aStates.top().getDestination() == Destination::FOOTNOTESEPARATOR)
100 break; // just ignore it - only thing we read in here is CHFTNSEP
101 checkFirstRun();
102 bool bNeedPap = m_bNeedPap;
103 checkNeedPap();
104 if (bNeedPap)
105 runProps();
106 if (!m_aStates.top().getCurrentBuffer())
108 parBreak();
109 // Not in table? Reset max width.
110 if (m_nCellxMax)
112 // Was in table, but not anymore -> tblEnd.
113 RTFSprms aAttributes;
114 RTFSprms aSprms;
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);
120 m_nCellxMax = 0;
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
128 m_bNeedPap = true;
129 if (!m_aStates.top().getFrame().inFrame())
130 m_bNeedPar = false;
131 m_bNeedFinalPar = false;
133 break;
134 case RTFKeyword::SECT:
136 if (m_bNeedCr)
137 dispatchSymbol(RTFKeyword::PAR);
139 m_bHadSect = true;
140 if (m_bIgnoreNextContSectBreak)
141 m_bIgnoreNextContSectBreak = false;
142 else
144 sectBreak();
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
154 break;
155 case RTFKeyword::NOBREAK:
157 OUString aStr(SVT_HARD_SPACE);
158 text(aStr);
160 break;
161 case RTFKeyword::NOBRKHYPH:
163 OUString aStr(SVT_HARD_HYPHEN);
164 text(aStr);
166 break;
167 case RTFKeyword::OPTHYPH:
169 OUString aStr(SVT_SOFT_HYPHEN);
170 text(aStr);
172 break;
173 case RTFKeyword::HEXCHAR:
174 m_aStates.top().setInternalState(RTFInternalState::HEX);
175 break;
176 case RTFKeyword::CELL:
177 case RTFKeyword::NESTCELL:
179 if (nKeyword == RTFKeyword::CELL)
180 m_bAfterCellBeforeRow = true;
182 checkFirstRun();
183 if (m_bNeedPap)
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));
196 m_bNeedPap = true;
198 break;
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",
211 nullptr);
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();
229 m_nNestedCells = 0;
230 m_bNeedPap = true;
232 break;
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();
257 bRestored = true;
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;
281 else
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);
318 m_bNeedPap = true;
319 m_bNeedFinalPar = true;
320 m_aTableBufferStack.back().clear();
321 m_nTopLevelCells = 0;
323 if (bRestored)
324 // We restored cell definitions, clear these now.
325 // This is necessary, as later cell definitions want to overwrite the restored ones.
326 resetTableRowProperties();
328 break;
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);
334 if (pCols)
336 RTFValue::Pointer_t pNum = pCols->getAttributes().find(NS_ooxml::LN_CT_Columns_num);
337 if (pNum && pNum->getInt() > 1)
338 bColumns = true;
340 checkFirstRun();
341 if (bColumns)
343 sal_uInt8 const sBreak[] = { 0xe };
344 Mapper().startCharacterGroup();
345 Mapper().text(sBreak, 1);
346 Mapper().endCharacterGroup();
348 else
349 dispatchSymbol(RTFKeyword::PAGE);
351 break;
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);
357 break;
359 case RTFKeyword::PAGE:
361 // Ignore page breaks inside tables.
362 if (m_aStates.top().getCurrentBuffer() == &m_aTableBufferStack.back())
363 break;
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);
371 if (((pBreak
372 && pBreak->getInt()
373 == static_cast<sal_Int32>(NS_ooxml::LN_Value_ST_SectionMark_continuous))
374 || m_nResetBreakOnSectBreak == RTFKeyword::SBKNONE)
375 && !(pTitlePg && pTitlePg->getInt()))
377 if (m_bWasInFrame)
379 dispatchSymbol(RTFKeyword::PAR);
380 m_bWasInFrame = false;
382 sectBreak();
383 // note: this will not affect the following section break
384 // but the one just pushed
385 dispatchFlag(RTFKeyword::SBKPAGE);
386 if (m_bNeedPar)
387 dispatchSymbol(RTFKeyword::PAR);
388 m_bIgnoreNextContSectBreak = true;
389 // arrange to clean up the synthetic RTFKeyword::SBKPAGE
390 m_nResetBreakOnSectBreak = RTFKeyword::SBKNONE;
392 else
394 checkFirstRun();
395 checkNeedPap();
396 sal_uInt8 const sBreak[] = { 0xc };
397 Mapper().text(sBreak, 1);
398 if (!m_bNeedPap)
400 parBreak();
401 m_bNeedPap = true;
403 m_bNeedCr = true;
406 break;
407 case RTFKeyword::CHPGN:
409 OUString aStr("PAGE");
410 singleChar(cFieldStart);
411 text(aStr);
412 singleChar(cFieldSep, true);
413 singleChar(cFieldEnd);
415 break;
416 case RTFKeyword::CHFTNSEP:
418 static const sal_Unicode uFtnEdnSep = 0x3;
419 Mapper().utext(reinterpret_cast<const sal_uInt8*>(&uFtnEdnSep), 1);
421 break;
422 default:
424 SAL_INFO("writerfilter.rtf",
425 "TODO handle symbol '" << keywordToString(nKeyword) << "'");
426 aSkip.setParsed(false);
428 break;
430 return RTFError::OK;
433 } // namespace writerfilter::rtftok
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */