summaryrefslogtreecommitdiffstats
path: root/sw/source/core/bastyp/swrect.cxx
blob: 97e9683a59abc49d7e6726103e01dcf930ef6931 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <swrect.hxx>

#include <libxml/xmlwriter.h>

#ifdef DBG_UTIL
#include <tools/stream.hxx>
#endif

SwRect::SwRect( const tools::Rectangle &rRect ) :
    m_Point( rRect.Left(), rRect.Top() )
{
    m_Size.setWidth( rRect.IsWidthEmpty() ? 0 : rRect.Right()  - rRect.Left() + 1);
    m_Size.setHeight(rRect.IsHeightEmpty() ? 0 : rRect.Bottom() - rRect.Top()  + 1);
}

SwRect& SwRect::Union( const SwRect& rRect )
{
    if( rRect.IsEmpty())
        return *this;
    if( IsEmpty())
    {
        *this = rRect;
        return *this;
    }
    if ( Top() > rRect.Top() )
        Top( rRect.Top() );
    if ( Left() > rRect.Left() )
        Left( rRect.Left() );
    tools::Long n = rRect.Right();
    if ( Right() < n )
        Right( n );
    n = rRect.Bottom();
    if ( Bottom() < n )
        Bottom( n );
    return *this;
}

SwRect& SwRect::Intersection( const SwRect& rRect )
{
    // any similarity between me and given element?
    if ( Overlaps( rRect ) )
    {
        // get smaller right and lower, and greater left and upper edge
        if ( Left() < rRect.Left() )
            Left( rRect.Left() );
        if ( Top() < rRect.Top() )
            Top( rRect.Top() );
        tools::Long n = rRect.Right();
        if ( Right() > n )
            Right( n );
        n = rRect.Bottom();
        if ( Bottom() > n )
            Bottom( n );
    }
    else
        // Def.: if intersection is empty, set only SSize to 0
        SSize(0, 0);

    return *this;
}

SwRect& SwRect::Intersection_( const SwRect& rOther )
{
    // get smaller right and lower, and greater left and upper edge
    auto left   = std::max( m_Point.X(), rOther.m_Point.X() );
    auto top    = std::max( m_Point.Y(), rOther.m_Point.Y() );
    tools::Long right  = std::min( m_Point.X() + m_Size.Width(), rOther.m_Point.X() + rOther.m_Size.Width() );
    auto bottom = std::min( m_Point.Y() + m_Size.Height(), rOther.m_Point.Y() + rOther.m_Size.Height() );

    *this = SwRect( left, top, right - left, bottom - top );

    return *this;
}

void SwRect::Justify()
{
    if ( m_Size.getHeight() < 0 )
    {
        m_Point.setY(m_Point.getY() + m_Size.getHeight() + 1);
        m_Size.setHeight(-m_Size.getHeight());
    }
    if ( m_Size.getWidth() < 0 )
    {
        m_Point.setX(m_Point.getX() + m_Size.getWidth() + 1);
        m_Size.setWidth(-m_Size.getWidth());
    }
}

// Similar to the inline methods, but we need the function pointers
void SwRect::Width_( const tools::Long nNew ) { m_Size.setWidth(nNew); }
void SwRect::Height_( const tools::Long nNew ) { m_Size.setHeight(nNew); }
void SwRect::Left_( const tools::Long nLeft ){ m_Size.AdjustWidth(m_Point.getX() - nLeft ); m_Point.setX(nLeft); }
void SwRect::Right_( const tools::Long nRight ){ m_Size.setWidth(nRight - m_Point.getX()); }
void SwRect::Top_( const tools::Long nTop ){ m_Size.AdjustHeight(m_Point.getY() - nTop ); m_Point.setY(nTop); }
void SwRect::Bottom_( const tools::Long nBottom ){ m_Size.setHeight(nBottom - m_Point.getY()); }

tools::Long SwRect::Width_() const{ return m_Size.getWidth(); }
tools::Long SwRect::Height_() const{ return m_Size.getHeight(); }
tools::Long SwRect::Left_() const{ return m_Point.getX(); }
tools::Long SwRect::Right_() const{ return m_Point.getX() + m_Size.getWidth(); }
tools::Long SwRect::Top_() const{ return m_Point.getY(); }
tools::Long SwRect::Bottom_() const{ return m_Point.getY() + m_Size.getHeight(); }

void SwRect::AddWidth( const tools::Long nAdd ) { m_Size.AdjustWidth(nAdd ); }
void SwRect::AddHeight( const tools::Long nAdd ) { m_Size.AdjustHeight(nAdd ); }
void SwRect::AddLeft( const tools::Long nAdd ){ m_Size.AdjustWidth(-nAdd ); m_Point.setX(m_Point.getX() + nAdd); }
void SwRect::SubLeft( const tools::Long nSub ){ m_Size.AdjustWidth(nSub ); m_Point.setX(m_Point.getX() - nSub); }
void SwRect::AddRight( const tools::Long nAdd ){ m_Size.AdjustWidth(nAdd ); }
void SwRect::AddTop( const tools::Long nAdd ){ m_Size.AdjustHeight(-nAdd ); m_Point.setY(m_Point.getY() + nAdd); }
void SwRect::SubTop( const tools::Long nSub ){ m_Size.AdjustHeight(nSub ); m_Point.setY(m_Point.getY() - nSub); }
void SwRect::AddBottom( const tools::Long nAdd ){ m_Size.AdjustHeight(nAdd ); }
void SwRect::SetPosX( const tools::Long nNew ){ m_Point.setX(nNew); }
void SwRect::SetPosY( const tools::Long nNew ){ m_Point.setY(nNew); }

Size  SwRect::Size_() const { return SSize(); }
Size  SwRect::SwappedSize() const { return Size( m_Size.getHeight(), m_Size.getWidth() ); }

tools::Long SwRect::GetLeftDistance( tools::Long nLimit ) const { return m_Point.getX() - nLimit; }
tools::Long SwRect::GetBottomDistance( tools::Long nLim ) const { return nLim - m_Point.getY() - m_Size.getHeight();}
tools::Long SwRect::GetTopDistance( tools::Long nLimit ) const { return m_Point.getY() - nLimit; }
tools::Long SwRect::GetRightDistance( tools::Long nLim ) const { return nLim - m_Point.getX() - m_Size.getWidth(); }

bool SwRect::OverStepLeft( tools::Long nLimit ) const
    { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
bool SwRect::OverStepBottom( tools::Long nLimit ) const
    { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
bool SwRect::OverStepTop( tools::Long nLimit ) const
    { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
bool SwRect::OverStepRight( tools::Long nLimit ) const
    { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }

void SwRect::SetLeftAndWidth( tools::Long nLeft, tools::Long nNew )
{
    m_Point.setX(nLeft);
    m_Size.setWidth(nNew);
}
void SwRect::SetTopAndHeight( tools::Long nTop, tools::Long nNew )
{
    m_Point.setY(nTop);
    m_Size.setHeight(nNew);
}
void SwRect::SetRightAndWidth( tools::Long nRight, tools::Long nNew )
{
    m_Point.setX(nRight - nNew);
    m_Size.setWidth(nNew);
}
void SwRect::SetBottomAndHeight( tools::Long nBottom, tools::Long nNew )
{
    m_Point.setY(nBottom - nNew);
    m_Size.setHeight(nNew);
}
void SwRect::SetUpperLeftCorner(  const Point& rNew )
    { m_Point = rNew; }
void SwRect::SetUpperRightCorner(  const Point& rNew )
    { m_Point = Point(rNew.X() - m_Size.getWidth(), rNew.Y()); }
void SwRect::SetLowerLeftCorner(  const Point& rNew )
    { m_Point = Point(rNew.X(), rNew.Y() - m_Size.getHeight()); }

void SwRect::dumpAsXmlAttributes(xmlTextWriterPtr writer) const
{
    (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("left"), "%li", Left());
    (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("top"), "%li", Top());
    (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("width"), "%li", Width());
    (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("height"), "%li", Height());
    (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("bottom"), "%li", Bottom());
    (void)xmlTextWriterWriteFormatAttribute(writer, BAD_CAST("right"), "%li", Right());
}

#ifdef DBG_UTIL
SvStream& WriteSwRect(SvStream &rStream, const SwRect &rRect)
{
    rStream.WriteChar('[').WriteInt32(rRect.Top()).
            WriteChar('/').WriteInt32(rRect.Left()).
            WriteChar(',').WriteInt32(rRect.Width()).
            WriteChar('x').WriteInt32(rRect.Height()).
            WriteOString("] ");
    return rStream;
}
#endif

static_assert( std::is_trivially_copyable< SwRect >::value );

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */