calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / drawinglayer / source / primitive2d / cropprimitive2d.cxx
blobda28d9e41663d5b4b2a9bd420c2f5eb91c2c5452
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
27 #include <utility>
30 using namespace com::sun::star;
33 namespace drawinglayer::primitive2d
35 CropPrimitive2D::CropPrimitive2D(
36 Primitive2DContainer&& aChildren,
37 basegfx::B2DHomMatrix aTransformation,
38 double fCropLeft,
39 double fCropTop,
40 double fCropRight,
41 double fCropBottom)
42 : GroupPrimitive2D(std::move(aChildren)),
43 maTransformation(std::move(aTransformation)),
44 mfCropLeft(fCropLeft),
45 mfCropTop(fCropTop),
46 mfCropRight(fCropRight),
47 mfCropBottom(fCropBottom)
51 bool CropPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
53 if(GroupPrimitive2D::operator==(rPrimitive))
55 const CropPrimitive2D& rCompare = static_cast< const CropPrimitive2D& >(rPrimitive);
57 return (getTransformation() == rCompare.getTransformation()
58 && getCropLeft() == rCompare.getCropLeft()
59 && getCropTop() == rCompare.getCropTop()
60 && getCropRight() == rCompare.getCropRight()
61 && getCropBottom() == rCompare.getCropBottom());
64 return false;
67 void CropPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& /*rViewInformation*/) const
69 if(getChildren().empty())
70 return;
72 // get original object scale in unit coordinates (no mirroring)
73 const basegfx::B2DVector aObjectScale(basegfx::absolute(getTransformation() * basegfx::B2DVector(1.0, 1.0)));
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 if(aObjectScale.equalZero())
78 return;
80 // calculate crop distances in unit coordinates. They are already combined with CropScaleFactor, thus
81 // are relative only to object scale
82 const double fBackScaleX(basegfx::fTools::equalZero(aObjectScale.getX()) ? 1.0 : 1.0 / fabs(aObjectScale.getX()));
83 const double fBackScaleY(basegfx::fTools::equalZero(aObjectScale.getY()) ? 1.0 : 1.0 / fabs(aObjectScale.getY()));
84 const double fLeft(getCropLeft() * fBackScaleX);
85 const double fTop(getCropTop() * fBackScaleY);
86 const double fRight(getCropRight() * fBackScaleX);
87 const double fBottom(getCropBottom() * fBackScaleY);
89 // calc new unit range for comparisons; the original range is the unit range
90 const basegfx::B2DRange aUnitRange(0.0, 0.0, 1.0, 1.0);
91 const basegfx::B2DRange aNewRange(
92 -fLeft,
93 -fTop,
94 1.0 + fRight,
95 1.0 + fBottom);
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 if(!aNewRange.overlaps(aUnitRange))
100 return;
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 basegfx::B2DHomMatrix aNewTransform(getTransformation());
107 aNewTransform.invert();
109 // apply crop enlargement in unit coordinates
110 aNewTransform = basegfx::utils::createScaleTranslateB2DHomMatrix(
111 aNewRange.getRange(),
112 aNewRange.getMinimum()) * aNewTransform;
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 aNewTransform = getTransformation() * aNewTransform;
120 // prepare TransformPrimitive2D with xPrimitive
121 const Primitive2DReference xTransformPrimitive(
122 new TransformPrimitive2D(
123 aNewTransform,
124 Primitive2DContainer(getChildren())));
126 if(aUnitRange.isInside(aNewRange))
128 // the new range is completely inside the old range (unit range),
129 // so no masking is needed
130 rVisitor.visit(xTransformPrimitive);
132 else
134 // mask with original object's bounds
135 basegfx::B2DPolyPolygon aMaskPolyPolygon(basegfx::utils::createUnitPolygon());
136 aMaskPolyPolygon.transform(getTransformation());
138 // create maskPrimitive with aMaskPolyPolygon and aMaskContentVector
139 const Primitive2DReference xMask(
140 new MaskPrimitive2D(
141 std::move(aMaskPolyPolygon),
142 Primitive2DContainer { xTransformPrimitive }));
144 rVisitor.visit(xMask);
148 // provide unique ID
149 sal_uInt32 CropPrimitive2D::getPrimitive2DID() const
151 return PRIMITIVE2D_ID_CROPPRIMITIVE2D;
154 } // end of namespace
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */