1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker ******************************************************************************
5*0e209d39SAndroid Build Coastguard Worker *
6*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 1999-2015, International Business Machines
7*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved.
8*0e209d39SAndroid Build Coastguard Worker *
9*0e209d39SAndroid Build Coastguard Worker ******************************************************************************
10*0e209d39SAndroid Build Coastguard Worker * file name: ubidi.c
11*0e209d39SAndroid Build Coastguard Worker * encoding: UTF-8
12*0e209d39SAndroid Build Coastguard Worker * tab size: 8 (not used)
13*0e209d39SAndroid Build Coastguard Worker * indentation:4
14*0e209d39SAndroid Build Coastguard Worker *
15*0e209d39SAndroid Build Coastguard Worker * created on: 1999jul27
16*0e209d39SAndroid Build Coastguard Worker * created by: Markus W. Scherer, updated by Matitiahu Allouche
17*0e209d39SAndroid Build Coastguard Worker *
18*0e209d39SAndroid Build Coastguard Worker */
19*0e209d39SAndroid Build Coastguard Worker
20*0e209d39SAndroid Build Coastguard Worker #include "cmemory.h"
21*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
22*0e209d39SAndroid Build Coastguard Worker #include "unicode/ustring.h"
23*0e209d39SAndroid Build Coastguard Worker #include "unicode/uchar.h"
24*0e209d39SAndroid Build Coastguard Worker #include "unicode/ubidi.h"
25*0e209d39SAndroid Build Coastguard Worker #include "unicode/utf16.h"
26*0e209d39SAndroid Build Coastguard Worker #include "ubidi_props.h"
27*0e209d39SAndroid Build Coastguard Worker #include "ubidiimp.h"
28*0e209d39SAndroid Build Coastguard Worker #include "uassert.h"
29*0e209d39SAndroid Build Coastguard Worker
30*0e209d39SAndroid Build Coastguard Worker /*
31*0e209d39SAndroid Build Coastguard Worker * General implementation notes:
32*0e209d39SAndroid Build Coastguard Worker *
33*0e209d39SAndroid Build Coastguard Worker * Throughout the implementation, there are comments like (W2) that refer to
34*0e209d39SAndroid Build Coastguard Worker * rules of the BiDi algorithm, in this example to the second rule of the
35*0e209d39SAndroid Build Coastguard Worker * resolution of weak types.
36*0e209d39SAndroid Build Coastguard Worker *
37*0e209d39SAndroid Build Coastguard Worker * For handling surrogate pairs, where two char16_t's form one "abstract" (or UTF-32)
38*0e209d39SAndroid Build Coastguard Worker * character according to UTF-16, the second char16_t gets the directional property of
39*0e209d39SAndroid Build Coastguard Worker * the entire character assigned, while the first one gets a BN, a boundary
40*0e209d39SAndroid Build Coastguard Worker * neutral, type, which is ignored by most of the algorithm according to
41*0e209d39SAndroid Build Coastguard Worker * rule (X9) and the implementation suggestions of the BiDi algorithm.
42*0e209d39SAndroid Build Coastguard Worker *
43*0e209d39SAndroid Build Coastguard Worker * Later, adjustWSLevels() will set the level for each BN to that of the
44*0e209d39SAndroid Build Coastguard Worker * following character (char16_t), which results in surrogate pairs getting the
45*0e209d39SAndroid Build Coastguard Worker * same level on each of their surrogates.
46*0e209d39SAndroid Build Coastguard Worker *
47*0e209d39SAndroid Build Coastguard Worker * In a UTF-8 implementation, the same thing could be done: the last byte of
48*0e209d39SAndroid Build Coastguard Worker * a multi-byte sequence would get the "real" property, while all previous
49*0e209d39SAndroid Build Coastguard Worker * bytes of that sequence would get BN.
50*0e209d39SAndroid Build Coastguard Worker *
51*0e209d39SAndroid Build Coastguard Worker * It is not possible to assign all those parts of a character the same real
52*0e209d39SAndroid Build Coastguard Worker * property because this would fail in the resolution of weak types with rules
53*0e209d39SAndroid Build Coastguard Worker * that look at immediately surrounding types.
54*0e209d39SAndroid Build Coastguard Worker *
55*0e209d39SAndroid Build Coastguard Worker * As a related topic, this implementation does not remove Boundary Neutral
56*0e209d39SAndroid Build Coastguard Worker * types from the input, but ignores them wherever this is relevant.
57*0e209d39SAndroid Build Coastguard Worker * For example, the loop for the resolution of the weak types reads
58*0e209d39SAndroid Build Coastguard Worker * types until it finds a non-BN.
59*0e209d39SAndroid Build Coastguard Worker * Also, explicit embedding codes are neither changed into BN nor removed.
60*0e209d39SAndroid Build Coastguard Worker * They are only treated the same way real BNs are.
61*0e209d39SAndroid Build Coastguard Worker * As stated before, adjustWSLevels() takes care of them at the end.
62*0e209d39SAndroid Build Coastguard Worker * For the purpose of conformance, the levels of all these codes
63*0e209d39SAndroid Build Coastguard Worker * do not matter.
64*0e209d39SAndroid Build Coastguard Worker *
65*0e209d39SAndroid Build Coastguard Worker * Note that this implementation modifies the dirProps
66*0e209d39SAndroid Build Coastguard Worker * after the initial setup, when applying X5c (replace FSI by LRI or RLI),
67*0e209d39SAndroid Build Coastguard Worker * X6, N0 (replace paired brackets by L or R).
68*0e209d39SAndroid Build Coastguard Worker *
69*0e209d39SAndroid Build Coastguard Worker * In this implementation, the resolution of weak types (W1 to W6),
70*0e209d39SAndroid Build Coastguard Worker * neutrals (N1 and N2), and the assignment of the resolved level (In)
71*0e209d39SAndroid Build Coastguard Worker * are all done in one single loop, in resolveImplicitLevels().
72*0e209d39SAndroid Build Coastguard Worker * Changes of dirProp values are done on the fly, without writing
73*0e209d39SAndroid Build Coastguard Worker * them back to the dirProps array.
74*0e209d39SAndroid Build Coastguard Worker *
75*0e209d39SAndroid Build Coastguard Worker *
76*0e209d39SAndroid Build Coastguard Worker * This implementation contains code that allows to bypass steps of the
77*0e209d39SAndroid Build Coastguard Worker * algorithm that are not needed on the specific paragraph
78*0e209d39SAndroid Build Coastguard Worker * in order to speed up the most common cases considerably,
79*0e209d39SAndroid Build Coastguard Worker * like text that is entirely LTR, or RTL text without numbers.
80*0e209d39SAndroid Build Coastguard Worker *
81*0e209d39SAndroid Build Coastguard Worker * Most of this is done by setting a bit for each directional property
82*0e209d39SAndroid Build Coastguard Worker * in a flags variable and later checking for whether there are
83*0e209d39SAndroid Build Coastguard Worker * any LTR characters or any RTL characters, or both, whether
84*0e209d39SAndroid Build Coastguard Worker * there are any explicit embedding codes, etc.
85*0e209d39SAndroid Build Coastguard Worker *
86*0e209d39SAndroid Build Coastguard Worker * If the (Xn) steps are performed, then the flags are re-evaluated,
87*0e209d39SAndroid Build Coastguard Worker * because they will then not contain the embedding codes any more
88*0e209d39SAndroid Build Coastguard Worker * and will be adjusted for override codes, so that subsequently
89*0e209d39SAndroid Build Coastguard Worker * more bypassing may be possible than what the initial flags suggested.
90*0e209d39SAndroid Build Coastguard Worker *
91*0e209d39SAndroid Build Coastguard Worker * If the text is not mixed-directional, then the
92*0e209d39SAndroid Build Coastguard Worker * algorithm steps for the weak type resolution are not performed,
93*0e209d39SAndroid Build Coastguard Worker * and all levels are set to the paragraph level.
94*0e209d39SAndroid Build Coastguard Worker *
95*0e209d39SAndroid Build Coastguard Worker * If there are no explicit embedding codes, then the (Xn) steps
96*0e209d39SAndroid Build Coastguard Worker * are not performed.
97*0e209d39SAndroid Build Coastguard Worker *
98*0e209d39SAndroid Build Coastguard Worker * If embedding levels are supplied as a parameter, then all
99*0e209d39SAndroid Build Coastguard Worker * explicit embedding codes are ignored, and the (Xn) steps
100*0e209d39SAndroid Build Coastguard Worker * are not performed.
101*0e209d39SAndroid Build Coastguard Worker *
102*0e209d39SAndroid Build Coastguard Worker * White Space types could get the level of the run they belong to,
103*0e209d39SAndroid Build Coastguard Worker * and are checked with a test of (flags&MASK_EMBEDDING) to
104*0e209d39SAndroid Build Coastguard Worker * consider if the paragraph direction should be considered in
105*0e209d39SAndroid Build Coastguard Worker * the flags variable.
106*0e209d39SAndroid Build Coastguard Worker *
107*0e209d39SAndroid Build Coastguard Worker * If there are no White Space types in the paragraph, then
108*0e209d39SAndroid Build Coastguard Worker * (L1) is not necessary in adjustWSLevels().
109*0e209d39SAndroid Build Coastguard Worker */
110*0e209d39SAndroid Build Coastguard Worker
111*0e209d39SAndroid Build Coastguard Worker /* to avoid some conditional statements, use tiny constant arrays */
112*0e209d39SAndroid Build Coastguard Worker static const Flags flagLR[2]={ DIRPROP_FLAG(L), DIRPROP_FLAG(R) };
113*0e209d39SAndroid Build Coastguard Worker static const Flags flagE[2]={ DIRPROP_FLAG(LRE), DIRPROP_FLAG(RLE) };
114*0e209d39SAndroid Build Coastguard Worker static const Flags flagO[2]={ DIRPROP_FLAG(LRO), DIRPROP_FLAG(RLO) };
115*0e209d39SAndroid Build Coastguard Worker
116*0e209d39SAndroid Build Coastguard Worker #define DIRPROP_FLAG_LR(level) flagLR[(level)&1]
117*0e209d39SAndroid Build Coastguard Worker #define DIRPROP_FLAG_E(level) flagE[(level)&1]
118*0e209d39SAndroid Build Coastguard Worker #define DIRPROP_FLAG_O(level) flagO[(level)&1]
119*0e209d39SAndroid Build Coastguard Worker
120*0e209d39SAndroid Build Coastguard Worker #define DIR_FROM_STRONG(strong) ((strong)==L ? L : R)
121*0e209d39SAndroid Build Coastguard Worker
122*0e209d39SAndroid Build Coastguard Worker #define NO_OVERRIDE(level) ((level)&~UBIDI_LEVEL_OVERRIDE)
123*0e209d39SAndroid Build Coastguard Worker
124*0e209d39SAndroid Build Coastguard Worker /* UBiDi object management -------------------------------------------------- */
125*0e209d39SAndroid Build Coastguard Worker
126*0e209d39SAndroid Build Coastguard Worker U_CAPI UBiDi * U_EXPORT2
ubidi_open()127*0e209d39SAndroid Build Coastguard Worker ubidi_open()
128*0e209d39SAndroid Build Coastguard Worker {
129*0e209d39SAndroid Build Coastguard Worker UErrorCode errorCode=U_ZERO_ERROR;
130*0e209d39SAndroid Build Coastguard Worker return ubidi_openSized(0, 0, &errorCode);
131*0e209d39SAndroid Build Coastguard Worker }
132*0e209d39SAndroid Build Coastguard Worker
133*0e209d39SAndroid Build Coastguard Worker U_CAPI UBiDi * U_EXPORT2
ubidi_openSized(int32_t maxLength,int32_t maxRunCount,UErrorCode * pErrorCode)134*0e209d39SAndroid Build Coastguard Worker ubidi_openSized(int32_t maxLength, int32_t maxRunCount, UErrorCode *pErrorCode) {
135*0e209d39SAndroid Build Coastguard Worker UBiDi *pBiDi;
136*0e209d39SAndroid Build Coastguard Worker
137*0e209d39SAndroid Build Coastguard Worker /* check the argument values */
138*0e209d39SAndroid Build Coastguard Worker if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
139*0e209d39SAndroid Build Coastguard Worker return nullptr;
140*0e209d39SAndroid Build Coastguard Worker } else if(maxLength<0 || maxRunCount<0) {
141*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
142*0e209d39SAndroid Build Coastguard Worker return nullptr; /* invalid arguments */
143*0e209d39SAndroid Build Coastguard Worker }
144*0e209d39SAndroid Build Coastguard Worker
145*0e209d39SAndroid Build Coastguard Worker /* allocate memory for the object */
146*0e209d39SAndroid Build Coastguard Worker pBiDi=(UBiDi *)uprv_malloc(sizeof(UBiDi));
147*0e209d39SAndroid Build Coastguard Worker if(pBiDi==nullptr) {
148*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
149*0e209d39SAndroid Build Coastguard Worker return nullptr;
150*0e209d39SAndroid Build Coastguard Worker }
151*0e209d39SAndroid Build Coastguard Worker
152*0e209d39SAndroid Build Coastguard Worker /* reset the object, all pointers nullptr, all flags false, all sizes 0 */
153*0e209d39SAndroid Build Coastguard Worker uprv_memset(pBiDi, 0, sizeof(UBiDi));
154*0e209d39SAndroid Build Coastguard Worker
155*0e209d39SAndroid Build Coastguard Worker /* allocate memory for arrays as requested */
156*0e209d39SAndroid Build Coastguard Worker if(maxLength>0) {
157*0e209d39SAndroid Build Coastguard Worker if( !getInitialDirPropsMemory(pBiDi, maxLength) ||
158*0e209d39SAndroid Build Coastguard Worker !getInitialLevelsMemory(pBiDi, maxLength)
159*0e209d39SAndroid Build Coastguard Worker ) {
160*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
161*0e209d39SAndroid Build Coastguard Worker }
162*0e209d39SAndroid Build Coastguard Worker } else {
163*0e209d39SAndroid Build Coastguard Worker pBiDi->mayAllocateText=true;
164*0e209d39SAndroid Build Coastguard Worker }
165*0e209d39SAndroid Build Coastguard Worker
166*0e209d39SAndroid Build Coastguard Worker if(maxRunCount>0) {
167*0e209d39SAndroid Build Coastguard Worker if(maxRunCount==1) {
168*0e209d39SAndroid Build Coastguard Worker /* use simpleRuns[] */
169*0e209d39SAndroid Build Coastguard Worker pBiDi->runsSize=sizeof(Run);
170*0e209d39SAndroid Build Coastguard Worker } else if(!getInitialRunsMemory(pBiDi, maxRunCount)) {
171*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
172*0e209d39SAndroid Build Coastguard Worker }
173*0e209d39SAndroid Build Coastguard Worker } else {
174*0e209d39SAndroid Build Coastguard Worker pBiDi->mayAllocateRuns=true;
175*0e209d39SAndroid Build Coastguard Worker }
176*0e209d39SAndroid Build Coastguard Worker
177*0e209d39SAndroid Build Coastguard Worker if(U_SUCCESS(*pErrorCode)) {
178*0e209d39SAndroid Build Coastguard Worker return pBiDi;
179*0e209d39SAndroid Build Coastguard Worker } else {
180*0e209d39SAndroid Build Coastguard Worker ubidi_close(pBiDi);
181*0e209d39SAndroid Build Coastguard Worker return nullptr;
182*0e209d39SAndroid Build Coastguard Worker }
183*0e209d39SAndroid Build Coastguard Worker }
184*0e209d39SAndroid Build Coastguard Worker
185*0e209d39SAndroid Build Coastguard Worker /*
186*0e209d39SAndroid Build Coastguard Worker * We are allowed to allocate memory if memory==nullptr or
187*0e209d39SAndroid Build Coastguard Worker * mayAllocate==true for each array that we need.
188*0e209d39SAndroid Build Coastguard Worker * We also try to grow memory as needed if we
189*0e209d39SAndroid Build Coastguard Worker * allocate it.
190*0e209d39SAndroid Build Coastguard Worker *
191*0e209d39SAndroid Build Coastguard Worker * Assume sizeNeeded>0.
192*0e209d39SAndroid Build Coastguard Worker * If *pMemory!=nullptr, then assume *pSize>0.
193*0e209d39SAndroid Build Coastguard Worker *
194*0e209d39SAndroid Build Coastguard Worker * ### this realloc() may unnecessarily copy the old data,
195*0e209d39SAndroid Build Coastguard Worker * which we know we don't need any more;
196*0e209d39SAndroid Build Coastguard Worker * is this the best way to do this??
197*0e209d39SAndroid Build Coastguard Worker */
198*0e209d39SAndroid Build Coastguard Worker U_CFUNC UBool
ubidi_getMemory(BidiMemoryForAllocation * bidiMem,int32_t * pSize,UBool mayAllocate,int32_t sizeNeeded)199*0e209d39SAndroid Build Coastguard Worker ubidi_getMemory(BidiMemoryForAllocation *bidiMem, int32_t *pSize, UBool mayAllocate, int32_t sizeNeeded) {
200*0e209d39SAndroid Build Coastguard Worker void **pMemory = (void **)bidiMem;
201*0e209d39SAndroid Build Coastguard Worker /* check for existing memory */
202*0e209d39SAndroid Build Coastguard Worker if(*pMemory==nullptr) {
203*0e209d39SAndroid Build Coastguard Worker /* we need to allocate memory */
204*0e209d39SAndroid Build Coastguard Worker if(mayAllocate && (*pMemory=uprv_malloc(sizeNeeded))!=nullptr) {
205*0e209d39SAndroid Build Coastguard Worker *pSize=sizeNeeded;
206*0e209d39SAndroid Build Coastguard Worker return true;
207*0e209d39SAndroid Build Coastguard Worker } else {
208*0e209d39SAndroid Build Coastguard Worker return false;
209*0e209d39SAndroid Build Coastguard Worker }
210*0e209d39SAndroid Build Coastguard Worker } else {
211*0e209d39SAndroid Build Coastguard Worker if(sizeNeeded<=*pSize) {
212*0e209d39SAndroid Build Coastguard Worker /* there is already enough memory */
213*0e209d39SAndroid Build Coastguard Worker return true;
214*0e209d39SAndroid Build Coastguard Worker }
215*0e209d39SAndroid Build Coastguard Worker else if(!mayAllocate) {
216*0e209d39SAndroid Build Coastguard Worker /* not enough memory, and we must not allocate */
217*0e209d39SAndroid Build Coastguard Worker return false;
218*0e209d39SAndroid Build Coastguard Worker } else {
219*0e209d39SAndroid Build Coastguard Worker /* we try to grow */
220*0e209d39SAndroid Build Coastguard Worker void *memory;
221*0e209d39SAndroid Build Coastguard Worker /* in most cases, we do not need the copy-old-data part of
222*0e209d39SAndroid Build Coastguard Worker * realloc, but it is needed when adding runs using getRunsMemory()
223*0e209d39SAndroid Build Coastguard Worker * in setParaRunsOnly()
224*0e209d39SAndroid Build Coastguard Worker */
225*0e209d39SAndroid Build Coastguard Worker if((memory=uprv_realloc(*pMemory, sizeNeeded))!=nullptr) {
226*0e209d39SAndroid Build Coastguard Worker *pMemory=memory;
227*0e209d39SAndroid Build Coastguard Worker *pSize=sizeNeeded;
228*0e209d39SAndroid Build Coastguard Worker return true;
229*0e209d39SAndroid Build Coastguard Worker } else {
230*0e209d39SAndroid Build Coastguard Worker /* we failed to grow */
231*0e209d39SAndroid Build Coastguard Worker return false;
232*0e209d39SAndroid Build Coastguard Worker }
233*0e209d39SAndroid Build Coastguard Worker }
234*0e209d39SAndroid Build Coastguard Worker }
235*0e209d39SAndroid Build Coastguard Worker }
236*0e209d39SAndroid Build Coastguard Worker
237*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
ubidi_close(UBiDi * pBiDi)238*0e209d39SAndroid Build Coastguard Worker ubidi_close(UBiDi *pBiDi) {
239*0e209d39SAndroid Build Coastguard Worker if(pBiDi!=nullptr) {
240*0e209d39SAndroid Build Coastguard Worker pBiDi->pParaBiDi=nullptr; /* in case one tries to reuse this block */
241*0e209d39SAndroid Build Coastguard Worker if(pBiDi->dirPropsMemory!=nullptr) {
242*0e209d39SAndroid Build Coastguard Worker uprv_free(pBiDi->dirPropsMemory);
243*0e209d39SAndroid Build Coastguard Worker }
244*0e209d39SAndroid Build Coastguard Worker if(pBiDi->levelsMemory!=nullptr) {
245*0e209d39SAndroid Build Coastguard Worker uprv_free(pBiDi->levelsMemory);
246*0e209d39SAndroid Build Coastguard Worker }
247*0e209d39SAndroid Build Coastguard Worker if(pBiDi->openingsMemory!=nullptr) {
248*0e209d39SAndroid Build Coastguard Worker uprv_free(pBiDi->openingsMemory);
249*0e209d39SAndroid Build Coastguard Worker }
250*0e209d39SAndroid Build Coastguard Worker if(pBiDi->parasMemory!=nullptr) {
251*0e209d39SAndroid Build Coastguard Worker uprv_free(pBiDi->parasMemory);
252*0e209d39SAndroid Build Coastguard Worker }
253*0e209d39SAndroid Build Coastguard Worker if(pBiDi->runsMemory!=nullptr) {
254*0e209d39SAndroid Build Coastguard Worker uprv_free(pBiDi->runsMemory);
255*0e209d39SAndroid Build Coastguard Worker }
256*0e209d39SAndroid Build Coastguard Worker if(pBiDi->isolatesMemory!=nullptr) {
257*0e209d39SAndroid Build Coastguard Worker uprv_free(pBiDi->isolatesMemory);
258*0e209d39SAndroid Build Coastguard Worker }
259*0e209d39SAndroid Build Coastguard Worker if(pBiDi->insertPoints.points!=nullptr) {
260*0e209d39SAndroid Build Coastguard Worker uprv_free(pBiDi->insertPoints.points);
261*0e209d39SAndroid Build Coastguard Worker }
262*0e209d39SAndroid Build Coastguard Worker
263*0e209d39SAndroid Build Coastguard Worker uprv_free(pBiDi);
264*0e209d39SAndroid Build Coastguard Worker }
265*0e209d39SAndroid Build Coastguard Worker }
266*0e209d39SAndroid Build Coastguard Worker
267*0e209d39SAndroid Build Coastguard Worker /* set to approximate "inverse BiDi" ---------------------------------------- */
268*0e209d39SAndroid Build Coastguard Worker
269*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
ubidi_setInverse(UBiDi * pBiDi,UBool isInverse)270*0e209d39SAndroid Build Coastguard Worker ubidi_setInverse(UBiDi *pBiDi, UBool isInverse) {
271*0e209d39SAndroid Build Coastguard Worker if(pBiDi!=nullptr) {
272*0e209d39SAndroid Build Coastguard Worker pBiDi->isInverse=isInverse;
273*0e209d39SAndroid Build Coastguard Worker pBiDi->reorderingMode = isInverse ? UBIDI_REORDER_INVERSE_NUMBERS_AS_L
274*0e209d39SAndroid Build Coastguard Worker : UBIDI_REORDER_DEFAULT;
275*0e209d39SAndroid Build Coastguard Worker }
276*0e209d39SAndroid Build Coastguard Worker }
277*0e209d39SAndroid Build Coastguard Worker
278*0e209d39SAndroid Build Coastguard Worker U_CAPI UBool U_EXPORT2
ubidi_isInverse(UBiDi * pBiDi)279*0e209d39SAndroid Build Coastguard Worker ubidi_isInverse(UBiDi *pBiDi) {
280*0e209d39SAndroid Build Coastguard Worker if(pBiDi!=nullptr) {
281*0e209d39SAndroid Build Coastguard Worker return pBiDi->isInverse;
282*0e209d39SAndroid Build Coastguard Worker } else {
283*0e209d39SAndroid Build Coastguard Worker return false;
284*0e209d39SAndroid Build Coastguard Worker }
285*0e209d39SAndroid Build Coastguard Worker }
286*0e209d39SAndroid Build Coastguard Worker
287*0e209d39SAndroid Build Coastguard Worker /* FOOD FOR THOUGHT: currently the reordering modes are a mixture of
288*0e209d39SAndroid Build Coastguard Worker * algorithm for direct BiDi, algorithm for inverse BiDi and the bizarre
289*0e209d39SAndroid Build Coastguard Worker * concept of RUNS_ONLY which is a double operation.
290*0e209d39SAndroid Build Coastguard Worker * It could be advantageous to divide this into 3 concepts:
291*0e209d39SAndroid Build Coastguard Worker * a) Operation: direct / inverse / RUNS_ONLY
292*0e209d39SAndroid Build Coastguard Worker * b) Direct algorithm: default / NUMBERS_SPECIAL / GROUP_NUMBERS_WITH_R
293*0e209d39SAndroid Build Coastguard Worker * c) Inverse algorithm: default / INVERSE_LIKE_DIRECT / NUMBERS_SPECIAL
294*0e209d39SAndroid Build Coastguard Worker * This would allow combinations not possible today like RUNS_ONLY with
295*0e209d39SAndroid Build Coastguard Worker * NUMBERS_SPECIAL.
296*0e209d39SAndroid Build Coastguard Worker * Also allow to set INSERT_MARKS for the direct step of RUNS_ONLY and
297*0e209d39SAndroid Build Coastguard Worker * REMOVE_CONTROLS for the inverse step.
298*0e209d39SAndroid Build Coastguard Worker * Not all combinations would be supported, and probably not all do make sense.
299*0e209d39SAndroid Build Coastguard Worker * This would need to document which ones are supported and what are the
300*0e209d39SAndroid Build Coastguard Worker * fallbacks for unsupported combinations.
301*0e209d39SAndroid Build Coastguard Worker */
302*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
ubidi_setReorderingMode(UBiDi * pBiDi,UBiDiReorderingMode reorderingMode)303*0e209d39SAndroid Build Coastguard Worker ubidi_setReorderingMode(UBiDi *pBiDi, UBiDiReorderingMode reorderingMode) UPRV_NO_SANITIZE_UNDEFINED {
304*0e209d39SAndroid Build Coastguard Worker if ((pBiDi!=nullptr) && (reorderingMode >= UBIDI_REORDER_DEFAULT)
305*0e209d39SAndroid Build Coastguard Worker && (reorderingMode < UBIDI_REORDER_COUNT)) {
306*0e209d39SAndroid Build Coastguard Worker pBiDi->reorderingMode = reorderingMode;
307*0e209d39SAndroid Build Coastguard Worker pBiDi->isInverse = (UBool)(reorderingMode == UBIDI_REORDER_INVERSE_NUMBERS_AS_L);
308*0e209d39SAndroid Build Coastguard Worker }
309*0e209d39SAndroid Build Coastguard Worker }
310*0e209d39SAndroid Build Coastguard Worker
311*0e209d39SAndroid Build Coastguard Worker U_CAPI UBiDiReorderingMode U_EXPORT2
ubidi_getReorderingMode(UBiDi * pBiDi)312*0e209d39SAndroid Build Coastguard Worker ubidi_getReorderingMode(UBiDi *pBiDi) {
313*0e209d39SAndroid Build Coastguard Worker if (pBiDi!=nullptr) {
314*0e209d39SAndroid Build Coastguard Worker return pBiDi->reorderingMode;
315*0e209d39SAndroid Build Coastguard Worker } else {
316*0e209d39SAndroid Build Coastguard Worker return UBIDI_REORDER_DEFAULT;
317*0e209d39SAndroid Build Coastguard Worker }
318*0e209d39SAndroid Build Coastguard Worker }
319*0e209d39SAndroid Build Coastguard Worker
320*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
ubidi_setReorderingOptions(UBiDi * pBiDi,uint32_t reorderingOptions)321*0e209d39SAndroid Build Coastguard Worker ubidi_setReorderingOptions(UBiDi *pBiDi, uint32_t reorderingOptions) {
322*0e209d39SAndroid Build Coastguard Worker if (reorderingOptions & UBIDI_OPTION_REMOVE_CONTROLS) {
323*0e209d39SAndroid Build Coastguard Worker reorderingOptions&=~UBIDI_OPTION_INSERT_MARKS;
324*0e209d39SAndroid Build Coastguard Worker }
325*0e209d39SAndroid Build Coastguard Worker if (pBiDi!=nullptr) {
326*0e209d39SAndroid Build Coastguard Worker pBiDi->reorderingOptions=reorderingOptions;
327*0e209d39SAndroid Build Coastguard Worker }
328*0e209d39SAndroid Build Coastguard Worker }
329*0e209d39SAndroid Build Coastguard Worker
330*0e209d39SAndroid Build Coastguard Worker U_CAPI uint32_t U_EXPORT2
ubidi_getReorderingOptions(UBiDi * pBiDi)331*0e209d39SAndroid Build Coastguard Worker ubidi_getReorderingOptions(UBiDi *pBiDi) {
332*0e209d39SAndroid Build Coastguard Worker if (pBiDi!=nullptr) {
333*0e209d39SAndroid Build Coastguard Worker return pBiDi->reorderingOptions;
334*0e209d39SAndroid Build Coastguard Worker } else {
335*0e209d39SAndroid Build Coastguard Worker return 0;
336*0e209d39SAndroid Build Coastguard Worker }
337*0e209d39SAndroid Build Coastguard Worker }
338*0e209d39SAndroid Build Coastguard Worker
339*0e209d39SAndroid Build Coastguard Worker U_CAPI UBiDiDirection U_EXPORT2
ubidi_getBaseDirection(const char16_t * text,int32_t length)340*0e209d39SAndroid Build Coastguard Worker ubidi_getBaseDirection(const char16_t *text,
341*0e209d39SAndroid Build Coastguard Worker int32_t length){
342*0e209d39SAndroid Build Coastguard Worker
343*0e209d39SAndroid Build Coastguard Worker int32_t i;
344*0e209d39SAndroid Build Coastguard Worker UChar32 uchar;
345*0e209d39SAndroid Build Coastguard Worker UCharDirection dir;
346*0e209d39SAndroid Build Coastguard Worker
347*0e209d39SAndroid Build Coastguard Worker if( text==nullptr || length<-1 ){
348*0e209d39SAndroid Build Coastguard Worker return UBIDI_NEUTRAL;
349*0e209d39SAndroid Build Coastguard Worker }
350*0e209d39SAndroid Build Coastguard Worker
351*0e209d39SAndroid Build Coastguard Worker if(length==-1) {
352*0e209d39SAndroid Build Coastguard Worker length=u_strlen(text);
353*0e209d39SAndroid Build Coastguard Worker }
354*0e209d39SAndroid Build Coastguard Worker
355*0e209d39SAndroid Build Coastguard Worker for( i = 0 ; i < length; ) {
356*0e209d39SAndroid Build Coastguard Worker /* i is incremented by U16_NEXT */
357*0e209d39SAndroid Build Coastguard Worker U16_NEXT(text, i, length, uchar);
358*0e209d39SAndroid Build Coastguard Worker dir = u_charDirection(uchar);
359*0e209d39SAndroid Build Coastguard Worker if( dir == U_LEFT_TO_RIGHT )
360*0e209d39SAndroid Build Coastguard Worker return UBIDI_LTR;
361*0e209d39SAndroid Build Coastguard Worker if( dir == U_RIGHT_TO_LEFT || dir ==U_RIGHT_TO_LEFT_ARABIC )
362*0e209d39SAndroid Build Coastguard Worker return UBIDI_RTL;
363*0e209d39SAndroid Build Coastguard Worker }
364*0e209d39SAndroid Build Coastguard Worker return UBIDI_NEUTRAL;
365*0e209d39SAndroid Build Coastguard Worker }
366*0e209d39SAndroid Build Coastguard Worker
367*0e209d39SAndroid Build Coastguard Worker /* perform (P2)..(P3) ------------------------------------------------------- */
368*0e209d39SAndroid Build Coastguard Worker
369*0e209d39SAndroid Build Coastguard Worker /**
370*0e209d39SAndroid Build Coastguard Worker * Returns the directionality of the first strong character
371*0e209d39SAndroid Build Coastguard Worker * after the last B in prologue, if any.
372*0e209d39SAndroid Build Coastguard Worker * Requires prologue!=null.
373*0e209d39SAndroid Build Coastguard Worker */
374*0e209d39SAndroid Build Coastguard Worker static DirProp
firstL_R_AL(UBiDi * pBiDi)375*0e209d39SAndroid Build Coastguard Worker firstL_R_AL(UBiDi *pBiDi) {
376*0e209d39SAndroid Build Coastguard Worker const char16_t *text=pBiDi->prologue;
377*0e209d39SAndroid Build Coastguard Worker int32_t length=pBiDi->proLength;
378*0e209d39SAndroid Build Coastguard Worker int32_t i;
379*0e209d39SAndroid Build Coastguard Worker UChar32 uchar;
380*0e209d39SAndroid Build Coastguard Worker DirProp dirProp, result=ON;
381*0e209d39SAndroid Build Coastguard Worker for(i=0; i<length; ) {
382*0e209d39SAndroid Build Coastguard Worker /* i is incremented by U16_NEXT */
383*0e209d39SAndroid Build Coastguard Worker U16_NEXT(text, i, length, uchar);
384*0e209d39SAndroid Build Coastguard Worker dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar);
385*0e209d39SAndroid Build Coastguard Worker if(result==ON) {
386*0e209d39SAndroid Build Coastguard Worker if(dirProp==L || dirProp==R || dirProp==AL) {
387*0e209d39SAndroid Build Coastguard Worker result=dirProp;
388*0e209d39SAndroid Build Coastguard Worker }
389*0e209d39SAndroid Build Coastguard Worker } else {
390*0e209d39SAndroid Build Coastguard Worker if(dirProp==B) {
391*0e209d39SAndroid Build Coastguard Worker result=ON;
392*0e209d39SAndroid Build Coastguard Worker }
393*0e209d39SAndroid Build Coastguard Worker }
394*0e209d39SAndroid Build Coastguard Worker }
395*0e209d39SAndroid Build Coastguard Worker return result;
396*0e209d39SAndroid Build Coastguard Worker }
397*0e209d39SAndroid Build Coastguard Worker
398*0e209d39SAndroid Build Coastguard Worker /*
399*0e209d39SAndroid Build Coastguard Worker * Check that there are enough entries in the array pointed to by pBiDi->paras
400*0e209d39SAndroid Build Coastguard Worker */
401*0e209d39SAndroid Build Coastguard Worker static UBool
checkParaCount(UBiDi * pBiDi)402*0e209d39SAndroid Build Coastguard Worker checkParaCount(UBiDi *pBiDi) {
403*0e209d39SAndroid Build Coastguard Worker int32_t count=pBiDi->paraCount;
404*0e209d39SAndroid Build Coastguard Worker if(pBiDi->paras==pBiDi->simpleParas) {
405*0e209d39SAndroid Build Coastguard Worker if(count<=SIMPLE_PARAS_COUNT)
406*0e209d39SAndroid Build Coastguard Worker return true;
407*0e209d39SAndroid Build Coastguard Worker if(!getInitialParasMemory(pBiDi, SIMPLE_PARAS_COUNT * 2))
408*0e209d39SAndroid Build Coastguard Worker return false;
409*0e209d39SAndroid Build Coastguard Worker pBiDi->paras=pBiDi->parasMemory;
410*0e209d39SAndroid Build Coastguard Worker uprv_memcpy(pBiDi->parasMemory, pBiDi->simpleParas, SIMPLE_PARAS_COUNT * sizeof(Para));
411*0e209d39SAndroid Build Coastguard Worker return true;
412*0e209d39SAndroid Build Coastguard Worker }
413*0e209d39SAndroid Build Coastguard Worker if(!getInitialParasMemory(pBiDi, count * 2))
414*0e209d39SAndroid Build Coastguard Worker return false;
415*0e209d39SAndroid Build Coastguard Worker pBiDi->paras=pBiDi->parasMemory;
416*0e209d39SAndroid Build Coastguard Worker return true;
417*0e209d39SAndroid Build Coastguard Worker }
418*0e209d39SAndroid Build Coastguard Worker
419*0e209d39SAndroid Build Coastguard Worker /*
420*0e209d39SAndroid Build Coastguard Worker * Get the directional properties for the text, calculate the flags bit-set, and
421*0e209d39SAndroid Build Coastguard Worker * determine the paragraph level if necessary (in pBiDi->paras[i].level).
422*0e209d39SAndroid Build Coastguard Worker * FSI initiators are also resolved and their dirProp replaced with LRI or RLI.
423*0e209d39SAndroid Build Coastguard Worker * When encountering an FSI, it is initially replaced with an LRI, which is the
424*0e209d39SAndroid Build Coastguard Worker * default. Only if a strong R or AL is found within its scope will the LRI be
425*0e209d39SAndroid Build Coastguard Worker * replaced by an RLI.
426*0e209d39SAndroid Build Coastguard Worker */
427*0e209d39SAndroid Build Coastguard Worker static UBool
getDirProps(UBiDi * pBiDi)428*0e209d39SAndroid Build Coastguard Worker getDirProps(UBiDi *pBiDi) {
429*0e209d39SAndroid Build Coastguard Worker const char16_t *text=pBiDi->text;
430*0e209d39SAndroid Build Coastguard Worker DirProp *dirProps=pBiDi->dirPropsMemory; /* pBiDi->dirProps is const */
431*0e209d39SAndroid Build Coastguard Worker
432*0e209d39SAndroid Build Coastguard Worker int32_t i=0, originalLength=pBiDi->originalLength;
433*0e209d39SAndroid Build Coastguard Worker Flags flags=0; /* collect all directionalities in the text */
434*0e209d39SAndroid Build Coastguard Worker UChar32 uchar;
435*0e209d39SAndroid Build Coastguard Worker DirProp dirProp=0, defaultParaLevel=0; /* initialize to avoid compiler warnings */
436*0e209d39SAndroid Build Coastguard Worker UBool isDefaultLevel=IS_DEFAULT_LEVEL(pBiDi->paraLevel);
437*0e209d39SAndroid Build Coastguard Worker /* for inverse BiDi, the default para level is set to RTL if there is a
438*0e209d39SAndroid Build Coastguard Worker strong R or AL character at either end of the text */
439*0e209d39SAndroid Build Coastguard Worker UBool isDefaultLevelInverse=isDefaultLevel && (UBool)
440*0e209d39SAndroid Build Coastguard Worker (pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_LIKE_DIRECT ||
441*0e209d39SAndroid Build Coastguard Worker pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL);
442*0e209d39SAndroid Build Coastguard Worker int32_t lastArabicPos=-1;
443*0e209d39SAndroid Build Coastguard Worker int32_t controlCount=0;
444*0e209d39SAndroid Build Coastguard Worker UBool removeBiDiControls = (UBool)(pBiDi->reorderingOptions &
445*0e209d39SAndroid Build Coastguard Worker UBIDI_OPTION_REMOVE_CONTROLS);
446*0e209d39SAndroid Build Coastguard Worker
447*0e209d39SAndroid Build Coastguard Worker enum State {
448*0e209d39SAndroid Build Coastguard Worker NOT_SEEKING_STRONG, /* 0: not contextual paraLevel, not after FSI */
449*0e209d39SAndroid Build Coastguard Worker SEEKING_STRONG_FOR_PARA, /* 1: looking for first strong char in para */
450*0e209d39SAndroid Build Coastguard Worker SEEKING_STRONG_FOR_FSI, /* 2: looking for first strong after FSI */
451*0e209d39SAndroid Build Coastguard Worker LOOKING_FOR_PDI /* 3: found strong after FSI, looking for PDI */
452*0e209d39SAndroid Build Coastguard Worker };
453*0e209d39SAndroid Build Coastguard Worker State state;
454*0e209d39SAndroid Build Coastguard Worker DirProp lastStrong=ON; /* for default level & inverse BiDi */
455*0e209d39SAndroid Build Coastguard Worker /* The following stacks are used to manage isolate sequences. Those
456*0e209d39SAndroid Build Coastguard Worker sequences may be nested, but obviously never more deeply than the
457*0e209d39SAndroid Build Coastguard Worker maximum explicit embedding level.
458*0e209d39SAndroid Build Coastguard Worker lastStack is the index of the last used entry in the stack. A value of -1
459*0e209d39SAndroid Build Coastguard Worker means that there is no open isolate sequence.
460*0e209d39SAndroid Build Coastguard Worker lastStack is reset to -1 on paragraph boundaries. */
461*0e209d39SAndroid Build Coastguard Worker /* The following stack contains the position of the initiator of
462*0e209d39SAndroid Build Coastguard Worker each open isolate sequence */
463*0e209d39SAndroid Build Coastguard Worker int32_t isolateStartStack[UBIDI_MAX_EXPLICIT_LEVEL+1];
464*0e209d39SAndroid Build Coastguard Worker /* The following stack contains the last known state before
465*0e209d39SAndroid Build Coastguard Worker encountering the initiator of an isolate sequence */
466*0e209d39SAndroid Build Coastguard Worker State previousStateStack[UBIDI_MAX_EXPLICIT_LEVEL+1];
467*0e209d39SAndroid Build Coastguard Worker int32_t stackLast=-1;
468*0e209d39SAndroid Build Coastguard Worker
469*0e209d39SAndroid Build Coastguard Worker if(pBiDi->reorderingOptions & UBIDI_OPTION_STREAMING)
470*0e209d39SAndroid Build Coastguard Worker pBiDi->length=0;
471*0e209d39SAndroid Build Coastguard Worker defaultParaLevel=pBiDi->paraLevel&1;
472*0e209d39SAndroid Build Coastguard Worker if(isDefaultLevel) {
473*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[0].level=defaultParaLevel;
474*0e209d39SAndroid Build Coastguard Worker lastStrong=defaultParaLevel;
475*0e209d39SAndroid Build Coastguard Worker if(pBiDi->proLength>0 && /* there is a prologue */
476*0e209d39SAndroid Build Coastguard Worker (dirProp=firstL_R_AL(pBiDi))!=ON) { /* with a strong character */
477*0e209d39SAndroid Build Coastguard Worker if(dirProp==L)
478*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[0].level=0; /* set the default para level */
479*0e209d39SAndroid Build Coastguard Worker else
480*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[0].level=1; /* set the default para level */
481*0e209d39SAndroid Build Coastguard Worker state=NOT_SEEKING_STRONG;
482*0e209d39SAndroid Build Coastguard Worker } else {
483*0e209d39SAndroid Build Coastguard Worker state=SEEKING_STRONG_FOR_PARA;
484*0e209d39SAndroid Build Coastguard Worker }
485*0e209d39SAndroid Build Coastguard Worker } else {
486*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[0].level=pBiDi->paraLevel;
487*0e209d39SAndroid Build Coastguard Worker state=NOT_SEEKING_STRONG;
488*0e209d39SAndroid Build Coastguard Worker }
489*0e209d39SAndroid Build Coastguard Worker /* count paragraphs and determine the paragraph level (P2..P3) */
490*0e209d39SAndroid Build Coastguard Worker /*
491*0e209d39SAndroid Build Coastguard Worker * see comment in ubidi.h:
492*0e209d39SAndroid Build Coastguard Worker * the UBIDI_DEFAULT_XXX values are designed so that
493*0e209d39SAndroid Build Coastguard Worker * their bit 0 alone yields the intended default
494*0e209d39SAndroid Build Coastguard Worker */
495*0e209d39SAndroid Build Coastguard Worker for( /* i=0 above */ ; i<originalLength; ) {
496*0e209d39SAndroid Build Coastguard Worker /* i is incremented by U16_NEXT */
497*0e209d39SAndroid Build Coastguard Worker U16_NEXT(text, i, originalLength, uchar);
498*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar));
499*0e209d39SAndroid Build Coastguard Worker dirProps[i-1]=dirProp;
500*0e209d39SAndroid Build Coastguard Worker if(uchar>0xffff) { /* set the lead surrogate's property to BN */
501*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(BN);
502*0e209d39SAndroid Build Coastguard Worker dirProps[i-2]=BN;
503*0e209d39SAndroid Build Coastguard Worker }
504*0e209d39SAndroid Build Coastguard Worker if(removeBiDiControls && IS_BIDI_CONTROL_CHAR(uchar))
505*0e209d39SAndroid Build Coastguard Worker controlCount++;
506*0e209d39SAndroid Build Coastguard Worker if(dirProp==L) {
507*0e209d39SAndroid Build Coastguard Worker if(state==SEEKING_STRONG_FOR_PARA) {
508*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[pBiDi->paraCount-1].level=0;
509*0e209d39SAndroid Build Coastguard Worker state=NOT_SEEKING_STRONG;
510*0e209d39SAndroid Build Coastguard Worker }
511*0e209d39SAndroid Build Coastguard Worker else if(state==SEEKING_STRONG_FOR_FSI) {
512*0e209d39SAndroid Build Coastguard Worker if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) {
513*0e209d39SAndroid Build Coastguard Worker /* no need for next statement, already set by default */
514*0e209d39SAndroid Build Coastguard Worker /* dirProps[isolateStartStack[stackLast]]=LRI; */
515*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(LRI);
516*0e209d39SAndroid Build Coastguard Worker }
517*0e209d39SAndroid Build Coastguard Worker state=LOOKING_FOR_PDI;
518*0e209d39SAndroid Build Coastguard Worker }
519*0e209d39SAndroid Build Coastguard Worker lastStrong=L;
520*0e209d39SAndroid Build Coastguard Worker continue;
521*0e209d39SAndroid Build Coastguard Worker }
522*0e209d39SAndroid Build Coastguard Worker if(dirProp==R || dirProp==AL) {
523*0e209d39SAndroid Build Coastguard Worker if(state==SEEKING_STRONG_FOR_PARA) {
524*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[pBiDi->paraCount-1].level=1;
525*0e209d39SAndroid Build Coastguard Worker state=NOT_SEEKING_STRONG;
526*0e209d39SAndroid Build Coastguard Worker }
527*0e209d39SAndroid Build Coastguard Worker else if(state==SEEKING_STRONG_FOR_FSI) {
528*0e209d39SAndroid Build Coastguard Worker if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) {
529*0e209d39SAndroid Build Coastguard Worker dirProps[isolateStartStack[stackLast]]=RLI;
530*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(RLI);
531*0e209d39SAndroid Build Coastguard Worker }
532*0e209d39SAndroid Build Coastguard Worker state=LOOKING_FOR_PDI;
533*0e209d39SAndroid Build Coastguard Worker }
534*0e209d39SAndroid Build Coastguard Worker lastStrong=R;
535*0e209d39SAndroid Build Coastguard Worker if(dirProp==AL)
536*0e209d39SAndroid Build Coastguard Worker lastArabicPos=i-1;
537*0e209d39SAndroid Build Coastguard Worker continue;
538*0e209d39SAndroid Build Coastguard Worker }
539*0e209d39SAndroid Build Coastguard Worker if(dirProp>=FSI && dirProp<=RLI) { /* FSI, LRI or RLI */
540*0e209d39SAndroid Build Coastguard Worker stackLast++;
541*0e209d39SAndroid Build Coastguard Worker if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) {
542*0e209d39SAndroid Build Coastguard Worker isolateStartStack[stackLast]=i-1;
543*0e209d39SAndroid Build Coastguard Worker previousStateStack[stackLast]=state;
544*0e209d39SAndroid Build Coastguard Worker }
545*0e209d39SAndroid Build Coastguard Worker if(dirProp==FSI) {
546*0e209d39SAndroid Build Coastguard Worker dirProps[i-1]=LRI; /* default if no strong char */
547*0e209d39SAndroid Build Coastguard Worker state=SEEKING_STRONG_FOR_FSI;
548*0e209d39SAndroid Build Coastguard Worker }
549*0e209d39SAndroid Build Coastguard Worker else
550*0e209d39SAndroid Build Coastguard Worker state=LOOKING_FOR_PDI;
551*0e209d39SAndroid Build Coastguard Worker continue;
552*0e209d39SAndroid Build Coastguard Worker }
553*0e209d39SAndroid Build Coastguard Worker if(dirProp==PDI) {
554*0e209d39SAndroid Build Coastguard Worker if(state==SEEKING_STRONG_FOR_FSI) {
555*0e209d39SAndroid Build Coastguard Worker if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) {
556*0e209d39SAndroid Build Coastguard Worker /* no need for next statement, already set by default */
557*0e209d39SAndroid Build Coastguard Worker /* dirProps[isolateStartStack[stackLast]]=LRI; */
558*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(LRI);
559*0e209d39SAndroid Build Coastguard Worker }
560*0e209d39SAndroid Build Coastguard Worker }
561*0e209d39SAndroid Build Coastguard Worker if(stackLast>=0) {
562*0e209d39SAndroid Build Coastguard Worker if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL)
563*0e209d39SAndroid Build Coastguard Worker state=previousStateStack[stackLast];
564*0e209d39SAndroid Build Coastguard Worker stackLast--;
565*0e209d39SAndroid Build Coastguard Worker }
566*0e209d39SAndroid Build Coastguard Worker continue;
567*0e209d39SAndroid Build Coastguard Worker }
568*0e209d39SAndroid Build Coastguard Worker if(dirProp==B) {
569*0e209d39SAndroid Build Coastguard Worker if(i<originalLength && uchar==CR && text[i]==LF) /* do nothing on the CR */
570*0e209d39SAndroid Build Coastguard Worker continue;
571*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[pBiDi->paraCount-1].limit=i;
572*0e209d39SAndroid Build Coastguard Worker if(isDefaultLevelInverse && lastStrong==R)
573*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[pBiDi->paraCount-1].level=1;
574*0e209d39SAndroid Build Coastguard Worker if(pBiDi->reorderingOptions & UBIDI_OPTION_STREAMING) {
575*0e209d39SAndroid Build Coastguard Worker /* When streaming, we only process whole paragraphs
576*0e209d39SAndroid Build Coastguard Worker thus some updates are only done on paragraph boundaries */
577*0e209d39SAndroid Build Coastguard Worker pBiDi->length=i; /* i is index to next character */
578*0e209d39SAndroid Build Coastguard Worker pBiDi->controlCount=controlCount;
579*0e209d39SAndroid Build Coastguard Worker }
580*0e209d39SAndroid Build Coastguard Worker if(i<originalLength) { /* B not last char in text */
581*0e209d39SAndroid Build Coastguard Worker pBiDi->paraCount++;
582*0e209d39SAndroid Build Coastguard Worker if(checkParaCount(pBiDi)==false) /* not enough memory for a new para entry */
583*0e209d39SAndroid Build Coastguard Worker return false;
584*0e209d39SAndroid Build Coastguard Worker if(isDefaultLevel) {
585*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[pBiDi->paraCount-1].level=defaultParaLevel;
586*0e209d39SAndroid Build Coastguard Worker state=SEEKING_STRONG_FOR_PARA;
587*0e209d39SAndroid Build Coastguard Worker lastStrong=defaultParaLevel;
588*0e209d39SAndroid Build Coastguard Worker } else {
589*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[pBiDi->paraCount-1].level=pBiDi->paraLevel;
590*0e209d39SAndroid Build Coastguard Worker state=NOT_SEEKING_STRONG;
591*0e209d39SAndroid Build Coastguard Worker }
592*0e209d39SAndroid Build Coastguard Worker stackLast=-1;
593*0e209d39SAndroid Build Coastguard Worker }
594*0e209d39SAndroid Build Coastguard Worker continue;
595*0e209d39SAndroid Build Coastguard Worker }
596*0e209d39SAndroid Build Coastguard Worker }
597*0e209d39SAndroid Build Coastguard Worker /* Ignore still open isolate sequences with overflow */
598*0e209d39SAndroid Build Coastguard Worker if(stackLast>UBIDI_MAX_EXPLICIT_LEVEL) {
599*0e209d39SAndroid Build Coastguard Worker stackLast=UBIDI_MAX_EXPLICIT_LEVEL;
600*0e209d39SAndroid Build Coastguard Worker state=SEEKING_STRONG_FOR_FSI; /* to be on the safe side */
601*0e209d39SAndroid Build Coastguard Worker }
602*0e209d39SAndroid Build Coastguard Worker /* Resolve direction of still unresolved open FSI sequences */
603*0e209d39SAndroid Build Coastguard Worker while(stackLast>=0) {
604*0e209d39SAndroid Build Coastguard Worker if(state==SEEKING_STRONG_FOR_FSI) {
605*0e209d39SAndroid Build Coastguard Worker /* no need for next statement, already set by default */
606*0e209d39SAndroid Build Coastguard Worker /* dirProps[isolateStartStack[stackLast]]=LRI; */
607*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(LRI);
608*0e209d39SAndroid Build Coastguard Worker break;
609*0e209d39SAndroid Build Coastguard Worker }
610*0e209d39SAndroid Build Coastguard Worker state=previousStateStack[stackLast];
611*0e209d39SAndroid Build Coastguard Worker stackLast--;
612*0e209d39SAndroid Build Coastguard Worker }
613*0e209d39SAndroid Build Coastguard Worker /* When streaming, ignore text after the last paragraph separator */
614*0e209d39SAndroid Build Coastguard Worker if(pBiDi->reorderingOptions & UBIDI_OPTION_STREAMING) {
615*0e209d39SAndroid Build Coastguard Worker if(pBiDi->length<originalLength)
616*0e209d39SAndroid Build Coastguard Worker pBiDi->paraCount--;
617*0e209d39SAndroid Build Coastguard Worker } else {
618*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[pBiDi->paraCount-1].limit=originalLength;
619*0e209d39SAndroid Build Coastguard Worker pBiDi->controlCount=controlCount;
620*0e209d39SAndroid Build Coastguard Worker }
621*0e209d39SAndroid Build Coastguard Worker /* For inverse bidi, default para direction is RTL if there is
622*0e209d39SAndroid Build Coastguard Worker a strong R or AL at either end of the paragraph */
623*0e209d39SAndroid Build Coastguard Worker if(isDefaultLevelInverse && lastStrong==R) {
624*0e209d39SAndroid Build Coastguard Worker pBiDi->paras[pBiDi->paraCount-1].level=1;
625*0e209d39SAndroid Build Coastguard Worker }
626*0e209d39SAndroid Build Coastguard Worker if(isDefaultLevel) {
627*0e209d39SAndroid Build Coastguard Worker pBiDi->paraLevel=static_cast<UBiDiLevel>(pBiDi->paras[0].level);
628*0e209d39SAndroid Build Coastguard Worker }
629*0e209d39SAndroid Build Coastguard Worker /* The following is needed to resolve the text direction for default level
630*0e209d39SAndroid Build Coastguard Worker paragraphs containing no strong character */
631*0e209d39SAndroid Build Coastguard Worker for(i=0; i<pBiDi->paraCount; i++)
632*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG_LR(pBiDi->paras[i].level);
633*0e209d39SAndroid Build Coastguard Worker
634*0e209d39SAndroid Build Coastguard Worker if(pBiDi->orderParagraphsLTR && (flags&DIRPROP_FLAG(B))) {
635*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(L);
636*0e209d39SAndroid Build Coastguard Worker }
637*0e209d39SAndroid Build Coastguard Worker pBiDi->flags=flags;
638*0e209d39SAndroid Build Coastguard Worker pBiDi->lastArabicPos=lastArabicPos;
639*0e209d39SAndroid Build Coastguard Worker return true;
640*0e209d39SAndroid Build Coastguard Worker }
641*0e209d39SAndroid Build Coastguard Worker
642*0e209d39SAndroid Build Coastguard Worker /* determine the paragraph level at position index */
643*0e209d39SAndroid Build Coastguard Worker U_CFUNC UBiDiLevel
ubidi_getParaLevelAtIndex(const UBiDi * pBiDi,int32_t pindex)644*0e209d39SAndroid Build Coastguard Worker ubidi_getParaLevelAtIndex(const UBiDi *pBiDi, int32_t pindex) {
645*0e209d39SAndroid Build Coastguard Worker int32_t i;
646*0e209d39SAndroid Build Coastguard Worker for(i=0; i<pBiDi->paraCount; i++)
647*0e209d39SAndroid Build Coastguard Worker if(pindex<pBiDi->paras[i].limit)
648*0e209d39SAndroid Build Coastguard Worker break;
649*0e209d39SAndroid Build Coastguard Worker if(i>=pBiDi->paraCount)
650*0e209d39SAndroid Build Coastguard Worker i=pBiDi->paraCount-1;
651*0e209d39SAndroid Build Coastguard Worker return (UBiDiLevel)(pBiDi->paras[i].level);
652*0e209d39SAndroid Build Coastguard Worker }
653*0e209d39SAndroid Build Coastguard Worker
654*0e209d39SAndroid Build Coastguard Worker /* Functions for handling paired brackets ----------------------------------- */
655*0e209d39SAndroid Build Coastguard Worker
656*0e209d39SAndroid Build Coastguard Worker /* In the isoRuns array, the first entry is used for text outside of any
657*0e209d39SAndroid Build Coastguard Worker isolate sequence. Higher entries are used for each more deeply nested
658*0e209d39SAndroid Build Coastguard Worker isolate sequence. isoRunLast is the index of the last used entry. The
659*0e209d39SAndroid Build Coastguard Worker openings array is used to note the data of opening brackets not yet
660*0e209d39SAndroid Build Coastguard Worker matched by a closing bracket, or matched but still susceptible to change
661*0e209d39SAndroid Build Coastguard Worker level.
662*0e209d39SAndroid Build Coastguard Worker Each isoRun entry contains the index of the first and
663*0e209d39SAndroid Build Coastguard Worker one-after-last openings entries for pending opening brackets it
664*0e209d39SAndroid Build Coastguard Worker contains. The next openings entry to use is the one-after-last of the
665*0e209d39SAndroid Build Coastguard Worker most deeply nested isoRun entry.
666*0e209d39SAndroid Build Coastguard Worker isoRun entries also contain their current embedding level and the last
667*0e209d39SAndroid Build Coastguard Worker encountered strong character, since these will be needed to resolve
668*0e209d39SAndroid Build Coastguard Worker the level of paired brackets. */
669*0e209d39SAndroid Build Coastguard Worker
670*0e209d39SAndroid Build Coastguard Worker static void
bracketInit(UBiDi * pBiDi,BracketData * bd)671*0e209d39SAndroid Build Coastguard Worker bracketInit(UBiDi *pBiDi, BracketData *bd) {
672*0e209d39SAndroid Build Coastguard Worker bd->pBiDi=pBiDi;
673*0e209d39SAndroid Build Coastguard Worker bd->isoRunLast=0;
674*0e209d39SAndroid Build Coastguard Worker bd->isoRuns[0].start=0;
675*0e209d39SAndroid Build Coastguard Worker bd->isoRuns[0].limit=0;
676*0e209d39SAndroid Build Coastguard Worker bd->isoRuns[0].level=GET_PARALEVEL(pBiDi, 0);
677*0e209d39SAndroid Build Coastguard Worker UBiDiLevel t = GET_PARALEVEL(pBiDi, 0) & 1;
678*0e209d39SAndroid Build Coastguard Worker bd->isoRuns[0].lastStrong = bd->isoRuns[0].lastBase = t;
679*0e209d39SAndroid Build Coastguard Worker bd->isoRuns[0].contextDir = (UBiDiDirection)t;
680*0e209d39SAndroid Build Coastguard Worker bd->isoRuns[0].contextPos=0;
681*0e209d39SAndroid Build Coastguard Worker if(pBiDi->openingsMemory) {
682*0e209d39SAndroid Build Coastguard Worker bd->openings=pBiDi->openingsMemory;
683*0e209d39SAndroid Build Coastguard Worker bd->openingsCount=pBiDi->openingsSize / sizeof(Opening);
684*0e209d39SAndroid Build Coastguard Worker } else {
685*0e209d39SAndroid Build Coastguard Worker bd->openings=bd->simpleOpenings;
686*0e209d39SAndroid Build Coastguard Worker bd->openingsCount=SIMPLE_OPENINGS_COUNT;
687*0e209d39SAndroid Build Coastguard Worker }
688*0e209d39SAndroid Build Coastguard Worker bd->isNumbersSpecial=bd->pBiDi->reorderingMode==UBIDI_REORDER_NUMBERS_SPECIAL ||
689*0e209d39SAndroid Build Coastguard Worker bd->pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL;
690*0e209d39SAndroid Build Coastguard Worker }
691*0e209d39SAndroid Build Coastguard Worker
692*0e209d39SAndroid Build Coastguard Worker /* paragraph boundary */
693*0e209d39SAndroid Build Coastguard Worker static void
bracketProcessB(BracketData * bd,UBiDiLevel level)694*0e209d39SAndroid Build Coastguard Worker bracketProcessB(BracketData *bd, UBiDiLevel level) {
695*0e209d39SAndroid Build Coastguard Worker bd->isoRunLast=0;
696*0e209d39SAndroid Build Coastguard Worker bd->isoRuns[0].limit=0;
697*0e209d39SAndroid Build Coastguard Worker bd->isoRuns[0].level=level;
698*0e209d39SAndroid Build Coastguard Worker bd->isoRuns[0].lastStrong=bd->isoRuns[0].lastBase=level&1;
699*0e209d39SAndroid Build Coastguard Worker bd->isoRuns[0].contextDir=(UBiDiDirection)(level&1);
700*0e209d39SAndroid Build Coastguard Worker bd->isoRuns[0].contextPos=0;
701*0e209d39SAndroid Build Coastguard Worker }
702*0e209d39SAndroid Build Coastguard Worker
703*0e209d39SAndroid Build Coastguard Worker /* LRE, LRO, RLE, RLO, PDF */
704*0e209d39SAndroid Build Coastguard Worker static void
bracketProcessBoundary(BracketData * bd,int32_t lastCcPos,UBiDiLevel contextLevel,UBiDiLevel embeddingLevel)705*0e209d39SAndroid Build Coastguard Worker bracketProcessBoundary(BracketData *bd, int32_t lastCcPos,
706*0e209d39SAndroid Build Coastguard Worker UBiDiLevel contextLevel, UBiDiLevel embeddingLevel) {
707*0e209d39SAndroid Build Coastguard Worker IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
708*0e209d39SAndroid Build Coastguard Worker DirProp *dirProps=bd->pBiDi->dirProps;
709*0e209d39SAndroid Build Coastguard Worker if(DIRPROP_FLAG(dirProps[lastCcPos])&MASK_ISO) /* after an isolate */
710*0e209d39SAndroid Build Coastguard Worker return;
711*0e209d39SAndroid Build Coastguard Worker if(NO_OVERRIDE(embeddingLevel)>NO_OVERRIDE(contextLevel)) /* not a PDF */
712*0e209d39SAndroid Build Coastguard Worker contextLevel=embeddingLevel;
713*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->limit=pLastIsoRun->start;
714*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->level=embeddingLevel;
715*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastStrong=pLastIsoRun->lastBase=contextLevel&1;
716*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextDir=(UBiDiDirection)(contextLevel&1);
717*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextPos=(UBiDiDirection)lastCcPos;
718*0e209d39SAndroid Build Coastguard Worker }
719*0e209d39SAndroid Build Coastguard Worker
720*0e209d39SAndroid Build Coastguard Worker /* LRI or RLI */
721*0e209d39SAndroid Build Coastguard Worker static void
bracketProcessLRI_RLI(BracketData * bd,UBiDiLevel level)722*0e209d39SAndroid Build Coastguard Worker bracketProcessLRI_RLI(BracketData *bd, UBiDiLevel level) {
723*0e209d39SAndroid Build Coastguard Worker IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
724*0e209d39SAndroid Build Coastguard Worker int16_t lastLimit;
725*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastBase=ON;
726*0e209d39SAndroid Build Coastguard Worker lastLimit=pLastIsoRun->limit;
727*0e209d39SAndroid Build Coastguard Worker bd->isoRunLast++;
728*0e209d39SAndroid Build Coastguard Worker pLastIsoRun++;
729*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->start=pLastIsoRun->limit=lastLimit;
730*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->level=level;
731*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastStrong=pLastIsoRun->lastBase=level&1;
732*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextDir=(UBiDiDirection)(level&1);
733*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextPos=0;
734*0e209d39SAndroid Build Coastguard Worker }
735*0e209d39SAndroid Build Coastguard Worker
736*0e209d39SAndroid Build Coastguard Worker /* PDI */
737*0e209d39SAndroid Build Coastguard Worker static void
bracketProcessPDI(BracketData * bd)738*0e209d39SAndroid Build Coastguard Worker bracketProcessPDI(BracketData *bd) {
739*0e209d39SAndroid Build Coastguard Worker IsoRun *pLastIsoRun;
740*0e209d39SAndroid Build Coastguard Worker bd->isoRunLast--;
741*0e209d39SAndroid Build Coastguard Worker pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
742*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastBase=ON;
743*0e209d39SAndroid Build Coastguard Worker }
744*0e209d39SAndroid Build Coastguard Worker
745*0e209d39SAndroid Build Coastguard Worker /* newly found opening bracket: create an openings entry */
746*0e209d39SAndroid Build Coastguard Worker static UBool /* return true if success */
bracketAddOpening(BracketData * bd,char16_t match,int32_t position)747*0e209d39SAndroid Build Coastguard Worker bracketAddOpening(BracketData *bd, char16_t match, int32_t position) {
748*0e209d39SAndroid Build Coastguard Worker IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
749*0e209d39SAndroid Build Coastguard Worker Opening *pOpening;
750*0e209d39SAndroid Build Coastguard Worker if(pLastIsoRun->limit>=bd->openingsCount) { /* no available new entry */
751*0e209d39SAndroid Build Coastguard Worker UBiDi *pBiDi=bd->pBiDi;
752*0e209d39SAndroid Build Coastguard Worker if(!getInitialOpeningsMemory(pBiDi, pLastIsoRun->limit * 2))
753*0e209d39SAndroid Build Coastguard Worker return false;
754*0e209d39SAndroid Build Coastguard Worker if(bd->openings==bd->simpleOpenings)
755*0e209d39SAndroid Build Coastguard Worker uprv_memcpy(pBiDi->openingsMemory, bd->simpleOpenings,
756*0e209d39SAndroid Build Coastguard Worker SIMPLE_OPENINGS_COUNT * sizeof(Opening));
757*0e209d39SAndroid Build Coastguard Worker bd->openings=pBiDi->openingsMemory; /* may have changed */
758*0e209d39SAndroid Build Coastguard Worker bd->openingsCount=pBiDi->openingsSize / sizeof(Opening);
759*0e209d39SAndroid Build Coastguard Worker }
760*0e209d39SAndroid Build Coastguard Worker pOpening=&bd->openings[pLastIsoRun->limit];
761*0e209d39SAndroid Build Coastguard Worker pOpening->position=position;
762*0e209d39SAndroid Build Coastguard Worker pOpening->match=match;
763*0e209d39SAndroid Build Coastguard Worker pOpening->contextDir=pLastIsoRun->contextDir;
764*0e209d39SAndroid Build Coastguard Worker pOpening->contextPos=pLastIsoRun->contextPos;
765*0e209d39SAndroid Build Coastguard Worker pOpening->flags=0;
766*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->limit++;
767*0e209d39SAndroid Build Coastguard Worker return true;
768*0e209d39SAndroid Build Coastguard Worker }
769*0e209d39SAndroid Build Coastguard Worker
770*0e209d39SAndroid Build Coastguard Worker /* change N0c1 to N0c2 when a preceding bracket is assigned the embedding level */
771*0e209d39SAndroid Build Coastguard Worker static void
fixN0c(BracketData * bd,int32_t openingIndex,int32_t newPropPosition,DirProp newProp)772*0e209d39SAndroid Build Coastguard Worker fixN0c(BracketData *bd, int32_t openingIndex, int32_t newPropPosition, DirProp newProp) {
773*0e209d39SAndroid Build Coastguard Worker /* This function calls itself recursively */
774*0e209d39SAndroid Build Coastguard Worker IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
775*0e209d39SAndroid Build Coastguard Worker Opening *qOpening;
776*0e209d39SAndroid Build Coastguard Worker DirProp *dirProps=bd->pBiDi->dirProps;
777*0e209d39SAndroid Build Coastguard Worker int32_t k, openingPosition, closingPosition;
778*0e209d39SAndroid Build Coastguard Worker for(k=openingIndex+1, qOpening=&bd->openings[k]; k<pLastIsoRun->limit; k++, qOpening++) {
779*0e209d39SAndroid Build Coastguard Worker if(qOpening->match>=0) /* not an N0c match */
780*0e209d39SAndroid Build Coastguard Worker continue;
781*0e209d39SAndroid Build Coastguard Worker if(newPropPosition<qOpening->contextPos)
782*0e209d39SAndroid Build Coastguard Worker break;
783*0e209d39SAndroid Build Coastguard Worker if(newPropPosition>=qOpening->position)
784*0e209d39SAndroid Build Coastguard Worker continue;
785*0e209d39SAndroid Build Coastguard Worker if(newProp==qOpening->contextDir)
786*0e209d39SAndroid Build Coastguard Worker break;
787*0e209d39SAndroid Build Coastguard Worker openingPosition=qOpening->position;
788*0e209d39SAndroid Build Coastguard Worker dirProps[openingPosition]=newProp;
789*0e209d39SAndroid Build Coastguard Worker closingPosition=-(qOpening->match);
790*0e209d39SAndroid Build Coastguard Worker dirProps[closingPosition]=newProp;
791*0e209d39SAndroid Build Coastguard Worker qOpening->match=0; /* prevent further changes */
792*0e209d39SAndroid Build Coastguard Worker fixN0c(bd, k, openingPosition, newProp);
793*0e209d39SAndroid Build Coastguard Worker fixN0c(bd, k, closingPosition, newProp);
794*0e209d39SAndroid Build Coastguard Worker }
795*0e209d39SAndroid Build Coastguard Worker }
796*0e209d39SAndroid Build Coastguard Worker
797*0e209d39SAndroid Build Coastguard Worker /* process closing bracket */
798*0e209d39SAndroid Build Coastguard Worker static DirProp /* return L or R if N0b or N0c, ON if N0d */
bracketProcessClosing(BracketData * bd,int32_t openIdx,int32_t position)799*0e209d39SAndroid Build Coastguard Worker bracketProcessClosing(BracketData *bd, int32_t openIdx, int32_t position) {
800*0e209d39SAndroid Build Coastguard Worker IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
801*0e209d39SAndroid Build Coastguard Worker Opening *pOpening, *qOpening;
802*0e209d39SAndroid Build Coastguard Worker UBiDiDirection direction;
803*0e209d39SAndroid Build Coastguard Worker UBool stable;
804*0e209d39SAndroid Build Coastguard Worker DirProp newProp;
805*0e209d39SAndroid Build Coastguard Worker pOpening=&bd->openings[openIdx];
806*0e209d39SAndroid Build Coastguard Worker direction=(UBiDiDirection)(pLastIsoRun->level&1);
807*0e209d39SAndroid Build Coastguard Worker stable=true; /* assume stable until proved otherwise */
808*0e209d39SAndroid Build Coastguard Worker
809*0e209d39SAndroid Build Coastguard Worker /* The stable flag is set when brackets are paired and their
810*0e209d39SAndroid Build Coastguard Worker level is resolved and cannot be changed by what will be
811*0e209d39SAndroid Build Coastguard Worker found later in the source string.
812*0e209d39SAndroid Build Coastguard Worker An unstable match can occur only when applying N0c, where
813*0e209d39SAndroid Build Coastguard Worker the resolved level depends on the preceding context, and
814*0e209d39SAndroid Build Coastguard Worker this context may be affected by text occurring later.
815*0e209d39SAndroid Build Coastguard Worker Example: RTL paragraph containing: abc[(latin) HEBREW]
816*0e209d39SAndroid Build Coastguard Worker When the closing parenthesis is encountered, it appears
817*0e209d39SAndroid Build Coastguard Worker that N0c1 must be applied since 'abc' sets an opposite
818*0e209d39SAndroid Build Coastguard Worker direction context and both parentheses receive level 2.
819*0e209d39SAndroid Build Coastguard Worker However, when the closing square bracket is processed,
820*0e209d39SAndroid Build Coastguard Worker N0b applies because of 'HEBREW' being included within the
821*0e209d39SAndroid Build Coastguard Worker brackets, thus the square brackets are treated like R and
822*0e209d39SAndroid Build Coastguard Worker receive level 1. However, this changes the preceding
823*0e209d39SAndroid Build Coastguard Worker context of the opening parenthesis, and it now appears
824*0e209d39SAndroid Build Coastguard Worker that N0c2 must be applied to the parentheses rather than
825*0e209d39SAndroid Build Coastguard Worker N0c1. */
826*0e209d39SAndroid Build Coastguard Worker
827*0e209d39SAndroid Build Coastguard Worker if((direction==0 && pOpening->flags&FOUND_L) ||
828*0e209d39SAndroid Build Coastguard Worker (direction==1 && pOpening->flags&FOUND_R)) { /* N0b */
829*0e209d39SAndroid Build Coastguard Worker newProp=static_cast<DirProp>(direction);
830*0e209d39SAndroid Build Coastguard Worker }
831*0e209d39SAndroid Build Coastguard Worker else if(pOpening->flags&(FOUND_L|FOUND_R)) { /* N0c */
832*0e209d39SAndroid Build Coastguard Worker /* it is stable if there is no containing pair or in
833*0e209d39SAndroid Build Coastguard Worker conditions too complicated and not worth checking */
834*0e209d39SAndroid Build Coastguard Worker stable=(openIdx==pLastIsoRun->start);
835*0e209d39SAndroid Build Coastguard Worker if(direction!=pOpening->contextDir)
836*0e209d39SAndroid Build Coastguard Worker newProp= static_cast<DirProp>(pOpening->contextDir); /* N0c1 */
837*0e209d39SAndroid Build Coastguard Worker else
838*0e209d39SAndroid Build Coastguard Worker newProp= static_cast<DirProp>(direction); /* N0c2 */
839*0e209d39SAndroid Build Coastguard Worker } else {
840*0e209d39SAndroid Build Coastguard Worker /* forget this and any brackets nested within this pair */
841*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->limit= static_cast<uint16_t>(openIdx);
842*0e209d39SAndroid Build Coastguard Worker return ON; /* N0d */
843*0e209d39SAndroid Build Coastguard Worker }
844*0e209d39SAndroid Build Coastguard Worker bd->pBiDi->dirProps[pOpening->position]=newProp;
845*0e209d39SAndroid Build Coastguard Worker bd->pBiDi->dirProps[position]=newProp;
846*0e209d39SAndroid Build Coastguard Worker /* Update nested N0c pairs that may be affected */
847*0e209d39SAndroid Build Coastguard Worker fixN0c(bd, openIdx, pOpening->position, newProp);
848*0e209d39SAndroid Build Coastguard Worker if(stable) {
849*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->limit= static_cast<uint16_t>(openIdx); /* forget any brackets nested within this pair */
850*0e209d39SAndroid Build Coastguard Worker /* remove lower located synonyms if any */
851*0e209d39SAndroid Build Coastguard Worker while(pLastIsoRun->limit>pLastIsoRun->start &&
852*0e209d39SAndroid Build Coastguard Worker bd->openings[pLastIsoRun->limit-1].position==pOpening->position)
853*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->limit--;
854*0e209d39SAndroid Build Coastguard Worker } else {
855*0e209d39SAndroid Build Coastguard Worker int32_t k;
856*0e209d39SAndroid Build Coastguard Worker pOpening->match=-position;
857*0e209d39SAndroid Build Coastguard Worker /* neutralize lower located synonyms if any */
858*0e209d39SAndroid Build Coastguard Worker k=openIdx-1;
859*0e209d39SAndroid Build Coastguard Worker while(k>=pLastIsoRun->start &&
860*0e209d39SAndroid Build Coastguard Worker bd->openings[k].position==pOpening->position)
861*0e209d39SAndroid Build Coastguard Worker bd->openings[k--].match=0;
862*0e209d39SAndroid Build Coastguard Worker /* neutralize any unmatched opening between the current pair;
863*0e209d39SAndroid Build Coastguard Worker this will also neutralize higher located synonyms if any */
864*0e209d39SAndroid Build Coastguard Worker for(k=openIdx+1; k<pLastIsoRun->limit; k++) {
865*0e209d39SAndroid Build Coastguard Worker qOpening=&bd->openings[k];
866*0e209d39SAndroid Build Coastguard Worker if(qOpening->position>=position)
867*0e209d39SAndroid Build Coastguard Worker break;
868*0e209d39SAndroid Build Coastguard Worker if(qOpening->match>0)
869*0e209d39SAndroid Build Coastguard Worker qOpening->match=0;
870*0e209d39SAndroid Build Coastguard Worker }
871*0e209d39SAndroid Build Coastguard Worker }
872*0e209d39SAndroid Build Coastguard Worker return newProp;
873*0e209d39SAndroid Build Coastguard Worker }
874*0e209d39SAndroid Build Coastguard Worker
875*0e209d39SAndroid Build Coastguard Worker /* handle strong characters, digits and candidates for closing brackets */
876*0e209d39SAndroid Build Coastguard Worker static UBool /* return true if success */
bracketProcessChar(BracketData * bd,int32_t position)877*0e209d39SAndroid Build Coastguard Worker bracketProcessChar(BracketData *bd, int32_t position) {
878*0e209d39SAndroid Build Coastguard Worker IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
879*0e209d39SAndroid Build Coastguard Worker DirProp *dirProps, dirProp, newProp;
880*0e209d39SAndroid Build Coastguard Worker UBiDiLevel level;
881*0e209d39SAndroid Build Coastguard Worker dirProps=bd->pBiDi->dirProps;
882*0e209d39SAndroid Build Coastguard Worker dirProp=dirProps[position];
883*0e209d39SAndroid Build Coastguard Worker if(dirProp==ON) {
884*0e209d39SAndroid Build Coastguard Worker char16_t c, match;
885*0e209d39SAndroid Build Coastguard Worker int32_t idx;
886*0e209d39SAndroid Build Coastguard Worker /* First see if it is a matching closing bracket. Hopefully, this is
887*0e209d39SAndroid Build Coastguard Worker more efficient than checking if it is a closing bracket at all */
888*0e209d39SAndroid Build Coastguard Worker c=bd->pBiDi->text[position];
889*0e209d39SAndroid Build Coastguard Worker for(idx=pLastIsoRun->limit-1; idx>=pLastIsoRun->start; idx--) {
890*0e209d39SAndroid Build Coastguard Worker if(bd->openings[idx].match!=c)
891*0e209d39SAndroid Build Coastguard Worker continue;
892*0e209d39SAndroid Build Coastguard Worker /* We have a match */
893*0e209d39SAndroid Build Coastguard Worker newProp=bracketProcessClosing(bd, idx, position);
894*0e209d39SAndroid Build Coastguard Worker if(newProp==ON) { /* N0d */
895*0e209d39SAndroid Build Coastguard Worker c=0; /* prevent handling as an opening */
896*0e209d39SAndroid Build Coastguard Worker break;
897*0e209d39SAndroid Build Coastguard Worker }
898*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastBase=ON;
899*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextDir=(UBiDiDirection)newProp;
900*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextPos=position;
901*0e209d39SAndroid Build Coastguard Worker level=bd->pBiDi->levels[position];
902*0e209d39SAndroid Build Coastguard Worker if(level&UBIDI_LEVEL_OVERRIDE) { /* X4, X5 */
903*0e209d39SAndroid Build Coastguard Worker uint16_t flag;
904*0e209d39SAndroid Build Coastguard Worker int32_t i;
905*0e209d39SAndroid Build Coastguard Worker newProp=level&1;
906*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastStrong=newProp;
907*0e209d39SAndroid Build Coastguard Worker flag=DIRPROP_FLAG(newProp);
908*0e209d39SAndroid Build Coastguard Worker for(i=pLastIsoRun->start; i<idx; i++)
909*0e209d39SAndroid Build Coastguard Worker bd->openings[i].flags|=flag;
910*0e209d39SAndroid Build Coastguard Worker /* matching brackets are not overridden by LRO/RLO */
911*0e209d39SAndroid Build Coastguard Worker bd->pBiDi->levels[position]&=~UBIDI_LEVEL_OVERRIDE;
912*0e209d39SAndroid Build Coastguard Worker }
913*0e209d39SAndroid Build Coastguard Worker /* matching brackets are not overridden by LRO/RLO */
914*0e209d39SAndroid Build Coastguard Worker bd->pBiDi->levels[bd->openings[idx].position]&=~UBIDI_LEVEL_OVERRIDE;
915*0e209d39SAndroid Build Coastguard Worker return true;
916*0e209d39SAndroid Build Coastguard Worker }
917*0e209d39SAndroid Build Coastguard Worker /* We get here only if the ON character is not a matching closing
918*0e209d39SAndroid Build Coastguard Worker bracket or it is a case of N0d */
919*0e209d39SAndroid Build Coastguard Worker /* Now see if it is an opening bracket */
920*0e209d39SAndroid Build Coastguard Worker if(c)
921*0e209d39SAndroid Build Coastguard Worker match= static_cast<char16_t>(u_getBidiPairedBracket(c)); /* get the matching char */
922*0e209d39SAndroid Build Coastguard Worker else
923*0e209d39SAndroid Build Coastguard Worker match=0;
924*0e209d39SAndroid Build Coastguard Worker if(match!=c && /* has a matching char */
925*0e209d39SAndroid Build Coastguard Worker ubidi_getPairedBracketType(c)==U_BPT_OPEN) { /* opening bracket */
926*0e209d39SAndroid Build Coastguard Worker /* special case: process synonyms
927*0e209d39SAndroid Build Coastguard Worker create an opening entry for each synonym */
928*0e209d39SAndroid Build Coastguard Worker if(match==0x232A) { /* RIGHT-POINTING ANGLE BRACKET */
929*0e209d39SAndroid Build Coastguard Worker if(!bracketAddOpening(bd, 0x3009, position))
930*0e209d39SAndroid Build Coastguard Worker return false;
931*0e209d39SAndroid Build Coastguard Worker }
932*0e209d39SAndroid Build Coastguard Worker else if(match==0x3009) { /* RIGHT ANGLE BRACKET */
933*0e209d39SAndroid Build Coastguard Worker if(!bracketAddOpening(bd, 0x232A, position))
934*0e209d39SAndroid Build Coastguard Worker return false;
935*0e209d39SAndroid Build Coastguard Worker }
936*0e209d39SAndroid Build Coastguard Worker if(!bracketAddOpening(bd, match, position))
937*0e209d39SAndroid Build Coastguard Worker return false;
938*0e209d39SAndroid Build Coastguard Worker }
939*0e209d39SAndroid Build Coastguard Worker }
940*0e209d39SAndroid Build Coastguard Worker level=bd->pBiDi->levels[position];
941*0e209d39SAndroid Build Coastguard Worker if(level&UBIDI_LEVEL_OVERRIDE) { /* X4, X5 */
942*0e209d39SAndroid Build Coastguard Worker newProp=level&1;
943*0e209d39SAndroid Build Coastguard Worker if(dirProp!=S && dirProp!=WS && dirProp!=ON)
944*0e209d39SAndroid Build Coastguard Worker dirProps[position]=newProp;
945*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastBase=newProp;
946*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastStrong=newProp;
947*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextDir=(UBiDiDirection)newProp;
948*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextPos=position;
949*0e209d39SAndroid Build Coastguard Worker }
950*0e209d39SAndroid Build Coastguard Worker else if(dirProp<=R || dirProp==AL) {
951*0e209d39SAndroid Build Coastguard Worker newProp= static_cast<DirProp>(DIR_FROM_STRONG(dirProp));
952*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastBase=dirProp;
953*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastStrong=dirProp;
954*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextDir=(UBiDiDirection)newProp;
955*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextPos=position;
956*0e209d39SAndroid Build Coastguard Worker }
957*0e209d39SAndroid Build Coastguard Worker else if(dirProp==EN) {
958*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastBase=EN;
959*0e209d39SAndroid Build Coastguard Worker if(pLastIsoRun->lastStrong==L) {
960*0e209d39SAndroid Build Coastguard Worker newProp=L; /* W7 */
961*0e209d39SAndroid Build Coastguard Worker if(!bd->isNumbersSpecial)
962*0e209d39SAndroid Build Coastguard Worker dirProps[position]=ENL;
963*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextDir=(UBiDiDirection)L;
964*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextPos=position;
965*0e209d39SAndroid Build Coastguard Worker }
966*0e209d39SAndroid Build Coastguard Worker else {
967*0e209d39SAndroid Build Coastguard Worker newProp=R; /* N0 */
968*0e209d39SAndroid Build Coastguard Worker if(pLastIsoRun->lastStrong==AL)
969*0e209d39SAndroid Build Coastguard Worker dirProps[position]=AN; /* W2 */
970*0e209d39SAndroid Build Coastguard Worker else
971*0e209d39SAndroid Build Coastguard Worker dirProps[position]=ENR;
972*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextDir=(UBiDiDirection)R;
973*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextPos=position;
974*0e209d39SAndroid Build Coastguard Worker }
975*0e209d39SAndroid Build Coastguard Worker }
976*0e209d39SAndroid Build Coastguard Worker else if(dirProp==AN) {
977*0e209d39SAndroid Build Coastguard Worker newProp=R; /* N0 */
978*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastBase=AN;
979*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextDir=(UBiDiDirection)R;
980*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->contextPos=position;
981*0e209d39SAndroid Build Coastguard Worker }
982*0e209d39SAndroid Build Coastguard Worker else if(dirProp==NSM) {
983*0e209d39SAndroid Build Coastguard Worker /* if the last real char was ON, change NSM to ON so that it
984*0e209d39SAndroid Build Coastguard Worker will stay ON even if the last real char is a bracket which
985*0e209d39SAndroid Build Coastguard Worker may be changed to L or R */
986*0e209d39SAndroid Build Coastguard Worker newProp=pLastIsoRun->lastBase;
987*0e209d39SAndroid Build Coastguard Worker if(newProp==ON)
988*0e209d39SAndroid Build Coastguard Worker dirProps[position]=newProp;
989*0e209d39SAndroid Build Coastguard Worker }
990*0e209d39SAndroid Build Coastguard Worker else {
991*0e209d39SAndroid Build Coastguard Worker newProp=dirProp;
992*0e209d39SAndroid Build Coastguard Worker pLastIsoRun->lastBase=dirProp;
993*0e209d39SAndroid Build Coastguard Worker }
994*0e209d39SAndroid Build Coastguard Worker if(newProp<=R || newProp==AL) {
995*0e209d39SAndroid Build Coastguard Worker int32_t i;
996*0e209d39SAndroid Build Coastguard Worker uint16_t flag=DIRPROP_FLAG(DIR_FROM_STRONG(newProp));
997*0e209d39SAndroid Build Coastguard Worker for(i=pLastIsoRun->start; i<pLastIsoRun->limit; i++)
998*0e209d39SAndroid Build Coastguard Worker if(position>bd->openings[i].position)
999*0e209d39SAndroid Build Coastguard Worker bd->openings[i].flags|=flag;
1000*0e209d39SAndroid Build Coastguard Worker }
1001*0e209d39SAndroid Build Coastguard Worker return true;
1002*0e209d39SAndroid Build Coastguard Worker }
1003*0e209d39SAndroid Build Coastguard Worker
1004*0e209d39SAndroid Build Coastguard Worker /* perform (X1)..(X9) ------------------------------------------------------- */
1005*0e209d39SAndroid Build Coastguard Worker
1006*0e209d39SAndroid Build Coastguard Worker /* determine if the text is mixed-directional or single-directional */
1007*0e209d39SAndroid Build Coastguard Worker static UBiDiDirection
directionFromFlags(UBiDi * pBiDi)1008*0e209d39SAndroid Build Coastguard Worker directionFromFlags(UBiDi *pBiDi) {
1009*0e209d39SAndroid Build Coastguard Worker Flags flags=pBiDi->flags;
1010*0e209d39SAndroid Build Coastguard Worker /* if the text contains AN and neutrals, then some neutrals may become RTL */
1011*0e209d39SAndroid Build Coastguard Worker if(!(flags&MASK_RTL || ((flags&DIRPROP_FLAG(AN)) && (flags&MASK_POSSIBLE_N)))) {
1012*0e209d39SAndroid Build Coastguard Worker return UBIDI_LTR;
1013*0e209d39SAndroid Build Coastguard Worker } else if(!(flags&MASK_LTR)) {
1014*0e209d39SAndroid Build Coastguard Worker return UBIDI_RTL;
1015*0e209d39SAndroid Build Coastguard Worker } else {
1016*0e209d39SAndroid Build Coastguard Worker return UBIDI_MIXED;
1017*0e209d39SAndroid Build Coastguard Worker }
1018*0e209d39SAndroid Build Coastguard Worker }
1019*0e209d39SAndroid Build Coastguard Worker
1020*0e209d39SAndroid Build Coastguard Worker /*
1021*0e209d39SAndroid Build Coastguard Worker * Resolve the explicit levels as specified by explicit embedding codes.
1022*0e209d39SAndroid Build Coastguard Worker * Recalculate the flags to have them reflect the real properties
1023*0e209d39SAndroid Build Coastguard Worker * after taking the explicit embeddings into account.
1024*0e209d39SAndroid Build Coastguard Worker *
1025*0e209d39SAndroid Build Coastguard Worker * The BiDi algorithm is designed to result in the same behavior whether embedding
1026*0e209d39SAndroid Build Coastguard Worker * levels are externally specified (from "styled text", supposedly the preferred
1027*0e209d39SAndroid Build Coastguard Worker * method) or set by explicit embedding codes (LRx, RLx, PDF, FSI, PDI) in the plain text.
1028*0e209d39SAndroid Build Coastguard Worker * That is why (X9) instructs to remove all not-isolate explicit codes (and BN).
1029*0e209d39SAndroid Build Coastguard Worker * However, in a real implementation, the removal of these codes and their index
1030*0e209d39SAndroid Build Coastguard Worker * positions in the plain text is undesirable since it would result in
1031*0e209d39SAndroid Build Coastguard Worker * reallocated, reindexed text.
1032*0e209d39SAndroid Build Coastguard Worker * Instead, this implementation leaves the codes in there and just ignores them
1033*0e209d39SAndroid Build Coastguard Worker * in the subsequent processing.
1034*0e209d39SAndroid Build Coastguard Worker * In order to get the same reordering behavior, positions with a BN or a not-isolate
1035*0e209d39SAndroid Build Coastguard Worker * explicit embedding code just get the same level assigned as the last "real"
1036*0e209d39SAndroid Build Coastguard Worker * character.
1037*0e209d39SAndroid Build Coastguard Worker *
1038*0e209d39SAndroid Build Coastguard Worker * Some implementations, not this one, then overwrite some of these
1039*0e209d39SAndroid Build Coastguard Worker * directionality properties at "real" same-level-run boundaries by
1040*0e209d39SAndroid Build Coastguard Worker * L or R codes so that the resolution of weak types can be performed on the
1041*0e209d39SAndroid Build Coastguard Worker * entire paragraph at once instead of having to parse it once more and
1042*0e209d39SAndroid Build Coastguard Worker * perform that resolution on same-level-runs.
1043*0e209d39SAndroid Build Coastguard Worker * This limits the scope of the implicit rules in effectively
1044*0e209d39SAndroid Build Coastguard Worker * the same way as the run limits.
1045*0e209d39SAndroid Build Coastguard Worker *
1046*0e209d39SAndroid Build Coastguard Worker * Instead, this implementation does not modify these codes, except for
1047*0e209d39SAndroid Build Coastguard Worker * paired brackets whose properties (ON) may be replaced by L or R.
1048*0e209d39SAndroid Build Coastguard Worker * On one hand, the paragraph has to be scanned for same-level-runs, but
1049*0e209d39SAndroid Build Coastguard Worker * on the other hand, this saves another loop to reset these codes,
1050*0e209d39SAndroid Build Coastguard Worker * or saves making and modifying a copy of dirProps[].
1051*0e209d39SAndroid Build Coastguard Worker *
1052*0e209d39SAndroid Build Coastguard Worker *
1053*0e209d39SAndroid Build Coastguard Worker * Note that (Pn) and (Xn) changed significantly from version 4 of the BiDi algorithm.
1054*0e209d39SAndroid Build Coastguard Worker *
1055*0e209d39SAndroid Build Coastguard Worker *
1056*0e209d39SAndroid Build Coastguard Worker * Handling the stack of explicit levels (Xn):
1057*0e209d39SAndroid Build Coastguard Worker *
1058*0e209d39SAndroid Build Coastguard Worker * With the BiDi stack of explicit levels, as pushed with each
1059*0e209d39SAndroid Build Coastguard Worker * LRE, RLE, LRO, RLO, LRI, RLI and FSI and popped with each PDF and PDI,
1060*0e209d39SAndroid Build Coastguard Worker * the explicit level must never exceed UBIDI_MAX_EXPLICIT_LEVEL.
1061*0e209d39SAndroid Build Coastguard Worker *
1062*0e209d39SAndroid Build Coastguard Worker * In order to have a correct push-pop semantics even in the case of overflows,
1063*0e209d39SAndroid Build Coastguard Worker * overflow counters and a valid isolate counter are used as described in UAX#9
1064*0e209d39SAndroid Build Coastguard Worker * section 3.3.2 "Explicit Levels and Directions".
1065*0e209d39SAndroid Build Coastguard Worker *
1066*0e209d39SAndroid Build Coastguard Worker * This implementation assumes that UBIDI_MAX_EXPLICIT_LEVEL is odd.
1067*0e209d39SAndroid Build Coastguard Worker *
1068*0e209d39SAndroid Build Coastguard Worker * Returns normally the direction; -1 if there was a memory shortage
1069*0e209d39SAndroid Build Coastguard Worker *
1070*0e209d39SAndroid Build Coastguard Worker */
1071*0e209d39SAndroid Build Coastguard Worker static UBiDiDirection
resolveExplicitLevels(UBiDi * pBiDi,UErrorCode * pErrorCode)1072*0e209d39SAndroid Build Coastguard Worker resolveExplicitLevels(UBiDi *pBiDi, UErrorCode *pErrorCode) {
1073*0e209d39SAndroid Build Coastguard Worker DirProp *dirProps=pBiDi->dirProps;
1074*0e209d39SAndroid Build Coastguard Worker UBiDiLevel *levels=pBiDi->levels;
1075*0e209d39SAndroid Build Coastguard Worker const char16_t *text=pBiDi->text;
1076*0e209d39SAndroid Build Coastguard Worker
1077*0e209d39SAndroid Build Coastguard Worker int32_t i=0, length=pBiDi->length;
1078*0e209d39SAndroid Build Coastguard Worker Flags flags=pBiDi->flags; /* collect all directionalities in the text */
1079*0e209d39SAndroid Build Coastguard Worker DirProp dirProp;
1080*0e209d39SAndroid Build Coastguard Worker UBiDiLevel level=GET_PARALEVEL(pBiDi, 0);
1081*0e209d39SAndroid Build Coastguard Worker UBiDiDirection direction;
1082*0e209d39SAndroid Build Coastguard Worker pBiDi->isolateCount=0;
1083*0e209d39SAndroid Build Coastguard Worker
1084*0e209d39SAndroid Build Coastguard Worker if(U_FAILURE(*pErrorCode)) { return UBIDI_LTR; }
1085*0e209d39SAndroid Build Coastguard Worker
1086*0e209d39SAndroid Build Coastguard Worker /* determine if the text is mixed-directional or single-directional */
1087*0e209d39SAndroid Build Coastguard Worker direction=directionFromFlags(pBiDi);
1088*0e209d39SAndroid Build Coastguard Worker
1089*0e209d39SAndroid Build Coastguard Worker /* we may not need to resolve any explicit levels */
1090*0e209d39SAndroid Build Coastguard Worker if((direction!=UBIDI_MIXED)) {
1091*0e209d39SAndroid Build Coastguard Worker /* not mixed directionality: levels don't matter - trailingWSStart will be 0 */
1092*0e209d39SAndroid Build Coastguard Worker return direction;
1093*0e209d39SAndroid Build Coastguard Worker }
1094*0e209d39SAndroid Build Coastguard Worker if(pBiDi->reorderingMode > UBIDI_REORDER_LAST_LOGICAL_TO_VISUAL) {
1095*0e209d39SAndroid Build Coastguard Worker /* inverse BiDi: mixed, but all characters are at the same embedding level */
1096*0e209d39SAndroid Build Coastguard Worker /* set all levels to the paragraph level */
1097*0e209d39SAndroid Build Coastguard Worker int32_t paraIndex, start, limit;
1098*0e209d39SAndroid Build Coastguard Worker for(paraIndex=0; paraIndex<pBiDi->paraCount; paraIndex++) {
1099*0e209d39SAndroid Build Coastguard Worker if(paraIndex==0)
1100*0e209d39SAndroid Build Coastguard Worker start=0;
1101*0e209d39SAndroid Build Coastguard Worker else
1102*0e209d39SAndroid Build Coastguard Worker start=pBiDi->paras[paraIndex-1].limit;
1103*0e209d39SAndroid Build Coastguard Worker limit=pBiDi->paras[paraIndex].limit;
1104*0e209d39SAndroid Build Coastguard Worker level= static_cast<UBiDiLevel>(pBiDi->paras[paraIndex].level);
1105*0e209d39SAndroid Build Coastguard Worker for(i=start; i<limit; i++)
1106*0e209d39SAndroid Build Coastguard Worker levels[i]=level;
1107*0e209d39SAndroid Build Coastguard Worker }
1108*0e209d39SAndroid Build Coastguard Worker return direction; /* no bracket matching for inverse BiDi */
1109*0e209d39SAndroid Build Coastguard Worker }
1110*0e209d39SAndroid Build Coastguard Worker if(!(flags&(MASK_EXPLICIT|MASK_ISO))) {
1111*0e209d39SAndroid Build Coastguard Worker /* no embeddings, set all levels to the paragraph level */
1112*0e209d39SAndroid Build Coastguard Worker /* we still have to perform bracket matching */
1113*0e209d39SAndroid Build Coastguard Worker int32_t paraIndex, start, limit;
1114*0e209d39SAndroid Build Coastguard Worker BracketData bracketData;
1115*0e209d39SAndroid Build Coastguard Worker bracketInit(pBiDi, &bracketData);
1116*0e209d39SAndroid Build Coastguard Worker for(paraIndex=0; paraIndex<pBiDi->paraCount; paraIndex++) {
1117*0e209d39SAndroid Build Coastguard Worker if(paraIndex==0)
1118*0e209d39SAndroid Build Coastguard Worker start=0;
1119*0e209d39SAndroid Build Coastguard Worker else
1120*0e209d39SAndroid Build Coastguard Worker start=pBiDi->paras[paraIndex-1].limit;
1121*0e209d39SAndroid Build Coastguard Worker limit=pBiDi->paras[paraIndex].limit;
1122*0e209d39SAndroid Build Coastguard Worker level= static_cast<UBiDiLevel>(pBiDi->paras[paraIndex].level);
1123*0e209d39SAndroid Build Coastguard Worker for(i=start; i<limit; i++) {
1124*0e209d39SAndroid Build Coastguard Worker levels[i]=level;
1125*0e209d39SAndroid Build Coastguard Worker dirProp=dirProps[i];
1126*0e209d39SAndroid Build Coastguard Worker if(dirProp==BN)
1127*0e209d39SAndroid Build Coastguard Worker continue;
1128*0e209d39SAndroid Build Coastguard Worker if(dirProp==B) {
1129*0e209d39SAndroid Build Coastguard Worker if((i+1)<length) {
1130*0e209d39SAndroid Build Coastguard Worker if(text[i]==CR && text[i+1]==LF)
1131*0e209d39SAndroid Build Coastguard Worker continue; /* skip CR when followed by LF */
1132*0e209d39SAndroid Build Coastguard Worker bracketProcessB(&bracketData, level);
1133*0e209d39SAndroid Build Coastguard Worker }
1134*0e209d39SAndroid Build Coastguard Worker continue;
1135*0e209d39SAndroid Build Coastguard Worker }
1136*0e209d39SAndroid Build Coastguard Worker if(!bracketProcessChar(&bracketData, i)) {
1137*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
1138*0e209d39SAndroid Build Coastguard Worker return UBIDI_LTR;
1139*0e209d39SAndroid Build Coastguard Worker }
1140*0e209d39SAndroid Build Coastguard Worker }
1141*0e209d39SAndroid Build Coastguard Worker }
1142*0e209d39SAndroid Build Coastguard Worker return direction;
1143*0e209d39SAndroid Build Coastguard Worker }
1144*0e209d39SAndroid Build Coastguard Worker {
1145*0e209d39SAndroid Build Coastguard Worker /* continue to perform (Xn) */
1146*0e209d39SAndroid Build Coastguard Worker
1147*0e209d39SAndroid Build Coastguard Worker /* (X1) level is set for all codes, embeddingLevel keeps track of the push/pop operations */
1148*0e209d39SAndroid Build Coastguard Worker /* both variables may carry the UBIDI_LEVEL_OVERRIDE flag to indicate the override status */
1149*0e209d39SAndroid Build Coastguard Worker UBiDiLevel embeddingLevel=level, newLevel;
1150*0e209d39SAndroid Build Coastguard Worker UBiDiLevel previousLevel=level; /* previous level for regular (not CC) characters */
1151*0e209d39SAndroid Build Coastguard Worker int32_t lastCcPos=0; /* index of last effective LRx,RLx, PDx */
1152*0e209d39SAndroid Build Coastguard Worker
1153*0e209d39SAndroid Build Coastguard Worker /* The following stack remembers the embedding level and the ISOLATE flag of level runs.
1154*0e209d39SAndroid Build Coastguard Worker stackLast points to its current entry. */
1155*0e209d39SAndroid Build Coastguard Worker uint16_t stack[UBIDI_MAX_EXPLICIT_LEVEL+2]; /* we never push anything >=UBIDI_MAX_EXPLICIT_LEVEL
1156*0e209d39SAndroid Build Coastguard Worker but we need one more entry as base */
1157*0e209d39SAndroid Build Coastguard Worker uint32_t stackLast=0;
1158*0e209d39SAndroid Build Coastguard Worker int32_t overflowIsolateCount=0;
1159*0e209d39SAndroid Build Coastguard Worker int32_t overflowEmbeddingCount=0;
1160*0e209d39SAndroid Build Coastguard Worker int32_t validIsolateCount=0;
1161*0e209d39SAndroid Build Coastguard Worker BracketData bracketData;
1162*0e209d39SAndroid Build Coastguard Worker bracketInit(pBiDi, &bracketData);
1163*0e209d39SAndroid Build Coastguard Worker stack[0]=level; /* initialize base entry to para level, no override, no isolate */
1164*0e209d39SAndroid Build Coastguard Worker
1165*0e209d39SAndroid Build Coastguard Worker /* recalculate the flags */
1166*0e209d39SAndroid Build Coastguard Worker flags=0;
1167*0e209d39SAndroid Build Coastguard Worker
1168*0e209d39SAndroid Build Coastguard Worker for(i=0; i<length; ++i) {
1169*0e209d39SAndroid Build Coastguard Worker dirProp=dirProps[i];
1170*0e209d39SAndroid Build Coastguard Worker switch(dirProp) {
1171*0e209d39SAndroid Build Coastguard Worker case LRE:
1172*0e209d39SAndroid Build Coastguard Worker case RLE:
1173*0e209d39SAndroid Build Coastguard Worker case LRO:
1174*0e209d39SAndroid Build Coastguard Worker case RLO:
1175*0e209d39SAndroid Build Coastguard Worker /* (X2, X3, X4, X5) */
1176*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(BN);
1177*0e209d39SAndroid Build Coastguard Worker levels[i]=previousLevel;
1178*0e209d39SAndroid Build Coastguard Worker if (dirProp==LRE || dirProp==LRO)
1179*0e209d39SAndroid Build Coastguard Worker /* least greater even level */
1180*0e209d39SAndroid Build Coastguard Worker newLevel=(UBiDiLevel)((embeddingLevel+2)&~(UBIDI_LEVEL_OVERRIDE|1));
1181*0e209d39SAndroid Build Coastguard Worker else
1182*0e209d39SAndroid Build Coastguard Worker /* least greater odd level */
1183*0e209d39SAndroid Build Coastguard Worker newLevel=(UBiDiLevel)((NO_OVERRIDE(embeddingLevel)+1)|1);
1184*0e209d39SAndroid Build Coastguard Worker if(newLevel<=UBIDI_MAX_EXPLICIT_LEVEL && overflowIsolateCount==0 &&
1185*0e209d39SAndroid Build Coastguard Worker overflowEmbeddingCount==0) {
1186*0e209d39SAndroid Build Coastguard Worker lastCcPos=i;
1187*0e209d39SAndroid Build Coastguard Worker embeddingLevel=newLevel;
1188*0e209d39SAndroid Build Coastguard Worker if(dirProp==LRO || dirProp==RLO)
1189*0e209d39SAndroid Build Coastguard Worker embeddingLevel|=UBIDI_LEVEL_OVERRIDE;
1190*0e209d39SAndroid Build Coastguard Worker stackLast++;
1191*0e209d39SAndroid Build Coastguard Worker stack[stackLast]=embeddingLevel;
1192*0e209d39SAndroid Build Coastguard Worker /* we don't need to set UBIDI_LEVEL_OVERRIDE off for LRE and RLE
1193*0e209d39SAndroid Build Coastguard Worker since this has already been done for newLevel which is
1194*0e209d39SAndroid Build Coastguard Worker the source for embeddingLevel.
1195*0e209d39SAndroid Build Coastguard Worker */
1196*0e209d39SAndroid Build Coastguard Worker } else {
1197*0e209d39SAndroid Build Coastguard Worker if(overflowIsolateCount==0)
1198*0e209d39SAndroid Build Coastguard Worker overflowEmbeddingCount++;
1199*0e209d39SAndroid Build Coastguard Worker }
1200*0e209d39SAndroid Build Coastguard Worker break;
1201*0e209d39SAndroid Build Coastguard Worker case PDF:
1202*0e209d39SAndroid Build Coastguard Worker /* (X7) */
1203*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(BN);
1204*0e209d39SAndroid Build Coastguard Worker levels[i]=previousLevel;
1205*0e209d39SAndroid Build Coastguard Worker /* handle all the overflow cases first */
1206*0e209d39SAndroid Build Coastguard Worker if(overflowIsolateCount) {
1207*0e209d39SAndroid Build Coastguard Worker break;
1208*0e209d39SAndroid Build Coastguard Worker }
1209*0e209d39SAndroid Build Coastguard Worker if(overflowEmbeddingCount) {
1210*0e209d39SAndroid Build Coastguard Worker overflowEmbeddingCount--;
1211*0e209d39SAndroid Build Coastguard Worker break;
1212*0e209d39SAndroid Build Coastguard Worker }
1213*0e209d39SAndroid Build Coastguard Worker if(stackLast>0 && stack[stackLast]<ISOLATE) { /* not an isolate entry */
1214*0e209d39SAndroid Build Coastguard Worker lastCcPos=i;
1215*0e209d39SAndroid Build Coastguard Worker stackLast--;
1216*0e209d39SAndroid Build Coastguard Worker embeddingLevel=(UBiDiLevel)stack[stackLast];
1217*0e209d39SAndroid Build Coastguard Worker }
1218*0e209d39SAndroid Build Coastguard Worker break;
1219*0e209d39SAndroid Build Coastguard Worker case LRI:
1220*0e209d39SAndroid Build Coastguard Worker case RLI:
1221*0e209d39SAndroid Build Coastguard Worker flags|=(DIRPROP_FLAG(ON)|DIRPROP_FLAG_LR(embeddingLevel));
1222*0e209d39SAndroid Build Coastguard Worker levels[i]=NO_OVERRIDE(embeddingLevel);
1223*0e209d39SAndroid Build Coastguard Worker if(NO_OVERRIDE(embeddingLevel)!=NO_OVERRIDE(previousLevel)) {
1224*0e209d39SAndroid Build Coastguard Worker bracketProcessBoundary(&bracketData, lastCcPos,
1225*0e209d39SAndroid Build Coastguard Worker previousLevel, embeddingLevel);
1226*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG_MULTI_RUNS;
1227*0e209d39SAndroid Build Coastguard Worker }
1228*0e209d39SAndroid Build Coastguard Worker previousLevel=embeddingLevel;
1229*0e209d39SAndroid Build Coastguard Worker /* (X5a, X5b) */
1230*0e209d39SAndroid Build Coastguard Worker if(dirProp==LRI)
1231*0e209d39SAndroid Build Coastguard Worker /* least greater even level */
1232*0e209d39SAndroid Build Coastguard Worker newLevel=(UBiDiLevel)((embeddingLevel+2)&~(UBIDI_LEVEL_OVERRIDE|1));
1233*0e209d39SAndroid Build Coastguard Worker else
1234*0e209d39SAndroid Build Coastguard Worker /* least greater odd level */
1235*0e209d39SAndroid Build Coastguard Worker newLevel=(UBiDiLevel)((NO_OVERRIDE(embeddingLevel)+1)|1);
1236*0e209d39SAndroid Build Coastguard Worker if(newLevel<=UBIDI_MAX_EXPLICIT_LEVEL && overflowIsolateCount==0 &&
1237*0e209d39SAndroid Build Coastguard Worker overflowEmbeddingCount==0) {
1238*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(dirProp);
1239*0e209d39SAndroid Build Coastguard Worker lastCcPos=i;
1240*0e209d39SAndroid Build Coastguard Worker validIsolateCount++;
1241*0e209d39SAndroid Build Coastguard Worker if(validIsolateCount>pBiDi->isolateCount)
1242*0e209d39SAndroid Build Coastguard Worker pBiDi->isolateCount=validIsolateCount;
1243*0e209d39SAndroid Build Coastguard Worker embeddingLevel=newLevel;
1244*0e209d39SAndroid Build Coastguard Worker /* we can increment stackLast without checking because newLevel
1245*0e209d39SAndroid Build Coastguard Worker will exceed UBIDI_MAX_EXPLICIT_LEVEL before stackLast overflows */
1246*0e209d39SAndroid Build Coastguard Worker stackLast++;
1247*0e209d39SAndroid Build Coastguard Worker stack[stackLast]=embeddingLevel+ISOLATE;
1248*0e209d39SAndroid Build Coastguard Worker bracketProcessLRI_RLI(&bracketData, embeddingLevel);
1249*0e209d39SAndroid Build Coastguard Worker } else {
1250*0e209d39SAndroid Build Coastguard Worker /* make it WS so that it is handled by adjustWSLevels() */
1251*0e209d39SAndroid Build Coastguard Worker dirProps[i]=WS;
1252*0e209d39SAndroid Build Coastguard Worker overflowIsolateCount++;
1253*0e209d39SAndroid Build Coastguard Worker }
1254*0e209d39SAndroid Build Coastguard Worker break;
1255*0e209d39SAndroid Build Coastguard Worker case PDI:
1256*0e209d39SAndroid Build Coastguard Worker if(NO_OVERRIDE(embeddingLevel)!=NO_OVERRIDE(previousLevel)) {
1257*0e209d39SAndroid Build Coastguard Worker bracketProcessBoundary(&bracketData, lastCcPos,
1258*0e209d39SAndroid Build Coastguard Worker previousLevel, embeddingLevel);
1259*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG_MULTI_RUNS;
1260*0e209d39SAndroid Build Coastguard Worker }
1261*0e209d39SAndroid Build Coastguard Worker /* (X6a) */
1262*0e209d39SAndroid Build Coastguard Worker if(overflowIsolateCount) {
1263*0e209d39SAndroid Build Coastguard Worker overflowIsolateCount--;
1264*0e209d39SAndroid Build Coastguard Worker /* make it WS so that it is handled by adjustWSLevels() */
1265*0e209d39SAndroid Build Coastguard Worker dirProps[i]=WS;
1266*0e209d39SAndroid Build Coastguard Worker }
1267*0e209d39SAndroid Build Coastguard Worker else if(validIsolateCount) {
1268*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(PDI);
1269*0e209d39SAndroid Build Coastguard Worker lastCcPos=i;
1270*0e209d39SAndroid Build Coastguard Worker overflowEmbeddingCount=0;
1271*0e209d39SAndroid Build Coastguard Worker while(stack[stackLast]<ISOLATE) /* pop embedding entries */
1272*0e209d39SAndroid Build Coastguard Worker stackLast--; /* until the last isolate entry */
1273*0e209d39SAndroid Build Coastguard Worker stackLast--; /* pop also the last isolate entry */
1274*0e209d39SAndroid Build Coastguard Worker validIsolateCount--;
1275*0e209d39SAndroid Build Coastguard Worker bracketProcessPDI(&bracketData);
1276*0e209d39SAndroid Build Coastguard Worker } else
1277*0e209d39SAndroid Build Coastguard Worker /* make it WS so that it is handled by adjustWSLevels() */
1278*0e209d39SAndroid Build Coastguard Worker dirProps[i]=WS;
1279*0e209d39SAndroid Build Coastguard Worker embeddingLevel=(UBiDiLevel)stack[stackLast]&~ISOLATE;
1280*0e209d39SAndroid Build Coastguard Worker flags|=(DIRPROP_FLAG(ON)|DIRPROP_FLAG_LR(embeddingLevel));
1281*0e209d39SAndroid Build Coastguard Worker previousLevel=embeddingLevel;
1282*0e209d39SAndroid Build Coastguard Worker levels[i]=NO_OVERRIDE(embeddingLevel);
1283*0e209d39SAndroid Build Coastguard Worker break;
1284*0e209d39SAndroid Build Coastguard Worker case B:
1285*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(B);
1286*0e209d39SAndroid Build Coastguard Worker levels[i]=GET_PARALEVEL(pBiDi, i);
1287*0e209d39SAndroid Build Coastguard Worker if((i+1)<length) {
1288*0e209d39SAndroid Build Coastguard Worker if(text[i]==CR && text[i+1]==LF)
1289*0e209d39SAndroid Build Coastguard Worker break; /* skip CR when followed by LF */
1290*0e209d39SAndroid Build Coastguard Worker overflowEmbeddingCount=overflowIsolateCount=0;
1291*0e209d39SAndroid Build Coastguard Worker validIsolateCount=0;
1292*0e209d39SAndroid Build Coastguard Worker stackLast=0;
1293*0e209d39SAndroid Build Coastguard Worker previousLevel=embeddingLevel=GET_PARALEVEL(pBiDi, i+1);
1294*0e209d39SAndroid Build Coastguard Worker stack[0]=embeddingLevel; /* initialize base entry to para level, no override, no isolate */
1295*0e209d39SAndroid Build Coastguard Worker bracketProcessB(&bracketData, embeddingLevel);
1296*0e209d39SAndroid Build Coastguard Worker }
1297*0e209d39SAndroid Build Coastguard Worker break;
1298*0e209d39SAndroid Build Coastguard Worker case BN:
1299*0e209d39SAndroid Build Coastguard Worker /* BN, LRE, RLE, and PDF are supposed to be removed (X9) */
1300*0e209d39SAndroid Build Coastguard Worker /* they will get their levels set correctly in adjustWSLevels() */
1301*0e209d39SAndroid Build Coastguard Worker levels[i]=previousLevel;
1302*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(BN);
1303*0e209d39SAndroid Build Coastguard Worker break;
1304*0e209d39SAndroid Build Coastguard Worker default:
1305*0e209d39SAndroid Build Coastguard Worker /* all other types are normal characters and get the "real" level */
1306*0e209d39SAndroid Build Coastguard Worker if(NO_OVERRIDE(embeddingLevel)!=NO_OVERRIDE(previousLevel)) {
1307*0e209d39SAndroid Build Coastguard Worker bracketProcessBoundary(&bracketData, lastCcPos,
1308*0e209d39SAndroid Build Coastguard Worker previousLevel, embeddingLevel);
1309*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG_MULTI_RUNS;
1310*0e209d39SAndroid Build Coastguard Worker if(embeddingLevel&UBIDI_LEVEL_OVERRIDE)
1311*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG_O(embeddingLevel);
1312*0e209d39SAndroid Build Coastguard Worker else
1313*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG_E(embeddingLevel);
1314*0e209d39SAndroid Build Coastguard Worker }
1315*0e209d39SAndroid Build Coastguard Worker previousLevel=embeddingLevel;
1316*0e209d39SAndroid Build Coastguard Worker levels[i]=embeddingLevel;
1317*0e209d39SAndroid Build Coastguard Worker if(!bracketProcessChar(&bracketData, i))
1318*0e209d39SAndroid Build Coastguard Worker return (UBiDiDirection)-1;
1319*0e209d39SAndroid Build Coastguard Worker /* the dirProp may have been changed in bracketProcessChar() */
1320*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(dirProps[i]);
1321*0e209d39SAndroid Build Coastguard Worker break;
1322*0e209d39SAndroid Build Coastguard Worker }
1323*0e209d39SAndroid Build Coastguard Worker }
1324*0e209d39SAndroid Build Coastguard Worker if(flags&MASK_EMBEDDING)
1325*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG_LR(pBiDi->paraLevel);
1326*0e209d39SAndroid Build Coastguard Worker if(pBiDi->orderParagraphsLTR && (flags&DIRPROP_FLAG(B)))
1327*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG(L);
1328*0e209d39SAndroid Build Coastguard Worker /* again, determine if the text is mixed-directional or single-directional */
1329*0e209d39SAndroid Build Coastguard Worker pBiDi->flags=flags;
1330*0e209d39SAndroid Build Coastguard Worker direction=directionFromFlags(pBiDi);
1331*0e209d39SAndroid Build Coastguard Worker }
1332*0e209d39SAndroid Build Coastguard Worker return direction;
1333*0e209d39SAndroid Build Coastguard Worker }
1334*0e209d39SAndroid Build Coastguard Worker
1335*0e209d39SAndroid Build Coastguard Worker /*
1336*0e209d39SAndroid Build Coastguard Worker * Use a pre-specified embedding levels array:
1337*0e209d39SAndroid Build Coastguard Worker *
1338*0e209d39SAndroid Build Coastguard Worker * Adjust the directional properties for overrides (->LEVEL_OVERRIDE),
1339*0e209d39SAndroid Build Coastguard Worker * ignore all explicit codes (X9),
1340*0e209d39SAndroid Build Coastguard Worker * and check all the preset levels.
1341*0e209d39SAndroid Build Coastguard Worker *
1342*0e209d39SAndroid Build Coastguard Worker * Recalculate the flags to have them reflect the real properties
1343*0e209d39SAndroid Build Coastguard Worker * after taking the explicit embeddings into account.
1344*0e209d39SAndroid Build Coastguard Worker */
1345*0e209d39SAndroid Build Coastguard Worker static UBiDiDirection
checkExplicitLevels(UBiDi * pBiDi,UErrorCode * pErrorCode)1346*0e209d39SAndroid Build Coastguard Worker checkExplicitLevels(UBiDi *pBiDi, UErrorCode *pErrorCode) {
1347*0e209d39SAndroid Build Coastguard Worker DirProp *dirProps=pBiDi->dirProps;
1348*0e209d39SAndroid Build Coastguard Worker UBiDiLevel *levels=pBiDi->levels;
1349*0e209d39SAndroid Build Coastguard Worker int32_t isolateCount=0;
1350*0e209d39SAndroid Build Coastguard Worker
1351*0e209d39SAndroid Build Coastguard Worker int32_t length=pBiDi->length;
1352*0e209d39SAndroid Build Coastguard Worker Flags flags=0; /* collect all directionalities in the text */
1353*0e209d39SAndroid Build Coastguard Worker pBiDi->isolateCount=0;
1354*0e209d39SAndroid Build Coastguard Worker
1355*0e209d39SAndroid Build Coastguard Worker int32_t currentParaIndex = 0;
1356*0e209d39SAndroid Build Coastguard Worker int32_t currentParaLimit = pBiDi->paras[0].limit;
1357*0e209d39SAndroid Build Coastguard Worker int32_t currentParaLevel = pBiDi->paraLevel;
1358*0e209d39SAndroid Build Coastguard Worker
1359*0e209d39SAndroid Build Coastguard Worker for(int32_t i=0; i<length; ++i) {
1360*0e209d39SAndroid Build Coastguard Worker UBiDiLevel level=levels[i];
1361*0e209d39SAndroid Build Coastguard Worker DirProp dirProp=dirProps[i];
1362*0e209d39SAndroid Build Coastguard Worker if(dirProp==LRI || dirProp==RLI) {
1363*0e209d39SAndroid Build Coastguard Worker isolateCount++;
1364*0e209d39SAndroid Build Coastguard Worker if(isolateCount>pBiDi->isolateCount)
1365*0e209d39SAndroid Build Coastguard Worker pBiDi->isolateCount=isolateCount;
1366*0e209d39SAndroid Build Coastguard Worker }
1367*0e209d39SAndroid Build Coastguard Worker else if(dirProp==PDI)
1368*0e209d39SAndroid Build Coastguard Worker isolateCount--;
1369*0e209d39SAndroid Build Coastguard Worker else if(dirProp==B)
1370*0e209d39SAndroid Build Coastguard Worker isolateCount=0;
1371*0e209d39SAndroid Build Coastguard Worker
1372*0e209d39SAndroid Build Coastguard Worker // optimized version of int32_t currentParaLevel = GET_PARALEVEL(pBiDi, i);
1373*0e209d39SAndroid Build Coastguard Worker if (pBiDi->defaultParaLevel != 0 &&
1374*0e209d39SAndroid Build Coastguard Worker i == currentParaLimit && (currentParaIndex + 1) < pBiDi->paraCount) {
1375*0e209d39SAndroid Build Coastguard Worker currentParaLevel = pBiDi->paras[++currentParaIndex].level;
1376*0e209d39SAndroid Build Coastguard Worker currentParaLimit = pBiDi->paras[currentParaIndex].limit;
1377*0e209d39SAndroid Build Coastguard Worker }
1378*0e209d39SAndroid Build Coastguard Worker
1379*0e209d39SAndroid Build Coastguard Worker UBiDiLevel overrideFlag = level & UBIDI_LEVEL_OVERRIDE;
1380*0e209d39SAndroid Build Coastguard Worker level &= ~UBIDI_LEVEL_OVERRIDE;
1381*0e209d39SAndroid Build Coastguard Worker if (level < currentParaLevel || UBIDI_MAX_EXPLICIT_LEVEL < level) {
1382*0e209d39SAndroid Build Coastguard Worker if (level == 0) {
1383*0e209d39SAndroid Build Coastguard Worker if (dirProp == B) {
1384*0e209d39SAndroid Build Coastguard Worker // Paragraph separators are ok with explicit level 0.
1385*0e209d39SAndroid Build Coastguard Worker // Prevents reordering of paragraphs.
1386*0e209d39SAndroid Build Coastguard Worker } else {
1387*0e209d39SAndroid Build Coastguard Worker // Treat explicit level 0 as a wildcard for the paragraph level.
1388*0e209d39SAndroid Build Coastguard Worker // Avoid making the caller guess what the paragraph level would be.
1389*0e209d39SAndroid Build Coastguard Worker level = (UBiDiLevel)currentParaLevel;
1390*0e209d39SAndroid Build Coastguard Worker levels[i] = level | overrideFlag;
1391*0e209d39SAndroid Build Coastguard Worker }
1392*0e209d39SAndroid Build Coastguard Worker } else {
1393*0e209d39SAndroid Build Coastguard Worker // 1 <= level < currentParaLevel or UBIDI_MAX_EXPLICIT_LEVEL < level
1394*0e209d39SAndroid Build Coastguard Worker /* level out of bounds */
1395*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1396*0e209d39SAndroid Build Coastguard Worker return UBIDI_LTR;
1397*0e209d39SAndroid Build Coastguard Worker }
1398*0e209d39SAndroid Build Coastguard Worker }
1399*0e209d39SAndroid Build Coastguard Worker if (overrideFlag != 0) {
1400*0e209d39SAndroid Build Coastguard Worker /* keep the override flag in levels[i] but adjust the flags */
1401*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG_O(level);
1402*0e209d39SAndroid Build Coastguard Worker } else {
1403*0e209d39SAndroid Build Coastguard Worker /* set the flags */
1404*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG_E(level)|DIRPROP_FLAG(dirProp);
1405*0e209d39SAndroid Build Coastguard Worker }
1406*0e209d39SAndroid Build Coastguard Worker }
1407*0e209d39SAndroid Build Coastguard Worker if(flags&MASK_EMBEDDING)
1408*0e209d39SAndroid Build Coastguard Worker flags|=DIRPROP_FLAG_LR(pBiDi->paraLevel);
1409*0e209d39SAndroid Build Coastguard Worker /* determine if the text is mixed-directional or single-directional */
1410*0e209d39SAndroid Build Coastguard Worker pBiDi->flags=flags;
1411*0e209d39SAndroid Build Coastguard Worker return directionFromFlags(pBiDi);
1412*0e209d39SAndroid Build Coastguard Worker }
1413*0e209d39SAndroid Build Coastguard Worker
1414*0e209d39SAndroid Build Coastguard Worker /******************************************************************
1415*0e209d39SAndroid Build Coastguard Worker The Properties state machine table
1416*0e209d39SAndroid Build Coastguard Worker *******************************************************************
1417*0e209d39SAndroid Build Coastguard Worker
1418*0e209d39SAndroid Build Coastguard Worker All table cells are 8 bits:
1419*0e209d39SAndroid Build Coastguard Worker bits 0..4: next state
1420*0e209d39SAndroid Build Coastguard Worker bits 5..7: action to perform (if > 0)
1421*0e209d39SAndroid Build Coastguard Worker
1422*0e209d39SAndroid Build Coastguard Worker Cells may be of format "n" where n represents the next state
1423*0e209d39SAndroid Build Coastguard Worker (except for the rightmost column).
1424*0e209d39SAndroid Build Coastguard Worker Cells may also be of format "s(x,y)" where x represents an action
1425*0e209d39SAndroid Build Coastguard Worker to perform and y represents the next state.
1426*0e209d39SAndroid Build Coastguard Worker
1427*0e209d39SAndroid Build Coastguard Worker *******************************************************************
1428*0e209d39SAndroid Build Coastguard Worker Definitions and type for properties state table
1429*0e209d39SAndroid Build Coastguard Worker *******************************************************************
1430*0e209d39SAndroid Build Coastguard Worker */
1431*0e209d39SAndroid Build Coastguard Worker #define IMPTABPROPS_COLUMNS 16
1432*0e209d39SAndroid Build Coastguard Worker #define IMPTABPROPS_RES (IMPTABPROPS_COLUMNS - 1)
1433*0e209d39SAndroid Build Coastguard Worker #define GET_STATEPROPS(cell) ((cell)&0x1f)
1434*0e209d39SAndroid Build Coastguard Worker #define GET_ACTIONPROPS(cell) ((cell)>>5)
1435*0e209d39SAndroid Build Coastguard Worker #define s(action, newState) ((uint8_t)(newState+(action<<5)))
1436*0e209d39SAndroid Build Coastguard Worker
1437*0e209d39SAndroid Build Coastguard Worker static const uint8_t groupProp[] = /* dirProp regrouped */
1438*0e209d39SAndroid Build Coastguard Worker {
1439*0e209d39SAndroid Build Coastguard Worker /* L R EN ES ET AN CS B S WS ON LRE LRO AL RLE RLO PDF NSM BN FSI LRI RLI PDI ENL ENR */
1440*0e209d39SAndroid Build Coastguard Worker 0, 1, 2, 7, 8, 3, 9, 6, 5, 4, 4, 10, 10, 12, 10, 10, 10, 11, 10, 4, 4, 4, 4, 13, 14
1441*0e209d39SAndroid Build Coastguard Worker };
1442*0e209d39SAndroid Build Coastguard Worker enum { DirProp_L=0, DirProp_R=1, DirProp_EN=2, DirProp_AN=3, DirProp_ON=4, DirProp_S=5, DirProp_B=6 }; /* reduced dirProp */
1443*0e209d39SAndroid Build Coastguard Worker
1444*0e209d39SAndroid Build Coastguard Worker /******************************************************************
1445*0e209d39SAndroid Build Coastguard Worker
1446*0e209d39SAndroid Build Coastguard Worker PROPERTIES STATE TABLE
1447*0e209d39SAndroid Build Coastguard Worker
1448*0e209d39SAndroid Build Coastguard Worker In table impTabProps,
1449*0e209d39SAndroid Build Coastguard Worker - the ON column regroups ON and WS, FSI, RLI, LRI and PDI
1450*0e209d39SAndroid Build Coastguard Worker - the BN column regroups BN, LRE, RLE, LRO, RLO, PDF
1451*0e209d39SAndroid Build Coastguard Worker - the Res column is the reduced property assigned to a run
1452*0e209d39SAndroid Build Coastguard Worker
1453*0e209d39SAndroid Build Coastguard Worker Action 1: process current run1, init new run1
1454*0e209d39SAndroid Build Coastguard Worker 2: init new run2
1455*0e209d39SAndroid Build Coastguard Worker 3: process run1, process run2, init new run1
1456*0e209d39SAndroid Build Coastguard Worker 4: process run1, set run1=run2, init new run2
1457*0e209d39SAndroid Build Coastguard Worker
1458*0e209d39SAndroid Build Coastguard Worker Notes:
1459*0e209d39SAndroid Build Coastguard Worker 1) This table is used in resolveImplicitLevels().
1460*0e209d39SAndroid Build Coastguard Worker 2) This table triggers actions when there is a change in the Bidi
1461*0e209d39SAndroid Build Coastguard Worker property of incoming characters (action 1).
1462*0e209d39SAndroid Build Coastguard Worker 3) Most such property sequences are processed immediately (in
1463*0e209d39SAndroid Build Coastguard Worker fact, passed to processPropertySeq().
1464*0e209d39SAndroid Build Coastguard Worker 4) However, numbers are assembled as one sequence. This means
1465*0e209d39SAndroid Build Coastguard Worker that undefined situations (like CS following digits, until
1466*0e209d39SAndroid Build Coastguard Worker it is known if the next char will be a digit) are held until
1467*0e209d39SAndroid Build Coastguard Worker following chars define them.
1468*0e209d39SAndroid Build Coastguard Worker Example: digits followed by CS, then comes another CS or ON;
1469*0e209d39SAndroid Build Coastguard Worker the digits will be processed, then the CS assigned
1470*0e209d39SAndroid Build Coastguard Worker as the start of an ON sequence (action 3).
1471*0e209d39SAndroid Build Coastguard Worker 5) There are cases where more than one sequence must be
1472*0e209d39SAndroid Build Coastguard Worker processed, for instance digits followed by CS followed by L:
1473*0e209d39SAndroid Build Coastguard Worker the digits must be processed as one sequence, and the CS
1474*0e209d39SAndroid Build Coastguard Worker must be processed as an ON sequence, all this before starting
1475*0e209d39SAndroid Build Coastguard Worker assembling chars for the opening L sequence.
1476*0e209d39SAndroid Build Coastguard Worker
1477*0e209d39SAndroid Build Coastguard Worker
1478*0e209d39SAndroid Build Coastguard Worker */
1479*0e209d39SAndroid Build Coastguard Worker static const uint8_t impTabProps[][IMPTABPROPS_COLUMNS] =
1480*0e209d39SAndroid Build Coastguard Worker {
1481*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , ES , ET , CS , BN , NSM , AL , ENL , ENR , Res */
1482*0e209d39SAndroid Build Coastguard Worker /* 0 Init */ { 1 , 2 , 4 , 5 , 7 , 15 , 17 , 7 , 9 , 7 , 0 , 7 , 3 , 18 , 21 , DirProp_ON },
1483*0e209d39SAndroid Build Coastguard Worker /* 1 L */ { 1 , s(1,2), s(1,4), s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), s(1,9), s(1,7), 1 , 1 , s(1,3),s(1,18),s(1,21), DirProp_L },
1484*0e209d39SAndroid Build Coastguard Worker /* 2 R */ { s(1,1), 2 , s(1,4), s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), s(1,9), s(1,7), 2 , 2 , s(1,3),s(1,18),s(1,21), DirProp_R },
1485*0e209d39SAndroid Build Coastguard Worker /* 3 AL */ { s(1,1), s(1,2), s(1,6), s(1,6), s(1,8),s(1,16),s(1,17), s(1,8), s(1,8), s(1,8), 3 , 3 , 3 ,s(1,18),s(1,21), DirProp_R },
1486*0e209d39SAndroid Build Coastguard Worker /* 4 EN */ { s(1,1), s(1,2), 4 , s(1,5), s(1,7),s(1,15),s(1,17),s(2,10), 11 ,s(2,10), 4 , 4 , s(1,3), 18 , 21 , DirProp_EN },
1487*0e209d39SAndroid Build Coastguard Worker /* 5 AN */ { s(1,1), s(1,2), s(1,4), 5 , s(1,7),s(1,15),s(1,17), s(1,7), s(1,9),s(2,12), 5 , 5 , s(1,3),s(1,18),s(1,21), DirProp_AN },
1488*0e209d39SAndroid Build Coastguard Worker /* 6 AL:EN/AN */ { s(1,1), s(1,2), 6 , 6 , s(1,8),s(1,16),s(1,17), s(1,8), s(1,8),s(2,13), 6 , 6 , s(1,3), 18 , 21 , DirProp_AN },
1489*0e209d39SAndroid Build Coastguard Worker /* 7 ON */ { s(1,1), s(1,2), s(1,4), s(1,5), 7 ,s(1,15),s(1,17), 7 ,s(2,14), 7 , 7 , 7 , s(1,3),s(1,18),s(1,21), DirProp_ON },
1490*0e209d39SAndroid Build Coastguard Worker /* 8 AL:ON */ { s(1,1), s(1,2), s(1,6), s(1,6), 8 ,s(1,16),s(1,17), 8 , 8 , 8 , 8 , 8 , s(1,3),s(1,18),s(1,21), DirProp_ON },
1491*0e209d39SAndroid Build Coastguard Worker /* 9 ET */ { s(1,1), s(1,2), 4 , s(1,5), 7 ,s(1,15),s(1,17), 7 , 9 , 7 , 9 , 9 , s(1,3), 18 , 21 , DirProp_ON },
1492*0e209d39SAndroid Build Coastguard Worker /*10 EN+ES/CS */ { s(3,1), s(3,2), 4 , s(3,5), s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 10 , s(4,7), s(3,3), 18 , 21 , DirProp_EN },
1493*0e209d39SAndroid Build Coastguard Worker /*11 EN+ET */ { s(1,1), s(1,2), 4 , s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), 11 , s(1,7), 11 , 11 , s(1,3), 18 , 21 , DirProp_EN },
1494*0e209d39SAndroid Build Coastguard Worker /*12 AN+CS */ { s(3,1), s(3,2), s(3,4), 5 , s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 12 , s(4,7), s(3,3),s(3,18),s(3,21), DirProp_AN },
1495*0e209d39SAndroid Build Coastguard Worker /*13 AL:EN/AN+CS */ { s(3,1), s(3,2), 6 , 6 , s(4,8),s(3,16),s(3,17), s(4,8), s(4,8), s(4,8), 13 , s(4,8), s(3,3), 18 , 21 , DirProp_AN },
1496*0e209d39SAndroid Build Coastguard Worker /*14 ON+ET */ { s(1,1), s(1,2), s(4,4), s(1,5), 7 ,s(1,15),s(1,17), 7 , 14 , 7 , 14 , 14 , s(1,3),s(4,18),s(4,21), DirProp_ON },
1497*0e209d39SAndroid Build Coastguard Worker /*15 S */ { s(1,1), s(1,2), s(1,4), s(1,5), s(1,7), 15 ,s(1,17), s(1,7), s(1,9), s(1,7), 15 , s(1,7), s(1,3),s(1,18),s(1,21), DirProp_S },
1498*0e209d39SAndroid Build Coastguard Worker /*16 AL:S */ { s(1,1), s(1,2), s(1,6), s(1,6), s(1,8), 16 ,s(1,17), s(1,8), s(1,8), s(1,8), 16 , s(1,8), s(1,3),s(1,18),s(1,21), DirProp_S },
1499*0e209d39SAndroid Build Coastguard Worker /*17 B */ { s(1,1), s(1,2), s(1,4), s(1,5), s(1,7),s(1,15), 17 , s(1,7), s(1,9), s(1,7), 17 , s(1,7), s(1,3),s(1,18),s(1,21), DirProp_B },
1500*0e209d39SAndroid Build Coastguard Worker /*18 ENL */ { s(1,1), s(1,2), 18 , s(1,5), s(1,7),s(1,15),s(1,17),s(2,19), 20 ,s(2,19), 18 , 18 , s(1,3), 18 , 21 , DirProp_L },
1501*0e209d39SAndroid Build Coastguard Worker /*19 ENL+ES/CS */ { s(3,1), s(3,2), 18 , s(3,5), s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 19 , s(4,7), s(3,3), 18 , 21 , DirProp_L },
1502*0e209d39SAndroid Build Coastguard Worker /*20 ENL+ET */ { s(1,1), s(1,2), 18 , s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), 20 , s(1,7), 20 , 20 , s(1,3), 18 , 21 , DirProp_L },
1503*0e209d39SAndroid Build Coastguard Worker /*21 ENR */ { s(1,1), s(1,2), 21 , s(1,5), s(1,7),s(1,15),s(1,17),s(2,22), 23 ,s(2,22), 21 , 21 , s(1,3), 18 , 21 , DirProp_AN },
1504*0e209d39SAndroid Build Coastguard Worker /*22 ENR+ES/CS */ { s(3,1), s(3,2), 21 , s(3,5), s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 22 , s(4,7), s(3,3), 18 , 21 , DirProp_AN },
1505*0e209d39SAndroid Build Coastguard Worker /*23 ENR+ET */ { s(1,1), s(1,2), 21 , s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), 23 , s(1,7), 23 , 23 , s(1,3), 18 , 21 , DirProp_AN }
1506*0e209d39SAndroid Build Coastguard Worker };
1507*0e209d39SAndroid Build Coastguard Worker
1508*0e209d39SAndroid Build Coastguard Worker /* we must undef macro s because the levels tables have a different
1509*0e209d39SAndroid Build Coastguard Worker * structure (4 bits for action and 4 bits for next state.
1510*0e209d39SAndroid Build Coastguard Worker */
1511*0e209d39SAndroid Build Coastguard Worker #undef s
1512*0e209d39SAndroid Build Coastguard Worker
1513*0e209d39SAndroid Build Coastguard Worker /******************************************************************
1514*0e209d39SAndroid Build Coastguard Worker The levels state machine tables
1515*0e209d39SAndroid Build Coastguard Worker *******************************************************************
1516*0e209d39SAndroid Build Coastguard Worker
1517*0e209d39SAndroid Build Coastguard Worker All table cells are 8 bits:
1518*0e209d39SAndroid Build Coastguard Worker bits 0..3: next state
1519*0e209d39SAndroid Build Coastguard Worker bits 4..7: action to perform (if > 0)
1520*0e209d39SAndroid Build Coastguard Worker
1521*0e209d39SAndroid Build Coastguard Worker Cells may be of format "n" where n represents the next state
1522*0e209d39SAndroid Build Coastguard Worker (except for the rightmost column).
1523*0e209d39SAndroid Build Coastguard Worker Cells may also be of format "s(x,y)" where x represents an action
1524*0e209d39SAndroid Build Coastguard Worker to perform and y represents the next state.
1525*0e209d39SAndroid Build Coastguard Worker
1526*0e209d39SAndroid Build Coastguard Worker This format limits each table to 16 states each and to 15 actions.
1527*0e209d39SAndroid Build Coastguard Worker
1528*0e209d39SAndroid Build Coastguard Worker *******************************************************************
1529*0e209d39SAndroid Build Coastguard Worker Definitions and type for levels state tables
1530*0e209d39SAndroid Build Coastguard Worker *******************************************************************
1531*0e209d39SAndroid Build Coastguard Worker */
1532*0e209d39SAndroid Build Coastguard Worker #define IMPTABLEVELS_COLUMNS (DirProp_B + 2)
1533*0e209d39SAndroid Build Coastguard Worker #define IMPTABLEVELS_RES (IMPTABLEVELS_COLUMNS - 1)
1534*0e209d39SAndroid Build Coastguard Worker #define GET_STATE(cell) ((cell)&0x0f)
1535*0e209d39SAndroid Build Coastguard Worker #define GET_ACTION(cell) ((cell)>>4)
1536*0e209d39SAndroid Build Coastguard Worker #define s(action, newState) ((uint8_t)(newState+(action<<4)))
1537*0e209d39SAndroid Build Coastguard Worker
1538*0e209d39SAndroid Build Coastguard Worker typedef uint8_t ImpTab[][IMPTABLEVELS_COLUMNS];
1539*0e209d39SAndroid Build Coastguard Worker typedef uint8_t ImpAct[];
1540*0e209d39SAndroid Build Coastguard Worker
1541*0e209d39SAndroid Build Coastguard Worker /* FOOD FOR THOUGHT: each ImpTab should have its associated ImpAct,
1542*0e209d39SAndroid Build Coastguard Worker * instead of having a pair of ImpTab and a pair of ImpAct.
1543*0e209d39SAndroid Build Coastguard Worker */
1544*0e209d39SAndroid Build Coastguard Worker typedef struct ImpTabPair {
1545*0e209d39SAndroid Build Coastguard Worker const void * pImpTab[2];
1546*0e209d39SAndroid Build Coastguard Worker const void * pImpAct[2];
1547*0e209d39SAndroid Build Coastguard Worker } ImpTabPair;
1548*0e209d39SAndroid Build Coastguard Worker
1549*0e209d39SAndroid Build Coastguard Worker /******************************************************************
1550*0e209d39SAndroid Build Coastguard Worker
1551*0e209d39SAndroid Build Coastguard Worker LEVELS STATE TABLES
1552*0e209d39SAndroid Build Coastguard Worker
1553*0e209d39SAndroid Build Coastguard Worker In all levels state tables,
1554*0e209d39SAndroid Build Coastguard Worker - state 0 is the initial state
1555*0e209d39SAndroid Build Coastguard Worker - the Res column is the increment to add to the text level
1556*0e209d39SAndroid Build Coastguard Worker for this property sequence.
1557*0e209d39SAndroid Build Coastguard Worker
1558*0e209d39SAndroid Build Coastguard Worker The impAct arrays for each table of a pair map the local action
1559*0e209d39SAndroid Build Coastguard Worker numbers of the table to the total list of actions. For instance,
1560*0e209d39SAndroid Build Coastguard Worker action 2 in a given table corresponds to the action number which
1561*0e209d39SAndroid Build Coastguard Worker appears in entry [2] of the impAct array for that table.
1562*0e209d39SAndroid Build Coastguard Worker The first entry of all impAct arrays must be 0.
1563*0e209d39SAndroid Build Coastguard Worker
1564*0e209d39SAndroid Build Coastguard Worker Action 1: init conditional sequence
1565*0e209d39SAndroid Build Coastguard Worker 2: prepend conditional sequence to current sequence
1566*0e209d39SAndroid Build Coastguard Worker 3: set ON sequence to new level - 1
1567*0e209d39SAndroid Build Coastguard Worker 4: init EN/AN/ON sequence
1568*0e209d39SAndroid Build Coastguard Worker 5: fix EN/AN/ON sequence followed by R
1569*0e209d39SAndroid Build Coastguard Worker 6: set previous level sequence to level 2
1570*0e209d39SAndroid Build Coastguard Worker
1571*0e209d39SAndroid Build Coastguard Worker Notes:
1572*0e209d39SAndroid Build Coastguard Worker 1) These tables are used in processPropertySeq(). The input
1573*0e209d39SAndroid Build Coastguard Worker is property sequences as determined by resolveImplicitLevels.
1574*0e209d39SAndroid Build Coastguard Worker 2) Most such property sequences are processed immediately
1575*0e209d39SAndroid Build Coastguard Worker (levels are assigned).
1576*0e209d39SAndroid Build Coastguard Worker 3) However, some sequences cannot be assigned a final level till
1577*0e209d39SAndroid Build Coastguard Worker one or more following sequences are received. For instance,
1578*0e209d39SAndroid Build Coastguard Worker ON following an R sequence within an even-level paragraph.
1579*0e209d39SAndroid Build Coastguard Worker If the following sequence is R, the ON sequence will be
1580*0e209d39SAndroid Build Coastguard Worker assigned basic run level+1, and so will the R sequence.
1581*0e209d39SAndroid Build Coastguard Worker 4) S is generally handled like ON, since its level will be fixed
1582*0e209d39SAndroid Build Coastguard Worker to paragraph level in adjustWSLevels().
1583*0e209d39SAndroid Build Coastguard Worker
1584*0e209d39SAndroid Build Coastguard Worker */
1585*0e209d39SAndroid Build Coastguard Worker
1586*0e209d39SAndroid Build Coastguard Worker static const ImpTab impTabL_DEFAULT = /* Even paragraph level */
1587*0e209d39SAndroid Build Coastguard Worker /* In this table, conditional sequences receive the lower possible level
1588*0e209d39SAndroid Build Coastguard Worker until proven otherwise.
1589*0e209d39SAndroid Build Coastguard Worker */
1590*0e209d39SAndroid Build Coastguard Worker {
1591*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , Res */
1592*0e209d39SAndroid Build Coastguard Worker /* 0 : init */ { 0 , 1 , 0 , 2 , 0 , 0 , 0 , 0 },
1593*0e209d39SAndroid Build Coastguard Worker /* 1 : R */ { 0 , 1 , 3 , 3 , s(1,4), s(1,4), 0 , 1 },
1594*0e209d39SAndroid Build Coastguard Worker /* 2 : AN */ { 0 , 1 , 0 , 2 , s(1,5), s(1,5), 0 , 2 },
1595*0e209d39SAndroid Build Coastguard Worker /* 3 : R+EN/AN */ { 0 , 1 , 3 , 3 , s(1,4), s(1,4), 0 , 2 },
1596*0e209d39SAndroid Build Coastguard Worker /* 4 : R+ON */ { 0 , s(2,1), s(3,3), s(3,3), 4 , 4 , 0 , 0 },
1597*0e209d39SAndroid Build Coastguard Worker /* 5 : AN+ON */ { 0 , s(2,1), 0 , s(3,2), 5 , 5 , 0 , 0 }
1598*0e209d39SAndroid Build Coastguard Worker };
1599*0e209d39SAndroid Build Coastguard Worker static const ImpTab impTabR_DEFAULT = /* Odd paragraph level */
1600*0e209d39SAndroid Build Coastguard Worker /* In this table, conditional sequences receive the lower possible level
1601*0e209d39SAndroid Build Coastguard Worker until proven otherwise.
1602*0e209d39SAndroid Build Coastguard Worker */
1603*0e209d39SAndroid Build Coastguard Worker {
1604*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , Res */
1605*0e209d39SAndroid Build Coastguard Worker /* 0 : init */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 0 },
1606*0e209d39SAndroid Build Coastguard Worker /* 1 : L */ { 1 , 0 , 1 , 3 , s(1,4), s(1,4), 0 , 1 },
1607*0e209d39SAndroid Build Coastguard Worker /* 2 : EN/AN */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 1 },
1608*0e209d39SAndroid Build Coastguard Worker /* 3 : L+AN */ { 1 , 0 , 1 , 3 , 5 , 5 , 0 , 1 },
1609*0e209d39SAndroid Build Coastguard Worker /* 4 : L+ON */ { s(2,1), 0 , s(2,1), 3 , 4 , 4 , 0 , 0 },
1610*0e209d39SAndroid Build Coastguard Worker /* 5 : L+AN+ON */ { 1 , 0 , 1 , 3 , 5 , 5 , 0 , 0 }
1611*0e209d39SAndroid Build Coastguard Worker };
1612*0e209d39SAndroid Build Coastguard Worker static const ImpAct impAct0 = {0,1,2,3,4};
1613*0e209d39SAndroid Build Coastguard Worker static const ImpTabPair impTab_DEFAULT = {{&impTabL_DEFAULT,
1614*0e209d39SAndroid Build Coastguard Worker &impTabR_DEFAULT},
1615*0e209d39SAndroid Build Coastguard Worker {&impAct0, &impAct0}};
1616*0e209d39SAndroid Build Coastguard Worker
1617*0e209d39SAndroid Build Coastguard Worker static const ImpTab impTabL_NUMBERS_SPECIAL = /* Even paragraph level */
1618*0e209d39SAndroid Build Coastguard Worker /* In this table, conditional sequences receive the lower possible level
1619*0e209d39SAndroid Build Coastguard Worker until proven otherwise.
1620*0e209d39SAndroid Build Coastguard Worker */
1621*0e209d39SAndroid Build Coastguard Worker {
1622*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , Res */
1623*0e209d39SAndroid Build Coastguard Worker /* 0 : init */ { 0 , 2 , s(1,1), s(1,1), 0 , 0 , 0 , 0 },
1624*0e209d39SAndroid Build Coastguard Worker /* 1 : L+EN/AN */ { 0 , s(4,2), 1 , 1 , 0 , 0 , 0 , 0 },
1625*0e209d39SAndroid Build Coastguard Worker /* 2 : R */ { 0 , 2 , 4 , 4 , s(1,3), s(1,3), 0 , 1 },
1626*0e209d39SAndroid Build Coastguard Worker /* 3 : R+ON */ { 0 , s(2,2), s(3,4), s(3,4), 3 , 3 , 0 , 0 },
1627*0e209d39SAndroid Build Coastguard Worker /* 4 : R+EN/AN */ { 0 , 2 , 4 , 4 , s(1,3), s(1,3), 0 , 2 }
1628*0e209d39SAndroid Build Coastguard Worker };
1629*0e209d39SAndroid Build Coastguard Worker static const ImpTabPair impTab_NUMBERS_SPECIAL = {{&impTabL_NUMBERS_SPECIAL,
1630*0e209d39SAndroid Build Coastguard Worker &impTabR_DEFAULT},
1631*0e209d39SAndroid Build Coastguard Worker {&impAct0, &impAct0}};
1632*0e209d39SAndroid Build Coastguard Worker
1633*0e209d39SAndroid Build Coastguard Worker static const ImpTab impTabL_GROUP_NUMBERS_WITH_R =
1634*0e209d39SAndroid Build Coastguard Worker /* In this table, EN/AN+ON sequences receive levels as if associated with R
1635*0e209d39SAndroid Build Coastguard Worker until proven that there is L or sor/eor on both sides. AN is handled like EN.
1636*0e209d39SAndroid Build Coastguard Worker */
1637*0e209d39SAndroid Build Coastguard Worker {
1638*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , Res */
1639*0e209d39SAndroid Build Coastguard Worker /* 0 init */ { 0 , 3 , s(1,1), s(1,1), 0 , 0 , 0 , 0 },
1640*0e209d39SAndroid Build Coastguard Worker /* 1 EN/AN */ { s(2,0), 3 , 1 , 1 , 2 , s(2,0), s(2,0), 2 },
1641*0e209d39SAndroid Build Coastguard Worker /* 2 EN/AN+ON */ { s(2,0), 3 , 1 , 1 , 2 , s(2,0), s(2,0), 1 },
1642*0e209d39SAndroid Build Coastguard Worker /* 3 R */ { 0 , 3 , 5 , 5 , s(1,4), 0 , 0 , 1 },
1643*0e209d39SAndroid Build Coastguard Worker /* 4 R+ON */ { s(2,0), 3 , 5 , 5 , 4 , s(2,0), s(2,0), 1 },
1644*0e209d39SAndroid Build Coastguard Worker /* 5 R+EN/AN */ { 0 , 3 , 5 , 5 , s(1,4), 0 , 0 , 2 }
1645*0e209d39SAndroid Build Coastguard Worker };
1646*0e209d39SAndroid Build Coastguard Worker static const ImpTab impTabR_GROUP_NUMBERS_WITH_R =
1647*0e209d39SAndroid Build Coastguard Worker /* In this table, EN/AN+ON sequences receive levels as if associated with R
1648*0e209d39SAndroid Build Coastguard Worker until proven that there is L on both sides. AN is handled like EN.
1649*0e209d39SAndroid Build Coastguard Worker */
1650*0e209d39SAndroid Build Coastguard Worker {
1651*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , Res */
1652*0e209d39SAndroid Build Coastguard Worker /* 0 init */ { 2 , 0 , 1 , 1 , 0 , 0 , 0 , 0 },
1653*0e209d39SAndroid Build Coastguard Worker /* 1 EN/AN */ { 2 , 0 , 1 , 1 , 0 , 0 , 0 , 1 },
1654*0e209d39SAndroid Build Coastguard Worker /* 2 L */ { 2 , 0 , s(1,4), s(1,4), s(1,3), 0 , 0 , 1 },
1655*0e209d39SAndroid Build Coastguard Worker /* 3 L+ON */ { s(2,2), 0 , 4 , 4 , 3 , 0 , 0 , 0 },
1656*0e209d39SAndroid Build Coastguard Worker /* 4 L+EN/AN */ { s(2,2), 0 , 4 , 4 , 3 , 0 , 0 , 1 }
1657*0e209d39SAndroid Build Coastguard Worker };
1658*0e209d39SAndroid Build Coastguard Worker static const ImpTabPair impTab_GROUP_NUMBERS_WITH_R = {
1659*0e209d39SAndroid Build Coastguard Worker {&impTabL_GROUP_NUMBERS_WITH_R,
1660*0e209d39SAndroid Build Coastguard Worker &impTabR_GROUP_NUMBERS_WITH_R},
1661*0e209d39SAndroid Build Coastguard Worker {&impAct0, &impAct0}};
1662*0e209d39SAndroid Build Coastguard Worker
1663*0e209d39SAndroid Build Coastguard Worker
1664*0e209d39SAndroid Build Coastguard Worker static const ImpTab impTabL_INVERSE_NUMBERS_AS_L =
1665*0e209d39SAndroid Build Coastguard Worker /* This table is identical to the Default LTR table except that EN and AN are
1666*0e209d39SAndroid Build Coastguard Worker handled like L.
1667*0e209d39SAndroid Build Coastguard Worker */
1668*0e209d39SAndroid Build Coastguard Worker {
1669*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , Res */
1670*0e209d39SAndroid Build Coastguard Worker /* 0 : init */ { 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 },
1671*0e209d39SAndroid Build Coastguard Worker /* 1 : R */ { 0 , 1 , 0 , 0 , s(1,4), s(1,4), 0 , 1 },
1672*0e209d39SAndroid Build Coastguard Worker /* 2 : AN */ { 0 , 1 , 0 , 0 , s(1,5), s(1,5), 0 , 2 },
1673*0e209d39SAndroid Build Coastguard Worker /* 3 : R+EN/AN */ { 0 , 1 , 0 , 0 , s(1,4), s(1,4), 0 , 2 },
1674*0e209d39SAndroid Build Coastguard Worker /* 4 : R+ON */ { s(2,0), 1 , s(2,0), s(2,0), 4 , 4 , s(2,0), 1 },
1675*0e209d39SAndroid Build Coastguard Worker /* 5 : AN+ON */ { s(2,0), 1 , s(2,0), s(2,0), 5 , 5 , s(2,0), 1 }
1676*0e209d39SAndroid Build Coastguard Worker };
1677*0e209d39SAndroid Build Coastguard Worker static const ImpTab impTabR_INVERSE_NUMBERS_AS_L =
1678*0e209d39SAndroid Build Coastguard Worker /* This table is identical to the Default RTL table except that EN and AN are
1679*0e209d39SAndroid Build Coastguard Worker handled like L.
1680*0e209d39SAndroid Build Coastguard Worker */
1681*0e209d39SAndroid Build Coastguard Worker {
1682*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , Res */
1683*0e209d39SAndroid Build Coastguard Worker /* 0 : init */ { 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 },
1684*0e209d39SAndroid Build Coastguard Worker /* 1 : L */ { 1 , 0 , 1 , 1 , s(1,4), s(1,4), 0 , 1 },
1685*0e209d39SAndroid Build Coastguard Worker /* 2 : EN/AN */ { 1 , 0 , 1 , 1 , 0 , 0 , 0 , 1 },
1686*0e209d39SAndroid Build Coastguard Worker /* 3 : L+AN */ { 1 , 0 , 1 , 1 , 5 , 5 , 0 , 1 },
1687*0e209d39SAndroid Build Coastguard Worker /* 4 : L+ON */ { s(2,1), 0 , s(2,1), s(2,1), 4 , 4 , 0 , 0 },
1688*0e209d39SAndroid Build Coastguard Worker /* 5 : L+AN+ON */ { 1 , 0 , 1 , 1 , 5 , 5 , 0 , 0 }
1689*0e209d39SAndroid Build Coastguard Worker };
1690*0e209d39SAndroid Build Coastguard Worker static const ImpTabPair impTab_INVERSE_NUMBERS_AS_L = {
1691*0e209d39SAndroid Build Coastguard Worker {&impTabL_INVERSE_NUMBERS_AS_L,
1692*0e209d39SAndroid Build Coastguard Worker &impTabR_INVERSE_NUMBERS_AS_L},
1693*0e209d39SAndroid Build Coastguard Worker {&impAct0, &impAct0}};
1694*0e209d39SAndroid Build Coastguard Worker
1695*0e209d39SAndroid Build Coastguard Worker static const ImpTab impTabR_INVERSE_LIKE_DIRECT = /* Odd paragraph level */
1696*0e209d39SAndroid Build Coastguard Worker /* In this table, conditional sequences receive the lower possible level
1697*0e209d39SAndroid Build Coastguard Worker until proven otherwise.
1698*0e209d39SAndroid Build Coastguard Worker */
1699*0e209d39SAndroid Build Coastguard Worker {
1700*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , Res */
1701*0e209d39SAndroid Build Coastguard Worker /* 0 : init */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 0 },
1702*0e209d39SAndroid Build Coastguard Worker /* 1 : L */ { 1 , 0 , 1 , 2 , s(1,3), s(1,3), 0 , 1 },
1703*0e209d39SAndroid Build Coastguard Worker /* 2 : EN/AN */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 1 },
1704*0e209d39SAndroid Build Coastguard Worker /* 3 : L+ON */ { s(2,1), s(3,0), 6 , 4 , 3 , 3 , s(3,0), 0 },
1705*0e209d39SAndroid Build Coastguard Worker /* 4 : L+ON+AN */ { s(2,1), s(3,0), 6 , 4 , 5 , 5 , s(3,0), 3 },
1706*0e209d39SAndroid Build Coastguard Worker /* 5 : L+AN+ON */ { s(2,1), s(3,0), 6 , 4 , 5 , 5 , s(3,0), 2 },
1707*0e209d39SAndroid Build Coastguard Worker /* 6 : L+ON+EN */ { s(2,1), s(3,0), 6 , 4 , 3 , 3 , s(3,0), 1 }
1708*0e209d39SAndroid Build Coastguard Worker };
1709*0e209d39SAndroid Build Coastguard Worker static const ImpAct impAct1 = {0,1,13,14};
1710*0e209d39SAndroid Build Coastguard Worker /* FOOD FOR THOUGHT: in LTR table below, check case "JKL 123abc"
1711*0e209d39SAndroid Build Coastguard Worker */
1712*0e209d39SAndroid Build Coastguard Worker static const ImpTabPair impTab_INVERSE_LIKE_DIRECT = {
1713*0e209d39SAndroid Build Coastguard Worker {&impTabL_DEFAULT,
1714*0e209d39SAndroid Build Coastguard Worker &impTabR_INVERSE_LIKE_DIRECT},
1715*0e209d39SAndroid Build Coastguard Worker {&impAct0, &impAct1}};
1716*0e209d39SAndroid Build Coastguard Worker
1717*0e209d39SAndroid Build Coastguard Worker static const ImpTab impTabL_INVERSE_LIKE_DIRECT_WITH_MARKS =
1718*0e209d39SAndroid Build Coastguard Worker /* The case handled in this table is (visually): R EN L
1719*0e209d39SAndroid Build Coastguard Worker */
1720*0e209d39SAndroid Build Coastguard Worker {
1721*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , Res */
1722*0e209d39SAndroid Build Coastguard Worker /* 0 : init */ { 0 , s(6,3), 0 , 1 , 0 , 0 , 0 , 0 },
1723*0e209d39SAndroid Build Coastguard Worker /* 1 : L+AN */ { 0 , s(6,3), 0 , 1 , s(1,2), s(3,0), 0 , 4 },
1724*0e209d39SAndroid Build Coastguard Worker /* 2 : L+AN+ON */ { s(2,0), s(6,3), s(2,0), 1 , 2 , s(3,0), s(2,0), 3 },
1725*0e209d39SAndroid Build Coastguard Worker /* 3 : R */ { 0 , s(6,3), s(5,5), s(5,6), s(1,4), s(3,0), 0 , 3 },
1726*0e209d39SAndroid Build Coastguard Worker /* 4 : R+ON */ { s(3,0), s(4,3), s(5,5), s(5,6), 4 , s(3,0), s(3,0), 3 },
1727*0e209d39SAndroid Build Coastguard Worker /* 5 : R+EN */ { s(3,0), s(4,3), 5 , s(5,6), s(1,4), s(3,0), s(3,0), 4 },
1728*0e209d39SAndroid Build Coastguard Worker /* 6 : R+AN */ { s(3,0), s(4,3), s(5,5), 6 , s(1,4), s(3,0), s(3,0), 4 }
1729*0e209d39SAndroid Build Coastguard Worker };
1730*0e209d39SAndroid Build Coastguard Worker static const ImpTab impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS =
1731*0e209d39SAndroid Build Coastguard Worker /* The cases handled in this table are (visually): R EN L
1732*0e209d39SAndroid Build Coastguard Worker R L AN L
1733*0e209d39SAndroid Build Coastguard Worker */
1734*0e209d39SAndroid Build Coastguard Worker {
1735*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , Res */
1736*0e209d39SAndroid Build Coastguard Worker /* 0 : init */ { s(1,3), 0 , 1 , 1 , 0 , 0 , 0 , 0 },
1737*0e209d39SAndroid Build Coastguard Worker /* 1 : R+EN/AN */ { s(2,3), 0 , 1 , 1 , 2 , s(4,0), 0 , 1 },
1738*0e209d39SAndroid Build Coastguard Worker /* 2 : R+EN/AN+ON */ { s(2,3), 0 , 1 , 1 , 2 , s(4,0), 0 , 0 },
1739*0e209d39SAndroid Build Coastguard Worker /* 3 : L */ { 3 , 0 , 3 , s(3,6), s(1,4), s(4,0), 0 , 1 },
1740*0e209d39SAndroid Build Coastguard Worker /* 4 : L+ON */ { s(5,3), s(4,0), 5 , s(3,6), 4 , s(4,0), s(4,0), 0 },
1741*0e209d39SAndroid Build Coastguard Worker /* 5 : L+ON+EN */ { s(5,3), s(4,0), 5 , s(3,6), 4 , s(4,0), s(4,0), 1 },
1742*0e209d39SAndroid Build Coastguard Worker /* 6 : L+AN */ { s(5,3), s(4,0), 6 , 6 , 4 , s(4,0), s(4,0), 3 }
1743*0e209d39SAndroid Build Coastguard Worker };
1744*0e209d39SAndroid Build Coastguard Worker static const ImpAct impAct2 = {0,1,2,5,6,7,8};
1745*0e209d39SAndroid Build Coastguard Worker static const ImpAct impAct3 = {0,1,9,10,11,12};
1746*0e209d39SAndroid Build Coastguard Worker static const ImpTabPair impTab_INVERSE_LIKE_DIRECT_WITH_MARKS = {
1747*0e209d39SAndroid Build Coastguard Worker {&impTabL_INVERSE_LIKE_DIRECT_WITH_MARKS,
1748*0e209d39SAndroid Build Coastguard Worker &impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS},
1749*0e209d39SAndroid Build Coastguard Worker {&impAct2, &impAct3}};
1750*0e209d39SAndroid Build Coastguard Worker
1751*0e209d39SAndroid Build Coastguard Worker static const ImpTabPair impTab_INVERSE_FOR_NUMBERS_SPECIAL = {
1752*0e209d39SAndroid Build Coastguard Worker {&impTabL_NUMBERS_SPECIAL,
1753*0e209d39SAndroid Build Coastguard Worker &impTabR_INVERSE_LIKE_DIRECT},
1754*0e209d39SAndroid Build Coastguard Worker {&impAct0, &impAct1}};
1755*0e209d39SAndroid Build Coastguard Worker
1756*0e209d39SAndroid Build Coastguard Worker static const ImpTab impTabL_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS =
1757*0e209d39SAndroid Build Coastguard Worker /* The case handled in this table is (visually): R EN L
1758*0e209d39SAndroid Build Coastguard Worker */
1759*0e209d39SAndroid Build Coastguard Worker {
1760*0e209d39SAndroid Build Coastguard Worker /* L , R , EN , AN , ON , S , B , Res */
1761*0e209d39SAndroid Build Coastguard Worker /* 0 : init */ { 0 , s(6,2), 1 , 1 , 0 , 0 , 0 , 0 },
1762*0e209d39SAndroid Build Coastguard Worker /* 1 : L+EN/AN */ { 0 , s(6,2), 1 , 1 , 0 , s(3,0), 0 , 4 },
1763*0e209d39SAndroid Build Coastguard Worker /* 2 : R */ { 0 , s(6,2), s(5,4), s(5,4), s(1,3), s(3,0), 0 , 3 },
1764*0e209d39SAndroid Build Coastguard Worker /* 3 : R+ON */ { s(3,0), s(4,2), s(5,4), s(5,4), 3 , s(3,0), s(3,0), 3 },
1765*0e209d39SAndroid Build Coastguard Worker /* 4 : R+EN/AN */ { s(3,0), s(4,2), 4 , 4 , s(1,3), s(3,0), s(3,0), 4 }
1766*0e209d39SAndroid Build Coastguard Worker };
1767*0e209d39SAndroid Build Coastguard Worker static const ImpTabPair impTab_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS = {
1768*0e209d39SAndroid Build Coastguard Worker {&impTabL_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS,
1769*0e209d39SAndroid Build Coastguard Worker &impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS},
1770*0e209d39SAndroid Build Coastguard Worker {&impAct2, &impAct3}};
1771*0e209d39SAndroid Build Coastguard Worker
1772*0e209d39SAndroid Build Coastguard Worker #undef s
1773*0e209d39SAndroid Build Coastguard Worker
1774*0e209d39SAndroid Build Coastguard Worker typedef struct {
1775*0e209d39SAndroid Build Coastguard Worker const ImpTab * pImpTab; /* level table pointer */
1776*0e209d39SAndroid Build Coastguard Worker const ImpAct * pImpAct; /* action map array */
1777*0e209d39SAndroid Build Coastguard Worker int32_t startON; /* start of ON sequence */
1778*0e209d39SAndroid Build Coastguard Worker int32_t startL2EN; /* start of level 2 sequence */
1779*0e209d39SAndroid Build Coastguard Worker int32_t lastStrongRTL; /* index of last found R or AL */
1780*0e209d39SAndroid Build Coastguard Worker int32_t state; /* current state */
1781*0e209d39SAndroid Build Coastguard Worker int32_t runStart; /* start position of the run */
1782*0e209d39SAndroid Build Coastguard Worker UBiDiLevel runLevel; /* run level before implicit solving */
1783*0e209d39SAndroid Build Coastguard Worker } LevState;
1784*0e209d39SAndroid Build Coastguard Worker
1785*0e209d39SAndroid Build Coastguard Worker /*------------------------------------------------------------------------*/
1786*0e209d39SAndroid Build Coastguard Worker
1787*0e209d39SAndroid Build Coastguard Worker static void
addPoint(UBiDi * pBiDi,int32_t pos,int32_t flag)1788*0e209d39SAndroid Build Coastguard Worker addPoint(UBiDi *pBiDi, int32_t pos, int32_t flag)
1789*0e209d39SAndroid Build Coastguard Worker /* param pos: position where to insert
1790*0e209d39SAndroid Build Coastguard Worker param flag: one of LRM_BEFORE, LRM_AFTER, RLM_BEFORE, RLM_AFTER
1791*0e209d39SAndroid Build Coastguard Worker */
1792*0e209d39SAndroid Build Coastguard Worker {
1793*0e209d39SAndroid Build Coastguard Worker #define FIRSTALLOC 10
1794*0e209d39SAndroid Build Coastguard Worker Point point;
1795*0e209d39SAndroid Build Coastguard Worker InsertPoints * pInsertPoints=&(pBiDi->insertPoints);
1796*0e209d39SAndroid Build Coastguard Worker
1797*0e209d39SAndroid Build Coastguard Worker if (pInsertPoints->capacity == 0)
1798*0e209d39SAndroid Build Coastguard Worker {
1799*0e209d39SAndroid Build Coastguard Worker pInsertPoints->points=static_cast<Point *>(uprv_malloc(sizeof(Point)*FIRSTALLOC));
1800*0e209d39SAndroid Build Coastguard Worker if (pInsertPoints->points == nullptr)
1801*0e209d39SAndroid Build Coastguard Worker {
1802*0e209d39SAndroid Build Coastguard Worker pInsertPoints->errorCode=U_MEMORY_ALLOCATION_ERROR;
1803*0e209d39SAndroid Build Coastguard Worker return;
1804*0e209d39SAndroid Build Coastguard Worker }
1805*0e209d39SAndroid Build Coastguard Worker pInsertPoints->capacity=FIRSTALLOC;
1806*0e209d39SAndroid Build Coastguard Worker }
1807*0e209d39SAndroid Build Coastguard Worker if (pInsertPoints->size >= pInsertPoints->capacity) /* no room for new point */
1808*0e209d39SAndroid Build Coastguard Worker {
1809*0e209d39SAndroid Build Coastguard Worker Point * savePoints=pInsertPoints->points;
1810*0e209d39SAndroid Build Coastguard Worker pInsertPoints->points=static_cast<Point *>(uprv_realloc(pInsertPoints->points,
1811*0e209d39SAndroid Build Coastguard Worker pInsertPoints->capacity*2*sizeof(Point)));
1812*0e209d39SAndroid Build Coastguard Worker if (pInsertPoints->points == nullptr)
1813*0e209d39SAndroid Build Coastguard Worker {
1814*0e209d39SAndroid Build Coastguard Worker pInsertPoints->points=savePoints;
1815*0e209d39SAndroid Build Coastguard Worker pInsertPoints->errorCode=U_MEMORY_ALLOCATION_ERROR;
1816*0e209d39SAndroid Build Coastguard Worker return;
1817*0e209d39SAndroid Build Coastguard Worker }
1818*0e209d39SAndroid Build Coastguard Worker else pInsertPoints->capacity*=2;
1819*0e209d39SAndroid Build Coastguard Worker }
1820*0e209d39SAndroid Build Coastguard Worker point.pos=pos;
1821*0e209d39SAndroid Build Coastguard Worker point.flag=flag;
1822*0e209d39SAndroid Build Coastguard Worker pInsertPoints->points[pInsertPoints->size]=point;
1823*0e209d39SAndroid Build Coastguard Worker pInsertPoints->size++;
1824*0e209d39SAndroid Build Coastguard Worker #undef FIRSTALLOC
1825*0e209d39SAndroid Build Coastguard Worker }
1826*0e209d39SAndroid Build Coastguard Worker
1827*0e209d39SAndroid Build Coastguard Worker static void
setLevelsOutsideIsolates(UBiDi * pBiDi,int32_t start,int32_t limit,UBiDiLevel level)1828*0e209d39SAndroid Build Coastguard Worker setLevelsOutsideIsolates(UBiDi *pBiDi, int32_t start, int32_t limit, UBiDiLevel level)
1829*0e209d39SAndroid Build Coastguard Worker {
1830*0e209d39SAndroid Build Coastguard Worker DirProp *dirProps=pBiDi->dirProps, dirProp;
1831*0e209d39SAndroid Build Coastguard Worker UBiDiLevel *levels=pBiDi->levels;
1832*0e209d39SAndroid Build Coastguard Worker int32_t isolateCount=0, k;
1833*0e209d39SAndroid Build Coastguard Worker for(k=start; k<limit; k++) {
1834*0e209d39SAndroid Build Coastguard Worker dirProp=dirProps[k];
1835*0e209d39SAndroid Build Coastguard Worker if(dirProp==PDI)
1836*0e209d39SAndroid Build Coastguard Worker isolateCount--;
1837*0e209d39SAndroid Build Coastguard Worker if(isolateCount==0)
1838*0e209d39SAndroid Build Coastguard Worker levels[k]=level;
1839*0e209d39SAndroid Build Coastguard Worker if(dirProp==LRI || dirProp==RLI)
1840*0e209d39SAndroid Build Coastguard Worker isolateCount++;
1841*0e209d39SAndroid Build Coastguard Worker }
1842*0e209d39SAndroid Build Coastguard Worker }
1843*0e209d39SAndroid Build Coastguard Worker
1844*0e209d39SAndroid Build Coastguard Worker /* perform rules (Wn), (Nn), and (In) on a run of the text ------------------ */
1845*0e209d39SAndroid Build Coastguard Worker
1846*0e209d39SAndroid Build Coastguard Worker /*
1847*0e209d39SAndroid Build Coastguard Worker * This implementation of the (Wn) rules applies all rules in one pass.
1848*0e209d39SAndroid Build Coastguard Worker * In order to do so, it needs a look-ahead of typically 1 character
1849*0e209d39SAndroid Build Coastguard Worker * (except for W5: sequences of ET) and keeps track of changes
1850*0e209d39SAndroid Build Coastguard Worker * in a rule Wp that affect a later Wq (p<q).
1851*0e209d39SAndroid Build Coastguard Worker *
1852*0e209d39SAndroid Build Coastguard Worker * The (Nn) and (In) rules are also performed in that same single loop,
1853*0e209d39SAndroid Build Coastguard Worker * but effectively one iteration behind for white space.
1854*0e209d39SAndroid Build Coastguard Worker *
1855*0e209d39SAndroid Build Coastguard Worker * Since all implicit rules are performed in one step, it is not necessary
1856*0e209d39SAndroid Build Coastguard Worker * to actually store the intermediate directional properties in dirProps[].
1857*0e209d39SAndroid Build Coastguard Worker */
1858*0e209d39SAndroid Build Coastguard Worker
1859*0e209d39SAndroid Build Coastguard Worker static void
processPropertySeq(UBiDi * pBiDi,LevState * pLevState,uint8_t _prop,int32_t start,int32_t limit)1860*0e209d39SAndroid Build Coastguard Worker processPropertySeq(UBiDi *pBiDi, LevState *pLevState, uint8_t _prop,
1861*0e209d39SAndroid Build Coastguard Worker int32_t start, int32_t limit) {
1862*0e209d39SAndroid Build Coastguard Worker uint8_t cell, oldStateSeq, actionSeq;
1863*0e209d39SAndroid Build Coastguard Worker const ImpTab * pImpTab=pLevState->pImpTab;
1864*0e209d39SAndroid Build Coastguard Worker const ImpAct * pImpAct=pLevState->pImpAct;
1865*0e209d39SAndroid Build Coastguard Worker UBiDiLevel * levels=pBiDi->levels;
1866*0e209d39SAndroid Build Coastguard Worker UBiDiLevel level, addLevel;
1867*0e209d39SAndroid Build Coastguard Worker InsertPoints * pInsertPoints;
1868*0e209d39SAndroid Build Coastguard Worker int32_t start0, k;
1869*0e209d39SAndroid Build Coastguard Worker
1870*0e209d39SAndroid Build Coastguard Worker start0=start; /* save original start position */
1871*0e209d39SAndroid Build Coastguard Worker oldStateSeq=(uint8_t)pLevState->state;
1872*0e209d39SAndroid Build Coastguard Worker cell=(*pImpTab)[oldStateSeq][_prop];
1873*0e209d39SAndroid Build Coastguard Worker pLevState->state=GET_STATE(cell); /* isolate the new state */
1874*0e209d39SAndroid Build Coastguard Worker actionSeq=(*pImpAct)[GET_ACTION(cell)]; /* isolate the action */
1875*0e209d39SAndroid Build Coastguard Worker addLevel=(*pImpTab)[pLevState->state][IMPTABLEVELS_RES];
1876*0e209d39SAndroid Build Coastguard Worker
1877*0e209d39SAndroid Build Coastguard Worker if(actionSeq) {
1878*0e209d39SAndroid Build Coastguard Worker switch(actionSeq) {
1879*0e209d39SAndroid Build Coastguard Worker case 1: /* init ON seq */
1880*0e209d39SAndroid Build Coastguard Worker pLevState->startON=start0;
1881*0e209d39SAndroid Build Coastguard Worker break;
1882*0e209d39SAndroid Build Coastguard Worker
1883*0e209d39SAndroid Build Coastguard Worker case 2: /* prepend ON seq to current seq */
1884*0e209d39SAndroid Build Coastguard Worker start=pLevState->startON;
1885*0e209d39SAndroid Build Coastguard Worker break;
1886*0e209d39SAndroid Build Coastguard Worker
1887*0e209d39SAndroid Build Coastguard Worker case 3: /* EN/AN after R+ON */
1888*0e209d39SAndroid Build Coastguard Worker level=pLevState->runLevel+1;
1889*0e209d39SAndroid Build Coastguard Worker setLevelsOutsideIsolates(pBiDi, pLevState->startON, start0, level);
1890*0e209d39SAndroid Build Coastguard Worker break;
1891*0e209d39SAndroid Build Coastguard Worker
1892*0e209d39SAndroid Build Coastguard Worker case 4: /* EN/AN before R for NUMBERS_SPECIAL */
1893*0e209d39SAndroid Build Coastguard Worker level=pLevState->runLevel+2;
1894*0e209d39SAndroid Build Coastguard Worker setLevelsOutsideIsolates(pBiDi, pLevState->startON, start0, level);
1895*0e209d39SAndroid Build Coastguard Worker break;
1896*0e209d39SAndroid Build Coastguard Worker
1897*0e209d39SAndroid Build Coastguard Worker case 5: /* L or S after possible relevant EN/AN */
1898*0e209d39SAndroid Build Coastguard Worker /* check if we had EN after R/AL */
1899*0e209d39SAndroid Build Coastguard Worker if (pLevState->startL2EN >= 0) {
1900*0e209d39SAndroid Build Coastguard Worker addPoint(pBiDi, pLevState->startL2EN, LRM_BEFORE);
1901*0e209d39SAndroid Build Coastguard Worker }
1902*0e209d39SAndroid Build Coastguard Worker pLevState->startL2EN=-1; /* not within previous if since could also be -2 */
1903*0e209d39SAndroid Build Coastguard Worker /* check if we had any relevant EN/AN after R/AL */
1904*0e209d39SAndroid Build Coastguard Worker pInsertPoints=&(pBiDi->insertPoints);
1905*0e209d39SAndroid Build Coastguard Worker if ((pInsertPoints->capacity == 0) ||
1906*0e209d39SAndroid Build Coastguard Worker (pInsertPoints->size <= pInsertPoints->confirmed))
1907*0e209d39SAndroid Build Coastguard Worker {
1908*0e209d39SAndroid Build Coastguard Worker /* nothing, just clean up */
1909*0e209d39SAndroid Build Coastguard Worker pLevState->lastStrongRTL=-1;
1910*0e209d39SAndroid Build Coastguard Worker /* check if we have a pending conditional segment */
1911*0e209d39SAndroid Build Coastguard Worker level=(*pImpTab)[oldStateSeq][IMPTABLEVELS_RES];
1912*0e209d39SAndroid Build Coastguard Worker if ((level & 1) && (pLevState->startON > 0)) { /* after ON */
1913*0e209d39SAndroid Build Coastguard Worker start=pLevState->startON; /* reset to basic run level */
1914*0e209d39SAndroid Build Coastguard Worker }
1915*0e209d39SAndroid Build Coastguard Worker if (_prop == DirProp_S) /* add LRM before S */
1916*0e209d39SAndroid Build Coastguard Worker {
1917*0e209d39SAndroid Build Coastguard Worker addPoint(pBiDi, start0, LRM_BEFORE);
1918*0e209d39SAndroid Build Coastguard Worker pInsertPoints->confirmed=pInsertPoints->size;
1919*0e209d39SAndroid Build Coastguard Worker }
1920*0e209d39SAndroid Build Coastguard Worker break;
1921*0e209d39SAndroid Build Coastguard Worker }
1922*0e209d39SAndroid Build Coastguard Worker /* reset previous RTL cont to level for LTR text */
1923*0e209d39SAndroid Build Coastguard Worker for (k=pLevState->lastStrongRTL+1; k<start0; k++)
1924*0e209d39SAndroid Build Coastguard Worker {
1925*0e209d39SAndroid Build Coastguard Worker /* reset odd level, leave runLevel+2 as is */
1926*0e209d39SAndroid Build Coastguard Worker levels[k]=(levels[k] - 2) & ~1;
1927*0e209d39SAndroid Build Coastguard Worker }
1928*0e209d39SAndroid Build Coastguard Worker /* mark insert points as confirmed */
1929*0e209d39SAndroid Build Coastguard Worker pInsertPoints->confirmed=pInsertPoints->size;
1930*0e209d39SAndroid Build Coastguard Worker pLevState->lastStrongRTL=-1;
1931*0e209d39SAndroid Build Coastguard Worker if (_prop == DirProp_S) /* add LRM before S */
1932*0e209d39SAndroid Build Coastguard Worker {
1933*0e209d39SAndroid Build Coastguard Worker addPoint(pBiDi, start0, LRM_BEFORE);
1934*0e209d39SAndroid Build Coastguard Worker pInsertPoints->confirmed=pInsertPoints->size;
1935*0e209d39SAndroid Build Coastguard Worker }
1936*0e209d39SAndroid Build Coastguard Worker break;
1937*0e209d39SAndroid Build Coastguard Worker
1938*0e209d39SAndroid Build Coastguard Worker case 6: /* R/AL after possible relevant EN/AN */
1939*0e209d39SAndroid Build Coastguard Worker /* just clean up */
1940*0e209d39SAndroid Build Coastguard Worker pInsertPoints=&(pBiDi->insertPoints);
1941*0e209d39SAndroid Build Coastguard Worker if (pInsertPoints->capacity > 0)
1942*0e209d39SAndroid Build Coastguard Worker /* remove all non confirmed insert points */
1943*0e209d39SAndroid Build Coastguard Worker pInsertPoints->size=pInsertPoints->confirmed;
1944*0e209d39SAndroid Build Coastguard Worker pLevState->startON=-1;
1945*0e209d39SAndroid Build Coastguard Worker pLevState->startL2EN=-1;
1946*0e209d39SAndroid Build Coastguard Worker pLevState->lastStrongRTL=limit - 1;
1947*0e209d39SAndroid Build Coastguard Worker break;
1948*0e209d39SAndroid Build Coastguard Worker
1949*0e209d39SAndroid Build Coastguard Worker case 7: /* EN/AN after R/AL + possible cont */
1950*0e209d39SAndroid Build Coastguard Worker /* check for real AN */
1951*0e209d39SAndroid Build Coastguard Worker if ((_prop == DirProp_AN) && (pBiDi->dirProps[start0] == AN) &&
1952*0e209d39SAndroid Build Coastguard Worker (pBiDi->reorderingMode!=UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL))
1953*0e209d39SAndroid Build Coastguard Worker {
1954*0e209d39SAndroid Build Coastguard Worker /* real AN */
1955*0e209d39SAndroid Build Coastguard Worker if (pLevState->startL2EN == -1) /* if no relevant EN already found */
1956*0e209d39SAndroid Build Coastguard Worker {
1957*0e209d39SAndroid Build Coastguard Worker /* just note the righmost digit as a strong RTL */
1958*0e209d39SAndroid Build Coastguard Worker pLevState->lastStrongRTL=limit - 1;
1959*0e209d39SAndroid Build Coastguard Worker break;
1960*0e209d39SAndroid Build Coastguard Worker }
1961*0e209d39SAndroid Build Coastguard Worker if (pLevState->startL2EN >= 0) /* after EN, no AN */
1962*0e209d39SAndroid Build Coastguard Worker {
1963*0e209d39SAndroid Build Coastguard Worker addPoint(pBiDi, pLevState->startL2EN, LRM_BEFORE);
1964*0e209d39SAndroid Build Coastguard Worker pLevState->startL2EN=-2;
1965*0e209d39SAndroid Build Coastguard Worker }
1966*0e209d39SAndroid Build Coastguard Worker /* note AN */
1967*0e209d39SAndroid Build Coastguard Worker addPoint(pBiDi, start0, LRM_BEFORE);
1968*0e209d39SAndroid Build Coastguard Worker break;
1969*0e209d39SAndroid Build Coastguard Worker }
1970*0e209d39SAndroid Build Coastguard Worker /* if first EN/AN after R/AL */
1971*0e209d39SAndroid Build Coastguard Worker if (pLevState->startL2EN == -1) {
1972*0e209d39SAndroid Build Coastguard Worker pLevState->startL2EN=start0;
1973*0e209d39SAndroid Build Coastguard Worker }
1974*0e209d39SAndroid Build Coastguard Worker break;
1975*0e209d39SAndroid Build Coastguard Worker
1976*0e209d39SAndroid Build Coastguard Worker case 8: /* note location of latest R/AL */
1977*0e209d39SAndroid Build Coastguard Worker pLevState->lastStrongRTL=limit - 1;
1978*0e209d39SAndroid Build Coastguard Worker pLevState->startON=-1;
1979*0e209d39SAndroid Build Coastguard Worker break;
1980*0e209d39SAndroid Build Coastguard Worker
1981*0e209d39SAndroid Build Coastguard Worker case 9: /* L after R+ON/EN/AN */
1982*0e209d39SAndroid Build Coastguard Worker /* include possible adjacent number on the left */
1983*0e209d39SAndroid Build Coastguard Worker for (k=start0-1; k>=0 && !(levels[k]&1); k--);
1984*0e209d39SAndroid Build Coastguard Worker if(k>=0) {
1985*0e209d39SAndroid Build Coastguard Worker addPoint(pBiDi, k, RLM_BEFORE); /* add RLM before */
1986*0e209d39SAndroid Build Coastguard Worker pInsertPoints=&(pBiDi->insertPoints);
1987*0e209d39SAndroid Build Coastguard Worker pInsertPoints->confirmed=pInsertPoints->size; /* confirm it */
1988*0e209d39SAndroid Build Coastguard Worker }
1989*0e209d39SAndroid Build Coastguard Worker pLevState->startON=start0;
1990*0e209d39SAndroid Build Coastguard Worker break;
1991*0e209d39SAndroid Build Coastguard Worker
1992*0e209d39SAndroid Build Coastguard Worker case 10: /* AN after L */
1993*0e209d39SAndroid Build Coastguard Worker /* AN numbers between L text on both sides may be trouble. */
1994*0e209d39SAndroid Build Coastguard Worker /* tentatively bracket with LRMs; will be confirmed if followed by L */
1995*0e209d39SAndroid Build Coastguard Worker addPoint(pBiDi, start0, LRM_BEFORE); /* add LRM before */
1996*0e209d39SAndroid Build Coastguard Worker addPoint(pBiDi, start0, LRM_AFTER); /* add LRM after */
1997*0e209d39SAndroid Build Coastguard Worker break;
1998*0e209d39SAndroid Build Coastguard Worker
1999*0e209d39SAndroid Build Coastguard Worker case 11: /* R after L+ON/EN/AN */
2000*0e209d39SAndroid Build Coastguard Worker /* false alert, infirm LRMs around previous AN */
2001*0e209d39SAndroid Build Coastguard Worker pInsertPoints=&(pBiDi->insertPoints);
2002*0e209d39SAndroid Build Coastguard Worker pInsertPoints->size=pInsertPoints->confirmed;
2003*0e209d39SAndroid Build Coastguard Worker if (_prop == DirProp_S) /* add RLM before S */
2004*0e209d39SAndroid Build Coastguard Worker {
2005*0e209d39SAndroid Build Coastguard Worker addPoint(pBiDi, start0, RLM_BEFORE);
2006*0e209d39SAndroid Build Coastguard Worker pInsertPoints->confirmed=pInsertPoints->size;
2007*0e209d39SAndroid Build Coastguard Worker }
2008*0e209d39SAndroid Build Coastguard Worker break;
2009*0e209d39SAndroid Build Coastguard Worker
2010*0e209d39SAndroid Build Coastguard Worker case 12: /* L after L+ON/AN */
2011*0e209d39SAndroid Build Coastguard Worker level=pLevState->runLevel + addLevel;
2012*0e209d39SAndroid Build Coastguard Worker for(k=pLevState->startON; k<start0; k++) {
2013*0e209d39SAndroid Build Coastguard Worker if (levels[k]<level)
2014*0e209d39SAndroid Build Coastguard Worker levels[k]=level;
2015*0e209d39SAndroid Build Coastguard Worker }
2016*0e209d39SAndroid Build Coastguard Worker pInsertPoints=&(pBiDi->insertPoints);
2017*0e209d39SAndroid Build Coastguard Worker pInsertPoints->confirmed=pInsertPoints->size; /* confirm inserts */
2018*0e209d39SAndroid Build Coastguard Worker pLevState->startON=start0;
2019*0e209d39SAndroid Build Coastguard Worker break;
2020*0e209d39SAndroid Build Coastguard Worker
2021*0e209d39SAndroid Build Coastguard Worker case 13: /* L after L+ON+EN/AN/ON */
2022*0e209d39SAndroid Build Coastguard Worker level=pLevState->runLevel;
2023*0e209d39SAndroid Build Coastguard Worker for(k=start0-1; k>=pLevState->startON; k--) {
2024*0e209d39SAndroid Build Coastguard Worker if(levels[k]==level+3) {
2025*0e209d39SAndroid Build Coastguard Worker while(levels[k]==level+3) {
2026*0e209d39SAndroid Build Coastguard Worker levels[k--]-=2;
2027*0e209d39SAndroid Build Coastguard Worker }
2028*0e209d39SAndroid Build Coastguard Worker while(levels[k]==level) {
2029*0e209d39SAndroid Build Coastguard Worker k--;
2030*0e209d39SAndroid Build Coastguard Worker }
2031*0e209d39SAndroid Build Coastguard Worker }
2032*0e209d39SAndroid Build Coastguard Worker if(levels[k]==level+2) {
2033*0e209d39SAndroid Build Coastguard Worker levels[k]=level;
2034*0e209d39SAndroid Build Coastguard Worker continue;
2035*0e209d39SAndroid Build Coastguard Worker }
2036*0e209d39SAndroid Build Coastguard Worker levels[k]=level+1;
2037*0e209d39SAndroid Build Coastguard Worker }
2038*0e209d39SAndroid Build Coastguard Worker break;
2039*0e209d39SAndroid Build Coastguard Worker
2040*0e209d39SAndroid Build Coastguard Worker case 14: /* R after L+ON+EN/AN/ON */
2041*0e209d39SAndroid Build Coastguard Worker level=pLevState->runLevel+1;
2042*0e209d39SAndroid Build Coastguard Worker for(k=start0-1; k>=pLevState->startON; k--) {
2043*0e209d39SAndroid Build Coastguard Worker if(levels[k]>level) {
2044*0e209d39SAndroid Build Coastguard Worker levels[k]-=2;
2045*0e209d39SAndroid Build Coastguard Worker }
2046*0e209d39SAndroid Build Coastguard Worker }
2047*0e209d39SAndroid Build Coastguard Worker break;
2048*0e209d39SAndroid Build Coastguard Worker
2049*0e209d39SAndroid Build Coastguard Worker default: /* we should never get here */
2050*0e209d39SAndroid Build Coastguard Worker UPRV_UNREACHABLE_EXIT;
2051*0e209d39SAndroid Build Coastguard Worker }
2052*0e209d39SAndroid Build Coastguard Worker }
2053*0e209d39SAndroid Build Coastguard Worker if((addLevel) || (start < start0)) {
2054*0e209d39SAndroid Build Coastguard Worker level=pLevState->runLevel + addLevel;
2055*0e209d39SAndroid Build Coastguard Worker if(start>=pLevState->runStart) {
2056*0e209d39SAndroid Build Coastguard Worker for(k=start; k<limit; k++) {
2057*0e209d39SAndroid Build Coastguard Worker levels[k]=level;
2058*0e209d39SAndroid Build Coastguard Worker }
2059*0e209d39SAndroid Build Coastguard Worker } else {
2060*0e209d39SAndroid Build Coastguard Worker setLevelsOutsideIsolates(pBiDi, start, limit, level);
2061*0e209d39SAndroid Build Coastguard Worker }
2062*0e209d39SAndroid Build Coastguard Worker }
2063*0e209d39SAndroid Build Coastguard Worker }
2064*0e209d39SAndroid Build Coastguard Worker
2065*0e209d39SAndroid Build Coastguard Worker /**
2066*0e209d39SAndroid Build Coastguard Worker * Returns the directionality of the last strong character at the end of the prologue, if any.
2067*0e209d39SAndroid Build Coastguard Worker * Requires prologue!=null.
2068*0e209d39SAndroid Build Coastguard Worker */
2069*0e209d39SAndroid Build Coastguard Worker static DirProp
lastL_R_AL(UBiDi * pBiDi)2070*0e209d39SAndroid Build Coastguard Worker lastL_R_AL(UBiDi *pBiDi) {
2071*0e209d39SAndroid Build Coastguard Worker const char16_t *text=pBiDi->prologue;
2072*0e209d39SAndroid Build Coastguard Worker int32_t length=pBiDi->proLength;
2073*0e209d39SAndroid Build Coastguard Worker int32_t i;
2074*0e209d39SAndroid Build Coastguard Worker UChar32 uchar;
2075*0e209d39SAndroid Build Coastguard Worker DirProp dirProp;
2076*0e209d39SAndroid Build Coastguard Worker for(i=length; i>0; ) {
2077*0e209d39SAndroid Build Coastguard Worker /* i is decremented by U16_PREV */
2078*0e209d39SAndroid Build Coastguard Worker U16_PREV(text, 0, i, uchar);
2079*0e209d39SAndroid Build Coastguard Worker dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar);
2080*0e209d39SAndroid Build Coastguard Worker if(dirProp==L) {
2081*0e209d39SAndroid Build Coastguard Worker return DirProp_L;
2082*0e209d39SAndroid Build Coastguard Worker }
2083*0e209d39SAndroid Build Coastguard Worker if(dirProp==R || dirProp==AL) {
2084*0e209d39SAndroid Build Coastguard Worker return DirProp_R;
2085*0e209d39SAndroid Build Coastguard Worker }
2086*0e209d39SAndroid Build Coastguard Worker if(dirProp==B) {
2087*0e209d39SAndroid Build Coastguard Worker return DirProp_ON;
2088*0e209d39SAndroid Build Coastguard Worker }
2089*0e209d39SAndroid Build Coastguard Worker }
2090*0e209d39SAndroid Build Coastguard Worker return DirProp_ON;
2091*0e209d39SAndroid Build Coastguard Worker }
2092*0e209d39SAndroid Build Coastguard Worker
2093*0e209d39SAndroid Build Coastguard Worker /**
2094*0e209d39SAndroid Build Coastguard Worker * Returns the directionality of the first strong character, or digit, in the epilogue, if any.
2095*0e209d39SAndroid Build Coastguard Worker * Requires epilogue!=null.
2096*0e209d39SAndroid Build Coastguard Worker */
2097*0e209d39SAndroid Build Coastguard Worker static DirProp
firstL_R_AL_EN_AN(UBiDi * pBiDi)2098*0e209d39SAndroid Build Coastguard Worker firstL_R_AL_EN_AN(UBiDi *pBiDi) {
2099*0e209d39SAndroid Build Coastguard Worker const char16_t *text=pBiDi->epilogue;
2100*0e209d39SAndroid Build Coastguard Worker int32_t length=pBiDi->epiLength;
2101*0e209d39SAndroid Build Coastguard Worker int32_t i;
2102*0e209d39SAndroid Build Coastguard Worker UChar32 uchar;
2103*0e209d39SAndroid Build Coastguard Worker DirProp dirProp;
2104*0e209d39SAndroid Build Coastguard Worker for(i=0; i<length; ) {
2105*0e209d39SAndroid Build Coastguard Worker /* i is incremented by U16_NEXT */
2106*0e209d39SAndroid Build Coastguard Worker U16_NEXT(text, i, length, uchar);
2107*0e209d39SAndroid Build Coastguard Worker dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar);
2108*0e209d39SAndroid Build Coastguard Worker if(dirProp==L) {
2109*0e209d39SAndroid Build Coastguard Worker return DirProp_L;
2110*0e209d39SAndroid Build Coastguard Worker }
2111*0e209d39SAndroid Build Coastguard Worker if(dirProp==R || dirProp==AL) {
2112*0e209d39SAndroid Build Coastguard Worker return DirProp_R;
2113*0e209d39SAndroid Build Coastguard Worker }
2114*0e209d39SAndroid Build Coastguard Worker if(dirProp==EN) {
2115*0e209d39SAndroid Build Coastguard Worker return DirProp_EN;
2116*0e209d39SAndroid Build Coastguard Worker }
2117*0e209d39SAndroid Build Coastguard Worker if(dirProp==AN) {
2118*0e209d39SAndroid Build Coastguard Worker return DirProp_AN;
2119*0e209d39SAndroid Build Coastguard Worker }
2120*0e209d39SAndroid Build Coastguard Worker }
2121*0e209d39SAndroid Build Coastguard Worker return DirProp_ON;
2122*0e209d39SAndroid Build Coastguard Worker }
2123*0e209d39SAndroid Build Coastguard Worker
2124*0e209d39SAndroid Build Coastguard Worker static void
resolveImplicitLevels(UBiDi * pBiDi,int32_t start,int32_t limit,DirProp sor,DirProp eor)2125*0e209d39SAndroid Build Coastguard Worker resolveImplicitLevels(UBiDi *pBiDi,
2126*0e209d39SAndroid Build Coastguard Worker int32_t start, int32_t limit,
2127*0e209d39SAndroid Build Coastguard Worker DirProp sor, DirProp eor) {
2128*0e209d39SAndroid Build Coastguard Worker const DirProp *dirProps=pBiDi->dirProps;
2129*0e209d39SAndroid Build Coastguard Worker DirProp dirProp;
2130*0e209d39SAndroid Build Coastguard Worker LevState levState;
2131*0e209d39SAndroid Build Coastguard Worker int32_t i, start1, start2;
2132*0e209d39SAndroid Build Coastguard Worker uint16_t oldStateImp, stateImp, actionImp;
2133*0e209d39SAndroid Build Coastguard Worker uint8_t gprop, resProp, cell;
2134*0e209d39SAndroid Build Coastguard Worker UBool inverseRTL;
2135*0e209d39SAndroid Build Coastguard Worker DirProp nextStrongProp=R;
2136*0e209d39SAndroid Build Coastguard Worker int32_t nextStrongPos=-1;
2137*0e209d39SAndroid Build Coastguard Worker
2138*0e209d39SAndroid Build Coastguard Worker /* check for RTL inverse BiDi mode */
2139*0e209d39SAndroid Build Coastguard Worker /* FOOD FOR THOUGHT: in case of RTL inverse BiDi, it would make sense to
2140*0e209d39SAndroid Build Coastguard Worker * loop on the text characters from end to start.
2141*0e209d39SAndroid Build Coastguard Worker * This would need a different properties state table (at least different
2142*0e209d39SAndroid Build Coastguard Worker * actions) and different levels state tables (maybe very similar to the
2143*0e209d39SAndroid Build Coastguard Worker * LTR corresponding ones.
2144*0e209d39SAndroid Build Coastguard Worker */
2145*0e209d39SAndroid Build Coastguard Worker inverseRTL=(UBool)
2146*0e209d39SAndroid Build Coastguard Worker ((start<pBiDi->lastArabicPos) && (GET_PARALEVEL(pBiDi, start) & 1) &&
2147*0e209d39SAndroid Build Coastguard Worker (pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_LIKE_DIRECT ||
2148*0e209d39SAndroid Build Coastguard Worker pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL));
2149*0e209d39SAndroid Build Coastguard Worker
2150*0e209d39SAndroid Build Coastguard Worker /* initialize for property and levels state tables */
2151*0e209d39SAndroid Build Coastguard Worker levState.startL2EN=-1; /* used for INVERSE_LIKE_DIRECT_WITH_MARKS */
2152*0e209d39SAndroid Build Coastguard Worker levState.lastStrongRTL=-1; /* used for INVERSE_LIKE_DIRECT_WITH_MARKS */
2153*0e209d39SAndroid Build Coastguard Worker levState.runStart=start;
2154*0e209d39SAndroid Build Coastguard Worker levState.runLevel=pBiDi->levels[start];
2155*0e209d39SAndroid Build Coastguard Worker levState.pImpTab=(const ImpTab*)((pBiDi->pImpTabPair)->pImpTab)[levState.runLevel&1];
2156*0e209d39SAndroid Build Coastguard Worker levState.pImpAct=(const ImpAct*)((pBiDi->pImpTabPair)->pImpAct)[levState.runLevel&1];
2157*0e209d39SAndroid Build Coastguard Worker if(start==0 && pBiDi->proLength>0) {
2158*0e209d39SAndroid Build Coastguard Worker DirProp lastStrong=lastL_R_AL(pBiDi);
2159*0e209d39SAndroid Build Coastguard Worker if(lastStrong!=DirProp_ON) {
2160*0e209d39SAndroid Build Coastguard Worker sor=lastStrong;
2161*0e209d39SAndroid Build Coastguard Worker }
2162*0e209d39SAndroid Build Coastguard Worker }
2163*0e209d39SAndroid Build Coastguard Worker /* The isolates[] entries contain enough information to
2164*0e209d39SAndroid Build Coastguard Worker resume the bidi algorithm in the same state as it was
2165*0e209d39SAndroid Build Coastguard Worker when it was interrupted by an isolate sequence. */
2166*0e209d39SAndroid Build Coastguard Worker if(dirProps[start]==PDI && pBiDi->isolateCount >= 0) {
2167*0e209d39SAndroid Build Coastguard Worker levState.startON=pBiDi->isolates[pBiDi->isolateCount].startON;
2168*0e209d39SAndroid Build Coastguard Worker start1=pBiDi->isolates[pBiDi->isolateCount].start1;
2169*0e209d39SAndroid Build Coastguard Worker stateImp=pBiDi->isolates[pBiDi->isolateCount].stateImp;
2170*0e209d39SAndroid Build Coastguard Worker levState.state=pBiDi->isolates[pBiDi->isolateCount].state;
2171*0e209d39SAndroid Build Coastguard Worker pBiDi->isolateCount--;
2172*0e209d39SAndroid Build Coastguard Worker } else {
2173*0e209d39SAndroid Build Coastguard Worker levState.startON=-1;
2174*0e209d39SAndroid Build Coastguard Worker start1=start;
2175*0e209d39SAndroid Build Coastguard Worker if(dirProps[start]==NSM)
2176*0e209d39SAndroid Build Coastguard Worker stateImp = 1 + sor;
2177*0e209d39SAndroid Build Coastguard Worker else
2178*0e209d39SAndroid Build Coastguard Worker stateImp=0;
2179*0e209d39SAndroid Build Coastguard Worker levState.state=0;
2180*0e209d39SAndroid Build Coastguard Worker processPropertySeq(pBiDi, &levState, sor, start, start);
2181*0e209d39SAndroid Build Coastguard Worker }
2182*0e209d39SAndroid Build Coastguard Worker start2=start; /* to make Java compiler happy */
2183*0e209d39SAndroid Build Coastguard Worker
2184*0e209d39SAndroid Build Coastguard Worker for(i=start; i<=limit; i++) {
2185*0e209d39SAndroid Build Coastguard Worker if(i>=limit) {
2186*0e209d39SAndroid Build Coastguard Worker int32_t k;
2187*0e209d39SAndroid Build Coastguard Worker for(k=limit-1; k>start&&(DIRPROP_FLAG(dirProps[k])&MASK_BN_EXPLICIT); k--);
2188*0e209d39SAndroid Build Coastguard Worker dirProp=dirProps[k];
2189*0e209d39SAndroid Build Coastguard Worker if(dirProp==LRI || dirProp==RLI)
2190*0e209d39SAndroid Build Coastguard Worker break; /* no forced closing for sequence ending with LRI/RLI */
2191*0e209d39SAndroid Build Coastguard Worker gprop=eor;
2192*0e209d39SAndroid Build Coastguard Worker } else {
2193*0e209d39SAndroid Build Coastguard Worker DirProp prop, prop1;
2194*0e209d39SAndroid Build Coastguard Worker prop=dirProps[i];
2195*0e209d39SAndroid Build Coastguard Worker if(prop==B) {
2196*0e209d39SAndroid Build Coastguard Worker pBiDi->isolateCount=-1; /* current isolates stack entry == none */
2197*0e209d39SAndroid Build Coastguard Worker }
2198*0e209d39SAndroid Build Coastguard Worker if(inverseRTL) {
2199*0e209d39SAndroid Build Coastguard Worker if(prop==AL) {
2200*0e209d39SAndroid Build Coastguard Worker /* AL before EN does not make it AN */
2201*0e209d39SAndroid Build Coastguard Worker prop=R;
2202*0e209d39SAndroid Build Coastguard Worker } else if(prop==EN) {
2203*0e209d39SAndroid Build Coastguard Worker if(nextStrongPos<=i) {
2204*0e209d39SAndroid Build Coastguard Worker /* look for next strong char (L/R/AL) */
2205*0e209d39SAndroid Build Coastguard Worker int32_t j;
2206*0e209d39SAndroid Build Coastguard Worker nextStrongProp=R; /* set default */
2207*0e209d39SAndroid Build Coastguard Worker nextStrongPos=limit;
2208*0e209d39SAndroid Build Coastguard Worker for(j=i+1; j<limit; j++) {
2209*0e209d39SAndroid Build Coastguard Worker prop1=dirProps[j];
2210*0e209d39SAndroid Build Coastguard Worker if(prop1==L || prop1==R || prop1==AL) {
2211*0e209d39SAndroid Build Coastguard Worker nextStrongProp=prop1;
2212*0e209d39SAndroid Build Coastguard Worker nextStrongPos=j;
2213*0e209d39SAndroid Build Coastguard Worker break;
2214*0e209d39SAndroid Build Coastguard Worker }
2215*0e209d39SAndroid Build Coastguard Worker }
2216*0e209d39SAndroid Build Coastguard Worker }
2217*0e209d39SAndroid Build Coastguard Worker if(nextStrongProp==AL) {
2218*0e209d39SAndroid Build Coastguard Worker prop=AN;
2219*0e209d39SAndroid Build Coastguard Worker }
2220*0e209d39SAndroid Build Coastguard Worker }
2221*0e209d39SAndroid Build Coastguard Worker }
2222*0e209d39SAndroid Build Coastguard Worker gprop=groupProp[prop];
2223*0e209d39SAndroid Build Coastguard Worker }
2224*0e209d39SAndroid Build Coastguard Worker oldStateImp=stateImp;
2225*0e209d39SAndroid Build Coastguard Worker cell=impTabProps[oldStateImp][gprop];
2226*0e209d39SAndroid Build Coastguard Worker stateImp=GET_STATEPROPS(cell); /* isolate the new state */
2227*0e209d39SAndroid Build Coastguard Worker actionImp=GET_ACTIONPROPS(cell); /* isolate the action */
2228*0e209d39SAndroid Build Coastguard Worker if((i==limit) && (actionImp==0)) {
2229*0e209d39SAndroid Build Coastguard Worker /* there is an unprocessed sequence if its property == eor */
2230*0e209d39SAndroid Build Coastguard Worker actionImp=1; /* process the last sequence */
2231*0e209d39SAndroid Build Coastguard Worker }
2232*0e209d39SAndroid Build Coastguard Worker if(actionImp) {
2233*0e209d39SAndroid Build Coastguard Worker resProp=impTabProps[oldStateImp][IMPTABPROPS_RES];
2234*0e209d39SAndroid Build Coastguard Worker switch(actionImp) {
2235*0e209d39SAndroid Build Coastguard Worker case 1: /* process current seq1, init new seq1 */
2236*0e209d39SAndroid Build Coastguard Worker processPropertySeq(pBiDi, &levState, resProp, start1, i);
2237*0e209d39SAndroid Build Coastguard Worker start1=i;
2238*0e209d39SAndroid Build Coastguard Worker break;
2239*0e209d39SAndroid Build Coastguard Worker case 2: /* init new seq2 */
2240*0e209d39SAndroid Build Coastguard Worker start2=i;
2241*0e209d39SAndroid Build Coastguard Worker break;
2242*0e209d39SAndroid Build Coastguard Worker case 3: /* process seq1, process seq2, init new seq1 */
2243*0e209d39SAndroid Build Coastguard Worker processPropertySeq(pBiDi, &levState, resProp, start1, start2);
2244*0e209d39SAndroid Build Coastguard Worker processPropertySeq(pBiDi, &levState, DirProp_ON, start2, i);
2245*0e209d39SAndroid Build Coastguard Worker start1=i;
2246*0e209d39SAndroid Build Coastguard Worker break;
2247*0e209d39SAndroid Build Coastguard Worker case 4: /* process seq1, set seq1=seq2, init new seq2 */
2248*0e209d39SAndroid Build Coastguard Worker processPropertySeq(pBiDi, &levState, resProp, start1, start2);
2249*0e209d39SAndroid Build Coastguard Worker start1=start2;
2250*0e209d39SAndroid Build Coastguard Worker start2=i;
2251*0e209d39SAndroid Build Coastguard Worker break;
2252*0e209d39SAndroid Build Coastguard Worker default: /* we should never get here */
2253*0e209d39SAndroid Build Coastguard Worker UPRV_UNREACHABLE_EXIT;
2254*0e209d39SAndroid Build Coastguard Worker }
2255*0e209d39SAndroid Build Coastguard Worker }
2256*0e209d39SAndroid Build Coastguard Worker }
2257*0e209d39SAndroid Build Coastguard Worker
2258*0e209d39SAndroid Build Coastguard Worker /* flush possible pending sequence, e.g. ON */
2259*0e209d39SAndroid Build Coastguard Worker if(limit==pBiDi->length && pBiDi->epiLength>0) {
2260*0e209d39SAndroid Build Coastguard Worker DirProp firstStrong=firstL_R_AL_EN_AN(pBiDi);
2261*0e209d39SAndroid Build Coastguard Worker if(firstStrong!=DirProp_ON) {
2262*0e209d39SAndroid Build Coastguard Worker eor=firstStrong;
2263*0e209d39SAndroid Build Coastguard Worker }
2264*0e209d39SAndroid Build Coastguard Worker }
2265*0e209d39SAndroid Build Coastguard Worker
2266*0e209d39SAndroid Build Coastguard Worker /* look for the last char not a BN or LRE/RLE/LRO/RLO/PDF */
2267*0e209d39SAndroid Build Coastguard Worker for(i=limit-1; i>start&&(DIRPROP_FLAG(dirProps[i])&MASK_BN_EXPLICIT); i--);
2268*0e209d39SAndroid Build Coastguard Worker dirProp=dirProps[i];
2269*0e209d39SAndroid Build Coastguard Worker if((dirProp==LRI || dirProp==RLI) && limit<pBiDi->length) {
2270*0e209d39SAndroid Build Coastguard Worker pBiDi->isolateCount++;
2271*0e209d39SAndroid Build Coastguard Worker pBiDi->isolates[pBiDi->isolateCount].stateImp=stateImp;
2272*0e209d39SAndroid Build Coastguard Worker pBiDi->isolates[pBiDi->isolateCount].state=levState.state;
2273*0e209d39SAndroid Build Coastguard Worker pBiDi->isolates[pBiDi->isolateCount].start1=start1;
2274*0e209d39SAndroid Build Coastguard Worker pBiDi->isolates[pBiDi->isolateCount].startON=levState.startON;
2275*0e209d39SAndroid Build Coastguard Worker }
2276*0e209d39SAndroid Build Coastguard Worker else
2277*0e209d39SAndroid Build Coastguard Worker processPropertySeq(pBiDi, &levState, eor, limit, limit);
2278*0e209d39SAndroid Build Coastguard Worker }
2279*0e209d39SAndroid Build Coastguard Worker
2280*0e209d39SAndroid Build Coastguard Worker /* perform (L1) and (X9) ---------------------------------------------------- */
2281*0e209d39SAndroid Build Coastguard Worker
2282*0e209d39SAndroid Build Coastguard Worker /*
2283*0e209d39SAndroid Build Coastguard Worker * Reset the embedding levels for some non-graphic characters (L1).
2284*0e209d39SAndroid Build Coastguard Worker * This function also sets appropriate levels for BN, and
2285*0e209d39SAndroid Build Coastguard Worker * explicit embedding types that are supposed to have been removed
2286*0e209d39SAndroid Build Coastguard Worker * from the paragraph in (X9).
2287*0e209d39SAndroid Build Coastguard Worker */
2288*0e209d39SAndroid Build Coastguard Worker static void
adjustWSLevels(UBiDi * pBiDi)2289*0e209d39SAndroid Build Coastguard Worker adjustWSLevels(UBiDi *pBiDi) {
2290*0e209d39SAndroid Build Coastguard Worker const DirProp *dirProps=pBiDi->dirProps;
2291*0e209d39SAndroid Build Coastguard Worker UBiDiLevel *levels=pBiDi->levels;
2292*0e209d39SAndroid Build Coastguard Worker int32_t i;
2293*0e209d39SAndroid Build Coastguard Worker
2294*0e209d39SAndroid Build Coastguard Worker if(pBiDi->flags&MASK_WS) {
2295*0e209d39SAndroid Build Coastguard Worker UBool orderParagraphsLTR=pBiDi->orderParagraphsLTR;
2296*0e209d39SAndroid Build Coastguard Worker Flags flag;
2297*0e209d39SAndroid Build Coastguard Worker
2298*0e209d39SAndroid Build Coastguard Worker i=pBiDi->trailingWSStart;
2299*0e209d39SAndroid Build Coastguard Worker while(i>0) {
2300*0e209d39SAndroid Build Coastguard Worker /* reset a sequence of WS/BN before eop and B/S to the paragraph paraLevel */
2301*0e209d39SAndroid Build Coastguard Worker while(i>0 && (flag=DIRPROP_FLAG(dirProps[--i]))&MASK_WS) {
2302*0e209d39SAndroid Build Coastguard Worker if(orderParagraphsLTR&&(flag&DIRPROP_FLAG(B))) {
2303*0e209d39SAndroid Build Coastguard Worker levels[i]=0;
2304*0e209d39SAndroid Build Coastguard Worker } else {
2305*0e209d39SAndroid Build Coastguard Worker levels[i]=GET_PARALEVEL(pBiDi, i);
2306*0e209d39SAndroid Build Coastguard Worker }
2307*0e209d39SAndroid Build Coastguard Worker }
2308*0e209d39SAndroid Build Coastguard Worker
2309*0e209d39SAndroid Build Coastguard Worker /* reset BN to the next character's paraLevel until B/S, which restarts above loop */
2310*0e209d39SAndroid Build Coastguard Worker /* here, i+1 is guaranteed to be <length */
2311*0e209d39SAndroid Build Coastguard Worker while(i>0) {
2312*0e209d39SAndroid Build Coastguard Worker flag=DIRPROP_FLAG(dirProps[--i]);
2313*0e209d39SAndroid Build Coastguard Worker if(flag&MASK_BN_EXPLICIT) {
2314*0e209d39SAndroid Build Coastguard Worker levels[i]=levels[i+1];
2315*0e209d39SAndroid Build Coastguard Worker } else if(orderParagraphsLTR&&(flag&DIRPROP_FLAG(B))) {
2316*0e209d39SAndroid Build Coastguard Worker levels[i]=0;
2317*0e209d39SAndroid Build Coastguard Worker break;
2318*0e209d39SAndroid Build Coastguard Worker } else if(flag&MASK_B_S) {
2319*0e209d39SAndroid Build Coastguard Worker levels[i]=GET_PARALEVEL(pBiDi, i);
2320*0e209d39SAndroid Build Coastguard Worker break;
2321*0e209d39SAndroid Build Coastguard Worker }
2322*0e209d39SAndroid Build Coastguard Worker }
2323*0e209d39SAndroid Build Coastguard Worker }
2324*0e209d39SAndroid Build Coastguard Worker }
2325*0e209d39SAndroid Build Coastguard Worker }
2326*0e209d39SAndroid Build Coastguard Worker
2327*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
ubidi_setContext(UBiDi * pBiDi,const char16_t * prologue,int32_t proLength,const char16_t * epilogue,int32_t epiLength,UErrorCode * pErrorCode)2328*0e209d39SAndroid Build Coastguard Worker ubidi_setContext(UBiDi *pBiDi,
2329*0e209d39SAndroid Build Coastguard Worker const char16_t *prologue, int32_t proLength,
2330*0e209d39SAndroid Build Coastguard Worker const char16_t *epilogue, int32_t epiLength,
2331*0e209d39SAndroid Build Coastguard Worker UErrorCode *pErrorCode) {
2332*0e209d39SAndroid Build Coastguard Worker /* check the argument values */
2333*0e209d39SAndroid Build Coastguard Worker RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode);
2334*0e209d39SAndroid Build Coastguard Worker if(pBiDi==nullptr || proLength<-1 || epiLength<-1 ||
2335*0e209d39SAndroid Build Coastguard Worker (prologue==nullptr && proLength!=0) || (epilogue==nullptr && epiLength!=0)) {
2336*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
2337*0e209d39SAndroid Build Coastguard Worker return;
2338*0e209d39SAndroid Build Coastguard Worker }
2339*0e209d39SAndroid Build Coastguard Worker
2340*0e209d39SAndroid Build Coastguard Worker if(proLength==-1) {
2341*0e209d39SAndroid Build Coastguard Worker pBiDi->proLength=u_strlen(prologue);
2342*0e209d39SAndroid Build Coastguard Worker } else {
2343*0e209d39SAndroid Build Coastguard Worker pBiDi->proLength=proLength;
2344*0e209d39SAndroid Build Coastguard Worker }
2345*0e209d39SAndroid Build Coastguard Worker if(epiLength==-1) {
2346*0e209d39SAndroid Build Coastguard Worker pBiDi->epiLength=u_strlen(epilogue);
2347*0e209d39SAndroid Build Coastguard Worker } else {
2348*0e209d39SAndroid Build Coastguard Worker pBiDi->epiLength=epiLength;
2349*0e209d39SAndroid Build Coastguard Worker }
2350*0e209d39SAndroid Build Coastguard Worker pBiDi->prologue=prologue;
2351*0e209d39SAndroid Build Coastguard Worker pBiDi->epilogue=epilogue;
2352*0e209d39SAndroid Build Coastguard Worker }
2353*0e209d39SAndroid Build Coastguard Worker
2354*0e209d39SAndroid Build Coastguard Worker static void
setParaSuccess(UBiDi * pBiDi)2355*0e209d39SAndroid Build Coastguard Worker setParaSuccess(UBiDi *pBiDi) {
2356*0e209d39SAndroid Build Coastguard Worker pBiDi->proLength=0; /* forget the last context */
2357*0e209d39SAndroid Build Coastguard Worker pBiDi->epiLength=0;
2358*0e209d39SAndroid Build Coastguard Worker pBiDi->pParaBiDi=pBiDi; /* mark successful setPara */
2359*0e209d39SAndroid Build Coastguard Worker }
2360*0e209d39SAndroid Build Coastguard Worker
2361*0e209d39SAndroid Build Coastguard Worker #define BIDI_MIN(x, y) ((x)<(y) ? (x) : (y))
2362*0e209d39SAndroid Build Coastguard Worker #define BIDI_ABS(x) ((x)>=0 ? (x) : (-(x)))
2363*0e209d39SAndroid Build Coastguard Worker
2364*0e209d39SAndroid Build Coastguard Worker static void
setParaRunsOnly(UBiDi * pBiDi,const char16_t * text,int32_t length,UBiDiLevel paraLevel,UErrorCode * pErrorCode)2365*0e209d39SAndroid Build Coastguard Worker setParaRunsOnly(UBiDi *pBiDi, const char16_t *text, int32_t length,
2366*0e209d39SAndroid Build Coastguard Worker UBiDiLevel paraLevel, UErrorCode *pErrorCode) {
2367*0e209d39SAndroid Build Coastguard Worker int32_t *runsOnlyMemory = nullptr;
2368*0e209d39SAndroid Build Coastguard Worker int32_t *visualMap;
2369*0e209d39SAndroid Build Coastguard Worker char16_t *visualText;
2370*0e209d39SAndroid Build Coastguard Worker int32_t saveLength, saveTrailingWSStart;
2371*0e209d39SAndroid Build Coastguard Worker const UBiDiLevel *levels;
2372*0e209d39SAndroid Build Coastguard Worker UBiDiLevel *saveLevels;
2373*0e209d39SAndroid Build Coastguard Worker UBiDiDirection saveDirection;
2374*0e209d39SAndroid Build Coastguard Worker UBool saveMayAllocateText;
2375*0e209d39SAndroid Build Coastguard Worker Run *runs;
2376*0e209d39SAndroid Build Coastguard Worker int32_t visualLength, i, j, visualStart, logicalStart,
2377*0e209d39SAndroid Build Coastguard Worker runCount, runLength, addedRuns, insertRemove,
2378*0e209d39SAndroid Build Coastguard Worker start, limit, step, indexOddBit, logicalPos,
2379*0e209d39SAndroid Build Coastguard Worker index0, index1;
2380*0e209d39SAndroid Build Coastguard Worker uint32_t saveOptions;
2381*0e209d39SAndroid Build Coastguard Worker
2382*0e209d39SAndroid Build Coastguard Worker pBiDi->reorderingMode=UBIDI_REORDER_DEFAULT;
2383*0e209d39SAndroid Build Coastguard Worker if(length==0) {
2384*0e209d39SAndroid Build Coastguard Worker ubidi_setPara(pBiDi, text, length, paraLevel, nullptr, pErrorCode);
2385*0e209d39SAndroid Build Coastguard Worker goto cleanup3;
2386*0e209d39SAndroid Build Coastguard Worker }
2387*0e209d39SAndroid Build Coastguard Worker /* obtain memory for mapping table and visual text */
2388*0e209d39SAndroid Build Coastguard Worker runsOnlyMemory=static_cast<int32_t *>(uprv_malloc(length*(sizeof(int32_t)+sizeof(char16_t)+sizeof(UBiDiLevel))));
2389*0e209d39SAndroid Build Coastguard Worker if(runsOnlyMemory==nullptr) {
2390*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
2391*0e209d39SAndroid Build Coastguard Worker goto cleanup3;
2392*0e209d39SAndroid Build Coastguard Worker }
2393*0e209d39SAndroid Build Coastguard Worker visualMap=runsOnlyMemory;
2394*0e209d39SAndroid Build Coastguard Worker visualText=(char16_t *)&visualMap[length];
2395*0e209d39SAndroid Build Coastguard Worker saveLevels=(UBiDiLevel *)&visualText[length];
2396*0e209d39SAndroid Build Coastguard Worker saveOptions=pBiDi->reorderingOptions;
2397*0e209d39SAndroid Build Coastguard Worker if(saveOptions & UBIDI_OPTION_INSERT_MARKS) {
2398*0e209d39SAndroid Build Coastguard Worker pBiDi->reorderingOptions&=~UBIDI_OPTION_INSERT_MARKS;
2399*0e209d39SAndroid Build Coastguard Worker pBiDi->reorderingOptions|=UBIDI_OPTION_REMOVE_CONTROLS;
2400*0e209d39SAndroid Build Coastguard Worker }
2401*0e209d39SAndroid Build Coastguard Worker paraLevel&=1; /* accept only 0 or 1 */
2402*0e209d39SAndroid Build Coastguard Worker ubidi_setPara(pBiDi, text, length, paraLevel, nullptr, pErrorCode);
2403*0e209d39SAndroid Build Coastguard Worker if(U_FAILURE(*pErrorCode)) {
2404*0e209d39SAndroid Build Coastguard Worker goto cleanup3;
2405*0e209d39SAndroid Build Coastguard Worker }
2406*0e209d39SAndroid Build Coastguard Worker /* we cannot access directly pBiDi->levels since it is not yet set if
2407*0e209d39SAndroid Build Coastguard Worker * direction is not MIXED
2408*0e209d39SAndroid Build Coastguard Worker */
2409*0e209d39SAndroid Build Coastguard Worker levels=ubidi_getLevels(pBiDi, pErrorCode);
2410*0e209d39SAndroid Build Coastguard Worker uprv_memcpy(saveLevels, levels, (size_t)pBiDi->length*sizeof(UBiDiLevel));
2411*0e209d39SAndroid Build Coastguard Worker saveTrailingWSStart=pBiDi->trailingWSStart;
2412*0e209d39SAndroid Build Coastguard Worker saveLength=pBiDi->length;
2413*0e209d39SAndroid Build Coastguard Worker saveDirection=pBiDi->direction;
2414*0e209d39SAndroid Build Coastguard Worker
2415*0e209d39SAndroid Build Coastguard Worker /* FOOD FOR THOUGHT: instead of writing the visual text, we could use
2416*0e209d39SAndroid Build Coastguard Worker * the visual map and the dirProps array to drive the second call
2417*0e209d39SAndroid Build Coastguard Worker * to ubidi_setPara (but must make provision for possible removal of
2418*0e209d39SAndroid Build Coastguard Worker * BiDi controls. Alternatively, only use the dirProps array via
2419*0e209d39SAndroid Build Coastguard Worker * customized classifier callback.
2420*0e209d39SAndroid Build Coastguard Worker */
2421*0e209d39SAndroid Build Coastguard Worker visualLength=ubidi_writeReordered(pBiDi, visualText, length,
2422*0e209d39SAndroid Build Coastguard Worker UBIDI_DO_MIRRORING, pErrorCode);
2423*0e209d39SAndroid Build Coastguard Worker ubidi_getVisualMap(pBiDi, visualMap, pErrorCode);
2424*0e209d39SAndroid Build Coastguard Worker if(U_FAILURE(*pErrorCode)) {
2425*0e209d39SAndroid Build Coastguard Worker goto cleanup2;
2426*0e209d39SAndroid Build Coastguard Worker }
2427*0e209d39SAndroid Build Coastguard Worker pBiDi->reorderingOptions=saveOptions;
2428*0e209d39SAndroid Build Coastguard Worker
2429*0e209d39SAndroid Build Coastguard Worker pBiDi->reorderingMode=UBIDI_REORDER_INVERSE_LIKE_DIRECT;
2430*0e209d39SAndroid Build Coastguard Worker paraLevel^=1;
2431*0e209d39SAndroid Build Coastguard Worker /* Because what we did with reorderingOptions, visualText may be shorter
2432*0e209d39SAndroid Build Coastguard Worker * than the original text. But we don't want the levels memory to be
2433*0e209d39SAndroid Build Coastguard Worker * reallocated shorter than the original length, since we need to restore
2434*0e209d39SAndroid Build Coastguard Worker * the levels as after the first call to ubidi_setpara() before returning.
2435*0e209d39SAndroid Build Coastguard Worker * We will force mayAllocateText to false before the second call to
2436*0e209d39SAndroid Build Coastguard Worker * ubidi_setpara(), and will restore it afterwards.
2437*0e209d39SAndroid Build Coastguard Worker */
2438*0e209d39SAndroid Build Coastguard Worker saveMayAllocateText=pBiDi->mayAllocateText;
2439*0e209d39SAndroid Build Coastguard Worker pBiDi->mayAllocateText=false;
2440*0e209d39SAndroid Build Coastguard Worker ubidi_setPara(pBiDi, visualText, visualLength, paraLevel, nullptr, pErrorCode);
2441*0e209d39SAndroid Build Coastguard Worker pBiDi->mayAllocateText=saveMayAllocateText;
2442*0e209d39SAndroid Build Coastguard Worker ubidi_getRuns(pBiDi, pErrorCode);
2443*0e209d39SAndroid Build Coastguard Worker if(U_FAILURE(*pErrorCode)) {
2444*0e209d39SAndroid Build Coastguard Worker goto cleanup1;
2445*0e209d39SAndroid Build Coastguard Worker }
2446*0e209d39SAndroid Build Coastguard Worker /* check if some runs must be split, count how many splits */
2447*0e209d39SAndroid Build Coastguard Worker addedRuns=0;
2448*0e209d39SAndroid Build Coastguard Worker runCount=pBiDi->runCount;
2449*0e209d39SAndroid Build Coastguard Worker runs=pBiDi->runs;
2450*0e209d39SAndroid Build Coastguard Worker visualStart=0;
2451*0e209d39SAndroid Build Coastguard Worker for(i=0; i<runCount; i++, visualStart+=runLength) {
2452*0e209d39SAndroid Build Coastguard Worker runLength=runs[i].visualLimit-visualStart;
2453*0e209d39SAndroid Build Coastguard Worker if(runLength<2) {
2454*0e209d39SAndroid Build Coastguard Worker continue;
2455*0e209d39SAndroid Build Coastguard Worker }
2456*0e209d39SAndroid Build Coastguard Worker logicalStart=GET_INDEX(runs[i].logicalStart);
2457*0e209d39SAndroid Build Coastguard Worker for(j=logicalStart+1; j<logicalStart+runLength; j++) {
2458*0e209d39SAndroid Build Coastguard Worker index0=visualMap[j];
2459*0e209d39SAndroid Build Coastguard Worker index1=visualMap[j-1];
2460*0e209d39SAndroid Build Coastguard Worker if((BIDI_ABS(index0-index1)!=1) || (saveLevels[index0]!=saveLevels[index1])) {
2461*0e209d39SAndroid Build Coastguard Worker addedRuns++;
2462*0e209d39SAndroid Build Coastguard Worker }
2463*0e209d39SAndroid Build Coastguard Worker }
2464*0e209d39SAndroid Build Coastguard Worker }
2465*0e209d39SAndroid Build Coastguard Worker if(addedRuns) {
2466*0e209d39SAndroid Build Coastguard Worker if(getRunsMemory(pBiDi, runCount+addedRuns)) {
2467*0e209d39SAndroid Build Coastguard Worker if(runCount==1) {
2468*0e209d39SAndroid Build Coastguard Worker /* because we switch from UBiDi.simpleRuns to UBiDi.runs */
2469*0e209d39SAndroid Build Coastguard Worker pBiDi->runsMemory[0]=runs[0];
2470*0e209d39SAndroid Build Coastguard Worker }
2471*0e209d39SAndroid Build Coastguard Worker runs=pBiDi->runs=pBiDi->runsMemory;
2472*0e209d39SAndroid Build Coastguard Worker pBiDi->runCount+=addedRuns;
2473*0e209d39SAndroid Build Coastguard Worker } else {
2474*0e209d39SAndroid Build Coastguard Worker goto cleanup1;
2475*0e209d39SAndroid Build Coastguard Worker }
2476*0e209d39SAndroid Build Coastguard Worker }
2477*0e209d39SAndroid Build Coastguard Worker /* split runs which are not consecutive in source text */
2478*0e209d39SAndroid Build Coastguard Worker for(i=runCount-1; i>=0; i--) {
2479*0e209d39SAndroid Build Coastguard Worker runLength= i==0 ? runs[0].visualLimit :
2480*0e209d39SAndroid Build Coastguard Worker runs[i].visualLimit-runs[i-1].visualLimit;
2481*0e209d39SAndroid Build Coastguard Worker logicalStart=runs[i].logicalStart;
2482*0e209d39SAndroid Build Coastguard Worker indexOddBit=GET_ODD_BIT(logicalStart);
2483*0e209d39SAndroid Build Coastguard Worker logicalStart=GET_INDEX(logicalStart);
2484*0e209d39SAndroid Build Coastguard Worker if(runLength<2) {
2485*0e209d39SAndroid Build Coastguard Worker if(addedRuns) {
2486*0e209d39SAndroid Build Coastguard Worker runs[i+addedRuns]=runs[i];
2487*0e209d39SAndroid Build Coastguard Worker }
2488*0e209d39SAndroid Build Coastguard Worker logicalPos=visualMap[logicalStart];
2489*0e209d39SAndroid Build Coastguard Worker runs[i+addedRuns].logicalStart=MAKE_INDEX_ODD_PAIR(logicalPos,
2490*0e209d39SAndroid Build Coastguard Worker saveLevels[logicalPos]^indexOddBit);
2491*0e209d39SAndroid Build Coastguard Worker continue;
2492*0e209d39SAndroid Build Coastguard Worker }
2493*0e209d39SAndroid Build Coastguard Worker if(indexOddBit) {
2494*0e209d39SAndroid Build Coastguard Worker start=logicalStart;
2495*0e209d39SAndroid Build Coastguard Worker limit=logicalStart+runLength-1;
2496*0e209d39SAndroid Build Coastguard Worker step=1;
2497*0e209d39SAndroid Build Coastguard Worker } else {
2498*0e209d39SAndroid Build Coastguard Worker start=logicalStart+runLength-1;
2499*0e209d39SAndroid Build Coastguard Worker limit=logicalStart;
2500*0e209d39SAndroid Build Coastguard Worker step=-1;
2501*0e209d39SAndroid Build Coastguard Worker }
2502*0e209d39SAndroid Build Coastguard Worker for(j=start; j!=limit; j+=step) {
2503*0e209d39SAndroid Build Coastguard Worker index0=visualMap[j];
2504*0e209d39SAndroid Build Coastguard Worker index1=visualMap[j+step];
2505*0e209d39SAndroid Build Coastguard Worker if((BIDI_ABS(index0-index1)!=1) || (saveLevels[index0]!=saveLevels[index1])) {
2506*0e209d39SAndroid Build Coastguard Worker logicalPos=BIDI_MIN(visualMap[start], index0);
2507*0e209d39SAndroid Build Coastguard Worker runs[i+addedRuns].logicalStart=MAKE_INDEX_ODD_PAIR(logicalPos,
2508*0e209d39SAndroid Build Coastguard Worker saveLevels[logicalPos]^indexOddBit);
2509*0e209d39SAndroid Build Coastguard Worker runs[i+addedRuns].visualLimit=runs[i].visualLimit;
2510*0e209d39SAndroid Build Coastguard Worker runs[i].visualLimit-=BIDI_ABS(j-start)+1;
2511*0e209d39SAndroid Build Coastguard Worker insertRemove=runs[i].insertRemove&(LRM_AFTER|RLM_AFTER);
2512*0e209d39SAndroid Build Coastguard Worker runs[i+addedRuns].insertRemove=insertRemove;
2513*0e209d39SAndroid Build Coastguard Worker runs[i].insertRemove&=~insertRemove;
2514*0e209d39SAndroid Build Coastguard Worker start=j+step;
2515*0e209d39SAndroid Build Coastguard Worker addedRuns--;
2516*0e209d39SAndroid Build Coastguard Worker }
2517*0e209d39SAndroid Build Coastguard Worker }
2518*0e209d39SAndroid Build Coastguard Worker if(addedRuns) {
2519*0e209d39SAndroid Build Coastguard Worker runs[i+addedRuns]=runs[i];
2520*0e209d39SAndroid Build Coastguard Worker }
2521*0e209d39SAndroid Build Coastguard Worker logicalPos=BIDI_MIN(visualMap[start], visualMap[limit]);
2522*0e209d39SAndroid Build Coastguard Worker runs[i+addedRuns].logicalStart=MAKE_INDEX_ODD_PAIR(logicalPos,
2523*0e209d39SAndroid Build Coastguard Worker saveLevels[logicalPos]^indexOddBit);
2524*0e209d39SAndroid Build Coastguard Worker }
2525*0e209d39SAndroid Build Coastguard Worker
2526*0e209d39SAndroid Build Coastguard Worker cleanup1:
2527*0e209d39SAndroid Build Coastguard Worker /* restore initial paraLevel */
2528*0e209d39SAndroid Build Coastguard Worker pBiDi->paraLevel^=1;
2529*0e209d39SAndroid Build Coastguard Worker cleanup2:
2530*0e209d39SAndroid Build Coastguard Worker /* restore real text */
2531*0e209d39SAndroid Build Coastguard Worker pBiDi->text=text;
2532*0e209d39SAndroid Build Coastguard Worker pBiDi->length=saveLength;
2533*0e209d39SAndroid Build Coastguard Worker pBiDi->originalLength=length;
2534*0e209d39SAndroid Build Coastguard Worker pBiDi->direction=saveDirection;
2535*0e209d39SAndroid Build Coastguard Worker /* the saved levels should never excess levelsSize, but we check anyway */
2536*0e209d39SAndroid Build Coastguard Worker if(saveLength>pBiDi->levelsSize) {
2537*0e209d39SAndroid Build Coastguard Worker saveLength=pBiDi->levelsSize;
2538*0e209d39SAndroid Build Coastguard Worker }
2539*0e209d39SAndroid Build Coastguard Worker uprv_memcpy(pBiDi->levels, saveLevels, (size_t)saveLength*sizeof(UBiDiLevel));
2540*0e209d39SAndroid Build Coastguard Worker pBiDi->trailingWSStart=saveTrailingWSStart;
2541*0e209d39SAndroid Build Coastguard Worker if(pBiDi->runCount>1) {
2542*0e209d39SAndroid Build Coastguard Worker pBiDi->direction=UBIDI_MIXED;
2543*0e209d39SAndroid Build Coastguard Worker }
2544*0e209d39SAndroid Build Coastguard Worker cleanup3:
2545*0e209d39SAndroid Build Coastguard Worker /* free memory for mapping table and visual text */
2546*0e209d39SAndroid Build Coastguard Worker uprv_free(runsOnlyMemory);
2547*0e209d39SAndroid Build Coastguard Worker
2548*0e209d39SAndroid Build Coastguard Worker pBiDi->reorderingMode=UBIDI_REORDER_RUNS_ONLY;
2549*0e209d39SAndroid Build Coastguard Worker }
2550*0e209d39SAndroid Build Coastguard Worker
2551*0e209d39SAndroid Build Coastguard Worker /* ubidi_setPara ------------------------------------------------------------ */
2552*0e209d39SAndroid Build Coastguard Worker
2553*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
ubidi_setPara(UBiDi * pBiDi,const char16_t * text,int32_t length,UBiDiLevel paraLevel,UBiDiLevel * embeddingLevels,UErrorCode * pErrorCode)2554*0e209d39SAndroid Build Coastguard Worker ubidi_setPara(UBiDi *pBiDi, const char16_t *text, int32_t length,
2555*0e209d39SAndroid Build Coastguard Worker UBiDiLevel paraLevel, UBiDiLevel *embeddingLevels,
2556*0e209d39SAndroid Build Coastguard Worker UErrorCode *pErrorCode) {
2557*0e209d39SAndroid Build Coastguard Worker UBiDiDirection direction;
2558*0e209d39SAndroid Build Coastguard Worker DirProp *dirProps;
2559*0e209d39SAndroid Build Coastguard Worker
2560*0e209d39SAndroid Build Coastguard Worker /* check the argument values */
2561*0e209d39SAndroid Build Coastguard Worker RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode);
2562*0e209d39SAndroid Build Coastguard Worker if(pBiDi==nullptr || text==nullptr || length<-1 ||
2563*0e209d39SAndroid Build Coastguard Worker (paraLevel>UBIDI_MAX_EXPLICIT_LEVEL && paraLevel<UBIDI_DEFAULT_LTR)) {
2564*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
2565*0e209d39SAndroid Build Coastguard Worker return;
2566*0e209d39SAndroid Build Coastguard Worker }
2567*0e209d39SAndroid Build Coastguard Worker
2568*0e209d39SAndroid Build Coastguard Worker if(length==-1) {
2569*0e209d39SAndroid Build Coastguard Worker length=u_strlen(text);
2570*0e209d39SAndroid Build Coastguard Worker }
2571*0e209d39SAndroid Build Coastguard Worker
2572*0e209d39SAndroid Build Coastguard Worker /* special treatment for RUNS_ONLY mode */
2573*0e209d39SAndroid Build Coastguard Worker if(pBiDi->reorderingMode==UBIDI_REORDER_RUNS_ONLY) {
2574*0e209d39SAndroid Build Coastguard Worker setParaRunsOnly(pBiDi, text, length, paraLevel, pErrorCode);
2575*0e209d39SAndroid Build Coastguard Worker return;
2576*0e209d39SAndroid Build Coastguard Worker }
2577*0e209d39SAndroid Build Coastguard Worker
2578*0e209d39SAndroid Build Coastguard Worker /* initialize the UBiDi structure */
2579*0e209d39SAndroid Build Coastguard Worker pBiDi->pParaBiDi=nullptr; /* mark unfinished setPara */
2580*0e209d39SAndroid Build Coastguard Worker pBiDi->text=text;
2581*0e209d39SAndroid Build Coastguard Worker pBiDi->length=pBiDi->originalLength=pBiDi->resultLength=length;
2582*0e209d39SAndroid Build Coastguard Worker pBiDi->paraLevel=paraLevel;
2583*0e209d39SAndroid Build Coastguard Worker pBiDi->direction=(UBiDiDirection)(paraLevel&1);
2584*0e209d39SAndroid Build Coastguard Worker pBiDi->paraCount=1;
2585*0e209d39SAndroid Build Coastguard Worker
2586*0e209d39SAndroid Build Coastguard Worker pBiDi->dirProps=nullptr;
2587*0e209d39SAndroid Build Coastguard Worker pBiDi->levels=nullptr;
2588*0e209d39SAndroid Build Coastguard Worker pBiDi->runs=nullptr;
2589*0e209d39SAndroid Build Coastguard Worker pBiDi->insertPoints.size=0; /* clean up from last call */
2590*0e209d39SAndroid Build Coastguard Worker pBiDi->insertPoints.confirmed=0; /* clean up from last call */
2591*0e209d39SAndroid Build Coastguard Worker
2592*0e209d39SAndroid Build Coastguard Worker /*
2593*0e209d39SAndroid Build Coastguard Worker * Save the original paraLevel if contextual; otherwise, set to 0.
2594*0e209d39SAndroid Build Coastguard Worker */
2595*0e209d39SAndroid Build Coastguard Worker pBiDi->defaultParaLevel=IS_DEFAULT_LEVEL(paraLevel);
2596*0e209d39SAndroid Build Coastguard Worker
2597*0e209d39SAndroid Build Coastguard Worker if(length==0) {
2598*0e209d39SAndroid Build Coastguard Worker /*
2599*0e209d39SAndroid Build Coastguard Worker * For an empty paragraph, create a UBiDi object with the paraLevel and
2600*0e209d39SAndroid Build Coastguard Worker * the flags and the direction set but without allocating zero-length arrays.
2601*0e209d39SAndroid Build Coastguard Worker * There is nothing more to do.
2602*0e209d39SAndroid Build Coastguard Worker */
2603*0e209d39SAndroid Build Coastguard Worker if(IS_DEFAULT_LEVEL(paraLevel)) {
2604*0e209d39SAndroid Build Coastguard Worker pBiDi->paraLevel&=1;
2605*0e209d39SAndroid Build Coastguard Worker pBiDi->defaultParaLevel=0;
2606*0e209d39SAndroid Build Coastguard Worker }
2607*0e209d39SAndroid Build Coastguard Worker pBiDi->flags=DIRPROP_FLAG_LR(paraLevel);
2608*0e209d39SAndroid Build Coastguard Worker pBiDi->runCount=0;
2609*0e209d39SAndroid Build Coastguard Worker pBiDi->paraCount=0;
2610*0e209d39SAndroid Build Coastguard Worker setParaSuccess(pBiDi); /* mark successful setPara */
2611*0e209d39SAndroid Build Coastguard Worker return;
2612*0e209d39SAndroid Build Coastguard Worker }
2613*0e209d39SAndroid Build Coastguard Worker
2614*0e209d39SAndroid Build Coastguard Worker pBiDi->runCount=-1;
2615*0e209d39SAndroid Build Coastguard Worker
2616*0e209d39SAndroid Build Coastguard Worker /* allocate paras memory */
2617*0e209d39SAndroid Build Coastguard Worker if(pBiDi->parasMemory)
2618*0e209d39SAndroid Build Coastguard Worker pBiDi->paras=pBiDi->parasMemory;
2619*0e209d39SAndroid Build Coastguard Worker else
2620*0e209d39SAndroid Build Coastguard Worker pBiDi->paras=pBiDi->simpleParas;
2621*0e209d39SAndroid Build Coastguard Worker
2622*0e209d39SAndroid Build Coastguard Worker /*
2623*0e209d39SAndroid Build Coastguard Worker * Get the directional properties,
2624*0e209d39SAndroid Build Coastguard Worker * the flags bit-set, and
2625*0e209d39SAndroid Build Coastguard Worker * determine the paragraph level if necessary.
2626*0e209d39SAndroid Build Coastguard Worker */
2627*0e209d39SAndroid Build Coastguard Worker if(getDirPropsMemory(pBiDi, length)) {
2628*0e209d39SAndroid Build Coastguard Worker pBiDi->dirProps=pBiDi->dirPropsMemory;
2629*0e209d39SAndroid Build Coastguard Worker if(!getDirProps(pBiDi)) {
2630*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
2631*0e209d39SAndroid Build Coastguard Worker return;
2632*0e209d39SAndroid Build Coastguard Worker }
2633*0e209d39SAndroid Build Coastguard Worker } else {
2634*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
2635*0e209d39SAndroid Build Coastguard Worker return;
2636*0e209d39SAndroid Build Coastguard Worker }
2637*0e209d39SAndroid Build Coastguard Worker dirProps=pBiDi->dirProps;
2638*0e209d39SAndroid Build Coastguard Worker /* the processed length may have changed if UBIDI_OPTION_STREAMING */
2639*0e209d39SAndroid Build Coastguard Worker length= pBiDi->length;
2640*0e209d39SAndroid Build Coastguard Worker pBiDi->trailingWSStart=length; /* the levels[] will reflect the WS run */
2641*0e209d39SAndroid Build Coastguard Worker
2642*0e209d39SAndroid Build Coastguard Worker /* are explicit levels specified? */
2643*0e209d39SAndroid Build Coastguard Worker if(embeddingLevels==nullptr) {
2644*0e209d39SAndroid Build Coastguard Worker /* no: determine explicit levels according to the (Xn) rules */\
2645*0e209d39SAndroid Build Coastguard Worker if(getLevelsMemory(pBiDi, length)) {
2646*0e209d39SAndroid Build Coastguard Worker pBiDi->levels=pBiDi->levelsMemory;
2647*0e209d39SAndroid Build Coastguard Worker direction=resolveExplicitLevels(pBiDi, pErrorCode);
2648*0e209d39SAndroid Build Coastguard Worker if(U_FAILURE(*pErrorCode)) {
2649*0e209d39SAndroid Build Coastguard Worker return;
2650*0e209d39SAndroid Build Coastguard Worker }
2651*0e209d39SAndroid Build Coastguard Worker } else {
2652*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
2653*0e209d39SAndroid Build Coastguard Worker return;
2654*0e209d39SAndroid Build Coastguard Worker }
2655*0e209d39SAndroid Build Coastguard Worker } else {
2656*0e209d39SAndroid Build Coastguard Worker /* set BN for all explicit codes, check that all levels are 0 or paraLevel..UBIDI_MAX_EXPLICIT_LEVEL */
2657*0e209d39SAndroid Build Coastguard Worker pBiDi->levels=embeddingLevels;
2658*0e209d39SAndroid Build Coastguard Worker direction=checkExplicitLevels(pBiDi, pErrorCode);
2659*0e209d39SAndroid Build Coastguard Worker if(U_FAILURE(*pErrorCode)) {
2660*0e209d39SAndroid Build Coastguard Worker return;
2661*0e209d39SAndroid Build Coastguard Worker }
2662*0e209d39SAndroid Build Coastguard Worker }
2663*0e209d39SAndroid Build Coastguard Worker
2664*0e209d39SAndroid Build Coastguard Worker /* allocate isolate memory */
2665*0e209d39SAndroid Build Coastguard Worker if(pBiDi->isolateCount<=SIMPLE_ISOLATES_COUNT)
2666*0e209d39SAndroid Build Coastguard Worker pBiDi->isolates=pBiDi->simpleIsolates;
2667*0e209d39SAndroid Build Coastguard Worker else
2668*0e209d39SAndroid Build Coastguard Worker if((int32_t)(pBiDi->isolateCount*sizeof(Isolate))<=pBiDi->isolatesSize)
2669*0e209d39SAndroid Build Coastguard Worker pBiDi->isolates=pBiDi->isolatesMemory;
2670*0e209d39SAndroid Build Coastguard Worker else {
2671*0e209d39SAndroid Build Coastguard Worker if(getInitialIsolatesMemory(pBiDi, pBiDi->isolateCount)) {
2672*0e209d39SAndroid Build Coastguard Worker pBiDi->isolates=pBiDi->isolatesMemory;
2673*0e209d39SAndroid Build Coastguard Worker } else {
2674*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
2675*0e209d39SAndroid Build Coastguard Worker return;
2676*0e209d39SAndroid Build Coastguard Worker }
2677*0e209d39SAndroid Build Coastguard Worker }
2678*0e209d39SAndroid Build Coastguard Worker pBiDi->isolateCount=-1; /* current isolates stack entry == none */
2679*0e209d39SAndroid Build Coastguard Worker
2680*0e209d39SAndroid Build Coastguard Worker /*
2681*0e209d39SAndroid Build Coastguard Worker * The steps after (X9) in the UBiDi algorithm are performed only if
2682*0e209d39SAndroid Build Coastguard Worker * the paragraph text has mixed directionality!
2683*0e209d39SAndroid Build Coastguard Worker */
2684*0e209d39SAndroid Build Coastguard Worker pBiDi->direction=direction;
2685*0e209d39SAndroid Build Coastguard Worker switch(direction) {
2686*0e209d39SAndroid Build Coastguard Worker case UBIDI_LTR:
2687*0e209d39SAndroid Build Coastguard Worker /* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */
2688*0e209d39SAndroid Build Coastguard Worker pBiDi->trailingWSStart=0;
2689*0e209d39SAndroid Build Coastguard Worker break;
2690*0e209d39SAndroid Build Coastguard Worker case UBIDI_RTL:
2691*0e209d39SAndroid Build Coastguard Worker /* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */
2692*0e209d39SAndroid Build Coastguard Worker pBiDi->trailingWSStart=0;
2693*0e209d39SAndroid Build Coastguard Worker break;
2694*0e209d39SAndroid Build Coastguard Worker default:
2695*0e209d39SAndroid Build Coastguard Worker /*
2696*0e209d39SAndroid Build Coastguard Worker * Choose the right implicit state table
2697*0e209d39SAndroid Build Coastguard Worker */
2698*0e209d39SAndroid Build Coastguard Worker switch(pBiDi->reorderingMode) {
2699*0e209d39SAndroid Build Coastguard Worker case UBIDI_REORDER_DEFAULT:
2700*0e209d39SAndroid Build Coastguard Worker pBiDi->pImpTabPair=&impTab_DEFAULT;
2701*0e209d39SAndroid Build Coastguard Worker break;
2702*0e209d39SAndroid Build Coastguard Worker case UBIDI_REORDER_NUMBERS_SPECIAL:
2703*0e209d39SAndroid Build Coastguard Worker pBiDi->pImpTabPair=&impTab_NUMBERS_SPECIAL;
2704*0e209d39SAndroid Build Coastguard Worker break;
2705*0e209d39SAndroid Build Coastguard Worker case UBIDI_REORDER_GROUP_NUMBERS_WITH_R:
2706*0e209d39SAndroid Build Coastguard Worker pBiDi->pImpTabPair=&impTab_GROUP_NUMBERS_WITH_R;
2707*0e209d39SAndroid Build Coastguard Worker break;
2708*0e209d39SAndroid Build Coastguard Worker case UBIDI_REORDER_INVERSE_NUMBERS_AS_L:
2709*0e209d39SAndroid Build Coastguard Worker pBiDi->pImpTabPair=&impTab_INVERSE_NUMBERS_AS_L;
2710*0e209d39SAndroid Build Coastguard Worker break;
2711*0e209d39SAndroid Build Coastguard Worker case UBIDI_REORDER_INVERSE_LIKE_DIRECT:
2712*0e209d39SAndroid Build Coastguard Worker if (pBiDi->reorderingOptions & UBIDI_OPTION_INSERT_MARKS) {
2713*0e209d39SAndroid Build Coastguard Worker pBiDi->pImpTabPair=&impTab_INVERSE_LIKE_DIRECT_WITH_MARKS;
2714*0e209d39SAndroid Build Coastguard Worker } else {
2715*0e209d39SAndroid Build Coastguard Worker pBiDi->pImpTabPair=&impTab_INVERSE_LIKE_DIRECT;
2716*0e209d39SAndroid Build Coastguard Worker }
2717*0e209d39SAndroid Build Coastguard Worker break;
2718*0e209d39SAndroid Build Coastguard Worker case UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL:
2719*0e209d39SAndroid Build Coastguard Worker if (pBiDi->reorderingOptions & UBIDI_OPTION_INSERT_MARKS) {
2720*0e209d39SAndroid Build Coastguard Worker pBiDi->pImpTabPair=&impTab_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS;
2721*0e209d39SAndroid Build Coastguard Worker } else {
2722*0e209d39SAndroid Build Coastguard Worker pBiDi->pImpTabPair=&impTab_INVERSE_FOR_NUMBERS_SPECIAL;
2723*0e209d39SAndroid Build Coastguard Worker }
2724*0e209d39SAndroid Build Coastguard Worker break;
2725*0e209d39SAndroid Build Coastguard Worker default:
2726*0e209d39SAndroid Build Coastguard Worker /* we should never get here */
2727*0e209d39SAndroid Build Coastguard Worker UPRV_UNREACHABLE_EXIT;
2728*0e209d39SAndroid Build Coastguard Worker }
2729*0e209d39SAndroid Build Coastguard Worker /*
2730*0e209d39SAndroid Build Coastguard Worker * If there are no external levels specified and there
2731*0e209d39SAndroid Build Coastguard Worker * are no significant explicit level codes in the text,
2732*0e209d39SAndroid Build Coastguard Worker * then we can treat the entire paragraph as one run.
2733*0e209d39SAndroid Build Coastguard Worker * Otherwise, we need to perform the following rules on runs of
2734*0e209d39SAndroid Build Coastguard Worker * the text with the same embedding levels. (X10)
2735*0e209d39SAndroid Build Coastguard Worker * "Significant" explicit level codes are ones that actually
2736*0e209d39SAndroid Build Coastguard Worker * affect non-BN characters.
2737*0e209d39SAndroid Build Coastguard Worker * Examples for "insignificant" ones are empty embeddings
2738*0e209d39SAndroid Build Coastguard Worker * LRE-PDF, LRE-RLE-PDF-PDF, etc.
2739*0e209d39SAndroid Build Coastguard Worker */
2740*0e209d39SAndroid Build Coastguard Worker if(embeddingLevels==nullptr && pBiDi->paraCount<=1 &&
2741*0e209d39SAndroid Build Coastguard Worker !(pBiDi->flags&DIRPROP_FLAG_MULTI_RUNS)) {
2742*0e209d39SAndroid Build Coastguard Worker resolveImplicitLevels(pBiDi, 0, length,
2743*0e209d39SAndroid Build Coastguard Worker GET_LR_FROM_LEVEL(GET_PARALEVEL(pBiDi, 0)),
2744*0e209d39SAndroid Build Coastguard Worker GET_LR_FROM_LEVEL(GET_PARALEVEL(pBiDi, length-1)));
2745*0e209d39SAndroid Build Coastguard Worker } else {
2746*0e209d39SAndroid Build Coastguard Worker /* sor, eor: start and end types of same-level-run */
2747*0e209d39SAndroid Build Coastguard Worker UBiDiLevel *levels=pBiDi->levels;
2748*0e209d39SAndroid Build Coastguard Worker int32_t start, limit=0;
2749*0e209d39SAndroid Build Coastguard Worker UBiDiLevel level, nextLevel;
2750*0e209d39SAndroid Build Coastguard Worker DirProp sor, eor;
2751*0e209d39SAndroid Build Coastguard Worker
2752*0e209d39SAndroid Build Coastguard Worker /* determine the first sor and set eor to it because of the loop body (sor=eor there) */
2753*0e209d39SAndroid Build Coastguard Worker level=GET_PARALEVEL(pBiDi, 0);
2754*0e209d39SAndroid Build Coastguard Worker nextLevel=levels[0];
2755*0e209d39SAndroid Build Coastguard Worker if(level<nextLevel) {
2756*0e209d39SAndroid Build Coastguard Worker eor=GET_LR_FROM_LEVEL(nextLevel);
2757*0e209d39SAndroid Build Coastguard Worker } else {
2758*0e209d39SAndroid Build Coastguard Worker eor=GET_LR_FROM_LEVEL(level);
2759*0e209d39SAndroid Build Coastguard Worker }
2760*0e209d39SAndroid Build Coastguard Worker
2761*0e209d39SAndroid Build Coastguard Worker do {
2762*0e209d39SAndroid Build Coastguard Worker /* determine start and limit of the run (end points just behind the run) */
2763*0e209d39SAndroid Build Coastguard Worker
2764*0e209d39SAndroid Build Coastguard Worker /* the values for this run's start are the same as for the previous run's end */
2765*0e209d39SAndroid Build Coastguard Worker start=limit;
2766*0e209d39SAndroid Build Coastguard Worker level=nextLevel;
2767*0e209d39SAndroid Build Coastguard Worker if((start>0) && (dirProps[start-1]==B)) {
2768*0e209d39SAndroid Build Coastguard Worker /* except if this is a new paragraph, then set sor = para level */
2769*0e209d39SAndroid Build Coastguard Worker sor=GET_LR_FROM_LEVEL(GET_PARALEVEL(pBiDi, start));
2770*0e209d39SAndroid Build Coastguard Worker } else {
2771*0e209d39SAndroid Build Coastguard Worker sor=eor;
2772*0e209d39SAndroid Build Coastguard Worker }
2773*0e209d39SAndroid Build Coastguard Worker
2774*0e209d39SAndroid Build Coastguard Worker /* search for the limit of this run */
2775*0e209d39SAndroid Build Coastguard Worker while((++limit<length) &&
2776*0e209d39SAndroid Build Coastguard Worker ((levels[limit]==level) ||
2777*0e209d39SAndroid Build Coastguard Worker (DIRPROP_FLAG(dirProps[limit])&MASK_BN_EXPLICIT))) {}
2778*0e209d39SAndroid Build Coastguard Worker
2779*0e209d39SAndroid Build Coastguard Worker /* get the correct level of the next run */
2780*0e209d39SAndroid Build Coastguard Worker if(limit<length) {
2781*0e209d39SAndroid Build Coastguard Worker nextLevel=levels[limit];
2782*0e209d39SAndroid Build Coastguard Worker } else {
2783*0e209d39SAndroid Build Coastguard Worker nextLevel=GET_PARALEVEL(pBiDi, length-1);
2784*0e209d39SAndroid Build Coastguard Worker }
2785*0e209d39SAndroid Build Coastguard Worker
2786*0e209d39SAndroid Build Coastguard Worker /* determine eor from max(level, nextLevel); sor is last run's eor */
2787*0e209d39SAndroid Build Coastguard Worker if(NO_OVERRIDE(level)<NO_OVERRIDE(nextLevel)) {
2788*0e209d39SAndroid Build Coastguard Worker eor=GET_LR_FROM_LEVEL(nextLevel);
2789*0e209d39SAndroid Build Coastguard Worker } else {
2790*0e209d39SAndroid Build Coastguard Worker eor=GET_LR_FROM_LEVEL(level);
2791*0e209d39SAndroid Build Coastguard Worker }
2792*0e209d39SAndroid Build Coastguard Worker
2793*0e209d39SAndroid Build Coastguard Worker /* if the run consists of overridden directional types, then there
2794*0e209d39SAndroid Build Coastguard Worker are no implicit types to be resolved */
2795*0e209d39SAndroid Build Coastguard Worker if(!(level&UBIDI_LEVEL_OVERRIDE)) {
2796*0e209d39SAndroid Build Coastguard Worker resolveImplicitLevels(pBiDi, start, limit, sor, eor);
2797*0e209d39SAndroid Build Coastguard Worker } else {
2798*0e209d39SAndroid Build Coastguard Worker /* remove the UBIDI_LEVEL_OVERRIDE flags */
2799*0e209d39SAndroid Build Coastguard Worker do {
2800*0e209d39SAndroid Build Coastguard Worker levels[start++]&=~UBIDI_LEVEL_OVERRIDE;
2801*0e209d39SAndroid Build Coastguard Worker } while(start<limit);
2802*0e209d39SAndroid Build Coastguard Worker }
2803*0e209d39SAndroid Build Coastguard Worker } while(limit<length);
2804*0e209d39SAndroid Build Coastguard Worker }
2805*0e209d39SAndroid Build Coastguard Worker /* check if we got any memory shortage while adding insert points */
2806*0e209d39SAndroid Build Coastguard Worker if (U_FAILURE(pBiDi->insertPoints.errorCode))
2807*0e209d39SAndroid Build Coastguard Worker {
2808*0e209d39SAndroid Build Coastguard Worker *pErrorCode=pBiDi->insertPoints.errorCode;
2809*0e209d39SAndroid Build Coastguard Worker return;
2810*0e209d39SAndroid Build Coastguard Worker }
2811*0e209d39SAndroid Build Coastguard Worker /* reset the embedding levels for some non-graphic characters (L1), (X9) */
2812*0e209d39SAndroid Build Coastguard Worker adjustWSLevels(pBiDi);
2813*0e209d39SAndroid Build Coastguard Worker break;
2814*0e209d39SAndroid Build Coastguard Worker }
2815*0e209d39SAndroid Build Coastguard Worker /* add RLM for inverse Bidi with contextual orientation resolving
2816*0e209d39SAndroid Build Coastguard Worker * to RTL which would not round-trip otherwise
2817*0e209d39SAndroid Build Coastguard Worker */
2818*0e209d39SAndroid Build Coastguard Worker if((pBiDi->defaultParaLevel>0) &&
2819*0e209d39SAndroid Build Coastguard Worker (pBiDi->reorderingOptions & UBIDI_OPTION_INSERT_MARKS) &&
2820*0e209d39SAndroid Build Coastguard Worker ((pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_LIKE_DIRECT) ||
2821*0e209d39SAndroid Build Coastguard Worker (pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL))) {
2822*0e209d39SAndroid Build Coastguard Worker int32_t i, j, start, last;
2823*0e209d39SAndroid Build Coastguard Worker UBiDiLevel level;
2824*0e209d39SAndroid Build Coastguard Worker DirProp dirProp;
2825*0e209d39SAndroid Build Coastguard Worker for(i=0; i<pBiDi->paraCount; i++) {
2826*0e209d39SAndroid Build Coastguard Worker last=(pBiDi->paras[i].limit)-1;
2827*0e209d39SAndroid Build Coastguard Worker level= static_cast<UBiDiLevel>(pBiDi->paras[i].level);
2828*0e209d39SAndroid Build Coastguard Worker if(level==0)
2829*0e209d39SAndroid Build Coastguard Worker continue; /* LTR paragraph */
2830*0e209d39SAndroid Build Coastguard Worker start= i==0 ? 0 : pBiDi->paras[i-1].limit;
2831*0e209d39SAndroid Build Coastguard Worker for(j=last; j>=start; j--) {
2832*0e209d39SAndroid Build Coastguard Worker dirProp=dirProps[j];
2833*0e209d39SAndroid Build Coastguard Worker if(dirProp==L) {
2834*0e209d39SAndroid Build Coastguard Worker if(j<last) {
2835*0e209d39SAndroid Build Coastguard Worker while(dirProps[last]==B) {
2836*0e209d39SAndroid Build Coastguard Worker last--;
2837*0e209d39SAndroid Build Coastguard Worker }
2838*0e209d39SAndroid Build Coastguard Worker }
2839*0e209d39SAndroid Build Coastguard Worker addPoint(pBiDi, last, RLM_BEFORE);
2840*0e209d39SAndroid Build Coastguard Worker break;
2841*0e209d39SAndroid Build Coastguard Worker }
2842*0e209d39SAndroid Build Coastguard Worker if(DIRPROP_FLAG(dirProp) & MASK_R_AL) {
2843*0e209d39SAndroid Build Coastguard Worker break;
2844*0e209d39SAndroid Build Coastguard Worker }
2845*0e209d39SAndroid Build Coastguard Worker }
2846*0e209d39SAndroid Build Coastguard Worker }
2847*0e209d39SAndroid Build Coastguard Worker }
2848*0e209d39SAndroid Build Coastguard Worker
2849*0e209d39SAndroid Build Coastguard Worker if(pBiDi->reorderingOptions & UBIDI_OPTION_REMOVE_CONTROLS) {
2850*0e209d39SAndroid Build Coastguard Worker pBiDi->resultLength -= pBiDi->controlCount;
2851*0e209d39SAndroid Build Coastguard Worker } else {
2852*0e209d39SAndroid Build Coastguard Worker pBiDi->resultLength += pBiDi->insertPoints.size;
2853*0e209d39SAndroid Build Coastguard Worker }
2854*0e209d39SAndroid Build Coastguard Worker setParaSuccess(pBiDi); /* mark successful setPara */
2855*0e209d39SAndroid Build Coastguard Worker }
2856*0e209d39SAndroid Build Coastguard Worker
2857*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
ubidi_orderParagraphsLTR(UBiDi * pBiDi,UBool orderParagraphsLTR)2858*0e209d39SAndroid Build Coastguard Worker ubidi_orderParagraphsLTR(UBiDi *pBiDi, UBool orderParagraphsLTR) {
2859*0e209d39SAndroid Build Coastguard Worker if(pBiDi!=nullptr) {
2860*0e209d39SAndroid Build Coastguard Worker pBiDi->orderParagraphsLTR=orderParagraphsLTR;
2861*0e209d39SAndroid Build Coastguard Worker }
2862*0e209d39SAndroid Build Coastguard Worker }
2863*0e209d39SAndroid Build Coastguard Worker
2864*0e209d39SAndroid Build Coastguard Worker U_CAPI UBool U_EXPORT2
ubidi_isOrderParagraphsLTR(UBiDi * pBiDi)2865*0e209d39SAndroid Build Coastguard Worker ubidi_isOrderParagraphsLTR(UBiDi *pBiDi) {
2866*0e209d39SAndroid Build Coastguard Worker if(pBiDi!=nullptr) {
2867*0e209d39SAndroid Build Coastguard Worker return pBiDi->orderParagraphsLTR;
2868*0e209d39SAndroid Build Coastguard Worker } else {
2869*0e209d39SAndroid Build Coastguard Worker return false;
2870*0e209d39SAndroid Build Coastguard Worker }
2871*0e209d39SAndroid Build Coastguard Worker }
2872*0e209d39SAndroid Build Coastguard Worker
2873*0e209d39SAndroid Build Coastguard Worker U_CAPI UBiDiDirection U_EXPORT2
ubidi_getDirection(const UBiDi * pBiDi)2874*0e209d39SAndroid Build Coastguard Worker ubidi_getDirection(const UBiDi *pBiDi) {
2875*0e209d39SAndroid Build Coastguard Worker if(IS_VALID_PARA_OR_LINE(pBiDi)) {
2876*0e209d39SAndroid Build Coastguard Worker return pBiDi->direction;
2877*0e209d39SAndroid Build Coastguard Worker } else {
2878*0e209d39SAndroid Build Coastguard Worker return UBIDI_LTR;
2879*0e209d39SAndroid Build Coastguard Worker }
2880*0e209d39SAndroid Build Coastguard Worker }
2881*0e209d39SAndroid Build Coastguard Worker
2882*0e209d39SAndroid Build Coastguard Worker U_CAPI const char16_t * U_EXPORT2
ubidi_getText(const UBiDi * pBiDi)2883*0e209d39SAndroid Build Coastguard Worker ubidi_getText(const UBiDi *pBiDi) {
2884*0e209d39SAndroid Build Coastguard Worker if(IS_VALID_PARA_OR_LINE(pBiDi)) {
2885*0e209d39SAndroid Build Coastguard Worker return pBiDi->text;
2886*0e209d39SAndroid Build Coastguard Worker } else {
2887*0e209d39SAndroid Build Coastguard Worker return nullptr;
2888*0e209d39SAndroid Build Coastguard Worker }
2889*0e209d39SAndroid Build Coastguard Worker }
2890*0e209d39SAndroid Build Coastguard Worker
2891*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
ubidi_getLength(const UBiDi * pBiDi)2892*0e209d39SAndroid Build Coastguard Worker ubidi_getLength(const UBiDi *pBiDi) {
2893*0e209d39SAndroid Build Coastguard Worker if(IS_VALID_PARA_OR_LINE(pBiDi)) {
2894*0e209d39SAndroid Build Coastguard Worker return pBiDi->originalLength;
2895*0e209d39SAndroid Build Coastguard Worker } else {
2896*0e209d39SAndroid Build Coastguard Worker return 0;
2897*0e209d39SAndroid Build Coastguard Worker }
2898*0e209d39SAndroid Build Coastguard Worker }
2899*0e209d39SAndroid Build Coastguard Worker
2900*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
ubidi_getProcessedLength(const UBiDi * pBiDi)2901*0e209d39SAndroid Build Coastguard Worker ubidi_getProcessedLength(const UBiDi *pBiDi) {
2902*0e209d39SAndroid Build Coastguard Worker if(IS_VALID_PARA_OR_LINE(pBiDi)) {
2903*0e209d39SAndroid Build Coastguard Worker return pBiDi->length;
2904*0e209d39SAndroid Build Coastguard Worker } else {
2905*0e209d39SAndroid Build Coastguard Worker return 0;
2906*0e209d39SAndroid Build Coastguard Worker }
2907*0e209d39SAndroid Build Coastguard Worker }
2908*0e209d39SAndroid Build Coastguard Worker
2909*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
ubidi_getResultLength(const UBiDi * pBiDi)2910*0e209d39SAndroid Build Coastguard Worker ubidi_getResultLength(const UBiDi *pBiDi) {
2911*0e209d39SAndroid Build Coastguard Worker if(IS_VALID_PARA_OR_LINE(pBiDi)) {
2912*0e209d39SAndroid Build Coastguard Worker return pBiDi->resultLength;
2913*0e209d39SAndroid Build Coastguard Worker } else {
2914*0e209d39SAndroid Build Coastguard Worker return 0;
2915*0e209d39SAndroid Build Coastguard Worker }
2916*0e209d39SAndroid Build Coastguard Worker }
2917*0e209d39SAndroid Build Coastguard Worker
2918*0e209d39SAndroid Build Coastguard Worker /* paragraphs API functions ------------------------------------------------- */
2919*0e209d39SAndroid Build Coastguard Worker
2920*0e209d39SAndroid Build Coastguard Worker U_CAPI UBiDiLevel U_EXPORT2
ubidi_getParaLevel(const UBiDi * pBiDi)2921*0e209d39SAndroid Build Coastguard Worker ubidi_getParaLevel(const UBiDi *pBiDi) {
2922*0e209d39SAndroid Build Coastguard Worker if(IS_VALID_PARA_OR_LINE(pBiDi)) {
2923*0e209d39SAndroid Build Coastguard Worker return pBiDi->paraLevel;
2924*0e209d39SAndroid Build Coastguard Worker } else {
2925*0e209d39SAndroid Build Coastguard Worker return 0;
2926*0e209d39SAndroid Build Coastguard Worker }
2927*0e209d39SAndroid Build Coastguard Worker }
2928*0e209d39SAndroid Build Coastguard Worker
2929*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
ubidi_countParagraphs(UBiDi * pBiDi)2930*0e209d39SAndroid Build Coastguard Worker ubidi_countParagraphs(UBiDi *pBiDi) {
2931*0e209d39SAndroid Build Coastguard Worker if(!IS_VALID_PARA_OR_LINE(pBiDi)) {
2932*0e209d39SAndroid Build Coastguard Worker return 0;
2933*0e209d39SAndroid Build Coastguard Worker } else {
2934*0e209d39SAndroid Build Coastguard Worker return pBiDi->paraCount;
2935*0e209d39SAndroid Build Coastguard Worker }
2936*0e209d39SAndroid Build Coastguard Worker }
2937*0e209d39SAndroid Build Coastguard Worker
2938*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
ubidi_getParagraphByIndex(const UBiDi * pBiDi,int32_t paraIndex,int32_t * pParaStart,int32_t * pParaLimit,UBiDiLevel * pParaLevel,UErrorCode * pErrorCode)2939*0e209d39SAndroid Build Coastguard Worker ubidi_getParagraphByIndex(const UBiDi *pBiDi, int32_t paraIndex,
2940*0e209d39SAndroid Build Coastguard Worker int32_t *pParaStart, int32_t *pParaLimit,
2941*0e209d39SAndroid Build Coastguard Worker UBiDiLevel *pParaLevel, UErrorCode *pErrorCode) {
2942*0e209d39SAndroid Build Coastguard Worker int32_t paraStart;
2943*0e209d39SAndroid Build Coastguard Worker
2944*0e209d39SAndroid Build Coastguard Worker /* check the argument values */
2945*0e209d39SAndroid Build Coastguard Worker RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode);
2946*0e209d39SAndroid Build Coastguard Worker RETURN_VOID_IF_NOT_VALID_PARA_OR_LINE(pBiDi, *pErrorCode);
2947*0e209d39SAndroid Build Coastguard Worker RETURN_VOID_IF_BAD_RANGE(paraIndex, 0, pBiDi->paraCount, *pErrorCode);
2948*0e209d39SAndroid Build Coastguard Worker
2949*0e209d39SAndroid Build Coastguard Worker pBiDi=pBiDi->pParaBiDi; /* get Para object if Line object */
2950*0e209d39SAndroid Build Coastguard Worker if(paraIndex) {
2951*0e209d39SAndroid Build Coastguard Worker paraStart=pBiDi->paras[paraIndex-1].limit;
2952*0e209d39SAndroid Build Coastguard Worker } else {
2953*0e209d39SAndroid Build Coastguard Worker paraStart=0;
2954*0e209d39SAndroid Build Coastguard Worker }
2955*0e209d39SAndroid Build Coastguard Worker if(pParaStart!=nullptr) {
2956*0e209d39SAndroid Build Coastguard Worker *pParaStart=paraStart;
2957*0e209d39SAndroid Build Coastguard Worker }
2958*0e209d39SAndroid Build Coastguard Worker if(pParaLimit!=nullptr) {
2959*0e209d39SAndroid Build Coastguard Worker *pParaLimit=pBiDi->paras[paraIndex].limit;
2960*0e209d39SAndroid Build Coastguard Worker }
2961*0e209d39SAndroid Build Coastguard Worker if(pParaLevel!=nullptr) {
2962*0e209d39SAndroid Build Coastguard Worker *pParaLevel=GET_PARALEVEL(pBiDi, paraStart);
2963*0e209d39SAndroid Build Coastguard Worker }
2964*0e209d39SAndroid Build Coastguard Worker }
2965*0e209d39SAndroid Build Coastguard Worker
2966*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
ubidi_getParagraph(const UBiDi * pBiDi,int32_t charIndex,int32_t * pParaStart,int32_t * pParaLimit,UBiDiLevel * pParaLevel,UErrorCode * pErrorCode)2967*0e209d39SAndroid Build Coastguard Worker ubidi_getParagraph(const UBiDi *pBiDi, int32_t charIndex,
2968*0e209d39SAndroid Build Coastguard Worker int32_t *pParaStart, int32_t *pParaLimit,
2969*0e209d39SAndroid Build Coastguard Worker UBiDiLevel *pParaLevel, UErrorCode *pErrorCode) {
2970*0e209d39SAndroid Build Coastguard Worker int32_t paraIndex;
2971*0e209d39SAndroid Build Coastguard Worker
2972*0e209d39SAndroid Build Coastguard Worker /* check the argument values */
2973*0e209d39SAndroid Build Coastguard Worker /* pErrorCode will be checked by the call to ubidi_getParagraphByIndex */
2974*0e209d39SAndroid Build Coastguard Worker RETURN_IF_NULL_OR_FAILING_ERRCODE(pErrorCode, -1);
2975*0e209d39SAndroid Build Coastguard Worker RETURN_IF_NOT_VALID_PARA_OR_LINE(pBiDi, *pErrorCode, -1);
2976*0e209d39SAndroid Build Coastguard Worker pBiDi=pBiDi->pParaBiDi; /* get Para object if Line object */
2977*0e209d39SAndroid Build Coastguard Worker RETURN_IF_BAD_RANGE(charIndex, 0, pBiDi->length, *pErrorCode, -1);
2978*0e209d39SAndroid Build Coastguard Worker
2979*0e209d39SAndroid Build Coastguard Worker for(paraIndex=0; charIndex>=pBiDi->paras[paraIndex].limit; paraIndex++);
2980*0e209d39SAndroid Build Coastguard Worker ubidi_getParagraphByIndex(pBiDi, paraIndex, pParaStart, pParaLimit, pParaLevel, pErrorCode);
2981*0e209d39SAndroid Build Coastguard Worker return paraIndex;
2982*0e209d39SAndroid Build Coastguard Worker }
2983*0e209d39SAndroid Build Coastguard Worker
2984*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
ubidi_setClassCallback(UBiDi * pBiDi,UBiDiClassCallback * newFn,const void * newContext,UBiDiClassCallback ** oldFn,const void ** oldContext,UErrorCode * pErrorCode)2985*0e209d39SAndroid Build Coastguard Worker ubidi_setClassCallback(UBiDi *pBiDi, UBiDiClassCallback *newFn,
2986*0e209d39SAndroid Build Coastguard Worker const void *newContext, UBiDiClassCallback **oldFn,
2987*0e209d39SAndroid Build Coastguard Worker const void **oldContext, UErrorCode *pErrorCode)
2988*0e209d39SAndroid Build Coastguard Worker {
2989*0e209d39SAndroid Build Coastguard Worker RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode);
2990*0e209d39SAndroid Build Coastguard Worker if(pBiDi==nullptr) {
2991*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
2992*0e209d39SAndroid Build Coastguard Worker return;
2993*0e209d39SAndroid Build Coastguard Worker }
2994*0e209d39SAndroid Build Coastguard Worker if( oldFn )
2995*0e209d39SAndroid Build Coastguard Worker {
2996*0e209d39SAndroid Build Coastguard Worker *oldFn = pBiDi->fnClassCallback;
2997*0e209d39SAndroid Build Coastguard Worker }
2998*0e209d39SAndroid Build Coastguard Worker if( oldContext )
2999*0e209d39SAndroid Build Coastguard Worker {
3000*0e209d39SAndroid Build Coastguard Worker *oldContext = pBiDi->coClassCallback;
3001*0e209d39SAndroid Build Coastguard Worker }
3002*0e209d39SAndroid Build Coastguard Worker pBiDi->fnClassCallback = newFn;
3003*0e209d39SAndroid Build Coastguard Worker pBiDi->coClassCallback = newContext;
3004*0e209d39SAndroid Build Coastguard Worker }
3005*0e209d39SAndroid Build Coastguard Worker
3006*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
ubidi_getClassCallback(UBiDi * pBiDi,UBiDiClassCallback ** fn,const void ** context)3007*0e209d39SAndroid Build Coastguard Worker ubidi_getClassCallback(UBiDi *pBiDi, UBiDiClassCallback **fn, const void **context)
3008*0e209d39SAndroid Build Coastguard Worker {
3009*0e209d39SAndroid Build Coastguard Worker if(pBiDi==nullptr) {
3010*0e209d39SAndroid Build Coastguard Worker return;
3011*0e209d39SAndroid Build Coastguard Worker }
3012*0e209d39SAndroid Build Coastguard Worker if( fn )
3013*0e209d39SAndroid Build Coastguard Worker {
3014*0e209d39SAndroid Build Coastguard Worker *fn = pBiDi->fnClassCallback;
3015*0e209d39SAndroid Build Coastguard Worker }
3016*0e209d39SAndroid Build Coastguard Worker if( context )
3017*0e209d39SAndroid Build Coastguard Worker {
3018*0e209d39SAndroid Build Coastguard Worker *context = pBiDi->coClassCallback;
3019*0e209d39SAndroid Build Coastguard Worker }
3020*0e209d39SAndroid Build Coastguard Worker }
3021*0e209d39SAndroid Build Coastguard Worker
3022*0e209d39SAndroid Build Coastguard Worker U_CAPI UCharDirection U_EXPORT2
ubidi_getCustomizedClass(UBiDi * pBiDi,UChar32 c)3023*0e209d39SAndroid Build Coastguard Worker ubidi_getCustomizedClass(UBiDi *pBiDi, UChar32 c)
3024*0e209d39SAndroid Build Coastguard Worker {
3025*0e209d39SAndroid Build Coastguard Worker UCharDirection dir;
3026*0e209d39SAndroid Build Coastguard Worker
3027*0e209d39SAndroid Build Coastguard Worker if( pBiDi->fnClassCallback == nullptr ||
3028*0e209d39SAndroid Build Coastguard Worker (dir = (*pBiDi->fnClassCallback)(pBiDi->coClassCallback, c)) == U_BIDI_CLASS_DEFAULT )
3029*0e209d39SAndroid Build Coastguard Worker {
3030*0e209d39SAndroid Build Coastguard Worker dir = ubidi_getClass(c);
3031*0e209d39SAndroid Build Coastguard Worker }
3032*0e209d39SAndroid Build Coastguard Worker if(dir >= U_CHAR_DIRECTION_COUNT) {
3033*0e209d39SAndroid Build Coastguard Worker dir = (UCharDirection)ON;
3034*0e209d39SAndroid Build Coastguard Worker }
3035*0e209d39SAndroid Build Coastguard Worker return dir;
3036*0e209d39SAndroid Build Coastguard Worker }
3037