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_basegfx.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <basegfx/tools/gradienttools.hxx> 32*cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx> 33*cdf0e10cSrcweir #include <basegfx/range/b2drange.hxx> 34*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir namespace basegfx 37*cdf0e10cSrcweir { 38*cdf0e10cSrcweir /** Most of the setup for linear & axial gradient is the same, except 39*cdf0e10cSrcweir for the border treatment. Factored out here. 40*cdf0e10cSrcweir */ 41*cdf0e10cSrcweir static void init1DGradientInfo(ODFGradientInfo& o_rGradientInfo, 42*cdf0e10cSrcweir const B2DRange& rTargetRange, 43*cdf0e10cSrcweir sal_uInt32 nSteps, 44*cdf0e10cSrcweir double fBorder, 45*cdf0e10cSrcweir double fAngle, 46*cdf0e10cSrcweir bool bAxial) 47*cdf0e10cSrcweir { 48*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.identity(); 49*cdf0e10cSrcweir o_rGradientInfo.maBackTextureTransform.identity(); 50*cdf0e10cSrcweir o_rGradientInfo.mnSteps = nSteps; 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir fAngle = -fAngle; 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir double fTargetSizeX(rTargetRange.getWidth()); 55*cdf0e10cSrcweir double fTargetSizeY(rTargetRange.getHeight()); 56*cdf0e10cSrcweir double fTargetOffsetX(rTargetRange.getMinX()); 57*cdf0e10cSrcweir double fTargetOffsetY(rTargetRange.getMinY()); 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir // add object expansion 60*cdf0e10cSrcweir if(0.0 != fAngle) 61*cdf0e10cSrcweir { 62*cdf0e10cSrcweir const double fAbsCos(fabs(cos(fAngle))); 63*cdf0e10cSrcweir const double fAbsSin(fabs(sin(fAngle))); 64*cdf0e10cSrcweir const double fNewX(fTargetSizeX * fAbsCos + fTargetSizeY * fAbsSin); 65*cdf0e10cSrcweir const double fNewY(fTargetSizeY * fAbsCos + fTargetSizeX * fAbsSin); 66*cdf0e10cSrcweir fTargetOffsetX -= (fNewX - fTargetSizeX) / 2.0; 67*cdf0e10cSrcweir fTargetOffsetY -= (fNewY - fTargetSizeY) / 2.0; 68*cdf0e10cSrcweir fTargetSizeX = fNewX; 69*cdf0e10cSrcweir fTargetSizeY = fNewY; 70*cdf0e10cSrcweir } 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir const double fSizeWithoutBorder=1.0 - fBorder; 73*cdf0e10cSrcweir if( bAxial ) 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.scale(1.0, fSizeWithoutBorder * .5); 76*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.translate(0.0, 0.5); 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir else 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir if(!fTools::equal(fSizeWithoutBorder, 1.0)) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.scale(1.0, fSizeWithoutBorder); 83*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.translate(0.0, fBorder); 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.scale(fTargetSizeX, fTargetSizeY); 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir // add texture rotate after scale to keep perpendicular angles 90*cdf0e10cSrcweir if(0.0 != fAngle) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir const B2DPoint aCenter(0.5*fTargetSizeX, 93*cdf0e10cSrcweir 0.5*fTargetSizeY); 94*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform *= 95*cdf0e10cSrcweir basegfx::tools::createRotateAroundPoint(aCenter, fAngle); 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir // add object translate 99*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.translate(fTargetOffsetX, fTargetOffsetY); 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir // prepare aspect for texture 102*cdf0e10cSrcweir o_rGradientInfo.mfAspectRatio = (0.0 != fTargetSizeY) ? fTargetSizeX / fTargetSizeY : 1.0; 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir // build transform from u,v to [0.0 .. 1.0]. 105*cdf0e10cSrcweir o_rGradientInfo.maBackTextureTransform = o_rGradientInfo.maTextureTransform; 106*cdf0e10cSrcweir o_rGradientInfo.maBackTextureTransform.invert(); 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir /** Most of the setup for radial & ellipsoidal gradient is the same, 110*cdf0e10cSrcweir except for the border treatment. Factored out here. 111*cdf0e10cSrcweir */ 112*cdf0e10cSrcweir static void initEllipticalGradientInfo(ODFGradientInfo& o_rGradientInfo, 113*cdf0e10cSrcweir const B2DRange& rTargetRange, 114*cdf0e10cSrcweir const B2DVector& rOffset, 115*cdf0e10cSrcweir sal_uInt32 nSteps, 116*cdf0e10cSrcweir double fBorder, 117*cdf0e10cSrcweir double fAngle, 118*cdf0e10cSrcweir bool bCircular) 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.identity(); 121*cdf0e10cSrcweir o_rGradientInfo.maBackTextureTransform.identity(); 122*cdf0e10cSrcweir o_rGradientInfo.mnSteps = nSteps; 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir fAngle = -fAngle; 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir double fTargetSizeX(rTargetRange.getWidth()); 127*cdf0e10cSrcweir double fTargetSizeY(rTargetRange.getHeight()); 128*cdf0e10cSrcweir double fTargetOffsetX(rTargetRange.getMinX()); 129*cdf0e10cSrcweir double fTargetOffsetY(rTargetRange.getMinY()); 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir // add object expansion 132*cdf0e10cSrcweir if( bCircular ) 133*cdf0e10cSrcweir { 134*cdf0e10cSrcweir const double fOriginalDiag(sqrt((fTargetSizeX * fTargetSizeX) + (fTargetSizeY * fTargetSizeY))); 135*cdf0e10cSrcweir fTargetOffsetX -= (fOriginalDiag - fTargetSizeX) / 2.0; 136*cdf0e10cSrcweir fTargetOffsetY -= (fOriginalDiag - fTargetSizeY) / 2.0; 137*cdf0e10cSrcweir fTargetSizeX = fOriginalDiag; 138*cdf0e10cSrcweir fTargetSizeY = fOriginalDiag; 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir else 141*cdf0e10cSrcweir { 142*cdf0e10cSrcweir fTargetOffsetX -= (0.4142 / 2.0 ) * fTargetSizeX; 143*cdf0e10cSrcweir fTargetOffsetY -= (0.4142 / 2.0 ) * fTargetSizeY; 144*cdf0e10cSrcweir fTargetSizeX = 1.4142 * fTargetSizeX; 145*cdf0e10cSrcweir fTargetSizeY = 1.4142 * fTargetSizeY; 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir const double fHalfBorder((1.0 - fBorder) * 0.5); 149*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.scale(fHalfBorder, fHalfBorder); 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.translate(0.5, 0.5); 152*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.scale(fTargetSizeX, fTargetSizeY); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir // add texture rotate after scale to keep perpendicular angles 155*cdf0e10cSrcweir if( !bCircular && 0.0 != fAngle) 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir const B2DPoint aCenter(0.5*fTargetSizeX, 158*cdf0e10cSrcweir 0.5*fTargetSizeY); 159*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform *= 160*cdf0e10cSrcweir basegfx::tools::createRotateAroundPoint(aCenter, fAngle); 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir // add defined offsets after rotation 164*cdf0e10cSrcweir if(0.5 != rOffset.getX() || 0.5 != rOffset.getY()) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir // use original target size 167*cdf0e10cSrcweir fTargetOffsetX += (rOffset.getX() - 0.5) * rTargetRange.getWidth(); 168*cdf0e10cSrcweir fTargetOffsetY += (rOffset.getY() - 0.5) * rTargetRange.getHeight(); 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir // add object translate 172*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.translate(fTargetOffsetX, fTargetOffsetY); 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir // prepare aspect for texture 175*cdf0e10cSrcweir o_rGradientInfo.mfAspectRatio = (0.0 != fTargetSizeY) ? fTargetSizeX / fTargetSizeY : 1.0; 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir // build transform from u,v to [0.0 .. 1.0]. 178*cdf0e10cSrcweir o_rGradientInfo.maBackTextureTransform = o_rGradientInfo.maTextureTransform; 179*cdf0e10cSrcweir o_rGradientInfo.maBackTextureTransform.invert(); 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir /** Setup for rect & square gradient is exactly the same. Factored out 183*cdf0e10cSrcweir here. 184*cdf0e10cSrcweir */ 185*cdf0e10cSrcweir static void initRectGradientInfo(ODFGradientInfo& o_rGradientInfo, 186*cdf0e10cSrcweir const B2DRange& rTargetRange, 187*cdf0e10cSrcweir const B2DVector& rOffset, 188*cdf0e10cSrcweir sal_uInt32 nSteps, 189*cdf0e10cSrcweir double fBorder, 190*cdf0e10cSrcweir double fAngle) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.identity(); 193*cdf0e10cSrcweir o_rGradientInfo.maBackTextureTransform.identity(); 194*cdf0e10cSrcweir o_rGradientInfo.mnSteps = nSteps; 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir fAngle = -fAngle; 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir double fTargetSizeX(rTargetRange.getWidth()); 199*cdf0e10cSrcweir double fTargetSizeY(rTargetRange.getHeight()); 200*cdf0e10cSrcweir double fTargetOffsetX(rTargetRange.getMinX()); 201*cdf0e10cSrcweir double fTargetOffsetY(rTargetRange.getMinY()); 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir // add object expansion 204*cdf0e10cSrcweir if(0.0 != fAngle) 205*cdf0e10cSrcweir { 206*cdf0e10cSrcweir const double fAbsCos(fabs(cos(fAngle))); 207*cdf0e10cSrcweir const double fAbsSin(fabs(sin(fAngle))); 208*cdf0e10cSrcweir const double fNewX(fTargetSizeX * fAbsCos + fTargetSizeY * fAbsSin); 209*cdf0e10cSrcweir const double fNewY(fTargetSizeY * fAbsCos + fTargetSizeX * fAbsSin); 210*cdf0e10cSrcweir fTargetOffsetX -= (fNewX - fTargetSizeX) / 2.0; 211*cdf0e10cSrcweir fTargetOffsetY -= (fNewY - fTargetSizeY) / 2.0; 212*cdf0e10cSrcweir fTargetSizeX = fNewX; 213*cdf0e10cSrcweir fTargetSizeY = fNewY; 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir const double fHalfBorder((1.0 - fBorder) * 0.5); 217*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.scale(fHalfBorder, fHalfBorder); 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.translate(0.5, 0.5); 220*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.scale(fTargetSizeX, fTargetSizeY); 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir // add texture rotate after scale to keep perpendicular angles 223*cdf0e10cSrcweir if(0.0 != fAngle) 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir const B2DPoint aCenter(0.5*fTargetSizeX, 226*cdf0e10cSrcweir 0.5*fTargetSizeY); 227*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform *= 228*cdf0e10cSrcweir basegfx::tools::createRotateAroundPoint(aCenter, fAngle); 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir // add defined offsets after rotation 232*cdf0e10cSrcweir if(0.5 != rOffset.getX() || 0.5 != rOffset.getY()) 233*cdf0e10cSrcweir { 234*cdf0e10cSrcweir // use scaled target size 235*cdf0e10cSrcweir fTargetOffsetX += (rOffset.getX() - 0.5) * fTargetSizeX; 236*cdf0e10cSrcweir fTargetOffsetY += (rOffset.getY() - 0.5) * fTargetSizeY; 237*cdf0e10cSrcweir } 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir // add object translate 240*cdf0e10cSrcweir o_rGradientInfo.maTextureTransform.translate(fTargetOffsetX, fTargetOffsetY); 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir // prepare aspect for texture 243*cdf0e10cSrcweir o_rGradientInfo.mfAspectRatio = (0.0 != fTargetSizeY) ? fTargetSizeX / fTargetSizeY : 1.0; 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir // build transform from u,v to [0.0 .. 1.0]. As base, use inverse texture transform 246*cdf0e10cSrcweir o_rGradientInfo.maBackTextureTransform = o_rGradientInfo.maTextureTransform; 247*cdf0e10cSrcweir o_rGradientInfo.maBackTextureTransform.invert(); 248*cdf0e10cSrcweir } 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir namespace tools 251*cdf0e10cSrcweir { 252*cdf0e10cSrcweir ODFGradientInfo& createLinearODFGradientInfo(ODFGradientInfo& o_rGradientInfo, 253*cdf0e10cSrcweir const B2DRange& rTargetArea, 254*cdf0e10cSrcweir sal_uInt32 nSteps, 255*cdf0e10cSrcweir double fBorder, 256*cdf0e10cSrcweir double fAngle) 257*cdf0e10cSrcweir { 258*cdf0e10cSrcweir init1DGradientInfo(o_rGradientInfo, 259*cdf0e10cSrcweir rTargetArea, 260*cdf0e10cSrcweir nSteps, 261*cdf0e10cSrcweir fBorder, 262*cdf0e10cSrcweir fAngle, 263*cdf0e10cSrcweir false); 264*cdf0e10cSrcweir return o_rGradientInfo; 265*cdf0e10cSrcweir } 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir ODFGradientInfo& createAxialODFGradientInfo(ODFGradientInfo& o_rGradientInfo, 268*cdf0e10cSrcweir const B2DRange& rTargetArea, 269*cdf0e10cSrcweir sal_uInt32 nSteps, 270*cdf0e10cSrcweir double fBorder, 271*cdf0e10cSrcweir double fAngle) 272*cdf0e10cSrcweir { 273*cdf0e10cSrcweir init1DGradientInfo(o_rGradientInfo, 274*cdf0e10cSrcweir rTargetArea, 275*cdf0e10cSrcweir nSteps, 276*cdf0e10cSrcweir fBorder, 277*cdf0e10cSrcweir fAngle, 278*cdf0e10cSrcweir true); 279*cdf0e10cSrcweir return o_rGradientInfo; 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir ODFGradientInfo& createRadialODFGradientInfo(ODFGradientInfo& o_rGradientInfo, 283*cdf0e10cSrcweir const B2DRange& rTargetArea, 284*cdf0e10cSrcweir const B2DVector& rOffset, 285*cdf0e10cSrcweir sal_uInt32 nSteps, 286*cdf0e10cSrcweir double fBorder) 287*cdf0e10cSrcweir { 288*cdf0e10cSrcweir initEllipticalGradientInfo(o_rGradientInfo, 289*cdf0e10cSrcweir rTargetArea, 290*cdf0e10cSrcweir rOffset, 291*cdf0e10cSrcweir nSteps, 292*cdf0e10cSrcweir fBorder, 293*cdf0e10cSrcweir 0.0, 294*cdf0e10cSrcweir true); 295*cdf0e10cSrcweir return o_rGradientInfo; 296*cdf0e10cSrcweir } 297*cdf0e10cSrcweir 298*cdf0e10cSrcweir ODFGradientInfo& createEllipticalODFGradientInfo(ODFGradientInfo& o_rGradientInfo, 299*cdf0e10cSrcweir const B2DRange& rTargetArea, 300*cdf0e10cSrcweir const B2DVector& rOffset, 301*cdf0e10cSrcweir sal_uInt32 nSteps, 302*cdf0e10cSrcweir double fBorder, 303*cdf0e10cSrcweir double fAngle) 304*cdf0e10cSrcweir { 305*cdf0e10cSrcweir initEllipticalGradientInfo(o_rGradientInfo, 306*cdf0e10cSrcweir rTargetArea, 307*cdf0e10cSrcweir rOffset, 308*cdf0e10cSrcweir nSteps, 309*cdf0e10cSrcweir fBorder, 310*cdf0e10cSrcweir fAngle, 311*cdf0e10cSrcweir false); 312*cdf0e10cSrcweir return o_rGradientInfo; 313*cdf0e10cSrcweir } 314*cdf0e10cSrcweir 315*cdf0e10cSrcweir ODFGradientInfo& createSquareODFGradientInfo(ODFGradientInfo& o_rGradientInfo, 316*cdf0e10cSrcweir const B2DRange& rTargetArea, 317*cdf0e10cSrcweir const B2DVector& rOffset, 318*cdf0e10cSrcweir sal_uInt32 nSteps, 319*cdf0e10cSrcweir double fBorder, 320*cdf0e10cSrcweir double fAngle) 321*cdf0e10cSrcweir { 322*cdf0e10cSrcweir initRectGradientInfo(o_rGradientInfo, 323*cdf0e10cSrcweir rTargetArea, 324*cdf0e10cSrcweir rOffset, 325*cdf0e10cSrcweir nSteps, 326*cdf0e10cSrcweir fBorder, 327*cdf0e10cSrcweir fAngle); 328*cdf0e10cSrcweir return o_rGradientInfo; 329*cdf0e10cSrcweir } 330*cdf0e10cSrcweir 331*cdf0e10cSrcweir ODFGradientInfo& createRectangularODFGradientInfo(ODFGradientInfo& o_rGradientInfo, 332*cdf0e10cSrcweir const B2DRange& rTargetArea, 333*cdf0e10cSrcweir const B2DVector& rOffset, 334*cdf0e10cSrcweir sal_uInt32 nSteps, 335*cdf0e10cSrcweir double fBorder, 336*cdf0e10cSrcweir double fAngle) 337*cdf0e10cSrcweir { 338*cdf0e10cSrcweir initRectGradientInfo(o_rGradientInfo, 339*cdf0e10cSrcweir rTargetArea, 340*cdf0e10cSrcweir rOffset, 341*cdf0e10cSrcweir nSteps, 342*cdf0e10cSrcweir fBorder, 343*cdf0e10cSrcweir fAngle); 344*cdf0e10cSrcweir return o_rGradientInfo; 345*cdf0e10cSrcweir } 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir } // namespace tools 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir } // namespace basegfx 350