[Libreoffice-commits] core.git: sw/qa writerfilter/source

László Németh (via logerrit) logerrit at kemper.freedesktop.org
Wed Mar 11 07:38:46 UTC 2020


 sw/qa/extras/ooxmlexport/data/tdf59274.docx              |binary
 sw/qa/extras/ooxmlexport/ooxmlexport10.cxx               |   14 +++++
 writerfilter/source/dmapper/DomainMapperTableManager.cxx |   37 ++++++++++++++-
 3 files changed, 50 insertions(+), 1 deletion(-)

New commits:
commit 116cadb5d2582532c69677a2f8499e8e9b7b9b80
Author:     László Németh <nemeth at numbertext.org>
AuthorDate: Tue Mar 10 15:44:59 2020 +0100
Commit:     László Németh <nemeth at numbertext.org>
CommitDate: Wed Mar 11 08:38:09 2020 +0100

    tdf#59274 DOCX import: fix tables with incomplete grid
    
    Fix layout of "auto" width tables with incomplete grids,
    where table width is defined by cells of an arbitrary
    table row, not necessarily the first row, and last cells
    of the rows can be wider, than their saved values.
    
    Change-Id: I68bc8f1a4f57f3c64d0e83c585f2be129d9b5a84
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/90261
    Tested-by: László Németh <nemeth at numbertext.org>
    Reviewed-by: László Németh <nemeth at numbertext.org>

diff --git a/sw/qa/extras/ooxmlexport/data/tdf59274.docx b/sw/qa/extras/ooxmlexport/data/tdf59274.docx
new file mode 100644
index 000000000000..38aad9ae9137
Binary files /dev/null and b/sw/qa/extras/ooxmlexport/data/tdf59274.docx differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport10.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport10.cxx
index 75403a4deeb9..1979a5180ec3 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport10.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport10.cxx
@@ -378,6 +378,20 @@ DECLARE_OOXMLEXPORT_EXPORTONLY_TEST(testFdo73389,"fdo73389.docx")
     assertXPath(pXmlDoc, "/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tblPr/w:tblW","w","5000");
 }
 
+DECLARE_OOXMLEXPORT_EXPORTONLY_TEST(testTdf59274, "tdf59274.docx")
+{
+    // Table with "auto" table width and incomplete grid: 11 columns, but only 4 gridCol elements.
+    xmlDocPtr pXmlDoc = parseExport();
+
+    assertXPath(pXmlDoc, "/w:document/w:body/w:tbl/w:tblPr/w:tblW", "type", "dxa");
+    // This was 7349: sum of the cell widths in first row, but the table width is determined by a longer row later.
+    assertXPath(pXmlDoc, "/w:document/w:body/w:tbl/w:tblPr/w:tblW", "w", "9048");
+    // This was 1224: too narrow first cell in first row
+    assertXPath(pXmlDoc, "/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:tcPr/w:tcW", "w", "4291");
+    // This was 3674: too wide last cell in first row
+    assertXPath(pXmlDoc, "/w:document/w:body/w:tbl/w:tr[1]/w:tc[4]/w:tcPr/w:tcW", "w", "1695");
+}
+
 DECLARE_OOXMLEXPORT_TEST(testDMLGroupshapeSdt, "dml-groupshape-sdt.docx")
 {
     uno::Reference<drawing::XShapes> xGroupShape(getShape(1), uno::UNO_QUERY);
diff --git a/writerfilter/source/dmapper/DomainMapperTableManager.cxx b/writerfilter/source/dmapper/DomainMapperTableManager.cxx
index 18ddcd902b35..ea322b6a58f3 100644
--- a/writerfilter/source/dmapper/DomainMapperTableManager.cxx
+++ b/writerfilter/source/dmapper/DomainMapperTableManager.cxx
@@ -649,6 +649,7 @@ void DomainMapperTableManager::endOfRowAction()
     for (int i : (*pTableGrid))
         nFullWidthRelative = o3tl::saturating_add(nFullWidthRelative, i);
 
+    bool bIsIncompleteGrid = false;
     if( pTableGrid->size() == ( nGrids + m_nGridAfter ) && m_nCell.back( ) > 0 )
     {
         /*
@@ -717,7 +718,8 @@ void DomainMapperTableManager::endOfRowAction()
     }
     else if ( !pCellWidths->empty() &&
                ( m_nLayoutType == NS_ooxml::LN_Value_doc_ST_TblLayout_fixed
-                 || pCellWidths->size() == ( nGrids + m_nGridAfter ) )
+                 || pCellWidths->size() == ( nGrids + m_nGridAfter )
+                 || ((bIsIncompleteGrid = true) && nGrids + m_nGridAfter > pTableGrid->size() && pCellWidths->size() > 0) )
              )
     {
         // If we're here, then the number of cells does not equal to the amount
@@ -728,21 +730,54 @@ void DomainMapperTableManager::endOfRowAction()
         // On the other hand even if the layout is not fixed, but the cell widths
         // provided equal the total number of cells, and there are no after/before cells
         // then use the cell widths to calculate the column separators.
+        // Also handle autofit tables with incomplete grids, when rows can have
+        // different widths and last cells can be wider, than their values.
         uno::Sequence< text::TableColumnSeparator > aSeparators(pCellWidths->size() - 1);
         text::TableColumnSeparator* pSeparators = aSeparators.getArray();
         sal_Int16 nSum = 0;
         sal_uInt32 nPos = 0;
+
+        if (bIsIncompleteGrid)
+            nFullWidthRelative = 0;
+
         // Avoid divide by zero (if there's no grid, position using cell widths).
         if( nFullWidthRelative == 0 )
             for (size_t i = 0; i < pCellWidths->size(); ++i)
                 nFullWidthRelative += (*pCellWidths)[i];
 
+        if (bIsIncompleteGrid)
+        {
+            /*
+             * If table width property set earlier is smaller than the current table row width,
+             * then replace the TABLE_WIDTH property, set earlier.
+             */
+            sal_Int32 nFullWidth = static_cast<sal_Int32>(ceil(ConversionHelper::convertTwipToMM100Double(nFullWidthRelative)));
+            sal_Int32 nTableWidth(0);
+            sal_Int32 nTableWidthType(text::SizeType::VARIABLE);
+            pTablePropMap->getValue(TablePropertyMap::TABLE_WIDTH, nTableWidth);
+            pTablePropMap->getValue(TablePropertyMap::TABLE_WIDTH_TYPE, nTableWidthType);
+            if (nTableWidth < nFullWidth)
+            {
+                pTablePropMap->setValue(TablePropertyMap::TABLE_WIDTH, nFullWidth);
+            }
+        }
+
         size_t nWidthsBound = pCellWidths->size() - 1;
         if (nWidthsBound)
         {
             if (nFullWidthRelative == 0)
                 throw o3tl::divide_by_zero();
 
+            // At incomplete table grids, last cell width can be smaller, than its final width.
+            // Correct it based on the last but one column width and their span values.
+            if ( bIsIncompleteGrid && pCurrentSpans->size()-1 == nWidthsBound )
+            {
+                auto aSpansIter = std::next(pCurrentSpans->begin( ), nWidthsBound - 1);
+                sal_Int32 nFixLastCellWidth = (*pCellWidths)[nWidthsBound-1] / *aSpansIter * *std::next(aSpansIter);
+                if (nFixLastCellWidth > (*pCellWidths)[nWidthsBound])
+                    nFullWidthRelative += nFixLastCellWidth - (*pCellWidths)[nWidthsBound];
+            }
+
             for (size_t i = 0; i < nWidthsBound; ++i)
             {
                 nSum += (*pCellWidths)[i];


More information about the Libreoffice-commits mailing list