xref: /AOO41X/main/tools/inc/tools/gen.hxx (revision 8b851043d896eaadc6634f0a22437412985b1d4a)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _SV_GEN_HXX
24 #define _SV_GEN_HXX
25 
26 #include "tools/toolsdllapi.h"
27 #include <tools/solar.h>
28 
29 #include <limits.h>
30 
31 class SvStream;
32 
33 // --------------------
34 // - Helper functions -
35 // --------------------
36 
37 inline long MinMax( long nVal, long nMin, long nMax );
38 inline long AlignedWidth4Bytes( long nWidthBits );
39 inline long FRound( double fVal );
40 
41 // ---------------
42 // - Inlines -
43 // ---------------
44 
MinMax(long nVal,long nMin,long nMax)45 inline long MinMax( long nVal, long nMin, long nMax )
46 {
47     return( nVal >= nMin ? ( nVal <= nMax ? nVal : nMax ) : nMin );
48 }
49 
50 // ------------------------------------------------------------------
51 
AlignedWidth4Bytes(long nWidthBits)52 inline long AlignedWidth4Bytes( long nWidthBits )
53 {
54     return( ( ( nWidthBits + 31 ) >> 5 ) << 2 );
55 }
56 
57 // ------------------------------------------------------------------
58 
FRound(double fVal)59 inline long FRound( double fVal )
60 {
61     return( fVal > 0.0 ? (long) ( fVal + 0.5 ) : -(long) ( -fVal + 0.5 ) );
62 }
63 
64 // --------
65 // - Pair -
66 // --------
67 
68 class Pair
69 {
70 public:
71     long                nA;
72     long                nB;
73 
74                         Pair();
75                         Pair( long nA, long nB );
76 
A() const77     long                A() const { return nA; }
B() const78     long                B() const { return nB; }
79 
A()80     long&               A() { return nA; }
B()81     long&               B() { return nB; }
82 
83     sal_Bool                operator == ( const Pair& rPair ) const;
84     sal_Bool                operator != ( const Pair& rPair ) const;
85 
86     TOOLS_DLLPUBLIC friend SvStream&    operator>>( SvStream& rIStream, Pair& rPair );
87     TOOLS_DLLPUBLIC friend SvStream&    operator<<( SvStream& rOStream, const Pair& rPair );
88 };
89 
Pair()90 inline Pair::Pair()
91 {
92     nA = nB = 0;
93 }
94 
Pair(long _nA,long _nB)95 inline Pair::Pair( long _nA, long _nB )
96 {
97     Pair::nA = _nA;
98     Pair::nB = _nB;
99 }
100 
operator ==(const Pair & rPair) const101 inline sal_Bool Pair::operator == ( const Pair& rPair ) const
102 {
103     return ((nA == rPair.nA) && (nB == rPair.nB));
104 }
105 
operator !=(const Pair & rPair) const106 inline sal_Bool Pair::operator != ( const Pair& rPair ) const
107 {
108     return ((nA != rPair.nA) || (nB != rPair.nB));
109 }
110 
111 // ---------
112 // - Point -
113 // ---------
114 
115 class Point : public Pair
116 {
117 public:
118                         Point();
119                         Point( long nX, long nY );
120 
X() const121     long                X() const { return nA; }
Y() const122     long                Y() const { return nB; }
123 
X()124     long&               X() { return nA; }
Y()125     long&               Y() { return nB; }
126 
127     void                Move( long nHorzMove, long nVertMove );
128     sal_Bool                IsAbove( const Point& rPoint ) const;
129     sal_Bool                IsBelow( const Point& rPoint ) const;
130     sal_Bool                IsLeft( const Point& rPoint ) const;
131     sal_Bool                IsRight( const Point& rPoint ) const;
132 
133     Point&              operator += ( const Point& rPoint );
134     Point&              operator -= ( const Point& rPoint );
135     Point&              operator *= ( const long nVal );
136     Point&              operator /= ( const long nVal );
137 
138 #ifdef __BORLANDC__
139     friend Point        operator+( const Point &rVal1, const Point &rVal2 );
140     friend Point        operator-( const Point &rVal1, const Point &rVal2 );
141     friend Point        operator*( const Point &rVal1, const long nVal2 );
142     friend Point        operator/( const Point &rVal1, const long nVal2 );
143 #else
144     friend inline Point operator+( const Point &rVal1, const Point &rVal2 );
145     friend inline Point operator-( const Point &rVal1, const Point &rVal2 );
146     friend inline Point operator*( const Point &rVal1, const long nVal2 );
147     friend inline Point operator/( const Point &rVal1, const long nVal2 );
148 #endif
149 
getX() const150     long                getX() const { return X(); }
getY() const151     long                getY() const { return Y(); }
setX(long nX)152     void                setX(long nX)  { X() = nX; }
setY(long nY)153     void                setY(long nY)  { Y() = nY; }
154 };
155 
Point()156 inline Point::Point()
157 {
158 }
159 
Point(long nX,long nY)160 inline Point::Point( long nX, long nY ) : Pair( nX, nY )
161 {
162 }
163 
Move(long nHorzMove,long nVertMove)164 inline void Point::Move( long nHorzMove, long nVertMove )
165 {
166     nA += nHorzMove;
167     nB += nVertMove;
168 }
169 
IsAbove(const Point & rPoint) const170 inline sal_Bool Point::IsAbove( const Point& rPoint ) const
171 {
172     return (nB > rPoint.nB);
173 }
174 
IsBelow(const Point & rPoint) const175 inline sal_Bool Point::IsBelow( const Point& rPoint ) const
176 {
177     return (nB < rPoint.nB);
178 }
179 
IsLeft(const Point & rPoint) const180 inline sal_Bool Point::IsLeft( const Point& rPoint ) const
181 {
182     return (nA < rPoint.nA);
183 }
184 
IsRight(const Point & rPoint) const185 inline sal_Bool Point::IsRight( const Point& rPoint ) const
186 {
187     return (nA > rPoint.nA);
188 }
189 
operator +=(const Point & rPoint)190 inline Point& Point::operator += ( const Point& rPoint )
191 {
192     nA += rPoint.nA;
193     nB += rPoint.nB;
194     return *this;
195 }
196 
operator -=(const Point & rPoint)197 inline Point& Point::operator -= ( const Point& rPoint )
198 {
199     nA -= rPoint.nA;
200     nB -= rPoint.nB;
201     return *this;
202 }
203 
operator *=(const long nVal)204 inline Point& Point::operator *= ( const long nVal )
205 {
206     nA *= nVal;
207     nB *= nVal;
208     return *this;
209 }
210 
operator /=(const long nVal)211 inline Point& Point::operator /= ( const long nVal )
212 {
213     nA /= nVal;
214     nB /= nVal;
215     return *this;
216 }
217 
operator +(const Point & rVal1,const Point & rVal2)218 inline Point operator+( const Point &rVal1, const Point &rVal2 )
219 {
220     return Point( rVal1.nA+rVal2.nA, rVal1.nB+rVal2.nB );
221 }
222 
operator -(const Point & rVal1,const Point & rVal2)223 inline Point operator-( const Point &rVal1, const Point &rVal2 )
224 {
225     return Point( rVal1.nA-rVal2.nA, rVal1.nB-rVal2.nB );
226 }
227 
operator *(const Point & rVal1,const long nVal2)228 inline Point operator*( const Point &rVal1, const long nVal2 )
229 {
230     return Point( rVal1.nA*nVal2, rVal1.nB*nVal2 );
231 }
232 
operator /(const Point & rVal1,const long nVal2)233 inline Point operator/( const Point &rVal1, const long nVal2 )
234 {
235     return Point( rVal1.nA/nVal2, rVal1.nB/nVal2 );
236 }
237 
238 // --------
239 // - Size -
240 // --------
241 
242 class Size : public Pair
243 {
244 public:
245                     Size();
246                     Size( long nWidth, long nHeight );
247 
Width() const248     long            Width() const  { return nA; }
Height() const249     long            Height() const { return nB; }
250 
Width()251     long&           Width()  { return nA; }
Height()252     long&           Height() { return nB; }
253 
getWidth() const254     long                getWidth() const { return Width(); }
getHeight() const255     long                getHeight() const { return Height(); }
setWidth(long nWidth)256     void                setWidth(long nWidth)  { Width() = nWidth; }
setHeight(long nHeight)257     void                setHeight(long nHeight)  { Height() = nHeight; }
258 };
259 
Size()260 inline Size::Size()
261 {
262 }
263 
Size(long nWidth,long nHeight)264 inline Size::Size( long nWidth, long nHeight ) :
265                 Pair( nWidth, nHeight )
266 {
267 }
268 
269 // ---------
270 // - Range -
271 // ---------
272 
273 #define RANGE_MIN   LONG_MIN
274 #define RANGE_MAX   LONG_MAX
275 
276 class Range : public Pair
277 {
278 public:
279                     Range();
280                     Range( long nMin, long nMax );
281 
Min() const282     long            Min() const { return nA; }
Max() const283     long            Max() const { return nB; }
Len() const284     long            Len() const { return nB - nA + 1; }
285 
Min()286     long&           Min() { return nA; }
Max()287     long&           Max() { return nB; }
288 
289     sal_Bool            IsInside( long nIs ) const;
290 
291     void            Justify();
292 };
293 
Range()294 inline Range::Range()
295 {
296 }
297 
Range(long nMin,long nMax)298 inline Range::Range( long nMin, long nMax ) : Pair( nMin, nMax )
299 {
300 }
301 
IsInside(long nIs) const302 inline sal_Bool Range::IsInside( long nIs ) const
303 {
304     return ((nA <= nIs) && (nIs <= nB ));
305 }
306 
Justify()307 inline void Range::Justify()
308 {
309     if ( nA > nB )
310     {
311         long nHelp = nA;
312         nA = nB;
313         nB = nHelp;
314     }
315 }
316 
317 // -------------
318 // - Selection -
319 // -------------
320 
321 #define SELECTION_MIN   LONG_MIN
322 #define SELECTION_MAX   LONG_MAX
323 
324 class Selection : public Pair
325 {
326 public:
327                     Selection();
328                     Selection( long nPos );
329                     Selection( long nMin, long nMax );
330 
Min() const331     long            Min() const { return nA; }
Max() const332     long            Max() const { return nB; }
Len() const333     long            Len() const { return nB - nA; }
334 
Min()335     long&           Min() { return nA; }
Max()336     long&           Max() { return nB; }
337 
338     sal_Bool            IsInside( long nIs ) const;
339 
340     void            Justify();
341 
operator !() const342     sal_Bool            operator !() const { return !Len(); }
343 
getMin() const344     long                getMin() const { return Min(); }
getMax() const345     long                getMax() const { return Max(); }
setMin(long nMin)346     void                setMin(long nMin)  { Min() = nMin; }
setMax(long nMax)347     void                setMax(long nMax)  { Max() = nMax; }
348 };
349 
Selection()350 inline Selection::Selection()
351 {
352 }
353 
Selection(long nPos)354 inline Selection::Selection( long nPos ) : Pair( nPos, nPos )
355 {
356 }
357 
Selection(long nMin,long nMax)358 inline Selection::Selection( long nMin, long nMax ) :
359            Pair( nMin, nMax )
360 {
361 }
362 
IsInside(long nIs) const363 inline sal_Bool Selection::IsInside( long nIs ) const
364 {
365     return ((nA <= nIs) && (nIs < nB ));
366 }
367 
Justify()368 inline void Selection::Justify()
369 {
370     if ( nA > nB )
371     {
372         long nHelp = nA;
373         nA = nB;
374         nB = nHelp;
375     }
376 }
377 
378 // -------------
379 // - Rectangle -
380 // -------------
381 
382 #define RECT_EMPTY  ((short)-32767)
383 
384 class TOOLS_DLLPUBLIC Rectangle
385 {
386 public:
387     long                nLeft;
388     long                nTop;
389     long                nRight;
390     long                nBottom;
391 
392                         Rectangle();
393                         Rectangle( const Point& rLT, const Point& rRB );
394                         Rectangle( long nLeft, long nTop,
395                                    long nRight, long nBottom );
396                         Rectangle( const Point& rLT, const Size& rSize );
397 
Left() const398     long                Left() const    { return nLeft;   }
Right() const399     long                Right() const   { return nRight;  }
Top() const400     long                Top() const     { return nTop;    }
Bottom() const401     long                Bottom() const  { return nBottom; }
402 
Left()403     long&               Left()          { return nLeft;   }
Right()404     long&               Right()         { return nRight;  }
Top()405     long&               Top()           { return nTop;    }
Bottom()406     long&               Bottom()        { return nBottom; }
407 
408     inline Point                TopLeft() const;
409     Point               TopRight() const;
410     Point               TopCenter() const;
411     Point               BottomLeft() const;
412     Point               BottomRight() const;
413     Point               BottomCenter() const;
414     Point               LeftCenter() const;
415     Point               RightCenter() const;
416     Point               Center() const;
417 
418     void                Move( long nHorzMove, long nVertMove );
419     inline void         Transpose();
420     inline void         SetPos( const Point& rPoint );
421     void                SetSize( const Size& rSize );
422     inline Size         GetSize() const;
423 
424     long                GetWidth() const;
425     long                GetHeight() const;
426 
427     Rectangle&          Union( const Rectangle& rRect );
428     Rectangle&          Intersection( const Rectangle& rRect );
429     Rectangle           GetUnion( const Rectangle& rRect ) const;
430     Rectangle           GetIntersection( const Rectangle& rRect ) const;
431 
432     void                Justify();
433 
434     sal_Bool                IsInside( const Point& rPOINT ) const;
435     sal_Bool                IsInside( const Rectangle& rRect ) const;
436     sal_Bool                IsOver( const Rectangle& rRect ) const;
437 
SetEmpty()438     void                SetEmpty() { nRight = nBottom = RECT_EMPTY; }
439     sal_Bool                IsEmpty() const;
440 
441     sal_Bool                operator == ( const Rectangle& rRect ) const;
442     sal_Bool                operator != ( const Rectangle& rRect ) const;
443 
444     Rectangle&          operator += ( const Point& rPt );
445     Rectangle&          operator -= ( const Point& rPt );
446 
447 #ifdef __BORLANDC__
448     friend Rectangle    operator + ( const Rectangle& rRect, const Point& rPt );
449     friend Rectangle    operator - ( const Rectangle& rRect, const Point& rPt );
450 #else
451     friend inline Rectangle operator + ( const Rectangle& rRect, const Point& rPt );
452     friend inline Rectangle operator - ( const Rectangle& rRect, const Point& rPt );
453 #endif
454 
455     TOOLS_DLLPUBLIC friend SvStream&    operator>>( SvStream& rIStream, Rectangle& rRect );
456     TOOLS_DLLPUBLIC friend SvStream&    operator<<( SvStream& rOStream, const Rectangle& rRect );
457 
458     // ONE
getX() const459     long                getX() const { return nLeft; }
getY() const460     long                getY() const { return nTop; }
getWidth() const461     long                getWidth() const { return nRight - nLeft; }
getHeight() const462     long                getHeight() const { return nBottom - nTop; }
setX(long n)463     void                setX( long n ) { nRight += n-nLeft; nLeft = n; }
setY(long n)464     void                setY( long n ) { nBottom += n-nTop; nTop = n; }
setWidth(long n)465     void                setWidth( long n ) { nRight = nLeft + n; }
setHeight(long n)466     void                setHeight( long n ) { nBottom = nTop + n; }
467 };
468 
Rectangle()469 inline Rectangle::Rectangle()
470 {
471     nLeft = nTop = 0;
472     nRight = nBottom = RECT_EMPTY;
473 }
474 
Rectangle(const Point & rLT,const Point & rRB)475 inline Rectangle::Rectangle( const Point& rLT, const Point& rRB )
476 {
477     nLeft   = rLT.X();
478     nTop    = rLT.Y();
479     nRight  = rRB.X();
480     nBottom = rRB.Y();
481 }
482 
Rectangle(long _nLeft,long _nTop,long _nRight,long _nBottom)483 inline Rectangle::Rectangle( long _nLeft,  long _nTop,
484                              long _nRight, long _nBottom )
485 {
486     nLeft   = _nLeft;
487     nTop    = _nTop;
488     nRight  = _nRight;
489     nBottom = _nBottom;
490 }
491 
Rectangle(const Point & rLT,const Size & rSize)492 inline Rectangle::Rectangle( const Point& rLT, const Size& rSize )
493 {
494     nLeft   = rLT.X();
495     nTop    = rLT.Y();
496     nRight  = rSize.Width()  ? nLeft+rSize.Width()-1 : RECT_EMPTY;
497     nBottom = rSize.Height() ? nTop+rSize.Height()-1 : RECT_EMPTY;
498 }
499 
IsEmpty() const500 inline sal_Bool Rectangle::IsEmpty() const
501 {
502     return ((nRight == RECT_EMPTY) || (nBottom == RECT_EMPTY));
503 }
504 
TopLeft() const505 inline Point Rectangle::TopLeft() const
506 {
507     return Point( nLeft, nTop );
508 }
509 
TopRight() const510 inline Point Rectangle::TopRight() const
511 {
512     return Point( (nRight == RECT_EMPTY) ? nLeft : nRight, nTop );
513 }
514 
BottomLeft() const515 inline Point Rectangle::BottomLeft() const
516 {
517     return Point( nLeft, (nBottom == RECT_EMPTY) ? nTop : nBottom );
518 }
519 
BottomRight() const520 inline Point Rectangle::BottomRight() const
521 {
522     return Point( (nRight  == RECT_EMPTY) ? nLeft : nRight,
523                   (nBottom == RECT_EMPTY) ? nTop  : nBottom );
524 }
525 
TopCenter() const526 inline Point Rectangle::TopCenter() const
527 {
528     if ( IsEmpty() )
529         return Point( nLeft, nTop );
530     else
531         return Point( Min( nLeft, nRight ) + Abs( (nRight - nLeft)/2 ),
532                       Min( nTop,  nBottom) );
533 }
534 
BottomCenter() const535 inline Point Rectangle::BottomCenter() const
536 {
537     if ( IsEmpty() )
538         return Point( nLeft, nTop );
539     else
540         return Point( Min( nLeft, nRight ) + Abs( (nRight - nLeft)/2 ),
541                       Max( nTop,  nBottom) );
542 }
543 
LeftCenter() const544 inline Point Rectangle::LeftCenter() const
545 {
546     if ( IsEmpty() )
547         return Point( nLeft, nTop );
548     else
549         return Point( Min( nLeft, nRight ), nTop + (nBottom - nTop)/2 );
550 }
551 
RightCenter() const552 inline Point Rectangle::RightCenter() const
553 {
554     if ( IsEmpty() )
555         return Point( nLeft, nTop );
556     else
557         return Point( Max( nLeft, nRight ), nTop + (nBottom - nTop)/2 );
558 }
559 
Center() const560 inline Point Rectangle::Center() const
561 {
562     if ( IsEmpty() )
563         return Point( nLeft, nTop );
564     else
565         return Point( nLeft+(nRight-nLeft)/2 , nTop+(nBottom-nTop)/2 );
566 }
567 
Move(long nHorzMove,long nVertMove)568 inline void Rectangle::Move( long nHorzMove, long nVertMove )
569 {
570     nLeft += nHorzMove;
571     nTop  += nVertMove;
572     if ( nRight != RECT_EMPTY )
573         nRight += nHorzMove;
574     if ( nBottom != RECT_EMPTY )
575         nBottom += nVertMove;
576 }
577 
Transpose()578 void Rectangle::Transpose()
579 {
580     if ( !IsEmpty() )
581     {
582         long swap( nLeft );
583         nLeft = nTop;
584         nTop = swap;
585 
586         swap = nRight;
587         nRight = nBottom;
588         nBottom = swap;
589     }
590 }
591 
SetPos(const Point & rPoint)592 inline void Rectangle::SetPos( const Point& rPoint )
593 {
594     if ( nRight != RECT_EMPTY )
595         nRight += rPoint.X() - nLeft;
596     if ( nBottom != RECT_EMPTY )
597         nBottom += rPoint.Y() - nTop;
598     nLeft = rPoint.X();
599     nTop  = rPoint.Y();
600 }
601 
GetWidth() const602 inline long Rectangle::GetWidth() const
603 {
604     long n;
605     if ( nRight == RECT_EMPTY )
606         n = 0;
607     else
608     {
609         n = nRight - nLeft;
610         if( n < 0 )
611             n--;
612         else
613             n++;
614     }
615 
616     return n;
617 }
618 
GetHeight() const619 inline long Rectangle::GetHeight() const
620 {
621     long n;
622     if ( nBottom == RECT_EMPTY )
623         n = 0;
624     else
625     {
626         n = nBottom - nTop;
627         if ( n < 0 )
628             n--;
629         else
630             n++;
631     }
632 
633     return n;
634 }
635 
GetSize() const636 inline Size Rectangle::GetSize() const
637 {
638     return Size( GetWidth(), GetHeight() );
639 }
640 
GetUnion(const Rectangle & rRect) const641 inline Rectangle Rectangle::GetUnion( const Rectangle& rRect ) const
642 {
643     Rectangle aTmpRect( *this );
644     return aTmpRect.Union( rRect );
645 }
646 
GetIntersection(const Rectangle & rRect) const647 inline Rectangle Rectangle::GetIntersection( const Rectangle& rRect ) const
648 {
649     Rectangle aTmpRect( *this );
650     return aTmpRect.Intersection( rRect );
651 }
652 
operator ==(const Rectangle & rRect) const653 inline sal_Bool Rectangle::operator == ( const Rectangle& rRect ) const
654 {
655     return ((nLeft   == rRect.nLeft   ) &&
656             (nTop    == rRect.nTop    ) &&
657             (nRight  == rRect.nRight  ) &&
658             (nBottom == rRect.nBottom ));
659 }
660 
operator !=(const Rectangle & rRect) const661 inline sal_Bool Rectangle::operator != ( const Rectangle& rRect ) const
662 {
663     return ((nLeft   != rRect.nLeft   ) ||
664             (nTop    != rRect.nTop    ) ||
665             (nRight  != rRect.nRight  ) ||
666             (nBottom != rRect.nBottom ));
667 }
668 
operator +=(const Point & rPt)669 inline Rectangle& Rectangle::operator +=( const Point& rPt )
670 {
671     nLeft += rPt.X();
672     nTop  += rPt.Y();
673     if ( nRight != RECT_EMPTY )
674         nRight += rPt.X();
675     if ( nBottom != RECT_EMPTY )
676         nBottom += rPt.Y();
677     return *this;
678 }
679 
operator -=(const Point & rPt)680 inline Rectangle& Rectangle::operator -= ( const Point& rPt )
681 {
682     nLeft -= rPt.X();
683     nTop  -= rPt.Y();
684     if ( nRight != RECT_EMPTY )
685         nRight -= rPt.X();
686     if ( nBottom != RECT_EMPTY )
687         nBottom -= rPt.Y();
688     return *this;
689 }
690 
operator +(const Rectangle & rRect,const Point & rPt)691 inline Rectangle operator + ( const Rectangle& rRect, const Point& rPt )
692 {
693     Rectangle aRect( rRect.nLeft  + rPt.X(), rRect.nTop    + rPt.Y(),
694                      (rRect.nRight  == RECT_EMPTY) ? RECT_EMPTY : rRect.nRight + rPt.X(),
695                      (rRect.nBottom == RECT_EMPTY) ? RECT_EMPTY : rRect.nBottom + rPt.Y() );
696     return aRect;
697 }
698 
operator -(const Rectangle & rRect,const Point & rPt)699 inline Rectangle operator - ( const Rectangle& rRect, const Point& rPt )
700 {
701     Rectangle aRect( rRect.nLeft - rPt.X(),
702                      rRect.nTop - rPt.Y(),
703                      (rRect.nRight  == RECT_EMPTY) ? RECT_EMPTY : rRect.nRight - rPt.X(),
704                      (rRect.nBottom == RECT_EMPTY) ? RECT_EMPTY : rRect.nBottom - rPt.Y() );
705     return aRect;
706 }
707 
708 #endif // _GEN_HXX
709