]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/ubidiwrt.c
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / common / ubidiwrt.c
1 /*
2 ******************************************************************************
3 *
4 * Copyright (C) 2000-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 ******************************************************************************
8 * file name: ubidiwrt.c
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 1999aug06
14 * created by: Markus W. Scherer
15 *
16 * This file contains implementations for BiDi functions that use
17 * the core algorithm and core API to write reordered text.
18 */
19
20 /* set import/export definitions */
21 #ifndef U_COMMON_IMPLEMENTATION
22 # define U_COMMON_IMPLEMENTATION
23 #endif
24
25 #include "unicode/utypes.h"
26 #include "unicode/ustring.h"
27 #include "unicode/uchar.h"
28 #include "unicode/ubidi.h"
29 #include "cmemory.h"
30 #include "ustr_imp.h"
31 #include "ubidiimp.h"
32
33 /*
34 * The function implementations in this file are designed
35 * for UTF-16 and UTF-32, not for UTF-8.
36 *
37 * Assumptions that are not true for UTF-8:
38 * - Any code point always needs the same number of code units
39 * ("minimum-length-problem" of UTF-8)
40 * - The BiDi control characters need only one code unit each
41 *
42 * Further assumptions for all UTFs:
43 * - u_charMirror(c) needs the same number of code units as c
44 */
45 #if UTF_SIZE==8
46 # error reimplement ubidi_writeReordered() for UTF-8, see comment above
47 #endif
48
49 #define IS_COMBINING(type) ((1UL<<(type))&(1UL<<U_NON_SPACING_MARK|1UL<<U_COMBINING_SPACING_MARK|1UL<<U_ENCLOSING_MARK))
50
51 /*
52 * When we have UBIDI_OUTPUT_REVERSE set on ubidi_writeReordered(), then we
53 * semantically write RTL runs in reverse and later reverse them again.
54 * Instead, we actually write them in forward order to begin with.
55 * However, if the RTL run was to be mirrored, we need to mirror here now
56 * since the implicit second reversal must not do it.
57 * It looks strange to do mirroring in LTR output, but it is only because
58 * we are writing RTL output in reverse.
59 */
60 static int32_t
61 doWriteForward(const UChar *src, int32_t srcLength,
62 UChar *dest, int32_t destSize,
63 uint16_t options,
64 UErrorCode *pErrorCode) {
65 /* optimize for several combinations of options */
66 switch(options&(UBIDI_REMOVE_BIDI_CONTROLS|UBIDI_DO_MIRRORING)) {
67 case 0: {
68 /* simply copy the LTR run to the destination */
69 int32_t length=srcLength;
70 if(destSize<length) {
71 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
72 return srcLength;
73 }
74 do {
75 *dest++=*src++;
76 } while(--length>0);
77 return srcLength;
78 }
79 case UBIDI_DO_MIRRORING: {
80 /* do mirroring */
81 int32_t i=0, j=0;
82 UChar32 c;
83
84 if(destSize<srcLength) {
85 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
86 return srcLength;
87 }
88 do {
89 UTF_NEXT_CHAR(src, i, srcLength, c);
90 c=u_charMirror(c);
91 UTF_APPEND_CHAR_UNSAFE(dest, j, c);
92 } while(i<srcLength);
93 return srcLength;
94 }
95 case UBIDI_REMOVE_BIDI_CONTROLS: {
96 /* copy the LTR run and remove any BiDi control characters */
97 int32_t remaining=destSize;
98 UChar c;
99 do {
100 c=*src++;
101 if(!IS_BIDI_CONTROL_CHAR(c)) {
102 if(--remaining<0) {
103 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
104
105 /* preflight the length */
106 while(--srcLength>0) {
107 c=*src++;
108 if(!IS_BIDI_CONTROL_CHAR(c)) {
109 --remaining;
110 }
111 }
112 return destSize-remaining;
113 }
114 *dest++=c;
115 }
116 } while(--srcLength>0);
117 return destSize-remaining;
118 }
119 default: {
120 /* remove BiDi control characters and do mirroring */
121 int32_t remaining=destSize;
122 int32_t i, j=0;
123 UChar32 c;
124 do {
125 i=0;
126 UTF_NEXT_CHAR(src, i, srcLength, c);
127 src+=i;
128 srcLength-=i;
129 if(!IS_BIDI_CONTROL_CHAR(c)) {
130 remaining-=i;
131 if(remaining<0) {
132 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
133
134 /* preflight the length */
135 while(srcLength>0) {
136 c=*src++;
137 if(!IS_BIDI_CONTROL_CHAR(c)) {
138 --remaining;
139 }
140 --srcLength;
141 }
142 return destSize-remaining;
143 }
144 c=u_charMirror(c);
145 UTF_APPEND_CHAR_UNSAFE(dest, j, c);
146 }
147 } while(srcLength>0);
148 return j;
149 }
150 } /* end of switch */
151 }
152
153 static int32_t
154 doWriteReverse(const UChar *src, int32_t srcLength,
155 UChar *dest, int32_t destSize,
156 uint16_t options,
157 UErrorCode *pErrorCode) {
158 /*
159 * RTL run -
160 *
161 * RTL runs need to be copied to the destination in reverse order
162 * of code points, not code units, to keep Unicode characters intact.
163 *
164 * The general strategy for this is to read the source text
165 * in backward order, collect all code units for a code point
166 * (and optionally following combining characters, see below),
167 * and copy all these code units in ascending order
168 * to the destination for this run.
169 *
170 * Several options request whether combining characters
171 * should be kept after their base characters,
172 * whether BiDi control characters should be removed, and
173 * whether characters should be replaced by their mirror-image
174 * equivalent Unicode characters.
175 */
176 int32_t i, j;
177 UChar32 c;
178
179 /* optimize for several combinations of options */
180 switch(options&(UBIDI_REMOVE_BIDI_CONTROLS|UBIDI_DO_MIRRORING|UBIDI_KEEP_BASE_COMBINING)) {
181 case 0:
182 /*
183 * With none of the "complicated" options set, the destination
184 * run will have the same length as the source run,
185 * and there is no mirroring and no keeping combining characters
186 * with their base characters.
187 */
188 if(destSize<srcLength) {
189 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
190 return srcLength;
191 }
192 destSize=srcLength;
193
194 /* preserve character integrity */
195 do {
196 /* i is always after the last code unit known to need to be kept in this segment */
197 i=srcLength;
198
199 /* collect code units for one base character */
200 UTF_BACK_1(src, 0, srcLength);
201
202 /* copy this base character */
203 j=srcLength;
204 do {
205 *dest++=src[j++];
206 } while(j<i);
207 } while(srcLength>0);
208 break;
209 case UBIDI_KEEP_BASE_COMBINING:
210 /*
211 * Here, too, the destination
212 * run will have the same length as the source run,
213 * and there is no mirroring.
214 * We do need to keep combining characters with their base characters.
215 */
216 if(destSize<srcLength) {
217 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
218 return srcLength;
219 }
220 destSize=srcLength;
221
222 /* preserve character integrity */
223 do {
224 /* i is always after the last code unit known to need to be kept in this segment */
225 i=srcLength;
226
227 /* collect code units and modifier letters for one base character */
228 do {
229 UTF_PREV_CHAR(src, 0, srcLength, c);
230 } while(srcLength>0 && IS_COMBINING(u_charType(c)));
231
232 /* copy this "user character" */
233 j=srcLength;
234 do {
235 *dest++=src[j++];
236 } while(j<i);
237 } while(srcLength>0);
238 break;
239 default:
240 /*
241 * With several "complicated" options set, this is the most
242 * general and the slowest copying of an RTL run.
243 * We will do mirroring, remove BiDi controls, and
244 * keep combining characters with their base characters
245 * as requested.
246 */
247 if(!(options&UBIDI_REMOVE_BIDI_CONTROLS)) {
248 i=srcLength;
249 } else {
250 /* we need to find out the destination length of the run,
251 which will not include the BiDi control characters */
252 int32_t length=srcLength;
253 UChar ch;
254
255 i=0;
256 do {
257 ch=*src++;
258 if(!IS_BIDI_CONTROL_CHAR(ch)) {
259 ++i;
260 }
261 } while(--length>0);
262 src-=srcLength;
263 }
264
265 if(destSize<i) {
266 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
267 return i;
268 }
269 destSize=i;
270
271 /* preserve character integrity */
272 do {
273 /* i is always after the last code unit known to need to be kept in this segment */
274 i=srcLength;
275
276 /* collect code units for one base character */
277 UTF_PREV_CHAR(src, 0, srcLength, c);
278 if(options&UBIDI_KEEP_BASE_COMBINING) {
279 /* collect modifier letters for this base character */
280 while(srcLength>0 && IS_COMBINING(u_charType(c))) {
281 UTF_PREV_CHAR(src, 0, srcLength, c);
282 }
283 }
284
285 if(options&UBIDI_REMOVE_BIDI_CONTROLS && IS_BIDI_CONTROL_CHAR(c)) {
286 /* do not copy this BiDi control character */
287 continue;
288 }
289
290 /* copy this "user character" */
291 j=srcLength;
292 if(options&UBIDI_DO_MIRRORING) {
293 /* mirror only the base character */
294 int32_t k=0;
295 c=u_charMirror(c);
296 UTF_APPEND_CHAR_UNSAFE(dest, k, c);
297 dest+=k;
298 j+=k;
299 }
300 while(j<i) {
301 *dest++=src[j++];
302 }
303 } while(srcLength>0);
304 break;
305 } /* end of switch */
306
307 return destSize;
308 }
309
310 U_CAPI int32_t U_EXPORT2
311 ubidi_writeReverse(const UChar *src, int32_t srcLength,
312 UChar *dest, int32_t destSize,
313 uint16_t options,
314 UErrorCode *pErrorCode) {
315 int32_t destLength;
316
317 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
318 return 0;
319 }
320
321 /* more error checking */
322 if( src==NULL || srcLength<-1 ||
323 destSize<0 || (destSize>0 && dest==NULL))
324 {
325 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
326 return 0;
327 }
328
329 /* do input and output overlap? */
330 if( dest!=NULL &&
331 ((src>=dest && src<dest+destSize) ||
332 (dest>=src && dest<src+srcLength)))
333 {
334 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
335 return 0;
336 }
337
338 if(srcLength==-1) {
339 srcLength=u_strlen(src);
340 }
341 if(srcLength>0) {
342 destLength=doWriteReverse(src, srcLength, dest, destSize, options, pErrorCode);
343 } else {
344 /* nothing to do */
345 destLength=0;
346 }
347
348 return u_terminateUChars(dest, destSize, destLength, pErrorCode);
349 }
350
351 #define MASK_R_AL (1UL<<U_RIGHT_TO_LEFT|1UL<<U_RIGHT_TO_LEFT_ARABIC)
352
353 U_CAPI int32_t U_EXPORT2
354 ubidi_writeReordered(UBiDi *pBiDi,
355 UChar *dest, int32_t destSize,
356 uint16_t options,
357 UErrorCode *pErrorCode) {
358 const UChar *text;
359 UChar *saveDest;
360 int32_t length, destCapacity;
361 int32_t run, runCount, logicalStart, runLength;
362
363 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
364 return 0;
365 }
366
367 /* more error checking */
368 if( pBiDi==NULL ||
369 (text=pBiDi->text)==NULL || (length=pBiDi->length)<0 ||
370 destSize<0 || (destSize>0 && dest==NULL))
371 {
372 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
373 return 0;
374 }
375
376 /* do input and output overlap? */
377 if( dest!=NULL &&
378 ((text>=dest && text<dest+destSize) ||
379 (dest>=text && dest<text+pBiDi->originalLength)))
380 {
381 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
382 return 0;
383 }
384
385 if(length==0) {
386 /* nothing to do */
387 return u_terminateUChars(dest, destSize, 0, pErrorCode);
388 }
389
390 runCount=ubidi_countRuns(pBiDi, pErrorCode);
391 if(U_FAILURE(*pErrorCode)) {
392 return 0;
393 }
394
395 /* destSize shrinks, later destination length=destCapacity-destSize */
396 saveDest=dest;
397 destCapacity=destSize;
398
399 /*
400 * Option "insert marks" implies UBIDI_INSERT_LRM_FOR_NUMERIC if the
401 * reordering mode (checked below) is appropriate.
402 */
403 if(pBiDi->reorderingOptions & UBIDI_OPTION_INSERT_MARKS) {
404 options|=UBIDI_INSERT_LRM_FOR_NUMERIC;
405 options&=~UBIDI_REMOVE_BIDI_CONTROLS;
406 }
407 /*
408 * Option "remove controls" implies UBIDI_REMOVE_BIDI_CONTROLS
409 * and cancels UBIDI_INSERT_LRM_FOR_NUMERIC.
410 */
411 if(pBiDi->reorderingOptions & UBIDI_OPTION_REMOVE_CONTROLS) {
412 options|=UBIDI_REMOVE_BIDI_CONTROLS;
413 options&=~UBIDI_INSERT_LRM_FOR_NUMERIC;
414 }
415 /*
416 * If we do not perform the "inverse BiDi" algorithm, then we
417 * don't need to insert any LRMs, and don't need to test for it.
418 */
419 if((pBiDi->reorderingMode != UBIDI_REORDER_INVERSE_NUMBERS_AS_L) &&
420 (pBiDi->reorderingMode != UBIDI_REORDER_INVERSE_LIKE_DIRECT) &&
421 (pBiDi->reorderingMode != UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL) &&
422 (pBiDi->reorderingMode != UBIDI_REORDER_RUNS_ONLY)) {
423 options&=~UBIDI_INSERT_LRM_FOR_NUMERIC;
424 }
425 /*
426 * Iterate through all visual runs and copy the run text segments to
427 * the destination, according to the options.
428 *
429 * The tests for where to insert LRMs ignore the fact that there may be
430 * BN codes or non-BMP code points at the beginning and end of a run;
431 * they may insert LRMs unnecessarily but the tests are faster this way
432 * (this would have to be improved for UTF-8).
433 *
434 * Note that the only errors that are set by doWriteXY() are buffer overflow
435 * errors. Ignore them until the end, and continue for preflighting.
436 */
437 if(!(options&UBIDI_OUTPUT_REVERSE)) {
438 /* forward output */
439 if(!(options&UBIDI_INSERT_LRM_FOR_NUMERIC)) {
440 /* do not insert BiDi controls */
441 for(run=0; run<runCount; ++run) {
442 if(UBIDI_LTR==ubidi_getVisualRun(pBiDi, run, &logicalStart, &runLength)) {
443 runLength=doWriteForward(text+logicalStart, runLength,
444 dest, destSize,
445 (uint16_t)(options&~UBIDI_DO_MIRRORING), pErrorCode);
446 } else {
447 runLength=doWriteReverse(text+logicalStart, runLength,
448 dest, destSize,
449 options, pErrorCode);
450 }
451 dest+=runLength;
452 destSize-=runLength;
453 }
454 } else {
455 /* insert BiDi controls for "inverse BiDi" */
456 const DirProp *dirProps=pBiDi->dirProps;
457 const UChar *src;
458 UChar uc;
459 UBiDiDirection dir;
460 int32_t markFlag;
461
462 for(run=0; run<runCount; ++run) {
463 dir=ubidi_getVisualRun(pBiDi, run, &logicalStart, &runLength);
464 src=text+logicalStart;
465 /* check if something relevant in insertPoints */
466 markFlag=pBiDi->runs[run].insertRemove;
467 if(markFlag<0) { /* insert count */
468 markFlag=0;
469 }
470
471 if(UBIDI_LTR==dir) {
472 if((pBiDi->isInverse) &&
473 (/*run>0 &&*/ dirProps[logicalStart]!=L)) {
474 markFlag |= LRM_BEFORE;
475 }
476 if (markFlag & LRM_BEFORE) {
477 uc=LRM_CHAR;
478 }
479 else if (markFlag & RLM_BEFORE) {
480 uc=RLM_CHAR;
481 }
482 else uc=0;
483 if(uc) {
484 if(destSize>0) {
485 *dest++=uc;
486 }
487 --destSize;
488 }
489
490 runLength=doWriteForward(src, runLength,
491 dest, destSize,
492 (uint16_t)(options&~UBIDI_DO_MIRRORING), pErrorCode);
493 dest+=runLength;
494 destSize-=runLength;
495
496 if((pBiDi->isInverse) &&
497 (/*run<runCount-1 &&*/ dirProps[logicalStart+runLength-1]!=L)) {
498 markFlag |= LRM_AFTER;
499 }
500 if (markFlag & LRM_AFTER) {
501 uc=LRM_CHAR;
502 }
503 else if (markFlag & RLM_AFTER) {
504 uc=RLM_CHAR;
505 }
506 else uc=0;
507 if(uc) {
508 if(destSize>0) {
509 *dest++=uc;
510 }
511 --destSize;
512 }
513 } else { /* RTL run */
514 if((pBiDi->isInverse) &&
515 (/*run>0 &&*/ !(MASK_R_AL&DIRPROP_FLAG(dirProps[logicalStart+runLength-1])))) {
516 markFlag |= RLM_BEFORE;
517 }
518 if (markFlag & LRM_BEFORE) {
519 uc=LRM_CHAR;
520 }
521 else if (markFlag & RLM_BEFORE) {
522 uc=RLM_CHAR;
523 }
524 else uc=0;
525 if(uc) {
526 if(destSize>0) {
527 *dest++=uc;
528 }
529 --destSize;
530 }
531
532 runLength=doWriteReverse(src, runLength,
533 dest, destSize,
534 options, pErrorCode);
535 dest+=runLength;
536 destSize-=runLength;
537
538 if((pBiDi->isInverse) &&
539 (/*run<runCount-1 &&*/ !(MASK_R_AL&DIRPROP_FLAG(dirProps[logicalStart])))) {
540 markFlag |= RLM_AFTER;
541 }
542 if (markFlag & LRM_AFTER) {
543 uc=LRM_CHAR;
544 }
545 else if (markFlag & RLM_AFTER) {
546 uc=RLM_CHAR;
547 }
548 else uc=0;
549 if(uc) {
550 if(destSize>0) {
551 *dest++=uc;
552 }
553 --destSize;
554 }
555 }
556 }
557 }
558 } else {
559 /* reverse output */
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,
565 dest, destSize,
566 (uint16_t)(options&~UBIDI_DO_MIRRORING), pErrorCode);
567 } else {
568 runLength=doWriteForward(text+logicalStart, runLength,
569 dest, destSize,
570 options, pErrorCode);
571 }
572 dest+=runLength;
573 destSize-=runLength;
574 }
575 } else {
576 /* insert BiDi controls for "inverse BiDi" */
577 const DirProp *dirProps=pBiDi->dirProps;
578 const UChar *src;
579 UBiDiDirection dir;
580
581 for(run=runCount; --run>=0;) {
582 /* reverse output */
583 dir=ubidi_getVisualRun(pBiDi, run, &logicalStart, &runLength);
584 src=text+logicalStart;
585
586 if(UBIDI_LTR==dir) {
587 if(/*run<runCount-1 &&*/ dirProps[logicalStart+runLength-1]!=L) {
588 if(destSize>0) {
589 *dest++=LRM_CHAR;
590 }
591 --destSize;
592 }
593
594 runLength=doWriteReverse(src, runLength,
595 dest, destSize,
596 (uint16_t)(options&~UBIDI_DO_MIRRORING), pErrorCode);
597 dest+=runLength;
598 destSize-=runLength;
599
600 if(/*run>0 &&*/ dirProps[logicalStart]!=L) {
601 if(destSize>0) {
602 *dest++=LRM_CHAR;
603 }
604 --destSize;
605 }
606 } else {
607 if(/*run<runCount-1 &&*/ !(MASK_R_AL&DIRPROP_FLAG(dirProps[logicalStart]))) {
608 if(destSize>0) {
609 *dest++=RLM_CHAR;
610 }
611 --destSize;
612 }
613
614 runLength=doWriteForward(src, runLength,
615 dest, destSize,
616 options, pErrorCode);
617 dest+=runLength;
618 destSize-=runLength;
619
620 if(/*run>0 &&*/ !(MASK_R_AL&DIRPROP_FLAG(dirProps[logicalStart+runLength-1]))) {
621 if(destSize>0) {
622 *dest++=RLM_CHAR;
623 }
624 --destSize;
625 }
626 }
627 }
628 }
629 }
630
631 return u_terminateUChars(saveDest, destCapacity, destCapacity-destSize, pErrorCode);
632 }