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