From 1837a267a2cf82b0152631e416d8be66c2adef25 Mon Sep 17 00:00:00 2001 From: thb Date: Fri, 16 Oct 2009 00:43:16 +0200 Subject: #i105937# Much improved gradient support for canvas/basegfx/drawinglayer. See http://blog.thebehrens.net/2009/07/28/hackweek-iv-canvas-convwatch/ for more background information --- basegfx/inc/basegfx/tools/gradienttools.hxx | 18 ++++- basegfx/inc/basegfx/tools/keystoplerp.hxx | 100 ++++++++++++++++++++++++++++ basegfx/inc/basegfx/tools/lerp.hxx | 60 +++++++++++++++++ 3 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 basegfx/inc/basegfx/tools/keystoplerp.hxx create mode 100644 basegfx/inc/basegfx/tools/lerp.hxx (limited to 'basegfx/inc/basegfx/tools') diff --git a/basegfx/inc/basegfx/tools/gradienttools.hxx b/basegfx/inc/basegfx/tools/gradienttools.hxx index 0c7f2ab2c060..dbcc5a3221f0 100644 --- a/basegfx/inc/basegfx/tools/gradienttools.hxx +++ b/basegfx/inc/basegfx/tools/gradienttools.hxx @@ -37,6 +37,9 @@ #include #include +#include +#include + namespace basegfx { /** Gradient definition as used in ODF 1.2 @@ -78,6 +81,8 @@ namespace basegfx { /** Create matrix for ODF's linear gradient definition + Note that odf linear gradients are varying in y direction. + @param o_rGradientInfo Receives the calculated texture transformation matrix (for use with standard [0,1]x[0,1] texture coordinates) @@ -109,7 +114,7 @@ namespace basegfx @param rUV Current uv coordinate. Values outside [0,1] will be - clamped. + clamped. Assumes gradient color varies along the y axis. @param rGradInfo Gradient info, for transformation and number of steps @@ -129,6 +134,14 @@ namespace basegfx /** Create matrix for ODF's axial gradient definition + Note that odf axial gradients are varying in y + direction. Note further that you can map the axial + gradient to a linear gradient (in case you want or need to + avoid an extra gradient renderer), by using + createLinearODFGradientInfo() instead, shifting the + resulting texture transformation by 0.5 to the top and + appending the same stop colors again, but mirrored. + @param o_rGradientInfo Receives the calculated texture transformation matrix (for use with standard [0,1]x[0,1] texture coordinates) @@ -160,7 +173,7 @@ namespace basegfx @param rUV Current uv coordinate. Values outside [0,1] will be - clamped. + clamped. Assumes gradient color varies along the y axis. @param rGradInfo Gradient info, for transformation and number of steps @@ -394,7 +407,6 @@ namespace basegfx { return getSquareGradientAlpha(rUV, rGradInfo); // only matrix setup differs } - } } diff --git a/basegfx/inc/basegfx/tools/keystoplerp.hxx b/basegfx/inc/basegfx/tools/keystoplerp.hxx new file mode 100644 index 000000000000..a54b3485b1a1 --- /dev/null +++ b/basegfx/inc/basegfx/tools/keystoplerp.hxx @@ -0,0 +1,100 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: canvastools.hxx,v $ + * $Revision: 1.10 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _BGFX_TOOLS_KEYSTOPLERP_HXX +#define _BGFX_TOOLS_KEYSTOPLERP_HXX + +#include +#include + +namespace com{ namespace sun{ namespace star{ namespace uno { + template class Sequence; +}}}} + +namespace basegfx +{ + namespace tools + { + /** Lerp in a vector of key stops + + This class holds a key stop vector and provides the + functionality to lerp inside it. Useful e.g. for + multi-stop gradients, or the SMIL key time activity. + + For those, given a global [0,1] lerp alpha, one need to + find the suitable bucket index from key stop vector, and + then calculate the relative alpha between the two buckets + found. + */ + class KeyStopLerp + { + public: + typedef std::pair ResultType; + + /** Create lerper with given vector of stops + + @param rKeyStops + + Vector of stops, must contain at least two elements + (though preferrably more, otherwise you probably don't + need key stop lerping in the first place). All + elements must be of monotonically increasing value. + */ + explicit KeyStopLerp( const std::vector& rKeyStops ); + + /** Create lerper with given sequence of stops + + @param rKeyStops + + Sequence of stops, must contain at least two elements + (though preferrably more, otherwise you probably don't + need key stop lerping in the first place). All + elements must be of monotonically increasing value. + */ + explicit KeyStopLerp( const ::com::sun::star::uno::Sequence& rKeyStops ); + + /** Find two nearest bucket index & interpolate + + @param fAlpha + Find bucket index i, with keyStops[i] < fAlpha <= + keyStops[i+1]. Return new alpha value in [0,1), + proportional to fAlpha's position between keyStops[i] + and keyStops[i+1] + */ + ResultType lerp(double fAlpha) const; + + private: + std::vector maKeyStops; + mutable std::ptrdiff_t mnLastIndex; + }; + } +} + +#endif diff --git a/basegfx/inc/basegfx/tools/lerp.hxx b/basegfx/inc/basegfx/tools/lerp.hxx new file mode 100644 index 000000000000..590ef34c2009 --- /dev/null +++ b/basegfx/inc/basegfx/tools/lerp.hxx @@ -0,0 +1,60 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: lerp.hxx,v $ + * $Revision: 1.6 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _BGFX_TOOLS_LERP_HXX +#define _BGFX_TOOLS_LERP_HXX + +#include + +namespace basegfx +{ + namespace tools + { + /** Generic linear interpolator + + @tpl ValueType + Must have operator+ and operator* defined, and should + have value semantics. + + @param t + As usual, t must be in the [0,1] range + */ + template< typename ValueType > ValueType lerp( const ValueType& rFrom, + const ValueType& rTo, + double t ) + { + // This is only to suppress a double->int warning. All other + // types should be okay here. + return static_cast( (1.0-t)*rFrom + t*rTo ); + } + } +} + +#endif /* _BGFX_TOOLS_LERP_HXX */ -- cgit v1.2.3