merge the formfield patch from ooo-build
[ooovba.git] / vcl / source / gdi / graphictools.cxx
blob0447dd81a99ada4de1d70f4a71e8dd1eb8044449
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: graphictools.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
33 #include <tools/vcompat.hxx>
35 #include <vcl/graphictools.hxx>
37 static ::rtl::OString polyToString( const Polygon& rPoly )
39 ::rtl::OString aStr;
40 USHORT nVertex;
41 for(nVertex=0; nVertex<rPoly.GetSize(); ++nVertex)
43 aStr += "(";
44 switch( rPoly.GetFlags(nVertex) )
46 case POLY_NORMAL:
47 case POLY_SMOOTH:
48 case POLY_SYMMTR:
49 aStr += "n: ";
50 break;
52 case POLY_CONTROL:
53 aStr += "c: ";
54 break;
56 default:
57 DBG_ERROR( "SvtGraphicStroke::polyToString invalid flag");
58 break;
60 aStr += ::rtl::OString::valueOf( static_cast< double >( rPoly[nVertex].getX() ) );
61 aStr += ",";
62 aStr += ::rtl::OString::valueOf( static_cast< double >( rPoly[nVertex].getY() ) );
63 aStr += ") ";
66 return aStr;
69 static ::rtl::OString polyPolyToString( const PolyPolygon& rPolyPoly )
71 ::rtl::OString aStr;
72 USHORT nPoly;
73 for(nPoly=0; nPoly<rPolyPoly.Count(); ++nPoly)
75 const Polygon& rPoly = rPolyPoly[nPoly];
77 aStr += "{ ";
78 aStr += polyToString( rPoly );
79 aStr += "} ";
82 return aStr;
85 static ::rtl::OString dashToString( const SvtGraphicStroke::DashArray& rDashArray )
87 ::rtl::OString aStr;
89 aStr += "dash: [ ";
91 int i, nDashes( rDashArray.size() );
92 for(i=0; i<nDashes; ++i)
94 aStr += ::rtl::OString::valueOf( rDashArray[i] );
95 aStr += " ";
98 aStr += "] ";
100 return aStr;
103 static ::rtl::OString colorToString( Color aColor )
105 ::rtl::OString aStr;
107 aStr += "color: [ ";
108 aStr += ::rtl::OString::valueOf( aColor.GetRed() );
109 aStr += " ";
110 aStr += ::rtl::OString::valueOf( aColor.GetGreen() );
111 aStr += " ";
112 aStr += ::rtl::OString::valueOf( aColor.GetBlue() );
113 aStr += " ] ";
115 return aStr;
118 ////////////////////////////////////////////////////////////////////////////
120 SvtGraphicFill::Transform::Transform()
122 matrix[0] = 1.0; matrix[1] = 0.0; matrix[2] = 0.0;
123 matrix[3] = 0.0; matrix[4] = 1.0; matrix[5] = 0.0;
126 ////////////////////////////////////////////////////////////////////////////
128 SvtGraphicStroke::SvtGraphicStroke() :
129 maPath(),
130 maStartArrow(),
131 maEndArrow(),
132 mfTransparency(),
133 mfStrokeWidth(),
134 maCapType(),
135 maJoinType(),
136 mfMiterLimit( 3.0 ),
137 maDashArray()
141 SvtGraphicStroke::SvtGraphicStroke( const Polygon& rPath,
142 const PolyPolygon& rStartArrow,
143 const PolyPolygon& rEndArrow,
144 double fTransparency,
145 double fStrokeWidth,
146 CapType aCap,
147 JoinType aJoin,
148 double fMiterLimit,
149 const DashArray& rDashArray ) :
150 maPath( rPath ),
151 maStartArrow( rStartArrow ),
152 maEndArrow( rEndArrow ),
153 mfTransparency( fTransparency ),
154 mfStrokeWidth( fStrokeWidth ),
155 maCapType( aCap ),
156 maJoinType( aJoin ),
157 mfMiterLimit( fMiterLimit ),
158 maDashArray( rDashArray )
162 void SvtGraphicStroke::getPath( Polygon& rPath ) const
164 rPath = maPath;
167 void SvtGraphicStroke::getStartArrow( PolyPolygon& rPath ) const
169 rPath = maStartArrow;
172 void SvtGraphicStroke::getEndArrow( PolyPolygon& rPath ) const
174 rPath = maEndArrow;
177 double SvtGraphicStroke::getTransparency() const
179 return mfTransparency;
182 double SvtGraphicStroke::getStrokeWidth() const
184 return mfStrokeWidth;
187 SvtGraphicStroke::CapType SvtGraphicStroke::getCapType() const
189 return maCapType;
192 SvtGraphicStroke::JoinType SvtGraphicStroke::getJoinType() const
194 return maJoinType;
197 double SvtGraphicStroke::getMiterLimit() const
199 return mfMiterLimit;
202 void SvtGraphicStroke::getDashArray( DashArray& rDashArray ) const
204 rDashArray = maDashArray;
207 ::rtl::OString SvtGraphicStroke::toString() const
209 ::rtl::OString aStr;
211 aStr += polyToString( maPath );
212 aStr += "trans: ";
213 aStr += ::rtl::OString::valueOf( static_cast< double >(getTransparency()) );
214 aStr += " width: ";
215 aStr += ::rtl::OString::valueOf( static_cast< double >(getStrokeWidth()) );
216 aStr += " cap: ";
217 switch( getCapType() )
219 case capButt:
220 aStr += "butt";
221 break;
223 case capRound:
224 aStr += "round";
225 break;
227 case capSquare:
228 aStr += "square";
229 break;
231 default:
232 DBG_ERROR( "SvtGraphicStroke::toString missing cap type");
233 break;
235 aStr += " join: ";
236 switch( getJoinType() )
238 case joinMiter:
239 aStr += "miter";
240 break;
242 case joinRound:
243 aStr += "round";
244 break;
246 case joinBevel:
247 aStr += "bevel";
248 break;
250 case joinNone:
251 aStr += "none";
252 break;
254 default:
255 DBG_ERROR( "SvtGraphicStroke::toString missing join type");
256 break;
258 aStr += " ";
260 if( maStartArrow.Count() )
262 aStr += "start: ";
263 aStr += polyPolyToString( maStartArrow );
264 aStr += " ";
267 if( maEndArrow.Count() )
269 aStr += "end: ";
270 aStr += polyPolyToString( maEndArrow );
271 aStr += " ";
274 aStr += dashToString( maDashArray );
276 return aStr;
279 void SvtGraphicStroke::setPath( const Polygon& rPoly )
281 maPath = rPoly;
284 void SvtGraphicStroke::setStartArrow( const PolyPolygon& rPoly )
286 maStartArrow = rPoly;
289 void SvtGraphicStroke::setEndArrow( const PolyPolygon& rPoly )
291 maEndArrow = rPoly;
294 void SvtGraphicStroke::setTransparency( double fTrans )
296 mfTransparency = fTrans;
299 void SvtGraphicStroke::setStrokeWidth( double fWidth )
301 mfStrokeWidth = fWidth;
304 void SvtGraphicStroke::setCapType( CapType eType )
306 maCapType = eType;
309 void SvtGraphicStroke::setJoinType( JoinType eType )
311 maJoinType = eType;
314 void SvtGraphicStroke::setMiterLimit( double fMiterLimit )
316 mfMiterLimit = fMiterLimit;
319 void SvtGraphicStroke::setDashArray( const DashArray& rDashArray )
321 maDashArray = rDashArray;
324 SvStream& operator<<( SvStream& rOStm, const SvtGraphicStroke& rClass )
326 VersionCompat aCompat( rOStm, STREAM_WRITE, 1 );
328 rClass.maPath.Write( rOStm );
329 rClass.maStartArrow.Write( rOStm );
330 rClass.maEndArrow.Write( rOStm );
331 rOStm << rClass.mfTransparency;
332 rOStm << rClass.mfStrokeWidth;
333 UINT16 nTmp = sal::static_int_cast<UINT16>( rClass.maCapType );
334 rOStm << nTmp;
335 nTmp = sal::static_int_cast<UINT16>( rClass.maJoinType );
336 rOStm << nTmp;
337 rOStm << rClass.mfMiterLimit;
339 rOStm << static_cast<sal_uInt32>(rClass.maDashArray.size());
340 size_t i;
341 for(i=0; i<rClass.maDashArray.size(); ++i)
342 rOStm << rClass.maDashArray[i];
344 return rOStm;
347 SvStream& operator>>( SvStream& rIStm, SvtGraphicStroke& rClass )
349 VersionCompat aCompat( rIStm, STREAM_READ );
351 rClass.maPath.Read( rIStm );
352 rClass.maStartArrow.Read( rIStm );
353 rClass.maEndArrow.Read( rIStm );
354 rIStm >> rClass.mfTransparency;
355 rIStm >> rClass.mfStrokeWidth;
356 UINT16 nTmp;
357 rIStm >> nTmp;
358 rClass.maCapType = SvtGraphicStroke::CapType(nTmp);
359 rIStm >> nTmp;
360 rClass.maJoinType = SvtGraphicStroke::JoinType(nTmp);
361 rIStm >> rClass.mfMiterLimit;
363 sal_uInt32 nSize;
364 rIStm >> nSize;
365 rClass.maDashArray.resize(nSize);
366 size_t i;
367 for(i=0; i<rClass.maDashArray.size(); ++i)
368 rIStm >> rClass.maDashArray[i];
370 return rIStm;
374 /////////////////////////////////////////////////////////////////////////////
376 SvtGraphicFill::SvtGraphicFill() :
377 maPath(),
378 maFillColor( COL_BLACK ),
379 mfTransparency(),
380 maFillRule(),
381 maFillType(),
382 maFillTransform(),
383 maHatchType(),
384 maHatchColor( COL_BLACK ),
385 maGradientType(),
386 maGradient1stColor( COL_BLACK ),
387 maGradient2ndColor( COL_BLACK ),
388 maGradientStepCount( gradientStepsInfinite ),
389 maFillGraphic()
393 SvtGraphicFill::SvtGraphicFill( const PolyPolygon& rPath,
394 Color aFillColor,
395 double fTransparency,
396 FillRule aFillRule,
397 FillType aFillType,
398 const Transform& aFillTransform,
399 bool bTiling,
400 HatchType aHatchType,
401 Color aHatchColor,
402 GradientType aGradientType,
403 Color aGradient1stColor,
404 Color aGradient2ndColor,
405 int aGradientStepCount,
406 const Graphic& aFillGraphic ) :
407 maPath( rPath ),
408 maFillColor( aFillColor ),
409 mfTransparency( fTransparency ),
410 maFillRule( aFillRule ),
411 maFillType( aFillType ),
412 maFillTransform( aFillTransform ),
413 mbTiling( bTiling ),
414 maHatchType( aHatchType ),
415 maHatchColor( aHatchColor ),
416 maGradientType( aGradientType ),
417 maGradient1stColor( aGradient1stColor ),
418 maGradient2ndColor( aGradient2ndColor ),
419 maGradientStepCount( aGradientStepCount ),
420 maFillGraphic( aFillGraphic )
424 void SvtGraphicFill::getPath( PolyPolygon& rPath ) const
426 rPath = maPath;
429 Color SvtGraphicFill::getFillColor() const
431 return maFillColor;
434 double SvtGraphicFill::getTransparency() const
436 return mfTransparency;
439 SvtGraphicFill::FillRule SvtGraphicFill::getFillRule() const
441 return maFillRule;
444 SvtGraphicFill::FillType SvtGraphicFill::getFillType() const
446 return maFillType;
449 void SvtGraphicFill::getTransform( Transform& rTrans ) const
451 rTrans = maFillTransform;
454 bool SvtGraphicFill::IsTiling() const
456 return mbTiling;
459 bool SvtGraphicFill::isTiling() const
461 return mbTiling;
464 SvtGraphicFill::HatchType SvtGraphicFill::getHatchType() const
466 return maHatchType;
469 Color SvtGraphicFill::getHatchColor() const
471 return maHatchColor;
474 SvtGraphicFill::GradientType SvtGraphicFill::getGradientType() const
476 return maGradientType;
479 Color SvtGraphicFill::getGradient1stColor() const
481 return maGradient1stColor;
484 Color SvtGraphicFill::getGradient2ndColor() const
486 return maGradient2ndColor;
489 int SvtGraphicFill::getGradientStepCount() const
491 return maGradientStepCount;
494 void SvtGraphicFill::getGraphic( Graphic& rGraphic ) const
496 rGraphic = maFillGraphic;
499 ::rtl::OString SvtGraphicFill::toString() const
501 ::rtl::OString aStr;
503 aStr += polyPolyToString( maPath );
504 aStr += "fill";
505 aStr += colorToString( getFillColor() );
506 aStr += " trans: ";
507 aStr += ::rtl::OString::valueOf( static_cast< double >(getTransparency()) );
508 aStr += " rule: ";
509 switch( getFillRule() )
511 case fillNonZero:
512 aStr += "nonzero";
513 break;
515 case fillEvenOdd:
516 aStr += "evenodd";
517 break;
519 default:
520 DBG_ERROR( "SvtGraphicFill::toString missing fill rule");
521 break;
523 aStr += " type: ";
524 switch( getFillType() )
526 case fillSolid:
527 aStr += "solid";
528 break;
530 case fillGradient:
531 aStr += "gradient";
532 break;
534 case fillHatch:
535 aStr += "hatch";
536 break;
538 case fillTexture:
539 aStr += "bitmap";
540 break;
542 default:
543 DBG_ERROR( "SvtGraphicStroke::toString missing fill type");
544 break;
547 aStr += " transform: [ ";
548 int i;
549 for(i=0; i<Transform::MatrixSize; ++i)
550 aStr += ::rtl::OString::valueOf( maFillTransform.matrix[i] );
551 aStr += " ] ";
553 aStr += " hatch: ";
554 switch( getHatchType() )
556 case hatchSingle:
557 aStr += "single";
558 break;
560 case hatchDouble:
561 aStr += "double";
562 break;
564 case hatchTriple:
565 aStr += "triple";
566 break;
568 default:
569 DBG_ERROR( "SvtGraphicStroke::toString missing hatch type");
570 break;
573 aStr += " hatch";
574 aStr += colorToString( getHatchColor() );
576 aStr += " gradient: ";
577 switch( getGradientType() )
579 case gradientLinear:
580 aStr += "linear";
581 break;
583 case gradientRadial:
584 aStr += "radial";
585 break;
587 case gradientRectangular:
588 aStr += "rectangular";
589 break;
591 default:
592 DBG_ERROR( "SvtGraphicStroke::toString missing gradient type");
593 break;
596 aStr += " grad1st";
597 aStr += colorToString( getGradient1stColor() );
599 aStr += " grad2nd";
600 aStr += colorToString( getGradient2ndColor() );
602 aStr += " gradstep";
603 aStr += ::rtl::OString::valueOf( (sal_Int32)getGradientStepCount() );
605 if( maFillGraphic.GetType() != GRAPHIC_NONE )
607 aStr += " fillgraphic: ";
608 switch( maFillGraphic.GetType() )
610 case GRAPHIC_BITMAP:
611 aStr += "bitmap";
612 break;
614 case GRAPHIC_GDIMETAFILE:
615 aStr += "metafile";
616 break;
618 case GRAPHIC_DEFAULT:
619 aStr += "default";
620 break;
622 default:
623 DBG_ERROR( "SvtGraphicStroke::toString missing graphic type");
624 break;
627 aStr += " of ";
628 aStr += ::rtl::OString::valueOf( static_cast< sal_Int32 >(maFillGraphic.GetSizeBytes()) );
629 aStr += " bytes";
632 return aStr;
635 void SvtGraphicFill::setPath( const PolyPolygon& rPath )
637 maPath = rPath;
640 void SvtGraphicFill::setFillColor( Color aFillColor )
642 maFillColor = aFillColor;
645 void SvtGraphicFill::setTransparency( double fTransparency )
647 mfTransparency = fTransparency;
650 void SvtGraphicFill::setFillRule( FillRule aFillRule )
652 maFillRule = aFillRule;
655 void SvtGraphicFill::setFillType( FillType aFillType )
657 maFillType = aFillType;
660 void SvtGraphicFill::setTransform( const Transform& rTransform )
662 maFillTransform = rTransform;
665 void SvtGraphicFill::setTiling( bool bTiling )
667 mbTiling = bTiling;
670 void SvtGraphicFill::setHatchType( HatchType aHatchType )
672 maHatchType = aHatchType;
675 void SvtGraphicFill::setHatchColor( Color aHatchColor )
677 maHatchColor = aHatchColor;
680 void SvtGraphicFill::setGradientType( GradientType aGradType )
682 maGradientType = aGradType;
685 void SvtGraphicFill::setGradient1stColor( Color aColor )
687 maGradient1stColor = aColor;
690 void SvtGraphicFill::setGradient2ndColor( Color aColor )
692 maGradient2ndColor = aColor;
695 void SvtGraphicFill::setGradientStepCount( int aCount )
697 maGradientStepCount = aCount;
700 void SvtGraphicFill::setGraphic( const Graphic& rGraphic )
702 maFillGraphic = rGraphic;
705 SvStream& operator<<( SvStream& rOStm, const SvtGraphicFill& rClass )
707 VersionCompat aCompat( rOStm, STREAM_WRITE, 1 );
709 rClass.maPath.Write( rOStm );
710 rOStm << rClass.maFillColor;
711 rOStm << rClass.mfTransparency;
712 UINT16 nTmp = sal::static_int_cast<UINT16>( rClass.maFillRule );
713 rOStm << nTmp;
714 nTmp = sal::static_int_cast<UINT16>( rClass.maFillType );
715 rOStm << nTmp;
716 int i;
717 for(i=0; i<SvtGraphicFill::Transform::MatrixSize; ++i)
718 rOStm << rClass.maFillTransform.matrix[i];
719 nTmp = rClass.mbTiling;
720 rOStm << nTmp;
721 nTmp = sal::static_int_cast<UINT16>( rClass.maHatchType );
722 rOStm << nTmp;
723 rOStm << rClass.maHatchColor;
724 nTmp = sal::static_int_cast<UINT16>( rClass.maGradientType );
725 rOStm << nTmp;
726 rOStm << rClass.maGradient1stColor;
727 rOStm << rClass.maGradient2ndColor;
728 rOStm << rClass.maGradientStepCount;
729 rOStm << rClass.maFillGraphic;
731 return rOStm;
734 SvStream& operator>>( SvStream& rIStm, SvtGraphicFill& rClass )
736 VersionCompat aCompat( rIStm, STREAM_READ );
738 rClass.maPath.Read( rIStm );
739 rIStm >> rClass.maFillColor;
740 rIStm >> rClass.mfTransparency;
741 UINT16 nTmp;
742 rIStm >> nTmp;
743 rClass.maFillRule = SvtGraphicFill::FillRule( nTmp );
744 rIStm >> nTmp;
745 rClass.maFillType = SvtGraphicFill::FillType( nTmp );
746 int i;
747 for(i=0; i<SvtGraphicFill::Transform::MatrixSize; ++i)
748 rIStm >> rClass.maFillTransform.matrix[i];
749 rIStm >> nTmp;
750 rClass.mbTiling = nTmp;
751 rIStm >> nTmp;
752 rClass.maHatchType = SvtGraphicFill::HatchType( nTmp );
753 rIStm >> rClass.maHatchColor;
754 rIStm >> nTmp;
755 rClass.maGradientType = SvtGraphicFill::GradientType( nTmp );
756 rIStm >> rClass.maGradient1stColor;
757 rIStm >> rClass.maGradient2ndColor;
758 rIStm >> rClass.maGradientStepCount;
759 rIStm >> rClass.maFillGraphic;
761 return rIStm;