Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | ****************************************************************************** |
| 3 | * |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 4 | * Copyright (C) 2000-2009, 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 | |
| 79 | UChar tailChar = OLD_TAIL_CHAR; |
| 80 | uint32_t uShapeLamalefBegin = U_SHAPE_LAMALEF_BEGIN; |
| 81 | uint32_t uShapeLamalefEnd = U_SHAPE_LAMALEF_END; |
| 82 | uint32_t uShapeTashkeelBegin = U_SHAPE_TASHKEEL_BEGIN; |
| 83 | uint32_t uShapeTashkeelEnd = U_SHAPE_TASHKEEL_END; |
| 84 | int spacesRelativeToTextBeginEnd = 0; |
| 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, |
| 710 | uint32_t options, |
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){ |
| 766 | if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_NEAR ){ |
| 767 | lamAlefOption = 1; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (lamAlefOption){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 772 | /* Lam+Alef is already shaped into LamAlef + FFFF */ |
| 773 | i = 0; |
| 774 | while(i < sourceLength) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 775 | if(lamAlefOption&&dest[i] == LAMALEF_SPACE_SUB){ |
| 776 | dest[i] = SPACE_CHAR; |
| 777 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 778 | i++; |
| 779 | } |
| 780 | destSize = sourceLength; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 781 | } |
| 782 | lamAlefOption = 0; |
| 783 | tashkeelOption = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 784 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 785 | if (shapingMode == 0) { |
| 786 | if ( ((options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefBegin) || |
| 787 | (((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO ) |
| 788 | && (spacesRelativeToTextBeginEnd==1)) ) { |
| 789 | lamAlefOption = 1; |
| 790 | } |
| 791 | if ( (options&U_SHAPE_TASHKEEL_MASK) == uShapeTashkeelBegin ) { |
| 792 | tashkeelOption = 1; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | if(lamAlefOption || tashkeelOption){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 797 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); |
| 798 | |
| 799 | /* Test for NULL */ |
| 800 | if(tempbuffer == NULL) { |
| 801 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 806 | i = j = sourceLength; count = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 807 | while(i >= 0) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 808 | if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) || |
| 809 | (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 810 | j++; |
| 811 | count++; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 812 | }else { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 813 | tempbuffer[j] = dest[i]; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 814 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 815 | i--; |
| 816 | j--; |
| 817 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 818 | |
| 819 | for(i=0 ;i < count; i++){ |
| 820 | tempbuffer[i] = SPACE_CHAR; |
| 821 | } |
| 822 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 823 | uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR); |
| 824 | destSize = sourceLength; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 825 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 826 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 827 | |
| 828 | |
| 829 | lamAlefOption = 0; |
| 830 | tashkeelOption = 0; |
| 831 | |
| 832 | if (shapingMode == 0) { |
| 833 | if ( ((options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefEnd) || |
| 834 | (((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO ) |
| 835 | && (spacesRelativeToTextBeginEnd==0)) ) { |
| 836 | lamAlefOption = 1; |
| 837 | } |
| 838 | if ( (options&U_SHAPE_TASHKEEL_MASK) == uShapeTashkeelEnd ){ |
| 839 | tashkeelOption = 1; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if(lamAlefOption || tashkeelOption){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 844 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 845 | /* Test for NULL */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 846 | if(tempbuffer == NULL) { |
| 847 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); |
| 852 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 853 | i = j = 0; count = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 854 | while(i < sourceLength) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 855 | if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) || |
| 856 | (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 857 | j--; |
| 858 | count++; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 859 | }else { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 860 | tempbuffer[j] = dest[i]; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 861 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 862 | i++; |
| 863 | j++; |
| 864 | } |
| 865 | |
| 866 | while(count >= 0) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 867 | tempbuffer[i] = SPACE_CHAR; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 868 | i--; |
| 869 | count--; |
| 870 | } |
| 871 | |
| 872 | uprv_memcpy(dest,tempbuffer, sourceLength*U_SIZEOF_UCHAR); |
| 873 | destSize = sourceLength; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 876 | |
| 877 | if(tempbuffer){ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 878 | uprv_free(tempbuffer); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 879 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 880 | |
| 881 | return destSize; |
| 882 | } |
| 883 | |
| 884 | /* |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 885 | *Name :expandCompositCharAtBegin |
| 886 | *Function :Expands the LamAlef character to Lam and Alef consuming the required |
| 887 | * space from beginning of the buffer. If the text type was visual_LTR |
| 888 | * and the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END was selected |
| 889 | * the spaces will be located at end of buffer. |
| 890 | * If there are no spaces to expand the LamAlef, an error |
| 891 | * 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] | 892 | */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 893 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 894 | static int32_t |
| 895 | expandCompositCharAtBegin(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 896 | int32_t i = 0,j = 0; |
| 897 | int32_t countl = 0; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 898 | UChar *tempbuffer=NULL; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 899 | |
| 900 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 901 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 902 | /* Test for NULL */ |
| 903 | if(tempbuffer == NULL) { |
| 904 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 905 | return 0; |
| 906 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 907 | |
| 908 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); |
| 909 | |
| 910 | i = 0; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 911 | while(dest[i] == SPACE_CHAR) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 912 | countl++; |
| 913 | i++; |
| 914 | } |
| 915 | |
| 916 | i = j = sourceLength-1; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 917 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 918 | while(i >= 0 && j >= 0) { |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 919 | if( countl>0 && isLamAlefChar(dest[i])) { |
| 920 | tempbuffer[j] = LAM_CHAR; |
| 921 | tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ]; |
| 922 | j--; |
| 923 | countl--; |
| 924 | }else { |
| 925 | if( countl == 0 && isLamAlefChar(dest[i]) ) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 926 | *pErrorCode=U_NO_SPACE_AVAILABLE; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 927 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 928 | tempbuffer[j] = dest[i]; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 929 | } |
| 930 | i--; |
| 931 | j--; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 932 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 933 | uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 934 | |
| 935 | uprv_free(tempbuffer); |
| 936 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 937 | destSize = sourceLength; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 938 | return destSize; |
| 939 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 940 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 941 | /* |
| 942 | *Name : expandCompositCharAtEnd |
| 943 | *Function : Expands the LamAlef character to Lam and Alef consuming the |
| 944 | * required space from end of the buffer. If the text type was |
| 945 | * Visual LTR and the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END |
| 946 | * was used, the spaces will be consumed from begin of buffer. If |
| 947 | * there are no spaces to expand the LamAlef, an error |
| 948 | * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h |
| 949 | */ |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 950 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 951 | static int32_t |
| 952 | expandCompositCharAtEnd(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode) { |
| 953 | int32_t i = 0,j = 0; |
| 954 | |
| 955 | int32_t countr = 0; |
| 956 | int32_t inpsize = sourceLength; |
| 957 | |
| 958 | UChar *tempbuffer=NULL; |
| 959 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); |
| 960 | |
| 961 | /* Test for NULL */ |
| 962 | if(tempbuffer == NULL) { |
| 963 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); |
| 968 | |
| 969 | while(dest[inpsize-1] == SPACE_CHAR) { |
| 970 | countr++; |
| 971 | inpsize--; |
| 972 | } |
| 973 | |
| 974 | i = sourceLength - countr - 1; |
| 975 | j = sourceLength - 1; |
| 976 | |
| 977 | while(i >= 0 && j >= 0) { |
| 978 | if( countr>0 && isLamAlefChar(dest[i]) ) { |
| 979 | tempbuffer[j] = LAM_CHAR; |
| 980 | tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ]; |
| 981 | j--; |
| 982 | countr--; |
| 983 | }else { |
| 984 | if ((countr == 0) && isLamAlefChar(dest[i]) ) { |
| 985 | *pErrorCode=U_NO_SPACE_AVAILABLE; |
| 986 | } |
| 987 | tempbuffer[j] = dest[i]; |
| 988 | } |
| 989 | i--; |
| 990 | j--; |
| 991 | } |
| 992 | |
| 993 | if(countr > 0) { |
| 994 | uprv_memmove(tempbuffer, tempbuffer+countr, sourceLength*U_SIZEOF_UCHAR); |
| 995 | if(u_strlen(tempbuffer) < sourceLength) { |
| 996 | for(i=sourceLength-1;i>=sourceLength-countr;i--) { |
| 997 | tempbuffer[i] = SPACE_CHAR; |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR); |
| 1002 | |
| 1003 | uprv_free(tempbuffer); |
| 1004 | |
| 1005 | destSize = sourceLength; |
| 1006 | return destSize; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | *Name : expandCompositCharAtNear |
| 1011 | *Function : Expands the LamAlef character into Lam + Alef, YehHamza character |
| 1012 | * into Yeh + Hamza, SeenFamily character into SeenFamily character |
| 1013 | * + Tail, while consuming the space next to the character. |
| 1014 | * If there are no spaces next to the character, an error |
| 1015 | * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h |
| 1016 | */ |
| 1017 | |
| 1018 | static int32_t |
| 1019 | expandCompositCharAtNear(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode, |
| 1020 | int yehHamzaOption, int seenTailOption, int lamAlefOption) { |
| 1021 | int32_t i = 0; |
| 1022 | |
| 1023 | |
| 1024 | UChar lamalefChar, yehhamzaChar; |
| 1025 | |
| 1026 | for(i = 0 ;i<=sourceLength-1;i++) { |
| 1027 | if (seenTailOption && isSeenTailFamilyChar(dest[i])) { |
| 1028 | if ((i>0) && (dest[i-1] == SPACE_CHAR) ) { |
| 1029 | dest[i-1] = tailChar; |
| 1030 | }else { |
| 1031 | *pErrorCode=U_NO_SPACE_AVAILABLE; |
| 1032 | } |
| 1033 | }else if(yehHamzaOption && (isYehHamzaChar(dest[i])) ) { |
| 1034 | if ((i>0) && (dest[i-1] == SPACE_CHAR) ) { |
| 1035 | yehhamzaChar = dest[i]; |
| 1036 | dest[i] = yehHamzaToYeh[yehhamzaChar - YEH_HAMZAFE_CHAR]; |
| 1037 | dest[i-1] = HAMZAFE_CHAR; |
| 1038 | }else { |
| 1039 | |
| 1040 | *pErrorCode=U_NO_SPACE_AVAILABLE; |
| 1041 | } |
| 1042 | }else if(lamAlefOption && isLamAlefChar(dest[i+1])) { |
| 1043 | if(dest[i] == SPACE_CHAR){ |
| 1044 | lamalefChar = dest[i+1]; |
| 1045 | dest[i+1] = LAM_CHAR; |
| 1046 | dest[i] = convertLamAlef[ lamalefChar - 0xFEF5 ]; |
| 1047 | }else { |
| 1048 | *pErrorCode=U_NO_SPACE_AVAILABLE; |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | destSize = sourceLength; |
| 1053 | return destSize; |
| 1054 | } |
| 1055 | /* |
| 1056 | * Name : expandCompositChar |
| 1057 | * Function : LamAlef, need special handling, since it expands from one |
| 1058 | * character into two characters while shaping or deshaping. |
| 1059 | * In order to expand it, near or far spaces according to the |
| 1060 | * options user specifies. Also buffer size can be increased. |
| 1061 | * |
| 1062 | * For SeenFamily characters and YehHamza only the near option is |
| 1063 | * supported, while for LamAlef we can take spaces from begin, end, |
| 1064 | * near or even increase the buffer size. |
| 1065 | * There is also the Auto option for LamAlef only, which will first |
| 1066 | * search for a space at end, begin then near, respectively. |
| 1067 | * If there are no spaces to expand these characters, an error will be set to |
| 1068 | * U_NO_SPACE_AVAILABLE as defined in utypes.h |
| 1069 | */ |
| 1070 | |
| 1071 | static int32_t |
| 1072 | expandCompositChar(UChar *dest, int32_t sourceLength, |
| 1073 | int32_t destSize,uint32_t options, |
| 1074 | UErrorCode *pErrorCode, int shapingMode) { |
| 1075 | |
| 1076 | int32_t i = 0,j = 0; |
| 1077 | |
| 1078 | UChar *tempbuffer=NULL; |
| 1079 | int yehHamzaOption = 0; |
| 1080 | int seenTailOption = 0; |
| 1081 | int lamAlefOption = 0; |
| 1082 | |
| 1083 | if (shapingMode == 1){ |
| 1084 | if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO){ |
| 1085 | |
| 1086 | if(spacesRelativeToTextBeginEnd == 0) { |
| 1087 | destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode); |
| 1088 | |
| 1089 | if(*pErrorCode == U_NO_SPACE_AVAILABLE) { |
| 1090 | *pErrorCode = U_ZERO_ERROR; |
| 1091 | destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode); |
| 1092 | } |
| 1093 | }else { |
| 1094 | destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode); |
| 1095 | |
| 1096 | if(*pErrorCode == U_NO_SPACE_AVAILABLE) { |
| 1097 | *pErrorCode = U_ZERO_ERROR; |
| 1098 | destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | if(*pErrorCode == U_NO_SPACE_AVAILABLE) { |
| 1103 | *pErrorCode = U_ZERO_ERROR; |
| 1104 | destSize = expandCompositCharAtNear(dest, sourceLength, destSize, pErrorCode, yehHamzaOption, |
| 1105 | seenTailOption, 1); |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | if (shapingMode == 1){ |
| 1111 | if ( (options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefEnd){ |
| 1112 | destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | if (shapingMode == 1){ |
| 1117 | if ( (options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefBegin){ |
| 1118 | destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | if (shapingMode == 0){ |
| 1123 | if ((options&U_SHAPE_YEHHAMZA_MASK) == U_SHAPE_YEHHAMZA_TWOCELL_NEAR){ |
| 1124 | yehHamzaOption = 1; |
| 1125 | } |
| 1126 | if ((options&U_SHAPE_SEEN_MASK) == U_SHAPE_SEEN_TWOCELL_NEAR){ |
| 1127 | seenTailOption = 1; |
| 1128 | } |
| 1129 | } |
| 1130 | if (shapingMode == 1) { |
| 1131 | if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_NEAR) { |
| 1132 | lamAlefOption = 1; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | |
| 1137 | if (yehHamzaOption || seenTailOption || lamAlefOption){ |
| 1138 | destSize = expandCompositCharAtNear(dest, sourceLength, destSize, pErrorCode, yehHamzaOption, |
| 1139 | seenTailOption,lamAlefOption); |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | if (shapingMode == 1){ |
| 1144 | if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE){ |
| 1145 | destSize = calculateSize(dest,sourceLength,destSize,options); |
| 1146 | tempbuffer = (UChar *)uprv_malloc((destSize+1)*U_SIZEOF_UCHAR); |
| 1147 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1148 | /* Test for NULL */ |
| 1149 | if(tempbuffer == NULL) { |
| 1150 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 1151 | return 0; |
| 1152 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1153 | |
| 1154 | uprv_memset(tempbuffer, 0, (destSize+1)*U_SIZEOF_UCHAR); |
| 1155 | |
| 1156 | i = j = 0; |
| 1157 | while(i < destSize && j < destSize) { |
| 1158 | if(isLamAlefChar(dest[i]) ) { |
| 1159 | tempbuffer[j] = convertLamAlef[ dest[i] - 0xFEF5 ]; |
| 1160 | tempbuffer[j+1] = LAM_CHAR; |
| 1161 | j++; |
| 1162 | }else { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1163 | tempbuffer[j] = dest[i]; |
| 1164 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1165 | i++; |
| 1166 | j++; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1167 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1168 | |
| 1169 | uprv_memcpy(dest, tempbuffer, destSize*U_SIZEOF_UCHAR); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1170 | destSize = sourceLength; |
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, |
| 1187 | int32_t destSize,uint32_t options, |
| 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) ){ |
| 1339 | destSize = handleGeneratedSpaces(dest,sourceLength,destSize,options,pErrorCode); |
| 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, |
| 1409 | uint32_t options, |
| 1410 | UErrorCode *pErrorCode) { |
| 1411 | |
| 1412 | int32_t destLength; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1413 | |
| 1414 | spacesRelativeToTextBeginEnd = 0; |
| 1415 | uShapeLamalefBegin = U_SHAPE_LAMALEF_BEGIN; |
| 1416 | uShapeLamalefEnd = U_SHAPE_LAMALEF_END; |
| 1417 | uShapeTashkeelBegin = U_SHAPE_TASHKEEL_BEGIN; |
| 1418 | uShapeTashkeelEnd = U_SHAPE_TASHKEEL_END; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1419 | |
| 1420 | /* usual error checking */ |
| 1421 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
| 1425 | /* make sure that no reserved options values are used; allow dest==NULL only for preflighting */ |
| 1426 | if( source==NULL || sourceLength<-1 || (dest==NULL && destCapacity!=0) || destCapacity<0 || |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1427 | (((options&U_SHAPE_TASHKEEL_MASK) > 0) && |
| 1428 | ((options&U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) == U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) ) || |
| 1429 | (((options&U_SHAPE_TASHKEEL_MASK) > 0) && |
| 1430 | ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_UNSHAPE)) || |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1431 | (options&U_SHAPE_DIGIT_TYPE_RESERVED)==U_SHAPE_DIGIT_TYPE_RESERVED || |
| 1432 | (options&U_SHAPE_DIGITS_MASK)==U_SHAPE_DIGITS_RESERVED || |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1433 | ((options&U_SHAPE_LAMALEF_MASK) != U_SHAPE_LAMALEF_RESIZE && |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1434 | (options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) != 0) || |
| 1435 | ((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) == U_SHAPE_AGGREGATE_TASHKEEL && |
| 1436 | (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^] | 1437 | ) |
| 1438 | { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1439 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
| 1440 | return 0; |
| 1441 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1442 | /* Validate lamalef options */ |
| 1443 | if(((options&U_SHAPE_LAMALEF_MASK) > 0)&& |
| 1444 | !(((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_BEGIN) || |
| 1445 | ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_END ) || |
| 1446 | ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_RESIZE )|| |
| 1447 | ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_AUTO) || |
| 1448 | ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_NEAR))) |
| 1449 | { |
| 1450 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
| 1451 | return 0; |
| 1452 | } |
| 1453 | /* Validate Tashkeel options */ |
| 1454 | if(((options&U_SHAPE_TASHKEEL_MASK) > 0)&& |
| 1455 | !(((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_BEGIN) || |
| 1456 | ((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_END ) |
| 1457 | ||((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_RESIZE )|| |
| 1458 | ((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL))) |
| 1459 | { |
| 1460 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
| 1461 | return 0; |
| 1462 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1463 | /* determine the source length */ |
| 1464 | if(sourceLength==-1) { |
| 1465 | sourceLength=u_strlen(source); |
| 1466 | } |
| 1467 | if(sourceLength<=0) { |
| 1468 | return u_terminateUChars(dest, destCapacity, 0, pErrorCode); |
| 1469 | } |
| 1470 | |
| 1471 | /* check that source and destination do not overlap */ |
| 1472 | if( dest!=NULL && |
| 1473 | ((source<=dest && dest<source+sourceLength) || |
| 1474 | (dest<=source && source<dest+destCapacity))) { |
| 1475 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
| 1476 | return 0; |
| 1477 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1478 | |
| 1479 | /* Does Options contain the new Seen Tail Unicode code point option */ |
| 1480 | if ( (options&SHAPE_TAIL_TYPE_MASK) == SHAPE_TAIL_NEW_UNICODE){ |
| 1481 | tailChar = NEW_TAIL_CHAR; |
| 1482 | }else { |
| 1483 | tailChar = OLD_TAIL_CHAR; |
| 1484 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1485 | |
| 1486 | if((options&U_SHAPE_LETTERS_MASK)!=U_SHAPE_LETTERS_NOOP) { |
| 1487 | UChar buffer[300]; |
| 1488 | UChar *tempbuffer, *tempsource = NULL; |
| 1489 | int32_t outputSize, spacesCountl=0, spacesCountr=0; |
| 1490 | |
| 1491 | if((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK)>0) { |
| 1492 | int32_t logical_order = (options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL; |
| 1493 | int32_t aggregate_tashkeel = |
| 1494 | (options&(U_SHAPE_AGGREGATE_TASHKEEL_MASK+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED)) == |
| 1495 | (U_SHAPE_AGGREGATE_TASHKEEL+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED); |
| 1496 | int step=logical_order?1:-1; |
| 1497 | int j=logical_order?-1:2*sourceLength; |
| 1498 | int i=logical_order?-1:sourceLength; |
| 1499 | int end=logical_order?sourceLength:-1; |
| 1500 | int aggregation_possible = 1; |
| 1501 | UChar prev = 0; |
| 1502 | UChar prevLink, currLink = 0; |
| 1503 | int newSourceLength = 0; |
| 1504 | tempsource = (UChar *)uprv_malloc(2*sourceLength*U_SIZEOF_UCHAR); |
| 1505 | if(tempsource == NULL) { |
| 1506 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
| 1510 | while ((i+=step) != end) { |
| 1511 | prevLink = currLink; |
| 1512 | currLink = getLink(source[i]); |
| 1513 | if (aggregate_tashkeel && ((prevLink|currLink)&COMBINE) == COMBINE && aggregation_possible) { |
| 1514 | aggregation_possible = 0; |
| 1515 | tempsource[j] = (prev<source[i]?prev:source[i])-0x064C+0xFC5E; |
| 1516 | currLink = getLink(tempsource[j]); |
| 1517 | } else { |
| 1518 | aggregation_possible = 1; |
| 1519 | tempsource[j+=step] = source[i]; |
| 1520 | prev = source[i]; |
| 1521 | newSourceLength++; |
| 1522 | } |
| 1523 | } |
| 1524 | source = tempsource+(logical_order?0:j); |
| 1525 | sourceLength = newSourceLength; |
| 1526 | } |
| 1527 | |
| 1528 | /* calculate destination size */ |
| 1529 | /* TODO: do we ever need to do this pure preflighting? */ |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1530 | if(((options&U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_RESIZE) || |
| 1531 | ((options&U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_RESIZE)) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1532 | outputSize=calculateSize(source,sourceLength,destCapacity,options); |
| 1533 | } else { |
| 1534 | outputSize=sourceLength; |
| 1535 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1536 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1537 | if(outputSize>destCapacity) { |
| 1538 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
| 1539 | if (tempsource != NULL) uprv_free(tempsource); |
| 1540 | return outputSize; |
| 1541 | } |
| 1542 | |
| 1543 | /* |
| 1544 | * need a temporary buffer of size max(outputSize, sourceLength) |
| 1545 | * because at first we copy source->temp |
| 1546 | */ |
| 1547 | if(sourceLength>outputSize) { |
| 1548 | outputSize=sourceLength; |
| 1549 | } |
| 1550 | |
| 1551 | /* Start of Arabic letter shaping part */ |
| 1552 | if(outputSize<=sizeof(buffer)/U_SIZEOF_UCHAR) { |
| 1553 | outputSize=sizeof(buffer)/U_SIZEOF_UCHAR; |
| 1554 | tempbuffer=buffer; |
| 1555 | } else { |
| 1556 | tempbuffer = (UChar *)uprv_malloc(outputSize*U_SIZEOF_UCHAR); |
| 1557 | |
| 1558 | /*Test for NULL*/ |
| 1559 | if(tempbuffer == NULL) { |
| 1560 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
| 1561 | if (tempsource != NULL) uprv_free(tempsource); |
| 1562 | return 0; |
| 1563 | } |
| 1564 | } |
| 1565 | uprv_memcpy(tempbuffer, source, sourceLength*U_SIZEOF_UCHAR); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1566 | if (tempsource != NULL){ |
| 1567 | uprv_free(tempsource); |
| 1568 | } |
| 1569 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1570 | if(sourceLength<outputSize) { |
| 1571 | uprv_memset(tempbuffer+sourceLength, 0, (outputSize-sourceLength)*U_SIZEOF_UCHAR); |
| 1572 | } |
| 1573 | |
| 1574 | if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) { |
| 1575 | countSpaces(tempbuffer,sourceLength,options,&spacesCountl,&spacesCountr); |
| 1576 | invertBuffer(tempbuffer,sourceLength,options,spacesCountl,spacesCountr); |
| 1577 | } |
| 1578 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1579 | if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_VISUAL_LTR) { |
| 1580 | if((options&U_SHAPE_SPACES_RELATIVE_TO_TEXT_MASK) == U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END) { |
| 1581 | spacesRelativeToTextBeginEnd = 1; |
| 1582 | uShapeLamalefBegin = U_SHAPE_LAMALEF_END; |
| 1583 | uShapeLamalefEnd = U_SHAPE_LAMALEF_BEGIN; |
| 1584 | |
| 1585 | uShapeTashkeelBegin = U_SHAPE_TASHKEEL_END; |
| 1586 | uShapeTashkeelEnd = U_SHAPE_TASHKEEL_BEGIN; |
| 1587 | } |
| 1588 | } |
| 1589 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1590 | switch(options&U_SHAPE_LETTERS_MASK) { |
| 1591 | case U_SHAPE_LETTERS_SHAPE : |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1592 | if( (options&U_SHAPE_TASHKEEL_MASK)> 0 |
| 1593 | && ((options&U_SHAPE_TASHKEEL_MASK) !=U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL)) { |
| 1594 | /* Call the shaping function with tashkeel flag == 2 for removal of tashkeel */ |
| 1595 | destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,2); |
| 1596 | }else { |
| 1597 | /* default Call the shaping function with tashkeel flag == 1 */ |
| 1598 | destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,1); |
| 1599 | |
| 1600 | /*After shaping text check if user wants to remove tashkeel and replace it with tatweel*/ |
| 1601 | if( (options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL){ |
| 1602 | destLength = handleTashkeelWithTatweel(tempbuffer,destLength,destCapacity,options,pErrorCode); |
| 1603 | } |
| 1604 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1605 | break; |
| 1606 | case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED : |
| 1607 | /* Call the shaping function with tashkeel flag == 0 */ |
| 1608 | destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,0); |
| 1609 | break; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame^] | 1610 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 1611 | case U_SHAPE_LETTERS_UNSHAPE : |
| 1612 | /* Call the deshaping function */ |
| 1613 | destLength = deShapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode); |
| 1614 | break; |
| 1615 | default : |
| 1616 | /* will never occur because of validity checks above */ |
| 1617 | destLength = 0; |
| 1618 | break; |
| 1619 | } |
| 1620 | |
| 1621 | /* |
| 1622 | * TODO: (markus 2002aug01) |
| 1623 | * For as long as we always preflight the outputSize above |
| 1624 | * we should U_ASSERT(outputSize==destLength) |
| 1625 | * except for the adjustment above before the tempbuffer allocation |
| 1626 | */ |
| 1627 | |
| 1628 | if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) { |
| 1629 | countSpaces(tempbuffer,destLength,options,&spacesCountl,&spacesCountr); |
| 1630 | invertBuffer(tempbuffer,destLength,options,spacesCountl,spacesCountr); |
| 1631 | } |
| 1632 | uprv_memcpy(dest, tempbuffer, uprv_min(destLength, destCapacity)*U_SIZEOF_UCHAR); |
| 1633 | |
| 1634 | if(tempbuffer!=buffer) { |
| 1635 | uprv_free(tempbuffer); |
| 1636 | } |
| 1637 | |
| 1638 | if(destLength>destCapacity) { |
| 1639 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
| 1640 | return destLength; |
| 1641 | } |
| 1642 | |
| 1643 | /* End of Arabic letter shaping part */ |
| 1644 | } else { |
| 1645 | /* |
| 1646 | * No letter shaping: |
| 1647 | * just make sure the destination is large enough and copy the string. |
| 1648 | */ |
| 1649 | if(destCapacity<sourceLength) { |
| 1650 | /* this catches preflighting, too */ |
| 1651 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
| 1652 | return sourceLength; |
| 1653 | } |
| 1654 | uprv_memcpy(dest, source, sourceLength*U_SIZEOF_UCHAR); |
| 1655 | destLength=sourceLength; |
| 1656 | } |
| 1657 | |
| 1658 | /* |
| 1659 | * Perform number shaping. |
| 1660 | * With UTF-16 or UTF-32, the length of the string is constant. |
| 1661 | * The easiest way to do this is to operate on the destination and |
| 1662 | * "shape" the digits in-place. |
| 1663 | */ |
| 1664 | if((options&U_SHAPE_DIGITS_MASK)!=U_SHAPE_DIGITS_NOOP) { |
| 1665 | UChar digitBase; |
| 1666 | int32_t i; |
| 1667 | |
| 1668 | /* select the requested digit group */ |
| 1669 | switch(options&U_SHAPE_DIGIT_TYPE_MASK) { |
| 1670 | case U_SHAPE_DIGIT_TYPE_AN: |
| 1671 | digitBase=0x660; /* Unicode: "Arabic-Indic digits" */ |
| 1672 | break; |
| 1673 | case U_SHAPE_DIGIT_TYPE_AN_EXTENDED: |
| 1674 | digitBase=0x6f0; /* Unicode: "Eastern Arabic-Indic digits (Persian and Urdu)" */ |
| 1675 | break; |
| 1676 | default: |
| 1677 | /* will never occur because of validity checks above */ |
| 1678 | digitBase=0; |
| 1679 | break; |
| 1680 | } |
| 1681 | |
| 1682 | /* perform the requested operation */ |
| 1683 | switch(options&U_SHAPE_DIGITS_MASK) { |
| 1684 | case U_SHAPE_DIGITS_EN2AN: |
| 1685 | /* add (digitBase-'0') to each European (ASCII) digit code point */ |
| 1686 | digitBase-=0x30; |
| 1687 | for(i=0; i<destLength; ++i) { |
| 1688 | if(((uint32_t)dest[i]-0x30)<10) { |
| 1689 | dest[i]+=digitBase; |
| 1690 | } |
| 1691 | } |
| 1692 | break; |
| 1693 | case U_SHAPE_DIGITS_AN2EN: |
| 1694 | /* subtract (digitBase-'0') from each Arabic digit code point */ |
| 1695 | for(i=0; i<destLength; ++i) { |
| 1696 | if(((uint32_t)dest[i]-(uint32_t)digitBase)<10) { |
| 1697 | dest[i]-=digitBase-0x30; |
| 1698 | } |
| 1699 | } |
| 1700 | break; |
| 1701 | case U_SHAPE_DIGITS_ALEN2AN_INIT_LR: |
| 1702 | _shapeToArabicDigitsWithContext(dest, destLength, |
| 1703 | digitBase, |
| 1704 | (UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL), |
| 1705 | FALSE); |
| 1706 | break; |
| 1707 | case U_SHAPE_DIGITS_ALEN2AN_INIT_AL: |
| 1708 | _shapeToArabicDigitsWithContext(dest, destLength, |
| 1709 | digitBase, |
| 1710 | (UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL), |
| 1711 | TRUE); |
| 1712 | break; |
| 1713 | default: |
| 1714 | /* will never occur because of validity checks above */ |
| 1715 | break; |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | return u_terminateUChars(dest, destCapacity, destLength, pErrorCode); |
| 1720 | } |