From e3f254ab8211fbab7541cde2100a35c875b0c240 Mon Sep 17 00:00:00 2001 From: Michael Stahl Date: Thu, 27 Feb 2014 23:48:59 +0100 Subject: RTF import: fix spurious page breaks at doc end (related: rhbz#1065629) When a document ends with \sect it's possible that a spurious page break is created. In fact the spurious page break is always created by the RTF importer, sometimes it is deleted again by DomainMapper_Impl::RemoveLastParagraph() and sometimes not. It is created because on the final \sect RTFDocumentImpl::sectBreak() still calls startSectionGroup(), and the popState() for the \rtf1 group then calls sectBreak() another time. To prevent this, do not call startSectionGroup() from sectBreak() but instead from setNeedSect(), and ensure that it is called as soon as anything after \sect is read. One unit test fails because the \page is not handled properly: the conversion to \skbpage \sect \skbnone is not correct, because the \skb* keywords are an exception and affect the \sect that precedes them, not the following one; sending the \skbpage later unfortunately requires additional cleanup later. Change-Id: I3c1a3bceb2c8b75bbecdc748170562451ce5f5c3 --- sw/qa/extras/rtfimport/rtfimport.cxx | 3 ++ writerfilter/source/rtftok/rtfdocumentimpl.cxx | 70 ++++++++++++++++++-------- writerfilter/source/rtftok/rtfdocumentimpl.hxx | 3 ++ 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/sw/qa/extras/rtfimport/rtfimport.cxx b/sw/qa/extras/rtfimport/rtfimport.cxx index 88b8ca1d1a2e..2c119f3bf76b 100644 --- a/sw/qa/extras/rtfimport/rtfimport.cxx +++ b/sw/qa/extras/rtfimport/rtfimport.cxx @@ -1415,6 +1415,9 @@ DECLARE_RTFIMPORT_TEST(testNestedTable, "rhbz1065629.rtf") getProperty(xCell, "RightBorder")); CPPUNIT_ASSERT_EQUAL(static_cast(0xffffffff), getProperty(xCell, "BackColor")); + + // \sect at the end resulted in spurious page break + CPPUNIT_ASSERT_EQUAL(1, getPages()); } DECLARE_RTFIMPORT_TEST(testCp1000016, "hello.rtf") diff --git a/writerfilter/source/rtftok/rtfdocumentimpl.cxx b/writerfilter/source/rtftok/rtfdocumentimpl.cxx index 8da5ede3a6fb..38d427fb6ae3 100644 --- a/writerfilter/source/rtftok/rtfdocumentimpl.cxx +++ b/writerfilter/source/rtftok/rtfdocumentimpl.cxx @@ -280,7 +280,8 @@ RTFDocumentImpl::RTFDocumentImpl(uno::Reference const& x m_aHexBuffer(), m_bMathNor(false), m_bIgnoreNextContSectBreak(false), - m_bNeedSect(true), + m_nResetBreakOnSectBreak(static_cast(-1)), + m_bNeedSect(false), // done by checkFirstRun m_bWasInFrame(false), m_bHadPicture(false), m_bHadSect(false), @@ -393,16 +394,15 @@ void RTFDocumentImpl::checkFirstRun() writerfilter::Reference::Pointer_t const pTable(new RTFReferenceTable(aSettingsTableEntries)); Mapper().table(NS_ooxml::LN_settings_settings, pTable); // start initial paragraph - if (!m_pSuperstream) - Mapper().startSectionGroup(); - Mapper().startParagraphGroup(); + m_bFirstRun = false; + assert(!m_bNeedSect); + setNeedSect(); // first call that succeeds // set the requested default font, if there are none RTFValue::Pointer_t pFont = m_aDefaultState.aCharacterSprms.find(NS_sprm::LN_CRgFtc0); RTFValue::Pointer_t pCurrentFont = m_aStates.top().aCharacterSprms.find(NS_sprm::LN_CRgFtc0); if (pFont && !pCurrentFont) dispatchValue(RTF_F, pFont->getInt()); - m_bFirstRun = false; } } @@ -418,7 +418,21 @@ void RTFDocumentImpl::setNeedPar(bool bNeedPar) void RTFDocumentImpl::setNeedSect(bool bNeedSect) { - m_bNeedSect = bNeedSect; + // ignore setting before checkFirstRun - every keyword calls setNeedSect! + if (!m_bNeedSect && bNeedSect && !m_bFirstRun) + { + if (!m_pSuperstream) // no sections in header/footer! + { + Mapper().startSectionGroup(); + m_bNeedSect = bNeedSect; + } + Mapper().startParagraphGroup(); + setNeedPar(true); + } + else if (m_bNeedSect && !bNeedSect) + { + m_bNeedSect = bNeedSect; + } } writerfilter::Reference::Pointer_t RTFDocumentImpl::getProperties(RTFSprms& rAttributes, RTFSprms& rSprms) @@ -543,6 +557,7 @@ void RTFDocumentImpl::sectBreak(bool bFinal = false) { dispatchFlag(RTF_PARD); dispatchSymbol(RTF_PAR); + m_bNeedSect = bNeedSect; } while (!m_nHeaderFooterPositions.empty()) { @@ -573,12 +588,7 @@ void RTFDocumentImpl::sectBreak(bool bFinal = false) Mapper().endParagraphGroup(); if (!m_pSuperstream) Mapper().endSectionGroup(); - if (!bFinal) - { - Mapper().startSectionGroup(); - Mapper().startParagraphGroup(); - } - m_bNeedPar = true; + m_bNeedPar = false; m_bNeedSect = false; } @@ -1368,8 +1378,8 @@ void RTFDocumentImpl::replayBuffer(RTFBuffer_t& rBuffer, int RTFDocumentImpl::dispatchDestination(RTFKeyword nKeyword) { - checkUnicode(/*bUnicode =*/ true, /*bHex =*/ true); setNeedSect(); + checkUnicode(/*bUnicode =*/ true, /*bHex =*/ true); RTFSkipDestination aSkip(*this); switch (nKeyword) { @@ -1885,11 +1895,11 @@ int RTFDocumentImpl::dispatchDestination(RTFKeyword nKeyword) int RTFDocumentImpl::dispatchSymbol(RTFKeyword nKeyword) { + setNeedSect(); if (nKeyword != RTF_HEXCHAR) checkUnicode(/*bUnicode =*/ true, /*bHex =*/ true); else checkUnicode(/*bUnicode =*/ true, /*bHex =*/ false); - setNeedSect(); RTFSkipDestination aSkip(*this); if (RTF_LINE == nKeyword) @@ -1968,7 +1978,15 @@ int RTFDocumentImpl::dispatchSymbol(RTFKeyword nKeyword) if (m_bIgnoreNextContSectBreak) m_bIgnoreNextContSectBreak = false; else + { sectBreak(); + if (m_nResetBreakOnSectBreak != -1) + { // this should run on _second_ \sect after \page + dispatchSymbol(m_nResetBreakOnSectBreak); // lazy reset + m_nResetBreakOnSectBreak = static_cast(-1); + m_bNeedSect = false; // dispatchSymbol set it + } + } } break; case RTF_NOBREAK: @@ -2140,19 +2158,24 @@ int RTFDocumentImpl::dispatchSymbol(RTFKeyword nKeyword) RTFValue::Pointer_t pBreak = m_aStates.top().aSectionSprms.find(NS_ooxml::LN_EG_SectPrContents_type); // Unless we're on a title page. RTFValue::Pointer_t pTitlePg = m_aStates.top().aSectionSprms.find(NS_ooxml::LN_EG_SectPrContents_titlePg); - if ((pBreak.get() && !pBreak->getInt()) && !(pTitlePg.get() && pTitlePg->getInt())) + if (((pBreak.get() && !pBreak->getInt()) + || m_nResetBreakOnSectBreak == RTF_SBKNONE) + && !(pTitlePg.get() && pTitlePg->getInt())) { if (m_bWasInFrame) { dispatchSymbol(RTF_PAR); m_bWasInFrame = false; } - dispatchFlag(RTF_SBKPAGE); sectBreak(); - dispatchFlag(RTF_SBKNONE); + // note: this will not affect the following section break + // but the one just pushed + dispatchFlag(RTF_SBKPAGE); if (m_bNeedPar) dispatchSymbol(RTF_PAR); m_bIgnoreNextContSectBreak = true; + // arrange to clean up the syntetic RTF_SBKPAGE + m_nResetBreakOnSectBreak = RTF_SBKNONE; } else { @@ -2192,8 +2215,8 @@ int RTFDocumentImpl::dispatchSymbol(RTFKeyword nKeyword) int RTFDocumentImpl::dispatchFlag(RTFKeyword nKeyword) { - checkUnicode(/*bUnicode =*/ true, /*bHex =*/ true); setNeedSect(); + checkUnicode(/*bUnicode =*/ true, /*bHex =*/ true); RTFSkipDestination aSkip(*this); int nParam = -1; int nSprm = -1; @@ -2325,6 +2348,10 @@ int RTFDocumentImpl::dispatchFlag(RTFKeyword nKeyword) } if (nParam >= 0) { + if (m_nResetBreakOnSectBreak != -1) + { + m_nResetBreakOnSectBreak = nKeyword; + } RTFValue::Pointer_t pValue(new RTFValue(nParam)); m_aStates.top().aSectionSprms.set(NS_ooxml::LN_EG_SectPrContents_type, pValue); return 0; @@ -2932,8 +2959,8 @@ int RTFDocumentImpl::dispatchFlag(RTFKeyword nKeyword) int RTFDocumentImpl::dispatchValue(RTFKeyword nKeyword, int nParam) { - checkUnicode(/*bUnicode =*/ nKeyword != RTF_U, /*bHex =*/ true); setNeedSect(); + checkUnicode(/*bUnicode =*/ nKeyword != RTF_U, /*bHex =*/ true); RTFSkipDestination aSkip(*this); int nSprm = 0; RTFValue::Pointer_t pIntValue(new RTFValue(nParam)); @@ -3931,8 +3958,8 @@ int RTFDocumentImpl::dispatchValue(RTFKeyword nKeyword, int nParam) int RTFDocumentImpl::dispatchToggle(RTFKeyword nKeyword, bool bParam, int nParam) { - checkUnicode(/*bUnicode =*/ true, /*bHex =*/ true); setNeedSect(); + checkUnicode(/*bUnicode =*/ true, /*bHex =*/ true); RTFSkipDestination aSkip(*this); int nSprm = -1; RTFValue::Pointer_t pBoolValue(new RTFValue(int(!bParam || nParam != 0))); @@ -4805,7 +4832,8 @@ int RTFDocumentImpl::popState() // not in case of other substreams, like headers. if (m_bNeedCr && !(m_nStreamType == NS_ooxml::LN_footnote || m_nStreamType == NS_ooxml::LN_endnote)) dispatchSymbol(RTF_PAR); - sectBreak(true); + if (m_bNeedSect) // may be set by dispatchSymbol above! + sectBreak(true); } m_aStates.pop(); diff --git a/writerfilter/source/rtftok/rtfdocumentimpl.hxx b/writerfilter/source/rtftok/rtfdocumentimpl.hxx index cbc8ed9e9e03..1c9ff0690420 100644 --- a/writerfilter/source/rtftok/rtfdocumentimpl.hxx +++ b/writerfilter/source/rtftok/rtfdocumentimpl.hxx @@ -526,6 +526,9 @@ namespace writerfilter { bool m_bMathNor; /// If the next continuous section break should be ignored. bool m_bIgnoreNextContSectBreak; + /// clean up a synthetic page break, see RTF_PAGE + /// if inactive value is -1, otherwise the RTF_SKB* to restore + RTFKeyword m_nResetBreakOnSectBreak; /// If a section break is needed before the end of the doc (false right after a section break). bool m_bNeedSect; /// If aFrame.inFrame() was true in the previous state. -- cgit v1.2.3