1*464702f4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*464702f4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*464702f4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*464702f4SAndrew Rist  * distributed with this work for additional information
6*464702f4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*464702f4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*464702f4SAndrew Rist  * "License"); you may not use this file except in compliance
9*464702f4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*464702f4SAndrew Rist  *
11*464702f4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*464702f4SAndrew Rist  *
13*464702f4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*464702f4SAndrew Rist  * software distributed under the License is distributed on an
15*464702f4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*464702f4SAndrew Rist  * KIND, either express or implied.  See the License for the
17*464702f4SAndrew Rist  * specific language governing permissions and limitations
18*464702f4SAndrew Rist  * under the License.
19*464702f4SAndrew Rist  *
20*464702f4SAndrew Rist  *************************************************************/
21*464702f4SAndrew Rist 
22*464702f4SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_drawinglayer.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <drawinglayer/primitive2d/pagepreviewprimitive2d.hxx>
28cdf0e10cSrcweir #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
29cdf0e10cSrcweir #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
30cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
31cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
32cdf0e10cSrcweir #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
33cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace com::sun::star;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace drawinglayer
42cdf0e10cSrcweir {
43cdf0e10cSrcweir 	namespace primitive2d
44cdf0e10cSrcweir 	{
45cdf0e10cSrcweir 		Primitive2DSequence PagePreviewPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
46cdf0e10cSrcweir         {
47cdf0e10cSrcweir     		Primitive2DSequence xRetval;
48cdf0e10cSrcweir             Primitive2DSequence aContent(getPageContent());
49cdf0e10cSrcweir 
50cdf0e10cSrcweir             if(aContent.hasElements()
51cdf0e10cSrcweir 				&& basegfx::fTools::more(getContentWidth(), 0.0)
52cdf0e10cSrcweir 				&& basegfx::fTools::more(getContentHeight(), 0.0))
53cdf0e10cSrcweir             {
54cdf0e10cSrcweir 				// the decomposed matrix will be needed
55cdf0e10cSrcweir 				basegfx::B2DVector aScale, aTranslate;
56cdf0e10cSrcweir 				double fRotate, fShearX;
57cdf0e10cSrcweir 				getTransform().decompose(aScale, aTranslate, fRotate, fShearX);
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 				if(basegfx::fTools::more(aScale.getX(), 0.0) && basegfx::fTools::more(aScale.getY(), 0.0))
60cdf0e10cSrcweir 				{
61cdf0e10cSrcweir 					// check if content overlaps with tageted size and needs to be embedded with a
62cdf0e10cSrcweir 					// clipping primitive
63cdf0e10cSrcweir 					const basegfx::B2DRange aRealContentRange(getB2DRangeFromPrimitive2DSequence(aContent, rViewInformation));
64cdf0e10cSrcweir 					const basegfx::B2DRange aAllowedContentRange(0.0, 0.0, getContentWidth(), getContentHeight());
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 					if(!aAllowedContentRange.isInside(aRealContentRange))
67cdf0e10cSrcweir 					{
68cdf0e10cSrcweir 						const Primitive2DReference xReferenceA(
69cdf0e10cSrcweir                             new MaskPrimitive2D(
70cdf0e10cSrcweir                                 basegfx::B2DPolyPolygon(
71cdf0e10cSrcweir                                     basegfx::tools::createPolygonFromRect(aAllowedContentRange)), aContent));
72cdf0e10cSrcweir 						aContent = Primitive2DSequence(&xReferenceA, 1);
73cdf0e10cSrcweir 					}
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 					// create a mapping from content to object.
76cdf0e10cSrcweir 					basegfx::B2DHomMatrix aPageTrans;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 					if(getKeepAspectRatio())
79cdf0e10cSrcweir 					{
80cdf0e10cSrcweir 					    // #i101075# when keeping the aspect ratio is wanted, it is necessary to calculate
81cdf0e10cSrcweir                         // an equidistant scaling in X and Y and a corresponding translation to
82cdf0e10cSrcweir                         // center the output. Calculate needed scale factors
83cdf0e10cSrcweir 					    const double fScaleX(aScale.getX() / getContentWidth());
84cdf0e10cSrcweir 					    const double fScaleY(aScale.getY() / getContentHeight());
85cdf0e10cSrcweir 
86cdf0e10cSrcweir                         // to keep the aspect, use the smaller scale and adapt missing size by translation
87cdf0e10cSrcweir 						if(fScaleX < fScaleY)
88cdf0e10cSrcweir 						{
89cdf0e10cSrcweir 							// height needs to be adapted
90cdf0e10cSrcweir 							const double fNeededHeight(aScale.getY() / fScaleX);
91cdf0e10cSrcweir 							const double fSpaceToAdd(fNeededHeight - getContentHeight());
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 							aPageTrans.translate(0.0, fSpaceToAdd * 0.5);
94cdf0e10cSrcweir 							aPageTrans.scale(fScaleX, aScale.getY() / fNeededHeight);
95cdf0e10cSrcweir 						}
96cdf0e10cSrcweir 						else
97cdf0e10cSrcweir 						{
98cdf0e10cSrcweir 							// width needs to be adapted
99cdf0e10cSrcweir 							const double fNeededWidth(aScale.getX() / fScaleY);
100cdf0e10cSrcweir 							const double fSpaceToAdd(fNeededWidth - getContentWidth());
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 							aPageTrans.translate(fSpaceToAdd * 0.5, 0.0);
103cdf0e10cSrcweir 							aPageTrans.scale(aScale.getX() / fNeededWidth, fScaleY);
104cdf0e10cSrcweir 						}
105cdf0e10cSrcweir 
106cdf0e10cSrcweir                         // add the missing object transformation aspects
107cdf0e10cSrcweir 						const basegfx::B2DHomMatrix aCombined(basegfx::tools::createShearXRotateTranslateB2DHomMatrix(
108cdf0e10cSrcweir 							fShearX, fRotate, aTranslate.getX(), aTranslate.getY()));
109cdf0e10cSrcweir 					    aPageTrans = aCombined * aPageTrans;
110cdf0e10cSrcweir 					}
111cdf0e10cSrcweir 					else
112cdf0e10cSrcweir 					{
113cdf0e10cSrcweir                         // completely scale to PageObject size. Scale to unit size.
114cdf0e10cSrcweir 						aPageTrans.scale(1.0/ getContentWidth(), 1.0 / getContentHeight());
115cdf0e10cSrcweir 
116cdf0e10cSrcweir                         // apply object matrix
117cdf0e10cSrcweir 						aPageTrans *= getTransform();
118cdf0e10cSrcweir 					}
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 					// embed in necessary transformation to map from SdrPage to SdrPageObject
121cdf0e10cSrcweir 					const Primitive2DReference xReferenceB(new TransformPrimitive2D(aPageTrans, aContent));
122cdf0e10cSrcweir 					xRetval = Primitive2DSequence(&xReferenceB, 1);
123cdf0e10cSrcweir 				}
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir             return xRetval;
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 		PagePreviewPrimitive2D::PagePreviewPrimitive2D(
130cdf0e10cSrcweir 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& rxDrawPage,
131cdf0e10cSrcweir             const basegfx::B2DHomMatrix& rTransform,
132cdf0e10cSrcweir             double fContentWidth,
133cdf0e10cSrcweir             double fContentHeight,
134cdf0e10cSrcweir 			const Primitive2DSequence& rPageContent,
135cdf0e10cSrcweir 			bool bKeepAspectRatio)
136cdf0e10cSrcweir 		:	BufferedDecompositionPrimitive2D(),
137cdf0e10cSrcweir             mxDrawPage(rxDrawPage),
138cdf0e10cSrcweir             maPageContent(rPageContent),
139cdf0e10cSrcweir             maTransform(rTransform),
140cdf0e10cSrcweir             mfContentWidth(fContentWidth),
141cdf0e10cSrcweir             mfContentHeight(fContentHeight),
142cdf0e10cSrcweir 			mbKeepAspectRatio(bKeepAspectRatio)
143cdf0e10cSrcweir 		{
144cdf0e10cSrcweir 		}
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 		bool PagePreviewPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
147cdf0e10cSrcweir 		{
148cdf0e10cSrcweir 			if(BasePrimitive2D::operator==(rPrimitive))
149cdf0e10cSrcweir 			{
150cdf0e10cSrcweir 				const PagePreviewPrimitive2D& rCompare = static_cast< const PagePreviewPrimitive2D& >(rPrimitive);
151cdf0e10cSrcweir 
152cdf0e10cSrcweir                 return (getXDrawPage() == rCompare.getXDrawPage()
153cdf0e10cSrcweir                     && getPageContent() == rCompare.getPageContent()
154cdf0e10cSrcweir                     && getTransform() == rCompare.getTransform()
155cdf0e10cSrcweir                     && getContentWidth() == rCompare.getContentWidth()
156cdf0e10cSrcweir                     && getContentHeight() == rCompare.getContentHeight()
157cdf0e10cSrcweir                     && getKeepAspectRatio() == rCompare.getKeepAspectRatio());
158cdf0e10cSrcweir 			}
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 			return false;
161cdf0e10cSrcweir 		}
162cdf0e10cSrcweir 
163cdf0e10cSrcweir         basegfx::B2DRange PagePreviewPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation`*/) const
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             // nothing is allowed to stick out of a PagePreviewPrimitive, thus we
166cdf0e10cSrcweir             // can quickly deliver our range here
167cdf0e10cSrcweir 			basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
168cdf0e10cSrcweir 			aRetval.transform(getTransform());
169cdf0e10cSrcweir 			return aRetval;
170cdf0e10cSrcweir         }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 		// provide unique ID
173cdf0e10cSrcweir 		ImplPrimitrive2DIDBlock(PagePreviewPrimitive2D, PRIMITIVE2D_ID_PAGEPREVIEWPRIMITIVE2D)
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	} // end of namespace primitive2d
176cdf0e10cSrcweir } // end of namespace drawinglayer
177cdf0e10cSrcweir 
178cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
179cdf0e10cSrcweir // eof
180