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
35SwRect& 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 {
100 }
101 if ( m_Size.getWidth() < 0 )
102 {
105 }
106}
107
108// Similar to the inline methods, but we need the function pointers
109void SwRect::Width_( const tools::Long nNew ) { m_Size.setWidth(nNew); }
110void SwRect::Height_( const tools::Long nNew ) { m_Size.setHeight(nNew); }
111void SwRect::Left_( const tools::Long nLeft ){ m_Size.AdjustWidth(m_Point.getX() - nLeft ); m_Point.setX(nLeft); }
112void SwRect::Right_( const tools::Long nRight ){ m_Size.setWidth(nRight - m_Point.getX()); }
113void SwRect::Top_( const tools::Long nTop ){ m_Size.AdjustHeight(m_Point.getY() - nTop ); m_Point.setY(nTop); }
114void SwRect::Bottom_( const tools::Long nBottom ){ m_Size.setHeight(nBottom - m_Point.getY()); }
115
122
123void SwRect::AddWidth( const tools::Long nAdd ) { m_Size.AdjustWidth(nAdd ); }
124void SwRect::AddHeight( const tools::Long nAdd ) { m_Size.AdjustHeight(nAdd ); }
125void SwRect::AddLeft( const tools::Long nAdd ){ m_Size.AdjustWidth(-nAdd ); m_Point.setX(m_Point.getX() + nAdd); }
126void SwRect::SubLeft( const tools::Long nSub ){ m_Size.AdjustWidth(nSub ); m_Point.setX(m_Point.getX() - nSub); }
127void SwRect::AddRight( const tools::Long nAdd ){ m_Size.AdjustWidth(nAdd ); }
128void SwRect::AddTop( const tools::Long nAdd ){ m_Size.AdjustHeight(-nAdd ); m_Point.setY(m_Point.getY() + nAdd); }
129void SwRect::SubTop( const tools::Long nSub ){ m_Size.AdjustHeight(nSub ); m_Point.setY(m_Point.getY() - nSub); }
131void SwRect::SetPosX( const tools::Long nNew ){ m_Point.setX(nNew); }
132void SwRect::SetPosY( const tools::Long nNew ){ m_Point.setY(nNew); }
133
134Size SwRect::Size_() const { return SSize(); }
136
137tools::Long SwRect::GetLeftDistance( tools::Long nLimit ) const { return m_Point.getX() - nLimit; }
139tools::Long SwRect::GetTopDistance( tools::Long nLimit ) const { return m_Point.getY() - nLimit; }
141
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; }
147 { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
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}
172 { m_Point = rNew; }
174 { m_Point = Point(rNew.X() - m_Size.getWidth(), rNew.Y()); }
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
189SvStream& 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 WriteOString("] ");
196 return rStream;
197}
198#endif
199
200static_assert( std::is_trivially_copyable< SwRect >::value );
201
202/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr tools::Long Y() const
void setX(tools::Long nX)
void setY(tools::Long nY)
constexpr tools::Long X() const
constexpr tools::Long getX() const
constexpr tools::Long getY() const
constexpr tools::Long getHeight() const
constexpr tools::Long Height() const
tools::Long AdjustHeight(tools::Long n)
constexpr tools::Long getWidth() const
void setWidth(tools::Long nWidth)
tools::Long AdjustWidth(tools::Long n)
void setHeight(tools::Long nHeight)
constexpr tools::Long Width() const
SvStream & WriteInt32(sal_Int32 nInt32)
SvStream & WriteChar(char nChar)
Of course Writer needs its own rectangles.
Definition: swrect.hxx:35
void SubLeft(const tools::Long nSub)
Definition: swrect.cxx:126
SwRect & Intersection(const SwRect &rRect)
Definition: swrect.cxx:57
tools::Long Bottom() const
Definition: swrect.hxx:249
tools::Long Right_() const
Definition: swrect.cxx:119
void SubTop(const tools::Long nSub)
Definition: swrect.cxx:129
void Height(tools::Long nNew)
Definition: swrect.hxx:193
tools::Long Right() const
Definition: swrect.hxx:241
Point m_Point
Definition: swrect.hxx:36
bool IsEmpty() const
Definition: swrect.hxx:304
tools::Long GetBottomDistance(tools::Long) const
Definition: swrect.cxx:138
void SetBottomAndHeight(tools::Long nBottom, tools::Long nNew)
Definition: swrect.cxx:166
SwRect & Union(const SwRect &rRect)
Definition: swrect.cxx:35
void dumpAsXmlAttributes(xmlTextWriterPtr writer) const
Definition: swrect.cxx:178
void Top(const tools::Long nTop)
Definition: swrect.hxx:206
tools::Long Height_() const
Definition: swrect.cxx:117
tools::Long Left_() const
Definition: swrect.cxx:118
void SetTopAndHeight(tools::Long nTop, tools::Long nNew)
Definition: swrect.cxx:156
void SetRightAndWidth(tools::Long nRight, tools::Long nNew)
Definition: swrect.cxx:161
tools::Long Width_() const
Definition: swrect.cxx:116
void Right(const tools::Long nRight)
Definition: swrect.hxx:202
void Bottom(const tools::Long nBottom)
Definition: swrect.hxx:211
tools::Long GetRightDistance(tools::Long) const
Definition: swrect.cxx:140
bool OverStepBottom(tools::Long) const
Definition: swrect.cxx:144
void SetPosX(const tools::Long nNew)
Definition: swrect.cxx:131
SwRect & Intersection_(const SwRect &rRect)
Definition: swrect.cxx:81
void SetLeftAndWidth(tools::Long nLeft, tools::Long nNew)
Definition: swrect.cxx:151
const Size & SSize() const
Definition: swrect.hxx:225
void AddLeft(const tools::Long nAdd)
Definition: swrect.cxx:125
bool OverStepLeft(tools::Long) const
Definition: swrect.cxx:142
void SetLowerLeftCorner(const Point &rNew)
Definition: swrect.cxx:175
tools::Long Top_() const
Definition: swrect.cxx:120
bool OverStepRight(tools::Long) const
Definition: swrect.cxx:148
void AddBottom(const tools::Long nAdd)
Definition: swrect.cxx:130
tools::Long GetTopDistance(tools::Long) const
Definition: swrect.cxx:139
tools::Long GetLeftDistance(tools::Long) const
Definition: swrect.cxx:137
void AddRight(const tools::Long nAdd)
Definition: swrect.cxx:127
void SetUpperRightCorner(const Point &rNew)
Definition: swrect.cxx:173
Size Size_() const
Definition: swrect.cxx:134
void AddHeight(const tools::Long nAdd)
Definition: swrect.cxx:124
bool Overlaps(const SwRect &rRect) const
Definition: swrect.hxx:374
void AddTop(const tools::Long nAdd)
Definition: swrect.cxx:128
bool OverStepTop(tools::Long) const
Definition: swrect.cxx:146
void AddWidth(const tools::Long nAdd)
Definition: swrect.cxx:123
void SetUpperLeftCorner(const Point &rNew)
Definition: swrect.cxx:171
tools::Long Left() const
Definition: swrect.hxx:237
tools::Long Bottom_() const
Definition: swrect.cxx:121
void Justify()
Definition: swrect.cxx:94
tools::Long Width() const
Definition: swrect.hxx:229
Size SwappedSize() const
Definition: swrect.cxx:135
void SetPosY(const tools::Long nNew)
Definition: swrect.cxx:132
Size m_Size
Definition: swrect.hxx:37
void Left(const tools::Long nLeft)
Definition: swrect.hxx:197
tools::Long Height() const
Definition: swrect.hxx:233
SwRect()
Definition: swrect.hxx:317
tools::Long Top() const
Definition: swrect.hxx:245
void Width(tools::Long nNew)
Definition: swrect.hxx:189
constexpr tools::Long Top() const
constexpr tools::Long Right() const
constexpr bool IsWidthEmpty() const
constexpr bool IsHeightEmpty() const
constexpr tools::Long Left() const
constexpr tools::Long Bottom() const
struct _xmlTextWriter * xmlTextWriterPtr
OString right
OString top
OString bottom
sal_Int64 n
long Long
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
Definition: nodeoffset.hxx:35
SvStream & WriteSwRect(SvStream &rStream, const SwRect &rRect)
Definition: swrect.cxx:189
Left
sal_uInt64 left