fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / tools / source / generic / gen.cxx
blobcb5b1dc2a674f01bfa70a477d47d18ff2887f77c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <sstream>
24 #include <tools/debug.hxx>
25 #include <tools/gen.hxx>
26 #include <tools/stream.hxx>
28 SvStream& ReadPair( SvStream& rIStream, Pair& rPair )
30 DBG_ASSERTWARNING( rIStream.GetVersion(), "Pair::>> - Solar-Version not set on rIStream" );
32 sal_Int32 nTmpA(0), nTmpB(0);
33 rIStream.ReadInt32( nTmpA ).ReadInt32( nTmpB );
34 rPair.nA = nTmpA;
35 rPair.nB = nTmpB;
37 return rIStream;
40 SvStream& WritePair( SvStream& rOStream, const Pair& rPair )
42 DBG_ASSERTWARNING( rOStream.GetVersion(), "Pair::<< - Solar-Version not set on rOStream" );
44 rOStream.WriteInt32( rPair.nA ).WriteInt32( rPair.nB );
46 return rOStream;
49 void Rectangle::SetSize( const Size& rSize )
51 if ( rSize.Width() < 0 )
52 nRight = nLeft + rSize.Width() +1;
53 else if ( rSize.Width() > 0 )
54 nRight = nLeft + rSize.Width() -1;
55 else
56 nRight = RECT_EMPTY;
58 if ( rSize.Height() < 0 )
59 nBottom = nTop + rSize.Height() +1;
60 else if ( rSize.Height() > 0 )
61 nBottom = nTop + rSize.Height() -1;
62 else
63 nBottom = RECT_EMPTY;
66 Rectangle& Rectangle::Union( const Rectangle& rRect )
68 if ( rRect.IsEmpty() )
69 return *this;
71 if ( IsEmpty() )
72 *this = rRect;
73 else
75 nLeft = std::min( std::min( nLeft, rRect.nLeft ), std::min( nRight, rRect.nRight ) );
76 nRight = std::max( std::max( nLeft, rRect.nLeft ), std::max( nRight, rRect.nRight ) );
77 nTop = std::min( std::min( nTop, rRect.nTop ), std::min( nBottom, rRect.nBottom ) );
78 nBottom = std::max( std::max( nTop, rRect.nTop ), std::max( nBottom, rRect.nBottom ) );
81 return *this;
84 Rectangle& Rectangle::Intersection( const Rectangle& rRect )
86 if ( IsEmpty() )
87 return *this;
88 if ( rRect.IsEmpty() )
90 *this = Rectangle();
91 return *this;
94 // Justify rectangle
95 Rectangle aTmpRect( rRect );
96 Justify();
97 aTmpRect.Justify();
99 // Perform intersection
100 nLeft = std::max( nLeft, aTmpRect.nLeft );
101 nRight = std::min( nRight, aTmpRect.nRight );
102 nTop = std::max( nTop, aTmpRect.nTop );
103 nBottom= std::min( nBottom, aTmpRect.nBottom );
105 // Determine if intersection is empty
106 if ( nRight < nLeft || nBottom < nTop )
107 *this = Rectangle();
109 return *this;
112 void Rectangle::Justify()
114 long nHelp;
116 if ( (nRight < nLeft) && (nRight != RECT_EMPTY) )
118 nHelp = nLeft;
119 nLeft = nRight;
120 nRight = nHelp;
123 if ( (nBottom < nTop) && (nBottom != RECT_EMPTY) )
125 nHelp = nBottom;
126 nBottom = nTop;
127 nTop = nHelp;
131 bool Rectangle::IsInside( const Point& rPoint ) const
133 if ( IsEmpty() )
134 return false;
136 bool bRet = true;
137 if ( nLeft <= nRight )
139 if ( (rPoint.X() < nLeft) || (rPoint.X() > nRight) )
140 bRet = false;
142 else
144 if ( (rPoint.X() > nLeft) || (rPoint.X() < nRight) )
145 bRet = false;
147 if ( nTop <= nBottom )
149 if ( (rPoint.Y() < nTop) || (rPoint.Y() > nBottom) )
150 bRet = false;
152 else
154 if ( (rPoint.Y() > nTop) || (rPoint.Y() < nBottom) )
155 bRet = false;
157 return bRet;
160 bool Rectangle::IsInside( const Rectangle& rRect ) const
162 if ( IsInside( rRect.TopLeft() ) && IsInside( rRect.BottomRight() ) )
163 return true;
164 else
165 return false;
168 bool Rectangle::IsOver( const Rectangle& rRect ) const
170 // If there's no intersection, they don't overlap
171 return !GetIntersection( rRect ).IsEmpty();
174 SvStream& ReadRectangle( SvStream& rIStream, Rectangle& rRect )
176 DBG_ASSERTWARNING( rIStream.GetVersion(), "Rectangle::>> - Solar-Version not set on rIStream" );
178 sal_Int32 nTmpL(0), nTmpT(0), nTmpR(0), nTmpB(0);
180 rIStream.ReadInt32( nTmpL ).ReadInt32( nTmpT ).ReadInt32( nTmpR ).ReadInt32( nTmpB );
182 rRect.nLeft = nTmpL;
183 rRect.nTop = nTmpT;
184 rRect.nRight = nTmpR;
185 rRect.nBottom = nTmpB;
187 return rIStream;
190 SvStream& WriteRectangle( SvStream& rOStream, const Rectangle& rRect )
192 DBG_ASSERTWARNING( rOStream.GetVersion(), "Rectangle::<< - Solar-Version not set on rOStream" );
194 rOStream.WriteInt32( rRect.nLeft )
195 .WriteInt32( rRect.nTop )
196 .WriteInt32( rRect.nRight )
197 .WriteInt32( rRect.nBottom );
199 return rOStream;
202 OString Rectangle::toString() const
204 std::stringstream ss;
205 ss << getX() << ", " << getY() << ", " << getWidth() << ", " << getHeight();
206 return ss.str().c_str();
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */