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>
28 namespace drawinglayer
32 DiscreteShadow::DiscreteShadow(const BitmapEx
& rBitmapEx
)
33 : maBitmapEx(rBitmapEx
),
43 const Size
& rBitmapSize
= getBitmapEx().GetSizePixel();
45 if(rBitmapSize
.Width() != rBitmapSize
.Height() || rBitmapSize
.Width() < 7)
47 OSL_ENSURE(false, "DiscreteShadowPrimitive2D: wrong bitmap format (!)");
48 maBitmapEx
= BitmapEx();
52 const BitmapEx
& DiscreteShadow::getTopLeft() const
54 if(maTopLeft
.IsEmpty())
56 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
57 const_cast< DiscreteShadow
* >(this)->maTopLeft
= getBitmapEx();
58 const_cast< DiscreteShadow
* >(this)->maTopLeft
.Crop(
59 ::tools::Rectangle(Point(0, 0), Size((nQuarter
* 2) + 1, (nQuarter
* 2) + 1)));
65 const BitmapEx
& DiscreteShadow::getTop() const
69 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
70 const_cast< DiscreteShadow
* >(this)->maTop
= getBitmapEx();
71 const_cast< DiscreteShadow
* >(this)->maTop
.Crop(
72 ::tools::Rectangle(Point((nQuarter
* 2) + 1, 0), Size(1, nQuarter
)));
78 const BitmapEx
& DiscreteShadow::getTopRight() const
80 if(maTopRight
.IsEmpty())
82 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
83 const_cast< DiscreteShadow
* >(this)->maTopRight
= getBitmapEx();
84 const_cast< DiscreteShadow
* >(this)->maTopRight
.Crop(
85 ::tools::Rectangle(Point((nQuarter
* 2) + 2, 0), Size((nQuarter
* 2) + 1, (nQuarter
* 2) + 1)));
91 const BitmapEx
& DiscreteShadow::getRight() const
95 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
96 const_cast< DiscreteShadow
* >(this)->maRight
= getBitmapEx();
97 const_cast< DiscreteShadow
* >(this)->maRight
.Crop(
98 ::tools::Rectangle(Point((nQuarter
* 3) + 3, (nQuarter
* 2) + 1), Size(nQuarter
, 1)));
104 const BitmapEx
& DiscreteShadow::getBottomRight() const
106 if(maBottomRight
.IsEmpty())
108 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
109 const_cast< DiscreteShadow
* >(this)->maBottomRight
= getBitmapEx();
110 const_cast< DiscreteShadow
* >(this)->maBottomRight
.Crop(
111 ::tools::Rectangle(Point((nQuarter
* 2) + 2, (nQuarter
* 2) + 2), Size((nQuarter
* 2) + 1, (nQuarter
* 2) + 1)));
114 return maBottomRight
;
117 const BitmapEx
& DiscreteShadow::getBottom() const
119 if(maBottom
.IsEmpty())
121 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
122 const_cast< DiscreteShadow
* >(this)->maBottom
= getBitmapEx();
123 const_cast< DiscreteShadow
* >(this)->maBottom
.Crop(
124 ::tools::Rectangle(Point((nQuarter
* 2) + 1, (nQuarter
* 3) + 3), Size(1, nQuarter
)));
130 const BitmapEx
& DiscreteShadow::getBottomLeft() const
132 if(maBottomLeft
.IsEmpty())
134 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
135 const_cast< DiscreteShadow
* >(this)->maBottomLeft
= getBitmapEx();
136 const_cast< DiscreteShadow
* >(this)->maBottomLeft
.Crop(
137 ::tools::Rectangle(Point(0, (nQuarter
* 2) + 2), Size((nQuarter
* 2) + 1, (nQuarter
* 2) + 1)));
143 const BitmapEx
& DiscreteShadow::getLeft() const
147 const sal_Int32
nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
148 const_cast< DiscreteShadow
* >(this)->maLeft
= getBitmapEx();
149 const_cast< DiscreteShadow
* >(this)->maLeft
.Crop(
150 ::tools::Rectangle(Point(0, (nQuarter
* 2) + 1), Size(nQuarter
, 1)));
156 } // end of namespace primitive2d
157 } // end of namespace drawinglayer
160 namespace drawinglayer
162 namespace primitive2d
164 void DiscreteShadowPrimitive2D::create2DDecomposition(Primitive2DContainer
& rContainer
, const geometry::ViewInformation2D
& /*rViewInformation*/) const
166 Primitive2DContainer xRetval
;
168 if(!getDiscreteShadow().getBitmapEx().IsEmpty())
170 const sal_Int32
nQuarter((getDiscreteShadow().getBitmapEx().GetSizePixel().Width() - 3) >> 2);
171 const basegfx::B2DVector
aScale(getTransform() * basegfx::B2DVector(1.0, 1.0));
172 const double fSingleX(getDiscreteUnit() / aScale
.getX());
173 const double fSingleY(getDiscreteUnit() / aScale
.getY());
174 const double fBorderX(fSingleX
* nQuarter
);
175 const double fBorderY(fSingleY
* nQuarter
);
176 const double fBigLenX((fBorderX
* 2.0) + fSingleX
);
177 const double fBigLenY((fBorderY
* 2.0) + fSingleY
);
182 xRetval
[0] = Primitive2DReference(
183 new BitmapPrimitive2D(
184 getDiscreteShadow().getTopLeft(),
185 basegfx::utils::createScaleTranslateB2DHomMatrix(
192 xRetval
[1] = Primitive2DReference(
193 new BitmapPrimitive2D(
194 getDiscreteShadow().getTop(),
195 basegfx::utils::createScaleTranslateB2DHomMatrix(
196 1.0 - (2.0 * (fBorderX
+ fSingleX
)) + fSingleX
,
202 xRetval
[2] = Primitive2DReference(
203 new BitmapPrimitive2D(
204 getDiscreteShadow().getTopRight(),
205 basegfx::utils::createScaleTranslateB2DHomMatrix(
212 xRetval
[3] = Primitive2DReference(
213 new BitmapPrimitive2D(
214 getDiscreteShadow().getRight(),
215 basegfx::utils::createScaleTranslateB2DHomMatrix(
217 1.0 - (2.0 * (fBorderY
+ fSingleY
)) + fSingleY
,
219 fBorderY
+ fSingleY
)));
222 xRetval
[4] = Primitive2DReference(
223 new BitmapPrimitive2D(
224 getDiscreteShadow().getBottomRight(),
225 basegfx::utils::createScaleTranslateB2DHomMatrix(
228 1.0 - (fBorderX
+ fSingleX
) + fSingleX
,
229 1.0 - (fBorderY
+ fSingleY
) + fSingleY
)));
232 xRetval
[5] = Primitive2DReference(
233 new BitmapPrimitive2D(
234 getDiscreteShadow().getBottom(),
235 basegfx::utils::createScaleTranslateB2DHomMatrix(
236 1.0 - (2.0 * (fBorderX
+ fSingleX
)) + fSingleX
,
242 xRetval
[6] = Primitive2DReference(
243 new BitmapPrimitive2D(
244 getDiscreteShadow().getBottomLeft(),
245 basegfx::utils::createScaleTranslateB2DHomMatrix(
252 xRetval
[7] = Primitive2DReference(
253 new BitmapPrimitive2D(
254 getDiscreteShadow().getLeft(),
255 basegfx::utils::createScaleTranslateB2DHomMatrix(
257 1.0 - (2.0 * (fBorderY
+ fSingleY
)) + fSingleY
,
259 fBorderY
+ fSingleY
)));
261 // put all in object transformation to get to target positions
262 rContainer
.push_back(
263 new TransformPrimitive2D(
269 DiscreteShadowPrimitive2D::DiscreteShadowPrimitive2D(
270 const basegfx::B2DHomMatrix
& rTransform
,
271 const DiscreteShadow
& rDiscreteShadow
)
272 : DiscreteMetricDependentPrimitive2D(),
273 maTransform(rTransform
),
274 maDiscreteShadow(rDiscreteShadow
)
278 bool DiscreteShadowPrimitive2D::operator==(const BasePrimitive2D
& rPrimitive
) const
280 if(DiscreteMetricDependentPrimitive2D::operator==(rPrimitive
))
282 const DiscreteShadowPrimitive2D
& rCompare
= static_cast<const DiscreteShadowPrimitive2D
&>(rPrimitive
);
284 return (getTransform() == rCompare
.getTransform()
285 && getDiscreteShadow() == rCompare
.getDiscreteShadow());
291 basegfx::B2DRange
DiscreteShadowPrimitive2D::getB2DRange(const geometry::ViewInformation2D
& rViewInformation
) const
293 if(getDiscreteShadow().getBitmapEx().IsEmpty())
295 // no graphics without valid bitmap definition
296 return basegfx::B2DRange();
300 // prepare normal objectrange
301 basegfx::B2DRange
aRetval(0.0, 0.0, 1.0, 1.0);
302 aRetval
.transform(getTransform());
304 // extract discrete shadow size and grow
305 const basegfx::B2DVector
aScale(rViewInformation
.getViewTransformation() * basegfx::B2DVector(1.0, 1.0));
306 const sal_Int32
nQuarter((getDiscreteShadow().getBitmapEx().GetSizePixel().Width() - 3) >> 2);
307 const double fGrowX((1.0 / aScale
.getX()) * nQuarter
);
308 const double fGrowY((1.0 / aScale
.getY()) * nQuarter
);
309 aRetval
.grow(std::max(fGrowX
, fGrowY
));
316 ImplPrimitive2DIDBlock(DiscreteShadowPrimitive2D
, PRIMITIVE2D_ID_DISCRETESHADOWPRIMITIVE2D
)
318 } // end of namespace primitive2d
319 } // end of namespace drawinglayer
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */