xref: /AOO41X/main/drawinglayer/source/primitive2d/fillgradientprimitive2d.cxx (revision 8809db7a87f97847b57a57f4cd2b0104b2b83182)
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/fillgradientprimitive2d.hxx>
28 #include <basegfx/polygon/b2dpolygon.hxx>
29 #include <basegfx/polygon/b2dpolygontools.hxx>
30 #include <drawinglayer/texture/texture.hxx>
31 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
32 #include <basegfx/tools/canvastools.hxx>
33 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
34 
35 //////////////////////////////////////////////////////////////////////////////
36 
37 using namespace com::sun::star;
38 
39 //////////////////////////////////////////////////////////////////////////////
40 
41 namespace drawinglayer
42 {
43     namespace primitive2d
44     {
45         void FillGradientPrimitive2D::generateMatricesAndColors(
46             std::vector< basegfx::B2DHomMatrix >& rMatrices,
47             std::vector< basegfx::BColor >& rColors) const
48         {
49             rMatrices.clear();
50             rColors.clear();
51 
52             // make sure steps is not too high/low
53             const basegfx::BColor aStart(getFillGradient().getStartColor());
54             const basegfx::BColor aEnd(getFillGradient().getEndColor());
55             const sal_uInt32 nMaxSteps(sal_uInt32((aStart.getMaximumDistance(aEnd) * 127.5) + 0.5));
56             sal_uInt32 nSteps(getFillGradient().getSteps());
57 
58             if(nSteps == 0)
59             {
60                 nSteps = nMaxSteps;
61             }
62 
63             if(nSteps < 2)
64             {
65                 nSteps = 2;
66             }
67 
68             if(nSteps > nMaxSteps)
69             {
70                 nSteps = nMaxSteps;
71             }
72 
73             switch(getFillGradient().getStyle())
74             {
75                 case attribute::GRADIENTSTYLE_LINEAR:
76                 {
77                     texture::GeoTexSvxGradientLinear aGradient(getObjectRange(), aStart, aEnd, nSteps, getFillGradient().getBorder(), getFillGradient().getAngle());
78                     aGradient.appendTransformations(rMatrices);
79                     aGradient.appendColors(rColors);
80                     break;
81                 }
82                 case attribute::GRADIENTSTYLE_AXIAL:
83                 {
84                     texture::GeoTexSvxGradientAxial aGradient(getObjectRange(), aStart, aEnd, nSteps, getFillGradient().getBorder(), getFillGradient().getAngle());
85                     aGradient.appendTransformations(rMatrices);
86                     aGradient.appendColors(rColors);
87                     break;
88                 }
89                 case attribute::GRADIENTSTYLE_RADIAL:
90                 {
91                     texture::GeoTexSvxGradientRadial aGradient(getObjectRange(), aStart, aEnd, nSteps, getFillGradient().getBorder(), getFillGradient().getOffsetX(), getFillGradient().getOffsetY());
92                     aGradient.appendTransformations(rMatrices);
93                     aGradient.appendColors(rColors);
94                     break;
95                 }
96                 case attribute::GRADIENTSTYLE_ELLIPTICAL:
97                 {
98                     texture::GeoTexSvxGradientElliptical aGradient(getObjectRange(), aStart, aEnd, nSteps, getFillGradient().getBorder(), getFillGradient().getOffsetX(), getFillGradient().getOffsetY(), getFillGradient().getAngle());
99                     aGradient.appendTransformations(rMatrices);
100                     aGradient.appendColors(rColors);
101                     break;
102                 }
103                 case attribute::GRADIENTSTYLE_SQUARE:
104                 {
105                     texture::GeoTexSvxGradientSquare aGradient(getObjectRange(), aStart, aEnd, nSteps, getFillGradient().getBorder(), getFillGradient().getOffsetX(), getFillGradient().getOffsetY(), getFillGradient().getAngle());
106                     aGradient.appendTransformations(rMatrices);
107                     aGradient.appendColors(rColors);
108                     break;
109                 }
110                 case attribute::GRADIENTSTYLE_RECT:
111                 {
112                     texture::GeoTexSvxGradientRect aGradient(getObjectRange(), aStart, aEnd, nSteps, getFillGradient().getBorder(), getFillGradient().getOffsetX(), getFillGradient().getOffsetY(), getFillGradient().getAngle());
113                     aGradient.appendTransformations(rMatrices);
114                     aGradient.appendColors(rColors);
115                     break;
116                 }
117             }
118         }
119 
120         Primitive2DSequence FillGradientPrimitive2D::createOverlappingFill(
121             const std::vector< basegfx::B2DHomMatrix >& rMatrices,
122             const std::vector< basegfx::BColor >& rColors,
123             const basegfx::B2DPolygon& rUnitPolygon) const
124         {
125             // prepare return value
126             Primitive2DSequence aRetval(rColors.size() ? rMatrices.size() + 1 : rMatrices.size());
127 
128             // create solid fill with start color
129             if(rColors.size())
130             {
131                 // create primitive
132                 const Primitive2DReference xRef(
133                     new PolyPolygonColorPrimitive2D(
134                         basegfx::B2DPolyPolygon(basegfx::tools::createPolygonFromRect(getObjectRange())),
135                         rColors[0]));
136                 aRetval[0] = xRef;
137             }
138 
139             // create solid fill steps
140             for(sal_uInt32 a(0); a < rMatrices.size(); a++)
141             {
142                 // create part polygon
143                 basegfx::B2DPolygon aNewPoly(rUnitPolygon);
144                 aNewPoly.transform(rMatrices[a]);
145 
146                 // create solid fill
147                 const Primitive2DReference xRef(
148                     new PolyPolygonColorPrimitive2D(
149                         basegfx::B2DPolyPolygon(aNewPoly),
150                         rColors[a + 1]));
151                 aRetval[a + 1] = xRef;
152             }
153 
154             return aRetval;
155         }
156 
157         Primitive2DSequence FillGradientPrimitive2D::createNonOverlappingFill(
158             const std::vector< basegfx::B2DHomMatrix >& rMatrices,
159             const std::vector< basegfx::BColor >& rColors,
160             const basegfx::B2DPolygon& rUnitPolygon) const
161         {
162             // prepare return value
163             Primitive2DSequence aRetval;
164             const sal_uInt32 nMatricesSize(rMatrices.size());
165 
166             if(nMatricesSize)
167             {
168                 basegfx::B2DPolygon aOuterPoly(rUnitPolygon);
169                 aOuterPoly.transform(rMatrices[0]);
170                 basegfx::B2DPolyPolygon aCombinedPolyPoly(aOuterPoly);
171                 const sal_uInt32 nEntryCount(rColors.size() ? rMatrices.size() + 1 : rMatrices.size());
172                 sal_uInt32 nIndex(0);
173 
174                 aRetval.realloc(nEntryCount);
175 
176                 if(rColors.size())
177                 {
178                     basegfx::B2DRange aOuterPolyRange(aOuterPoly.getB2DRange());
179                     aOuterPolyRange.expand(getObjectRange());
180                     aCombinedPolyPoly.append(basegfx::tools::createPolygonFromRect(aOuterPolyRange));
181                     aRetval[nIndex++] = Primitive2DReference(new PolyPolygonColorPrimitive2D(aCombinedPolyPoly, rColors[0]));
182                     aCombinedPolyPoly = basegfx::B2DPolyPolygon(aOuterPoly);
183                 }
184 
185                 for(sal_uInt32 a(1); a < nMatricesSize - 1; a++)
186                 {
187                     basegfx::B2DPolygon aInnerPoly(rUnitPolygon);
188                     aInnerPoly.transform(rMatrices[a]);
189                     aCombinedPolyPoly.append(aInnerPoly);
190                     aRetval[nIndex++] = Primitive2DReference(new PolyPolygonColorPrimitive2D(aCombinedPolyPoly, rColors[a]));
191                     aCombinedPolyPoly = basegfx::B2DPolyPolygon(aInnerPoly);
192                 }
193 
194                 if(rColors.size())
195                 {
196                     aRetval[nIndex] = Primitive2DReference(new PolyPolygonColorPrimitive2D(
197                         aCombinedPolyPoly, rColors[rColors.size() - 1]));
198                 }
199             }
200 
201             return aRetval;
202         }
203 
204         Primitive2DSequence FillGradientPrimitive2D::createFill(bool bOverlapping) const
205         {
206             // prepare shape of the Unit Polygon
207             basegfx::B2DPolygon aUnitPolygon;
208 
209             if(attribute::GRADIENTSTYLE_RADIAL == getFillGradient().getStyle()
210                 || attribute::GRADIENTSTYLE_ELLIPTICAL == getFillGradient().getStyle())
211             {
212                 aUnitPolygon = basegfx::tools::createPolygonFromCircle(
213                     basegfx::B2DPoint(0,0), 1);
214             }
215             else if(attribute::GRADIENTSTYLE_LINEAR == maFillGradient.getStyle())
216             {
217                 aUnitPolygon = basegfx::tools::createPolygonFromRect(basegfx::B2DRange(0, 0, 1, 1));
218             }
219             else
220             {
221                 aUnitPolygon = basegfx::tools::createPolygonFromRect(basegfx::B2DRange(-1, -1, 1, 1));
222             }
223 
224             // get the transform matrices and colors (where colors
225             // will have one more entry that matrices)
226             std::vector< basegfx::B2DHomMatrix > aMatrices;
227             std::vector< basegfx::BColor > aColors;
228             generateMatricesAndColors(aMatrices, aColors);
229 
230             if(bOverlapping)
231             {
232                 return createOverlappingFill(aMatrices, aColors, aUnitPolygon);
233             }
234             else
235             {
236                 return createNonOverlappingFill(aMatrices, aColors, aUnitPolygon);
237             }
238         }
239 
240         Primitive2DSequence FillGradientPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
241         {
242             // default creates overlapping fill which works with AntiAliasing and without.
243             // The non-overlapping version does not create single filled polygons, but
244             // PolyPolygons where each one describes a 'ring' for the gradient such
245             // that the rings will not overlap. This is useful fir the old XOR-paint
246             // 'trick' of VCL which is recorded in Metafiles; so this version may be
247             // used from the MetafilePrimitive2D in it's decomposition.
248 
249             if(!getFillGradient().isDefault())
250             {
251                 return createFill(true);
252             }
253             else
254             {
255                 return Primitive2DSequence();
256             }
257         }
258 
259         FillGradientPrimitive2D::FillGradientPrimitive2D(
260             const basegfx::B2DRange& rObjectRange,
261             const attribute::FillGradientAttribute& rFillGradient)
262         :   BufferedDecompositionPrimitive2D(),
263             maObjectRange(rObjectRange),
264             maFillGradient(rFillGradient)
265         {
266         }
267 
268         bool FillGradientPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
269         {
270             if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
271             {
272                 const FillGradientPrimitive2D& rCompare = (FillGradientPrimitive2D&)rPrimitive;
273 
274                 return (getObjectRange() == rCompare.getObjectRange()
275                     && getFillGradient() == rCompare.getFillGradient());
276             }
277 
278             return false;
279         }
280 
281         basegfx::B2DRange FillGradientPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
282         {
283             // return ObjectRange
284             return getObjectRange();
285         }
286 
287         // provide unique ID
288         ImplPrimitrive2DIDBlock(FillGradientPrimitive2D, PRIMITIVE2D_ID_FILLGRADIENTPRIMITIVE2D)
289 
290     } // end of namespace primitive2d
291 } // end of namespace drawinglayer
292 
293 //////////////////////////////////////////////////////////////////////////////
294 // eof
295