Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sdext / source / presenter / PresenterGeometryHelper.cxx
blob8126e5c9639d80b2bff773f53bdedbc89c9750ee
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "PresenterGeometryHelper.hxx"
22 #include <math.h>
23 #include <algorithm>
25 using namespace ::com::sun::star;
26 using namespace ::com::sun::star::uno;
28 namespace {
30 sal_Int32 Right (const awt::Rectangle& rBox)
32 return rBox.X + rBox.Width - 1;
35 sal_Int32 Bottom (const awt::Rectangle& rBox)
37 return rBox.Y + rBox.Height - 1;
40 sal_Int32 Width (const sal_Int32 nLeft, const sal_Int32 nRight)
42 return nRight - nLeft + 1;
45 sal_Int32 Height (const sal_Int32 nTop, const sal_Int32 nBottom)
47 return nBottom - nTop + 1;
50 } // end of anonymous namespace
52 namespace sdext { namespace presenter {
54 sal_Int32 PresenterGeometryHelper::Floor (const double nValue)
56 return sal::static_int_cast<sal_Int32>(floor(nValue));
59 sal_Int32 PresenterGeometryHelper::Ceil (const double nValue)
61 return sal::static_int_cast<sal_Int32>(ceil(nValue));
64 sal_Int32 PresenterGeometryHelper::Round (const double nValue)
66 return sal::static_int_cast<sal_Int32>(floor(0.5 + nValue));
69 awt::Rectangle PresenterGeometryHelper::ConvertRectangle (
70 const geometry::RealRectangle2D& rBox)
72 const sal_Int32 nLeft (Floor(rBox.X1));
73 const sal_Int32 nTop (Floor(rBox.Y1));
74 const sal_Int32 nRight (Ceil(rBox.X2));
75 const sal_Int32 nBottom (Ceil(rBox.Y2));
76 return awt::Rectangle (nLeft,nTop,nRight-nLeft,nBottom-nTop);
79 awt::Rectangle PresenterGeometryHelper::ConvertRectangleWithConstantSize (
80 const geometry::RealRectangle2D& rBox)
82 return awt::Rectangle (
83 Round(rBox.X1),
84 Round(rBox.Y1),
85 Round(rBox.X2 - rBox.X1),
86 Round(rBox.Y2 - rBox.Y1));
89 geometry::RealRectangle2D PresenterGeometryHelper::ConvertRectangle (
90 const css::awt::Rectangle& rBox)
92 return geometry::RealRectangle2D(
93 rBox.X,
94 rBox.Y,
95 rBox.X + rBox.Width,
96 rBox.Y + rBox.Height);
99 awt::Rectangle PresenterGeometryHelper::TranslateRectangle (
100 const css::awt::Rectangle& rBox,
101 const sal_Int32 nXOffset,
102 const sal_Int32 nYOffset)
104 return awt::Rectangle(rBox.X + nXOffset, rBox.Y + nYOffset, rBox.Width, rBox.Height);
107 awt::Rectangle PresenterGeometryHelper::Intersection (
108 const css::awt::Rectangle& rBox1,
109 const css::awt::Rectangle& rBox2)
111 const sal_Int32 nLeft (::std::max(rBox1.X, rBox2.X));
112 const sal_Int32 nTop (::std::max(rBox1.Y, rBox2.Y));
113 const sal_Int32 nRight (::std::min(Right(rBox1), Right(rBox2)));
114 const sal_Int32 nBottom (::std::min(Bottom(rBox1), Bottom(rBox2)));
115 if (nLeft >= nRight || nTop >= nBottom)
116 return awt::Rectangle();
117 else
118 return awt::Rectangle(nLeft,nTop, Width(nLeft,nRight), Height(nTop,nBottom));
121 geometry::RealRectangle2D PresenterGeometryHelper::Intersection (
122 const geometry::RealRectangle2D& rBox1,
123 const geometry::RealRectangle2D& rBox2)
125 const double nLeft (::std::max(rBox1.X1, rBox2.X1));
126 const double nTop (::std::max(rBox1.Y1, rBox2.Y1));
127 const double nRight (::std::min(rBox1.X2, rBox2.X2));
128 const double nBottom (::std::min(rBox1.Y2, rBox2.Y2));
129 if (nLeft >= nRight || nTop >= nBottom)
130 return geometry::RealRectangle2D(0,0,0,0);
131 else
132 return geometry::RealRectangle2D(nLeft,nTop, nRight, nBottom);
135 bool PresenterGeometryHelper::IsInside (
136 const css::geometry::RealRectangle2D& rBox,
137 const css::geometry::RealPoint2D& rPoint)
139 return rBox.X1 <= rPoint.X
140 && rBox.Y1 <= rPoint.Y
141 && rBox.X2 >= rPoint.X
142 && rBox.Y2 >= rPoint.Y;
145 bool PresenterGeometryHelper::IsInside (
146 const css::awt::Rectangle& rBox1,
147 const css::awt::Rectangle& rBox2)
149 return rBox1.X >= rBox2.X
150 && rBox1.Y >= rBox2.Y
151 && rBox1.X+rBox1.Width <= rBox2.X+rBox2.Width
152 && rBox1.Y+rBox1.Height <= rBox2.Y+rBox2.Height;
155 geometry::RealRectangle2D PresenterGeometryHelper::Union (
156 const geometry::RealRectangle2D& rBox1,
157 const geometry::RealRectangle2D& rBox2)
159 const double nLeft (::std::min(rBox1.X1, rBox2.X1));
160 const double nTop (::std::min(rBox1.Y1, rBox2.Y1));
161 const double nRight (::std::max(rBox1.X2, rBox2.X2));
162 const double nBottom (::std::max(rBox1.Y2, rBox2.Y2));
163 if (nLeft >= nRight || nTop >= nBottom)
164 return geometry::RealRectangle2D(0,0,0,0);
165 else
166 return geometry::RealRectangle2D(nLeft,nTop, nRight, nBottom);
169 bool PresenterGeometryHelper::AreRectanglesDisjoint (
170 const css::awt::Rectangle& rBox1,
171 const css::awt::Rectangle& rBox2)
173 return rBox1.X+rBox1.Width <= rBox2.X
174 || rBox1.Y+rBox1.Height <= rBox2.Y
175 || rBox1.X >= rBox2.X+rBox2.Width
176 || rBox1.Y >= rBox2.Y+rBox2.Height;
179 Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon(
180 const awt::Rectangle& rBox,
181 const Reference<rendering::XGraphicDevice>& rxDevice)
183 if ( ! rxDevice.is())
184 return nullptr;
186 Sequence<Sequence<geometry::RealPoint2D> > aPoints(1);
187 aPoints[0] = Sequence<geometry::RealPoint2D>(4);
188 aPoints[0][0] = geometry::RealPoint2D(rBox.X, rBox.Y);
189 aPoints[0][1] = geometry::RealPoint2D(rBox.X, rBox.Y+rBox.Height);
190 aPoints[0][2] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y+rBox.Height);
191 aPoints[0][3] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y);
192 Reference<rendering::XLinePolyPolygon2D> xPolygon (
193 rxDevice->createCompatibleLinePolyPolygon(aPoints));
194 Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY);
195 if (xRectangle.is())
196 xRectangle->setClosed(0, true);
198 return xRectangle;
201 Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon(
202 const geometry::RealRectangle2D& rBox,
203 const Reference<rendering::XGraphicDevice>& rxDevice)
205 if ( ! rxDevice.is())
206 return nullptr;
208 Sequence<Sequence<geometry::RealPoint2D> > aPoints(1);
209 aPoints[0] = Sequence<geometry::RealPoint2D>(4);
210 aPoints[0][0] = geometry::RealPoint2D(rBox.X1, rBox.Y1);
211 aPoints[0][1] = geometry::RealPoint2D(rBox.X1, rBox.Y2);
212 aPoints[0][2] = geometry::RealPoint2D(rBox.X2, rBox.Y2);
213 aPoints[0][3] = geometry::RealPoint2D(rBox.X2, rBox.Y1);
214 Reference<rendering::XLinePolyPolygon2D> xPolygon (
215 rxDevice->createCompatibleLinePolyPolygon(aPoints));
216 Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY);
217 if (xRectangle.is())
218 xRectangle->setClosed(0, true);
220 return xRectangle;
223 Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon(
224 const ::std::vector<css::awt::Rectangle>& rBoxes,
225 const Reference<rendering::XGraphicDevice>& rxDevice)
227 if ( ! rxDevice.is())
228 return nullptr;
230 const sal_Int32 nCount (rBoxes.size());
231 Sequence<Sequence<geometry::RealPoint2D> > aPoints(nCount);
232 for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
234 const awt::Rectangle& rBox (rBoxes[nIndex]);
235 aPoints[nIndex] = Sequence<geometry::RealPoint2D>(4);
236 aPoints[nIndex][0] = geometry::RealPoint2D(rBox.X, rBox.Y);
237 aPoints[nIndex][1] = geometry::RealPoint2D(rBox.X, rBox.Y+rBox.Height);
238 aPoints[nIndex][2] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y+rBox.Height);
239 aPoints[nIndex][3] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y);
242 Reference<rendering::XLinePolyPolygon2D> xPolygon (
243 rxDevice->createCompatibleLinePolyPolygon(aPoints));
244 Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY);
245 if (xRectangle.is())
246 for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
247 xRectangle->setClosed(nIndex, true);
249 return xRectangle;
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */