xref: /AOO41X/main/drawinglayer/source/primitive2d/unifiedtransparenceprimitive2d.cxx (revision 4bfbcde8d64cc5d114df10dce4a9ed79eff32278)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_drawinglayer.hxx"
24 
25 #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
26 #include <basegfx/polygon/b2dpolygon.hxx>
27 #include <basegfx/polygon/b2dpolygontools.hxx>
28 #include <basegfx/color/bcolor.hxx>
29 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
30 #include <drawinglayer/primitive2d/transparenceprimitive2d.hxx>
31 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
32 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
33 
34 //////////////////////////////////////////////////////////////////////////////
35 
36 using namespace com::sun::star;
37 
38 //////////////////////////////////////////////////////////////////////////////
39 
40 namespace drawinglayer
41 {
42     namespace primitive2d
43     {
UnifiedTransparencePrimitive2D(const Primitive2DSequence & rChildren,double fTransparence)44         UnifiedTransparencePrimitive2D::UnifiedTransparencePrimitive2D(
45             const Primitive2DSequence& rChildren,
46             double fTransparence)
47         :   GroupPrimitive2D(rChildren),
48             mfTransparence(fTransparence)
49         {
50         }
51 
operator ==(const BasePrimitive2D & rPrimitive) const52         bool UnifiedTransparencePrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
53         {
54             if(GroupPrimitive2D::operator==(rPrimitive))
55             {
56                 const UnifiedTransparencePrimitive2D& rCompare = (UnifiedTransparencePrimitive2D&)rPrimitive;
57 
58                 return (getTransparence() == rCompare.getTransparence());
59             }
60 
61             return false;
62         }
63 
getB2DRange(const geometry::ViewInformation2D & rViewInformation) const64         basegfx::B2DRange UnifiedTransparencePrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const
65         {
66             // do not use the fallback to decomposition here since for a correct BoundRect we also
67             // need invisible (1.0 == getTransparence()) geometry; these would be deleted in the decomposition
68             return getB2DRangeFromPrimitive2DSequence(getChildren(), rViewInformation);
69         }
70 
get2DDecomposition(const geometry::ViewInformation2D & rViewInformation) const71         Primitive2DSequence UnifiedTransparencePrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
72         {
73             if(0.0 == getTransparence())
74             {
75                 // no transparence used, so just use the content
76                 return getChildren();
77             }
78             else if(getTransparence() > 0.0 && getTransparence() < 1.0)
79             {
80                 // The idea is to create a TransparencePrimitive2D with transparent content using a fill color
81                 // corresponding to the transparence value. Problem is that in most systems, the right
82                 // and bottom pixel array is not filled when filling polygons, thus this would not
83                 // always produce a complete transparent bitmap. There are some solutions:
84                 //
85                 // - Grow the used polygon range by one discrete unit in X and Y. This
86                 // will make the decomposition view-dependent.
87                 //
88                 // - For all filled polygon renderings, dra wthe polygon outline extra. This
89                 // would lead to unwanted side effects when using concatenated polygons.
90                 //
91                 // - At this decomposition, add a filled polygon and a hairline polygon. This
92                 // solution stays view-independent.
93                 //
94                 // I will take the last one here. The small overhead of two primitives will only be
95                 // used when UnifiedTransparencePrimitive2D is not handled directly.
96                 const basegfx::B2DRange aPolygonRange(getB2DRangeFromPrimitive2DSequence(getChildren(), rViewInformation));
97                 const basegfx::B2DPolygon aPolygon(basegfx::tools::createPolygonFromRect(aPolygonRange));
98                 const basegfx::BColor aGray(getTransparence(), getTransparence(), getTransparence());
99                 Primitive2DSequence aTransparenceContent(2);
100 
101                 aTransparenceContent[0] = Primitive2DReference(new PolyPolygonColorPrimitive2D(basegfx::B2DPolyPolygon(aPolygon), aGray));
102                 aTransparenceContent[1] = Primitive2DReference(new PolygonHairlinePrimitive2D(aPolygon, aGray));
103 
104                 // create sub-transparence group with a gray-colored rectangular fill polygon
105                 const Primitive2DReference xRefB(new TransparencePrimitive2D(getChildren(), aTransparenceContent));
106                 return Primitive2DSequence(&xRefB, 1L);
107             }
108             else
109             {
110                 // completely transparent or invalid definition, add nothing
111                 return Primitive2DSequence();
112             }
113         }
114 
115         // provide unique ID
116         ImplPrimitrive2DIDBlock(UnifiedTransparencePrimitive2D, PRIMITIVE2D_ID_UNIFIEDTRANSPARENCEPRIMITIVE2D)
117 
118     } // end of namespace primitive2d
119 } // end of namespace drawinglayer
120 
121 //////////////////////////////////////////////////////////////////////////////
122 // eof
123