LibreOffice Module drawinglayer (master) 1
cropprimitive2d.cxx
Go to the documentation of this file.
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
27#include <utility>
28
29
30using namespace com::sun::star;
31
32
34{
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)
48 {
49 }
50
51 bool CropPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
52 {
53 if(GroupPrimitive2D::operator==(rPrimitive))
54 {
55 const CropPrimitive2D& rCompare = static_cast< const CropPrimitive2D& >(rPrimitive);
56
57 return (getTransformation() == rCompare.getTransformation()
58 && getCropLeft() == rCompare.getCropLeft()
59 && getCropTop() == rCompare.getCropTop()
60 && getCropRight() == rCompare.getCropRight()
61 && getCropBottom() == rCompare.getCropBottom());
62 }
63
64 return false;
65 }
66
68 {
69 if(getChildren().empty())
70 return;
71
72 // get original object scale in unit coordinates (no mirroring)
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 if(aObjectScale.equalZero())
78 return;
79
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);
88
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);
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 if(!aNewRange.overlaps(aUnitRange))
100 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
106
107 aNewTransform.invert();
108
109 // apply crop enlargement in unit coordinates
111 aNewRange.getRange(),
112 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 aNewTransform = getTransformation() * aNewTransform;
119
120 // prepare TransformPrimitive2D with xPrimitive
121 const Primitive2DReference xTransformPrimitive(
123 aNewTransform,
125
126 if(aUnitRange.isInside(aNewRange))
127 {
128 // the new range is completely inside the old range (unit range),
129 // so no masking is needed
130 rVisitor.visit(xTransformPrimitive);
131 }
132 else
133 {
134 // mask with original object's bounds
136 aMaskPolyPolygon.transform(getTransformation());
137
138 // create maskPrimitive with aMaskPolyPolygon and aMaskContentVector
139 const Primitive2DReference xMask(
140 new MaskPrimitive2D(
141 std::move(aMaskPolyPolygon),
142 Primitive2DContainer { xTransformPrimitive }));
143
144 rVisitor.visit(xMask);
145 }
146 }
147
148 // provide unique ID
150 {
152 }
153
154} // end of namespace
155
156/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void transform(const basegfx::B2DHomMatrix &rMatrix)
B2DVector getRange() const
B2DPoint getMinimum() const
bool isInside(const Tuple2D< TYPE > &rTuple) const
bool overlaps(const Range2D &rRange) const
bool equalZero() const
TYPE getX() const
TYPE getY() const
virtual void get2DDecomposition(Primitive2DDecompositionVisitor &rVisitor, const geometry::ViewInformation2D &rViewInformation) const override
local decomposition
const basegfx::B2DHomMatrix & getTransformation() const
data read access
virtual sal_uInt32 getPrimitive2DID() const override
provide unique ID
CropPrimitive2D(Primitive2DContainer &&aChildren, basegfx::B2DHomMatrix aTransformation, double fCropLeft, double fCropTop, double fCropRight, double fCropBottom)
constructor
virtual bool operator==(const BasePrimitive2D &rPrimitive) const override
compare operator
const Primitive2DContainer & getChildren() const
data read access
virtual void visit(const Primitive2DReference &)=0
#define PRIMITIVE2D_ID_CROPPRIMITIVE2D
bool equalZero(const T &rfVal)
B2DHomMatrix createScaleTranslateB2DHomMatrix(double fScaleX, double fScaleY, double fTranslateX, double fTranslateY)
B2DPolygon const & createUnitPolygon()
B2DTuple absolute(const B2DTuple &rTup)
basegfx::B2DHomMatrix maTransformation