sw/qa/extras/rtfexport/data/tdf111851.rtf       |   17 +++
 sw/qa/extras/rtfexport/rtfexport4.cxx           |   36 ++++++
 writerfilter/source/rtftok/rtfdispatchvalue.cxx |  136 ++++++++++--------------
 3 files changed, 111 insertions(+), 78 deletions(-)

New commits:
commit e5f96e2881d46ed71e390524d7172dca211de3e4
Author:     Vasily Melenchuk <vasily.melenc...@cib.de>
AuthorDate: Tue Mar 22 14:02:35 2022 +0300
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Wed Mar 23 09:21:35 2022 +0100

    tdf#111851: rtf import: fifty shades of grey
    
    Unlike in DOCX in RTF token \clshdngN can represent much more
    transitional cell shade values from 0 (white) to 10000 (black).
    So we should not match these values strictly but use ranges
    instead.
    
    Change-Id: I4e0066e2b79e73cf6fbc3dd773047be8dab2b907
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131931
    Tested-by: Jenkins
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/sw/qa/extras/rtfexport/data/tdf111851.rtf 
b/sw/qa/extras/rtfexport/data/tdf111851.rtf
new file mode 100644
index 000000000000..242354364e76
--- /dev/null
+++ b/sw/qa/extras/rtfexport/data/tdf111851.rtf
@@ -0,0 +1,17 @@
+{\rtf
+
+\trowd
+\clshdng-20000\cellx200
+\clshdng0\cellx400
+\clshdng666\cellx600
+\clshdng3275\cellx800
+\clshdng10000\cellx1000
+\clshdng20000\cellx1200
+\intbl a\cell
+\intbl b\cell
+\intbl c\cell
+\intbl d\cell
+\intbl e\cell
+\intbl f\cell
+\row
+}
\ No newline at end of file
diff --git a/sw/qa/extras/rtfexport/rtfexport4.cxx 
b/sw/qa/extras/rtfexport/rtfexport4.cxx
index fcc2f455c612..73b9bcd425e7 100644
--- a/sw/qa/extras/rtfexport/rtfexport4.cxx
+++ b/sw/qa/extras/rtfexport/rtfexport4.cxx
@@ -462,6 +462,42 @@ CPPUNIT_TEST_FIXTURE(Test, testClearingBreak)
     verify();
 }
 
+DECLARE_RTFEXPORT_TEST(testTdf111851, "tdf111851.rtf")
+{
+    uno::Reference<text::XTextTable> xTable(getParagraphOrTable(1), 
uno::UNO_QUERY);
+
+    // No shading
+    uno::Reference<text::XTextRange> xCell1(xTable->getCellByName("A1"), 
uno::UNO_QUERY);
+    CPPUNIT_ASSERT_EQUAL(OUString("a"), xCell1->getString());
+    CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), getProperty<sal_Int32>(xCell1, 
"BackColor"));
+
+    uno::Reference<text::XTextRange> xCell2(xTable->getCellByName("B1"), 
uno::UNO_QUERY);
+    CPPUNIT_ASSERT_EQUAL(OUString("b"), xCell2->getString());
+    CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), getProperty<sal_Int32>(xCell2, 
"BackColor"));
+
+    // Check some random not standard shading values and ensure some non-white 
background color
+    uno::Reference<text::XTextRange> xCell3(xTable->getCellByName("C1"), 
uno::UNO_QUERY);
+    CPPUNIT_ASSERT_EQUAL(OUString("c"), xCell3->getString());
+    sal_Int32 nShadingColor3 = getProperty<sal_Int32>(xCell3, "BackColor");
+    CPPUNIT_ASSERT(0x00FFFFFF > nShadingColor3);
+    CPPUNIT_ASSERT(0 < nShadingColor3);
+
+    uno::Reference<text::XTextRange> xCell4(xTable->getCellByName("D1"), 
uno::UNO_QUERY);
+    CPPUNIT_ASSERT_EQUAL(OUString("d"), xCell4->getString());
+    sal_Int32 nShadingColor4 = getProperty<sal_Int32>(xCell4, "BackColor");
+    CPPUNIT_ASSERT(0x00FFFFFF > nShadingColor4);
+    CPPUNIT_ASSERT(0 < nShadingColor4);
+
+    // Values 10000 and more - black
+    uno::Reference<text::XTextRange> xCell5(xTable->getCellByName("E1"), 
uno::UNO_QUERY);
+    CPPUNIT_ASSERT_EQUAL(OUString("e"), xCell5->getString());
+    CPPUNIT_ASSERT_EQUAL(sal_Int32(0), getProperty<sal_Int32>(xCell5, 
"BackColor"));
+
+    uno::Reference<text::XTextRange> xCell6(xTable->getCellByName("F1"), 
uno::UNO_QUERY);
+    CPPUNIT_ASSERT_EQUAL(OUString("f"), xCell6->getString());
+    CPPUNIT_ASSERT_EQUAL(sal_Int32(0), getProperty<sal_Int32>(xCell6, 
"BackColor"));
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/writerfilter/source/rtftok/rtfdispatchvalue.cxx 
b/writerfilter/source/rtftok/rtfdispatchvalue.cxx
index f12c90575c33..b3c04bb0f1f1 100644
--- a/writerfilter/source/rtftok/rtfdispatchvalue.cxx
+++ b/writerfilter/source/rtftok/rtfdispatchvalue.cxx
@@ -485,84 +485,64 @@ bool RTFDocumentImpl::dispatchTableValue(RTFKeyword 
nKeyword, int nParam)
         case RTFKeyword::CLSHDNG:
         {
             int nValue = -1;
-            switch (nParam)
-            {
-                case 500:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct5;
-                    break;
-                case 1000:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct10;
-                    break;
-                case 1200:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct12;
-                    break;
-                case 1500:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct15;
-                    break;
-                case 2000:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct20;
-                    break;
-                case 2500:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct25;
-                    break;
-                case 3000:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct30;
-                    break;
-                case 3500:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct35;
-                    break;
-                case 3700:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct37;
-                    break;
-                case 4000:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct40;
-                    break;
-                case 4500:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct45;
-                    break;
-                case 5000:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct50;
-                    break;
-                case 5500:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct55;
-                    break;
-                case 6000:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct60;
-                    break;
-                case 6200:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct62;
-                    break;
-                case 6500:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct65;
-                    break;
-                case 7000:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct70;
-                    break;
-                case 7500:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct75;
-                    break;
-                case 8000:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct80;
-                    break;
-                case 8500:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct85;
-                    break;
-                case 8700:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct87;
-                    break;
-                case 9000:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct90;
-                    break;
-                case 9500:
-                    nValue = NS_ooxml::LN_Value_ST_Shd_pct95;
-                    break;
-                default:
-                    break;
-            }
-            if (nValue != -1)
-                putNestedAttribute(m_aStates.top().getTableCellSprms(),
-                                   NS_ooxml::LN_CT_TcPrBase_shd, 
NS_ooxml::LN_CT_Shd_val,
-                                   new RTFValue(nValue));
+
+            if (nParam < 1)
+                nValue = NS_ooxml::LN_Value_ST_Shd_clear;
+            else if (nParam < 750)
+                // Values in between 1 and 250 visually closer to 0% shading 
(white)
+                // But this will mean "no shading" while cell actually have 
some.
+                // So lets use minimal available value.
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct5;
+            else if (nParam < 1100)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct10;
+            else if (nParam < 1350)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct12;
+            else if (nParam < 1750)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct15;
+            else if (nParam < 2250)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct20;
+            else if (nParam < 2750)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct25;
+            else if (nParam < 3250)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct30;
+            else if (nParam < 3600)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct35;
+            else if (nParam < 3850)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct37;
+            else if (nParam < 4250)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct40;
+            else if (nParam < 4750)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct45;
+            else if (nParam < 5250)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct50;
+            else if (nParam < 5750)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct55;
+            else if (nParam < 6100)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct60;
+            else if (nParam < 6350)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct62;
+            else if (nParam < 6750)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct65;
+            else if (nParam < 7250)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct70;
+            else if (nParam < 7750)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct75;
+            else if (nParam < 8250)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct80;
+            else if (nParam < 8600)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct85;
+            else if (nParam < 8850)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct87;
+            else if (nParam < 9250)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct90;
+            else if (nParam < 9750)
+                nValue = NS_ooxml::LN_Value_ST_Shd_pct95;
+            else
+                // Solid fill
+                nValue = NS_ooxml::LN_Value_ST_Shd_solid;
+
+            putNestedAttribute(m_aStates.top().getTableCellSprms(), 
NS_ooxml::LN_CT_TcPrBase_shd,
+                               NS_ooxml::LN_CT_Shd_val, new RTFValue(nValue));
             return true;
         }
         break;

Reply via email to