xref: /aoo42x/main/svx/source/svdraw/impgrfll.cxx (revision e147e5f6)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 #include <svx/rectenum.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/outdev.hxx>
29 #include <vcl/bitmap.hxx>
30 
31 
ImpCalcBmpFillSizes(Size & rStartOffset,Size & rBmpOutputSize,const Rectangle & rOutputRect,const MapMode & rOutputMapMode,const Bitmap & rFillBitmap,const Size & rBmpSize,const Size & rBmpPerCent,const Size & rBmpOffPerCent,sal_Bool bBmpLogSize,sal_Bool bBmpTile,sal_Bool bBmpStretch,RECT_POINT eBmpRectPoint)32 void ImpCalcBmpFillSizes( Size&				rStartOffset,
33 						  Size&				rBmpOutputSize,
34 						  const Rectangle&	rOutputRect,
35 						  const MapMode&	rOutputMapMode,
36 						  const Bitmap&		rFillBitmap,
37 						  const Size&		rBmpSize,
38 						  const Size&		rBmpPerCent,
39 						  const Size&		rBmpOffPerCent,
40 						  sal_Bool			bBmpLogSize,
41 						  sal_Bool			bBmpTile,
42 						  sal_Bool			bBmpStretch,
43 						  RECT_POINT		eBmpRectPoint )
44 {
45 	sal_Bool	bOriginalSize = sal_False, bScaleSize = sal_False;
46 
47 	// Falls keine Groessen gegeben sind ( z.B. alte Dokumente )
48 	// berechnen wir uns die Groesse selber aus der Bitmap
49 	// ==> altes Verhalten;
50 	// wenn nur eine Groesse gegeben ist, wird die andere
51 	// Groesse angepasst berechnet
52 	if( bBmpLogSize )
53 	{
54 		if( !rBmpSize.Width() && !rBmpSize.Height() )
55 			bOriginalSize = sal_True;
56 		else if( !rBmpSize.Width() || !rBmpSize.Height() )
57 			bScaleSize = sal_True;
58 	}
59 	else
60 	{
61 		if( !rBmpPerCent.Width() && !rBmpPerCent.Height() )
62 			bOriginalSize = sal_True;
63 		else if( !rBmpPerCent.Width() || !rBmpPerCent.Height() )
64 			bScaleSize = sal_True;
65 	}
66 
67 	// entweder Originalgroesse oder angepasste Groesse
68 	if( bOriginalSize || bScaleSize )
69 	{
70 		MapMode aBmpPrefMapMode( rFillBitmap.GetPrefMapMode() );
71 		Size    aBmpPrefSize( rFillBitmap.GetPrefSize() );
72 
73 		// Falls keine gesetzt ist, nehmen wir Pixel
74 		if( !aBmpPrefSize.Width() || !aBmpPrefSize.Height() )
75 		{
76 			aBmpPrefSize = rFillBitmap.GetSizePixel();
77 			aBmpPrefMapMode = MAP_PIXEL;
78 		}
79 
80 		if( bOriginalSize )
81 		{
82 			if( MAP_PIXEL == aBmpPrefMapMode.GetMapUnit() )
83 				rBmpOutputSize = Application::GetDefaultDevice()->PixelToLogic( aBmpPrefSize, rOutputMapMode );
84 			else
85 				rBmpOutputSize = OutputDevice::LogicToLogic( aBmpPrefSize, aBmpPrefMapMode, rOutputMapMode );
86 		}
87 		else
88 		{
89 			if( bBmpLogSize )
90 			{
91 				rBmpOutputSize = rBmpSize;
92 
93 				if( !rBmpSize.Width() )
94 					rBmpOutputSize.Width() = basegfx::fround( (double) rBmpSize.Height() * aBmpPrefSize.Width() / aBmpPrefSize.Height() );
95 				else
96 					rBmpOutputSize.Height() = basegfx::fround( (double) rBmpSize.Width() * aBmpPrefSize.Height() / aBmpPrefSize.Width() );
97 			}
98 			else
99 			{
100 				if( !rBmpPerCent.Width() )
101 				{
102 					rBmpOutputSize.Height() = basegfx::fround( (double) rOutputRect.GetHeight() * rBmpPerCent.Height() / 100. );
103 					rBmpOutputSize.Width() = basegfx::fround( (double) rBmpOutputSize.Height() * aBmpPrefSize.Width() / aBmpPrefSize.Height() );
104 				}
105 				else
106 				{
107 					rBmpOutputSize.Width() = basegfx::fround( (double) rOutputRect.GetWidth() * rBmpPerCent.Width() / 100. );
108 					rBmpOutputSize.Height() = basegfx::fround( (double) rBmpOutputSize.Width() * aBmpPrefSize.Height() / aBmpPrefSize.Width() );
109 				}
110 			}
111 		}
112 	}
113 	// ansonsten koennen wir die Groesse leicht selber berechnen
114 	else
115 	{
116 		if( bBmpLogSize )
117 			rBmpOutputSize = rBmpSize;
118 		else
119 		{
120 			rBmpOutputSize.Width() = basegfx::fround( (double) rOutputRect.GetWidth() * rBmpPerCent.Width() / 100. );
121 			rBmpOutputSize.Height() = basegfx::fround( (double) rOutputRect.GetHeight() * rBmpPerCent.Height() / 100. );
122 		}
123 	}
124 
125 	// nur bei Kachelung die anderen Positionen berechnen
126 	if( bBmpTile )
127 	{
128 		Point aStartPoint;
129 
130 		// Grundposition der ersten Kachel berechen;
131 		// Diese Position wird spaeter zur Berechnung der absoluten
132 		// Startposition links oberhalb des Objektes benutzt
133 		switch( eBmpRectPoint )
134 		{
135 			case( RP_MT ):
136 			{
137 				aStartPoint.X() = rOutputRect.Left() + ( ( rOutputRect.GetWidth() - rBmpOutputSize.Width() ) >> 1 );
138 				aStartPoint.Y() = rOutputRect.Top();
139 			}
140 			break;
141 
142 			case( RP_RT ):
143 			{
144 				aStartPoint.X() = rOutputRect.Right() - rBmpOutputSize.Width();
145 				aStartPoint.Y() = rOutputRect.Top();
146 			}
147 			break;
148 
149 			case( RP_LM ):
150 			{
151 				aStartPoint.X() = rOutputRect.Left();
152 				aStartPoint.Y() = rOutputRect.Top() + ( ( rOutputRect.GetHeight() - rBmpOutputSize.Height() ) >> 1  );
153 			}
154 			break;
155 
156 			case( RP_MM ):
157 			{
158 				aStartPoint.X() = rOutputRect.Left() + ( ( rOutputRect.GetWidth() - rBmpOutputSize.Width() ) >> 1 );
159 				aStartPoint.Y() = rOutputRect.Top() + ( ( rOutputRect.GetHeight() - rBmpOutputSize.Height() ) >> 1 );
160 			}
161 			break;
162 
163 			case( RP_RM ):
164 			{
165 				aStartPoint.X() = rOutputRect.Right() - rBmpOutputSize.Width();
166 				aStartPoint.Y() = rOutputRect.Top() + ( ( rOutputRect.GetHeight() - rBmpOutputSize.Height() ) >> 1 );
167 			}
168 			break;
169 
170 			case( RP_LB ):
171 			{
172 				aStartPoint.X() = rOutputRect.Left();
173 				aStartPoint.Y() = rOutputRect.Bottom() - rBmpOutputSize.Height();
174 			}
175 			break;
176 
177 			case( RP_MB ):
178 			{
179 				aStartPoint.X() = rOutputRect.Left() + ( ( rOutputRect.GetWidth() - rBmpOutputSize.Width() ) >> 1 );
180 				aStartPoint.Y() = rOutputRect.Bottom() - rBmpOutputSize.Height();
181 			}
182 			break;
183 
184 			case( RP_RB ):
185 			{
186 				aStartPoint.X() = rOutputRect.Right() - rBmpOutputSize.Width();
187 				aStartPoint.Y() = rOutputRect.Bottom() - rBmpOutputSize.Height();
188 			}
189 			break;
190 
191 			// default linke obere Ecke
192 			default:
193 				aStartPoint = rOutputRect.TopLeft();
194 			break;
195 		}
196 
197 		// X- oder Y-Positionsoffset beruecksichtigen
198 		if( rBmpOffPerCent.Width() )
199 			aStartPoint.X() += ( rBmpOutputSize.Width() * rBmpOffPerCent.Width() / 100 );
200 
201 		if( rBmpOffPerCent.Height() )
202 			aStartPoint.Y() += ( rBmpOutputSize.Height() * rBmpOffPerCent.Height() / 100 );
203 
204 		// echten Startpunkt berechnen (links oben)
205 		if( rBmpOutputSize.Width() && rBmpOutputSize.Height() )
206 		{
207 			const long nDiffX = aStartPoint.X() - rOutputRect.Left();
208 			const long nDiffY = aStartPoint.Y() - rOutputRect.Top();
209 
210 			if ( nDiffX )
211 			{
212 				long nCount = nDiffX / rBmpOutputSize.Width() + 1;
213 
214 				if ( rBmpOffPerCent.Height() && ( nCount & 1L ) )
215 					nCount++;
216 
217 				aStartPoint.X() -= ( nCount * rBmpOutputSize.Width() );
218 			}
219 
220 			if ( nDiffY )
221 			{
222 				long nCount = nDiffY / rBmpOutputSize.Height() + 1;
223 
224 				if ( rBmpOffPerCent.Width() && ( nCount & 1L ) )
225 					nCount++;
226 
227 				aStartPoint.Y() -= ( nCount * rBmpOutputSize.Height() );
228 			}
229 		}
230 
231 		rStartOffset = Size( aStartPoint.X() - rOutputRect.Left(),
232 							 aStartPoint.Y() - rOutputRect.Top() );
233 	}
234 	else
235 	{
236 		if( bBmpStretch )
237 		{
238 			rStartOffset = Size(0, 0);
239 			rBmpOutputSize = rOutputRect.GetSize();
240 		}
241 		else
242 		{
243 			rStartOffset = Size( ( rOutputRect.GetWidth() - rBmpOutputSize.Width() ) >> 1,
244 								 ( rOutputRect.GetHeight() - rBmpOutputSize.Height() ) >> 1 );
245 		}
246 	}
247 }
248 
249 /* vim: set noet sw=4 ts=4: */
250