1464702f4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3464702f4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4464702f4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5464702f4SAndrew Rist  * distributed with this work for additional information
6464702f4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7464702f4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8464702f4SAndrew Rist  * "License"); you may not use this file except in compliance
9464702f4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10464702f4SAndrew Rist  *
11464702f4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12464702f4SAndrew Rist  *
13464702f4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14464702f4SAndrew Rist  * software distributed under the License is distributed on an
15464702f4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16464702f4SAndrew Rist  * KIND, either express or implied.  See the License for the
17464702f4SAndrew Rist  * specific language governing permissions and limitations
18464702f4SAndrew Rist  * under the License.
19464702f4SAndrew Rist  *
20464702f4SAndrew Rist  *************************************************************/
21464702f4SAndrew Rist 
22464702f4SAndrew 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>
30*035a2f44SArmin Le Grand #include <drawinglayer/primitive2d/fillgraphicprimitive2d.hxx>
31cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
32cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
33cdf0e10cSrcweir #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
34*035a2f44SArmin Le Grand #include <basegfx/matrix/b2dhommatrixtools.hxx>
35*035a2f44SArmin Le Grand #include <vcl/graph.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace drawinglayer
40cdf0e10cSrcweir {
41cdf0e10cSrcweir 	namespace primitive2d
42cdf0e10cSrcweir 	{
create2DDecomposition(const geometry::ViewInformation2D &) const43cdf0e10cSrcweir 		Primitive2DSequence WallpaperBitmapPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
44cdf0e10cSrcweir 		{
45cdf0e10cSrcweir 			Primitive2DSequence aRetval;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 			if(!getLocalObjectRange().isEmpty() && !getBitmapEx().IsEmpty())
48cdf0e10cSrcweir 			{
49cdf0e10cSrcweir 				// get bitmap PIXEL size
50cdf0e10cSrcweir 			    const Size& rPixelSize = getBitmapEx().GetSizePixel();
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 				if(rPixelSize.Width() > 0 && rPixelSize.Height() > 0)
53cdf0e10cSrcweir 				{
54cdf0e10cSrcweir 					if(WALLPAPER_SCALE == getWallpaperStyle())
55cdf0e10cSrcweir 					{
56cdf0e10cSrcweir 						// shortcut for scale; use simple BitmapPrimitive2D
57cdf0e10cSrcweir 						basegfx::B2DHomMatrix aObjectTransform;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 						aObjectTransform.set(0, 0, getLocalObjectRange().getWidth());
60cdf0e10cSrcweir 						aObjectTransform.set(1, 1, getLocalObjectRange().getHeight());
61cdf0e10cSrcweir 						aObjectTransform.set(0, 2, getLocalObjectRange().getMinX());
62cdf0e10cSrcweir 						aObjectTransform.set(1, 2, getLocalObjectRange().getMinY());
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 						Primitive2DReference xReference(
65cdf0e10cSrcweir 							new BitmapPrimitive2D(
66cdf0e10cSrcweir 								getBitmapEx(),
67cdf0e10cSrcweir 								aObjectTransform));
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 						aRetval = Primitive2DSequence(&xReference, 1);
70cdf0e10cSrcweir 					}
71cdf0e10cSrcweir 					else
72cdf0e10cSrcweir 					{
73cdf0e10cSrcweir 						// transform to logic size
74cdf0e10cSrcweir 						basegfx::B2DHomMatrix aInverseViewTransformation(getViewTransformation());
75cdf0e10cSrcweir 						aInverseViewTransformation.invert();
76cdf0e10cSrcweir 						basegfx::B2DVector aLogicSize(rPixelSize.Width(), rPixelSize.Height());
77cdf0e10cSrcweir 						aLogicSize = aInverseViewTransformation * aLogicSize;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 						// apply laout
80cdf0e10cSrcweir 						basegfx::B2DPoint aTargetTopLeft(getLocalObjectRange().getMinimum());
81cdf0e10cSrcweir 						bool bUseTargetTopLeft(true);
82cdf0e10cSrcweir 						bool bNeedsClipping(false);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 						switch(getWallpaperStyle())
85cdf0e10cSrcweir 						{
86cdf0e10cSrcweir 							default: //case WALLPAPER_TILE :, also WALLPAPER_NULL and WALLPAPER_APPLICATIONGRADIENT
87cdf0e10cSrcweir 							{
88cdf0e10cSrcweir 								bUseTargetTopLeft = false;
89cdf0e10cSrcweir 								break;
90cdf0e10cSrcweir 							}
91cdf0e10cSrcweir 							case WALLPAPER_SCALE :
92cdf0e10cSrcweir 							{
93cdf0e10cSrcweir 								// handled by shortcut above
94cdf0e10cSrcweir 								break;
95cdf0e10cSrcweir 							}
96cdf0e10cSrcweir 							case WALLPAPER_TOPLEFT :
97cdf0e10cSrcweir 							{
98cdf0e10cSrcweir 								// nothing to do
99cdf0e10cSrcweir 								break;
100cdf0e10cSrcweir 							}
101cdf0e10cSrcweir 							case WALLPAPER_TOP :
102cdf0e10cSrcweir 							{
103cdf0e10cSrcweir 								const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
104cdf0e10cSrcweir 								aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5));
105cdf0e10cSrcweir 								break;
106cdf0e10cSrcweir 							}
107cdf0e10cSrcweir 							case WALLPAPER_TOPRIGHT :
108cdf0e10cSrcweir 							{
109cdf0e10cSrcweir 								aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX());
110cdf0e10cSrcweir 								break;
111cdf0e10cSrcweir 							}
112cdf0e10cSrcweir 							case WALLPAPER_LEFT :
113cdf0e10cSrcweir 							{
114cdf0e10cSrcweir 								const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
115cdf0e10cSrcweir 								aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5));
116cdf0e10cSrcweir 								break;
117cdf0e10cSrcweir 							}
118cdf0e10cSrcweir 							case WALLPAPER_CENTER :
119cdf0e10cSrcweir 							{
120cdf0e10cSrcweir 								const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
121cdf0e10cSrcweir 								aTargetTopLeft = aCenter - (aLogicSize * 0.5);
122cdf0e10cSrcweir 								break;
123cdf0e10cSrcweir 							}
124cdf0e10cSrcweir 							case WALLPAPER_RIGHT :
125cdf0e10cSrcweir 							{
126cdf0e10cSrcweir 								const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
127cdf0e10cSrcweir 								aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX());
128cdf0e10cSrcweir 								aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5));
129cdf0e10cSrcweir 								break;
130cdf0e10cSrcweir 							}
131cdf0e10cSrcweir 							case WALLPAPER_BOTTOMLEFT :
132cdf0e10cSrcweir 							{
133cdf0e10cSrcweir 								aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY());
134cdf0e10cSrcweir 								break;
135cdf0e10cSrcweir 							}
136cdf0e10cSrcweir 							case WALLPAPER_BOTTOM :
137cdf0e10cSrcweir 							{
138cdf0e10cSrcweir 								const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
139cdf0e10cSrcweir 								aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5));
140cdf0e10cSrcweir 								aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY());
141cdf0e10cSrcweir 								break;
142cdf0e10cSrcweir 							}
143cdf0e10cSrcweir 							case WALLPAPER_BOTTOMRIGHT :
144cdf0e10cSrcweir 							{
145cdf0e10cSrcweir 								aTargetTopLeft = getLocalObjectRange().getMaximum() - aLogicSize;
146cdf0e10cSrcweir 								break;
147cdf0e10cSrcweir 							}
148cdf0e10cSrcweir 						}
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 						if(bUseTargetTopLeft)
151cdf0e10cSrcweir 						{
152cdf0e10cSrcweir 							// fill target range
153cdf0e10cSrcweir 							const basegfx::B2DRange aTargetRange(aTargetTopLeft, aTargetTopLeft + aLogicSize);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 							// create aligned, single BitmapPrimitive2D
156cdf0e10cSrcweir 							basegfx::B2DHomMatrix aObjectTransform;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 							aObjectTransform.set(0, 0, aTargetRange.getWidth());
159cdf0e10cSrcweir 							aObjectTransform.set(1, 1, aTargetRange.getHeight());
160cdf0e10cSrcweir 							aObjectTransform.set(0, 2, aTargetRange.getMinX());
161cdf0e10cSrcweir 							aObjectTransform.set(1, 2, aTargetRange.getMinY());
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 							Primitive2DReference xReference(
164cdf0e10cSrcweir 								new BitmapPrimitive2D(
165cdf0e10cSrcweir 									getBitmapEx(),
166cdf0e10cSrcweir 									aObjectTransform));
167cdf0e10cSrcweir 							aRetval = Primitive2DSequence(&xReference, 1);
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 							// clip when not completely inside object range
170cdf0e10cSrcweir 							bNeedsClipping = !getLocalObjectRange().isInside(aTargetRange);
171cdf0e10cSrcweir 						}
172cdf0e10cSrcweir 						else
173cdf0e10cSrcweir 						{
174cdf0e10cSrcweir 							// WALLPAPER_TILE, WALLPAPER_NULL, WALLPAPER_APPLICATIONGRADIENT
175cdf0e10cSrcweir 							// convert to relative positions
176cdf0e10cSrcweir 							const basegfx::B2DVector aRelativeSize(
177cdf0e10cSrcweir 								aLogicSize.getX() / (getLocalObjectRange().getWidth() ? getLocalObjectRange().getWidth() : 1.0),
178cdf0e10cSrcweir 								aLogicSize.getY() / (getLocalObjectRange().getHeight() ? getLocalObjectRange().getHeight() : 1.0));
179cdf0e10cSrcweir 							basegfx::B2DPoint aRelativeTopLeft(0.0, 0.0);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 							if(WALLPAPER_TILE != getWallpaperStyle())
182cdf0e10cSrcweir 							{
183cdf0e10cSrcweir 								aRelativeTopLeft.setX(0.5 - aRelativeSize.getX());
184cdf0e10cSrcweir 								aRelativeTopLeft.setY(0.5 - aRelativeSize.getY());
185cdf0e10cSrcweir 							}
186cdf0e10cSrcweir 
187*035a2f44SArmin Le Grand 							// prepare FillGraphicAttribute
188*035a2f44SArmin Le Grand 							const attribute::FillGraphicAttribute aFillGraphicAttribute(
189*035a2f44SArmin Le Grand 								Graphic(getBitmapEx()),
190*035a2f44SArmin Le Grand 								basegfx::B2DRange(aRelativeTopLeft, aRelativeTopLeft+ aRelativeSize),
191cdf0e10cSrcweir 								true);
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 							// create ObjectTransform
194*035a2f44SArmin Le Grand 							const basegfx::B2DHomMatrix aObjectTransform(
195*035a2f44SArmin Le Grand                                 basegfx::tools::createScaleTranslateB2DHomMatrix(
196*035a2f44SArmin Le Grand                                     getLocalObjectRange().getRange(),
197*035a2f44SArmin Le Grand                                     getLocalObjectRange().getMinimum()));
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 							// create FillBitmapPrimitive
200cdf0e10cSrcweir 							const drawinglayer::primitive2d::Primitive2DReference xFillBitmap(
201*035a2f44SArmin Le Grand 								new drawinglayer::primitive2d::FillGraphicPrimitive2D(
202cdf0e10cSrcweir 									aObjectTransform,
203*035a2f44SArmin Le Grand 									aFillGraphicAttribute));
204cdf0e10cSrcweir 							aRetval = Primitive2DSequence(&xFillBitmap, 1);
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 							// always embed tiled fill to clipping
207cdf0e10cSrcweir 							bNeedsClipping = true;
208cdf0e10cSrcweir 						}
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 						if(bNeedsClipping)
211cdf0e10cSrcweir 						{
212cdf0e10cSrcweir 							// embed to clipping; this is necessary for tiled fills
213cdf0e10cSrcweir 							const basegfx::B2DPolyPolygon aPolyPolygon(
214cdf0e10cSrcweir                                 basegfx::tools::createPolygonFromRect(getLocalObjectRange()));
215cdf0e10cSrcweir 							const drawinglayer::primitive2d::Primitive2DReference xClippedFill(
216cdf0e10cSrcweir 								new drawinglayer::primitive2d::MaskPrimitive2D(
217cdf0e10cSrcweir 									aPolyPolygon,
218cdf0e10cSrcweir 									aRetval));
219cdf0e10cSrcweir 							aRetval = Primitive2DSequence(&xClippedFill, 1);
220cdf0e10cSrcweir 						}
221cdf0e10cSrcweir 					}
222cdf0e10cSrcweir 				}
223cdf0e10cSrcweir 			}
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 			return aRetval;
226cdf0e10cSrcweir 		}
227cdf0e10cSrcweir 
WallpaperBitmapPrimitive2D(const basegfx::B2DRange & rObjectRange,const BitmapEx & rBitmapEx,WallpaperStyle eWallpaperStyle)228cdf0e10cSrcweir 		WallpaperBitmapPrimitive2D::WallpaperBitmapPrimitive2D(
229cdf0e10cSrcweir 			const basegfx::B2DRange& rObjectRange,
230cdf0e10cSrcweir 			const BitmapEx& rBitmapEx,
231cdf0e10cSrcweir 			WallpaperStyle eWallpaperStyle)
232cdf0e10cSrcweir 		:	ViewTransformationDependentPrimitive2D(),
233cdf0e10cSrcweir 			maObjectRange(rObjectRange),
234cdf0e10cSrcweir 			maBitmapEx(rBitmapEx),
235cdf0e10cSrcweir 			meWallpaperStyle(eWallpaperStyle)
236cdf0e10cSrcweir 		{
237cdf0e10cSrcweir 		}
238cdf0e10cSrcweir 
operator ==(const BasePrimitive2D & rPrimitive) const239cdf0e10cSrcweir 		bool WallpaperBitmapPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
240cdf0e10cSrcweir 		{
241cdf0e10cSrcweir 			if(ViewTransformationDependentPrimitive2D::operator==(rPrimitive))
242cdf0e10cSrcweir 			{
243cdf0e10cSrcweir 				const WallpaperBitmapPrimitive2D& rCompare = (WallpaperBitmapPrimitive2D&)rPrimitive;
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 				return (getLocalObjectRange() == rCompare.getLocalObjectRange()
246cdf0e10cSrcweir 					&& getBitmapEx() == rCompare.getBitmapEx()
247cdf0e10cSrcweir 					&& getWallpaperStyle() == rCompare.getWallpaperStyle());
248cdf0e10cSrcweir 			}
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 			return false;
251cdf0e10cSrcweir 		}
252cdf0e10cSrcweir 
getB2DRange(const geometry::ViewInformation2D &) const253cdf0e10cSrcweir 		basegfx::B2DRange WallpaperBitmapPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
254cdf0e10cSrcweir 		{
255cdf0e10cSrcweir 			return getLocalObjectRange();
256cdf0e10cSrcweir 		}
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 		// provide unique ID
259cdf0e10cSrcweir 		ImplPrimitrive2DIDBlock(WallpaperBitmapPrimitive2D, PRIMITIVE2D_ID_WALLPAPERBITMAPPRIMITIVE2D)
260cdf0e10cSrcweir 	} // end of namespace primitive2d
261cdf0e10cSrcweir } // end of namespace drawinglayer
262cdf0e10cSrcweir 
263cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
264cdf0e10cSrcweir // eof
265