intl/icu/source/common/ubidi_props.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/ubidi_props.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,248 @@
     1.4 +/*
     1.5 +*******************************************************************************
     1.6 +*
     1.7 +*   Copyright (C) 2004-2013, International Business Machines
     1.8 +*   Corporation and others.  All Rights Reserved.
     1.9 +*
    1.10 +*******************************************************************************
    1.11 +*   file name:  ubidi_props.c
    1.12 +*   encoding:   US-ASCII
    1.13 +*   tab size:   8 (not used)
    1.14 +*   indentation:4
    1.15 +*
    1.16 +*   created on: 2004dec30
    1.17 +*   created by: Markus W. Scherer
    1.18 +*
    1.19 +*   Low-level Unicode bidi/shaping properties access.
    1.20 +*/
    1.21 +
    1.22 +#include "unicode/utypes.h"
    1.23 +#include "unicode/uset.h"
    1.24 +#include "unicode/udata.h" /* UDataInfo */
    1.25 +#include "ucmndata.h" /* DataHeader */
    1.26 +#include "udatamem.h"
    1.27 +#include "uassert.h"
    1.28 +#include "cmemory.h"
    1.29 +#include "utrie2.h"
    1.30 +#include "ubidi_props.h"
    1.31 +#include "ucln_cmn.h"
    1.32 +
    1.33 +struct UBiDiProps {
    1.34 +    UDataMemory *mem;
    1.35 +    const int32_t *indexes;
    1.36 +    const uint32_t *mirrors;
    1.37 +    const uint8_t *jgArray;
    1.38 +
    1.39 +    UTrie2 trie;
    1.40 +    uint8_t formatVersion[4];
    1.41 +};
    1.42 +
    1.43 +/* ubidi_props_data.h is machine-generated by genbidi --csource */
    1.44 +#define INCLUDED_FROM_UBIDI_PROPS_C
    1.45 +#include "ubidi_props_data.h"
    1.46 +
    1.47 +/* UBiDiProps singleton ----------------------------------------------------- */
    1.48 +
    1.49 +U_CFUNC const UBiDiProps *
    1.50 +ubidi_getSingleton() {
    1.51 +    return &ubidi_props_singleton;
    1.52 +}
    1.53 +
    1.54 +/* set of property starts for UnicodeSet ------------------------------------ */
    1.55 +
    1.56 +static UBool U_CALLCONV
    1.57 +_enumPropertyStartsRange(const void *context, UChar32 start, UChar32 end, uint32_t value) {
    1.58 +    /* add the start code point to the USet */
    1.59 +    const USetAdder *sa=(const USetAdder *)context;
    1.60 +    sa->add(sa->set, start);
    1.61 +    return TRUE;
    1.62 +}
    1.63 +
    1.64 +U_CFUNC void
    1.65 +ubidi_addPropertyStarts(const UBiDiProps *bdp, const USetAdder *sa, UErrorCode *pErrorCode) {
    1.66 +    int32_t i, length;
    1.67 +    UChar32 c, start, limit;
    1.68 +
    1.69 +    const uint8_t *jgArray;
    1.70 +    uint8_t prev, jg;
    1.71 +
    1.72 +    if(U_FAILURE(*pErrorCode)) {
    1.73 +        return;
    1.74 +    }
    1.75 +
    1.76 +    /* add the start code point of each same-value range of the trie */
    1.77 +    utrie2_enum(&bdp->trie, NULL, _enumPropertyStartsRange, sa);
    1.78 +
    1.79 +    /* add the code points from the bidi mirroring table */
    1.80 +    length=bdp->indexes[UBIDI_IX_MIRROR_LENGTH];
    1.81 +    for(i=0; i<length; ++i) {
    1.82 +        c=UBIDI_GET_MIRROR_CODE_POINT(bdp->mirrors[i]);
    1.83 +        sa->addRange(sa->set, c, c+1);
    1.84 +    }
    1.85 +
    1.86 +    /* add the code points from the Joining_Group array where the value changes */
    1.87 +    start=bdp->indexes[UBIDI_IX_JG_START];
    1.88 +    limit=bdp->indexes[UBIDI_IX_JG_LIMIT];
    1.89 +    jgArray=bdp->jgArray;
    1.90 +    prev=0;
    1.91 +    while(start<limit) {
    1.92 +        jg=*jgArray++;
    1.93 +        if(jg!=prev) {
    1.94 +            sa->add(sa->set, start);
    1.95 +            prev=jg;
    1.96 +        }
    1.97 +        ++start;
    1.98 +    }
    1.99 +    if(prev!=0) {
   1.100 +        /* add the limit code point if the last value was not 0 (it is now start==limit) */
   1.101 +        sa->add(sa->set, limit);
   1.102 +    }
   1.103 +
   1.104 +    /* add code points with hardcoded properties, plus the ones following them */
   1.105 +
   1.106 +    /* (none right now) */
   1.107 +}
   1.108 +
   1.109 +/* property access functions ------------------------------------------------ */
   1.110 +
   1.111 +U_CFUNC int32_t
   1.112 +ubidi_getMaxValue(const UBiDiProps *bdp, UProperty which) {
   1.113 +    int32_t max;
   1.114 +
   1.115 +    if(bdp==NULL) {
   1.116 +        return -1;
   1.117 +    }
   1.118 +
   1.119 +    max=bdp->indexes[UBIDI_MAX_VALUES_INDEX];
   1.120 +    switch(which) {
   1.121 +    case UCHAR_BIDI_CLASS:
   1.122 +        return (max&UBIDI_CLASS_MASK);
   1.123 +    case UCHAR_JOINING_GROUP:
   1.124 +        return (max&UBIDI_MAX_JG_MASK)>>UBIDI_MAX_JG_SHIFT;
   1.125 +    case UCHAR_JOINING_TYPE:
   1.126 +        return (max&UBIDI_JT_MASK)>>UBIDI_JT_SHIFT;
   1.127 +    case UCHAR_BIDI_PAIRED_BRACKET_TYPE:
   1.128 +        return (max&UBIDI_BPT_MASK)>>UBIDI_BPT_SHIFT;
   1.129 +    default:
   1.130 +        return -1; /* undefined */
   1.131 +    }
   1.132 +}
   1.133 +
   1.134 +U_CAPI UCharDirection
   1.135 +ubidi_getClass(const UBiDiProps *bdp, UChar32 c) {
   1.136 +    uint16_t props=UTRIE2_GET16(&bdp->trie, c);
   1.137 +    return (UCharDirection)UBIDI_GET_CLASS(props);
   1.138 +}
   1.139 +
   1.140 +U_CFUNC UBool
   1.141 +ubidi_isMirrored(const UBiDiProps *bdp, UChar32 c) {
   1.142 +    uint16_t props=UTRIE2_GET16(&bdp->trie, c);
   1.143 +    return (UBool)UBIDI_GET_FLAG(props, UBIDI_IS_MIRRORED_SHIFT);
   1.144 +}
   1.145 +
   1.146 +static UChar32
   1.147 +getMirror(const UBiDiProps *bdp, UChar32 c, uint16_t props) {
   1.148 +    int32_t delta=UBIDI_GET_MIRROR_DELTA(props);
   1.149 +    if(delta!=UBIDI_ESC_MIRROR_DELTA) {
   1.150 +        return c+delta;
   1.151 +    } else {
   1.152 +        /* look for mirror code point in the mirrors[] table */
   1.153 +        const uint32_t *mirrors;
   1.154 +        uint32_t m;
   1.155 +        int32_t i, length;
   1.156 +        UChar32 c2;
   1.157 +
   1.158 +        mirrors=bdp->mirrors;
   1.159 +        length=bdp->indexes[UBIDI_IX_MIRROR_LENGTH];
   1.160 +
   1.161 +        /* linear search */
   1.162 +        for(i=0; i<length; ++i) {
   1.163 +            m=mirrors[i];
   1.164 +            c2=UBIDI_GET_MIRROR_CODE_POINT(m);
   1.165 +            if(c==c2) {
   1.166 +                /* found c, return its mirror code point using the index in m */
   1.167 +                return UBIDI_GET_MIRROR_CODE_POINT(mirrors[UBIDI_GET_MIRROR_INDEX(m)]);
   1.168 +            } else if(c<c2) {
   1.169 +                break;
   1.170 +            }
   1.171 +        }
   1.172 +
   1.173 +        /* c not found, return it itself */
   1.174 +        return c;
   1.175 +    }
   1.176 +}
   1.177 +
   1.178 +U_CFUNC UChar32
   1.179 +ubidi_getMirror(const UBiDiProps *bdp, UChar32 c) {
   1.180 +    uint16_t props=UTRIE2_GET16(&bdp->trie, c);
   1.181 +    return getMirror(bdp, c, props);
   1.182 +}
   1.183 +
   1.184 +U_CFUNC UBool
   1.185 +ubidi_isBidiControl(const UBiDiProps *bdp, UChar32 c) {
   1.186 +    uint16_t props=UTRIE2_GET16(&bdp->trie, c);
   1.187 +    return (UBool)UBIDI_GET_FLAG(props, UBIDI_BIDI_CONTROL_SHIFT);
   1.188 +}
   1.189 +
   1.190 +U_CFUNC UBool
   1.191 +ubidi_isJoinControl(const UBiDiProps *bdp, UChar32 c) {
   1.192 +    uint16_t props=UTRIE2_GET16(&bdp->trie, c);
   1.193 +    return (UBool)UBIDI_GET_FLAG(props, UBIDI_JOIN_CONTROL_SHIFT);
   1.194 +}
   1.195 +
   1.196 +U_CFUNC UJoiningType
   1.197 +ubidi_getJoiningType(const UBiDiProps *bdp, UChar32 c) {
   1.198 +    uint16_t props=UTRIE2_GET16(&bdp->trie, c);
   1.199 +    return (UJoiningType)((props&UBIDI_JT_MASK)>>UBIDI_JT_SHIFT);
   1.200 +}
   1.201 +
   1.202 +U_CFUNC UJoiningGroup
   1.203 +ubidi_getJoiningGroup(const UBiDiProps *bdp, UChar32 c) {
   1.204 +    UChar32 start, limit;
   1.205 +
   1.206 +    start=bdp->indexes[UBIDI_IX_JG_START];
   1.207 +    limit=bdp->indexes[UBIDI_IX_JG_LIMIT];
   1.208 +    if(start<=c && c<limit) {
   1.209 +        return (UJoiningGroup)bdp->jgArray[c-start];
   1.210 +    } else {
   1.211 +        return U_JG_NO_JOINING_GROUP;
   1.212 +    }
   1.213 +}
   1.214 +
   1.215 +U_CFUNC UBidiPairedBracketType
   1.216 +ubidi_getPairedBracketType(const UBiDiProps *bdp, UChar32 c) {
   1.217 +    uint16_t props=UTRIE2_GET16(&bdp->trie, c);
   1.218 +    return (UBidiPairedBracketType)((props&UBIDI_BPT_MASK)>>UBIDI_BPT_SHIFT);
   1.219 +}
   1.220 +
   1.221 +U_CFUNC UChar32
   1.222 +ubidi_getPairedBracket(const UBiDiProps *bdp, UChar32 c) {
   1.223 +    uint16_t props=UTRIE2_GET16(&bdp->trie, c);
   1.224 +    if((props&UBIDI_BPT_MASK)==0) {
   1.225 +        return c;
   1.226 +    } else {
   1.227 +        return getMirror(bdp, c, props);
   1.228 +    }
   1.229 +}
   1.230 +
   1.231 +/* public API (see uchar.h) ------------------------------------------------- */
   1.232 +
   1.233 +U_CFUNC UCharDirection
   1.234 +u_charDirection(UChar32 c) {   
   1.235 +    return ubidi_getClass(&ubidi_props_singleton, c);
   1.236 +}
   1.237 +
   1.238 +U_CFUNC UBool
   1.239 +u_isMirrored(UChar32 c) {
   1.240 +    return ubidi_isMirrored(&ubidi_props_singleton, c);
   1.241 +}
   1.242 +
   1.243 +U_CFUNC UChar32
   1.244 +u_charMirror(UChar32 c) {
   1.245 +    return ubidi_getMirror(&ubidi_props_singleton, c);
   1.246 +}
   1.247 +
   1.248 +U_STABLE UChar32 U_EXPORT2
   1.249 +u_getBidiPairedBracket(UChar32 c) {
   1.250 +    return ubidi_getPairedBracket(&ubidi_props_singleton, c);
   1.251 +}

mercurial