LibreOffice Module sw (master)  1
swrect.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 <swrect.hxx>
21 
22 #include <libxml/xmlwriter.h>
23 
24 #ifdef DBG_UTIL
25 #include <tools/stream.hxx>
26 #endif
27 
29  m_Point( rRect.Left(), rRect.Top() )
30 {
31  m_Size.setWidth( rRect.IsWidthEmpty() ? 0 : rRect.Right() - rRect.Left() + 1);
32  m_Size.setHeight(rRect.IsHeightEmpty() ? 0 : rRect.Bottom() - rRect.Top() + 1);
33 }
34 
36 {
37  return Point( Left() + Width() / 2,
38  Top() + Height() / 2 );
39 }
40 
41 SwRect& SwRect::Union( const SwRect& rRect )
42 {
43  if ( Top() > rRect.Top() )
44  Top( rRect.Top() );
45  if ( Left() > rRect.Left() )
46  Left( rRect.Left() );
47  long n = rRect.Right();
48  if ( Right() < n )
49  Right( n );
50  n = rRect.Bottom();
51  if ( Bottom() < n )
52  Bottom( n );
53  return *this;
54 }
55 
57 {
58  // any similarity between me and given element?
59  if ( IsOver( rRect ) )
60  {
61  // get smaller right and lower, and greater left and upper edge
62  if ( Left() < rRect.Left() )
63  Left( rRect.Left() );
64  if ( Top() < rRect.Top() )
65  Top( rRect.Top() );
66  long n = rRect.Right();
67  if ( Right() > n )
68  Right( n );
69  n = rRect.Bottom();
70  if ( Bottom() > n )
71  Bottom( n );
72  }
73  else
74  // Def.: if intersection is empty, set only SSize to 0
75  SSize(0, 0);
76 
77  return *this;
78 }
79 
81 {
82  // get smaller right and lower, and greater left and upper edge
83  auto left = std::max( m_Point.X(), rOther.m_Point.X() );
84  auto top = std::max( m_Point.Y(), rOther.m_Point.Y() );
85  long right = std::min( m_Point.X() + m_Size.Width(), rOther.m_Point.X() + rOther.m_Size.Width() );
86  auto bottom = std::min( m_Point.Y() + m_Size.Height(), rOther.m_Point.Y() + rOther.m_Size.Height() );
87 
88  *this = SwRect( left, top, right - left, bottom - top );
89 
90  return *this;
91 }
92 
93 bool SwRect::IsInside( const SwRect& rRect ) const
94 {
95  const long nRight = Right();
96  const long nBottom = Bottom();
97  const long nrRight = rRect.Right();
98  const long nrBottom= rRect.Bottom();
99  return (Left() <= rRect.Left()) && (rRect.Left()<= nRight) &&
100  (Left() <= nrRight) && (nrRight <= nRight) &&
101  (Top() <= rRect.Top()) && (rRect.Top() <= nBottom) &&
102  (Top() <= nrBottom) && (nrBottom <= nBottom);
103 }
104 
105 bool SwRect::IsInside( const Point& rPoint ) const
106 {
107  return (Left() <= rPoint.X()) &&
108  (Top() <= rPoint.Y()) &&
109  (Right() >= rPoint.X()) &&
110  (Bottom()>= rPoint.Y());
111 }
112 
113 // mouse moving of table borders
114 bool SwRect::IsNear( const Point& rPoint, long nTolerance ) const
115 {
116  bool bIsNearby = (((Left() - nTolerance) <= rPoint.X()) &&
117  ((Top() - nTolerance) <= rPoint.Y()) &&
118  ((Right() + nTolerance) >= rPoint.X()) &&
119  ((Bottom() + nTolerance) >= rPoint.Y()));
120  return IsInside(rPoint) || bIsNearby;
121 }
122 
123 bool SwRect::IsOver( const SwRect& rRect ) const
124 {
125  return (Top() <= rRect.Bottom()) &&
126  (Left() <= rRect.Right()) &&
127  (Right() >= rRect.Left()) &&
128  (Bottom()>= rRect.Top());
129 }
130 
132 {
133  if ( m_Size.getHeight() < 0 )
134  {
135  m_Point.setY(m_Point.getY() + m_Size.getHeight() + 1);
137  }
138  if ( m_Size.getWidth() < 0 )
139  {
140  m_Point.setX(m_Point.getX() + m_Size.getWidth() + 1);
142  }
143 }
144 
145 // Similar to the inline methods, but we need the function pointers
146 void SwRect::Width_( const long nNew ) { m_Size.setWidth(nNew); }
147 void SwRect::Height_( const long nNew ) { m_Size.setHeight(nNew); }
148 void SwRect::Left_( const long nLeft ){ m_Size.AdjustWidth(m_Point.getX() - nLeft ); m_Point.setX(nLeft); }
149 void SwRect::Right_( const long nRight ){ m_Size.setWidth(nRight - m_Point.getX()); }
150 void SwRect::Top_( const long nTop ){ m_Size.AdjustHeight(m_Point.getY() - nTop ); m_Point.setY(nTop); }
151 void SwRect::Bottom_( const long nBottom ){ m_Size.setHeight(nBottom - m_Point.getY()); }
152 
153 long SwRect::Width_() const{ return m_Size.getWidth(); }
154 long SwRect::Height_() const{ return m_Size.getHeight(); }
155 long SwRect::Left_() const{ return m_Point.getX(); }
156 long SwRect::Right_() const{ return m_Point.getX() + m_Size.getWidth(); }
157 long SwRect::Top_() const{ return m_Point.getY(); }
158 long SwRect::Bottom_() const{ return m_Point.getY() + m_Size.getHeight(); }
159 
160 void SwRect::AddWidth( const long nAdd ) { m_Size.AdjustWidth(nAdd ); }
161 void SwRect::AddHeight( const long nAdd ) { m_Size.AdjustHeight(nAdd ); }
162 void SwRect::AddLeft( const long nAdd ){ m_Size.AdjustWidth(-nAdd ); m_Point.setX(m_Point.getX() + nAdd); }
163 void SwRect::SubLeft( const long nSub ){ m_Size.AdjustWidth(nSub ); m_Point.setX(m_Point.getX() - nSub); }
164 void SwRect::AddRight( const long nAdd ){ m_Size.AdjustWidth(nAdd ); }
165 void SwRect::AddTop( const long nAdd ){ m_Size.AdjustHeight(-nAdd ); m_Point.setY(m_Point.getY() + nAdd); }
166 void SwRect::SubTop( const long nSub ){ m_Size.AdjustHeight(nSub ); m_Point.setY(m_Point.getY() - nSub); }
167 void SwRect::AddBottom( const long nAdd ){ m_Size.AdjustHeight(nAdd ); }
168 void SwRect::SetPosX( const long nNew ){ m_Point.setX(nNew); }
169 void SwRect::SetPosY( const long nNew ){ m_Point.setY(nNew); }
170 
171 Size SwRect::Size_() const { return SSize(); }
173 
174 Point SwRect::TopLeft() const { return Pos(); }
175 Point SwRect::TopRight() const { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() ); }
176 Point SwRect::BottomLeft() const { return Point( m_Point.getX(), m_Point.getY() + m_Size.getHeight() ); }
178  { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() + m_Size.getHeight() ); }
179 
180 long SwRect::GetLeftDistance( long nLimit ) const { return m_Point.getX() - nLimit; }
181 long SwRect::GetBottomDistance( long nLim ) const { return nLim - m_Point.getY() - m_Size.getHeight();}
182 long SwRect::GetTopDistance( long nLimit ) const { return m_Point.getY() - nLimit; }
183 long SwRect::GetRightDistance( long nLim ) const { return nLim - m_Point.getX() - m_Size.getWidth(); }
184 
185 bool SwRect::OverStepLeft( long nLimit ) const
186  { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
187 bool SwRect::OverStepBottom( long nLimit ) const
188  { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
189 bool SwRect::OverStepTop( long nLimit ) const
190  { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
191 bool SwRect::OverStepRight( long nLimit ) const
192  { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
193 
194 void SwRect::SetLeftAndWidth( long nLeft, long nNew )
195 {
196  m_Point.setX(nLeft);
197  m_Size.setWidth(nNew);
198 }
199 void SwRect::SetTopAndHeight( long nTop, long nNew )
200 {
201  m_Point.setY(nTop);
202  m_Size.setHeight(nNew);
203 }
204 void SwRect::SetRightAndWidth( long nRight, long nNew )
205 {
206  m_Point.setX(nRight - nNew);
207  m_Size.setWidth(nNew);
208 }
209 void SwRect::SetBottomAndHeight( long nBottom, long nNew )
210 {
211  m_Point.setY(nBottom - nNew);
212  m_Size.setHeight(nNew);
213 }
214 void SwRect::SetUpperLeftCorner( const Point& rNew )
215  { m_Point = rNew; }
216 void SwRect::SetUpperRightCorner( const Point& rNew )
217  { m_Point = Point(rNew.X() - m_Size.getWidth(), rNew.Y()); }
218 void SwRect::SetLowerLeftCorner( const Point& rNew )
219  { m_Point = Point(rNew.X(), rNew.Y() - m_Size.getHeight()); }
220 
222 {
223  xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("left"), "%li", Left());
224  xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("top"), "%li", Top());
225  xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("width"), "%li", Width());
226  xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("height"), "%li", Height());
227  xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("bottom"), "%li", Bottom());
228  xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("right"), "%li", Right());
229 }
230 
231 #ifdef DBG_UTIL
232 SvStream& WriteSwRect(SvStream &rStream, const SwRect &rRect)
233 {
234  rStream.WriteChar('[').WriteInt32(rRect.Top()).
235  WriteChar('/').WriteInt32(rRect.Left()).
236  WriteChar(',').WriteInt32(rRect.Width()).
237  WriteChar('x').WriteInt32(rRect.Height()).
238  WriteCharPtr("] ");
239  return rStream;
240 }
241 #endif
242 
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
Size m_Size
Definition: swrect.hxx:38
Size Size_() const
Definition: swrect.cxx:171
long Right() const
Definition: swrect.hxx:239
bool OverStepLeft(long) const
Definition: swrect.cxx:185
bool IsHeightEmpty() const
long Bottom_() const
Definition: swrect.cxx:158
SwRect & Union(const SwRect &rRect)
Definition: swrect.cxx:41
long AdjustWidth(long n)
sal_uInt64 left
SvStream & WriteInt32(sal_Int32 nInt32)
long Height() const
long GetBottomDistance(long) const
Definition: swrect.cxx:181
sal_Int64 n
void Height(long nNew)
Definition: swrect.hxx:191
bool IsNear(const Point &rPoint, long nTolerance) const
Definition: swrect.cxx:114
void SetPosX(const long nNew)
Definition: swrect.cxx:168
Of course Writer needs its own rectangles.
Definition: swrect.hxx:35
long Left() const
Definition: swrect.hxx:235
long GetLeftDistance(long) const
Definition: swrect.cxx:180
long AdjustHeight(long n)
long Width() const
Definition: swrect.hxx:227
bool OverStepTop(long) const
Definition: swrect.cxx:189
SwRect & Intersection_(const SwRect &rRect)
Definition: swrect.cxx:80
Point TopLeft() const
Definition: swrect.cxx:174
long Left_() const
Definition: swrect.cxx:155
long Right() const
void Top(const long nTop)
Definition: swrect.hxx:204
SwRect & Intersection(const SwRect &rRect)
Definition: swrect.cxx:56
long Top() const
void Right(const long nRight)
Definition: swrect.hxx:200
long GetRightDistance(long) const
Definition: swrect.cxx:183
long Height() const
Definition: swrect.hxx:231
void SubLeft(const long nSub)
Definition: swrect.cxx:163
Point BottomRight() const
Definition: swrect.cxx:177
void SetLeftAndWidth(long nLeft, long nNew)
Definition: swrect.cxx:194
struct _xmlTextWriter * xmlTextWriterPtr
def right
long Height_() const
Definition: swrect.cxx:154
const Size & SSize() const
Definition: swrect.hxx:223
long Width_() const
Definition: swrect.cxx:153
void SubTop(const long nSub)
Definition: swrect.cxx:166
void AddWidth(const long nAdd)
Definition: swrect.cxx:160
void SetPosY(const long nNew)
Definition: swrect.cxx:169
void AddRight(const long nAdd)
Definition: swrect.cxx:164
void AddHeight(const long nAdd)
Definition: swrect.cxx:161
long Bottom() const
Point Center() const
Definition: swrect.cxx:35
void SetUpperLeftCorner(const Point &rNew)
Definition: swrect.cxx:214
void SetRightAndWidth(long nRight, long nNew)
Definition: swrect.cxx:204
bool OverStepBottom(long) const
Definition: swrect.cxx:187
void Left(const long nLeft)
Definition: swrect.hxx:195
void Bottom(const long nBottom)
Definition: swrect.hxx:209
void SetUpperRightCorner(const Point &rNew)
Definition: swrect.cxx:216
void Justify()
Definition: swrect.cxx:131
void SetTopAndHeight(long nTop, long nNew)
Definition: swrect.cxx:199
long Bottom() const
Definition: swrect.hxx:247
long Top() const
Definition: swrect.hxx:243
Point m_Point
Definition: swrect.hxx:37
void Width(long nNew)
Definition: swrect.hxx:187
void AddLeft(const long nAdd)
Definition: swrect.cxx:162
long GetTopDistance(long) const
Definition: swrect.cxx:182
void SetLowerLeftCorner(const Point &rNew)
Definition: swrect.cxx:218
bool IsInside(const Point &rPOINT) const
Definition: swrect.cxx:105
bool IsWidthEmpty() const
long Left() const
void AddTop(const long nAdd)
Definition: swrect.cxx:165
SvStream & WriteChar(char nChar)
Size SwappedSize() const
Definition: swrect.cxx:172
Point TopRight() const
Definition: swrect.cxx:175
void AddBottom(const long nAdd)
Definition: swrect.cxx:167
void SetBottomAndHeight(long nBottom, long nNew)
Definition: swrect.cxx:209
Left
long getHeight() const
bool OverStepRight(long) const
Definition: swrect.cxx:191
long Top_() const
Definition: swrect.cxx:157
bool IsOver(const SwRect &rRect) const
Definition: swrect.cxx:123
long getWidth() const
void setWidth(long nWidth)
const Point & Pos() const
Definition: swrect.hxx:215
SwRect()
Definition: swrect.hxx:305
void dumpAsXmlAttributes(xmlTextWriterPtr writer) const
Definition: swrect.cxx:221
SvStream & WriteSwRect(SvStream &rStream, const SwRect &rRect)
Definition: swrect.cxx:232
long Right_() const
Definition: swrect.cxx:156
Point BottomLeft() const
Definition: swrect.cxx:176
void setHeight(long nHeight)