1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_drawinglayer.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <drawinglayer/primitive2d/pagepreviewprimitive2d.hxx>
32*cdf0e10cSrcweir #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
33*cdf0e10cSrcweir #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
34*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
35*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
36*cdf0e10cSrcweir #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
37*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace com::sun::star;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir namespace drawinglayer
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir 	namespace primitive2d
48*cdf0e10cSrcweir 	{
49*cdf0e10cSrcweir 		Primitive2DSequence PagePreviewPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
50*cdf0e10cSrcweir         {
51*cdf0e10cSrcweir     		Primitive2DSequence xRetval;
52*cdf0e10cSrcweir             Primitive2DSequence aContent(getPageContent());
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir             if(aContent.hasElements()
55*cdf0e10cSrcweir 				&& basegfx::fTools::more(getContentWidth(), 0.0)
56*cdf0e10cSrcweir 				&& basegfx::fTools::more(getContentHeight(), 0.0))
57*cdf0e10cSrcweir             {
58*cdf0e10cSrcweir 				// the decomposed matrix will be needed
59*cdf0e10cSrcweir 				basegfx::B2DVector aScale, aTranslate;
60*cdf0e10cSrcweir 				double fRotate, fShearX;
61*cdf0e10cSrcweir 				getTransform().decompose(aScale, aTranslate, fRotate, fShearX);
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 				if(basegfx::fTools::more(aScale.getX(), 0.0) && basegfx::fTools::more(aScale.getY(), 0.0))
64*cdf0e10cSrcweir 				{
65*cdf0e10cSrcweir 					// check if content overlaps with tageted size and needs to be embedded with a
66*cdf0e10cSrcweir 					// clipping primitive
67*cdf0e10cSrcweir 					const basegfx::B2DRange aRealContentRange(getB2DRangeFromPrimitive2DSequence(aContent, rViewInformation));
68*cdf0e10cSrcweir 					const basegfx::B2DRange aAllowedContentRange(0.0, 0.0, getContentWidth(), getContentHeight());
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 					if(!aAllowedContentRange.isInside(aRealContentRange))
71*cdf0e10cSrcweir 					{
72*cdf0e10cSrcweir 						const Primitive2DReference xReferenceA(
73*cdf0e10cSrcweir                             new MaskPrimitive2D(
74*cdf0e10cSrcweir                                 basegfx::B2DPolyPolygon(
75*cdf0e10cSrcweir                                     basegfx::tools::createPolygonFromRect(aAllowedContentRange)), aContent));
76*cdf0e10cSrcweir 						aContent = Primitive2DSequence(&xReferenceA, 1);
77*cdf0e10cSrcweir 					}
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 					// create a mapping from content to object.
80*cdf0e10cSrcweir 					basegfx::B2DHomMatrix aPageTrans;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 					if(getKeepAspectRatio())
83*cdf0e10cSrcweir 					{
84*cdf0e10cSrcweir 					    // #i101075# when keeping the aspect ratio is wanted, it is necessary to calculate
85*cdf0e10cSrcweir                         // an equidistant scaling in X and Y and a corresponding translation to
86*cdf0e10cSrcweir                         // center the output. Calculate needed scale factors
87*cdf0e10cSrcweir 					    const double fScaleX(aScale.getX() / getContentWidth());
88*cdf0e10cSrcweir 					    const double fScaleY(aScale.getY() / getContentHeight());
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir                         // to keep the aspect, use the smaller scale and adapt missing size by translation
91*cdf0e10cSrcweir 						if(fScaleX < fScaleY)
92*cdf0e10cSrcweir 						{
93*cdf0e10cSrcweir 							// height needs to be adapted
94*cdf0e10cSrcweir 							const double fNeededHeight(aScale.getY() / fScaleX);
95*cdf0e10cSrcweir 							const double fSpaceToAdd(fNeededHeight - getContentHeight());
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 							aPageTrans.translate(0.0, fSpaceToAdd * 0.5);
98*cdf0e10cSrcweir 							aPageTrans.scale(fScaleX, aScale.getY() / fNeededHeight);
99*cdf0e10cSrcweir 						}
100*cdf0e10cSrcweir 						else
101*cdf0e10cSrcweir 						{
102*cdf0e10cSrcweir 							// width needs to be adapted
103*cdf0e10cSrcweir 							const double fNeededWidth(aScale.getX() / fScaleY);
104*cdf0e10cSrcweir 							const double fSpaceToAdd(fNeededWidth - getContentWidth());
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 							aPageTrans.translate(fSpaceToAdd * 0.5, 0.0);
107*cdf0e10cSrcweir 							aPageTrans.scale(aScale.getX() / fNeededWidth, fScaleY);
108*cdf0e10cSrcweir 						}
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir                         // add the missing object transformation aspects
111*cdf0e10cSrcweir 						const basegfx::B2DHomMatrix aCombined(basegfx::tools::createShearXRotateTranslateB2DHomMatrix(
112*cdf0e10cSrcweir 							fShearX, fRotate, aTranslate.getX(), aTranslate.getY()));
113*cdf0e10cSrcweir 					    aPageTrans = aCombined * aPageTrans;
114*cdf0e10cSrcweir 					}
115*cdf0e10cSrcweir 					else
116*cdf0e10cSrcweir 					{
117*cdf0e10cSrcweir                         // completely scale to PageObject size. Scale to unit size.
118*cdf0e10cSrcweir 						aPageTrans.scale(1.0/ getContentWidth(), 1.0 / getContentHeight());
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir                         // apply object matrix
121*cdf0e10cSrcweir 						aPageTrans *= getTransform();
122*cdf0e10cSrcweir 					}
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 					// embed in necessary transformation to map from SdrPage to SdrPageObject
125*cdf0e10cSrcweir 					const Primitive2DReference xReferenceB(new TransformPrimitive2D(aPageTrans, aContent));
126*cdf0e10cSrcweir 					xRetval = Primitive2DSequence(&xReferenceB, 1);
127*cdf0e10cSrcweir 				}
128*cdf0e10cSrcweir             }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir             return xRetval;
131*cdf0e10cSrcweir         }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 		PagePreviewPrimitive2D::PagePreviewPrimitive2D(
134*cdf0e10cSrcweir 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& rxDrawPage,
135*cdf0e10cSrcweir             const basegfx::B2DHomMatrix& rTransform,
136*cdf0e10cSrcweir             double fContentWidth,
137*cdf0e10cSrcweir             double fContentHeight,
138*cdf0e10cSrcweir 			const Primitive2DSequence& rPageContent,
139*cdf0e10cSrcweir 			bool bKeepAspectRatio)
140*cdf0e10cSrcweir 		:	BufferedDecompositionPrimitive2D(),
141*cdf0e10cSrcweir             mxDrawPage(rxDrawPage),
142*cdf0e10cSrcweir             maPageContent(rPageContent),
143*cdf0e10cSrcweir             maTransform(rTransform),
144*cdf0e10cSrcweir             mfContentWidth(fContentWidth),
145*cdf0e10cSrcweir             mfContentHeight(fContentHeight),
146*cdf0e10cSrcweir 			mbKeepAspectRatio(bKeepAspectRatio)
147*cdf0e10cSrcweir 		{
148*cdf0e10cSrcweir 		}
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 		bool PagePreviewPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
151*cdf0e10cSrcweir 		{
152*cdf0e10cSrcweir 			if(BasePrimitive2D::operator==(rPrimitive))
153*cdf0e10cSrcweir 			{
154*cdf0e10cSrcweir 				const PagePreviewPrimitive2D& rCompare = static_cast< const PagePreviewPrimitive2D& >(rPrimitive);
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir                 return (getXDrawPage() == rCompare.getXDrawPage()
157*cdf0e10cSrcweir                     && getPageContent() == rCompare.getPageContent()
158*cdf0e10cSrcweir                     && getTransform() == rCompare.getTransform()
159*cdf0e10cSrcweir                     && getContentWidth() == rCompare.getContentWidth()
160*cdf0e10cSrcweir                     && getContentHeight() == rCompare.getContentHeight()
161*cdf0e10cSrcweir                     && getKeepAspectRatio() == rCompare.getKeepAspectRatio());
162*cdf0e10cSrcweir 			}
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 			return false;
165*cdf0e10cSrcweir 		}
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir         basegfx::B2DRange PagePreviewPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation`*/) const
168*cdf0e10cSrcweir         {
169*cdf0e10cSrcweir             // nothing is allowed to stick out of a PagePreviewPrimitive, thus we
170*cdf0e10cSrcweir             // can quickly deliver our range here
171*cdf0e10cSrcweir 			basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
172*cdf0e10cSrcweir 			aRetval.transform(getTransform());
173*cdf0e10cSrcweir 			return aRetval;
174*cdf0e10cSrcweir         }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 		// provide unique ID
177*cdf0e10cSrcweir 		ImplPrimitrive2DIDBlock(PagePreviewPrimitive2D, PRIMITIVE2D_ID_PAGEPREVIEWPRIMITIVE2D)
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	} // end of namespace primitive2d
180*cdf0e10cSrcweir } // end of namespace drawinglayer
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
183*cdf0e10cSrcweir // eof
184