Coverage Report

Created: 2025-12-08 09:28

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
9.25M
{
168
9.25M
    m_Point = rNP;
169
9.25M
    m_Size = rNS;
170
9.25M
}
171
inline void SwRect::Pos(  const Point& rNew )
172
556k
{
173
556k
    m_Point = rNew;
174
556k
}
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
790k
{
182
790k
    m_Size = rNew;
183
790k
}
184
inline void SwRect::SSize( const tools::Long nNewHeight, const tools::Long nNewWidth )
185
55
{
186
55
    m_Size.setWidth(nNewWidth);
187
55
    m_Size.setHeight(nNewHeight);
188
55
}
189
inline void SwRect::Width(  tools::Long nNew )
190
621k
{
191
621k
    m_Size.setWidth(nNew);
192
621k
}
193
inline void SwRect::Height( tools::Long nNew )
194
400k
{
195
400k
    m_Size.setHeight(nNew);
196
400k
}
197
inline void SwRect::Left( const tools::Long nLeft )
198
9.55M
{
199
9.55M
    m_Size.AdjustWidth( m_Point.getX() - nLeft );
200
9.55M
    m_Point.setX(nLeft);
201
9.55M
}
202
inline void SwRect::Right( const tools::Long nRight )
203
9.12M
{
204
9.12M
    m_Size.setWidth(nRight - m_Point.getX() + 1);
205
9.12M
}
206
inline void SwRect::Top( const tools::Long nTop )
207
996k
{
208
996k
    m_Size.AdjustHeight( m_Point.getY() - nTop );
209
996k
    m_Point.setY(nTop);
210
996k
}
211
inline void SwRect::Bottom( const tools::Long nBottom )
212
1.41M
{
213
1.41M
    m_Size.setHeight(nBottom - m_Point.getY() + 1);
214
1.41M
}
215
216
// Get-Methods
217
inline const Point &SwRect::Pos()  const
218
10.9M
{
219
10.9M
    return m_Point;
220
10.9M
}
221
inline Point &SwRect::Pos()
222
5.16M
{
223
5.16M
    return m_Point;
224
5.16M
}
225
inline const Size  &SwRect::SSize() const
226
11.8M
{
227
11.8M
    return m_Size;
228
11.8M
}
229
inline tools::Long SwRect::Width()  const
230
67.4M
{
231
67.4M
    return m_Size.Width();
232
67.4M
}
233
inline tools::Long SwRect::Height() const
234
53.6M
{
235
53.6M
    return m_Size.Height();
236
53.6M
}
237
inline tools::Long SwRect::Left()   const
238
83.9M
{
239
83.9M
    return m_Point.X();
240
83.9M
}
241
inline tools::Long SwRect::Right()  const
242
71.3M
{
243
71.3M
    return m_Size.getWidth() ? m_Point.getX() + m_Size.getWidth() - 1 : m_Point.getX();
244
71.3M
}
245
inline tools::Long SwRect::Top()    const
246
69.5M
{
247
69.5M
    return m_Point.Y();
248
69.5M
}
249
inline tools::Long SwRect::Bottom() const
250
61.5M
{
251
61.5M
    return m_Size.getHeight() ? m_Point.getY() + m_Size.getHeight() - 1 : m_Point.getY();
252
61.5M
}
253
254
inline Point SwRect::TopLeft() const
255
920k
{
256
920k
    return Point( Left(), Top());
257
920k
}
258
inline Point SwRect::TopRight() const
259
4
{
260
4
    return Point( Right(), Top());
261
4
}
262
inline Point SwRect::BottomLeft() const
263
4
{
264
4
    return Point( Left(), Bottom());
265
4
}
266
inline Point SwRect::BottomRight() const
267
4
{
268
4
    return Point( Right(), Bottom());
269
4
}
270
271
inline bool SwRect::operator == ( const SwRect& rRect ) const
272
534k
{
273
534k
    return (m_Point == rRect.m_Point && m_Size == rRect.m_Size);
274
534k
}
275
inline bool SwRect::operator != ( const SwRect& rRect ) const
276
4.06M
{
277
4.06M
    return (m_Point != rRect.m_Point || m_Size != rRect.m_Size);
278
4.06M
}
279
280
inline SwRect &SwRect::operator+=( const Point &rPt )
281
30.6k
{
282
30.6k
    m_Point += rPt;
283
30.6k
    return *this;
284
30.6k
}
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
66.6M
{
294
66.6M
    SAL_INFO_IF( IsEmpty(), "sw.core", "SVRect() without Width or Height" );
295
66.6M
    return tools::Rectangle( m_Point.getX(), m_Point.getY(),
296
66.6M
        m_Point.getX() + m_Size.getWidth() - 1,         //Right()
297
66.6M
        m_Point.getY() + m_Size.getHeight() - 1 );      //Bottom()
298
66.6M
}
299
300
inline bool SwRect::HasArea() const
301
22.7M
{
302
22.7M
    return !IsEmpty();
303
22.7M
}
304
inline bool SwRect::IsEmpty() const
305
22.8M
{
306
22.8M
    return !(m_Size.getHeight() && m_Size.getWidth());
307
22.8M
}
308
inline void SwRect::Clear()
309
79.4k
{
310
79.4k
    m_Point.setX(0);
311
79.4k
    m_Point.setY(0);
312
79.4k
    m_Size.setWidth(0);
313
79.4k
    m_Size.setHeight(0);
314
79.4k
}
315
316
// constructors
317
inline SwRect::SwRect() :
318
24.7M
    m_Point( 0, 0 ),
319
24.7M
    m_Size( 0, 0 )
320
24.7M
{
321
24.7M
}
322
inline SwRect::SwRect( const Point& rLT, const Size&  rSize ) :
323
1.27M
    m_Point( rLT ),
324
1.27M
    m_Size( rSize )
325
1.27M
{
326
1.27M
}
327
inline SwRect::SwRect( const Point& rLT, const Point& rRB ) :
328
406k
    m_Point( rLT ),
329
406k
    m_Size( rRB.X() - rLT.X() + 1, rRB.Y() - rLT.Y() + 1 )
330
406k
{
331
406k
}
332
inline SwRect::SwRect( tools::Long X, tools::Long Y, tools::Long W, tools::Long H ) :
333
8.91M
    m_Point( X, Y ),
334
8.91M
    m_Size( W, H )
335
8.91M
{
336
8.91M
}
337
338
inline Point SwRect::Center() const
339
27.0k
{
340
27.0k
    return Point( Left() + Width()  / 2,
341
27.0k
                  Top()  + Height() / 2 );
342
27.0k
}
343
344
inline bool SwRect::Contains( const SwRect& rRect ) const
345
118k
{
346
118k
    const tools::Long nRight  = Right();
347
118k
    const tools::Long nBottom = Bottom();
348
118k
    const tools::Long nrRight = rRect.Right();
349
118k
    const tools::Long nrBottom= rRect.Bottom();
350
118k
    return (Left() <= rRect.Left()) && (rRect.Left()<= nRight)  &&
351
80.7k
           (Left() <= nrRight)      && (nrRight     <= nRight)  &&
352
80.7k
           (Top()  <= rRect.Top())  && (rRect.Top() <= nBottom) &&
353
80.7k
           (Top()  <= nrBottom)     && (nrBottom    <= nBottom);
354
118k
}
355
356
inline bool SwRect::Contains( const Point& rPoint ) const
357
429k
{
358
429k
    return (Left()  <= rPoint.X()) &&
359
382k
           (Top()   <= rPoint.Y()) &&
360
379k
           (Right() >= rPoint.X()) &&
361
378k
           (Bottom()>= rPoint.Y());
362
429k
}
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
11.2M
{
376
11.2M
    return (Top()   <= rRect.Bottom()) &&
377
11.0M
           (Left()  <= rRect.Right())  &&
378
11.0M
           (Right() >= rRect.Left())   &&
379
9.65M
           (Bottom()>= rRect.Top());
380
11.2M
}
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: */