Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <drawinglayer/attribute/sdrfillbitmapattribute.hxx>
30 : : #include <drawinglayer/attribute/fillbitmapattribute.hxx>
31 : : #include <vcl/bitmapex.hxx>
32 : :
33 : : //////////////////////////////////////////////////////////////////////////////
34 : :
35 : : namespace drawinglayer
36 : : {
37 : : namespace attribute
38 : : {
39 : 575 : class ImpSdrFillBitmapAttribute
40 : : {
41 : : public:
42 : : // refcounter
43 : : sal_uInt32 mnRefCount;
44 : :
45 : : // data definitions
46 : : Bitmap maBitmap;
47 : : basegfx::B2DVector maSize;
48 : : basegfx::B2DVector maOffset;
49 : : basegfx::B2DVector maOffsetPosition;
50 : : basegfx::B2DVector maRectPoint;
51 : :
52 : : // bitfield
53 : : unsigned mbTiling : 1;
54 : : unsigned mbStretch : 1;
55 : : unsigned mbLogSize : 1;
56 : :
57 : 616 : ImpSdrFillBitmapAttribute(
58 : : const Bitmap& rBitmap,
59 : : const basegfx::B2DVector& rSize,
60 : : const basegfx::B2DVector& rOffset,
61 : : const basegfx::B2DVector& rOffsetPosition,
62 : : const basegfx::B2DVector& rRectPoint,
63 : : bool bTiling,
64 : : bool bStretch,
65 : : bool bLogSize)
66 : : : mnRefCount(0),
67 : : maBitmap(rBitmap),
68 : : maSize(rSize),
69 : : maOffset(rOffset),
70 : : maOffsetPosition(rOffsetPosition),
71 : : maRectPoint(rRectPoint),
72 : : mbTiling(bTiling),
73 : : mbStretch(bStretch),
74 : 616 : mbLogSize(bLogSize)
75 : : {
76 : 616 : }
77 : :
78 : : // data read access
79 : 888 : const Bitmap& getBitmap() const { return maBitmap; }
80 : 1150 : const basegfx::B2DVector& getSize() const { return maSize; }
81 : 1150 : const basegfx::B2DVector& getOffset() const { return maOffset; }
82 : 1150 : const basegfx::B2DVector& getOffsetPosition() const { return maOffsetPosition; }
83 : 888 : const basegfx::B2DVector& getRectPoint() const { return maRectPoint; }
84 : 2198 : bool getTiling() const { return mbTiling; }
85 : 626 : bool getStretch() const { return mbStretch; }
86 : 626 : bool getLogSize() const { return mbLogSize; }
87 : :
88 : 313 : bool operator==(const ImpSdrFillBitmapAttribute& rCandidate) const
89 : : {
90 : 313 : return (getBitmap() == rCandidate.getBitmap()
91 : 313 : && getSize() == rCandidate.getSize()
92 : 313 : && getOffset() == rCandidate.getOffset()
93 : 313 : && getOffsetPosition() == rCandidate.getOffsetPosition()
94 : 313 : && getRectPoint() == rCandidate.getRectPoint()
95 : 313 : && getTiling() == rCandidate.getTiling()
96 : 313 : && getStretch() == rCandidate.getStretch()
97 [ + - ][ + - : 2191 : && getLogSize() == rCandidate.getLogSize());
+ - + - +
- + - + -
+ - ]
98 : : }
99 : :
100 : 69955 : static ImpSdrFillBitmapAttribute* get_global_default()
101 : : {
102 : : static ImpSdrFillBitmapAttribute* pDefault = 0;
103 : :
104 [ + + ]: 69955 : if(!pDefault)
105 : : {
106 : : pDefault = new ImpSdrFillBitmapAttribute(
107 : : Bitmap(),
108 : : basegfx::B2DVector(),
109 : : basegfx::B2DVector(),
110 : : basegfx::B2DVector(),
111 : : basegfx::B2DVector(),
112 : : false,
113 : : false,
114 [ + - ][ + - ]: 41 : false);
115 : :
116 : : // never delete; start with RefCount 1, not 0
117 : 41 : pDefault->mnRefCount++;
118 : : }
119 : :
120 : 69955 : return pDefault;
121 : : }
122 : : };
123 : :
124 : 575 : SdrFillBitmapAttribute::SdrFillBitmapAttribute(
125 : : const Bitmap& rBitmap,
126 : : const basegfx::B2DVector& rSize,
127 : : const basegfx::B2DVector& rOffset,
128 : : const basegfx::B2DVector& rOffsetPosition,
129 : : const basegfx::B2DVector& rRectPoint,
130 : : bool bTiling,
131 : : bool bStretch,
132 : : bool bLogSize)
133 : : : mpSdrFillBitmapAttribute(new ImpSdrFillBitmapAttribute(
134 [ + - ]: 575 : rBitmap, rSize, rOffset, rOffsetPosition, rRectPoint, bTiling, bStretch, bLogSize))
135 : : {
136 : 575 : }
137 : :
138 : 56309 : SdrFillBitmapAttribute::SdrFillBitmapAttribute()
139 : 56309 : : mpSdrFillBitmapAttribute(ImpSdrFillBitmapAttribute::get_global_default())
140 : : {
141 : 56309 : mpSdrFillBitmapAttribute->mnRefCount++;
142 : 56309 : }
143 : :
144 : 56309 : SdrFillBitmapAttribute::SdrFillBitmapAttribute(const SdrFillBitmapAttribute& rCandidate)
145 : 56309 : : mpSdrFillBitmapAttribute(rCandidate.mpSdrFillBitmapAttribute)
146 : : {
147 : 56309 : mpSdrFillBitmapAttribute->mnRefCount++;
148 : 56309 : }
149 : :
150 : 113152 : SdrFillBitmapAttribute::~SdrFillBitmapAttribute()
151 : : {
152 [ + + ]: 113152 : if(mpSdrFillBitmapAttribute->mnRefCount)
153 : : {
154 : 112577 : mpSdrFillBitmapAttribute->mnRefCount--;
155 : : }
156 : : else
157 : : {
158 [ + - ]: 575 : delete mpSdrFillBitmapAttribute;
159 : : }
160 : 113152 : }
161 : :
162 : 13646 : bool SdrFillBitmapAttribute::isDefault() const
163 : : {
164 : 13646 : return mpSdrFillBitmapAttribute == ImpSdrFillBitmapAttribute::get_global_default();
165 : : }
166 : :
167 : 575 : SdrFillBitmapAttribute& SdrFillBitmapAttribute::operator=(const SdrFillBitmapAttribute& rCandidate)
168 : : {
169 [ + - ]: 575 : if(rCandidate.mpSdrFillBitmapAttribute != mpSdrFillBitmapAttribute)
170 : : {
171 [ + - ]: 575 : if(mpSdrFillBitmapAttribute->mnRefCount)
172 : : {
173 : 575 : mpSdrFillBitmapAttribute->mnRefCount--;
174 : : }
175 : : else
176 : : {
177 [ # # ]: 0 : delete mpSdrFillBitmapAttribute;
178 : : }
179 : :
180 : 575 : mpSdrFillBitmapAttribute = rCandidate.mpSdrFillBitmapAttribute;
181 : 575 : mpSdrFillBitmapAttribute->mnRefCount++;
182 : : }
183 : :
184 : 575 : return *this;
185 : : }
186 : :
187 : 33353 : bool SdrFillBitmapAttribute::operator==(const SdrFillBitmapAttribute& rCandidate) const
188 : : {
189 [ + + ]: 33353 : if(rCandidate.mpSdrFillBitmapAttribute == mpSdrFillBitmapAttribute)
190 : : {
191 : 33040 : return true;
192 : : }
193 : :
194 [ - + ]: 313 : if(rCandidate.isDefault() != isDefault())
195 : : {
196 : 0 : return false;
197 : : }
198 : :
199 : 33353 : return (*rCandidate.mpSdrFillBitmapAttribute == *mpSdrFillBitmapAttribute);
200 : : }
201 : :
202 : 262 : const Bitmap& SdrFillBitmapAttribute::getBitmap() const
203 : : {
204 : 262 : return mpSdrFillBitmapAttribute->getBitmap();
205 : : }
206 : :
207 : 524 : const basegfx::B2DVector& SdrFillBitmapAttribute::getSize() const
208 : : {
209 : 524 : return mpSdrFillBitmapAttribute->getSize();
210 : : }
211 : :
212 : 524 : const basegfx::B2DVector& SdrFillBitmapAttribute::getOffset() const
213 : : {
214 : 524 : return mpSdrFillBitmapAttribute->getOffset();
215 : : }
216 : :
217 : 524 : const basegfx::B2DVector& SdrFillBitmapAttribute::getOffsetPosition() const
218 : : {
219 : 524 : return mpSdrFillBitmapAttribute->getOffsetPosition();
220 : : }
221 : :
222 : 262 : const basegfx::B2DVector& SdrFillBitmapAttribute::getRectPoint() const
223 : : {
224 : 262 : return mpSdrFillBitmapAttribute->getRectPoint();
225 : : }
226 : :
227 : 1572 : bool SdrFillBitmapAttribute::getTiling() const
228 : : {
229 : 1572 : return mpSdrFillBitmapAttribute->getTiling();
230 : : }
231 : :
232 : 0 : bool SdrFillBitmapAttribute::getStretch() const
233 : : {
234 : 0 : return mpSdrFillBitmapAttribute->getStretch();
235 : : }
236 : :
237 : 262 : FillBitmapAttribute SdrFillBitmapAttribute::getFillBitmapAttribute(const basegfx::B2DRange& rRange) const
238 : : {
239 : : // get logical size of bitmap (before expanding eventually)
240 [ + - ][ + - ]: 262 : Bitmap aBitmap(getBitmap());
241 : 262 : const basegfx::B2DVector aLogicalSize(aBitmap.GetPrefSize().getWidth(), aBitmap.GetPrefSize().getHeight());
242 : :
243 : : // get hor/ver shiftings and apply them eventually to the bitmap, but only
244 : : // when tiling is on
245 : 262 : bool bExpandWidth(false);
246 : 262 : bool bExpandHeight(false);
247 : :
248 [ + - ][ + - ]: 262 : if(getTiling())
249 : : {
250 [ + - ][ + - ]: 262 : if(0.0 != getOffset().getX() || 0.0 != getOffset().getY())
[ + - ][ - + ]
[ - + ]
251 : : {
252 [ # # ]: 0 : const sal_uInt32 nWidth(aBitmap.GetSizePixel().getWidth());
253 [ # # ]: 0 : const sal_uInt32 nHeight(aBitmap.GetSizePixel().getHeight());
254 : :
255 [ # # ][ # # ]: 0 : if(0.0 != getOffset().getX())
256 : : {
257 : 0 : bExpandHeight = true;
258 [ # # ]: 0 : const sal_uInt32 nOffset(basegfx::fround(((double)nWidth * getOffset().getX()) / 100.0));
259 [ # # ]: 0 : aBitmap.Expand(0L, nHeight);
260 : :
261 : 0 : const Size aSizeA(nOffset, nHeight);
262 [ # # ]: 0 : const Rectangle aDstA(Point(0L, nHeight), aSizeA);
263 [ # # ]: 0 : const Rectangle aSrcA(Point(nWidth - nOffset, 0L), aSizeA);
264 [ # # ]: 0 : aBitmap.CopyPixel(aDstA, aSrcA);
265 : :
266 : 0 : const Size aSizeB(nWidth - nOffset, nHeight);
267 [ # # ]: 0 : const Rectangle aDstB(Point(nOffset, nHeight), aSizeB);
268 [ # # ]: 0 : const Rectangle aSrcB(Point(0L, 0L), aSizeB);
269 [ # # ]: 0 : aBitmap.CopyPixel(aDstB, aSrcB);
270 : : }
271 : : else
272 : : {
273 : 0 : bExpandWidth = true;
274 [ # # ]: 0 : const sal_uInt32 nOffset(basegfx::fround(((double)nHeight * getOffset().getY()) / 100.0));
275 [ # # ]: 0 : aBitmap.Expand(nWidth, 0L);
276 : :
277 : 0 : const Size aSize(nWidth, nHeight);
278 [ # # ]: 0 : const Rectangle aDst(Point(nWidth, 0L), aSize);
279 [ # # ]: 0 : const Rectangle aSrc(Point(0L, 0L), aSize);
280 [ # # ]: 0 : aBitmap.CopyPixel(aDst, aSrc);
281 : :
282 : 0 : const Size aSizeA(nWidth, nOffset);
283 [ # # ]: 0 : const Rectangle aDstA(Point(0L, 0L), aSizeA);
284 [ # # ]: 0 : const Rectangle aSrcA(Point(nWidth, nHeight - nOffset), aSizeA);
285 [ # # ]: 0 : aBitmap.CopyPixel(aDstA, aSrcA);
286 : :
287 : 0 : const Size aSizeB(nWidth, nHeight - nOffset);
288 [ # # ]: 0 : const Rectangle aDstB(Point(0L, nOffset), aSizeB);
289 [ # # ]: 0 : const Rectangle aSrcB(Point(nWidth, 0L), aSizeB);
290 [ # # ]: 0 : aBitmap.CopyPixel(aDstB, aSrcB);
291 : : }
292 : : }
293 : : }
294 : :
295 : : // init values with defaults
296 : 262 : basegfx::B2DPoint aBitmapSize(1.0, 1.0);
297 : 262 : basegfx::B2DVector aBitmapTopLeft(0.0, 0.0);
298 : :
299 : : // are canges needed?
300 [ - + ][ # # ]: 262 : if(getTiling() || !getStretch())
[ # # ][ + - ]
[ + - ]
301 : : {
302 : : // init values with range sizes
303 [ + - ][ + - ]: 262 : const double fRangeWidth(0.0 != rRange.getWidth() ? rRange.getWidth() : 1.0);
[ + - ]
304 [ + - ][ + - ]: 262 : const double fRangeHeight(0.0 != rRange.getHeight() ? rRange.getHeight() : 1.0);
[ + - ]
305 : 262 : aBitmapSize = basegfx::B2DPoint(fRangeWidth, fRangeHeight);
306 : :
307 : : // size changes
308 [ - + ][ + - ]: 262 : if(0.0 != getSize().getX())
309 : : {
310 [ # # ][ # # ]: 0 : if(getSize().getX() < 0.0)
311 : : {
312 [ # # ]: 0 : aBitmapSize.setX(aBitmapSize.getX() * (getSize().getX() * -0.01));
313 : : }
314 : : else
315 : : {
316 [ # # ]: 0 : aBitmapSize.setX(getSize().getX());
317 : : }
318 : : }
319 : : else
320 : : {
321 : 262 : aBitmapSize.setX(aLogicalSize.getX());
322 : : }
323 : :
324 [ + - ][ - + ]: 262 : if(0.0 != getSize().getY())
325 : : {
326 [ # # ][ # # ]: 0 : if(getSize().getY() < 0.0)
327 : : {
328 [ # # ]: 0 : aBitmapSize.setY(aBitmapSize.getY() * (getSize().getY() * -0.01));
329 : : }
330 : : else
331 : : {
332 [ # # ]: 0 : aBitmapSize.setY(getSize().getY());
333 : : }
334 : : }
335 : : else
336 : : {
337 : 262 : aBitmapSize.setY(aLogicalSize.getY());
338 : : }
339 : :
340 : : // get values, force to centered if necessary
341 [ + - ][ + - ]: 262 : const basegfx::B2DVector aRectPoint(getTiling() ? getRectPoint() : basegfx::B2DVector(0.0, 0.0));
[ + - ]
342 : :
343 : : // position changes X
344 [ + - ]: 262 : if(0.0 == aRectPoint.getX())
345 : : {
346 : 262 : aBitmapTopLeft.setX((fRangeWidth - aBitmapSize.getX()) * 0.5);
347 : : }
348 [ # # ]: 0 : else if(1.0 == aRectPoint.getX())
349 : : {
350 : 0 : aBitmapTopLeft.setX(fRangeWidth - aBitmapSize.getX());
351 : : }
352 : :
353 [ + - ][ + - ]: 262 : if(getTiling() && 0.0 != getOffsetPosition().getX())
[ + - ][ - + ]
[ - + ]
354 : : {
355 [ # # ]: 0 : aBitmapTopLeft.setX(aBitmapTopLeft.getX() + (aBitmapSize.getX() * (getOffsetPosition().getX() * 0.01)));
356 : : }
357 : :
358 : : // position changes Y
359 [ + - ]: 262 : if(0.0 == aRectPoint.getY())
360 : : {
361 : 262 : aBitmapTopLeft.setY((fRangeHeight - aBitmapSize.getY()) * 0.5);
362 : : }
363 [ # # ]: 0 : else if(1.0 == aRectPoint.getY())
364 : : {
365 : 0 : aBitmapTopLeft.setY(fRangeHeight - aBitmapSize.getY());
366 : : }
367 : :
368 [ + - ][ + - ]: 262 : if(getTiling() && 0.0 != getOffsetPosition().getY())
[ + - ][ - + ]
[ - + ]
369 : : {
370 [ # # ]: 0 : aBitmapTopLeft.setY(aBitmapTopLeft.getY() + (aBitmapSize.getY() * (getOffsetPosition().getY() * 0.01)));
371 : : }
372 : :
373 : : // apply expand
374 [ - + ]: 262 : if(bExpandWidth)
375 : : {
376 : 0 : aBitmapSize.setX(aBitmapSize.getX() * 2.0);
377 : : }
378 : :
379 [ - + ]: 262 : if(bExpandHeight)
380 : : {
381 : 0 : aBitmapSize.setY(aBitmapSize.getY() * 2.0);
382 : : }
383 : :
384 : : // apply bitmap size scaling to unit rectangle
385 : 262 : aBitmapTopLeft.setX(aBitmapTopLeft.getX() / fRangeWidth);
386 : 262 : aBitmapTopLeft.setY(aBitmapTopLeft.getY() / fRangeHeight);
387 : 262 : aBitmapSize.setX(aBitmapSize.getX() / fRangeWidth);
388 : 262 : aBitmapSize.setY(aBitmapSize.getY() / fRangeHeight);
389 : : }
390 : :
391 [ + - ][ + - ]: 262 : return FillBitmapAttribute(BitmapEx(aBitmap), aBitmapTopLeft, aBitmapSize, getTiling());
[ + - ][ + - ]
[ + - ]
392 : : }
393 : : } // end of namespace attribute
394 : : } // end of namespace drawinglayer
395 : :
396 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|