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