/* If the line is terminated by a block separator, all preceding WS etc... arealreadysettoparagraphlevel. SettingtrailingWSStarttopBidi->lengthwillavoidchangingthe levelofBcharsfrom0toparaLevelinubidi_getLevelswhen orderParagraphsLTR==true.
*/
if(dirProps[start-1]==B) {
pBiDi->trailingWSStart=start; /* currently == pBiDi->length */ return;
} /* go backwards across all WS, BN, explicit codes */ while(start>0 && DIRPROP_FLAG(dirProps[start-1])&MASK_WS) {
--start;
}
/* if the WS run can be merged with the previous run then do so here */ while(start>0 && levels[start-1]==paraLevel) {
--start;
}
/* recalculate pLineBiDi->direction */
if(trailingWSStart==0) { /* all levels are at paraLevel */
pLineBiDi->direction=(UBiDiDirection)(pLineBiDi->paraLevel&1);
} else { /* get the level of the first character */
level=(UBiDiLevel)(levels[0]&1);
/* if there is anything of a different level, then the line is mixed */
if(trailingWSStart<length && (pLineBiDi->paraLevel&1)!=level) { /* the trailing WS is at paraLevel, which differs from levels[0] */
pLineBiDi->direction=UBIDI_MIXED;
} else { /* see if levels[1..trailingWSStart-1] have the same direction as levels[0] and paraLevel */
i=1;
for(;;) {
if(i==trailingWSStart) { /* the direction values match those in level */
pLineBiDi->direction=(UBiDiDirection)level; break;
} else if((levels[i]&1)!=level) {
pLineBiDi->direction=UBIDI_MIXED; break;
}
++i;
}
}
}
switch(pLineBiDi->direction) { case UBIDI_LTR: /* make sure paraLevel is even */
pLineBiDi->paraLevel=(UBiDiLevel)((pLineBiDi->paraLevel+1)&~1);
/* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */
pLineBiDi->trailingWSStart=0; break; case UBIDI_RTL: /* make sure paraLevel is odd */
pLineBiDi->paraLevel|=1;
/* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */
pLineBiDi->trailingWSStart=0; break; default: break;
}
}
pLineBiDi->pParaBiDi=pParaBiDi; /* mark successful setLine */
}
U_CAPI UBiDiLevel U_EXPORT2
ubidi_getLevelAt(const UBiDi *pBiDi, int32_t charIndex) { /* return paraLevel if in the trailing WS run, otherwise the real level */
if(!IS_VALID_PARA_OR_LINE(pBiDi) || charIndex<0 || pBiDi->length<=charIndex) { return0;
} else if(pBiDi->direction!=UBIDI_MIXED || charIndex>=pBiDi->trailingWSStart) { return GET_PARALEVEL(pBiDi, charIndex);
} else { return pBiDi->levels[charIndex];
}
}
if(start>0 && levels!=pBiDi->levels) {
uprv_memcpy(levels, pBiDi->levels, start);
} /* pBiDi->paraLevel is ok even if contextual multiple paragraphs,
since pBidi is a line object */
uprv_memset(levels+start, pBiDi->paraLevel, length-start);
/* this new levels array is set for the line and reflects the WS run */
pBiDi->trailingWSStart=length; return pBiDi->levels=levels;
} else { /* out of memory */
*pErrorCode=U_MEMORY_ALLOCATION_ERROR; return nullptr;
}
}
errorCode=U_ZERO_ERROR;
RETURN_VOID_IF_BAD_RANGE(logicalPosition, 0, pBiDi->length, errorCode); /* ubidi_countRuns will check VALID_PARA_OR_LINE */
runCount=ubidi_countRuns((UBiDi *)pBiDi, &errorCode);
if(U_FAILURE(errorCode)) { return;
} /* this is done based on runs rather than on levels since levels have aspecialinterpretationwhenUBIDI_REORDER_RUNS_ONLY
*/
visualStart=logicalLimit=0;
iRun=pBiDi->runs[0];
/* in trivial cases there is only one trivial run; called by ubidi_getRuns() */ staticvoid
getSingleRun(UBiDi *pBiDi, UBiDiLevel level) { /* simple, single-run case */
pBiDi->runs=pBiDi->simpleRuns;
pBiDi->runCount=1;
/* fill and reorder the single run */
pBiDi->runs[0].logicalStart=MAKE_INDEX_ODD_PAIR(0, level);
pBiDi->runs[0].visualLimit=pBiDi->length;
pBiDi->runs[0].insertRemove=0;
}
/* reorder the runs array (L2) ---------------------------------------------- */
/* do not include the WS run at paraLevel<=old minLevel except in the simple loop */
if(pBiDi->trailingWSStart<pBiDi->length) {
--runCount;
}
while(--maxLevel>=minLevel) {
firstRun=0;
/* loop for all sequences of runs */
for(;;) { /* look for a sequence of runs that are all at >=maxLevel */ /* look for the first run of such a sequence */ while(firstRun<runCount && levels[runs[firstRun].logicalStart]<maxLevel) {
++firstRun;
}
if(firstRun>=runCount) { break; /* no more such runs */
}
/* look for the limit run of such a sequence (the run behind it) */
for(limitRun=firstRun; ++limitRun<runCount && levels[runs[limitRun].logicalStart]>=maxLevel;) {}
/* Swap the entire sequence of runs from firstRun to limitRun-1. */
endRun=limitRun-1; while(firstRun<endRun) {
tempRun = runs[firstRun];
runs[firstRun]=runs[endRun];
runs[endRun]=tempRun;
++firstRun;
--endRun;
}
if(limitRun==runCount) { break; /* no more such runs */
} else {
firstRun=limitRun+1;
}
}
}
/* now do maxLevel==old minLevel (==odd!), see above */
if(!(minLevel&1)) {
firstRun=0;
/* include the trailing WS run in this complete reordering */
if(pBiDi->trailingWSStart==pBiDi->length) {
--runCount;
}
/* Swap the entire sequence of all runs. (endRun==runCount) */ while(firstRun<runCount) {
tempRun=runs[firstRun];
runs[firstRun]=runs[runCount];
runs[runCount]=tempRun;
++firstRun;
--runCount;
}
}
}
/* compute the runs array --------------------------------------------------- */
static int32_t getRunFromLogicalIndex(UBiDi *pBiDi, int32_t logicalIndex) {
Run *runs=pBiDi->runs;
int32_t runCount=pBiDi->runCount, visualStart=0, i, length, logicalStart;
for(i=0; i<runCount; i++) {
length=runs[i].visualLimit-visualStart;
logicalStart=GET_INDEX(runs[i].logicalStart);
if((logicalIndex>=logicalStart) && (logicalIndex<(logicalStart+length))) { return i;
}
visualStart+=length;
} /* we should never get here */
UPRV_UNREACHABLE_EXIT;
}
if(pBiDi->direction!=UBIDI_MIXED) { /* simple, single-run case - this covers length==0 */ /* pBiDi->paraLevel is ok even for contextual multiple paragraphs */
getSingleRun(pBiDi, pBiDi->paraLevel);
} else/* UBIDI_MIXED, length>0 */ { /* mixed directionality */
int32_t length=pBiDi->length, limit;
UBiDiLevel *levels=pBiDi->levels;
int32_t i, runCount;
UBiDiLevel level=UBIDI_DEFAULT_LTR; /* initialize with no valid level */ /* *IfthereareWScharactersattheendoftheline *andtherunprecedingthemhasaleveldifferentfrom *paraLevel,thentheywillformtheirownrunatparaLevel(L1). *Countthemseparately. *Weneedsomespecialtreatmentforthisinordertonot *modifythelevelsarraywhichalineUBiDiobjectshares *withitsparagraphparentanditsotherlinesiblings. *Inotherwords,forthetrailingWS,itmaybe *levels[]!=paraLevelbutwehavetotreatitlikeitwereso.
*/
limit=pBiDi->trailingWSStart; /* count the runs, there is at least one non-WS run, and limit>0 */
runCount=0;
for(i=0; i<limit; ++i) { /* increment runCount at the start of each run */
if(levels[i]!=level) {
++runCount;
level=levels[i];
}
}
/* *Wedon'tneedtoseeifthelastruncanbemergedwithatrailing *WSrunbecausesetTrailingWSStart()wouldhavedonethat.
*/
if(runCount==1 && limit==length) { /* There is only one non-WS run and no trailing WS-run. */
getSingleRun(pBiDi, levels[0]);
} else/* runCount>1 || limit<length */ { /* allocate and set the runs */
Run *runs;
int32_t runIndex, start;
UBiDiLevel minLevel=UBIDI_MAX_EXPLICIT_LEVEL+1, maxLevel=0;
/* now, count a (non-mergeable) WS run */
if(limit<length) {
++runCount;
}
/* set the runs */ /* FOOD FOR THOUGHT: this could be optimized, e.g.: *464->444,484->444,575->555,595->555 *However,thatwouldtakelonger.Checkalsohowitwould *interactwithBiDicontrolremovalandinsertingMarks.
*/
runIndex=0;
/* search for the run limits and initialize visualLimit values with the run lengths */
i=0; do { /* prepare this run */
start=i;
level=levels[i];
if(level<minLevel) {
minLevel=level;
}
if(level>maxLevel) {
maxLevel=level;
}
/* look for the run limit */ while(++i<limit && levels[i]==level) {}
/* i is another run limit */
runs[runIndex].logicalStart=start;
runs[runIndex].visualLimit=i-start;
runs[runIndex].insertRemove=0;
++runIndex;
} while(i<limit);
if(limit<length) { /* there is a separate WS run */
runs[runIndex].logicalStart=limit;
runs[runIndex].visualLimit=length-limit; /* For the trailing WS run, pBiDi->paraLevel is ok even
if contextual multiple paragraphs. */
if(pBiDi->paraLevel<minLevel) {
minLevel=pBiDi->paraLevel;
}
}
/* set the object fields */
pBiDi->runs=runs;
pBiDi->runCount=runCount;
reorderLine(pBiDi, minLevel, maxLevel);
/* now add the direction flags and adjust the visualLimit's to be just that */ /* this loop will also handle the trailing WS run */
limit=0;
for(i=0; i<runCount; ++i) {
ADD_ODD_BIT_FROM_LEVEL(runs[i].logicalStart, levels[runs[i].logicalStart]);
limit+=runs[i].visualLimit;
runs[i].visualLimit=limit;
}
/* Set the "odd" bit for the trailing WS run. */ /* For a RTL paragraph, it will be the *first* run in visual order. */ /* For the trailing WS run, pBiDi->paraLevel is ok even if
contextual multiple paragraphs. */
if(runIndex<runCount) {
int32_t trailingRun = ((pBiDi->paraLevel & 1) != 0)? 0 : runIndex;
/* reorder only down to the lowest odd level */
minLevel|=1;
/* loop maxLevel..minLevel */ do {
start=0;
/* loop for all sequences of levels to reorder at the current maxLevel */
for(;;) { /* look for a sequence of levels that are all at >=maxLevel */ /* look for the first index of such a sequence */ while(start<length && levels[start]<maxLevel) {
++start;
}
if(start>=length) { break; /* no more such sequences */
}
/* look for the limit of such a sequence (the index behind it) */
for(limit=start; ++limit<length && levels[limit]>=maxLevel;) {}
/* reorder only down to the lowest odd level */
minLevel|=1;
/* loop maxLevel..minLevel */ do {
start=0;
/* loop for all sequences of levels to reorder at the current maxLevel */
for(;;) { /* look for a sequence of levels that are all at >=maxLevel */ /* look for the first index of such a sequence */ while(start<length && levels[start]<maxLevel) {
++start;
}
if(start>=length) { break; /* no more such runs */
}
/* look for the limit of such a sequence (the index behind it) */
for(limit=start; ++limit<length && levels[limit]>=maxLevel;) {}
/* we can do the trivial cases without the runs array */ switch(pBiDi->direction) { case UBIDI_LTR:
visualIndex=logicalIndex; break; case UBIDI_RTL:
visualIndex=pBiDi->length-logicalIndex-1; break; default:
if(!ubidi_getRuns(pBiDi, pErrorCode)) {
*pErrorCode=U_MEMORY_ALLOCATION_ERROR; return -1;
} else {
Run *runs=pBiDi->runs;
int32_t i, visualStart=0, offset, length;
/* linear search for the run, search on the visual runs */
for(i=0; i<pBiDi->runCount; ++i) {
length=runs[i].visualLimit-visualStart;
offset=logicalIndex-GET_INDEX(runs[i].logicalStart);
if(offset>=0 && offset<length) {
if(IS_EVEN_RUN(runs[i].logicalStart)) { /* LTR */
visualIndex=visualStart+offset;
} else { /* RTL */
visualIndex=visualStart+length-offset-1;
} break; /* exit for loop */
}
visualStart+=length;
}
if(i>=pBiDi->runCount) { return UBIDI_MAP_NOWHERE;
}
}
}
if(pBiDi->insertPoints.size>0) { /* add the number of added marks until the calculated visual index */
Run *runs=pBiDi->runs;
int32_t i, length, insertRemove;
int32_t visualStart=0, markFound=0;
for(i=0; ; i++, visualStart+=length) {
length=runs[i].visualLimit-visualStart;
insertRemove=runs[i].insertRemove;
if(insertRemove & (LRM_BEFORE|RLM_BEFORE)) {
markFound++;
} /* is it the run containing the visual index? */
if(visualIndex<runs[i].visualLimit) { return visualIndex+markFound;
}
if(insertRemove & (LRM_AFTER|RLM_AFTER)) {
markFound++;
}
}
} else if(pBiDi->controlCount>0) { /* subtract the number of controls until the calculated visual index */
Run *runs=pBiDi->runs;
int32_t i, j, start, limit, length, insertRemove;
int32_t visualStart=0, controlFound=0;
char16_t uchar=pBiDi->text[logicalIndex]; /* is the logical index pointing to a control ? */
if(IS_BIDI_CONTROL_CHAR(uchar)) { return UBIDI_MAP_NOWHERE;
} /* loop on runs */
for(i=0; ; i++, visualStart+=length) {
length=runs[i].visualLimit-visualStart;
insertRemove=runs[i].insertRemove; /* calculated visual index is beyond this run? */
if(visualIndex>=runs[i].visualLimit) {
controlFound-=insertRemove; continue;
} /* calculated visual index must be within current run */
if(insertRemove==0) { return visualIndex-controlFound;
}
if(IS_EVEN_RUN(runs[i].logicalStart)) { /* LTR: check from run start to logical index */
start=runs[i].logicalStart;
limit=logicalIndex;
} else { /* RTL: check from logical index to run end */
start=logicalIndex+1;
limit=GET_INDEX(runs[i].logicalStart)+length;
}
for(j=start; j<limit; j++) {
uchar=pBiDi->text[j];
if(IS_BIDI_CONTROL_CHAR(uchar)) {
controlFound++;
}
} return visualIndex-controlFound;
}
}
return visualIndex;
}
U_CAPI int32_t U_EXPORT2
ubidi_getLogicalIndex(UBiDi *pBiDi, int32_t visualIndex, UErrorCode *pErrorCode) {
Run *runs;
int32_t i, runCount, start;
RETURN_IF_NULL_OR_FAILING_ERRCODE(pErrorCode, -1);
RETURN_IF_NOT_VALID_PARA_OR_LINE(pBiDi, *pErrorCode, -1);
RETURN_IF_BAD_RANGE(visualIndex, 0, pBiDi->resultLength, *pErrorCode, -1); /* we can do the trivial cases without the runs array */
if(pBiDi->insertPoints.size==0 && pBiDi->controlCount==0) {
if(pBiDi->direction==UBIDI_LTR) { return visualIndex;
} else if(pBiDi->direction==UBIDI_RTL) { return pBiDi->length-visualIndex-1;
}
}
if(!ubidi_getRuns(pBiDi, pErrorCode)) {
*pErrorCode=U_MEMORY_ALLOCATION_ERROR; return -1;
}
runs=pBiDi->runs;
runCount=pBiDi->runCount;
if(pBiDi->insertPoints.size>0) { /* handle inserted LRM/RLM */
int32_t markFound=0, insertRemove;
int32_t visualStart=0, length;
runs=pBiDi->runs; /* subtract number of marks until visual index */
for(i=0; ; i++, visualStart+=length) {
length=runs[i].visualLimit-visualStart;
insertRemove=runs[i].insertRemove;
if(insertRemove&(LRM_BEFORE|RLM_BEFORE)) {
if(visualIndex<=(visualStart+markFound)) { return UBIDI_MAP_NOWHERE;
}
markFound++;
} /* is adjusted visual index within this run? */
if(visualIndex<(runs[i].visualLimit+markFound)) {
visualIndex-=markFound; break;
}
if(insertRemove&(LRM_AFTER|RLM_AFTER)) {
if(visualIndex==(visualStart+length+markFound)) { return UBIDI_MAP_NOWHERE;
}
markFound++;
}
}
} else if(pBiDi->controlCount>0) { /* handle removed BiDi control characters */
int32_t controlFound=0, insertRemove, length;
int32_t logicalStart, logicalEnd, visualStart=0, j, k;
char16_t uchar;
UBool evenRun; /* add number of controls until visual index */
for(i=0; ; i++, visualStart+=length) {
length=runs[i].visualLimit-visualStart;
insertRemove=runs[i].insertRemove; /* is adjusted visual index beyond current run? */
if(visualIndex>=(runs[i].visualLimit-controlFound+insertRemove)) {
controlFound-=insertRemove; continue;
} /* adjusted visual index is within current run */
if(insertRemove==0) {
visualIndex+=controlFound; break;
} /* count non-control chars until visualIndex */
logicalStart=runs[i].logicalStart;
evenRun=IS_EVEN_RUN(logicalStart);
REMOVE_ODD_BIT(logicalStart);
logicalEnd=logicalStart+length-1;
for(j=0; j<length; j++) {
k= evenRun ? logicalStart+j : logicalEnd-j;
uchar=pBiDi->text[k];
if(IS_BIDI_CONTROL_CHAR(uchar)) {
controlFound++;
}
if((visualIndex+controlFound)==(visualStart+j)) { break;
}
}
visualIndex+=controlFound; break;
}
} /* handle all cases */
if(runCount<=10) { /* linear search for the run */
for(i=0; visualIndex>=runs[i].visualLimit; ++i) {}
} else { /* binary search for the run */
int32_t begin=0, limit=runCount;
/* the middle if() is guaranteed to find the run, we don't need a loop limit */
for(;;) {
i=(begin+limit)/2;
if(visualIndex>=runs[i].visualLimit) {
begin=i+1;
} else if(i==0 || visualIndex>=runs[i-1].visualLimit) { break;
} else {
limit=i;
}
}
}
start=runs[i].logicalStart;
if(IS_EVEN_RUN(start)) { /* LTR */ /* the offset in runs[i] is visualIndex-runs[i-1].visualLimit */
if(i>0) {
visualIndex-=runs[i-1].visualLimit;
} return start+visualIndex;
} else { /* RTL */ return GET_INDEX(start)+runs[i].visualLimit-visualIndex-1;
}
}
U_CAPI void U_EXPORT2
ubidi_getLogicalMap(UBiDi *pBiDi, int32_t *indexMap, UErrorCode *pErrorCode) {
RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode); /* ubidi_countRuns() checks for VALID_PARA_OR_LINE */
ubidi_countRuns(pBiDi, pErrorCode);
if(U_FAILURE(*pErrorCode)) { /* no op */
} else if(indexMap==nullptr) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
} else { /* fill a logical-to-visual index map using the runs[] */
int32_t visualStart, visualLimit, i, j, k;
int32_t logicalStart, logicalLimit;
Run *runs=pBiDi->runs;
if (pBiDi->length<=0) { return;
}
if (pBiDi->length>pBiDi->resultLength) {
uprv_memset(indexMap, 0xFF, pBiDi->length*sizeof(int32_t));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.