xref: /AOO41X/main/svx/source/svdraw/impgrfll.cxx (revision f6e50924346d0b8c0b07c91832a97665dd718b0c)
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 ///////////////////////////////////////////////////////////////////////////////
32 
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)33 void ImpCalcBmpFillSizes( Size&            rStartOffset,
34                           Size&            rBmpOutputSize,
35                           const Rectangle& rOutputRect,
36                           const MapMode&   rOutputMapMode,
37                           const Bitmap&    rFillBitmap,
38                           const Size&      rBmpSize,
39                           const Size&      rBmpPerCent,
40                           const Size&      rBmpOffPerCent,
41                           sal_Bool             bBmpLogSize,
42                           sal_Bool             bBmpTile,
43                           sal_Bool             bBmpStretch,
44                           RECT_POINT       eBmpRectPoint )
45 {
46     sal_Bool    bOriginalSize = sal_False, bScaleSize = sal_False;
47 
48     // Falls keine Groessen gegeben sind ( z.B. alte Dokumente )
49     // berechnen wir uns die Groesse selber aus der Bitmap
50     // ==> altes Verhalten;
51     // wenn nur eine Groesse gegeben ist, wird die andere
52     // Groesse angepasst berechnet
53     if( bBmpLogSize )
54     {
55         if( !rBmpSize.Width() && !rBmpSize.Height() )
56             bOriginalSize = sal_True;
57         else if( !rBmpSize.Width() || !rBmpSize.Height() )
58             bScaleSize = sal_True;
59     }
60     else
61     {
62         if( !rBmpPerCent.Width() && !rBmpPerCent.Height() )
63             bOriginalSize = sal_True;
64         else if( !rBmpPerCent.Width() || !rBmpPerCent.Height() )
65             bScaleSize = sal_True;
66     }
67 
68     // entweder Originalgroesse oder angepasste Groesse
69     if( bOriginalSize || bScaleSize )
70     {
71         MapMode aBmpPrefMapMode( rFillBitmap.GetPrefMapMode() );
72         Size    aBmpPrefSize( rFillBitmap.GetPrefSize() );
73 
74         // Falls keine gesetzt ist, nehmen wir Pixel
75         if( !aBmpPrefSize.Width() || !aBmpPrefSize.Height() )
76         {
77             aBmpPrefSize = rFillBitmap.GetSizePixel();
78             aBmpPrefMapMode = MAP_PIXEL;
79         }
80 
81         if( bOriginalSize )
82         {
83             if( MAP_PIXEL == aBmpPrefMapMode.GetMapUnit() )
84                 rBmpOutputSize = Application::GetDefaultDevice()->PixelToLogic( aBmpPrefSize, rOutputMapMode );
85             else
86                 rBmpOutputSize = OutputDevice::LogicToLogic( aBmpPrefSize, aBmpPrefMapMode, rOutputMapMode );
87         }
88         else
89         {
90             if( bBmpLogSize )
91             {
92                 rBmpOutputSize = rBmpSize;
93 
94                 if( !rBmpSize.Width() )
95                     rBmpOutputSize.Width() = basegfx::fround( (double) rBmpSize.Height() * aBmpPrefSize.Width() / aBmpPrefSize.Height() );
96                 else
97                     rBmpOutputSize.Height() = basegfx::fround( (double) rBmpSize.Width() * aBmpPrefSize.Height() / aBmpPrefSize.Width() );
98             }
99             else
100             {
101                 if( !rBmpPerCent.Width() )
102                 {
103                     rBmpOutputSize.Height() = basegfx::fround( (double) rOutputRect.GetHeight() * rBmpPerCent.Height() / 100. );
104                     rBmpOutputSize.Width() = basegfx::fround( (double) rBmpOutputSize.Height() * aBmpPrefSize.Width() / aBmpPrefSize.Height() );
105                 }
106                 else
107                 {
108                     rBmpOutputSize.Width() = basegfx::fround( (double) rOutputRect.GetWidth() * rBmpPerCent.Width() / 100. );
109                     rBmpOutputSize.Height() = basegfx::fround( (double) rBmpOutputSize.Width() * aBmpPrefSize.Height() / aBmpPrefSize.Width() );
110                 }
111             }
112         }
113     }
114     // ansonsten koennen wir die Groesse leicht selber berechnen
115     else
116     {
117         if( bBmpLogSize )
118             rBmpOutputSize = rBmpSize;
119         else
120         {
121             rBmpOutputSize.Width() = basegfx::fround( (double) rOutputRect.GetWidth() * rBmpPerCent.Width() / 100. );
122             rBmpOutputSize.Height() = basegfx::fround( (double) rOutputRect.GetHeight() * rBmpPerCent.Height() / 100. );
123         }
124     }
125 
126     // nur bei Kachelung die anderen Positionen berechnen
127     if( bBmpTile )
128     {
129         Point aStartPoint;
130 
131         // Grundposition der ersten Kachel berechen;
132         // Diese Position wird spaeter zur Berechnung der absoluten
133         // Startposition links oberhalb des Objektes benutzt
134         switch( eBmpRectPoint )
135         {
136             case( RP_MT ):
137             {
138                 aStartPoint.X() = rOutputRect.Left() + ( ( rOutputRect.GetWidth() - rBmpOutputSize.Width() ) >> 1 );
139                 aStartPoint.Y() = rOutputRect.Top();
140             }
141             break;
142 
143             case( RP_RT ):
144             {
145                 aStartPoint.X() = rOutputRect.Right() - rBmpOutputSize.Width();
146                 aStartPoint.Y() = rOutputRect.Top();
147             }
148             break;
149 
150             case( RP_LM ):
151             {
152                 aStartPoint.X() = rOutputRect.Left();
153                 aStartPoint.Y() = rOutputRect.Top() + ( ( rOutputRect.GetHeight() - rBmpOutputSize.Height() ) >> 1  );
154             }
155             break;
156 
157             case( RP_MM ):
158             {
159                 aStartPoint.X() = rOutputRect.Left() + ( ( rOutputRect.GetWidth() - rBmpOutputSize.Width() ) >> 1 );
160                 aStartPoint.Y() = rOutputRect.Top() + ( ( rOutputRect.GetHeight() - rBmpOutputSize.Height() ) >> 1 );
161             }
162             break;
163 
164             case( RP_RM ):
165             {
166                 aStartPoint.X() = rOutputRect.Right() - rBmpOutputSize.Width();
167                 aStartPoint.Y() = rOutputRect.Top() + ( ( rOutputRect.GetHeight() - rBmpOutputSize.Height() ) >> 1 );
168             }
169             break;
170 
171             case( RP_LB ):
172             {
173                 aStartPoint.X() = rOutputRect.Left();
174                 aStartPoint.Y() = rOutputRect.Bottom() - rBmpOutputSize.Height();
175             }
176             break;
177 
178             case( RP_MB ):
179             {
180                 aStartPoint.X() = rOutputRect.Left() + ( ( rOutputRect.GetWidth() - rBmpOutputSize.Width() ) >> 1 );
181                 aStartPoint.Y() = rOutputRect.Bottom() - rBmpOutputSize.Height();
182             }
183             break;
184 
185             case( RP_RB ):
186             {
187                 aStartPoint.X() = rOutputRect.Right() - rBmpOutputSize.Width();
188                 aStartPoint.Y() = rOutputRect.Bottom() - rBmpOutputSize.Height();
189             }
190             break;
191 
192             // default linke obere Ecke
193             default:
194                 aStartPoint = rOutputRect.TopLeft();
195             break;
196         }
197 
198         // X- oder Y-Positionsoffset beruecksichtigen
199         if( rBmpOffPerCent.Width() )
200             aStartPoint.X() += ( rBmpOutputSize.Width() * rBmpOffPerCent.Width() / 100 );
201 
202         if( rBmpOffPerCent.Height() )
203             aStartPoint.Y() += ( rBmpOutputSize.Height() * rBmpOffPerCent.Height() / 100 );
204 
205         // echten Startpunkt berechnen ( links oben )
206         if( rBmpOutputSize.Width() && rBmpOutputSize.Height() )
207         {
208             const long nDiffX = aStartPoint.X() - rOutputRect.Left();
209             const long nDiffY = aStartPoint.Y() - rOutputRect.Top();
210 
211             if ( nDiffX )
212             {
213                 long nCount = nDiffX / rBmpOutputSize.Width() + 1;
214 
215                 if ( rBmpOffPerCent.Height() && ( nCount & 1L ) )
216                     nCount++;
217 
218                 aStartPoint.X() -= ( nCount * rBmpOutputSize.Width() );
219             }
220 
221             if ( nDiffY )
222             {
223                 long nCount = nDiffY / rBmpOutputSize.Height() + 1;
224 
225                 if ( rBmpOffPerCent.Width() && ( nCount & 1L ) )
226                     nCount++;
227 
228                 aStartPoint.Y() -= ( nCount * rBmpOutputSize.Height() );
229             }
230         }
231 
232         rStartOffset = Size( aStartPoint.X() - rOutputRect.Left(),
233                              aStartPoint.Y() - rOutputRect.Top() );
234     }
235     else
236     {
237         if( bBmpStretch )
238         {
239             rStartOffset = Size(0, 0);
240             rBmpOutputSize = rOutputRect.GetSize();
241         }
242         else
243         {
244             rStartOffset = Size( ( rOutputRect.GetWidth() - rBmpOutputSize.Width() ) >> 1,
245                                  ( rOutputRect.GetHeight() - rBmpOutputSize.Height() ) >> 1 );
246         }
247     }
248 }
249 
250 //////////////////////////////////////////////////////////////////////////////
251 // eof
252