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 .
21 #include <tools/diagnose_ex.h>
22 #include "clippingfunctor.hxx"
23 #include "transitiontools.hxx"
25 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
26 #include <basegfx/polygon/b2dpolygonclipper.hxx>
27 #include <basegfx/polygon/b2dpolygontools.hxx>
28 #include <basegfx/matrix/b2dhommatrixtools.hxx>
34 ClippingFunctor::ClippingFunctor(const ParametricPolyPolygonSharedPtr
& rPolygon
,
35 const TransitionInfo
& rTransitionInfo
,
36 bool bDirectionForward
,
38 mpParametricPoly( rPolygon
),
39 maStaticTransformation(),
40 mbForwardParameterSweep( true ),
41 mbSubtractPolygon( false ),
42 mbScaleIsotrophically( rTransitionInfo
.mbScaleIsotrophically
),
45 ENSURE_OR_THROW( rPolygon
,
46 "ClippingFunctor::ClippingFunctor(): Invalid parametric polygon" );
48 // maBackgroundRect serves as the minuent when
49 // subtracting a given clip polygon from the
50 // background. To speed up the clipper algo, avoid
51 // actual intersections of the generated
52 // poly-polygon with the minuent - i.e. choose the
53 // polygon to subtract from sufficiently large.
55 // blow up unit rect to (-1,-1),(2,2)
56 // AW: Not needed, just use range
57 // ::basegfx::B2DHomMatrix aMatrix;
58 // aMatrix.scale(3.0,3.0);
59 // aMatrix.translate(-1.0,-1.0);
60 // maBackgroundRect.transform( aMatrix );
62 // extract modification info from maTransitionInfo
65 // perform general transformations _before_ the reverse
66 // mode changes. This allows the Transition table to be
67 // filled more consistently (otherwise, when e.g. rotating
68 // a clip 90 degrees, the REVERSEMETHOD_FLIP_X becomes
69 // REVERSEMETHOD_FLIP_Y instead)
70 if (rTransitionInfo
.mnRotationAngle
!= 0.0 ||
71 rTransitionInfo
.mnScaleX
!= 1.0 ||
72 rTransitionInfo
.mnScaleY
!= 1.0)
74 maStaticTransformation
.translate( -0.5, -0.5 );
75 // apply further transformations:
76 if (rTransitionInfo
.mnRotationAngle
!= 0.0)
78 maStaticTransformation
.rotate(
79 basegfx::deg2rad(rTransitionInfo
.mnRotationAngle
) );
81 if (rTransitionInfo
.mnScaleX
!= 1.0 ||
82 rTransitionInfo
.mnScaleY
!= 1.0)
84 maStaticTransformation
.scale(
85 rTransitionInfo
.mnScaleX
,
86 rTransitionInfo
.mnScaleY
);
88 maStaticTransformation
.translate( 0.5, 0.5 );
91 if( !bDirectionForward
)
93 // Client has requested reversed
94 // direction. Apply TransitionInfo's choice
96 switch( rTransitionInfo
.meReverseMethod
)
101 "TransitionFactory::TransitionFactory(): Unexpected reverse method" );
104 case TransitionInfo::REVERSEMETHOD_IGNORE
:
107 case TransitionInfo::REVERSEMETHOD_INVERT_SWEEP
:
108 mbForwardParameterSweep
= !mbForwardParameterSweep
;
111 case TransitionInfo::REVERSEMETHOD_SUBTRACT_POLYGON
:
112 mbSubtractPolygon
= !mbSubtractPolygon
;
115 case TransitionInfo::REVERSEMETHOD_SUBTRACT_AND_INVERT
:
116 mbForwardParameterSweep
= !mbForwardParameterSweep
;
117 mbSubtractPolygon
= !mbSubtractPolygon
;
120 case TransitionInfo::REVERSEMETHOD_ROTATE_180
:
121 maStaticTransformation
= basegfx::tools::createRotateAroundPoint(0.5, 0.5, M_PI
)
122 * maStaticTransformation
;
125 case TransitionInfo::REVERSEMETHOD_FLIP_X
:
126 maStaticTransformation
= basegfx::tools::createScaleTranslateB2DHomMatrix(-1.0, 1.0, 1.0, 0.0)
127 * maStaticTransformation
;
131 case TransitionInfo::REVERSEMETHOD_FLIP_Y
:
132 maStaticTransformation
= basegfx::tools::createScaleTranslateB2DHomMatrix(1.0, -1.0, 0.0, 1.0)
133 * maStaticTransformation
;
141 // client has requested 'out' mode. Apply
142 // TransitionInfo's method of choice
143 if( rTransitionInfo
.mbOutInvertsSweep
)
144 mbForwardParameterSweep
= !mbForwardParameterSweep
;
146 mbSubtractPolygon
= !mbSubtractPolygon
;
150 ::basegfx::B2DPolyPolygon
ClippingFunctor::operator()( double nValue
,
151 const ::basegfx::B2DSize
& rTargetSize
)
153 // modify clip polygon according to static
154 // transformation plus current shape size
155 ::basegfx::B2DHomMatrix
aMatrix( maStaticTransformation
);
157 // retrieve current clip polygon
158 ::basegfx::B2DPolyPolygon aClipPoly
= (*mpParametricPoly
)(
159 mbForwardParameterSweep
? nValue
: 1.0 - nValue
);
161 // TODO(Q4): workaround here, better be fixed in cppcanvas
162 if (aClipPoly
.count() == 0)
163 aClipPoly
.append( basegfx::B2DPolygon() );
168 // currently, clipper cannot cope with curves. Subdivide first
169 // AW: Should be no longer necessary; clipping tools are now bezier-safe
170 // if( aClipPoly.areControlPointsUsed() )
171 // aClipPoly = ::basegfx::tools::adaptiveSubdivideByAngle(aClipPoly);
173 if( mbSubtractPolygon
)
175 // subtract given polygon from background
176 // rect. Do that before any transformations.
178 // calc maBackgroundRect \ aClipPoly
179 // =================================
182 // use a range with fixed size (-1,-1),(2,2)
183 const basegfx::B2DRange
aBackgroundRange(-1, -1, 2, 2);
184 const basegfx::B2DRange
aClipPolyRange(aClipPoly
.getB2DRange());
186 if(aBackgroundRange
.isInside(aClipPolyRange
))
188 // combine polygons; make the clip polygon the hole
189 aClipPoly
= ::basegfx::tools::correctOrientations(aClipPoly
);
191 aClipPoly
.insert(0, basegfx::tools::createPolygonFromRect(aBackgroundRange
));
195 // when not completely inside aBackgroundRange clipping is needed
196 // subtract aClipPoly from aBackgroundRange
197 const basegfx::B2DPolyPolygon
aBackgroundPolyPoly(basegfx::tools::createPolygonFromRect(aBackgroundRange
));
198 aClipPoly
= basegfx::tools::solvePolygonOperationDiff(aBackgroundPolyPoly
, aClipPoly
);
202 // scale polygon up to current shape size
203 if( mbScaleIsotrophically
)
205 const double nScale( ::std::max( rTargetSize
.getX(),
206 rTargetSize
.getY() ) );
207 aMatrix
.scale( nScale
, nScale
);
208 aMatrix
.translate( -(nScale
-rTargetSize
.getX())/2.0,
209 -(nScale
-rTargetSize
.getY())/2.0 );
213 aMatrix
.scale( rTargetSize
.getX(),
214 rTargetSize
.getY() );
217 // apply cumulative transformation to clip polygon
218 aClipPoly
.transform( aMatrix
);
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */