Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/drawinglayer/source/primitive2d/cropprimitive2d.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <primitive2d/cropprimitive2d.hxx>
21
#include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
22
#include <basegfx/matrix/b2dhommatrix.hxx>
23
#include <basegfx/matrix/b2dhommatrixtools.hxx>
24
#include <drawinglayer/primitive2d/transformprimitive2d.hxx>
25
#include <basegfx/polygon/b2dpolygontools.hxx>
26
#include <drawinglayer/primitive2d/maskprimitive2d.hxx>
27
#include <utility>
28
29
30
using namespace com::sun::star;
31
32
33
namespace drawinglayer::primitive2d
34
{
35
        CropPrimitive2D::CropPrimitive2D(
36
            Primitive2DContainer&& aChildren,
37
            basegfx::B2DHomMatrix aTransformation,
38
            double fCropLeft,
39
            double fCropTop,
40
            double fCropRight,
41
            double fCropBottom)
42
0
        :   GroupPrimitive2D(std::move(aChildren)),
43
0
            maTransformation(std::move(aTransformation)),
44
0
            mfCropLeft(fCropLeft),
45
0
            mfCropTop(fCropTop),
46
0
            mfCropRight(fCropRight),
47
0
            mfCropBottom(fCropBottom)
48
0
        {
49
0
        }
50
51
        bool CropPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
52
0
        {
53
0
            if(GroupPrimitive2D::operator==(rPrimitive))
54
0
            {
55
0
                const CropPrimitive2D& rCompare = static_cast< const CropPrimitive2D& >(rPrimitive);
56
57
0
                return (getTransformation() == rCompare.getTransformation()
58
0
                    && getCropLeft() == rCompare.getCropLeft()
59
0
                    && getCropTop() == rCompare.getCropTop()
60
0
                    && getCropRight() == rCompare.getCropRight()
61
0
                    && getCropBottom() == rCompare.getCropBottom());
62
0
            }
63
64
0
            return false;
65
0
        }
66
67
        void CropPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& /*rViewInformation*/) const
68
0
        {
69
0
            if(getChildren().empty())
70
0
                return;
71
72
            // get original object scale in unit coordinates (no mirroring)
73
0
            const basegfx::B2DVector aObjectScale(basegfx::absolute(getTransformation() * basegfx::B2DVector(1.0, 1.0)));
74
75
            // we handle cropping, so when no width or no height, content will be empty,
76
            // so only do something when we have a width and a height
77
0
            if(aObjectScale.equalZero())
78
0
                return;
79
80
            // calculate crop distances in unit coordinates. They are already combined with CropScaleFactor, thus
81
            // are relative only to object scale
82
0
            const double fBackScaleX(basegfx::fTools::equalZero(aObjectScale.getX()) ? 1.0 : 1.0 / fabs(aObjectScale.getX()));
83
0
            const double fBackScaleY(basegfx::fTools::equalZero(aObjectScale.getY()) ? 1.0 : 1.0 / fabs(aObjectScale.getY()));
84
0
            const double fLeft(getCropLeft() * fBackScaleX);
85
0
            const double fTop(getCropTop() * fBackScaleY);
86
0
            const double fRight(getCropRight() * fBackScaleX);
87
0
            const double fBottom(getCropBottom() * fBackScaleY);
88
89
            // calc new unit range for comparisons; the original range is the unit range
90
0
            const basegfx::B2DRange aUnitRange(0.0, 0.0, 1.0, 1.0);
91
0
            const basegfx::B2DRange aNewRange(
92
0
                -fLeft,
93
0
                -fTop,
94
0
                1.0 + fRight,
95
0
                1.0 + fBottom);
96
97
            // if we have no overlap the crop has removed everything, so we do only
98
            // have to create content if this is not the case
99
0
            if(!aNewRange.overlaps(aUnitRange))
100
0
                return;
101
102
            // create new transform; first take out old transform to get
103
            // to unit coordinates by inverting. Inverting should be flawless
104
            // since we already checked that object size is not zero in X or Y
105
0
            basegfx::B2DHomMatrix aNewTransform(getTransformation());
106
107
0
            aNewTransform.invert();
108
109
            // apply crop enlargement in unit coordinates
110
0
            aNewTransform = basegfx::utils::createScaleTranslateB2DHomMatrix(
111
0
                aNewRange.getRange(),
112
0
                aNewRange.getMinimum()) * aNewTransform;
113
114
            // apply original transformation. Since we have manipulated the crop
115
            // in unit coordinates we do not need to care about mirroring or
116
            // a corrected point for a possible shear or rotation, this all comes for
117
            // free
118
0
            aNewTransform = getTransformation() * aNewTransform;
119
120
            // prepare TransformPrimitive2D with xPrimitive
121
0
            const Primitive2DReference xTransformPrimitive(
122
0
                new TransformPrimitive2D(
123
0
                    aNewTransform,
124
0
                    Primitive2DContainer(getChildren())));
125
126
0
            if(aUnitRange.isInside(aNewRange))
127
0
            {
128
                // the new range is completely inside the old range (unit range),
129
                // so no masking is needed
130
0
                rVisitor.visit(xTransformPrimitive);
131
0
            }
132
0
            else
133
0
            {
134
                // mask with original object's bounds
135
0
                basegfx::B2DPolyPolygon aMaskPolyPolygon(basegfx::utils::createUnitPolygon());
136
0
                aMaskPolyPolygon.transform(getTransformation());
137
138
                // create maskPrimitive with aMaskPolyPolygon and aMaskContentVector
139
0
                const Primitive2DReference xMask(
140
0
                    new MaskPrimitive2D(
141
0
                        std::move(aMaskPolyPolygon),
142
0
                        Primitive2DContainer { xTransformPrimitive }));
143
144
0
                rVisitor.visit(xMask);
145
0
            }
146
0
        }
147
148
        // provide unique ID
149
        sal_uInt32 CropPrimitive2D::getPrimitive2DID() const
150
0
        {
151
0
            return PRIMITIVE2D_ID_CROPPRIMITIVE2D;
152
0
        }
153
154
} // end of namespace
155
156
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */