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 <vmiklos@collabora.com>
This commit is contained in:
Vasily Melenchuk
2022-03-22 14:02:35 +03:00
committed by Miklos Vajna
parent be5ef79b93
commit e5f96e2881
3 changed files with 111 additions and 78 deletions

View File

@@ -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
}

View File

@@ -462,6 +462,42 @@ CPPUNIT_TEST_FIXTURE(Test, testClearingBreak)
verify(); 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(); CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@@ -485,84 +485,64 @@ bool RTFDocumentImpl::dispatchTableValue(RTFKeyword nKeyword, int nParam)
case RTFKeyword::CLSHDNG: case RTFKeyword::CLSHDNG:
{ {
int nValue = -1; int nValue = -1;
switch (nParam)
{ if (nParam < 1)
case 500: nValue = NS_ooxml::LN_Value_ST_Shd_clear;
nValue = NS_ooxml::LN_Value_ST_Shd_pct5; else if (nParam < 750)
break; // Values in between 1 and 250 visually closer to 0% shading (white)
case 1000: // But this will mean "no shading" while cell actually have some.
nValue = NS_ooxml::LN_Value_ST_Shd_pct10; // So lets use minimal available value.
break; nValue = NS_ooxml::LN_Value_ST_Shd_pct5;
case 1200: else if (nParam < 1100)
nValue = NS_ooxml::LN_Value_ST_Shd_pct12; nValue = NS_ooxml::LN_Value_ST_Shd_pct10;
break; else if (nParam < 1350)
case 1500: nValue = NS_ooxml::LN_Value_ST_Shd_pct12;
nValue = NS_ooxml::LN_Value_ST_Shd_pct15; else if (nParam < 1750)
break; nValue = NS_ooxml::LN_Value_ST_Shd_pct15;
case 2000: else if (nParam < 2250)
nValue = NS_ooxml::LN_Value_ST_Shd_pct20; nValue = NS_ooxml::LN_Value_ST_Shd_pct20;
break; else if (nParam < 2750)
case 2500: nValue = NS_ooxml::LN_Value_ST_Shd_pct25;
nValue = NS_ooxml::LN_Value_ST_Shd_pct25; else if (nParam < 3250)
break; nValue = NS_ooxml::LN_Value_ST_Shd_pct30;
case 3000: else if (nParam < 3600)
nValue = NS_ooxml::LN_Value_ST_Shd_pct30; nValue = NS_ooxml::LN_Value_ST_Shd_pct35;
break; else if (nParam < 3850)
case 3500: nValue = NS_ooxml::LN_Value_ST_Shd_pct37;
nValue = NS_ooxml::LN_Value_ST_Shd_pct35; else if (nParam < 4250)
break; nValue = NS_ooxml::LN_Value_ST_Shd_pct40;
case 3700: else if (nParam < 4750)
nValue = NS_ooxml::LN_Value_ST_Shd_pct37; nValue = NS_ooxml::LN_Value_ST_Shd_pct45;
break; else if (nParam < 5250)
case 4000: nValue = NS_ooxml::LN_Value_ST_Shd_pct50;
nValue = NS_ooxml::LN_Value_ST_Shd_pct40; else if (nParam < 5750)
break; nValue = NS_ooxml::LN_Value_ST_Shd_pct55;
case 4500: else if (nParam < 6100)
nValue = NS_ooxml::LN_Value_ST_Shd_pct45; nValue = NS_ooxml::LN_Value_ST_Shd_pct60;
break; else if (nParam < 6350)
case 5000: nValue = NS_ooxml::LN_Value_ST_Shd_pct62;
nValue = NS_ooxml::LN_Value_ST_Shd_pct50; else if (nParam < 6750)
break; nValue = NS_ooxml::LN_Value_ST_Shd_pct65;
case 5500: else if (nParam < 7250)
nValue = NS_ooxml::LN_Value_ST_Shd_pct55; nValue = NS_ooxml::LN_Value_ST_Shd_pct70;
break; else if (nParam < 7750)
case 6000: nValue = NS_ooxml::LN_Value_ST_Shd_pct75;
nValue = NS_ooxml::LN_Value_ST_Shd_pct60; else if (nParam < 8250)
break; nValue = NS_ooxml::LN_Value_ST_Shd_pct80;
case 6200: else if (nParam < 8600)
nValue = NS_ooxml::LN_Value_ST_Shd_pct62; nValue = NS_ooxml::LN_Value_ST_Shd_pct85;
break; else if (nParam < 8850)
case 6500: nValue = NS_ooxml::LN_Value_ST_Shd_pct87;
nValue = NS_ooxml::LN_Value_ST_Shd_pct65; else if (nParam < 9250)
break; nValue = NS_ooxml::LN_Value_ST_Shd_pct90;
case 7000: else if (nParam < 9750)
nValue = NS_ooxml::LN_Value_ST_Shd_pct70; nValue = NS_ooxml::LN_Value_ST_Shd_pct95;
break; else
case 7500: // Solid fill
nValue = NS_ooxml::LN_Value_ST_Shd_pct75; nValue = NS_ooxml::LN_Value_ST_Shd_solid;
break;
case 8000: putNestedAttribute(m_aStates.top().getTableCellSprms(), NS_ooxml::LN_CT_TcPrBase_shd,
nValue = NS_ooxml::LN_Value_ST_Shd_pct80; NS_ooxml::LN_CT_Shd_val, new RTFValue(nValue));
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));
return true; return true;
} }
break; break;