1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
29 using namespace com::sun::star
;
32 namespace drawinglayer::primitive2d
34 CropPrimitive2D::CropPrimitive2D(
35 const Primitive2DContainer
& rChildren
,
36 const basegfx::B2DHomMatrix
& rTransformation
,
41 : GroupPrimitive2D(rChildren
),
42 maTransformation(rTransformation
),
43 mfCropLeft(fCropLeft
),
45 mfCropRight(fCropRight
),
46 mfCropBottom(fCropBottom
)
50 bool CropPrimitive2D::operator==(const BasePrimitive2D
& rPrimitive
) const
52 if(GroupPrimitive2D::operator==(rPrimitive
))
54 const CropPrimitive2D
& rCompare
= static_cast< const CropPrimitive2D
& >(rPrimitive
);
56 return (getTransformation() == rCompare
.getTransformation()
57 && getCropLeft() == rCompare
.getCropLeft()
58 && getCropTop() == rCompare
.getCropTop()
59 && getCropRight() == rCompare
.getCropRight()
60 && getCropBottom() == rCompare
.getCropBottom());
66 void CropPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor
& rVisitor
, const geometry::ViewInformation2D
& /*rViewInformation*/) const
68 if(getChildren().empty())
71 // get original object scale in unit coordinates (no mirroring)
72 const basegfx::B2DVector
aObjectScale(basegfx::absolute(getTransformation() * basegfx::B2DVector(1.0, 1.0)));
74 // we handle cropping, so when no width or no height, content will be empty,
75 // so only do something when we have a width and a height
76 if(aObjectScale
.equalZero())
79 // calculate crop distances in unit coordinates. They are already combined with CropScaleFactor, thus
80 // are relative only to object scale
81 const double fBackScaleX(basegfx::fTools::equalZero(aObjectScale
.getX()) ? 1.0 : 1.0 / fabs(aObjectScale
.getX()));
82 const double fBackScaleY(basegfx::fTools::equalZero(aObjectScale
.getY()) ? 1.0 : 1.0 / fabs(aObjectScale
.getY()));
83 const double fLeft(getCropLeft() * fBackScaleX
);
84 const double fTop(getCropTop() * fBackScaleY
);
85 const double fRight(getCropRight() * fBackScaleX
);
86 const double fBottom(getCropBottom() * fBackScaleY
);
88 // calc new unit range for comparisons; the original range is the unit range
89 const basegfx::B2DRange
aUnitRange(0.0, 0.0, 1.0, 1.0);
90 const basegfx::B2DRange
aNewRange(
96 // if we have no overlap the crop has removed everything, so we do only
97 // have to create content if this is not the case
98 if(!aNewRange
.overlaps(aUnitRange
))
101 // create new transform; first take out old transform to get
102 // to unit coordinates by inverting. Inverting should be flawless
103 // since we already checked that object size is not zero in X or Y
104 basegfx::B2DHomMatrix
aNewTransform(getTransformation());
106 aNewTransform
.invert();
108 // apply crop enlargement in unit coordinates
109 aNewTransform
= basegfx::utils::createScaleTranslateB2DHomMatrix(
110 aNewRange
.getRange(),
111 aNewRange
.getMinimum()) * aNewTransform
;
113 // apply original transformation. Since we have manipulated the crop
114 // in unit coordinates we do not need to care about mirroring or
115 // a corrected point for a possible shear or rotation, this all comes for
117 aNewTransform
= getTransformation() * aNewTransform
;
119 // prepare TransformPrimitive2D with xPrimitive
120 const Primitive2DReference
xTransformPrimitive(
121 new TransformPrimitive2D(
125 if(aUnitRange
.isInside(aNewRange
))
127 // the new range is completely inside the old range (unit range),
128 // so no masking is needed
129 rVisitor
.append(xTransformPrimitive
);
133 // mask with original object's bounds
134 basegfx::B2DPolyPolygon
aMaskPolyPolygon(basegfx::utils::createUnitPolygon());
135 aMaskPolyPolygon
.transform(getTransformation());
137 // create maskPrimitive with aMaskPolyPolygon and aMaskContentVector
138 const Primitive2DReference
xMask(
141 Primitive2DContainer
{ xTransformPrimitive
}));
143 rVisitor
.append(xMask
);
148 ImplPrimitive2DIDBlock(CropPrimitive2D
, PRIMITIVE2D_ID_CROPPRIMITIVE2D
)
150 } // end of namespace
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */