~ubuntu-branches/ubuntu/quantal/icu/quantal

« back to all changes in this revision

Viewing changes to source/common/ubidi.c

  • Committer: Package Import Robot
  • Author(s): Yves Arrouye
  • Date: 2002-03-03 15:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20020303153113-3ssceqlq45xbmbnc
Tags: upstream-2.0-2.1pre20020303
ImportĀ upstreamĀ versionĀ 2.0-2.1pre20020303

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  
 
2
******************************************************************************
 
3
*
 
4
*   Copyright (C) 1999-2001, International Business Machines
 
5
*   Corporation and others.  All Rights Reserved.
 
6
*
 
7
******************************************************************************
 
8
*   file name:  ubidi.c
 
9
*   encoding:   US-ASCII
 
10
*   tab size:   8 (not used)
 
11
*   indentation:4
 
12
*
 
13
*   created on: 1999jul27
 
14
*   created by: Markus W. Scherer
 
15
*/
 
16
 
 
17
/* set import/export definitions */
 
18
#ifndef U_COMMON_IMPLEMENTATION
 
19
#   define U_COMMON_IMPLEMENTATION
 
20
#endif
 
21
 
 
22
#include "cmemory.h"
 
23
#include "unicode/utypes.h"
 
24
#include "unicode/ustring.h"
 
25
#include "unicode/uchar.h"
 
26
#include "unicode/ubidi.h"
 
27
#include "ubidiimp.h"
 
28
 
 
29
/*
 
30
 * General implementation notes:
 
31
 *
 
32
 * Throughout the implementation, there are comments like (W2) that refer to
 
33
 * rules of the BiDi algorithm in its version 5, in this example to the second
 
34
 * rule of the resolution of weak types.
 
35
 *
 
36
 * For handling surrogate pairs, where two UChar's form one "abstract" (or UTF-32)
 
37
 * character according to UTF-16, the second UChar gets the directional property of
 
38
 * the entire character assigned, while the first one gets a BN, a boundary
 
39
 * neutral, type, which is ignored by most of the algorithm according to
 
40
 * rule (X9) and the implementation suggestions of the BiDi algorithm.
 
41
 *
 
42
 * Later, adjustWSLevels() will set the level for each BN to that of the
 
43
 * following character (UChar), which results in surrogate pairs getting the
 
44
 * same level on each of their surrogates.
 
45
 *
 
46
 * In a UTF-8 implementation, the same thing could be done: the last byte of
 
47
 * a multi-byte sequence would get the "real" property, while all previous
 
48
 * bytes of that sequence would get BN.
 
49
 *
 
50
 * It is not possible to assign all those parts of a character the same real
 
51
 * property because this would fail in the resolution of weak types with rules
 
52
 * that look at immediately surrounding types.
 
53
 *
 
54
 * As a related topic, this implementation does not remove Boundary Neutral
 
55
 * types from the input, but ignores them whereever this is relevant.
 
56
 * For example, the loop for the resolution of the weak types reads
 
57
 * types until it finds a non-BN.
 
58
 * Also, explicit embedding codes are neither changed into BN nor removed.
 
59
 * They are only treated the same way real BNs are.
 
60
 * As stated before, adjustWSLevels() takes care of them at the end.
 
61
 * For the purpose of conformance, the levels of all these codes
 
62
 * do not matter.
 
63
 *
 
64
 * Note that this implementation never modifies the dirProps
 
65
 * after the initial setup.
 
66
 *
 
67
 *
 
68
 * In this implementation, the resolution of weak types (Wn),
 
69
 * neutrals (Nn), and the assignment of the resolved level (In)
 
70
 * are all done in one single loop, in resolveImplicitLevels().
 
71
 * Changes of dirProp values are done on the fly, without writing
 
72
 * them back to the dirProps array.
 
73
 *
 
74
 *
 
75
 * This implementation contains code that allows to bypass steps of the
 
76
 * algorithm that are not needed on the specific paragraph
 
77
 * in order to speed up the most common cases considerably,
 
78
 * like text that is entirely LTR, or RTL text without numbers.
 
79
 *
 
80
 * Most of this is done by setting a bit for each directional property
 
81
 * in a flags variable and later checking for whether there are
 
82
 * any LTR characters or any RTL characters, or both, whether
 
83
 * there are any explicit embedding codes, etc.
 
84
 *
 
85
 * If the (Xn) steps are performed, then the flags are re-evaluated,
 
86
 * because they will then not contain the embedding codes any more
 
87
 * and will be adjusted for override codes, so that subsequently
 
88
 * more bypassing may be possible than what the initial flags suggested.
 
89
 *
 
90
 * If the text is not mixed-directional, then the
 
91
 * algorithm steps for the weak type resolution are not performed,
 
92
 * and all levels are set to the paragraph level.
 
93
 *
 
94
 * If there are no explicit embedding codes, then the (Xn) steps
 
95
 * are not performed.
 
96
 *
 
97
 * If embedding levels are supplied as a parameter, then all
 
98
 * explicit embedding codes are ignored, and the (Xn) steps
 
99
 * are not performed.
 
100
 *
 
101
 * White Space types could get the level of the run they belong to,
 
102
 * and are checked with a test of (flags&MASK_EMBEDDING) to
 
103
 * consider if the paragraph direction should be considered in
 
104
 * the flags variable.
 
105
 *
 
106
 * If there are no White Space types in the paragraph, then
 
107
 * (L1) is not necessary in adjustWSLevels().
 
108
 */
 
109
 
 
110
/* prototypes --------------------------------------------------------------- */
 
111
 
 
112
static void
 
113
getDirProps(UBiDi *pBiDi, const UChar *text);
 
114
 
 
115
static UBiDiDirection
 
116
resolveExplicitLevels(UBiDi *pBiDi);
 
117
 
 
118
static UBiDiDirection
 
119
checkExplicitLevels(UBiDi *pBiDi, UErrorCode *pErrorCode);
 
120
 
 
121
static UBiDiDirection
 
122
directionFromFlags(Flags flags);
 
123
 
 
124
static void
 
125
resolveImplicitLevels(UBiDi *pBiDi,
 
126
                      UTextOffset start, UTextOffset limit,
 
127
                      DirProp sor, DirProp eor);
 
128
 
 
129
static void
 
130
adjustWSLevels(UBiDi *pBiDi);
 
131
 
 
132
/* to avoid some conditional statements, use tiny constant arrays */
 
133
static const Flags flagLR[2]={ DIRPROP_FLAG(L), DIRPROP_FLAG(R) };
 
134
static const Flags flagE[2]={ DIRPROP_FLAG(LRE), DIRPROP_FLAG(RLE) };
 
135
static const Flags flagO[2]={ DIRPROP_FLAG(LRO), DIRPROP_FLAG(RLO) };
 
136
 
 
137
#define DIRPROP_FLAG_LR(level) flagLR[(level)&1]
 
138
#define DIRPROP_FLAG_E(level) flagE[(level)&1]
 
139
#define DIRPROP_FLAG_O(level) flagO[(level)&1]
 
140
 
 
141
/* UBiDi object management -------------------------------------------------- */
 
142
 
 
143
U_CAPI UBiDi * U_EXPORT2
 
144
ubidi_open(void) 
 
145
{
 
146
    UErrorCode errorCode=U_ZERO_ERROR;
 
147
    return ubidi_openSized(0, 0, &errorCode);
 
148
}
 
149
 
 
150
U_CAPI UBiDi * U_EXPORT2
 
151
ubidi_openSized(UTextOffset maxLength, UTextOffset maxRunCount, UErrorCode *pErrorCode) {
 
152
    UBiDi *pBiDi;
 
153
 
 
154
    /* check the argument values */
 
155
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
 
156
        return NULL;
 
157
    } else if(maxLength<0 || maxRunCount<0) {
 
158
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
 
159
        return NULL;    /* invalid arguments */
 
160
    }
 
161
 
 
162
    /* allocate memory for the object */
 
163
    pBiDi=(UBiDi *)uprv_malloc(sizeof(UBiDi));
 
164
    if(pBiDi==NULL) {
 
165
        *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
 
166
        return NULL;
 
167
    }
 
168
 
 
169
    /* reset the object, all pointers NULL, all flags FALSE, all sizes 0 */
 
170
    uprv_memset(pBiDi, 0, sizeof(UBiDi));
 
171
 
 
172
    /* allocate memory for arrays as requested */
 
173
    if(maxLength>0) {
 
174
        if( !getInitialDirPropsMemory(pBiDi, maxLength) ||
 
175
            !getInitialLevelsMemory(pBiDi, maxLength)
 
176
        ) {
 
177
            *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
 
178
        }
 
179
    } else {
 
180
        pBiDi->mayAllocateText=TRUE;
 
181
    }
 
182
 
 
183
    if(maxRunCount>0) {
 
184
        if(maxRunCount==1) {
 
185
            /* use simpleRuns[] */
 
186
            pBiDi->runsSize=sizeof(Run);
 
187
        } else if(!getInitialRunsMemory(pBiDi, maxRunCount)) {
 
188
            *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
 
189
        }
 
190
    } else {
 
191
        pBiDi->mayAllocateRuns=TRUE;
 
192
    }
 
193
 
 
194
    if(U_SUCCESS(*pErrorCode)) {
 
195
        return pBiDi;
 
196
    } else {
 
197
        ubidi_close(pBiDi);
 
198
        return NULL;
 
199
    }
 
200
}
 
201
 
 
202
/*
 
203
 * We are allowed to allocate memory if memory==NULL or
 
204
 * mayAllocate==TRUE for each array that we need.
 
205
 * We also try to grow and shrink memory as needed if we
 
206
 * allocate it.
 
207
 *
 
208
 * Assume sizeNeeded>0.
 
209
 * If *pMemory!=NULL, then assume *pSize>0.
 
210
 *
 
211
 * ### this realloc() may unnecessarily copy the old data,
 
212
 * which we know we don't need any more;
 
213
 * is this the best way to do this??
 
214
 */
 
215
U_CFUNC UBool
 
216
ubidi_getMemory(void **pMemory, UTextOffset *pSize, UBool mayAllocate, UTextOffset sizeNeeded) {
 
217
    /* check for existing memory */
 
218
    if(*pMemory==NULL) {
 
219
        /* we need to allocate memory */
 
220
        if(mayAllocate && (*pMemory=uprv_malloc(sizeNeeded))!=NULL) {
 
221
            *pSize=sizeNeeded;
 
222
            return TRUE;
 
223
        } else {
 
224
            return FALSE;
 
225
        }
 
226
    } else {
 
227
        /* there is some memory, is it enough or too much? */
 
228
        if(sizeNeeded>*pSize && !mayAllocate) {
 
229
            /* not enough memory, and we must not allocate */
 
230
            return FALSE;
 
231
        } else if(sizeNeeded!=*pSize && mayAllocate) {
 
232
            /* we may try to grow or shrink */
 
233
            void *memory;
 
234
 
 
235
            if((memory=uprv_realloc(*pMemory, sizeNeeded))!=NULL) {
 
236
                *pMemory=memory;
 
237
                *pSize=sizeNeeded;
 
238
                return TRUE;
 
239
            } else {
 
240
                /* we failed to grow */
 
241
                return FALSE;
 
242
            }
 
243
        } else {
 
244
            /* we have at least enough memory and must not allocate */
 
245
            return TRUE;
 
246
        }
 
247
    }
 
248
}
 
249
 
 
250
U_CAPI void U_EXPORT2
 
251
ubidi_close(UBiDi *pBiDi) {
 
252
    if(pBiDi!=NULL) {
 
253
        if(pBiDi->dirPropsMemory!=NULL) {
 
254
            uprv_free(pBiDi->dirPropsMemory);
 
255
        }
 
256
        if(pBiDi->levelsMemory!=NULL) {
 
257
            uprv_free(pBiDi->levelsMemory);
 
258
        }
 
259
        if(pBiDi->runsMemory!=NULL) {
 
260
            uprv_free(pBiDi->runsMemory);
 
261
        }
 
262
        uprv_free(pBiDi);
 
263
    }
 
264
}
 
265
 
 
266
/* set to approximate "inverse BiDi" ---------------------------------------- */
 
267
 
 
268
U_CAPI void U_EXPORT2
 
269
ubidi_setInverse(UBiDi *pBiDi, UBool isInverse) {
 
270
    if(pBiDi!=NULL) {
 
271
        pBiDi->isInverse=isInverse;
 
272
    }
 
273
}
 
274
 
 
275
U_CAPI UBool U_EXPORT2
 
276
ubidi_isInverse(UBiDi *pBiDi) {
 
277
    if(pBiDi!=NULL) {
 
278
        return pBiDi->isInverse;
 
279
    } else {
 
280
        return FALSE;
 
281
    }
 
282
}
 
283
 
 
284
/* ubidi_setPara ------------------------------------------------------------ */
 
285
 
 
286
U_CAPI void U_EXPORT2
 
287
ubidi_setPara(UBiDi *pBiDi, const UChar *text, UTextOffset length,
 
288
              UBiDiLevel paraLevel, UBiDiLevel *embeddingLevels,
 
289
              UErrorCode *pErrorCode) {
 
290
    UBiDiDirection direction;
 
291
 
 
292
    /* check the argument values */
 
293
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
 
294
        return;
 
295
    } else if(pBiDi==NULL || text==NULL ||
 
296
              ((UBIDI_MAX_EXPLICIT_LEVEL<paraLevel) && !IS_DEFAULT_LEVEL(paraLevel)) ||
 
297
              length<-1
 
298
    ) {
 
299
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
 
300
        return;
 
301
    }
 
302
 
 
303
    if(length==-1) {
 
304
        length=u_strlen(text);
 
305
    }
 
306
 
 
307
    /* initialize the UBiDi structure */
 
308
    pBiDi->text=text;
 
309
    pBiDi->length=length;
 
310
    pBiDi->paraLevel=paraLevel;
 
311
    pBiDi->direction=UBIDI_LTR;
 
312
    pBiDi->trailingWSStart=length;  /* the levels[] will reflect the WS run */
 
313
 
 
314
    pBiDi->dirProps=NULL;
 
315
    pBiDi->levels=NULL;
 
316
    pBiDi->runs=NULL;
 
317
 
 
318
    if(length==0) {
 
319
        /*
 
320
         * For an empty paragraph, create a UBiDi object with the paraLevel and
 
321
         * the flags and the direction set but without allocating zero-length arrays.
 
322
         * There is nothing more to do.
 
323
         */
 
324
        if(IS_DEFAULT_LEVEL(paraLevel)) {
 
325
            pBiDi->paraLevel&=1;
 
326
        }
 
327
        if(paraLevel&1) {
 
328
            pBiDi->flags=DIRPROP_FLAG(R);
 
329
            pBiDi->direction=UBIDI_RTL;
 
330
        } else {
 
331
            pBiDi->flags=DIRPROP_FLAG(L);
 
332
            pBiDi->direction=UBIDI_LTR;
 
333
        }
 
334
 
 
335
        pBiDi->runCount=0;
 
336
        return;
 
337
    }
 
338
 
 
339
    pBiDi->runCount=-1;
 
340
 
 
341
    /*
 
342
     * Get the directional properties,
 
343
     * the flags bit-set, and
 
344
     * determine the partagraph level if necessary.
 
345
     */
 
346
    if(getDirPropsMemory(pBiDi, length)) {
 
347
        pBiDi->dirProps=pBiDi->dirPropsMemory;
 
348
        getDirProps(pBiDi, text);
 
349
    } else {
 
350
        *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
 
351
        return;
 
352
    }
 
353
 
 
354
    /* are explicit levels specified? */
 
355
    if(embeddingLevels==NULL) {
 
356
        /* no: determine explicit levels according to the (Xn) rules */\
 
357
        if(getLevelsMemory(pBiDi, length)) {
 
358
            pBiDi->levels=pBiDi->levelsMemory;
 
359
            direction=resolveExplicitLevels(pBiDi);
 
360
        } else {
 
361
            *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
 
362
            return;
 
363
        }
 
364
    } else {
 
365
        /* set BN for all explicit codes, check that all levels are paraLevel..UBIDI_MAX_EXPLICIT_LEVEL */
 
366
        pBiDi->levels=embeddingLevels;
 
367
        direction=checkExplicitLevels(pBiDi, pErrorCode);
 
368
        if(U_FAILURE(*pErrorCode)) {
 
369
            return;
 
370
        }
 
371
    }
 
372
 
 
373
    /*
 
374
     * The steps after (X9) in the UBiDi algorithm are performed only if
 
375
     * the paragraph text has mixed directionality!
 
376
     */
 
377
    pBiDi->direction=direction;
 
378
    switch(direction) {
 
379
    case UBIDI_LTR:
 
380
        /* make sure paraLevel is even */
 
381
        pBiDi->paraLevel=(UBiDiLevel)((pBiDi->paraLevel+1)&~1);
 
382
 
 
383
        /* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */
 
384
        pBiDi->trailingWSStart=0;
 
385
        break;
 
386
    case UBIDI_RTL:
 
387
        /* make sure paraLevel is odd */
 
388
        pBiDi->paraLevel|=1;
 
389
 
 
390
        /* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */
 
391
        pBiDi->trailingWSStart=0;
 
392
        break;
 
393
    default:
 
394
        /*
 
395
         * If there are no external levels specified and there
 
396
         * are no significant explicit level codes in the text,
 
397
         * then we can treat the entire paragraph as one run.
 
398
         * Otherwise, we need to perform the following rules on runs of
 
399
         * the text with the same embedding levels. (X10)
 
400
         * "Significant" explicit level codes are ones that actually
 
401
         * affect non-BN characters.
 
402
         * Examples for "insignificant" ones are empty embeddings
 
403
         * LRE-PDF, LRE-RLE-PDF-PDF, etc.
 
404
         */
 
405
        if(embeddingLevels==NULL && !(pBiDi->flags&DIRPROP_FLAG_MULTI_RUNS)) {
 
406
            resolveImplicitLevels(pBiDi, 0, length,
 
407
                                    GET_LR_FROM_LEVEL(pBiDi->paraLevel),
 
408
                                    GET_LR_FROM_LEVEL(pBiDi->paraLevel));
 
409
        } else {
 
410
            /* sor, eor: start and end types of same-level-run */
 
411
            UBiDiLevel *levels=pBiDi->levels;
 
412
            UTextOffset start, limit=0;
 
413
            UBiDiLevel level, nextLevel;
 
414
            DirProp sor, eor;
 
415
 
 
416
            /* determine the first sor and set eor to it because of the loop body (sor=eor there) */
 
417
            level=pBiDi->paraLevel;
 
418
            nextLevel=levels[0];
 
419
            if(level<nextLevel) {
 
420
                eor=GET_LR_FROM_LEVEL(nextLevel);
 
421
            } else {
 
422
                eor=GET_LR_FROM_LEVEL(level);
 
423
            }
 
424
 
 
425
            do {
 
426
                /* determine start and limit of the run (end points just behind the run) */
 
427
 
 
428
                /* the values for this run's start are the same as for the previous run's end */
 
429
                sor=eor;
 
430
                start=limit;
 
431
                level=nextLevel;
 
432
 
 
433
                /* search for the limit of this run */
 
434
                while(++limit<length && levels[limit]==level) {}
 
435
 
 
436
                /* get the correct level of the next run */
 
437
                if(limit<length) {
 
438
                    nextLevel=levels[limit];
 
439
                } else {
 
440
                    nextLevel=pBiDi->paraLevel;
 
441
                }
 
442
 
 
443
                /* determine eor from max(level, nextLevel); sor is last run's eor */
 
444
                if((level&~UBIDI_LEVEL_OVERRIDE)<(nextLevel&~UBIDI_LEVEL_OVERRIDE)) {
 
445
                    eor=GET_LR_FROM_LEVEL(nextLevel);
 
446
                } else {
 
447
                    eor=GET_LR_FROM_LEVEL(level);
 
448
                }
 
449
 
 
450
                /* if the run consists of overridden directional types, then there
 
451
                   are no implicit types to be resolved */
 
452
                if(!(level&UBIDI_LEVEL_OVERRIDE)) {
 
453
                    resolveImplicitLevels(pBiDi, start, limit, sor, eor);
 
454
                } else {
 
455
                    /* remove the UBIDI_LEVEL_OVERRIDE flags */
 
456
                    do {
 
457
                        levels[start++]&=~UBIDI_LEVEL_OVERRIDE;
 
458
                    } while(start<limit);
 
459
                }
 
460
            } while(limit<length);
 
461
        }
 
462
 
 
463
        /* reset the embedding levels for some non-graphic characters (L1), (X9) */
 
464
        adjustWSLevels(pBiDi);
 
465
 
 
466
        /* for "inverse BiDi", ubidi_getRuns() modifies the levels of numeric runs following RTL runs */
 
467
        if(pBiDi->isInverse) {
 
468
            if(!ubidi_getRuns(pBiDi)) {
 
469
                *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
 
470
                return;
 
471
            }
 
472
        }
 
473
        break;
 
474
    }
 
475
}
 
476
 
 
477
/* perform (P2)..(P3) ------------------------------------------------------- */
 
478
 
 
479
/*
 
480
 * Get the directional properties for the text,
 
481
 * calculate the flags bit-set, and
 
482
 * determine the partagraph level if necessary.
 
483
 */
 
484
static void
 
485
getDirProps(UBiDi *pBiDi, const UChar *text) {
 
486
    DirProp *dirProps=pBiDi->dirPropsMemory;    /* pBiDi->dirProps is const */
 
487
 
 
488
    UTextOffset i=0, i0, i1, length=pBiDi->length;
 
489
    Flags flags=0;      /* collect all directionalities in the text */
 
490
    UChar32 uchar;
 
491
    DirProp dirProp;
 
492
 
 
493
    if(IS_DEFAULT_LEVEL(pBiDi->paraLevel)) {
 
494
        /* determine the paragraph level (P2..P3) */
 
495
        for(;;) {
 
496
            i0=i;           /* index of first code unit */
 
497
            UTF_NEXT_CHAR(text, i, length, uchar);
 
498
            i1=i-1;         /* index of last code unit, gets the directional property */
 
499
            flags|=DIRPROP_FLAG(dirProps[i1]=dirProp=u_charDirection(uchar));
 
500
            if(i1>i0) {     /* set previous code units' properties to BN */
 
501
                flags|=DIRPROP_FLAG(BN);
 
502
                do {
 
503
                    dirProps[--i1]=BN;
 
504
                } while(i1>i0);
 
505
            }
 
506
 
 
507
            if(dirProp==L) {
 
508
                pBiDi->paraLevel=0;
 
509
                break;
 
510
            } else if(dirProp==R || dirProp==AL) {
 
511
                pBiDi->paraLevel=1;
 
512
                break;
 
513
            } else if(i>=length) {
 
514
                /*
 
515
                 * see comment in ubidi.h:
 
516
                 * the DEFAULT_XXX values are designed so that
 
517
                 * their bit 0 alone yields the intended default
 
518
                 */
 
519
                pBiDi->paraLevel&=1;
 
520
                break;
 
521
            }
 
522
        }
 
523
    }
 
524
 
 
525
    /* get the rest of the directional properties and the flags bits */
 
526
    while(i<length) {
 
527
        i0=i;           /* index of first code unit */
 
528
        UTF_NEXT_CHAR(text, i, length, uchar);
 
529
        i1=i-1;         /* index of last code unit, gets the directional property */
 
530
        flags|=DIRPROP_FLAG(dirProps[i1]=dirProp=u_charDirection(uchar));
 
531
        if(i1>i0) {     /* set previous code units' properties to BN */
 
532
            flags|=DIRPROP_FLAG(BN);
 
533
            do {
 
534
                dirProps[--i1]=BN;
 
535
            } while(i1>i0);
 
536
        }
 
537
    }
 
538
    if(flags&MASK_EMBEDDING) {
 
539
        flags|=DIRPROP_FLAG_LR(pBiDi->paraLevel);
 
540
    }
 
541
 
 
542
    pBiDi->flags=flags;
 
543
}
 
544
 
 
545
/* perform (X1)..(X9) ------------------------------------------------------- */
 
546
 
 
547
/*
 
548
 * Resolve the explicit levels as specified by explicit embedding codes.
 
549
 * Recalculate the flags to have them reflect the real properties
 
550
 * after taking the explicit embeddings into account.
 
551
 *
 
552
 * The BiDi algorithm is designed to result in the same behavior whether embedding
 
553
 * levels are externally specified (from "styled text", supposedly the preferred
 
554
 * method) or set by explicit embedding codes (LRx, RLx, PDF) in the plain text.
 
555
 * That is why (X9) instructs to remove all explicit codes (and BN).
 
556
 * However, in a real implementation, this removal of these codes and their index
 
557
 * positions in the plain text is undesirable since it would result in
 
558
 * reallocated, reindexed text.
 
559
 * Instead, this implementation leaves the codes in there and just ignores them
 
560
 * in the subsequent processing.
 
561
 * In order to get the same reordering behavior, positions with a BN or an
 
562
 * explicit embedding code just get the same level assigned as the last "real"
 
563
 * character.
 
564
 *
 
565
 * Some implementations, not this one, then overwrite some of these
 
566
 * directionality properties at "real" same-level-run boundaries by
 
567
 * L or R codes so that the resolution of weak types can be performed on the
 
568
 * entire paragraph at once instead of having to parse it once more and
 
569
 * perform that resolution on same-level-runs.
 
570
 * This limits the scope of the implicit rules in effectively
 
571
 * the same way as the run limits.
 
572
 *
 
573
 * Instead, this implementation does not modify these codes.
 
574
 * On one hand, the paragraph has to be scanned for same-level-runs, but
 
575
 * on the other hand, this saves another loop to reset these codes,
 
576
 * or saves making and modifying a copy of dirProps[].
 
577
 *
 
578
 *
 
579
 * Note that (Pn) and (Xn) changed significantly from version 4 of the BiDi algorithm.
 
580
 *
 
581
 *
 
582
 * Handling the stack of explicit levels (Xn):
 
583
 *
 
584
 * With the BiDi stack of explicit levels,
 
585
 * as pushed with each LRE, RLE, LRO, and RLO and popped with each PDF,
 
586
 * the explicit level must never exceed UBIDI_MAX_EXPLICIT_LEVEL==61.
 
587
 *
 
588
 * In order to have a correct push-pop semantics even in the case of overflows,
 
589
 * there are two overflow counters:
 
590
 * - countOver60 is incremented with each LRx at level 60
 
591
 * - from level 60, one RLx increases the level to 61
 
592
 * - countOver61 is incremented with each LRx and RLx at level 61
 
593
 *
 
594
 * Popping levels with PDF must work in the opposite order so that level 61
 
595
 * is correct at the correct point. Underflows (too many PDFs) must be checked.
 
596
 *
 
597
 * This implementation assumes that UBIDI_MAX_EXPLICIT_LEVEL is odd.
 
598
 */
 
599
 
 
600
static UBiDiDirection
 
601
resolveExplicitLevels(UBiDi *pBiDi) {
 
602
    const DirProp *dirProps=pBiDi->dirProps;
 
603
    UBiDiLevel *levels=pBiDi->levels;
 
604
    
 
605
    UTextOffset i=0, length=pBiDi->length;
 
606
    Flags flags=pBiDi->flags;       /* collect all directionalities in the text */
 
607
    DirProp dirProp;
 
608
    UBiDiLevel level=pBiDi->paraLevel;
 
609
 
 
610
    UBiDiDirection direction;
 
611
 
 
612
    /* determine if the text is mixed-directional or single-directional */
 
613
    direction=directionFromFlags(flags);
 
614
 
 
615
    /* we may not need to resolve any explicit levels */
 
616
    if(direction!=UBIDI_MIXED) {
 
617
        /* not mixed directionality: levels don't matter - trailingWSStart will be 0 */
 
618
    } else if(!(flags&MASK_EXPLICIT) || pBiDi->isInverse) {
 
619
        /* mixed, but all characters are at the same embedding level */
 
620
        /* or we are in "inverse BiDi" */
 
621
        /* set all levels to the paragraph level */
 
622
        for(i=0; i<length; ++i) {
 
623
            levels[i]=level;
 
624
        }
 
625
    } else {
 
626
        /* continue to perform (Xn) */
 
627
 
 
628
        /* (X1) level is set for all codes, embeddingLevel keeps track of the push/pop operations */
 
629
        /* both variables may carry the UBIDI_LEVEL_OVERRIDE flag to indicate the override status */
 
630
        UBiDiLevel embeddingLevel=level, newLevel, stackTop=0;
 
631
 
 
632
        UBiDiLevel stack[UBIDI_MAX_EXPLICIT_LEVEL];        /* we never push anything >=UBIDI_MAX_EXPLICIT_LEVEL */
 
633
        uint32_t countOver60=0, countOver61=0;  /* count overflows of explicit levels */
 
634
 
 
635
        /* recalculate the flags */
 
636
        flags=0;
 
637
 
 
638
        /* since we assume that this is a single paragraph, we ignore (X8) */
 
639
        for(i=0; i<length; ++i) {
 
640
            dirProp=dirProps[i];
 
641
            switch(dirProp) {
 
642
            case LRE:
 
643
            case LRO:
 
644
                /* (X3, X5) */
 
645
                newLevel=(UBiDiLevel)((embeddingLevel+2)&~(UBIDI_LEVEL_OVERRIDE|1)); /* least greater even level */
 
646
                if(newLevel<=UBIDI_MAX_EXPLICIT_LEVEL) {
 
647
                    stack[stackTop]=embeddingLevel;
 
648
                    ++stackTop;
 
649
                    embeddingLevel=newLevel;
 
650
                    if(dirProp==LRO) {
 
651
                        embeddingLevel|=UBIDI_LEVEL_OVERRIDE;
 
652
                    } else {
 
653
                        embeddingLevel&=~UBIDI_LEVEL_OVERRIDE;
 
654
                    }
 
655
                } else if((embeddingLevel&~UBIDI_LEVEL_OVERRIDE)==UBIDI_MAX_EXPLICIT_LEVEL) {
 
656
                    ++countOver61;
 
657
                } else /* (embeddingLevel&~UBIDI_LEVEL_OVERRIDE)==UBIDI_MAX_EXPLICIT_LEVEL-1 */ {
 
658
                    ++countOver60;
 
659
                }
 
660
                flags|=DIRPROP_FLAG(BN);
 
661
                break;
 
662
            case RLE:
 
663
            case RLO:
 
664
                /* (X2, X4) */
 
665
                newLevel=(UBiDiLevel)(((embeddingLevel&~UBIDI_LEVEL_OVERRIDE)+1)|1); /* least greater odd level */
 
666
                if(newLevel<=UBIDI_MAX_EXPLICIT_LEVEL) {
 
667
                    stack[stackTop]=embeddingLevel;
 
668
                    ++stackTop;
 
669
                    embeddingLevel=newLevel;
 
670
                    if(dirProp==RLO) {
 
671
                        embeddingLevel|=UBIDI_LEVEL_OVERRIDE;
 
672
                    } else {
 
673
                        embeddingLevel&=~UBIDI_LEVEL_OVERRIDE;
 
674
                    }
 
675
                } else {
 
676
                    ++countOver61;
 
677
                }
 
678
                flags|=DIRPROP_FLAG(BN);
 
679
                break;
 
680
            case PDF:
 
681
                /* (X7) */
 
682
                /* handle all the overflow cases first */
 
683
                if(countOver61>0) {
 
684
                    --countOver61;
 
685
                } else if(countOver60>0 && (embeddingLevel&~UBIDI_LEVEL_OVERRIDE)!=UBIDI_MAX_EXPLICIT_LEVEL) {
 
686
                    /* handle LRx overflows from level 60 */
 
687
                    --countOver60;
 
688
                } else if(stackTop>0) {
 
689
                    /* this is the pop operation; it also pops level 61 while countOver60>0 */
 
690
                    --stackTop;
 
691
                    embeddingLevel=stack[stackTop];
 
692
                /* } else { (underflow) */
 
693
                }
 
694
                flags|=DIRPROP_FLAG(BN);
 
695
                break;
 
696
            case B:
 
697
                /*
 
698
                 * We do not really expect to see a paragraph separator (B),
 
699
                 * but we should do something reasonable with it,
 
700
                 * especially at the end of the text.
 
701
                 */
 
702
                stackTop=0;
 
703
                countOver60=countOver61=0;
 
704
                embeddingLevel=level=pBiDi->paraLevel;
 
705
                flags|=DIRPROP_FLAG(B);
 
706
                break;
 
707
            case BN:
 
708
                /* BN, LRE, RLE, and PDF are supposed to be removed (X9) */
 
709
                /* they will get their levels set correctly in adjustWSLevels() */
 
710
                flags|=DIRPROP_FLAG(BN);
 
711
                break;
 
712
            default:
 
713
                /* all other types get the "real" level */
 
714
                if(level!=embeddingLevel) {
 
715
                    level=embeddingLevel;
 
716
                    if(level&UBIDI_LEVEL_OVERRIDE) {
 
717
                        flags|=DIRPROP_FLAG_O(level)|DIRPROP_FLAG_MULTI_RUNS;
 
718
                    } else {
 
719
                        flags|=DIRPROP_FLAG_E(level)|DIRPROP_FLAG_MULTI_RUNS;
 
720
                    }
 
721
                }
 
722
                if(!(level&UBIDI_LEVEL_OVERRIDE)) {
 
723
                    flags|=DIRPROP_FLAG(dirProp);
 
724
                }
 
725
                break;
 
726
            }
 
727
 
 
728
            /*
 
729
             * We need to set reasonable levels even on BN codes and
 
730
             * explicit codes because we will later look at same-level runs (X10).
 
731
             */
 
732
            levels[i]=level;
 
733
        }
 
734
        if(flags&MASK_EMBEDDING) {
 
735
            flags|=DIRPROP_FLAG_LR(pBiDi->paraLevel);
 
736
        }
 
737
 
 
738
        /* subsequently, ignore the explicit codes and BN (X9) */
 
739
 
 
740
        /* again, determine if the text is mixed-directional or single-directional */
 
741
        pBiDi->flags=flags;
 
742
        direction=directionFromFlags(flags);
 
743
    }
 
744
    return direction;
 
745
}
 
746
 
 
747
/*
 
748
 * Use a pre-specified embedding levels array:
 
749
 *
 
750
 * Adjust the directional properties for overrides (->LEVEL_OVERRIDE),
 
751
 * ignore all explicit codes (X9),
 
752
 * and check all the preset levels.
 
753
 *
 
754
 * Recalculate the flags to have them reflect the real properties
 
755
 * after taking the explicit embeddings into account.
 
756
 */
 
757
static UBiDiDirection
 
758
checkExplicitLevels(UBiDi *pBiDi, UErrorCode *pErrorCode) {
 
759
    const DirProp *dirProps=pBiDi->dirProps;
 
760
    UBiDiLevel *levels=pBiDi->levels;
 
761
    
 
762
    UTextOffset i, length=pBiDi->length;
 
763
    Flags flags=0;  /* collect all directionalities in the text */
 
764
    UBiDiLevel level, paraLevel=pBiDi->paraLevel;
 
765
 
 
766
    for(i=0; i<length; ++i) {
 
767
        level=levels[i];
 
768
        if(level&UBIDI_LEVEL_OVERRIDE) {
 
769
            /* keep the override flag in levels[i] but adjust the flags */
 
770
            level&=~UBIDI_LEVEL_OVERRIDE;     /* make the range check below simpler */
 
771
            flags|=DIRPROP_FLAG_O(level);
 
772
        } else {
 
773
            /* set the flags */
 
774
            flags|=DIRPROP_FLAG_E(level)|DIRPROP_FLAG(dirProps[i]);
 
775
        }
 
776
        if(level<paraLevel || UBIDI_MAX_EXPLICIT_LEVEL<level) {
 
777
            /* level out of bounds */
 
778
            *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
 
779
            return UBIDI_LTR;
 
780
        }
 
781
    }
 
782
    if(flags&MASK_EMBEDDING) {
 
783
        flags|=DIRPROP_FLAG_LR(pBiDi->paraLevel);
 
784
    }
 
785
 
 
786
    /* determine if the text is mixed-directional or single-directional */
 
787
    pBiDi->flags=flags;
 
788
    return directionFromFlags(flags);
 
789
}
 
790
 
 
791
/* determine if the text is mixed-directional or single-directional */
 
792
static UBiDiDirection
 
793
directionFromFlags(Flags flags) {
 
794
    /* if the text contains AN and neutrals, then some neutrals may become RTL */
 
795
    if(!(flags&MASK_RTL || ((flags&DIRPROP_FLAG(AN)) && (flags&MASK_POSSIBLE_N)))) {
 
796
        return UBIDI_LTR;
 
797
    } else if(!(flags&MASK_LTR)) {
 
798
        return UBIDI_RTL;
 
799
    } else {
 
800
        return UBIDI_MIXED;
 
801
    }
 
802
}
 
803
 
 
804
/* perform rules (Wn), (Nn), and (In) on a run of the text ------------------ */
 
805
 
 
806
/*
 
807
 * This implementation of the (Wn) rules applies all rules in one pass.
 
808
 * In order to do so, it needs a look-ahead of typically 1 character
 
809
 * (except for W5: sequences of ET) and keeps track of changes
 
810
 * in a rule Wp that affect a later Wq (p<q).
 
811
 *
 
812
 * historyOfEN is a variable-saver: it contains 4 boolean states;
 
813
 * a bit in it set to 1 means:
 
814
 *  bit 0: the current code is an EN after W2
 
815
 *  bit 1: the current code is an EN after W4
 
816
 *  bit 2: the previous code was an EN after W2
 
817
 *  bit 3: the previous code was an EN after W4
 
818
 * In other words, b0..1 have transitions of EN in the current iteration,
 
819
 * while b2..3 have the transitions of EN in the previous iteration.
 
820
 * A simple historyOfEN<<=2 suffices for the propagation.
 
821
 *
 
822
 * The (Nn) and (In) rules are also performed in that same single loop,
 
823
 * but effectively one iteration behind for white space.
 
824
 *
 
825
 * Since all implicit rules are performed in one step, it is not necessary
 
826
 * to actually store the intermediate directional properties in dirProps[].
 
827
 */
 
828
 
 
829
#define EN_SHIFT 2
 
830
#define EN_AFTER_W2 1
 
831
#define EN_AFTER_W4 2
 
832
#define EN_ALL 3
 
833
#define PREV_EN_AFTER_W2 4
 
834
#define PREV_EN_AFTER_W4 8
 
835
 
 
836
static void
 
837
resolveImplicitLevels(UBiDi *pBiDi,
 
838
                      UTextOffset start, UTextOffset limit,
 
839
                      DirProp sor, DirProp eor) {
 
840
    const DirProp *dirProps=pBiDi->dirProps;
 
841
    UBiDiLevel *levels=pBiDi->levels;
 
842
 
 
843
    UTextOffset i, next, neutralStart=-1;
 
844
    DirProp prevDirProp, dirProp, nextDirProp, lastStrong, beforeNeutral=L;
 
845
    UBiDiLevel numberLevel;
 
846
    uint8_t historyOfEN;
 
847
 
 
848
    /* initialize: current at sor, next at start (it is start<limit) */
 
849
    next=start;
 
850
    dirProp=lastStrong=sor;
 
851
    nextDirProp=dirProps[next];
 
852
    historyOfEN=0;
 
853
 
 
854
    if(pBiDi->isInverse) {
 
855
        /*
 
856
         * For "inverse BiDi", we set the levels of numbers just like for
 
857
         * regular L characters, plus a flag that ubidi_getRuns() will use
 
858
         * to set a similar flag on the corresponding output run.
 
859
         */
 
860
        numberLevel=levels[start];
 
861
        if(numberLevel&1) {
 
862
            ++numberLevel;
 
863
        }
 
864
    } else {
 
865
        /* normal BiDi: least greater even level */
 
866
        numberLevel=(UBiDiLevel)((levels[start]+2)&~1);
 
867
    }
 
868
 
 
869
    /*
 
870
     * In all steps of this implementation, BN and explicit embedding codes
 
871
     * must be treated as if they didn't exist (X9).
 
872
     * They will get levels set before a non-neutral character, and remain
 
873
     * undefined before a neutral one, but adjustWSLevels() will take care
 
874
     * of all of them.
 
875
     */
 
876
    while(DIRPROP_FLAG(nextDirProp)&MASK_BN_EXPLICIT) {
 
877
        if(++next<limit) {
 
878
            nextDirProp=dirProps[next];
 
879
        } else {
 
880
            nextDirProp=eor;
 
881
            break;
 
882
        }
 
883
    }
 
884
 
 
885
    /*
 
886
     * Note: at the end of this file, there is a prototype
 
887
     * of a version of this function that uses a statetable
 
888
     * at the core of this state machine.
 
889
     * If you make changes to this state machine,
 
890
     * please update that prototype as well.
 
891
     */
 
892
 
 
893
    /* loop for entire run */
 
894
    while(next<limit) {
 
895
        /* advance */
 
896
        prevDirProp=dirProp;
 
897
        dirProp=nextDirProp;
 
898
        i=next;
 
899
        do {
 
900
            if(++next<limit) {
 
901
                nextDirProp=dirProps[next];
 
902
            } else {
 
903
                nextDirProp=eor;
 
904
                break;
 
905
            }
 
906
        } while(DIRPROP_FLAG(nextDirProp)&MASK_BN_EXPLICIT);
 
907
        historyOfEN<<=EN_SHIFT;
 
908
 
 
909
        /* (W1..W7) */
 
910
        switch(dirProp) {
 
911
        case L:
 
912
            lastStrong=L;
 
913
            break;
 
914
        case R:
 
915
            lastStrong=R;
 
916
            break;
 
917
        case AL:
 
918
            /* (W3) */
 
919
            lastStrong=AL;
 
920
            dirProp=R;
 
921
            break;
 
922
        case EN:
 
923
            /* we have to set historyOfEN correctly */
 
924
            if(lastStrong==AL) {
 
925
                /* (W2) */
 
926
                dirProp=AN;
 
927
            } else {
 
928
                if(lastStrong==L) {
 
929
                    /* (W7) */
 
930
                    dirProp=L;
 
931
                }
 
932
                /* this EN stays after (W2) and (W4) - at least before (W7) */
 
933
                historyOfEN|=EN_ALL;
 
934
            }
 
935
            break;
 
936
        case ES:
 
937
            if( historyOfEN&PREV_EN_AFTER_W2 &&     /* previous was EN before (W4) */
 
938
                nextDirProp==EN && lastStrong!=AL   /* next is EN and (W2) won't make it AN */
 
939
            ) {
 
940
                /* (W4) */
 
941
                if(lastStrong!=L) {
 
942
                    dirProp=EN;
 
943
                } else {
 
944
                    /* (W7) */
 
945
                    dirProp=L;
 
946
                }
 
947
                historyOfEN|=EN_AFTER_W4;
 
948
            } else {
 
949
                /* (W6) */
 
950
                dirProp=ON;
 
951
            }
 
952
            break;
 
953
        case CS:
 
954
            if( historyOfEN&PREV_EN_AFTER_W2 &&     /* previous was EN before (W4) */
 
955
                nextDirProp==EN && lastStrong!=AL   /* next is EN and (W2) won't make it AN */
 
956
            ) {
 
957
                /* (W4) */
 
958
                if(lastStrong!=L) {
 
959
                    dirProp=EN;
 
960
                } else {
 
961
                    /* (W7) */
 
962
                    dirProp=L;
 
963
                }
 
964
                historyOfEN|=EN_AFTER_W4;
 
965
            } else if(prevDirProp==AN &&                    /* previous was AN */
 
966
                      (nextDirProp==AN ||                   /* next is AN */
 
967
                      (nextDirProp==EN && lastStrong==AL))  /* or (W2) will make it one */
 
968
            ) {
 
969
                /* (W4) */
 
970
                dirProp=AN;
 
971
            } else {
 
972
                /* (W6) */
 
973
                dirProp=ON;
 
974
            }
 
975
            break;
 
976
        case ET:
 
977
            /* get sequence of ET; advance only next, not current, previous or historyOfEN */
 
978
            if(next<limit) {
 
979
                while(DIRPROP_FLAG(nextDirProp)&MASK_ET_NSM_BN /* (W1), (X9) */) {
 
980
                    if(++next<limit) {
 
981
                        nextDirProp=dirProps[next];
 
982
                    } else {
 
983
                        nextDirProp=eor;
 
984
                        break;
 
985
                    }
 
986
                }
 
987
            }
 
988
 
 
989
            /* now process the sequence of ET like a single ET */
 
990
            if((historyOfEN&PREV_EN_AFTER_W4) ||     /* previous was EN before (W5) */
 
991
                (nextDirProp==EN && lastStrong!=AL)   /* next is EN and (W2) won't make it AN */
 
992
            ) {
 
993
                /* (W5) */
 
994
                if(lastStrong!=L) {
 
995
                    dirProp=EN;
 
996
                } else {
 
997
                    /* (W7) */
 
998
                    dirProp=L;
 
999
                }
 
1000
            } else {
 
1001
                /* (W6) */
 
1002
                dirProp=ON;
 
1003
            }
 
1004
 
 
1005
            /* apply the result of (W1), (W5)..(W7) to the entire sequence of ET */
 
1006
            break;
 
1007
        case NSM:
 
1008
            /* (W1) */
 
1009
            dirProp=prevDirProp;
 
1010
            /* set historyOfEN back to prevDirProp's historyOfEN */
 
1011
            historyOfEN>>=EN_SHIFT;
 
1012
            /*
 
1013
             * Technically, this should be done before the switch() in the form
 
1014
             *      if(nextDirProp==NSM) {
 
1015
             *          dirProps[next]=nextDirProp=dirProp;
 
1016
             *      }
 
1017
             *
 
1018
             * - effectively one iteration ahead.
 
1019
             * However, whether the next dirProp is NSM or is equal to the current dirProp
 
1020
             * does not change the outcome of any condition in (W2)..(W7).
 
1021
             */
 
1022
            break;
 
1023
        default:
 
1024
            break;
 
1025
        }
 
1026
 
 
1027
        /* here, it is always [prev,this,next]dirProp!=BN; it may be next>i+1 */
 
1028
 
 
1029
        /* perform (Nn) - here, only L, R, EN, AN, and neutrals are left */
 
1030
        /* for "inverse BiDi", treat neutrals like L */
 
1031
        /* this is one iteration late for the neutrals */
 
1032
        if(DIRPROP_FLAG(dirProp)&MASK_N) {
 
1033
            if(neutralStart<0) {
 
1034
                /* start of a sequence of neutrals */
 
1035
                neutralStart=i;
 
1036
                beforeNeutral=prevDirProp;
 
1037
            }
 
1038
        } else /* not a neutral, can be only one of { L, R, EN, AN } */ {
 
1039
            /*
 
1040
             * Note that all levels[] values are still the same at this
 
1041
             * point because this function is called for an entire
 
1042
             * same-level run.
 
1043
             * Therefore, we need to read only one actual level.
 
1044
             */
 
1045
            UBiDiLevel level=levels[i];
 
1046
 
 
1047
            if(neutralStart>=0) {
 
1048
                UBiDiLevel final;
 
1049
                /* end of a sequence of neutrals (dirProp is "afterNeutral") */
 
1050
                if(!(pBiDi->isInverse)) {
 
1051
                    if(beforeNeutral==L) {
 
1052
                        if(dirProp==L) {
 
1053
                            final=0;                /* make all neutrals L (N1) */
 
1054
                        } else {
 
1055
                            final=level;            /* make all neutrals "e" (N2) */
 
1056
                        }
 
1057
                    } else /* beforeNeutral is one of { R, EN, AN } */ {
 
1058
                        if(dirProp==L) {
 
1059
                            final=level;            /* make all neutrals "e" (N2) */
 
1060
                        } else {
 
1061
                            final=1;                /* make all neutrals R (N1) */
 
1062
                        }
 
1063
                    }
 
1064
                } else {
 
1065
                    /* "inverse BiDi": collapse [before]dirProps L, EN, AN into L */
 
1066
                    if(beforeNeutral!=R) {
 
1067
                        if(dirProp!=R) {
 
1068
                            final=0;                /* make all neutrals L (N1) */
 
1069
                        } else {
 
1070
                            final=level;            /* make all neutrals "e" (N2) */
 
1071
                        }
 
1072
                    } else /* beforeNeutral is one of { R, EN, AN } */ {
 
1073
                        if(dirProp!=R) {
 
1074
                            final=level;            /* make all neutrals "e" (N2) */
 
1075
                        } else {
 
1076
                            final=1;                /* make all neutrals R (N1) */
 
1077
                        }
 
1078
                    }
 
1079
                }
 
1080
                /* perform (In) on the sequence of neutrals */
 
1081
                if((level^final)&1) {
 
1082
                    /* do something only if we need to _change_ the level */
 
1083
                    do {
 
1084
                        ++levels[neutralStart];
 
1085
                    } while(++neutralStart<i);
 
1086
                }
 
1087
                neutralStart=-1;
 
1088
            }
 
1089
 
 
1090
            /* perform (In) on the non-neutral character */
 
1091
            /*
 
1092
             * in the cases of (W5), processing a sequence of ET,
 
1093
             * and of (X9), skipping BN,
 
1094
             * there may be multiple characters from i to <next
 
1095
             * that all get (virtually) the same dirProp and (really) the same level
 
1096
             */
 
1097
            if(dirProp==L) {
 
1098
                if(level&1) {
 
1099
                    ++level;
 
1100
                } else {
 
1101
                    i=next;     /* we keep the levels */
 
1102
                }
 
1103
            } else if(dirProp==R) {
 
1104
                if(!(level&1)) {
 
1105
                    ++level;
 
1106
                } else {
 
1107
                    i=next;     /* we keep the levels */
 
1108
                }
 
1109
            } else /* EN or AN */ {
 
1110
                /* this level depends on whether we do "inverse BiDi" */
 
1111
                level=numberLevel;
 
1112
            }
 
1113
 
 
1114
            /* apply the new level to the sequence, if necessary */
 
1115
            while(i<next) {
 
1116
                levels[i++]=level;
 
1117
            }
 
1118
        }
 
1119
    }
 
1120
 
 
1121
    /* perform (Nn) - here,
 
1122
       the character after the the neutrals is eor, which is either L or R */
 
1123
    /* this is one iteration late for the neutrals */
 
1124
    if(neutralStart>=0) {
 
1125
        /*
 
1126
         * Note that all levels[] values are still the same at this
 
1127
         * point because this function is called for an entire
 
1128
         * same-level run.
 
1129
         * Therefore, we need to read only one actual level.
 
1130
         */
 
1131
        UBiDiLevel level=levels[neutralStart], final;
 
1132
 
 
1133
        /* end of a sequence of neutrals (eor is "afterNeutral") */
 
1134
        if(!(pBiDi->isInverse)) {
 
1135
            if(beforeNeutral==L) {
 
1136
                if(eor==L) {
 
1137
                    final=0;                /* make all neutrals L (N1) */
 
1138
                } else {
 
1139
                    final=level;            /* make all neutrals "e" (N2) */
 
1140
                }
 
1141
            } else /* beforeNeutral is one of { R, EN, AN } */ {
 
1142
                if(eor==L) {
 
1143
                    final=level;            /* make all neutrals "e" (N2) */
 
1144
                } else {
 
1145
                    final=1;                /* make all neutrals R (N1) */
 
1146
                }
 
1147
            }
 
1148
        } else {
 
1149
            /* "inverse BiDi": collapse [before]dirProps L, EN, AN into L */
 
1150
            if(beforeNeutral!=R) {
 
1151
                if(eor!=R) {
 
1152
                    final=0;                /* make all neutrals L (N1) */
 
1153
                } else {
 
1154
                    final=level;            /* make all neutrals "e" (N2) */
 
1155
                }
 
1156
            } else /* beforeNeutral is one of { R, EN, AN } */ {
 
1157
                if(eor!=R) {
 
1158
                    final=level;            /* make all neutrals "e" (N2) */
 
1159
                } else {
 
1160
                    final=1;                /* make all neutrals R (N1) */
 
1161
                }
 
1162
            }
 
1163
        }
 
1164
        /* perform (In) on the sequence of neutrals */
 
1165
        if((level^final)&1) {
 
1166
            /* do something only if we need to _change_ the level */
 
1167
            do {
 
1168
                ++levels[neutralStart];
 
1169
            } while(++neutralStart<limit);
 
1170
        }
 
1171
    }
 
1172
}
 
1173
 
 
1174
/* perform (L1) and (X9) ---------------------------------------------------- */
 
1175
 
 
1176
/*
 
1177
 * Reset the embedding levels for some non-graphic characters (L1).
 
1178
 * This function also sets appropriate levels for BN, and
 
1179
 * explicit embedding types that are supposed to have been removed
 
1180
 * from the paragraph in (X9).
 
1181
 */
 
1182
static void
 
1183
adjustWSLevels(UBiDi *pBiDi) {
 
1184
    const DirProp *dirProps=pBiDi->dirProps;
 
1185
    UBiDiLevel *levels=pBiDi->levels;
 
1186
    UTextOffset i;
 
1187
 
 
1188
    if(pBiDi->flags&MASK_WS) {
 
1189
        UBiDiLevel paraLevel=pBiDi->paraLevel;
 
1190
        Flags flag;
 
1191
 
 
1192
        i=pBiDi->trailingWSStart;
 
1193
        while(i>0) {
 
1194
            /* reset a sequence of WS/BN before eop and B/S to the paragraph paraLevel */
 
1195
            while(i>0 && DIRPROP_FLAG(dirProps[--i])&MASK_WS) {
 
1196
                levels[i]=paraLevel;
 
1197
            }
 
1198
 
 
1199
            /* reset BN to the next character's paraLevel until B/S, which restarts above loop */
 
1200
            /* here, i+1 is guaranteed to be <length */
 
1201
            while(i>0) {
 
1202
                flag=DIRPROP_FLAG(dirProps[--i]);
 
1203
                if(flag&MASK_BN_EXPLICIT) {
 
1204
                    levels[i]=levels[i+1];
 
1205
                } else if(flag&MASK_B_S) {
 
1206
                    levels[i]=paraLevel;
 
1207
                    break;
 
1208
                }
 
1209
            }
 
1210
        }
 
1211
    }
 
1212
}
 
1213
 
 
1214
/* -------------------------------------------------------------------------- */
 
1215
 
 
1216
U_CAPI UBiDiDirection U_EXPORT2
 
1217
ubidi_getDirection(const UBiDi *pBiDi) {
 
1218
    if(pBiDi!=NULL) {
 
1219
        return pBiDi->direction;
 
1220
    } else {
 
1221
        return UBIDI_LTR;
 
1222
    }
 
1223
}
 
1224
 
 
1225
U_CAPI const UChar * U_EXPORT2
 
1226
ubidi_getText(const UBiDi *pBiDi) {
 
1227
    if(pBiDi!=NULL) {
 
1228
        return pBiDi->text;
 
1229
    } else {
 
1230
        return NULL;
 
1231
    }
 
1232
}
 
1233
 
 
1234
U_CAPI UTextOffset U_EXPORT2
 
1235
ubidi_getLength(const UBiDi *pBiDi) {
 
1236
    if(pBiDi!=NULL) {
 
1237
        return pBiDi->length;
 
1238
    } else {
 
1239
        return 0;
 
1240
    }
 
1241
}
 
1242
 
 
1243
U_CAPI UBiDiLevel U_EXPORT2
 
1244
ubidi_getParaLevel(const UBiDi *pBiDi) {
 
1245
    if(pBiDi!=NULL) {
 
1246
        return pBiDi->paraLevel;
 
1247
    } else {
 
1248
        return 0;
 
1249
    }
 
1250
}
 
1251
 
 
1252
/* statetable prototype ----------------------------------------------------- */
 
1253
 
 
1254
/*
 
1255
 * This is here for possible future
 
1256
 * performance work and is not compiled right now.
 
1257
 */
 
1258
 
 
1259
#if 0
 
1260
/*
 
1261
 * This is a piece of code that could be part of ubidi.c/resolveImplicitLevels().
 
1262
 * It replaces in the (Wn) state machine the switch()-if()-cascade with
 
1263
 * just a few if()s and a state table.
 
1264
 */
 
1265
 
 
1266
/* use the state table only for the following dirProp's */
 
1267
#define MASK_W_TABLE (FLAG(L)|FLAG(R)|FLAG(AL)|FLAG(EN)|FLAG(ES)|FLAG(CS)|FLAG(ET)|FLAG(AN))
 
1268
 
 
1269
/*
 
1270
 * inputs:
 
1271
 *
 
1272
 * 0..1 historyOfEN - 2b
 
1273
 * 2    prevDirProp==AN - 1b
 
1274
 * 3..4 lastStrong, one of { L, R, AL, none } - 2b
 
1275
 * 5..7 dirProp, one of { L, R, AL, EN, ES, CS, ET, AN } - 3b
 
1276
 * 8..9 nextDirProp, one of { EN, AN, other }
 
1277
 *
 
1278
 * total: 10b=1024 states
 
1279
 */
 
1280
enum { _L, _R, _AL, _EN, _ES, _CS, _ET, _AN, _OTHER };  /* lastStrong, dirProp */
 
1281
enum { __EN, __AN, __OTHER };                           /* nextDirProp */
 
1282
 
 
1283
#define LAST_STRONG_SHIFT 3
 
1284
#define DIR_PROP_SHIFT 5
 
1285
#define NEXT_DIR_PROP_SHIFT 8
 
1286
 
 
1287
/* masks after shifting */
 
1288
#define LAST_STRONG_MASK 3
 
1289
#define DIR_PROP_MASK 7
 
1290
#define STATE_MASK 0x1f
 
1291
 
 
1292
/* convert dirProp into _dirProp (above enum) */
 
1293
static DirProp inputDirProp[dirPropCount]={ _X<<DIR_PROP_SHIFT, ... };
 
1294
 
 
1295
/* convert dirProp into __dirProp (above enum) */
 
1296
static DirProp inputNextDirProp[dirPropCount]={ __X<<NEXT_DIR_PROP_SHIFT, ... };
 
1297
 
 
1298
/*
 
1299
 * outputs:
 
1300
 *
 
1301
 * dirProp, one of { L, R, EN, AN, ON } - 3b
 
1302
 *
 
1303
 * 0..1 historyOfEN - 2b
 
1304
 * 2    prevDirProp==AN - 1b
 
1305
 * 3..4 lastStrong, one of { L, R, AL, none } - 2b
 
1306
 * 5..7 new dirProp, one of { L, R, EN, AN, ON }
 
1307
 *
 
1308
 * total: 8 bits=1 byte per state
 
1309
 */
 
1310
enum { ___L, ___R, ___EN, ___AN, ___ON, ___count };
 
1311
 
 
1312
/* convert ___dirProp into dirProp (above enum) */
 
1313
static DirProp outputDirProp[___count]={ X, ... };
 
1314
 
 
1315
/* state table */
 
1316
static uint8_t wnTable[1024]={ /* calculate with switch()-if()-cascade */ };
 
1317
 
 
1318
static void
 
1319
resolveImplicitLevels(BiDi *pBiDi,
 
1320
                      Index start, Index end,
 
1321
                      DirProp sor, DirProp eor) {
 
1322
    /* new variable */
 
1323
    uint8_t state;
 
1324
 
 
1325
    /* remove variable lastStrong */
 
1326
 
 
1327
    /* set initial state (set lastStrong, the rest is 0) */
 
1328
    state= sor==L ? 0 : _R<<LAST_STRONG_SHIFT;
 
1329
 
 
1330
    while(next<limit) {
 
1331
        /* advance */
 
1332
        prevDirProp=dirProp;
 
1333
        dirProp=nextDirProp;
 
1334
        i=next;
 
1335
        do {
 
1336
            if(++next<limit) {
 
1337
                nextDirProp=dirProps[next];
 
1338
            } else {
 
1339
                nextDirProp=eor;
 
1340
                break;
 
1341
            }
 
1342
        } while(FLAG(nextDirProp)&MASK_BN_EXPLICIT);
 
1343
 
 
1344
        /* (W1..W7) */
 
1345
        /* ### This may be more efficient with a switch(dirProp). */
 
1346
        if(FLAG(dirProp)&MASK_W_TABLE) {
 
1347
            state=wnTable[
 
1348
                    ((int)state)|
 
1349
                    inputDirProp[dirProp]|
 
1350
                    inputNextDirProp[nextDirProp]
 
1351
            ];
 
1352
            dirProp=outputDirProp[state>>DIR_PROP_SHIFT];
 
1353
            state&=STATE_MASK;
 
1354
        } else if(dirProp==ET) {
 
1355
            /* get sequence of ET; advance only next, not current, previous or historyOfEN */
 
1356
            while(next<limit && FLAG(nextDirProp)&MASK_ET_NSM_BN /* (W1), (X9) */) {
 
1357
                if(++next<limit) {
 
1358
                    nextDirProp=dirProps[next];
 
1359
                } else {
 
1360
                    nextDirProp=eor;
 
1361
                    break;
 
1362
                }
 
1363
            }
 
1364
 
 
1365
            state=wnTable[
 
1366
                    ((int)state)|
 
1367
                    _ET<<DIR_PROP_SHIFT|
 
1368
                    inputNextDirProp[nextDirProp]
 
1369
            ];
 
1370
            dirProp=outputDirProp[state>>DIR_PROP_SHIFT];
 
1371
            state&=STATE_MASK;
 
1372
 
 
1373
            /* apply the result of (W1), (W5)..(W7) to the entire sequence of ET */
 
1374
        } else if(dirProp==NSM) {
 
1375
            /* (W1) */
 
1376
            dirProp=prevDirProp;
 
1377
            /* keep prevDirProp's EN and AN states! */
 
1378
        } else /* other */ {
 
1379
            /* set EN and AN states to 0 */
 
1380
            state&=LAST_STRONG_MASK<<LAST_STRONG_SHIFT;
 
1381
        }
 
1382
 
 
1383
        /* perform (Nn) and (In) as usual */
 
1384
    }
 
1385
    /* perform (Nn) and (In) as usual */
 
1386
}
 
1387
#endif