| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#ifndef UBIDIIMP_H |
| 20 |
#define UBIDIIMP_H |
| 21 |
|
| 22 |
#include "unicode/utypes.h" |
| 23 |
#include "unicode/ubidi.h" |
| 24 |
#include "unicode/uchar.h" |
| 25 |
#include "ubidi_props.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#ifdef CS |
| 31 |
# undef CS |
| 32 |
#endif |
| 33 |
#ifdef ES |
| 34 |
# undef ES |
| 35 |
#endif |
| 36 |
|
| 37 |
typedef uint8_t DirProp; |
| 38 |
typedef uint32_t Flags; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
enum { |
| 45 |
L= U_LEFT_TO_RIGHT, |
| 46 |
R= U_RIGHT_TO_LEFT, |
| 47 |
EN= U_EUROPEAN_NUMBER, |
| 48 |
ES= U_EUROPEAN_NUMBER_SEPARATOR, |
| 49 |
ET= U_EUROPEAN_NUMBER_TERMINATOR, |
| 50 |
AN= U_ARABIC_NUMBER, |
| 51 |
CS= U_COMMON_NUMBER_SEPARATOR, |
| 52 |
B= U_BLOCK_SEPARATOR, |
| 53 |
S= U_SEGMENT_SEPARATOR, |
| 54 |
WS= U_WHITE_SPACE_NEUTRAL, |
| 55 |
ON= U_OTHER_NEUTRAL, |
| 56 |
LRE=U_LEFT_TO_RIGHT_EMBEDDING, |
| 57 |
LRO=U_LEFT_TO_RIGHT_OVERRIDE, |
| 58 |
AL= U_RIGHT_TO_LEFT_ARABIC, |
| 59 |
RLE=U_RIGHT_TO_LEFT_EMBEDDING, |
| 60 |
RLO=U_RIGHT_TO_LEFT_OVERRIDE, |
| 61 |
PDF=U_POP_DIRECTIONAL_FORMAT, |
| 62 |
NSM=U_DIR_NON_SPACING_MARK, |
| 63 |
BN= U_BOUNDARY_NEUTRAL, |
| 64 |
FSI=U_FIRST_STRONG_ISOLATE, |
| 65 |
LRI=U_LEFT_TO_RIGHT_ISOLATE, |
| 66 |
RLI=U_RIGHT_TO_LEFT_ISOLATE, |
| 67 |
PDI=U_POP_DIRECTIONAL_ISOLATE, |
| 68 |
ENL, |
| 69 |
ENR, |
| 70 |
dirPropCount |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
#define DIRPROP_FLAG(dir) (1UL<<(dir)) |
| 79 |
#define PURE_DIRPROP(prop) ((prop)&~0xE0) ????????????????????????? |
| 80 |
|
| 81 |
|
| 82 |
#define DIRPROP_FLAG_MULTI_RUNS (1UL<<31) |
| 83 |
|
| 84 |
|
| 85 |
#define MASK_LTR (DIRPROP_FLAG(L)|DIRPROP_FLAG(EN)|DIRPROP_FLAG(ENL)|DIRPROP_FLAG(ENR)|DIRPROP_FLAG(AN)|DIRPROP_FLAG(LRE)|DIRPROP_FLAG(LRO)|DIRPROP_FLAG(LRI)) |
| 86 |
#define MASK_RTL (DIRPROP_FLAG(R)|DIRPROP_FLAG(AL)|DIRPROP_FLAG(RLE)|DIRPROP_FLAG(RLO)|DIRPROP_FLAG(RLI)) |
| 87 |
#define MASK_R_AL (DIRPROP_FLAG(R)|DIRPROP_FLAG(AL)) |
| 88 |
#define MASK_STRONG_EN_AN (DIRPROP_FLAG(L)|DIRPROP_FLAG(R)|DIRPROP_FLAG(AL)|DIRPROP_FLAG(EN)|DIRPROP_FLAG(AN)) |
| 89 |
|
| 90 |
|
| 91 |
#define MASK_EXPLICIT (DIRPROP_FLAG(LRE)|DIRPROP_FLAG(LRO)|DIRPROP_FLAG(RLE)|DIRPROP_FLAG(RLO)|DIRPROP_FLAG(PDF)) |
| 92 |
|
| 93 |
|
| 94 |
#define MASK_ISO (DIRPROP_FLAG(LRI)|DIRPROP_FLAG(RLI)|DIRPROP_FLAG(FSI)|DIRPROP_FLAG(PDI)) |
| 95 |
|
| 96 |
#define MASK_BN_EXPLICIT (DIRPROP_FLAG(BN)|MASK_EXPLICIT) |
| 97 |
|
| 98 |
|
| 99 |
#define MASK_B_S (DIRPROP_FLAG(B)|DIRPROP_FLAG(S)) |
| 100 |
|
| 101 |
|
| 102 |
#define MASK_WS (MASK_B_S|DIRPROP_FLAG(WS)|MASK_BN_EXPLICIT|MASK_ISO) |
| 103 |
|
| 104 |
|
| 105 |
#define MASK_POSSIBLE_N (DIRPROP_FLAG(ON)|DIRPROP_FLAG(CS)|DIRPROP_FLAG(ES)|DIRPROP_FLAG(ET)|MASK_WS) |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
#define MASK_EMBEDDING (DIRPROP_FLAG(NSM)|MASK_POSSIBLE_N) |
| 113 |
|
| 114 |
|
| 115 |
#define GET_LR_FROM_LEVEL(level) ((DirProp)((level)&1)) |
| 116 |
|
| 117 |
#define IS_DEFAULT_LEVEL(level) ((level)>=0xfe) |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
#define ISOLATE 0x0100 |
| 124 |
|
| 125 |
U_CFUNC UBiDiLevel |
| 126 |
ubidi_getParaLevelAtIndex(const UBiDi *pBiDi, int32_t index); |
| 127 |
|
| 128 |
#define GET_PARALEVEL(ubidi, index) \ |
| 129 |
((UBiDiLevel)(!(ubidi)->defaultParaLevel || (index)<(ubidi)->paras[0].limit ? \ |
| 130 |
(ubidi)->paraLevel : ubidi_getParaLevelAtIndex((ubidi), (index)))) |
| 131 |
|
| 132 |
|
| 133 |
#define SIMPLE_PARAS_COUNT 10 |
| 134 |
|
| 135 |
#define SIMPLE_ISOLATES_COUNT 5 |
| 136 |
|
| 137 |
#define SIMPLE_OPENINGS_COUNT 20 |
| 138 |
|
| 139 |
#define CR 0x000D |
| 140 |
#define LF 0x000A |
| 141 |
|
| 142 |
|
| 143 |
enum { |
| 144 |
LRM_BEFORE=1, |
| 145 |
LRM_AFTER=2, |
| 146 |
RLM_BEFORE=4, |
| 147 |
RLM_AFTER=8 |
| 148 |
}; |
| 149 |
|
| 150 |
typedef struct Para { |
| 151 |
int32_t limit; |
| 152 |
int32_t level; |
| 153 |
} Para; |
| 154 |
|
| 155 |
enum { |
| 156 |
FOUND_L=DIRPROP_FLAG(L), |
| 157 |
FOUND_R=DIRPROP_FLAG(R) |
| 158 |
}; |
| 159 |
|
| 160 |
typedef struct Opening { |
| 161 |
int32_t position; |
| 162 |
int32_t match; |
| 163 |
int32_t contextPos; |
| 164 |
uint16_t flags; |
| 165 |
UBiDiDirection contextDir; |
| 166 |
uint8_t filler; |
| 167 |
} Opening; |
| 168 |
|
| 169 |
typedef struct IsoRun { |
| 170 |
int32_t contextPos; |
| 171 |
uint16_t start; |
| 172 |
uint16_t limit; |
| 173 |
UBiDiLevel level; |
| 174 |
DirProp lastStrong; |
| 175 |
DirProp lastBase; |
| 176 |
UBiDiDirection contextDir; |
| 177 |
} IsoRun; |
| 178 |
|
| 179 |
typedef struct BracketData { |
| 180 |
UBiDi *pBiDi; |
| 181 |
|
| 182 |
Opening simpleOpenings[SIMPLE_OPENINGS_COUNT]; |
| 183 |
Opening *openings; |
| 184 |
int32_t openingsCount; |
| 185 |
int32_t isoRunLast; |
| 186 |
|
| 187 |
|
| 188 |
IsoRun isoRuns[UBIDI_MAX_EXPLICIT_LEVEL+2]; |
| 189 |
UBool isNumbersSpecial; |
| 190 |
} BracketData; |
| 191 |
|
| 192 |
typedef struct Isolate { |
| 193 |
int32_t startON; |
| 194 |
int32_t start1; |
| 195 |
int32_t state; |
| 196 |
int16_t stateImp; |
| 197 |
} Isolate; |
| 198 |
|
| 199 |
typedef struct Run { |
| 200 |
int32_t logicalStart, |
| 201 |
visualLimit, |
| 202 |
insertRemove; |
| 203 |
|
| 204 |
} Run; |
| 205 |
|
| 206 |
|
| 207 |
#define INDEX_ODD_BIT (1UL<<31) |
| 208 |
|
| 209 |
#define MAKE_INDEX_ODD_PAIR(index, level) ((index)|((int32_t)((level)&1)<<31)) |
| 210 |
#define ADD_ODD_BIT_FROM_LEVEL(x, level) ((x)|=((int32_t)((level)&1)<<31)) |
| 211 |
#define REMOVE_ODD_BIT(x) ((x)&=~INDEX_ODD_BIT) |
| 212 |
|
| 213 |
#define GET_INDEX(x) ((x)&~INDEX_ODD_BIT) |
| 214 |
#define GET_ODD_BIT(x) ((uint32_t)(x)>>31) |
| 215 |
#define IS_ODD_RUN(x) ((UBool)(((x)&INDEX_ODD_BIT)!=0)) |
| 216 |
#define IS_EVEN_RUN(x) ((UBool)(((x)&INDEX_ODD_BIT)==0)) |
| 217 |
|
| 218 |
U_CFUNC UBool |
| 219 |
ubidi_getRuns(UBiDi *pBiDi, UErrorCode *pErrorCode); |
| 220 |
|
| 221 |
|
| 222 |
enum { |
| 223 |
ZWNJ_CHAR=0x200c, |
| 224 |
ZWJ_CHAR, |
| 225 |
LRM_CHAR, |
| 226 |
RLM_CHAR, |
| 227 |
LRE_CHAR=0x202a, |
| 228 |
RLE_CHAR, |
| 229 |
PDF_CHAR, |
| 230 |
LRO_CHAR, |
| 231 |
RLO_CHAR, |
| 232 |
LRI_CHAR=0x2066, |
| 233 |
RLI_CHAR, |
| 234 |
FSI_CHAR, |
| 235 |
PDI_CHAR |
| 236 |
}; |
| 237 |
|
| 238 |
#define IS_BIDI_CONTROL_CHAR(c) (((uint32_t)(c)&0xfffffffc)==ZWNJ_CHAR || (uint32_t)((c)-LRE_CHAR)<5 || (uint32_t)((c)-LRI_CHAR)<4) |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
typedef struct Point { |
| 243 |
int32_t pos; |
| 244 |
int32_t flag; |
| 245 |
} Point; |
| 246 |
|
| 247 |
typedef struct InsertPoints { |
| 248 |
int32_t capacity; |
| 249 |
int32_t size; |
| 250 |
int32_t confirmed; |
| 251 |
UErrorCode errorCode; |
| 252 |
Point *points; |
| 253 |
} InsertPoints; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
struct UBiDi { |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
const UBiDi * pParaBiDi; |
| 264 |
|
| 265 |
|
| 266 |
const UChar *text; |
| 267 |
|
| 268 |
|
| 269 |
int32_t originalLength; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
int32_t length; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
int32_t resultLength; |
| 283 |
|
| 284 |
|
| 285 |
int32_t dirPropsSize, levelsSize, openingsSize, parasSize, runsSize, isolatesSize; |
| 286 |
|
| 287 |
|
| 288 |
DirProp *dirPropsMemory; |
| 289 |
UBiDiLevel *levelsMemory; |
| 290 |
Opening *openingsMemory; |
| 291 |
Para *parasMemory; |
| 292 |
Run *runsMemory; |
| 293 |
Isolate *isolatesMemory; |
| 294 |
|
| 295 |
|
| 296 |
UBool mayAllocateText, mayAllocateRuns; |
| 297 |
|
| 298 |
|
| 299 |
DirProp *dirProps; |
| 300 |
UBiDiLevel *levels; |
| 301 |
|
| 302 |
|
| 303 |
UBool isInverse; |
| 304 |
|
| 305 |
|
| 306 |
UBiDiReorderingMode reorderingMode; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
#define UBIDI_REORDER_LAST_LOGICAL_TO_VISUAL UBIDI_REORDER_NUMBERS_SPECIAL |
| 313 |
|
| 314 |
|
| 315 |
uint32_t reorderingOptions; |
| 316 |
|
| 317 |
|
| 318 |
UBool orderParagraphsLTR; |
| 319 |
|
| 320 |
|
| 321 |
UBiDiLevel paraLevel; |
| 322 |
|
| 323 |
|
| 324 |
UBiDiLevel defaultParaLevel; |
| 325 |
|
| 326 |
|
| 327 |
const UChar *prologue; |
| 328 |
int32_t proLength; |
| 329 |
const UChar *epilogue; |
| 330 |
int32_t epiLength; |
| 331 |
|
| 332 |
|
| 333 |
const struct ImpTabPair * pImpTabPair; |
| 334 |
|
| 335 |
|
| 336 |
UBiDiDirection direction; |
| 337 |
|
| 338 |
|
| 339 |
Flags flags; |
| 340 |
|
| 341 |
|
| 342 |
int32_t lastArabicPos; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
int32_t trailingWSStart; |
| 347 |
|
| 348 |
|
| 349 |
int32_t paraCount; |
| 350 |
|
| 351 |
Para *paras; |
| 352 |
|
| 353 |
|
| 354 |
Para simpleParas[SIMPLE_PARAS_COUNT]; |
| 355 |
|
| 356 |
|
| 357 |
int32_t runCount; |
| 358 |
Run *runs; |
| 359 |
|
| 360 |
|
| 361 |
Run simpleRuns[1]; |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
int32_t isolateCount; |
| 369 |
Isolate *isolates; |
| 370 |
|
| 371 |
|
| 372 |
Isolate simpleIsolates[SIMPLE_ISOLATES_COUNT]; |
| 373 |
|
| 374 |
|
| 375 |
InsertPoints insertPoints; |
| 376 |
|
| 377 |
|
| 378 |
int32_t controlCount; |
| 379 |
|
| 380 |
|
| 381 |
UBiDiClassCallback *fnClassCallback; |
| 382 |
const void *coClassCallback; |
| 383 |
}; |
| 384 |
|
| 385 |
#define IS_VALID_PARA(x) ((x) && ((x)->pParaBiDi==(x))) |
| 386 |
#define IS_VALID_PARA_OR_LINE(x) ((x) && ((x)->pParaBiDi==(x) || (((x)->pParaBiDi) && (x)->pParaBiDi->pParaBiDi==(x)->pParaBiDi))) |
| 387 |
|
| 388 |
typedef union { |
| 389 |
DirProp *dirPropsMemory; |
| 390 |
UBiDiLevel *levelsMemory; |
| 391 |
Opening *openingsMemory; |
| 392 |
Para *parasMemory; |
| 393 |
Run *runsMemory; |
| 394 |
Isolate *isolatesMemory; |
| 395 |
} BidiMemoryForAllocation; |
| 396 |
|
| 397 |
|
| 398 |
#define RETURN_IF_NULL_OR_FAILING_ERRCODE(pErrcode, retvalue) UPRV_BLOCK_MACRO_BEGIN { \ |
| 399 |
if((pErrcode)==NULL || U_FAILURE(*pErrcode)) return retvalue; \ |
| 400 |
} UPRV_BLOCK_MACRO_END |
| 401 |
#define RETURN_IF_NOT_VALID_PARA(bidi, errcode, retvalue) UPRV_BLOCK_MACRO_BEGIN { \ |
| 402 |
if(!IS_VALID_PARA(bidi)) { \ |
| 403 |
errcode=U_INVALID_STATE_ERROR; \ |
| 404 |
return retvalue; \ |
| 405 |
} \ |
| 406 |
} UPRV_BLOCK_MACRO_END |
| 407 |
#define RETURN_IF_NOT_VALID_PARA_OR_LINE(bidi, errcode, retvalue) UPRV_BLOCK_MACRO_BEGIN { \ |
| 408 |
if(!IS_VALID_PARA_OR_LINE(bidi)) { \ |
| 409 |
errcode=U_INVALID_STATE_ERROR; \ |
| 410 |
return retvalue; \ |
| 411 |
} \ |
| 412 |
} UPRV_BLOCK_MACRO_END |
| 413 |
#define RETURN_IF_BAD_RANGE(arg, start, limit, errcode, retvalue) UPRV_BLOCK_MACRO_BEGIN { \ |
| 414 |
if((arg)<(start) || (arg)>=(limit)) { \ |
| 415 |
(errcode)=U_ILLEGAL_ARGUMENT_ERROR; \ |
| 416 |
return retvalue; \ |
| 417 |
} \ |
| 418 |
} UPRV_BLOCK_MACRO_END |
| 419 |
|
| 420 |
#define RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrcode) UPRV_BLOCK_MACRO_BEGIN { \ |
| 421 |
if((pErrcode)==NULL || U_FAILURE(*pErrcode)) return; \ |
| 422 |
} UPRV_BLOCK_MACRO_END |
| 423 |
#define RETURN_VOID_IF_NOT_VALID_PARA(bidi, errcode) UPRV_BLOCK_MACRO_BEGIN { \ |
| 424 |
if(!IS_VALID_PARA(bidi)) { \ |
| 425 |
errcode=U_INVALID_STATE_ERROR; \ |
| 426 |
return; \ |
| 427 |
} \ |
| 428 |
} UPRV_BLOCK_MACRO_END |
| 429 |
#define RETURN_VOID_IF_NOT_VALID_PARA_OR_LINE(bidi, errcode) UPRV_BLOCK_MACRO_BEGIN { \ |
| 430 |
if(!IS_VALID_PARA_OR_LINE(bidi)) { \ |
| 431 |
errcode=U_INVALID_STATE_ERROR; \ |
| 432 |
return; \ |
| 433 |
} \ |
| 434 |
} UPRV_BLOCK_MACRO_END |
| 435 |
#define RETURN_VOID_IF_BAD_RANGE(arg, start, limit, errcode) UPRV_BLOCK_MACRO_BEGIN { \ |
| 436 |
if((arg)<(start) || (arg)>=(limit)) { \ |
| 437 |
(errcode)=U_ILLEGAL_ARGUMENT_ERROR; \ |
| 438 |
return; \ |
| 439 |
} \ |
| 440 |
} UPRV_BLOCK_MACRO_END |
| 441 |
|
| 442 |
|
| 443 |
U_CFUNC UBool |
| 444 |
ubidi_getMemory(BidiMemoryForAllocation *pMemory, int32_t *pSize, UBool mayAllocate, int32_t sizeNeeded); |
| 445 |
|
| 446 |
|
| 447 |
#define getDirPropsMemory(pBiDi, length) \ |
| 448 |
ubidi_getMemory((BidiMemoryForAllocation *)&(pBiDi)->dirPropsMemory, &(pBiDi)->dirPropsSize, \ |
| 449 |
(pBiDi)->mayAllocateText, (length)) |
| 450 |
|
| 451 |
#define getLevelsMemory(pBiDi, length) \ |
| 452 |
ubidi_getMemory((BidiMemoryForAllocation *)&(pBiDi)->levelsMemory, &(pBiDi)->levelsSize, \ |
| 453 |
(pBiDi)->mayAllocateText, (length)) |
| 454 |
|
| 455 |
#define getRunsMemory(pBiDi, length) \ |
| 456 |
ubidi_getMemory((BidiMemoryForAllocation *)&(pBiDi)->runsMemory, &(pBiDi)->runsSize, \ |
| 457 |
(pBiDi)->mayAllocateRuns, (length)*sizeof(Run)) |
| 458 |
|
| 459 |
|
| 460 |
#define getInitialDirPropsMemory(pBiDi, length) \ |
| 461 |
ubidi_getMemory((BidiMemoryForAllocation *)&(pBiDi)->dirPropsMemory, &(pBiDi)->dirPropsSize, \ |
| 462 |
true, (length)) |
| 463 |
|
| 464 |
#define getInitialLevelsMemory(pBiDi, length) \ |
| 465 |
ubidi_getMemory((BidiMemoryForAllocation *)&(pBiDi)->levelsMemory, &(pBiDi)->levelsSize, \ |
| 466 |
true, (length)) |
| 467 |
|
| 468 |
#define getInitialOpeningsMemory(pBiDi, length) \ |
| 469 |
ubidi_getMemory((BidiMemoryForAllocation *)&(pBiDi)->openingsMemory, &(pBiDi)->openingsSize, \ |
| 470 |
true, (length)*sizeof(Opening)) |
| 471 |
|
| 472 |
#define getInitialParasMemory(pBiDi, length) \ |
| 473 |
ubidi_getMemory((BidiMemoryForAllocation *)&(pBiDi)->parasMemory, &(pBiDi)->parasSize, \ |
| 474 |
true, (length)*sizeof(Para)) |
| 475 |
|
| 476 |
#define getInitialRunsMemory(pBiDi, length) \ |
| 477 |
ubidi_getMemory((BidiMemoryForAllocation *)&(pBiDi)->runsMemory, &(pBiDi)->runsSize, \ |
| 478 |
true, (length)*sizeof(Run)) |
| 479 |
|
| 480 |
#define getInitialIsolatesMemory(pBiDi, length) \ |
| 481 |
ubidi_getMemory((BidiMemoryForAllocation *)&(pBiDi)->isolatesMemory, &(pBiDi)->isolatesSize, \ |
| 482 |
true, (length)*sizeof(Isolate)) |
| 483 |
|
| 484 |
#endif |