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_drawinglayer.hxx"
26 
27 #include <drawinglayer/primitive2d/discreteshadowprimitive2d.hxx>
28 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
29 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
30 #include <basegfx/matrix/b2dhommatrixtools.hxx>
31 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
32 #include <drawinglayer/geometry/viewinformation2d.hxx>
33 
34 //////////////////////////////////////////////////////////////////////////////
35 
36 namespace drawinglayer
37 {
38 	namespace primitive2d
39 	{
40 		DiscreteShadow::DiscreteShadow(const BitmapEx& rBitmapEx)
41 		:	maBitmapEx(rBitmapEx),
42 			maTopLeft(),
43 			maTop(),
44 			maTopRight(),
45 			maRight(),
46 			maBottomRight(),
47 			maBottom(),
48 			maBottomLeft(),
49 			maLeft()
50 		{
51             const Size& rBitmapSize = getBitmapEx().GetSizePixel();
52 
53             if(rBitmapSize.Width() != rBitmapSize.Height() || rBitmapSize.Width() < 7)
54             {
55                 OSL_ENSURE(false, "DiscreteShadowPrimitive2D: wrong bitmap format (!)");
56                 maBitmapEx = BitmapEx();
57             }
58 		}
59 
60 		const BitmapEx& DiscreteShadow::getTopLeft() const
61 		{
62 			if(maTopLeft.IsEmpty())
63 			{
64 				const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
65 				const_cast< DiscreteShadow* >(this)->maTopLeft = getBitmapEx();
66 				const_cast< DiscreteShadow* >(this)->maTopLeft.Crop(
67                     Rectangle(Point(0,0),Size(nQuarter*2+1,nQuarter*2+1)));
68 			}
69 
70 			return maTopLeft;
71 		}
72 
73 		const BitmapEx& DiscreteShadow::getTop() const
74 		{
75 			if(maTop.IsEmpty())
76 			{
77 				const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
78 				const_cast< DiscreteShadow* >(this)->maTop = getBitmapEx();
79 				const_cast< DiscreteShadow* >(this)->maTop.Crop(
80                     Rectangle(Point(nQuarter*2+1,0),Size(1,nQuarter+1)));
81 			}
82 
83 			return maTop;
84 		}
85 
86 		const BitmapEx& DiscreteShadow::getTopRight() const
87 		{
88 			if(maTopRight.IsEmpty())
89 			{
90 				const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
91 				const_cast< DiscreteShadow* >(this)->maTopRight = getBitmapEx();
92 				const_cast< DiscreteShadow* >(this)->maTopRight.Crop(
93                     Rectangle(Point(nQuarter*2+2,0),Size(nQuarter*2+1,nQuarter*2+1)));
94 			}
95 
96 			return maTopRight;
97 		}
98 
99 		const BitmapEx& DiscreteShadow::getRight() const
100 		{
101 			if(maRight.IsEmpty())
102 			{
103 				const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
104 				const_cast< DiscreteShadow* >(this)->maRight = getBitmapEx();
105 				const_cast< DiscreteShadow* >(this)->maRight.Crop(
106                     Rectangle(Point(nQuarter*3+2,nQuarter*2+1),Size(nQuarter+1,1)));
107 			}
108 
109 			return maRight;
110 		}
111 
112 		const BitmapEx& DiscreteShadow::getBottomRight() const
113 		{
114 			if(maBottomRight.IsEmpty())
115 			{
116 				const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
117 				const_cast< DiscreteShadow* >(this)->maBottomRight = getBitmapEx();
118 				const_cast< DiscreteShadow* >(this)->maBottomRight.Crop(
119                     Rectangle(Point(nQuarter*2+2,nQuarter*2+2),Size(nQuarter*2+1,nQuarter*2+1)));
120 			}
121 
122 			return maBottomRight;
123 		}
124 
125 		const BitmapEx& DiscreteShadow::getBottom() const
126 		{
127 			if(maBottom.IsEmpty())
128 			{
129 				const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
130 				const_cast< DiscreteShadow* >(this)->maBottom = getBitmapEx();
131 				const_cast< DiscreteShadow* >(this)->maBottom.Crop(
132                     Rectangle(Point(nQuarter*2+1,nQuarter*3+2),Size(1,nQuarter+1)));
133 			}
134 
135 			return maBottom;
136 		}
137 
138 		const BitmapEx& DiscreteShadow::getBottomLeft() const
139 		{
140 			if(maBottomLeft.IsEmpty())
141 			{
142 				const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
143 				const_cast< DiscreteShadow* >(this)->maBottomLeft = getBitmapEx();
144 				const_cast< DiscreteShadow* >(this)->maBottomLeft.Crop(
145                     Rectangle(Point(0,nQuarter*2+2),Size(nQuarter*2+1,nQuarter*2+1)));
146 			}
147 
148 			return maBottomLeft;
149 		}
150 
151 		const BitmapEx& DiscreteShadow::getLeft() const
152 		{
153 			if(maLeft.IsEmpty())
154 			{
155 				const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2);
156 				const_cast< DiscreteShadow* >(this)->maLeft = getBitmapEx();
157 				const_cast< DiscreteShadow* >(this)->maLeft.Crop(
158                     Rectangle(Point(0,nQuarter*2+1),Size(nQuarter+1,1)));
159 			}
160 
161 			return maLeft;
162 		}
163 
164 	} // end of namespace primitive2d
165 } // end of namespace drawinglayer
166 
167 //////////////////////////////////////////////////////////////////////////////
168 
169 namespace drawinglayer
170 {
171 	namespace primitive2d
172 	{
173 		Primitive2DSequence DiscreteShadowPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
174         {
175             Primitive2DSequence xRetval;
176 
177             if(!getDiscreteShadow().getBitmapEx().IsEmpty())
178             {
179 				const sal_Int32 nQuarter((getDiscreteShadow().getBitmapEx().GetSizePixel().Width() - 3) >> 2);
180                 const basegfx::B2DVector aScale(getTransform() * basegfx::B2DVector(1.0, 1.0));
181                 const double fSingleX(getDiscreteUnit() / aScale.getX());
182                 const double fSingleY(getDiscreteUnit() / aScale.getY());
183                 const double fBorderX(fSingleX * nQuarter);
184                 const double fBorderY(fSingleY * nQuarter);
185                 const double fBigLenX((fBorderX * 2.0) + fSingleX);
186                 const double fBigLenY((fBorderY * 2.0) + fSingleY);
187 
188                 xRetval.realloc(8);
189 
190 				// TopLeft
191 				xRetval[0] = Primitive2DReference(
192 					new BitmapPrimitive2D(
193 						getDiscreteShadow().getTopLeft(),
194 						basegfx::tools::createScaleTranslateB2DHomMatrix(
195 							fBigLenX,
196                             fBigLenY,
197 							-fBorderX,
198                             -fBorderY)));
199 
200 				// Top
201 				xRetval[1] = Primitive2DReference(
202 					new BitmapPrimitive2D(
203 						getDiscreteShadow().getTop(),
204 						basegfx::tools::createScaleTranslateB2DHomMatrix(
205 							1.0 - (2.0 * fBorderX) - fSingleX,
206                             fBorderY + fSingleY,
207 							fBorderX + fSingleX,
208                             -fBorderY)));
209 
210 				// TopRight
211 				xRetval[2] = Primitive2DReference(
212 					new BitmapPrimitive2D(
213 						getDiscreteShadow().getTopRight(),
214 						basegfx::tools::createScaleTranslateB2DHomMatrix(
215 							fBigLenX,
216                             fBigLenY,
217 							1.0 - fBorderX,
218                             -fBorderY)));
219 
220 				// Right
221 				xRetval[3] = Primitive2DReference(
222 					new BitmapPrimitive2D(
223 						getDiscreteShadow().getRight(),
224 						basegfx::tools::createScaleTranslateB2DHomMatrix(
225 							fBorderX + fSingleX,
226                             1.0 - (2.0 * fBorderY) - fSingleY,
227 							1.0,
228                             fBorderY + fSingleY)));
229 
230 				// BottomRight
231 				xRetval[4] = Primitive2DReference(
232 					new BitmapPrimitive2D(
233 						getDiscreteShadow().getBottomRight(),
234 						basegfx::tools::createScaleTranslateB2DHomMatrix(
235 							fBigLenX,
236                             fBigLenY,
237 							1.0 - fBorderX,
238                             1.0 - fBorderY)));
239 
240 				// Bottom
241 				xRetval[5] = Primitive2DReference(
242 					new BitmapPrimitive2D(
243 						getDiscreteShadow().getBottom(),
244 						basegfx::tools::createScaleTranslateB2DHomMatrix(
245 							1.0 - (2.0 * fBorderX) - fSingleX,
246                             fBorderY + fSingleY,
247 							fBorderX + fSingleX,
248                             1.0)));
249 
250 				// BottomLeft
251 				xRetval[6] = Primitive2DReference(
252 					new BitmapPrimitive2D(
253 						getDiscreteShadow().getBottomLeft(),
254 						basegfx::tools::createScaleTranslateB2DHomMatrix(
255 							fBigLenX,
256                             fBigLenY,
257 							-fBorderX,
258                             1.0 - fBorderY)));
259 
260 				// Left
261 				xRetval[7] = Primitive2DReference(
262 					new BitmapPrimitive2D(
263 						getDiscreteShadow().getLeft(),
264 						basegfx::tools::createScaleTranslateB2DHomMatrix(
265 							fBorderX + fSingleX,
266                             1.0 - (2.0 * fBorderY) - fSingleY,
267 							-fBorderX,
268                             fBorderY + fSingleY)));
269 
270 				// put all in object transformation to get to target positions
271 				const Primitive2DReference xTransformed(
272 					new TransformPrimitive2D(
273 						getTransform(),
274 						xRetval));
275 
276 				xRetval = Primitive2DSequence(&xTransformed, 1);
277             }
278 
279             return xRetval;
280         }
281 
282 		DiscreteShadowPrimitive2D::DiscreteShadowPrimitive2D(
283 			const basegfx::B2DHomMatrix& rTransform,
284 			const DiscreteShadow& rDiscreteShadow)
285         :   DiscreteMetricDependentPrimitive2D(),
286             maTransform(rTransform),
287             maDiscreteShadow(rDiscreteShadow)
288         {
289         }
290 
291         bool DiscreteShadowPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
292 		{
293 			if(DiscreteMetricDependentPrimitive2D::operator==(rPrimitive))
294 			{
295 				const DiscreteShadowPrimitive2D& rCompare = (DiscreteShadowPrimitive2D&)rPrimitive;
296 
297 				return (getTransform() == rCompare.getTransform()
298 					&& getDiscreteShadow() == rCompare.getDiscreteShadow());
299 			}
300 
301 			return false;
302 		}
303 
304 		basegfx::B2DRange DiscreteShadowPrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const
305 		{
306             if(getDiscreteShadow().getBitmapEx().IsEmpty())
307             {
308 				// no graphics without valid bitmap definition
309                 return basegfx::B2DRange();
310             }
311             else
312             {
313 				// prepare normal objectrange
314 			    basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
315 			    aRetval.transform(getTransform());
316 
317 				// extract discrete shadow size and grow
318                 const basegfx::B2DVector aScale(rViewInformation.getViewTransformation() * basegfx::B2DVector(1.0, 1.0));
319 				const sal_Int32 nQuarter((getDiscreteShadow().getBitmapEx().GetSizePixel().Width() - 3) >> 2);
320                 const double fGrowX((1.0 / aScale.getX()) * nQuarter);
321                 const double fGrowY((1.0 / aScale.getY()) * nQuarter);
322 				aRetval.grow(std::max(fGrowX, fGrowY));
323 
324 			    return aRetval;
325             }
326 		}
327 
328 		// provide unique ID
329 		ImplPrimitrive2DIDBlock(DiscreteShadowPrimitive2D, PRIMITIVE2D_ID_DISCRETESHADOWPRIMITIVE2D)
330 
331 	} // end of namespace primitive2d
332 } // end of namespace drawinglayer
333 
334 //////////////////////////////////////////////////////////////////////////////
335 // eof
336