Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | ****************************************************************************** |
| 3 | * |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 4 | * Copyright (C) 2000-2012, International Business Machines |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 5 | * Corporation and others. All Rights Reserved. |
| 6 | * |
| 7 | ****************************************************************************** |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 8 | * file name: ushape.cpp |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 9 | * encoding: US-ASCII |
| 10 | * tab size: 8 (not used) |
| 11 | * indentation:4 |
| 12 | * |
| 13 | * created on: 2000jun29 |
| 14 | * created by: Markus W. Scherer |
| 15 | * |
| 16 | * Arabic letter shaping implemented by Ayman Roshdy |
| 17 | */ |
| 18 | |
| 19 | #include "unicode/utypes.h" |
| 20 | #include "unicode/uchar.h" |
| 21 | #include "unicode/ustring.h" |
| 22 | #include "unicode/ushape.h" |
| 23 | #include "cmemory.h" |
| 24 | #include "putilimp.h" |
| 25 | #include "ustr_imp.h" |
| 26 | #include "ubidi_props.h" |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 27 | #include "uassert.h" |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 28 | |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 29 | #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) |
| 30 | |
| 31 | /* |
| 32 | * This implementation is designed for 16-bit Unicode strings. |
| 33 | * The main assumption is that the Arabic characters and their |
| 34 | * presentation forms each fit into a single UChar. |
| 35 | * With UTF-8, they occupy 2 or 3 bytes, and more than the ASCII |
| 36 | * characters. |
| 37 | */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * ### TODO in general for letter shaping: |
| 41 | * - the letter shaping code is UTF-16-unaware; needs update |
| 42 | * + especially invertBuffer()?! |
| 43 | * - needs to handle the "Arabic Tail" that is used in some legacy codepages |
| 44 | * as a glyph fragment of wide-glyph letters |
| 45 | * + IBM Unicode conversion tables map it to U+200B (ZWSP) |
| 46 | * + IBM Egypt has proposed to encode the tail in Unicode among Arabic Presentation Forms |
| 47 | */ |
| 48 | |
| 49 | /* definitions for Arabic letter shaping ------------------------------------ */ |
| 50 | |
| 51 | #define IRRELEVANT 4 |
| 52 | #define LAMTYPE 16 |
| 53 | #define ALEFTYPE 32 |
| 54 | #define LINKR 1 |
| 55 | #define LINKL 2 |
| 56 | #define APRESENT 8 |
| 57 | #define SHADDA 64 |
| 58 | #define CSHADDA 128 |
| 59 | #define COMBINE (SHADDA+CSHADDA) |
| 60 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 61 | #define HAMZAFE_CHAR 0xfe80 |
| 62 | #define HAMZA06_CHAR 0x0621 |
| 63 | #define YEH_HAMZA_CHAR 0x0626 |
| 64 | #define YEH_HAMZAFE_CHAR 0xFE89 |
| 65 | #define LAMALEF_SPACE_SUB 0xFFFF |
| 66 | #define TASHKEEL_SPACE_SUB 0xFFFE |
| 67 | #define NEW_TAIL_CHAR 0xFE73 |
| 68 | #define OLD_TAIL_CHAR 0x200B |
| 69 | #define LAM_CHAR 0x0644 |
| 70 | #define SPACE_CHAR 0x0020 |
| 71 | #define SHADDA_CHAR 0xFE7C |
| 72 | #define TATWEEL_CHAR 0x0640 |
| 73 | #define SHADDA_TATWEEL_CHAR 0xFE7D |
| 74 | |
| 75 | #define SHAPE_MODE 0 |
| 76 | #define DESHAPE_MODE 1 |
| 77 | |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 78 | static UChar tailChar = OLD_TAIL_CHAR; |
| 79 | static uint32_t uShapeLamalefBegin = U_SHAPE_LAMALEF_BEGIN; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 80 | static uint32_t uShapeLamalefEnd = U_SHAPE_LAMALEF_END; |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 81 | static uint32_t uShapeTashkeelBegin = U_SHAPE_TASHKEEL_BEGIN; |
| 82 | static uint32_t uShapeTashkeelEnd = U_SHAPE_TASHKEEL_END; |
| 83 | static int spacesRelativeToTextBeginEnd = 0; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 84 | |
| 85 | static const uint8_t tailFamilyIsolatedFinal[] = { |
| 86 | /* FEB1 */ 1, |
| 87 | /* FEB2 */ 1, |
| 88 | /* FEB3 */ 0, |
| 89 | /* FEB4 */ 0, |
| 90 | /* FEB5 */ 1, |
| 91 | /* FEB6 */ 1, |
| 92 | /* FEB7 */ 0, |
| 93 | /* FEB8 */ 0, |
| 94 | /* FEB9 */ 1, |
| 95 | /* FEBA */ 1, |
| 96 | /* FEBB */ 0, |
| 97 | /* FEBC */ 0, |
| 98 | /* FEBD */ 1, |
| 99 | /* FEBE */ 1 |
| 100 | }; |
| 101 | |
| 102 | static const uint8_t tashkeelMedial[] = { |
| 103 | /* FE70 */ 0, |
| 104 | /* FE71 */ 1, |
| 105 | /* FE72 */ 0, |
| 106 | /* FE73 */ 0, |
| 107 | /* FE74 */ 0, |
| 108 | /* FE75 */ 0, |
| 109 | /* FE76 */ 0, |
| 110 | /* FE77 */ 1, |
| 111 | /* FE78 */ 0, |
| 112 | /* FE79 */ 1, |
| 113 | /* FE7A */ 0, |
| 114 | /* FE7B */ 1, |
| 115 | /* FE7C */ 0, |
| 116 | /* FE7D */ 1, |
| 117 | /* FE7E */ 0, |
| 118 | /* FE7F */ 1 |
| 119 | }; |
| 120 | |
| 121 | static const UChar yehHamzaToYeh[] = |
| 122 | { |
| 123 | /* isolated*/ 0xFEEF, |
| 124 | /* final */ 0xFEF0 |
| 125 | }; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 126 | |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 127 | static const uint8_t IrrelevantPos[] = { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 128 | 0x0, 0x2, 0x4, 0x6, |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 129 | 0x8, 0xA, 0xC, 0xE |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 132 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 133 | static const UChar convertLamAlef[] = |
| 134 | { |
| 135 | /*FEF5*/ 0x0622, |
| 136 | /*FEF6*/ 0x0622, |
| 137 | /*FEF7*/ 0x0623, |
| 138 | /*FEF8*/ 0x0623, |
| 139 | /*FEF9*/ 0x0625, |
| 140 | /*FEFA*/ 0x0625, |
| 141 | /*FEFB*/ 0x0627, |
| 142 | /*FEFC*/ 0x0627 |
| 143 | }; |
| 144 | |
| 145 | static const UChar araLink[178]= |
| 146 | { |
| 147 | 1 + 32 + 256 * 0x11,/*0x0622*/ |
| 148 | 1 + 32 + 256 * 0x13,/*0x0623*/ |
| 149 | 1 + 256 * 0x15,/*0x0624*/ |
| 150 | 1 + 32 + 256 * 0x17,/*0x0625*/ |
| 151 | 1 + 2 + 256 * 0x19,/*0x0626*/ |
| 152 | 1 + 32 + 256 * 0x1D,/*0x0627*/ |
| 153 | 1 + 2 + 256 * 0x1F,/*0x0628*/ |
| 154 | 1 + 256 * 0x23,/*0x0629*/ |
| 155 | 1 + 2 + 256 * 0x25,/*0x062A*/ |
| 156 | 1 + 2 + 256 * 0x29,/*0x062B*/ |
| 157 | 1 + 2 + 256 * 0x2D,/*0x062C*/ |
| 158 | 1 + 2 + 256 * 0x31,/*0x062D*/ |
| 159 | 1 + 2 + 256 * 0x35,/*0x062E*/ |
| 160 | 1 + 256 * 0x39,/*0x062F*/ |
| 161 | 1 + 256 * 0x3B,/*0x0630*/ |
| 162 | 1 + 256 * 0x3D,/*0x0631*/ |
| 163 | 1 + 256 * 0x3F,/*0x0632*/ |
| 164 | 1 + 2 + 256 * 0x41,/*0x0633*/ |
| 165 | 1 + 2 + 256 * 0x45,/*0x0634*/ |
| 166 | 1 + 2 + 256 * 0x49,/*0x0635*/ |
| 167 | 1 + 2 + 256 * 0x4D,/*0x0636*/ |
| 168 | 1 + 2 + 256 * 0x51,/*0x0637*/ |
| 169 | 1 + 2 + 256 * 0x55,/*0x0638*/ |
| 170 | 1 + 2 + 256 * 0x59,/*0x0639*/ |
| 171 | 1 + 2 + 256 * 0x5D,/*0x063A*/ |
| 172 | 0, 0, 0, 0, 0, /*0x063B-0x063F*/ |
| 173 | 1 + 2, /*0x0640*/ |
| 174 | 1 + 2 + 256 * 0x61,/*0x0641*/ |
| 175 | 1 + 2 + 256 * 0x65,/*0x0642*/ |
| 176 | 1 + 2 + 256 * 0x69,/*0x0643*/ |
| 177 | 1 + 2 + 16 + 256 * 0x6D,/*0x0644*/ |
| 178 | 1 + 2 + 256 * 0x71,/*0x0645*/ |
| 179 | 1 + 2 + 256 * 0x75,/*0x0646*/ |
| 180 | 1 + 2 + 256 * 0x79,/*0x0647*/ |
| 181 | 1 + 256 * 0x7D,/*0x0648*/ |
| 182 | 1 + 256 * 0x7F,/*0x0649*/ |
| 183 | 1 + 2 + 256 * 0x81,/*0x064A*/ |
| 184 | 4 + 256 * 1, /*0x064B*/ |
| 185 | 4 + 128 + 256 * 1, /*0x064C*/ |
| 186 | 4 + 128 + 256 * 1, /*0x064D*/ |
| 187 | 4 + 128 + 256 * 1, /*0x064E*/ |
| 188 | 4 + 128 + 256 * 1, /*0x064F*/ |
| 189 | 4 + 128 + 256 * 1, /*0x0650*/ |
| 190 | 4 + 64 + 256 * 3, /*0x0651*/ |
| 191 | 4 + 256 * 1, /*0x0652*/ |
| 192 | 4 + 256 * 7, /*0x0653*/ |
| 193 | 4 + 256 * 8, /*0x0654*/ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 194 | 4 + 256 * 8, /*0x0655*/ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 195 | 4 + 256 * 1, /*0x0656*/ |
| 196 | 0, 0, 0, 0, 0, /*0x0657-0x065B*/ |
| 197 | 1 + 256 * 0x85,/*0x065C*/ |
| 198 | 1 + 256 * 0x87,/*0x065D*/ |
| 199 | 1 + 256 * 0x89,/*0x065E*/ |
| 200 | 1 + 256 * 0x8B,/*0x065F*/ |
| 201 | 0, 0, 0, 0, 0, /*0x0660-0x0664*/ |
| 202 | 0, 0, 0, 0, 0, /*0x0665-0x0669*/ |
| 203 | 0, 0, 0, 0, 0, 0, /*0x066A-0x066F*/ |
| 204 | 4 + 256 * 6, /*0x0670*/ |
| 205 | 1 + 8 + 256 * 0x00,/*0x0671*/ |
| 206 | 1 + 32, /*0x0672*/ |
| 207 | 1 + 32, /*0x0673*/ |
| 208 | 0, /*0x0674*/ |
| 209 | 1 + 32, /*0x0675*/ |
| 210 | 1, 1, /*0x0676-0x0677*/ |
| 211 | 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x0678-0x067D*/ |
| 212 | 1+2+8+256 * 0x06, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x067E-0x0683*/ |
| 213 | 1+2, 1+2, 1+2+8+256 * 0x2A, 1+2, /*0x0684-0x0687*/ |
| 214 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x0688-0x0691*/ |
| 215 | 1, 1, 1, 1, 1, 1, 1+8+256 * 0x3A, 1, /*0x0692-0x0699*/ |
| 216 | 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/ |
| 217 | 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/ |
| 218 | 1+2, 1+2, 1+2, 1+2, 1+2, 1+2+8+256 * 0x3E, /*0x06A4-0x06AD*/ |
| 219 | 1+2, 1+2, 1+2, 1+2, /*0x06A4-0x06AD*/ |
| 220 | 1+2, 1+2+8+256 * 0x42, 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/ |
| 221 | 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/ |
| 222 | 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06B8-0x06BF*/ |
| 223 | 1+2, 1+2, /*0x06B8-0x06BF*/ |
| 224 | 1, /*0x06C0*/ |
| 225 | 1+2, /*0x06C1*/ |
| 226 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x06C2-0x06CB*/ |
| 227 | 1+2+8+256 * 0xAC, /*0x06CC*/ |
| 228 | 1, /*0x06CD*/ |
| 229 | 1+2, 1+2, 1+2, 1+2, /*0x06CE-0x06D1*/ |
| 230 | 1, 1 /*0x06D2-0x06D3*/ |
| 231 | }; |
| 232 | |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 233 | static const uint8_t presALink[] = { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 234 | /***********0*****1*****2*****3*****4*****5*****6*****7*****8*****9*****A*****B*****C*****D*****E*****F*/ |
| 235 | /*FB5*/ 0, 1, 0, 0, 0, 0, 0, 1, 2,1 + 2, 0, 0, 0, 0, 0, 0, |
| 236 | /*FB6*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 237 | /*FB7*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,1 + 2, 0, 0, |
| 238 | /*FB8*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, |
| 239 | /*FB9*/ 2,1 + 2, 0, 1, 2,1 + 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 240 | /*FBA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 241 | /*FBB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 242 | /*FBC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 243 | /*FBD*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 244 | /*FBE*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 245 | /*FBF*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,1 + 2, |
| 246 | /*FC0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 247 | /*FC1*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 248 | /*FC2*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 249 | /*FC3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 250 | /*FC4*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 251 | /*FC5*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, |
| 252 | /*FC6*/ 4, 4, 4 |
| 253 | }; |
| 254 | |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 255 | static const uint8_t presBLink[]= |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 256 | { |
| 257 | /***********0*****1*****2*****3*****4*****5*****6*****7*****8*****9*****A*****B*****C*****D*****E*****F*/ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 258 | /*FE7*/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, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 259 | /*FE8*/ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2,1 + 2, 0, 1, 0, |
| 260 | /*FE9*/ 1, 2,1 + 2, 0, 1, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2, |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 261 | /*FEA*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 0, 1, 0, 1, 0, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 262 | /*FEB*/ 1, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2, |
| 263 | /*FEC*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2, |
| 264 | /*FED*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2, |
| 265 | /*FEE*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 0, |
| 266 | /*FEF*/ 1, 0, 1, 2,1 + 2, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0 |
| 267 | }; |
| 268 | |
| 269 | static const UChar convertFBto06[] = |
| 270 | { |
| 271 | /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/ |
| 272 | /*FB5*/ 0x671, 0x671, 0, 0, 0, 0, 0x07E, 0x07E, 0x07E, 0x07E, 0, 0, 0, 0, 0, 0, |
| 273 | /*FB6*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 274 | /*FB7*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x686, 0x686, 0x686, 0x686, 0, 0, |
| 275 | /*FB8*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x698, 0x698, 0, 0, 0x6A9, 0x6A9, |
| 276 | /*FB9*/ 0x6A9, 0x6A9, 0x6AF, 0x6AF, 0x6AF, 0x6AF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 277 | /*FBA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 278 | /*FBB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 279 | /*FBC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 280 | /*FBD*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 281 | /*FBE*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 282 | /*FBF*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x6CC, 0x6CC, 0x6CC, 0x6CC |
| 283 | }; |
| 284 | |
| 285 | static const UChar convertFEto06[] = |
| 286 | { |
| 287 | /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/ |
| 288 | /*FE7*/ 0x64B, 0x64B, 0x64C, 0x64C, 0x64D, 0x64D, 0x64E, 0x64E, 0x64F, 0x64F, 0x650, 0x650, 0x651, 0x651, 0x652, 0x652, |
| 289 | /*FE8*/ 0x621, 0x622, 0x622, 0x623, 0x623, 0x624, 0x624, 0x625, 0x625, 0x626, 0x626, 0x626, 0x626, 0x627, 0x627, 0x628, |
| 290 | /*FE9*/ 0x628, 0x628, 0x628, 0x629, 0x629, 0x62A, 0x62A, 0x62A, 0x62A, 0x62B, 0x62B, 0x62B, 0x62B, 0x62C, 0x62C, 0x62C, |
| 291 | /*FEA*/ 0x62C, 0x62D, 0x62D, 0x62D, 0x62D, 0x62E, 0x62E, 0x62E, 0x62E, 0x62F, 0x62F, 0x630, 0x630, 0x631, 0x631, 0x632, |
| 292 | /*FEB*/ 0x632, 0x633, 0x633, 0x633, 0x633, 0x634, 0x634, 0x634, 0x634, 0x635, 0x635, 0x635, 0x635, 0x636, 0x636, 0x636, |
| 293 | /*FEC*/ 0x636, 0x637, 0x637, 0x637, 0x637, 0x638, 0x638, 0x638, 0x638, 0x639, 0x639, 0x639, 0x639, 0x63A, 0x63A, 0x63A, |
| 294 | /*FED*/ 0x63A, 0x641, 0x641, 0x641, 0x641, 0x642, 0x642, 0x642, 0x642, 0x643, 0x643, 0x643, 0x643, 0x644, 0x644, 0x644, |
| 295 | /*FEE*/ 0x644, 0x645, 0x645, 0x645, 0x645, 0x646, 0x646, 0x646, 0x646, 0x647, 0x647, 0x647, 0x647, 0x648, 0x648, 0x649, |
| 296 | /*FEF*/ 0x649, 0x64A, 0x64A, 0x64A, 0x64A, 0x65C, 0x65C, 0x65D, 0x65D, 0x65E, 0x65E, 0x65F, 0x65F |
| 297 | }; |
| 298 | |
Jean-Baptiste Queru | c69afce | 2009-07-20 15:02:39 -0700 | [diff] [blame] | 299 | static const uint8_t shapeTable[4][4][4]= |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 300 | { |
| 301 | { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,1} }, |
| 302 | { {0,0,2,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} }, |
| 303 | { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,3} }, |
| 304 | { {0,0,1,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} } |
| 305 | }; |
| 306 | |
| 307 | /* |
| 308 | * This function shapes European digits to Arabic-Indic digits |
| 309 | * in-place, writing over the input characters. |
| 310 | * Since we know that we are only looking for BMP code points, |
| 311 | * we can safely just work with code units (again, at least UTF-16). |
| 312 | */ |
| 313 | static void |
| 314 | _shapeToArabicDigitsWithContext(UChar *s, int32_t length, |
| 315 | UChar digitBase, |
| 316 | UBool isLogical, UBool lastStrongWasAL) { |
| 317 | const UBiDiProps *bdp; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 318 | int32_t i; |
| 319 | UChar c; |
| 320 | |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 321 | bdp=ubidi_getSingleton(); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 322 | digitBase-=0x30; |
| 323 | |
| 324 | /* the iteration direction depends on the type of input */ |
| 325 | if(isLogical) { |
| 326 | for(i=0; i<length; ++i) { |
| 327 | c=s[i]; |
| 328 | switch(ubidi_getClass(bdp, c)) { |
| 329 | case U_LEFT_TO_RIGHT: /* L */ |
| 330 | case U_RIGHT_TO_LEFT: /* R */ |
| 331 | lastStrongWasAL=FALSE; |
| 332 | break; |
| 333 | case U_RIGHT_TO_LEFT_ARABIC: /* AL */ |
| 334 | lastStrongWasAL=TRUE; |
| 335 | break; |
| 336 | case U_EUROPEAN_NUMBER: /* EN */ |
| 337 | if(lastStrongWasAL && (uint32_t)(c-0x30)<10) { |
| 338 | s[i]=(UChar)(digitBase+c); /* digitBase+(c-0x30) - digitBase was modified above */ |
| 339 | } |
| 340 | break; |
| 341 | default : |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | } else { |
| 346 | for(i=length; i>0; /* pre-decrement in the body */) { |
| 347 | c=s[--i]; |
| 348 | switch(ubidi_getClass(bdp, c)) { |
| 349 | case U_LEFT_TO_RIGHT: /* L */ |
| 350 | case U_RIGHT_TO_LEFT: /* R */ |
| 351 | lastStrongWasAL=FALSE; |
| 352 | break; |
| 353 | case U_RIGHT_TO_LEFT_ARABIC: /* AL */ |
| 354 | lastStrongWasAL=TRUE; |
| 355 | break; |
| 356 | case U_EUROPEAN_NUMBER: /* EN */ |
| 357 | if(lastStrongWasAL && (uint32_t)(c-0x30)<10) { |
| 358 | s[i]=(UChar)(digitBase+c); /* digitBase+(c-0x30) - digitBase was modified above */ |
| 359 | } |
| 360 | break; |
| 361 | default : |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | *Name : invertBuffer |
| 370 | *Function : This function inverts the buffer, it's used |
| 371 | * in case the user specifies the buffer to be |
| 372 | * U_SHAPE_TEXT_DIRECTION_LOGICAL |
| 373 | */ |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 374 | static void |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 375 | invertBuffer(UChar *buffer, int32_t size, uint32_t /*options*/, int32_t lowlimit, int32_t highlimit) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 376 | UChar temp; |
| 377 | int32_t i=0,j=0; |
| 378 | for(i=lowlimit,j=size-highlimit-1;i<j;i++,j--) { |
| 379 | temp = buffer[i]; |
| 380 | buffer[i] = buffer[j]; |
| 381 | buffer[j] = temp; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | *Name : changeLamAlef |
| 387 | *Function : Converts the Alef characters into an equivalent |
| 388 | * LamAlef location in the 0x06xx Range, this is an |
| 389 | * intermediate stage in the operation of the program |
| 390 | * later it'll be converted into the 0xFExx LamAlefs |
| 391 | * in the shaping function. |
| 392 | */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 393 | static inline UChar |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 394 | changeLamAlef(UChar ch) { |
| 395 | switch(ch) { |
| 396 | case 0x0622 : |
| 397 | return 0x065C; |
| 398 | case 0x0623 : |
| 399 | return 0x065D; |
| 400 | case 0x0625 : |
| 401 | return 0x065E; |
| 402 | case 0x0627 : |
| 403 | return 0x065F; |
| 404 | } |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | *Name : getLink |
| 410 | *Function : Resolves the link between the characters as |
| 411 | * Arabic characters have four forms : |
| 412 | * Isolated, Initial, Middle and Final Form |
| 413 | */ |
| 414 | static UChar |
| 415 | getLink(UChar ch) { |
| 416 | if(ch >= 0x0622 && ch <= 0x06D3) { |
| 417 | return(araLink[ch-0x0622]); |
| 418 | } else if(ch == 0x200D) { |
| 419 | return(3); |
| 420 | } else if(ch >= 0x206D && ch <= 0x206F) { |
| 421 | return(4); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 422 | }else if(ch >= 0xFB50 && ch <= 0xFC62) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 423 | return(presALink[ch-0xFB50]); |
| 424 | } else if(ch >= 0xFE70 && ch <= 0xFEFC) { |
| 425 | return(presBLink[ch-0xFE70]); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 426 | }else { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 427 | return(0); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | *Name : countSpaces |
| 433 | *Function : Counts the number of spaces |
| 434 | * at each end of the logical buffer |
| 435 | */ |
| 436 | static void |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 437 | countSpaces(UChar *dest, int32_t size, uint32_t /*options*/, int32_t *spacesCountl, int32_t *spacesCountr) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 438 | int32_t i = 0; |
| 439 | int32_t countl = 0,countr = 0; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 440 | while((dest[i] == SPACE_CHAR) && (countl < size)) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 441 | countl++; |
| 442 | i++; |
| 443 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 444 | if (countl < size) { /* the entire buffer is not all space */ |
| 445 | while(dest[size-1] == SPACE_CHAR) { |
| 446 | countr++; |
| 447 | size--; |
| 448 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 449 | } |
| 450 | *spacesCountl = countl; |
| 451 | *spacesCountr = countr; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | *Name : isTashkeelChar |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 456 | *Function : Returns 1 for Tashkeel characters in 06 range else return 0 |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 457 | */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 458 | static inline int32_t |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 459 | isTashkeelChar(UChar ch) { |
| 460 | return (int32_t)( ch>=0x064B && ch<= 0x0652 ); |
| 461 | } |
| 462 | |
| 463 | /* |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 464 | *Name : isTashkeelCharFE |
| 465 | *Function : Returns 1 for Tashkeel characters in FE range else return 0 |
| 466 | */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 467 | static inline int32_t |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 468 | isTashkeelCharFE(UChar ch) { |
| 469 | return (int32_t)( ch>=0xFE70 && ch<= 0xFE7F ); |
| 470 | } |
| 471 | |
| 472 | /* |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 473 | *Name : isAlefChar |
| 474 | *Function : Returns 1 for Alef characters else return 0 |
| 475 | */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 476 | static inline int32_t |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 477 | isAlefChar(UChar ch) { |
| 478 | return (int32_t)( (ch==0x0622)||(ch==0x0623)||(ch==0x0625)||(ch==0x0627) ); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | *Name : isLamAlefChar |
| 483 | *Function : Returns 1 for LamAlef characters else return 0 |
| 484 | */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 485 | static inline int32_t |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 486 | isLamAlefChar(UChar ch) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 487 | return (int32_t)((ch>=0xFEF5)&&(ch<=0xFEFC) ); |
| 488 | } |
| 489 | |
| 490 | /*BIDI |
| 491 | *Name : isTailChar |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 492 | *Function : returns 1 if the character matches one of the tail characters (0xfe73 or 0x200b) otherwise returns 0 |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 493 | */ |
| 494 | |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 495 | static inline int32_t |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 496 | isTailChar(UChar ch) { |
| 497 | if(ch == OLD_TAIL_CHAR || ch == NEW_TAIL_CHAR){ |
| 498 | return 1; |
| 499 | }else{ |
| 500 | return 0; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /*BIDI |
| 505 | *Name : isSeenTailFamilyChar |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 506 | *Function : returns 1 if the character is a seen family isolated character |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 507 | * in the FE range otherwise returns 0 |
| 508 | */ |
| 509 | |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 510 | static inline int32_t |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 511 | isSeenTailFamilyChar(UChar ch) { |
| 512 | if(ch >= 0xfeb1 && ch < 0xfebf){ |
| 513 | return tailFamilyIsolatedFinal [ch - 0xFEB1]; |
| 514 | }else{ |
| 515 | return 0; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /* Name : isSeenFamilyChar |
| 520 | * Function : returns 1 if the character is a seen family character in the Unicode |
| 521 | * 06 range otherwise returns 0 |
| 522 | */ |
| 523 | |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 524 | static inline int32_t |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 525 | isSeenFamilyChar(UChar ch){ |
| 526 | if(ch >= 0x633 && ch <= 0x636){ |
| 527 | return 1; |
| 528 | }else { |
| 529 | return 0; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | /*Start of BIDI*/ |
| 534 | /* |
| 535 | *Name : isAlefMaksouraChar |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 536 | *Function : returns 1 if the character is a Alef Maksoura Final or isolated |
| 537 | * otherwise returns 0 |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 538 | */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 539 | static inline int32_t |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 540 | isAlefMaksouraChar(UChar ch) { |
| 541 | return (int32_t)( (ch == 0xFEEF) || ( ch == 0xFEF0) || (ch == 0x0649)); |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 542 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 543 | |
| 544 | /* |
| 545 | * Name : isYehHamzaChar |
| 546 | * Function : returns 1 if the character is a yehHamza isolated or yehhamza |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 547 | * final is found otherwise returns 0 |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 548 | */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 549 | static inline int32_t |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 550 | isYehHamzaChar(UChar ch) { |
| 551 | if((ch==0xFE89)||(ch==0xFE8A)){ |
| 552 | return 1; |
| 553 | }else{ |
| 554 | return 0; |
| 555 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 556 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 557 | |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 558 | /* |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 559 | * Name: isTashkeelOnTatweelChar |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 560 | * Function: Checks if the Tashkeel Character is on Tatweel or not,if the |
| 561 | * Tashkeel on tatweel (FE range), it returns 1 else if the |
| 562 | * Tashkeel with shadda on tatweel (FC range)return 2 otherwise |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 563 | * returns 0 |
| 564 | */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 565 | static inline int32_t |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 566 | isTashkeelOnTatweelChar(UChar ch){ |
| 567 | if(ch >= 0xfe70 && ch <= 0xfe7f && ch != NEW_TAIL_CHAR && ch != 0xFE75 && ch != SHADDA_TATWEEL_CHAR) |
| 568 | { |
| 569 | return tashkeelMedial [ch - 0xFE70]; |
| 570 | }else if( (ch >= 0xfcf2 && ch <= 0xfcf4) || (ch == SHADDA_TATWEEL_CHAR)) { |
| 571 | return 2; |
| 572 | }else{ |
| 573 | return 0; |
| 574 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 578 | * Name: isIsolatedTashkeelChar |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 579 | * Function: Checks if the Tashkeel Character is in the isolated form |
| 580 | * (i.e. Unicode FE range) returns 1 else if the Tashkeel |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 581 | * with shadda is in the isolated form (i.e. Unicode FC range) |
| 582 | * returns 2 otherwise returns 0 |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 583 | */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 584 | static inline int32_t |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 585 | isIsolatedTashkeelChar(UChar ch){ |
| 586 | if(ch >= 0xfe70 && ch <= 0xfe7f && ch != NEW_TAIL_CHAR && ch != 0xFE75){ |
| 587 | return (1 - tashkeelMedial [ch - 0xFE70]); |
| 588 | }else if(ch >= 0xfc5e && ch <= 0xfc63){ |
| 589 | return 1; |
| 590 | }else{ |
| 591 | return 0; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | /* |
| 599 | *Name : calculateSize |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 600 | *Function : This function calculates the destSize to be used in preflighting |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 601 | * when the destSize is equal to 0 |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 602 | * It is used also to calculate the new destsize in case the |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 603 | * destination buffer will be resized. |
| 604 | */ |
| 605 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 606 | static int32_t |
| 607 | calculateSize(const UChar *source, int32_t sourceLength, |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 608 | int32_t destSize,uint32_t options) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 609 | int32_t i = 0; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 610 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 611 | int lamAlefOption = 0; |
| 612 | int tashkeelOption = 0; |
| 613 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 614 | destSize = sourceLength; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 615 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 616 | if (((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE || |
| 617 | ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED )) && |
| 618 | ((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE )){ |
| 619 | lamAlefOption = 1; |
| 620 | } |
| 621 | if((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE && |
| 622 | ((options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_RESIZE ) ){ |
| 623 | tashkeelOption = 1; |
| 624 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 625 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 626 | if(lamAlefOption || tashkeelOption){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 627 | if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_VISUAL_LTR) { |
| 628 | for(i=0;i<sourceLength;i++) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 629 | if( ((isAlefChar(source[i]))&& (i<(sourceLength-1)) &&(source[i+1] == LAM_CHAR)) || (isTashkeelCharFE(source[i])) ) { |
| 630 | destSize--; |
| 631 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 632 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 633 | }else if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL) { |
| 634 | for(i=0;i<sourceLength;i++) { |
| 635 | if( ( (source[i] == LAM_CHAR) && (i<(sourceLength-1)) && (isAlefChar(source[i+1]))) || (isTashkeelCharFE(source[i])) ) { |
| 636 | destSize--; |
| 637 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 641 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 642 | if ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_UNSHAPE){ |
| 643 | if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE){ |
| 644 | for(i=0;i<sourceLength;i++) { |
| 645 | if(isLamAlefChar(source[i])) |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 646 | destSize++; |
| 647 | } |
| 648 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | return destSize; |
| 652 | } |
| 653 | |
| 654 | /* |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 655 | *Name : handleTashkeelWithTatweel |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 656 | *Function : Replaces Tashkeel as following: |
| 657 | * Case 1 :if the Tashkeel on tatweel, replace it with Tatweel. |
| 658 | * Case 2 :if the Tashkeel aggregated with Shadda on Tatweel, replace |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 659 | * it with Shadda on Tatweel. |
| 660 | * Case 3: if the Tashkeel is isolated replace it with Space. |
| 661 | * |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 662 | */ |
| 663 | static int32_t |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 664 | handleTashkeelWithTatweel(UChar *dest, int32_t sourceLength, |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 665 | int32_t /*destSize*/, uint32_t /*options*/, |
| 666 | UErrorCode * /*pErrorCode*/) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 667 | int i; |
| 668 | for(i = 0; i < sourceLength; i++){ |
| 669 | if((isTashkeelOnTatweelChar(dest[i]) == 1)){ |
| 670 | dest[i] = TATWEEL_CHAR; |
| 671 | }else if((isTashkeelOnTatweelChar(dest[i]) == 2)){ |
| 672 | dest[i] = SHADDA_TATWEEL_CHAR; |
| 673 | }else if(isIsolatedTashkeelChar(dest[i]) && dest[i] != SHADDA_CHAR){ |
| 674 | dest[i] = SPACE_CHAR; |
| 675 | } |
| 676 | } |
| 677 | return sourceLength; |
| 678 | } |
| 679 | |
| 680 | |
| 681 | |
| 682 | /* |
| 683 | *Name : handleGeneratedSpaces |
| 684 | *Function : The shapeUnicode function converts Lam + Alef into LamAlef + space, |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 685 | * and Tashkeel to space. |
| 686 | * handleGeneratedSpaces function puts these generated spaces |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 687 | * according to the options the user specifies. LamAlef and Tashkeel |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 688 | * spaces can be replaced at begin, at end, at near or decrease the |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 689 | * buffer size. |
| 690 | * |
| 691 | * There is also Auto option for LamAlef and tashkeel, which will put |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 692 | * the spaces at end of the buffer (or end of text if the user used |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 693 | * the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END). |
| 694 | * |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 695 | * If the text type was visual_LTR and the option |
| 696 | * U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END was selected the END |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 697 | * option will place the space at the beginning of the buffer and |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 698 | * BEGIN will place the space at the end of the buffer. |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 699 | */ |
| 700 | |
| 701 | static int32_t |
| 702 | handleGeneratedSpaces(UChar *dest, int32_t sourceLength, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 703 | int32_t destSize, |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 704 | /* BEGIN android-changed */ |
Doug Felt | 2bae19d | 2010-06-07 16:28:19 -0700 | [diff] [blame] | 705 | uint64_t options, |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 706 | /* END android-changed */ |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 707 | UErrorCode *pErrorCode ) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 708 | |
| 709 | int32_t i = 0, j = 0; |
| 710 | int32_t count = 0; |
| 711 | UChar *tempbuffer=NULL; |
| 712 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 713 | int lamAlefOption = 0; |
| 714 | int tashkeelOption = 0; |
| 715 | int shapingMode = SHAPE_MODE; |
| 716 | |
| 717 | if (shapingMode == 0){ |
| 718 | if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE ){ |
| 719 | lamAlefOption = 1; |
| 720 | } |
| 721 | if ( (options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_RESIZE ){ |
| 722 | tashkeelOption = 1; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | if (lamAlefOption || tashkeelOption){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 727 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); |
| 728 | /* Test for NULL */ |
| 729 | if(tempbuffer == NULL) { |
| 730 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); |
| 735 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 736 | i = j = 0; count = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 737 | while(i < sourceLength) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 738 | if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) || |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 739 | (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 740 | j--; |
| 741 | count++; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 742 | } else { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 743 | tempbuffer[j] = dest[i]; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 744 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 745 | i++; |
| 746 | j++; |
| 747 | } |
| 748 | |
| 749 | while(count >= 0) { |
| 750 | tempbuffer[i] = 0x0000; |
| 751 | i--; |
| 752 | count--; |
| 753 | } |
| 754 | |
| 755 | uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR); |
| 756 | destSize = u_strlen(dest); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 757 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 758 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 759 | lamAlefOption = 0; |
| 760 | |
| 761 | if (shapingMode == 0){ |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 762 | /* BEGIN android-changed */ |
Doug Felt | e0266fb | 2010-06-08 14:11:11 -0700 | [diff] [blame] | 763 | if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_NEAR && |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 764 | (options&U_SHAPE_X_LAMALEF_SUB_ALTERNATE) == 0) { /* if set, leave LAMALEF_SPACE_SUB in the output */ |
| 765 | /* END android-changed */ |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 766 | lamAlefOption = 1; |
| 767 | } |
| 768 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 769 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 770 | if (lamAlefOption){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 771 | /* Lam+Alef is already shaped into LamAlef + FFFF */ |
| 772 | i = 0; |
| 773 | while(i < sourceLength) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 774 | if(lamAlefOption&&dest[i] == LAMALEF_SPACE_SUB){ |
| 775 | dest[i] = SPACE_CHAR; |
| 776 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 777 | i++; |
| 778 | } |
| 779 | destSize = sourceLength; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 780 | } |
| 781 | lamAlefOption = 0; |
| 782 | tashkeelOption = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 783 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 784 | if (shapingMode == 0) { |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 785 | if ( ((options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefBegin) || |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 786 | (((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO ) |
| 787 | && (spacesRelativeToTextBeginEnd==1)) ) { |
| 788 | lamAlefOption = 1; |
| 789 | } |
| 790 | if ( (options&U_SHAPE_TASHKEEL_MASK) == uShapeTashkeelBegin ) { |
| 791 | tashkeelOption = 1; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | if(lamAlefOption || tashkeelOption){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 796 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 797 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 798 | /* Test for NULL */ |
| 799 | if(tempbuffer == NULL) { |
| 800 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 805 | i = j = sourceLength; count = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 806 | while(i >= 0) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 807 | if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) || |
| 808 | (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 809 | j++; |
| 810 | count++; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 811 | }else { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 812 | tempbuffer[j] = dest[i]; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 813 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 814 | i--; |
| 815 | j--; |
| 816 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 817 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 818 | for(i=0 ;i < count; i++){ |
| 819 | tempbuffer[i] = SPACE_CHAR; |
| 820 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 821 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 822 | uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR); |
| 823 | destSize = sourceLength; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 824 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 825 | |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 826 | |
| 827 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 828 | lamAlefOption = 0; |
| 829 | tashkeelOption = 0; |
| 830 | |
| 831 | if (shapingMode == 0) { |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 832 | if ( ((options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefEnd) || |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 833 | (((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO ) |
| 834 | && (spacesRelativeToTextBeginEnd==0)) ) { |
| 835 | lamAlefOption = 1; |
| 836 | } |
| 837 | if ( (options&U_SHAPE_TASHKEEL_MASK) == uShapeTashkeelEnd ){ |
| 838 | tashkeelOption = 1; |
| 839 | } |
| 840 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 841 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 842 | if(lamAlefOption || tashkeelOption){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 843 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 844 | /* Test for NULL */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 845 | if(tempbuffer == NULL) { |
| 846 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); |
| 851 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 852 | i = j = 0; count = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 853 | while(i < sourceLength) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 854 | if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) || |
| 855 | (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 856 | j--; |
| 857 | count++; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 858 | }else { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 859 | tempbuffer[j] = dest[i]; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 860 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 861 | i++; |
| 862 | j++; |
| 863 | } |
| 864 | |
| 865 | while(count >= 0) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 866 | tempbuffer[i] = SPACE_CHAR; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 867 | i--; |
| 868 | count--; |
| 869 | } |
| 870 | |
| 871 | uprv_memcpy(dest,tempbuffer, sourceLength*U_SIZEOF_UCHAR); |
| 872 | destSize = sourceLength; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 875 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 876 | if(tempbuffer){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 877 | uprv_free(tempbuffer); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 878 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 879 | |
| 880 | return destSize; |
| 881 | } |
| 882 | |
| 883 | /* |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 884 | *Name :expandCompositCharAtBegin |
| 885 | *Function :Expands the LamAlef character to Lam and Alef consuming the required |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 886 | * space from beginning of the buffer. If the text type was visual_LTR |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 887 | * and the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END was selected |
| 888 | * the spaces will be located at end of buffer. |
| 889 | * If there are no spaces to expand the LamAlef, an error |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 890 | * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 891 | */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 892 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 893 | static int32_t |
| 894 | expandCompositCharAtBegin(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 895 | int32_t i = 0,j = 0; |
| 896 | int32_t countl = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 897 | UChar *tempbuffer=NULL; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 898 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 899 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 900 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 901 | /* Test for NULL */ |
| 902 | if(tempbuffer == NULL) { |
| 903 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 904 | return 0; |
| 905 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 906 | |
| 907 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); |
| 908 | |
| 909 | i = 0; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 910 | while(dest[i] == SPACE_CHAR) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 911 | countl++; |
| 912 | i++; |
| 913 | } |
| 914 | |
| 915 | i = j = sourceLength-1; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 916 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 917 | while(i >= 0 && j >= 0) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 918 | if( countl>0 && isLamAlefChar(dest[i])) { |
| 919 | tempbuffer[j] = LAM_CHAR; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 920 | /* to ensure the array index is within the range */ |
| 921 | U_ASSERT(dest[i] >= 0xFEF5u |
| 922 | && dest[i]-0xFEF5u < sizeof(convertLamAlef)/sizeof(convertLamAlef[0])); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 923 | tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ]; |
| 924 | j--; |
| 925 | countl--; |
| 926 | }else { |
| 927 | if( countl == 0 && isLamAlefChar(dest[i]) ) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 928 | *pErrorCode=U_NO_SPACE_AVAILABLE; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 929 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 930 | tempbuffer[j] = dest[i]; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 931 | } |
| 932 | i--; |
| 933 | j--; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 934 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 935 | uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR); |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 936 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 937 | uprv_free(tempbuffer); |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 938 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 939 | destSize = sourceLength; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 940 | return destSize; |
| 941 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 942 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 943 | /* |
| 944 | *Name : expandCompositCharAtEnd |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 945 | *Function : Expands the LamAlef character to Lam and Alef consuming the |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 946 | * required space from end of the buffer. If the text type was |
| 947 | * Visual LTR and the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 948 | * was used, the spaces will be consumed from begin of buffer. If |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 949 | * there are no spaces to expand the LamAlef, an error |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 950 | * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 951 | */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 952 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 953 | static int32_t |
| 954 | expandCompositCharAtEnd(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode) { |
| 955 | int32_t i = 0,j = 0; |
| 956 | |
| 957 | int32_t countr = 0; |
| 958 | int32_t inpsize = sourceLength; |
| 959 | |
| 960 | UChar *tempbuffer=NULL; |
| 961 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); |
| 962 | |
| 963 | /* Test for NULL */ |
| 964 | if(tempbuffer == NULL) { |
| 965 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 966 | return 0; |
| 967 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 968 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 969 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 970 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 971 | while(dest[inpsize-1] == SPACE_CHAR) { |
| 972 | countr++; |
| 973 | inpsize--; |
| 974 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 975 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 976 | i = sourceLength - countr - 1; |
| 977 | j = sourceLength - 1; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 978 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 979 | while(i >= 0 && j >= 0) { |
| 980 | if( countr>0 && isLamAlefChar(dest[i]) ) { |
| 981 | tempbuffer[j] = LAM_CHAR; |
| 982 | tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ]; |
| 983 | j--; |
| 984 | countr--; |
| 985 | }else { |
| 986 | if ((countr == 0) && isLamAlefChar(dest[i]) ) { |
| 987 | *pErrorCode=U_NO_SPACE_AVAILABLE; |
| 988 | } |
| 989 | tempbuffer[j] = dest[i]; |
| 990 | } |
| 991 | i--; |
| 992 | j--; |
| 993 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 994 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 995 | if(countr > 0) { |
| 996 | uprv_memmove(tempbuffer, tempbuffer+countr, sourceLength*U_SIZEOF_UCHAR); |
| 997 | if(u_strlen(tempbuffer) < sourceLength) { |
| 998 | for(i=sourceLength-1;i>=sourceLength-countr;i--) { |
| 999 | tempbuffer[i] = SPACE_CHAR; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR); |
| 1004 | |
| 1005 | uprv_free(tempbuffer); |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1006 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1007 | destSize = sourceLength; |
| 1008 | return destSize; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | *Name : expandCompositCharAtNear |
| 1013 | *Function : Expands the LamAlef character into Lam + Alef, YehHamza character |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1014 | * into Yeh + Hamza, SeenFamily character into SeenFamily character |
| 1015 | * + Tail, while consuming the space next to the character. |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1016 | * If there are no spaces next to the character, an error |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1017 | * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1018 | */ |
| 1019 | |
| 1020 | static int32_t |
| 1021 | expandCompositCharAtNear(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode, |
| 1022 | int yehHamzaOption, int seenTailOption, int lamAlefOption) { |
| 1023 | int32_t i = 0; |
| 1024 | |
| 1025 | |
| 1026 | UChar lamalefChar, yehhamzaChar; |
| 1027 | |
| 1028 | for(i = 0 ;i<=sourceLength-1;i++) { |
| 1029 | if (seenTailOption && isSeenTailFamilyChar(dest[i])) { |
| 1030 | if ((i>0) && (dest[i-1] == SPACE_CHAR) ) { |
| 1031 | dest[i-1] = tailChar; |
| 1032 | }else { |
| 1033 | *pErrorCode=U_NO_SPACE_AVAILABLE; |
| 1034 | } |
| 1035 | }else if(yehHamzaOption && (isYehHamzaChar(dest[i])) ) { |
| 1036 | if ((i>0) && (dest[i-1] == SPACE_CHAR) ) { |
| 1037 | yehhamzaChar = dest[i]; |
| 1038 | dest[i] = yehHamzaToYeh[yehhamzaChar - YEH_HAMZAFE_CHAR]; |
| 1039 | dest[i-1] = HAMZAFE_CHAR; |
| 1040 | }else { |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1041 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1042 | *pErrorCode=U_NO_SPACE_AVAILABLE; |
| 1043 | } |
| 1044 | }else if(lamAlefOption && isLamAlefChar(dest[i+1])) { |
| 1045 | if(dest[i] == SPACE_CHAR){ |
| 1046 | lamalefChar = dest[i+1]; |
| 1047 | dest[i+1] = LAM_CHAR; |
| 1048 | dest[i] = convertLamAlef[ lamalefChar - 0xFEF5 ]; |
| 1049 | }else { |
| 1050 | *pErrorCode=U_NO_SPACE_AVAILABLE; |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | destSize = sourceLength; |
| 1055 | return destSize; |
| 1056 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1057 | /* |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1058 | * Name : expandCompositChar |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1059 | * Function : LamAlef, need special handling, since it expands from one |
| 1060 | * character into two characters while shaping or deshaping. |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1061 | * In order to expand it, near or far spaces according to the |
| 1062 | * options user specifies. Also buffer size can be increased. |
| 1063 | * |
| 1064 | * For SeenFamily characters and YehHamza only the near option is |
| 1065 | * supported, while for LamAlef we can take spaces from begin, end, |
| 1066 | * near or even increase the buffer size. |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1067 | * There is also the Auto option for LamAlef only, which will first |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1068 | * search for a space at end, begin then near, respectively. |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1069 | * If there are no spaces to expand these characters, an error will be set to |
| 1070 | * U_NO_SPACE_AVAILABLE as defined in utypes.h |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1071 | */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1072 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1073 | static int32_t |
| 1074 | expandCompositChar(UChar *dest, int32_t sourceLength, |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1075 | int32_t destSize,uint32_t options, |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1076 | UErrorCode *pErrorCode, int shapingMode) { |
| 1077 | |
| 1078 | int32_t i = 0,j = 0; |
| 1079 | |
| 1080 | UChar *tempbuffer=NULL; |
| 1081 | int yehHamzaOption = 0; |
| 1082 | int seenTailOption = 0; |
| 1083 | int lamAlefOption = 0; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1084 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1085 | if (shapingMode == 1){ |
| 1086 | if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO){ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1087 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1088 | if(spacesRelativeToTextBeginEnd == 0) { |
| 1089 | destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode); |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1090 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1091 | if(*pErrorCode == U_NO_SPACE_AVAILABLE) { |
| 1092 | *pErrorCode = U_ZERO_ERROR; |
| 1093 | destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode); |
| 1094 | } |
| 1095 | }else { |
| 1096 | destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode); |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1097 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1098 | if(*pErrorCode == U_NO_SPACE_AVAILABLE) { |
| 1099 | *pErrorCode = U_ZERO_ERROR; |
| 1100 | destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode); |
| 1101 | } |
| 1102 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1103 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1104 | if(*pErrorCode == U_NO_SPACE_AVAILABLE) { |
| 1105 | *pErrorCode = U_ZERO_ERROR; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1106 | destSize = expandCompositCharAtNear(dest, sourceLength, destSize, pErrorCode, yehHamzaOption, |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1107 | seenTailOption, 1); |
| 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | if (shapingMode == 1){ |
| 1113 | if ( (options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefEnd){ |
| 1114 | destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | if (shapingMode == 1){ |
| 1119 | if ( (options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefBegin){ |
| 1120 | destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode); |
| 1121 | } |
| 1122 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1123 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1124 | if (shapingMode == 0){ |
| 1125 | if ((options&U_SHAPE_YEHHAMZA_MASK) == U_SHAPE_YEHHAMZA_TWOCELL_NEAR){ |
| 1126 | yehHamzaOption = 1; |
| 1127 | } |
| 1128 | if ((options&U_SHAPE_SEEN_MASK) == U_SHAPE_SEEN_TWOCELL_NEAR){ |
| 1129 | seenTailOption = 1; |
| 1130 | } |
| 1131 | } |
| 1132 | if (shapingMode == 1) { |
| 1133 | if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_NEAR) { |
| 1134 | lamAlefOption = 1; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | if (yehHamzaOption || seenTailOption || lamAlefOption){ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1140 | destSize = expandCompositCharAtNear(dest, sourceLength, destSize, pErrorCode, yehHamzaOption, |
| 1141 | seenTailOption,lamAlefOption); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1142 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1143 | |
| 1144 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1145 | if (shapingMode == 1){ |
| 1146 | if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE){ |
| 1147 | destSize = calculateSize(dest,sourceLength,destSize,options); |
| 1148 | tempbuffer = (UChar *)uprv_malloc((destSize+1)*U_SIZEOF_UCHAR); |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1149 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1150 | /* Test for NULL */ |
| 1151 | if(tempbuffer == NULL) { |
| 1152 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 1153 | return 0; |
| 1154 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1155 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1156 | uprv_memset(tempbuffer, 0, (destSize+1)*U_SIZEOF_UCHAR); |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1157 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1158 | i = j = 0; |
| 1159 | while(i < destSize && j < destSize) { |
| 1160 | if(isLamAlefChar(dest[i]) ) { |
| 1161 | tempbuffer[j] = convertLamAlef[ dest[i] - 0xFEF5 ]; |
| 1162 | tempbuffer[j+1] = LAM_CHAR; |
| 1163 | j++; |
| 1164 | }else { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1165 | tempbuffer[j] = dest[i]; |
| 1166 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1167 | i++; |
| 1168 | j++; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1169 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1170 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1171 | uprv_memcpy(dest, tempbuffer, destSize*U_SIZEOF_UCHAR); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1172 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1173 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1174 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1175 | if(tempbuffer) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1176 | uprv_free(tempbuffer); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1177 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1178 | return destSize; |
| 1179 | } |
| 1180 | |
| 1181 | /* |
| 1182 | *Name : shapeUnicode |
| 1183 | *Function : Converts an Arabic Unicode buffer in 06xx Range into a shaped |
| 1184 | * arabic Unicode buffer in FExx Range |
| 1185 | */ |
| 1186 | static int32_t |
| 1187 | shapeUnicode(UChar *dest, int32_t sourceLength, |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1188 | int32_t destSize,uint32_t options, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1189 | UErrorCode *pErrorCode, |
| 1190 | int tashkeelFlag) { |
| 1191 | |
| 1192 | int32_t i, iend; |
| 1193 | int32_t step; |
| 1194 | int32_t lastPos,Nx, Nw; |
| 1195 | unsigned int Shape; |
| 1196 | int32_t lamalef_found = 0; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1197 | int32_t seenfamFound = 0, yehhamzaFound =0, tashkeelFound = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1198 | UChar prevLink = 0, lastLink = 0, currLink, nextLink = 0; |
| 1199 | UChar wLamalef; |
| 1200 | |
| 1201 | /* |
| 1202 | * Converts the input buffer from FExx Range into 06xx Range |
| 1203 | * to make sure that all characters are in the 06xx range |
| 1204 | * even the lamalef is converted to the special region in |
| 1205 | * the 06xx range |
| 1206 | */ |
| 1207 | if ((options & U_SHAPE_PRESERVE_PRESENTATION_MASK) == U_SHAPE_PRESERVE_PRESENTATION_NOOP) { |
| 1208 | for (i = 0; i < sourceLength; i++) { |
| 1209 | UChar inputChar = dest[i]; |
| 1210 | if ( (inputChar >= 0xFB50) && (inputChar <= 0xFBFF)) { |
| 1211 | UChar c = convertFBto06 [ (inputChar - 0xFB50) ]; |
| 1212 | if (c != 0) |
| 1213 | dest[i] = c; |
| 1214 | } else if ( (inputChar >= 0xFE70) && (inputChar <= 0xFEFC)) { |
| 1215 | dest[i] = convertFEto06 [ (inputChar - 0xFE70) ] ; |
| 1216 | } else { |
| 1217 | dest[i] = inputChar ; |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1222 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1223 | /* sets the index to the end of the buffer, together with the step point to -1 */ |
| 1224 | i = sourceLength - 1; |
| 1225 | iend = -1; |
| 1226 | step = -1; |
| 1227 | |
| 1228 | /* |
| 1229 | * This function resolves the link between the characters . |
| 1230 | * Arabic characters have four forms : |
| 1231 | * Isolated Form, Initial Form, Middle Form and Final Form |
| 1232 | */ |
| 1233 | currLink = getLink(dest[i]); |
| 1234 | |
| 1235 | lastPos = i; |
| 1236 | Nx = -2, Nw = 0; |
| 1237 | |
| 1238 | while (i != iend) { |
| 1239 | /* If high byte of currLink > 0 then more than one shape */ |
| 1240 | if ((currLink & 0xFF00) > 0 || (getLink(dest[i]) & IRRELEVANT) != 0) { |
| 1241 | Nw = i + step; |
| 1242 | while (Nx < 0) { /* we need to know about next char */ |
| 1243 | if(Nw == iend) { |
| 1244 | nextLink = 0; |
| 1245 | Nx = 3000; |
| 1246 | } else { |
| 1247 | nextLink = getLink(dest[Nw]); |
| 1248 | if((nextLink & IRRELEVANT) == 0) { |
| 1249 | Nx = Nw; |
| 1250 | } else { |
| 1251 | Nw = Nw + step; |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | if ( ((currLink & ALEFTYPE) > 0) && ((lastLink & LAMTYPE) > 0) ) { |
| 1257 | lamalef_found = 1; |
| 1258 | wLamalef = changeLamAlef(dest[i]); /*get from 0x065C-0x065f */ |
| 1259 | if ( wLamalef != 0) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1260 | dest[i] = LAMALEF_SPACE_SUB; /* The default case is to drop the Alef and replace */ |
| 1261 | dest[lastPos] =wLamalef; /* it by LAMALEF_SPACE_SUB which is the last character in the */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1262 | i=lastPos; /* unicode private use area, this is done to make */ |
| 1263 | } /* sure that removeLamAlefSpaces() handles only the */ |
| 1264 | lastLink = prevLink; /* spaces generated during lamalef generation. */ |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1265 | currLink = getLink(wLamalef); /* LAMALEF_SPACE_SUB is added here and is replaced by spaces */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1266 | } /* in removeLamAlefSpaces() */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1267 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1268 | if ((i > 0) && (dest[i-1] == SPACE_CHAR)){ |
| 1269 | if ( isSeenFamilyChar(dest[i])){ |
| 1270 | seenfamFound = 1; |
| 1271 | } else if (dest[i] == YEH_HAMZA_CHAR) { |
| 1272 | yehhamzaFound = 1; |
| 1273 | } |
| 1274 | } |
| 1275 | else if(i==0){ |
| 1276 | if ( isSeenFamilyChar(dest[i])){ |
| 1277 | seenfamFound = 1; |
| 1278 | } else if (dest[i] == YEH_HAMZA_CHAR) { |
| 1279 | yehhamzaFound = 1; |
| 1280 | } |
| 1281 | } |
| 1282 | |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1283 | /* |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1284 | * get the proper shape according to link ability of neighbors |
| 1285 | * and of character; depends on the order of the shapes |
| 1286 | * (isolated, initial, middle, final) in the compatibility area |
| 1287 | */ |
| 1288 | Shape = shapeTable[nextLink & (LINKR + LINKL)] |
| 1289 | [lastLink & (LINKR + LINKL)] |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1290 | [currLink & (LINKR + LINKL)]; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1291 | |
| 1292 | if ((currLink & (LINKR+LINKL)) == 1) { |
| 1293 | Shape &= 1; |
| 1294 | } else if(isTashkeelChar(dest[i])) { |
| 1295 | if( (lastLink & LINKL) && (nextLink & LINKR) && (tashkeelFlag == 1) && |
| 1296 | dest[i] != 0x064C && dest[i] != 0x064D ) |
| 1297 | { |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1298 | Shape = 1; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1299 | if( (nextLink&ALEFTYPE) == ALEFTYPE && (lastLink&LAMTYPE) == LAMTYPE ) { |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1300 | Shape = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | else { |
| 1304 | Shape = 0; |
| 1305 | } |
| 1306 | } |
| 1307 | if ((dest[i] ^ 0x0600) < 0x100) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1308 | if ( isTashkeelChar(dest[i]) ){ |
| 1309 | if (tashkeelFlag == 2){ |
| 1310 | dest[i] = TASHKEEL_SPACE_SUB; |
| 1311 | tashkeelFound = 1; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1312 | } else { |
| 1313 | /* to ensure the array index is within the range */ |
| 1314 | U_ASSERT(dest[i] >= 0x064Bu |
| 1315 | && dest[i]-0x064Bu < sizeof(IrrelevantPos)/sizeof(IrrelevantPos[0])); |
| 1316 | dest[i] = 0xFE70 + IrrelevantPos[(dest[i] - 0x064B)] + Shape; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1317 | } |
| 1318 | }else if ((currLink & APRESENT) > 0) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1319 | dest[i] = (UChar)(0xFB50 + (currLink >> 8) + Shape); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1320 | }else if ((currLink >> 8) > 0 && (currLink & IRRELEVANT) == 0) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1321 | dest[i] = (UChar)(0xFE70 + (currLink >> 8) + Shape); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1322 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | /* move one notch forward */ |
| 1327 | if ((currLink & IRRELEVANT) == 0) { |
| 1328 | prevLink = lastLink; |
| 1329 | lastLink = currLink; |
| 1330 | lastPos = i; |
| 1331 | } |
| 1332 | |
| 1333 | i = i + step; |
| 1334 | if (i == Nx) { |
| 1335 | currLink = nextLink; |
| 1336 | Nx = -2; |
| 1337 | } else if(i != iend) { |
| 1338 | currLink = getLink(dest[i]); |
| 1339 | } |
| 1340 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1341 | destSize = sourceLength; |
| 1342 | if ( (lamalef_found != 0 ) || (tashkeelFound != 0) ){ |
Doug Felt | 2bae19d | 2010-06-07 16:28:19 -0700 | [diff] [blame] | 1343 | destSize = handleGeneratedSpaces(dest,sourceLength,destSize,options,pErrorCode); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1344 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1345 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1346 | if ( (seenfamFound != 0) || (yehhamzaFound != 0) ) { |
| 1347 | destSize = expandCompositChar(dest, sourceLength,destSize,options,pErrorCode, SHAPE_MODE); |
| 1348 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1349 | return destSize; |
| 1350 | } |
| 1351 | |
| 1352 | /* |
| 1353 | *Name : deShapeUnicode |
| 1354 | *Function : Converts an Arabic Unicode buffer in FExx Range into unshaped |
| 1355 | * arabic Unicode buffer in 06xx Range |
| 1356 | */ |
| 1357 | static int32_t |
| 1358 | deShapeUnicode(UChar *dest, int32_t sourceLength, |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1359 | int32_t destSize,uint32_t options, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1360 | UErrorCode *pErrorCode) { |
| 1361 | int32_t i = 0; |
| 1362 | int32_t lamalef_found = 0; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1363 | int32_t yehHamzaComposeEnabled = 0; |
| 1364 | int32_t seenComposeEnabled = 0; |
| 1365 | |
| 1366 | yehHamzaComposeEnabled = ((options&U_SHAPE_YEHHAMZA_MASK) == U_SHAPE_YEHHAMZA_TWOCELL_NEAR) ? 1 : 0; |
| 1367 | seenComposeEnabled = ((options&U_SHAPE_SEEN_MASK) == U_SHAPE_SEEN_TWOCELL_NEAR)? 1 : 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1368 | |
| 1369 | /* |
| 1370 | *This for loop changes the buffer from the Unicode FE range to |
| 1371 | *the Unicode 06 range |
| 1372 | */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1373 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1374 | for(i = 0; i < sourceLength; i++) { |
| 1375 | UChar inputChar = dest[i]; |
| 1376 | if ( (inputChar >= 0xFB50) && (inputChar <= 0xFBFF)) { /* FBxx Arabic range */ |
| 1377 | UChar c = convertFBto06 [ (inputChar - 0xFB50) ]; |
| 1378 | if (c != 0) |
| 1379 | dest[i] = c; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1380 | } else if( (yehHamzaComposeEnabled == 1) && ((inputChar == HAMZA06_CHAR) || (inputChar == HAMZAFE_CHAR)) |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1381 | && (i < (sourceLength - 1)) && isAlefMaksouraChar(dest[i+1] )) { |
| 1382 | dest[i] = SPACE_CHAR; |
| 1383 | dest[i+1] = YEH_HAMZA_CHAR; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1384 | } else if ( (seenComposeEnabled == 1) && (isTailChar(inputChar)) && (i< (sourceLength - 1)) |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1385 | && (isSeenTailFamilyChar(dest[i+1])) ) { |
| 1386 | dest[i] = SPACE_CHAR; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1387 | } else if (( inputChar >= 0xFE70) && (inputChar <= 0xFEF4 )) { /* FExx Arabic range */ |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1388 | dest[i] = convertFEto06 [ (inputChar - 0xFE70) ]; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1389 | } else { |
| 1390 | dest[i] = inputChar ; |
| 1391 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1392 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1393 | if( isLamAlefChar(dest[i]) ) |
| 1394 | lamalef_found = 1; |
| 1395 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1396 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1397 | destSize = sourceLength; |
| 1398 | if (lamalef_found != 0){ |
| 1399 | destSize = expandCompositChar(dest,sourceLength,destSize,options,pErrorCode,DESHAPE_MODE); |
| 1400 | } |
| 1401 | return destSize; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1404 | /* |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1405 | **************************************** |
| 1406 | * u_shapeArabic |
| 1407 | **************************************** |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1408 | */ |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1409 | |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 1410 | /* BEGIN android-changed */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1411 | U_CAPI int32_t U_EXPORT2 |
| 1412 | u_shapeArabic(const UChar *source, int32_t sourceLength, |
| 1413 | UChar *dest, int32_t destCapacity, |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 1414 | uint64_t options, |
| 1415 | UErrorCode *pErrorCode) { |
| 1416 | /* END android-changed */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1417 | |
| 1418 | int32_t destLength; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1419 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1420 | spacesRelativeToTextBeginEnd = 0; |
| 1421 | uShapeLamalefBegin = U_SHAPE_LAMALEF_BEGIN; |
| 1422 | uShapeLamalefEnd = U_SHAPE_LAMALEF_END; |
| 1423 | uShapeTashkeelBegin = U_SHAPE_TASHKEEL_BEGIN; |
| 1424 | uShapeTashkeelEnd = U_SHAPE_TASHKEEL_END; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1425 | |
| 1426 | /* usual error checking */ |
| 1427 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
| 1431 | /* make sure that no reserved options values are used; allow dest==NULL only for preflighting */ |
| 1432 | if( source==NULL || sourceLength<-1 || (dest==NULL && destCapacity!=0) || destCapacity<0 || |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1433 | (((options&U_SHAPE_TASHKEEL_MASK) > 0) && |
| 1434 | ((options&U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) == U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) ) || |
| 1435 | (((options&U_SHAPE_TASHKEEL_MASK) > 0) && |
| 1436 | ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_UNSHAPE)) || |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1437 | (options&U_SHAPE_DIGIT_TYPE_RESERVED)==U_SHAPE_DIGIT_TYPE_RESERVED || |
| 1438 | (options&U_SHAPE_DIGITS_MASK)==U_SHAPE_DIGITS_RESERVED || |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1439 | ((options&U_SHAPE_LAMALEF_MASK) != U_SHAPE_LAMALEF_RESIZE && |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1440 | (options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) != 0) || |
| 1441 | ((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) == U_SHAPE_AGGREGATE_TASHKEEL && |
| 1442 | (options&U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) != U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1443 | ) |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1444 | { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1445 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
| 1446 | return 0; |
| 1447 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1448 | /* Validate lamalef options */ |
| 1449 | if(((options&U_SHAPE_LAMALEF_MASK) > 0)&& |
| 1450 | !(((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_BEGIN) || |
| 1451 | ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_END ) || |
| 1452 | ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_RESIZE )|| |
| 1453 | ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_AUTO) || |
| 1454 | ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_NEAR))) |
| 1455 | { |
| 1456 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
| 1457 | return 0; |
| 1458 | } |
| 1459 | /* Validate Tashkeel options */ |
| 1460 | if(((options&U_SHAPE_TASHKEEL_MASK) > 0)&& |
| 1461 | !(((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_BEGIN) || |
| 1462 | ((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_END ) |
| 1463 | ||((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_RESIZE )|| |
| 1464 | ((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL))) |
| 1465 | { |
| 1466 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
| 1467 | return 0; |
| 1468 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1469 | /* determine the source length */ |
| 1470 | if(sourceLength==-1) { |
| 1471 | sourceLength=u_strlen(source); |
| 1472 | } |
| 1473 | if(sourceLength<=0) { |
| 1474 | return u_terminateUChars(dest, destCapacity, 0, pErrorCode); |
| 1475 | } |
| 1476 | |
| 1477 | /* check that source and destination do not overlap */ |
| 1478 | if( dest!=NULL && |
| 1479 | ((source<=dest && dest<source+sourceLength) || |
| 1480 | (dest<=source && source<dest+destCapacity))) { |
| 1481 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
| 1482 | return 0; |
| 1483 | } |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1484 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1485 | /* Does Options contain the new Seen Tail Unicode code point option */ |
claireho | b26ce3a | 2012-01-10 17:54:41 -0800 | [diff] [blame] | 1486 | if ( (options&U_SHAPE_TAIL_TYPE_MASK) == U_SHAPE_TAIL_NEW_UNICODE){ |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1487 | tailChar = NEW_TAIL_CHAR; |
| 1488 | }else { |
| 1489 | tailChar = OLD_TAIL_CHAR; |
| 1490 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1491 | |
| 1492 | if((options&U_SHAPE_LETTERS_MASK)!=U_SHAPE_LETTERS_NOOP) { |
| 1493 | UChar buffer[300]; |
| 1494 | UChar *tempbuffer, *tempsource = NULL; |
| 1495 | int32_t outputSize, spacesCountl=0, spacesCountr=0; |
| 1496 | |
| 1497 | if((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK)>0) { |
| 1498 | int32_t logical_order = (options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL; |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1499 | int32_t aggregate_tashkeel = |
| 1500 | (options&(U_SHAPE_AGGREGATE_TASHKEEL_MASK+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED)) == |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1501 | (U_SHAPE_AGGREGATE_TASHKEEL+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED); |
| 1502 | int step=logical_order?1:-1; |
| 1503 | int j=logical_order?-1:2*sourceLength; |
| 1504 | int i=logical_order?-1:sourceLength; |
| 1505 | int end=logical_order?sourceLength:-1; |
| 1506 | int aggregation_possible = 1; |
| 1507 | UChar prev = 0; |
| 1508 | UChar prevLink, currLink = 0; |
| 1509 | int newSourceLength = 0; |
| 1510 | tempsource = (UChar *)uprv_malloc(2*sourceLength*U_SIZEOF_UCHAR); |
| 1511 | if(tempsource == NULL) { |
| 1512 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 1513 | return 0; |
| 1514 | } |
| 1515 | |
| 1516 | while ((i+=step) != end) { |
| 1517 | prevLink = currLink; |
| 1518 | currLink = getLink(source[i]); |
| 1519 | if (aggregate_tashkeel && ((prevLink|currLink)&COMBINE) == COMBINE && aggregation_possible) { |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1520 | aggregation_possible = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1521 | tempsource[j] = (prev<source[i]?prev:source[i])-0x064C+0xFC5E; |
| 1522 | currLink = getLink(tempsource[j]); |
| 1523 | } else { |
| 1524 | aggregation_possible = 1; |
| 1525 | tempsource[j+=step] = source[i]; |
| 1526 | prev = source[i]; |
| 1527 | newSourceLength++; |
| 1528 | } |
| 1529 | } |
| 1530 | source = tempsource+(logical_order?0:j); |
| 1531 | sourceLength = newSourceLength; |
| 1532 | } |
| 1533 | |
| 1534 | /* calculate destination size */ |
| 1535 | /* TODO: do we ever need to do this pure preflighting? */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1536 | if(((options&U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_RESIZE) || |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1537 | ((options&U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_RESIZE)) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1538 | outputSize=calculateSize(source,sourceLength,destCapacity,options); |
| 1539 | } else { |
| 1540 | outputSize=sourceLength; |
| 1541 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1542 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1543 | if(outputSize>destCapacity) { |
| 1544 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
| 1545 | if (tempsource != NULL) uprv_free(tempsource); |
| 1546 | return outputSize; |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * need a temporary buffer of size max(outputSize, sourceLength) |
| 1551 | * because at first we copy source->temp |
| 1552 | */ |
| 1553 | if(sourceLength>outputSize) { |
| 1554 | outputSize=sourceLength; |
| 1555 | } |
| 1556 | |
| 1557 | /* Start of Arabic letter shaping part */ |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1558 | if(outputSize<=LENGTHOF(buffer)) { |
| 1559 | outputSize=LENGTHOF(buffer); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1560 | tempbuffer=buffer; |
| 1561 | } else { |
| 1562 | tempbuffer = (UChar *)uprv_malloc(outputSize*U_SIZEOF_UCHAR); |
| 1563 | |
| 1564 | /*Test for NULL*/ |
| 1565 | if(tempbuffer == NULL) { |
| 1566 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 1567 | if (tempsource != NULL) uprv_free(tempsource); |
| 1568 | return 0; |
| 1569 | } |
| 1570 | } |
| 1571 | uprv_memcpy(tempbuffer, source, sourceLength*U_SIZEOF_UCHAR); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1572 | if (tempsource != NULL){ |
| 1573 | uprv_free(tempsource); |
| 1574 | } |
| 1575 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1576 | if(sourceLength<outputSize) { |
| 1577 | uprv_memset(tempbuffer+sourceLength, 0, (outputSize-sourceLength)*U_SIZEOF_UCHAR); |
| 1578 | } |
| 1579 | |
| 1580 | if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) { |
| 1581 | countSpaces(tempbuffer,sourceLength,options,&spacesCountl,&spacesCountr); |
| 1582 | invertBuffer(tempbuffer,sourceLength,options,spacesCountl,spacesCountr); |
| 1583 | } |
| 1584 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1585 | if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_VISUAL_LTR) { |
| 1586 | if((options&U_SHAPE_SPACES_RELATIVE_TO_TEXT_MASK) == U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END) { |
| 1587 | spacesRelativeToTextBeginEnd = 1; |
| 1588 | uShapeLamalefBegin = U_SHAPE_LAMALEF_END; |
| 1589 | uShapeLamalefEnd = U_SHAPE_LAMALEF_BEGIN; |
| 1590 | |
| 1591 | uShapeTashkeelBegin = U_SHAPE_TASHKEEL_END; |
| 1592 | uShapeTashkeelEnd = U_SHAPE_TASHKEEL_BEGIN; |
| 1593 | } |
| 1594 | } |
| 1595 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1596 | switch(options&U_SHAPE_LETTERS_MASK) { |
| 1597 | case U_SHAPE_LETTERS_SHAPE : |
Craig Cornelius | 103e9ff | 2012-10-09 17:03:29 -0700 | [diff] [blame^] | 1598 | if( (options&U_SHAPE_TASHKEEL_MASK)> 0 |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1599 | && ((options&U_SHAPE_TASHKEEL_MASK) !=U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL)) { |
| 1600 | /* Call the shaping function with tashkeel flag == 2 for removal of tashkeel */ |
Doug Felt | 2bae19d | 2010-06-07 16:28:19 -0700 | [diff] [blame] | 1601 | destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,2); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1602 | }else { |
| 1603 | /* default Call the shaping function with tashkeel flag == 1 */ |
Doug Felt | 2bae19d | 2010-06-07 16:28:19 -0700 | [diff] [blame] | 1604 | destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,1); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1605 | |
| 1606 | /*After shaping text check if user wants to remove tashkeel and replace it with tatweel*/ |
| 1607 | if( (options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL){ |
| 1608 | destLength = handleTashkeelWithTatweel(tempbuffer,destLength,destCapacity,options,pErrorCode); |
| 1609 | } |
| 1610 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1611 | break; |
| 1612 | case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED : |
| 1613 | /* Call the shaping function with tashkeel flag == 0 */ |
Doug Felt | 2bae19d | 2010-06-07 16:28:19 -0700 | [diff] [blame] | 1614 | destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,0); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1615 | break; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 1616 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1617 | case U_SHAPE_LETTERS_UNSHAPE : |
| 1618 | /* Call the deshaping function */ |
| 1619 | destLength = deShapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode); |
| 1620 | break; |
| 1621 | default : |
| 1622 | /* will never occur because of validity checks above */ |
| 1623 | destLength = 0; |
| 1624 | break; |
| 1625 | } |
| 1626 | |
| 1627 | /* |
| 1628 | * TODO: (markus 2002aug01) |
| 1629 | * For as long as we always preflight the outputSize above |
| 1630 | * we should U_ASSERT(outputSize==destLength) |
| 1631 | * except for the adjustment above before the tempbuffer allocation |
| 1632 | */ |
| 1633 | |
| 1634 | if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) { |
| 1635 | countSpaces(tempbuffer,destLength,options,&spacesCountl,&spacesCountr); |
| 1636 | invertBuffer(tempbuffer,destLength,options,spacesCountl,spacesCountr); |
| 1637 | } |
| 1638 | uprv_memcpy(dest, tempbuffer, uprv_min(destLength, destCapacity)*U_SIZEOF_UCHAR); |
| 1639 | |
| 1640 | if(tempbuffer!=buffer) { |
| 1641 | uprv_free(tempbuffer); |
| 1642 | } |
| 1643 | |
| 1644 | if(destLength>destCapacity) { |
| 1645 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
| 1646 | return destLength; |
| 1647 | } |
| 1648 | |
| 1649 | /* End of Arabic letter shaping part */ |
| 1650 | } else { |
| 1651 | /* |
| 1652 | * No letter shaping: |
| 1653 | * just make sure the destination is large enough and copy the string. |
| 1654 | */ |
| 1655 | if(destCapacity<sourceLength) { |
| 1656 | /* this catches preflighting, too */ |
| 1657 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
| 1658 | return sourceLength; |
| 1659 | } |
| 1660 | uprv_memcpy(dest, source, sourceLength*U_SIZEOF_UCHAR); |
| 1661 | destLength=sourceLength; |
| 1662 | } |
| 1663 | |
| 1664 | /* |
| 1665 | * Perform number shaping. |
| 1666 | * With UTF-16 or UTF-32, the length of the string is constant. |
| 1667 | * The easiest way to do this is to operate on the destination and |
| 1668 | * "shape" the digits in-place. |
| 1669 | */ |
| 1670 | if((options&U_SHAPE_DIGITS_MASK)!=U_SHAPE_DIGITS_NOOP) { |
| 1671 | UChar digitBase; |
| 1672 | int32_t i; |
| 1673 | |
| 1674 | /* select the requested digit group */ |
| 1675 | switch(options&U_SHAPE_DIGIT_TYPE_MASK) { |
| 1676 | case U_SHAPE_DIGIT_TYPE_AN: |
| 1677 | digitBase=0x660; /* Unicode: "Arabic-Indic digits" */ |
| 1678 | break; |
| 1679 | case U_SHAPE_DIGIT_TYPE_AN_EXTENDED: |
| 1680 | digitBase=0x6f0; /* Unicode: "Eastern Arabic-Indic digits (Persian and Urdu)" */ |
| 1681 | break; |
| 1682 | default: |
| 1683 | /* will never occur because of validity checks above */ |
| 1684 | digitBase=0; |
| 1685 | break; |
| 1686 | } |
| 1687 | |
| 1688 | /* perform the requested operation */ |
| 1689 | switch(options&U_SHAPE_DIGITS_MASK) { |
| 1690 | case U_SHAPE_DIGITS_EN2AN: |
| 1691 | /* add (digitBase-'0') to each European (ASCII) digit code point */ |
| 1692 | digitBase-=0x30; |
| 1693 | for(i=0; i<destLength; ++i) { |
| 1694 | if(((uint32_t)dest[i]-0x30)<10) { |
| 1695 | dest[i]+=digitBase; |
| 1696 | } |
| 1697 | } |
| 1698 | break; |
| 1699 | case U_SHAPE_DIGITS_AN2EN: |
| 1700 | /* subtract (digitBase-'0') from each Arabic digit code point */ |
| 1701 | for(i=0; i<destLength; ++i) { |
| 1702 | if(((uint32_t)dest[i]-(uint32_t)digitBase)<10) { |
| 1703 | dest[i]-=digitBase-0x30; |
| 1704 | } |
| 1705 | } |
| 1706 | break; |
| 1707 | case U_SHAPE_DIGITS_ALEN2AN_INIT_LR: |
| 1708 | _shapeToArabicDigitsWithContext(dest, destLength, |
| 1709 | digitBase, |
| 1710 | (UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL), |
| 1711 | FALSE); |
| 1712 | break; |
| 1713 | case U_SHAPE_DIGITS_ALEN2AN_INIT_AL: |
| 1714 | _shapeToArabicDigitsWithContext(dest, destLength, |
| 1715 | digitBase, |
| 1716 | (UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL), |
| 1717 | TRUE); |
| 1718 | break; |
| 1719 | default: |
| 1720 | /* will never occur because of validity checks above */ |
| 1721 | break; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | return u_terminateUChars(dest, destCapacity, destLength, pErrorCode); |
| 1726 | } |