xref: /trunk/main/tools/source/generic/gen.cxx (revision 89b56da7)
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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_tools.hxx"
26 #include <tools/debug.hxx>
27 #include <tools/gen.hxx>
28 #include <tools/stream.hxx>
29 
30 // =======================================================================
31 
operator >>(SvStream & rIStream,Pair & rPair)32 SvStream& operator>>( SvStream& rIStream, Pair& rPair )
33 {
34 	DBG_ASSERTWARNING( rIStream.GetVersion(), "Pair::>> - Solar-Version not set on rIStream" );
35 
36 	if ( rIStream.GetCompressMode() == COMPRESSMODE_FULL )
37 	{
38 		unsigned char	cId;
39 		unsigned char	cAry[8];
40 		int 			i;
41 		int 			i1;
42 		int 			i2;
43 		sal_uInt32			nNum;
44 
45 		rIStream >> cId;
46 		i1 = (cId & 0x70) >> 4;
47 		i2 = cId & 0x07;
48 		rIStream.Read( cAry, i1+i2 );
49 
50 		nNum = 0;
51 		i = i1;
52 		while ( i )
53 		{
54 			i--;
55 			nNum <<= 8;
56 			nNum |= cAry[i];
57 		}
58 		if ( cId & 0x80 )
59 			nNum ^= 0xFFFFFFFF;
60 		rPair.nA = (sal_Int32)nNum;
61 
62 		nNum = 0;
63 		i = i1+i2;
64 		while ( i > i1 )
65 		{
66 			i--;
67 			nNum <<= 8;
68 			nNum |= cAry[i];
69 		}
70 		if ( cId & 0x08 )
71 			nNum ^= 0xFFFFFFFF;
72 		rPair.nB = (sal_Int32)nNum;
73 	}
74 	else
75 	{
76 		rIStream >> rPair.nA >> rPair.nB;
77 	}
78 
79 	return rIStream;
80 }
81 
82 // -----------------------------------------------------------------------
83 
operator <<(SvStream & rOStream,const Pair & rPair)84 SvStream& operator<<( SvStream& rOStream, const Pair& rPair )
85 {
86 	DBG_ASSERTWARNING( rOStream.GetVersion(), "Pair::<< - Solar-Version not set on rOStream" );
87 
88 	if ( rOStream.GetCompressMode() == COMPRESSMODE_FULL )
89 	{
90 		unsigned char	cAry[9];
91 		int 			i = 1;
92 		sal_uInt32			nNum;
93 
94 		cAry[0] = 0;
95 
96 		nNum = (sal_uInt32)(sal_Int32)rPair.nA;
97 		if ( rPair.nA < 0 )
98 		{
99 			cAry[0] |= 0x80;
100 			nNum ^= 0xFFFFFFFF;
101 		}
102 		if ( nNum )
103 		{
104 			cAry[i] = (unsigned char)(nNum & 0xFF);
105 			nNum >>= 8;
106 			i++;
107 
108 			if ( nNum )
109 			{
110 				cAry[i] = (unsigned char)(nNum & 0xFF);
111 				nNum >>= 8;
112 				i++;
113 
114 				if ( nNum )
115 				{
116 					cAry[i] = (unsigned char)(nNum & 0xFF);
117 					nNum >>= 8;
118 					i++;
119 
120 					if ( nNum )
121 					{
122 						cAry[i] = (unsigned char)(nNum & 0xFF);
123 						nNum >>= 8;
124 						i++;
125 						cAry[0] |= 0x40;
126 					}
127 					else
128 						cAry[0] |= 0x30;
129 				}
130 				else
131 					cAry[0] |= 0x20;
132 			}
133 			else
134 				cAry[0] |= 0x10;
135 		}
136 
137 		nNum = (sal_uInt32)(sal_Int32)rPair.nB;
138 		if ( rPair.nB < 0 )
139 		{
140 			cAry[0] |= 0x08;
141 			nNum ^= 0xFFFFFFFF;
142 		}
143 		if ( nNum )
144 		{
145 			cAry[i] = (unsigned char)(nNum & 0xFF);
146 			nNum >>= 8;
147 			i++;
148 
149 			if ( nNum )
150 			{
151 				cAry[i] = (unsigned char)(nNum & 0xFF);
152 				nNum >>= 8;
153 				i++;
154 
155 				if ( nNum )
156 				{
157 					cAry[i] = (unsigned char)(nNum & 0xFF);
158 					nNum >>= 8;
159 					i++;
160 
161 					if ( nNum )
162 					{
163 						cAry[i] = (unsigned char)(nNum & 0xFF);
164 						nNum >>= 8;
165 						i++;
166 						cAry[0] |= 0x04;
167 					}
168 					else
169 						cAry[0] |= 0x03;
170 				}
171 				else
172 					cAry[0] |= 0x02;
173 			}
174 			else
175 				cAry[0] |= 0x01;
176 		}
177 
178 		rOStream.Write( cAry, i );
179 	}
180 	else
181 	{
182 		rOStream << rPair.nA << rPair.nB;
183 	}
184 
185 	return rOStream;
186 }
187 
188 /*************************************************************************
189 |*
190 |*    Rectangle::SetSize()
191 |*
192 |*    Beschreibung      GEN.SDW
193 |*    Ersterstellung    DV 29.10.91
194 |*    Letzte Aenderung  MM 21.04.94
195 |*
196 *************************************************************************/
197 
SetSize(const Size & rSize)198 void Rectangle::SetSize( const Size& rSize )
199 {
200     if ( rSize.Width() < 0 )
201         nRight  = nLeft + rSize.Width() +1;
202     else if ( rSize.Width() > 0 )
203         nRight  = nLeft + rSize.Width() -1;
204     else
205         nRight = RECT_EMPTY;
206 
207     if ( rSize.Height() < 0 )
208         nBottom  = nTop + rSize.Height() +1;
209     else if ( rSize.Height() > 0 )
210         nBottom  = nTop + rSize.Height() -1;
211     else
212         nBottom = RECT_EMPTY;
213 }
214 
215 /*************************************************************************
216 |*
217 |*    Rectangle::Union()
218 |*
219 |*    Beschreibung      GEN.SDW
220 |*    Ersterstellung    TH 20.10.92
221 |*    Letzte Aenderung  MM 21.04.94
222 |*
223 *************************************************************************/
224 
Union(const Rectangle & rRect)225 Rectangle& Rectangle::Union( const Rectangle& rRect )
226 {
227     if ( rRect.IsEmpty() )
228         return *this;
229 
230     if ( IsEmpty() )
231         *this = rRect;
232     else
233     {
234         nLeft  =  Min( Min( nLeft, rRect.nLeft ), Min( nRight, rRect.nRight )   );
235         nRight  = Max( Max( nLeft, rRect.nLeft ), Max( nRight, rRect.nRight )   );
236         nTop    = Min( Min( nTop, rRect.nTop ),   Min( nBottom, rRect.nBottom ) );
237         nBottom = Max( Max( nTop, rRect.nTop ),   Max( nBottom, rRect.nBottom ) );
238     }
239 
240     return *this;
241 }
242 
243 /*************************************************************************
244 |*
245 |*    Rectangle::Intersection()
246 |*
247 |*    Beschreibung      GEN.SDW
248 |*    Ersterstellung    TH 20.10.92
249 |*    Letzte Aenderung  MM 21.04.94
250 |*
251 *************************************************************************/
252 
Intersection(const Rectangle & rRect)253 Rectangle& Rectangle::Intersection( const Rectangle& rRect )
254 {
255     if ( IsEmpty() )
256         return *this;
257     if ( rRect.IsEmpty() )
258     {
259         *this = Rectangle();
260         return *this;
261     }
262 
263     // nicht mit umgedrehten Rechtecken arbeiten
264     Rectangle aTmpRect( rRect );
265     Justify();
266     aTmpRect.Justify();
267 
268     // Schnitt bilden
269     nLeft  = Max( nLeft, aTmpRect.nLeft );
270     nRight = Min( nRight, aTmpRect.nRight );
271     nTop   = Max( nTop, aTmpRect.nTop );
272     nBottom= Min( nBottom, aTmpRect.nBottom );
273 
274     // Feststellen ob Schnitt leer
275     if ( nRight < nLeft || nBottom < nTop )
276         *this = Rectangle();
277 
278     return *this;
279 }
280 
281 /*************************************************************************
282 |*
283 |*    Rectangle::Justify()
284 |*
285 |*    Beschreibung      GEN.SDW
286 |*    Ersterstellung    DV 07.03.91
287 |*    Letzte Aenderung  DV 07.03.91
288 |*
289 *************************************************************************/
290 
Justify()291 void Rectangle::Justify()
292 {
293     long nHelp;
294 
295     // Abfrage, ob Right kleiner Left
296     if ( (nRight < nLeft) && (nRight != RECT_EMPTY) )
297     {
298         nHelp = nLeft;
299         nLeft = nRight;
300         nRight = nHelp;
301     }
302 
303     // Abfrage, ob Bottom kleiner Top
304     if ( (nBottom < nTop) && (nBottom != RECT_EMPTY) )
305     {
306         nHelp = nBottom;
307         nBottom = nTop;
308         nTop = nHelp;
309     }
310 }
311 
312 /*************************************************************************
313 |*
314 |*    Rectangle::IsInside()
315 |*
316 |*    Beschreibung      GEN.SDW
317 |*    Ersterstellung    TH 19.03.90
318 |*    Letzte Aenderung  MM 21.04.94
319 |*
320 *************************************************************************/
321 
IsInside(const Point & rPoint) const322 sal_Bool Rectangle::IsInside( const Point& rPoint ) const
323 {
324     if ( IsEmpty() )
325         return sal_False;
326 
327     sal_Bool bRet = sal_True;
328     if ( nLeft <= nRight )
329     {
330         if ( (rPoint.X() < nLeft) || (rPoint.X() > nRight) )
331             bRet = sal_False;
332     }
333     else
334     {
335         if ( (rPoint.X() > nLeft) || (rPoint.X() < nRight) )
336             bRet = sal_False;
337     }
338     if ( nTop <= nBottom )
339     {
340         if ( (rPoint.Y() < nTop) || (rPoint.Y() > nBottom) )
341             bRet = sal_False;
342     }
343     else
344     {
345         if ( (rPoint.Y() > nTop) || (rPoint.Y() < nBottom) )
346             bRet = sal_False;
347     }
348     return bRet;
349 }
350 
351 /*************************************************************************
352 |*
353 |*    Rectangle::IsInside()
354 |*
355 |*    Beschreibung      GEN.SDW
356 |*    Ersterstellung    TH 19.03.90
357 |*    Letzte Aenderung  MM 21.04.94
358 |*
359 *************************************************************************/
360 
IsInside(const Rectangle & rRect) const361 sal_Bool Rectangle::IsInside( const Rectangle& rRect ) const
362 {
363     if ( IsInside( rRect.TopLeft() ) && IsInside( rRect.BottomRight() ) )
364         return sal_True;
365     else
366         return sal_False;
367 }
368 
369 /*************************************************************************
370 |*
371 |*    Rectangle::IsOver()
372 |*
373 |*    Beschreibung      GEN.SDW
374 |*    Ersterstellung    TH 19.03.90
375 |*    Letzte Aenderung  MM 21.04.94
376 |*
377 *************************************************************************/
378 
IsOver(const Rectangle & rRect) const379 sal_Bool Rectangle::IsOver( const Rectangle& rRect ) const
380 {
381     // Wenn sie sich nicht schneiden, ueberlappen sie auch nicht
382     return !GetIntersection( rRect ).IsEmpty();
383 }
384 
385 // =======================================================================
386 
operator >>(SvStream & rIStream,Rectangle & rRect)387 SvStream& operator>>( SvStream& rIStream, Rectangle& rRect )
388 {
389 	DBG_ASSERTWARNING( rIStream.GetVersion(), "Rectangle::>> - Solar-Version not set on rIStream" );
390 
391 	if ( rIStream.GetCompressMode() == COMPRESSMODE_FULL )
392 	{
393 		unsigned char	cIdAry[2];
394 		unsigned char	cAry[16];
395 		int 			i;
396 		int 			iLast;
397 		int 			i1;
398 		int 			i2;
399 		int 			i3;
400 		int 			i4;
401 		sal_uInt32			nNum;
402 
403 		rIStream.Read( cIdAry, 2 );
404 		i1 = (cIdAry[0] & 0x70) >> 4;
405 		i2 = cIdAry[0] & 0x07;
406 		i3 = (cIdAry[1] & 0x70) >> 4;
407 		i4 = cIdAry[1] & 0x07;
408 		rIStream.Read( cAry, i1+i2+i3+i4 );
409 
410 		nNum = 0;
411 		i = i1;
412 		iLast = i;
413 		while ( i )
414 		{
415 			i--;
416 			nNum <<= 8;
417 			nNum |= cAry[i];
418 		}
419 		iLast = i1;
420 		if ( cIdAry[0] & 0x80 )
421 			nNum ^= 0xFFFFFFFF;
422 		rRect.nLeft = (sal_Int32)nNum;
423 
424 		nNum = 0;
425 		i = iLast+i2;
426 		while ( i > iLast )
427 		{
428 			i--;
429 			nNum <<= 8;
430 			nNum |= cAry[i];
431 		}
432 		iLast += i2;
433 		if ( cIdAry[0] & 0x08 )
434 			nNum ^= 0xFFFFFFFF;
435 		rRect.nTop = (sal_Int32)nNum;
436 
437 		nNum = 0;
438 		i = iLast+i3;
439 		while ( i > iLast )
440 		{
441 			i--;
442 			nNum <<= 8;
443 			nNum |= cAry[i];
444 		}
445 		iLast += i3;
446 		if ( cIdAry[1] & 0x80 )
447 			nNum ^= 0xFFFFFFFF;
448 		rRect.nRight = (sal_Int32)nNum;
449 
450 		nNum = 0;
451 		i = iLast+i4;
452 		while ( i > iLast )
453 		{
454 			i--;
455 			nNum <<= 8;
456 			nNum |= cAry[i];
457 		}
458 		if ( cIdAry[1] & 0x08 )
459 			nNum ^= 0xFFFFFFFF;
460 		rRect.nBottom = (sal_Int32)nNum;
461 	}
462 	else
463 	{
464 		rIStream >> rRect.nLeft >> rRect.nTop >> rRect.nRight >> rRect.nBottom;
465 	}
466 
467 	return rIStream;
468 }
469 
470 // -----------------------------------------------------------------------
471 
operator <<(SvStream & rOStream,const Rectangle & rRect)472 SvStream& operator<<( SvStream& rOStream, const Rectangle& rRect )
473 {
474 	DBG_ASSERTWARNING( rOStream.GetVersion(), "Rectangle::<< - Solar-Version not set on rOStream" );
475 
476 	if ( rOStream.GetCompressMode() == COMPRESSMODE_FULL )
477 	{
478 		unsigned char	cAry[18];
479 		int 			i = 2;
480 		sal_uInt32			nNum;
481 
482 		cAry[0] = 0;
483 		cAry[1] = 0;
484 
485 		nNum = (sal_uInt32)(sal_Int32)rRect.nLeft;
486 		if ( rRect.nLeft < 0 )
487 		{
488 			cAry[0] |= 0x80;
489 			nNum ^= 0xFFFFFFFF;
490 		}
491 		if ( nNum )
492 		{
493 			cAry[i] = (unsigned char)(nNum & 0xFF);
494 			nNum >>= 8;
495 			i++;
496 
497 			if ( nNum )
498 			{
499 				cAry[i] = (unsigned char)(nNum & 0xFF);
500 				nNum >>= 8;
501 				i++;
502 
503 				if ( nNum )
504 				{
505 					cAry[i] = (unsigned char)(nNum & 0xFF);
506 					nNum >>= 8;
507 					i++;
508 
509 					if ( nNum )
510 					{
511 						cAry[i] = (unsigned char)(nNum & 0xFF);
512 						nNum >>= 8;
513 						i++;
514 						cAry[0] |= 0x40;
515 					}
516 					else
517 						cAry[0] |= 0x30;
518 				}
519 				else
520 					cAry[0] |= 0x20;
521 			}
522 			else
523 				cAry[0] |= 0x10;
524 		}
525 
526 		nNum = (sal_uInt32)(sal_Int32)rRect.nTop;
527 		if ( rRect.nTop < 0 )
528 		{
529 			cAry[0] |= 0x08;
530 			nNum ^= 0xFFFFFFFF;
531 		}
532 		if ( nNum )
533 		{
534 			cAry[i] = (unsigned char)(nNum & 0xFF);
535 			nNum >>= 8;
536 			i++;
537 
538 			if ( nNum )
539 			{
540 				cAry[i] = (unsigned char)(nNum & 0xFF);
541 				nNum >>= 8;
542 				i++;
543 
544 				if ( nNum )
545 				{
546 					cAry[i] = (unsigned char)(nNum & 0xFF);
547 					nNum >>= 8;
548 					i++;
549 
550 					if ( nNum )
551 					{
552 						cAry[i] = (unsigned char)(nNum & 0xFF);
553 						nNum >>= 8;
554 						i++;
555 						cAry[0] |= 0x04;
556 					}
557 					else
558 						cAry[0] |= 0x03;
559 				}
560 				else
561 					cAry[0] |= 0x02;
562 			}
563 			else
564 				cAry[0] |= 0x01;
565 		}
566 
567 		nNum = (sal_uInt32)(sal_Int32)rRect.nRight;
568 		if ( rRect.nRight < 0 )
569 		{
570 			cAry[1] |= 0x80;
571 			nNum ^= 0xFFFFFFFF;
572 		}
573 		if ( nNum )
574 		{
575 			cAry[i] = (unsigned char)(nNum & 0xFF);
576 			nNum >>= 8;
577 			i++;
578 
579 			if ( nNum )
580 			{
581 				cAry[i] = (unsigned char)(nNum & 0xFF);
582 				nNum >>= 8;
583 				i++;
584 
585 				if ( nNum )
586 				{
587 					cAry[i] = (unsigned char)(nNum & 0xFF);
588 					nNum >>= 8;
589 					i++;
590 
591 					if ( nNum )
592 					{
593 						cAry[i] = (unsigned char)(nNum & 0xFF);
594 						nNum >>= 8;
595 						i++;
596 						cAry[1] |= 0x40;
597 					}
598 					else
599 						cAry[1] |= 0x30;
600 				}
601 				else
602 					cAry[1] |= 0x20;
603 			}
604 			else
605 				cAry[1] |= 0x10;
606 		}
607 
608 		nNum = (sal_uInt32)(sal_Int32)rRect.nBottom;
609 		if ( rRect.nBottom < 0 )
610 		{
611 			cAry[1] |= 0x08;
612 			nNum ^= 0xFFFFFFFF;
613 		}
614 		if ( nNum )
615 		{
616 			cAry[i] = (unsigned char)(nNum & 0xFF);
617 			nNum >>= 8;
618 			i++;
619 
620 			if ( nNum )
621 			{
622 				cAry[i] = (unsigned char)(nNum & 0xFF);
623 				nNum >>= 8;
624 				i++;
625 
626 				if ( nNum )
627 				{
628 					cAry[i] = (unsigned char)(nNum & 0xFF);
629 					nNum >>= 8;
630 					i++;
631 
632 					if ( nNum )
633 					{
634 						cAry[i] = (unsigned char)(nNum & 0xFF);
635 						nNum >>= 8;
636 						i++;
637 						cAry[1] |= 0x04;
638 					}
639 					else
640 						cAry[1] |= 0x03;
641 				}
642 				else
643 					cAry[1] |= 0x02;
644 			}
645 			else
646 				cAry[1] |= 0x01;
647 		}
648 
649 		rOStream.Write( cAry, i );
650 	}
651 	else
652 	{
653 		rOStream << rRect.nLeft << rRect.nTop << rRect.nRight << rRect.nBottom;
654 	}
655 
656 	return rOStream;
657 }
658