tdf#48062: Add support for feComposite filter

At the moment, only 'over' operator is supported

Change-Id: If21c9ba39b3cd0b772ea27165cda7ae40fb42d60
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164765
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
diff --git a/svgio/Library_svgio.mk b/svgio/Library_svgio.mk
index e1b0e55a..7bc518c 100644
--- a/svgio/Library_svgio.mk
+++ b/svgio/Library_svgio.mk
@@ -62,6 +62,7 @@ $(eval $(call gb_Library_add_exception_objects,svgio,\
    svgio/source/svgreader/svganode \
    svgio/source/svgreader/svgfeblendnode \
    svgio/source/svgreader/svgfecolormatrixnode \
    svgio/source/svgreader/svgfecompositenode \
    svgio/source/svgreader/svgfedropshadownode \
    svgio/source/svgreader/svgfefloodnode \
    svgio/source/svgreader/svgfeimagenode \
diff --git a/svgio/inc/svgfecompositenode.hxx b/svgio/inc/svgfecompositenode.hxx
new file mode 100644
index 0000000..44cef84
--- /dev/null
+++ b/svgio/inc/svgfecompositenode.hxx
@@ -0,0 +1,46 @@
/* -*- 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#pragma once

#include "svgfilternode.hxx"
#include "svgstyleattributes.hxx"

namespace svgio::svgreader
{
class SvgFeCompositeNode : public SvgFilterNode
{
private:
    OUString maIn;
    OUString maIn2;
    OUString maResult;

public:
    SvgFeCompositeNode(SvgDocument& rDocument, SvgNode* pParent);
    virtual ~SvgFeCompositeNode() override;

    virtual void parseAttribute(SVGToken aSVGToken, const OUString& aContent) override;

    void apply(drawinglayer::primitive2d::Primitive2DContainer& rTarget,
               const SvgFilterNode* pParent) const override;
};

} // end of namespace svgio::svgreader

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svgio/inc/svgtoken.hxx b/svgio/inc/svgtoken.hxx
index 5bc79f6..aa2d2a74 100644
--- a/svgio/inc/svgtoken.hxx
+++ b/svgio/inc/svgtoken.hxx
@@ -84,6 +84,7 @@ namespace svgio::svgreader
            ClipPathProperty,
            FeBlend,
            FeColorMatrix,
            FeComposite,
            FeDropShadow,
            FeFlood,
            FeImage,
diff --git a/svgio/qa/cppunit/SvgImportTest.cxx b/svgio/qa/cppunit/SvgImportTest.cxx
index 05bf498..1d875e2b 100644
--- a/svgio/qa/cppunit/SvgImportTest.cxx
+++ b/svgio/qa/cppunit/SvgImportTest.cxx
@@ -225,6 +225,32 @@ CPPUNIT_TEST_FIXTURE(Test, testFeColorMatrix)
    assertXPath(pDocument, "/primitive2D/transform/mask/modifiedColor[4]"_ostr, "modifier"_ostr, "luminance_to_alpha");
}

CPPUNIT_TEST_FIXTURE(Test, testFilterFeComposite)
{
    Primitive2DSequence aSequence = parseSvg(u"/svgio/qa/cppunit/data/filterFeComposite.svg");
    CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(aSequence.getLength()));

    drawinglayer::Primitive2dXmlDump dumper;
    xmlDocUniquePtr pDocument = dumper.dumpAndParse(aSequence);

    CPPUNIT_ASSERT (pDocument);

    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[1]"_ostr, "color"_ostr, "#ff0000");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[1]/polypolygon"_ostr, "height"_ostr, "100");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[1]/polypolygon"_ostr, "width"_ostr, "100");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[1]/polypolygon"_ostr, "minx"_ostr, "70");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[1]/polypolygon"_ostr, "miny"_ostr, "70");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[1]/polypolygon"_ostr, "maxx"_ostr, "170");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[1]/polypolygon"_ostr, "maxy"_ostr, "170");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[2]"_ostr, "color"_ostr, "#6ab150");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[2]/polypolygon"_ostr, "height"_ostr, "100");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[2]/polypolygon"_ostr, "width"_ostr, "100");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[2]/polypolygon"_ostr, "minx"_ostr, "30");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[2]/polypolygon"_ostr, "miny"_ostr, "30");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[2]/polypolygon"_ostr, "maxx"_ostr, "130");
    assertXPath(pDocument, "/primitive2D/transform/transform/polypolygoncolor[2]/polypolygon"_ostr, "maxy"_ostr, "130");
}

CPPUNIT_TEST_FIXTURE(Test, testFilterFeGaussianBlur)
{
    Primitive2DSequence aSequenceTdf132246 = parseSvg(u"/svgio/qa/cppunit/data/filterFeGaussianBlur.svg");
diff --git a/svgio/qa/cppunit/data/filterFeComposite.svg b/svgio/qa/cppunit/data/filterFeComposite.svg
new file mode 100644
index 0000000..0662eaa
--- /dev/null
+++ b/svgio/qa/cppunit/data/filterFeComposite.svg
@@ -0,0 +1,10 @@
<svg width="100" height="120" viewBox="0 0 200 240" xmlns="http://www.w3.org/2000/svg">
  <defs>
  <filter id="filter" filterUnits="userSpaceOnUse" x="0" y="0" width="100%" height="100%">
    <feFlood x="30" y="30" width="100" height="100" flood-color="#6ab150" flood-opacity="1" result="img1"></feFlood>
    <feFlood x="70" y="70" width="100" height="100" flood-color="#ff0000" flood-opacity="1" result="img2"></feFlood>
    <feComposite in="img1" in2="img2" operator="over"></feComposite>
  </filter>
  </defs>
  <use style="filter: url(#filter)"></use>
</svg>
diff --git a/svgio/source/svgreader/svgdocumenthandler.cxx b/svgio/source/svgreader/svgdocumenthandler.cxx
index fcce219..6e31aaf 100644
--- a/svgio/source/svgreader/svgdocumenthandler.cxx
+++ b/svgio/source/svgreader/svgdocumenthandler.cxx
@@ -43,6 +43,7 @@
#include <svgclippathnode.hxx>
#include <svgfeblendnode.hxx>
#include <svgfecolormatrixnode.hxx>
#include <svgfecompositenode.hxx>
#include <svgfedropshadownode.hxx>
#include <svgfefloodnode.hxx>
#include <svgfeimagenode.hxx>
@@ -392,6 +393,13 @@ namespace
                    mpTarget->parseAttributes(xAttribs);
                    break;
                }
                case SVGToken::FeComposite:
                {
                    /// new node for feComposite
                    mpTarget = new SvgFeCompositeNode(maDocument, mpTarget);
                    mpTarget->parseAttributes(xAttribs);
                    break;
                }
                case SVGToken::FeDropShadow:
                {
                    /// new node for feDropShadow
diff --git a/svgio/source/svgreader/svgfecompositenode.cxx b/svgio/source/svgreader/svgfecompositenode.cxx
new file mode 100644
index 0000000..b3418fc
--- /dev/null
+++ b/svgio/source/svgreader/svgfecompositenode.cxx
@@ -0,0 +1,83 @@
/*
 * 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <svgfecompositenode.hxx>
#include <o3tl/string_view.hxx>

namespace svgio::svgreader
{
SvgFeCompositeNode::SvgFeCompositeNode(SvgDocument& rDocument, SvgNode* pParent)
    : SvgFilterNode(SVGToken::FeComposite, rDocument, pParent)
{
}

SvgFeCompositeNode::~SvgFeCompositeNode() {}

void SvgFeCompositeNode::parseAttribute(SVGToken aSVGToken, const OUString& aContent)
{
    // parse own
    switch (aSVGToken)
    {
        case SVGToken::Style:
        {
            readLocalCssStyle(aContent);
            break;
        }
        case SVGToken::In:
        {
            maIn = aContent.trim();
            break;
        }
        case SVGToken::In2:
        {
            maIn2 = aContent.trim();
            break;
        }
        case SVGToken::Result:
        {
            maResult = aContent.trim();
            break;
        }
        default:
        {
            break;
        }
    }
}

void SvgFeCompositeNode::apply(drawinglayer::primitive2d::Primitive2DContainer& rTarget,
                               const SvgFilterNode* pParent) const
{
    if (const drawinglayer::primitive2d::Primitive2DContainer* rSource2
        = pParent->findGraphicSource(maIn2))
    {
        rTarget = *rSource2;
    }

    if (const drawinglayer::primitive2d::Primitive2DContainer* rSource
        = pParent->findGraphicSource(maIn))
    {
        rTarget.append(*rSource);
    }

    pParent->addGraphicSourceToMapper(maResult, rTarget);
}

} // end of namespace svgio::svgreader

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svgio/source/svgreader/svgtoken.cxx b/svgio/source/svgreader/svgtoken.cxx
index f2cee5b..04b3f9f 100644
--- a/svgio/source/svgreader/svgtoken.cxx
+++ b/svgio/source/svgreader/svgtoken.cxx
@@ -82,6 +82,7 @@ constexpr auto aSVGTokenMap = frozen::make_unordered_map<std::u16string_view, SV
    { u"clip-path", SVGToken::ClipPathProperty },
    { u"feBlend", SVGToken::FeBlend },
    { u"feColorMatrix", SVGToken::FeColorMatrix },
    { u"feComposite", SVGToken::FeComposite },
    { u"feDropShadow", SVGToken::FeDropShadow },
    { u"feFlood", SVGToken::FeFlood },
    { u"feImage", SVGToken::FeImage },