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 : #ifndef _BGFX_TOOLS_GRADIENTTOOLS_HXX
21 : #define _BGFX_TOOLS_GRADIENTTOOLS_HXX
22 :
23 : #include <basegfx/point/b2dpoint.hxx>
24 : #include <basegfx/range/b2drange.hxx>
25 : #include <basegfx/vector/b2dvector.hxx>
26 : #include <basegfx/matrix/b2dhommatrix.hxx>
27 : #include <basegfx/numeric/ftools.hxx>
28 : #include <basegfx/basegfxdllapi.h>
29 :
30 : #include <vector>
31 : #include <algorithm>
32 :
33 : namespace basegfx
34 : {
35 : /** Gradient definition as used in ODF 1.2
36 :
37 : This struct collects all data necessary for rendering ODF
38 : 1.2-compatible gradients. Use the createXXXODFGradientInfo()
39 : methods below for initializing from ODF attributes.
40 : */
41 6 : struct BASEGFX_DLLPUBLIC ODFGradientInfo
42 : {
43 : /** transformation mapping from [0,1]^2 texture coordinate
44 : space to [0,1]^2 shape coordinate space
45 : */
46 : B2DHomMatrix maTextureTransform;
47 :
48 : /** transformation mapping from [0,1]^2 shape coordinate space
49 : to [0,1]^2 texture coordinate space. This is the
50 : transformation commonly used to create gradients from a
51 : scanline rasterizer (put shape u/v coordinates into it, get
52 : texture s/t coordinates out of it)
53 : */
54 : B2DHomMatrix maBackTextureTransform;
55 :
56 : /** Aspect ratio of the gradient. Only used in drawinglayer
57 : for generating nested gradient polygons currently. Already
58 : catered for in the transformations above.
59 : */
60 : double mfAspectRatio;
61 :
62 : /** Requested gradient steps to render. See the
63 : implementations of the getXXXGradientAlpha() methods below,
64 : the semantic differs slightly for the different gradient
65 : types.
66 : */
67 : sal_uInt32 mnSteps;
68 : };
69 :
70 : namespace tools
71 : {
72 : /** Create matrix for ODF's linear gradient definition
73 :
74 : Note that odf linear gradients are varying in y direction.
75 :
76 : @param o_rGradientInfo
77 : Receives the calculated texture transformation matrix (for
78 : use with standard [0,1]x[0,1] texture coordinates)
79 :
80 : @param rTargetArea
81 : Output area, needed for aspect ratio calculations and
82 : texture transformation
83 :
84 : @param nSteps
85 : Number of gradient steps (from ODF)
86 :
87 : @param fBorder
88 : Width of gradient border (from ODF)
89 :
90 : @param fAngle
91 : Gradient angle (from ODF)
92 : */
93 : BASEGFX_DLLPUBLIC ODFGradientInfo& createLinearODFGradientInfo(ODFGradientInfo& o_rGradientInfo,
94 : const B2DRange& rTargetArea,
95 : sal_uInt32 nSteps,
96 : double fBorder,
97 : double fAngle);
98 :
99 : /** Calculate linear gradient blend value
100 :
101 : This method generates you the lerp alpha value for
102 : blending linearly between gradient start and end color,
103 : according to the formula (startCol*(1.0-alpha) + endCol*alpha)
104 :
105 : @param rUV
106 : Current uv coordinate. Values outside [0,1] will be
107 : clamped. Assumes gradient color varies along the y axis.
108 :
109 : @param rGradInfo
110 : Gradient info, for transformation and number of steps
111 : */
112 0 : inline double getLinearGradientAlpha(const B2DPoint& rUV,
113 : const ODFGradientInfo& rGradInfo )
114 : {
115 0 : const B2DPoint aCoor(rGradInfo.maBackTextureTransform * rUV);
116 0 : const double t(clamp(aCoor.getY(), 0.0, 1.0));
117 0 : const sal_uInt32 nSteps(rGradInfo.mnSteps);
118 :
119 0 : if(nSteps > 2L && nSteps < 128L)
120 0 : return floor(t * nSteps) / double(nSteps + 1L);
121 :
122 0 : return t;
123 : }
124 :
125 : /** Create matrix for ODF's axial gradient definition
126 :
127 : Note that odf axial gradients are varying in y
128 : direction. Note further that you can map the axial
129 : gradient to a linear gradient (in case you want or need to
130 : avoid an extra gradient renderer), by using
131 : createLinearODFGradientInfo() instead, shifting the
132 : resulting texture transformation by 0.5 to the top and
133 : appending the same stop colors again, but mirrored.
134 :
135 : @param o_rGradientInfo
136 : Receives the calculated texture transformation matrix (for
137 : use with standard [0,1]x[0,1] texture coordinates)
138 :
139 : @param rTargetArea
140 : Output area, needed for aspect ratio calculations and
141 : texture transformation
142 :
143 : @param nSteps
144 : Number of gradient steps (from ODF)
145 :
146 : @param fBorder
147 : Width of gradient border (from ODF)
148 :
149 : @param fAngle
150 : Gradient angle (from ODF)
151 : */
152 : BASEGFX_DLLPUBLIC ODFGradientInfo& createAxialODFGradientInfo(ODFGradientInfo& o_rGradientInfo,
153 : const B2DRange& rTargetArea,
154 : sal_uInt32 nSteps,
155 : double fBorder,
156 : double fAngle);
157 :
158 : /** Calculate axial gradient blend value
159 :
160 : This method generates you the lerp alpha value for
161 : blending linearly between gradient start and end color,
162 : according to the formula (startCol*(1.0-alpha) + endCol*alpha)
163 :
164 : @param rUV
165 : Current uv coordinate. Values outside [0,1] will be
166 : clamped. Assumes gradient color varies along the y axis.
167 :
168 : @param rGradInfo
169 : Gradient info, for transformation and number of steps
170 : */
171 0 : inline double getAxialGradientAlpha(const B2DPoint& rUV,
172 : const ODFGradientInfo& rGradInfo )
173 : {
174 0 : const B2DPoint aCoor(rGradInfo.maBackTextureTransform * rUV);
175 0 : const double t(clamp(fabs(aCoor.getY()), 0.0, 1.0));
176 0 : const sal_uInt32 nSteps(rGradInfo.mnSteps);
177 0 : const double fInternalSteps((nSteps * 2L) - 1L);
178 :
179 0 : if(nSteps > 2L && nSteps < 128L)
180 0 : return floor(((t * fInternalSteps) + 1.0) / 2.0) / double(nSteps - 1L);
181 :
182 0 : return t;
183 : }
184 :
185 : /** Create matrix for ODF's radial gradient definition
186 :
187 : @param o_rGradientInfo
188 : Receives the calculated texture transformation matrix (for
189 : use with standard [0,1]x[0,1] texture coordinates)
190 :
191 : @param rTargetArea
192 : Output area, needed for aspect ratio calculations and
193 : texture transformation
194 :
195 : @param rOffset
196 : Gradient offset value (from ODF)
197 :
198 : @param nSteps
199 : Number of gradient steps (from ODF)
200 :
201 : @param fBorder
202 : Width of gradient border (from ODF)
203 :
204 : @param fAngle
205 : Gradient angle (from ODF)
206 : */
207 : BASEGFX_DLLPUBLIC ODFGradientInfo& createRadialODFGradientInfo(ODFGradientInfo& o_rGradientInfo,
208 : const B2DRange& rTargetArea,
209 : const B2DVector& rOffset,
210 : sal_uInt32 nSteps,
211 : double fBorder);
212 :
213 : /** Calculate radial gradient blend value
214 :
215 : This method generates you the lerp alpha value for
216 : blending linearly between gradient start and end color,
217 : according to the formula (startCol*(1.0-alpha) + endCol*alpha)
218 :
219 : @param rUV
220 : Current uv coordinate. Values outside [0,1] will be
221 : clamped.
222 :
223 : @param rGradInfo
224 : Gradient info, for transformation and number of steps
225 : */
226 0 : inline double getRadialGradientAlpha(const B2DPoint& rUV,
227 : const ODFGradientInfo& rGradInfo )
228 : {
229 0 : const B2DPoint aCoor(rGradInfo.maBackTextureTransform * rUV);
230 : const double fDist(
231 0 : clamp(aCoor.getX() * aCoor.getX() + aCoor.getY() * aCoor.getY(),
232 : 0.0,
233 0 : 1.0));
234 :
235 0 : const double t(1.0 - sqrt(fDist));
236 0 : const sal_uInt32 nSteps(rGradInfo.mnSteps);
237 :
238 0 : if(nSteps > 2L && nSteps < 128L)
239 0 : return floor(t * nSteps) / double(nSteps - 1L);
240 :
241 0 : return t;
242 : }
243 :
244 : /** Create matrix for ODF's elliptical gradient definition
245 :
246 : @param o_rGradientInfo
247 : Receives the calculated texture transformation matrix (for
248 : use with standard [0,1]x[0,1] texture coordinates)
249 :
250 : @param rTargetArea
251 : Output area, needed for aspect ratio calculations and
252 : texture transformation
253 :
254 : @param rOffset
255 : Gradient offset value (from ODF)
256 :
257 : @param nSteps
258 : Number of gradient steps (from ODF)
259 :
260 : @param fBorder
261 : Width of gradient border (from ODF)
262 :
263 : @param fAngle
264 : Gradient angle (from ODF)
265 : */
266 : BASEGFX_DLLPUBLIC ODFGradientInfo& createEllipticalODFGradientInfo(ODFGradientInfo& o_rGradientInfo,
267 : const B2DRange& rTargetArea,
268 : const B2DVector& rOffset,
269 : sal_uInt32 nSteps,
270 : double fBorder,
271 : double fAngle);
272 :
273 : /** Calculate elliptical gradient blend value
274 :
275 : This method generates you the lerp alpha value for
276 : blending linearly between gradient start and end color,
277 : according to the formula (startCol*(1.0-alpha) + endCol*alpha)
278 :
279 : @param rUV
280 : Current uv coordinate. Values outside [0,1] will be
281 : clamped.
282 :
283 : @param rGradInfo
284 : Gradient info, for transformation and number of steps
285 : */
286 0 : inline double getEllipticalGradientAlpha(const B2DPoint& rUV,
287 : const ODFGradientInfo& rGradInfo )
288 : {
289 0 : return getRadialGradientAlpha(rUV,rGradInfo); // only matrix setup differs
290 : }
291 :
292 : /** Create matrix for ODF's square gradient definition
293 :
294 : @param o_rGradientInfo
295 : Receives the calculated texture transformation matrix (for
296 : use with standard [0,1]x[0,1] texture coordinates)
297 :
298 : @param rTargetArea
299 : Output area, needed for aspect ratio calculations and
300 : texture transformation
301 :
302 : @param rOffset
303 : Gradient offset value (from ODF)
304 :
305 : @param nSteps
306 : Number of gradient steps (from ODF)
307 :
308 : @param fBorder
309 : Width of gradient border (from ODF)
310 :
311 : @param fAngle
312 : Gradient angle (from ODF)
313 : */
314 : BASEGFX_DLLPUBLIC ODFGradientInfo& createSquareODFGradientInfo(ODFGradientInfo& o_rGradientInfo,
315 : const B2DRange& rTargetArea,
316 : const B2DVector& rOffset,
317 : sal_uInt32 nSteps,
318 : double fBorder,
319 : double fAngle);
320 :
321 : /** Calculate square gradient blend value
322 :
323 : This method generates you the lerp alpha value for
324 : blending linearly between gradient start and end color,
325 : according to the formula (startCol*(1.0-alpha) + endCol*alpha)
326 :
327 : @param rUV
328 : Current uv coordinate. Values outside [0,1] will be
329 : clamped.
330 :
331 : @param rGradInfo
332 : Gradient info, for transformation and number of steps
333 : */
334 0 : inline double getSquareGradientAlpha(const B2DPoint& rUV,
335 : const ODFGradientInfo& rGradInfo )
336 : {
337 0 : const B2DPoint aCoor(rGradInfo.maBackTextureTransform * rUV);
338 0 : const double fAbsX(fabs(aCoor.getX()));
339 0 : const double fAbsY(fabs(aCoor.getY()));
340 :
341 0 : if(fTools::moreOrEqual(fAbsX, 1.0) || fTools::moreOrEqual(fAbsY, 1.0))
342 0 : return 0.0;
343 :
344 0 : const double t(1.0 - (fAbsX > fAbsY ? fAbsX : fAbsY));
345 0 : const sal_uInt32 nSteps(rGradInfo.mnSteps);
346 :
347 0 : if(nSteps > 2L && nSteps < 128L)
348 0 : return floor(t * nSteps) / double(nSteps - 1L);
349 :
350 0 : return t;
351 : }
352 :
353 : /** Create matrix for ODF's rectangular gradient definition
354 :
355 : @param o_rGradientInfo
356 : Receives the calculated texture transformation matrix (for
357 : use with standard [0,1]x[0,1] texture coordinates)
358 :
359 : @param rTargetArea
360 : Output area, needed for aspect ratio calculations and
361 : texture transformation
362 :
363 : @param rOffset
364 : Gradient offset value (from ODF)
365 :
366 : @param nSteps
367 : Number of gradient steps (from ODF)
368 :
369 : @param fBorder
370 : Width of gradient border (from ODF)
371 :
372 : @param fAngle
373 : Gradient angle (from ODF)
374 : */
375 : BASEGFX_DLLPUBLIC ODFGradientInfo& createRectangularODFGradientInfo(ODFGradientInfo& o_rGradientInfo,
376 : const B2DRange& rTargetArea,
377 : const B2DVector& rOffset,
378 : sal_uInt32 nSteps,
379 : double fBorder,
380 : double fAngle);
381 :
382 : /** Calculate rectangular gradient blend value
383 :
384 : This method generates you the lerp alpha value for
385 : blending linearly between gradient start and end color,
386 : according to the formula (startCol*(1.0-alpha) + endCol*alpha)
387 :
388 : @param rUV
389 : Current uv coordinate. Values outside [0,1] will be
390 : clamped.
391 :
392 : @param rGradInfo
393 : Gradient info, for transformation and number of steps
394 : */
395 0 : inline double getRectangularGradientAlpha(const B2DPoint& rUV,
396 : const ODFGradientInfo& rGradInfo )
397 : {
398 0 : return getSquareGradientAlpha(rUV, rGradInfo); // only matrix setup differs
399 : }
400 : }
401 : }
402 :
403 : #endif
404 :
405 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|