os/textandloc/fontservices/textbase/sgdi/BIDI.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/fontservices/textbase/sgdi/BIDI.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1163 @@
     1.4 +// Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Bidirectional text reordering; based on the Unicode Bidirectional Reordering Algorithm.
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <bidi.h>
    1.22 +#include "BidiCopy.h"
    1.23 +#include <s32std.h>
    1.24 +
    1.25 +const TInt KBidirectionalStateOverrideStreamValueNone = 0;
    1.26 +const TInt KBidirectionalStateOverrideStreamValueLeftToRight = 1;
    1.27 +const TInt KBidirectionalStateOverrideStreamValueRightToLeft = 2;
    1.28 +
    1.29 +inline TBool IsSupplementary(TUint aChar)
    1.30 +/**
    1.31 +@param aChar The 32-bit code point value of a Unicode character.
    1.32 +
    1.33 +@return True, if aChar is supplementary character; false, otherwise.
    1.34 +*/
    1.35 +	{
    1.36 +	return (aChar > 0xFFFF);
    1.37 +	}
    1.38 +
    1.39 +inline TBool IsHighSurrogate(TText16 aInt16)
    1.40 +/**
    1.41 +@return True, if aText16 is high surrogate; false, otherwise.
    1.42 +*/
    1.43 +	{
    1.44 +	return (aInt16 & 0xFC00) == 0xD800;
    1.45 +	}
    1.46 +
    1.47 +inline TBool IsLowSurrogate(TText16 aInt16)
    1.48 +/**
    1.49 +@return True, if aText16 is low surrogate; false, otherwise.
    1.50 +*/
    1.51 +	{
    1.52 +	return (aInt16 & 0xFC00) == 0xDC00;
    1.53 +	}
    1.54 +
    1.55 +inline TUint JoinSurrogate(TText16 aHighSurrogate, TText16 aLowSurrogate)
    1.56 +/**
    1.57 +Combine a high surrogate and a low surrogate into a supplementary character.
    1.58 +
    1.59 +@return The 32-bit code point value of the generated Unicode supplementary
    1.60 +        character.
    1.61 +*/
    1.62 +	{
    1.63 +	return ((aHighSurrogate - 0xD7F7) << 10) + aLowSurrogate;
    1.64 +	}
    1.65 +
    1.66 +TBool TextDefaultsToRightToLeft(const TDesC& aText, TBool* aFound);
    1.67 +
    1.68 +TBidirectionalState::TCategory TBidirectionalState::CharToBdCat(TChar::TBdCategory aCat)
    1.69 +	{
    1.70 +	return static_cast<TBidirectionalState::TCategory>(
    1.71 +		1 << static_cast<TInt>(aCat));
    1.72 +	}
    1.73 +
    1.74 +TBidirectionalState::TCategory TBidirectionalState::UintToBdCat(TUint aCat)
    1.75 +	{
    1.76 +	return static_cast<TBidirectionalState::TCategory>(1 << aCat);
    1.77 +	}
    1.78 +
    1.79 +void TBidirectionalState::TReorderContext::SetNextCategory(
    1.80 +	TChar::TBdCategory aCat)
    1.81 +	{
    1.82 +	iNextCategory = CharToBdCat(aCat);
    1.83 +	}
    1.84 +
    1.85 +void TBidirectionalState::TReorderContext::SetNextStrongCategory(
    1.86 +	TChar::TBdCategory aCat)
    1.87 +	{
    1.88 +	iNextStrongCategory = CharToBdCat(aCat);
    1.89 +	}
    1.90 +
    1.91 +
    1.92 +EXPORT_C void TBidirectionalState::ReverseGroups(TText* aStart,TInt aLength)
    1.93 +/** A utility to reverse text apart from combining characters, which remains after 
    1.94 +their base characters. This is what is needed when drawing right-to-left text.
    1.95 +
    1.96 +@param aStart Start position of text to be reversed.
    1.97 +@param aLength Length of text to be reversed. */
    1.98 +	{
    1.99 +	BidiCopy::ReverseCodes(aStart, aLength);
   1.100 +	BidiCopy::DeleteUnreversedSurrogates(aStart, aLength);
   1.101 +	BidiCopy::SubstituteMirrorImages(aStart, aLength);
   1.102 +	BidiCopy::CorrectGroups(aStart, aLength);
   1.103 +	BidiCopy::CorrectSurrogatePairs(aStart, aLength);
   1.104 +	}
   1.105 +
   1.106 +
   1.107 +// A local helper function. Get the next character from a buffer. This
   1.108 +// function won't check buffer length.
   1.109 +//
   1.110 +// @param aText The text buffer to read character from.
   1.111 +// @param aCharacterIndex Count of characters to skip in aText.
   1.112 +// @return The character.
   1.113 +TUint GetOneCharacter(const TText16 *aText, TInt aCharacterIndex)
   1.114 +	{
   1.115 +	const TText16 *p = aText;
   1.116 +	TUint c = 0xFFFF;
   1.117 +	while (aCharacterIndex >= 0)
   1.118 +		{
   1.119 +		c = *p++;
   1.120 +		ASSERT(!IsLowSurrogate(c));
   1.121 +		if (IsHighSurrogate(c))
   1.122 +			{
   1.123 +			ASSERT(IsLowSurrogate(*p));
   1.124 +			c = JoinSurrogate(c, *p++);
   1.125 +			}
   1.126 +		--aCharacterIndex;
   1.127 +		}
   1.128 +	return c;
   1.129 +	}
   1.130 +
   1.131 +
   1.132 +TInt TBidirectionalState::GenerateBdRunArray(const TText* aText, TInt aLength,
   1.133 +	TBidirectionalState::TRunInfo* aRun, TInt aMaxRuns)
   1.134 +/** Analyse the input text for runs of characters that share the same
   1.135 +bidirectional class. Categories TChar::EEuropeanNumberSeparator and
   1.136 +TChar::ECommonNumberSeparator are kept as singletons due to a limitation in
   1.137 +the reordering logic.
   1.138 +@param aText The text to be analysed.
   1.139 +@param aLength The length of the text to be analysed.
   1.140 +@param aRun	Output buffer for the runs after analysis. May be null if there 
   1.141 +is to be no output.
   1.142 +@param aMaxRuns The size of the aRun array. No more than this number of runs 
   1.143 +will be	output.
   1.144 +@return The number of runs that are required for the full results of the
   1.145 +analysis.
   1.146 +@internalTechnology */	
   1.147 +    {
   1.148 +	if (aLength == 0)
   1.149 +		{
   1.150 +		if (aRun && 0 < aMaxRuns)
   1.151 +			{
   1.152 +			aRun[0].iCategory = TChar::EOtherNeutral;
   1.153 +			aRun[0].iStart = 0;
   1.154 +			aRun[0].iLength = 0;
   1.155 +			}
   1.156 +		return 1;
   1.157 +		}
   1.158 +	int runs = 0;
   1.159 +	int run_start = 0;
   1.160 +	int run_end = 1;
   1.161 +	const TText* p = aText;
   1.162 +	const TText* q = p + aLength;
   1.163 +	
   1.164 +	// get the character pointed by 'p', then move 'p' to next character, and adjust 'run_end' if need
   1.165 +	TChar pc = ::GetOneCharacter(p, 0);
   1.166 +	TChar::TBdCategory cur_cat = pc.GetBdCategory();
   1.167 +	++p;
   1.168 +	if (IsSupplementary(pc))
   1.169 +		{
   1.170 +		++p;
   1.171 +		run_end = 2;	// run_end points to "end" of current character
   1.172 +		}
   1.173 +	
   1.174 +	while (p < q)
   1.175 +		{
   1.176 +		// get the character pointed by 'p'
   1.177 +		pc = ::GetOneCharacter(p, 0);
   1.178 +		
   1.179 +		TChar::TBdCategory new_cat = pc.GetBdCategory();
   1.180 +		if (new_cat != cur_cat)
   1.181 +			{
   1.182 +			if (new_cat == TChar::ENonSpacingMark &&
   1.183 +				cur_cat != TChar::ELeftToRightEmbedding &&
   1.184 +				cur_cat != TChar::ELeftToRightOverride &&
   1.185 +				cur_cat != TChar::ERightToLeftEmbedding &&
   1.186 +				cur_cat != TChar::ERightToLeftOverride &&
   1.187 +				cur_cat != TChar::EPopDirectionalFormat)
   1.188 +				new_cat = cur_cat;
   1.189 +			else if (p < q - 1 &&
   1.190 +					 (new_cat == TChar::EWhitespace ||
   1.191 +					  new_cat == TChar::EEuropeanNumberSeparator ||
   1.192 +					  new_cat == TChar::ECommonNumberSeparator))
   1.193 +				{
   1.194 +				TChar nextChar = ::GetOneCharacter(p, 1);
   1.195 +				TChar::TBdCategory next_cat = nextChar.GetBdCategory();
   1.196 +				if (new_cat == TChar::EWhitespace)
   1.197 +					{
   1.198 +					if ((cur_cat == TChar::ELeftToRight ||
   1.199 +						 cur_cat == TChar::ERightToLeft ||
   1.200 +						 cur_cat == TChar::ERightToLeftArabic) && cur_cat == next_cat)
   1.201 +						new_cat = cur_cat;
   1.202 +					}
   1.203 +				else if (cur_cat == TChar::EEuropeanNumber && next_cat == TChar::EEuropeanNumber)
   1.204 +					new_cat = TChar::EEuropeanNumber;
   1.205 +				}
   1.206 +			}
   1.207 +
   1.208 +		if (new_cat != cur_cat ||
   1.209 +			cur_cat == TChar::EEuropeanNumberSeparator ||
   1.210 +			cur_cat == TChar::ECommonNumberSeparator)
   1.211 +			{
   1.212 +			if (aRun && runs < aMaxRuns)
   1.213 +				{
   1.214 +				aRun[runs].iCategory = cur_cat;
   1.215 +				aRun[runs].iStart = run_start;
   1.216 +				aRun[runs].iLength = run_end - run_start;
   1.217 +				}
   1.218 +			
   1.219 +			runs++;
   1.220 +			run_start = run_end;
   1.221 +			}
   1.222 +
   1.223 +		p++;
   1.224 +		run_end++;
   1.225 +
   1.226 +		// adjust 'p' and 'run_end'
   1.227 +		if (IsSupplementary(pc))
   1.228 +			{
   1.229 +			p++;
   1.230 +			run_end++;
   1.231 +			}
   1.232 +
   1.233 +		cur_cat = new_cat;
   1.234 +		}
   1.235 +
   1.236 +	if (aRun && runs < aMaxRuns)
   1.237 +		{
   1.238 +		aRun[runs].iCategory = cur_cat;
   1.239 +		aRun[runs].iStart = run_start;
   1.240 +		aRun[runs].iLength = run_end - run_start;
   1.241 +		}
   1.242 +
   1.243 +	return runs + 1;
   1.244 +	}
   1.245 +
   1.246 +
   1.247 +EXPORT_C TInt TBidirectionalState::ReorderText(const TText* aText,TInt aLength,TBool aParRightToLeft,
   1.248 +											   TText*& aNewText)
   1.249 +/** Reorders text according to the Unicode Bidirectional Reordering algorithm.
   1.250 +
   1.251 +Reorders the input text from logical order (which may be bidirectional) to 
   1.252 +display order (strictly left to right).
   1.253 +
   1.254 +@param aText The input text in logical order.
   1.255 +@param aLength The length of the input text.
   1.256 +@param aParRightToLeft ETrue if the default directionality of the text to be 
   1.257 +re-ordered is right-to-left.
   1.258 +@param aNewText Returns the re-ordered text. If the text did not need re-ordering, 
   1.259 +or if there was an error, aText will be returned. Otherwise, ownership of 
   1.260 +a newly allocated buffer will be returned to the caller. This buffer must 
   1.261 +be deleted with delete[] (or CleanupArrayDeletePushL()) and not delete (or 
   1.262 +CleanupStack::PushL()).
   1.263 +@return A system-wide error value if there has been an error; KErrNone if there 
   1.264 +has not. */
   1.265 +	{
   1.266 +	aNewText = const_cast<TText*>(aText);
   1.267 +	if (aLength < 2)
   1.268 +		return KErrNone;
   1.269 +
   1.270 +	int error = KErrNone;
   1.271 +	TBidirectionalState::TRunInfo run_info;
   1.272 +	run_info.iDirection = 0;
   1.273 +	run_info.iIndex = 0;
   1.274 +	run_info.iStart = 0;
   1.275 +	run_info.iLength = aLength;
   1.276 +	TBidirectionalState::TRunInfo* run_info_array = &run_info;
   1.277 +	TBidirectionalState::TRunInfo* allocated_run_info_array = 0;
   1.278 +	int runs = GenerateBdRunArray(aText, aLength, run_info_array, 1);
   1.279 +	if (runs > 1)
   1.280 +		{
   1.281 +		allocated_run_info_array = new TBidirectionalState::TRunInfo[runs];
   1.282 +		if (allocated_run_info_array)
   1.283 +			{
   1.284 +			run_info_array = allocated_run_info_array;
   1.285 +			GenerateBdRunArray(aText, aLength, run_info_array, runs);
   1.286 +			}
   1.287 +		else
   1.288 +			{
   1.289 +			// the run cannot be allocated: stick with our single l-to-r run
   1.290 +			error = KErrNoMemory;
   1.291 +			runs = 1;
   1.292 +			}
   1.293 +		}
   1.294 +	if (error == KErrNone)
   1.295 +		{
   1.296 +		TBidirectionalState state;
   1.297 +		state.ReorderLine(run_info_array, runs, ETrue, ETrue, aParRightToLeft,
   1.298 +			TChar::EOtherNeutral, TChar::EOtherNeutral);
   1.299 +		}
   1.300 +
   1.301 +	// If there was only one run and it's left-to-right, we've finished.
   1.302 +	if (!allocated_run_info_array && run_info.iDirection == 0)
   1.303 +		return error;
   1.304 +
   1.305 +	// Reorder the text into a new buffer.
   1.306 +	TText* buffer = new TText[aLength];
   1.307 +	if (!buffer)
   1.308 +		{
   1.309 +		delete [] allocated_run_info_array;
   1.310 +		return KErrNoMemory;
   1.311 +		}
   1.312 +	const TBidirectionalState::TRunInfo* r = run_info_array;
   1.313 +	TText* dest = buffer;
   1.314 +	for (int i = 0; i < runs; i++, r++)
   1.315 +		{
   1.316 +		const TText* source = &aText[r->iStart];
   1.317 +		int length = r->iLength;
   1.318 +		Mem::Copy(dest,source,length * sizeof(TText));
   1.319 +		if (r->iDirection)
   1.320 +			ReverseGroups(dest,length);
   1.321 +		dest += length;
   1.322 +		}
   1.323 +
   1.324 +	delete [] allocated_run_info_array;
   1.325 +	aNewText = buffer;
   1.326 +	return KErrNone;
   1.327 +	}
   1.328 +
   1.329 +
   1.330 +EXPORT_C TBidirectionalState::TBidirectionalState()
   1.331 +/** Standard constructor. */
   1.332 +	{
   1.333 +	Reset();
   1.334 +	}
   1.335 +
   1.336 +
   1.337 +/** Reorders a line of text and updates the bidirectional state for the next line.
   1.338 +
   1.339 +@param aRunInfo An array of objects representing runs of characters with the 
   1.340 +same bidirectional category. Any number of characters can be combined into 
   1.341 +a run if they have the same category, except for the categories TChar::EEuropeanNumberSeparator 
   1.342 +and TChar::ECommonNumberSeparator, which should be put into single-character 
   1.343 +runs because the reordering logic depends on this.
   1.344 +@param aRuns Number of 'run info' objects.
   1.345 +@param aParStart Tells the function whether the line is the first line of a 
   1.346 +paragraph.
   1.347 +@param aParEnd Tells the function whether the line is the last line of a paragraph.
   1.348 +@param aParRightToLeft ETrue if the default directionality of the text to be 
   1.349 +re-ordered is right-to-left.
   1.350 +@param aNextCategory The category of the character immediately after the end 
   1.351 +of the line. This is ignored if aParEnd is ETrue.
   1.352 +@param aNextStrongCategory The category of the first strong character (one 
   1.353 +of the categories ELeftToRight, ELeftToRightEmbedding, ELeftToRightOverride, 
   1.354 +ERightToLeft, ERightToLeftArabic, ERightToLeftEmbedding or ERightToLeftOverride) 
   1.355 +after the end of the line. This is ignored if aParEnd is ETrue.
   1.356 +@param aVisualEndIsAmbiguous EFalse if the logical end of this line is at the
   1.357 +visual end and the logical beginning of the next line is at the visual beginning.
   1.358 +*/
   1.359 +EXPORT_C void TBidirectionalState::ReorderLine(TRunInfo* aRunInfo, TInt aRuns,
   1.360 +	TBool aParStart, TBool aParEnd, TBool aParRightToLeft,
   1.361 +	TChar::TBdCategory aNextCategory, TChar::TBdCategory aNextStrongCategory,
   1.362 +	TBool& aVisualEndIsAmbiguous)
   1.363 +	{
   1.364 +	ReorderLine(aRunInfo, aRuns, aParStart, aParEnd, aParRightToLeft,
   1.365 +		aNextCategory, aNextStrongCategory);
   1.366 +	if (iStackLevel  != 0)
   1.367 +		{
   1.368 +		aVisualEndIsAmbiguous = ETrue;
   1.369 +		return;
   1.370 +		}
   1.371 +	TCategory nextCat = CharToBdCat(aNextCategory);
   1.372 +	TCategory nextStrong = CharToBdCat(aNextStrongCategory);
   1.373 +	const TUint KAllStrongLeftToRight =
   1.374 +		ELeftToRight | ELeftToRightEmbedding | ELeftToRightOverride;
   1.375 +	const TUint KAllStrongRightToLeft =
   1.376 +		ERightToLeft | ERightToLeftArabic | ERightToLeftEmbedding | ERightToLeftOverride;
   1.377 +	if (aParRightToLeft)
   1.378 +		{
   1.379 +		// Ambiguous if any of the surrounding categories are strongly left-to-right
   1.380 +		aVisualEndIsAmbiguous =
   1.381 +			(iPreviousStrongCategory | iPreviousCategory | nextCat | nextStrong)
   1.382 +			& KAllStrongLeftToRight;
   1.383 +		}
   1.384 +	else
   1.385 +		{
   1.386 +		// Ambiguous if any of the surrounding categories are strongly right-to-left
   1.387 +		aVisualEndIsAmbiguous =
   1.388 +			(iPreviousStrongCategory | iPreviousCategory | nextCat | nextStrong)
   1.389 +			& KAllStrongRightToLeft;
   1.390 +		}
   1.391 +	}
   1.392 +/** Reorders a line of text and updates the bidirectional state for the next line.
   1.393 +
   1.394 +@param aRunInfo An array of objects representing runs of characters with the 
   1.395 +same bidirectional category. Any number of characters can be combined into 
   1.396 +a run if they have the same category, except for the categories TChar::EEuropeanNumberSeparator 
   1.397 +and TChar::ECommonNumberSeparator, which should be put into single-character 
   1.398 +runs because the reordering logic depends on this.
   1.399 +@param aRuns Number of 'run info' objects.
   1.400 +@param aParStart Tells the function whether the line is the first line of a 
   1.401 +paragraph.
   1.402 +@param aParEnd Tells the function whether the line is the last line of a paragraph.
   1.403 +@param aParRightToLeft ETrue if the default directionality of the text to be 
   1.404 +re-ordered is right-to-left.
   1.405 +@param aNextCategory The category of the character immediately after the end 
   1.406 +of the line. This is ignored if aParEnd is ETrue.
   1.407 +@param aNextStrongCategory The category of the first strong character (one 
   1.408 +of the categories ELeftToRight, ELeftToRightEmbedding, ELeftToRightOverride, 
   1.409 +ERightToLeft, ERightToLeftArabic, ERightToLeftEmbedding or ERightToLeftOverride) 
   1.410 +after the end of the line. This is ignored if aParEnd is ETrue. */
   1.411 +EXPORT_C void TBidirectionalState::ReorderLine(TRunInfo* aRunInfo, TInt aRuns,
   1.412 +	TBool aParStart, TBool aParEnd, TBool aParRightToLeft,
   1.413 +	TChar::TBdCategory aNextCategory, TChar::TBdCategory aNextStrongCategory)
   1.414 +	{
   1.415 +	// Reset if this is a new paragraph.
   1.416 +	if (aParStart)
   1.417 +		{
   1.418 +		Reset();
   1.419 +		iPreviousCategory = EOtherNeutral;
   1.420 +		if (aParRightToLeft)
   1.421 +			{
   1.422 +			iStack[0].iEmbeddingLevel = 1;
   1.423 +			iPreviousStrongCategory = ERightToLeft;
   1.424 +			}
   1.425 +		}
   1.426 +
   1.427 +	// Initialise the context object.
   1.428 +	TReorderContext context;
   1.429 +	context.iRunInfo = aRunInfo;
   1.430 +	context.iRuns = aRuns;
   1.431 +	context.iLastStrongCategory = iPreviousStrongCategory;
   1.432 +	if (aParEnd)
   1.433 +		context.iNextCategory = context.iNextStrongCategory = EOtherNeutral;
   1.434 +	else
   1.435 +		{
   1.436 +		context.iNextCategory = CharToBdCat(aNextCategory);
   1.437 +		context.iNextStrongCategory = CharToBdCat(aNextStrongCategory);
   1.438 +		}
   1.439 +
   1.440 +	// Initialise output data and find out what categories are present so that unnecessary steps can be skipped.
   1.441 +	context.iCategories = iPreviousCategory | context.iNextCategory | context.iNextStrongCategory;
   1.442 +	for (TInt i = 0; i != aRuns; ++i)
   1.443 +		{
   1.444 +		aRunInfo[i].iEmbeddingLevel = iStack[0].iEmbeddingLevel;
   1.445 +		aRunInfo[i].iDirection = 0;
   1.446 +		aRunInfo[i].iIndex = i;
   1.447 +		aRunInfo[i].iCategory = UintToBdCat(aRunInfo[i].iCategory);
   1.448 +		context.iCategories |= aRunInfo[i].iCategory;
   1.449 +		}
   1.450 +
   1.451 +	// Do nothing if no right-to-left material is present.
   1.452 +	if (aRuns == 0 ||
   1.453 +		(iStackLevel == 0 && iStack[0].iEmbeddingLevel == 0 &&
   1.454 +		 !(context.iCategories & (ERightToLeftGroup | EBdControlsGroup))))
   1.455 +		return;
   1.456 +
   1.457 +	// Perform the bidirectional algorithm.
   1.458 +	if ((context.iCategories & EBdControlsGroup) ||
   1.459 +		State().iOverrideState != ENoOverrideState)
   1.460 +		HandleBdControls(context);
   1.461 +	ResolveWeakTypesW1W2W3(context);
   1.462 +	ResolveWeakTypesW4W5W6(context);
   1.463 +	ResolveWeakTypesW7(context);
   1.464 +	if (context.iCategories & EOtherNeutral)
   1.465 +		ResolveNeutralTypes(context);
   1.466 +	ResolveImplicitLevels(context);
   1.467 +	PrepareForNextLine(context);
   1.468 +	ReorderRuns(context);
   1.469 +	}
   1.470 +
   1.471 +
   1.472 +void TBidirectionalState::PrepareForNextLine(const TReorderContext& aContext)
   1.473 +/**
   1.474 +Fold context information back into TBidirectionalState.
   1.475 +@internalComponent
   1.476 +*/	
   1.477 +   {
   1.478 +	if (aContext.iRuns != 0)
   1.479 +		{
   1.480 +		iPreviousCategory = static_cast<TCategory>(
   1.481 +			aContext.iRunInfo[aContext.iRuns - 1].iCategory);
   1.482 +		iPreviousStrongCategory = aContext.iLastStrongCategory;
   1.483 +		}
   1.484 +	}
   1.485 +
   1.486 +
   1.487 +void TBidirectionalState::HandleBdControls(TReorderContext& aContext)
   1.488 +/**
   1.489 +Handle LRO, RLO, LRE, RLE and PDF. After this phase, these categories will no
   1.490 +longer be found.
   1.491 +
   1.492 +This corresponds to Unicode(3.2) Bidirectional Algorithm phases X1-X7.
   1.493 +Phase X8 is not required as the run is assumed to be all in one paragraph.
   1.494 +Phases X9-X10 are implicit in other functions.
   1.495 +
   1.496 +@internalComponent
   1.497 +*/	
   1.498 +   {
   1.499 +	aContext.iCategories = iPreviousCategory | aContext.iNextCategory;
   1.500 +	for (TInt i = 0; i != aContext.iRuns; ++i)
   1.501 +		{
   1.502 +		TRunInfo* r = aContext.iRunInfo + i;
   1.503 +		TCategory nextCatInLine = i < aContext.iRuns - 1?
   1.504 +			(TCategory)(r[1].iCategory) : ENoCategory;
   1.505 +
   1.506 +		TBool was_pdf = FALSE;
   1.507 +		if (r->iCategory & EBdControlsGroup)
   1.508 +			{
   1.509 +			if (r->iCategory == EPopDirectionalFormat)
   1.510 +				{
   1.511 +				if (iStackLevel > 0)
   1.512 +					{
   1.513 +					was_pdf = TRUE;
   1.514 +					r->iEmbeddingLevel = State().iEmbeddingLevel;
   1.515 +					if (nextCatInLine == State().iStartCategory)
   1.516 +						// Ignore POP-PUSH pair with nothing between.
   1.517 +						// This is surely wrong? Perhaps it is a hack to
   1.518 +						// help other parts of the algorithm. Must investigate.
   1.519 +						// TPB.
   1.520 +						r->iCategory = r[1].iCategory = EBoundaryNeutral;
   1.521 +					else
   1.522 +						{
   1.523 +						r->iCategory = Pop();
   1.524 +						}
   1.525 +					}
   1.526 +				else
   1.527 +					r->iCategory = EBoundaryNeutral;
   1.528 +				}
   1.529 +			else
   1.530 +				{
   1.531 +				// Category is LRE, RLE, LRO or RLO.
   1.532 +				if (nextCatInLine == EPopDirectionalFormat)
   1.533 +					// Ignore PUSH-POP pair with nothing between.
   1.534 +					r->iCategory = r[1].iCategory = EBoundaryNeutral;
   1.535 +				else
   1.536 +					r->iCategory = Push(static_cast<TCategory>(r->iCategory));
   1.537 +				}
   1.538 +			}
   1.539 +
   1.540 +		if (!was_pdf)
   1.541 +			{
   1.542 +			switch (State().iOverrideState)
   1.543 +				{
   1.544 +				case ELeftToRightOverrideState:
   1.545 +					r->iCategory = ELeftToRight;
   1.546 +					break;
   1.547 +				case ERightToLeftOverrideState:
   1.548 +					r->iCategory = ERightToLeft;
   1.549 +					break;
   1.550 +				default:
   1.551 +					break;
   1.552 +				}
   1.553 +			r->iEmbeddingLevel = State().iEmbeddingLevel;
   1.554 +			}
   1.555 +		if (r->iCategory & EStrongGroup)
   1.556 +			aContext.iLastStrongCategory = static_cast<TCategory>(r->iCategory);
   1.557 +		aContext.iCategories |= r->iCategory;
   1.558 +		}
   1.559 +	}
   1.560 +
   1.561 +
   1.562 +void TBidirectionalState::ResolveWeakTypesW1W2W3(TReorderContext& aContext)
   1.563 +/**
   1.564 +Unicode(3.2) Bidirectional Algorithm phases W1, W2 and W3.
   1.565 +@internalComponent
   1.566 +*/	
   1.567 +    {
   1.568 +	if (!(aContext.iCategories
   1.569 +		& (ENonSpacingMark | ERightToLeftArabic | EEuropeanNumber)))
   1.570 +		return;
   1.571 +
   1.572 +	TRunInfo* endOfRuns = aContext.iRunInfo + aContext.iRuns;
   1.573 +	TCategory prev_cat = iPreviousCategory;
   1.574 +	TBool european_to_arabic_number = iPreviousStrongCategory == ERightToLeftArabic;
   1.575 +
   1.576 +	aContext.iCategories = iPreviousCategory | aContext.iNextCategory;
   1.577 +	for (TRunInfo* r = aContext.iRunInfo; r != endOfRuns; r++)
   1.578 +		{
   1.579 +		switch (r->iCategory)
   1.580 +			{
   1.581 +			case ENonSpacingMark:					// non-spacing marks change to the previous category
   1.582 +				r->iCategory = prev_cat;
   1.583 +				break;
   1.584 +			case ELeftToRight:
   1.585 +				european_to_arabic_number = EFalse;
   1.586 +				break;
   1.587 +			case ERightToLeft:
   1.588 +				european_to_arabic_number = EFalse;
   1.589 +				break;
   1.590 +			case ERightToLeftArabic:				// Arabic letters change to R
   1.591 +				european_to_arabic_number = ETrue;
   1.592 +				r->iCategory = ERightToLeft;
   1.593 +				break;
   1.594 +			case EEuropeanNumber:				    // European numbers change to Arabic if last strong category was R
   1.595 +				if (european_to_arabic_number)
   1.596 +					r->iCategory = EArabicNumber;
   1.597 +				break;
   1.598 +			default:
   1.599 +				break;
   1.600 +			}
   1.601 +		aContext.iCategories |= r->iCategory;
   1.602 +		prev_cat = static_cast<TCategory>(r->iCategory);
   1.603 +		}
   1.604 +	}
   1.605 +/**
   1.606 +This phase removes categories NSM, AL, ES, ET, CS, BS, S, WS and BN, leaving
   1.607 +only L, R, EN, AN and ON.
   1.608 +@internalComponent
   1.609 +*/
   1.610 +void TBidirectionalState::ResolveWeakTypesW4W5W6(TReorderContext& aContext)
   1.611 +	{
   1.612 +	int i;
   1.613 +	TRunInfo* r;
   1.614 +	TCategory prev_cat = iPreviousCategory;
   1.615 +	TCategory next_cat = EOtherNeutral;
   1.616 +
   1.617 +	// Phase P0b.
   1.618 +	prev_cat = iPreviousCategory;
   1.619 +	if (aContext.iCategories & EBoundaryNeutral)
   1.620 +		{
   1.621 +		for (i = 0, aContext.iCategories = iPreviousCategory | aContext.iNextCategory, r = aContext.iRunInfo;
   1.622 +			 i < aContext.iRuns;
   1.623 +			 i++, aContext.iCategories |= r->iCategory, r++)
   1.624 +			{
   1.625 +			if (r->iCategory == EBoundaryNeutral)		// runs of boundary neutrals change to EN, ET or AN if adjacent to
   1.626 +				{										// one of these, otherwise to ON
   1.627 +				int end = i + 1;
   1.628 +				while (end < aContext.iRuns && aContext.iRunInfo[end].iCategory == EBoundaryNeutral)
   1.629 +					end++;
   1.630 +				next_cat = end < aContext.iRuns ? (TCategory)(aContext.iRunInfo[end].iCategory) : aContext.iNextCategory;
   1.631 +				TCategory c = EOtherNeutral;
   1.632 +				if (prev_cat == EEuropeanNumber || next_cat == EEuropeanNumber)
   1.633 +					c = EEuropeanNumber;
   1.634 +				else if (prev_cat == EEuropeanNumberTerminator || next_cat == EEuropeanNumberTerminator)
   1.635 +					c = EEuropeanNumberTerminator;
   1.636 +				else if (prev_cat == EArabicNumber || next_cat == EArabicNumber)
   1.637 +					c = EArabicNumber;
   1.638 +				for (int j = i; j < end; j++)
   1.639 +					aContext.iRunInfo[j].iCategory = c;
   1.640 +				i = end - 1;
   1.641 +				r = &aContext.iRunInfo[i];
   1.642 +				}
   1.643 +			prev_cat = (TCategory)r->iCategory;
   1.644 +			}
   1.645 +		}
   1.646 +
   1.647 +	// Phase P1.
   1.648 +	prev_cat = iPreviousCategory;
   1.649 +	if (aContext.iCategories & (EEuropeanNumberSeparator | ECommonNumberSeparator))
   1.650 +		{
   1.651 +		for (i = 0, aContext.iCategories = iPreviousCategory | aContext.iNextCategory, r = aContext.iRunInfo;
   1.652 +			 i < aContext.iRuns;
   1.653 +			 i++, aContext.iCategories |= r->iCategory, r++)
   1.654 +			{
   1.655 +			next_cat = i < aContext.iRuns - 1 ? (TCategory)(r[1].iCategory) : aContext.iNextCategory;
   1.656 +			switch (r->iCategory)
   1.657 +				{
   1.658 +				case EEuropeanNumberSeparator:			// European separators change to EN if between two ENs, else to ON
   1.659 +					if (prev_cat == EEuropeanNumber && next_cat == EEuropeanNumber)
   1.660 +						r->iCategory = EEuropeanNumber;
   1.661 +					else
   1.662 +						r->iCategory = EOtherNeutral;
   1.663 +					break;
   1.664 +				case ECommonNumberSeparator:			// CSs change to EN or AN if between two of the same, else to ON
   1.665 +					if (prev_cat == EEuropeanNumber && next_cat == EEuropeanNumber)
   1.666 +						r->iCategory = EEuropeanNumber;
   1.667 +					else if (prev_cat == EArabicNumber && next_cat == EArabicNumber)
   1.668 +						r->iCategory = EArabicNumber;
   1.669 +					else
   1.670 +						r->iCategory = EOtherNeutral;
   1.671 +					break;
   1.672 +				default:
   1.673 +					break;
   1.674 +				}
   1.675 +			prev_cat = (TCategory)r->iCategory;
   1.676 +			}
   1.677 +		}
   1.678 +
   1.679 +	/*
   1.680 +	Phase L1: tabs, whitespace before tabs, and trailing whitespace, is set to the base embedding level.
   1.681 +	We ought to do this just before the final reordering, but the whitespace and segment separator
   1.682 +	categories have disappeared by then so we use the sentinel value 255 which tells
   1.683 +	ResolveImplicitLevels what to do.
   1.684 +	*/
   1.685 +	TBool demote_whitespace = TRUE;
   1.686 +	for (i = aContext.iRuns - 1, r = &aContext.iRunInfo[i]; i >= 0; i--, r--)
   1.687 +		{
   1.688 +		switch (r->iCategory)
   1.689 +			{
   1.690 +			case EWhitespace:
   1.691 +				break;
   1.692 +			case ESegmentSeparator:
   1.693 +				demote_whitespace = TRUE;
   1.694 +				break;
   1.695 +			default:
   1.696 +				demote_whitespace = FALSE;
   1.697 +				break;
   1.698 +			}
   1.699 +		if (demote_whitespace)
   1.700 +			r->iEmbeddingLevel = 255;
   1.701 +		}
   1.702 +
   1.703 +	// Phases P2 and P3.
   1.704 +	prev_cat = iPreviousCategory;
   1.705 +	if (aContext.iCategories & (EEuropeanNumberTerminator | ESegmentSeparator | EWhitespace))
   1.706 +		{
   1.707 +		for (i = 0, aContext.iCategories = iPreviousCategory | aContext.iNextCategory, r = aContext.iRunInfo;
   1.708 +			 i < aContext.iRuns;
   1.709 +			 i++, aContext.iCategories |= r->iCategory, r++)
   1.710 +			{
   1.711 +			next_cat = i < aContext.iRuns - 1 ? (TCategory)(r[1].iCategory) : aContext.iNextCategory;
   1.712 +			switch (r->iCategory)
   1.713 +				{
   1.714 +				case EEuropeanNumberTerminator:			// runs of ETs change to ENs if next to an EN, else to ON
   1.715 +					{
   1.716 +					int end = i + 1;
   1.717 +					while (end < aContext.iRuns && aContext.iRunInfo[end].iCategory == EEuropeanNumberTerminator)
   1.718 +						end++;
   1.719 +					next_cat = end < aContext.iRuns ? (TCategory)(aContext.iRunInfo[end].iCategory) : aContext.iNextCategory;
   1.720 +					TCategory c = EOtherNeutral;
   1.721 +					if (prev_cat == EEuropeanNumber || next_cat == EEuropeanNumber)
   1.722 +						c = EEuropeanNumber;
   1.723 +					for (int j = i; j < end; j++)
   1.724 +						aContext.iRunInfo[j].iCategory = c;
   1.725 +					i = end - 1;
   1.726 +					r = &aContext.iRunInfo[i];
   1.727 +					}
   1.728 +					break;
   1.729 +				case ESegmentSeparator:					// S and WS change to ON
   1.730 +				case EWhitespace:
   1.731 +					r->iCategory = EOtherNeutral;
   1.732 +					break;
   1.733 +				default:
   1.734 +					break;
   1.735 +				}
   1.736 +			prev_cat = (TCategory)r->iCategory;
   1.737 +			}
   1.738 +		}
   1.739 +	}
   1.740 +
   1.741 +void TBidirectionalState::ResolveWeakTypesW7(TReorderContext& aContext)
   1.742 +	{
   1.743 +	if (!(aContext.iCategories & EEuropeanNumber))
   1.744 +		return;
   1.745 +
   1.746 +	TCategory prev_strong_cat = iPreviousStrongCategory;
   1.747 +
   1.748 +	aContext.iCategories = iPreviousCategory | aContext.iNextCategory;
   1.749 +	TRunInfo* endOfRuns = aContext.iRunInfo + aContext.iRuns;
   1.750 +	for (TRunInfo* r = aContext.iRunInfo; r != endOfRuns; r++)
   1.751 +		{
   1.752 +		switch (r->iCategory)
   1.753 +			{
   1.754 +			case ELeftToRight:
   1.755 +				prev_strong_cat = ELeftToRight;
   1.756 +				break;
   1.757 +			case ERightToLeft:
   1.758 +				prev_strong_cat = ERightToLeft;
   1.759 +				break;
   1.760 +			case EEuropeanNumber: 
   1.761 +				if (prev_strong_cat == ELeftToRight)
   1.762 +					r->iCategory = ELeftToRight;
   1.763 +				break;
   1.764 +			default:
   1.765 +				break;
   1.766 +			}
   1.767 +		aContext.iCategories |= r->iCategory;
   1.768 +		}
   1.769 +	}
   1.770 +
   1.771 +
   1.772 +
   1.773 +void TBidirectionalState::DeneutralizeRuns(TRunInfo* aStart, TRunInfo* aEnd,
   1.774 +	TCategory aStartCategory, TCategory aEndCategory)
   1.775 +/**
   1.776 +Turn all ON (Other Neutral) into non-neutrals according to the rules N1 and N2.
   1.777 +@param aStart The start of the run array to be altered.
   1.778 +@param aEnd One past the end of the run array to be altered.
   1.779 +@param aStartCategory
   1.780 +	The last non-neutral before the run, must be ELeftToRight or ERightToLeft.
   1.781 +@param aEndCategory
   1.782 +	The first non-neutral after the run, must be ELeftToRight or ERightToLeft.
   1.783 +@internalComponent
   1.784 +*/	{
   1.785 +	// if sandwiched by the same category, neutrals become that.
   1.786 +	if (aStartCategory == aEndCategory)
   1.787 +		{
   1.788 +		for (; aStart != aEnd; ++aStart)
   1.789 +			aStart->iCategory = aStartCategory;
   1.790 +		return;
   1.791 +		}
   1.792 +	// otherwise look at the embedding level in each case
   1.793 +	for (; aStart != aEnd; ++aStart)
   1.794 +		{
   1.795 +		aStart->iCategory = aStart->iEmbeddingLevel & 1?
   1.796 +			ERightToLeft : ELeftToRight;
   1.797 +		}
   1.798 +	}
   1.799 +
   1.800 +
   1.801 +void TBidirectionalState::ResolveNeutralTypes(TReorderContext& aContext)
   1.802 +	/**
   1.803 +This phase removes the ON (Other Neutral) category, leaving only L, R, EN, and
   1.804 +AN; no need to update aContext.iCategories.
   1.805 +@internalComponent
   1.806 +*/
   1.807 +    {
   1.808 +	// Really we should find if any number intervenes, but this would require
   1.809 +	// a BC break.
   1.810 +	TCategory prevNonNeutral = iPreviousStrongCategory;
   1.811 +	if (prevNonNeutral & ELeftToRightGroup)
   1.812 +		prevNonNeutral = ELeftToRight;
   1.813 +	else if (prevNonNeutral & ERightToLeftGroup)
   1.814 +		prevNonNeutral = ERightToLeft;
   1.815 +	TRunInfo *prevNonNeutralRun = aContext.iRunInfo;	// one past the last non-neutral found
   1.816 +	TRunInfo *endOfRuns = aContext.iRunInfo + aContext.iRuns;
   1.817 +
   1.818 +	// All neutrals have now been changed to ON; change them to L or R depending on context.
   1.819 +	for (TRunInfo *p = aContext.iRunInfo; p != endOfRuns; ++p)
   1.820 +		{
   1.821 +		TCategory nonNeutral = EOtherNeutral;
   1.822 +		switch (p->iCategory)
   1.823 +			{
   1.824 +			case ELeftToRight:
   1.825 +				nonNeutral = ELeftToRight;
   1.826 +				break;
   1.827 +			case ERightToLeft:
   1.828 +				nonNeutral = ERightToLeft;
   1.829 +				break;
   1.830 +			case EArabicNumber:
   1.831 +			case EEuropeanNumber: 
   1.832 +				nonNeutral = ERightToLeft;
   1.833 +				break;
   1.834 +			default:
   1.835 +				break;
   1.836 +			}
   1.837 +		if (nonNeutral != EOtherNeutral)
   1.838 +			{
   1.839 +			if (p != prevNonNeutralRun)
   1.840 +				DeneutralizeRuns(prevNonNeutralRun, p,
   1.841 +					prevNonNeutral, nonNeutral);
   1.842 +			prevNonNeutral = nonNeutral;
   1.843 +			prevNonNeutralRun = p + 1;
   1.844 +			}
   1.845 +		}
   1.846 +	DeneutralizeRuns(prevNonNeutralRun, endOfRuns, prevNonNeutral,
   1.847 +		aContext.iNextStrongCategory);
   1.848 +	}
   1.849 +
   1.850 +
   1.851 +void TBidirectionalState::ResolveImplicitLevels(TReorderContext& aContext)
   1.852 +/**
   1.853 +Phases I1 and I2.
   1.854 +@internalComponent
   1.855 +*/	{
   1.856 +	int i;
   1.857 +	TRunInfo* r;
   1.858 +	for (i = 0, r = aContext.iRunInfo; i < aContext.iRuns; i++, r++)
   1.859 +		{
   1.860 +		if (r->iEmbeddingLevel == 255) // sentinel indicating this is a tab or segment-final whitespace
   1.861 +			r->iEmbeddingLevel = iStack[0].iEmbeddingLevel;
   1.862 +		else switch (r->iCategory)
   1.863 +			{
   1.864 +			case ELeftToRight:
   1.865 +				if (r->iEmbeddingLevel & 1)
   1.866 +					r->iEmbeddingLevel++;
   1.867 +				break;
   1.868 +			case ERightToLeft:
   1.869 +				if (!(r->iEmbeddingLevel & 1))
   1.870 +					r->iEmbeddingLevel++;
   1.871 +				break;
   1.872 +			case EEuropeanNumber: case EArabicNumber:
   1.873 +				if (r->iEmbeddingLevel & 1)
   1.874 +					r->iEmbeddingLevel++;
   1.875 +				else
   1.876 +					r->iEmbeddingLevel += 2;
   1.877 +				break;
   1.878 +			default:
   1.879 +				break;
   1.880 +			}
   1.881 +		}
   1.882 +	}
   1.883 +
   1.884 +
   1.885 +void TBidirectionalState::ReorderRuns(TReorderContext& aContext)
   1.886 +/**
   1.887 +Phase L2.
   1.888 +@internalComponent
   1.889 +*/	{
   1.890 +	// Find the highest level and lowest odd level.
   1.891 +	int i;
   1.892 +	TRunInfo* r;
   1.893 +	int highest = 0;
   1.894 +	int lowest_odd = EMaxLevel;
   1.895 +	int level = 0;
   1.896 +	for (i = 0, r = aContext.iRunInfo; i < aContext.iRuns; i++, r++)
   1.897 +		{
   1.898 +		level = r->iEmbeddingLevel;
   1.899 +		if (level > highest)
   1.900 +			highest = level;
   1.901 +		if ((level & 1) && level < lowest_odd)
   1.902 +			lowest_odd = level;
   1.903 +		}
   1.904 +
   1.905 +	// From the highest level to the lowest odd level, reverse any run at that level or higher.
   1.906 +	for (level = highest; level >= lowest_odd; level--)
   1.907 +		{
   1.908 +		int run_start = 0;
   1.909 +		r = aContext.iRunInfo;
   1.910 +		while (run_start < aContext.iRuns)
   1.911 +			{
   1.912 +			while (run_start < aContext.iRuns && r->iEmbeddingLevel < level)
   1.913 +				{
   1.914 +				run_start++;
   1.915 +				r++;
   1.916 +				}
   1.917 +			int run_end = run_start;
   1.918 +			while (run_end < aContext.iRuns && r->iEmbeddingLevel >= level)
   1.919 +				{
   1.920 +				r->iDirection = !r->iDirection;
   1.921 +				run_end++;
   1.922 +				r++;
   1.923 +				}
   1.924 +			TRunInfo* p = &aContext.iRunInfo[run_start];
   1.925 +			TRunInfo* q = &aContext.iRunInfo[run_end - 1];
   1.926 +			while (p < q)
   1.927 +				{
   1.928 +				TRunInfo temp = *p;
   1.929 +				*p = *q;
   1.930 +				*q = temp;
   1.931 +				p++;
   1.932 +				q--;
   1.933 +				}
   1.934 +			run_start = run_end;
   1.935 +			}
   1.936 +		}
   1.937 +	}
   1.938 +
   1.939 +
   1.940 +TBidirectionalState::TCategory TBidirectionalState::Push(TCategory aStartCategory)
   1.941 +/** @internalComponent */
   1.942 +	{
   1.943 +	TInt rightToLeftFlag = (static_cast<TInt>(aStartCategory)
   1.944 +		& ERightToLeftGroup)? 1 : 0;
   1.945 +	TInt oldLevel = State().iEmbeddingLevel;
   1.946 +	TInt newLevel = oldLevel + 1;
   1.947 +	// And add an extra one if the bottom bit is not correct.
   1.948 +	newLevel += (newLevel & 1) ^ rightToLeftFlag;
   1.949 +
   1.950 +	if (EMaxExplicitLevel < newLevel)
   1.951 +		{
   1.952 +		if (oldLevel == 60)
   1.953 +			++iPushesBeyond60;
   1.954 +		else
   1.955 +			++iPushesBeyond61;
   1.956 +		return EBoundaryNeutral;
   1.957 +		}
   1.958 +
   1.959 +	++iStackLevel;
   1.960 +	TStackItem& state = iStack[iStackLevel];
   1.961 +	state.iEmbeddingLevel = static_cast<TUint8>(newLevel);
   1.962 +	state.iOverrideState = static_cast<TOverrideState>(aStartCategory
   1.963 +		& (ELeftToRightOverride | ERightToLeftOverride));
   1.964 +	state.iStartCategory = aStartCategory;
   1.965 +
   1.966 +	return rightToLeftFlag? ERightToLeft : ELeftToRight;
   1.967 +	}
   1.968 +
   1.969 +
   1.970 +TBidirectionalState::TCategory TBidirectionalState::Pop()
   1.971 +/** @internalComponent */	
   1.972 +   {
   1.973 +	__ASSERT_DEBUG(0 < iStackLevel, User::Invariant());
   1.974 +	TInt level = State().iEmbeddingLevel;
   1.975 +	if (level < 60)
   1.976 +		--iStackLevel;
   1.977 +	else if (iPushesBeyond61 != 0)
   1.978 +		--iPushesBeyond61;
   1.979 +	else if (level == 61)
   1.980 +		--iStackLevel;
   1.981 +	else if (iPushesBeyond60)
   1.982 +		--iPushesBeyond60;
   1.983 +	else
   1.984 +		--iStackLevel;
   1.985 +	return (level & 1)? ERightToLeft : ELeftToRight;
   1.986 +	}
   1.987 +
   1.988 +
   1.989 +EXPORT_C void TBidirectionalState::Reset()
   1.990 +/** Sets the object to its default 'start of paragraph' state. */
   1.991 +	{
   1.992 +	iStackLevel = 0;
   1.993 +	iPushesBeyond60 = 0;
   1.994 +	iPushesBeyond61 = 0;
   1.995 +	iStack[0].iEmbeddingLevel = 0;
   1.996 +	iStack[0].iOverrideState = ENoOverrideState;
   1.997 +	iStack[0].iStartCategory = EOtherNeutral;
   1.998 +	iPreviousCategory = ELeftToRight;
   1.999 +	iPreviousStrongCategory = ELeftToRight;
  1.1000 +	}
  1.1001 +
  1.1002 +
  1.1003 +EXPORT_C TBool TBidirectionalState::IsDefault() const
  1.1004 +/** Returns Gets the default 'start of paragraph' state.
  1.1005 +
  1.1006 +@return ETrue if the object is in its default 'start of paragraph' state. */
  1.1007 +	{
  1.1008 +	return iStackLevel == 0 &&
  1.1009 +		   iStack[0].iEmbeddingLevel == 0 &&
  1.1010 +		   iStack[0].iOverrideState == ENoOverrideState &&
  1.1011 +		   iStack[0].iStartCategory == EOtherNeutral &&
  1.1012 +		   iPreviousCategory == ELeftToRight &&
  1.1013 +		   iPreviousStrongCategory == ELeftToRight;
  1.1014 +	}
  1.1015 +
  1.1016 +
  1.1017 +EXPORT_C TBool TBidirectionalState::operator==(const TBidirectionalState& aState) const
  1.1018 +/** Return ETrue if two bidirectional states are identical.
  1.1019 +
  1.1020 +@param aState A bidirectional state.
  1.1021 +@return ETrue if two bidirectional states are identical. */
  1.1022 +	{
  1.1023 +	if (iPreviousCategory != aState.iPreviousCategory ||
  1.1024 +		iPreviousStrongCategory != aState.iPreviousStrongCategory ||
  1.1025 +		iStackLevel != aState.iStackLevel)
  1.1026 +		return FALSE;
  1.1027 +	const TStackItem* p = iStack;
  1.1028 +	const TStackItem* q = aState.iStack;
  1.1029 +	for (int i = 0; i <= iStackLevel; i++, p++, q++)
  1.1030 +		{
  1.1031 +		if (p->iStartCategory != q->iStartCategory ||
  1.1032 +			p->iOverrideState != q->iOverrideState ||
  1.1033 +			p->iEmbeddingLevel != q->iEmbeddingLevel)
  1.1034 +			return FALSE;
  1.1035 +		}
  1.1036 +	return TRUE;
  1.1037 +	}
  1.1038 +
  1.1039 +
  1.1040 +TInt TBidirectionalState::CatToNumber(TInt aCat)
  1.1041 +/**
  1.1042 +Finds the highest bit set in the input. Used to convert
  1.1043 +TBidirectionalState::TCategory into TChar::TBdCategory.
  1.1044 +@param aCat a TBidirectionalState::TCategory.
  1.1045 +@return The equivalent TChar::TBdCategory.
  1.1046 +@internalComponent
  1.1047 +*/	{
  1.1048 +	TInt shifts = 0;
  1.1049 +	TInt bits = 32;
  1.1050 +	TInt mask = ~0L;
  1.1051 +	while (bits != 0)
  1.1052 +		{
  1.1053 +		bits >>= 1;
  1.1054 +		mask <<= bits;
  1.1055 +		if ((aCat & mask) == 0)
  1.1056 +			{
  1.1057 +			aCat <<= bits;
  1.1058 +			shifts += bits;
  1.1059 +			}
  1.1060 +		}
  1.1061 +	return 31 - shifts;
  1.1062 +	}
  1.1063 +
  1.1064 +
  1.1065 +EXPORT_C void TBidirectionalState::ExternalizeL(RWriteStream& aDest)
  1.1066 +/** Serializes a bidirectional state to an output stream.
  1.1067 +
  1.1068 +@param aDest An output stream. */
  1.1069 +	{
  1.1070 +	//+ put the prev cat, prev strong cat and stack levels in one number?
  1.1071 +	// Write the previous category and previous strong category.
  1.1072 +	aDest.WriteInt8L(CatToNumber(iPreviousCategory));
  1.1073 +	aDest.WriteInt8L(CatToNumber(iPreviousStrongCategory));
  1.1074 +
  1.1075 +	// Write the number of stack levels
  1.1076 +	aDest.WriteInt8L(iStackLevel);
  1.1077 +
  1.1078 +	/*
  1.1079 +	Write each stack level as a single number: 5 bits for the start category, 2 for the override state,
  1.1080 +	6 for the embedding level.
  1.1081 +	*/
  1.1082 +	for (int i = 0; i <= iStackLevel; i++)
  1.1083 +		{
  1.1084 +		TInt x = CatToNumber(iStack[i].iStartCategory);
  1.1085 +		if (iStack[i].iOverrideState == ELeftToRightOverrideState)
  1.1086 +			{
  1.1087 +			x |= (KBidirectionalStateOverrideStreamValueLeftToRight << 5);	
  1.1088 +			}
  1.1089 +        else if (iStack[i].iOverrideState == ERightToLeftOverrideState)
  1.1090 +        	{
  1.1091 +        	x |= (KBidirectionalStateOverrideStreamValueRightToLeft << 5); 	
  1.1092 +        	}
  1.1093 +       	x |= ((TInt)iStack[i].iEmbeddingLevel << 7);
  1.1094 +		aDest.WriteInt16L(x);
  1.1095 +		}
  1.1096 +
  1.1097 +	TInt level = State().iEmbeddingLevel;
  1.1098 +	if (60 <= level)
  1.1099 +		{
  1.1100 +		aDest.WriteInt8L(iPushesBeyond60);
  1.1101 +		aDest.WriteInt8L(iPushesBeyond61);
  1.1102 +		}
  1.1103 +	}
  1.1104 +
  1.1105 +
  1.1106 +EXPORT_C void TBidirectionalState::InternalizeL(RReadStream& aSource)
  1.1107 +/** Reads a bidirectional state from an input stream, translating it from its serialized 
  1.1108 +form.
  1.1109 +
  1.1110 +@param aSource A source stream. */
  1.1111 +	{
  1.1112 +	// Read the previous category and the previous strong category.
  1.1113 +	TInt x = aSource.ReadInt8L();
  1.1114 +	iPreviousCategory = (TCategory)(1 << x);
  1.1115 +	x = aSource.ReadInt8L();
  1.1116 +	iPreviousStrongCategory = (TCategory)(1 << x);
  1.1117 +
  1.1118 +	// Read the number of stack levels.
  1.1119 +	iStackLevel = aSource.ReadInt8L();
  1.1120 +
  1.1121 +	// Read the stack levels.
  1.1122 +	for (int i = 0; i <= iStackLevel; i++)
  1.1123 +		{
  1.1124 +		x = aSource.ReadInt16L();
  1.1125 +		iStack[i].iStartCategory = (TCategory)(1 << (x & 0x1F));
  1.1126 +		switch ((x >> 5) & 3)
  1.1127 +        	{
  1.1128 +    	case KBidirectionalStateOverrideStreamValueLeftToRight: 
  1.1129 +    		iStack[i].iOverrideState = ELeftToRightOverrideState;
  1.1130 +    		break;
  1.1131 +    	case KBidirectionalStateOverrideStreamValueRightToLeft:
  1.1132 +    		iStack[i].iOverrideState = ERightToLeftOverrideState; 
  1.1133 +    		break;
  1.1134 +    	case KBidirectionalStateOverrideStreamValueNone:
  1.1135 +    	default: iStack[i].iOverrideState = ENoOverrideState; break;
  1.1136 +        	};
  1.1137 +		iStack[i].iEmbeddingLevel = (TUint8)(x >> 7);
  1.1138 +		}
  1.1139 +
  1.1140 +	TInt level = State().iEmbeddingLevel;
  1.1141 +	if (60 <= level)
  1.1142 +		{
  1.1143 +		iPushesBeyond60 = aSource.ReadInt8L();
  1.1144 +		iPushesBeyond61 = aSource.ReadInt8L();
  1.1145 +		}
  1.1146 +	else
  1.1147 +		{
  1.1148 +		iPushesBeyond60 = 0;
  1.1149 +		iPushesBeyond61 = 0;
  1.1150 +		}
  1.1151 +	}
  1.1152 +
  1.1153 +
  1.1154 +TBidirectionalState::TBidirectionalState(TChar::TBdCategory aPrevCat,
  1.1155 +	TChar::TBdCategory aPrevStrongCat,
  1.1156 +	TBool aParRightToLeft)
  1.1157 +	/**
  1.1158 +Constructor suitable for test code.
  1.1159 +@internalComponent
  1.1160 +*/
  1.1161 +    {
  1.1162 +	Reset();
  1.1163 +	iPreviousCategory = CharToBdCat(aPrevCat);
  1.1164 +	iPreviousStrongCategory = CharToBdCat(aPrevStrongCat);
  1.1165 +	iStack[0].iEmbeddingLevel = (TUint8) (aParRightToLeft? 1 : 0);
  1.1166 +	}