Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / bastyp / swrect.cxx
blob97e9683a59abc49d7e6726103e01dcf930ef6931
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 <swrect.hxx>
22 #include <libxml/xmlwriter.h>
24 #ifdef DBG_UTIL
25 #include <tools/stream.hxx>
26 #endif
28 SwRect::SwRect( const tools::Rectangle &rRect ) :
29 m_Point( rRect.Left(), rRect.Top() )
31 m_Size.setWidth( rRect.IsWidthEmpty() ? 0 : rRect.Right() - rRect.Left() + 1);
32 m_Size.setHeight(rRect.IsHeightEmpty() ? 0 : rRect.Bottom() - rRect.Top() + 1);
35 SwRect& SwRect::Union( const SwRect& rRect )
37 if( rRect.IsEmpty())
38 return *this;
39 if( IsEmpty())
41 *this = rRect;
42 return *this;
44 if ( Top() > rRect.Top() )
45 Top( rRect.Top() );
46 if ( Left() > rRect.Left() )
47 Left( rRect.Left() );
48 tools::Long n = rRect.Right();
49 if ( Right() < n )
50 Right( n );
51 n = rRect.Bottom();
52 if ( Bottom() < n )
53 Bottom( n );
54 return *this;
57 SwRect& SwRect::Intersection( const SwRect& rRect )
59 // any similarity between me and given element?
60 if ( Overlaps( rRect ) )
62 // get smaller right and lower, and greater left and upper edge
63 if ( Left() < rRect.Left() )
64 Left( rRect.Left() );
65 if ( Top() < rRect.Top() )
66 Top( rRect.Top() );
67 tools::Long n = rRect.Right();
68 if ( Right() > n )
69 Right( n );
70 n = rRect.Bottom();
71 if ( Bottom() > n )
72 Bottom( n );
74 else
75 // Def.: if intersection is empty, set only SSize to 0
76 SSize(0, 0);
78 return *this;
81 SwRect& SwRect::Intersection_( const SwRect& rOther )
83 // get smaller right and lower, and greater left and upper edge
84 auto left = std::max( m_Point.X(), rOther.m_Point.X() );
85 auto top = std::max( m_Point.Y(), rOther.m_Point.Y() );
86 tools::Long right = std::min( m_Point.X() + m_Size.Width(), rOther.m_Point.X() + rOther.m_Size.Width() );
87 auto bottom = std::min( m_Point.Y() + m_Size.Height(), rOther.m_Point.Y() + rOther.m_Size.Height() );
89 *this = SwRect( left, top, right - left, bottom - top );
91 return *this;
94 void SwRect::Justify()
96 if ( m_Size.getHeight() < 0 )
98 m_Point.setY(m_Point.getY() + m_Size.getHeight() + 1);
99 m_Size.setHeight(-m_Size.getHeight());
101 if ( m_Size.getWidth() < 0 )
103 m_Point.setX(m_Point.getX() + m_Size.getWidth() + 1);
104 m_Size.setWidth(-m_Size.getWidth());
108 // Similar to the inline methods, but we need the function pointers
109 void SwRect::Width_( const tools::Long nNew ) { m_Size.setWidth(nNew); }
110 void SwRect::Height_( const tools::Long nNew ) { m_Size.setHeight(nNew); }
111 void SwRect::Left_( const tools::Long nLeft ){ m_Size.AdjustWidth(m_Point.getX() - nLeft ); m_Point.setX(nLeft); }
112 void SwRect::Right_( const tools::Long nRight ){ m_Size.setWidth(nRight - m_Point.getX()); }
113 void SwRect::Top_( const tools::Long nTop ){ m_Size.AdjustHeight(m_Point.getY() - nTop ); m_Point.setY(nTop); }
114 void SwRect::Bottom_( const tools::Long nBottom ){ m_Size.setHeight(nBottom - m_Point.getY()); }
116 tools::Long SwRect::Width_() const{ return m_Size.getWidth(); }
117 tools::Long SwRect::Height_() const{ return m_Size.getHeight(); }
118 tools::Long SwRect::Left_() const{ return m_Point.getX(); }
119 tools::Long SwRect::Right_() const{ return m_Point.getX() + m_Size.getWidth(); }
120 tools::Long SwRect::Top_() const{ return m_Point.getY(); }
121 tools::Long SwRect::Bottom_() const{ return m_Point.getY() + m_Size.getHeight(); }
123 void SwRect::AddWidth( const tools::Long nAdd ) { m_Size.AdjustWidth(nAdd ); }
124 void SwRect::AddHeight( const tools::Long nAdd ) { m_Size.AdjustHeight(nAdd ); }
125 void SwRect::AddLeft( const tools::Long nAdd ){ m_Size.AdjustWidth(-nAdd ); m_Point.setX(m_Point.getX() + nAdd); }
126 void SwRect::SubLeft( const tools::Long nSub ){ m_Size.AdjustWidth(nSub ); m_Point.setX(m_Point.getX() - nSub); }
127 void SwRect::AddRight( const tools::Long nAdd ){ m_Size.AdjustWidth(nAdd ); }
128 void SwRect::AddTop( const tools::Long nAdd ){ m_Size.AdjustHeight(-nAdd ); m_Point.setY(m_Point.getY() + nAdd); }
129 void SwRect::SubTop( const tools::Long nSub ){ m_Size.AdjustHeight(nSub ); m_Point.setY(m_Point.getY() - nSub); }
130 void SwRect::AddBottom( const tools::Long nAdd ){ m_Size.AdjustHeight(nAdd ); }
131 void SwRect::SetPosX( const tools::Long nNew ){ m_Point.setX(nNew); }
132 void SwRect::SetPosY( const tools::Long nNew ){ m_Point.setY(nNew); }
134 Size SwRect::Size_() const { return SSize(); }
135 Size SwRect::SwappedSize() const { return Size( m_Size.getHeight(), m_Size.getWidth() ); }
137 tools::Long SwRect::GetLeftDistance( tools::Long nLimit ) const { return m_Point.getX() - nLimit; }
138 tools::Long SwRect::GetBottomDistance( tools::Long nLim ) const { return nLim - m_Point.getY() - m_Size.getHeight();}
139 tools::Long SwRect::GetTopDistance( tools::Long nLimit ) const { return m_Point.getY() - nLimit; }
140 tools::Long SwRect::GetRightDistance( tools::Long nLim ) const { return nLim - m_Point.getX() - m_Size.getWidth(); }
142 bool SwRect::OverStepLeft( tools::Long nLimit ) const
143 { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
144 bool SwRect::OverStepBottom( tools::Long nLimit ) const
145 { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
146 bool SwRect::OverStepTop( tools::Long nLimit ) const
147 { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
148 bool SwRect::OverStepRight( tools::Long nLimit ) const
149 { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
151 void SwRect::SetLeftAndWidth( tools::Long nLeft, tools::Long nNew )
153 m_Point.setX(nLeft);
154 m_Size.setWidth(nNew);
156 void SwRect::SetTopAndHeight( tools::Long nTop, tools::Long nNew )
158 m_Point.setY(nTop);
159 m_Size.setHeight(nNew);
161 void SwRect::SetRightAndWidth( tools::Long nRight, tools::Long nNew )
163 m_Point.setX(nRight - nNew);
164 m_Size.setWidth(nNew);
166 void SwRect::SetBottomAndHeight( tools::Long nBottom, tools::Long nNew )
168 m_Point.setY(nBottom - nNew);
169 m_Size.setHeight(nNew);
171 void SwRect::SetUpperLeftCorner( const Point& rNew )
172 { m_Point = rNew; }
173 void SwRect::SetUpperRightCorner( const Point& rNew )
174 { m_Point = Point(rNew.X() - m_Size.getWidth(), rNew.Y()); }
175 void SwRect::SetLowerLeftCorner( const Point& rNew )
176 { m_Point = Point(rNew.X(), rNew.Y() - m_Size.getHeight()); }
178 void SwRect::dumpAsXmlAttributes(xmlTextWriterPtr writer) const
180 (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("left"), "%li", Left());
181 (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("top"), "%li", Top());
182 (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("width"), "%li", Width());
183 (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("height"), "%li", Height());
184 (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("bottom"), "%li", Bottom());
185 (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("right"), "%li", Right());
188 #ifdef DBG_UTIL
189 SvStream& WriteSwRect(SvStream &rStream, const SwRect &rRect)
191 rStream.WriteChar('[').WriteInt32(rRect.Top()).
192 WriteChar('/').WriteInt32(rRect.Left()).
193 WriteChar(',').WriteInt32(rRect.Width()).
194 WriteChar('x').WriteInt32(rRect.Height()).
195 WriteOString("] ");
196 return rStream;
198 #endif
200 static_assert( std::is_trivially_copyable< SwRect >::value );
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */