#include "unicode/utypes.h"
#include "unicode/uchar.h"
#include "unicode/ustring.h"
#include "unicode/ushape.h"
#include "cmemory.h"
#include "putilimp.h"
#include "ustr_imp.h"
#include "ubidi_props.h"
#if UTF_SIZE<16
# error This implementation assumes UTF-16 or UTF-32 (check UTF_SIZE)
#endif
#define IRRELEVANT 4
#define LAMTYPE 16
#define ALEFTYPE 32
#define LINKR 1
#define LINKL 2
#define APRESENT 8
#define SHADDA 64
#define CSHADDA 128
#define COMBINE (SHADDA+CSHADDA)
static const uint8_t IrrelevantPos[] = {
0x0, 0x2, 0x4, 0x6,
0x8, 0xA, 0xC, 0xE,
};
static const UChar convertLamAlef[] =
{
0x0622,
0x0622,
0x0623,
0x0623,
0x0625,
0x0625,
0x0627,
0x0627
};
static const UChar araLink[178]=
{
1 + 32 + 256 * 0x11,
1 + 32 + 256 * 0x13,
1 + 256 * 0x15,
1 + 32 + 256 * 0x17,
1 + 2 + 256 * 0x19,
1 + 32 + 256 * 0x1D,
1 + 2 + 256 * 0x1F,
1 + 256 * 0x23,
1 + 2 + 256 * 0x25,
1 + 2 + 256 * 0x29,
1 + 2 + 256 * 0x2D,
1 + 2 + 256 * 0x31,
1 + 2 + 256 * 0x35,
1 + 256 * 0x39,
1 + 256 * 0x3B,
1 + 256 * 0x3D,
1 + 256 * 0x3F,
1 + 2 + 256 * 0x41,
1 + 2 + 256 * 0x45,
1 + 2 + 256 * 0x49,
1 + 2 + 256 * 0x4D,
1 + 2 + 256 * 0x51,
1 + 2 + 256 * 0x55,
1 + 2 + 256 * 0x59,
1 + 2 + 256 * 0x5D,
0, 0, 0, 0, 0,
1 + 2,
1 + 2 + 256 * 0x61,
1 + 2 + 256 * 0x65,
1 + 2 + 256 * 0x69,
1 + 2 + 16 + 256 * 0x6D,
1 + 2 + 256 * 0x71,
1 + 2 + 256 * 0x75,
1 + 2 + 256 * 0x79,
1 + 256 * 0x7D,
1 + 256 * 0x7F,
1 + 2 + 256 * 0x81,
4 + 256 * 1,
4 + 128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1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 0,
1, 0, 1, 2,1 + 2, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0
};
static const UChar convertFBto06[] =
{
0x671, 0x671, 0, 0, 0, 0, 0x07E, 0x07E, 0x07E, 0x07E, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x686, 0x686, 0x686, 0x686, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x698, 0x698, 0, 0, 0x6A9, 0x6A9,
0x6A9, 0x6A9, 0x6AF, 0x6AF, 0x6AF, 0x6AF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x6CC, 0x6CC, 0x6CC, 0x6CC
};
static const UChar convertFEto06[] =
{
0x64B, 0x64B, 0x64C, 0x64C, 0x64D, 0x64D, 0x64E, 0x64E, 0x64F, 0x64F, 0x650, 0x650, 0x651, 0x651, 0x652, 0x652,
0x621, 0x622, 0x622, 0x623, 0x623, 0x624, 0x624, 0x625, 0x625, 0x626, 0x626, 0x626, 0x626, 0x627, 0x627, 0x628,
0x628, 0x628, 0x628, 0x629, 0x629, 0x62A, 0x62A, 0x62A, 0x62A, 0x62B, 0x62B, 0x62B, 0x62B, 0x62C, 0x62C, 0x62C,
0x62C, 0x62D, 0x62D, 0x62D, 0x62D, 0x62E, 0x62E, 0x62E, 0x62E, 0x62F, 0x62F, 0x630, 0x630, 0x631, 0x631, 0x632,
0x632, 0x633, 0x633, 0x633, 0x633, 0x634, 0x634, 0x634, 0x634, 0x635, 0x635, 0x635, 0x635, 0x636, 0x636, 0x636,
0x636, 0x637, 0x637, 0x637, 0x637, 0x638, 0x638, 0x638, 0x638, 0x639, 0x639, 0x639, 0x639, 0x63A, 0x63A, 0x63A,
0x63A, 0x641, 0x641, 0x641, 0x641, 0x642, 0x642, 0x642, 0x642, 0x643, 0x643, 0x643, 0x643, 0x644, 0x644, 0x644,
0x644, 0x645, 0x645, 0x645, 0x645, 0x646, 0x646, 0x646, 0x646, 0x647, 0x647, 0x647, 0x647, 0x648, 0x648, 0x649,
0x649, 0x64A, 0x64A, 0x64A, 0x64A, 0x65C, 0x65C, 0x65D, 0x65D, 0x65E, 0x65E, 0x65F, 0x65F
};
static const uint8_t shapeTable[4][4][4]=
{
{ {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,1} },
{ {0,0,2,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} },
{ {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,3} },
{ {0,0,1,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} }
};
static void
_shapeToArabicDigitsWithContext(UChar *s, int32_t length,
UChar digitBase,
UBool isLogical, UBool lastStrongWasAL) {
const UBiDiProps *bdp;
UErrorCode errorCode;
int32_t i;
UChar c;
errorCode=U_ZERO_ERROR;
bdp=ubidi_getSingleton(&errorCode);
if(U_FAILURE(errorCode)) {
return;
}
digitBase-=0x30;
if(isLogical) {
for(i=0; i<length; ++i) {
c=s[i];
switch(ubidi_getClass(bdp, c)) {
case U_LEFT_TO_RIGHT:
case U_RIGHT_TO_LEFT:
lastStrongWasAL=FALSE;
break;
case U_RIGHT_TO_LEFT_ARABIC:
lastStrongWasAL=TRUE;
break;
case U_EUROPEAN_NUMBER:
if(lastStrongWasAL && (uint32_t)(c-0x30)<10) {
s[i]=(UChar)(digitBase+c);
}
break;
default :
break;
}
}
} else {
for(i=length; i>0; ) {
c=s[--i];
switch(ubidi_getClass(bdp, c)) {
case U_LEFT_TO_RIGHT:
case U_RIGHT_TO_LEFT:
lastStrongWasAL=FALSE;
break;
case U_RIGHT_TO_LEFT_ARABIC:
lastStrongWasAL=TRUE;
break;
case U_EUROPEAN_NUMBER:
if(lastStrongWasAL && (uint32_t)(c-0x30)<10) {
s[i]=(UChar)(digitBase+c);
}
break;
default :
break;
}
}
}
}
static void
invertBuffer(UChar *buffer,int32_t size,uint32_t options,int32_t lowlimit,int32_t highlimit) {
UChar temp;
int32_t i=0,j=0;
for(i=lowlimit,j=size-highlimit-1;i<j;i++,j--) {
temp = buffer[i];
buffer[i] = buffer[j];
buffer[j] = temp;
}
}
static U_INLINE UChar
changeLamAlef(UChar ch) {
switch(ch) {
case 0x0622 :
return 0x065C;
case 0x0623 :
return 0x065D;
case 0x0625 :
return 0x065E;
case 0x0627 :
return 0x065F;
}
return 0;
}
static UChar
getLink(UChar ch) {
if(ch >= 0x0622 && ch <= 0x06D3) {
return(araLink[ch-0x0622]);
} else if(ch == 0x200D) {
return(3);
} else if(ch >= 0x206D && ch <= 0x206F) {
return(4);
} else if(ch >= 0xFB50 && ch <= 0xFC62) {
return(presALink[ch-0xFB50]);
} else if(ch >= 0xFE70 && ch <= 0xFEFC) {
return(presBLink[ch-0xFE70]);
} else {
return(0);
}
}
static void
countSpaces(UChar *dest,int32_t size,uint32_t options,int32_t *spacesCountl,int32_t *spacesCountr) {
int32_t i = 0;
int32_t countl = 0,countr = 0;
while(dest[i] == 0x0020) {
countl++;
i++;
}
while(dest[size-1] == 0x0020) {
countr++;
size--;
}
*spacesCountl = countl;
*spacesCountr = countr;
}
static U_INLINE int32_t
isTashkeelChar(UChar ch) {
return (int32_t)( ch>=0x064B && ch<= 0x0652 );
}
static U_INLINE int32_t
isAlefChar(UChar ch) {
return (int32_t)( (ch==0x0622)||(ch==0x0623)||(ch==0x0625)||(ch==0x0627) );
}
static U_INLINE int32_t
isLamAlefChar(UChar ch) {
return (int32_t)( (ch>=0xFEF5)&&(ch<=0xFEFC) );
}
static int32_t
calculateSize(const UChar *source, int32_t sourceLength,
int32_t destSize,uint32_t options) {
int32_t i = 0;
destSize = sourceLength;
switch(options&U_SHAPE_LETTERS_MASK) {
case U_SHAPE_LETTERS_SHAPE :
case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED:
if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_VISUAL_LTR) {
for(i=0;i<sourceLength;i++) {
if( (isAlefChar(source[i]))&&(source[i+1]==0x0644) ) {
destSize--;
}
}
} else if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL) {
for(i=0;i<sourceLength;i++) {
if( (isAlefChar(source[i+1]))&&(source[i]==0x0644) ) {
destSize--;
}
}
}
break;
case U_SHAPE_LETTERS_UNSHAPE :
for(i=0;i<sourceLength;i++) {
if( isLamAlefChar(source[i]) ) {
destSize++;
}
}
break;
default :
break;
}
return destSize;
}
static int32_t
removeLamAlefSpaces(UChar *dest, int32_t sourceLength,
int32_t destSize,
uint32_t options,
UErrorCode *pErrorCode) {
int32_t i = 0, j = 0;
int32_t count = 0;
UChar *tempbuffer=NULL;
switch(options&U_SHAPE_LENGTH_MASK) {
case U_SHAPE_LENGTH_GROW_SHRINK :
tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
if(tempbuffer == NULL) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
i = j = 0;
while(i < sourceLength) {
if(dest[i] == 0xFFFF) {
j--;
count++;
}
else
tempbuffer[j] = dest[i];
i++;
j++;
}
while(count >= 0) {
tempbuffer[i] = 0x0000;
i--;
count--;
}
uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
destSize = u_strlen(dest);
break;
case U_SHAPE_LENGTH_FIXED_SPACES_NEAR :
i = 0;
while(i < sourceLength) {
if(dest[i] == 0xFFFF)
dest[i] = 0x0020;
i++;
}
destSize = sourceLength;
break;
case U_SHAPE_LENGTH_FIXED_SPACES_AT_BEGINNING :
tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
if(tempbuffer == NULL) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
i = j = sourceLength;
while(i >= 0) {
if(dest[i] == 0xFFFF) {
j++;
count++;
}
else
tempbuffer[j] = dest[i];
i--;
j--;
}
for(i=0;i<count;i++)
tempbuffer[i] = 0x0020;
uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
destSize = sourceLength;
break;
case U_SHAPE_LENGTH_FIXED_SPACES_AT_END :
tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
if(tempbuffer == NULL) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
i = j = 0;
while(i < sourceLength) {
if(dest[i] == 0xFFFF) {
j--;
count++;
}
else
tempbuffer[j] = dest[i];
i++;
j++;
}
while(count >= 0) {
tempbuffer[i] = 0x0020;
i--;
count--;
}
uprv_memcpy(dest,tempbuffer, sourceLength*U_SIZEOF_UCHAR);
destSize = sourceLength;
break;
default :
break;
}
if(tempbuffer)
uprv_free(tempbuffer);
return destSize;
}
static int32_t
expandLamAlef(UChar *dest, int32_t sourceLength,
int32_t destSize,uint32_t options,
UErrorCode *pErrorCode) {
int32_t i = 0,j = 0;
int32_t countl = 0;
int32_t countr = 0;
int32_t inpsize = sourceLength;
UChar lamalefChar;
UChar *tempbuffer=NULL;
switch(options&U_SHAPE_LENGTH_MASK) {
case U_SHAPE_LENGTH_GROW_SHRINK :
destSize = calculateSize(dest,sourceLength,destSize,options);
tempbuffer = (UChar *)uprv_malloc((destSize+1)*U_SIZEOF_UCHAR);
if(tempbuffer == NULL) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
uprv_memset(tempbuffer, 0, (destSize+1)*U_SIZEOF_UCHAR);
i = j = 0;
while(i < destSize && j < destSize) {
if( isLamAlefChar(dest[i]) ) {
tempbuffer[j] = convertLamAlef[ dest[i] - 0xFEF5 ];
tempbuffer[j+1] = 0x0644;
j++;
}
else
tempbuffer[j] = dest[i];
i++;
j++;
}
uprv_memcpy(dest, tempbuffer, destSize*U_SIZEOF_UCHAR);
break;
case U_SHAPE_LENGTH_FIXED_SPACES_NEAR :
for(i = 0;i<sourceLength;i++) {
if((dest[i] == 0x0020) && isLamAlefChar(dest[i+1])) {
lamalefChar = dest[i+1];
dest[i+1] = 0x0644;
dest[i] = convertLamAlef[ lamalefChar - 0xFEF5 ];
}
else
if((dest[i] != 0x0020) && isLamAlefChar(dest[i+1])) {
*pErrorCode=U_NO_SPACE_AVAILABLE;
}
}
destSize = sourceLength;
break;
case U_SHAPE_LENGTH_FIXED_SPACES_AT_BEGINNING :
tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
if(tempbuffer == NULL) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
i = 0;
while(dest[i] == 0x0020) {
countl++;
i++;
}
i = j = sourceLength-1;
while(i >= 0 && j >= 0) {
if( countl>0 && isLamAlefChar(dest[i]) ) {
tempbuffer[j] = 0x0644;
tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ];
j--;
countl--;
}
else {
if( countl == 0 && isLamAlefChar(dest[i]) )
*pErrorCode=U_NO_SPACE_AVAILABLE;
tempbuffer[j] = dest[i];
}
i--;
j--;
}
uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
destSize = sourceLength;
break;
case U_SHAPE_LENGTH_FIXED_SPACES_AT_END :
tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
if(tempbuffer == NULL) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
while(dest[inpsize-1] == 0x0020) {
countr++;
inpsize--;
}
i = sourceLength - countr - 1;
j = sourceLength - 1;
while(i >= 0 && j >= 0) {
if( countr>0 && isLamAlefChar(dest[i]) ) {
tempbuffer[j] = 0x0644;
tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ];
j--;
countr--;
}
else {
if( countr == 0 && isLamAlefChar(dest[i]) )
*pErrorCode=U_NO_SPACE_AVAILABLE;
tempbuffer[j] = dest[i];
}
i--;
j--;
}
if(countr > 0) {
uprv_memmove(tempbuffer, tempbuffer+countr, sourceLength*U_SIZEOF_UCHAR);
if(u_strlen(tempbuffer) < sourceLength) {
for(i=sourceLength-1;i>=sourceLength-countr;i--) {
tempbuffer[i] = 0x0020;
}
}
}
uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
destSize = sourceLength;
break;
default :
break;
}
if(tempbuffer)
uprv_free(tempbuffer);
return destSize;
}
static int32_t
shapeUnicode(UChar *dest, int32_t sourceLength,
int32_t destSize,uint32_t options,
UErrorCode *pErrorCode,
int tashkeelFlag) {
int32_t i, iend;
int32_t step;
int32_t lastPos,Nx, Nw;
unsigned int Shape;
int32_t lamalef_found = 0;
UChar prevLink = 0, lastLink = 0, currLink, nextLink = 0;
UChar wLamalef;
if ((options & U_SHAPE_PRESERVE_PRESENTATION_MASK) == U_SHAPE_PRESERVE_PRESENTATION_NOOP) {
for (i = 0; i < sourceLength; i++) {
UChar inputChar = dest[i];
if ( (inputChar >= 0xFB50) && (inputChar <= 0xFBFF)) {
UChar c = convertFBto06 [ (inputChar - 0xFB50) ];
if (c != 0)
dest[i] = c;
} else if ( (inputChar >= 0xFE70) && (inputChar <= 0xFEFC)) {
dest[i] = convertFEto06 [ (inputChar - 0xFE70) ] ;
} else {
dest[i] = inputChar ;
}
}
}
i = sourceLength - 1;
iend = -1;
step = -1;
currLink = getLink(dest[i]);
lastPos = i;
Nx = -2, Nw = 0;
while (i != iend) {
if ((currLink & 0xFF00) > 0 || (getLink(dest[i]) & IRRELEVANT) != 0) {
Nw = i + step;
while (Nx < 0) {
if(Nw == iend) {
nextLink = 0;
Nx = 3000;
} else {
nextLink = getLink(dest[Nw]);
if((nextLink & IRRELEVANT) == 0) {
Nx = Nw;
} else {
Nw = Nw + step;
}
}
}
if ( ((currLink & ALEFTYPE) > 0) && ((lastLink & LAMTYPE) > 0) ) {
lamalef_found = 1;
wLamalef = changeLamAlef(dest[i]);
if ( wLamalef != 0) {
dest[i] = 0xFFFF;
dest[lastPos] =wLamalef;
i=lastPos;
}
lastLink = prevLink;
currLink = getLink(wLamalef);
}
Shape = shapeTable[nextLink & (LINKR + LINKL)]
[lastLink & (LINKR + LINKL)]
[currLink & (LINKR + LINKL)];
if ((currLink & (LINKR+LINKL)) == 1) {
Shape &= 1;
} else if(isTashkeelChar(dest[i])) {
if( (lastLink & LINKL) && (nextLink & LINKR) && (tashkeelFlag == 1) &&
dest[i] != 0x064C && dest[i] != 0x064D )
{
Shape = 1;
if( (nextLink&ALEFTYPE) == ALEFTYPE && (lastLink&LAMTYPE) == LAMTYPE ) {
Shape = 0;
}
}
else {
Shape = 0;
}
}
if ((dest[i] ^ 0x0600) < 0x100) {
if(isTashkeelChar(dest[i]))
dest[i] = 0xFE70 + IrrelevantPos[(dest[i] - 0x064B)] + Shape;
else if ((currLink & APRESENT) > 0)
dest[i] = (UChar)(0xFB50 + (currLink >> 8) + Shape);
else if ((currLink >> 8) > 0 && (currLink & IRRELEVANT) == 0)
dest[i] = (UChar)(0xFE70 + (currLink >> 8) + Shape);
}
}
if ((currLink & IRRELEVANT) == 0) {
prevLink = lastLink;
lastLink = currLink;
lastPos = i;
}
i = i + step;
if (i == Nx) {
currLink = nextLink;
Nx = -2;
} else if(i != iend) {
currLink = getLink(dest[i]);
}
}
if(lamalef_found != 0)
destSize = removeLamAlefSpaces(dest,sourceLength,destSize,options,pErrorCode);
else
destSize = sourceLength;
return destSize;
}
static int32_t
deShapeUnicode(UChar *dest, int32_t sourceLength,
int32_t destSize,uint32_t options,
UErrorCode *pErrorCode) {
int32_t i = 0;
int32_t lamalef_found = 0;
for(i = 0; i < sourceLength; i++) {
UChar inputChar = dest[i];
if ( (inputChar >= 0xFB50) && (inputChar <= 0xFBFF)) {
UChar c = convertFBto06 [ (inputChar - 0xFB50) ];
if (c != 0)
dest[i] = c;
} else if (( inputChar >= 0xFE70) && (inputChar <= 0xFEF4 )) {
dest[i] = convertFEto06 [ (inputChar - 0xFE70) ] ;
} else {
dest[i] = inputChar ;
}
if( isLamAlefChar(dest[i]) )
lamalef_found = 1;
}
if(lamalef_found != 0)
destSize = expandLamAlef(dest,sourceLength,destSize,options,pErrorCode);
else
destSize = sourceLength;
return destSize;
}
U_CAPI int32_t U_EXPORT2
u_shapeArabic(const UChar *source, int32_t sourceLength,
UChar *dest, int32_t destCapacity,
uint32_t options,
UErrorCode *pErrorCode) {
int32_t destLength;
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
if( source==NULL || sourceLength<-1 || (dest==NULL && destCapacity!=0) || destCapacity<0 ||
(options&U_SHAPE_DIGIT_TYPE_RESERVED)==U_SHAPE_DIGIT_TYPE_RESERVED ||
(options&U_SHAPE_DIGITS_MASK)==U_SHAPE_DIGITS_RESERVED ||
((options&U_SHAPE_LENGTH_MASK) != U_SHAPE_LENGTH_GROW_SHRINK &&
(options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) != 0) ||
((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) == U_SHAPE_AGGREGATE_TASHKEEL &&
(options&U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) != U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED)
) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if(sourceLength==-1) {
sourceLength=u_strlen(source);
}
if(sourceLength<=0) {
return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
}
if( dest!=NULL &&
((source<=dest && dest<source+sourceLength) ||
(dest<=source && source<dest+destCapacity))) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if((options&U_SHAPE_LETTERS_MASK)!=U_SHAPE_LETTERS_NOOP) {
UChar buffer[300];
UChar *tempbuffer, *tempsource = NULL;
int32_t outputSize, spacesCountl=0, spacesCountr=0;
if((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK)>0) {
int32_t logical_order = (options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL;
int32_t aggregate_tashkeel =
(options&(U_SHAPE_AGGREGATE_TASHKEEL_MASK+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED)) ==
(U_SHAPE_AGGREGATE_TASHKEEL+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED);
int step=logical_order?1:-1;
int j=logical_order?-1:2*sourceLength;
int i=logical_order?-1:sourceLength;
int end=logical_order?sourceLength:-1;
int aggregation_possible = 1;
UChar prev = 0;
UChar prevLink, currLink = 0;
int newSourceLength = 0;
tempsource = (UChar *)uprv_malloc(2*sourceLength*U_SIZEOF_UCHAR);
if(tempsource == NULL) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
while ((i+=step) != end) {
prevLink = currLink;
currLink = getLink(source[i]);
if (aggregate_tashkeel && ((prevLink|currLink)&COMBINE) == COMBINE && aggregation_possible) {
aggregation_possible = 0;
tempsource[j] = (prev<source[i]?prev:source[i])-0x064C+0xFC5E;
currLink = getLink(tempsource[j]);
} else {
aggregation_possible = 1;
tempsource[j+=step] = source[i];
prev = source[i];
newSourceLength++;
}
}
source = tempsource+(logical_order?0:j);
sourceLength = newSourceLength;
}
if((options&U_SHAPE_LENGTH_MASK)==U_SHAPE_LENGTH_GROW_SHRINK) {
outputSize=calculateSize(source,sourceLength,destCapacity,options);
} else {
outputSize=sourceLength;
}
if(outputSize>destCapacity) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
if (tempsource != NULL) uprv_free(tempsource);
return outputSize;
}
if(sourceLength>outputSize) {
outputSize=sourceLength;
}
if(outputSize<=sizeof(buffer)/U_SIZEOF_UCHAR) {
outputSize=sizeof(buffer)/U_SIZEOF_UCHAR;
tempbuffer=buffer;
} else {
tempbuffer = (UChar *)uprv_malloc(outputSize*U_SIZEOF_UCHAR);
if(tempbuffer == NULL) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
if (tempsource != NULL) uprv_free(tempsource);
return 0;
}
}
uprv_memcpy(tempbuffer, source, sourceLength*U_SIZEOF_UCHAR);
if (tempsource != NULL) uprv_free(tempsource);
if(sourceLength<outputSize) {
uprv_memset(tempbuffer+sourceLength, 0, (outputSize-sourceLength)*U_SIZEOF_UCHAR);
}
if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) {
countSpaces(tempbuffer,sourceLength,options,&spacesCountl,&spacesCountr);
invertBuffer(tempbuffer,sourceLength,options,spacesCountl,spacesCountr);
}
switch(options&U_SHAPE_LETTERS_MASK) {
case U_SHAPE_LETTERS_SHAPE :
destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,1);
break;
case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED :
destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,0);
break;
case U_SHAPE_LETTERS_UNSHAPE :
destLength = deShapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode);
break;
default :
destLength = 0;
break;
}
if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) {
countSpaces(tempbuffer,destLength,options,&spacesCountl,&spacesCountr);
invertBuffer(tempbuffer,destLength,options,spacesCountl,spacesCountr);
}
uprv_memcpy(dest, tempbuffer, uprv_min(destLength, destCapacity)*U_SIZEOF_UCHAR);
if(tempbuffer!=buffer) {
uprv_free(tempbuffer);
}
if(destLength>destCapacity) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
return destLength;
}
} else {
if(destCapacity<sourceLength) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
return sourceLength;
}
uprv_memcpy(dest, source, sourceLength*U_SIZEOF_UCHAR);
destLength=sourceLength;
}
if((options&U_SHAPE_DIGITS_MASK)!=U_SHAPE_DIGITS_NOOP) {
UChar digitBase;
int32_t i;
switch(options&U_SHAPE_DIGIT_TYPE_MASK) {
case U_SHAPE_DIGIT_TYPE_AN:
digitBase=0x660;
break;
case U_SHAPE_DIGIT_TYPE_AN_EXTENDED:
digitBase=0x6f0;
break;
default:
digitBase=0;
break;
}
switch(options&U_SHAPE_DIGITS_MASK) {
case U_SHAPE_DIGITS_EN2AN:
digitBase-=0x30;
for(i=0; i<destLength; ++i) {
if(((uint32_t)dest[i]-0x30)<10) {
dest[i]+=digitBase;
}
}
break;
case U_SHAPE_DIGITS_AN2EN:
for(i=0; i<destLength; ++i) {
if(((uint32_t)dest[i]-(uint32_t)digitBase)<10) {
dest[i]-=digitBase-0x30;
}
}
break;
case U_SHAPE_DIGITS_ALEN2AN_INIT_LR:
_shapeToArabicDigitsWithContext(dest, destLength,
digitBase,
(UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL),
FALSE);
break;
case U_SHAPE_DIGITS_ALEN2AN_INIT_AL:
_shapeToArabicDigitsWithContext(dest, destLength,
digitBase,
(UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL),
TRUE);
break;
default:
break;
}
}
return u_terminateUChars(dest, destCapacity, destLength, pErrorCode);
}