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  tools::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  tools::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  tools::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 tools::Long nRight = Right();
96  const tools::Long nBottom = Bottom();
97  const tools::Long nrRight = rRect.Right();
98  const tools::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, tools::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 tools::Long nNew ) { m_Size.setWidth(nNew); }
147 void SwRect::Height_( const tools::Long nNew ) { m_Size.setHeight(nNew); }
148 void SwRect::Left_( const tools::Long nLeft ){ m_Size.AdjustWidth(m_Point.getX() - nLeft ); m_Point.setX(nLeft); }
149 void SwRect::Right_( const tools::Long nRight ){ m_Size.setWidth(nRight - m_Point.getX()); }
150 void SwRect::Top_( const tools::Long nTop ){ m_Size.AdjustHeight(m_Point.getY() - nTop ); m_Point.setY(nTop); }
151 void SwRect::Bottom_( const tools::Long nBottom ){ m_Size.setHeight(nBottom - m_Point.getY()); }
152 
155 tools::Long SwRect::Left_() const{ return m_Point.getX(); }
156 tools::Long SwRect::Right_() const{ return m_Point.getX() + m_Size.getWidth(); }
157 tools::Long SwRect::Top_() const{ return m_Point.getY(); }
158 tools::Long SwRect::Bottom_() const{ return m_Point.getY() + m_Size.getHeight(); }
159 
160 void SwRect::AddWidth( const tools::Long nAdd ) { m_Size.AdjustWidth(nAdd ); }
161 void SwRect::AddHeight( const tools::Long nAdd ) { m_Size.AdjustHeight(nAdd ); }
162 void SwRect::AddLeft( const tools::Long nAdd ){ m_Size.AdjustWidth(-nAdd ); m_Point.setX(m_Point.getX() + nAdd); }
163 void SwRect::SubLeft( const tools::Long nSub ){ m_Size.AdjustWidth(nSub ); m_Point.setX(m_Point.getX() - nSub); }
164 void SwRect::AddRight( const tools::Long nAdd ){ m_Size.AdjustWidth(nAdd ); }
165 void SwRect::AddTop( const tools::Long nAdd ){ m_Size.AdjustHeight(-nAdd ); m_Point.setY(m_Point.getY() + nAdd); }
166 void SwRect::SubTop( const tools::Long nSub ){ m_Size.AdjustHeight(nSub ); m_Point.setY(m_Point.getY() - nSub); }
167 void SwRect::AddBottom( const tools::Long nAdd ){ m_Size.AdjustHeight(nAdd ); }
168 void SwRect::SetPosX( const tools::Long nNew ){ m_Point.setX(nNew); }
169 void SwRect::SetPosY( const tools::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 tools::Long SwRect::GetLeftDistance( tools::Long nLimit ) const { return m_Point.getX() - nLimit; }
181 tools::Long SwRect::GetBottomDistance( tools::Long nLim ) const { return nLim - m_Point.getY() - m_Size.getHeight();}
182 tools::Long SwRect::GetTopDistance( tools::Long nLimit ) const { return m_Point.getY() - nLimit; }
183 tools::Long SwRect::GetRightDistance( tools::Long nLim ) const { return nLim - m_Point.getX() - m_Size.getWidth(); }
184 
185 bool SwRect::OverStepLeft( tools::Long nLimit ) const
186  { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
188  { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
189 bool SwRect::OverStepTop( tools::Long nLimit ) const
190  { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
191 bool SwRect::OverStepRight( tools::Long nLimit ) const
192  { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
193 
195 {
196  m_Point.setX(nLeft);
197  m_Size.setWidth(nNew);
198 }
200 {
201  m_Point.setY(nTop);
202  m_Size.setHeight(nNew);
203 }
205 {
206  m_Point.setX(nRight - nNew);
207  m_Size.setWidth(nNew);
208 }
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: */
Size m_Size
Definition: swrect.hxx:38
Size Size_() const
Definition: swrect.cxx:171
bool OverStepBottom(tools::Long) const
Definition: swrect.cxx:187
void Right(const tools::Long nRight)
Definition: swrect.hxx:200
bool IsHeightEmpty() const
tools::Long GetRightDistance(tools::Long) const
Definition: swrect.cxx:183
SwRect & Union(const SwRect &rRect)
Definition: swrect.cxx:41
void setWidth(tools::Long nWidth)
sal_uInt64 left
SvStream & WriteInt32(sal_Int32 nInt32)
void SubLeft(const tools::Long nSub)
Definition: swrect.cxx:163
void Left(const tools::Long nLeft)
Definition: swrect.hxx:195
tools::Long getWidth() const
long Long
tools::Long Right() const
Definition: swrect.hxx:239
sal_Int64 n
tools::Long Height() const
Definition: swrect.hxx:231
Of course Writer needs its own rectangles.
Definition: swrect.hxx:35
void SetRightAndWidth(tools::Long nRight, tools::Long nNew)
Definition: swrect.cxx:204
void SetTopAndHeight(tools::Long nTop, tools::Long nNew)
Definition: swrect.cxx:199
void AddTop(const tools::Long nAdd)
Definition: swrect.cxx:165
SwRect & Intersection_(const SwRect &rRect)
Definition: swrect.cxx:80
tools::Long Right_() const
Definition: swrect.cxx:156
Point TopLeft() const
Definition: swrect.cxx:174
tools::Long Bottom() const
Definition: swrect.hxx:247
tools::Long Left() const
bool OverStepRight(tools::Long) const
Definition: swrect.cxx:191
SwRect & Intersection(const SwRect &rRect)
Definition: swrect.cxx:56
tools::Long Bottom() const
void Width(tools::Long nNew)
Definition: swrect.hxx:187
bool OverStepLeft(tools::Long) const
Definition: swrect.cxx:185
Point BottomRight() const
Definition: swrect.cxx:177
tools::Long getHeight() const
struct _xmlTextWriter * xmlTextWriterPtr
void SetLeftAndWidth(tools::Long nLeft, tools::Long nNew)
Definition: swrect.cxx:194
def right
const Size & SSize() const
Definition: swrect.hxx:223
void AddBottom(const tools::Long nAdd)
Definition: swrect.cxx:167
tools::Long Height_() const
Definition: swrect.cxx:154
tools::Long Width() const
void AddHeight(const tools::Long nAdd)
Definition: swrect.cxx:161
void AddWidth(const tools::Long nAdd)
Definition: swrect.cxx:160
tools::Long Left_() const
Definition: swrect.cxx:155
void AddRight(const tools::Long nAdd)
Definition: swrect.cxx:164
void SetPosX(const tools::Long nNew)
Definition: swrect.cxx:168
tools::Long Top() const
Point Center() const
Definition: swrect.cxx:35
void SetUpperLeftCorner(const Point &rNew)
Definition: swrect.cxx:214
tools::Long GetTopDistance(tools::Long) const
Definition: swrect.cxx:182
void AddLeft(const tools::Long nAdd)
Definition: swrect.cxx:162
void Bottom(const tools::Long nBottom)
Definition: swrect.hxx:209
void SubTop(const tools::Long nSub)
Definition: swrect.cxx:166
void SetUpperRightCorner(const Point &rNew)
Definition: swrect.cxx:216
tools::Long Left() const
Definition: swrect.hxx:235
void Justify()
Definition: swrect.cxx:131
tools::Long Width_() const
Definition: swrect.cxx:153
tools::Long GetBottomDistance(tools::Long) const
Definition: swrect.cxx:181
Point m_Point
Definition: swrect.hxx:37
tools::Long Top() const
Definition: swrect.hxx:243
void SetLowerLeftCorner(const Point &rNew)
Definition: swrect.cxx:218
bool IsInside(const Point &rPOINT) const
Definition: swrect.cxx:105
bool OverStepTop(tools::Long) const
Definition: swrect.cxx:189
tools::Long AdjustWidth(tools::Long n)
void Top(const tools::Long nTop)
Definition: swrect.hxx:204
tools::Long Height() const
tools::Long Bottom_() const
Definition: swrect.cxx:158
bool IsWidthEmpty() const
SvStream & WriteChar(char nChar)
tools::Long AdjustHeight(tools::Long n)
void SetPosY(const tools::Long nNew)
Definition: swrect.cxx:169
Size SwappedSize() const
Definition: swrect.cxx:172
void setHeight(tools::Long nHeight)
Point TopRight() const
Definition: swrect.cxx:175
tools::Long Top_() const
Definition: swrect.cxx:157
void SetBottomAndHeight(tools::Long nBottom, tools::Long nNew)
Definition: swrect.cxx:209
Left
tools::Long GetLeftDistance(tools::Long) const
Definition: swrect.cxx:180
bool IsNear(const Point &rPoint, tools::Long nTolerance) const
Definition: swrect.cxx:114
bool IsOver(const SwRect &rRect) const
Definition: swrect.cxx:123
tools::Long Width() const
Definition: swrect.hxx:227
void Height(tools::Long nNew)
Definition: swrect.hxx:191
const Point & Pos() const
Definition: swrect.hxx:215
tools::Long Right() const
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
Point BottomLeft() const
Definition: swrect.cxx:176