sw/qa/extras/htmlimport/data/green-highlight.html | 1 sw/qa/extras/htmlimport/htmlimport.cxx | 12 ++++++++++ sw/source/filter/html/parcss1.cxx | 26 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-)
New commits: commit dd13717076e412d7b1079a12adaff01efebf0f80 Author: Andras Timar <andras.ti...@collabora.com> AuthorDate: Wed Jan 4 14:34:49 2023 +0100 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Wed Jan 4 14:27:16 2023 +0000 import colors with transparency from html The support is limited, but better than before. Before: the color was not imported. After: the color is imported as RGB, as if the background color was white. It is pretty much common in Writer that the background is white. On the other hand, transparency is not supported in many use cases, such as character background in this example green-highlight.html. Change-Id: Ia9449e2535ddfd8cd8c2672cb3bd32987083fdbb Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145039 Tested-by: Jenkins Reviewed-by: Miklos Vajna <vmik...@collabora.com> diff --git a/sw/qa/extras/htmlimport/data/green-highlight.html b/sw/qa/extras/htmlimport/data/green-highlight.html new file mode 100644 index 000000000000..b8986e78ffd8 --- /dev/null +++ b/sw/qa/extras/htmlimport/data/green-highlight.html @@ -0,0 +1 @@ +<p><span style="background-color: #5fb23680">Highlight green (transparency: 0.5)</span></p> diff --git a/sw/qa/extras/htmlimport/htmlimport.cxx b/sw/qa/extras/htmlimport/htmlimport.cxx index b9d9595129f5..c97d4e4cbde4 100644 --- a/sw/qa/extras/htmlimport/htmlimport.cxx +++ b/sw/qa/extras/htmlimport/htmlimport.cxx @@ -562,6 +562,18 @@ CPPUNIT_TEST_FIXTURE(SwHtmlOptionsImportTest, testOleData2) CPPUNIT_ASSERT(getProperty<OUString>(xShape, "HyperLinkURL").isEmpty()); } +CPPUNIT_TEST_FIXTURE(HtmlImportTest, testRGBAColor) +{ + createSwWebDoc("green-highlight.html"); + const uno::Reference<text::XTextRange> xPara = getParagraph(1); + const uno::Reference<beans::XPropertySet> xRun(getRun(xPara,1), uno::UNO_QUERY); + const Color nBackColor(0xaed89a); + + // Without the accompanying fix in place, this test would have failed, the backround + // color was not imported at all, when it was in hex RGBA format in HTML. + CPPUNIT_ASSERT_EQUAL(nBackColor, getProperty<Color>(xRun, "CharBackColor")); +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/filter/html/parcss1.cxx b/sw/source/filter/html/parcss1.cxx index db126e28d2bd..6da42b87415e 100644 --- a/sw/source/filter/html/parcss1.cxx +++ b/sw/source/filter/html/parcss1.cxx @@ -530,11 +530,11 @@ CSS1Token CSS1Parser::GetNextToken() bool bEOFSave = m_bEOF; // first try to parse a hex digit - OUStringBuffer sTmpBuffer(6); + OUStringBuffer sTmpBuffer(8); do { sTmpBuffer.append( m_cNextCh ); m_cNextCh = GetNextChar(); - } while( sTmpBuffer.getLength() < 7 && + } while( sTmpBuffer.getLength() < 9 && ( ('0'<=m_cNextCh && '9'>=m_cNextCh) || ('A'<=m_cNextCh && 'F'>=m_cNextCh) || ('a'<=m_cNextCh && 'f'>=m_cNextCh) ) && @@ -542,7 +542,7 @@ CSS1Token CSS1Parser::GetNextToken() if( sTmpBuffer.getLength()==6 || sTmpBuffer.getLength()==3 ) { - // we found a color in hex + // we found a color in hex (RGB) m_aToken += sTmpBuffer; nRet = CSS1_HEXCOLOR; bNextCh = false; @@ -550,6 +550,26 @@ CSS1Token CSS1Parser::GetNextToken() break; } + if( sTmpBuffer.getLength()==8 ) + { + // we found a color in hex (RGBA) + // we convert it to RGB assuming white background + sal_uInt32 nColor = sTmpBuffer.makeStringAndClear().toUInt32(16); + sal_uInt32 nRed = (nColor & 0xff000000) >> 24; + sal_uInt32 nGreen = (nColor & 0xff0000) >> 16; + sal_uInt32 nBlue = (nColor & 0xff00) >> 8; + double nAlpha = (nColor & 0xff) / 255.0; + nRed = (1 - nAlpha) * 255 + nAlpha * nRed; + nGreen = (1 - nAlpha) * 255 + nAlpha * nGreen; + nBlue = (1 - nAlpha) * 255 + nAlpha * nBlue; + nColor = (nRed << 16) + (nGreen << 8) + nBlue; + m_aToken += OUString::number(nColor, 16); + nRet = CSS1_HEXCOLOR; + bNextCh = false; + + break; + } + // otherwise we try a number m_nInPos = nInPosSave; m_cNextCh = cNextChSave;