#include "config.h"
#include "ShapeArabic.h"
#include <unicode/utypes.h>
#include <unicode/uchar.h>
#include <unicode/ustring.h>
#include <unicode/ushape.h>
#include <wtf/Assertions.h>
#define IRRELEVANT 4
#define LAMTYPE 16
#define ALEFTYPE 32
#define LINKR 1
#define LINKL 2
static const UChar IrrelevantPos[] = {
0x0, 0x2, 0x4, 0x6,
0x8, 0xA, 0xC, 0xE,
};
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, 4, 4, 4,
4, 4, 4, 4,
4, 4, 4, 0, 0,
0, 0, 0, 0,
1 + 256 * 0x85,
1 + 256 * 0x87,
1 + 256 * 0x89,
1 + 256 * 0x8B,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
4,
0,
1 + 32,
1 + 32,
0,
1 + 32,
1, 1,
1+2,
1+2 + 256 * 0x16,
1+2 + 256 * 0x0E,
1+2 + 256 * 0x02,
1+2, 1+2,
1+2 + 256 * 0x06,
1+2 + 256 * 0x12,
1+2 + 256 * 0x0A,
1+2, 1+2,
1+2 + 256 * 0x26,
1+2 + 256 * 0x22,
1+2,
1+2 + 256 * 0x2A,
1+2 + 256 * 0x2E,
1 + 256 * 0x38,
1, 1, 1,
1 + 256 * 0x34,
1 + 256 * 0x32,
1 + 256 * 0x36,
1, 1,
1 + 256 * 0x3C,
1, 1, 1, 1, 1, 1,
1 + 256 * 0x3A,
1,
1+2, 1+2, 1+2, 1+2, 1+2, 1+2,
1+2, 1+2, 1+2, 1+2,
1+2 + 256 * 0x2E,
1+2,
1+2 + 256 * 0x1E,
1+2, 1+2,
1+2 + 256 * 0x3E,
1+2, 1+2, 1+2,
1+2 + 256 * 0x83,
1+2,
1+2 + 256 * 0x42,
1+2,
1+2 + 256 * 0x4A,
1+2,
1+2 + 256 * 0x46,
1+2, 1+2, 1+2, 1+2, 1+2, 1+2,
1+2, 1+2 + 256 * 0x50,
1+2, 1+2,
1+2 + 256 * 0x5A,
1+2,
1,
1+2 + 256 * 0x56,
1+2,
1, 1,
1 + 256 * 0x90,
1 + 256 * 0x89,
1 + 256 * 0x87,
1 + 256 * 0x8B,
1 + 256 * 0x92,
1,
1 + 256 * 0x8E,
1+2 + 256 * 0xAC,
1,
1+2,
1,
1+2 + 256 * 0x94,
1+2,
1 + 256 * 0x5E,
1 + 256 * 0x60
};
static const UChar presLink[141]=
{
1 + 2,
1 + 2,
1 + 2, 0, 1+ 2, 0, 1+ 2,
1 + 2,
1+ 2, 1 + 2, 1+2, 1 + 2,
1+ 2, 1 + 2, 1+2, 1 + 2,
0, 0 + 32, 1 + 32, 0 + 32,
1 + 32, 0, 1, 0 + 32,
1 + 32, 0, 2, 1 + 2,
1, 0 + 32, 1 + 32, 0,
2, 1 + 2, 1, 0,
1, 0, 2, 1 + 2,
1, 0, 2, 1 + 2,
1, 0, 2, 1 + 2,
1, 0, 2, 1 + 2,
1, 0, 2, 1 + 2,
1, 0, 1, 0,
1, 0, 1, 0,
1, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0 + 16, 2 + 16, 1 + 2 +16,
1 + 16, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0, 2, 1+2,
1, 0, 1, 0,
1, 0, 2, 1+2,
1, 0, 1, 0,
1, 0, 1, 0,
1
};
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 UChar 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 UChar
changeLamAlef(UChar ch) {
switch(ch) {
case 0x0622 :
return(0x065C);
break;
case 0x0623 :
return(0x065D);
break;
case 0x0625 :
return(0x065E);
break;
case 0x0627 :
return(0x065F);
break;
default :
return(0);
break;
}
}
static int32_t
specialChar(UChar ch) {
if( (ch>0x0621 && ch<0x0626)||(ch==0x0627)||(ch>0x062e && ch<0x0633)||
(ch>0x0647 && ch<0x064a)||(ch==0x0629) ) {
return (1);
}
else
if( ch>=0x064B && ch<= 0x0652 )
return (2);
else
if( (ch>=0x0653 && ch<= 0x0655) || ch == 0x0670 ||
(ch>=0xFE70 && ch<= 0xFE7F) )
return (3);
else
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 >= 0xFE70 && ch <= 0xFEFC) {
return(presLink[ch-0xFE70]);
} else {
return(0);
}
}
static int32_t
isTashkeelChar(UChar ch) {
if( ch>=0x064B && ch<= 0x0652 )
return (1);
else
return (0);
}
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 prevPos, lastPos,Nx, Nw;
unsigned int Shape;
int32_t flag;
int32_t lamalef_found = 0;
UChar prevLink = 0, lastLink = 0, currLink, nextLink = 0;
UChar wLamalef;
for (i = 0; i < sourceLength; i++) {
UChar inputChar = dest[i];
if ( (inputChar >= 0xFE70) && (inputChar <= 0xFEFC)) {
dest[i] = convertFEto06 [ (inputChar - 0xFE70) ] ;
}
}
i = 0;
iend = sourceLength;
currLink = getLink(dest[i]);
prevPos = i;
lastPos = i;
Nx = sourceLength + 2, Nw = 0;
while (i != iend) {
if ((currLink & 0xFF00) > 0 || isTashkeelChar(dest[i])) {
Nw = i + 1;
while (Nx >= sourceLength) {
if(Nw == iend) {
nextLink = 0;
Nx = -1;
} else {
nextLink = getLink(dest[Nw]);
if((nextLink & IRRELEVANT) == 0) {
Nx = Nw;
} else {
Nw = Nw + 1;
}
}
}
if ( ((currLink & ALEFTYPE) > 0) && ((lastLink & LAMTYPE) > 0) ) {
lamalef_found = 1;
wLamalef = changeLamAlef(dest[i]);
if ( wLamalef != 0) {
dest[i] = ' ';
dest[lastPos] =wLamalef;
i=lastPos;
}
lastLink = prevLink;
currLink = getLink(wLamalef);
}
flag = specialChar(dest[i]);
Shape = shapeTable[nextLink & (LINKR + LINKL)]
[lastLink & (LINKR + LINKL)]
[currLink & (LINKR + LINKL)];
if (flag == 1) {
Shape = (Shape == 1 || Shape == 3) ? 1 : 0;
}
else
if(flag == 2) {
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(flag == 2) {
dest[i] = 0xFE70 + IrrelevantPos[(dest[i] - 0x064B)] + Shape;
}
else
dest[i] = (UChar)((dest[i] < 0x0670 ? 0xFE70 : 0xFB50) + (currLink >> 8) + Shape);
}
if ((currLink & IRRELEVANT) == 0) {
prevLink = lastLink;
lastLink = currLink;
prevPos = lastPos;
lastPos = i;
}
i++;
if (i == Nx) {
currLink = nextLink;
Nx = sourceLength + 2;
}
else if(i != iend) {
currLink = getLink(dest[i]);
}
}
destSize = sourceLength;
return destSize;
}
int32_t 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 ||
(options&U_SHAPE_DIGITS_MASK)>=U_SHAPE_DIGITS_RESERVED
) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if(sourceLength==-1) {
sourceLength=u_strlen(source);
}
if(sourceLength==0) {
return 0;
}
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) {
int32_t outputSize = sourceLength;
ASSERT((options&U_SHAPE_LENGTH_MASK) != U_SHAPE_LENGTH_GROW_SHRINK);
if(outputSize>destCapacity) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
return outputSize;
}
memcpy(dest, source, sourceLength*U_SIZEOF_UCHAR);
ASSERT((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL);
switch(options&U_SHAPE_LETTERS_MASK) {
case U_SHAPE_LETTERS_SHAPE :
destLength = shapeUnicode(dest,sourceLength,destCapacity,options,pErrorCode,1);
break;
case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED :
destLength = shapeUnicode(dest,sourceLength,destCapacity,options,pErrorCode,0);
break;
case U_SHAPE_LETTERS_UNSHAPE :
ASSERT_NOT_REACHED();
break;
default :
destLength = 0;
break;
}
} else
ASSERT_NOT_REACHED();
ASSERT((options & U_SHAPE_DIGITS_MASK) == U_SHAPE_DIGITS_NOOP);
return sourceLength;
}