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 81c6e01e5ba9d46963ae3323804ab8379b864cc3
Author:     Andras Timar <andras.ti...@collabora.com>
AuthorDate: Wed Jan 4 14:34:49 2023 +0100
Commit:     Andras Timar <andras.ti...@collabora.com>
CommitDate: Wed Jan 4 18:17:54 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/+/145041
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Andras Timar <andras.ti...@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 00e2ec99191f..e4547d02a008 100644
--- a/sw/qa/extras/htmlimport/htmlimport.cxx
+++ b/sw/qa/extras/htmlimport/htmlimport.cxx
@@ -536,6 +536,18 @@ CPPUNIT_TEST_FIXTURE(HtmlImportTest, testUTF16_nonBMP)
                          getParagraph(1)->getString());
 }
 
+CPPUNIT_TEST_FIXTURE(HtmlImportTest, testRGBAColor)
+{
+    load(mpTestDocumentPath, "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 e0b20ef4168e..518604bc1bc5 100644
--- a/sw/source/filter/html/parcss1.cxx
+++ b/sw/source/filter/html/parcss1.cxx
@@ -529,11 +529,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) ) &&
@@ -541,7 +541,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;
@@ -549,6 +549,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;

Reply via email to