1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_sdext.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "PresenterGeometryHelper.hxx" 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include <math.h> 34*cdf0e10cSrcweir #include <algorithm> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir using namespace ::com::sun::star; 37*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir namespace { 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir sal_Int32 Right (const awt::Rectangle& rBox) 42*cdf0e10cSrcweir { 43*cdf0e10cSrcweir return rBox.X + rBox.Width - 1; 44*cdf0e10cSrcweir } 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir sal_Int32 Bottom (const awt::Rectangle& rBox) 47*cdf0e10cSrcweir { 48*cdf0e10cSrcweir return rBox.Y + rBox.Height - 1; 49*cdf0e10cSrcweir } 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir sal_Int32 Width (const sal_Int32 nLeft, const sal_Int32 nRight) 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir return nRight - nLeft + 1; 54*cdf0e10cSrcweir } 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir sal_Int32 Height (const sal_Int32 nTop, const sal_Int32 nBottom) 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir return nBottom - nTop + 1; 59*cdf0e10cSrcweir } 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir } // end of anonymous namespace 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir namespace sdext { namespace presenter { 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir sal_Int32 PresenterGeometryHelper::Floor (const double nValue) 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir return sal::static_int_cast<sal_Int32>(floor(nValue)); 71*cdf0e10cSrcweir } 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir sal_Int32 PresenterGeometryHelper::Ceil (const double nValue) 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir return sal::static_int_cast<sal_Int32>(ceil(nValue)); 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir sal_Int32 PresenterGeometryHelper::Round (const double nValue) 85*cdf0e10cSrcweir { 86*cdf0e10cSrcweir return sal::static_int_cast<sal_Int32>(floor(0.5 + nValue)); 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir awt::Rectangle PresenterGeometryHelper::ConvertRectangle ( 93*cdf0e10cSrcweir const geometry::RealRectangle2D& rBox) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir const sal_Int32 nLeft (Floor(rBox.X1)); 96*cdf0e10cSrcweir const sal_Int32 nTop (Floor(rBox.Y1)); 97*cdf0e10cSrcweir const sal_Int32 nRight (Ceil(rBox.X2)); 98*cdf0e10cSrcweir const sal_Int32 nBottom (Ceil(rBox.Y2)); 99*cdf0e10cSrcweir return awt::Rectangle (nLeft,nTop,nRight-nLeft,nBottom-nTop); 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir awt::Rectangle PresenterGeometryHelper::ConvertRectangleWithConstantSize ( 106*cdf0e10cSrcweir const geometry::RealRectangle2D& rBox) 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir return awt::Rectangle ( 109*cdf0e10cSrcweir Round(rBox.X1), 110*cdf0e10cSrcweir Round(rBox.Y1), 111*cdf0e10cSrcweir Round(rBox.X2 - rBox.X1), 112*cdf0e10cSrcweir Round(rBox.Y2 - rBox.Y1)); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir geometry::RealRectangle2D PresenterGeometryHelper::ConvertRectangle ( 119*cdf0e10cSrcweir const css::awt::Rectangle& rBox) 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir return geometry::RealRectangle2D( 122*cdf0e10cSrcweir rBox.X, 123*cdf0e10cSrcweir rBox.Y, 124*cdf0e10cSrcweir rBox.X + rBox.Width, 125*cdf0e10cSrcweir rBox.Y + rBox.Height); 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir awt::Rectangle PresenterGeometryHelper::TranslateRectangle ( 132*cdf0e10cSrcweir const css::awt::Rectangle& rBox, 133*cdf0e10cSrcweir const sal_Int32 nXOffset, 134*cdf0e10cSrcweir const sal_Int32 nYOffset) 135*cdf0e10cSrcweir { 136*cdf0e10cSrcweir return awt::Rectangle(rBox.X + nXOffset, rBox.Y + nYOffset, rBox.Width, rBox.Height); 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir awt::Rectangle PresenterGeometryHelper::Intersection ( 143*cdf0e10cSrcweir const css::awt::Rectangle& rBox1, 144*cdf0e10cSrcweir const css::awt::Rectangle& rBox2) 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir const sal_Int32 nLeft (::std::max(rBox1.X, rBox2.X)); 147*cdf0e10cSrcweir const sal_Int32 nTop (::std::max(rBox1.Y, rBox2.Y)); 148*cdf0e10cSrcweir const sal_Int32 nRight (::std::min(Right(rBox1), Right(rBox2))); 149*cdf0e10cSrcweir const sal_Int32 nBottom (::std::min(Bottom(rBox1), Bottom(rBox2))); 150*cdf0e10cSrcweir if (nLeft >= nRight || nTop >= nBottom) 151*cdf0e10cSrcweir return awt::Rectangle(); 152*cdf0e10cSrcweir else 153*cdf0e10cSrcweir return awt::Rectangle(nLeft,nTop, Width(nLeft,nRight), Height(nTop,nBottom)); 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir geometry::RealRectangle2D PresenterGeometryHelper::Intersection ( 160*cdf0e10cSrcweir const geometry::RealRectangle2D& rBox1, 161*cdf0e10cSrcweir const geometry::RealRectangle2D& rBox2) 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir const double nLeft (::std::max(rBox1.X1, rBox2.X1)); 164*cdf0e10cSrcweir const double nTop (::std::max(rBox1.Y1, rBox2.Y1)); 165*cdf0e10cSrcweir const double nRight (::std::min(rBox1.X2, rBox2.X2)); 166*cdf0e10cSrcweir const double nBottom (::std::min(rBox1.Y2, rBox2.Y2)); 167*cdf0e10cSrcweir if (nLeft >= nRight || nTop >= nBottom) 168*cdf0e10cSrcweir return geometry::RealRectangle2D(0,0,0,0); 169*cdf0e10cSrcweir else 170*cdf0e10cSrcweir return geometry::RealRectangle2D(nLeft,nTop, nRight, nBottom); 171*cdf0e10cSrcweir } 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir bool PresenterGeometryHelper::IsInside ( 177*cdf0e10cSrcweir const css::geometry::RealRectangle2D& rBox, 178*cdf0e10cSrcweir const css::geometry::RealPoint2D& rPoint) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir return rBox.X1 <= rPoint.X 181*cdf0e10cSrcweir && rBox.Y1 <= rPoint.Y 182*cdf0e10cSrcweir && rBox.X2 >= rPoint.X 183*cdf0e10cSrcweir && rBox.Y2 >= rPoint.Y; 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir bool PresenterGeometryHelper::IsInside ( 190*cdf0e10cSrcweir const css::awt::Rectangle& rBox1, 191*cdf0e10cSrcweir const css::awt::Rectangle& rBox2) 192*cdf0e10cSrcweir { 193*cdf0e10cSrcweir return rBox1.X >= rBox2.X 194*cdf0e10cSrcweir && rBox1.Y >= rBox2.Y 195*cdf0e10cSrcweir && rBox1.X+rBox1.Width <= rBox2.X+rBox2.Width 196*cdf0e10cSrcweir && rBox1.Y+rBox1.Height <= rBox2.Y+rBox2.Height; 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir awt::Rectangle PresenterGeometryHelper::Union ( 203*cdf0e10cSrcweir const css::awt::Rectangle& rBox1, 204*cdf0e10cSrcweir const css::awt::Rectangle& rBox2) 205*cdf0e10cSrcweir { 206*cdf0e10cSrcweir if (rBox1.Width<=0 || rBox1.Height<=0) 207*cdf0e10cSrcweir return rBox2; 208*cdf0e10cSrcweir else if (rBox2.Width<=0 || rBox2.Height<=0) 209*cdf0e10cSrcweir return rBox1; 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir const sal_Int32 nLeft (::std::min(rBox1.X, rBox2.X)); 212*cdf0e10cSrcweir const sal_Int32 nTop (::std::min(rBox1.Y, rBox2.Y)); 213*cdf0e10cSrcweir const sal_Int32 nRight (::std::max(Right(rBox1), Right(rBox2))); 214*cdf0e10cSrcweir const sal_Int32 nBottom (::std::max(Bottom(rBox1), Bottom(rBox2))); 215*cdf0e10cSrcweir if (nLeft >= nRight || nTop >= nBottom) 216*cdf0e10cSrcweir return awt::Rectangle(); 217*cdf0e10cSrcweir else 218*cdf0e10cSrcweir return awt::Rectangle(nLeft,nTop, Width(nLeft,nRight), Height(nTop,nBottom)); 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir geometry::RealRectangle2D PresenterGeometryHelper::Union ( 225*cdf0e10cSrcweir const geometry::RealRectangle2D& rBox1, 226*cdf0e10cSrcweir const geometry::RealRectangle2D& rBox2) 227*cdf0e10cSrcweir { 228*cdf0e10cSrcweir const double nLeft (::std::min(rBox1.X1, rBox2.X1)); 229*cdf0e10cSrcweir const double nTop (::std::min(rBox1.Y1, rBox2.Y1)); 230*cdf0e10cSrcweir const double nRight (::std::max(rBox1.X2, rBox2.X2)); 231*cdf0e10cSrcweir const double nBottom (::std::max(rBox1.Y2, rBox2.Y2)); 232*cdf0e10cSrcweir if (nLeft >= nRight || nTop >= nBottom) 233*cdf0e10cSrcweir return geometry::RealRectangle2D(0,0,0,0); 234*cdf0e10cSrcweir else 235*cdf0e10cSrcweir return geometry::RealRectangle2D(nLeft,nTop, nRight, nBottom); 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir bool PresenterGeometryHelper::AreRectanglesDisjoint ( 242*cdf0e10cSrcweir const css::awt::Rectangle& rBox1, 243*cdf0e10cSrcweir const css::awt::Rectangle& rBox2) 244*cdf0e10cSrcweir { 245*cdf0e10cSrcweir return rBox1.X+rBox1.Width <= rBox2.X 246*cdf0e10cSrcweir || rBox1.Y+rBox1.Height <= rBox2.Y 247*cdf0e10cSrcweir || rBox1.X >= rBox2.X+rBox2.Width 248*cdf0e10cSrcweir || rBox1.Y >= rBox2.Y+rBox2.Height; 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon( 255*cdf0e10cSrcweir const awt::Rectangle& rBox, 256*cdf0e10cSrcweir const Reference<rendering::XGraphicDevice>& rxDevice) 257*cdf0e10cSrcweir { 258*cdf0e10cSrcweir if ( ! rxDevice.is()) 259*cdf0e10cSrcweir return NULL; 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir Sequence<Sequence<geometry::RealPoint2D> > aPoints(1); 262*cdf0e10cSrcweir aPoints[0] = Sequence<geometry::RealPoint2D>(4); 263*cdf0e10cSrcweir aPoints[0][0] = geometry::RealPoint2D(rBox.X, rBox.Y); 264*cdf0e10cSrcweir aPoints[0][1] = geometry::RealPoint2D(rBox.X, rBox.Y+rBox.Height); 265*cdf0e10cSrcweir aPoints[0][2] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y+rBox.Height); 266*cdf0e10cSrcweir aPoints[0][3] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y); 267*cdf0e10cSrcweir Reference<rendering::XLinePolyPolygon2D> xPolygon ( 268*cdf0e10cSrcweir rxDevice->createCompatibleLinePolyPolygon(aPoints)); 269*cdf0e10cSrcweir Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY); 270*cdf0e10cSrcweir if (xRectangle.is()) 271*cdf0e10cSrcweir xRectangle->setClosed(0, sal_True); 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir return xRectangle; 274*cdf0e10cSrcweir } 275*cdf0e10cSrcweir 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon( 280*cdf0e10cSrcweir const geometry::RealRectangle2D& rBox, 281*cdf0e10cSrcweir const Reference<rendering::XGraphicDevice>& rxDevice) 282*cdf0e10cSrcweir { 283*cdf0e10cSrcweir if ( ! rxDevice.is()) 284*cdf0e10cSrcweir return NULL; 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir Sequence<Sequence<geometry::RealPoint2D> > aPoints(1); 287*cdf0e10cSrcweir aPoints[0] = Sequence<geometry::RealPoint2D>(4); 288*cdf0e10cSrcweir aPoints[0][0] = geometry::RealPoint2D(rBox.X1, rBox.Y1); 289*cdf0e10cSrcweir aPoints[0][1] = geometry::RealPoint2D(rBox.X1, rBox.Y2); 290*cdf0e10cSrcweir aPoints[0][2] = geometry::RealPoint2D(rBox.X2, rBox.Y2); 291*cdf0e10cSrcweir aPoints[0][3] = geometry::RealPoint2D(rBox.X2, rBox.Y1); 292*cdf0e10cSrcweir Reference<rendering::XLinePolyPolygon2D> xPolygon ( 293*cdf0e10cSrcweir rxDevice->createCompatibleLinePolyPolygon(aPoints)); 294*cdf0e10cSrcweir Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY); 295*cdf0e10cSrcweir if (xRectangle.is()) 296*cdf0e10cSrcweir xRectangle->setClosed(0, sal_True); 297*cdf0e10cSrcweir 298*cdf0e10cSrcweir return xRectangle; 299*cdf0e10cSrcweir } 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir 302*cdf0e10cSrcweir 303*cdf0e10cSrcweir 304*cdf0e10cSrcweir Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon( 305*cdf0e10cSrcweir const ::std::vector<css::awt::Rectangle>& rBoxes, 306*cdf0e10cSrcweir const Reference<rendering::XGraphicDevice>& rxDevice) 307*cdf0e10cSrcweir { 308*cdf0e10cSrcweir if ( ! rxDevice.is()) 309*cdf0e10cSrcweir return NULL; 310*cdf0e10cSrcweir 311*cdf0e10cSrcweir const sal_Int32 nCount (rBoxes.size()); 312*cdf0e10cSrcweir Sequence<Sequence<geometry::RealPoint2D> > aPoints(nCount); 313*cdf0e10cSrcweir for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex) 314*cdf0e10cSrcweir { 315*cdf0e10cSrcweir const awt::Rectangle& rBox (rBoxes[nIndex]); 316*cdf0e10cSrcweir aPoints[nIndex] = Sequence<geometry::RealPoint2D>(4); 317*cdf0e10cSrcweir aPoints[nIndex][0] = geometry::RealPoint2D(rBox.X, rBox.Y); 318*cdf0e10cSrcweir aPoints[nIndex][1] = geometry::RealPoint2D(rBox.X, rBox.Y+rBox.Height); 319*cdf0e10cSrcweir aPoints[nIndex][2] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y+rBox.Height); 320*cdf0e10cSrcweir aPoints[nIndex][3] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y); 321*cdf0e10cSrcweir } 322*cdf0e10cSrcweir 323*cdf0e10cSrcweir Reference<rendering::XLinePolyPolygon2D> xPolygon ( 324*cdf0e10cSrcweir rxDevice->createCompatibleLinePolyPolygon(aPoints)); 325*cdf0e10cSrcweir Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY); 326*cdf0e10cSrcweir if (xRectangle.is()) 327*cdf0e10cSrcweir for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex) 328*cdf0e10cSrcweir xRectangle->setClosed(nIndex, sal_True); 329*cdf0e10cSrcweir 330*cdf0e10cSrcweir return xRectangle; 331*cdf0e10cSrcweir } 332*cdf0e10cSrcweir 333*cdf0e10cSrcweir 334*cdf0e10cSrcweir } } 335