tdf#42112 Add support for Custom Line Cap

original patch updated by Chris Sherlock

Change-Id: Ie68c3cc40d2c7263a0f786a973da77b00e4cbeb8
Reviewed-on: https://gerrit.libreoffice.org/82564
Tested-by: Jenkins
Reviewed-by: Bartosz Kosiorek <gang65@poczta.onet.pl>
(cherry picked from commit d15518584a3197e4b8318d0176352a0584f42167)
Reviewed-on: https://gerrit.libreoffice.org/83297
Reviewed-by: Chris Sherlock <chris.sherlock79@gmail.com>
diff --git a/drawinglayer/source/tools/emfpcustomlinecap.cxx b/drawinglayer/source/tools/emfpcustomlinecap.cxx
index 82e8f94..98af0f5 100644
--- a/drawinglayer/source/tools/emfpcustomlinecap.cxx
+++ b/drawinglayer/source/tools/emfpcustomlinecap.cxx
@@ -67,13 +67,7 @@ namespace emfplushelper
        path.Read(s, pathFlags);
        polygon = path.GetPolygon(rR, false);
        mbIsFilled = bFill;

        // transformation to convert the path to what LibreOffice
        // expects
        B2DHomMatrix aMatrix;
        aMatrix.scale(1.0, -1.0);
        polygon.transform(aMatrix);
    };
    }

    void EMFPCustomLineCap::Read(SvStream& s, EmfPlusHelperData const & rR)
    {
diff --git a/drawinglayer/source/tools/emfphelperdata.cxx b/drawinglayer/source/tools/emfphelperdata.cxx
index b72c026..4d7ec51 100644
--- a/drawinglayer/source/tools/emfphelperdata.cxx
+++ b/drawinglayer/source/tools/emfphelperdata.cxx
@@ -17,6 +17,7 @@
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "emfpcustomlinecap.hxx"
#include "emfphelperdata.hxx"
#include "emfpbrush.hxx"
#include "emfppen.hxx"
@@ -476,6 +477,96 @@ namespace emfplushelper
                                pen->GetColor().GetTransparency() / 255.0));
            }

            if ((pen->penDataFlags & 0x00000800) && (pen->customStartCap->polygon.begin()->count() > 1))
            {
                SAL_WARN("drawinglayer", "EMF+\tCustom Start Line Cap");
                ::basegfx::B2DPolyPolygon startCapPolygon(pen->customStartCap->polygon);

                // get the gradient of the first line in the polypolygon
                double x1 = polygon.begin()->getB2DPoint(0).getX();
                double y1 = polygon.begin()->getB2DPoint(0).getY();
                double x2 = polygon.begin()->getB2DPoint(1).getX();
                double y2 = polygon.begin()->getB2DPoint(1).getY();

                if ((x2 - x1) != 0)
                {
                    double gradient = (y2 - y1) / (x2 - x1);

                    // now we get the angle that we need to rotate the arrow by
                    double angle = (M_PI / 2) - atan(gradient);

                    // rotate the arrow
                    startCapPolygon.transform(basegfx::utils::createRotateB2DHomMatrix(angle));
                }

                startCapPolygon.transform(maMapTransform);

                basegfx::B2DHomMatrix tran(pen->penWidth, 0.0, polygon.begin()->getB2DPoint(0).getX(),
                                           0.0, pen->penWidth, polygon.begin()->getB2DPoint(0).getY());
                startCapPolygon.transform(tran);

                if (pen->customStartCap->mbIsFilled)
                {
                    mrTargetHolders.Current().append(
                                std::make_unique<drawinglayer::primitive2d::PolyPolygonColorPrimitive2D>(
                                    startCapPolygon,
                                    pen->GetColor().getBColor()));
                }
                else
                {
                    mrTargetHolders.Current().append(
                                std::make_unique<drawinglayer::primitive2d::PolyPolygonStrokePrimitive2D>(
                                    startCapPolygon,
                                    lineAttribute,
                                    aStrokeAttribute));
                }
            }

            if ((pen->penDataFlags & 0x00001000) && (pen->customEndCap->polygon.begin()->count() > 1))
            {
                SAL_WARN("drawinglayer", "EMF+\tCustom End Line Cap");

                ::basegfx::B2DPolyPolygon endCapPolygon(pen->customEndCap->polygon);

                // get the gradient of the first line in the polypolygon
                double x1 = polygon.begin()->getB2DPoint(polygon.begin()->count() - 1).getX();
                double y1 = polygon.begin()->getB2DPoint(polygon.begin()->count() - 1).getY();
                double x2 = polygon.begin()->getB2DPoint(polygon.begin()->count() - 2).getX();
                double y2 = polygon.begin()->getB2DPoint(polygon.begin()->count() - 2).getY();

                if ((x2 - x1) != 0)
                {
                    double gradient = (y2 - y1) / (x2 - x1);

                    // now we get the angle that we need to rotate the arrow by
                    double angle = (M_PI / 2) - atan(gradient);

                    // rotate the arrow
                    endCapPolygon.transform(basegfx::utils::createRotateB2DHomMatrix(angle));
                }

                endCapPolygon.transform(maMapTransform);
                basegfx::B2DHomMatrix tran(pen->penWidth, 0.0, polygon.begin()->getB2DPoint(polygon.begin()->count() - 1).getX(),
                                           0.0, pen->penWidth, polygon.begin()->getB2DPoint(polygon.begin()->count() - 1).getY());
                endCapPolygon.transform(tran);

                if (pen->customEndCap->mbIsFilled)
                {
                    mrTargetHolders.Current().append(
                                std::make_unique<drawinglayer::primitive2d::PolyPolygonColorPrimitive2D>(
                                    endCapPolygon,
                                    pen->GetColor().getBColor()));
                }
                else
                {
                    mrTargetHolders.Current().append(
                                std::make_unique<drawinglayer::primitive2d::PolyPolygonStrokePrimitive2D>(
                                    endCapPolygon,
                                    lineAttribute,
                                    aStrokeAttribute));
                }
            }

            mrPropertyHolders.Current().setLineColor(pen->GetColor().getBColor());
            mrPropertyHolders.Current().setLineColorActive(true);
            mrPropertyHolders.Current().setFillColorActive(false);