]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/unicode/ubidi.h
ICU-57131.0.1.tar.gz
[apple/icu.git] / icuSources / common / unicode / ubidi.h
CommitLineData
b75a7d8f
A
1/*
2******************************************************************************
3*
2ca993e8 4* Copyright (C) 1999-2015, International Business Machines
b75a7d8f
A
5* Corporation and others. All Rights Reserved.
6*
7******************************************************************************
8* file name: ubidi.h
9* encoding: US-ASCII
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 1999jul27
46f4442e 14* created by: Markus W. Scherer, updated by Matitiahu Allouche
b75a7d8f
A
15*/
16
17#ifndef UBIDI_H
18#define UBIDI_H
19
20#include "unicode/utypes.h"
21#include "unicode/uchar.h"
729e4ab9 22#include "unicode/localpointer.h"
b75a7d8f 23
b75a7d8f
A
24/**
25 *\file
46f4442e 26 * \brief C API: Bidi algorithm
b75a7d8f 27 *
46f4442e 28 * <h2>Bidi algorithm for ICU</h2>
b75a7d8f 29 *
729e4ab9 30 * This is an implementation of the Unicode Bidirectional Algorithm.
b75a7d8f 31 * The algorithm is defined in the
729e4ab9 32 * <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Standard Annex #9</a>.<p>
b75a7d8f
A
33 *
34 * Note: Libraries that perform a bidirectional algorithm and
35 * reorder strings accordingly are sometimes called "Storage Layout Engines".
46f4442e 36 * ICU's Bidi and shaping (u_shapeArabic()) APIs can be used at the core of such
b75a7d8f
A
37 * "Storage Layout Engines".
38 *
39 * <h3>General remarks about the API:</h3>
40 *
41 * In functions with an error code parameter,
42 * the <code>pErrorCode</code> pointer must be valid
43 * and the value that it points to must not indicate a failure before
44 * the function call. Otherwise, the function returns immediately.
45 * After the function call, the value indicates success or failure.<p>
46 *
47 * The &quot;limit&quot; of a sequence of characters is the position just after their
48 * last character, i.e., one more than that position.<p>
49 *
50 * Some of the API functions provide access to &quot;runs&quot;.
51 * Such a &quot;run&quot; is defined as a sequence of characters
52 * that are at the same embedding level
46f4442e 53 * after performing the Bidi algorithm.<p>
b75a7d8f
A
54 *
55 * @author Markus W. Scherer
56 * @version 1.0
57 *
58 *
46f4442e 59 * <h4> Sample code for the ICU Bidi API </h4>
b75a7d8f 60 *
46f4442e 61 * <h5>Rendering a paragraph with the ICU Bidi API</h5>
b75a7d8f
A
62 *
63 * This is (hypothetical) sample code that illustrates
46f4442e 64 * how the ICU Bidi API could be used to render a paragraph of text.
b75a7d8f
A
65 * Rendering code depends highly on the graphics system,
66 * therefore this sample code must make a lot of assumptions,
67 * which may or may not match any existing graphics system's properties.
68 *
69 * <p>The basic assumptions are:</p>
70 * <ul>
71 * <li>Rendering is done from left to right on a horizontal line.</li>
72 * <li>A run of single-style, unidirectional text can be rendered at once.</li>
73 * <li>Such a run of text is passed to the graphics system with
74 * characters (code units) in logical order.</li>
75 * <li>The line-breaking algorithm is very complicated
76 * and Locale-dependent -
77 * and therefore its implementation omitted from this sample code.</li>
78 * </ul>
79 *
80 * <pre>
81 * \code
82 *#include "unicode/ubidi.h"
83 *
84 *typedef enum {
85 * styleNormal=0, styleSelected=1,
86 * styleBold=2, styleItalics=4,
87 * styleSuper=8, styleSub=16
88 *} Style;
89 *
90 *typedef struct { int32_t limit; Style style; } StyleRun;
91 *
92 *int getTextWidth(const UChar *text, int32_t start, int32_t limit,
93 * const StyleRun *styleRuns, int styleRunCount);
94 *
95 * // set *pLimit and *pStyleRunLimit for a line
96 * // from text[start] and from styleRuns[styleRunStart]
97 * // using ubidi_getLogicalRun(para, ...)
98 *void getLineBreak(const UChar *text, int32_t start, int32_t *pLimit,
99 * UBiDi *para,
100 * const StyleRun *styleRuns, int styleRunStart, int *pStyleRunLimit,
101 * int *pLineWidth);
102 *
103 * // render runs on a line sequentially, always from left to right
104 *
105 * // prepare rendering a new line
106 * void startLine(UBiDiDirection textDirection, int lineWidth);
107 *
108 * // render a run of text and advance to the right by the run width
109 * // the text[start..limit-1] is always in logical order
110 * void renderRun(const UChar *text, int32_t start, int32_t limit,
111 * UBiDiDirection textDirection, Style style);
112 *
113 * // We could compute a cross-product
114 * // from the style runs with the directional runs
115 * // and then reorder it.
116 * // Instead, here we iterate over each run type
117 * // and render the intersections -
118 * // with shortcuts in simple (and common) cases.
119 * // renderParagraph() is the main function.
120 *
121 * // render a directional run with
122 * // (possibly) multiple style runs intersecting with it
123 * void renderDirectionalRun(const UChar *text,
124 * int32_t start, int32_t limit,
125 * UBiDiDirection direction,
126 * const StyleRun *styleRuns, int styleRunCount) {
127 * int i;
128 *
129 * // iterate over style runs
130 * if(direction==UBIDI_LTR) {
131 * int styleLimit;
132 *
133 * for(i=0; i<styleRunCount; ++i) {
134 * styleLimit=styleRun[i].limit;
135 * if(start<styleLimit) {
136 * if(styleLimit>limit) { styleLimit=limit; }
137 * renderRun(text, start, styleLimit,
138 * direction, styleRun[i].style);
139 * if(styleLimit==limit) { break; }
140 * start=styleLimit;
141 * }
142 * }
143 * } else {
144 * int styleStart;
145 *
146 * for(i=styleRunCount-1; i>=0; --i) {
147 * if(i>0) {
148 * styleStart=styleRun[i-1].limit;
149 * } else {
150 * styleStart=0;
151 * }
152 * if(limit>=styleStart) {
153 * if(styleStart<start) { styleStart=start; }
154 * renderRun(text, styleStart, limit,
155 * direction, styleRun[i].style);
156 * if(styleStart==start) { break; }
157 * limit=styleStart;
158 * }
159 * }
160 * }
161 * }
162 *
163 * // the line object represents text[start..limit-1]
164 * void renderLine(UBiDi *line, const UChar *text,
165 * int32_t start, int32_t limit,
166 * const StyleRun *styleRuns, int styleRunCount) {
167 * UBiDiDirection direction=ubidi_getDirection(line);
168 * if(direction!=UBIDI_MIXED) {
169 * // unidirectional
170 * if(styleRunCount<=1) {
171 * renderRun(text, start, limit, direction, styleRuns[0].style);
172 * } else {
173 * renderDirectionalRun(text, start, limit,
174 * direction, styleRuns, styleRunCount);
175 * }
176 * } else {
177 * // mixed-directional
178 * int32_t count, i, length;
179 * UBiDiLevel level;
180 *
181 * count=ubidi_countRuns(para, pErrorCode);
182 * if(U_SUCCESS(*pErrorCode)) {
183 * if(styleRunCount<=1) {
184 * Style style=styleRuns[0].style;
185 *
186 * // iterate over directional runs
187 * for(i=0; i<count; ++i) {
188 * direction=ubidi_getVisualRun(para, i, &start, &length);
189 * renderRun(text, start, start+length, direction, style);
190 * }
191 * } else {
192 * int32_t j;
193 *
194 * // iterate over both directional and style runs
195 * for(i=0; i<count; ++i) {
196 * direction=ubidi_getVisualRun(line, i, &start, &length);
197 * renderDirectionalRun(text, start, start+length,
198 * direction, styleRuns, styleRunCount);
199 * }
200 * }
201 * }
202 * }
203 * }
204 *
205 *void renderParagraph(const UChar *text, int32_t length,
206 * UBiDiDirection textDirection,
207 * const StyleRun *styleRuns, int styleRunCount,
208 * int lineWidth,
209 * UErrorCode *pErrorCode) {
210 * UBiDi *para;
211 *
212 * if(pErrorCode==NULL || U_FAILURE(*pErrorCode) || length<=0) {
213 * return;
214 * }
215 *
216 * para=ubidi_openSized(length, 0, pErrorCode);
217 * if(para==NULL) { return; }
218 *
219 * ubidi_setPara(para, text, length,
220 * textDirection ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR,
221 * NULL, pErrorCode);
222 * if(U_SUCCESS(*pErrorCode)) {
223 * UBiDiLevel paraLevel=1&ubidi_getParaLevel(para);
224 * StyleRun styleRun={ length, styleNormal };
225 * int width;
226 *
227 * if(styleRuns==NULL || styleRunCount<=0) {
228 * styleRunCount=1;
229 * styleRuns=&styleRun;
230 * }
231 *
232 * // assume styleRuns[styleRunCount-1].limit>=length
233 *
234 * width=getTextWidth(text, 0, length, styleRuns, styleRunCount);
235 * if(width<=lineWidth) {
236 * // everything fits onto one line
237 *
238 * // prepare rendering a new line from either left or right
239 * startLine(paraLevel, width);
240 *
241 * renderLine(para, text, 0, length,
242 * styleRuns, styleRunCount);
243 * } else {
244 * UBiDi *line;
245 *
246 * // we need to render several lines
247 * line=ubidi_openSized(length, 0, pErrorCode);
248 * if(line!=NULL) {
249 * int32_t start=0, limit;
250 * int styleRunStart=0, styleRunLimit;
251 *
252 * for(;;) {
253 * limit=length;
254 * styleRunLimit=styleRunCount;
255 * getLineBreak(text, start, &limit, para,
256 * styleRuns, styleRunStart, &styleRunLimit,
257 * &width);
258 * ubidi_setLine(para, start, limit, line, pErrorCode);
259 * if(U_SUCCESS(*pErrorCode)) {
260 * // prepare rendering a new line
261 * // from either left or right
262 * startLine(paraLevel, width);
263 *
264 * renderLine(line, text, start, limit,
265 * styleRuns+styleRunStart,
266 * styleRunLimit-styleRunStart);
267 * }
268 * if(limit==length) { break; }
269 * start=limit;
270 * styleRunStart=styleRunLimit-1;
271 * if(start>=styleRuns[styleRunStart].limit) {
272 * ++styleRunStart;
273 * }
274 * }
275 *
276 * ubidi_close(line);
277 * }
278 * }
279 * }
280 *
281 * ubidi_close(para);
282 *}
283 *\endcode
284 * </pre>
285 */
374ca955 286
b75a7d8f
A
287/*DOCXX_TAG*/
288/*@{*/
289
290/**
291 * UBiDiLevel is the type of the level values in this
46f4442e 292 * Bidi implementation.
b75a7d8f
A
293 * It holds an embedding level and indicates the visual direction
294 * by its bit&nbsp;0 (even/odd value).<p>
295 *
296 * It can also hold non-level values for the
297 * <code>paraLevel</code> and <code>embeddingLevels</code>
298 * arguments of <code>ubidi_setPara()</code>; there:
299 * <ul>
300 * <li>bit&nbsp;7 of an <code>embeddingLevels[]</code>
301 * value indicates whether the using application is
302 * specifying the level of a character to <i>override</i> whatever the
46f4442e 303 * Bidi implementation would resolve it to.</li>
b75a7d8f 304 * <li><code>paraLevel</code> can be set to the
374ca955 305 * pseudo-level values <code>UBIDI_DEFAULT_LTR</code>
b75a7d8f
A
306 * and <code>UBIDI_DEFAULT_RTL</code>.</li>
307 * </ul>
308 *
309 * @see ubidi_setPara
310 *
311 * <p>The related constants are not real, valid level values.
312 * <code>UBIDI_DEFAULT_XXX</code> can be used to specify
313 * a default for the paragraph level for
314 * when the <code>ubidi_setPara()</code> function
315 * shall determine it but there is no
316 * strongly typed character in the input.<p>
317 *
318 * Note that the value for <code>UBIDI_DEFAULT_LTR</code> is even
319 * and the one for <code>UBIDI_DEFAULT_RTL</code> is odd,
320 * just like with normal LTR and RTL level values -
321 * these special values are designed that way. Also, the implementation
322 * assumes that UBIDI_MAX_EXPLICIT_LEVEL is odd.
323 *
324 * @see UBIDI_DEFAULT_LTR
325 * @see UBIDI_DEFAULT_RTL
326 * @see UBIDI_LEVEL_OVERRIDE
327 * @see UBIDI_MAX_EXPLICIT_LEVEL
328 * @stable ICU 2.0
329 */
330typedef uint8_t UBiDiLevel;
331
46f4442e
A
332/** Paragraph level setting.<p>
333 *
334 * Constant indicating that the base direction depends on the first strong
335 * directional character in the text according to the Unicode Bidirectional
336 * Algorithm. If no strong directional character is present,
337 * then set the paragraph level to 0 (left-to-right).<p>
338 *
339 * If this value is used in conjunction with reordering modes
340 * <code>UBIDI_REORDER_INVERSE_LIKE_DIRECT</code> or
341 * <code>UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL</code>, the text to reorder
342 * is assumed to be visual LTR, and the text after reordering is required
343 * to be the corresponding logical string with appropriate contextual
344 * direction. The direction of the result string will be RTL if either
345 * the righmost or leftmost strong character of the source text is RTL
346 * or Arabic Letter, the direction will be LTR otherwise.<p>
347 *
348 * If reordering option <code>UBIDI_OPTION_INSERT_MARKS</code> is set, an RLM may
349 * be added at the beginning of the result string to ensure round trip
350 * (that the result string, when reordered back to visual, will produce
351 * the original source text).
352 * @see UBIDI_REORDER_INVERSE_LIKE_DIRECT
353 * @see UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL
b75a7d8f
A
354 * @stable ICU 2.0
355 */
356#define UBIDI_DEFAULT_LTR 0xfe
357
46f4442e
A
358/** Paragraph level setting.<p>
359 *
360 * Constant indicating that the base direction depends on the first strong
361 * directional character in the text according to the Unicode Bidirectional
362 * Algorithm. If no strong directional character is present,
363 * then set the paragraph level to 1 (right-to-left).<p>
364 *
365 * If this value is used in conjunction with reordering modes
366 * <code>UBIDI_REORDER_INVERSE_LIKE_DIRECT</code> or
367 * <code>UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL</code>, the text to reorder
368 * is assumed to be visual LTR, and the text after reordering is required
369 * to be the corresponding logical string with appropriate contextual
370 * direction. The direction of the result string will be RTL if either
371 * the righmost or leftmost strong character of the source text is RTL
372 * or Arabic Letter, or if the text contains no strong character;
373 * the direction will be LTR otherwise.<p>
374 *
375 * If reordering option <code>UBIDI_OPTION_INSERT_MARKS</code> is set, an RLM may
376 * be added at the beginning of the result string to ensure round trip
377 * (that the result string, when reordered back to visual, will produce
378 * the original source text).
379 * @see UBIDI_REORDER_INVERSE_LIKE_DIRECT
380 * @see UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL
b75a7d8f
A
381 * @stable ICU 2.0
382 */
383#define UBIDI_DEFAULT_RTL 0xff
384
385/**
386 * Maximum explicit embedding level.
387 * (The maximum resolved level can be up to <code>UBIDI_MAX_EXPLICIT_LEVEL+1</code>).
388 * @stable ICU 2.0
389 */
57a6839d 390#define UBIDI_MAX_EXPLICIT_LEVEL 125
b75a7d8f 391
374ca955
A
392/** Bit flag for level input.
393 * Overrides directional properties.
b75a7d8f
A
394 * @stable ICU 2.0
395 */
396#define UBIDI_LEVEL_OVERRIDE 0x80
397
73c04bcf
A
398/**
399 * Special value which can be returned by the mapping functions when a logical
400 * index has no corresponding visual index or vice-versa. This may happen
46f4442e 401 * for the logical-to-visual mapping of a Bidi control when option
73c04bcf 402 * <code>#UBIDI_OPTION_REMOVE_CONTROLS</code> is specified. This can also happen
46f4442e 403 * for the visual-to-logical mapping of a Bidi mark (LRM or RLM) inserted
73c04bcf
A
404 * by option <code>#UBIDI_OPTION_INSERT_MARKS</code>.
405 * @see ubidi_getVisualIndex
406 * @see ubidi_getVisualMap
407 * @see ubidi_getLogicalIndex
408 * @see ubidi_getLogicalMap
46f4442e 409 * @stable ICU 3.6
73c04bcf
A
410 */
411#define UBIDI_MAP_NOWHERE (-1)
412
b75a7d8f 413/**
374ca955 414 * <code>UBiDiDirection</code> values indicate the text direction.
b75a7d8f
A
415 * @stable ICU 2.0
416 */
417enum UBiDiDirection {
729e4ab9
A
418 /** Left-to-right text. This is a 0 value.
419 * <ul>
420 * <li>As return value for <code>ubidi_getDirection()</code>, it means
421 * that the source string contains no right-to-left characters, or
422 * that the source string is empty and the paragraph level is even.
423 * <li> As return value for <code>ubidi_getBaseDirection()</code>, it
424 * means that the first strong character of the source string has
425 * a left-to-right direction.
426 * </ul>
427 * @stable ICU 2.0
428 */
429 UBIDI_LTR,
430 /** Right-to-left text. This is a 1 value.
431 * <ul>
432 * <li>As return value for <code>ubidi_getDirection()</code>, it means
433 * that the source string contains no left-to-right characters, or
434 * that the source string is empty and the paragraph level is odd.
435 * <li> As return value for <code>ubidi_getBaseDirection()</code>, it
436 * means that the first strong character of the source string has
437 * a right-to-left direction.
438 * </ul>
439 * @stable ICU 2.0
440 */
441 UBIDI_RTL,
442 /** Mixed-directional text.
443 * <p>As return value for <code>ubidi_getDirection()</code>, it means
444 * that the source string contains both left-to-right and
445 * right-to-left characters.
446 * @stable ICU 2.0
447 */
448 UBIDI_MIXED,
449 /** No strongly directional text.
450 * <p>As return value for <code>ubidi_getBaseDirection()</code>, it means
451 * that the source string is missing or empty, or contains neither left-to-right
452 * nor right-to-left characters.
4388f060 453 * @stable ICU 4.6
729e4ab9
A
454 */
455 UBIDI_NEUTRAL
b75a7d8f
A
456};
457
458/** @stable ICU 2.0 */
459typedef enum UBiDiDirection UBiDiDirection;
460
461/**
462 * Forward declaration of the <code>UBiDi</code> structure for the declaration of
463 * the API functions. Its fields are implementation-specific.<p>
73c04bcf 464 * This structure holds information about a paragraph (or multiple paragraphs)
46f4442e 465 * of text with Bidi-algorithm-related details, or about one line of
b75a7d8f 466 * such a paragraph.<p>
73c04bcf
A
467 * Reordering can be done on a line, or on one or more paragraphs which are
468 * then interpreted each as one single line.
b75a7d8f
A
469 * @stable ICU 2.0
470 */
471struct UBiDi;
472
473/** @stable ICU 2.0 */
474typedef struct UBiDi UBiDi;
475
476/**
477 * Allocate a <code>UBiDi</code> structure.
478 * Such an object is initially empty. It is assigned
46f4442e 479 * the Bidi properties of a piece of text containing one or more paragraphs
73c04bcf 480 * by <code>ubidi_setPara()</code>
46f4442e 481 * or the Bidi properties of a line within a paragraph by
b75a7d8f
A
482 * <code>ubidi_setLine()</code>.<p>
483 * This object can be reused for as long as it is not deallocated
484 * by calling <code>ubidi_close()</code>.<p>
73c04bcf
A
485 * <code>ubidi_setPara()</code> and <code>ubidi_setLine()</code> will allocate
486 * additional memory for internal structures as necessary.
b75a7d8f
A
487 *
488 * @return An empty <code>UBiDi</code> object.
489 * @stable ICU 2.0
490 */
374ca955 491U_STABLE UBiDi * U_EXPORT2
b75a7d8f
A
492ubidi_open(void);
493
494/**
495 * Allocate a <code>UBiDi</code> structure with preallocated memory
496 * for internal structures.
497 * This function provides a <code>UBiDi</code> object like <code>ubidi_open()</code>
498 * with no arguments, but it also preallocates memory for internal structures
499 * according to the sizings supplied by the caller.<p>
500 * Subsequent functions will not allocate any more memory, and are thus
501 * guaranteed not to fail because of lack of memory.<p>
502 * The preallocation can be limited to some of the internal memory
503 * by setting some values to 0 here. That means that if, e.g.,
504 * <code>maxRunCount</code> cannot be reasonably predetermined and should not
505 * be set to <code>maxLength</code> (the only failproof value) to avoid
506 * wasting memory, then <code>maxRunCount</code> could be set to 0 here
507 * and the internal structures that are associated with it will be allocated
508 * on demand, just like with <code>ubidi_open()</code>.
509 *
73c04bcf 510 * @param maxLength is the maximum text or line length that internal memory
b75a7d8f
A
511 * will be preallocated for. An attempt to associate this object with a
512 * longer text will fail, unless this value is 0, which leaves the allocation
513 * up to the implementation.
514 *
515 * @param maxRunCount is the maximum anticipated number of same-level runs
516 * that internal memory will be preallocated for. An attempt to access
517 * visual runs on an object that was not preallocated for as many runs
518 * as the text was actually resolved to will fail,
73c04bcf 519 * unless this value is 0, which leaves the allocation up to the implementation.<br><br>
b75a7d8f 520 * The number of runs depends on the actual text and maybe anywhere between
73c04bcf 521 * 1 and <code>maxLength</code>. It is typically small.
b75a7d8f 522 *
73c04bcf 523 * @param pErrorCode must be a valid pointer to an error code value.
b75a7d8f
A
524 *
525 * @return An empty <code>UBiDi</code> object with preallocated memory.
526 * @stable ICU 2.0
527 */
374ca955 528U_STABLE UBiDi * U_EXPORT2
b75a7d8f
A
529ubidi_openSized(int32_t maxLength, int32_t maxRunCount, UErrorCode *pErrorCode);
530
531/**
532 * <code>ubidi_close()</code> must be called to free the memory
533 * associated with a UBiDi object.<p>
534 *
535 * <strong>Important: </strong>
374ca955
A
536 * A parent <code>UBiDi</code> object must not be destroyed or reused if
537 * it still has children.
73c04bcf
A
538 * If a <code>UBiDi</code> object has become the <i>child</i>
539 * of another one (its <i>parent</i>) by calling
b75a7d8f
A
540 * <code>ubidi_setLine()</code>, then the child object must
541 * be destroyed (closed) or reused (by calling
542 * <code>ubidi_setPara()</code> or <code>ubidi_setLine()</code>)
543 * before the parent object.
544 *
545 * @param pBiDi is a <code>UBiDi</code> object.
546 *
547 * @see ubidi_setPara
548 * @see ubidi_setLine
549 * @stable ICU 2.0
550 */
374ca955 551U_STABLE void U_EXPORT2
b75a7d8f
A
552ubidi_close(UBiDi *pBiDi);
553
729e4ab9
A
554#if U_SHOW_CPLUSPLUS_API
555
556U_NAMESPACE_BEGIN
557
558/**
559 * \class LocalUBiDiPointer
560 * "Smart pointer" class, closes a UBiDi via ubidi_close().
561 * For most methods see the LocalPointerBase base class.
562 *
563 * @see LocalPointerBase
564 * @see LocalPointer
565 * @stable ICU 4.4
566 */
567U_DEFINE_LOCAL_OPEN_POINTER(LocalUBiDiPointer, UBiDi, ubidi_close);
568
569U_NAMESPACE_END
570
571#endif
572
b75a7d8f 573/**
46f4442e
A
574 * Modify the operation of the Bidi algorithm such that it
575 * approximates an "inverse Bidi" algorithm. This function
b75a7d8f
A
576 * must be called before <code>ubidi_setPara()</code>.
577 *
46f4442e 578 * <p>The normal operation of the Bidi algorithm as described
b75a7d8f
A
579 * in the Unicode Technical Report is to take text stored in logical
580 * (keyboard, typing) order and to determine the reordering of it for visual
581 * rendering.
374ca955 582 * Some legacy systems store text in visual order, and for operations
b75a7d8f
A
583 * with standard, Unicode-based algorithms, the text needs to be transformed
584 * to logical order. This is effectively the inverse algorithm of the
46f4442e
A
585 * described Bidi algorithm. Note that there is no standard algorithm for
586 * this "inverse Bidi" and that the current implementation provides only an
587 * approximation of "inverse Bidi".</p>
374ca955 588 *
b75a7d8f
A
589 * <p>With <code>isInverse</code> set to <code>TRUE</code>,
590 * this function changes the behavior of some of the subsequent functions
46f4442e 591 * in a way that they can be used for the inverse Bidi algorithm.
b75a7d8f
A
592 * Specifically, runs of text with numeric characters will be treated in a
593 * special way and may need to be surrounded with LRM characters when they are
594 * written in reordered sequence.</p>
595 *
596 * <p>Output runs should be retrieved using <code>ubidi_getVisualRun()</code>.
46f4442e 597 * Since the actual input for "inverse Bidi" is visually ordered text and
b75a7d8f
A
598 * <code>ubidi_getVisualRun()</code> gets the reordered runs, these are actually
599 * the runs of the logically ordered output.</p>
600 *
73c04bcf
A
601 * <p>Calling this function with argument <code>isInverse</code> set to
602 * <code>TRUE</code> is equivalent to calling
603 * <code>ubidi_setReorderingMode</code> with argument
604 * <code>reorderingMode</code>
605 * set to <code>#UBIDI_REORDER_INVERSE_NUMBERS_AS_L</code>.<br>
606 * Calling this function with argument <code>isInverse</code> set to
607 * <code>FALSE</code> is equivalent to calling
608 * <code>ubidi_setReorderingMode</code> with argument
609 * <code>reorderingMode</code>
610 * set to <code>#UBIDI_REORDER_DEFAULT</code>.
611 *
b75a7d8f
A
612 * @param pBiDi is a <code>UBiDi</code> object.
613 *
46f4442e 614 * @param isInverse specifies "forward" or "inverse" Bidi operation.
b75a7d8f
A
615 *
616 * @see ubidi_setPara
617 * @see ubidi_writeReordered
73c04bcf 618 * @see ubidi_setReorderingMode
b75a7d8f
A
619 * @stable ICU 2.0
620 */
374ca955 621U_STABLE void U_EXPORT2
b75a7d8f
A
622ubidi_setInverse(UBiDi *pBiDi, UBool isInverse);
623
624/**
46f4442e 625 * Is this Bidi object set to perform the inverse Bidi algorithm?
73c04bcf
A
626 * <p>Note: calling this function after setting the reordering mode with
627 * <code>ubidi_setReorderingMode</code> will return <code>TRUE</code> if the
628 * reordering mode was set to <code>#UBIDI_REORDER_INVERSE_NUMBERS_AS_L</code>,
629 * <code>FALSE</code> for all other values.</p>
b75a7d8f
A
630 *
631 * @param pBiDi is a <code>UBiDi</code> object.
46f4442e 632 * @return TRUE if the Bidi object is set to perform the inverse Bidi algorithm
73c04bcf 633 * by handling numbers as L.
b75a7d8f
A
634 *
635 * @see ubidi_setInverse
73c04bcf 636 * @see ubidi_setReorderingMode
b75a7d8f
A
637 * @stable ICU 2.0
638 */
73c04bcf 639
374ca955 640U_STABLE UBool U_EXPORT2
b75a7d8f
A
641ubidi_isInverse(UBiDi *pBiDi);
642
73c04bcf
A
643/**
644 * Specify whether block separators must be allocated level zero,
645 * so that successive paragraphs will progress from left to right.
646 * This function must be called before <code>ubidi_setPara()</code>.
647 * Paragraph separators (B) may appear in the text. Setting them to level zero
648 * means that all paragraph separators (including one possibly appearing
649 * in the last text position) are kept in the reordered text after the text
650 * that they follow in the source text.
651 * When this feature is not enabled, a paragraph separator at the last
652 * position of the text before reordering will go to the first position
653 * of the reordered text when the paragraph level is odd.
654 *
655 * @param pBiDi is a <code>UBiDi</code> object.
656 *
657 * @param orderParagraphsLTR specifies whether paragraph separators (B) must
658 * receive level 0, so that successive paragraphs progress from left to right.
659 *
660 * @see ubidi_setPara
661 * @stable ICU 3.4
662 */
663U_STABLE void U_EXPORT2
664ubidi_orderParagraphsLTR(UBiDi *pBiDi, UBool orderParagraphsLTR);
665
666/**
46f4442e 667 * Is this Bidi object set to allocate level 0 to block separators so that
73c04bcf
A
668 * successive paragraphs progress from left to right?
669 *
670 * @param pBiDi is a <code>UBiDi</code> object.
46f4442e 671 * @return TRUE if the Bidi object is set to allocate level 0 to block
73c04bcf
A
672 * separators.
673 *
674 * @see ubidi_orderParagraphsLTR
675 * @stable ICU 3.4
676 */
677U_STABLE UBool U_EXPORT2
678ubidi_isOrderParagraphsLTR(UBiDi *pBiDi);
679
680/**
46f4442e 681 * <code>UBiDiReorderingMode</code> values indicate which variant of the Bidi
73c04bcf
A
682 * algorithm to use.
683 *
684 * @see ubidi_setReorderingMode
46f4442e 685 * @stable ICU 3.6
73c04bcf
A
686 */
687typedef enum UBiDiReorderingMode {
46f4442e
A
688 /** Regular Logical to Visual Bidi algorithm according to Unicode.
689 * This is a 0 value.
690 * @stable ICU 3.6 */
73c04bcf
A
691 UBIDI_REORDER_DEFAULT = 0,
692 /** Logical to Visual algorithm which handles numbers in a way which
693 * mimicks the behavior of Windows XP.
46f4442e 694 * @stable ICU 3.6 */
73c04bcf
A
695 UBIDI_REORDER_NUMBERS_SPECIAL,
696 /** Logical to Visual algorithm grouping numbers with adjacent R characters
697 * (reversible algorithm).
46f4442e 698 * @stable ICU 3.6 */
73c04bcf
A
699 UBIDI_REORDER_GROUP_NUMBERS_WITH_R,
700 /** Reorder runs only to transform a Logical LTR string to the Logical RTL
701 * string with the same display, or vice-versa.<br>
702 * If this mode is set together with option
46f4442e 703 * <code>#UBIDI_OPTION_INSERT_MARKS</code>, some Bidi controls in the source
73c04bcf
A
704 * text may be removed and other controls may be added to produce the
705 * minimum combination which has the required display.
46f4442e 706 * @stable ICU 3.6 */
73c04bcf
A
707 UBIDI_REORDER_RUNS_ONLY,
708 /** Visual to Logical algorithm which handles numbers like L
709 * (same algorithm as selected by <code>ubidi_setInverse(TRUE)</code>.
710 * @see ubidi_setInverse
46f4442e 711 * @stable ICU 3.6 */
73c04bcf
A
712 UBIDI_REORDER_INVERSE_NUMBERS_AS_L,
713 /** Visual to Logical algorithm equivalent to the regular Logical to Visual
46f4442e
A
714 * algorithm.
715 * @stable ICU 3.6 */
73c04bcf 716 UBIDI_REORDER_INVERSE_LIKE_DIRECT,
46f4442e
A
717 /** Inverse Bidi (Visual to Logical) algorithm for the
718 * <code>UBIDI_REORDER_NUMBERS_SPECIAL</code> Bidi algorithm.
719 * @stable ICU 3.6 */
73c04bcf
A
720 UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL,
721 /** Number of values for reordering mode.
46f4442e 722 * @stable ICU 3.6 */
73c04bcf
A
723 UBIDI_REORDER_COUNT
724} UBiDiReorderingMode;
725
726/**
46f4442e
A
727 * Modify the operation of the Bidi algorithm such that it implements some
728 * variant to the basic Bidi algorithm or approximates an "inverse Bidi"
73c04bcf
A
729 * algorithm, depending on different values of the "reordering mode".
730 * This function must be called before <code>ubidi_setPara()</code>, and stays
731 * in effect until called again with a different argument.
732 *
46f4442e 733 * <p>The normal operation of the Bidi algorithm as described
73c04bcf
A
734 * in the Unicode Standard Annex #9 is to take text stored in logical
735 * (keyboard, typing) order and to determine how to reorder it for visual
736 * rendering.</p>
737 *
738 * <p>With the reordering mode set to a value other than
739 * <code>#UBIDI_REORDER_DEFAULT</code>, this function changes the behavior of
740 * some of the subsequent functions in a way such that they implement an
46f4442e 741 * inverse Bidi algorithm or some other algorithm variants.</p>
73c04bcf
A
742 *
743 * <p>Some legacy systems store text in visual order, and for operations
744 * with standard, Unicode-based algorithms, the text needs to be transformed
745 * into logical order. This is effectively the inverse algorithm of the
46f4442e
A
746 * described Bidi algorithm. Note that there is no standard algorithm for
747 * this "inverse Bidi", so a number of variants are implemented here.</p>
73c04bcf
A
748 *
749 * <p>In other cases, it may be desirable to emulate some variant of the
750 * Logical to Visual algorithm (e.g. one used in MS Windows), or perform a
751 * Logical to Logical transformation.</p>
752 *
753 * <ul>
754 * <li>When the reordering mode is set to <code>#UBIDI_REORDER_DEFAULT</code>,
46f4442e 755 * the standard Bidi Logical to Visual algorithm is applied.</li>
73c04bcf
A
756 *
757 * <li>When the reordering mode is set to
758 * <code>#UBIDI_REORDER_NUMBERS_SPECIAL</code>,
46f4442e 759 * the algorithm used to perform Bidi transformations when calling
73c04bcf 760 * <code>ubidi_setPara</code> should approximate the algorithm used in
46f4442e 761 * Microsoft Windows XP rather than strictly conform to the Unicode Bidi
73c04bcf
A
762 * algorithm.
763 * <br>
764 * The differences between the basic algorithm and the algorithm addressed
765 * by this option are as follows:
766 * <ul>
767 * <li>Within text at an even embedding level, the sequence "123AB"
768 * (where AB represent R or AL letters) is transformed to "123BA" by the
769 * Unicode algorithm and to "BA123" by the Windows algorithm.</li>
770 * <li>Arabic-Indic numbers (AN) are handled by the Windows algorithm just
771 * like regular numbers (EN).</li>
772 * </ul></li>
773 *
774 * <li>When the reordering mode is set to
775 * <code>#UBIDI_REORDER_GROUP_NUMBERS_WITH_R</code>,
776 * numbers located between LTR text and RTL text are associated with the RTL
777 * text. For instance, an LTR paragraph with content "abc 123 DEF" (where
778 * upper case letters represent RTL characters) will be transformed to
779 * "abc FED 123" (and not "abc 123 FED"), "DEF 123 abc" will be transformed
780 * to "123 FED abc" and "123 FED abc" will be transformed to "DEF 123 abc".
781 * This makes the algorithm reversible and makes it useful when round trip
782 * (from visual to logical and back to visual) must be achieved without
783 * adding LRM characters. However, this is a variation from the standard
784 * Unicode Bidi algorithm.<br>
46f4442e 785 * The source text should not contain Bidi control characters other than LRM
73c04bcf
A
786 * or RLM.</li>
787 *
788 * <li>When the reordering mode is set to
789 * <code>#UBIDI_REORDER_RUNS_ONLY</code>,
790 * a "Logical to Logical" transformation must be performed:
791 * <ul>
792 * <li>If the default text level of the source text (argument <code>paraLevel</code>
793 * in <code>ubidi_setPara</code>) is even, the source text will be handled as
794 * LTR logical text and will be transformed to the RTL logical text which has
795 * the same LTR visual display.</li>
796 * <li>If the default level of the source text is odd, the source text
797 * will be handled as RTL logical text and will be transformed to the
798 * LTR logical text which has the same LTR visual display.</li>
799 * </ul>
800 * This mode may be needed when logical text which is basically Arabic or
801 * Hebrew, with possible included numbers or phrases in English, has to be
802 * displayed as if it had an even embedding level (this can happen if the
46f4442e 803 * displaying application treats all text as if it was basically LTR).
73c04bcf
A
804 * <br>
805 * This mode may also be needed in the reverse case, when logical text which is
806 * basically English, with possible included phrases in Arabic or Hebrew, has to
807 * be displayed as if it had an odd embedding level.
808 * <br>
809 * Both cases could be handled by adding LRE or RLE at the head of the text,
810 * if the display subsystem supports these formatting controls. If it does not,
811 * the problem may be handled by transforming the source text in this mode
812 * before displaying it, so that it will be displayed properly.<br>
46f4442e 813 * The source text should not contain Bidi control characters other than LRM
73c04bcf
A
814 * or RLM.</li>
815 *
816 * <li>When the reordering mode is set to
46f4442e 817 * <code>#UBIDI_REORDER_INVERSE_NUMBERS_AS_L</code>, an "inverse Bidi" algorithm
73c04bcf
A
818 * is applied.
819 * Runs of text with numeric characters will be treated like LTR letters and
820 * may need to be surrounded with LRM characters when they are written in
821 * reordered sequence (the option <code>#UBIDI_INSERT_LRM_FOR_NUMERIC</code> can
822 * be used with function <code>ubidi_writeReordered</code> to this end. This
823 * mode is equivalent to calling <code>ubidi_setInverse()</code> with
824 * argument <code>isInverse</code> set to <code>TRUE</code>.</li>
825 *
826 * <li>When the reordering mode is set to
827 * <code>#UBIDI_REORDER_INVERSE_LIKE_DIRECT</code>, the "direct" Logical to Visual
46f4442e 828 * Bidi algorithm is used as an approximation of an "inverse Bidi" algorithm.
73c04bcf 829 * This mode is similar to mode <code>#UBIDI_REORDER_INVERSE_NUMBERS_AS_L</code>
46f4442e 830 * but is closer to the regular Bidi algorithm.
73c04bcf
A
831 * <br>
832 * For example, an LTR paragraph with the content "FED 123 456 CBA" (where
833 * upper case represents RTL characters) will be transformed to
834 * "ABC 456 123 DEF", as opposed to "DEF 123 456 ABC"
835 * with mode <code>UBIDI_REORDER_INVERSE_NUMBERS_AS_L</code>.<br>
836 * When used in conjunction with option
837 * <code>#UBIDI_OPTION_INSERT_MARKS</code>, this mode generally
46f4442e 838 * adds Bidi marks to the output significantly more sparingly than mode
73c04bcf
A
839 * <code>#UBIDI_REORDER_INVERSE_NUMBERS_AS_L</code> with option
840 * <code>#UBIDI_INSERT_LRM_FOR_NUMERIC</code> in calls to
841 * <code>ubidi_writeReordered</code>.</li>
842 *
843 * <li>When the reordering mode is set to
844 * <code>#UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL</code>, the Logical to Visual
729e4ab9 845 * Bidi algorithm used in Windows XP is used as an approximation of an "inverse Bidi" algorithm.
73c04bcf
A
846 * <br>
847 * For example, an LTR paragraph with the content "abc FED123" (where
729e4ab9 848 * upper case represents RTL characters) will be transformed to "abc 123DEF."</li>
73c04bcf
A
849 * </ul>
850 *
46f4442e 851 * <p>In all the reordering modes specifying an "inverse Bidi" algorithm
73c04bcf
A
852 * (i.e. those with a name starting with <code>UBIDI_REORDER_INVERSE</code>),
853 * output runs should be retrieved using
854 * <code>ubidi_getVisualRun()</code>, and the output text with
855 * <code>ubidi_writeReordered()</code>. The caller should keep in mind that in
46f4442e 856 * "inverse Bidi" modes the input is actually visually ordered text and
73c04bcf
A
857 * reordered output returned by <code>ubidi_getVisualRun()</code> or
858 * <code>ubidi_writeReordered()</code> are actually runs or character string
859 * of logically ordered output.<br>
46f4442e
A
860 * For all the "inverse Bidi" modes, the source text should not contain
861 * Bidi control characters other than LRM or RLM.</p>
73c04bcf
A
862 *
863 * <p>Note that option <code>#UBIDI_OUTPUT_REVERSE</code> of
864 * <code>ubidi_writeReordered</code> has no useful meaning and should not be
865 * used in conjunction with any value of the reordering mode specifying
46f4442e 866 * "inverse Bidi" or with value <code>UBIDI_REORDER_RUNS_ONLY</code>.
73c04bcf
A
867 *
868 * @param pBiDi is a <code>UBiDi</code> object.
46f4442e 869 * @param reorderingMode specifies the required variant of the Bidi algorithm.
73c04bcf
A
870 *
871 * @see UBiDiReorderingMode
872 * @see ubidi_setInverse
873 * @see ubidi_setPara
874 * @see ubidi_writeReordered
46f4442e 875 * @stable ICU 3.6
73c04bcf 876 */
46f4442e 877U_STABLE void U_EXPORT2
73c04bcf
A
878ubidi_setReorderingMode(UBiDi *pBiDi, UBiDiReorderingMode reorderingMode);
879
880/**
46f4442e 881 * What is the requested reordering mode for a given Bidi object?
73c04bcf
A
882 *
883 * @param pBiDi is a <code>UBiDi</code> object.
46f4442e 884 * @return the current reordering mode of the Bidi object
73c04bcf 885 * @see ubidi_setReorderingMode
46f4442e 886 * @stable ICU 3.6
73c04bcf 887 */
46f4442e 888U_STABLE UBiDiReorderingMode U_EXPORT2
73c04bcf
A
889ubidi_getReorderingMode(UBiDi *pBiDi);
890
891/**
892 * <code>UBiDiReorderingOption</code> values indicate which options are
46f4442e 893 * specified to affect the Bidi algorithm.
73c04bcf
A
894 *
895 * @see ubidi_setReorderingOptions
46f4442e 896 * @stable ICU 3.6
73c04bcf
A
897 */
898typedef enum UBiDiReorderingOption {
899 /**
900 * option value for <code>ubidi_setReorderingOptions</code>:
901 * disable all the options which can be set with this function
902 * @see ubidi_setReorderingOptions
46f4442e 903 * @stable ICU 3.6
73c04bcf
A
904 */
905 UBIDI_OPTION_DEFAULT = 0,
906
907 /**
908 * option bit for <code>ubidi_setReorderingOptions</code>:
46f4442e 909 * insert Bidi marks (LRM or RLM) when needed to ensure correct result of
73c04bcf
A
910 * a reordering to a Logical order
911 *
912 * <p>This option must be set or reset before calling
913 * <code>ubidi_setPara</code>.</p>
914 *
915 * <p>This option is significant only with reordering modes which generate
916 * a result with Logical order, specifically:</p>
917 * <ul>
918 * <li><code>#UBIDI_REORDER_RUNS_ONLY</code></li>
919 * <li><code>#UBIDI_REORDER_INVERSE_NUMBERS_AS_L</code></li>
920 * <li><code>#UBIDI_REORDER_INVERSE_LIKE_DIRECT</code></li>
921 * <li><code>#UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL</code></li>
922 * </ul>
923 *
924 * <p>If this option is set in conjunction with reordering mode
925 * <code>#UBIDI_REORDER_INVERSE_NUMBERS_AS_L</code> or with calling
926 * <code>ubidi_setInverse(TRUE)</code>, it implies
927 * option <code>#UBIDI_INSERT_LRM_FOR_NUMERIC</code>
928 * in calls to function <code>ubidi_writeReordered()</code>.</p>
929 *
930 * <p>For other reordering modes, a minimum number of LRM or RLM characters
931 * will be added to the source text after reordering it so as to ensure
932 * round trip, i.e. when applying the inverse reordering mode on the
46f4442e 933 * resulting logical text with removal of Bidi marks
73c04bcf
A
934 * (option <code>#UBIDI_OPTION_REMOVE_CONTROLS</code> set before calling
935 * <code>ubidi_setPara()</code> or option <code>#UBIDI_REMOVE_BIDI_CONTROLS</code>
936 * in <code>ubidi_writeReordered</code>), the result will be identical to the
937 * source text in the first transformation.
938 *
939 * <p>This option will be ignored if specified together with option
940 * <code>#UBIDI_OPTION_REMOVE_CONTROLS</code>. It inhibits option
941 * <code>UBIDI_REMOVE_BIDI_CONTROLS</code> in calls to function
942 * <code>ubidi_writeReordered()</code> and it implies option
943 * <code>#UBIDI_INSERT_LRM_FOR_NUMERIC</code> in calls to function
944 * <code>ubidi_writeReordered()</code> if the reordering mode is
945 * <code>#UBIDI_REORDER_INVERSE_NUMBERS_AS_L</code>.</p>
946 *
947 * @see ubidi_setReorderingMode
948 * @see ubidi_setReorderingOptions
46f4442e 949 * @stable ICU 3.6
73c04bcf
A
950 */
951 UBIDI_OPTION_INSERT_MARKS = 1,
952
953 /**
954 * option bit for <code>ubidi_setReorderingOptions</code>:
46f4442e 955 * remove Bidi control characters
73c04bcf
A
956 *
957 * <p>This option must be set or reset before calling
958 * <code>ubidi_setPara</code>.</p>
959 *
960 * <p>This option nullifies option <code>#UBIDI_OPTION_INSERT_MARKS</code>.
961 * It inhibits option <code>#UBIDI_INSERT_LRM_FOR_NUMERIC</code> in calls
962 * to function <code>ubidi_writeReordered()</code> and it implies option
963 * <code>#UBIDI_REMOVE_BIDI_CONTROLS</code> in calls to that function.</p>
964 *
965 * @see ubidi_setReorderingMode
966 * @see ubidi_setReorderingOptions
46f4442e 967 * @stable ICU 3.6
73c04bcf
A
968 */
969 UBIDI_OPTION_REMOVE_CONTROLS = 2,
970
971 /**
972 * option bit for <code>ubidi_setReorderingOptions</code>:
973 * process the output as part of a stream to be continued
974 *
975 * <p>This option must be set or reset before calling
976 * <code>ubidi_setPara</code>.</p>
977 *
978 * <p>This option specifies that the caller is interested in processing large
979 * text object in parts.
980 * The results of the successive calls are expected to be concatenated by the
981 * caller. Only the call for the last part will have this option bit off.</p>
982 *
983 * <p>When this option bit is on, <code>ubidi_setPara()</code> may process
984 * less than the full source text in order to truncate the text at a meaningful
985 * boundary. The caller should call <code>ubidi_getProcessedLength()</code>
986 * immediately after calling <code>ubidi_setPara()</code> in order to
987 * determine how much of the source text has been processed.
988 * Source text beyond that length should be resubmitted in following calls to
989 * <code>ubidi_setPara</code>. The processed length may be less than
990 * the length of the source text if a character preceding the last character of
991 * the source text constitutes a reasonable boundary (like a block separator)
992 * for text to be continued.<br>
993 * If the last character of the source text constitutes a reasonable
994 * boundary, the whole text will be processed at once.<br>
995 * If nowhere in the source text there exists
996 * such a reasonable boundary, the processed length will be zero.<br>
997 * The caller should check for such an occurrence and do one of the following:
998 * <ul><li>submit a larger amount of text with a better chance to include
999 * a reasonable boundary.</li>
1000 * <li>resubmit the same text after turning off option
1001 * <code>UBIDI_OPTION_STREAMING</code>.</li></ul>
1002 * In all cases, this option should be turned off before processing the last
1003 * part of the text.</p>
1004 *
1005 * <p>When the <code>UBIDI_OPTION_STREAMING</code> option is used,
1006 * it is recommended to call <code>ubidi_orderParagraphsLTR()</code> with
1007 * argument <code>orderParagraphsLTR</code> set to <code>TRUE</code> before
1008 * calling <code>ubidi_setPara</code> so that later paragraphs may be
1009 * concatenated to previous paragraphs on the right.</p>
1010 *
1011 * @see ubidi_setReorderingMode
1012 * @see ubidi_setReorderingOptions
1013 * @see ubidi_getProcessedLength
1014 * @see ubidi_orderParagraphsLTR
46f4442e 1015 * @stable ICU 3.6
73c04bcf
A
1016 */
1017 UBIDI_OPTION_STREAMING = 4
1018} UBiDiReorderingOption;
1019
1020/**
1021 * Specify which of the reordering options
46f4442e 1022 * should be applied during Bidi transformations.
73c04bcf
A
1023 *
1024 * @param pBiDi is a <code>UBiDi</code> object.
1025 * @param reorderingOptions is a combination of zero or more of the following
1026 * options:
1027 * <code>#UBIDI_OPTION_DEFAULT</code>, <code>#UBIDI_OPTION_INSERT_MARKS</code>,
1028 * <code>#UBIDI_OPTION_REMOVE_CONTROLS</code>, <code>#UBIDI_OPTION_STREAMING</code>.
1029 *
1030 * @see ubidi_getReorderingOptions
46f4442e 1031 * @stable ICU 3.6
73c04bcf 1032 */
46f4442e 1033U_STABLE void U_EXPORT2
73c04bcf
A
1034ubidi_setReorderingOptions(UBiDi *pBiDi, uint32_t reorderingOptions);
1035
1036/**
46f4442e 1037 * What are the reordering options applied to a given Bidi object?
73c04bcf
A
1038 *
1039 * @param pBiDi is a <code>UBiDi</code> object.
46f4442e 1040 * @return the current reordering options of the Bidi object
73c04bcf 1041 * @see ubidi_setReorderingOptions
46f4442e 1042 * @stable ICU 3.6
73c04bcf 1043 */
46f4442e 1044U_STABLE uint32_t U_EXPORT2
73c04bcf
A
1045ubidi_getReorderingOptions(UBiDi *pBiDi);
1046
4388f060
A
1047/**
1048 * Set the context before a call to ubidi_setPara().<p>
1049 *
1050 * ubidi_setPara() computes the left-right directionality for a given piece
1051 * of text which is supplied as one of its arguments. Sometimes this piece
1052 * of text (the "main text") should be considered in context, because text
1053 * appearing before ("prologue") and/or after ("epilogue") the main text
1054 * may affect the result of this computation.<p>
1055 *
1056 * This function specifies the prologue and/or the epilogue for the next
1057 * call to ubidi_setPara(). The characters specified as prologue and
1058 * epilogue should not be modified by the calling program until the call
1059 * to ubidi_setPara() has returned. If successive calls to ubidi_setPara()
1060 * all need specification of a context, ubidi_setContext() must be called
1061 * before each call to ubidi_setPara(). In other words, a context is not
1062 * "remembered" after the following successful call to ubidi_setPara().<p>
1063 *
1064 * If a call to ubidi_setPara() specifies UBIDI_DEFAULT_LTR or
1065 * UBIDI_DEFAULT_RTL as paraLevel and is preceded by a call to
1066 * ubidi_setContext() which specifies a prologue, the paragraph level will
1067 * be computed taking in consideration the text in the prologue.<p>
1068 *
1069 * When ubidi_setPara() is called without a previous call to
1070 * ubidi_setContext, the main text is handled as if preceded and followed
1071 * by strong directional characters at the current paragraph level.
1072 * Calling ubidi_setContext() with specification of a prologue will change
1073 * this behavior by handling the main text as if preceded by the last
1074 * strong character appearing in the prologue, if any.
1075 * Calling ubidi_setContext() with specification of an epilogue will change
1076 * the behavior of ubidi_setPara() by handling the main text as if followed
1077 * by the first strong character or digit appearing in the epilogue, if any.<p>
1078 *
1079 * Note 1: if <code>ubidi_setContext</code> is called repeatedly without
1080 * calling <code>ubidi_setPara</code>, the earlier calls have no effect,
1081 * only the last call will be remembered for the next call to
1082 * <code>ubidi_setPara</code>.<p>
1083 *
1084 * Note 2: calling <code>ubidi_setContext(pBiDi, NULL, 0, NULL, 0, &errorCode)</code>
1085 * cancels any previous setting of non-empty prologue or epilogue.
1086 * The next call to <code>ubidi_setPara()</code> will process no
1087 * prologue or epilogue.<p>
1088 *
1089 * Note 3: users must be aware that even after setting the context
1090 * before a call to ubidi_setPara() to perform e.g. a logical to visual
1091 * transformation, the resulting string may not be identical to what it
1092 * would have been if all the text, including prologue and epilogue, had
1093 * been processed together.<br>
1094 * Example (upper case letters represent RTL characters):<br>
1095 * &nbsp;&nbsp;prologue = "<code>abc DE</code>"<br>
1096 * &nbsp;&nbsp;epilogue = none<br>
1097 * &nbsp;&nbsp;main text = "<code>FGH xyz</code>"<br>
1098 * &nbsp;&nbsp;paraLevel = UBIDI_LTR<br>
1099 * &nbsp;&nbsp;display without prologue = "<code>HGF xyz</code>"
1100 * ("HGF" is adjacent to "xyz")<br>
1101 * &nbsp;&nbsp;display with prologue = "<code>abc HGFED xyz</code>"
1102 * ("HGF" is not adjacent to "xyz")<br>
1103 *
1104 * @param pBiDi is a paragraph <code>UBiDi</code> object.
1105 *
1106 * @param prologue is a pointer to the text which precedes the text that
1107 * will be specified in a coming call to ubidi_setPara().
1108 * If there is no prologue to consider, then <code>proLength</code>
1109 * must be zero and this pointer can be NULL.
1110 *
1111 * @param proLength is the length of the prologue; if <code>proLength==-1</code>
1112 * then the prologue must be zero-terminated.
1113 * Otherwise proLength must be >= 0. If <code>proLength==0</code>, it means
1114 * that there is no prologue to consider.
1115 *
1116 * @param epilogue is a pointer to the text which follows the text that
1117 * will be specified in a coming call to ubidi_setPara().
1118 * If there is no epilogue to consider, then <code>epiLength</code>
1119 * must be zero and this pointer can be NULL.
1120 *
1121 * @param epiLength is the length of the epilogue; if <code>epiLength==-1</code>
1122 * then the epilogue must be zero-terminated.
1123 * Otherwise epiLength must be >= 0. If <code>epiLength==0</code>, it means
1124 * that there is no epilogue to consider.
1125 *
1126 * @param pErrorCode must be a valid pointer to an error code value.
1127 *
1128 * @see ubidi_setPara
1129 * @stable ICU 4.8
1130 */
51004dcb 1131U_STABLE void U_EXPORT2
4388f060
A
1132ubidi_setContext(UBiDi *pBiDi,
1133 const UChar *prologue, int32_t proLength,
1134 const UChar *epilogue, int32_t epiLength,
1135 UErrorCode *pErrorCode);
1136
b75a7d8f 1137/**
46f4442e 1138 * Perform the Unicode Bidi algorithm. It is defined in the
2ca993e8
A
1139 * <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Standard Annex #9</a>,
1140 * Unicode 8.0.0 / revision 33,
1141 * also described in The Unicode Standard, Version 8.0 .<p>
b75a7d8f 1142 *
73c04bcf
A
1143 * This function takes a piece of plain text containing one or more paragraphs,
1144 * with or without externally specified embedding levels from <i>styled</i>
1145 * text and computes the left-right-directionality of each character.<p>
b75a7d8f 1146 *
73c04bcf 1147 * If the entire text is all of the same directionality, then
b75a7d8f
A
1148 * the function may not perform all the steps described by the algorithm,
1149 * i.e., some levels may not be the same as if all steps were performed.
1150 * This is not relevant for unidirectional text.<br>
1151 * For example, in pure LTR text with numbers the numbers would get
1152 * a resolved level of 2 higher than the surrounding text according to
1153 * the algorithm. This implementation may set all resolved levels to
1154 * the same value in such a case.<p>
1155 *
73c04bcf
A
1156 * The text can be composed of multiple paragraphs. Occurrence of a block
1157 * separator in the text terminates a paragraph, and whatever comes next starts
1158 * a new paragraph. The exception to this rule is when a Carriage Return (CR)
1159 * is followed by a Line Feed (LF). Both CR and LF are block separators, but
1160 * in that case, the pair of characters is considered as terminating the
1161 * preceding paragraph, and a new paragraph will be started by a character
1162 * coming after the LF.
b75a7d8f
A
1163 *
1164 * @param pBiDi A <code>UBiDi</code> object allocated with <code>ubidi_open()</code>
1165 * which will be set to contain the reordering information,
1166 * especially the resolved levels for all the characters in <code>text</code>.
1167 *
46f4442e 1168 * @param text is a pointer to the text that the Bidi algorithm will be performed on.
b75a7d8f 1169 * This pointer is stored in the UBiDi object and can be retrieved
73c04bcf
A
1170 * with <code>ubidi_getText()</code>.<br>
1171 * <strong>Note:</strong> the text must be (at least) <code>length</code> long.
b75a7d8f
A
1172 *
1173 * @param length is the length of the text; if <code>length==-1</code> then
1174 * the text must be zero-terminated.
1175 *
73c04bcf 1176 * @param paraLevel specifies the default level for the text;
b75a7d8f
A
1177 * it is typically 0 (LTR) or 1 (RTL).
1178 * If the function shall determine the paragraph level from the text,
1179 * then <code>paraLevel</code> can be set to
73c04bcf
A
1180 * either <code>#UBIDI_DEFAULT_LTR</code>
1181 * or <code>#UBIDI_DEFAULT_RTL</code>; if the text contains multiple
1182 * paragraphs, the paragraph level shall be determined separately for
1183 * each paragraph; if a paragraph does not include any strongly typed
1184 * character, then the desired default is used (0 for LTR or 1 for RTL).
1185 * Any other value between 0 and <code>#UBIDI_MAX_EXPLICIT_LEVEL</code>
1186 * is also valid, with odd levels indicating RTL.
b75a7d8f
A
1187 *
1188 * @param embeddingLevels (in) may be used to preset the embedding and override levels,
1189 * ignoring characters like LRE and PDF in the text.
1190 * A level overrides the directional property of its corresponding
1191 * (same index) character if the level has the
73c04bcf 1192 * <code>#UBIDI_LEVEL_OVERRIDE</code> bit set.<br><br>
b75a7d8f 1193 * Except for that bit, it must be
73c04bcf
A
1194 * <code>paraLevel<=embeddingLevels[]<=UBIDI_MAX_EXPLICIT_LEVEL</code>,
1195 * with one exception: a level of zero may be specified for a paragraph
1196 * separator even if <code>paraLevel>0</code> when multiple paragraphs
1197 * are submitted in the same call to <code>ubidi_setPara()</code>.<br><br>
b75a7d8f
A
1198 * <strong>Caution: </strong>A copy of this pointer, not of the levels,
1199 * will be stored in the <code>UBiDi</code> object;
1200 * the <code>embeddingLevels</code> array must not be
1201 * deallocated before the <code>UBiDi</code> structure is destroyed or reused,
1202 * and the <code>embeddingLevels</code>
46f4442e 1203 * should not be modified to avoid unexpected results on subsequent Bidi operations.
b75a7d8f 1204 * However, the <code>ubidi_setPara()</code> and
73c04bcf 1205 * <code>ubidi_setLine()</code> functions may modify some or all of the levels.<br><br>
b75a7d8f 1206 * After the <code>UBiDi</code> object is reused or destroyed, the caller
73c04bcf
A
1207 * must take care of the deallocation of the <code>embeddingLevels</code> array.<br><br>
1208 * <strong>Note:</strong> the <code>embeddingLevels</code> array must be
1209 * at least <code>length</code> long.
729e4ab9
A
1210 * This pointer can be <code>NULL</code> if this
1211 * value is not necessary.
b75a7d8f 1212 *
73c04bcf 1213 * @param pErrorCode must be a valid pointer to an error code value.
b75a7d8f
A
1214 * @stable ICU 2.0
1215 */
374ca955 1216U_STABLE void U_EXPORT2
b75a7d8f
A
1217ubidi_setPara(UBiDi *pBiDi, const UChar *text, int32_t length,
1218 UBiDiLevel paraLevel, UBiDiLevel *embeddingLevels,
1219 UErrorCode *pErrorCode);
1220
2ca993e8
A
1221#ifndef U_HIDE_INTERNAL_API
1222/**
1223 * Perform the Unicode Bidi algorithm. It is defined in the
1224 * <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Standard Annex #9</a>,
1225 * Unicode 8.0.0 / revision 33,
1226 * also described in The Unicode Standard, Version 8.0 .<p>
1227 *
1228 * This function takes a piece of plain text containing one or more paragraphs,
1229 * with or without externally specified direction overrides (in the form of
1230 * sequences of one or more bidi control characters for
1231 * embeddings/overrides/isolates to be effectively inserted at specified points
1232 * in the text), and computes the left-right-directionality of each character.
1233 * Note that ubidi_setContext may be used to set the context before or after the
1234 * text passed to ubidi_setPara, so ubidi_setParaWithControls is only needed if
1235 * externally specified direction overrides need to be effectively inserted at
1236 * other locations in the text.<p>
1237 *
1238 * Note: Currently the external specified direction overrides are only supported
1239 * for the Logical to Visual values of UBiDiReorderingMode: UBIDI_REORDER_DEFAULT,
1240 * UBIDI_REORDER_NUMBERS_SPECIAL, UBIDI_REORDER_GROUP_NUMBERS_WITH_R. With other
1241 * UBiDiReorderingMode settings, this function behaves as if offsetCount is 0.<p>
1242 *
1243 * If the entire text is all of the same directionality, then the function may
1244 * not perform all the steps described by the algorithm, i.e., some levels may
1245 * not be the same as if all steps were performed. This is not relevant for
1246 * unidirectional text.<br>
1247 * For example, in pure LTR text with numbers the numbers would get a resolved
1248 * level of 2 higher than the surrounding text according to the algorithm. This
1249 * implementation may set all resolved levels to the same value in such a case.<p>
1250 *
1251 * The text can be composed of multiple paragraphs. Occurrence of a block
1252 * separator in the text terminates a paragraph, and whatever comes next starts
1253 * a new paragraph. The exception to this rule is when a Carriage Return (CR)
1254 * is followed by a Line Feed (LF). Both CR and LF are block separators, but
1255 * in that case, the pair of characters is considered as terminating the
1256 * preceding paragraph, and a new paragraph will be started by a character
1257 * coming after the LF.<p>
1258 *
1259 * @param pBiDi A <code>UBiDi</code> object allocated with <code>ubidi_open()</code>
1260 * which will be set to contain the reordering information,
1261 * especially the resolved levels for all the characters in <code>text</code>.
1262 *
1263 * @param text is a pointer to the text that the Bidi algorithm will be performed on.
1264 * This pointer is stored in the UBiDi object and can be retrieved
1265 * with <code>ubidi_getText()</code>.<br>
1266 * <strong>Note:</strong> the text must be (at least) <code>length</code> long.
1267 *
1268 * @param length is the length of the text; if <code>length==-1</code> then
1269 * the text must be zero-terminated.
1270 *
1271 * @param paraLevel specifies the default level for the text;
1272 * it is typically 0 (LTR) or 1 (RTL).
1273 * If the function shall determine the paragraph level from the text,
1274 * then <code>paraLevel</code> can be set to
1275 * either <code>#UBIDI_DEFAULT_LTR</code>
1276 * or <code>#UBIDI_DEFAULT_RTL</code>; if the text contains multiple
1277 * paragraphs, the paragraph level shall be determined separately for
1278 * each paragraph; if a paragraph does not include any strongly typed
1279 * character, then the desired default is used (0 for LTR or 1 for RTL).
1280 * Any other value between 0 and <code>#UBIDI_MAX_EXPLICIT_LEVEL</code>
1281 * is also valid, with odd levels indicating RTL.
1282 *
1283 * @param offsets Array of text offsets at which sequences of one or more
1284 * bidi controls are to be effectively inserted. The offset values must
1285 * be >= 0 and < <code>length</code> (use <code>ubidi_setContext</code>
1286 * to provide the effect of inserting controls after the last character
1287 * of the text). This must be non-NULL if <code>offsetCount</code> > 0.
1288 *
1289 * @param offsetCount The number of entries in the offsets array, and in the
1290 * controlStringIndices array if the latter is present (non NULL). If
1291 * <code>offsetCount</code> is 0, then no controls will be inserted and
1292 * the parameters <code>offsets</code>, <code>controlStringIndices</code>
1293 * and <code>controlStrings</code> will be ignored.
1294 *
1295 * @param controlStringIndices If not NULL, this array must have the same
1296 * number of entries as the offsets array; each entry in this array
1297 * maps from the corresponding offset to the index in controlStrings
1298 * of the control sequence that is to be effectively inserted at that
1299 * offset. This indirection is useful when certain control sequences
1300 * are to be effectively inserted in many different places in the text.
1301 * If this array is NULL, then the entries in controlStrings correspond
1302 * directly to the entries in the offsets array.
1303 *
1304 * @param controlStrings Array of const pointers to zero-terminated
1305 * const UChar strings each consisting of zero or more characters that
1306 * are bidi controls for embeddings, overrides, or isolates (see list
1307 * below). Other characters that might be supported in the future
1308 * (depending on need) include bidi marks an characters with
1309 * bidi class B (block separator) or class S (segment separator).
1310 * The characters in these strings only affect the bidi levels assigned
1311 * to the characters in he text array, they are not used for any other
1312 * purpose.<br>
1313 * If controlStringIndices is NULL, then controlStrings must have the
1314 * same number of entries as the offsets array, and each entry provides
1315 * the UChar string that is effectively inserted at the corresponding
1316 * offset. If controlStringIndices is not NULL, then controlStrings must
1317 * have at least enough entries to accommodate to all of the index values
1318 * in the controlStringIndices array. This must be non-NULL if
1319 * offsetCount > 0.<br>
1320 * Current limitations:<br>
1321 * Each zero-terminated const UChar string is limited a maximum length
1322 * of 4, not including the zero terminator.<br>
1323 * Each zero-terminated const UChar string may contain at most one
1324 * instance of FSI, LRI, or RLI.<br>
1325 *
1326 * @param pErrorCode must be a valid pointer to an error code value.
1327 *
1328 * @discussion
1329 * <pre>
1330 * Supported bidi controls for embeddings / overrides / isolates as of Unicode 8.0:
1331 * LRE U+202A LEFT-TO-RIGHT EMBEDDING
1332 * RLE U+202B RIGHT-TO-LEFT EMBEDDING
1333 * PDF U+202C POP DIRECTIONAL FORMATTING
1334 * LRO U+202D LEFT-TO-RIGHT OVERRIDE
1335 * RLO U+202E RIGHT-TO-LEFT OVERRIDE
1336 * #
1337 * LRI U+2066 LEFT‑TO‑RIGHT ISOLATE
1338 * RLI U+2067 RIGHT‑TO‑LEFT ISOLATE
1339 * FSI U+2068 FIRST STRONG ISOLATE
1340 * PDI U+2069 POP DIRECTIONAL ISOLATE
1341 *
1342 * Bidi marks as of Unicode 8.0:
1343 * ALM U+061C ARABIC LETTER MARK (bidi class AL)
1344 * LRM U+200E LEFT-TO-RIGHT MARK (bidi class L)
1345 * RLM U+200F RIGHT-TO-LEFT MARK (bidi class R)
1346 * Characters with bidi class B (block separator) as of Unicode 8.0:
1347 * B U+000A LINE FEED (LF)
1348 * B U+000D CARRIAGE RETURN (CR)
1349 * B U+001C INFORMATION SEPARATOR FOUR
1350 * B U+001D INFORMATION SEPARATOR THREE
1351 * B U+001E INFORMATION SEPARATOR TWO
1352 * B U+0085 NEXT LINE (NEL)
1353 * B U+2029 PARAGRAPH SEPARATOR
1354 * Characters with bidi class S (segment separator) as of Unicode 8.0:
1355 * S U+0009 CHARACTER TABULATION
1356 * S U+000B LINE TABULATION
1357 * S U+001F INFORMATION SEPARATOR ONE
1358 * </pre>
1359 *
1360 * @see ubidi_setContext
1361 * @internal technology preview as of ICU 57
1362 */
1363U_INTERNAL void U_EXPORT2
1364ubidi_setParaWithControls(UBiDi *pBiDi,
1365 const UChar *text, int32_t length,
1366 UBiDiLevel paraLevel,
1367 const int32_t *offsets, int32_t offsetCount,
1368 const int32_t *controlStringIndices,
1369 const UChar * const * controlStrings,
1370 UErrorCode *pErrorCode);
1371
1372#endif /* U_HIDE_INTERNAL_API */
1373
b75a7d8f
A
1374/**
1375 * <code>ubidi_setLine()</code> sets a <code>UBiDi</code> to
1376 * contain the reordering information, especially the resolved levels,
1377 * for all the characters in a line of text. This line of text is
1378 * specified by referring to a <code>UBiDi</code> object representing
73c04bcf
A
1379 * this information for a piece of text containing one or more paragraphs,
1380 * and by specifying a range of indexes in this text.<p>
374ca955 1381 * In the new line object, the indexes will range from 0 to <code>limit-start-1</code>.<p>
b75a7d8f
A
1382 *
1383 * This is used after calling <code>ubidi_setPara()</code>
73c04bcf
A
1384 * for a piece of text, and after line-breaking on that text.
1385 * It is not necessary if each paragraph is treated as a single line.<p>
b75a7d8f
A
1386 *
1387 * After line-breaking, rules (L1) and (L2) for the treatment of
1388 * trailing WS and for reordering are performed on
1389 * a <code>UBiDi</code> object that represents a line.<p>
1390 *
1391 * <strong>Important: </strong><code>pLineBiDi</code> shares data with
1392 * <code>pParaBiDi</code>.
1393 * You must destroy or reuse <code>pLineBiDi</code> before <code>pParaBiDi</code>.
1394 * In other words, you must destroy or reuse the <code>UBiDi</code> object for a line
1395 * before the object for its parent paragraph.<p>
1396 *
1397 * The text pointer that was stored in <code>pParaBiDi</code> is also copied,
1398 * and <code>start</code> is added to it so that it points to the beginning of the
1399 * line for this object.
1400 *
73c04bcf
A
1401 * @param pParaBiDi is the parent paragraph object. It must have been set
1402 * by a successful call to ubidi_setPara.
b75a7d8f 1403 *
73c04bcf 1404 * @param start is the line's first index into the text.
b75a7d8f 1405 *
73c04bcf 1406 * @param limit is just behind the line's last index into the text
b75a7d8f 1407 * (its last index +1).<br>
46f4442e 1408 * It must be <code>0<=start<limit<=</code>containing paragraph limit.
73c04bcf
A
1409 * If the specified line crosses a paragraph boundary, the function
1410 * will terminate with error code U_ILLEGAL_ARGUMENT_ERROR.
b75a7d8f 1411 *
73c04bcf 1412 * @param pLineBiDi is the object that will now represent a line of the text.
b75a7d8f 1413 *
73c04bcf 1414 * @param pErrorCode must be a valid pointer to an error code value.
b75a7d8f
A
1415 *
1416 * @see ubidi_setPara
73c04bcf 1417 * @see ubidi_getProcessedLength
b75a7d8f
A
1418 * @stable ICU 2.0
1419 */
374ca955 1420U_STABLE void U_EXPORT2
b75a7d8f
A
1421ubidi_setLine(const UBiDi *pParaBiDi,
1422 int32_t start, int32_t limit,
1423 UBiDi *pLineBiDi,
1424 UErrorCode *pErrorCode);
1425
1426/**
1427 * Get the directionality of the text.
1428 *
1429 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1430 *
46f4442e
A
1431 * @return a value of <code>UBIDI_LTR</code>, <code>UBIDI_RTL</code>
1432 * or <code>UBIDI_MIXED</code>
1433 * that indicates if the entire text
b75a7d8f
A
1434 * represented by this object is unidirectional,
1435 * and which direction, or if it is mixed-directional.
729e4ab9 1436 * Note - The value <code>UBIDI_NEUTRAL</code> is never returned from this method.
b75a7d8f
A
1437 *
1438 * @see UBiDiDirection
1439 * @stable ICU 2.0
1440 */
374ca955 1441U_STABLE UBiDiDirection U_EXPORT2
b75a7d8f
A
1442ubidi_getDirection(const UBiDi *pBiDi);
1443
729e4ab9
A
1444/**
1445 * Gets the base direction of the text provided according
1446 * to the Unicode Bidirectional Algorithm. The base direction
1447 * is derived from the first character in the string with bidirectional
1448 * character type L, R, or AL. If the first such character has type L,
1449 * <code>UBIDI_LTR</code> is returned. If the first such character has
1450 * type R or AL, <code>UBIDI_RTL</code> is returned. If the string does
1451 * not contain any character of these types, then
1452 * <code>UBIDI_NEUTRAL</code> is returned.
1453 *
1454 * This is a lightweight function for use when only the base direction
1455 * is needed and no further bidi processing of the text is needed.
1456 *
1457 * @param text is a pointer to the text whose base
1458 * direction is needed.
1459 * Note: the text must be (at least) @c length long.
1460 *
1461 * @param length is the length of the text;
1462 * if <code>length==-1</code> then the text
1463 * must be zero-terminated.
1464 *
1465 * @return <code>UBIDI_LTR</code>, <code>UBIDI_RTL</code>,
1466 * <code>UBIDI_NEUTRAL</code>
1467 *
1468 * @see UBiDiDirection
4388f060 1469 * @stable ICU 4.6
729e4ab9 1470 */
51004dcb 1471U_STABLE UBiDiDirection U_EXPORT2
729e4ab9
A
1472ubidi_getBaseDirection(const UChar *text, int32_t length );
1473
b75a7d8f
A
1474/**
1475 * Get the pointer to the text.
1476 *
1477 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1478 *
1479 * @return The pointer to the text that the UBiDi object was created for.
1480 *
1481 * @see ubidi_setPara
1482 * @see ubidi_setLine
1483 * @stable ICU 2.0
1484 */
374ca955 1485U_STABLE const UChar * U_EXPORT2
b75a7d8f
A
1486ubidi_getText(const UBiDi *pBiDi);
1487
1488/**
1489 * Get the length of the text.
1490 *
1491 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1492 *
1493 * @return The length of the text that the UBiDi object was created for.
1494 * @stable ICU 2.0
1495 */
374ca955 1496U_STABLE int32_t U_EXPORT2
b75a7d8f
A
1497ubidi_getLength(const UBiDi *pBiDi);
1498
1499/**
1500 * Get the paragraph level of the text.
1501 *
1502 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1503 *
73c04bcf
A
1504 * @return The paragraph level. If there are multiple paragraphs, their
1505 * level may vary if the required paraLevel is UBIDI_DEFAULT_LTR or
1506 * UBIDI_DEFAULT_RTL. In that case, the level of the first paragraph
1507 * is returned.
b75a7d8f
A
1508 *
1509 * @see UBiDiLevel
73c04bcf
A
1510 * @see ubidi_getParagraph
1511 * @see ubidi_getParagraphByIndex
b75a7d8f
A
1512 * @stable ICU 2.0
1513 */
374ca955 1514U_STABLE UBiDiLevel U_EXPORT2
b75a7d8f
A
1515ubidi_getParaLevel(const UBiDi *pBiDi);
1516
73c04bcf
A
1517/**
1518 * Get the number of paragraphs.
1519 *
1520 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1521 *
1522 * @return The number of paragraphs.
1523 * @stable ICU 3.4
1524 */
1525U_STABLE int32_t U_EXPORT2
1526ubidi_countParagraphs(UBiDi *pBiDi);
1527
1528/**
1529 * Get a paragraph, given a position within the text.
46f4442e
A
1530 * This function returns information about a paragraph.<br>
1531 * Note: if the paragraph index is known, it is more efficient to
1532 * retrieve the paragraph information using ubidi_getParagraphByIndex().<p>
73c04bcf
A
1533 *
1534 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1535 *
1536 * @param charIndex is the index of a character within the text, in the
1537 * range <code>[0..ubidi_getProcessedLength(pBiDi)-1]</code>.
1538 *
1539 * @param pParaStart will receive the index of the first character of the
1540 * paragraph in the text.
1541 * This pointer can be <code>NULL</code> if this
1542 * value is not necessary.
1543 *
1544 * @param pParaLimit will receive the limit of the paragraph.
1545 * The l-value that you point to here may be the
1546 * same expression (variable) as the one for
1547 * <code>charIndex</code>.
1548 * This pointer can be <code>NULL</code> if this
1549 * value is not necessary.
1550 *
1551 * @param pParaLevel will receive the level of the paragraph.
1552 * This pointer can be <code>NULL</code> if this
1553 * value is not necessary.
1554 *
1555 * @param pErrorCode must be a valid pointer to an error code value.
1556 *
1557 * @return The index of the paragraph containing the specified position.
1558 *
1559 * @see ubidi_getProcessedLength
1560 * @stable ICU 3.4
1561 */
1562U_STABLE int32_t U_EXPORT2
1563ubidi_getParagraph(const UBiDi *pBiDi, int32_t charIndex, int32_t *pParaStart,
1564 int32_t *pParaLimit, UBiDiLevel *pParaLevel,
1565 UErrorCode *pErrorCode);
1566
1567/**
1568 * Get a paragraph, given the index of this paragraph.
1569 *
1570 * This function returns information about a paragraph.<p>
1571 *
1572 * @param pBiDi is the paragraph <code>UBiDi</code> object.
1573 *
1574 * @param paraIndex is the number of the paragraph, in the
1575 * range <code>[0..ubidi_countParagraphs(pBiDi)-1]</code>.
1576 *
1577 * @param pParaStart will receive the index of the first character of the
1578 * paragraph in the text.
1579 * This pointer can be <code>NULL</code> if this
1580 * value is not necessary.
1581 *
1582 * @param pParaLimit will receive the limit of the paragraph.
1583 * This pointer can be <code>NULL</code> if this
1584 * value is not necessary.
1585 *
1586 * @param pParaLevel will receive the level of the paragraph.
1587 * This pointer can be <code>NULL</code> if this
1588 * value is not necessary.
1589 *
1590 * @param pErrorCode must be a valid pointer to an error code value.
1591 *
1592 * @stable ICU 3.4
1593 */
1594U_STABLE void U_EXPORT2
1595ubidi_getParagraphByIndex(const UBiDi *pBiDi, int32_t paraIndex,
1596 int32_t *pParaStart, int32_t *pParaLimit,
1597 UBiDiLevel *pParaLevel, UErrorCode *pErrorCode);
1598
b75a7d8f
A
1599/**
1600 * Get the level for one character.
1601 *
1602 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1603 *
46f4442e
A
1604 * @param charIndex the index of a character. It must be in the range
1605 * [0..ubidi_getProcessedLength(pBiDi)].
b75a7d8f 1606 *
46f4442e
A
1607 * @return The level for the character at charIndex (0 if charIndex is not
1608 * in the valid range).
b75a7d8f
A
1609 *
1610 * @see UBiDiLevel
73c04bcf 1611 * @see ubidi_getProcessedLength
b75a7d8f
A
1612 * @stable ICU 2.0
1613 */
374ca955 1614U_STABLE UBiDiLevel U_EXPORT2
b75a7d8f
A
1615ubidi_getLevelAt(const UBiDi *pBiDi, int32_t charIndex);
1616
1617/**
1618 * Get an array of levels for each character.<p>
1619 *
1620 * Note that this function may allocate memory under some
1621 * circumstances, unlike <code>ubidi_getLevelAt()</code>.
1622 *
73c04bcf
A
1623 * @param pBiDi is the paragraph or line <code>UBiDi</code> object, whose
1624 * text length must be strictly positive.
b75a7d8f 1625 *
73c04bcf 1626 * @param pErrorCode must be a valid pointer to an error code value.
b75a7d8f
A
1627 *
1628 * @return The levels array for the text,
1629 * or <code>NULL</code> if an error occurs.
1630 *
1631 * @see UBiDiLevel
73c04bcf 1632 * @see ubidi_getProcessedLength
b75a7d8f
A
1633 * @stable ICU 2.0
1634 */
374ca955 1635U_STABLE const UBiDiLevel * U_EXPORT2
b75a7d8f
A
1636ubidi_getLevels(UBiDi *pBiDi, UErrorCode *pErrorCode);
1637
1638/**
1639 * Get a logical run.
1640 * This function returns information about a run and is used
1641 * to retrieve runs in logical order.<p>
1642 * This is especially useful for line-breaking on a paragraph.
1643 *
1644 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1645 *
46f4442e 1646 * @param logicalPosition is a logical position within the source text.
b75a7d8f 1647 *
46f4442e 1648 * @param pLogicalLimit will receive the limit of the corresponding run.
b75a7d8f
A
1649 * The l-value that you point to here may be the
1650 * same expression (variable) as the one for
46f4442e 1651 * <code>logicalPosition</code>.
b75a7d8f
A
1652 * This pointer can be <code>NULL</code> if this
1653 * value is not necessary.
1654 *
46f4442e 1655 * @param pLevel will receive the level of the corresponding run.
b75a7d8f
A
1656 * This pointer can be <code>NULL</code> if this
1657 * value is not necessary.
73c04bcf
A
1658 *
1659 * @see ubidi_getProcessedLength
b75a7d8f
A
1660 * @stable ICU 2.0
1661 */
374ca955 1662U_STABLE void U_EXPORT2
46f4442e 1663ubidi_getLogicalRun(const UBiDi *pBiDi, int32_t logicalPosition,
b75a7d8f
A
1664 int32_t *pLogicalLimit, UBiDiLevel *pLevel);
1665
1666/**
1667 * Get the number of runs.
1668 * This function may invoke the actual reordering on the
1669 * <code>UBiDi</code> object, after <code>ubidi_setPara()</code>
1670 * may have resolved only the levels of the text. Therefore,
1671 * <code>ubidi_countRuns()</code> may have to allocate memory,
1672 * and may fail doing so.
1673 *
1674 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1675 *
73c04bcf 1676 * @param pErrorCode must be a valid pointer to an error code value.
b75a7d8f
A
1677 *
1678 * @return The number of runs.
1679 * @stable ICU 2.0
1680 */
374ca955 1681U_STABLE int32_t U_EXPORT2
b75a7d8f
A
1682ubidi_countRuns(UBiDi *pBiDi, UErrorCode *pErrorCode);
1683
1684/**
1685 * Get one run's logical start, length, and directionality,
1686 * which can be 0 for LTR or 1 for RTL.
1687 * In an RTL run, the character at the logical start is
1688 * visually on the right of the displayed run.
1689 * The length is the number of characters in the run.<p>
1690 * <code>ubidi_countRuns()</code> should be called
1691 * before the runs are retrieved.
1692 *
1693 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1694 *
1695 * @param runIndex is the number of the run in visual order, in the
1696 * range <code>[0..ubidi_countRuns(pBiDi)-1]</code>.
1697 *
1698 * @param pLogicalStart is the first logical character index in the text.
1699 * The pointer may be <code>NULL</code> if this index is not needed.
1700 *
1701 * @param pLength is the number of characters (at least one) in the run.
1702 * The pointer may be <code>NULL</code> if this is not needed.
1703 *
1704 * @return the directionality of the run,
1705 * <code>UBIDI_LTR==0</code> or <code>UBIDI_RTL==1</code>,
729e4ab9
A
1706 * never <code>UBIDI_MIXED</code>,
1707 * never <code>UBIDI_NEUTRAL</code>.
b75a7d8f
A
1708 *
1709 * @see ubidi_countRuns
1710 *
1711 * Example:
1712 * <pre>
1713 * \code
1714 * int32_t i, count=ubidi_countRuns(pBiDi),
1715 * logicalStart, visualIndex=0, length;
1716 * for(i=0; i<count; ++i) {
1717 * if(UBIDI_LTR==ubidi_getVisualRun(pBiDi, i, &logicalStart, &length)) {
1718 * do { // LTR
1719 * show_char(text[logicalStart++], visualIndex++);
1720 * } while(--length>0);
1721 * } else {
1722 * logicalStart+=length; // logicalLimit
1723 * do { // RTL
1724 * show_char(text[--logicalStart], visualIndex++);
1725 * } while(--length>0);
1726 * }
1727 * }
1728 *\endcode
1729 * </pre>
1730 *
1731 * Note that in right-to-left runs, code like this places
46f4442e
A
1732 * second surrogates before first ones (which is generally a bad idea)
1733 * and combining characters before base characters.
1734 * <p>
1735 * Use of <code>ubidi_writeReordered()</code>, optionally with the
1736 * <code>#UBIDI_KEEP_BASE_COMBINING</code> option, can be considered in order
1737 * to avoid these issues.
b75a7d8f
A
1738 * @stable ICU 2.0
1739 */
374ca955 1740U_STABLE UBiDiDirection U_EXPORT2
b75a7d8f
A
1741ubidi_getVisualRun(UBiDi *pBiDi, int32_t runIndex,
1742 int32_t *pLogicalStart, int32_t *pLength);
1743
1744/**
1745 * Get the visual position from a logical text position.
1746 * If such a mapping is used many times on the same
1747 * <code>UBiDi</code> object, then calling
1748 * <code>ubidi_getLogicalMap()</code> is more efficient.<p>
1749 *
73c04bcf 1750 * The value returned may be <code>#UBIDI_MAP_NOWHERE</code> if there is no
46f4442e 1751 * visual position because the corresponding text character is a Bidi control
73c04bcf
A
1752 * removed from output by the option <code>#UBIDI_OPTION_REMOVE_CONTROLS</code>.
1753 * <p>
46f4442e
A
1754 * When the visual output is altered by using options of
1755 * <code>ubidi_writeReordered()</code> such as <code>UBIDI_INSERT_LRM_FOR_NUMERIC</code>,
1756 * <code>UBIDI_KEEP_BASE_COMBINING</code>, <code>UBIDI_OUTPUT_REVERSE</code>,
1757 * <code>UBIDI_REMOVE_BIDI_CONTROLS</code>, the visual position returned may not
1758 * be correct. It is advised to use, when possible, reordering options
1759 * such as <code>UBIDI_OPTION_INSERT_MARKS</code> and <code>UBIDI_OPTION_REMOVE_CONTROLS</code>.
1760 * <p>
b75a7d8f 1761 * Note that in right-to-left runs, this mapping places
46f4442e
A
1762 * second surrogates before first ones (which is generally a bad idea)
1763 * and combining characters before base characters.
1764 * Use of <code>ubidi_writeReordered()</code>, optionally with the
1765 * <code>#UBIDI_KEEP_BASE_COMBINING</code> option can be considered instead
1766 * of using the mapping, in order to avoid these issues.
b75a7d8f
A
1767 *
1768 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1769 *
1770 * @param logicalIndex is the index of a character in the text.
1771 *
73c04bcf 1772 * @param pErrorCode must be a valid pointer to an error code value.
b75a7d8f
A
1773 *
1774 * @return The visual position of this character.
1775 *
1776 * @see ubidi_getLogicalMap
1777 * @see ubidi_getLogicalIndex
73c04bcf 1778 * @see ubidi_getProcessedLength
b75a7d8f
A
1779 * @stable ICU 2.0
1780 */
374ca955 1781U_STABLE int32_t U_EXPORT2
b75a7d8f
A
1782ubidi_getVisualIndex(UBiDi *pBiDi, int32_t logicalIndex, UErrorCode *pErrorCode);
1783
1784/**
1785 * Get the logical text position from a visual position.
1786 * If such a mapping is used many times on the same
1787 * <code>UBiDi</code> object, then calling
1788 * <code>ubidi_getVisualMap()</code> is more efficient.<p>
1789 *
73c04bcf 1790 * The value returned may be <code>#UBIDI_MAP_NOWHERE</code> if there is no
46f4442e 1791 * logical position because the corresponding text character is a Bidi mark
73c04bcf
A
1792 * inserted in the output by option <code>#UBIDI_OPTION_INSERT_MARKS</code>.
1793 * <p>
b75a7d8f 1794 * This is the inverse function to <code>ubidi_getVisualIndex()</code>.
46f4442e
A
1795 * <p>
1796 * When the visual output is altered by using options of
1797 * <code>ubidi_writeReordered()</code> such as <code>UBIDI_INSERT_LRM_FOR_NUMERIC</code>,
1798 * <code>UBIDI_KEEP_BASE_COMBINING</code>, <code>UBIDI_OUTPUT_REVERSE</code>,
1799 * <code>UBIDI_REMOVE_BIDI_CONTROLS</code>, the logical position returned may not
1800 * be correct. It is advised to use, when possible, reordering options
1801 * such as <code>UBIDI_OPTION_INSERT_MARKS</code> and <code>UBIDI_OPTION_REMOVE_CONTROLS</code>.
b75a7d8f
A
1802 *
1803 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1804 *
1805 * @param visualIndex is the visual position of a character.
1806 *
73c04bcf 1807 * @param pErrorCode must be a valid pointer to an error code value.
b75a7d8f
A
1808 *
1809 * @return The index of this character in the text.
1810 *
1811 * @see ubidi_getVisualMap
1812 * @see ubidi_getVisualIndex
73c04bcf 1813 * @see ubidi_getResultLength
b75a7d8f
A
1814 * @stable ICU 2.0
1815 */
374ca955 1816U_STABLE int32_t U_EXPORT2
b75a7d8f
A
1817ubidi_getLogicalIndex(UBiDi *pBiDi, int32_t visualIndex, UErrorCode *pErrorCode);
1818
1819/**
1820 * Get a logical-to-visual index map (array) for the characters in the UBiDi
1821 * (paragraph or line) object.
73c04bcf
A
1822 * <p>
1823 * Some values in the map may be <code>#UBIDI_MAP_NOWHERE</code> if the
46f4442e 1824 * corresponding text characters are Bidi controls removed from the visual
73c04bcf 1825 * output by the option <code>#UBIDI_OPTION_REMOVE_CONTROLS</code>.
46f4442e
A
1826 * <p>
1827 * When the visual output is altered by using options of
1828 * <code>ubidi_writeReordered()</code> such as <code>UBIDI_INSERT_LRM_FOR_NUMERIC</code>,
1829 * <code>UBIDI_KEEP_BASE_COMBINING</code>, <code>UBIDI_OUTPUT_REVERSE</code>,
1830 * <code>UBIDI_REMOVE_BIDI_CONTROLS</code>, the visual positions returned may not
1831 * be correct. It is advised to use, when possible, reordering options
1832 * such as <code>UBIDI_OPTION_INSERT_MARKS</code> and <code>UBIDI_OPTION_REMOVE_CONTROLS</code>.
1833 * <p>
1834 * Note that in right-to-left runs, this mapping places
1835 * second surrogates before first ones (which is generally a bad idea)
1836 * and combining characters before base characters.
1837 * Use of <code>ubidi_writeReordered()</code>, optionally with the
1838 * <code>#UBIDI_KEEP_BASE_COMBINING</code> option can be considered instead
1839 * of using the mapping, in order to avoid these issues.
b75a7d8f
A
1840 *
1841 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1842 *
73c04bcf 1843 * @param indexMap is a pointer to an array of <code>ubidi_getProcessedLength()</code>
b75a7d8f 1844 * indexes which will reflect the reordering of the characters.
73c04bcf
A
1845 * If option <code>#UBIDI_OPTION_INSERT_MARKS</code> is set, the number
1846 * of elements allocated in <code>indexMap</code> must be no less than
1847 * <code>ubidi_getResultLength()</code>.
1848 * The array does not need to be initialized.<br><br>
1849 * The index map will result in <code>indexMap[logicalIndex]==visualIndex</code>.
b75a7d8f 1850 *
73c04bcf 1851 * @param pErrorCode must be a valid pointer to an error code value.
b75a7d8f
A
1852 *
1853 * @see ubidi_getVisualMap
1854 * @see ubidi_getVisualIndex
73c04bcf
A
1855 * @see ubidi_getProcessedLength
1856 * @see ubidi_getResultLength
b75a7d8f
A
1857 * @stable ICU 2.0
1858 */
374ca955 1859U_STABLE void U_EXPORT2
b75a7d8f
A
1860ubidi_getLogicalMap(UBiDi *pBiDi, int32_t *indexMap, UErrorCode *pErrorCode);
1861
1862/**
1863 * Get a visual-to-logical index map (array) for the characters in the UBiDi
1864 * (paragraph or line) object.
73c04bcf
A
1865 * <p>
1866 * Some values in the map may be <code>#UBIDI_MAP_NOWHERE</code> if the
46f4442e 1867 * corresponding text characters are Bidi marks inserted in the visual output
73c04bcf 1868 * by the option <code>#UBIDI_OPTION_INSERT_MARKS</code>.
46f4442e
A
1869 * <p>
1870 * When the visual output is altered by using options of
1871 * <code>ubidi_writeReordered()</code> such as <code>UBIDI_INSERT_LRM_FOR_NUMERIC</code>,
1872 * <code>UBIDI_KEEP_BASE_COMBINING</code>, <code>UBIDI_OUTPUT_REVERSE</code>,
1873 * <code>UBIDI_REMOVE_BIDI_CONTROLS</code>, the logical positions returned may not
1874 * be correct. It is advised to use, when possible, reordering options
1875 * such as <code>UBIDI_OPTION_INSERT_MARKS</code> and <code>UBIDI_OPTION_REMOVE_CONTROLS</code>.
b75a7d8f
A
1876 *
1877 * @param pBiDi is the paragraph or line <code>UBiDi</code> object.
1878 *
73c04bcf 1879 * @param indexMap is a pointer to an array of <code>ubidi_getResultLength()</code>
b75a7d8f 1880 * indexes which will reflect the reordering of the characters.
73c04bcf
A
1881 * If option <code>#UBIDI_OPTION_REMOVE_CONTROLS</code> is set, the number
1882 * of elements allocated in <code>indexMap</code> must be no less than
1883 * <code>ubidi_getProcessedLength()</code>.
1884 * The array does not need to be initialized.<br><br>
1885 * The index map will result in <code>indexMap[visualIndex]==logicalIndex</code>.
b75a7d8f 1886 *
73c04bcf 1887 * @param pErrorCode must be a valid pointer to an error code value.
b75a7d8f
A
1888 *
1889 * @see ubidi_getLogicalMap
1890 * @see ubidi_getLogicalIndex
73c04bcf
A
1891 * @see ubidi_getProcessedLength
1892 * @see ubidi_getResultLength
b75a7d8f
A
1893 * @stable ICU 2.0
1894 */
374ca955 1895U_STABLE void U_EXPORT2
b75a7d8f
A
1896ubidi_getVisualMap(UBiDi *pBiDi, int32_t *indexMap, UErrorCode *pErrorCode);
1897
1898/**
1899 * This is a convenience function that does not use a UBiDi object.
1900 * It is intended to be used for when an application has determined the levels
1901 * of objects (character sequences) and just needs to have them reordered (L2).
73c04bcf 1902 * This is equivalent to using <code>ubidi_getLogicalMap()</code> on a
b75a7d8f
A
1903 * <code>UBiDi</code> object.
1904 *
1905 * @param levels is an array with <code>length</code> levels that have been determined by
1906 * the application.
1907 *
1908 * @param length is the number of levels in the array, or, semantically,
1909 * the number of objects to be reordered.
1910 * It must be <code>length>0</code>.
1911 *
1912 * @param indexMap is a pointer to an array of <code>length</code>
1913 * indexes which will reflect the reordering of the characters.
1914 * The array does not need to be initialized.<p>
1915 * The index map will result in <code>indexMap[logicalIndex]==visualIndex</code>.
1916 * @stable ICU 2.0
1917 */
374ca955 1918U_STABLE void U_EXPORT2
b75a7d8f
A
1919ubidi_reorderLogical(const UBiDiLevel *levels, int32_t length, int32_t *indexMap);
1920
1921/**
1922 * This is a convenience function that does not use a UBiDi object.
1923 * It is intended to be used for when an application has determined the levels
1924 * of objects (character sequences) and just needs to have them reordered (L2).
73c04bcf 1925 * This is equivalent to using <code>ubidi_getVisualMap()</code> on a
b75a7d8f
A
1926 * <code>UBiDi</code> object.
1927 *
1928 * @param levels is an array with <code>length</code> levels that have been determined by
1929 * the application.
1930 *
1931 * @param length is the number of levels in the array, or, semantically,
1932 * the number of objects to be reordered.
1933 * It must be <code>length>0</code>.
1934 *
1935 * @param indexMap is a pointer to an array of <code>length</code>
1936 * indexes which will reflect the reordering of the characters.
1937 * The array does not need to be initialized.<p>
1938 * The index map will result in <code>indexMap[visualIndex]==logicalIndex</code>.
1939 * @stable ICU 2.0
1940 */
374ca955 1941U_STABLE void U_EXPORT2
b75a7d8f
A
1942ubidi_reorderVisual(const UBiDiLevel *levels, int32_t length, int32_t *indexMap);
1943
1944/**
1945 * Invert an index map.
73c04bcf 1946 * The index mapping of the first map is inverted and written to
b75a7d8f
A
1947 * the second one.
1948 *
46f4442e 1949 * @param srcMap is an array with <code>length</code> elements
73c04bcf
A
1950 * which defines the original mapping from a source array containing
1951 * <code>length</code> elements to a destination array.
46f4442e
A
1952 * Some elements of the source array may have no mapping in the
1953 * destination array. In that case, their value will be
1954 * the special value <code>UBIDI_MAP_NOWHERE</code>.
1955 * All elements must be >=0 or equal to <code>UBIDI_MAP_NOWHERE</code>.
1956 * Some elements may have a value >= <code>length</code>, if the
73c04bcf 1957 * destination array has more elements than the source array.
46f4442e 1958 * There must be no duplicate indexes (two or more elements with the
73c04bcf
A
1959 * same value except <code>UBIDI_MAP_NOWHERE</code>).
1960 *
46f4442e 1961 * @param destMap is an array with a number of elements equal to 1 + the highest
73c04bcf
A
1962 * value in <code>srcMap</code>.
1963 * <code>destMap</code> will be filled with the inverse mapping.
46f4442e
A
1964 * If element with index i in <code>srcMap</code> has a value k different
1965 * from <code>UBIDI_MAP_NOWHERE</code>, this means that element i of
1966 * the source array maps to element k in the destination array.
1967 * The inverse map will have value i in its k-th element.
1968 * For all elements of the destination array which do not map to
1969 * an element in the source array, the corresponding element in the
1970 * inverse map will have a value equal to <code>UBIDI_MAP_NOWHERE</code>.
b75a7d8f
A
1971 *
1972 * @param length is the length of each array.
46f4442e 1973 * @see UBIDI_MAP_NOWHERE
b75a7d8f
A
1974 * @stable ICU 2.0
1975 */
374ca955 1976U_STABLE void U_EXPORT2
b75a7d8f
A
1977ubidi_invertMap(const int32_t *srcMap, int32_t *destMap, int32_t length);
1978
1979/** option flags for ubidi_writeReordered() */
1980
1981/**
1982 * option bit for ubidi_writeReordered():
1983 * keep combining characters after their base characters in RTL runs
1984 *
1985 * @see ubidi_writeReordered
1986 * @stable ICU 2.0
1987 */
1988#define UBIDI_KEEP_BASE_COMBINING 1
1989
1990/**
1991 * option bit for ubidi_writeReordered():
1992 * replace characters with the "mirrored" property in RTL runs
1993 * by their mirror-image mappings
1994 *
1995 * @see ubidi_writeReordered
1996 * @stable ICU 2.0
1997 */
1998#define UBIDI_DO_MIRRORING 2
1999
2000/**
2001 * option bit for ubidi_writeReordered():
2002 * surround the run with LRMs if necessary;
46f4442e 2003 * this is part of the approximate "inverse Bidi" algorithm
b75a7d8f 2004 *
73c04bcf
A
2005 * <p>This option does not imply corresponding adjustment of the index
2006 * mappings.</p>
2007 *
b75a7d8f
A
2008 * @see ubidi_setInverse
2009 * @see ubidi_writeReordered
2010 * @stable ICU 2.0
2011 */
2012#define UBIDI_INSERT_LRM_FOR_NUMERIC 4
2013
2014/**
2015 * option bit for ubidi_writeReordered():
46f4442e 2016 * remove Bidi control characters
73c04bcf
A
2017 * (this does not affect #UBIDI_INSERT_LRM_FOR_NUMERIC)
2018 *
2019 * <p>This option does not imply corresponding adjustment of the index
2020 * mappings.</p>
b75a7d8f
A
2021 *
2022 * @see ubidi_writeReordered
2023 * @stable ICU 2.0
2024 */
2025#define UBIDI_REMOVE_BIDI_CONTROLS 8
2026
2027/**
2028 * option bit for ubidi_writeReordered():
2029 * write the output in reverse order
2030 *
2031 * <p>This has the same effect as calling <code>ubidi_writeReordered()</code>
2032 * first without this option, and then calling
2033 * <code>ubidi_writeReverse()</code> without mirroring.
2034 * Doing this in the same step is faster and avoids a temporary buffer.
2035 * An example for using this option is output to a character terminal that
2036 * is designed for RTL scripts and stores text in reverse order.</p>
2037 *
2038 * @see ubidi_writeReordered
2039 * @stable ICU 2.0
2040 */
2041#define UBIDI_OUTPUT_REVERSE 16
2042
73c04bcf
A
2043/**
2044 * Get the length of the source text processed by the last call to
2045 * <code>ubidi_setPara()</code>. This length may be different from the length
2046 * of the source text if option <code>#UBIDI_OPTION_STREAMING</code>
2047 * has been set.
2048 * <br>
2049 * Note that whenever the length of the text affects the execution or the
2050 * result of a function, it is the processed length which must be considered,
2051 * except for <code>ubidi_setPara</code> (which receives unprocessed source
2052 * text) and <code>ubidi_getLength</code> (which returns the original length
2053 * of the source text).<br>
2054 * In particular, the processed length is the one to consider in the following
2055 * cases:
2056 * <ul>
2057 * <li>maximum value of the <code>limit</code> argument of
2058 * <code>ubidi_setLine</code></li>
2059 * <li>maximum value of the <code>charIndex</code> argument of
2060 * <code>ubidi_getParagraph</code></li>
2061 * <li>maximum value of the <code>charIndex</code> argument of
2062 * <code>ubidi_getLevelAt</code></li>
2063 * <li>number of elements in the array returned by <code>ubidi_getLevels</code></li>
2064 * <li>maximum value of the <code>logicalStart</code> argument of
2065 * <code>ubidi_getLogicalRun</code></li>
2066 * <li>maximum value of the <code>logicalIndex</code> argument of
2067 * <code>ubidi_getVisualIndex</code></li>
2068 * <li>number of elements filled in the <code>*indexMap</code> argument of
2069 * <code>ubidi_getLogicalMap</code></li>
2070 * <li>length of text processed by <code>ubidi_writeReordered</code></li>
2071 * </ul>
2072 *
2073 * @param pBiDi is the paragraph <code>UBiDi</code> object.
2074 *
2075 * @return The length of the part of the source text processed by
2076 * the last call to <code>ubidi_setPara</code>.
2077 * @see ubidi_setPara
2078 * @see UBIDI_OPTION_STREAMING
46f4442e 2079 * @stable ICU 3.6
73c04bcf 2080 */
46f4442e 2081U_STABLE int32_t U_EXPORT2
73c04bcf
A
2082ubidi_getProcessedLength(const UBiDi *pBiDi);
2083
2084/**
2085 * Get the length of the reordered text resulting from the last call to
2086 * <code>ubidi_setPara()</code>. This length may be different from the length
2087 * of the source text if option <code>#UBIDI_OPTION_INSERT_MARKS</code>
2088 * or option <code>#UBIDI_OPTION_REMOVE_CONTROLS</code> has been set.
2089 * <br>
2090 * This resulting length is the one to consider in the following cases:
2091 * <ul>
2092 * <li>maximum value of the <code>visualIndex</code> argument of
2093 * <code>ubidi_getLogicalIndex</code></li>
2094 * <li>number of elements of the <code>*indexMap</code> argument of
2095 * <code>ubidi_getVisualMap</code></li>
2096 * </ul>
2097 * Note that this length stays identical to the source text length if
46f4442e 2098 * Bidi marks are inserted or removed using option bits of
73c04bcf
A
2099 * <code>ubidi_writeReordered</code>, or if option
2100 * <code>#UBIDI_REORDER_INVERSE_NUMBERS_AS_L</code> has been set.
2101 *
2102 * @param pBiDi is the paragraph <code>UBiDi</code> object.
2103 *
2104 * @return The length of the reordered text resulting from
2105 * the last call to <code>ubidi_setPara</code>.
2106 * @see ubidi_setPara
2107 * @see UBIDI_OPTION_INSERT_MARKS
2108 * @see UBIDI_OPTION_REMOVE_CONTROLS
46f4442e 2109 * @stable ICU 3.6
73c04bcf 2110 */
46f4442e 2111U_STABLE int32_t U_EXPORT2
73c04bcf
A
2112ubidi_getResultLength(const UBiDi *pBiDi);
2113
2114U_CDECL_BEGIN
2115/**
2116 * value returned by <code>UBiDiClassCallback</code> callbacks when
46f4442e 2117 * there is no need to override the standard Bidi class for a given code point.
73c04bcf 2118 * @see UBiDiClassCallback
46f4442e 2119 * @stable ICU 3.6
73c04bcf
A
2120 */
2121#define U_BIDI_CLASS_DEFAULT U_CHAR_DIRECTION_COUNT
2122
2123/**
46f4442e 2124 * Callback type declaration for overriding default Bidi class values with
73c04bcf
A
2125 * custom ones.
2126 * <p>Usually, the function pointer will be propagated to a <code>UBiDi</code>
2127 * object by calling the <code>ubidi_setClassCallback()</code> function;
2128 * then the callback will be invoked by the UBA implementation any time the
2129 * class of a character is to be determined.</p>
2130 *
2131 * @param context is a pointer to the callback private data.
2132 *
46f4442e 2133 * @param c is the code point to get a Bidi class for.
73c04bcf 2134 *
46f4442e 2135 * @return The directional property / Bidi class for the given code point
73c04bcf 2136 * <code>c</code> if the default class has been overridden, or
46f4442e 2137 * <code>#U_BIDI_CLASS_DEFAULT</code> if the standard Bidi class value
73c04bcf
A
2138 * for <code>c</code> is to be used.
2139 * @see ubidi_setClassCallback
2140 * @see ubidi_getClassCallback
46f4442e 2141 * @stable ICU 3.6
73c04bcf
A
2142 */
2143typedef UCharDirection U_CALLCONV
2144UBiDiClassCallback(const void *context, UChar32 c);
2145
2146U_CDECL_END
2147
2148/**
46f4442e 2149 * Retrieve the Bidi class for a given code point.
73c04bcf
A
2150 * <p>If a <code>#UBiDiClassCallback</code> callback is defined and returns a
2151 * value other than <code>#U_BIDI_CLASS_DEFAULT</code>, that value is used;
2152 * otherwise the default class determination mechanism is invoked.</p>
2153 *
2154 * @param pBiDi is the paragraph <code>UBiDi</code> object.
2155 *
46f4442e 2156 * @param c is the code point whose Bidi class must be retrieved.
73c04bcf 2157 *
46f4442e 2158 * @return The Bidi class for character <code>c</code> based
73c04bcf
A
2159 * on the given <code>pBiDi</code> instance.
2160 * @see UBiDiClassCallback
46f4442e 2161 * @stable ICU 3.6
73c04bcf 2162 */
46f4442e 2163U_STABLE UCharDirection U_EXPORT2
73c04bcf
A
2164ubidi_getCustomizedClass(UBiDi *pBiDi, UChar32 c);
2165
2166/**
2167 * Set the callback function and callback data used by the UBA
46f4442e
A
2168 * implementation for Bidi class determination.
2169 * <p>This may be useful for assigning Bidi classes to PUA characters, or
73c04bcf
A
2170 * for special application needs. For instance, an application may want to
2171 * handle all spaces like L or R characters (according to the base direction)
2172 * when creating the visual ordering of logical lines which are part of a report
2173 * organized in columns: there should not be interaction between adjacent
2174 * cells.<p>
2175 *
2176 * @param pBiDi is the paragraph <code>UBiDi</code> object.
2177 *
2178 * @param newFn is the new callback function pointer.
2179 *
2180 * @param newContext is the new callback context pointer. This can be NULL.
2181 *
2182 * @param oldFn fillin: Returns the old callback function pointer. This can be
2183 * NULL.
2184 *
2185 * @param oldContext fillin: Returns the old callback's context. This can be
2186 * NULL.
2187 *
2188 * @param pErrorCode must be a valid pointer to an error code value.
2189 *
2190 * @see ubidi_getClassCallback
46f4442e 2191 * @stable ICU 3.6
73c04bcf 2192 */
46f4442e 2193U_STABLE void U_EXPORT2
73c04bcf
A
2194ubidi_setClassCallback(UBiDi *pBiDi, UBiDiClassCallback *newFn,
2195 const void *newContext, UBiDiClassCallback **oldFn,
2196 const void **oldContext, UErrorCode *pErrorCode);
2197
2198/**
46f4442e 2199 * Get the current callback function used for Bidi class determination.
73c04bcf
A
2200 *
2201 * @param pBiDi is the paragraph <code>UBiDi</code> object.
2202 *
2203 * @param fn fillin: Returns the callback function pointer.
2204 *
2205 * @param context fillin: Returns the callback's private context.
2206 *
2207 * @see ubidi_setClassCallback
46f4442e 2208 * @stable ICU 3.6
73c04bcf 2209 */
46f4442e 2210U_STABLE void U_EXPORT2
73c04bcf
A
2211ubidi_getClassCallback(UBiDi *pBiDi, UBiDiClassCallback **fn, const void **context);
2212
b75a7d8f
A
2213/**
2214 * Take a <code>UBiDi</code> object containing the reordering
73c04bcf
A
2215 * information for a piece of text (one or more paragraphs) set by
2216 * <code>ubidi_setPara()</code> or for a line of text set by
2217 * <code>ubidi_setLine()</code> and write a reordered string to the
2218 * destination buffer.
b75a7d8f
A
2219 *
2220 * This function preserves the integrity of characters with multiple
46f4442e 2221 * code units and (optionally) combining characters.
b75a7d8f
A
2222 * Characters in RTL runs can be replaced by mirror-image characters
2223 * in the destination buffer. Note that "real" mirroring has
2224 * to be done in a rendering engine by glyph selection
2225 * and that for many "mirrored" characters there are no
2226 * Unicode characters as mirror-image equivalents.
46f4442e 2227 * There are also options to insert or remove Bidi control
b75a7d8f
A
2228 * characters; see the description of the <code>destSize</code>
2229 * and <code>options</code> parameters and of the option bit flags.
2230 *
2231 * @param pBiDi A pointer to a <code>UBiDi</code> object that
2232 * is set by <code>ubidi_setPara()</code> or
2233 * <code>ubidi_setLine()</code> and contains the reordering
2234 * information for the text that it was defined for,
73c04bcf
A
2235 * as well as a pointer to that text.<br><br>
2236 * The text was aliased (only the pointer was stored
b75a7d8f 2237 * without copying the contents) and must not have been modified
73c04bcf 2238 * since the <code>ubidi_setPara()</code> call.
b75a7d8f
A
2239 *
2240 * @param dest A pointer to where the reordered text is to be copied.
2241 * The source text and <code>dest[destSize]</code>
2242 * must not overlap.
2243 *
2244 * @param destSize The size of the <code>dest</code> buffer,
2245 * in number of UChars.
2246 * If the <code>UBIDI_INSERT_LRM_FOR_NUMERIC</code>
2247 * option is set, then the destination length could be
2248 * as large as
2249 * <code>ubidi_getLength(pBiDi)+2*ubidi_countRuns(pBiDi)</code>.
2250 * If the <code>UBIDI_REMOVE_BIDI_CONTROLS</code> option
2251 * is set, then the destination length may be less than
2252 * <code>ubidi_getLength(pBiDi)</code>.
2253 * If none of these options is set, then the destination length
46f4442e 2254 * will be exactly <code>ubidi_getProcessedLength(pBiDi)</code>.
b75a7d8f
A
2255 *
2256 * @param options A bit set of options for the reordering that control
2257 * how the reordered text is written.
2258 * The options include mirroring the characters on a code
2259 * point basis and inserting LRM characters, which is used
2260 * especially for transforming visually stored text
2261 * to logically stored text (although this is still an
46f4442e
A
2262 * imperfect implementation of an "inverse Bidi" algorithm
2263 * because it uses the "forward Bidi" algorithm at its core).
374ca955
A
2264 * The available options are:
2265 * <code>#UBIDI_DO_MIRRORING</code>,
2266 * <code>#UBIDI_INSERT_LRM_FOR_NUMERIC</code>,
2267 * <code>#UBIDI_KEEP_BASE_COMBINING</code>,
2268 * <code>#UBIDI_OUTPUT_REVERSE</code>,
2269 * <code>#UBIDI_REMOVE_BIDI_CONTROLS</code>
b75a7d8f 2270 *
73c04bcf 2271 * @param pErrorCode must be a valid pointer to an error code value.
b75a7d8f
A
2272 *
2273 * @return The length of the output string.
73c04bcf
A
2274 *
2275 * @see ubidi_getProcessedLength
b75a7d8f
A
2276 * @stable ICU 2.0
2277 */
374ca955 2278U_STABLE int32_t U_EXPORT2
b75a7d8f
A
2279ubidi_writeReordered(UBiDi *pBiDi,
2280 UChar *dest, int32_t destSize,
2281 uint16_t options,
2282 UErrorCode *pErrorCode);
2283
2284/**
2285 * Reverse a Right-To-Left run of Unicode text.
2286 *
2287 * This function preserves the integrity of characters with multiple
46f4442e 2288 * code units and (optionally) combining characters.
b75a7d8f
A
2289 * Characters can be replaced by mirror-image characters
2290 * in the destination buffer. Note that "real" mirroring has
2291 * to be done in a rendering engine by glyph selection
2292 * and that for many "mirrored" characters there are no
2293 * Unicode characters as mirror-image equivalents.
46f4442e 2294 * There are also options to insert or remove Bidi control
b75a7d8f
A
2295 * characters.
2296 *
2297 * This function is the implementation for reversing RTL runs as part
2298 * of <code>ubidi_writeReordered()</code>. For detailed descriptions
2299 * of the parameters, see there.
46f4442e 2300 * Since no Bidi controls are inserted here, the output string length
b75a7d8f
A
2301 * will never exceed <code>srcLength</code>.
2302 *
2303 * @see ubidi_writeReordered
2304 *
2305 * @param src A pointer to the RTL run text.
2306 *
2307 * @param srcLength The length of the RTL run.
2308 *
2309 * @param dest A pointer to where the reordered text is to be copied.
2310 * <code>src[srcLength]</code> and <code>dest[destSize]</code>
2311 * must not overlap.
2312 *
2313 * @param destSize The size of the <code>dest</code> buffer,
2314 * in number of UChars.
2315 * If the <code>UBIDI_REMOVE_BIDI_CONTROLS</code> option
2316 * is set, then the destination length may be less than
2317 * <code>srcLength</code>.
2318 * If this option is not set, then the destination length
2319 * will be exactly <code>srcLength</code>.
2320 *
2321 * @param options A bit set of options for the reordering that control
2322 * how the reordered text is written.
374ca955 2323 * See the <code>options</code> parameter in <code>ubidi_writeReordered()</code>.
b75a7d8f 2324 *
73c04bcf 2325 * @param pErrorCode must be a valid pointer to an error code value.
b75a7d8f
A
2326 *
2327 * @return The length of the output string.
2328 * @stable ICU 2.0
2329 */
374ca955 2330U_STABLE int32_t U_EXPORT2
b75a7d8f
A
2331ubidi_writeReverse(const UChar *src, int32_t srcLength,
2332 UChar *dest, int32_t destSize,
2333 uint16_t options,
2334 UErrorCode *pErrorCode);
2335
2336/*#define BIDI_SAMPLE_CODE*/
2337/*@}*/
2338
2339#endif