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 <sal/config.h>
21 #include <rtl/string.hxx>
25 #include <o3tl/hash_combine.hxx>
26 #include <o3tl/safeint.hxx>
27 #include <tools/gen.hxx>
29 OString
Pair::toString() const
31 // Note that this is not just used for debugging output but the
32 // format is parsed by external code (passed in callbacks to
33 // LibreOfficeKit clients). So don't change.
34 return OString::number(A()) + ", " + OString::number(B());
37 size_t Pair::GetHashValue() const
40 o3tl::hash_combine( hash
, mnA
);
41 o3tl::hash_combine( hash
, mnB
);
45 void RectangleTemplateBase::SaturatingSetSize(const SizeTemplateBase
& rSize
)
47 if (rSize
.Width() < 0)
48 mnRight
= o3tl::saturating_add(mnLeft
, (rSize
.Width() + 1));
49 else if ( rSize
.Width() > 0 )
50 mnRight
= o3tl::saturating_add(mnLeft
, (rSize
.Width() - 1));
54 if ( rSize
.Height() < 0 )
55 mnBottom
= o3tl::saturating_add(mnTop
, (rSize
.Height() + 1));
56 else if ( rSize
.Height() > 0 )
57 mnBottom
= o3tl::saturating_add(mnTop
, (rSize
.Height() - 1));
62 void RectangleTemplateBase::SaturatingSetPosX(tools::Long x
)
65 mnRight
= o3tl::saturating_add(mnRight
, x
- mnLeft
);
69 void RectangleTemplateBase::SaturatingSetPosY(tools::Long y
)
72 mnBottom
= o3tl::saturating_add(mnBottom
, y
- mnTop
);
76 void RectangleTemplateBase::Union( const RectangleTemplateBase
& rRect
)
78 if ( rRect
.IsEmpty() )
85 std::tie(mnLeft
, mnRight
) = std::minmax({ mnLeft
, rRect
.mnLeft
, mnRight
, rRect
.mnRight
});
86 std::tie(mnTop
, mnBottom
) = std::minmax({ mnTop
, rRect
.mnTop
, mnBottom
, rRect
.mnBottom
});
90 void RectangleTemplateBase::Intersection( const RectangleTemplateBase
& rRect
)
94 if ( rRect
.IsEmpty() )
96 *this = tools::Rectangle();
100 // Normalize rectangle
101 RectangleTemplateBase
aTmpRect( rRect
);
103 aTmpRect
.Normalize();
105 // Perform intersection
106 mnLeft
= std::max( mnLeft
, aTmpRect
.mnLeft
);
107 mnRight
= std::min( mnRight
, aTmpRect
.mnRight
);
108 mnTop
= std::max( mnTop
, aTmpRect
.mnTop
);
109 mnBottom
= std::min( mnBottom
, aTmpRect
.mnBottom
);
111 // Determine if intersection is empty
112 if ( mnRight
< mnLeft
|| mnBottom
< mnTop
)
113 *this = tools::Rectangle();
116 void RectangleTemplateBase::Normalize()
118 if ((mnRight
< mnLeft
) && (!IsWidthEmpty()))
120 std::swap(mnLeft
, mnRight
);
123 if ((mnBottom
< mnTop
) && (!IsHeightEmpty()))
125 std::swap(mnBottom
, mnTop
);
129 bool RectangleTemplateBase::Contains( const PointTemplateBase
& rPoint
) const
134 if ( mnLeft
<= mnRight
)
136 if ( (rPoint
.X() < mnLeft
) || (rPoint
.X() > mnRight
) )
141 if ( (rPoint
.X() > mnLeft
) || (rPoint
.X() < mnRight
) )
144 if ( mnTop
<= mnBottom
)
146 if ( (rPoint
.Y() < mnTop
) || (rPoint
.Y() > mnBottom
) )
151 if ( (rPoint
.Y() > mnTop
) || (rPoint
.Y() < mnBottom
) )
157 bool RectangleTemplateBase::Contains( const RectangleTemplateBase
& rRect
) const
159 return Contains( PointTemplateBase
{ rRect
.Left(), rRect
.Top() } )
160 && Contains( PointTemplateBase
{ rRect
.Right(), rRect
.Bottom() } );
163 bool RectangleTemplateBase::Overlaps( const RectangleTemplateBase
& rRect
) const
165 // If there's no intersection, they don't overlap
166 RectangleTemplateBase
aTmp(*this);
167 aTmp
.Intersection(rRect
);
168 return !aTmp
.IsEmpty();
171 OString
RectangleTemplateBase::toString() const
173 // Note that this is not just used for debugging output but the
174 // format is parsed by external code (passed in callbacks to
175 // LibreOfficeKit clients). So don't change.
176 return OString::number(Left()) + ", "
177 + OString::number(Top()) + ", "
178 + OString::number(getOpenWidth()) + ", "
179 + OString::number(getOpenHeight());
182 void RectangleTemplateBase::expand(tools::Long nExpandBy
)
184 AdjustLeft(-nExpandBy
);
185 AdjustTop(-nExpandBy
);
186 AdjustRight(nExpandBy
);
187 AdjustBottom(nExpandBy
);
190 void RectangleTemplateBase::shrink(tools::Long nShrinkBy
)
195 mnRight
-= nShrinkBy
;
196 if (!IsHeightEmpty())
197 mnBottom
-= nShrinkBy
;
200 tools::Long
RectangleTemplateBase::AdjustRight(tools::Long nHorzMoveDelta
)
203 mnRight
= mnLeft
+ nHorzMoveDelta
- 1;
205 mnRight
+= nHorzMoveDelta
;
209 tools::Long
RectangleTemplateBase::AdjustBottom( tools::Long nVertMoveDelta
)
212 mnBottom
= mnTop
+ nVertMoveDelta
- 1;
214 mnBottom
+= nVertMoveDelta
;
218 static_assert( std::is_trivially_copyable
< Pair
>::value
);
219 static_assert( std::is_trivially_copyable
< Point
>::value
);
220 static_assert( std::is_trivially_copyable
< Size
>::value
);
221 static_assert( std::is_trivially_copyable
< Range
>::value
);
222 static_assert( std::is_trivially_copyable
< Selection
>::value
);
223 static_assert( std::is_trivially_copyable
< tools::Rectangle
>::value
);
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */