2 ******************************************************************************
4 * Copyright (C) 2000-2011, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: ubidiwrt.c
10 * tab size: 8 (not used)
13 * created on: 1999aug06
14 * created by: Markus W. Scherer, updated by Matitiahu Allouche
16 * This file contains implementations for BiDi functions that use
17 * the core algorithm and core API to write reordered text.
20 /* set import/export definitions */
21 #ifndef U_COMMON_IMPLEMENTATION
22 # define U_COMMON_IMPLEMENTATION
25 #include "unicode/utypes.h"
26 #include "unicode/ustring.h"
27 #include "unicode/uchar.h"
28 #include "unicode/ubidi.h"
29 #include "unicode/utf16.h"
35 * The function implementations in this file are designed
36 * for UTF-16 and UTF-32, not for UTF-8.
38 * Assumptions that are not true for UTF-8:
39 * - Any code point always needs the same number of code units
40 * ("minimum-length-problem" of UTF-8)
41 * - The BiDi control characters need only one code unit each
43 * Further assumptions for all UTFs:
44 * - u_charMirror(c) needs the same number of code units as c
47 # error reimplement ubidi_writeReordered() for UTF-8, see comment above
50 #define IS_COMBINING(type) ((1UL<<(type))&(1UL<<U_NON_SPACING_MARK|1UL<<U_COMBINING_SPACING_MARK|1UL<<U_ENCLOSING_MARK))
53 * When we have UBIDI_OUTPUT_REVERSE set on ubidi_writeReordered(), then we
54 * semantically write RTL runs in reverse and later reverse them again.
55 * Instead, we actually write them in forward order to begin with.
56 * However, if the RTL run was to be mirrored, we need to mirror here now
57 * since the implicit second reversal must not do it.
58 * It looks strange to do mirroring in LTR output, but it is only because
59 * we are writing RTL output in reverse.
62 doWriteForward(const UChar
*src
, int32_t srcLength
,
63 UChar
*dest
, int32_t destSize
,
65 UErrorCode
*pErrorCode
) {
66 /* optimize for several combinations of options */
67 switch(options
&(UBIDI_REMOVE_BIDI_CONTROLS
|UBIDI_DO_MIRRORING
)) {
69 /* simply copy the LTR run to the destination */
70 int32_t length
=srcLength
;
72 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
80 case UBIDI_DO_MIRRORING
: {
85 if(destSize
<srcLength
) {
86 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
90 U16_NEXT(src
, i
, srcLength
, c
);
92 U16_APPEND_UNSAFE(dest
, j
, c
);
96 case UBIDI_REMOVE_BIDI_CONTROLS
: {
97 /* copy the LTR run and remove any BiDi control characters */
98 int32_t remaining
=destSize
;
102 if(!IS_BIDI_CONTROL_CHAR(c
)) {
104 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
106 /* preflight the length */
107 while(--srcLength
>0) {
109 if(!IS_BIDI_CONTROL_CHAR(c
)) {
113 return destSize
-remaining
;
117 } while(--srcLength
>0);
118 return destSize
-remaining
;
121 /* remove BiDi control characters and do mirroring */
122 int32_t remaining
=destSize
;
127 U16_NEXT(src
, i
, srcLength
, c
);
130 if(!IS_BIDI_CONTROL_CHAR(c
)) {
133 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
135 /* preflight the length */
138 if(!IS_BIDI_CONTROL_CHAR(c
)) {
143 return destSize
-remaining
;
146 U16_APPEND_UNSAFE(dest
, j
, c
);
148 } while(srcLength
>0);
151 } /* end of switch */
155 doWriteReverse(const UChar
*src
, int32_t srcLength
,
156 UChar
*dest
, int32_t destSize
,
158 UErrorCode
*pErrorCode
) {
162 * RTL runs need to be copied to the destination in reverse order
163 * of code points, not code units, to keep Unicode characters intact.
165 * The general strategy for this is to read the source text
166 * in backward order, collect all code units for a code point
167 * (and optionally following combining characters, see below),
168 * and copy all these code units in ascending order
169 * to the destination for this run.
171 * Several options request whether combining characters
172 * should be kept after their base characters,
173 * whether BiDi control characters should be removed, and
174 * whether characters should be replaced by their mirror-image
175 * equivalent Unicode characters.
180 /* optimize for several combinations of options */
181 switch(options
&(UBIDI_REMOVE_BIDI_CONTROLS
|UBIDI_DO_MIRRORING
|UBIDI_KEEP_BASE_COMBINING
)) {
184 * With none of the "complicated" options set, the destination
185 * run will have the same length as the source run,
186 * and there is no mirroring and no keeping combining characters
187 * with their base characters.
189 if(destSize
<srcLength
) {
190 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
195 /* preserve character integrity */
197 /* i is always after the last code unit known to need to be kept in this segment */
200 /* collect code units for one base character */
201 U16_BACK_1(src
, 0, srcLength
);
203 /* copy this base character */
208 } while(srcLength
>0);
210 case UBIDI_KEEP_BASE_COMBINING
:
212 * Here, too, the destination
213 * run will have the same length as the source run,
214 * and there is no mirroring.
215 * We do need to keep combining characters with their base characters.
217 if(destSize
<srcLength
) {
218 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
223 /* preserve character integrity */
225 /* i is always after the last code unit known to need to be kept in this segment */
228 /* collect code units and modifier letters for one base character */
230 U16_PREV(src
, 0, srcLength
, c
);
231 } while(srcLength
>0 && IS_COMBINING(u_charType(c
)));
233 /* copy this "user character" */
238 } while(srcLength
>0);
242 * With several "complicated" options set, this is the most
243 * general and the slowest copying of an RTL run.
244 * We will do mirroring, remove BiDi controls, and
245 * keep combining characters with their base characters
248 if(!(options
&UBIDI_REMOVE_BIDI_CONTROLS
)) {
251 /* we need to find out the destination length of the run,
252 which will not include the BiDi control characters */
253 int32_t length
=srcLength
;
259 if(!IS_BIDI_CONTROL_CHAR(ch
)) {
267 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
272 /* preserve character integrity */
274 /* i is always after the last code unit known to need to be kept in this segment */
277 /* collect code units for one base character */
278 U16_PREV(src
, 0, srcLength
, c
);
279 if(options
&UBIDI_KEEP_BASE_COMBINING
) {
280 /* collect modifier letters for this base character */
281 while(srcLength
>0 && IS_COMBINING(u_charType(c
))) {
282 U16_PREV(src
, 0, srcLength
, c
);
286 if(options
&UBIDI_REMOVE_BIDI_CONTROLS
&& IS_BIDI_CONTROL_CHAR(c
)) {
287 /* do not copy this BiDi control character */
291 /* copy this "user character" */
293 if(options
&UBIDI_DO_MIRRORING
) {
294 /* mirror only the base character */
297 U16_APPEND_UNSAFE(dest
, k
, c
);
304 } while(srcLength
>0);
306 } /* end of switch */
311 U_CAPI
int32_t U_EXPORT2
312 ubidi_writeReverse(const UChar
*src
, int32_t srcLength
,
313 UChar
*dest
, int32_t destSize
,
315 UErrorCode
*pErrorCode
) {
318 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
322 /* more error checking */
323 if( src
==NULL
|| srcLength
<-1 ||
324 destSize
<0 || (destSize
>0 && dest
==NULL
))
326 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
330 /* do input and output overlap? */
332 ((src
>=dest
&& src
<dest
+destSize
) ||
333 (dest
>=src
&& dest
<src
+srcLength
)))
335 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
340 srcLength
=u_strlen(src
);
343 destLength
=doWriteReverse(src
, srcLength
, dest
, destSize
, options
, pErrorCode
);
349 return u_terminateUChars(dest
, destSize
, destLength
, pErrorCode
);
352 U_CAPI
int32_t U_EXPORT2
353 ubidi_writeReordered(UBiDi
*pBiDi
,
354 UChar
*dest
, int32_t destSize
,
356 UErrorCode
*pErrorCode
) {
359 int32_t length
, destCapacity
;
360 int32_t run
, runCount
, logicalStart
, runLength
;
362 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
366 /* more error checking */
368 (text
=pBiDi
->text
)==NULL
|| (length
=pBiDi
->length
)<0 ||
369 destSize
<0 || (destSize
>0 && dest
==NULL
))
371 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
375 /* do input and output overlap? */
377 ((text
>=dest
&& text
<dest
+destSize
) ||
378 (dest
>=text
&& dest
<text
+pBiDi
->originalLength
)))
380 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
386 return u_terminateUChars(dest
, destSize
, 0, pErrorCode
);
389 runCount
=ubidi_countRuns(pBiDi
, pErrorCode
);
390 if(U_FAILURE(*pErrorCode
)) {
394 /* destSize shrinks, later destination length=destCapacity-destSize */
396 destCapacity
=destSize
;
399 * Option "insert marks" implies UBIDI_INSERT_LRM_FOR_NUMERIC if the
400 * reordering mode (checked below) is appropriate.
402 if(pBiDi
->reorderingOptions
& UBIDI_OPTION_INSERT_MARKS
) {
403 options
|=UBIDI_INSERT_LRM_FOR_NUMERIC
;
404 options
&=~UBIDI_REMOVE_BIDI_CONTROLS
;
407 * Option "remove controls" implies UBIDI_REMOVE_BIDI_CONTROLS
408 * and cancels UBIDI_INSERT_LRM_FOR_NUMERIC.
410 if(pBiDi
->reorderingOptions
& UBIDI_OPTION_REMOVE_CONTROLS
) {
411 options
|=UBIDI_REMOVE_BIDI_CONTROLS
;
412 options
&=~UBIDI_INSERT_LRM_FOR_NUMERIC
;
415 * If we do not perform the "inverse BiDi" algorithm, then we
416 * don't need to insert any LRMs, and don't need to test for it.
418 if((pBiDi
->reorderingMode
!= UBIDI_REORDER_INVERSE_NUMBERS_AS_L
) &&
419 (pBiDi
->reorderingMode
!= UBIDI_REORDER_INVERSE_LIKE_DIRECT
) &&
420 (pBiDi
->reorderingMode
!= UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL
) &&
421 (pBiDi
->reorderingMode
!= UBIDI_REORDER_RUNS_ONLY
)) {
422 options
&=~UBIDI_INSERT_LRM_FOR_NUMERIC
;
425 * Iterate through all visual runs and copy the run text segments to
426 * the destination, according to the options.
428 * The tests for where to insert LRMs ignore the fact that there may be
429 * BN codes or non-BMP code points at the beginning and end of a run;
430 * they may insert LRMs unnecessarily but the tests are faster this way
431 * (this would have to be improved for UTF-8).
433 * Note that the only errors that are set by doWriteXY() are buffer overflow
434 * errors. Ignore them until the end, and continue for preflighting.
436 if(!(options
&UBIDI_OUTPUT_REVERSE
)) {
438 if(!(options
&UBIDI_INSERT_LRM_FOR_NUMERIC
)) {
439 /* do not insert BiDi controls */
440 for(run
=0; run
<runCount
; ++run
) {
441 if(UBIDI_LTR
==ubidi_getVisualRun(pBiDi
, run
, &logicalStart
, &runLength
)) {
442 runLength
=doWriteForward(text
+logicalStart
, runLength
,
444 (uint16_t)(options
&~UBIDI_DO_MIRRORING
), pErrorCode
);
446 runLength
=doWriteReverse(text
+logicalStart
, runLength
,
448 options
, pErrorCode
);
456 /* insert BiDi controls for "inverse BiDi" */
457 const DirProp
*dirProps
=pBiDi
->dirProps
;
463 for(run
=0; run
<runCount
; ++run
) {
464 dir
=ubidi_getVisualRun(pBiDi
, run
, &logicalStart
, &runLength
);
465 src
=text
+logicalStart
;
466 /* check if something relevant in insertPoints */
467 markFlag
=pBiDi
->runs
[run
].insertRemove
;
468 if(markFlag
<0) { /* BiDi controls count */
473 if((pBiDi
->isInverse
) &&
474 (/*run>0 &&*/ dirProps
[logicalStart
]!=L
)) {
475 markFlag
|= LRM_BEFORE
;
477 if (markFlag
& LRM_BEFORE
) {
480 else if (markFlag
& RLM_BEFORE
) {
491 runLength
=doWriteForward(src
, runLength
,
493 (uint16_t)(options
&~UBIDI_DO_MIRRORING
), pErrorCode
);
499 if((pBiDi
->isInverse
) &&
500 (/*run<runCount-1 &&*/ dirProps
[logicalStart
+runLength
-1]!=L
)) {
501 markFlag
|= LRM_AFTER
;
503 if (markFlag
& LRM_AFTER
) {
506 else if (markFlag
& RLM_AFTER
) {
516 } else { /* RTL run */
517 if((pBiDi
->isInverse
) &&
518 (/*run>0 &&*/ !(MASK_R_AL
&DIRPROP_FLAG(dirProps
[logicalStart
+runLength
-1])))) {
519 markFlag
|= RLM_BEFORE
;
521 if (markFlag
& LRM_BEFORE
) {
524 else if (markFlag
& RLM_BEFORE
) {
535 runLength
=doWriteReverse(src
, runLength
,
537 options
, pErrorCode
);
543 if((pBiDi
->isInverse
) &&
544 (/*run<runCount-1 &&*/ !(MASK_R_AL
&DIRPROP_FLAG(dirProps
[logicalStart
])))) {
545 markFlag
|= RLM_AFTER
;
547 if (markFlag
& LRM_AFTER
) {
550 else if (markFlag
& RLM_AFTER
) {
565 if(!(options
&UBIDI_INSERT_LRM_FOR_NUMERIC
)) {
566 /* do not insert BiDi controls */
567 for(run
=runCount
; --run
>=0;) {
568 if(UBIDI_LTR
==ubidi_getVisualRun(pBiDi
, run
, &logicalStart
, &runLength
)) {
569 runLength
=doWriteReverse(text
+logicalStart
, runLength
,
571 (uint16_t)(options
&~UBIDI_DO_MIRRORING
), pErrorCode
);
573 runLength
=doWriteForward(text
+logicalStart
, runLength
,
575 options
, pErrorCode
);
583 /* insert BiDi controls for "inverse BiDi" */
584 const DirProp
*dirProps
=pBiDi
->dirProps
;
588 for(run
=runCount
; --run
>=0;) {
590 dir
=ubidi_getVisualRun(pBiDi
, run
, &logicalStart
, &runLength
);
591 src
=text
+logicalStart
;
594 if(/*run<runCount-1 &&*/ dirProps
[logicalStart
+runLength
-1]!=L
) {
601 runLength
=doWriteReverse(src
, runLength
,
603 (uint16_t)(options
&~UBIDI_DO_MIRRORING
), pErrorCode
);
609 if(/*run>0 &&*/ dirProps
[logicalStart
]!=L
) {
616 if(/*run<runCount-1 &&*/ !(MASK_R_AL
&DIRPROP_FLAG(dirProps
[logicalStart
]))) {
623 runLength
=doWriteForward(src
, runLength
,
625 options
, pErrorCode
);
631 if(/*run>0 &&*/ !(MASK_R_AL
&DIRPROP_FLAG(dirProps
[logicalStart
+runLength
-1]))) {
642 return u_terminateUChars(saveDest
, destCapacity
, destCapacity
-destSize
, pErrorCode
);