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     {
create2DDecomposition(const geometry::ViewInformation2D &) const47         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(
78                             getDefinitionRange(),
79                             getOutputRange(),
80                             fDistance,
81                             fAngle - F_PI4);
82 
83                         aHatch.appendTransformations(aMatrices);
84 
85                         // fall-through by purpose
86                     }
87                     case attribute::HATCHSTYLE_DOUBLE:
88                     {
89                         // rotated 90 degrees
90                         texture::GeoTexSvxHatch aHatch(
91                             getDefinitionRange(),
92                             getOutputRange(),
93                             fDistance,
94                             fAngle - F_PI2);
95 
96                         aHatch.appendTransformations(aMatrices);
97 
98                         // fall-through by purpose
99                     }
100                     case attribute::HATCHSTYLE_SINGLE:
101                     {
102                         // angle as given
103                         texture::GeoTexSvxHatch aHatch(
104                             getDefinitionRange(),
105                             getOutputRange(),
106                             fDistance,
107                             fAngle);
108 
109                         aHatch.appendTransformations(aMatrices);
110                     }
111                 }
112 
113                 // prepare return value
114                 const bool bFillBackground(getFillHatch().isFillBackground());
115                 aRetval.realloc(bFillBackground ? aMatrices.size() + 1L : aMatrices.size());
116 
117                 // evtl. create filled background
118                 if(bFillBackground)
119                 {
120                     // create primitive for background
121                     const Primitive2DReference xRef(
122                         new PolyPolygonColorPrimitive2D(
123                             basegfx::B2DPolyPolygon(
124                                 basegfx::tools::createPolygonFromRect(getOutputRange())), getBColor()));
125                     aRetval[0] = xRef;
126                 }
127 
128                 // create primitives
129                 const basegfx::B2DPoint aStart(0.0, 0.0);
130                 const basegfx::B2DPoint aEnd(1.0, 0.0);
131 
132                 for(sal_uInt32 a(0L); a < aMatrices.size(); a++)
133                 {
134                     const basegfx::B2DHomMatrix& rMatrix = aMatrices[a];
135                     basegfx::B2DPolygon aNewLine;
136 
137                     aNewLine.append(rMatrix * aStart);
138                     aNewLine.append(rMatrix * aEnd);
139 
140                     // create hairline
141                     const Primitive2DReference xRef(new PolygonHairlinePrimitive2D(aNewLine, aHatchColor));
142                     aRetval[bFillBackground ? (a + 1) : a] = xRef;
143                 }
144             }
145 
146             return aRetval;
147         }
148 
FillHatchPrimitive2D(const basegfx::B2DRange & rOutputRange,const basegfx::BColor & rBColor,const attribute::FillHatchAttribute & rFillHatch)149         FillHatchPrimitive2D::FillHatchPrimitive2D(
150             const basegfx::B2DRange& rOutputRange,
151             const basegfx::BColor& rBColor,
152             const attribute::FillHatchAttribute& rFillHatch)
153         :   DiscreteMetricDependentPrimitive2D(),
154             maOutputRange(rOutputRange),
155             maDefinitionRange(rOutputRange),
156             maFillHatch(rFillHatch),
157             maBColor(rBColor)
158         {
159         }
160 
FillHatchPrimitive2D(const basegfx::B2DRange & rOutputRange,const basegfx::B2DRange & rDefinitionRange,const basegfx::BColor & rBColor,const attribute::FillHatchAttribute & rFillHatch)161         FillHatchPrimitive2D::FillHatchPrimitive2D(
162             const basegfx::B2DRange& rOutputRange,
163             const basegfx::B2DRange& rDefinitionRange,
164             const basegfx::BColor& rBColor,
165             const attribute::FillHatchAttribute& rFillHatch)
166         :   DiscreteMetricDependentPrimitive2D(),
167             maOutputRange(rOutputRange),
168             maDefinitionRange(rDefinitionRange),
169             maFillHatch(rFillHatch),
170             maBColor(rBColor)
171         {
172         }
173 
operator ==(const BasePrimitive2D & rPrimitive) const174         bool FillHatchPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
175         {
176             if(DiscreteMetricDependentPrimitive2D::operator==(rPrimitive))
177             {
178                 const FillHatchPrimitive2D& rCompare = (FillHatchPrimitive2D&)rPrimitive;
179 
180                 return (getOutputRange() == rCompare.getOutputRange()
181                     && getDefinitionRange() == rCompare.getDefinitionRange()
182                     && getFillHatch() == rCompare.getFillHatch()
183                     && getBColor() == rCompare.getBColor());
184             }
185 
186             return false;
187         }
188 
getB2DRange(const geometry::ViewInformation2D &) const189         basegfx::B2DRange FillHatchPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
190         {
191             // return the geometrically visible area
192             return getOutputRange();
193         }
194 
get2DDecomposition(const geometry::ViewInformation2D & rViewInformation) const195         Primitive2DSequence FillHatchPrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
196         {
197             ::osl::MutexGuard aGuard( m_aMutex );
198             bool bAdaptDistance(0 != getFillHatch().getMinimalDiscreteDistance());
199 
200             if(bAdaptDistance)
201             {
202                 // behave view-dependent
203                 return DiscreteMetricDependentPrimitive2D::get2DDecomposition(rViewInformation);
204             }
205             else
206             {
207                 // behave view-independent
208                 return BufferedDecompositionPrimitive2D::get2DDecomposition(rViewInformation);
209             }
210         }
211 
212         // provide unique ID
213         ImplPrimitrive2DIDBlock(FillHatchPrimitive2D, PRIMITIVE2D_ID_FILLHATCHPRIMITIVE2D)
214 
215     } // end of namespace primitive2d
216 } // end of namespace drawinglayer
217 
218 //////////////////////////////////////////////////////////////////////////////
219 // eof
220