static PRBool IsDiscardable(PRUnichar ch, PRUint32* aFlags) { // Unlike IS_DISCARDABLE, we don't discard \r. \r will be ignored by gfxTextRun // and discarding it would force us to copy text in many cases of preformatted // text containing \r\n. if (ch == CH_SHY) { *aFlags |= nsTextFrameUtils::TEXT_HAS_SHY; return PR_TRUE; } if ((ch & 0xFF00) != 0x2000) { // Not a Bidi control character return PR_FALSE; } return IS_BIDI_CONTROL_CHAR(ch); }
// To save time we only do this when we really want to know, not during // every allocation void nsTextFragment::UpdateBidiFlag(const PRUnichar* aBuffer, PRUint32 aLength) { if (mState.mIs2b && !mState.mIsBidi) { const PRUnichar* cp = aBuffer; const PRUnichar* end = cp + aLength; while (cp < end) { PRUnichar ch1 = *cp++; PRUint32 utf32Char = ch1; if (NS_IS_HIGH_SURROGATE(ch1) && cp < end && NS_IS_LOW_SURROGATE(*cp)) { PRUnichar ch2 = *cp++; utf32Char = SURROGATE_TO_UCS4(ch1, ch2); } if (UTF32_CHAR_IS_BIDI(utf32Char) || IS_BIDI_CONTROL_CHAR(utf32Char)) { mState.mIsBidi = PR_TRUE; break; } } } }
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; UChar 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 int32_t U_EXPORT2 ubidi_getVisualIndex(UBiDi *pBiDi, int32_t logicalIndex, UErrorCode *pErrorCode) { int32_t visualIndex=UBIDI_MAP_NOWHERE; RETURN_IF_NULL_OR_FAILING_ERRCODE(pErrorCode, -1); RETURN_IF_NOT_VALID_PARA_OR_LINE(pBiDi, *pErrorCode, -1); RETURN_IF_BAD_RANGE(logicalIndex, 0, pBiDi->length, *pErrorCode, -1); /* 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; UChar 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; }
/* * Compute the runs array from the levels array. * After ubidi_getRuns() returns TRUE, runCount is guaranteed to be >0 * and the runs are reordered. * Odd-level runs have visualStart on their visual right edge and * they progress visually to the left. * If option UBIDI_OPTION_INSERT_MARKS is set, insertRemove will contain the * sum of appropriate LRM/RLM_BEFORE/AFTER flags. * If option UBIDI_OPTION_REMOVE_CONTROLS is set, insertRemove will contain the * negative number of BiDi control characters within this run. */ U_CFUNC UBool ubidi_getRuns(UBiDi *pBiDi, UErrorCode *pErrorCode) { /* * This method returns immediately if the runs are already set. This * includes the case of length==0 (handled in setPara).. */ if (pBiDi->runCount>=0) { return TRUE; } 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 */ /* * If there are WS characters at the end of the line * and the run preceding them has a level different from * paraLevel, then they will form their own run at paraLevel (L1). * Count them separately. * We need some special treatment for this in order to not * modify the levels array which a line UBiDi object shares * with its paragraph parent and its other line siblings. * In other words, for the trailing WS, it may be * levels[]!=paraLevel but we have to treat it like it were so. */ 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]; } } /* * We don't need to see if the last run can be merged with a trailing * WS run because setTrailingWSStart() would have done that. */ 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; } /* runCount>1 */ if(getRunsMemory(pBiDi, runCount)) { runs=pBiDi->runsMemory; } else { return FALSE; } /* set the runs */ /* FOOD FOR THOUGHT: this could be optimized, e.g.: * 464->444, 484->444, 575->555, 595->555 * However, that would take longer. Check also how it would * interact with BiDi control removal and inserting Marks. */ 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; ADD_ODD_BIT_FROM_LEVEL(runs[trailingRun].logicalStart, pBiDi->paraLevel); } } } /* handle insert LRM/RLM BEFORE/AFTER run */ if(pBiDi->insertPoints.size>0) { Point *point, *start=pBiDi->insertPoints.points, *limit=start+pBiDi->insertPoints.size; int32_t runIndex; for(point=start; point<limit; point++) { runIndex=getRunFromLogicalIndex(pBiDi, point->pos, pErrorCode); pBiDi->runs[runIndex].insertRemove|=point->flag; } } /* handle remove BiDi control characters */ if(pBiDi->controlCount>0) { int32_t runIndex; const UChar *start=pBiDi->text, *limit=start+pBiDi->length, *pu; for(pu=start; pu<limit; pu++) { if(IS_BIDI_CONTROL_CHAR(*pu)) { runIndex=getRunFromLogicalIndex(pBiDi, (int32_t)(pu-start), pErrorCode); pBiDi->runs[runIndex].insertRemove--; } } } return TRUE; }
U_CAPI void U_EXPORT2 ubidi_setLine(const UBiDi *pParaBiDi, int32_t start, int32_t limit, UBiDi *pLineBiDi, UErrorCode *pErrorCode) { int32_t length; /* check the argument values */ RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode); RETURN_VOID_IF_NOT_VALID_PARA(pParaBiDi, *pErrorCode); RETURN_VOID_IF_BAD_RANGE(start, 0, limit, *pErrorCode); RETURN_VOID_IF_BAD_RANGE(limit, 0, pParaBiDi->length+1, *pErrorCode); if(pLineBiDi==NULL) { *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return; } if(ubidi_getParagraph(pParaBiDi, start, NULL, NULL, NULL, pErrorCode) != ubidi_getParagraph(pParaBiDi, limit-1, NULL, NULL, NULL, pErrorCode)) { /* the line crosses a paragraph boundary */ *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return; } /* set the values in pLineBiDi from its pParaBiDi parent */ pLineBiDi->pParaBiDi=NULL; /* mark unfinished setLine */ pLineBiDi->text=pParaBiDi->text+start; length=pLineBiDi->length=limit-start; pLineBiDi->resultLength=pLineBiDi->originalLength=length; pLineBiDi->paraLevel=GET_PARALEVEL(pParaBiDi, start); pLineBiDi->paraCount=pParaBiDi->paraCount; pLineBiDi->runs=NULL; pLineBiDi->flags=0; pLineBiDi->reorderingMode=pParaBiDi->reorderingMode; pLineBiDi->reorderingOptions=pParaBiDi->reorderingOptions; pLineBiDi->controlCount=0; if(pParaBiDi->controlCount>0) { int32_t j; for(j=start; j<limit; j++) { if(IS_BIDI_CONTROL_CHAR(pParaBiDi->text[j])) { pLineBiDi->controlCount++; } } pLineBiDi->resultLength-=pLineBiDi->controlCount; } pLineBiDi->dirProps=pParaBiDi->dirProps+start; pLineBiDi->levels=pParaBiDi->levels+start; pLineBiDi->runCount=-1; if(pParaBiDi->direction!=UBIDI_MIXED) { /* the parent is already trivial */ pLineBiDi->direction=pParaBiDi->direction; /* * The parent's levels are all either * implicitly or explicitly ==paraLevel; * do the same here. */ if(pParaBiDi->trailingWSStart<=start) { pLineBiDi->trailingWSStart=0; } else if(pParaBiDi->trailingWSStart<limit) { pLineBiDi->trailingWSStart=pParaBiDi->trailingWSStart-start; } else { pLineBiDi->trailingWSStart=length; } } else { const UBiDiLevel *levels=pLineBiDi->levels; int32_t i, trailingWSStart; UBiDiLevel level; setTrailingWSStart(pLineBiDi); trailingWSStart=pLineBiDi->trailingWSStart; /* 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 */ return; }
U_CAPI void U_EXPORT2 ubidi_getVisualMap(UBiDi *pBiDi, int32_t *indexMap, UErrorCode *pErrorCode) { RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode); if(indexMap==NULL) { *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return; } /* ubidi_countRuns() checks for VALID_PARA_OR_LINE */ ubidi_countRuns(pBiDi, pErrorCode); if(U_SUCCESS(*pErrorCode)) { /* fill a visual-to-logical index map using the runs[] */ Run *runs=pBiDi->runs, *runsLimit=runs+pBiDi->runCount; int32_t logicalStart, visualStart, visualLimit, *pi=indexMap; if (pBiDi->resultLength<=0) { return; } visualStart=0; for(; runs<runsLimit; ++runs) { logicalStart=runs->logicalStart; visualLimit=runs->visualLimit; if(IS_EVEN_RUN(logicalStart)) { do { /* LTR */ *pi++ = logicalStart++; } while(++visualStart<visualLimit); } else { REMOVE_ODD_BIT(logicalStart); logicalStart+=visualLimit-visualStart; /* logicalLimit */ do { /* RTL */ *pi++ = --logicalStart; } while(++visualStart<visualLimit); } /* visualStart==visualLimit; */ } if(pBiDi->insertPoints.size>0) { int32_t markFound=0, runCount=pBiDi->runCount; int32_t insertRemove, i, j, k; runs=pBiDi->runs; /* count all inserted marks */ for(i=0; i<runCount; i++) { insertRemove=runs[i].insertRemove; if(insertRemove&(LRM_BEFORE|RLM_BEFORE)) { markFound++; } if(insertRemove&(LRM_AFTER|RLM_AFTER)) { markFound++; } } /* move back indexes by number of preceding marks */ k=pBiDi->resultLength; for(i=runCount-1; i>=0 && markFound>0; i--) { insertRemove=runs[i].insertRemove; if(insertRemove&(LRM_AFTER|RLM_AFTER)) { indexMap[--k]= UBIDI_MAP_NOWHERE; markFound--; } visualStart= i>0 ? runs[i-1].visualLimit : 0; for(j=runs[i].visualLimit-1; j>=visualStart && markFound>0; j--) { indexMap[--k]=indexMap[j]; } if(insertRemove&(LRM_BEFORE|RLM_BEFORE)) { indexMap[--k]= UBIDI_MAP_NOWHERE; markFound--; } } } else if(pBiDi->controlCount>0) { int32_t runCount=pBiDi->runCount, logicalEnd; int32_t insertRemove, length, i, j, k, m; UChar uchar; UBool evenRun; runs=pBiDi->runs; visualStart=0; /* move forward indexes by number of preceding controls */ k=0; for(i=0; i<runCount; i++, visualStart+=length) { length=runs[i].visualLimit-visualStart; insertRemove=runs[i].insertRemove; /* if no control found yet, nothing to do in this run */ if((insertRemove==0)&&(k==visualStart)) { k+=length; continue; } /* if no control in this run */ if(insertRemove==0) { visualLimit=runs[i].visualLimit; for(j=visualStart; j<visualLimit; j++) { indexMap[k++]=indexMap[j]; } continue; } logicalStart=runs[i].logicalStart; evenRun=IS_EVEN_RUN(logicalStart); REMOVE_ODD_BIT(logicalStart); logicalEnd=logicalStart+length-1; for(j=0; j<length; j++) { m= evenRun ? logicalStart+j : logicalEnd-j; uchar=pBiDi->text[m]; if(!IS_BIDI_CONTROL_CHAR(uchar)) { indexMap[k++]=m; } } } } } }
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==NULL) { *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)); } visualStart=0; for(j=0; j<pBiDi->runCount; ++j) { logicalStart=GET_INDEX(runs[j].logicalStart); visualLimit=runs[j].visualLimit; if(IS_EVEN_RUN(runs[j].logicalStart)) { do { /* LTR */ indexMap[logicalStart++]=visualStart++; } while(visualStart<visualLimit); } else { logicalStart+=visualLimit-visualStart; /* logicalLimit */ do { /* RTL */ indexMap[--logicalStart]=visualStart++; } while(visualStart<visualLimit); } /* visualStart==visualLimit; */ } if(pBiDi->insertPoints.size>0) { int32_t markFound=0, runCount=pBiDi->runCount; int32_t length, insertRemove; visualStart=0; /* add number of marks found until each index */ for(i=0; i<runCount; i++, visualStart+=length) { length=runs[i].visualLimit-visualStart; insertRemove=runs[i].insertRemove; if(insertRemove&(LRM_BEFORE|RLM_BEFORE)) { markFound++; } if(markFound>0) { logicalStart=GET_INDEX(runs[i].logicalStart); logicalLimit=logicalStart+length; for(j=logicalStart; j<logicalLimit; j++) { indexMap[j]+=markFound; } } if(insertRemove&(LRM_AFTER|RLM_AFTER)) { markFound++; } } } else if(pBiDi->controlCount>0) { int32_t controlFound=0, runCount=pBiDi->runCount; int32_t length, insertRemove; UBool evenRun; UChar uchar; visualStart=0; /* subtract number of controls found until each index */ for(i=0; i<runCount; i++, visualStart+=length) { length=runs[i].visualLimit-visualStart; insertRemove=runs[i].insertRemove; /* no control found within previous runs nor within this run */ if((controlFound-insertRemove)==0) { continue; } logicalStart=runs[i].logicalStart; evenRun=IS_EVEN_RUN(logicalStart); REMOVE_ODD_BIT(logicalStart); logicalLimit=logicalStart+length; /* if no control within this run */ if(insertRemove==0) { for(j=logicalStart; j<logicalLimit; j++) { indexMap[j]-=controlFound; } continue; } for(j=0; j<length; j++) { k= evenRun ? logicalStart+j : logicalLimit-j-1; uchar=pBiDi->text[k]; if(IS_BIDI_CONTROL_CHAR(uchar)) { controlFound++; indexMap[k]=UBIDI_MAP_NOWHERE; continue; } indexMap[k]-=controlFound; } } } } }
/* * When we have UBIDI_OUTPUT_REVERSE set on ubidi_writeReordered(), then we * semantically write RTL runs in reverse and later reverse them again. * Instead, we actually write them in forward order to begin with. * However, if the RTL run was to be mirrored, we need to mirror here now * since the implicit second reversal must not do it. * It looks strange to do mirroring in LTR output, but it is only because * we are writing RTL output in reverse. */ static int32_t doWriteForward(const UChar *src, int32_t srcLength, UChar *dest, int32_t destSize, uint16_t options, UErrorCode *pErrorCode) { /* optimize for several combinations of options */ switch(options&(UBIDI_REMOVE_BIDI_CONTROLS|UBIDI_DO_MIRRORING)) { case 0: { /* simply copy the LTR run to the destination */ int32_t length=srcLength; if(destSize<length) { *pErrorCode=U_BUFFER_OVERFLOW_ERROR; return srcLength; } do { *dest++=*src++; } while(--length>0); return srcLength; } case UBIDI_DO_MIRRORING: { /* do mirroring */ int32_t i=0, j=0; UChar32 c; if(destSize<srcLength) { *pErrorCode=U_BUFFER_OVERFLOW_ERROR; return srcLength; } do { UTF_NEXT_CHAR(src, i, srcLength, c); c=u_charMirror(c); UTF_APPEND_CHAR_UNSAFE(dest, j, c); } while(i<srcLength); return srcLength; } case UBIDI_REMOVE_BIDI_CONTROLS: { /* copy the LTR run and remove any BiDi control characters */ int32_t remaining=destSize; UChar c; do { c=*src++; if(!IS_BIDI_CONTROL_CHAR(c)) { if(--remaining<0) { *pErrorCode=U_BUFFER_OVERFLOW_ERROR; /* preflight the length */ while(--srcLength>0) { c=*src++; if(!IS_BIDI_CONTROL_CHAR(c)) { --remaining; } } return destSize-remaining; } *dest++=c; } } while(--srcLength>0); return destSize-remaining; } default: { /* remove BiDi control characters and do mirroring */ int32_t remaining=destSize; int32_t i, j=0; UChar32 c; do { i=0; UTF_NEXT_CHAR(src, i, srcLength, c); src+=i; srcLength-=i; if(!IS_BIDI_CONTROL_CHAR(c)) { remaining-=i; if(remaining<0) { *pErrorCode=U_BUFFER_OVERFLOW_ERROR; /* preflight the length */ while(srcLength>0) { c=*src++; if(!IS_BIDI_CONTROL_CHAR(c)) { --remaining; } --srcLength; } return destSize-remaining; } c=u_charMirror(c); UTF_APPEND_CHAR_UNSAFE(dest, j, c); } } while(srcLength>0); return j; } } /* end of switch */ }
static int32_t doWriteReverse(const UChar *src, int32_t srcLength, UChar *dest, int32_t destSize, uint16_t options, UErrorCode *pErrorCode) { /* * RTL run - * * RTL runs need to be copied to the destination in reverse order * of code points, not code units, to keep Unicode characters intact. * * The general strategy for this is to read the source text * in backward order, collect all code units for a code point * (and optionally following combining characters, see below), * and copy all these code units in ascending order * to the destination for this run. * * Several options request whether combining characters * should be kept after their base characters, * whether BiDi control characters should be removed, and * whether characters should be replaced by their mirror-image * equivalent Unicode characters. */ int32_t i, j; UChar32 c; /* optimize for several combinations of options */ switch(options&(UBIDI_REMOVE_BIDI_CONTROLS|UBIDI_DO_MIRRORING|UBIDI_KEEP_BASE_COMBINING)) { case 0: /* * With none of the "complicated" options set, the destination * run will have the same length as the source run, * and there is no mirroring and no keeping combining characters * with their base characters. */ if(destSize<srcLength) { *pErrorCode=U_BUFFER_OVERFLOW_ERROR; return srcLength; } destSize=srcLength; /* preserve character integrity */ do { /* i is always after the last code unit known to need to be kept in this segment */ i=srcLength; /* collect code units for one base character */ UTF_BACK_1(src, 0, srcLength); /* copy this base character */ j=srcLength; do { *dest++=src[j++]; } while(j<i); } while(srcLength>0); break; case UBIDI_KEEP_BASE_COMBINING: /* * Here, too, the destination * run will have the same length as the source run, * and there is no mirroring. * We do need to keep combining characters with their base characters. */ if(destSize<srcLength) { *pErrorCode=U_BUFFER_OVERFLOW_ERROR; return srcLength; } destSize=srcLength; /* preserve character integrity */ do { /* i is always after the last code unit known to need to be kept in this segment */ i=srcLength; /* collect code units and modifier letters for one base character */ do { UTF_PREV_CHAR(src, 0, srcLength, c); } while(srcLength>0 && IS_COMBINING(u_charType(c))); /* copy this "user character" */ j=srcLength; do { *dest++=src[j++]; } while(j<i); } while(srcLength>0); break; default: /* * With several "complicated" options set, this is the most * general and the slowest copying of an RTL run. * We will do mirroring, remove BiDi controls, and * keep combining characters with their base characters * as requested. */ if(!(options&UBIDI_REMOVE_BIDI_CONTROLS)) { i=srcLength; } else { /* we need to find out the destination length of the run, which will not include the BiDi control characters */ int32_t length=srcLength; UChar ch; i=0; do { ch=*src++; if(!IS_BIDI_CONTROL_CHAR(ch)) { ++i; } } while(--length>0); src-=srcLength; } if(destSize<i) { *pErrorCode=U_BUFFER_OVERFLOW_ERROR; return i; } destSize=i; /* preserve character integrity */ do { /* i is always after the last code unit known to need to be kept in this segment */ i=srcLength; /* collect code units for one base character */ UTF_PREV_CHAR(src, 0, srcLength, c); if(options&UBIDI_KEEP_BASE_COMBINING) { /* collect modifier letters for this base character */ while(srcLength>0 && IS_COMBINING(u_charType(c))) { UTF_PREV_CHAR(src, 0, srcLength, c); } } if(options&UBIDI_REMOVE_BIDI_CONTROLS && IS_BIDI_CONTROL_CHAR(c)) { /* do not copy this BiDi control character */ continue; } /* copy this "user character" */ j=srcLength; if(options&UBIDI_DO_MIRRORING) { /* mirror only the base character */ int32_t k=0; c=u_charMirror(c); UTF_APPEND_CHAR_UNSAFE(dest, k, c); dest+=k; j+=k; } while(j<i) { *dest++=src[j++]; } } while(srcLength>0); break; } /* end of switch */ return destSize; }