Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <vclhelpergradient.hxx>
21 : #include <basegfx/range/b2drange.hxx>
22 : #include <vcl/outdev.hxx>
23 : #include <basegfx/polygon/b2dpolygon.hxx>
24 : #include <basegfx/polygon/b2dpolypolygontools.hxx>
25 : #include <basegfx/polygon/b2dpolygontools.hxx>
26 : #include <drawinglayer/texture/texture.hxx>
27 :
28 : //////////////////////////////////////////////////////////////////////////////
29 : // support methods for vcl direct gradient renderering
30 :
31 : namespace drawinglayer
32 : {
33 : namespace
34 : {
35 0 : sal_uInt32 impCalcGradientSteps(OutputDevice& rOutDev, sal_uInt32 nSteps, const basegfx::B2DRange& rRange, sal_uInt32 nMaxDist)
36 : {
37 0 : if(nSteps == 0L)
38 : {
39 0 : const Size aSize(rOutDev.LogicToPixel(Size(basegfx::fround(rRange.getWidth()), basegfx::fround(rRange.getHeight()))));
40 0 : nSteps = (aSize.getWidth() + aSize.getHeight()) >> 3L;
41 : }
42 :
43 0 : if(nSteps < 2L)
44 : {
45 0 : nSteps = 2L;
46 : }
47 :
48 0 : if(nSteps > nMaxDist)
49 : {
50 0 : nSteps = nMaxDist;
51 : }
52 :
53 0 : return nSteps;
54 : }
55 :
56 0 : void impDrawGradientToOutDevSimple(
57 : OutputDevice& rOutDev,
58 : const basegfx::B2DPolyPolygon& rTargetForm,
59 : const ::std::vector< basegfx::B2DHomMatrix >& rMatrices,
60 : const ::std::vector< basegfx::BColor >& rColors,
61 : const basegfx::B2DPolygon& rUnitPolygon)
62 : {
63 0 : rOutDev.SetLineColor();
64 :
65 0 : for(sal_uInt32 a(0L); a < rColors.size(); a++)
66 : {
67 : // set correct color
68 0 : const basegfx::BColor aFillColor(rColors[a]);
69 0 : rOutDev.SetFillColor(Color(aFillColor));
70 :
71 0 : if(a)
72 : {
73 0 : if(a - 1L < static_cast< sal_uInt32 >(rMatrices.size()))
74 : {
75 0 : basegfx::B2DPolygon aNewPoly(rUnitPolygon);
76 0 : aNewPoly.transform(rMatrices[a - 1L]);
77 0 : rOutDev.DrawPolygon(aNewPoly);
78 : }
79 : }
80 : else
81 : {
82 0 : rOutDev.DrawPolyPolygon(rTargetForm);
83 : }
84 0 : }
85 0 : }
86 :
87 0 : void impDrawGradientToOutDevComplex(
88 : OutputDevice& rOutDev,
89 : const basegfx::B2DPolyPolygon& rTargetForm,
90 : const ::std::vector< basegfx::B2DHomMatrix >& rMatrices,
91 : const ::std::vector< basegfx::BColor >& rColors,
92 : const basegfx::B2DPolygon& rUnitPolygon)
93 : {
94 0 : PolyPolygon aVclTargetForm(rTargetForm);
95 0 : ::std::vector< Polygon > aVclPolygons;
96 : sal_uInt32 a;
97 :
98 : // remember and set to XOR
99 0 : rOutDev.SetLineColor();
100 0 : rOutDev.Push(PUSH_RASTEROP);
101 0 : rOutDev.SetRasterOp(ROP_XOR);
102 :
103 : // draw gradient PolyPolygons
104 0 : for(a = 0L; a < rMatrices.size(); a++)
105 : {
106 : // create polygon and remember
107 0 : basegfx::B2DPolygon aNewPoly(rUnitPolygon);
108 0 : aNewPoly.transform(rMatrices[a]);
109 0 : aVclPolygons.push_back(Polygon(aNewPoly));
110 :
111 : // set correct color
112 0 : if(rColors.size() > a)
113 : {
114 0 : const basegfx::BColor aFillColor(rColors[a]);
115 0 : rOutDev.SetFillColor(Color(aFillColor));
116 : }
117 :
118 : // create vcl PolyPolygon and draw it
119 0 : if(a)
120 : {
121 0 : PolyPolygon aVclPolyPoly(aVclPolygons[a - 1L]);
122 0 : aVclPolyPoly.Insert(aVclPolygons[a]);
123 0 : rOutDev.DrawPolyPolygon(aVclPolyPoly);
124 : }
125 : else
126 : {
127 0 : PolyPolygon aVclPolyPoly(aVclTargetForm);
128 0 : aVclPolyPoly.Insert(aVclPolygons[0L]);
129 0 : rOutDev.DrawPolyPolygon(aVclPolyPoly);
130 : }
131 0 : }
132 :
133 : // draw last poly in last color
134 0 : if(!rColors.empty())
135 : {
136 0 : const basegfx::BColor aFillColor(rColors[rColors.size() - 1L]);
137 0 : rOutDev.SetFillColor(Color(aFillColor));
138 0 : rOutDev.DrawPolygon(aVclPolygons[aVclPolygons.size() - 1L]);
139 : }
140 :
141 : // draw object form in black and go back to XOR
142 0 : rOutDev.SetFillColor(COL_BLACK);
143 0 : rOutDev.SetRasterOp(ROP_0);
144 0 : rOutDev.DrawPolyPolygon(aVclTargetForm);
145 0 : rOutDev.SetRasterOp(ROP_XOR);
146 :
147 : // draw gradient PolyPolygons again
148 0 : for(a = 0L; a < rMatrices.size(); a++)
149 : {
150 : // set correct color
151 0 : if(rColors.size() > a)
152 : {
153 0 : const basegfx::BColor aFillColor(rColors[a]);
154 0 : rOutDev.SetFillColor(Color(aFillColor));
155 : }
156 :
157 : // create vcl PolyPolygon and draw it
158 0 : if(a)
159 : {
160 0 : PolyPolygon aVclPolyPoly(aVclPolygons[a - 1L]);
161 0 : aVclPolyPoly.Insert(aVclPolygons[a]);
162 0 : rOutDev.DrawPolyPolygon(aVclPolyPoly);
163 : }
164 : else
165 : {
166 0 : PolyPolygon aVclPolyPoly(aVclTargetForm);
167 0 : aVclPolyPoly.Insert(aVclPolygons[0L]);
168 0 : rOutDev.DrawPolyPolygon(aVclPolyPoly);
169 : }
170 : }
171 :
172 : // draw last poly in last color
173 0 : if(!rColors.empty())
174 : {
175 0 : const basegfx::BColor aFillColor(rColors[rColors.size() - 1L]);
176 0 : rOutDev.SetFillColor(Color(aFillColor));
177 0 : rOutDev.DrawPolygon(aVclPolygons[aVclPolygons.size() - 1L]);
178 : }
179 :
180 : // reset drawmode
181 0 : rOutDev.Pop();
182 0 : }
183 : } // end of anonymous namespace
184 : } // end of namespace drawinglayer
185 :
186 : namespace drawinglayer
187 : {
188 0 : void impDrawGradientToOutDev(
189 : OutputDevice& rOutDev,
190 : const basegfx::B2DPolyPolygon& rTargetForm,
191 : attribute::GradientStyle eGradientStyle,
192 : sal_uInt32 nSteps,
193 : const basegfx::BColor& rStart,
194 : const basegfx::BColor& rEnd,
195 : double fBorder, double fAngle, double fOffsetX, double fOffsetY, bool bSimple)
196 : {
197 0 : const basegfx::B2DRange aOutlineRange(basegfx::tools::getRange(rTargetForm));
198 0 : ::std::vector< basegfx::B2DHomMatrix > aMatrices;
199 0 : ::std::vector< basegfx::BColor > aColors;
200 0 : basegfx::B2DPolygon aUnitPolygon;
201 :
202 : // make sure steps is not too high/low
203 0 : nSteps = impCalcGradientSteps(rOutDev, nSteps, aOutlineRange, sal_uInt32((rStart.getMaximumDistance(rEnd) * 127.5) + 0.5));
204 :
205 : // create geometries
206 0 : switch(eGradientStyle)
207 : {
208 : case attribute::GRADIENTSTYLE_LINEAR:
209 : {
210 0 : texture::GeoTexSvxGradientLinear aGradient(aOutlineRange, rStart, rEnd, nSteps, fBorder, fAngle);
211 0 : aGradient.appendTransformations(aMatrices);
212 0 : aGradient.appendColors(aColors);
213 0 : aUnitPolygon = basegfx::tools::createUnitPolygon();
214 0 : break;
215 : }
216 : case attribute::GRADIENTSTYLE_AXIAL:
217 : {
218 0 : texture::GeoTexSvxGradientAxial aGradient(aOutlineRange, rStart, rEnd, nSteps, fBorder, fAngle);
219 0 : aGradient.appendTransformations(aMatrices);
220 0 : aGradient.appendColors(aColors);
221 0 : aUnitPolygon = basegfx::tools::createPolygonFromRect(basegfx::B2DRange(-1, -1, 1, 1));
222 0 : break;
223 : }
224 : case attribute::GRADIENTSTYLE_RADIAL:
225 : {
226 0 : texture::GeoTexSvxGradientRadial aGradient(aOutlineRange, rStart, rEnd, nSteps, fBorder, fOffsetX, fOffsetY);
227 0 : aGradient.appendTransformations(aMatrices);
228 0 : aGradient.appendColors(aColors);
229 0 : aUnitPolygon = basegfx::tools::createPolygonFromCircle(basegfx::B2DPoint(0,0), 1);
230 0 : break;
231 : }
232 : case attribute::GRADIENTSTYLE_ELLIPTICAL:
233 : {
234 0 : texture::GeoTexSvxGradientElliptical aGradient(aOutlineRange, rStart, rEnd, nSteps, fBorder, fOffsetX, fOffsetX, fAngle);
235 0 : aGradient.appendTransformations(aMatrices);
236 0 : aGradient.appendColors(aColors);
237 0 : aUnitPolygon = basegfx::tools::createPolygonFromCircle(basegfx::B2DPoint(0,0), 1);
238 0 : break;
239 : }
240 : case attribute::GRADIENTSTYLE_SQUARE:
241 : {
242 0 : texture::GeoTexSvxGradientSquare aGradient(aOutlineRange, rStart, rEnd, nSteps, fBorder, fOffsetX, fOffsetX, fAngle);
243 0 : aGradient.appendTransformations(aMatrices);
244 0 : aGradient.appendColors(aColors);
245 0 : aUnitPolygon = basegfx::tools::createPolygonFromRect(basegfx::B2DRange(-1, -1, 1, 1));
246 0 : break;
247 : }
248 : case attribute::GRADIENTSTYLE_RECT:
249 : {
250 0 : texture::GeoTexSvxGradientRect aGradient(aOutlineRange, rStart, rEnd, nSteps, fBorder, fOffsetX, fOffsetX, fAngle);
251 0 : aGradient.appendTransformations(aMatrices);
252 0 : aGradient.appendColors(aColors);
253 0 : aUnitPolygon = basegfx::tools::createPolygonFromRect(basegfx::B2DRange(-1, -1, 1, 1));
254 0 : break;
255 : }
256 : }
257 :
258 : // paint them with mask using the XOR method
259 0 : if(!aMatrices.empty())
260 : {
261 0 : if(bSimple)
262 : {
263 0 : impDrawGradientToOutDevSimple(rOutDev, rTargetForm, aMatrices, aColors, aUnitPolygon);
264 : }
265 : else
266 : {
267 0 : impDrawGradientToOutDevComplex(rOutDev, rTargetForm, aMatrices, aColors, aUnitPolygon);
268 : }
269 0 : }
270 0 : }
271 : } // end of namespace drawinglayer
272 :
273 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|