First public contribution.
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
19 #include "TGraphicsHarness.h"
22 inline TBool IsSupplementary(TUint aChar)
24 @param aChar The 32-bit code point value of a Unicode character.
26 @return True, if aChar is supplementary character; false, otherwise.
29 return (aChar > 0xFFFF);
32 inline TBool IsHighSurrogate(TText16 aInt16)
34 @return True, if aText16 is high surrogate; false, otherwise.
37 return (aInt16 & 0xFC00) == 0xD800;
40 inline TBool IsLowSurrogate(TText16 aInt16)
42 @return True, if aText16 is low surrogate; false, otherwise.
45 return (aInt16 & 0xFC00) == 0xDC00;
48 inline TUint JoinSurrogate(TText16 aHighSurrogate, TText16 aLowSurrogate)
50 Combine a high surrogate and a low surrogate into a supplementary character.
52 @return The 32-bit code point value of the generated Unicode supplementary
56 return ((aHighSurrogate - 0xD7F7) << 10) + aLowSurrogate;
59 inline TText16 GetHighSurrogate(TUint aChar)
61 Retrieve the high surrogate of a supplementary character.
63 @param aChar The 32-bit code point value of a Unicode character.
65 @return High surrogate of aChar, if aChar is a supplementary character;
66 aChar itself, if aChar is not a supplementary character.
69 return STATIC_CAST(TText16, 0xD7C0 + (aChar >> 10));
72 inline TText16 GetLowSurrogate(TUint aChar)
74 Retrieve the low surrogate of a supplementary character.
76 @param aChar The 32-bit code point value of a Unicode character.
78 @return Low surrogate of aChar, if aChar is a supplementary character;
79 zero, if aChar is not a supplementary character.
82 return STATIC_CAST(TText16, 0xDC00 | (aChar & 0x3FF));
85 void AppendCharacter(HBufC *aDes, TUint aChar);
86 void AppendCharacter(TBuf<24> *aDes, TUint aChar);
89 // Class to implement a wrapping iterator meant for extracting 16 bit characters
90 // from a block of text
91 // This class can handle surrogate pairs correctly.
95 TextIterator(const TText16* aData, const TInt aLength) :
96 iData(aData), iLength(aLength), iIndex(0), iWrapped(EFalse), iCombineSurrogatePairs(ETrue)
99 TextIterator(const TText16* aData, const TInt aLength, const TInt aIndex) :
100 iData(aData), iLength(aLength), iIndex(aIndex), iWrapped(EFalse), iCombineSurrogatePairs(ETrue)
103 TextIterator(const TText16* aData, const TInt aLength, const TInt aIndex, TBool aCombineSurrogatePairs) :
104 iData(aData), iLength(aLength), iIndex(aIndex), iWrapped(EFalse), iCombineSurrogatePairs(aCombineSurrogatePairs)
107 // Next character from data - wrap pointer to keep within data.
108 // Get current char, then move the internal pointer forward to next char.
112 if (iCombineSurrogatePairs && IsSupplementary(ch))
117 if (iIndex >= iLength)
124 // Move the internal pointer backward, then return the char pointed by internal char.
125 // Panic if iIndex already = 0.
130 if (iCombineSurrogatePairs && IsLowSurrogate(iData[iIndex]))
134 ASSERT(IsHighSurrogate(iData[iIndex]));
136 else if (iCombineSurrogatePairs && IsHighSurrogate(iData[iIndex]))
146 //Reset the iterator to the original values
152 void SetIndex(const TInt aIndex)
157 //Has the iterator wrapped back to the start of the buffer at least once?
158 TBool Wrapped() const
162 //Where is the current buffer index?
167 //Return pointer to data
168 const TText16* Ptr() const
177 //Get character at current index
181 TText16 i16 = iData[iIndex];
182 if (iCombineSurrogatePairs && IsHighSurrogate(i16))
184 ASSERT(iIndex+1 < iLength);
185 TText16 low = iData[iIndex+1];
186 ch = JoinSurrogate(i16, low);
188 else if (iCombineSurrogatePairs && IsLowSurrogate(i16))
198 // Fill aBuffer with a char, fill at aIndex, guarded by aMaxIndex (excluded).
199 // After return, aIndex points to the next position, if aUpdate_aIndex=ETrue.
200 // aUpdate_aIndex: do you want to update the parameter aIndex?
201 // aUpdate_iIndex: do you want to update the member variable iIndex?
202 // aChar: [out] current char
203 TBool NextCharInto(TDes &aBuffer, TInt &aIndex, TInt aMaxIndex=-1, TBool aUpdate_aIndex=ETrue, TBool aUpdate_iIndex=ETrue, TUint *aChar=NULL)
206 aMaxIndex = aBuffer.Length();
207 if (aIndex >= aMaxIndex)
211 if (iCombineSurrogatePairs && IsSupplementary(ch))
213 if (aIndex+1 >= aMaxIndex)
215 aBuffer[aIndex] = GetHighSurrogate(ch);
216 aBuffer[aIndex+1] = GetLowSurrogate(ch);
222 aBuffer[aIndex] = (TText16)ch;
230 if (iCombineSurrogatePairs && IsSupplementary(ch))
234 if (iIndex >= iLength)
242 // Fill aBuffer from aIndex to aMaxIndex (excluded).
243 // aMaxIndex=-1 means fill to index=aBuffer.Length.
244 // After return, aIndex points to the next position, if aUpdate_aIndex=ETrue.
245 TBool FillInto(TDes &aBuffer, TInt &aIndex, TInt aMaxIndex=-1)
248 aMaxIndex = aBuffer.Length();
249 while (aIndex < aMaxIndex)
251 TBool b = NextCharInto(aBuffer, aIndex, aMaxIndex);
261 const TText16* iData;
265 TBool iCombineSurrogatePairs; // ETrue = combine surrogates; EFalse = take single surrogate as character
268 class CTBiDiStep : public CTGraphicsStep
273 //from CTGraphicsStep
274 virtual CTGraphicsBase* CreateTestL();
277 _LIT(KTBiDiStep,"TBiDi");