From 9a946645c8029bc6382d8e455b76430d4e4bc37f Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Tue, 12 Dec 2023 08:39:54 +0100 Subject: sw floattable: fix split of anchor text in 2nd half of the paragraph MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If you go to the anchor text of the floating table and you press enter in the second half of the anchor text, you get a layout loop. The reason for this: an invariant around split text frames were violated, later resulting in a layout loop. The rule is that in case you have a split frame, then there can't be a frame for a new text node between the two split frames. So no new text frame for an other node after the master anchor of the fly; no new text frame for an other node before the follow anchor of the fly. Fix the problem by improving SwTextNode::SplitContentNode() to check if this is an anchor for a split fly: if so, always insert the new text frame after the follow anchor of the fly, which doesn't break the above invariant. The layout loop is fixed, but the text in the follow anchor of the fly still has a bad position, that still needs fixing. Also currently testSplitFlyMoveMaster enforces that split at the para start inserts a paragraph before the floating table, so leave that case unchanged for now. (cherry picked from commit 0746d13365139c356eb9d297a358c486bf47d6fb) Change-Id: I77962a354e297d2e9957edcce9bf140f2c72fc6e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/160671 Tested-by: Jenkins CollaboraOffice Reviewed-by: Caolán McNamara --- .../core/txtnode/data/floattable-anchor-split.docx | Bin 0 -> 13035 bytes sw/qa/core/txtnode/txtnode.cxx | 36 +++++++++++++++++++++ sw/source/core/txtnode/ndtxt.cxx | 21 +++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 sw/qa/core/txtnode/data/floattable-anchor-split.docx diff --git a/sw/qa/core/txtnode/data/floattable-anchor-split.docx b/sw/qa/core/txtnode/data/floattable-anchor-split.docx new file mode 100644 index 000000000000..a5dcdb28eb8c Binary files /dev/null and b/sw/qa/core/txtnode/data/floattable-anchor-split.docx differ diff --git a/sw/qa/core/txtnode/txtnode.cxx b/sw/qa/core/txtnode/txtnode.cxx index ed6f347d9b28..9d4d09cdf0f9 100644 --- a/sw/qa/core/txtnode/txtnode.cxx +++ b/sw/qa/core/txtnode/txtnode.cxx @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -34,6 +35,9 @@ #include #include #include +#include +#include +#include /// Covers sw/source/core/txtnode/ fixes. class SwCoreTxtnodeTest : public SwModelTestBase @@ -394,6 +398,38 @@ CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest, testFlySplitFootnote) CPPUNIT_ASSERT(!pDoc->GetFootnoteIdxs().empty()); } +CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest, testSplitFlyAnchorSplit) +{ + // Given a document with a 2 pages long floating table: + createSwDoc("floattable-anchor-split.docx"); + + // When splitting the "AB" anchor text into "A" (remains as anchor text) and "B" (new text node + // after it): + SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell(); + pWrtShell->SttEndDoc(/*bStt=*/false); + pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false); + // Without the accompanying fix in place, this test would have failed with a layout loop. + pWrtShell->SplitNode(); + + // Then make sure the resulting layout is what we want: + SwDoc* pDoc = getSwDoc(); + SwRootFrame* pLayout = pDoc->getIDocumentLayoutAccess().GetCurrentLayout(); + auto pPage1 = pLayout->Lower()->DynCastPageFrame(); + CPPUNIT_ASSERT(pPage1); + // Page 1 has the master fly: + CPPUNIT_ASSERT(pPage1->GetSortedObjs()); + auto pPage2 = pPage1->GetNext()->DynCastPageFrame(); + CPPUNIT_ASSERT(pPage2); + // Page 1 has the follow fly: + CPPUNIT_ASSERT(pPage2->GetSortedObjs()); + // Anchor text is now just "A": + auto pText1 = pPage2->FindFirstBodyContent()->DynCastTextFrame(); + CPPUNIT_ASSERT_EQUAL(OUString("A"), pText1->GetText()); + // New text frame is just "B": + auto pText2 = pText1->GetNext()->DynCastTextFrame(); + CPPUNIT_ASSERT_EQUAL(OUString("B"), pText2->GetText()); +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/txtnode/ndtxt.cxx b/sw/source/core/txtnode/ndtxt.cxx index 8dfb89835faa..ff05143b0052 100644 --- a/sw/source/core/txtnode/ndtxt.cxx +++ b/sw/source/core/txtnode/ndtxt.cxx @@ -82,6 +82,8 @@ #include #include #include +#include +#include using namespace ::com::sun::star; @@ -459,7 +461,24 @@ SwTextNode *SwTextNode::SplitContentNode(const SwPosition & rPos, ResetAttr( RES_PARATR_LIST_LEVEL ); } - if ( HasWriterListeners() && !m_Text.isEmpty() && (nTextLen / 2) < nSplitPos ) + bool bSplitFly = false; + std::optional> oFlys = sw::GetFlysAnchoredAt(GetDoc(), GetIndex()); + if (oFlys.has_value() && nSplitPos > 0) + { + // See if one of the flys is a split fly. If so, we need to keep + // the potentially split text frames unchanged and create a new + // text frame at the end. + for (const auto& rFly : *oFlys) + { + if (rFly->GetFlySplit().GetValue()) + { + bSplitFly = true; + break; + } + } + } + + if ( HasWriterListeners() && !m_Text.isEmpty() && ((nTextLen / 2) < nSplitPos || bSplitFly) ) { // optimization for SplitNode: If a split is at the end of a node then // move the frames from the current to the new one and create new ones -- cgit