2 ******************************************************************************
4 * Copyright (C) 2000-2015, 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 #include "unicode/utypes.h"
21 #include "unicode/ustring.h"
22 #include "unicode/uchar.h"
23 #include "unicode/ubidi.h"
24 #include "unicode/utf16.h"
30 * The function implementations in this file are designed
31 * for UTF-16 and UTF-32, not for UTF-8.
33 * Assumptions that are not true for UTF-8:
34 * - Any code point always needs the same number of code units
35 * ("minimum-length-problem" of UTF-8)
36 * - The BiDi control characters need only one code unit each
38 * Further assumptions for all UTFs:
39 * - u_charMirror(c) needs the same number of code units as c
42 # error reimplement ubidi_writeReordered() for UTF-8, see comment above
45 #define IS_COMBINING(type) ((1UL<<(type))&(1UL<<U_NON_SPACING_MARK|1UL<<U_COMBINING_SPACING_MARK|1UL<<U_ENCLOSING_MARK))
48 * When we have UBIDI_OUTPUT_REVERSE set on ubidi_writeReordered(), then we
49 * semantically write RTL runs in reverse and later reverse them again.
50 * Instead, we actually write them in forward order to begin with.
51 * However, if the RTL run was to be mirrored, we need to mirror here now
52 * since the implicit second reversal must not do it.
53 * It looks strange to do mirroring in LTR output, but it is only because
54 * we are writing RTL output in reverse.
57 doWriteForward(const UChar
*src
, int32_t srcLength
,
58 UChar
*dest
, int32_t destSize
,
60 UErrorCode
*pErrorCode
) {
61 /* optimize for several combinations of options */
62 switch(options
&(UBIDI_REMOVE_BIDI_CONTROLS
|UBIDI_DO_MIRRORING
)) {
64 /* simply copy the LTR run to the destination */
65 int32_t length
=srcLength
;
67 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
75 case UBIDI_DO_MIRRORING
: {
80 if(destSize
<srcLength
) {
81 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
85 U16_NEXT(src
, i
, srcLength
, c
);
87 U16_APPEND_UNSAFE(dest
, j
, c
);
91 case UBIDI_REMOVE_BIDI_CONTROLS
: {
92 /* copy the LTR run and remove any BiDi control characters */
93 int32_t remaining
=destSize
;
97 if(!IS_BIDI_CONTROL_CHAR(c
)) {
99 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
101 /* preflight the length */
102 while(--srcLength
>0) {
104 if(!IS_BIDI_CONTROL_CHAR(c
)) {
108 return destSize
-remaining
;
112 } while(--srcLength
>0);
113 return destSize
-remaining
;
116 /* remove BiDi control characters and do mirroring */
117 int32_t remaining
=destSize
;
122 U16_NEXT(src
, i
, srcLength
, c
);
125 if(!IS_BIDI_CONTROL_CHAR(c
)) {
128 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
130 /* preflight the length */
133 if(!IS_BIDI_CONTROL_CHAR(c
)) {
138 return destSize
-remaining
;
141 U16_APPEND_UNSAFE(dest
, j
, c
);
143 } while(srcLength
>0);
146 } /* end of switch */
150 doWriteReverse(const UChar
*src
, int32_t srcLength
,
151 UChar
*dest
, int32_t destSize
,
153 UErrorCode
*pErrorCode
) {
157 * RTL runs need to be copied to the destination in reverse order
158 * of code points, not code units, to keep Unicode characters intact.
160 * The general strategy for this is to read the source text
161 * in backward order, collect all code units for a code point
162 * (and optionally following combining characters, see below),
163 * and copy all these code units in ascending order
164 * to the destination for this run.
166 * Several options request whether combining characters
167 * should be kept after their base characters,
168 * whether BiDi control characters should be removed, and
169 * whether characters should be replaced by their mirror-image
170 * equivalent Unicode characters.
175 /* optimize for several combinations of options */
176 switch(options
&(UBIDI_REMOVE_BIDI_CONTROLS
|UBIDI_DO_MIRRORING
|UBIDI_KEEP_BASE_COMBINING
)) {
179 * With none of the "complicated" options set, the destination
180 * run will have the same length as the source run,
181 * and there is no mirroring and no keeping combining characters
182 * with their base characters.
184 if(destSize
<srcLength
) {
185 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
190 /* preserve character integrity */
192 /* i is always after the last code unit known to need to be kept in this segment */
195 /* collect code units for one base character */
196 U16_BACK_1(src
, 0, srcLength
);
198 /* copy this base character */
203 } while(srcLength
>0);
205 case UBIDI_KEEP_BASE_COMBINING
:
207 * Here, too, the destination
208 * run will have the same length as the source run,
209 * and there is no mirroring.
210 * We do need to keep combining characters with their base characters.
212 if(destSize
<srcLength
) {
213 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
218 /* preserve character integrity */
220 /* i is always after the last code unit known to need to be kept in this segment */
223 /* collect code units and modifier letters for one base character */
225 U16_PREV(src
, 0, srcLength
, c
);
226 } while(srcLength
>0 && IS_COMBINING(u_charType(c
)));
228 /* copy this "user character" */
233 } while(srcLength
>0);
237 * With several "complicated" options set, this is the most
238 * general and the slowest copying of an RTL run.
239 * We will do mirroring, remove BiDi controls, and
240 * keep combining characters with their base characters
243 if(!(options
&UBIDI_REMOVE_BIDI_CONTROLS
)) {
246 /* we need to find out the destination length of the run,
247 which will not include the BiDi control characters */
248 int32_t length
=srcLength
;
254 if(!IS_BIDI_CONTROL_CHAR(ch
)) {
262 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
267 /* preserve character integrity */
269 /* i is always after the last code unit known to need to be kept in this segment */
272 /* collect code units for one base character */
273 U16_PREV(src
, 0, srcLength
, c
);
274 if(options
&UBIDI_KEEP_BASE_COMBINING
) {
275 /* collect modifier letters for this base character */
276 while(srcLength
>0 && IS_COMBINING(u_charType(c
))) {
277 U16_PREV(src
, 0, srcLength
, c
);
281 if(options
&UBIDI_REMOVE_BIDI_CONTROLS
&& IS_BIDI_CONTROL_CHAR(c
)) {
282 /* do not copy this BiDi control character */
286 /* copy this "user character" */
288 if(options
&UBIDI_DO_MIRRORING
) {
289 /* mirror only the base character */
292 U16_APPEND_UNSAFE(dest
, k
, c
);
299 } while(srcLength
>0);
301 } /* end of switch */
306 U_CAPI
int32_t U_EXPORT2
307 ubidi_writeReverse(const UChar
*src
, int32_t srcLength
,
308 UChar
*dest
, int32_t destSize
,
310 UErrorCode
*pErrorCode
) {
313 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
317 /* more error checking */
318 if( src
==NULL
|| srcLength
<-1 ||
319 destSize
<0 || (destSize
>0 && dest
==NULL
))
321 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
325 /* do input and output overlap? */
327 ((src
>=dest
&& src
<dest
+destSize
) ||
328 (dest
>=src
&& dest
<src
+srcLength
)))
330 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
335 srcLength
=u_strlen(src
);
338 destLength
=doWriteReverse(src
, srcLength
, dest
, destSize
, options
, pErrorCode
);
344 return u_terminateUChars(dest
, destSize
, destLength
, pErrorCode
);
347 U_CAPI
int32_t U_EXPORT2
348 ubidi_writeReordered(UBiDi
*pBiDi
,
349 UChar
*dest
, int32_t destSize
,
351 UErrorCode
*pErrorCode
) {
354 int32_t length
, destCapacity
;
355 int32_t run
, runCount
, logicalStart
, runLength
;
357 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
361 /* more error checking */
363 (text
=pBiDi
->text
)==NULL
|| (length
=pBiDi
->length
)<0 ||
364 destSize
<0 || (destSize
>0 && dest
==NULL
))
366 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
370 /* do input and output overlap? */
372 ((text
>=dest
&& text
<dest
+destSize
) ||
373 (dest
>=text
&& dest
<text
+pBiDi
->originalLength
)))
375 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
381 return u_terminateUChars(dest
, destSize
, 0, pErrorCode
);
384 runCount
=ubidi_countRuns(pBiDi
, pErrorCode
);
385 if(U_FAILURE(*pErrorCode
)) {
389 /* destSize shrinks, later destination length=destCapacity-destSize */
391 destCapacity
=destSize
;
394 * Option "insert marks" implies UBIDI_INSERT_LRM_FOR_NUMERIC if the
395 * reordering mode (checked below) is appropriate.
397 if(pBiDi
->reorderingOptions
& UBIDI_OPTION_INSERT_MARKS
) {
398 options
|=UBIDI_INSERT_LRM_FOR_NUMERIC
;
399 options
&=~UBIDI_REMOVE_BIDI_CONTROLS
;
402 * Option "remove controls" implies UBIDI_REMOVE_BIDI_CONTROLS
403 * and cancels UBIDI_INSERT_LRM_FOR_NUMERIC.
405 if(pBiDi
->reorderingOptions
& UBIDI_OPTION_REMOVE_CONTROLS
) {
406 options
|=UBIDI_REMOVE_BIDI_CONTROLS
;
407 options
&=~UBIDI_INSERT_LRM_FOR_NUMERIC
;
410 * If we do not perform the "inverse BiDi" algorithm, then we
411 * don't need to insert any LRMs, and don't need to test for it.
413 if((pBiDi
->reorderingMode
!= UBIDI_REORDER_INVERSE_NUMBERS_AS_L
) &&
414 (pBiDi
->reorderingMode
!= UBIDI_REORDER_INVERSE_LIKE_DIRECT
) &&
415 (pBiDi
->reorderingMode
!= UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL
) &&
416 (pBiDi
->reorderingMode
!= UBIDI_REORDER_RUNS_ONLY
)) {
417 options
&=~UBIDI_INSERT_LRM_FOR_NUMERIC
;
420 * Iterate through all visual runs and copy the run text segments to
421 * the destination, according to the options.
423 * The tests for where to insert LRMs ignore the fact that there may be
424 * BN codes or non-BMP code points at the beginning and end of a run;
425 * they may insert LRMs unnecessarily but the tests are faster this way
426 * (this would have to be improved for UTF-8).
428 * Note that the only errors that are set by doWriteXY() are buffer overflow
429 * errors. Ignore them until the end, and continue for preflighting.
431 if(!(options
&UBIDI_OUTPUT_REVERSE
)) {
433 if(!(options
&UBIDI_INSERT_LRM_FOR_NUMERIC
)) {
434 /* do not insert BiDi controls */
435 for(run
=0; run
<runCount
; ++run
) {
436 if(UBIDI_LTR
==ubidi_getVisualRun(pBiDi
, run
, &logicalStart
, &runLength
)) {
437 runLength
=doWriteForward(text
+logicalStart
, runLength
,
439 (uint16_t)(options
&~UBIDI_DO_MIRRORING
), pErrorCode
);
441 runLength
=doWriteReverse(text
+logicalStart
, runLength
,
443 options
, pErrorCode
);
451 /* insert BiDi controls for "inverse BiDi" */
452 const DirProp
*dirProps
=pBiDi
->dirProps
;
458 for(run
=0; run
<runCount
; ++run
) {
459 dir
=ubidi_getVisualRun(pBiDi
, run
, &logicalStart
, &runLength
);
460 src
=text
+logicalStart
;
461 /* check if something relevant in insertPoints */
462 markFlag
=pBiDi
->runs
[run
].insertRemove
;
463 if(markFlag
<0) { /* BiDi controls count */
468 if((pBiDi
->isInverse
) &&
469 (/*run>0 &&*/ dirProps
[logicalStart
]!=L
)) {
470 markFlag
|= LRM_BEFORE
;
472 if (markFlag
& LRM_BEFORE
) {
475 else if (markFlag
& RLM_BEFORE
) {
486 runLength
=doWriteForward(src
, runLength
,
488 (uint16_t)(options
&~UBIDI_DO_MIRRORING
), pErrorCode
);
494 if((pBiDi
->isInverse
) &&
495 (/*run<runCount-1 &&*/ dirProps
[logicalStart
+runLength
-1]!=L
)) {
496 markFlag
|= LRM_AFTER
;
498 if (markFlag
& LRM_AFTER
) {
501 else if (markFlag
& RLM_AFTER
) {
511 } else { /* RTL run */
512 if((pBiDi
->isInverse
) &&
513 (/*run>0 &&*/ !(MASK_R_AL
&DIRPROP_FLAG(dirProps
[logicalStart
+runLength
-1])))) {
514 markFlag
|= RLM_BEFORE
;
516 if (markFlag
& LRM_BEFORE
) {
519 else if (markFlag
& RLM_BEFORE
) {
530 runLength
=doWriteReverse(src
, runLength
,
532 options
, pErrorCode
);
538 if((pBiDi
->isInverse
) &&
539 (/*run<runCount-1 &&*/ !(MASK_R_AL
&DIRPROP_FLAG(dirProps
[logicalStart
])))) {
540 markFlag
|= RLM_AFTER
;
542 if (markFlag
& LRM_AFTER
) {
545 else if (markFlag
& RLM_AFTER
) {
560 if(!(options
&UBIDI_INSERT_LRM_FOR_NUMERIC
)) {
561 /* do not insert BiDi controls */
562 for(run
=runCount
; --run
>=0;) {
563 if(UBIDI_LTR
==ubidi_getVisualRun(pBiDi
, run
, &logicalStart
, &runLength
)) {
564 runLength
=doWriteReverse(text
+logicalStart
, runLength
,
566 (uint16_t)(options
&~UBIDI_DO_MIRRORING
), pErrorCode
);
568 runLength
=doWriteForward(text
+logicalStart
, runLength
,
570 options
, pErrorCode
);
578 /* insert BiDi controls for "inverse BiDi" */
579 const DirProp
*dirProps
=pBiDi
->dirProps
;
583 for(run
=runCount
; --run
>=0;) {
585 dir
=ubidi_getVisualRun(pBiDi
, run
, &logicalStart
, &runLength
);
586 src
=text
+logicalStart
;
589 if(/*run<runCount-1 &&*/ dirProps
[logicalStart
+runLength
-1]!=L
) {
596 runLength
=doWriteReverse(src
, runLength
,
598 (uint16_t)(options
&~UBIDI_DO_MIRRORING
), pErrorCode
);
604 if(/*run>0 &&*/ dirProps
[logicalStart
]!=L
) {
611 if(/*run<runCount-1 &&*/ !(MASK_R_AL
&DIRPROP_FLAG(dirProps
[logicalStart
]))) {
618 runLength
=doWriteForward(src
, runLength
,
620 options
, pErrorCode
);
626 if(/*run>0 &&*/ !(MASK_R_AL
&DIRPROP_FLAG(dirProps
[logicalStart
+runLength
-1]))) {
637 return u_terminateUChars(saveDest
, destCapacity
, destCapacity
-destSize
, pErrorCode
);