xref: /aoo41x/main/vcl/source/gdi/graphictools.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 #include <tools/vcompat.hxx>
31 
32 #include <vcl/graphictools.hxx>
33 
34 static ::rtl::OString polyToString( const Polygon& rPoly )
35 {
36     ::rtl::OString aStr;
37     sal_uInt16 nVertex;
38     for(nVertex=0; nVertex<rPoly.GetSize(); ++nVertex)
39     {
40         aStr += "(";
41         switch( rPoly.GetFlags(nVertex) )
42         {
43             case POLY_NORMAL:
44             case POLY_SMOOTH:
45             case POLY_SYMMTR:
46                 aStr += "n: ";
47                 break;
48 
49             case POLY_CONTROL:
50                 aStr += "c: ";
51                 break;
52 
53             default:
54                 DBG_ERROR( "SvtGraphicStroke::polyToString invalid flag");
55                 break;
56         }
57         aStr += ::rtl::OString::valueOf( static_cast< double >( rPoly[nVertex].getX() ) );
58         aStr += ",";
59         aStr += ::rtl::OString::valueOf( static_cast< double >( rPoly[nVertex].getY() ) );
60         aStr += ") ";
61     }
62 
63     return aStr;
64 }
65 
66 static ::rtl::OString polyPolyToString( const PolyPolygon& rPolyPoly )
67 {
68     ::rtl::OString aStr;
69     sal_uInt16 nPoly;
70     for(nPoly=0; nPoly<rPolyPoly.Count(); ++nPoly)
71     {
72         const Polygon& rPoly = rPolyPoly[nPoly];
73 
74         aStr += "{ ";
75         aStr += polyToString( rPoly );
76         aStr += "} ";
77     }
78 
79     return aStr;
80 }
81 
82 static ::rtl::OString dashToString( const SvtGraphicStroke::DashArray& rDashArray )
83 {
84     ::rtl::OString aStr;
85 
86     aStr += "dash: [ ";
87 
88     int i, nDashes( rDashArray.size() );
89     for(i=0; i<nDashes; ++i)
90     {
91         aStr += ::rtl::OString::valueOf( rDashArray[i] );
92         aStr += " ";
93     }
94 
95     aStr += "] ";
96 
97     return aStr;
98 }
99 
100 static ::rtl::OString colorToString( Color aColor )
101 {
102     ::rtl::OString aStr;
103 
104     aStr += "color: [ ";
105     aStr += ::rtl::OString::valueOf( aColor.GetRed() );
106     aStr += " ";
107     aStr += ::rtl::OString::valueOf( aColor.GetGreen() );
108     aStr += " ";
109     aStr += ::rtl::OString::valueOf( aColor.GetBlue() );
110     aStr += " ] ";
111 
112     return aStr;
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////
116 
117 SvtGraphicFill::Transform::Transform()
118 {
119     matrix[0] = 1.0; matrix[1] = 0.0; matrix[2] = 0.0;
120     matrix[3] = 0.0; matrix[4] = 1.0; matrix[5] = 0.0;
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////
124 
125 SvtGraphicStroke::SvtGraphicStroke() :
126     maPath(),
127     maStartArrow(),
128     maEndArrow(),
129     mfTransparency(),
130     mfStrokeWidth(),
131     maCapType(),
132     maJoinType(),
133     mfMiterLimit( 3.0 ),
134     maDashArray()
135 {
136 }
137 
138 SvtGraphicStroke::SvtGraphicStroke( const Polygon& 		rPath,
139                                     const PolyPolygon&	rStartArrow,
140                                     const PolyPolygon&	rEndArrow,
141                                     double				fTransparency,
142                                     double 				fStrokeWidth,
143                                     CapType				aCap,
144                                     JoinType			aJoin,
145                                     double				fMiterLimit,
146                                     const DashArray&	rDashArray	) :
147     maPath( rPath ),
148     maStartArrow( rStartArrow ),
149     maEndArrow( rEndArrow ),
150     mfTransparency( fTransparency ),
151     mfStrokeWidth( fStrokeWidth ),
152     maCapType( aCap ),
153     maJoinType( aJoin ),
154     mfMiterLimit( fMiterLimit ),
155     maDashArray( rDashArray )
156 {
157 }
158 
159 void SvtGraphicStroke::getPath( Polygon& rPath ) const
160 {
161     rPath = maPath;
162 }
163 
164 void SvtGraphicStroke::getStartArrow( PolyPolygon& rPath ) const
165 {
166     rPath = maStartArrow;
167 }
168 
169 void SvtGraphicStroke::getEndArrow( PolyPolygon& rPath ) const
170 {
171     rPath = maEndArrow;
172 }
173 
174 double SvtGraphicStroke::getTransparency() const
175 {
176     return mfTransparency;
177 }
178 
179 double SvtGraphicStroke::getStrokeWidth() const
180 {
181     return mfStrokeWidth;
182 }
183 
184 SvtGraphicStroke::CapType SvtGraphicStroke::getCapType() const
185 {
186     return maCapType;
187 }
188 
189 SvtGraphicStroke::JoinType SvtGraphicStroke::getJoinType() const
190 {
191     return maJoinType;
192 }
193 
194 double SvtGraphicStroke::getMiterLimit() const
195 {
196     return mfMiterLimit;
197 }
198 
199 void SvtGraphicStroke::getDashArray( DashArray& rDashArray ) const
200 {
201     rDashArray = maDashArray;
202 }
203 
204 ::rtl::OString SvtGraphicStroke::toString() const
205 {
206     ::rtl::OString aStr;
207 
208     aStr += polyToString( maPath );
209     aStr += "trans: ";
210     aStr += ::rtl::OString::valueOf( static_cast< double >(getTransparency()) );
211     aStr += " width: ";
212     aStr += ::rtl::OString::valueOf( static_cast< double >(getStrokeWidth()) );
213     aStr += " cap: ";
214     switch( getCapType() )
215     {
216         case capButt:
217             aStr += "butt";
218             break;
219 
220         case capRound:
221             aStr += "round";
222             break;
223 
224         case capSquare:
225             aStr += "square";
226             break;
227 
228         default:
229             DBG_ERROR( "SvtGraphicStroke::toString missing cap type");
230             break;
231     }
232     aStr += " join: ";
233     switch( getJoinType() )
234     {
235         case joinMiter:
236             aStr += "miter";
237             break;
238 
239         case joinRound:
240             aStr += "round";
241             break;
242 
243         case joinBevel:
244             aStr += "bevel";
245             break;
246 
247         case joinNone:
248             aStr += "none";
249             break;
250 
251         default:
252             DBG_ERROR( "SvtGraphicStroke::toString missing join type");
253             break;
254     }
255     aStr += " ";
256 
257     if( maStartArrow.Count() )
258     {
259         aStr += "start: ";
260         aStr += polyPolyToString( maStartArrow );
261         aStr += " ";
262     }
263 
264     if( maEndArrow.Count() )
265     {
266         aStr += "end: ";
267         aStr += polyPolyToString( maEndArrow );
268         aStr += " ";
269     }
270 
271     aStr += dashToString( maDashArray );
272 
273     return aStr;
274 }
275 
276 void SvtGraphicStroke::setPath( const Polygon& rPoly )
277 {
278     maPath = rPoly;
279 }
280 
281 void SvtGraphicStroke::setStartArrow( const PolyPolygon& rPoly )
282 {
283     maStartArrow = rPoly;
284 }
285 
286 void SvtGraphicStroke::setEndArrow( const PolyPolygon& rPoly )
287 {
288     maEndArrow = rPoly;
289 }
290 
291 void SvtGraphicStroke::setTransparency( double fTrans )
292 {
293     mfTransparency = fTrans;
294 }
295 
296 void SvtGraphicStroke::setStrokeWidth( double fWidth )
297 {
298     mfStrokeWidth = fWidth;
299 }
300 
301 void SvtGraphicStroke::setCapType( CapType eType )
302 {
303     maCapType = eType;
304 }
305 
306 void SvtGraphicStroke::setJoinType( JoinType eType )
307 {
308     maJoinType = eType;
309 }
310 
311 void SvtGraphicStroke::setMiterLimit( double fMiterLimit )
312 {
313     mfMiterLimit = fMiterLimit;
314 }
315 
316 void SvtGraphicStroke::setDashArray( const DashArray& rDashArray )
317 {
318     maDashArray = rDashArray;
319 }
320 
321 SvStream& operator<<( SvStream& rOStm, const SvtGraphicStroke& rClass )
322 {
323 	VersionCompat aCompat( rOStm, STREAM_WRITE, 1 );
324 
325     rClass.maPath.Write( rOStm );
326     rClass.maStartArrow.Write( rOStm );
327     rClass.maEndArrow.Write( rOStm );
328     rOStm << rClass.mfTransparency;
329     rOStm << rClass.mfStrokeWidth;
330     sal_uInt16 nTmp = sal::static_int_cast<sal_uInt16>( rClass.maCapType );
331     rOStm << nTmp;
332     nTmp = sal::static_int_cast<sal_uInt16>( rClass.maJoinType );
333     rOStm << nTmp;
334     rOStm << rClass.mfMiterLimit;
335 
336     rOStm << static_cast<sal_uInt32>(rClass.maDashArray.size());
337     size_t i;
338     for(i=0; i<rClass.maDashArray.size(); ++i)
339         rOStm << rClass.maDashArray[i];
340 
341 	return rOStm;
342 }
343 
344 SvStream& operator>>( SvStream& rIStm, SvtGraphicStroke& rClass )
345 {
346 	VersionCompat aCompat( rIStm, STREAM_READ );
347 
348     rClass.maPath.Read( rIStm );
349     rClass.maStartArrow.Read( rIStm );
350     rClass.maEndArrow.Read( rIStm );
351     rIStm >> rClass.mfTransparency;
352     rIStm >> rClass.mfStrokeWidth;
353     sal_uInt16 nTmp;
354     rIStm >> nTmp;
355     rClass.maCapType = SvtGraphicStroke::CapType(nTmp);
356     rIStm >> nTmp;
357     rClass.maJoinType = SvtGraphicStroke::JoinType(nTmp);
358     rIStm >> rClass.mfMiterLimit;
359 
360     sal_uInt32 nSize;
361     rIStm >> nSize;
362     rClass.maDashArray.resize(nSize);
363     size_t i;
364     for(i=0; i<rClass.maDashArray.size(); ++i)
365         rIStm >> rClass.maDashArray[i];
366 
367 	return rIStm;
368 }
369 
370 
371 /////////////////////////////////////////////////////////////////////////////
372 
373 SvtGraphicFill::SvtGraphicFill() :
374     maPath(),
375     maFillColor( COL_BLACK ),
376     mfTransparency(),
377     maFillRule(),
378     maFillType(),
379     maFillTransform(),
380     maHatchType(),
381     maHatchColor( COL_BLACK ),
382     maGradientType(),
383     maGradient1stColor( COL_BLACK ),
384     maGradient2ndColor( COL_BLACK ),
385     maGradientStepCount( gradientStepsInfinite ),
386     maFillGraphic()
387 {
388 }
389 
390 SvtGraphicFill::SvtGraphicFill( const PolyPolygon& 	rPath,
391                                 Color				aFillColor,
392                                 double				fTransparency,
393                                 FillRule			aFillRule,
394                                 FillType			aFillType,
395                                 const Transform&	aFillTransform,
396                                 bool				bTiling,
397                                 HatchType			aHatchType,
398                                 Color				aHatchColor,
399                                 GradientType		aGradientType,
400                                 Color				aGradient1stColor,
401                                 Color				aGradient2ndColor,
402                                 int					aGradientStepCount,
403                                 const Graphic&		aFillGraphic ) :
404     maPath( rPath ),
405     maFillColor( aFillColor ),
406     mfTransparency( fTransparency ),
407     maFillRule( aFillRule ),
408     maFillType( aFillType ),
409     maFillTransform( aFillTransform ),
410     mbTiling( bTiling ),
411     maHatchType( aHatchType ),
412     maHatchColor( aHatchColor ),
413     maGradientType( aGradientType ),
414     maGradient1stColor( aGradient1stColor ),
415     maGradient2ndColor( aGradient2ndColor ),
416     maGradientStepCount( aGradientStepCount ),
417     maFillGraphic( aFillGraphic )
418 {
419 }
420 
421 void SvtGraphicFill::getPath( PolyPolygon& rPath ) const
422 {
423     rPath = maPath;
424 }
425 
426 Color SvtGraphicFill::getFillColor() const
427 {
428     return maFillColor;
429 }
430 
431 double SvtGraphicFill::getTransparency() const
432 {
433     return mfTransparency;
434 }
435 
436 SvtGraphicFill::FillRule SvtGraphicFill::getFillRule() const
437 {
438     return maFillRule;
439 }
440 
441 SvtGraphicFill::FillType SvtGraphicFill::getFillType() const
442 {
443     return maFillType;
444 }
445 
446 void SvtGraphicFill::getTransform( Transform& rTrans ) const
447 {
448     rTrans = maFillTransform;
449 }
450 
451 bool SvtGraphicFill::IsTiling() const
452 {
453     return mbTiling;
454 }
455 
456 bool SvtGraphicFill::isTiling() const
457 {
458     return mbTiling;
459 }
460 
461 SvtGraphicFill::HatchType SvtGraphicFill::getHatchType() const
462 {
463     return maHatchType;
464 }
465 
466 Color SvtGraphicFill::getHatchColor() const
467 {
468     return maHatchColor;
469 }
470 
471 SvtGraphicFill::GradientType SvtGraphicFill::getGradientType() const
472 {
473     return maGradientType;
474 }
475 
476 Color SvtGraphicFill::getGradient1stColor() const
477 {
478     return maGradient1stColor;
479 }
480 
481 Color SvtGraphicFill::getGradient2ndColor() const
482 {
483     return maGradient2ndColor;
484 }
485 
486 int SvtGraphicFill::getGradientStepCount() const
487 {
488     return maGradientStepCount;
489 }
490 
491 void SvtGraphicFill::getGraphic( Graphic& rGraphic ) const
492 {
493     rGraphic = maFillGraphic;
494 }
495 
496 ::rtl::OString SvtGraphicFill::toString() const
497 {
498     ::rtl::OString aStr;
499 
500     aStr += polyPolyToString( maPath );
501     aStr += "fill";
502     aStr += colorToString( getFillColor() );
503     aStr += " trans: ";
504     aStr += ::rtl::OString::valueOf( static_cast< double >(getTransparency()) );
505     aStr += " rule: ";
506     switch( getFillRule() )
507     {
508         case fillNonZero:
509             aStr += "nonzero";
510             break;
511 
512         case fillEvenOdd:
513             aStr += "evenodd";
514             break;
515 
516         default:
517             DBG_ERROR( "SvtGraphicFill::toString missing fill rule");
518             break;
519     }
520     aStr += " type: ";
521     switch( getFillType() )
522     {
523         case fillSolid:
524             aStr += "solid";
525             break;
526 
527         case fillGradient:
528             aStr += "gradient";
529             break;
530 
531         case fillHatch:
532             aStr += "hatch";
533             break;
534 
535         case fillTexture:
536             aStr += "bitmap";
537             break;
538 
539         default:
540             DBG_ERROR( "SvtGraphicStroke::toString missing fill type");
541             break;
542     }
543 
544     aStr += " transform: [ ";
545     int i;
546     for(i=0; i<Transform::MatrixSize; ++i)
547         aStr += ::rtl::OString::valueOf( maFillTransform.matrix[i] );
548     aStr += " ] ";
549 
550     aStr += " hatch: ";
551     switch( getHatchType() )
552     {
553         case hatchSingle:
554             aStr += "single";
555             break;
556 
557         case hatchDouble:
558             aStr += "double";
559             break;
560 
561         case hatchTriple:
562             aStr += "triple";
563             break;
564 
565         default:
566             DBG_ERROR( "SvtGraphicStroke::toString missing hatch type");
567             break;
568     }
569 
570     aStr += " hatch";
571     aStr += colorToString( getHatchColor() );
572 
573     aStr += " gradient: ";
574     switch( getGradientType() )
575     {
576         case gradientLinear:
577             aStr += "linear";
578             break;
579 
580         case gradientRadial:
581             aStr += "radial";
582             break;
583 
584         case gradientRectangular:
585             aStr += "rectangular";
586             break;
587 
588         default:
589             DBG_ERROR( "SvtGraphicStroke::toString missing gradient type");
590             break;
591     }
592 
593     aStr += " grad1st";
594     aStr += colorToString( getGradient1stColor() );
595 
596     aStr += " grad2nd";
597     aStr += colorToString( getGradient2ndColor() );
598 
599     aStr += " gradstep";
600     aStr += ::rtl::OString::valueOf( (sal_Int32)getGradientStepCount() );
601 
602     if( maFillGraphic.GetType() != GRAPHIC_NONE )
603     {
604         aStr += " fillgraphic: ";
605         switch( maFillGraphic.GetType() )
606         {
607             case GRAPHIC_BITMAP:
608                 aStr += "bitmap";
609                 break;
610 
611             case GRAPHIC_GDIMETAFILE:
612                 aStr += "metafile";
613                 break;
614 
615             case GRAPHIC_DEFAULT:
616                 aStr += "default";
617                 break;
618 
619             default:
620                 DBG_ERROR( "SvtGraphicStroke::toString missing graphic type");
621                 break;
622         }
623 
624         aStr += " of ";
625         aStr += ::rtl::OString::valueOf( static_cast< sal_Int32 >(maFillGraphic.GetSizeBytes()) );
626         aStr += " bytes";
627     }
628 
629     return aStr;
630 }
631 
632 void SvtGraphicFill::setPath( const PolyPolygon& rPath )
633 {
634     maPath = rPath;
635 }
636 
637 void SvtGraphicFill::setFillColor( Color aFillColor )
638 {
639     maFillColor = aFillColor;
640 }
641 
642 void SvtGraphicFill::setTransparency( double fTransparency )
643 {
644     mfTransparency = fTransparency;
645 }
646 
647 void SvtGraphicFill::setFillRule( FillRule aFillRule )
648 {
649     maFillRule = aFillRule;
650 }
651 
652 void SvtGraphicFill::setFillType( FillType aFillType )
653 {
654     maFillType = aFillType;
655 }
656 
657 void SvtGraphicFill::setTransform( const Transform& rTransform )
658 {
659     maFillTransform = rTransform;
660 }
661 
662 void SvtGraphicFill::setTiling( bool bTiling )
663 {
664     mbTiling = bTiling;
665 }
666 
667 void SvtGraphicFill::setHatchType( HatchType aHatchType )
668 {
669     maHatchType = aHatchType;
670 }
671 
672 void SvtGraphicFill::setHatchColor( Color aHatchColor )
673 {
674     maHatchColor = aHatchColor;
675 }
676 
677 void SvtGraphicFill::setGradientType( GradientType aGradType )
678 {
679     maGradientType = aGradType;
680 }
681 
682 void SvtGraphicFill::setGradient1stColor( Color aColor )
683 {
684     maGradient1stColor = aColor;
685 }
686 
687 void SvtGraphicFill::setGradient2ndColor( Color aColor )
688 {
689     maGradient2ndColor = aColor;
690 }
691 
692 void SvtGraphicFill::setGradientStepCount( int aCount )
693 {
694     maGradientStepCount = aCount;
695 }
696 
697 void SvtGraphicFill::setGraphic( const Graphic& rGraphic )
698 {
699     maFillGraphic = rGraphic;
700 }
701 
702 SvStream& operator<<( SvStream& rOStm, const SvtGraphicFill& rClass )
703 {
704 	VersionCompat aCompat( rOStm, STREAM_WRITE, 1 );
705 
706     rClass.maPath.Write( rOStm );
707     rOStm << rClass.maFillColor;
708     rOStm << rClass.mfTransparency;
709     sal_uInt16 nTmp = sal::static_int_cast<sal_uInt16>( rClass.maFillRule );
710     rOStm << nTmp;
711     nTmp = sal::static_int_cast<sal_uInt16>( rClass.maFillType );
712     rOStm << nTmp;
713     int i;
714     for(i=0; i<SvtGraphicFill::Transform::MatrixSize; ++i)
715         rOStm << rClass.maFillTransform.matrix[i];
716     nTmp = rClass.mbTiling;
717     rOStm << nTmp;
718     nTmp = sal::static_int_cast<sal_uInt16>( rClass.maHatchType );
719     rOStm << nTmp;
720     rOStm << rClass.maHatchColor;
721     nTmp = sal::static_int_cast<sal_uInt16>( rClass.maGradientType );
722     rOStm << nTmp;
723     rOStm << rClass.maGradient1stColor;
724     rOStm << rClass.maGradient2ndColor;
725     rOStm << rClass.maGradientStepCount;
726     rOStm << rClass.maFillGraphic;
727 
728 	return rOStm;
729 }
730 
731 SvStream& operator>>( SvStream& rIStm, SvtGraphicFill& rClass )
732 {
733 	VersionCompat aCompat( rIStm, STREAM_READ );
734 
735     rClass.maPath.Read( rIStm );
736     rIStm >> rClass.maFillColor;
737     rIStm >> rClass.mfTransparency;
738     sal_uInt16 nTmp;
739     rIStm >> nTmp;
740     rClass.maFillRule = SvtGraphicFill::FillRule( nTmp );
741     rIStm >> nTmp;
742     rClass.maFillType = SvtGraphicFill::FillType( nTmp );
743     int i;
744     for(i=0; i<SvtGraphicFill::Transform::MatrixSize; ++i)
745         rIStm >> rClass.maFillTransform.matrix[i];
746     rIStm >> nTmp;
747     rClass.mbTiling = nTmp;
748     rIStm >> nTmp;
749     rClass.maHatchType = SvtGraphicFill::HatchType( nTmp );
750     rIStm >> rClass.maHatchColor;
751     rIStm >> nTmp;
752     rClass.maGradientType = SvtGraphicFill::GradientType( nTmp );
753     rIStm >> rClass.maGradient1stColor;
754     rIStm >> rClass.maGradient2ndColor;
755     rIStm >> rClass.maGradientStepCount;
756     rIStm >> rClass.maFillGraphic;
757 
758 	return rIStm;
759 }
760