sl@0
|
1 |
// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#ifndef __TBIDI_H__
|
sl@0
|
17 |
#define __TBIDI_H__
|
sl@0
|
18 |
|
sl@0
|
19 |
#include "TGraphicsHarness.h"
|
sl@0
|
20 |
|
sl@0
|
21 |
|
sl@0
|
22 |
inline TBool IsSupplementary(TUint aChar)
|
sl@0
|
23 |
/**
|
sl@0
|
24 |
@param aChar The 32-bit code point value of a Unicode character.
|
sl@0
|
25 |
|
sl@0
|
26 |
@return True, if aChar is supplementary character; false, otherwise.
|
sl@0
|
27 |
*/
|
sl@0
|
28 |
{
|
sl@0
|
29 |
return (aChar > 0xFFFF);
|
sl@0
|
30 |
}
|
sl@0
|
31 |
|
sl@0
|
32 |
inline TBool IsHighSurrogate(TText16 aInt16)
|
sl@0
|
33 |
/**
|
sl@0
|
34 |
@return True, if aText16 is high surrogate; false, otherwise.
|
sl@0
|
35 |
*/
|
sl@0
|
36 |
{
|
sl@0
|
37 |
return (aInt16 & 0xFC00) == 0xD800;
|
sl@0
|
38 |
}
|
sl@0
|
39 |
|
sl@0
|
40 |
inline TBool IsLowSurrogate(TText16 aInt16)
|
sl@0
|
41 |
/**
|
sl@0
|
42 |
@return True, if aText16 is low surrogate; false, otherwise.
|
sl@0
|
43 |
*/
|
sl@0
|
44 |
{
|
sl@0
|
45 |
return (aInt16 & 0xFC00) == 0xDC00;
|
sl@0
|
46 |
}
|
sl@0
|
47 |
|
sl@0
|
48 |
inline TUint JoinSurrogate(TText16 aHighSurrogate, TText16 aLowSurrogate)
|
sl@0
|
49 |
/**
|
sl@0
|
50 |
Combine a high surrogate and a low surrogate into a supplementary character.
|
sl@0
|
51 |
|
sl@0
|
52 |
@return The 32-bit code point value of the generated Unicode supplementary
|
sl@0
|
53 |
character.
|
sl@0
|
54 |
*/
|
sl@0
|
55 |
{
|
sl@0
|
56 |
return ((aHighSurrogate - 0xD7F7) << 10) + aLowSurrogate;
|
sl@0
|
57 |
}
|
sl@0
|
58 |
|
sl@0
|
59 |
inline TText16 GetHighSurrogate(TUint aChar)
|
sl@0
|
60 |
/**
|
sl@0
|
61 |
Retrieve the high surrogate of a supplementary character.
|
sl@0
|
62 |
|
sl@0
|
63 |
@param aChar The 32-bit code point value of a Unicode character.
|
sl@0
|
64 |
|
sl@0
|
65 |
@return High surrogate of aChar, if aChar is a supplementary character;
|
sl@0
|
66 |
aChar itself, if aChar is not a supplementary character.
|
sl@0
|
67 |
*/
|
sl@0
|
68 |
{
|
sl@0
|
69 |
return STATIC_CAST(TText16, 0xD7C0 + (aChar >> 10));
|
sl@0
|
70 |
}
|
sl@0
|
71 |
|
sl@0
|
72 |
inline TText16 GetLowSurrogate(TUint aChar)
|
sl@0
|
73 |
/**
|
sl@0
|
74 |
Retrieve the low surrogate of a supplementary character.
|
sl@0
|
75 |
|
sl@0
|
76 |
@param aChar The 32-bit code point value of a Unicode character.
|
sl@0
|
77 |
|
sl@0
|
78 |
@return Low surrogate of aChar, if aChar is a supplementary character;
|
sl@0
|
79 |
zero, if aChar is not a supplementary character.
|
sl@0
|
80 |
*/
|
sl@0
|
81 |
{
|
sl@0
|
82 |
return STATIC_CAST(TText16, 0xDC00 | (aChar & 0x3FF));
|
sl@0
|
83 |
}
|
sl@0
|
84 |
|
sl@0
|
85 |
void AppendCharacter(HBufC *aDes, TUint aChar);
|
sl@0
|
86 |
void AppendCharacter(TBuf<24> *aDes, TUint aChar);
|
sl@0
|
87 |
|
sl@0
|
88 |
|
sl@0
|
89 |
// Class to implement a wrapping iterator meant for extracting 16 bit characters
|
sl@0
|
90 |
// from a block of text
|
sl@0
|
91 |
// This class can handle surrogate pairs correctly.
|
sl@0
|
92 |
class TextIterator
|
sl@0
|
93 |
{
|
sl@0
|
94 |
public:
|
sl@0
|
95 |
TextIterator(const TText16* aData, const TInt aLength) :
|
sl@0
|
96 |
iData(aData), iLength(aLength), iIndex(0), iWrapped(EFalse), iCombineSurrogatePairs(ETrue)
|
sl@0
|
97 |
{
|
sl@0
|
98 |
}
|
sl@0
|
99 |
TextIterator(const TText16* aData, const TInt aLength, const TInt aIndex) :
|
sl@0
|
100 |
iData(aData), iLength(aLength), iIndex(aIndex), iWrapped(EFalse), iCombineSurrogatePairs(ETrue)
|
sl@0
|
101 |
{
|
sl@0
|
102 |
}
|
sl@0
|
103 |
TextIterator(const TText16* aData, const TInt aLength, const TInt aIndex, TBool aCombineSurrogatePairs) :
|
sl@0
|
104 |
iData(aData), iLength(aLength), iIndex(aIndex), iWrapped(EFalse), iCombineSurrogatePairs(aCombineSurrogatePairs)
|
sl@0
|
105 |
{
|
sl@0
|
106 |
}
|
sl@0
|
107 |
// Next character from data - wrap pointer to keep within data.
|
sl@0
|
108 |
// Get current char, then move the internal pointer forward to next char.
|
sl@0
|
109 |
TUint NextChar()
|
sl@0
|
110 |
{
|
sl@0
|
111 |
TUint ch = Char();
|
sl@0
|
112 |
if (iCombineSurrogatePairs && IsSupplementary(ch))
|
sl@0
|
113 |
iIndex += 2;
|
sl@0
|
114 |
else
|
sl@0
|
115 |
iIndex += 1;
|
sl@0
|
116 |
// wrap
|
sl@0
|
117 |
if (iIndex >= iLength)
|
sl@0
|
118 |
{
|
sl@0
|
119 |
iIndex = 0;
|
sl@0
|
120 |
iWrapped = ETrue;
|
sl@0
|
121 |
}
|
sl@0
|
122 |
return ch;
|
sl@0
|
123 |
}
|
sl@0
|
124 |
// Move the internal pointer backward, then return the char pointed by internal char.
|
sl@0
|
125 |
// Panic if iIndex already = 0.
|
sl@0
|
126 |
TUint PreviousChar()
|
sl@0
|
127 |
{
|
sl@0
|
128 |
ASSERT(iIndex > 0);
|
sl@0
|
129 |
iIndex--;
|
sl@0
|
130 |
if (iCombineSurrogatePairs && IsLowSurrogate(iData[iIndex]))
|
sl@0
|
131 |
{
|
sl@0
|
132 |
ASSERT(iIndex > 0);
|
sl@0
|
133 |
iIndex--;
|
sl@0
|
134 |
ASSERT(IsHighSurrogate(iData[iIndex]));
|
sl@0
|
135 |
}
|
sl@0
|
136 |
else if (iCombineSurrogatePairs && IsHighSurrogate(iData[iIndex]))
|
sl@0
|
137 |
{
|
sl@0
|
138 |
ASSERT(EFalse);
|
sl@0
|
139 |
}
|
sl@0
|
140 |
else
|
sl@0
|
141 |
{
|
sl@0
|
142 |
// do nothing
|
sl@0
|
143 |
}
|
sl@0
|
144 |
return Char();
|
sl@0
|
145 |
}
|
sl@0
|
146 |
//Reset the iterator to the original values
|
sl@0
|
147 |
void Reset()
|
sl@0
|
148 |
{
|
sl@0
|
149 |
iIndex = 0;
|
sl@0
|
150 |
iWrapped = EFalse;
|
sl@0
|
151 |
}
|
sl@0
|
152 |
void SetIndex(const TInt aIndex)
|
sl@0
|
153 |
{
|
sl@0
|
154 |
iIndex = aIndex;
|
sl@0
|
155 |
iWrapped = EFalse;
|
sl@0
|
156 |
}
|
sl@0
|
157 |
//Has the iterator wrapped back to the start of the buffer at least once?
|
sl@0
|
158 |
TBool Wrapped() const
|
sl@0
|
159 |
{
|
sl@0
|
160 |
return iWrapped;
|
sl@0
|
161 |
}
|
sl@0
|
162 |
//Where is the current buffer index?
|
sl@0
|
163 |
TInt Index() const
|
sl@0
|
164 |
{
|
sl@0
|
165 |
return iIndex;
|
sl@0
|
166 |
}
|
sl@0
|
167 |
//Return pointer to data
|
sl@0
|
168 |
const TText16* Ptr() const
|
sl@0
|
169 |
{
|
sl@0
|
170 |
return iData;
|
sl@0
|
171 |
}
|
sl@0
|
172 |
//Get length of data
|
sl@0
|
173 |
TInt Length() const
|
sl@0
|
174 |
{
|
sl@0
|
175 |
return iLength;
|
sl@0
|
176 |
}
|
sl@0
|
177 |
//Get character at current index
|
sl@0
|
178 |
TUint Char() const
|
sl@0
|
179 |
{
|
sl@0
|
180 |
TUint ch = 0xFFFF;
|
sl@0
|
181 |
TText16 i16 = iData[iIndex];
|
sl@0
|
182 |
if (iCombineSurrogatePairs && IsHighSurrogate(i16))
|
sl@0
|
183 |
{
|
sl@0
|
184 |
ASSERT(iIndex+1 < iLength);
|
sl@0
|
185 |
TText16 low = iData[iIndex+1];
|
sl@0
|
186 |
ch = JoinSurrogate(i16, low);
|
sl@0
|
187 |
}
|
sl@0
|
188 |
else if (iCombineSurrogatePairs && IsLowSurrogate(i16))
|
sl@0
|
189 |
{
|
sl@0
|
190 |
ASSERT(EFalse);
|
sl@0
|
191 |
}
|
sl@0
|
192 |
else
|
sl@0
|
193 |
{
|
sl@0
|
194 |
ch = i16;
|
sl@0
|
195 |
}
|
sl@0
|
196 |
return ch;
|
sl@0
|
197 |
}
|
sl@0
|
198 |
// Fill aBuffer with a char, fill at aIndex, guarded by aMaxIndex (excluded).
|
sl@0
|
199 |
// After return, aIndex points to the next position, if aUpdate_aIndex=ETrue.
|
sl@0
|
200 |
// aUpdate_aIndex: do you want to update the parameter aIndex?
|
sl@0
|
201 |
// aUpdate_iIndex: do you want to update the member variable iIndex?
|
sl@0
|
202 |
// aChar: [out] current char
|
sl@0
|
203 |
TBool NextCharInto(TDes &aBuffer, TInt &aIndex, TInt aMaxIndex=-1, TBool aUpdate_aIndex=ETrue, TBool aUpdate_iIndex=ETrue, TUint *aChar=NULL)
|
sl@0
|
204 |
{
|
sl@0
|
205 |
if (aMaxIndex < 0)
|
sl@0
|
206 |
aMaxIndex = aBuffer.Length();
|
sl@0
|
207 |
if (aIndex >= aMaxIndex)
|
sl@0
|
208 |
return EFalse;
|
sl@0
|
209 |
|
sl@0
|
210 |
TUint ch = Char();
|
sl@0
|
211 |
if (iCombineSurrogatePairs && IsSupplementary(ch))
|
sl@0
|
212 |
{
|
sl@0
|
213 |
if (aIndex+1 >= aMaxIndex)
|
sl@0
|
214 |
return EFalse;
|
sl@0
|
215 |
aBuffer[aIndex] = GetHighSurrogate(ch);
|
sl@0
|
216 |
aBuffer[aIndex+1] = GetLowSurrogate(ch);
|
sl@0
|
217 |
if (aUpdate_aIndex)
|
sl@0
|
218 |
aIndex += 2;
|
sl@0
|
219 |
}
|
sl@0
|
220 |
else
|
sl@0
|
221 |
{
|
sl@0
|
222 |
aBuffer[aIndex] = (TText16)ch;
|
sl@0
|
223 |
if (aUpdate_aIndex)
|
sl@0
|
224 |
aIndex++;
|
sl@0
|
225 |
}
|
sl@0
|
226 |
if (aChar)
|
sl@0
|
227 |
*aChar = ch;
|
sl@0
|
228 |
if (aUpdate_iIndex)
|
sl@0
|
229 |
{
|
sl@0
|
230 |
if (iCombineSurrogatePairs && IsSupplementary(ch))
|
sl@0
|
231 |
iIndex += 2;
|
sl@0
|
232 |
else
|
sl@0
|
233 |
iIndex += 1;
|
sl@0
|
234 |
if (iIndex >= iLength)
|
sl@0
|
235 |
{
|
sl@0
|
236 |
iIndex = 0;
|
sl@0
|
237 |
iWrapped = ETrue;
|
sl@0
|
238 |
}
|
sl@0
|
239 |
}
|
sl@0
|
240 |
return ETrue;
|
sl@0
|
241 |
}
|
sl@0
|
242 |
// Fill aBuffer from aIndex to aMaxIndex (excluded).
|
sl@0
|
243 |
// aMaxIndex=-1 means fill to index=aBuffer.Length.
|
sl@0
|
244 |
// After return, aIndex points to the next position, if aUpdate_aIndex=ETrue.
|
sl@0
|
245 |
TBool FillInto(TDes &aBuffer, TInt &aIndex, TInt aMaxIndex=-1)
|
sl@0
|
246 |
{
|
sl@0
|
247 |
if (aMaxIndex == -1)
|
sl@0
|
248 |
aMaxIndex = aBuffer.Length();
|
sl@0
|
249 |
while (aIndex < aMaxIndex)
|
sl@0
|
250 |
{
|
sl@0
|
251 |
TBool b = NextCharInto(aBuffer, aIndex, aMaxIndex);
|
sl@0
|
252 |
if (!b)
|
sl@0
|
253 |
{
|
sl@0
|
254 |
return ETrue;
|
sl@0
|
255 |
}
|
sl@0
|
256 |
}
|
sl@0
|
257 |
return ETrue;
|
sl@0
|
258 |
}
|
sl@0
|
259 |
|
sl@0
|
260 |
private:
|
sl@0
|
261 |
const TText16* iData;
|
sl@0
|
262 |
TInt iLength;
|
sl@0
|
263 |
TInt iIndex;
|
sl@0
|
264 |
TBool iWrapped;
|
sl@0
|
265 |
TBool iCombineSurrogatePairs; // ETrue = combine surrogates; EFalse = take single surrogate as character
|
sl@0
|
266 |
};
|
sl@0
|
267 |
|
sl@0
|
268 |
class CTBiDiStep : public CTGraphicsStep
|
sl@0
|
269 |
{
|
sl@0
|
270 |
public:
|
sl@0
|
271 |
CTBiDiStep();
|
sl@0
|
272 |
protected:
|
sl@0
|
273 |
//from CTGraphicsStep
|
sl@0
|
274 |
virtual CTGraphicsBase* CreateTestL();
|
sl@0
|
275 |
};
|
sl@0
|
276 |
|
sl@0
|
277 |
_LIT(KTBiDiStep,"TBiDi");
|
sl@0
|
278 |
|
sl@0
|
279 |
|
sl@0
|
280 |
#endif
|