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 #include "precompiled_svx.hxx"
23 #include <svx/sdr/primitive2d/sdrgrafprimitive2d.hxx>
24 #include <drawinglayer/primitive2d/graphicprimitive2d.hxx>
25 #include <basegfx/polygon/b2dpolygontools.hxx>
26 #include <svx/sdr/primitive2d/sdrdecompositiontools.hxx>
27 #include <drawinglayer/primitive2d/groupprimitive2d.hxx>
28 #include <svx/sdr/primitive2d/svx_primitivetypes2d.hxx>
29 #include <drawinglayer/primitive2d/sdrdecompositiontools2d.hxx>
30 #include <basegfx/polygon/b2dpolygon.hxx>
31 #include <basegfx/matrix/b2dhommatrixtools.hxx>
32 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
33 
34 //////////////////////////////////////////////////////////////////////////////
35 
36 namespace drawinglayer
37 {
38 	namespace primitive2d
39 	{
40 		Primitive2DSequence SdrGrafPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*aViewInformation*/) const
41 		{
42 			Primitive2DSequence  aRetval;
43 
44 			// create unit outline polygon
45 			basegfx::B2DPolygon aUnitOutline(basegfx::tools::createUnitPolygon());
46 
47 			// add fill, but only when graphic ist transparent
48 			if(!getSdrLFSTAttribute().getFill().isDefault() && isTransparent())
49 			{
50 				appendPrimitive2DReferenceToPrimitive2DSequence(aRetval,
51 					createPolyPolygonFillPrimitive(
52                         basegfx::B2DPolyPolygon(aUnitOutline),
53                         getTransform(),
54                         getSdrLFSTAttribute().getFill(),
55                         getSdrLFSTAttribute().getFillFloatTransGradient()));
56 			}
57 
58 			// add line
59 			if(!getSdrLFSTAttribute().getLine().isDefault())
60 			{
61 				// if line width is given, polygon needs to be grown by half of it to make the
62 				// outline to be outside of the bitmap
63 				if(0.0 != getSdrLFSTAttribute().getLine().getWidth())
64 				{
65 					// decompose to get scale
66 					basegfx::B2DVector aScale, aTranslate;
67 					double fRotate, fShearX;
68 					getTransform().decompose(aScale, aTranslate, fRotate, fShearX);
69 
70 					// create expanded range (add relative half line width to unit rectangle)
71 					double fHalfLineWidth(getSdrLFSTAttribute().getLine().getWidth() * 0.5);
72 					double fScaleX(0.0 != aScale.getX() ? fHalfLineWidth / fabs(aScale.getX()) : 1.0);
73 					double fScaleY(0.0 != aScale.getY() ? fHalfLineWidth / fabs(aScale.getY()) : 1.0);
74 					const basegfx::B2DRange aExpandedRange(-fScaleX, -fScaleY, 1.0 + fScaleX, 1.0 + fScaleY);
75 					basegfx::B2DPolygon aExpandedUnitOutline(basegfx::tools::createPolygonFromRect(aExpandedRange));
76 
77 					appendPrimitive2DReferenceToPrimitive2DSequence(aRetval,
78                         createPolygonLinePrimitive(
79                             aExpandedUnitOutline,
80                             getTransform(),
81                             getSdrLFSTAttribute().getLine(),
82 							attribute::SdrLineStartEndAttribute()));
83 				}
84 				else
85 				{
86 					appendPrimitive2DReferenceToPrimitive2DSequence(aRetval,
87                         createPolygonLinePrimitive(
88                             aUnitOutline, getTransform(),
89                             getSdrLFSTAttribute().getLine(),
90 							attribute::SdrLineStartEndAttribute()));
91 				}
92 			}
93 
94 			// add graphic content
95 			if(255L != getGraphicAttr().GetTransparency())
96 			{
97                 // standard graphic fill
98                 const Primitive2DReference xGraphicContentPrimitive(
99                     new GraphicPrimitive2D(
100                         getTransform(),
101                         getGraphicObject(),
102                         getGraphicAttr()));
103 
104                 appendPrimitive2DReferenceToPrimitive2DSequence(aRetval, xGraphicContentPrimitive);
105 			}
106 
107 			// add text
108 			if(!getSdrLFSTAttribute().getText().isDefault())
109 			{
110 				appendPrimitive2DReferenceToPrimitive2DSequence(aRetval,
111                     createTextPrimitive(
112                         basegfx::B2DPolyPolygon(aUnitOutline),
113                         getTransform(),
114 						getSdrLFSTAttribute().getText(),
115                         getSdrLFSTAttribute().getLine(),
116                         false,
117                         false,
118                         false));
119 			}
120 
121 			// add shadow
122 			if(!getSdrLFSTAttribute().getShadow().isDefault())
123 			{
124                 aRetval = createEmbeddedShadowPrimitive(
125                     aRetval,
126                     getSdrLFSTAttribute().getShadow());
127 			}
128 
129 			return aRetval;
130 		}
131 
132 		SdrGrafPrimitive2D::SdrGrafPrimitive2D(
133 			const basegfx::B2DHomMatrix& rTransform,
134 			const attribute::SdrLineFillShadowTextAttribute& rSdrLFSTAttribute,
135 			const GraphicObject& rGraphicObject,
136 			const GraphicAttr& rGraphicAttr)
137 		:	BufferedDecompositionPrimitive2D(),
138 			maTransform(rTransform),
139 			maSdrLFSTAttribute(rSdrLFSTAttribute),
140 			maGraphicObject(rGraphicObject),
141 			maGraphicAttr(rGraphicAttr)
142 		{
143 			// reset some values from GraphicAttr which are part of transformation already
144 			maGraphicAttr.SetRotation(0L);
145 		}
146 
147 		bool SdrGrafPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
148 		{
149 			if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
150 			{
151 				const SdrGrafPrimitive2D& rCompare = (SdrGrafPrimitive2D&)rPrimitive;
152 
153 				return (getTransform() == rCompare.getTransform()
154 					&& getSdrLFSTAttribute() == rCompare.getSdrLFSTAttribute()
155 					&& getGraphicObject() == rCompare.getGraphicObject()
156 					&& getGraphicAttr() == rCompare.getGraphicAttr());
157 			}
158 
159 			return false;
160 		}
161 
162 		bool SdrGrafPrimitive2D::isTransparent() const
163 		{
164 			return ((0L != getGraphicAttr().GetTransparency())
165                 || (getGraphicObject().IsTransparent()));
166 		}
167 
168 		// provide unique ID
169 		ImplPrimitrive2DIDBlock(SdrGrafPrimitive2D, PRIMITIVE2D_ID_SDRGRAFPRIMITIVE2D)
170 
171 	} // end of namespace primitive2d
172 } // end of namespace drawinglayer
173 
174 //////////////////////////////////////////////////////////////////////////////
175 // eof
176