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