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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_drawinglayer.hxx"
26 
27 #include <drawinglayer/primitive2d/fillhatchprimitive2d.hxx>
28 #include <drawinglayer/texture/texture.hxx>
29 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
30 #include <basegfx/polygon/b2dpolygontools.hxx>
31 #include <basegfx/polygon/b2dpolygon.hxx>
32 #include <basegfx/tools/canvastools.hxx>
33 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
34 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
35 #include <drawinglayer/geometry/viewinformation2d.hxx>
36 
37 //////////////////////////////////////////////////////////////////////////////
38 
39 using namespace com::sun::star;
40 
41 //////////////////////////////////////////////////////////////////////////////
42 
43 namespace drawinglayer
44 {
45 	namespace primitive2d
46 	{
47 		Primitive2DSequence FillHatchPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
48 		{
49 		    Primitive2DSequence aRetval;
50 
51             if(!getFillHatch().isDefault())
52             {
53 			    // create hatch
54 			    const basegfx::BColor aHatchColor(getFillHatch().getColor());
55 			    const double fAngle(getFillHatch().getAngle());
56 			    ::std::vector< basegfx::B2DHomMatrix > aMatrices;
57                 double fDistance(getFillHatch().getDistance());
58                 const bool bAdaptDistance(0 != getFillHatch().getMinimalDiscreteDistance());
59 
60                 // #120230# evtl. adapt distance
61                 if(bAdaptDistance)
62                 {
63                     const double fDiscreteDistance(getFillHatch().getDistance() / getDiscreteUnit());
64 
65                     if(fDiscreteDistance < (double)getFillHatch().getMinimalDiscreteDistance())
66                     {
67                         fDistance = (double)getFillHatch().getMinimalDiscreteDistance() * getDiscreteUnit();
68                     }
69                 }
70 
71 			    // get hatch transformations
72 			    switch(getFillHatch().getStyle())
73 			    {
74 				    case attribute::HATCHSTYLE_TRIPLE:
75 				    {
76 					    // rotated 45 degrees
77 					    texture::GeoTexSvxHatch aHatch(getObjectRange(), fDistance, fAngle - F_PI4);
78 					    aHatch.appendTransformations(aMatrices);
79 
80 					    // fall-through by purpose
81 				    }
82 				    case attribute::HATCHSTYLE_DOUBLE:
83 				    {
84 					    // rotated 90 degrees
85 					    texture::GeoTexSvxHatch aHatch(getObjectRange(), fDistance, fAngle - F_PI2);
86 					    aHatch.appendTransformations(aMatrices);
87 
88 					    // fall-through by purpose
89 				    }
90 				    case attribute::HATCHSTYLE_SINGLE:
91 				    {
92 					    // angle as given
93 					    texture::GeoTexSvxHatch aHatch(getObjectRange(), fDistance, fAngle);
94 					    aHatch.appendTransformations(aMatrices);
95 				    }
96 			    }
97 
98 			    // prepare return value
99 			    const bool bFillBackground(getFillHatch().isFillBackground());
100 			    aRetval.realloc(bFillBackground ? aMatrices.size() + 1L : aMatrices.size());
101 
102 			    // evtl. create filled background
103 			    if(bFillBackground)
104 			    {
105 				    // create primitive for background
106 				    const Primitive2DReference xRef(
107                         new PolyPolygonColorPrimitive2D(
108                             basegfx::B2DPolyPolygon(
109                                 basegfx::tools::createPolygonFromRect(getObjectRange())), getBColor()));
110 				    aRetval[0] = xRef;
111 			    }
112 
113 			    // create primitives
114 			    const basegfx::B2DPoint aStart(0.0, 0.0);
115 			    const basegfx::B2DPoint aEnd(1.0, 0.0);
116 
117 			    for(sal_uInt32 a(0L); a < aMatrices.size(); a++)
118 			    {
119 				    const basegfx::B2DHomMatrix& rMatrix = aMatrices[a];
120 				    basegfx::B2DPolygon aNewLine;
121 
122 				    aNewLine.append(rMatrix * aStart);
123 				    aNewLine.append(rMatrix * aEnd);
124 
125 				    // create hairline
126 				    const Primitive2DReference xRef(new PolygonHairlinePrimitive2D(aNewLine, aHatchColor));
127 				    aRetval[bFillBackground ? (a + 1) : a] = xRef;
128 			    }
129             }
130 
131             return aRetval;
132 		}
133 
134 		FillHatchPrimitive2D::FillHatchPrimitive2D(
135 			const basegfx::B2DRange& rObjectRange,
136 			const basegfx::BColor& rBColor,
137 			const attribute::FillHatchAttribute& rFillHatch)
138 		:	DiscreteMetricDependentPrimitive2D(),
139 			maObjectRange(rObjectRange),
140 			maFillHatch(rFillHatch),
141 			maBColor(rBColor)
142 		{
143 		}
144 
145 		bool FillHatchPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
146 		{
147 			if(DiscreteMetricDependentPrimitive2D::operator==(rPrimitive))
148 			{
149 				const FillHatchPrimitive2D& rCompare = (FillHatchPrimitive2D&)rPrimitive;
150 
151 				return (getObjectRange() == rCompare.getObjectRange()
152 					&& getFillHatch() == rCompare.getFillHatch()
153 					&& getBColor() == rCompare.getBColor());
154 			}
155 
156 			return false;
157 		}
158 
159 		basegfx::B2DRange FillHatchPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
160 		{
161 			// return ObjectRange
162 			return getObjectRange();
163 		}
164 
165 		Primitive2DSequence FillHatchPrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
166         {
167 			::osl::MutexGuard aGuard( m_aMutex );
168             bool bAdaptDistance(0 != getFillHatch().getMinimalDiscreteDistance());
169 
170             if(bAdaptDistance)
171             {
172                 // behave view-dependent
173                 return DiscreteMetricDependentPrimitive2D::get2DDecomposition(rViewInformation);
174             }
175             else
176             {
177                 // behave view-independent
178                 return BufferedDecompositionPrimitive2D::get2DDecomposition(rViewInformation);
179             }
180         }
181 
182         // provide unique ID
183 		ImplPrimitrive2DIDBlock(FillHatchPrimitive2D, PRIMITIVE2D_ID_FILLHATCHPRIMITIVE2D)
184 
185 	} // end of namespace primitive2d
186 } // end of namespace drawinglayer
187 
188 //////////////////////////////////////////////////////////////////////////////
189 // eof
190