1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <drawinglayer/primitive2d/discreteshadowprimitive2d.hxx>
21 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
22 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
23 #include <basegfx/matrix/b2dhommatrixtools.hxx>
24 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
25 #include <drawinglayer/geometry/viewinformation2d.hxx>
27 //////////////////////////////////////////////////////////////////////////////
29 namespace drawinglayer
33 DiscreteShadow::DiscreteShadow(const BitmapEx
& rBitmapEx
)
34 : maBitmapEx(rBitmapEx
),
44 const Size
& rBitmapSize
= getBitmapEx().GetSizePixel();
46 if(rBitmapSize
.Width() != rBitmapSize
.Height() || rBitmapSize
.Width() < 7)
48 OSL_ENSURE(false, "DiscreteShadowPrimitive2D: wrong bitmap format (!)");
49 maBitmapEx
= BitmapEx();
53 const BitmapEx
& DiscreteShadow::getTopLeft() const
55 if(maTopLeft
.IsEmpty())
57 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
58 const_cast< DiscreteShadow
* >(this)->maTopLeft
= getBitmapEx();
59 const_cast< DiscreteShadow
* >(this)->maTopLeft
.Crop(
60 Rectangle(Point(0,0),Size(nQuarter
*2+1,nQuarter
*2+1)));
66 const BitmapEx
& DiscreteShadow::getTop() const
70 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
71 const_cast< DiscreteShadow
* >(this)->maTop
= getBitmapEx();
72 const_cast< DiscreteShadow
* >(this)->maTop
.Crop(
73 Rectangle(Point(nQuarter
*2+1,0),Size(1,nQuarter
+1)));
79 const BitmapEx
& DiscreteShadow::getTopRight() const
81 if(maTopRight
.IsEmpty())
83 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
84 const_cast< DiscreteShadow
* >(this)->maTopRight
= getBitmapEx();
85 const_cast< DiscreteShadow
* >(this)->maTopRight
.Crop(
86 Rectangle(Point(nQuarter
*2+2,0),Size(nQuarter
*2+1,nQuarter
*2+1)));
92 const BitmapEx
& DiscreteShadow::getRight() const
96 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
97 const_cast< DiscreteShadow
* >(this)->maRight
= getBitmapEx();
98 const_cast< DiscreteShadow
* >(this)->maRight
.Crop(
99 Rectangle(Point(nQuarter
*3+2,nQuarter
*2+1),Size(nQuarter
+1,1)));
105 const BitmapEx
& DiscreteShadow::getBottomRight() const
107 if(maBottomRight
.IsEmpty())
109 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
110 const_cast< DiscreteShadow
* >(this)->maBottomRight
= getBitmapEx();
111 const_cast< DiscreteShadow
* >(this)->maBottomRight
.Crop(
112 Rectangle(Point(nQuarter
*2+2,nQuarter
*2+2),Size(nQuarter
*2+1,nQuarter
*2+1)));
115 return maBottomRight
;
118 const BitmapEx
& DiscreteShadow::getBottom() const
120 if(maBottom
.IsEmpty())
122 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
123 const_cast< DiscreteShadow
* >(this)->maBottom
= getBitmapEx();
124 const_cast< DiscreteShadow
* >(this)->maBottom
.Crop(
125 Rectangle(Point(nQuarter
*2+1,nQuarter
*3+2),Size(1,nQuarter
+1)));
131 const BitmapEx
& DiscreteShadow::getBottomLeft() const
133 if(maBottomLeft
.IsEmpty())
135 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
136 const_cast< DiscreteShadow
* >(this)->maBottomLeft
= getBitmapEx();
137 const_cast< DiscreteShadow
* >(this)->maBottomLeft
.Crop(
138 Rectangle(Point(0,nQuarter
*2+2),Size(nQuarter
*2+1,nQuarter
*2+1)));
144 const BitmapEx
& DiscreteShadow::getLeft() const
148 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
149 const_cast< DiscreteShadow
* >(this)->maLeft
= getBitmapEx();
150 const_cast< DiscreteShadow
* >(this)->maLeft
.Crop(
151 Rectangle(Point(0,nQuarter
*2+1),Size(nQuarter
+1,1)));
157 } // end of namespace primitive2d
158 } // end of namespace drawinglayer
160 //////////////////////////////////////////////////////////////////////////////
162 namespace drawinglayer
164 namespace primitive2d
166 Primitive2DSequence
DiscreteShadowPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D
& /*rViewInformation*/) const
168 Primitive2DSequence xRetval
;
170 if(!getDiscreteShadow().getBitmapEx().IsEmpty())
172 const sal_Int32
nQuarter((getDiscreteShadow().getBitmapEx().GetSizePixel().Width() - 3) >> 2);
173 const basegfx::B2DVector
aScale(getTransform() * basegfx::B2DVector(1.0, 1.0));
174 const double fSingleX(getDiscreteUnit() / aScale
.getX());
175 const double fSingleY(getDiscreteUnit() / aScale
.getY());
176 const double fBorderX(fSingleX
* nQuarter
);
177 const double fBorderY(fSingleY
* nQuarter
);
178 const double fBigLenX((fBorderX
* 2.0) + fSingleX
);
179 const double fBigLenY((fBorderY
* 2.0) + fSingleY
);
184 xRetval
[0] = Primitive2DReference(
185 new BitmapPrimitive2D(
186 getDiscreteShadow().getTopLeft(),
187 basegfx::tools::createScaleTranslateB2DHomMatrix(
194 xRetval
[1] = Primitive2DReference(
195 new BitmapPrimitive2D(
196 getDiscreteShadow().getTop(),
197 basegfx::tools::createScaleTranslateB2DHomMatrix(
198 1.0 - (2.0 * fBorderX
) - fSingleX
,
204 xRetval
[2] = Primitive2DReference(
205 new BitmapPrimitive2D(
206 getDiscreteShadow().getTopRight(),
207 basegfx::tools::createScaleTranslateB2DHomMatrix(
214 xRetval
[3] = Primitive2DReference(
215 new BitmapPrimitive2D(
216 getDiscreteShadow().getRight(),
217 basegfx::tools::createScaleTranslateB2DHomMatrix(
219 1.0 - (2.0 * fBorderY
) - fSingleY
,
221 fBorderY
+ fSingleY
)));
224 xRetval
[4] = Primitive2DReference(
225 new BitmapPrimitive2D(
226 getDiscreteShadow().getBottomRight(),
227 basegfx::tools::createScaleTranslateB2DHomMatrix(
234 xRetval
[5] = Primitive2DReference(
235 new BitmapPrimitive2D(
236 getDiscreteShadow().getBottom(),
237 basegfx::tools::createScaleTranslateB2DHomMatrix(
238 1.0 - (2.0 * fBorderX
) - fSingleX
,
244 xRetval
[6] = Primitive2DReference(
245 new BitmapPrimitive2D(
246 getDiscreteShadow().getBottomLeft(),
247 basegfx::tools::createScaleTranslateB2DHomMatrix(
254 xRetval
[7] = Primitive2DReference(
255 new BitmapPrimitive2D(
256 getDiscreteShadow().getLeft(),
257 basegfx::tools::createScaleTranslateB2DHomMatrix(
259 1.0 - (2.0 * fBorderY
) - fSingleY
,
261 fBorderY
+ fSingleY
)));
263 // put all in object transformation to get to target positions
264 const Primitive2DReference
xTransformed(
265 new TransformPrimitive2D(
269 xRetval
= Primitive2DSequence(&xTransformed
, 1);
275 DiscreteShadowPrimitive2D::DiscreteShadowPrimitive2D(
276 const basegfx::B2DHomMatrix
& rTransform
,
277 const DiscreteShadow
& rDiscreteShadow
)
278 : DiscreteMetricDependentPrimitive2D(),
279 maTransform(rTransform
),
280 maDiscreteShadow(rDiscreteShadow
)
284 bool DiscreteShadowPrimitive2D::operator==(const BasePrimitive2D
& rPrimitive
) const
286 if(DiscreteMetricDependentPrimitive2D::operator==(rPrimitive
))
288 const DiscreteShadowPrimitive2D
& rCompare
= (DiscreteShadowPrimitive2D
&)rPrimitive
;
290 return (getTransform() == rCompare
.getTransform()
291 && getDiscreteShadow() == rCompare
.getDiscreteShadow());
297 basegfx::B2DRange
DiscreteShadowPrimitive2D::getB2DRange(const geometry::ViewInformation2D
& rViewInformation
) const
299 if(getDiscreteShadow().getBitmapEx().IsEmpty())
301 // no graphics without valid bitmap definition
302 return basegfx::B2DRange();
306 // prepare normal objectrange
307 basegfx::B2DRange
aRetval(0.0, 0.0, 1.0, 1.0);
308 aRetval
.transform(getTransform());
310 // extract discrete shadow size and grow
311 const basegfx::B2DVector
aScale(rViewInformation
.getViewTransformation() * basegfx::B2DVector(1.0, 1.0));
312 const sal_Int32
nQuarter((getDiscreteShadow().getBitmapEx().GetSizePixel().Width() - 3) >> 2);
313 const double fGrowX((1.0 / aScale
.getX()) * nQuarter
);
314 const double fGrowY((1.0 / aScale
.getY()) * nQuarter
);
315 aRetval
.grow(std::max(fGrowX
, fGrowY
));
322 ImplPrimitive2DIDBlock(DiscreteShadowPrimitive2D
, PRIMITIVE2D_ID_DISCRETESHADOWPRIMITIVE2D
)
324 } // end of namespace primitive2d
325 } // end of namespace drawinglayer
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */