Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sw/inc/swrect.hxx
Line
Count
Source
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
#pragma once
20
21
#include <ostream>
22
23
#include <sal/log.hxx>
24
#include <tools/gen.hxx>
25
#include "swdllapi.h"
26
27
class SvStream;
28
typedef struct _xmlTextWriter* xmlTextWriterPtr;
29
30
/// *Of course* Writer needs its own rectangles.
31
/// This is half-open so m_Point.X() + m_Size.getWidth() is *not* included.
32
/// Note the tools Rectangle is (usually? sometimes?) closed so there's a
33
/// SVRect() to subtract 1 for the conversion.
34
class SAL_WARN_UNUSED SwRect
35
{
36
    Point m_Point;
37
    Size m_Size;
38
39
public:
40
    inline SwRect();
41
    inline SwRect( const SwRect &rRect ) = default;
42
    inline SwRect( const Point& rLT, const Size&  rSize );
43
    inline SwRect( const Point& rLT, const Point& rRB );
44
    inline SwRect( tools::Long X, tools::Long Y, tools::Long Width, tools::Long Height );
45
46
    //SV-SS e.g. SwRect( pWin->GetClipRect() );
47
    explicit SwRect( const tools::Rectangle &rRect );
48
49
    //Set-Methods
50
    inline void Chg( const Point& rNP, const Size &rNS );
51
    inline void Pos(  const Point& rNew );
52
    inline void Pos( const tools::Long nNewX, const tools::Long nNewY );
53
    inline void SSize( const Size&  rNew  );
54
    inline void SSize( const tools::Long nHeight, const tools::Long nWidth );
55
    inline void Width(  tools::Long nNew );
56
    inline void Height( tools::Long nNew );
57
    inline void Left( const tools::Long nLeft );
58
    inline void Right( const tools::Long nRight );
59
    inline void Top( const tools::Long nTop );
60
    inline void Bottom( const tools::Long nBottom );
61
62
    //Get-Methods
63
    inline const Point &Pos()  const;
64
    inline const Size  &SSize() const;
65
    inline tools::Long Width()  const;
66
    inline tools::Long Height() const;
67
    inline tools::Long Left()   const;
68
    inline tools::Long Right()  const;
69
    inline tools::Long Top()    const;
70
    inline tools::Long Bottom() const;
71
72
    // In order to be able to access the members of Pos and SSize from the layout side.
73
    inline Point &Pos();
74
75
    Point Center() const;
76
77
    void Justify();
78
79
    SwRect &Union( const SwRect& rRect );
80
    SwRect &Intersection( const SwRect& rRect );
81
82
    SwRect GetUnion( const SwRect& rRect ) const;
83
    SwRect GetIntersection( const SwRect& rRect ) const;
84
85
    // Same as Intersection, only assume that Rects are overlapping!
86
    SwRect &Intersection_( const SwRect &rRect );
87
88
    bool Contains( const Point& rPOINT ) const;
89
    bool Contains( const SwRect& rRect ) const;
90
    bool Overlaps( const SwRect& rRect ) const;
91
    bool IsNear(const Point& rPoint, tools::Long nTolerance ) const;
92
    inline bool HasArea() const;
93
    inline bool IsEmpty() const;
94
    inline void Clear();
95
96
    SwRect &operator = ( const SwRect &rRect ) = default;
97
98
    inline bool operator == ( const SwRect& rRect ) const;
99
    inline bool operator != ( const SwRect& rRect ) const;
100
101
    inline SwRect &operator+=( const Point &rPt );
102
    inline SwRect &operator-=( const Point &rPt );
103
104
    //SV-SS e.g. pWin->DrawRect( aSwRect.SVRect() );
105
    inline tools::Rectangle  SVRect() const;
106
107
    // Output operator for debugging.
108
    friend SvStream& WriteSwRect( SvStream &rStream, const SwRect &rRect );
109
    void dumpAsXmlAttributes(xmlTextWriterPtr writer) const;
110
111
    void Top_(      const tools::Long nTop );
112
    void Bottom_(   const tools::Long nBottom );
113
    void Left_(     const tools::Long nLeft );
114
    void Right_(    const tools::Long nRight );
115
    void Width_(    const tools::Long nNew );
116
    void Height_(   const tools::Long nNew );
117
    tools::Long Top_()     const;
118
    tools::Long Bottom_()  const;
119
    tools::Long Left_()    const;
120
    tools::Long Right_()   const;
121
    tools::Long Width_()   const;
122
    tools::Long Height_()  const;
123
    void SubTop(    const tools::Long nSub );
124
    void AddTop(    const tools::Long nAdd );
125
    void AddBottom( const tools::Long nAdd );
126
    void AddLeft(   const tools::Long nAdd );
127
    void SubLeft(   const tools::Long nSub );
128
    void AddRight(  const tools::Long nAdd );
129
    void AddWidth(  const tools::Long nAdd );
130
    void AddHeight( const tools::Long nAdd );
131
    void SetPosX(   const tools::Long nNew );
132
    void SetPosY(   const tools::Long nNew );
133
    void SetLeftAndWidth( tools::Long nLeft, tools::Long nNew );
134
    void SetTopAndHeight( tools::Long nTop, tools::Long nNew );
135
    void SetRightAndWidth( tools::Long nRight, tools::Long nNew );
136
    void SetBottomAndHeight( tools::Long nBottom, tools::Long nNew );
137
    void SetUpperLeftCorner(  const Point& rNew );
138
    void SetUpperRightCorner(  const Point& rNew );
139
    void SetLowerLeftCorner(  const Point& rNew );
140
    Size  Size_() const;
141
    Point TopLeft()  const;
142
    Point TopRight()  const;
143
    Point BottomLeft()  const;
144
    Point BottomRight()  const;
145
    Size  SwappedSize() const;
146
    tools::Long GetLeftDistance( tools::Long ) const;
147
    tools::Long GetBottomDistance( tools::Long ) const;
148
    tools::Long GetRightDistance( tools::Long ) const;
149
    tools::Long GetTopDistance( tools::Long ) const;
150
    bool OverStepLeft( tools::Long ) const;
151
    bool OverStepBottom( tools::Long ) const;
152
    bool OverStepTop( tools::Long ) const;
153
    bool OverStepRight( tools::Long ) const;
154
};
155
156
typedef void (SwRect::*SwRectSet)( const tools::Long nNew );
157
typedef tools::Long (SwRect::*SwRectGet)() const;
158
typedef Point (SwRect::*SwRectPoint)() const;
159
typedef Size (SwRect::*SwRectSize)() const;
160
typedef bool (SwRect::*SwRectMax)( tools::Long ) const;
161
typedef tools::Long (SwRect::*SwRectDist)( tools::Long ) const;
162
typedef void (SwRect::*SwRectSetTwice)( tools::Long, tools::Long );
163
typedef void (SwRect::*SwRectSetPos)( const Point& );
164
165
//  Set-Methods
166
inline void SwRect::Chg( const Point& rNP, const Size &rNS )
167
7.61M
{
168
7.61M
    m_Point = rNP;
169
7.61M
    m_Size = rNS;
170
7.61M
}
171
inline void SwRect::Pos(  const Point& rNew )
172
460k
{
173
460k
    m_Point = rNew;
174
460k
}
175
inline void SwRect::Pos( const tools::Long nNewX, const tools::Long nNewY )
176
1
{
177
1
    m_Point.setX(nNewX);
178
1
    m_Point.setY(nNewY);
179
1
}
180
inline void SwRect::SSize( const Size&  rNew  )
181
652k
{
182
652k
    m_Size = rNew;
183
652k
}
184
inline void SwRect::SSize( const tools::Long nNewHeight, const tools::Long nNewWidth )
185
52
{
186
52
    m_Size.setWidth(nNewWidth);
187
52
    m_Size.setHeight(nNewHeight);
188
52
}
189
inline void SwRect::Width(  tools::Long nNew )
190
512k
{
191
512k
    m_Size.setWidth(nNew);
192
512k
}
193
inline void SwRect::Height( tools::Long nNew )
194
330k
{
195
330k
    m_Size.setHeight(nNew);
196
330k
}
197
inline void SwRect::Left( const tools::Long nLeft )
198
7.86M
{
199
7.86M
    m_Size.AdjustWidth( m_Point.getX() - nLeft );
200
7.86M
    m_Point.setX(nLeft);
201
7.86M
}
202
inline void SwRect::Right( const tools::Long nRight )
203
7.51M
{
204
7.51M
    m_Size.setWidth(nRight - m_Point.getX() + 1);
205
7.51M
}
206
inline void SwRect::Top( const tools::Long nTop )
207
825k
{
208
825k
    m_Size.AdjustHeight( m_Point.getY() - nTop );
209
825k
    m_Point.setY(nTop);
210
825k
}
211
inline void SwRect::Bottom( const tools::Long nBottom )
212
1.16M
{
213
1.16M
    m_Size.setHeight(nBottom - m_Point.getY() + 1);
214
1.16M
}
215
216
// Get-Methods
217
inline const Point &SwRect::Pos()  const
218
9.03M
{
219
9.03M
    return m_Point;
220
9.03M
}
221
inline Point &SwRect::Pos()
222
4.14M
{
223
4.14M
    return m_Point;
224
4.14M
}
225
inline const Size  &SwRect::SSize() const
226
9.71M
{
227
9.71M
    return m_Size;
228
9.71M
}
229
inline tools::Long SwRect::Width()  const
230
55.4M
{
231
55.4M
    return m_Size.Width();
232
55.4M
}
233
inline tools::Long SwRect::Height() const
234
44.1M
{
235
44.1M
    return m_Size.Height();
236
44.1M
}
237
inline tools::Long SwRect::Left()   const
238
69.3M
{
239
69.3M
    return m_Point.X();
240
69.3M
}
241
inline tools::Long SwRect::Right()  const
242
58.8M
{
243
58.8M
    return m_Size.getWidth() ? m_Point.getX() + m_Size.getWidth() - 1 : m_Point.getX();
244
58.8M
}
245
inline tools::Long SwRect::Top()    const
246
57.3M
{
247
57.3M
    return m_Point.Y();
248
57.3M
}
249
inline tools::Long SwRect::Bottom() const
250
50.7M
{
251
50.7M
    return m_Size.getHeight() ? m_Point.getY() + m_Size.getHeight() - 1 : m_Point.getY();
252
50.7M
}
253
254
inline Point SwRect::TopLeft() const
255
765k
{
256
765k
    return Point( Left(), Top());
257
765k
}
258
inline Point SwRect::TopRight() const
259
3
{
260
3
    return Point( Right(), Top());
261
3
}
262
inline Point SwRect::BottomLeft() const
263
3
{
264
3
    return Point( Left(), Bottom());
265
3
}
266
inline Point SwRect::BottomRight() const
267
3
{
268
3
    return Point( Right(), Bottom());
269
3
}
270
271
inline bool SwRect::operator == ( const SwRect& rRect ) const
272
446k
{
273
446k
    return (m_Point == rRect.m_Point && m_Size == rRect.m_Size);
274
446k
}
275
inline bool SwRect::operator != ( const SwRect& rRect ) const
276
3.32M
{
277
3.32M
    return (m_Point != rRect.m_Point || m_Size != rRect.m_Size);
278
3.32M
}
279
280
inline SwRect &SwRect::operator+=( const Point &rPt )
281
25.5k
{
282
25.5k
    m_Point += rPt;
283
25.5k
    return *this;
284
25.5k
}
285
inline SwRect &SwRect::operator-=( const Point &rPt )
286
0
{
287
0
    m_Point -= rPt;
288
0
    return *this;
289
0
}
290
291
// other
292
inline tools::Rectangle SwRect::SVRect() const
293
54.8M
{
294
54.8M
    SAL_INFO_IF( IsEmpty(), "sw.core", "SVRect() without Width or Height" );
295
54.8M
    return tools::Rectangle( m_Point.getX(), m_Point.getY(),
296
54.8M
        m_Point.getX() + m_Size.getWidth() - 1,         //Right()
297
54.8M
        m_Point.getY() + m_Size.getHeight() - 1 );      //Bottom()
298
54.8M
}
299
300
inline bool SwRect::HasArea() const
301
18.7M
{
302
18.7M
    return !IsEmpty();
303
18.7M
}
304
inline bool SwRect::IsEmpty() const
305
18.8M
{
306
18.8M
    return !(m_Size.getHeight() && m_Size.getWidth());
307
18.8M
}
308
inline void SwRect::Clear()
309
65.3k
{
310
65.3k
    m_Point.setX(0);
311
65.3k
    m_Point.setY(0);
312
65.3k
    m_Size.setWidth(0);
313
65.3k
    m_Size.setHeight(0);
314
65.3k
}
315
316
// constructors
317
inline SwRect::SwRect() :
318
20.4M
    m_Point( 0, 0 ),
319
20.4M
    m_Size( 0, 0 )
320
20.4M
{
321
20.4M
}
322
inline SwRect::SwRect( const Point& rLT, const Size&  rSize ) :
323
1.05M
    m_Point( rLT ),
324
1.05M
    m_Size( rSize )
325
1.05M
{
326
1.05M
}
327
inline SwRect::SwRect( const Point& rLT, const Point& rRB ) :
328
334k
    m_Point( rLT ),
329
334k
    m_Size( rRB.X() - rLT.X() + 1, rRB.Y() - rLT.Y() + 1 )
330
334k
{
331
334k
}
332
inline SwRect::SwRect( tools::Long X, tools::Long Y, tools::Long W, tools::Long H ) :
333
7.30M
    m_Point( X, Y ),
334
7.30M
    m_Size( W, H )
335
7.30M
{
336
7.30M
}
337
338
inline Point SwRect::Center() const
339
22.9k
{
340
22.9k
    return Point( Left() + Width()  / 2,
341
22.9k
                  Top()  + Height() / 2 );
342
22.9k
}
343
344
inline bool SwRect::Contains( const SwRect& rRect ) const
345
98.3k
{
346
98.3k
    const tools::Long nRight  = Right();
347
98.3k
    const tools::Long nBottom = Bottom();
348
98.3k
    const tools::Long nrRight = rRect.Right();
349
98.3k
    const tools::Long nrBottom= rRect.Bottom();
350
98.3k
    return (Left() <= rRect.Left()) && (rRect.Left()<= nRight)  &&
351
66.4k
           (Left() <= nrRight)      && (nrRight     <= nRight)  &&
352
66.4k
           (Top()  <= rRect.Top())  && (rRect.Top() <= nBottom) &&
353
66.4k
           (Top()  <= nrBottom)     && (nrBottom    <= nBottom);
354
98.3k
}
355
356
inline bool SwRect::Contains( const Point& rPoint ) const
357
356k
{
358
356k
    return (Left()  <= rPoint.X()) &&
359
315k
           (Top()   <= rPoint.Y()) &&
360
313k
           (Right() >= rPoint.X()) &&
361
312k
           (Bottom()>= rPoint.Y());
362
356k
}
363
364
// mouse moving of table borders
365
inline bool SwRect::IsNear( const Point& rPoint, tools::Long nTolerance ) const
366
0
{
367
0
    bool bIsNearby = (((Left()   - nTolerance) <= rPoint.X()) &&
368
0
                      ((Top()    - nTolerance) <= rPoint.Y()) &&
369
0
                      ((Right()  + nTolerance) >= rPoint.X()) &&
370
0
                      ((Bottom() + nTolerance) >= rPoint.Y()));
371
0
    return Contains(rPoint) || bIsNearby;
372
0
}
373
374
inline bool SwRect::Overlaps( const SwRect& rRect ) const
375
9.32M
{
376
9.32M
    return (Top()   <= rRect.Bottom()) &&
377
9.18M
           (Left()  <= rRect.Right())  &&
378
9.18M
           (Right() >= rRect.Left())   &&
379
7.95M
           (Bottom()>= rRect.Top());
380
9.32M
}
381
382
inline SwRect SwRect::GetUnion( const SwRect& rRect ) const
383
0
{
384
0
    return IsEmpty() ? rRect : rRect.IsEmpty() ? *this : SwRect(
385
0
        Point( std::min( Left(), rRect.Left()),
386
0
               std::min( Top(), rRect.Top())),
387
0
        Point( std::max( Right(), rRect.Right()),
388
0
               std::max( Bottom(), rRect.Bottom())));
389
0
}
390
391
inline SwRect SwRect::GetIntersection( const SwRect& rRect ) const
392
0
{
393
0
    return Overlaps( rRect )
394
0
        ? SwRect(
395
0
            Point( std::max( Left(), rRect.Left()),
396
0
                   std::max( Top(), rRect.Top())),
397
0
            Point( std::min( Right(), rRect.Right()),
398
0
                   std::min( Bottom(), rRect.Bottom())))
399
0
        : SwRect();
400
0
}
401
402
403
template< typename charT, typename traits >
404
inline std::basic_ostream<charT, traits> & operator <<(
405
    std::basic_ostream<charT, traits> & stream, const SwRect& rectangle )
406
{
407
    if (rectangle.IsEmpty())
408
        stream << "EMPTY:";
409
    return stream << rectangle.SSize()
410
                  << "@(" << rectangle.Pos() << ")";
411
}
412
413
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */