diff options
author | Michael Stahl <mstahl@redhat.com> | 2014-02-27 23:48:59 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.co.uk> | 2014-03-03 10:31:30 -0600 |
commit | 153993292cc9f11fed8fcba8de45b0c46d5e0ef2 (patch) | |
tree | 772310769244edceee186f784b51230598278472 /writerfilter/source/rtftok/rtfdocumentimpl.cxx | |
parent | cb83b334e489a4ec1cc067145c3f64b129475e7f (diff) |
RTF import: fix spurious page breaks at doc end (related: rhbz#1065629)cp-4.2-branch-point
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.
(cherry picked from commit e3f254ab8211fbab7541cde2100a35c875b0c240)
RTF import: add unit test for page break in continuous section
(cherry picked from commit f03218f43e8c25c2e136d364455f3cdaf95362b6)
RTF import: fix paragraphs in header/footer
(cherry picked from commit 74b3f4f00766d199df3b017d056cf7a00ae52988)
Change-Id: I3c1a3bceb2c8b75bbecdc748170562451ce5f5c3
Reviewed-on: https://gerrit.libreoffice.org/8439
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Miklos Vajna <vmiklos@collabora.co.uk>
Diffstat (limited to 'writerfilter/source/rtftok/rtfdocumentimpl.cxx')
-rw-r--r-- | writerfilter/source/rtftok/rtfdocumentimpl.cxx | 71 |
1 files changed, 50 insertions, 21 deletions
diff --git a/writerfilter/source/rtftok/rtfdocumentimpl.cxx b/writerfilter/source/rtftok/rtfdocumentimpl.cxx index d5e7843592ce..0dc20b0685fd 100644 --- a/writerfilter/source/rtftok/rtfdocumentimpl.cxx +++ b/writerfilter/source/rtftok/rtfdocumentimpl.cxx @@ -279,7 +279,8 @@ RTFDocumentImpl::RTFDocumentImpl(uno::Reference<uno::XComponentContext> const& x m_aHexBuffer(), m_bMathNor(false), m_bIgnoreNextContSectBreak(false), - m_bNeedSect(true), + m_nResetBreakOnSectBreak(static_cast<RTFKeyword>(-1)), + m_bNeedSect(false), // done by checkFirstRun m_bWasInFrame(false), m_bHadPicture(false), m_bHadSect(false), @@ -392,16 +393,15 @@ void RTFDocumentImpl::checkFirstRun() writerfilter::Reference<Table>::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; } } @@ -417,7 +417,22 @@ 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(); + } + // set flag in substream too - otherwise multiple startParagraphGroup + m_bNeedSect = bNeedSect; + Mapper().startParagraphGroup(); + setNeedPar(true); + } + else if (m_bNeedSect && !bNeedSect) + { + m_bNeedSect = bNeedSect; + } } writerfilter::Reference<Properties>::Pointer_t RTFDocumentImpl::getProperties(RTFSprms& rAttributes, RTFSprms& rSprms) @@ -542,6 +557,7 @@ void RTFDocumentImpl::sectBreak(bool bFinal = false) { dispatchFlag(RTF_PARD); dispatchSymbol(RTF_PAR); + m_bNeedSect = bNeedSect; } while (!m_nHeaderFooterPositions.empty()) { @@ -572,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; } @@ -1345,8 +1356,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) { @@ -1867,11 +1878,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) @@ -1944,7 +1955,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<RTFKeyword>(-1); + m_bNeedSect = false; // dispatchSymbol set it + } + } } break; case RTF_NOBREAK: @@ -2116,19 +2135,24 @@ int RTFDocumentImpl::dispatchSymbol(RTFKeyword nKeyword) RTFValue::Pointer_t pBreak = m_aStates.top().aSectionSprms.find(NS_sprm::LN_SBkc); // 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 { @@ -2162,8 +2186,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; @@ -2295,6 +2319,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_sprm::LN_SBkc, pValue); return 0; @@ -2902,8 +2930,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)); @@ -3869,8 +3897,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(!bParam || nParam != 0)); @@ -4721,7 +4749,8 @@ int RTFDocumentImpl::popState() // not in case of other substreams, like headers. if (m_bNeedCr && !(m_nStreamType == NS_rtf::LN_footnote || m_nStreamType == NS_rtf::LN_endnote)) dispatchSymbol(RTF_PAR); - sectBreak(true); + if (m_bNeedSect) // may be set by dispatchSymbol above! + sectBreak(true); } m_aStates.pop(); |