1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_drawinglayer.hxx" 30 31 #include <drawinglayer/primitive2d/discreteshadowprimitive2d.hxx> 32 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx> 33 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx> 34 #include <basegfx/matrix/b2dhommatrixtools.hxx> 35 #include <drawinglayer/primitive2d/transformprimitive2d.hxx> 36 #include <drawinglayer/geometry/viewinformation2d.hxx> 37 38 ////////////////////////////////////////////////////////////////////////////// 39 40 namespace drawinglayer 41 { 42 namespace primitive2d 43 { 44 DiscreteShadow::DiscreteShadow(const BitmapEx& rBitmapEx) 45 : maBitmapEx(rBitmapEx), 46 maTopLeft(), 47 maTop(), 48 maTopRight(), 49 maRight(), 50 maBottomRight(), 51 maBottom(), 52 maBottomLeft(), 53 maLeft() 54 { 55 const Size& rBitmapSize = getBitmapEx().GetSizePixel(); 56 57 if(rBitmapSize.Width() != rBitmapSize.Height() || rBitmapSize.Width() < 7) 58 { 59 OSL_ENSURE(false, "DiscreteShadowPrimitive2D: wrong bitmap format (!)"); 60 maBitmapEx = BitmapEx(); 61 } 62 } 63 64 const BitmapEx& DiscreteShadow::getTopLeft() const 65 { 66 if(maTopLeft.IsEmpty()) 67 { 68 const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2); 69 const_cast< DiscreteShadow* >(this)->maTopLeft = getBitmapEx(); 70 const_cast< DiscreteShadow* >(this)->maTopLeft.Crop( 71 Rectangle(Point(0,0),Size(nQuarter*2+1,nQuarter*2+1))); 72 } 73 74 return maTopLeft; 75 } 76 77 const BitmapEx& DiscreteShadow::getTop() const 78 { 79 if(maTop.IsEmpty()) 80 { 81 const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2); 82 const_cast< DiscreteShadow* >(this)->maTop = getBitmapEx(); 83 const_cast< DiscreteShadow* >(this)->maTop.Crop( 84 Rectangle(Point(nQuarter*2+1,0),Size(1,nQuarter+1))); 85 } 86 87 return maTop; 88 } 89 90 const BitmapEx& DiscreteShadow::getTopRight() const 91 { 92 if(maTopRight.IsEmpty()) 93 { 94 const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2); 95 const_cast< DiscreteShadow* >(this)->maTopRight = getBitmapEx(); 96 const_cast< DiscreteShadow* >(this)->maTopRight.Crop( 97 Rectangle(Point(nQuarter*2+2,0),Size(nQuarter*2+1,nQuarter*2+1))); 98 } 99 100 return maTopRight; 101 } 102 103 const BitmapEx& DiscreteShadow::getRight() const 104 { 105 if(maRight.IsEmpty()) 106 { 107 const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2); 108 const_cast< DiscreteShadow* >(this)->maRight = getBitmapEx(); 109 const_cast< DiscreteShadow* >(this)->maRight.Crop( 110 Rectangle(Point(nQuarter*3+2,nQuarter*2+1),Size(nQuarter+1,1))); 111 } 112 113 return maRight; 114 } 115 116 const BitmapEx& DiscreteShadow::getBottomRight() const 117 { 118 if(maBottomRight.IsEmpty()) 119 { 120 const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2); 121 const_cast< DiscreteShadow* >(this)->maBottomRight = getBitmapEx(); 122 const_cast< DiscreteShadow* >(this)->maBottomRight.Crop( 123 Rectangle(Point(nQuarter*2+2,nQuarter*2+2),Size(nQuarter*2+1,nQuarter*2+1))); 124 } 125 126 return maBottomRight; 127 } 128 129 const BitmapEx& DiscreteShadow::getBottom() const 130 { 131 if(maBottom.IsEmpty()) 132 { 133 const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2); 134 const_cast< DiscreteShadow* >(this)->maBottom = getBitmapEx(); 135 const_cast< DiscreteShadow* >(this)->maBottom.Crop( 136 Rectangle(Point(nQuarter*2+1,nQuarter*3+2),Size(1,nQuarter+1))); 137 } 138 139 return maBottom; 140 } 141 142 const BitmapEx& DiscreteShadow::getBottomLeft() const 143 { 144 if(maBottomLeft.IsEmpty()) 145 { 146 const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2); 147 const_cast< DiscreteShadow* >(this)->maBottomLeft = getBitmapEx(); 148 const_cast< DiscreteShadow* >(this)->maBottomLeft.Crop( 149 Rectangle(Point(0,nQuarter*2+2),Size(nQuarter*2+1,nQuarter*2+1))); 150 } 151 152 return maBottomLeft; 153 } 154 155 const BitmapEx& DiscreteShadow::getLeft() const 156 { 157 if(maLeft.IsEmpty()) 158 { 159 const sal_Int32 nQuarter((getBitmapEx().GetSizePixel().Width() - 3) >> 2); 160 const_cast< DiscreteShadow* >(this)->maLeft = getBitmapEx(); 161 const_cast< DiscreteShadow* >(this)->maLeft.Crop( 162 Rectangle(Point(0,nQuarter*2+1),Size(nQuarter+1,1))); 163 } 164 165 return maLeft; 166 } 167 168 } // end of namespace primitive2d 169 } // end of namespace drawinglayer 170 171 ////////////////////////////////////////////////////////////////////////////// 172 173 namespace drawinglayer 174 { 175 namespace primitive2d 176 { 177 Primitive2DSequence DiscreteShadowPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const 178 { 179 Primitive2DSequence xRetval; 180 181 if(!getDiscreteShadow().getBitmapEx().IsEmpty()) 182 { 183 const sal_Int32 nQuarter((getDiscreteShadow().getBitmapEx().GetSizePixel().Width() - 3) >> 2); 184 const basegfx::B2DVector aScale(getTransform() * basegfx::B2DVector(1.0, 1.0)); 185 const double fSingleX(getDiscreteUnit() / aScale.getX()); 186 const double fSingleY(getDiscreteUnit() / aScale.getY()); 187 const double fBorderX(fSingleX * nQuarter); 188 const double fBorderY(fSingleY * nQuarter); 189 const double fBigLenX((fBorderX * 2.0) + fSingleX); 190 const double fBigLenY((fBorderY * 2.0) + fSingleY); 191 192 xRetval.realloc(8); 193 194 // TopLeft 195 xRetval[0] = Primitive2DReference( 196 new BitmapPrimitive2D( 197 getDiscreteShadow().getTopLeft(), 198 basegfx::tools::createScaleTranslateB2DHomMatrix( 199 fBigLenX, 200 fBigLenY, 201 -fBorderX, 202 -fBorderY))); 203 204 // Top 205 xRetval[1] = Primitive2DReference( 206 new BitmapPrimitive2D( 207 getDiscreteShadow().getTop(), 208 basegfx::tools::createScaleTranslateB2DHomMatrix( 209 1.0 - (2.0 * fBorderX) - fSingleX, 210 fBorderY + fSingleY, 211 fBorderX + fSingleX, 212 -fBorderY))); 213 214 // TopRight 215 xRetval[2] = Primitive2DReference( 216 new BitmapPrimitive2D( 217 getDiscreteShadow().getTopRight(), 218 basegfx::tools::createScaleTranslateB2DHomMatrix( 219 fBigLenX, 220 fBigLenY, 221 1.0 - fBorderX, 222 -fBorderY))); 223 224 // Right 225 xRetval[3] = Primitive2DReference( 226 new BitmapPrimitive2D( 227 getDiscreteShadow().getRight(), 228 basegfx::tools::createScaleTranslateB2DHomMatrix( 229 fBorderX + fSingleX, 230 1.0 - (2.0 * fBorderY) - fSingleY, 231 1.0, 232 fBorderY + fSingleY))); 233 234 // BottomRight 235 xRetval[4] = Primitive2DReference( 236 new BitmapPrimitive2D( 237 getDiscreteShadow().getBottomRight(), 238 basegfx::tools::createScaleTranslateB2DHomMatrix( 239 fBigLenX, 240 fBigLenY, 241 1.0 - fBorderX, 242 1.0 - fBorderY))); 243 244 // Bottom 245 xRetval[5] = Primitive2DReference( 246 new BitmapPrimitive2D( 247 getDiscreteShadow().getBottom(), 248 basegfx::tools::createScaleTranslateB2DHomMatrix( 249 1.0 - (2.0 * fBorderX) - fSingleX, 250 fBorderY + fSingleY, 251 fBorderX + fSingleX, 252 1.0))); 253 254 // BottomLeft 255 xRetval[6] = Primitive2DReference( 256 new BitmapPrimitive2D( 257 getDiscreteShadow().getBottomLeft(), 258 basegfx::tools::createScaleTranslateB2DHomMatrix( 259 fBigLenX, 260 fBigLenY, 261 -fBorderX, 262 1.0 - fBorderY))); 263 264 // Left 265 xRetval[7] = Primitive2DReference( 266 new BitmapPrimitive2D( 267 getDiscreteShadow().getLeft(), 268 basegfx::tools::createScaleTranslateB2DHomMatrix( 269 fBorderX + fSingleX, 270 1.0 - (2.0 * fBorderY) - fSingleY, 271 -fBorderX, 272 fBorderY + fSingleY))); 273 274 // put all in object transformation to get to target positions 275 const Primitive2DReference xTransformed( 276 new TransformPrimitive2D( 277 getTransform(), 278 xRetval)); 279 280 xRetval = Primitive2DSequence(&xTransformed, 1); 281 } 282 283 return xRetval; 284 } 285 286 DiscreteShadowPrimitive2D::DiscreteShadowPrimitive2D( 287 const basegfx::B2DHomMatrix& rTransform, 288 const DiscreteShadow& rDiscreteShadow) 289 : DiscreteMetricDependentPrimitive2D(), 290 maTransform(rTransform), 291 maDiscreteShadow(rDiscreteShadow) 292 { 293 } 294 295 bool DiscreteShadowPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const 296 { 297 if(DiscreteMetricDependentPrimitive2D::operator==(rPrimitive)) 298 { 299 const DiscreteShadowPrimitive2D& rCompare = (DiscreteShadowPrimitive2D&)rPrimitive; 300 301 return (getTransform() == rCompare.getTransform() 302 && getDiscreteShadow() == rCompare.getDiscreteShadow()); 303 } 304 305 return false; 306 } 307 308 basegfx::B2DRange DiscreteShadowPrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const 309 { 310 if(getDiscreteShadow().getBitmapEx().IsEmpty()) 311 { 312 // no graphics without valid bitmap definition 313 return basegfx::B2DRange(); 314 } 315 else 316 { 317 // prepare normal objectrange 318 basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0); 319 aRetval.transform(getTransform()); 320 321 // extract discrete shadow size and grow 322 const basegfx::B2DVector aScale(rViewInformation.getViewTransformation() * basegfx::B2DVector(1.0, 1.0)); 323 const sal_Int32 nQuarter((getDiscreteShadow().getBitmapEx().GetSizePixel().Width() - 3) >> 2); 324 const double fGrowX((1.0 / aScale.getX()) * nQuarter); 325 const double fGrowY((1.0 / aScale.getY()) * nQuarter); 326 aRetval.grow(std::max(fGrowX, fGrowY)); 327 328 return aRetval; 329 } 330 } 331 332 // provide unique ID 333 ImplPrimitrive2DIDBlock(DiscreteShadowPrimitive2D, PRIMITIVE2D_ID_DISCRETESHADOWPRIMITIVE2D) 334 335 } // end of namespace primitive2d 336 } // end of namespace drawinglayer 337 338 ////////////////////////////////////////////////////////////////////////////// 339 // eof 340