1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
24 #include <o3tl/safeint.hxx>
27 using namespace ::com::sun::star
;
28 using namespace ::com::sun::star::uno
;
32 sal_Int32
Right (const awt::Rectangle
& rBox
)
34 return rBox
.X
+ rBox
.Width
- 1;
37 sal_Int32
Bottom (const awt::Rectangle
& rBox
)
39 return rBox
.Y
+ rBox
.Height
- 1;
42 sal_Int32
Width (const sal_Int32 nLeft
, const sal_Int32 nRight
)
44 return nRight
- nLeft
+ 1;
47 sal_Int32
Height (const sal_Int32 nTop
, const sal_Int32 nBottom
)
49 return nBottom
- nTop
+ 1;
52 } // end of anonymous namespace
54 namespace sdext::presenter
{
56 sal_Int32
PresenterGeometryHelper::Floor (const double nValue
)
58 return sal::static_int_cast
<sal_Int32
>(floor(nValue
));
61 sal_Int32
PresenterGeometryHelper::Ceil (const double nValue
)
63 return sal::static_int_cast
<sal_Int32
>(ceil(nValue
));
66 sal_Int32
PresenterGeometryHelper::Round (const double nValue
)
68 return sal::static_int_cast
<sal_Int32
>(floor(0.5 + nValue
));
71 awt::Rectangle
PresenterGeometryHelper::ConvertRectangle (
72 const geometry::RealRectangle2D
& rBox
)
74 const sal_Int32
nLeft (Floor(rBox
.X1
));
75 const sal_Int32
nTop (Floor(rBox
.Y1
));
76 const sal_Int32
nRight (Ceil(rBox
.X2
));
77 const sal_Int32
nBottom (Ceil(rBox
.Y2
));
78 return awt::Rectangle (nLeft
,nTop
,nRight
-nLeft
,nBottom
-nTop
);
81 awt::Rectangle
PresenterGeometryHelper::ConvertRectangleWithConstantSize (
82 const geometry::RealRectangle2D
& rBox
)
84 return awt::Rectangle (
87 Round(rBox
.X2
- rBox
.X1
),
88 Round(rBox
.Y2
- rBox
.Y1
));
91 geometry::RealRectangle2D
PresenterGeometryHelper::ConvertRectangle (
92 const css::awt::Rectangle
& rBox
)
94 return geometry::RealRectangle2D(
98 rBox
.Y
+ rBox
.Height
);
101 awt::Rectangle
PresenterGeometryHelper::TranslateRectangle (
102 const css::awt::Rectangle
& rBox
,
103 const sal_Int32 nXOffset
,
104 const sal_Int32 nYOffset
)
106 return awt::Rectangle(rBox
.X
+ nXOffset
, rBox
.Y
+ nYOffset
, rBox
.Width
, rBox
.Height
);
109 awt::Rectangle
PresenterGeometryHelper::Intersection (
110 const css::awt::Rectangle
& rBox1
,
111 const css::awt::Rectangle
& rBox2
)
113 const sal_Int32
nLeft (::std::max(rBox1
.X
, rBox2
.X
));
114 const sal_Int32
nTop (::std::max(rBox1
.Y
, rBox2
.Y
));
115 const sal_Int32
nRight (::std::min(Right(rBox1
), Right(rBox2
)));
116 const sal_Int32
nBottom (::std::min(Bottom(rBox1
), Bottom(rBox2
)));
117 if (nLeft
>= nRight
|| nTop
>= nBottom
)
118 return awt::Rectangle();
120 return awt::Rectangle(nLeft
,nTop
, Width(nLeft
,nRight
), Height(nTop
,nBottom
));
123 geometry::RealRectangle2D
PresenterGeometryHelper::Intersection (
124 const geometry::RealRectangle2D
& rBox1
,
125 const geometry::RealRectangle2D
& rBox2
)
127 const double nLeft (::std::max(rBox1
.X1
, rBox2
.X1
));
128 const double nTop (::std::max(rBox1
.Y1
, rBox2
.Y1
));
129 const double nRight (::std::min(rBox1
.X2
, rBox2
.X2
));
130 const double nBottom (::std::min(rBox1
.Y2
, rBox2
.Y2
));
131 if (nLeft
>= nRight
|| nTop
>= nBottom
)
132 return geometry::RealRectangle2D(0,0,0,0);
134 return geometry::RealRectangle2D(nLeft
,nTop
, nRight
, nBottom
);
137 bool PresenterGeometryHelper::IsInside (
138 const css::geometry::RealRectangle2D
& rBox
,
139 const css::geometry::RealPoint2D
& rPoint
)
141 return rBox
.X1
<= rPoint
.X
142 && rBox
.Y1
<= rPoint
.Y
143 && rBox
.X2
>= rPoint
.X
144 && rBox
.Y2
>= rPoint
.Y
;
147 bool PresenterGeometryHelper::IsInside (
148 const css::awt::Rectangle
& rBox1
,
149 const css::awt::Rectangle
& rBox2
)
151 return rBox1
.X
>= rBox2
.X
152 && rBox1
.Y
>= rBox2
.Y
153 && rBox1
.X
+rBox1
.Width
<= rBox2
.X
+rBox2
.Width
154 && rBox1
.Y
+rBox1
.Height
<= rBox2
.Y
+rBox2
.Height
;
157 geometry::RealRectangle2D
PresenterGeometryHelper::Union (
158 const geometry::RealRectangle2D
& rBox1
,
159 const geometry::RealRectangle2D
& rBox2
)
161 const double nLeft (::std::min(rBox1
.X1
, rBox2
.X1
));
162 const double nTop (::std::min(rBox1
.Y1
, rBox2
.Y1
));
163 const double nRight (::std::max(rBox1
.X2
, rBox2
.X2
));
164 const double nBottom (::std::max(rBox1
.Y2
, rBox2
.Y2
));
165 if (nLeft
>= nRight
|| nTop
>= nBottom
)
166 return geometry::RealRectangle2D(0,0,0,0);
168 return geometry::RealRectangle2D(nLeft
,nTop
, nRight
, nBottom
);
171 bool PresenterGeometryHelper::AreRectanglesDisjoint (
172 const css::awt::Rectangle
& rBox1
,
173 const css::awt::Rectangle
& rBox2
)
175 return rBox1
.X
+rBox1
.Width
<= rBox2
.X
176 || rBox1
.Y
+rBox1
.Height
<= rBox2
.Y
177 || rBox1
.X
>= rBox2
.X
+rBox2
.Width
178 || rBox1
.Y
>= rBox2
.Y
+rBox2
.Height
;
181 Reference
<rendering::XPolyPolygon2D
> PresenterGeometryHelper::CreatePolygon(
182 const awt::Rectangle
& rBox
,
183 const Reference
<rendering::XGraphicDevice
>& rxDevice
)
185 if ( ! rxDevice
.is())
188 Sequence
<Sequence
<geometry::RealPoint2D
> > aPoints
191 { o3tl::narrowing
<double>(rBox
.X
), o3tl::narrowing
<double>(rBox
.Y
) },
192 { o3tl::narrowing
<double>(rBox
.X
), o3tl::narrowing
<double>(rBox
.Y
+rBox
.Height
) },
193 { o3tl::narrowing
<double>(rBox
.X
+rBox
.Width
), o3tl::narrowing
<double>(rBox
.Y
+rBox
.Height
) },
194 { o3tl::narrowing
<double>(rBox
.X
+rBox
.Width
), o3tl::narrowing
<double>(rBox
.Y
) }
197 Reference
<rendering::XLinePolyPolygon2D
> xPolygon (
198 rxDevice
->createCompatibleLinePolyPolygon(aPoints
));
200 xPolygon
->setClosed(0, true);
205 Reference
<rendering::XPolyPolygon2D
> PresenterGeometryHelper::CreatePolygon(
206 const geometry::RealRectangle2D
& rBox
,
207 const Reference
<rendering::XGraphicDevice
>& rxDevice
)
209 if ( ! rxDevice
.is())
212 Sequence
<Sequence
<geometry::RealPoint2D
> > aPoints
215 { rBox
.X1
, rBox
.Y1
},
216 { rBox
.X1
, rBox
.Y2
},
217 { rBox
.X2
, rBox
.Y2
},
221 Reference
<rendering::XLinePolyPolygon2D
> xPolygon (
222 rxDevice
->createCompatibleLinePolyPolygon(aPoints
));
224 xPolygon
->setClosed(0, true);
229 Reference
<rendering::XPolyPolygon2D
> PresenterGeometryHelper::CreatePolygon(
230 const ::std::vector
<css::awt::Rectangle
>& rBoxes
,
231 const Reference
<rendering::XGraphicDevice
>& rxDevice
)
233 if ( ! rxDevice
.is())
236 const sal_Int32
nCount (rBoxes
.size());
237 Sequence
<Sequence
<geometry::RealPoint2D
> > aPoints(nCount
);
238 auto aPointsRange
= asNonConstRange(aPoints
);
239 for (sal_Int32 nIndex
=0; nIndex
<nCount
; ++nIndex
)
241 const awt::Rectangle
& rBox (rBoxes
[nIndex
]);
242 aPointsRange
[nIndex
] = Sequence
<geometry::RealPoint2D
>
244 { o3tl::narrowing
<double>(rBox
.X
), o3tl::narrowing
<double>(rBox
.Y
) },
245 { o3tl::narrowing
<double>(rBox
.X
), o3tl::narrowing
<double>(rBox
.Y
+rBox
.Height
) },
246 { o3tl::narrowing
<double>(rBox
.X
+rBox
.Width
), o3tl::narrowing
<double>(rBox
.Y
+rBox
.Height
) },
247 { o3tl::narrowing
<double>(rBox
.X
+rBox
.Width
), o3tl::narrowing
<double>(rBox
.Y
) }
251 Reference
<rendering::XLinePolyPolygon2D
> xPolygon (
252 rxDevice
->createCompatibleLinePolyPolygon(aPoints
));
254 for (sal_Int32 nIndex
=0; nIndex
<nCount
; ++nIndex
)
255 xPolygon
->setClosed(nIndex
, true);
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */