1 /*************************************************************************
2  *
3  *  OpenOffice.org - a multi-platform office productivity suite
4  *
5  *  $RCSfile: UnifiedTransparencePrimitive2D.cxx,v $
6  *
7  *  $Revision: 1.5 $
8  *
9  *  last change: $Author: aw $ $Date: 2008-05-27 14:11:20 $
10  *
11  *  The Contents of this file are made available subject to
12  *  the terms of GNU Lesser General Public License Version 2.1.
13  *
14  *
15  *    GNU Lesser General Public License Version 2.1
16  *    =============================================
17  *    Copyright 2005 by Sun Microsystems, Inc.
18  *    901 San Antonio Road, Palo Alto, CA 94303, USA
19  *
20  *    This library is free software; you can redistribute it and/or
21  *    modify it under the terms of the GNU Lesser General Public
22  *    License version 2.1, as published by the Free Software Foundation.
23  *
24  *    This library is distributed in the hope that it will be useful,
25  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27  *    Lesser General Public License for more details.
28  *
29  *    You should have received a copy of the GNU Lesser General Public
30  *    License along with this library; if not, write to the Free Software
31  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32  *    MA  02111-1307  USA
33  *
34  ************************************************************************/
35 
36 // MARKER(update_precomp.py): autogen include statement, do not remove
37 #include "precompiled_drawinglayer.hxx"
38 
39 #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
40 #include <basegfx/polygon/b2dpolygon.hxx>
41 #include <basegfx/polygon/b2dpolygontools.hxx>
42 #include <basegfx/color/bcolor.hxx>
43 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
44 #include <drawinglayer/primitive2d/transparenceprimitive2d.hxx>
45 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
46 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
47 
48 //////////////////////////////////////////////////////////////////////////////
49 
50 using namespace com::sun::star;
51 
52 //////////////////////////////////////////////////////////////////////////////
53 
54 namespace drawinglayer
55 {
56 	namespace primitive2d
57 	{
58 		UnifiedTransparencePrimitive2D::UnifiedTransparencePrimitive2D(
59 			const Primitive2DSequence& rChildren,
60 			double fTransparence)
61 		:	GroupPrimitive2D(rChildren),
62 			mfTransparence(fTransparence)
63 		{
64 		}
65 
66 		bool UnifiedTransparencePrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
67 		{
68 			if(GroupPrimitive2D::operator==(rPrimitive))
69 			{
70 				const UnifiedTransparencePrimitive2D& rCompare = (UnifiedTransparencePrimitive2D&)rPrimitive;
71 
72 				return (getTransparence() == rCompare.getTransparence());
73 			}
74 
75 			return false;
76 		}
77 
78 		basegfx::B2DRange UnifiedTransparencePrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const
79         {
80             // do not use the fallback to decomposition here since for a correct BoundRect we also
81             // need invisible (1.0 == getTransparence()) geometry; these would be deleted in the decomposition
82 			return getB2DRangeFromPrimitive2DSequence(getChildren(), rViewInformation);
83         }
84 
85 		Primitive2DSequence UnifiedTransparencePrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
86 		{
87 			if(0.0 == getTransparence())
88 			{
89 				// no transparence used, so just use the content
90 				return getChildren();
91 			}
92 			else if(getTransparence() > 0.0 && getTransparence() < 1.0)
93 			{
94                 // The idea is to create a TransparencePrimitive2D with transparent content using a fill color
95                 // corresponding to the transparence value. Problem is that in most systems, the right
96                 // and bottom pixel array is not filled when filling polygons, thus this would not
97                 // always produce a complete transparent bitmap. There are some solutions:
98                 //
99                 // - Grow the used polygon range by one discrete unit in X and Y. This
100                 // will make the decomposition view-dependent.
101                 //
102                 // - For all filled polygon renderings, dra wthe polygon outline extra. This
103                 // would lead to unwanted side effects when using concatenated polygons.
104                 //
105                 // - At this decomposition, add a filled polygon and a hairline polygon. This
106                 // solution stays view-independent.
107                 //
108                 // I will take the last one here. The small overhead of two primitives will only be
109                 // used when UnifiedTransparencePrimitive2D is not handled directly.
110 				const basegfx::B2DRange aPolygonRange(getB2DRangeFromPrimitive2DSequence(getChildren(), rViewInformation));
111 				const basegfx::B2DPolygon aPolygon(basegfx::tools::createPolygonFromRect(aPolygonRange));
112 				const basegfx::BColor aGray(getTransparence(), getTransparence(), getTransparence());
113 				Primitive2DSequence aTransparenceContent(2);
114 
115                 aTransparenceContent[0] = Primitive2DReference(new PolyPolygonColorPrimitive2D(basegfx::B2DPolyPolygon(aPolygon), aGray));
116                 aTransparenceContent[1] = Primitive2DReference(new PolygonHairlinePrimitive2D(aPolygon, aGray));
117 
118 				// create sub-transparence group with a gray-colored rectangular fill polygon
119 				const Primitive2DReference xRefB(new TransparencePrimitive2D(getChildren(), aTransparenceContent));
120 				return Primitive2DSequence(&xRefB, 1L);
121 			}
122 			else
123 			{
124 				// completely transparent or invalid definition, add nothing
125 				return Primitive2DSequence();
126 			}
127 		}
128 
129 		// provide unique ID
130 		ImplPrimitrive2DIDBlock(UnifiedTransparencePrimitive2D, PRIMITIVE2D_ID_UNIFIEDTRANSPARENCEPRIMITIVE2D)
131 
132 	} // end of namespace primitive2d
133 } // end of namespace drawinglayer
134 
135 //////////////////////////////////////////////////////////////////////////////
136 // eof
137