svgtopt/SVG/SVGImpl/src/SVGStopElementImpl.cpp
changeset 46 88edb906c587
equal deleted inserted replaced
-1:000000000000 46:88edb906c587
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  SVG Implementation source file
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "SvgStopElementImpl.h"
       
    20 #include "SVGDocumentImpl.h"
       
    21 #include "SVGGradientElementImpl.h"
       
    22 #include "SVGIntCssValueImpl.h"
       
    23 
       
    24 _LIT(OFFSET, "offset");
       
    25 _LIT(STOPOPACITY, "stop-opacity");
       
    26 _LIT(INHERIT, "inherit");
       
    27 _LIT(STOPCOLOR, "stop-color");
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 // ---------------------------------------------------------------------------
       
    32 CSvgStopElementImpl* CSvgStopElementImpl::NewL(const TUint8 aElemID,
       
    33 											   CSvgDocumentImpl* aDoc )
       
    34 	{
       
    35     CSvgStopElementImpl *self = new (ELeave) CSvgStopElementImpl(aDoc);
       
    36     CleanupStack::PushL(self);
       
    37     self->ConstructL(aElemID);
       
    38     CleanupStack::Pop(self);
       
    39     return self;
       
    40 	}
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 // ---------------------------------------------------------------------------
       
    45 void CSvgStopElementImpl::ConstructL(const TUint8 aElemID)
       
    46 	{
       
    47     CSvgElementImpl::InitializeL( aElemID );
       
    48      iSvgStyleProperties = new(ELeave) RPointerArray<CCssValue>(KCSS_MAX_ATTR);
       
    49      User::LeaveIfError( iSvgStyleProperties->Append( NULL ) );
       
    50      iSvgStyleProperties->Remove( 0 );
       
    51 
       
    52 
       
    53    	}
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 // ---------------------------------------------------------------------------
       
    58 CSvgStopElementImpl::CSvgStopElementImpl( CSvgDocumentImpl* aDoc ):
       
    59 	iOffset(-1),// This implies that offset value is yet to be initialized.
       
    60 	iStopOpacity(1),
       
    61 	iStopColor(0),
       
    62 
       
    63 	iValidOffsetValue(ETrue)
       
    64 	{
       
    65 	SetOwnerDocument(aDoc);
       
    66 	}
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 // ---------------------------------------------------------------------------
       
    71 TInt CSvgStopElementImpl::GetAttributeFloat(const TInt aNameId, TFloatFixPt& aValue)
       
    72 {
       
    73 	switch(aNameId)
       
    74 	{
       
    75 	case KAtrOffset:
       
    76 		aValue= iOffset;
       
    77 		break;
       
    78 	case KAtrStopOpacity:
       
    79 		aValue = iStopOpacity;
       
    80 		break;
       
    81 	default:
       
    82 		CSvgElementImpl::GetAttributeFloat(aNameId,aValue);
       
    83 		break;
       
    84 	}
       
    85 
       
    86     return 0;
       
    87 
       
    88 }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 // ---------------------------------------------------------------------------
       
    93 TInt CSvgStopElementImpl::SetAttributeFloatL(const TInt aNameId,const TFloatFixPt aValue)
       
    94 {
       
    95 	switch(aNameId)
       
    96 	{
       
    97 	case KAtrOffset:
       
    98 		iOffset= aValue;
       
    99 		
       
   100 		// Adjust the value of offset
       
   101 		if(((((CSvgElementImpl*)ParentNode())->ElemID()) == KSvgRadialGradientElement) ||
       
   102 			((((CSvgElementImpl*)ParentNode())->ElemID()) == KSvgLinearGradientElement))
       
   103 		   {
       
   104 		   CSvgGradientElementImpl *parent = ((CSvgGradientElementImpl *)ParentNode());
       
   105 		    
       
   106 		    if(parent)
       
   107 		       {
       
   108 		       parent->UpdateOffsetValues((CSvgStopElementImpl*)this);
       
   109 		       }
       
   110 		   }
       
   111 		
       
   112 		break;
       
   113 	case KAtrStopOpacity:
       
   114 		iStopOpacity= aValue;
       
   115 		break;
       
   116 
       
   117 	default:
       
   118 		CSvgElementImpl::SetAttributeFloatL(aNameId,aValue);
       
   119 		break;
       
   120 	}
       
   121 
       
   122     return 0;
       
   123 
       
   124 }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 //
       
   128 // ---------------------------------------------------------------------------
       
   129 TInt CSvgStopElementImpl::GetAttributeIntL( const TInt aNameId, TInt32& aValue )
       
   130 {
       
   131 	switch(aNameId)
       
   132 	{
       
   133 		case KAtrStopColor:
       
   134 		aValue = iStopColor;
       
   135 		break;
       
   136 		default:
       
   137 			CSvgElementImpl::GetAttributeIntL(  aNameId, aValue );
       
   138 			break;
       
   139 	}
       
   140 	return 0;
       
   141 }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 // ---------------------------------------------------------------------------
       
   146 TInt CSvgStopElementImpl::SetAttributeIntL( const TInt aNameId,
       
   147                                         const TInt32 aValue )
       
   148 {
       
   149 	switch(aNameId)
       
   150 	{
       
   151 	case KAtrStopColor:
       
   152 		{
       
   153 		iStopColor = (TUint32)aValue;
       
   154 		}
       
   155 		break;
       
   156 	default:
       
   157 		 CSvgElementImpl::SetAttributeIntL( aNameId,
       
   158                                          aValue );
       
   159 		 break;
       
   160 	}
       
   161 	return 0;
       
   162 }
       
   163 
       
   164 TBool CSvgStopElementImpl::IsValidValue(const TDesC& aValue)
       
   165 {
       
   166     TBool lValidValue = ETrue;
       
   167 	// aValue can only contain these characters "-+.%0123456789", If it contains any thing
       
   168 	// other than this it should be treated as 0.
       
   169 	
       
   170 	_LIT(KValidChars,"-+.%0123456789");
       
   171 	TBufC<14> lValidChars(KValidChars);
       
   172 		
       
   173 	TInt llength = aValue.Length();
       
   174 		
       
   175 	for( int charpos = 0 ; charpos < llength; charpos++ )
       
   176 	{
       
   177 		if( lValidChars.Locate(aValue[charpos]) == KErrNotFound )
       
   178 		{
       
   179 			
       
   180 			lValidValue = EFalse;
       
   181 			break;
       
   182 		}
       
   183 	}
       
   184 	
       
   185 	return lValidValue;
       
   186 }
       
   187 
       
   188 TBool CSvgStopElementImpl::IsValidElement() const
       
   189 {
       
   190 	return iValidOffsetValue;
       
   191 }
       
   192 
       
   193 // ---------------------------------------------------------------------------
       
   194 //
       
   195 // ---------------------------------------------------------------------------
       
   196 TInt CSvgStopElementImpl::SetAttributeL(const TDesC &aName, const TDesC &aValue)
       
   197 	{
       
   198 	CSvgElementImpl::SetAttributeL(aName,aValue);
       
   199 	TFloatFixPt KZero(KFloatFixZero);
       
   200     TFloatFixPt KOne( KFloatFixOne );
       
   201 
       
   202     if(aName == OFFSET)
       
   203 		{
       
   204 		iValidOffsetValue = IsValidValue(aValue);
       
   205 			
       
   206 		if( iValidOffsetValue  )
       
   207 		   {
       
   208 		   iOffset=TFloatFixPt::ConvertString(aValue); 
       
   209 		        
       
   210 		   if (aValue.Locate('%') != KErrNotFound)
       
   211 		       {
       
   212 		 	   iOffset = iOffset / TFloatFixPt(100);
       
   213 		       }
       
   214 		    
       
   215 		    // Finally if the iOffset value is lesser than 0, it should be made to 0.
       
   216 		    if( iOffset < KZero )
       
   217 			   {
       
   218 			   iOffset = KZero ;
       
   219 			   }
       
   220 			else if( iOffset > KOne )
       
   221 			   {
       
   222 			   iOffset = KOne ;
       
   223 			   }
       
   224 		   }
       
   225 		 else
       
   226 		   {
       
   227 		   // aValue contains invalid characters hence "0" is assigned to it.
       
   228 		   // Any stop element with wrong offset value should not be considered while 
       
   229 		   // rendereing and the effect should be as if no stop-offset is declared.
       
   230 		   iOffset = KZero;
       
   231 		   }
       
   232 		 
       
   233 		}
       
   234     else if(aName == STOPOPACITY)
       
   235         {
       
   236 
       
   237         if(aValue == INHERIT)
       
   238             {
       
   239             // inherit does not make any sense since the stop-opacity is
       
   240             //specific to stop element only and a stop element can not have another stop element as
       
   241             //its child.
       
   242             iStopOpacity = KOne;
       
   243             }
       
   244         else
       
   245             {
       
   246             if( IsValidValue(aValue) )
       
   247             {
       
   248     #ifdef SVG_FLOAT_BUILD
       
   249             iStopOpacity = TFloatFixPt::ConvertString (aValue);
       
   250 
       
   251             if (!(iStopOpacity >= TFloatFixPt(0) && iStopOpacity <= TFloatFixPt(1)))
       
   252     #else
       
   253             if ( TFloatFixPt::ConvertString (aValue, iStopOpacity ) != KErrNone )
       
   254     #endif
       
   255                 {
       
   256                 iStopOpacity = KOne;
       
   257                 }
       
   258             else
       
   259                 {
       
   260                 if (aValue.Locate('%') != KErrNotFound)
       
   261                     {
       
   262                     //if stop opacity was listed as a percentage
       
   263                     iStopOpacity = iStopOpacity / TFloatFixPt(100);
       
   264                     }
       
   265 
       
   266                 if ( iStopOpacity < KZero )
       
   267                     iStopOpacity = KZero;
       
   268                 else if ( iStopOpacity > KOne )
       
   269                     iStopOpacity = KOne;
       
   270                 }
       
   271             }
       
   272                 else
       
   273                 {
       
   274                 	iStopOpacity = KOne;
       
   275                 }	
       
   276 			}
       
   277 		}
       
   278     else if (aName == STOPCOLOR)
       
   279 		{
       
   280         TUint32 tempVal;
       
   281 
       
   282         TSvgColor tColor(KGfxColorNull);
       
   283 		if( tColor.GetStringL( aValue,tempVal) )
       
   284 			{
       
   285 
       
   286             }
       
   287         else
       
   288         	{
       
   289 			tempVal = 0;
       
   290 			}
       
   291         iStopColor=tempVal;
       
   292 		}
       
   293 
       
   294     return 0;
       
   295 
       
   296 }
       
   297 
       
   298 // ---------------------------------------------------------------------------
       
   299 //
       
   300 // ---------------------------------------------------------------------------
       
   301 CSvgStopElementImpl::~CSvgStopElementImpl()
       
   302 	{
       
   303 	if ( iSvgStyleProperties )
       
   304 		{
       
   305 		iSvgStyleProperties->Close();
       
   306 		delete iSvgStyleProperties;
       
   307 		iSvgStyleProperties = NULL;
       
   308 		}
       
   309 	}
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 //
       
   313 // ---------------------------------------------------------------------------
       
   314 void CSvgStopElementImpl::SetStopColorL(TUint32 &aVal)
       
   315 	{
       
   316 	iStopColor= aVal;
       
   317 	}
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 // ---------------------------------------------------------------------------
       
   322 void CSvgStopElementImpl::Print( TBool aIsEncodeOn )
       
   323 {
       
   324 	if (!aIsEncodeOn)
       
   325 	{
       
   326 		#ifdef _DEBUG
       
   327 		RDebug::Printf("<stop stop-color=\"hmmm\" stop-opacity=\"%d\" offset=\"%d\" />",
       
   328 		/*iStopColor,*/ (int)iStopOpacity, (int)iOffset );
       
   329 		#endif
       
   330 	}
       
   331 }
       
   332