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 
35 SwRect& SwRect::Union( const SwRect& rRect )
36 {
37  if( rRect.IsEmpty())
38  return *this;
39  if( IsEmpty())
40  {
41  *this = rRect;
42  return *this;
43  }
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;
55 }
56 
58 {
59  // any similarity between me and given element?
60  if ( Overlaps( rRect ) )
61  {
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 );
73  }
74  else
75  // Def.: if intersection is empty, set only SSize to 0
76  SSize(0, 0);
77 
78  return *this;
79 }
80 
82 {
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() );
88 
89  *this = SwRect( left, top, right - left, bottom - top );
90 
91  return *this;
92 }
93 
95 {
96  if ( m_Size.getHeight() < 0 )
97  {
98  m_Point.setY(m_Point.getY() + m_Size.getHeight() + 1);
100  }
101  if ( m_Size.getWidth() < 0 )
102  {
103  m_Point.setX(m_Point.getX() + m_Size.getWidth() + 1);
105  }
106 }
107 
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()); }
115 
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(); }
122 
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); }
133 
134 Size SwRect::Size_() const { return SSize(); }
136 
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(); }
141 
142 bool SwRect::OverStepLeft( tools::Long nLimit ) const
143  { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
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; }
150 
152 {
153  m_Point.setX(nLeft);
154  m_Size.setWidth(nNew);
155 }
157 {
158  m_Point.setY(nTop);
159  m_Size.setHeight(nNew);
160 }
162 {
163  m_Point.setX(nRight - nNew);
164  m_Size.setWidth(nNew);
165 }
167 {
168  m_Point.setY(nBottom - nNew);
169  m_Size.setHeight(nNew);
170 }
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()); }
177 
179 {
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());
186 }
187 
188 #ifdef DBG_UTIL
189 SvStream& WriteSwRect(SvStream &rStream, const SwRect &rRect)
190 {
191  rStream.WriteChar('[').WriteInt32(rRect.Top()).
192  WriteChar('/').WriteInt32(rRect.Left()).
193  WriteChar(',').WriteInt32(rRect.Width()).
194  WriteChar('x').WriteInt32(rRect.Height()).
195  WriteCharPtr("] ");
196  return rStream;
197 }
198 #endif
199 
200 static_assert( std::is_trivially_copyable< SwRect >::value );
201 
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Size m_Size
Definition: swrect.hxx:37
Size Size_() const
Definition: swrect.cxx:134
bool OverStepBottom(tools::Long) const
Definition: swrect.cxx:144
void Right(const tools::Long nRight)
Definition: swrect.hxx:202
tools::Long GetRightDistance(tools::Long) const
Definition: swrect.cxx:140
SwRect & Union(const SwRect &rRect)
Definition: swrect.cxx:35
void setWidth(tools::Long nWidth)
sal_uInt64 left
SvStream & WriteInt32(sal_Int32 nInt32)
void SubLeft(const tools::Long nSub)
Definition: swrect.cxx:126
constexpr tools::Long Left() const
void Left(const tools::Long nLeft)
Definition: swrect.hxx:197
long Long
tools::Long Right() const
Definition: swrect.hxx:241
sal_Int64 n
tools::Long Height() const
Definition: swrect.hxx:233
Of course Writer needs its own rectangles.
Definition: swrect.hxx:34
void SetRightAndWidth(tools::Long nRight, tools::Long nNew)
Definition: swrect.cxx:161
constexpr bool IsHeightEmpty() const
void SetTopAndHeight(tools::Long nTop, tools::Long nNew)
Definition: swrect.cxx:156
void AddTop(const tools::Long nAdd)
Definition: swrect.cxx:128
SwRect & Intersection_(const SwRect &rRect)
Definition: swrect.cxx:81
tools::Long Right_() const
Definition: swrect.cxx:119
constexpr tools::Long Width() const
tools::Long Bottom() const
Definition: swrect.hxx:249
bool OverStepRight(tools::Long) const
Definition: swrect.cxx:148
SwRect & Intersection(const SwRect &rRect)
Definition: swrect.cxx:57
void Width(tools::Long nNew)
Definition: swrect.hxx:189
constexpr tools::Long getHeight() const
bool IsEmpty() const
Definition: swrect.hxx:304
bool OverStepLeft(tools::Long) const
Definition: swrect.cxx:142
struct _xmlTextWriter * xmlTextWriterPtr
void SetLeftAndWidth(tools::Long nLeft, tools::Long nNew)
Definition: swrect.cxx:151
def right
const Size & SSize() const
Definition: swrect.hxx:225
void AddBottom(const tools::Long nAdd)
Definition: swrect.cxx:130
tools::Long Height_() const
Definition: swrect.cxx:117
constexpr tools::Long Right() const
void AddHeight(const tools::Long nAdd)
Definition: swrect.cxx:124
constexpr tools::Long Top() const
void AddWidth(const tools::Long nAdd)
Definition: swrect.cxx:123
tools::Long Left_() const
Definition: swrect.cxx:118
void AddRight(const tools::Long nAdd)
Definition: swrect.cxx:127
void SetPosX(const tools::Long nNew)
Definition: swrect.cxx:131
void SetUpperLeftCorner(const Point &rNew)
Definition: swrect.cxx:171
constexpr tools::Long Bottom() const
tools::Long GetTopDistance(tools::Long) const
Definition: swrect.cxx:139
void AddLeft(const tools::Long nAdd)
Definition: swrect.cxx:125
void Bottom(const tools::Long nBottom)
Definition: swrect.hxx:211
void SubTop(const tools::Long nSub)
Definition: swrect.cxx:129
void SetUpperRightCorner(const Point &rNew)
Definition: swrect.cxx:173
tools::Long Left() const
Definition: swrect.hxx:237
void Justify()
Definition: swrect.cxx:94
tools::Long Width_() const
Definition: swrect.cxx:116
tools::Long GetBottomDistance(tools::Long) const
Definition: swrect.cxx:138
Point m_Point
Definition: swrect.hxx:36
constexpr tools::Long Height() const
tools::Long Top() const
Definition: swrect.hxx:245
void SetLowerLeftCorner(const Point &rNew)
Definition: swrect.cxx:175
bool OverStepTop(tools::Long) const
Definition: swrect.cxx:146
tools::Long AdjustWidth(tools::Long n)
void Top(const tools::Long nTop)
Definition: swrect.hxx:206
tools::Long Bottom_() const
Definition: swrect.cxx:121
SvStream & WriteChar(char nChar)
tools::Long AdjustHeight(tools::Long n)
void SetPosY(const tools::Long nNew)
Definition: swrect.cxx:132
Size SwappedSize() const
Definition: swrect.cxx:135
void setHeight(tools::Long nHeight)
tools::Long Top_() const
Definition: swrect.cxx:120
void SetBottomAndHeight(tools::Long nBottom, tools::Long nNew)
Definition: swrect.cxx:166
constexpr tools::Long getWidth() const
Left
tools::Long GetLeftDistance(tools::Long) const
Definition: swrect.cxx:137
tools::Long Width() const
Definition: swrect.hxx:229
void Height(tools::Long nNew)
Definition: swrect.hxx:193
bool Overlaps(const SwRect &rRect) const
Definition: swrect.hxx:374
constexpr bool IsWidthEmpty() const
SwRect()
Definition: swrect.hxx:317
void dumpAsXmlAttributes(xmlTextWriterPtr writer) const
Definition: swrect.cxx:178
SvStream & WriteSwRect(SvStream &rStream, const SwRect &rRect)
Definition: swrect.cxx:189
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo