LibreOffice Module tools (master) 1
gen.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21#include <rtl/string.hxx>
22
23#include <algorithm>
24#include <tuple>
25#include <o3tl/hash_combine.hxx>
26#include <o3tl/safeint.hxx>
27#include <tools/gen.hxx>
28
29OString Pair::toString() const
30{
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());
35}
36
37size_t Pair::GetHashValue() const
38{
39 size_t hash = 0;
40 o3tl::hash_combine( hash, mnA );
41 o3tl::hash_combine( hash, mnB );
42 return hash;
43}
44
46{
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));
51 else
53
54 if ( rSize.Height() < 0 )
55 mnBottom = o3tl::saturating_add(mnTop, (rSize.Height() + 1));
56 else if ( rSize.Height() > 0 )
58 else
60}
61
63{
64 if (!IsWidthEmpty())
66 mnLeft = x;
67}
68
70{
71 if (!IsHeightEmpty())
73 mnTop = y;
74}
75
77{
78 if ( rRect.IsEmpty() )
79 return *this;
80
81 if ( IsEmpty() )
82 *this = rRect;
83 else
84 {
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 });
87 }
88
89 return *this;
90}
91
93{
94 if ( IsEmpty() )
95 return *this;
96 if ( rRect.IsEmpty() )
97 {
98 *this = tools::Rectangle();
99 return *this;
100 }
101
102 // Normalize rectangle
103 tools::Rectangle aTmpRect( rRect );
104 Normalize();
105 aTmpRect.Normalize();
106
107 // Perform intersection
108 mnLeft = std::max( mnLeft, aTmpRect.mnLeft );
109 mnRight = std::min( mnRight, aTmpRect.mnRight );
110 mnTop = std::max( mnTop, aTmpRect.mnTop );
111 mnBottom= std::min( mnBottom, aTmpRect.mnBottom );
112
113 // Determine if intersection is empty
114 if ( mnRight < mnLeft || mnBottom < mnTop )
115 *this = tools::Rectangle();
116
117 return *this;
118}
119
121{
122 if ((mnRight < mnLeft) && (!IsWidthEmpty()))
123 {
124 std::swap(mnLeft, mnRight);
125 }
126
127 if ((mnBottom < mnTop) && (!IsHeightEmpty()))
128 {
129 std::swap(mnBottom, mnTop);
130 }
131}
132
133bool tools::Rectangle::Contains( const Point& rPoint ) const
134{
135 if ( IsEmpty() )
136 return false;
137
138 if ( mnLeft <= mnRight )
139 {
140 if ( (rPoint.X() < mnLeft) || (rPoint.X() > mnRight) )
141 return false;
142 }
143 else
144 {
145 if ( (rPoint.X() > mnLeft) || (rPoint.X() < mnRight) )
146 return false;
147 }
148 if ( mnTop <= mnBottom )
149 {
150 if ( (rPoint.Y() < mnTop) || (rPoint.Y() > mnBottom) )
151 return false;
152 }
153 else
154 {
155 if ( (rPoint.Y() > mnTop) || (rPoint.Y() < mnBottom) )
156 return false;
157 }
158 return true;
159}
160
162{
163 return Contains( rRect.TopLeft() ) && Contains( rRect.BottomRight() );
164}
165
167{
168 // If there's no intersection, they don't overlap
169 return !GetIntersection( rRect ).IsEmpty();
170}
171
173{
174 // Note that this is not just used for debugging output but the
175 // format is parsed by external code (passed in callbacks to
176 // LibreOfficeKit clients). So don't change.
177 return OString::number(Left()) + ", "
178 + OString::number(Top()) + ", "
179 + OString::number(getOpenWidth()) + ", "
180 + OString::number(getOpenHeight());
181}
182
184{
185 AdjustLeft(-nExpandBy);
186 AdjustTop(-nExpandBy);
187 AdjustRight(nExpandBy);
188 AdjustBottom(nExpandBy);
189}
190
192{
193 mnLeft += nShrinkBy;
194 mnTop += nShrinkBy;
195 if (!IsWidthEmpty())
196 mnRight -= nShrinkBy;
197 if (!IsHeightEmpty())
198 mnBottom -= nShrinkBy;
199}
200
202{
203 if (IsWidthEmpty())
204 mnRight = mnLeft + nHorzMoveDelta - 1;
205 else
206 mnRight += nHorzMoveDelta;
207 return mnRight;
208}
209
211{
212 if (IsHeightEmpty())
213 mnBottom = mnTop + nVertMoveDelta - 1;
214 else
215 mnBottom += nVertMoveDelta;
216 return mnBottom;
217}
218
219static_assert( std::is_trivially_copyable< Pair >::value );
220static_assert( std::is_trivially_copyable< Point >::value );
221static_assert( std::is_trivially_copyable< Size >::value );
222static_assert( std::is_trivially_copyable< Range >::value );
223static_assert( std::is_trivially_copyable< Selection >::value );
224static_assert( std::is_trivially_copyable< tools::Rectangle >::value );
225
226/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 mnTop
sal_Int32 mnRight
sal_Int32 mnBottom
sal_Int32 mnLeft
tools::Long mnA
Definition: gen.hxx:60
tools::Long mnB
Definition: gen.hxx:61
tools::Long A() const
Definition: gen.hxx:48
TOOLS_DLLPUBLIC rtl::OString toString() const
Definition: gen.cxx:29
tools::Long B() const
Definition: gen.hxx:49
TOOLS_DLLPUBLIC size_t GetHashValue() const
Definition: gen.cxx:37
Definition: gen.hxx:78
constexpr tools::Long Y() const
Definition: gen.hxx:84
constexpr tools::Long X() const
Definition: gen.hxx:83
Definition: gen.hxx:212
constexpr tools::Long Height() const
Definition: gen.hxx:218
constexpr tools::Long Width() const
Definition: gen.hxx:217
bool Contains(const Point &rPOINT) const
Definition: gen.cxx:133
bool Overlaps(const tools::Rectangle &rRect) const
Definition: gen.cxx:166
tools::Long mnRight
Definition: gen.hxx:602
static constexpr Rectangle Normalize(const Point &rLT, const Point &rRB)
Definition: gen.hxx:631
constexpr Point TopLeft() const
Definition: gen.hxx:510
void SaturatingSetPosX(tools::Long x)
Definition: gen.cxx:62
void SaturatingSetPosY(tools::Long y)
Definition: gen.cxx:69
void shrink(tools::Long nShrinkBy)
Definition: gen.cxx:191
tools::Long AdjustRight(tools::Long nHorzMoveDelta)
Definition: gen.cxx:201
constexpr Point BottomRight() const
Definition: gen.hxx:514
void Normalize()
Definition: gen.cxx:120
tools::Long mnTop
Definition: gen.hxx:601
tools::Rectangle & Union(const tools::Rectangle &rRect)
Definition: gen.cxx:76
tools::Long AdjustBottom(tools::Long nVertMoveDelta)
Definition: gen.cxx:210
void SetHeightEmpty()
Definition: gen.hxx:557
tools::Long mnLeft
Definition: gen.hxx:600
tools::Long mnBottom
Definition: gen.hxx:603
tools::Rectangle & Intersection(const tools::Rectangle &rRect)
Definition: gen.cxx:92
constexpr bool IsEmpty() const
Definition: gen.hxx:558
void SetWidthEmpty()
Definition: gen.hxx:556
rtl::OString toString() const
Returns the string representation of the rectangle, format is "x, y, width, height".
Definition: gen.cxx:172
void SaturatingSetSize(const Size &rSize)
Sanitizing variants for handling data from the outside.
Definition: gen.cxx:45
void expand(tools::Long nExpandBy)
Expands the rectangle in all directions by the input value.
Definition: gen.cxx:183
float y
float x
std::enable_if_t<(sizeof(N)==4)> hash_combine(N &nSeed, T const *pValue, size_t nCount)
constexpr T saturating_add(T a, T b)
long Long
Definition: long.hxx:34
Left