Update ooo320-m1
[ooovba.git] / sdext / source / presenter / PresenterGeometryHelper.cxx
blob97c066f8c1d5994d1e5a0a2160c00a41256023e6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PresenterGeometryHelper.cxx,v $
11 * $Revision: 1.6 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "PresenterGeometryHelper.hxx"
37 #include <math.h>
38 #include <algorithm>
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::uno;
43 namespace {
45 sal_Int32 Right (const awt::Rectangle& rBox)
47 return rBox.X + rBox.Width - 1;
50 sal_Int32 Bottom (const awt::Rectangle& rBox)
52 return rBox.Y + rBox.Height - 1;
55 sal_Int32 Width (const sal_Int32 nLeft, const sal_Int32 nRight)
57 return nRight - nLeft + 1;
60 sal_Int32 Height (const sal_Int32 nTop, const sal_Int32 nBottom)
62 return nBottom - nTop + 1;
66 } // end of anonymous namespace
70 namespace sdext { namespace presenter {
72 sal_Int32 PresenterGeometryHelper::Floor (const double nValue)
74 return sal::static_int_cast<sal_Int32>(floor(nValue));
80 sal_Int32 PresenterGeometryHelper::Ceil (const double nValue)
82 return sal::static_int_cast<sal_Int32>(ceil(nValue));
88 sal_Int32 PresenterGeometryHelper::Round (const double nValue)
90 return sal::static_int_cast<sal_Int32>(floor(0.5 + nValue));
96 awt::Rectangle PresenterGeometryHelper::ConvertRectangle (
97 const geometry::RealRectangle2D& rBox)
99 const sal_Int32 nLeft (Floor(rBox.X1));
100 const sal_Int32 nTop (Floor(rBox.Y1));
101 const sal_Int32 nRight (Ceil(rBox.X2));
102 const sal_Int32 nBottom (Ceil(rBox.Y2));
103 return awt::Rectangle (nLeft,nTop,nRight-nLeft,nBottom-nTop);
109 awt::Rectangle PresenterGeometryHelper::ConvertRectangleWithConstantSize (
110 const geometry::RealRectangle2D& rBox)
112 return awt::Rectangle (
113 Round(rBox.X1),
114 Round(rBox.Y1),
115 Round(rBox.X2 - rBox.X1),
116 Round(rBox.Y2 - rBox.Y1));
122 geometry::RealRectangle2D PresenterGeometryHelper::ConvertRectangle (
123 const css::awt::Rectangle& rBox)
125 return geometry::RealRectangle2D(
126 rBox.X,
127 rBox.Y,
128 rBox.X + rBox.Width,
129 rBox.Y + rBox.Height);
135 awt::Rectangle PresenterGeometryHelper::TranslateRectangle (
136 const css::awt::Rectangle& rBox,
137 const sal_Int32 nXOffset,
138 const sal_Int32 nYOffset)
140 return awt::Rectangle(rBox.X + nXOffset, rBox.Y + nYOffset, rBox.Width, rBox.Height);
146 awt::Rectangle PresenterGeometryHelper::Intersection (
147 const css::awt::Rectangle& rBox1,
148 const css::awt::Rectangle& rBox2)
150 const sal_Int32 nLeft (::std::max(rBox1.X, rBox2.X));
151 const sal_Int32 nTop (::std::max(rBox1.Y, rBox2.Y));
152 const sal_Int32 nRight (::std::min(Right(rBox1), Right(rBox2)));
153 const sal_Int32 nBottom (::std::min(Bottom(rBox1), Bottom(rBox2)));
154 if (nLeft >= nRight || nTop >= nBottom)
155 return awt::Rectangle();
156 else
157 return awt::Rectangle(nLeft,nTop, Width(nLeft,nRight), Height(nTop,nBottom));
163 geometry::RealRectangle2D PresenterGeometryHelper::Intersection (
164 const geometry::RealRectangle2D& rBox1,
165 const geometry::RealRectangle2D& rBox2)
167 const double nLeft (::std::max(rBox1.X1, rBox2.X1));
168 const double nTop (::std::max(rBox1.Y1, rBox2.Y1));
169 const double nRight (::std::min(rBox1.X2, rBox2.X2));
170 const double nBottom (::std::min(rBox1.Y2, rBox2.Y2));
171 if (nLeft >= nRight || nTop >= nBottom)
172 return geometry::RealRectangle2D(0,0,0,0);
173 else
174 return geometry::RealRectangle2D(nLeft,nTop, nRight, nBottom);
180 bool PresenterGeometryHelper::IsInside (
181 const css::geometry::RealRectangle2D& rBox,
182 const css::geometry::RealPoint2D& rPoint)
184 return rBox.X1 <= rPoint.X
185 && rBox.Y1 <= rPoint.Y
186 && rBox.X2 >= rPoint.X
187 && rBox.Y2 >= rPoint.Y;
193 bool PresenterGeometryHelper::IsInside (
194 const css::awt::Rectangle& rBox1,
195 const css::awt::Rectangle& rBox2)
197 return rBox1.X >= rBox2.X
198 && rBox1.Y >= rBox2.Y
199 && rBox1.X+rBox1.Width <= rBox2.X+rBox2.Width
200 && rBox1.Y+rBox1.Height <= rBox2.Y+rBox2.Height;
206 awt::Rectangle PresenterGeometryHelper::Union (
207 const css::awt::Rectangle& rBox1,
208 const css::awt::Rectangle& rBox2)
210 if (rBox1.Width<=0 || rBox1.Height<=0)
211 return rBox2;
212 else if (rBox2.Width<=0 || rBox2.Height<=0)
213 return rBox1;
215 const sal_Int32 nLeft (::std::min(rBox1.X, rBox2.X));
216 const sal_Int32 nTop (::std::min(rBox1.Y, rBox2.Y));
217 const sal_Int32 nRight (::std::max(Right(rBox1), Right(rBox2)));
218 const sal_Int32 nBottom (::std::max(Bottom(rBox1), Bottom(rBox2)));
219 if (nLeft >= nRight || nTop >= nBottom)
220 return awt::Rectangle();
221 else
222 return awt::Rectangle(nLeft,nTop, Width(nLeft,nRight), Height(nTop,nBottom));
228 geometry::RealRectangle2D PresenterGeometryHelper::Union (
229 const geometry::RealRectangle2D& rBox1,
230 const geometry::RealRectangle2D& rBox2)
232 const double nLeft (::std::min(rBox1.X1, rBox2.X1));
233 const double nTop (::std::min(rBox1.Y1, rBox2.Y1));
234 const double nRight (::std::max(rBox1.X2, rBox2.X2));
235 const double nBottom (::std::max(rBox1.Y2, rBox2.Y2));
236 if (nLeft >= nRight || nTop >= nBottom)
237 return geometry::RealRectangle2D(0,0,0,0);
238 else
239 return geometry::RealRectangle2D(nLeft,nTop, nRight, nBottom);
245 bool PresenterGeometryHelper::AreRectanglesDisjoint (
246 const css::awt::Rectangle& rBox1,
247 const css::awt::Rectangle& rBox2)
249 return rBox1.X+rBox1.Width <= rBox2.X
250 || rBox1.Y+rBox1.Height <= rBox2.Y
251 || rBox1.X >= rBox2.X+rBox2.Width
252 || rBox1.Y >= rBox2.Y+rBox2.Height;
258 Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon(
259 const awt::Rectangle& rBox,
260 const Reference<rendering::XGraphicDevice>& rxDevice)
262 if ( ! rxDevice.is())
263 return NULL;
265 Sequence<Sequence<geometry::RealPoint2D> > aPoints(1);
266 aPoints[0] = Sequence<geometry::RealPoint2D>(4);
267 aPoints[0][0] = geometry::RealPoint2D(rBox.X, rBox.Y);
268 aPoints[0][1] = geometry::RealPoint2D(rBox.X, rBox.Y+rBox.Height);
269 aPoints[0][2] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y+rBox.Height);
270 aPoints[0][3] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y);
271 Reference<rendering::XLinePolyPolygon2D> xPolygon (
272 rxDevice->createCompatibleLinePolyPolygon(aPoints));
273 Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY);
274 if (xRectangle.is())
275 xRectangle->setClosed(0, sal_True);
277 return xRectangle;
283 Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon(
284 const geometry::RealRectangle2D& rBox,
285 const Reference<rendering::XGraphicDevice>& rxDevice)
287 if ( ! rxDevice.is())
288 return NULL;
290 Sequence<Sequence<geometry::RealPoint2D> > aPoints(1);
291 aPoints[0] = Sequence<geometry::RealPoint2D>(4);
292 aPoints[0][0] = geometry::RealPoint2D(rBox.X1, rBox.Y1);
293 aPoints[0][1] = geometry::RealPoint2D(rBox.X1, rBox.Y2);
294 aPoints[0][2] = geometry::RealPoint2D(rBox.X2, rBox.Y2);
295 aPoints[0][3] = geometry::RealPoint2D(rBox.X2, rBox.Y1);
296 Reference<rendering::XLinePolyPolygon2D> xPolygon (
297 rxDevice->createCompatibleLinePolyPolygon(aPoints));
298 Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY);
299 if (xRectangle.is())
300 xRectangle->setClosed(0, sal_True);
302 return xRectangle;
308 Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon(
309 const ::std::vector<css::awt::Rectangle>& rBoxes,
310 const Reference<rendering::XGraphicDevice>& rxDevice)
312 if ( ! rxDevice.is())
313 return NULL;
315 const sal_Int32 nCount (rBoxes.size());
316 Sequence<Sequence<geometry::RealPoint2D> > aPoints(nCount);
317 for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
319 const awt::Rectangle& rBox (rBoxes[nIndex]);
320 aPoints[nIndex] = Sequence<geometry::RealPoint2D>(4);
321 aPoints[nIndex][0] = geometry::RealPoint2D(rBox.X, rBox.Y);
322 aPoints[nIndex][1] = geometry::RealPoint2D(rBox.X, rBox.Y+rBox.Height);
323 aPoints[nIndex][2] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y+rBox.Height);
324 aPoints[nIndex][3] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y);
327 Reference<rendering::XLinePolyPolygon2D> xPolygon (
328 rxDevice->createCompatibleLinePolyPolygon(aPoints));
329 Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY);
330 if (xRectangle.is())
331 for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
332 xRectangle->setClosed(nIndex, sal_True);
334 return xRectangle;