oox: add PPTX import/export for semi-transparent shape text

And start a drawingml test suite in oox, so the test and the tested code
is close to each other (just like how it's done in chart2/ already).

Change-Id: I9a2810691f12604d240e4394e6a5ff4e7f52f1c1
Reviewed-on: https://gerrit.libreoffice.org/83412
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
diff --git a/oox/CppunitTest_oox_drawingml.mk b/oox/CppunitTest_oox_drawingml.mk
new file mode 100644
index 0000000..dd02880
--- /dev/null
+++ b/oox/CppunitTest_oox_drawingml.mk
@@ -0,0 +1,45 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#*************************************************************************
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
#*************************************************************************

$(eval $(call gb_CppunitTest_CppunitTest,oox_drawingml))

$(eval $(call gb_CppunitTest_use_externals,oox_drawingml,\
	boost_headers \
))

$(eval $(call gb_CppunitTest_add_exception_objects,oox_drawingml, \
    oox/qa/unit/drawingml \
))

$(eval $(call gb_CppunitTest_use_libraries,oox_drawingml, \
    comphelper \
    cppu \
    oox \
    sal \
    test \
    unotest \
    utl \
))

$(eval $(call gb_CppunitTest_use_sdk_api,oox_drawingml))

$(eval $(call gb_CppunitTest_use_ure,oox_drawingml))
$(eval $(call gb_CppunitTest_use_vcl,oox_drawingml))

$(eval $(call gb_CppunitTest_use_rdb,oox_drawingml,services))

$(eval $(call gb_CppunitTest_use_custom_headers,oox_drawingml,\
	officecfg/registry \
))

$(eval $(call gb_CppunitTest_use_configuration,oox_drawingml))

# vim: set noet sw=4 ts=4:
diff --git a/oox/Module_oox.mk b/oox/Module_oox.mk
index 636f9f6..6078a7e 100644
--- a/oox/Module_oox.mk
+++ b/oox/Module_oox.mk
@@ -26,6 +26,7 @@ $(eval $(call gb_Module_add_check_targets,oox,\
	CppunitTest_oox_vba_encryption \
	CppunitTest_oox_crypto \
	CppunitTest_oox_mathml \
	CppunitTest_oox_drawingml \
))

# vim: set noet sw=4 ts=4:
diff --git a/oox/qa/unit/data/transparent-text.pptx b/oox/qa/unit/data/transparent-text.pptx
new file mode 100644
index 0000000..b7b3ede
--- /dev/null
+++ b/oox/qa/unit/data/transparent-text.pptx
Binary files differ
diff --git a/oox/qa/unit/drawingml.cxx b/oox/qa/unit/drawingml.cxx
new file mode 100644
index 0000000..e3c455c
--- /dev/null
+++ b/oox/qa/unit/drawingml.cxx
@@ -0,0 +1,100 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <test/bootstrapfixture.hxx>
#include <unotest/macros_test.hxx>

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
#include <com/sun/star/embed/XStorage.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/frame/XStorable.hpp>

#include <comphelper/embeddedobjectcontainer.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertyvalue.hxx>
#include <comphelper/scopeguard.hxx>
#include <comphelper/storagehelper.hxx>
#include <unotools/mediadescriptor.hxx>
#include <unotools/tempfile.hxx>

using namespace ::com::sun::star;

/// oox drawingml tests.
class OoxDrawingmlTest : public test::BootstrapFixture, public unotest::MacrosTest
{
private:
    uno::Reference<uno::XComponentContext> mxComponentContext;
    uno::Reference<lang::XComponent> mxComponent;

public:
    void setUp() override;
    void tearDown() override;
    uno::Reference<lang::XComponent>& getComponent() { return mxComponent; }
    void loadAndReload(const OUString& rURL, const OUString& rFilterName);
};

void OoxDrawingmlTest::setUp()
{
    test::BootstrapFixture::setUp();

    mxComponentContext.set(comphelper::getComponentContext(getMultiServiceFactory()));
    mxDesktop.set(frame::Desktop::create(mxComponentContext));
}

void OoxDrawingmlTest::tearDown()
{
    if (mxComponent.is())
        mxComponent->dispose();

    test::BootstrapFixture::tearDown();
}

void OoxDrawingmlTest::loadAndReload(const OUString& rURL, const OUString& rFilterName)
{
    mxComponent = loadFromDesktop(rURL);
    uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
    utl::MediaDescriptor aMediaDescriptor;
    aMediaDescriptor["FilterName"] <<= rFilterName;
    utl::TempFile aTempFile;
    xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
    mxComponent->dispose();
    validate(aTempFile.GetFileName(), test::OOXML);
    mxComponent = loadFromDesktop(aTempFile.GetURL());
}

char const DATA_DIRECTORY[] = "/oox/qa/unit/data/";

CPPUNIT_TEST_FIXTURE(OoxDrawingmlTest, testTransparentText)
{
    OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "transparent-text.pptx";
    loadAndReload(aURL, "Impress Office Open XML");

    uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier(getComponent(), uno::UNO_QUERY);
    uno::Reference<drawing::XDrawPage> xDrawPage(xDrawPagesSupplier->getDrawPages()->getByIndex(0),
                                                 uno::UNO_QUERY);
    uno::Reference<container::XEnumerationAccess> xShape(xDrawPage->getByIndex(0), uno::UNO_QUERY);
    uno::Reference<container::XEnumerationAccess> xParagraph(
        xShape->createEnumeration()->nextElement(), uno::UNO_QUERY);
    uno::Reference<beans::XPropertySet> xPortion(xParagraph->createEnumeration()->nextElement(),
                                                 uno::UNO_QUERY);

    sal_Int16 nTransparency = 0;
    xPortion->getPropertyValue("CharTransparence") >>= nTransparency;

    // Without the accompanying fix in place, this test would have failed with:
    // - Expected: 75
    // - Actual  : 0
    // i.e. the transparency of the character color was lost on import/export.
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int16>(75), nTransparency);
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/oox/source/drawingml/textcharacterproperties.cxx b/oox/source/drawingml/textcharacterproperties.cxx
index e2f5cc6..de47058 100644
--- a/oox/source/drawingml/textcharacterproperties.cxx
+++ b/oox/source/drawingml/textcharacterproperties.cxx
@@ -109,7 +109,15 @@ void TextCharacterProperties::pushToPropMap( PropertyMap& rPropMap, const XmlFil
    }

    if ( maFillProperties.moFillType.has() )
        rPropMap.setProperty( PROP_CharColor, maFillProperties.getBestSolidColor().getColor( rFilter.getGraphicHelper() ));
    {
        Color aColor = maFillProperties.getBestSolidColor();
        rPropMap.setProperty(PROP_CharColor, aColor.getColor(rFilter.getGraphicHelper()));

        if (aColor.hasTransparency())
        {
            rPropMap.setProperty(PROP_CharTransparence, aColor.getTransparency());
        }
    }

    if( moLang.has() && !moLang.get().isEmpty() )
    {
diff --git a/oox/source/export/drawingml.cxx b/oox/source/export/drawingml.cxx
index 2cc160d..5dc6962 100644
--- a/oox/source/export/drawingml.cxx
+++ b/oox/source/export/drawingml.cxx
@@ -1883,13 +1883,23 @@ void DrawingML::WriteRunProperties( const Reference< XPropertySet >& rRun, bool 
        ::Color color( *o3tl::doAccess<sal_uInt32>(mAny) );
        SAL_INFO("oox.shape", "run color: " << sal_uInt32(color) << " auto: " << sal_uInt32(COL_AUTO));

        // WriteSolidFill() handles MAX_PERCENT as "no transparency".
        sal_Int32 nTransparency = MAX_PERCENT;
        if (rXPropSet->getPropertySetInfo()->hasPropertyByName("CharTransparence"))
        {
            rXPropSet->getPropertyValue("CharTransparence") >>= nTransparency;
            // UNO scale is 0..100, OOXML scale is 0..100000; also UNO tracks transparency, OOXML
            // tracks opacity.
            nTransparency = MAX_PERCENT - (nTransparency * PER_PERCENT);
        }

        // tdf#104219 In LibreOffice and MS Office, there are two types of colors:
        // Automatic and Fixed. OOXML is setting automatic color, by not providing color.
        if( color != COL_AUTO )
        {
            color.SetTransparency(0);
            // TODO: special handle embossed/engraved
            WriteSolidFill( color );
            WriteSolidFill(color, nTransparency);
        }
    }

diff --git a/oox/source/token/properties.txt b/oox/source/token/properties.txt
index db44e17..a319b2b 100644
--- a/oox/source/token/properties.txt
+++ b/oox/source/token/properties.txt
@@ -82,6 +82,7 @@ CharPostureComplex
CharShadowed
CharStrikeout
CharStyleName
CharTransparence
CharUnderline
CharUnderlineColor
CharUnderlineHasColor