svgtopt/SVG/SVGImpl/src/SVGGradientElementImpl.cpp
author hgs
Wed, 03 Nov 2010 18:56:10 +0200
changeset 46 88edb906c587
permissions -rw-r--r--
201044
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description:  SVG Implementation source file
hgs
parents:
diff changeset
    15
 *
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
hgs
parents:
diff changeset
    18
hgs
parents:
diff changeset
    19
#include "SVGGradientElementImpl.h"
hgs
parents:
diff changeset
    20
#include "SVGDocumentImpl.h"
hgs
parents:
diff changeset
    21
hgs
parents:
diff changeset
    22
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    23
//
hgs
parents:
diff changeset
    24
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    25
CSvgGradientElementImpl::CSvgGradientElementImpl( CSvgDocumentImpl* aDoc )
hgs
parents:
diff changeset
    26
	{
hgs
parents:
diff changeset
    27
	SetOwnerDocument(aDoc);
hgs
parents:
diff changeset
    28
	}
hgs
parents:
diff changeset
    29
hgs
parents:
diff changeset
    30
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    31
//
hgs
parents:
diff changeset
    32
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    33
void CSvgGradientElementImpl::ConstructL(const TUint8 aElemID)
hgs
parents:
diff changeset
    34
	{
hgs
parents:
diff changeset
    35
    CSvgElementImpl::InitializeL( aElemID );
hgs
parents:
diff changeset
    36
hgs
parents:
diff changeset
    37
    iSvgStyleProperties = new(ELeave) RPointerArray<CCssValue>(KCSS_MAX_ATTR);
hgs
parents:
diff changeset
    38
    User::LeaveIfError( iSvgStyleProperties->Append( NULL ) );
hgs
parents:
diff changeset
    39
    iSvgStyleProperties->Remove( 0 );
hgs
parents:
diff changeset
    40
	iSvgUriReferenceImpl = CSvgUriReferenceImpl::NewL();
hgs
parents:
diff changeset
    41
hgs
parents:
diff changeset
    42
    iSvgTransformable = CSvgTransformableImpl::NewL();
hgs
parents:
diff changeset
    43
    iSvgStopElementArray = new( ELeave ) RPointerArray<CSvgStopElementImpl> ( 1 );
hgs
parents:
diff changeset
    44
hgs
parents:
diff changeset
    45
    iStopElementData = new(ELeave) RArray<TSvgStopData> (1);
hgs
parents:
diff changeset
    46
    iStopElementData->Reset();
hgs
parents:
diff changeset
    47
	}
hgs
parents:
diff changeset
    48
hgs
parents:
diff changeset
    49
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    50
//
hgs
parents:
diff changeset
    51
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    52
CSvgGradientElementImpl::~CSvgGradientElementImpl()
hgs
parents:
diff changeset
    53
    {
hgs
parents:
diff changeset
    54
    if(iSvgStopElementArray)
hgs
parents:
diff changeset
    55
		{
hgs
parents:
diff changeset
    56
        iSvgStopElementArray->Close();
hgs
parents:
diff changeset
    57
        delete iSvgStopElementArray;
hgs
parents:
diff changeset
    58
		iSvgStopElementArray = NULL;
hgs
parents:
diff changeset
    59
		}
hgs
parents:
diff changeset
    60
hgs
parents:
diff changeset
    61
	if ( iSvgStyleProperties )
hgs
parents:
diff changeset
    62
		{
hgs
parents:
diff changeset
    63
		iSvgStyleProperties->Close();
hgs
parents:
diff changeset
    64
		delete iSvgStyleProperties;
hgs
parents:
diff changeset
    65
		iSvgStyleProperties = NULL;
hgs
parents:
diff changeset
    66
		}
hgs
parents:
diff changeset
    67
hgs
parents:
diff changeset
    68
	if ( iSvgUriReferenceImpl )
hgs
parents:
diff changeset
    69
		{
hgs
parents:
diff changeset
    70
		delete iSvgUriReferenceImpl;
hgs
parents:
diff changeset
    71
		iSvgUriReferenceImpl = NULL;
hgs
parents:
diff changeset
    72
		}
hgs
parents:
diff changeset
    73
hgs
parents:
diff changeset
    74
	if(iStopElementData)
hgs
parents:
diff changeset
    75
		{
hgs
parents:
diff changeset
    76
		iStopElementData->Close();
hgs
parents:
diff changeset
    77
		delete iStopElementData;
hgs
parents:
diff changeset
    78
		iStopElementData = NULL;
hgs
parents:
diff changeset
    79
		}
hgs
parents:
diff changeset
    80
	}
hgs
parents:
diff changeset
    81
hgs
parents:
diff changeset
    82
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    83
//
hgs
parents:
diff changeset
    84
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    85
TInt CSvgGradientElementImpl::SetAttributeL( const TDesC& aName,const TDesC& aValue )
hgs
parents:
diff changeset
    86
	{
hgs
parents:
diff changeset
    87
	CSvgElementImpl::SetAttributeL(aName,aValue);
hgs
parents:
diff changeset
    88
hgs
parents:
diff changeset
    89
    if(aName==_L("gradientUnits")||aName==_L("patternUnits"))
hgs
parents:
diff changeset
    90
		{
hgs
parents:
diff changeset
    91
        if(aValue==_L("userSpaceOnUse"))
hgs
parents:
diff changeset
    92
            iGradientPaint->iGradientUnits = KuserSpaceOnUse;
hgs
parents:
diff changeset
    93
        else if(aValue==_L("objectBoundingBox"))
hgs
parents:
diff changeset
    94
            iGradientPaint->iGradientUnits = KobjectBoundingBox;
hgs
parents:
diff changeset
    95
		}
hgs
parents:
diff changeset
    96
    else if(aName==_L("spreadMethod"))
hgs
parents:
diff changeset
    97
		{
hgs
parents:
diff changeset
    98
        if(aValue==_L("pad"))
hgs
parents:
diff changeset
    99
			iGradientPaint->iSpreadMethod = KspreadMethodPad;
hgs
parents:
diff changeset
   100
        else if(aValue==_L("reflect"))
hgs
parents:
diff changeset
   101
			iGradientPaint->iSpreadMethod = KspreadMethodReflect;
hgs
parents:
diff changeset
   102
        else if(aValue==_L("repeat"))
hgs
parents:
diff changeset
   103
			iGradientPaint->iSpreadMethod = KspreadMethodRepeat;
hgs
parents:
diff changeset
   104
		}
hgs
parents:
diff changeset
   105
	return KErrNone;
hgs
parents:
diff changeset
   106
	}
hgs
parents:
diff changeset
   107
hgs
parents:
diff changeset
   108
TInt CSvgGradientElementImpl::SetAttributeIntL( const TInt aNameId,
hgs
parents:
diff changeset
   109
                                        const TInt32 aValue )
hgs
parents:
diff changeset
   110
{
hgs
parents:
diff changeset
   111
	if(aNameId == KAtrSpreadMethods)
hgs
parents:
diff changeset
   112
	{
hgs
parents:
diff changeset
   113
hgs
parents:
diff changeset
   114
			iGradientPaint->iSpreadMethod = (TUint16) aValue;
hgs
parents:
diff changeset
   115
	}
hgs
parents:
diff changeset
   116
hgs
parents:
diff changeset
   117
	else if(aNameId == KAtrGradientUnits)
hgs
parents:
diff changeset
   118
		{
hgs
parents:
diff changeset
   119
		iGradientPaint->iGradientUnits = (TUint16) aValue;
hgs
parents:
diff changeset
   120
		}
hgs
parents:
diff changeset
   121
hgs
parents:
diff changeset
   122
	else
hgs
parents:
diff changeset
   123
		CSvgElementImpl::SetAttributeIntL (aNameId,aValue);
hgs
parents:
diff changeset
   124
	return 0;
hgs
parents:
diff changeset
   125
}
hgs
parents:
diff changeset
   126
TInt CSvgGradientElementImpl::GetAttributeIntL( const TInt aNameId, TInt32& aValue )
hgs
parents:
diff changeset
   127
{
hgs
parents:
diff changeset
   128
	if(aNameId == KAtrSpreadMethods)
hgs
parents:
diff changeset
   129
	{
hgs
parents:
diff changeset
   130
		aValue = iGradientPaint->iSpreadMethod ;
hgs
parents:
diff changeset
   131
	}
hgs
parents:
diff changeset
   132
hgs
parents:
diff changeset
   133
	else if(aNameId == KAtrGradientUnits)
hgs
parents:
diff changeset
   134
		{
hgs
parents:
diff changeset
   135
		aValue = iGradientPaint->iGradientUnits ;
hgs
parents:
diff changeset
   136
		}
hgs
parents:
diff changeset
   137
hgs
parents:
diff changeset
   138
	else
hgs
parents:
diff changeset
   139
		CSvgElementImpl::GetAttributeIntL (aNameId,aValue);
hgs
parents:
diff changeset
   140
	return 0;
hgs
parents:
diff changeset
   141
}
hgs
parents:
diff changeset
   142
hgs
parents:
diff changeset
   143
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   144
// Adjusts the offset values of all the previously added elements such that
hgs
parents:
diff changeset
   145
// each gradient offset value is greater than the previous gradient stop's
hgs
parents:
diff changeset
   146
// offset value.
hgs
parents:
diff changeset
   147
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   148
void CSvgGradientElementImpl::UpdateOffsetValues(CSvgStopElementImpl *aStopEl)
hgs
parents:
diff changeset
   149
{
hgs
parents:
diff changeset
   150
    TInt count = iSvgStopElementArray->Count();
hgs
parents:
diff changeset
   151
    
hgs
parents:
diff changeset
   152
    if (count == 1)
hgs
parents:
diff changeset
   153
        {
hgs
parents:
diff changeset
   154
    	return;
hgs
parents:
diff changeset
   155
        }
hgs
parents:
diff changeset
   156
    
hgs
parents:
diff changeset
   157
    TFloatFixPt aNewOffset = aStopEl->iOffset;
hgs
parents:
diff changeset
   158
    
hgs
parents:
diff changeset
   159
    //Since the Stop Element is already added, The loop runs for "count - 1" times.
hgs
parents:
diff changeset
   160
    for (TInt i=0; i < count - 1; i++)
hgs
parents:
diff changeset
   161
	     {
hgs
parents:
diff changeset
   162
		 TFloatFixPt OldOffset = iSvgStopElementArray->operator[](i)->iOffset;
hgs
parents:
diff changeset
   163
		
hgs
parents:
diff changeset
   164
		 if ( OldOffset >= aNewOffset )
hgs
parents:
diff changeset
   165
		     {
hgs
parents:
diff changeset
   166
		     //set this latter value up so it has at least as high a value as the
hgs
parents:
diff changeset
   167
			 //previous stop offsets
hgs
parents:
diff changeset
   168
			 aStopEl->iOffset = OldOffset;
hgs
parents:
diff changeset
   169
			 
hgs
parents:
diff changeset
   170
			 if (aStopEl->iOffset == TFloatFixPt(0))
hgs
parents:
diff changeset
   171
				 {
hgs
parents:
diff changeset
   172
				 aStopEl->iOffset += TFloatFixPt(.001f);
hgs
parents:
diff changeset
   173
				 }
hgs
parents:
diff changeset
   174
				else
hgs
parents:
diff changeset
   175
				 {
hgs
parents:
diff changeset
   176
				 //two stop offsets have the same value now so make the one before ever so slightly less
hgs
parents:
diff changeset
   177
				 //the latter one will control the "overlap point"
hgs
parents:
diff changeset
   178
				 OldOffset -= TFloatFixPt(.001f);
hgs
parents:
diff changeset
   179
				 }
hgs
parents:
diff changeset
   180
				
hgs
parents:
diff changeset
   181
			 iSvgStopElementArray->operator[](i)->iOffset = OldOffset;
hgs
parents:
diff changeset
   182
		     }
hgs
parents:
diff changeset
   183
	     }
hgs
parents:
diff changeset
   184
    	
hgs
parents:
diff changeset
   185
}
hgs
parents:
diff changeset
   186
hgs
parents:
diff changeset
   187
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   188
// The function not only adds the element in Stop element array but also
hgs
parents:
diff changeset
   189
// adjusts the offset values of all the previously added elements such that
hgs
parents:
diff changeset
   190
// each gradient offset value is greater than the previous gradient stop's
hgs
parents:
diff changeset
   191
// offset value.
hgs
parents:
diff changeset
   192
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   193
void CSvgGradientElementImpl::AddStopElementInArray(CSvgStopElementImpl *aStopEl)
hgs
parents:
diff changeset
   194
	{
hgs
parents:
diff changeset
   195
	
hgs
parents:
diff changeset
   196
	TBool lFound = EFalse;
hgs
parents:
diff changeset
   197
	
hgs
parents:
diff changeset
   198
	TInt err = iSvgStopElementArray->Find(aStopEl);
hgs
parents:
diff changeset
   199
	
hgs
parents:
diff changeset
   200
	if( err != KErrNotFound )
hgs
parents:
diff changeset
   201
	   {
hgs
parents:
diff changeset
   202
	   lFound = ETrue;
hgs
parents:
diff changeset
   203
	   }
hgs
parents:
diff changeset
   204
		
hgs
parents:
diff changeset
   205
	if(!lFound)
hgs
parents:
diff changeset
   206
	   {
hgs
parents:
diff changeset
   207
	   iSvgStopElementArray->Append(aStopEl);
hgs
parents:
diff changeset
   208
	   UpdateOffsetValues(aStopEl);
hgs
parents:
diff changeset
   209
	   }
hgs
parents:
diff changeset
   210
	
hgs
parents:
diff changeset
   211
	}
hgs
parents:
diff changeset
   212
hgs
parents:
diff changeset
   213
void CSvgGradientElementImpl::SetGradientTransMatrix(SVGReal aMatrix[2][3])
hgs
parents:
diff changeset
   214
	{
hgs
parents:
diff changeset
   215
	TGfxAffineTransform tempTransform;
hgs
parents:
diff changeset
   216
    MSvgTransformList*  trList;
hgs
parents:
diff changeset
   217
    GetTransform( trList );
hgs
parents:
diff changeset
   218
    TGfxAffineTransform originalTransform   = trList->GetItem( 0 );
hgs
parents:
diff changeset
   219
    tempTransform.iM00.iValue = aMatrix[0][0];
hgs
parents:
diff changeset
   220
    tempTransform.iM01.iValue = aMatrix[0][1];
hgs
parents:
diff changeset
   221
    tempTransform.iM02.iValue = aMatrix[0][2];
hgs
parents:
diff changeset
   222
    tempTransform.iM10.iValue = aMatrix[1][0];
hgs
parents:
diff changeset
   223
    tempTransform.iM11.iValue = aMatrix[1][1];
hgs
parents:
diff changeset
   224
	tempTransform.iM12.iValue = aMatrix[1][2];
hgs
parents:
diff changeset
   225
hgs
parents:
diff changeset
   226
    TUint32 lTransType = KTransformIdentity;
hgs
parents:
diff changeset
   227
hgs
parents:
diff changeset
   228
    TFloatFixPt KZero;
hgs
parents:
diff changeset
   229
    TFloatFixPt KOne( KFloatFixOne );
hgs
parents:
diff changeset
   230
    if ( tempTransform.iM02 != KZero || tempTransform.iM12 != KZero )
hgs
parents:
diff changeset
   231
        lTransType |= KTransformTranslate;
hgs
parents:
diff changeset
   232
hgs
parents:
diff changeset
   233
    if ( tempTransform.iM00 != KZero || tempTransform.iM11 != KZero )
hgs
parents:
diff changeset
   234
        lTransType |= KTransformScale;
hgs
parents:
diff changeset
   235
hgs
parents:
diff changeset
   236
    if ( tempTransform.iM01 != KZero || tempTransform.iM10 != KZero )
hgs
parents:
diff changeset
   237
        lTransType |= KTransformShear;
hgs
parents:
diff changeset
   238
hgs
parents:
diff changeset
   239
    if ( (int)lTransType == KTransformScale &&
hgs
parents:
diff changeset
   240
                    tempTransform.iM00 == KOne && tempTransform.iM11 == KOne )
hgs
parents:
diff changeset
   241
         lTransType = KTransformIdentity;
hgs
parents:
diff changeset
   242
hgs
parents:
diff changeset
   243
	tempTransform.iTransType = lTransType;
hgs
parents:
diff changeset
   244
hgs
parents:
diff changeset
   245
    originalTransform.Concatenate( tempTransform );
hgs
parents:
diff changeset
   246
    trList->ReplaceItem( originalTransform, 0 );
hgs
parents:
diff changeset
   247
	}