Line data Source code
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 :
21 : #include <canvas/debug.hxx>
22 : #include <tools/diagnose_ex.h>
23 : #include "clippingfunctor.hxx"
24 : #include "transitiontools.hxx"
25 :
26 : #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
27 : #include <basegfx/polygon/b2dpolygonclipper.hxx>
28 : #include <basegfx/polygon/b2dpolygontools.hxx>
29 : #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
30 : #include <basegfx/matrix/b2dhommatrixtools.hxx>
31 :
32 : namespace slideshow
33 : {
34 : namespace internal
35 : {
36 0 : ClippingFunctor::ClippingFunctor(const ParametricPolyPolygonSharedPtr& rPolygon,
37 : const TransitionInfo& rTransitionInfo,
38 : bool bDirectionForward,
39 : bool bModeIn ) :
40 : mpParametricPoly( rPolygon ),
41 : maStaticTransformation(),
42 : mbForwardParameterSweep( true ),
43 : mbSubtractPolygon( false ),
44 : mbScaleIsotrophically( rTransitionInfo.mbScaleIsotrophically ),
45 0 : mbFlip(false)
46 : {
47 0 : ENSURE_OR_THROW( rPolygon,
48 : "ClippingFunctor::ClippingFunctor(): Invalid parametric polygon" );
49 :
50 : // maBackgroundRect serves as the minuent when
51 : // subtracting a given clip polygon from the
52 : // background. To speed up the clipper algo, avoid
53 : // actual intersections of the generated
54 : // poly-polygon with the minuent - i.e. choose the
55 : // polygon to subtract from sufficiently large.
56 :
57 : // blow up unit rect to (-1,-1),(2,2)
58 : // AW: Not needed, just use range
59 : // ::basegfx::B2DHomMatrix aMatrix;
60 : // aMatrix.scale(3.0,3.0);
61 : // aMatrix.translate(-1.0,-1.0);
62 : // maBackgroundRect.transform( aMatrix );
63 :
64 : // extract modification info from maTransitionInfo
65 : // -----------------------------------------------
66 :
67 : // perform general transformations _before_ the reverse
68 : // mode changes. This allows the Transition table to be
69 : // filled more constitently (otherwise, when e.g. rotating
70 : // a clip 90 degrees, the REVERSEMETHOD_FLIP_X becomes
71 : // REVERSEMETHOD_FLIP_Y instead)
72 0 : if (rTransitionInfo.mnRotationAngle != 0.0 ||
73 : rTransitionInfo.mnScaleX != 1.0 ||
74 : rTransitionInfo.mnScaleY != 1.0)
75 : {
76 0 : maStaticTransformation.translate( -0.5, -0.5 );
77 : // apply further transformations:
78 0 : if (rTransitionInfo.mnRotationAngle != 0.0)
79 : {
80 : maStaticTransformation.rotate(
81 0 : basegfx::deg2rad(rTransitionInfo.mnRotationAngle) );
82 : }
83 0 : if (rTransitionInfo.mnScaleX != 1.0 ||
84 : rTransitionInfo.mnScaleY != 1.0)
85 : {
86 : maStaticTransformation.scale(
87 : rTransitionInfo.mnScaleX,
88 0 : rTransitionInfo.mnScaleY );
89 : }
90 0 : maStaticTransformation.translate( 0.5, 0.5 );
91 : }
92 :
93 0 : if( !bDirectionForward )
94 : {
95 : // Client has requested reversed
96 : // direction. Apply TransitionInfo's choice
97 : // for that
98 0 : switch( rTransitionInfo.meReverseMethod )
99 : {
100 : default:
101 0 : ENSURE_OR_THROW(
102 : false,
103 : "TransitionFactory::TransitionFactory(): Unexpected reverse method" );
104 : break;
105 :
106 : case TransitionInfo::REVERSEMETHOD_IGNORE:
107 0 : break;
108 :
109 : case TransitionInfo::REVERSEMETHOD_INVERT_SWEEP:
110 0 : mbForwardParameterSweep = !mbForwardParameterSweep;
111 0 : break;
112 :
113 : case TransitionInfo::REVERSEMETHOD_SUBTRACT_POLYGON:
114 0 : mbSubtractPolygon = !mbSubtractPolygon;
115 0 : break;
116 :
117 : case TransitionInfo::REVERSEMETHOD_SUBTRACT_AND_INVERT:
118 0 : mbForwardParameterSweep = !mbForwardParameterSweep;
119 0 : mbSubtractPolygon = !mbSubtractPolygon;
120 0 : break;
121 :
122 : case TransitionInfo::REVERSEMETHOD_ROTATE_180:
123 : maStaticTransformation = basegfx::tools::createRotateAroundPoint(0.5, 0.5, M_PI)
124 0 : * maStaticTransformation;
125 0 : break;
126 :
127 : case TransitionInfo::REVERSEMETHOD_FLIP_X:
128 : maStaticTransformation = basegfx::tools::createScaleTranslateB2DHomMatrix(-1.0, 1.0, 1.0, 0.0)
129 0 : * maStaticTransformation;
130 0 : mbFlip = true;
131 0 : break;
132 :
133 : case TransitionInfo::REVERSEMETHOD_FLIP_Y:
134 : maStaticTransformation = basegfx::tools::createScaleTranslateB2DHomMatrix(1.0, -1.0, 0.0, 1.0)
135 0 : * maStaticTransformation;
136 0 : mbFlip = true;
137 0 : break;
138 : }
139 : }
140 :
141 0 : if( !bModeIn )
142 : {
143 : // client has requested 'out' mode. Apply
144 : // TransitionInfo's method of choice
145 0 : if( rTransitionInfo.mbOutInvertsSweep )
146 0 : mbForwardParameterSweep = !mbForwardParameterSweep;
147 : else
148 0 : mbSubtractPolygon = !mbSubtractPolygon;
149 : }
150 0 : }
151 :
152 0 : ::basegfx::B2DPolyPolygon ClippingFunctor::operator()( double nValue,
153 : const ::basegfx::B2DSize& rTargetSize )
154 : {
155 : // modify clip polygon according to static
156 : // transformation plus current shape size
157 0 : ::basegfx::B2DHomMatrix aMatrix( maStaticTransformation );
158 :
159 : // retrieve current clip polygon
160 0 : ::basegfx::B2DPolyPolygon aClipPoly = (*mpParametricPoly)(
161 0 : mbForwardParameterSweep ? nValue : 1.0 - nValue );
162 :
163 : // TODO(Q4): workaround here, better be fixed in cppcanvas
164 0 : if (aClipPoly.count() == 0)
165 0 : aClipPoly.append( basegfx::B2DPolygon() );
166 :
167 0 : if (mbFlip)
168 0 : aClipPoly.flip();
169 :
170 : // currently, clipper cannot cope with curves. Subdivide first
171 : // AW: Should be no longer necessary; clipping tools are now bezier-safe
172 : // if( aClipPoly.areControlPointsUsed() )
173 : // aClipPoly = ::basegfx::tools::adaptiveSubdivideByAngle(aClipPoly);
174 :
175 0 : if( mbSubtractPolygon )
176 : {
177 : // subtract given polygon from background
178 : // rect. Do that before any transformations.
179 :
180 : // calc maBackgroundRect \ aClipPoly
181 : // =================================
182 :
183 : // AW: Simplified
184 : // use a range with fixed size (-1,-1),(2,2)
185 0 : const basegfx::B2DRange aBackgroundRange(-1, -1, 2, 2);
186 0 : const basegfx::B2DRange aClipPolyRange(aClipPoly.getB2DRange());
187 :
188 0 : if(aBackgroundRange.isInside(aClipPolyRange))
189 : {
190 : // combine polygons; make the clip polygon the hole
191 0 : aClipPoly = ::basegfx::tools::correctOrientations(aClipPoly);
192 0 : aClipPoly.flip();
193 0 : aClipPoly.insert(0, basegfx::tools::createPolygonFromRect(aBackgroundRange));
194 : }
195 : else
196 : {
197 : // when not completely inside aBackgroundRange clipping is needed
198 : // substract aClipPoly from aBackgroundRange
199 0 : const basegfx::B2DPolyPolygon aBackgroundPolyPoly(basegfx::tools::createPolygonFromRect(aBackgroundRange));
200 0 : aClipPoly = basegfx::tools::solvePolygonOperationDiff(aBackgroundPolyPoly, aClipPoly);
201 : }
202 : }
203 :
204 : // scale polygon up to current shape size
205 0 : if( mbScaleIsotrophically )
206 : {
207 0 : const double nScale( ::std::max( rTargetSize.getX(),
208 0 : rTargetSize.getY() ) );
209 0 : aMatrix.scale( nScale, nScale );
210 0 : aMatrix.translate( -(nScale-rTargetSize.getX())/2.0,
211 0 : -(nScale-rTargetSize.getY())/2.0 );
212 : }
213 : else
214 : {
215 : aMatrix.scale( rTargetSize.getX(),
216 0 : rTargetSize.getY() );
217 : }
218 :
219 : // apply cumulative transformation to clip polygon
220 0 : aClipPoly.transform( aMatrix );
221 :
222 0 : return aClipPoly;
223 : }
224 :
225 : }
226 : }
227 :
228 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|