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/wallpaperprimitive2d.hxx>
28cdf0e10cSrcweir #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
29cdf0e10cSrcweir #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
30cdf0e10cSrcweir #include <drawinglayer/primitive2d/fillbitmapprimitive2d.hxx>
31cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
32cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
33cdf0e10cSrcweir #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace drawinglayer
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 	namespace primitive2d
40cdf0e10cSrcweir 	{
41cdf0e10cSrcweir 		Primitive2DSequence WallpaperBitmapPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
42cdf0e10cSrcweir 		{
43cdf0e10cSrcweir 			Primitive2DSequence aRetval;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 			if(!getLocalObjectRange().isEmpty() && !getBitmapEx().IsEmpty())
46cdf0e10cSrcweir 			{
47cdf0e10cSrcweir 				// get bitmap PIXEL size
48cdf0e10cSrcweir 			    const Size& rPixelSize = getBitmapEx().GetSizePixel();
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 				if(rPixelSize.Width() > 0 && rPixelSize.Height() > 0)
51cdf0e10cSrcweir 				{
52cdf0e10cSrcweir 					if(WALLPAPER_SCALE == getWallpaperStyle())
53cdf0e10cSrcweir 					{
54cdf0e10cSrcweir 						// shortcut for scale; use simple BitmapPrimitive2D
55cdf0e10cSrcweir 						basegfx::B2DHomMatrix aObjectTransform;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 						aObjectTransform.set(0, 0, getLocalObjectRange().getWidth());
58cdf0e10cSrcweir 						aObjectTransform.set(1, 1, getLocalObjectRange().getHeight());
59cdf0e10cSrcweir 						aObjectTransform.set(0, 2, getLocalObjectRange().getMinX());
60cdf0e10cSrcweir 						aObjectTransform.set(1, 2, getLocalObjectRange().getMinY());
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 						Primitive2DReference xReference(
63cdf0e10cSrcweir 							new BitmapPrimitive2D(
64cdf0e10cSrcweir 								getBitmapEx(),
65cdf0e10cSrcweir 								aObjectTransform));
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 						aRetval = Primitive2DSequence(&xReference, 1);
68cdf0e10cSrcweir 					}
69cdf0e10cSrcweir 					else
70cdf0e10cSrcweir 					{
71cdf0e10cSrcweir 						// transform to logic size
72cdf0e10cSrcweir 						basegfx::B2DHomMatrix aInverseViewTransformation(getViewTransformation());
73cdf0e10cSrcweir 						aInverseViewTransformation.invert();
74cdf0e10cSrcweir 						basegfx::B2DVector aLogicSize(rPixelSize.Width(), rPixelSize.Height());
75cdf0e10cSrcweir 						aLogicSize = aInverseViewTransformation * aLogicSize;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 						// apply laout
78cdf0e10cSrcweir 						basegfx::B2DPoint aTargetTopLeft(getLocalObjectRange().getMinimum());
79cdf0e10cSrcweir 						bool bUseTargetTopLeft(true);
80cdf0e10cSrcweir 						bool bNeedsClipping(false);
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 						switch(getWallpaperStyle())
83cdf0e10cSrcweir 						{
84cdf0e10cSrcweir 							default: //case WALLPAPER_TILE :, also WALLPAPER_NULL and WALLPAPER_APPLICATIONGRADIENT
85cdf0e10cSrcweir 							{
86cdf0e10cSrcweir 								bUseTargetTopLeft = false;
87cdf0e10cSrcweir 								break;
88cdf0e10cSrcweir 							}
89cdf0e10cSrcweir 							case WALLPAPER_SCALE :
90cdf0e10cSrcweir 							{
91cdf0e10cSrcweir 								// handled by shortcut above
92cdf0e10cSrcweir 								break;
93cdf0e10cSrcweir 							}
94cdf0e10cSrcweir 							case WALLPAPER_TOPLEFT :
95cdf0e10cSrcweir 							{
96cdf0e10cSrcweir 								// nothing to do
97cdf0e10cSrcweir 								break;
98cdf0e10cSrcweir 							}
99cdf0e10cSrcweir 							case WALLPAPER_TOP :
100cdf0e10cSrcweir 							{
101cdf0e10cSrcweir 								const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
102cdf0e10cSrcweir 								aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5));
103cdf0e10cSrcweir 								break;
104cdf0e10cSrcweir 							}
105cdf0e10cSrcweir 							case WALLPAPER_TOPRIGHT :
106cdf0e10cSrcweir 							{
107cdf0e10cSrcweir 								aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX());
108cdf0e10cSrcweir 								break;
109cdf0e10cSrcweir 							}
110cdf0e10cSrcweir 							case WALLPAPER_LEFT :
111cdf0e10cSrcweir 							{
112cdf0e10cSrcweir 								const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
113cdf0e10cSrcweir 								aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5));
114cdf0e10cSrcweir 								break;
115cdf0e10cSrcweir 							}
116cdf0e10cSrcweir 							case WALLPAPER_CENTER :
117cdf0e10cSrcweir 							{
118cdf0e10cSrcweir 								const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
119cdf0e10cSrcweir 								aTargetTopLeft = aCenter - (aLogicSize * 0.5);
120cdf0e10cSrcweir 								break;
121cdf0e10cSrcweir 							}
122cdf0e10cSrcweir 							case WALLPAPER_RIGHT :
123cdf0e10cSrcweir 							{
124cdf0e10cSrcweir 								const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
125cdf0e10cSrcweir 								aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX());
126cdf0e10cSrcweir 								aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5));
127cdf0e10cSrcweir 								break;
128cdf0e10cSrcweir 							}
129cdf0e10cSrcweir 							case WALLPAPER_BOTTOMLEFT :
130cdf0e10cSrcweir 							{
131cdf0e10cSrcweir 								aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY());
132cdf0e10cSrcweir 								break;
133cdf0e10cSrcweir 							}
134cdf0e10cSrcweir 							case WALLPAPER_BOTTOM :
135cdf0e10cSrcweir 							{
136cdf0e10cSrcweir 								const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
137cdf0e10cSrcweir 								aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5));
138cdf0e10cSrcweir 								aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY());
139cdf0e10cSrcweir 								break;
140cdf0e10cSrcweir 							}
141cdf0e10cSrcweir 							case WALLPAPER_BOTTOMRIGHT :
142cdf0e10cSrcweir 							{
143cdf0e10cSrcweir 								aTargetTopLeft = getLocalObjectRange().getMaximum() - aLogicSize;
144cdf0e10cSrcweir 								break;
145cdf0e10cSrcweir 							}
146cdf0e10cSrcweir 						}
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 						if(bUseTargetTopLeft)
149cdf0e10cSrcweir 						{
150cdf0e10cSrcweir 							// fill target range
151cdf0e10cSrcweir 							const basegfx::B2DRange aTargetRange(aTargetTopLeft, aTargetTopLeft + aLogicSize);
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 							// create aligned, single BitmapPrimitive2D
154cdf0e10cSrcweir 							basegfx::B2DHomMatrix aObjectTransform;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 							aObjectTransform.set(0, 0, aTargetRange.getWidth());
157cdf0e10cSrcweir 							aObjectTransform.set(1, 1, aTargetRange.getHeight());
158cdf0e10cSrcweir 							aObjectTransform.set(0, 2, aTargetRange.getMinX());
159cdf0e10cSrcweir 							aObjectTransform.set(1, 2, aTargetRange.getMinY());
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 							Primitive2DReference xReference(
162cdf0e10cSrcweir 								new BitmapPrimitive2D(
163cdf0e10cSrcweir 									getBitmapEx(),
164cdf0e10cSrcweir 									aObjectTransform));
165cdf0e10cSrcweir 							aRetval = Primitive2DSequence(&xReference, 1);
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 							// clip when not completely inside object range
168cdf0e10cSrcweir 							bNeedsClipping = !getLocalObjectRange().isInside(aTargetRange);
169cdf0e10cSrcweir 						}
170cdf0e10cSrcweir 						else
171cdf0e10cSrcweir 						{
172cdf0e10cSrcweir 							// WALLPAPER_TILE, WALLPAPER_NULL, WALLPAPER_APPLICATIONGRADIENT
173cdf0e10cSrcweir 							// convert to relative positions
174cdf0e10cSrcweir 							const basegfx::B2DVector aRelativeSize(
175cdf0e10cSrcweir 								aLogicSize.getX() / (getLocalObjectRange().getWidth() ? getLocalObjectRange().getWidth() : 1.0),
176cdf0e10cSrcweir 								aLogicSize.getY() / (getLocalObjectRange().getHeight() ? getLocalObjectRange().getHeight() : 1.0));
177cdf0e10cSrcweir 							basegfx::B2DPoint aRelativeTopLeft(0.0, 0.0);
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 							if(WALLPAPER_TILE != getWallpaperStyle())
180cdf0e10cSrcweir 							{
181cdf0e10cSrcweir 								aRelativeTopLeft.setX(0.5 - aRelativeSize.getX());
182cdf0e10cSrcweir 								aRelativeTopLeft.setY(0.5 - aRelativeSize.getY());
183cdf0e10cSrcweir 							}
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 							// prepare FillBitmapAttribute
186cdf0e10cSrcweir 							const attribute::FillBitmapAttribute aFillBitmapAttribute(
187cdf0e10cSrcweir 								getBitmapEx(),
188cdf0e10cSrcweir 								aRelativeTopLeft,
189cdf0e10cSrcweir 								aRelativeSize,
190cdf0e10cSrcweir 								true);
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 							// create ObjectTransform
193cdf0e10cSrcweir 							basegfx::B2DHomMatrix aObjectTransform;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 							aObjectTransform.set(0, 0, getLocalObjectRange().getWidth());
196cdf0e10cSrcweir 							aObjectTransform.set(1, 1, getLocalObjectRange().getHeight());
197cdf0e10cSrcweir 							aObjectTransform.set(0, 2, getLocalObjectRange().getMinX());
198cdf0e10cSrcweir 							aObjectTransform.set(1, 2, getLocalObjectRange().getMinY());
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 							// create FillBitmapPrimitive
201cdf0e10cSrcweir 							const drawinglayer::primitive2d::Primitive2DReference xFillBitmap(
202cdf0e10cSrcweir 								new drawinglayer::primitive2d::FillBitmapPrimitive2D(
203cdf0e10cSrcweir 									aObjectTransform,
204cdf0e10cSrcweir 									aFillBitmapAttribute));
205cdf0e10cSrcweir 							aRetval = Primitive2DSequence(&xFillBitmap, 1);
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 							// always embed tiled fill to clipping
208cdf0e10cSrcweir 							bNeedsClipping = true;
209cdf0e10cSrcweir 						}
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 						if(bNeedsClipping)
212cdf0e10cSrcweir 						{
213cdf0e10cSrcweir 							// embed to clipping; this is necessary for tiled fills
214cdf0e10cSrcweir 							const basegfx::B2DPolyPolygon aPolyPolygon(
215cdf0e10cSrcweir                                 basegfx::tools::createPolygonFromRect(getLocalObjectRange()));
216cdf0e10cSrcweir 							const drawinglayer::primitive2d::Primitive2DReference xClippedFill(
217cdf0e10cSrcweir 								new drawinglayer::primitive2d::MaskPrimitive2D(
218cdf0e10cSrcweir 									aPolyPolygon,
219cdf0e10cSrcweir 									aRetval));
220cdf0e10cSrcweir 							aRetval = Primitive2DSequence(&xClippedFill, 1);
221cdf0e10cSrcweir 						}
222cdf0e10cSrcweir 					}
223cdf0e10cSrcweir 				}
224cdf0e10cSrcweir 			}
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 			return aRetval;
227cdf0e10cSrcweir 		}
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 		WallpaperBitmapPrimitive2D::WallpaperBitmapPrimitive2D(
230cdf0e10cSrcweir 			const basegfx::B2DRange& rObjectRange,
231cdf0e10cSrcweir 			const BitmapEx& rBitmapEx,
232cdf0e10cSrcweir 			WallpaperStyle eWallpaperStyle)
233cdf0e10cSrcweir 		:	ViewTransformationDependentPrimitive2D(),
234cdf0e10cSrcweir 			maObjectRange(rObjectRange),
235cdf0e10cSrcweir 			maBitmapEx(rBitmapEx),
236cdf0e10cSrcweir 			meWallpaperStyle(eWallpaperStyle)
237cdf0e10cSrcweir 		{
238cdf0e10cSrcweir 		}
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 		bool WallpaperBitmapPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
241cdf0e10cSrcweir 		{
242cdf0e10cSrcweir 			if(ViewTransformationDependentPrimitive2D::operator==(rPrimitive))
243cdf0e10cSrcweir 			{
244cdf0e10cSrcweir 				const WallpaperBitmapPrimitive2D& rCompare = (WallpaperBitmapPrimitive2D&)rPrimitive;
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 				return (getLocalObjectRange() == rCompare.getLocalObjectRange()
247cdf0e10cSrcweir 					&& getBitmapEx() == rCompare.getBitmapEx()
248cdf0e10cSrcweir 					&& getWallpaperStyle() == rCompare.getWallpaperStyle());
249cdf0e10cSrcweir 			}
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 			return false;
252cdf0e10cSrcweir 		}
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 		basegfx::B2DRange WallpaperBitmapPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
255cdf0e10cSrcweir 		{
256cdf0e10cSrcweir 			return getLocalObjectRange();
257cdf0e10cSrcweir 		}
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 		// provide unique ID
260cdf0e10cSrcweir 		ImplPrimitrive2DIDBlock(WallpaperBitmapPrimitive2D, PRIMITIVE2D_ID_WALLPAPERBITMAPPRIMITIVE2D)
261cdf0e10cSrcweir 	} // end of namespace primitive2d
262cdf0e10cSrcweir } // end of namespace drawinglayer
263cdf0e10cSrcweir 
264cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
265cdf0e10cSrcweir // eof
266