Home | History | Annotate | Download | only in mac
      1 /*
      2 ******************************************************************************
      3 *
      4 *   Copyright (C) 2000-2004, International Business Machines
      5 *   Corporation and others. All Rights Reserved.
      6 *   Copyright (C) 2007 Apple Inc. All rights reserved.
      7 *
      8 *   Permission is hereby granted, free of charge, to any person obtaining a copy of this
      9 *   software and associated documentation files (the "Software"), to deal in the Software
     10 *   without restriction, including without limitation the rights to use, copy, modify,
     11 *   merge, publish, distribute, and/or sell copies of the Software, and to permit persons
     12 *   to whom the Software is furnished to do so, provided that the above copyright notice(s)
     13 *   and this permission notice appear in all copies of the Software and that both the above
     14 *   copyright notice(s) and this permission notice appear in supporting documentation.
     15 *
     16 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
     17 *   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
     18 *   PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER
     19 *   OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR
     20 *   CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
     21 *   PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     22 *   OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     23 *
     24 *   Except as contained in this notice, the name of a copyright holder shall not be used in
     25 *   advertising or otherwise to promote the sale, use or other dealings in this Software
     26 *   without prior written authorization of the copyright holder.
     27 *
     28 ******************************************************************************
     29 *
     30 *   Arabic letter shaping implemented by Ayman Roshdy
     31 */
     32 
     33 #include "config.h"
     34 
     35 #if USE(ATSUI)
     36 
     37 #include "ShapeArabic.h"
     38 
     39 #include <stdbool.h>
     40 #include <string.h>
     41 #include <unicode/utypes.h>
     42 #include <unicode/uchar.h>
     43 #include <unicode/ustring.h>
     44 #include <unicode/ushape.h>
     45 #include <wtf/Assertions.h>
     46 
     47 /*
     48  * ### TODO in general for letter shaping:
     49  * - the letter shaping code is UTF-16-unaware; needs update
     50  *   + especially invertBuffer()?!
     51  * - needs to handle the "Arabic Tail" that is used in some legacy codepages
     52  *   as a glyph fragment of wide-glyph letters
     53  *   + IBM Unicode conversion tables map it to U+200B (ZWSP)
     54  *   + IBM Egypt has proposed to encode the tail in Unicode among Arabic Presentation Forms
     55  */
     56 
     57 /* definitions for Arabic letter shaping ------------------------------------ */
     58 
     59 #define IRRELEVANT 4
     60 #define LAMTYPE    16
     61 #define ALEFTYPE   32
     62 #define LINKR      1
     63 #define LINKL      2
     64 
     65 static const UChar IrrelevantPos[] = {
     66     0x0, 0x2, 0x4, 0x6,
     67     0x8, 0xA, 0xC, 0xE,
     68 };
     69 
     70 static const UChar araLink[178]=
     71 {
     72   1           + 32 + 256 * 0x11,/*0x0622*/
     73   1           + 32 + 256 * 0x13,/*0x0623*/
     74   1                + 256 * 0x15,/*0x0624*/
     75   1           + 32 + 256 * 0x17,/*0x0625*/
     76   1 + 2            + 256 * 0x19,/*0x0626*/
     77   1           + 32 + 256 * 0x1D,/*0x0627*/
     78   1 + 2            + 256 * 0x1F,/*0x0628*/
     79   1                + 256 * 0x23,/*0x0629*/
     80   1 + 2            + 256 * 0x25,/*0x062A*/
     81   1 + 2            + 256 * 0x29,/*0x062B*/
     82   1 + 2            + 256 * 0x2D,/*0x062C*/
     83   1 + 2            + 256 * 0x31,/*0x062D*/
     84   1 + 2            + 256 * 0x35,/*0x062E*/
     85   1                + 256 * 0x39,/*0x062F*/
     86   1                + 256 * 0x3B,/*0x0630*/
     87   1                + 256 * 0x3D,/*0x0631*/
     88   1                + 256 * 0x3F,/*0x0632*/
     89   1 + 2            + 256 * 0x41,/*0x0633*/
     90   1 + 2            + 256 * 0x45,/*0x0634*/
     91   1 + 2            + 256 * 0x49,/*0x0635*/
     92   1 + 2            + 256 * 0x4D,/*0x0636*/
     93   1 + 2            + 256 * 0x51,/*0x0637*/
     94   1 + 2            + 256 * 0x55,/*0x0638*/
     95   1 + 2            + 256 * 0x59,/*0x0639*/
     96   1 + 2            + 256 * 0x5D,/*0x063A*/
     97   0, 0, 0, 0, 0,                /*0x063B-0x063F*/
     98   1 + 2,                        /*0x0640*/
     99   1 + 2            + 256 * 0x61,/*0x0641*/
    100   1 + 2            + 256 * 0x65,/*0x0642*/
    101   1 + 2            + 256 * 0x69,/*0x0643*/
    102   1 + 2       + 16 + 256 * 0x6D,/*0x0644*/
    103   1 + 2            + 256 * 0x71,/*0x0645*/
    104   1 + 2            + 256 * 0x75,/*0x0646*/
    105   1 + 2            + 256 * 0x79,/*0x0647*/
    106   1                + 256 * 0x7D,/*0x0648*/
    107   1                + 256 * 0x7F,/*0x0649*/
    108   1 + 2            + 256 * 0x81,/*0x064A*/
    109   4, 4, 4, 4,                   /*0x064B-0x064E*/
    110   4, 4, 4, 4,                   /*0x064F-0x0652*/
    111   4, 4, 4, 0, 0,                /*0x0653-0x0657*/
    112   0, 0, 0, 0,                   /*0x0658-0x065B*/
    113   1                + 256 * 0x85,/*0x065C*/
    114   1                + 256 * 0x87,/*0x065D*/
    115   1                + 256 * 0x89,/*0x065E*/
    116   1                + 256 * 0x8B,/*0x065F*/
    117   0, 0, 0, 0, 0,                /*0x0660-0x0664*/
    118   0, 0, 0, 0, 0,                /*0x0665-0x0669*/
    119   0, 0, 0, 0, 0, 0,             /*0x066A-0x066F*/
    120   4,                            /*0x0670*/
    121   0,                            /*0x0671*/
    122   1           + 32,             /*0x0672*/
    123   1           + 32,             /*0x0673*/
    124   0,                            /*0x0674*/
    125   1           + 32,             /*0x0675*/
    126   1, 1,                         /*0x0676-0x0677*/
    127   1+2,                          /*0x0678*/
    128   1+2              + 256 * 0x16,/*0x0679*/
    129   1+2              + 256 * 0x0E,/*0x067A*/
    130   1+2              + 256 * 0x02,/*0x067B*/
    131   1+2, 1+2,                     /*0x067C-0x067D*/
    132   1+2              + 256 * 0x06,/*0x067E*/
    133   1+2              + 256 * 0x12,/*0x067F*/
    134   1+2              + 256 * 0x0A,/*0x0680*/
    135   1+2, 1+2,                     /*0x0681-0x0682*/
    136   1+2              + 256 * 0x26,/*0x0683*/
    137   1+2              + 256 * 0x22,/*0x0684*/
    138   1+2,                          /*0x0685*/
    139   1+2              + 256 * 0x2A,/*0x0686*/
    140   1+2              + 256 * 0x2E,/*0x0687*/
    141   1                + 256 * 0x38,/*0x0688*/
    142   1, 1, 1,                      /*0x0689-0x068B*/
    143   1                + 256 * 0x34,/*0x068C*/
    144   1                + 256 * 0x32,/*0x068D*/
    145   1                + 256 * 0x36,/*0x068E*/
    146   1, 1,                         /*0x068F-0x0690*/
    147   1                + 256 * 0x3C,/*0x0691*/
    148   1, 1, 1, 1, 1, 1,             /*0x0692-0x0697*/
    149   1                + 256 * 0x3A,/*0x0698*/
    150   1,                            /*0x0699*/
    151   1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x069A-0x069F*/
    152   1+2, 1+2, 1+2, 1+2,           /*0x06A0-0x06A3*/
    153   1+2              + 256 * 0x2E,/*0x06A4*/
    154   1+2,                          /*0x06A5*/
    155   1+2              + 256 * 0x1E,/*0x06A6*/
    156   1+2, 1+2,                     /*0x06A7-0x06A8*/
    157   1+2              + 256 * 0x3E,/*0x06A9*/
    158   1+2, 1+2, 1+2,                /*0x06AA-0x06AC*/
    159   1+2              + 256 * 0x83,/*0x06AD*/
    160   1+2,                          /*0x06AE*/
    161   1+2              + 256 * 0x42,/*0x06AF*/
    162   1+2,                          /*0x06B0*/
    163   1+2              + 256 * 0x4A,/*0x06B1*/
    164   1+2,                          /*0x06B2*/
    165   1+2              + 256 * 0x46,/*0x06B3*/
    166   1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06B4-0x06B9*/
    167   1+2,                          /*0x06BA*/          // FIXME: Seems to have a final form
    168   1+2              + 256 * 0x50,/*0x06BB*/
    169   1+2, 1+2,                     /*0x06BC-0x06BD*/
    170   1+2              + 256 * 0x5A,/*0x06BE*/
    171   1+2,                          /*0x06BF*/
    172   1,                            /*0x06C0*/
    173   1+2              + 256 * 0x56,/*0x06C1*/
    174   1+2,                          /*0x06C2*/
    175   1, 1,                         /*0x06C3-0x06C4*/
    176   1                + 256 * 0x90,/*0x06C5*/
    177   1                + 256 * 0x89,/*0x06C6*/
    178   1                + 256 * 0x87,/*0x06C7*/
    179   1                + 256 * 0x8B,/*0x06C8*/
    180   1                + 256 * 0x92,/*0x06C9*/
    181   1,                            /*0x06CA*/
    182   1                + 256 * 0x8E,/*0x06CB*/
    183   1+2              + 256 * 0xAC,/*0x06CC*/
    184   1,                            /*0x06CD*/
    185   1+2,                          /*0x06CE*/
    186   1,                            /*0x06CF*/
    187   1+2              + 256 * 0x94,/*0x06D0*/
    188   1+2,                          /*0x06D1*/
    189   1                + 256 * 0x5E,/*0x06D2*/
    190   1                + 256 * 0x60 /*0x06D3*/
    191 };
    192 
    193 static const UChar presLink[141]=
    194 {
    195   1 + 2,                        /*0xFE70*/
    196   1 + 2,                        /*0xFE71*/
    197   1 + 2, 0, 1+ 2, 0, 1+ 2,      /*0xFE72-0xFE76*/
    198   1 + 2,                        /*0xFE77*/
    199   1+ 2, 1 + 2, 1+2, 1 + 2,      /*0xFE78-0xFE81*/
    200   1+ 2, 1 + 2, 1+2, 1 + 2,      /*0xFE82-0xFE85*/
    201   0, 0 + 32, 1 + 32, 0 + 32,    /*0xFE86-0xFE89*/
    202   1 + 32, 0, 1,  0 + 32,        /*0xFE8A-0xFE8D*/
    203   1 + 32, 0, 2,  1 + 2,         /*0xFE8E-0xFE91*/
    204   1, 0 + 32, 1 + 32, 0,         /*0xFE92-0xFE95*/
    205   2, 1 + 2, 1, 0,               /*0xFE96-0xFE99*/
    206   1, 0, 2, 1 + 2,               /*0xFE9A-0xFE9D*/
    207   1, 0, 2, 1 + 2,               /*0xFE9E-0xFEA1*/
    208   1, 0, 2, 1 + 2,               /*0xFEA2-0xFEA5*/
    209   1, 0, 2, 1 + 2,               /*0xFEA6-0xFEA9*/
    210   1, 0, 2, 1 + 2,               /*0xFEAA-0xFEAD*/
    211   1, 0, 1, 0,                   /*0xFEAE-0xFEB1*/
    212   1, 0, 1, 0,                   /*0xFEB2-0xFEB5*/
    213   1, 0, 2, 1+2,                 /*0xFEB6-0xFEB9*/
    214   1, 0, 2, 1+2,                 /*0xFEBA-0xFEBD*/
    215   1, 0, 2, 1+2,                 /*0xFEBE-0xFEC1*/
    216   1, 0, 2, 1+2,                 /*0xFEC2-0xFEC5*/
    217   1, 0, 2, 1+2,                 /*0xFEC6-0xFEC9*/
    218   1, 0, 2, 1+2,                 /*0xFECA-0xFECD*/
    219   1, 0, 2, 1+2,                 /*0xFECE-0xFED1*/
    220   1, 0, 2, 1+2,                 /*0xFED2-0xFED5*/
    221   1, 0, 2, 1+2,                 /*0xFED6-0xFED9*/
    222   1, 0, 2, 1+2,                 /*0xFEDA-0xFEDD*/
    223   1, 0, 2, 1+2,                 /*0xFEDE-0xFEE1*/
    224   1, 0 + 16, 2 + 16, 1 + 2 +16, /*0xFEE2-0xFEE5*/
    225   1 + 16, 0, 2, 1+2,            /*0xFEE6-0xFEE9*/
    226   1, 0, 2, 1+2,                 /*0xFEEA-0xFEED*/
    227   1, 0, 2, 1+2,                 /*0xFEEE-0xFEF1*/
    228   1, 0, 1, 0,                   /*0xFEF2-0xFEF5*/
    229   1, 0, 2, 1+2,                 /*0xFEF6-0xFEF9*/
    230   1, 0, 1, 0,                   /*0xFEFA-0xFEFD*/
    231   1, 0, 1, 0,
    232   1
    233 };
    234 
    235 static const UChar convertFEto06[] =
    236 {
    237 /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
    238 /*FE7*/   0x64B, 0x64B, 0x64C, 0x64C, 0x64D, 0x64D, 0x64E, 0x64E, 0x64F, 0x64F, 0x650, 0x650, 0x651, 0x651, 0x652, 0x652,
    239 /*FE8*/   0x621, 0x622, 0x622, 0x623, 0x623, 0x624, 0x624, 0x625, 0x625, 0x626, 0x626, 0x626, 0x626, 0x627, 0x627, 0x628,
    240 /*FE9*/   0x628, 0x628, 0x628, 0x629, 0x629, 0x62A, 0x62A, 0x62A, 0x62A, 0x62B, 0x62B, 0x62B, 0x62B, 0x62C, 0x62C, 0x62C,
    241 /*FEA*/   0x62C, 0x62D, 0x62D, 0x62D, 0x62D, 0x62E, 0x62E, 0x62E, 0x62E, 0x62F, 0x62F, 0x630, 0x630, 0x631, 0x631, 0x632,
    242 /*FEB*/   0x632, 0x633, 0x633, 0x633, 0x633, 0x634, 0x634, 0x634, 0x634, 0x635, 0x635, 0x635, 0x635, 0x636, 0x636, 0x636,
    243 /*FEC*/   0x636, 0x637, 0x637, 0x637, 0x637, 0x638, 0x638, 0x638, 0x638, 0x639, 0x639, 0x639, 0x639, 0x63A, 0x63A, 0x63A,
    244 /*FED*/   0x63A, 0x641, 0x641, 0x641, 0x641, 0x642, 0x642, 0x642, 0x642, 0x643, 0x643, 0x643, 0x643, 0x644, 0x644, 0x644,
    245 /*FEE*/   0x644, 0x645, 0x645, 0x645, 0x645, 0x646, 0x646, 0x646, 0x646, 0x647, 0x647, 0x647, 0x647, 0x648, 0x648, 0x649,
    246 /*FEF*/   0x649, 0x64A, 0x64A, 0x64A, 0x64A, 0x65C, 0x65C, 0x65D, 0x65D, 0x65E, 0x65E, 0x65F, 0x65F
    247 };
    248 
    249 static const UChar shapeTable[4][4][4]=
    250 {
    251   { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,1} },
    252   { {0,0,2,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} },
    253   { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,3} },
    254   { {0,0,1,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} }
    255 };
    256 
    257 /*
    258  *Name     : changeLamAlef
    259  *Function : Converts the Alef characters into an equivalent
    260  *           LamAlef location in the 0x06xx Range, this is an
    261  *           intermediate stage in the operation of the program
    262  *           later it'll be converted into the 0xFExx LamAlefs
    263  *           in the shaping function.
    264  */
    265 static UChar
    266 changeLamAlef(UChar ch) {
    267 
    268     switch(ch) {
    269     case 0x0622 :
    270         return(0x065C);
    271         break;
    272     case 0x0623 :
    273         return(0x065D);
    274         break;
    275     case 0x0625 :
    276         return(0x065E);
    277         break;
    278     case 0x0627 :
    279         return(0x065F);
    280         break;
    281     default :
    282         return(0);
    283         break;
    284     }
    285 }
    286 
    287 /*
    288  *Name     : specialChar
    289  *Function : Special Arabic characters need special handling in the shapeUnicode
    290  *           function, this function returns 1 or 2 for these special characters
    291  */
    292 static int32_t
    293 specialChar(UChar ch) {
    294 
    295     if( (ch>0x0621 && ch<0x0626)||(ch==0x0627)||(ch>0x062e && ch<0x0633)||
    296         (ch>0x0647 && ch<0x064a)||(ch==0x0629) ) {
    297         return (1);
    298     }
    299     else
    300     if( ch>=0x064B && ch<= 0x0652 )
    301         return (2);
    302     else
    303     if( (ch>=0x0653 && ch<= 0x0655) || ch == 0x0670 ||
    304         (ch>=0xFE70 && ch<= 0xFE7F) )
    305         return (3);
    306     else
    307         return (0);
    308 }
    309 
    310 /*
    311  *Name     : getLink
    312  *Function : Resolves the link between the characters as
    313  *           Arabic characters have four forms :
    314  *           Isolated, Initial, Middle and Final Form
    315  */
    316 static UChar
    317 getLink(UChar ch) {
    318 
    319     if(ch >= 0x0622 && ch <= 0x06D3) {
    320         return(araLink[ch-0x0622]);
    321     } else if(ch == 0x200D) {
    322         return(3);
    323     } else if(ch >= 0x206D && ch <= 0x206F) {
    324         return(4);
    325     } else if(ch >= 0xFE70 && ch <= 0xFEFC) {
    326         return(presLink[ch-0xFE70]);
    327     } else {
    328         return(0);
    329     }
    330 }
    331 
    332 /*
    333  *Name     : isTashkeelChar
    334  *Function : Returns 1 for Tashkeel characters else return 0
    335  */
    336 static int32_t
    337 isTashkeelChar(UChar ch) {
    338 
    339     if( ch>=0x064B && ch<= 0x0652 )
    340         return (1);
    341     else
    342         return (0);
    343 }
    344 
    345 /*
    346  *Name     : shapeUnicode
    347  *Function : Converts an Arabic Unicode buffer in 06xx Range into a shaped
    348  *           arabic Unicode buffer in FExx Range
    349  */
    350 static int32_t
    351 shapeUnicode(UChar *dest, int32_t sourceLength,
    352              int32_t destSize,
    353              int tashkeelFlag) {
    354 
    355     int32_t          i, iend;
    356     int32_t          prevPos, lastPos,Nx, Nw;
    357     unsigned int     Shape;
    358     int32_t          flag;
    359     int32_t          lamalef_found = 0;
    360     UChar            prevLink = 0, lastLink = 0, currLink, nextLink = 0;
    361     UChar            wLamalef;
    362 
    363     /*
    364      * Converts the input buffer from FExx Range into 06xx Range
    365      * to make sure that all characters are in the 06xx range
    366      * even the lamalef is converted to the special region in
    367      * the 06xx range
    368      */
    369     for (i = 0; i < sourceLength; i++) {
    370         UChar inputChar = dest[i];
    371         if ( (inputChar >= 0xFE70) && (inputChar <= 0xFEFC)) {
    372             dest[i] = convertFEto06 [ (inputChar - 0xFE70) ] ;
    373         }
    374     }
    375 
    376     /* sets the index to the end of the buffer, together with the step point to -1 */
    377     i = 0;
    378     iend = sourceLength;
    379 
    380     /*
    381      * This function resolves the link between the characters .
    382      * Arabic characters have four forms :
    383      * Isolated Form, Initial Form, Middle Form and Final Form
    384      */
    385     currLink = getLink(dest[i]);
    386 
    387     prevPos = i;
    388     lastPos = i;
    389     Nx = sourceLength + 2, Nw = 0;
    390 
    391     while (i != iend) {
    392         /* If high byte of currLink > 0 then more than one shape */
    393         if ((currLink & 0xFF00) > 0 || isTashkeelChar(dest[i])) {
    394             Nw = i + 1;
    395             while (Nx >= sourceLength) {         /* we need to know about next char */
    396                 if(Nw == iend) {
    397                     nextLink = 0;
    398                     Nx = -1;
    399                 } else {
    400                     nextLink = getLink(dest[Nw]);
    401                     if((nextLink & IRRELEVANT) == 0) {
    402                         Nx = Nw;
    403                     } else {
    404                         Nw = Nw + 1;
    405                     }
    406                 }
    407             }
    408 
    409             if ( ((currLink & ALEFTYPE) > 0)  &&  ((lastLink & LAMTYPE) > 0) ) {
    410                 lamalef_found = 1;
    411                 wLamalef = changeLamAlef(dest[i]); /*get from 0x065C-0x065f */
    412                 if ( wLamalef != 0) {
    413                     dest[i] = ' ';               /* The default case is to drop the Alef and replace */
    414                     dest[lastPos] =wLamalef;     /* it by a space.                                   */
    415                     i=lastPos;
    416                 }
    417                 lastLink = prevLink;
    418                 currLink = getLink(wLamalef);
    419             }
    420             /*
    421              * get the proper shape according to link ability of neighbors
    422              * and of character; depends on the order of the shapes
    423              * (isolated, initial, middle, final) in the compatibility area
    424              */
    425              flag  = specialChar(dest[i]);
    426 
    427              Shape = shapeTable[nextLink & (LINKR + LINKL)]
    428                                [lastLink & (LINKR + LINKL)]
    429                                [currLink & (LINKR + LINKL)];
    430 
    431              if (flag == 1) {
    432                  Shape = (Shape == 1 || Shape == 3) ? 1 : 0;
    433              }
    434              else
    435              if(flag == 2) {
    436                  if( (lastLink & LINKL) && (nextLink & LINKR) && (tashkeelFlag == 1) &&
    437                       dest[i] != 0x064C && dest[i] != 0x064D ) {
    438                      Shape = 1;
    439                      if( (nextLink&ALEFTYPE) == ALEFTYPE && (lastLink&LAMTYPE) == LAMTYPE )
    440                          Shape = 0;
    441                  }
    442                  else {
    443                      Shape = 0;
    444                  }
    445              }
    446 
    447              if(flag == 2) {
    448                  dest[i] =  0xFE70 + IrrelevantPos[(dest[i] - 0x064B)] + Shape;
    449              }
    450              else
    451                  dest[i] = (UChar)((dest[i] < 0x0670 ? 0xFE70 : 0xFB50) + (currLink >> 8) + Shape);
    452         }
    453 
    454         /* move one notch forward */
    455         if ((currLink & IRRELEVANT) == 0) {
    456               prevLink = lastLink;
    457               lastLink = currLink;
    458               prevPos = lastPos;
    459               lastPos = i;
    460         }
    461 
    462         i++;
    463         if (i == Nx) {
    464             currLink = nextLink;
    465             Nx = sourceLength + 2;
    466         }
    467         else if(i != iend) {
    468             currLink = getLink(dest[i]);
    469         }
    470     }
    471 
    472     destSize = sourceLength;
    473 
    474     return destSize;
    475 }
    476 
    477 int32_t shapeArabic(const UChar *source, int32_t sourceLength, UChar *dest, int32_t destCapacity, uint32_t options, UErrorCode *pErrorCode) {
    478     int32_t destLength;
    479 
    480     /* usual error checking */
    481     if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
    482         return 0;
    483     }
    484 
    485     /* make sure that no reserved options values are used; allow dest==NULL only for preflighting */
    486     if( source==NULL || sourceLength<-1 ||
    487         (dest==NULL && destCapacity!=0) || destCapacity<0 ||
    488         options>=U_SHAPE_DIGIT_TYPE_RESERVED ||
    489         (options&U_SHAPE_DIGITS_MASK)>=U_SHAPE_DIGITS_RESERVED
    490     ) {
    491         *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
    492         return 0;
    493     }
    494 
    495     /* determine the source length */
    496     if(sourceLength==-1) {
    497         sourceLength=u_strlen(source);
    498     }
    499     if(sourceLength==0) {
    500         return 0;
    501     }
    502 
    503     /* check that source and destination do not overlap */
    504     if( dest!=NULL &&
    505         ((source<=dest && dest<source+sourceLength) ||
    506          (dest<=source && source<dest+destCapacity))
    507     ) {
    508         *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
    509         return 0;
    510     }
    511 
    512     if((options&U_SHAPE_LETTERS_MASK)!=U_SHAPE_LETTERS_NOOP) {
    513         int32_t outputSize = sourceLength;
    514 
    515         /* calculate destination size */
    516         /* TODO: do we ever need to do this pure preflighting? */
    517         ASSERT((options&U_SHAPE_LENGTH_MASK) != U_SHAPE_LENGTH_GROW_SHRINK);
    518 
    519         if(outputSize>destCapacity) {
    520             *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
    521             return outputSize;
    522         }
    523 
    524         /* Start of Arabic letter shaping part */
    525         memcpy(dest, source, sourceLength*U_SIZEOF_UCHAR);
    526 
    527         ASSERT((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL);
    528 
    529         switch(options&U_SHAPE_LETTERS_MASK) {
    530         case U_SHAPE_LETTERS_SHAPE :
    531             /* Call the shaping function with tashkeel flag == 1 */
    532             destLength = shapeUnicode(dest,sourceLength,destCapacity,1);
    533             break;
    534         case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED :
    535             /* Call the shaping function with tashkeel flag == 0 */
    536             destLength = shapeUnicode(dest,sourceLength,destCapacity,0);
    537             break;
    538         case U_SHAPE_LETTERS_UNSHAPE :
    539             ASSERT_NOT_REACHED();
    540             break;
    541         default :
    542             /* will never occur because of validity checks above */
    543             destLength = 0;
    544             break;
    545         }
    546 
    547         /* End of Arabic letter shaping part */
    548     } else
    549         ASSERT_NOT_REACHED();
    550 
    551     ASSERT((options & U_SHAPE_DIGITS_MASK) == U_SHAPE_DIGITS_NOOP);
    552 
    553     return sourceLength;
    554 }
    555 
    556 #endif // USE(ATSUI)
    557