textrendering/texthandling/stext/ParseLst.cpp
changeset 0 1fb32624e06b
child 40 91ef7621b7fc
equal deleted inserted replaced
-1:000000000000 0:1fb32624e06b
       
     1 /*
       
     2 * Copyright (c) 1999-2009 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "TXTSTD.H"
       
    20 #include "ParseLst.h"
       
    21 //++ sort out definitive hash includes
       
    22 
       
    23 
       
    24 CParserList::CParserItem::CParserItem(MParser* aParser, const TUint aTagIndex)
       
    25 : iParser(aParser),
       
    26   iTagIndex(aTagIndex)
       
    27 	{
       
    28 	}
       
    29 
       
    30 
       
    31 // Panic the process with UikParse as the category.
       
    32 void CParserList::Panic(TParserListPanic aPanic) const
       
    33 	{
       
    34 	_LIT(panicStr, "ParseLst");
       
    35 	User::Panic(panicStr, aPanic);
       
    36 	}
       
    37 
       
    38 
       
    39 CParserList::CParserList()
       
    40 : iParserList(4)
       
    41 	{
       
    42 	iHighestIndex = 0;
       
    43 	iNumberInList = 0;
       
    44 	}
       
    45 
       
    46 
       
    47 CParserList::~CParserList()
       
    48 	{
       
    49 	iParserList.ResetAndDestroy();	// Reset the list and destroy the CParserItems
       
    50 	iParserList.Close();			// but leave the parsers and free the resources
       
    51 	}
       
    52 
       
    53 
       
    54 // Add parser to appropriate list
       
    55 void CParserList::ActivateAParserL(MParser* aParser, const TBool aDefaultParser)
       
    56 	{
       
    57 	// Check the index isn't rolling over
       
    58 	// N.B. This is somewhat unlikely. Since index is effectively a 29-bit
       
    59 	// uint (I'm using the top 3 bits as flags) this allows 268435456 parsers
       
    60 	// to be installed. (Assuming we deinstall almost as many, as we went
       
    61 	// along, to avoid OOM.) At 50 per sec that's 20 years continuous action!
       
    62 	__ASSERT_DEBUG((iHighestIndex < EMaxParserIndex), Panic(EParserIndexRollover));
       
    63 	__ASSERT_DEBUG(aParser, Panic(EParserNullPtr));
       
    64 	TUint parserIndex = iHighestIndex + 1;
       
    65 	// Adjust flags to describe parser
       
    66 	if (aParser->ReformatOnRecognise())
       
    67 		parserIndex |= static_cast<TUint>( EReformatOnRecogniseFlag );
       
    68 	if (aParser->ReformatOnRollover())
       
    69 		parserIndex |= EReformatOnRolloverFlag;
       
    70 	if (aDefaultParser)
       
    71 		parserIndex |= EDefaultParserFlag;
       
    72 	// Create a parser item
       
    73 	TInt result;
       
    74 	CParserItem* parserItem = new (ELeave) CParserItem(aParser, parserIndex);
       
    75 	++iHighestIndex;
       
    76 	if (aDefaultParser)
       
    77 		{
       
    78 		CleanupStack::PushL(parserItem);
       
    79 		result = iParserList.Append(parserItem);
       
    80 		if (result) // We couldn't get it in the main list
       
    81 			User::Leave(result);
       
    82 		CleanupStack::Pop();
       
    83 		}
       
    84 	else
       
    85 		{
       
    86 		// Look for the end of the specific parsers and the start of the default set
       
    87 		TInt count;
       
    88 		for (count = 0; count < iNumberInList; count++)
       
    89 			{
       
    90 			if (DefaultParser(iParserList[count]->TagIndex()))
       
    91 				break;
       
    92 			}
       
    93 		CleanupStack::PushL(parserItem);
       
    94 		result = iParserList.Insert(parserItem, count);
       
    95 		if (result) // We couldn't get it in the main list
       
    96 			User::Leave(result);
       
    97 		CleanupStack::Pop();
       
    98 		}
       
    99 	iNumberInList++;
       
   100 	}
       
   101 
       
   102 
       
   103 // Activate an individual parser
       
   104 void CParserList::ActivateParserL(MParser* aParser)
       
   105 	{
       
   106 	ActivateAParserL(aParser, EFalse);
       
   107 	}
       
   108 
       
   109 
       
   110 // N.B. We can't just delete CParserList and zero TLS when we get back
       
   111 // to a list with no specific or default parsers because there could be
       
   112 // an EText left with a local ptr to it.
       
   113 void CParserList::DeactivateParser(MParser* aParser)
       
   114 	{
       
   115 	__ASSERT_DEBUG(iNumberInList, Panic(EParserNoneActive));
       
   116 	__ASSERT_DEBUG(aParser, Panic(EParserNullPtr));
       
   117 	// Run thru list till find entry we need
       
   118 	TInt count;
       
   119 	for (count = 0; count < iNumberInList; count++)
       
   120 		{
       
   121 		if (iParserList[count]->Parser() == aParser)
       
   122 			{
       
   123 			delete iParserList[count];
       
   124 			iParserList.Remove(count);
       
   125 			break;
       
   126 			}
       
   127 		}
       
   128 	__ASSERT_DEBUG((count < iNumberInList), Panic(EParserInstanceNotActive));
       
   129 	iNumberInList--;
       
   130 	}
       
   131 
       
   132 
       
   133 // Activate a parser as one of the default set
       
   134 void CParserList::ActivateDefaultParserL(MParser* aParser)
       
   135 	{
       
   136 	ActivateAParserL(aParser, ETrue);
       
   137 	}
       
   138 
       
   139 
       
   140 //++ Put comment here
       
   141 void CParserList::DeactivateParserDefaults()
       
   142 	{
       
   143 	if (iNumberInList)
       
   144 		{
       
   145 		// Take them out of the list
       
   146 		while (iNumberInList && DefaultParser(iParserList[iNumberInList - 1]->TagIndex()))
       
   147 			{
       
   148 			// Tell the parser to free itself
       
   149 			iParserList[iNumberInList - 1]->Parser()->Release();
       
   150 			// Delete the item that refers to it
       
   151 			delete iParserList[iNumberInList - 1];
       
   152 			// Remove the entry from the list
       
   153 			iParserList.Remove(iNumberInList - 1);
       
   154 			iNumberInList--;
       
   155 			}
       
   156 		}
       
   157 	}
       
   158 
       
   159 
       
   160 // Called by EText to scan an area of text
       
   161 TBool CParserList::ParseThisText(CRichText& aTextObj,TInt aStartScan,TInt aScanLength,TInt& aStartOfTags,TInt& aLength)
       
   162 	{
       
   163 	TBool foundSomething = EFalse;
       
   164 	TCharFormatX format;
       
   165 	TCharFormatXMask varies;
       
   166 	TPtrC ptr;
       
   167 	TInt endRange;
       
   168 
       
   169 	// Scan either side of the range in case part of some tagged text was deleted.
       
   170 	if (aStartScan > 0)
       
   171 		{
       
   172 		aStartScan--;
       
   173 		aScanLength++;
       
   174 		}
       
   175 	if (aStartScan + aScanLength < aTextObj.DocumentLength())
       
   176 		aScanLength++;
       
   177 
       
   178 	if (iNumberInList && aScanLength)
       
   179 		{
       
   180 		aStartOfTags = aStartScan + aScanLength;
       
   181 		aLength = 0;
       
   182 		for (TInt count = 0; count < iNumberInList; count++)
       
   183 			{
       
   184 			// For each parser in the list
       
   185 			TUint tagIndex = iParserList[count]->TagIndex();
       
   186 			TInt pos;
       
   187 			MParser* parser = iParserList[count]->Parser();
       
   188 			TInt localStartScan = aStartScan;
       
   189 			TInt localScanLength = aScanLength;
       
   190 			// Start by removing existing tags for this parser. This ensures
       
   191 			// that old tags that have been invalidated by subsequent editing
       
   192 			// are removed. Any that are still valid will be replaced.
       
   193 			aTextObj.GetExtendedCharFormat(format, varies, localStartScan, aScanLength);
       
   194 
       
   195 			if (format.iParserTag || varies.AttribIsSet(EAttParserTag))
       
   196 				{
       
   197 				// At least some of the object contains a non-zero tag - go through it
       
   198 				// Are we starting part way through a tag?
       
   199 				aTextObj.GetExtendedCharFormat(format, varies, localStartScan, 1);
       
   200 				if (format.iParserTag == tagIndex)
       
   201 					{
       
   202 					// The first char of this range has the current parsers tag
       
   203 					// so we need to check backwards for the start of that tag
       
   204 					for (pos = localStartScan; pos > 0; pos--)
       
   205 						{
       
   206 						aTextObj.GetExtendedCharFormat(format, varies, pos - 1, 1);
       
   207 						if (format.iParserTag != tagIndex)
       
   208 							break;
       
   209 						}
       
   210 					// Since we are going to remove a tag starting from here
       
   211 					// we need to allow this area to be rescanned
       
   212 					localScanLength += localStartScan - pos;
       
   213 					localStartScan = pos;
       
   214 					}
       
   215 				// What about off the end?
       
   216 				aTextObj.GetExtendedCharFormat(format, varies, localStartScan + localScanLength - 1, 1);
       
   217 				if (format.iParserTag == tagIndex)
       
   218 					{
       
   219 					// The last char of this range has the current parsers tag
       
   220 					// so we need to check forwards for the end of that tag
       
   221 					pos = localStartScan + localScanLength;
       
   222 					TInt end = aTextObj.DocumentLength();
       
   223 					while (pos < end)
       
   224 						{
       
   225 						aTextObj.GetTextAndExtendedFormat(ptr, format, pos);
       
   226 						if (format.iParserTag != tagIndex)
       
   227 							break;
       
   228 						pos += ptr.Length();
       
   229 						}
       
   230 					// Adjust scan length
       
   231 					localScanLength = pos - localStartScan;
       
   232 					}
       
   233 				pos = localStartScan;
       
   234 				while (pos < localStartScan + localScanLength)
       
   235 					{
       
   236 					// Run along the scan range
       
   237 					aTextObj.GetTextAndExtendedFormat(ptr, format, pos);
       
   238 					if (format.iParserTag == tagIndex)
       
   239 						{
       
   240 						// Remove this tag
       
   241 						format.iParserTag = 0;
       
   242 						varies.ClearAll();
       
   243 						varies.SetAttrib(EAttParserTag);
       
   244 						TRAPD(leaveCode, aTextObj.ApplyExtendedCharFormatL(format, varies, pos, ptr.Length()));
       
   245 						if (leaveCode==KErrNone)
       
   246 							foundSomething = ETrue;	// We are removing a tag
       
   247 						if (aLength)
       
   248 							{
       
   249 							if (pos < aStartOfTags)
       
   250 								{
       
   251 								aLength += aStartOfTags - pos;
       
   252 								aStartOfTags = pos;
       
   253 								}
       
   254 							if (pos + ptr.Length() > aStartOfTags + aLength)
       
   255 								aLength = pos + ptr.Length() - aStartOfTags;
       
   256 							}
       
   257 						else
       
   258 							{
       
   259 							aStartOfTags = pos;
       
   260 							aLength = ptr.Length();
       
   261 							}
       
   262 						}
       
   263 					pos += ptr.Length();
       
   264 					}
       
   265 				}
       
   266 			endRange = localStartScan + localScanLength;
       
   267 
       
   268 			// For this parser, run through text looking for changes
       
   269 			TBool allowBack = ETrue;
       
   270 			for (;;)	// Run forever
       
   271 				{
       
   272 				TInt localStartTag = aTextObj.DocumentLength();
       
   273 				TInt localTagLength = 0;
       
   274 				TInt result = parser->ParseThisText(aTextObj, allowBack, localStartScan, localScanLength, localStartTag, localTagLength);
       
   275 				if (!result)
       
   276 					break;
       
   277 				__ASSERT_DEBUG(allowBack || (localStartTag >= localStartScan), Panic(EParserIgnoringAllowFlag));
       
   278 				TInt startNewTag = localStartTag;
       
   279 				TInt lengthNewTag = localTagLength;
       
   280 				aTextObj.GetExtendedCharFormat(format, varies, localStartTag, localTagLength);
       
   281 				if (format.iParserTag || varies.AttribIsSet(EAttParserTag))
       
   282 					{
       
   283 					// At least some of this area contains a non-zero tag - go through it
       
   284 					pos = localStartTag;
       
   285 					TBool higher = EFalse;
       
   286 					while (pos < localStartTag + localTagLength)
       
   287 						{
       
   288 						aTextObj.GetTextAndExtendedFormat(ptr, format, pos);
       
   289 						if (format.iParserTag && (MaskedTag(format.iParserTag) < MaskedTag(tagIndex)))
       
   290 							{
       
   291 							// A higher precedence tag is already here so we can't
       
   292 							// insert our tag - let's see how far it goes
       
   293 							TUint tag = format.iParserTag;	// Stash tag before overwriting it
       
   294 							TInt len = aTextObj.DocumentLength();
       
   295 							while (pos < len)
       
   296 								{
       
   297 								aTextObj.GetTextAndExtendedFormat(ptr, format, pos);
       
   298 								if (format.iParserTag != tag)
       
   299 									break;
       
   300 								pos += ptr.Length();
       
   301 								}
       
   302 							result = EFalse;
       
   303 							startNewTag = pos;
       
   304 							lengthNewTag = 0;
       
   305 							break;
       
   306 							}
       
   307 						// If there aren't any higher precedence tags in here then this
       
   308 						// will save us having to go through again if there aren't any
       
   309 						// lower precedence tags either
       
   310 						if (MaskedTag(format.iParserTag) >= MaskedTag(tagIndex))
       
   311 							higher = ETrue;
       
   312 						pos += ptr.Length();
       
   313 						}
       
   314 					if (higher)
       
   315 						{
       
   316 						// There are lower or equal precedence tags in this range
       
   317 						// Do they extend back off the start?
       
   318 						aTextObj.GetExtendedCharFormat(format, varies, localStartTag, 1);
       
   319 						if (format.iParserTag)
       
   320 							{
       
   321 							// need to check backwards
       
   322 							TUint tag = format.iParserTag;	// Stash tag before overwriting it
       
   323 							for (pos = localStartTag; pos > 0; pos--)
       
   324 								{
       
   325 								aTextObj.GetExtendedCharFormat(format, varies, pos - 1, 1);
       
   326 								if (format.iParserTag != tag)
       
   327 									break;
       
   328 								}
       
   329 							localTagLength += localStartTag - pos;
       
   330 							localStartTag = pos;
       
   331 							}
       
   332 						// What about off the end?
       
   333 						pos = localStartTag + localTagLength;
       
   334 						aTextObj.GetExtendedCharFormat(format, varies, pos - 1, 1);
       
   335 						if (format.iParserTag)
       
   336 							{
       
   337 							// need to check forwards
       
   338 							TUint tag = format.iParserTag;	// Stash tag before overwriting it
       
   339 							TInt len = aTextObj.DocumentLength();
       
   340 							while (pos < len)
       
   341 								{
       
   342 								aTextObj.GetTextAndExtendedFormat(ptr, format, pos);
       
   343 								if (format.iParserTag != tag)
       
   344 									break;
       
   345 								pos += ptr.Length();
       
   346 								}
       
   347 							localTagLength = pos - localStartTag;
       
   348 							}
       
   349 
       
   350 						// Remove all tags in this area - they all have lower precedence
       
   351 						format.iParserTag = 0;
       
   352 						varies.ClearAll();
       
   353 						varies.SetAttrib(EAttCharLanguage);
       
   354 						TRAPD(leaveCode, aTextObj.ApplyExtendedCharFormatL(format, varies, localStartTag, localTagLength));
       
   355 						if (leaveCode==KErrNone)
       
   356 							foundSomething = ETrue;	// We are removing a tag
       
   357 						}
       
   358 					}
       
   359 
       
   360 				if (result)
       
   361 					{
       
   362 					// Format tag this area with tagIndex
       
   363 					format.iParserTag = tagIndex;
       
   364 					varies.ClearAll();
       
   365 					varies.SetAttrib(EAttParserTag);
       
   366 					// Use the original length, not the possibly expanded version
       
   367 					TRAPD(leaveCode, aTextObj.ApplyExtendedCharFormatL(format, varies, startNewTag, lengthNewTag));
       
   368 					if (leaveCode==KErrNone)
       
   369 						foundSomething = ETrue;		// We are applying a tag
       
   370 					if (aLength)
       
   371 						{
       
   372 						if (localStartTag < aStartOfTags)
       
   373 							{
       
   374 							aLength += aStartOfTags - localStartTag;
       
   375 							aStartOfTags = localStartTag;
       
   376 							}
       
   377 						if (localStartTag + localTagLength > aStartOfTags + aLength)
       
   378 							aLength = localStartTag + localTagLength - aStartOfTags;
       
   379 						}
       
   380 					else
       
   381 						{
       
   382 						aStartOfTags = localStartTag;
       
   383 						aLength = localTagLength;
       
   384 						}
       
   385 					}
       
   386 				// Jump over
       
   387 				localScanLength -= startNewTag + lengthNewTag - localStartScan;
       
   388 				localStartScan = startNewTag + lengthNewTag;	// Adjust start of next scan run
       
   389 				if (localStartScan >= endRange)	// Have we reached the end of the range yet?
       
   390 					break;
       
   391 				allowBack = EFalse;
       
   392 				}
       
   393 			}
       
   394 		}
       
   395 
       
   396 	return foundSomething;
       
   397 	}
       
   398 
       
   399 
       
   400 // given a tag, fetch a ptr to the parser - or null
       
   401 MParser* CParserList::ParserWithThisTag(const TUint aTagIndex) const
       
   402 	{
       
   403 	MParser* parser = NULL;
       
   404 	for (TInt count = 0; count < iNumberInList; count++)
       
   405 		{
       
   406 		if (aTagIndex == iParserList[count]->TagIndex())
       
   407 			{
       
   408 			parser = iParserList[count]->Parser();
       
   409 			break;
       
   410 			}
       
   411 		}
       
   412 	return parser;
       
   413 	}
       
   414 
       
   415 // given a ptr to a parser, fetch a tag - or zero
       
   416 TUint CParserList::TagForThisParser(const MParser *const aParser) const
       
   417 	{
       
   418 	TUint tagIndex = 0;
       
   419 	for (TInt count = 0; count < iNumberInList; count++)
       
   420 		{
       
   421 		if (aParser == iParserList[count]->Parser())
       
   422 			{
       
   423 			tagIndex = iParserList[count]->TagIndex();
       
   424 			break;
       
   425 			}
       
   426 		}
       
   427 	__ASSERT_DEBUG(tagIndex, Panic(EParserNoSuchTag));
       
   428 	return tagIndex;
       
   429 	}
       
   430 
       
   431 
       
   432 CParserData::CParserData(TInt aEndParse):
       
   433 	iStartParse(0),
       
   434 	iEndParse(aEndParse)
       
   435 	{
       
   436 	// Get parser data
       
   437 	iActiveParserList = (CParserList*)Dll::Tls();
       
   438 	iLastKnownCursor = -1;
       
   439 	if (iActiveParserList)
       
   440 		iActiveParserList->iRefCount++;
       
   441 	}
       
   442 
       
   443 
       
   444 CParserData::~CParserData()
       
   445 	{
       
   446 	if (iActiveParserList)
       
   447 		{
       
   448 		iActiveParserList->iRefCount--;
       
   449 		if ((iActiveParserList->iRefCount == 0) && (iActiveParserList->iNumberInList == 0))
       
   450 			{
       
   451 			Dll::FreeTls();
       
   452 			delete iActiveParserList;
       
   453 			}
       
   454 		}
       
   455 	}
       
   456 
       
   457 
       
   458 // Merge the specified range, which may have changed length, into the current range.
       
   459 // aOldLength is the # of chars deleted and to be removed from the range
       
   460 // aNewLength is the # of chars inserted and to be added to the range
       
   461 void CParserData::MergeRange(TInt aStart,TInt aOldLength,TInt aNewLength)
       
   462 	{
       
   463 	if (iStartParse == -1) // no current range
       
   464 		{
       
   465 		iStartParse = aStart;
       
   466 		iEndParse = aStart + aNewLength;
       
   467 		}
       
   468 	else
       
   469 		{
       
   470 		if (aStart < iStartParse)
       
   471 			iStartParse = aStart;
       
   472 		if (aStart + aOldLength > iEndParse)
       
   473 			iEndParse = aStart + aOldLength;
       
   474 		iEndParse += aNewLength - aOldLength;
       
   475 		}
       
   476 	}