tdf#122424 RTF import: ignore table row text outside the cells

instead of extending the next row with them.

Change-Id: I09941e977d0036fcea07885ccc7b745d396619c2
Reviewed-on: https://gerrit.libreoffice.org/65852
Tested-by: Jenkins
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/qa/extras/rtfexport/data/tdf122424.rtf b/sw/qa/extras/rtfexport/data/tdf122424.rtf
new file mode 100644
index 0000000..6baf526
--- /dev/null
+++ b/sw/qa/extras/rtfexport/data/tdf122424.rtf
@@ -0,0 +1,22 @@
{\rtf1\adeflang1025\ansi\ansicpg1250\uc1\adeff0\deff0
\colsx708
\cellx885
\cellx10260
\pard cell1
\cell
\pard cell2
\cell
X\trowd
\cellx885
\cellx10260\row
\trowd
\cellx885
\cellx5036
\pard cell3
\cell
\pard cell4
\cell
\trowd
\cellx885
\cellx5036\row
}
diff --git a/sw/qa/extras/rtfexport/rtfexport3.cxx b/sw/qa/extras/rtfexport/rtfexport3.cxx
index 6ada28d..2657c54 100644
--- a/sw/qa/extras/rtfexport/rtfexport3.cxx
+++ b/sw/qa/extras/rtfexport/rtfexport3.cxx
@@ -208,6 +208,16 @@
                         getProperty<sal_Int32>(getParagraph(1), "ParaLineNumberStartValue"));
}

DECLARE_RTFEXPORT_TEST(testTdf122424_textOutsideCellInATableRow, "tdf122424.rtf")
{
    uno::Reference<text::XTextTablesSupplier> xTextTablesSupplier(mxComponent, uno::UNO_QUERY);
    uno::Reference<container::XIndexAccess> xTables(xTextTablesSupplier->getTextTables(),
                                                    uno::UNO_QUERY);
    uno::Reference<text::XTextTable> xTable(xTables->getByIndex(0), uno::UNO_QUERY);
    uno::Reference<text::XTextRange> xCell(xTable->getCellByName("A2"), uno::UNO_QUERY_THROW);
    CPPUNIT_ASSERT_EQUAL(OUString("cell3"), xCell->getString());
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/writerfilter/source/rtftok/rtfdispatchsymbol.cxx b/writerfilter/source/rtftok/rtfdispatchsymbol.cxx
index f7371ee..ed97ad5 100644
--- a/writerfilter/source/rtftok/rtfdispatchsymbol.cxx
+++ b/writerfilter/source/rtftok/rtfdispatchsymbol.cxx
@@ -176,7 +176,16 @@
        case RTF_NESTCELL:
        {
            if (nKeyword == RTF_CELL)
            {
                m_bAfterCellBeforeRow = true;
                if (m_nCellsInRow != -1)
                    m_nCellsInRow++;
            }
            else
            {
                // in the case of nested tables, disable ignoring row text outside of cell content
                m_nCellsInRow = -1;
            }

            checkFirstRun();
            if (m_bNeedPap)
@@ -232,6 +241,7 @@
        case RTF_ROW:
        {
            m_bAfterCellBeforeRow = false;
            m_nActualCellInRow = 0;
            if (m_aStates.top().nTableRowWidthAfter > 0)
            {
                // Add fake cellx / cell, RTF equivalent of
diff --git a/writerfilter/source/rtftok/rtfdocumentimpl.cxx b/writerfilter/source/rtftok/rtfdocumentimpl.cxx
index 22b7668..f344925 100644
--- a/writerfilter/source/rtftok/rtfdocumentimpl.cxx
+++ b/writerfilter/source/rtftok/rtfdocumentimpl.cxx
@@ -300,6 +300,8 @@
    , m_hasRFooter(false)
    , m_hasFFooter(false)
    , m_bAfterCellBeforeRow(false)
    , m_nCellsInRow(0)
    , m_nActualCellInRow(0)
{
    OSL_ASSERT(xInputStream.is());
    m_pInStream = utl::UcbStreamHelper::CreateStream(xInputStream, true);
@@ -1478,6 +1480,12 @@

    RTFBuffer_t* pCurrentBuffer = m_aStates.top().pCurrentBuffer;

    if (m_nActualCellInRow > 0)
    {
        m_nActualCellInRow = 0;
        m_nCellsInRow = 0;
    }

    if (!pCurrentBuffer && m_aStates.top().eDestination != Destination::FOOTNOTE)
        Mapper().startCharacterGroup();
    else if (pCurrentBuffer)
@@ -1619,6 +1627,7 @@
        }
        else if (std::get<0>(aTuple) == BUFFER_CELLEND)
        {
            m_nActualCellInRow++;
            assert(pSprms && pAttributes);
            auto pValue = new RTFValue(1);
            pSprms->set(NS_ooxml::LN_tblCell, pValue);
@@ -1637,9 +1646,15 @@
        }
        else if (std::get<0>(aTuple) == BUFFER_UTEXT)
        {
            OUString const aString(std::get<1>(aTuple)->getString());
            Mapper().utext(reinterpret_cast<sal_uInt8 const*>(aString.getStr()),
                           aString.getLength());
            // ignore text outside the cell content in table rows
            // except in the case of nested tables
            if (m_nActualCellInRow == 0 || m_nActualCellInRow < m_nCellsInRow
                || m_nCellsInRow == -1)
            {
                OUString const aString(std::get<1>(aTuple)->getString());
                Mapper().utext(reinterpret_cast<sal_uInt8 const*>(aString.getStr()),
                               aString.getLength());
            }
        }
        else if (std::get<0>(aTuple) == BUFFER_ENDRUN)
            Mapper().endCharacterGroup();
diff --git a/writerfilter/source/rtftok/rtfdocumentimpl.hxx b/writerfilter/source/rtftok/rtfdocumentimpl.hxx
index 6fe2135..11c8dc85 100644
--- a/writerfilter/source/rtftok/rtfdocumentimpl.hxx
+++ b/writerfilter/source/rtftok/rtfdocumentimpl.hxx
@@ -720,6 +720,10 @@

    /// Are we after a \cell, but before a \row?
    bool m_bAfterCellBeforeRow;
    /// cells in row, to ignore extra text content of the row
    int m_nCellsInRow;
    /// actual cell in row
    int m_nActualCellInRow;
};
} // namespace rtftok
} // namespace writerfilter