home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / common / ubidiimp.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-01  |  7.8 KB  |  221 lines

  1. /*  
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright International Business Machines Corporation, 1999           *
  6. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  7. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  8. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  9. *                                                                             *
  10. *******************************************************************************
  11. *   file name:  ubidiimp.h
  12. *   encoding:   US-ASCII
  13. *   tab size:   8 (not used)
  14. *   indentation:4
  15. *
  16. *   created on: 1999aug06
  17. *   created by: Markus W. Scherer
  18. */
  19.  
  20. #ifndef UBIDIIMP_H
  21. #define UBIDIIMP_H
  22.  
  23. /* set import/export definitions */
  24. #ifdef U_COMMON_IMPLEMENTATION
  25.  
  26. #include "utypes.h"
  27. #include "uchar.h"
  28.  
  29. /* miscellaneous definitions ------------------------------------------------ */
  30.  
  31. typedef uint8_t DirProp;
  32. typedef uint32_t Flags;
  33.  
  34. /*  Comparing the description of the BiDi algorithm with this implementation
  35.     is easier with the same names for the BiDi types in the code as there.
  36.     See UCharDirection in uchar.h .
  37. */
  38. enum { 
  39.     L=  U_LEFT_TO_RIGHT,
  40.     R=  U_RIGHT_TO_LEFT,
  41.     EN= U_EUROPEAN_NUMBER,
  42.     ES= U_EUROPEAN_NUMBER_SEPARATOR,
  43.     ET= U_EUROPEAN_NUMBER_TERMINATOR,
  44.     AN= U_ARABIC_NUMBER,
  45.     CS= U_COMMON_NUMBER_SEPARATOR,
  46.     B=  U_BLOCK_SEPARATOR,
  47.     S=  U_SEGMENT_SEPARATOR,
  48.     WS= U_WHITE_SPACE_NEUTRAL,
  49.     ON= U_OTHER_NEUTRAL,
  50.     LRE=U_LEFT_TO_RIGHT_EMBEDDING,
  51.     LRO=U_LEFT_TO_RIGHT_OVERRIDE,
  52.     AL= U_RIGHT_TO_LEFT_ARABIC,
  53.     RLE=U_RIGHT_TO_LEFT_EMBEDDING,
  54.     RLO=U_RIGHT_TO_LEFT_OVERRIDE,
  55.     PDF=U_POP_DIRECTIONAL_FORMAT,
  56.     NSM=U_DIR_NON_SPACING_MARK,
  57.     BN= U_BOUNDARY_NEUTRAL,
  58.     dirPropCount
  59. };
  60.  
  61. /*
  62.  * Sometimes, bit values are more appropriate
  63.  * to deal with directionality properties.
  64.  * Abbreviations in these macro names refer to names
  65.  * used in the BiDi algorithm.
  66.  */
  67. #define DIRPROP_FLAG(dir) (1UL<<(dir))
  68.  
  69. /* special flag for multiple runs from explicit embedding codes */
  70. #define DIRPROP_FLAG_MULTI_RUNS (1UL<<31)
  71.  
  72. /* are there any characters that are LTR or RTL? */
  73. #define MASK_LTR (DIRPROP_FLAG(L)|DIRPROP_FLAG(EN)|DIRPROP_FLAG(AN)|DIRPROP_FLAG(LRE)|DIRPROP_FLAG(LRO))
  74. #define MASK_RTL (DIRPROP_FLAG(R)|DIRPROP_FLAG(AL)|DIRPROP_FLAG(RLE)|DIRPROP_FLAG(RLO))
  75.  
  76. /* explicit embedding codes */
  77. #define MASK_LRX (DIRPROP_FLAG(LRE)|DIRPROP_FLAG(LRO))
  78. #define MASK_RLX (DIRPROP_FLAG(RLE)|DIRPROP_FLAG(RLO))
  79. #define MASK_OVERRIDE (DIRPROP_FLAG(LRO)|DIRPROP_FLAG(RLO))
  80.  
  81. #define MASK_EXPLICIT (MASK_LRX|MASK_RLX|DIRPROP_FLAG(PDF))
  82. #define MASK_BN_EXPLICIT (DIRPROP_FLAG(BN)|MASK_EXPLICIT)
  83.  
  84. /* paragraph and segment separators */
  85. #define MASK_B_S (DIRPROP_FLAG(B)|DIRPROP_FLAG(S))
  86.  
  87. /* all types that are counted as White Space or Neutral in some steps */
  88. #define MASK_WS (MASK_B_S|DIRPROP_FLAG(WS)|MASK_BN_EXPLICIT)
  89. #define MASK_N (DIRPROP_FLAG(ON)|MASK_WS)
  90.  
  91. /* all types that are included in a sequence of European Terminators for (W5) */
  92. #define MASK_ET_NSM_BN (DIRPROP_FLAG(ET)|DIRPROP_FLAG(NSM)|MASK_BN_EXPLICIT)
  93.  
  94. /* types that are neutrals or could becomes neutrals in (Wn) */
  95. #define MASK_POSSIBLE_N (DIRPROP_FLAG(CS)|DIRPROP_FLAG(ES)|DIRPROP_FLAG(ET)|MASK_N)
  96.  
  97. /*
  98.  * These types may be changed to "e",
  99.  * the embedding type (L or R) of the run,
  100.  * in the BiDi algorithm (N2)
  101.  */
  102. #define MASK_EMBEDDING (DIRPROP_FLAG(NSM)|MASK_POSSIBLE_N)
  103.  
  104. /* to avoid some conditional statements, use tiny constant arrays */
  105. static Flags flagLR[2]={ DIRPROP_FLAG(L), DIRPROP_FLAG(R) };
  106. static Flags flagE[2]={ DIRPROP_FLAG(LRE), DIRPROP_FLAG(RLE) };
  107. static Flags flagO[2]={ DIRPROP_FLAG(LRO), DIRPROP_FLAG(RLO) };
  108.  
  109. #define DIRPROP_FLAG_LR(level) flagLR[(level)&1]
  110. #define DIRPROP_FLAG_E(level) flagE[(level)&1]
  111. #define DIRPROP_FLAG_O(level) flagO[(level)&1]
  112.  
  113. /* the dirProp's L and R are defined to 0 and 1 values in UCharDirection */
  114. #define GET_LR_FROM_LEVEL(level) ((DirProp)((level)&1))
  115.  
  116. #define IS_DEFAULT_LEVEL(level) (((level)&0xfe)==0xfe)
  117.  
  118. /* handle surrogate pairs --------------------------------------------------- */
  119.  
  120. #define IS_FIRST_SURROGATE(uchar) (((uchar)&0xfc00)==0xd800)
  121. #define IS_SECOND_SURROGATE(uchar) (((uchar)&0xfc00)==0xdc00)
  122.  
  123. /* get the UTF-32 value directly from the surrogate pseudo-characters */
  124. #define SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
  125. #define GET_UTF_32(first, second) (((first)<<10UL)+(second)-SURROGATE_OFFSET)
  126.  
  127. /* Run structure for reordering --------------------------------------------- */
  128.  
  129. typedef struct Run {
  130.     UTextOffset logicalStart,   /* first character of the run; b31 indicates even/odd level */
  131.                 visualLimit;    /* last visual position of the run +1 */
  132. } Run;
  133.  
  134. /* in a Run, logicalStart will get this bit set if the run level is odd */
  135. #define INDEX_ODD_BIT (1UL<<31)
  136.  
  137. #define MAKE_INDEX_ODD_PAIR(index, level) (index|((uint32_t)level<<31))
  138. #define ADD_ODD_BIT_FROM_LEVEL(x, level)  ((x)|=((uint32_t)level<<31))
  139. #define REMOVE_ODD_BIT(x)                 ((x)&=~INDEX_ODD_BIT)
  140.  
  141. #define GET_INDEX(x)   (x&~INDEX_ODD_BIT)
  142. #define GET_ODD_BIT(x) ((uint32_t)x>>31)
  143. #define IS_ODD_RUN(x)  ((x&INDEX_ODD_BIT)!=0)
  144. #define IS_EVEN_RUN(x) ((x&INDEX_ODD_BIT)==0)
  145.  
  146. /* UBiDi structure ----------------------------------------------------------- */
  147.  
  148. struct UBiDi {
  149.     /* length of the current text */
  150.     UTextOffset length;
  151.  
  152.     /* memory sizes in bytes */
  153.     UTextOffset dirPropsSize, levelsSize, runsSize;
  154.  
  155.     /* allocated memory */
  156.     DirProp *dirPropsMemory;
  157.     UBiDiLevel *levelsMemory;
  158.     Run *runsMemory;
  159.  
  160.     /* indicators for whether memory may be allocated after ubidi_open() */
  161.     bool_t mayAllocateText, mayAllocateRuns;
  162.  
  163.     /* arrays with one value per text-character */
  164.     const DirProp *dirProps;
  165.     UBiDiLevel *levels;
  166.  
  167.     /* the paragraph level */
  168.     UBiDiLevel paraLevel;
  169.  
  170.     /* flags is a bit set for which directional properties are in the text */
  171.     Flags flags;
  172.  
  173.     /* the overall paragraph or line directionality - see UBiDiDirection */
  174.     UBiDiDirection direction;
  175.  
  176.     /* characters after trailingWSStart are WS and are */
  177.     /* implicitly at the paraLevel (rule (L1)) - levels may not reflect that */
  178.     UTextOffset trailingWSStart;
  179.  
  180.     /* fields for line reordering */
  181.     UTextOffset runCount;     /* ==-1: runs not set up yet */
  182.     Run *runs;
  183.  
  184.     /* for non-mixed text, we only need a tiny array of runs (no malloc()) */
  185.     Run simpleRuns[1];
  186. };
  187.  
  188. /* helper function to (re)allocate memory if allowed */
  189. extern bool_t
  190. getMemory(void **pMemory, UTextOffset *pSize, bool_t mayAllocate, UTextOffset sizeNeeded);
  191.  
  192. /* helper macros for each allocated array in UBiDi */
  193. #define getDirPropsMemory(pBiDi, length) \
  194.         getMemory((void **)&(pBiDi)->dirPropsMemory, &(pBiDi)->dirPropsSize, \
  195.                         (pBiDi)->mayAllocateText, (length))
  196.  
  197. #define getLevelsMemory(pBiDi, length) \
  198.         getMemory((void **)&(pBiDi)->levelsMemory, &(pBiDi)->levelsSize, \
  199.                         (pBiDi)->mayAllocateText, (length))
  200.  
  201. #define getRunsMemory(pBiDi, length) \
  202.         getMemory((void **)&(pBiDi)->runsMemory, &(pBiDi)->runsSize, \
  203.                         (pBiDi)->mayAllocateRuns, (length)*sizeof(Run))
  204.  
  205. /* additional macros used by ubidi_open() - always allow allocation */
  206. #define getInitialDirPropsMemory(pBiDi, length) \
  207.         getMemory((void **)&(pBiDi)->dirPropsMemory, &(pBiDi)->dirPropsSize, \
  208.                         TRUE, (length))
  209.  
  210. #define getInitialLevelsMemory(pBiDi, length) \
  211.         getMemory((void **)&(pBiDi)->levelsMemory, &(pBiDi)->levelsSize, \
  212.                         TRUE, (length))
  213.  
  214. #define getInitialRunsMemory(pBiDi, length) \
  215.         getMemory((void **)&(pBiDi)->runsMemory, &(pBiDi)->runsSize, \
  216.                         TRUE, (length)*sizeof(Run))
  217.  
  218. #endif
  219.  
  220. #endif
  221.