os/textandloc/fontservices/textbase/tgdi/TBiDi.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/fontservices/textbase/tgdi/TBiDi.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,280 @@
     1.4 +// Copyright (c) 2003-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 +//
    1.18 +
    1.19 +#ifndef __TBIDI_H__
    1.20 +#define __TBIDI_H__
    1.21 +
    1.22 +#include "TGraphicsHarness.h"
    1.23 +
    1.24 +
    1.25 +inline TBool IsSupplementary(TUint aChar)
    1.26 +/**
    1.27 +@param aChar The 32-bit code point value of a Unicode character.
    1.28 +
    1.29 +@return True, if aChar is supplementary character; false, otherwise.
    1.30 +*/
    1.31 +	{
    1.32 +	return (aChar > 0xFFFF);
    1.33 +	}
    1.34 +
    1.35 +inline TBool IsHighSurrogate(TText16 aInt16)
    1.36 +/**
    1.37 +@return True, if aText16 is high surrogate; false, otherwise.
    1.38 +*/
    1.39 +	{
    1.40 +	return (aInt16 & 0xFC00) == 0xD800;
    1.41 +	}
    1.42 +
    1.43 +inline TBool IsLowSurrogate(TText16 aInt16)
    1.44 +/**
    1.45 +@return True, if aText16 is low surrogate; false, otherwise.
    1.46 +*/
    1.47 +	{
    1.48 +	return (aInt16 & 0xFC00) == 0xDC00;
    1.49 +	}
    1.50 +
    1.51 +inline TUint JoinSurrogate(TText16 aHighSurrogate, TText16 aLowSurrogate)
    1.52 +/**
    1.53 +Combine a high surrogate and a low surrogate into a supplementary character.
    1.54 +
    1.55 +@return The 32-bit code point value of the generated Unicode supplementary
    1.56 +        character.
    1.57 +*/
    1.58 +	{
    1.59 +	return ((aHighSurrogate - 0xD7F7) << 10) + aLowSurrogate;
    1.60 +	}
    1.61 +
    1.62 +inline TText16 GetHighSurrogate(TUint aChar)
    1.63 +/**
    1.64 +Retrieve the high surrogate of a supplementary character.
    1.65 +
    1.66 +@param aChar The 32-bit code point value of a Unicode character.
    1.67 +
    1.68 +@return High surrogate of aChar, if aChar is a supplementary character; 
    1.69 +        aChar itself, if aChar is not a supplementary character.
    1.70 +*/
    1.71 +	{
    1.72 +	return STATIC_CAST(TText16, 0xD7C0 + (aChar >> 10));
    1.73 +	}
    1.74 +
    1.75 +inline TText16 GetLowSurrogate(TUint aChar)
    1.76 +/**
    1.77 +Retrieve the low surrogate of a supplementary character.
    1.78 +
    1.79 +@param aChar The 32-bit code point value of a Unicode character.
    1.80 +
    1.81 +@return Low surrogate of aChar, if aChar is a supplementary character; 
    1.82 +        zero, if aChar is not a supplementary character.
    1.83 +*/
    1.84 +	{
    1.85 +	return STATIC_CAST(TText16, 0xDC00 | (aChar & 0x3FF));
    1.86 +	}
    1.87 +
    1.88 +void AppendCharacter(HBufC *aDes, TUint aChar);
    1.89 +void AppendCharacter(TBuf<24> *aDes, TUint aChar);
    1.90 +
    1.91 +
    1.92 +// Class to implement a wrapping iterator meant for extracting 16 bit characters
    1.93 +// from a block of text
    1.94 +// This class can handle surrogate pairs correctly.
    1.95 +class TextIterator
    1.96 +	{
    1.97 +public:
    1.98 +	TextIterator(const TText16* aData, const TInt aLength) : 
    1.99 +	  iData(aData), iLength(aLength), iIndex(0), iWrapped(EFalse), iCombineSurrogatePairs(ETrue)
   1.100 +		{
   1.101 +		}	
   1.102 +	TextIterator(const TText16* aData, const TInt aLength, const TInt aIndex) : 
   1.103 +		iData(aData), iLength(aLength), iIndex(aIndex), iWrapped(EFalse), iCombineSurrogatePairs(ETrue)
   1.104 +		{
   1.105 +		}
   1.106 +	TextIterator(const TText16* aData, const TInt aLength, const TInt aIndex, TBool aCombineSurrogatePairs) : 
   1.107 +		iData(aData), iLength(aLength), iIndex(aIndex), iWrapped(EFalse), iCombineSurrogatePairs(aCombineSurrogatePairs)
   1.108 +		{
   1.109 +		}
   1.110 +	// Next character from data - wrap pointer to keep within data.
   1.111 +	// Get current char, then move the internal pointer forward to next char.
   1.112 +	TUint NextChar()
   1.113 +		{
   1.114 +		TUint ch = Char();
   1.115 +		if (iCombineSurrogatePairs && IsSupplementary(ch))
   1.116 +			iIndex += 2;
   1.117 +		else
   1.118 +			iIndex += 1;
   1.119 +		// wrap
   1.120 +		if (iIndex >= iLength)
   1.121 +			{
   1.122 +			iIndex = 0;
   1.123 +			iWrapped = ETrue;
   1.124 +			}
   1.125 +		return ch;
   1.126 +		}
   1.127 +	// Move the internal pointer backward, then return the char pointed by internal char.
   1.128 +	// Panic if iIndex already = 0.
   1.129 +	TUint PreviousChar()
   1.130 +		{
   1.131 +		ASSERT(iIndex > 0);
   1.132 +		iIndex--;
   1.133 +		if (iCombineSurrogatePairs && IsLowSurrogate(iData[iIndex]))
   1.134 +			{
   1.135 +			ASSERT(iIndex > 0);
   1.136 +			iIndex--;
   1.137 +			ASSERT(IsHighSurrogate(iData[iIndex]));
   1.138 +			}
   1.139 +		else if (iCombineSurrogatePairs && IsHighSurrogate(iData[iIndex]))
   1.140 +			{
   1.141 +			ASSERT(EFalse);
   1.142 +			}
   1.143 +		else
   1.144 +			{
   1.145 +			// do nothing
   1.146 +			}
   1.147 +		return Char();
   1.148 +		}
   1.149 +	//Reset the iterator to the original values
   1.150 +	void Reset()
   1.151 +		{
   1.152 +		iIndex = 0;
   1.153 +		iWrapped = EFalse;
   1.154 +		}
   1.155 +	void SetIndex(const TInt aIndex)
   1.156 +		{
   1.157 +		iIndex = aIndex;
   1.158 +		iWrapped = EFalse;
   1.159 +		}
   1.160 +	//Has the iterator wrapped back to the start of the buffer at least once?
   1.161 +	TBool Wrapped() const
   1.162 +		{
   1.163 +		return iWrapped;
   1.164 +		}
   1.165 +	//Where is the current buffer index?
   1.166 +	TInt Index() const
   1.167 +		{
   1.168 +		return iIndex;
   1.169 +		}
   1.170 +	//Return pointer to data
   1.171 +	const TText16* Ptr() const
   1.172 +		{
   1.173 +		return iData;
   1.174 +		}
   1.175 +	//Get length of data
   1.176 +	TInt Length() const
   1.177 +		{
   1.178 +		return iLength;
   1.179 +		}
   1.180 +	//Get character at current index
   1.181 +	TUint Char() const
   1.182 +		{
   1.183 +		TUint ch = 0xFFFF;
   1.184 +		TText16 i16 = iData[iIndex];
   1.185 +		if (iCombineSurrogatePairs && IsHighSurrogate(i16))
   1.186 +			{
   1.187 +			ASSERT(iIndex+1 < iLength);
   1.188 +			TText16 low = iData[iIndex+1];
   1.189 +			ch = JoinSurrogate(i16, low);
   1.190 +			}
   1.191 +		else if (iCombineSurrogatePairs && IsLowSurrogate(i16))
   1.192 +			{
   1.193 +			ASSERT(EFalse);
   1.194 +			}
   1.195 +		else
   1.196 +			{
   1.197 +			ch = i16;
   1.198 +			}
   1.199 +		return ch;
   1.200 +		}
   1.201 +	// Fill aBuffer with a char, fill at aIndex, guarded by aMaxIndex (excluded).
   1.202 +	// After return, aIndex points to the next position, if aUpdate_aIndex=ETrue.
   1.203 +	// aUpdate_aIndex: do you want to update the parameter aIndex?
   1.204 +	// aUpdate_iIndex: do you want to update the member variable iIndex?
   1.205 +	// aChar: [out] current char
   1.206 +	TBool NextCharInto(TDes &aBuffer, TInt &aIndex, TInt aMaxIndex=-1, TBool aUpdate_aIndex=ETrue, TBool aUpdate_iIndex=ETrue, TUint *aChar=NULL)
   1.207 +		{
   1.208 +		if (aMaxIndex < 0)
   1.209 +			aMaxIndex = aBuffer.Length();
   1.210 +		if (aIndex >= aMaxIndex)
   1.211 +			return EFalse;
   1.212 +
   1.213 +		TUint ch = Char();
   1.214 +		if (iCombineSurrogatePairs && IsSupplementary(ch))
   1.215 +			{
   1.216 +			if (aIndex+1 >= aMaxIndex)
   1.217 +				return EFalse;
   1.218 +			aBuffer[aIndex] = GetHighSurrogate(ch);
   1.219 +			aBuffer[aIndex+1] = GetLowSurrogate(ch);
   1.220 +			if (aUpdate_aIndex)
   1.221 +				aIndex += 2;
   1.222 +			}
   1.223 +		else
   1.224 +			{
   1.225 +			aBuffer[aIndex] = (TText16)ch;
   1.226 +			if (aUpdate_aIndex)
   1.227 +				aIndex++;
   1.228 +			}
   1.229 +		if (aChar)
   1.230 +			*aChar = ch;
   1.231 +		if (aUpdate_iIndex)
   1.232 +			{
   1.233 +			if (iCombineSurrogatePairs && IsSupplementary(ch))
   1.234 +				iIndex += 2;
   1.235 +			else
   1.236 +				iIndex += 1;
   1.237 +			if (iIndex >= iLength)
   1.238 +				{
   1.239 +				iIndex = 0;
   1.240 +				iWrapped = ETrue;
   1.241 +				}
   1.242 +			}
   1.243 +		return ETrue;
   1.244 +		}
   1.245 +	// Fill aBuffer from aIndex to aMaxIndex (excluded).
   1.246 +	// aMaxIndex=-1 means fill to index=aBuffer.Length.
   1.247 +	// After return, aIndex points to the next position, if aUpdate_aIndex=ETrue.
   1.248 +	TBool FillInto(TDes &aBuffer, TInt &aIndex, TInt aMaxIndex=-1)
   1.249 +		{
   1.250 +		if (aMaxIndex == -1)
   1.251 +			aMaxIndex = aBuffer.Length();
   1.252 +		while (aIndex < aMaxIndex)
   1.253 +			{
   1.254 +			TBool b = NextCharInto(aBuffer, aIndex, aMaxIndex);
   1.255 +			if (!b)
   1.256 +				{
   1.257 +				return ETrue;
   1.258 +				}
   1.259 +			}
   1.260 +		return ETrue;
   1.261 +		}
   1.262 +
   1.263 +private:
   1.264 +	const TText16* iData;
   1.265 +	TInt  iLength;
   1.266 +	TInt  iIndex;
   1.267 +	TBool iWrapped;
   1.268 +	TBool iCombineSurrogatePairs;	// ETrue = combine surrogates; EFalse = take single surrogate as character
   1.269 +	};
   1.270 +
   1.271 +class CTBiDiStep : public CTGraphicsStep
   1.272 +	{
   1.273 +public:
   1.274 +	CTBiDiStep();
   1.275 +protected:	
   1.276 +	//from CTGraphicsStep
   1.277 +	virtual CTGraphicsBase* CreateTestL();
   1.278 +	};
   1.279 +
   1.280 +_LIT(KTBiDiStep,"TBiDi");
   1.281 +
   1.282 +
   1.283 +#endif