svgtopt/SVG/SVGImpl/src/SVGPathDataParser.cpp
changeset 0 d46562c3d99d
equal deleted inserted replaced
-1:000000000000 0:d46562c3d99d
       
     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 "SVGPathDataParser.h"
       
    20 #include "SVGPointLexer.h"
       
    21 
       
    22 #include "GfxFloatFixPt.h"
       
    23 #include "GfxAffineTransform.h"
       
    24 #include "GfxFlatteningPathIterator.h"
       
    25 
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 //
       
    29 // ---------------------------------------------------------------------------
       
    30 void TSvgPathDataParser::ParsePathData( const TDesC& aData,
       
    31                                           CGfxGeneralPath* aPath )
       
    32     {
       
    33 
       
    34 		TRAPD( error, ParsePathDataL( aData, aPath ) );
       
    35 		if ( error != KErrNone )
       
    36 		     {
       
    37 			 // error processing
       
    38 			 return;
       
    39 	         }
       
    40     }
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 // ---------------------------------------------------------------------------
       
    45 void TSvgPathDataParser::ParsePathDataL( const TDesC& aData,
       
    46                                            CGfxGeneralPath* aPath )
       
    47     {
       
    48     TUint8 lPrevCom = '\0';
       
    49 	TFloatFixPt xy[6];
       
    50 	TInt32 rslt = ETrue;
       
    51 	TUint8 com;
       
    52     TUint8 lPrevCom2 = '\0';
       
    53 
       
    54     TSvgPointLexer svgplex( aData );
       
    55 
       
    56     TFloatFixPt KZero;
       
    57     TFloatFixPt KMinVal = 0.01f;
       
    58     TFloatFixPt KOne( KFloatFixOne );
       
    59 
       
    60     while ( svgplex.GetCommand ( com ) )
       
    61         {
       
    62 
       
    63 			// Reset for command procesing
       
    64 			if( com == '\0' )
       
    65 				{
       
    66 				if( lPrevCom == 'z' ||lPrevCom == 'Z' )
       
    67 					{
       
    68 					aPath->Reset();
       
    69 					break ;
       
    70 					}
       
    71 				// As per 1.2  spec 8.3.2 The "moveto" commands
       
    72 	            // If a 'moveto' is followed by multiple pairs of coordinates without explicit commands,
       
    73 	            // the subsequent pairs shall be treated as implicit 'lineto' commands.
       
    74 
       
    75 				else if( lPrevCom == 'm')
       
    76 					{
       
    77 					com = 'l';
       
    78 					}
       
    79 				else if( lPrevCom == 'M' )
       
    80 					{
       
    81 					com = 'L';
       
    82 					}
       
    83 				else
       
    84 					{
       
    85 					com = lPrevCom;
       
    86 					}
       
    87 				}
       
    88 			else
       
    89 				{
       
    90 				lPrevCom = com;
       
    91 				}
       
    92 
       
    93 				switch ( com )
       
    94 					{
       
    95 					case 'M':
       
    96 							rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (2) );
       
    97 							if (rslt && lPrevCom2 == 'M')
       
    98 								{
       
    99 									//AJD do we want to remove this
       
   100 									// i.e. d="M 20 20 M 10 10 L 15 15
       
   101 									// has a bounding box of 5,5,10,10 or 10,10,10,10
       
   102 									//aPath->RemoveLastPathCommand();
       
   103 								}
       
   104 							if(rslt)
       
   105                                 {
       
   106                                 aPath->MoveToL( xy[0], xy[1], ETrue );
       
   107                                 }
       
   108                             break;
       
   109                     case 'm':
       
   110                             rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (2) );
       
   111                             // added to change values from 0.0 to 0.01f if both the co-oridnates
       
   112                             // are having value 0.0. 
       
   113                             // fix for bug "CGSR-78X9V2"
       
   114                             if(xy[0] == KZero && xy[1] == KZero)
       
   115                                {
       
   116                             	 xy[0] = KMinVal;
       
   117                             	 xy[1] = KMinVal;
       
   118                                }
       
   119                             if(rslt)
       
   120                                {
       
   121                                aPath->MoveToL( xy[0], xy[1], EFalse );
       
   122                                }
       
   123                             
       
   124 							break;
       
   125 					case 'L':
       
   126 							rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (2) );
       
   127 							if(rslt)
       
   128 								{
       
   129 								aPath->LineToL( xy[0], xy[1], ETrue );
       
   130 								}
       
   131 							break;
       
   132 					case 'H':
       
   133 							rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (1) );
       
   134 							if(rslt)
       
   135 								{
       
   136 								aPath->HorizonToL( xy[0], ETrue );
       
   137 								}
       
   138 							break;
       
   139 					case 'V':
       
   140 							rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (1) );
       
   141 							if(rslt)
       
   142                                 {
       
   143                                 aPath->VertToL( xy[0], ETrue );
       
   144                                 }
       
   145                             break;
       
   146                     case 'l':
       
   147                             rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (2) );
       
   148                             // added to change values from 0.0 to 0.01f if both the co-oridnates
       
   149                             // are having value 0.0. 
       
   150                             // fix for bug "CGSR-78X9V2"
       
   151                             if(xy[0] == KZero && xy[1] == KZero)
       
   152                                {
       
   153                                xy[0] = KMinVal;
       
   154                                xy[1] = KMinVal;
       
   155                                }
       
   156                            	if(rslt)
       
   157 							   {
       
   158 							   aPath->LineToL( xy[0], xy[1], EFalse );
       
   159 							   }
       
   160 						    break;
       
   161                     case 'h':
       
   162                             rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (1) );
       
   163                             // added to change values from 0.0 to 0.01f if both the co-oridnates
       
   164                             // are having value 0.0. 
       
   165                             // fix for bug "CGSR-78X9V2"
       
   166                             if(xy[0] == KZero && xy[1] == KZero)
       
   167                                {
       
   168                                xy[0] = KMinVal;
       
   169                                xy[1] = KMinVal;
       
   170                                }
       
   171                             if(rslt)
       
   172                                {
       
   173                                aPath->HorizonToL( xy[0], EFalse );
       
   174                                }
       
   175                              
       
   176                             break;
       
   177                     case 'v':
       
   178                             rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (1) );
       
   179                             // added to change values from 0.0 to 0.01f if both the co-oridnates
       
   180                             // are having value 0.0. 
       
   181                             // fix for bug "CGSR-78X9V2"
       
   182                             if(xy[0] == KZero && xy[1] == KZero)
       
   183                                {
       
   184                                xy[0] = KMinVal;
       
   185                                xy[1] = KMinVal;
       
   186                                }
       
   187                             if(rslt)
       
   188                                {
       
   189                                aPath->VertToL( xy[0], EFalse );
       
   190                                }
       
   191                             
       
   192 							break;
       
   193 					case 'C':
       
   194 							rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (6) );
       
   195 							if( rslt )
       
   196 								{
       
   197 								aPath->CubicToL( xy[0],
       
   198 										 xy[1],
       
   199 										 xy[2],
       
   200 										 xy[3],
       
   201 										 xy[4],
       
   202 										 xy[5],
       
   203 										 ETrue );
       
   204 								}
       
   205 							break;
       
   206 					case 'c':
       
   207 							rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (6) );
       
   208 							if(rslt)
       
   209 								{
       
   210 								aPath->CubicToL( xy[0],
       
   211 												 xy[1],
       
   212 												 xy[2],
       
   213 												 xy[3],
       
   214 												 xy[4],
       
   215 												 xy[5],
       
   216 												 EFalse );
       
   217 								}
       
   218 							break;
       
   219 					case 'S':
       
   220 							rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (4) );
       
   221 							if(rslt)
       
   222 								{
       
   223 								aPath->ShortCubicToL( xy[0],
       
   224 													  xy[1],
       
   225 													  xy[2],
       
   226 													  xy[3],
       
   227 													  ETrue );
       
   228 								}
       
   229 							break;
       
   230 					case 's':
       
   231 							rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (4) );
       
   232 							if(rslt)
       
   233 								{
       
   234 								aPath->ShortCubicToL( xy[0],
       
   235 													  xy[1],
       
   236 													  xy[2],
       
   237 													  xy[3],
       
   238 													  EFalse );
       
   239 								}
       
   240 							break;
       
   241 					case 'Q':
       
   242 							rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (4) );
       
   243 							if( rslt )
       
   244 								{
       
   245 								aPath->QuadToL( xy[0],
       
   246 												  xy[1],
       
   247 												  xy[2],
       
   248 												  xy[3],
       
   249 												ETrue );
       
   250 								}
       
   251 							break;
       
   252 					case 'T':
       
   253 							rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (2) );
       
   254 							if(rslt)
       
   255 								{
       
   256 								
       
   257 								// If there is no previous command or if
       
   258 								// the previous command was not a Q, q, T or t, the control point shall be current point.
       
   259 								if ( lPrevCom2 !='Q' && lPrevCom2 != 'q' && lPrevCom2 != 'T'
       
   260 								&& lPrevCom2 != 't' )
       
   261 								   {
       
   262 								   aPath->QuadToLWithNoControlPoint( xy[0], xy[1] );
       
   263 							   }
       
   264 								else
       
   265 								   {
       
   266 								   aPath->ShortQuadToL( xy[0], xy[1], ETrue );	
       
   267 								   }
       
   268 								}
       
   269 							break;
       
   270 					case 'q':
       
   271 							rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (4) );
       
   272 							if(rslt)
       
   273 								{
       
   274 								aPath->QuadToL( xy[0],
       
   275 										  xy[1],
       
   276 										  xy[2],
       
   277                                           xy[3],
       
   278                                         EFalse );
       
   279                                 }
       
   280                             break;
       
   281                     case 't':
       
   282                             rslt = svgplex.GetNextWithNumOfPoints( xy ,TInt (2) );
       
   283                             if(xy[0] != KZero || xy[1] != KZero)
       
   284                                 {
       
   285 								if(rslt)
       
   286 									{
       
   287 									aPath->ShortQuadToL( xy[0], xy[1], EFalse );
       
   288 									}
       
   289 								}
       
   290 							break;
       
   291 					case 'Z':
       
   292 					case 'z':
       
   293 							aPath->ClosePathL();
       
   294 							break;
       
   295 
       
   296 					default:
       
   297 							rslt=0;
       
   298 							break;
       
   299 
       
   300 					}// switch(lPrevCom)
       
   301 
       
   302 				if( !rslt )
       
   303 					{
       
   304 					// This is a failing path case reset.
       
   305 					aPath->Reset();
       
   306 					break ;
       
   307 					}
       
   308 
       
   309 				lPrevCom2 = com;
       
   310         }
       
   311     svgplex.Cleanup();
       
   312     }
       
   313 
       
   314 //
       
   315 // ---------------------------------------------------------------------------
       
   316 //
       
   317 // ---------------------------------------------------------------------------
       
   318 void TSvgPathDataParser::ParsePointDataL( const TDesC& aData,
       
   319                                            CGfxGeneralPath* aPath )
       
   320     {
       
   321     TSvgPointLexer svgplex( aData );
       
   322     TBool first = ETrue;
       
   323     while ( !svgplex.IsDone() )
       
   324         {
       
   325         TChar com;
       
   326         TFloatFixPt x, y;
       
   327         TInt32 rslt = svgplex.GetNextPoint( com, x, y );
       
   328 
       
   329         if ( rslt != KErrNone )
       
   330             {
       
   331             aPath->Reset();
       
   332             break;
       
   333             }
       
   334         if ( first )
       
   335             {
       
   336             aPath->MoveToL( x, y, ETrue );
       
   337             first = EFalse;
       
   338             }
       
   339         else
       
   340             {
       
   341             aPath->LineToL( x, y, ETrue );
       
   342             }
       
   343         }
       
   344     svgplex.Cleanup();
       
   345     }
       
   346 
       
   347 //
       
   348 // ---------------------------------------------------------------------------
       
   349 //
       
   350 // ---------------------------------------------------------------------------
       
   351 TFloatFixPt TSvgPathDataParser::PathLengthL( CGfxGeneralPath* aPath )
       
   352     {
       
   353     CGfxFlatteningPathIterator* pitr;
       
   354     TGfxAffineTransform ident;
       
   355     pitr = CGfxFlatteningPathIterator::NewL( aPath, & ident, 3 );
       
   356 
       
   357 
       
   358     CleanupStack::PushL( pitr );
       
   359 
       
   360     TFloatFixPt len;
       
   361     TFloatFixPt lx, ly;
       
   362     TFloatFixPt dx, dy, int0x7f( KMAXFLOATFIX );
       
   363     TFloatFixPt tmpcoords[6];
       
   364     while ( !pitr->IsDone() )
       
   365         {
       
   366         switch ( pitr->CurrentSegment( tmpcoords ) )
       
   367             {
       
   368             case EGfxSegMoveTo:
       
   369                 lx = tmpcoords[0];
       
   370                 ly = tmpcoords[1];
       
   371                 break;
       
   372             case EGfxSegLineTo:
       
   373                 dx = lx - tmpcoords[0];
       
   374                 dy = ly - tmpcoords[1];
       
   375                 if ( dx > int0x7f || dy > int0x7f )
       
   376                     {
       
   377                     // calculate as integer
       
   378                     TInt32 tmplen, tx, ty;
       
   379                     tx = ( TInt32 ) dx;
       
   380                     ty = ( TInt32 ) dy;
       
   381 	#ifdef SVG_FLOAT_BUILD
       
   382                     tmplen = TFloatFixPt::Sqrt(tx * tx + ty * ty);
       
   383 	#else
       
   384                     tmplen = TFloatFixPt::FixedSqrtGeneral( tx * tx + ty * ty, 0 );
       
   385 	#endif
       
   386                     len += TFloatFixPt( tmplen );
       
   387                     }
       
   388                 else
       
   389                     {
       
   390                     // calculate as TFixPt
       
   391 	
       
   392                     len += TFloatFixPt::Sqrt( dx * dx + dy * dy );
       
   393                     }
       
   394                 lx = tmpcoords[0];
       
   395                 ly = tmpcoords[1];
       
   396                 break;
       
   397             case EGfxSegClose:
       
   398                 ;// Not support multiple path length
       
   399                 break;
       
   400             default:
       
   401                 break;
       
   402             }
       
   403         pitr->NextL();
       
   404         }
       
   405 
       
   406     CleanupStack::PopAndDestroy( 1 );
       
   407 
       
   408     return len;
       
   409     }