Version 7.3.5.2, tag libreoffice-7.3.5.2
[LibreOffice.git] / tools / source / generic / gen.cxx
blob1fe47071d4ce1601496ea2600ba537531f461d28
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 <sal/config.h>
21 #include <sal/log.hxx>
23 #include <algorithm>
24 #include <cassert>
25 #include <sstream>
26 #include <o3tl/safeint.hxx>
27 #include <tools/gen.hxx>
28 #include <tools/stream.hxx>
30 OString Pair::toString() const
32 // Note that this is not just used for debugging output but the
33 // format is parsed by external code (passed in callbacks to
34 // LibreOfficeKit clients). So don't change.
35 return OString::number(A()) + ", " + OString::number(B());
38 void tools::Rectangle::SetSize( const Size& rSize )
40 if ( rSize.Width() < 0 )
41 nRight = nLeft + rSize.Width() +1;
42 else if ( rSize.Width() > 0 )
43 nRight = nLeft + rSize.Width() -1;
44 else
45 SetWidthEmpty();
47 if ( rSize.Height() < 0 )
48 nBottom = nTop + rSize.Height() +1;
49 else if ( rSize.Height() > 0 )
50 nBottom = nTop + rSize.Height() -1;
51 else
52 SetHeightEmpty();
55 void tools::Rectangle::SaturatingSetSize(const Size& rSize)
57 if (rSize.Width() < 0)
58 nRight = o3tl::saturating_add(nLeft, (rSize.Width() + 1));
59 else if ( rSize.Width() > 0 )
60 nRight = o3tl::saturating_add(nLeft, (rSize.Width() - 1));
61 else
62 SetWidthEmpty();
64 if ( rSize.Height() < 0 )
65 nBottom = o3tl::saturating_add(nTop, (rSize.Height() + 1));
66 else if ( rSize.Height() > 0 )
67 nBottom = o3tl::saturating_add(nTop, (rSize.Height() - 1));
68 else
69 SetHeightEmpty();
72 void tools::Rectangle::SaturatingSetPosX(tools::Long x)
74 if (!IsWidthEmpty())
75 nRight = o3tl::saturating_add(nRight, x - nLeft);
76 nLeft = x;
79 void tools::Rectangle::SaturatingSetPosY(tools::Long y)
81 if (!IsHeightEmpty())
82 nBottom = o3tl::saturating_add(nBottom, y - nTop);
83 nTop = y;
86 tools::Rectangle& tools::Rectangle::Union( const tools::Rectangle& rRect )
88 if ( rRect.IsEmpty() )
89 return *this;
91 if ( IsEmpty() )
92 *this = rRect;
93 else
95 std::tie(nLeft, nRight) = std::minmax({ nLeft, rRect.nLeft, nRight, rRect.nRight });
96 std::tie(nTop, nBottom) = std::minmax({ nTop, rRect.nTop, nBottom, rRect.nBottom });
99 return *this;
102 tools::Rectangle& tools::Rectangle::Intersection( const tools::Rectangle& rRect )
104 if ( IsEmpty() )
105 return *this;
106 if ( rRect.IsEmpty() )
108 *this = tools::Rectangle();
109 return *this;
112 // Justify rectangle
113 tools::Rectangle aTmpRect( rRect );
114 Justify();
115 aTmpRect.Justify();
117 // Perform intersection
118 nLeft = std::max( nLeft, aTmpRect.nLeft );
119 nRight = std::min( nRight, aTmpRect.nRight );
120 nTop = std::max( nTop, aTmpRect.nTop );
121 nBottom= std::min( nBottom, aTmpRect.nBottom );
123 // Determine if intersection is empty
124 if ( nRight < nLeft || nBottom < nTop )
125 *this = tools::Rectangle();
127 return *this;
130 void tools::Rectangle::Justify()
132 if ((nRight < nLeft) && (!IsWidthEmpty()))
134 std::swap(nLeft, nRight);
137 if ((nBottom < nTop) && (!IsHeightEmpty()))
139 std::swap(nBottom, nTop);
143 bool tools::Rectangle::Contains( const Point& rPoint ) const
145 if ( IsEmpty() )
146 return false;
148 if ( nLeft <= nRight )
150 if ( (rPoint.X() < nLeft) || (rPoint.X() > nRight) )
151 return false;
153 else
155 if ( (rPoint.X() > nLeft) || (rPoint.X() < nRight) )
156 return false;
158 if ( nTop <= nBottom )
160 if ( (rPoint.Y() < nTop) || (rPoint.Y() > nBottom) )
161 return false;
163 else
165 if ( (rPoint.Y() > nTop) || (rPoint.Y() < nBottom) )
166 return false;
168 return true;
171 bool tools::Rectangle::Contains( const tools::Rectangle& rRect ) const
173 return Contains( rRect.TopLeft() ) && Contains( rRect.BottomRight() );
176 bool tools::Rectangle::Overlaps( const tools::Rectangle& rRect ) const
178 // If there's no intersection, they don't overlap
179 return !GetIntersection( rRect ).IsEmpty();
182 OString tools::Rectangle::toString() const
184 // Note that this is not just used for debugging output but the
185 // format is parsed by external code (passed in callbacks to
186 // LibreOfficeKit clients). So don't change.
187 return OString::number(Left()) + ", " + OString::number(Top()) + ", " + OString::number(getWidth()) + ", " + OString::number(getHeight());
190 void tools::Rectangle::expand(tools::Long nExpandBy)
192 AdjustLeft(-nExpandBy);
193 AdjustTop(-nExpandBy);
194 AdjustRight(nExpandBy);
195 AdjustBottom(nExpandBy);
198 void tools::Rectangle::shrink(tools::Long nShrinkBy)
200 nLeft += nShrinkBy;
201 nTop += nShrinkBy;
202 if (!IsWidthEmpty())
203 nRight -= nShrinkBy;
204 if (!IsHeightEmpty())
205 nBottom -= nShrinkBy;
208 tools::Long tools::Rectangle::AdjustRight(tools::Long nHorzMoveDelta)
210 if (IsWidthEmpty())
211 nRight = nLeft + nHorzMoveDelta - 1;
212 else
213 nRight += nHorzMoveDelta;
214 return nRight;
217 tools::Long tools::Rectangle::AdjustBottom( tools::Long nVertMoveDelta )
219 if (IsHeightEmpty())
220 nBottom = nTop + nVertMoveDelta - 1;
221 else
222 nBottom += nVertMoveDelta;
223 return nBottom;
226 static_assert( std::is_trivially_copyable< Pair >::value );
227 static_assert( std::is_trivially_copyable< Point >::value );
228 static_assert( std::is_trivially_copyable< Size >::value );
229 static_assert( std::is_trivially_copyable< Range >::value );
230 static_assert( std::is_trivially_copyable< Selection >::value );
231 static_assert( std::is_trivially_copyable< tools::Rectangle >::value );
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */