2
******************************************************************************
4
* Copyright (C) 1999-2001, International Business Machines
5
* Corporation and others. All Rights Reserved.
7
******************************************************************************
10
* tab size: 8 (not used)
13
* created on: 1999jul27
14
* created by: Markus W. Scherer
20
#include "unicode/utypes.h"
21
#include "unicode/uchar.h"
24
* javadoc-style comments are intended to be transformed into HTML
26
* http://www.zib.de/Visual/software/doc++/index.html .
28
* The HTML documentation is created with
31
* The following #define trick allows us to do it all in one file
32
* and still be able to compile it.
35
/*#define BIDI_SAMPLE_CODE*/
39
* \brief C API: BIDI algorithm
41
* <h2>BIDI algorithm for ICU</h2>
43
* This is an implementation of the Unicode Bidirectional algorithm.
44
* The algorithm is defined in the
45
* <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Technical Report 9</a>,
46
* version 5, also described in The Unicode Standard, Version 3.0 .<p>
48
* <h3>General remarks about the API:</h3>
50
* In functions with an error code parameter,
51
* the <code>pErrorCode</code> pointer must be valid
52
* and the value that it points to must not indicate a failure before
53
* the function call. Otherwise, the function returns immediately.
54
* After the function call, the value indicates success or failure.<p>
56
* The "limit" of a sequence of characters is the position just after their
57
* last character, i.e., one more than that position.<p>
59
* Some of the API functions provide access to "runs".
60
* Such a "run" is defined as a sequence of characters
61
* that are at the same embedding level
62
* after performing the BIDI algorithm.<p>
64
* @author Markus W. Scherer
68
* <h4> Sample code for the ICU BIDI API </h4>
70
* <h5>Rendering a paragraph with the ICU BiDi API</h5>
72
* This is (hypothetical) sample code that illustrates
73
* how the ICU BiDi API could be used to render a paragraph of text.
74
* Rendering code depends highly on the graphics system,
75
* therefore this sample code must make a lot of assumptions,
76
* which may or may not match any existing graphics system's properties.
78
* <p>The basic assumptions are:</p>
80
* <li>Rendering is done from left to right on a horizontal line.</li>
81
* <li>A run of single-style, unidirectional text can be rendered at once.</li>
82
* <li>Such a run of text is passed to the graphics system with
83
* characters (code units) in logical order.</li>
84
* <li>The line-breaking algorithm is very complicated
85
* and Locale-dependent -
86
* and therefore its implementation omitted from this sample code.</li>
91
*#include "unicode/ubidi.h"
94
* styleNormal=0, styleSelected=1,
95
* styleBold=2, styleItalics=4,
96
* styleSuper=8, styleSub=16
99
*typedef struct { UTextOffset limit; Style style; } StyleRun;
101
*int getTextWidth(const UChar *text, UTextOffset start, UTextOffset limit,
102
* const StyleRun *styleRuns, int styleRunCount);
104
* // set *pLimit and *pStyleRunLimit for a line
105
* // from text[start] and from styleRuns[styleRunStart]
106
* // using ubidi_getLogicalRun(para, ...)
107
*void getLineBreak(const UChar *text, UTextOffset start, UTextOffset *pLimit,
109
* const StyleRun *styleRuns, int styleRunStart, int *pStyleRunLimit,
112
* // render runs on a line sequentially, always from left to right
114
* // prepare rendering a new line
115
*void startLine(UBiDiDirection textDirection, int lineWidth);
117
* // render a run of text and advance to the right by the run width
118
* // the text[start..limit-1] is always in logical order
119
*void renderRun(const UChar *text, UTextOffset start, UTextOffset limit,
120
* UBiDiDirection textDirection, Style style);
122
* // We could compute a cross-product
123
* // from the style runs with the directional runs
124
* // and then reorder it.
125
* // Instead, here we iterate over each run type
126
* // and render the intersections -
127
* // with shortcuts in simple (and common) cases.
128
* // renderParagraph() is the main function.
130
* // render a directional run with
131
* // (possibly) multiple style runs intersecting with it
132
*void renderDirectionalRun(const UChar *text,
133
* UTextOffset start, UTextOffset limit,
134
* UBiDiDirection direction,
135
* const StyleRun *styleRuns, int styleRunCount) {
138
* // iterate over style runs
139
* if(direction==UBIDI_LTR) {
142
* for(i=0; i<styleRunCount; ++i) {
143
* styleLimit=styleRun[i].limit;
144
* if(start<styleLimit) {
145
* if(styleLimit>limit) { styleLimit=limit; }
146
* renderRun(text, start, styleLimit,
147
* direction, styleRun[i].style);
148
* if(styleLimit==limit) { break; }
155
* for(i=styleRunCount-1; i>=0; --i) {
157
* styleStart=styleRun[i-1].limit;
161
* if(limit>=styleStart) {
162
* if(styleStart<start) { styleStart=start; }
163
* renderRun(text, styleStart, limit,
164
* direction, styleRun[i].style);
165
* if(styleStart==start) { break; }
172
* // the line object represents text[start..limit-1]
173
* void renderLine(UBiDi *line, const UChar *text,
174
* UTextOffset start, UTextOffset limit,
175
* const StyleRun *styleRuns, int styleRunCount) {
176
* UBiDiDirection direction=ubidi_getDirection(line);
177
* if(direction!=UBIDI_MIXED) {
179
* if(styleRunCount<=1) {
180
* renderRun(text, start, limit, direction, styleRuns[0].style);
182
* renderDirectionalRun(text, start, limit,
183
* direction, styleRuns, styleRunCount);
186
* // mixed-directional
187
* UTextOffset count, i, length;
190
* count=ubidi_countRuns(para, pErrorCode);
191
* if(U_SUCCESS(*pErrorCode)) {
192
* if(styleRunCount<=1) {
193
* Style style=styleRuns[0].style;
195
* // iterate over directional runs
196
* for(i=0; i<count; ++i) {
197
* direction=ubidi_getVisualRun(para, i, &start, &length);
198
* renderRun(text, start, start+length, direction, style);
203
* // iterate over both directional and style runs
204
* for(i=0; i<count; ++i) {
205
* direction=ubidi_getVisualRun(line, i, &start, &length);
206
* renderDirectionalRun(text, start, start+length,
207
* direction, styleRuns, styleRunCount);
214
*void renderParagraph(const UChar *text, UTextOffset length,
215
* UBiDiDirection textDirection,
216
* const StyleRun *styleRuns, int styleRunCount,
218
* UErrorCode *pErrorCode) {
221
* if(pErrorCode==NULL || U_FAILURE(*pErrorCode) || length<=0) {
225
* para=ubidi_openSized(length, 0, pErrorCode);
226
* if(para==NULL) { return; }
228
* ubidi_setPara(para, text, length,
229
* textDirection ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR,
231
* if(U_SUCCESS(*pErrorCode)) {
232
* UBiDiLevel paraLevel=1&ubidi_getParaLevel(para);
233
* StyleRun styleRun={ length, styleNormal };
236
* if(styleRuns==NULL || styleRunCount<=0) {
238
* styleRuns=&styleRun;
241
* // assume styleRuns[styleRunCount-1].limit>=length
243
* width=getTextWidth(text, 0, length, styleRuns, styleRunCount);
244
* if(width<=lineWidth) {
245
* // everything fits onto one line
247
* // prepare rendering a new line from either left or right
248
* startLine(paraLevel, width);
250
* renderLine(para, text, 0, length,
251
* styleRuns, styleRunCount);
255
* // we need to render several lines
256
* line=ubidi_openSized(length, 0, pErrorCode);
258
* UTextOffset start=0, limit;
259
* int styleRunStart=0, styleRunLimit;
263
* styleRunLimit=styleRunCount;
264
* getLineBreak(text, start, &limit, para,
265
* styleRuns, styleRunStart, &styleRunLimit,
267
* ubidi_setLine(para, start, limit, line, pErrorCode);
268
* if(U_SUCCESS(*pErrorCode)) {
269
* // prepare rendering a new line
270
* // from either left or right
271
* startLine(paraLevel, width);
273
* renderLine(line, text, start, limit,
274
* styleRuns+styleRunStart,
275
* styleRunLimit-styleRunStart);
277
* if(limit==length) { break; }
279
* styleRunStart=styleRunLimit-1;
280
* if(start>=styleRuns[styleRunStart].limit) {
300
* UBiDiLevel is the type of the level values in this
301
* BiDi implementation.
302
* It holds an embedding level and indicates the visual direction
303
* by its bit 0 (even/odd value).<p>
305
* It can also hold non-level values for the
306
* <code>paraLevel</code> and <code>embeddingLevels</code>
307
* arguments of <code>ubidi_setPara()</code>; there:
309
* <li>bit 7 of an <code>embeddingLevels[]</code>
310
* value indicates whether the using application is
311
* specifying the level of a character to <i>override</i> whatever the
312
* BiDi implementation would resolve it to.</li>
313
* <li><code>paraLevel</code> can be set to the
314
* pesudo-level values <code>UBIDI_DEFAULT_LTR</code>
315
* and <code>UBIDI_DEFAULT_RTL</code>.</li>
319
* <p>The related constants are not real, valid level values.
320
* <code>UBIDI_DEFAULT_XXX</code> can be used to specify
321
* a default for the paragraph level for
322
* when the <code>ubidi_setPara()</code> function
323
* shall determine it but there is no
324
* strongly typed character in the input.<p>
326
* Note that the value for <code>UBIDI_DEFAULT_LTR</code> is even
327
* and the one for <code>UBIDI_DEFAULT_RTL</code> is odd,
328
* just like with normal LTR and RTL level values -
329
* these special values are designed that way. Also, the implementation
330
* assumes that UBIDI_MAX_EXPLICIT_LEVEL is odd.
332
* @see UBIDI_DEFAULT_LTR
333
* @see UBIDI_DEFAULT_RTL
334
* @see UBIDI_LEVEL_OVERRIDE
335
* @see UBIDI_MAX_EXPLICIT_LEVEL
338
typedef uint8_t UBiDiLevel;
340
/** Paragraph level setting.
341
* If there is no strong character, then set the paragraph level to 0 (left-to-right).
344
#define UBIDI_DEFAULT_LTR 0xfe
346
/** Paragraph level setting.
347
* If there is no strong character, then set the paragraph level to 1 (right-to-left).
350
#define UBIDI_DEFAULT_RTL 0xff
353
* Maximum explicit embedding level.
354
* (The maximum resolved level can be up to <code>UBIDI_MAX_EXPLICIT_LEVEL+1</code>).
357
#define UBIDI_MAX_EXPLICIT_LEVEL 61
359
/** Bit flag for level input.
360
* Overrides directional properties.
363
#define UBIDI_LEVEL_OVERRIDE 0x80
366
* @memo <code>UBiDiDirection</code> values indicate the text direction.
369
enum UBiDiDirection {
370
/** @memo All left-to-right text. This is a 0 value. @stable */
372
/** @memo All right-to-left text. This is a 1 value. @stable */
374
/** @memo Mixed-directional text. @stable */
379
typedef enum UBiDiDirection UBiDiDirection;
382
* Forward declaration of the <code>UBiDi</code> structure for the declaration of
383
* the API functions. Its fields are implementation-specific.<p>
384
* This structure holds information about a paragraph of text
385
* with BiDi-algorithm-related details, or about one line of
386
* such a paragraph.<p>
387
* Reordering can be done on a line, or on a paragraph which is
388
* then interpreted as one single line.
394
typedef struct UBiDi UBiDi;
397
* Allocate a <code>UBiDi</code> structure.
398
* Such an object is initially empty. It is assigned
399
* the BiDi properties of a paragraph by <code>ubidi_setPara()</code>
400
* or the BiDi properties of a line of a paragraph by
401
* <code>ubidi_setLine()</code>.<p>
402
* This object can be reused for as long as it is not deallocated
403
* by calling <code>ubidi_close()</code>.<p>
404
* <code>ubidi_set()</code> will allocate additional memory for
405
* internal structures as necessary.
407
* @return An empty <code>UBiDi</code> object.
410
U_CAPI UBiDi * U_EXPORT2
414
* Allocate a <code>UBiDi</code> structure with preallocated memory
415
* for internal structures.
416
* This function provides a <code>UBiDi</code> object like <code>ubidi_open()</code>
417
* with no arguments, but it also preallocates memory for internal structures
418
* according to the sizings supplied by the caller.<p>
419
* Subsequent functions will not allocate any more memory, and are thus
420
* guaranteed not to fail because of lack of memory.<p>
421
* The preallocation can be limited to some of the internal memory
422
* by setting some values to 0 here. That means that if, e.g.,
423
* <code>maxRunCount</code> cannot be reasonably predetermined and should not
424
* be set to <code>maxLength</code> (the only failproof value) to avoid
425
* wasting memory, then <code>maxRunCount</code> could be set to 0 here
426
* and the internal structures that are associated with it will be allocated
427
* on demand, just like with <code>ubidi_open()</code>.
429
* @param maxLength is the maximum paragraph or line length that internal memory
430
* will be preallocated for. An attempt to associate this object with a
431
* longer text will fail, unless this value is 0, which leaves the allocation
432
* up to the implementation.
434
* @param maxRunCount is the maximum anticipated number of same-level runs
435
* that internal memory will be preallocated for. An attempt to access
436
* visual runs on an object that was not preallocated for as many runs
437
* as the text was actually resolved to will fail,
438
* unless this value is 0, which leaves the allocation up to the implementation.<p>
439
* The number of runs depends on the actual text and maybe anywhere between
440
* 1 and <code>maxLength</code>. It is typically small.<p>
442
* @param pErrorCode must be a valid pointer to an error code value,
443
* which must not indicate a failure before the function call.
445
* @return An empty <code>UBiDi</code> object with preallocated memory.
448
U_CAPI UBiDi * U_EXPORT2
449
ubidi_openSized(UTextOffset maxLength, UTextOffset maxRunCount, UErrorCode *pErrorCode);
452
* <code>ubidi_close()</code> must be called to free the memory
453
* associated with a UBiDi object.<p>
455
* <strong>Important: </strong>
456
* If a <code>UBiDi</code> object is the <quote>child</quote>
457
* of another one (its <quote>parent</quote>), after calling
458
* <code>ubidi_setLine()</code>, then the child object must
459
* be destroyed (closed) or reused (by calling
460
* <code>ubidi_setPara()</code> or <code>ubidi_setLine()</code>)
461
* before the parent object.
463
* @param pBiDi is a <code>UBiDi</code> object.
469
U_CAPI void U_EXPORT2
470
ubidi_close(UBiDi *pBiDi);
473
* Modify the operation of the BiDi algorithm such that it
474
* approximates an "inverse BiDi" algorithm. This function
475
* must be called before <code>ubidi_setPara()</code>.
477
* <p>The normal operation of the BiDi algorithm as described
478
* in the Unicode Technical Report is to take text stored in logical
479
* (keyboard, typing) order and to determine the reordering of it for visual
481
* Some legacy codepages store text in visual order, and for operations
482
* with standard, Unicode-based algorithms, the text needs to be transformed
483
* to logical order. This is effectively the inverse algorithm of the
484
* described BiDi algorithm. Note that there is no standard algorithm for
485
* this "inverse BiDi" and that the current implementation provides only an
486
* approximation of "inverse BiDi".</p>
488
* <p>With <code>isInverse</code> set to <code>TRUE</code>,
489
* this function changes the behavior of some of the subsequent functions
490
* in a way that they can be used for the inverse BiDi algorithm.
491
* Specifically, runs of text with numeric characters will be treated in a
492
* special way and may need to be surrounded with LRM characters when they are
493
* written in reordered sequence.</p>
495
* <p>Output runs should be retrieved using <code>ubidi_getVisualRun()</code>.
496
* Since the actual input for "inverse BiDi" is visually ordered text and
497
* <code>ubidi_getVisualRun()</code> gets the reordered runs, these are actually
498
* the runs of the logically ordered output.</p>
500
* @param pBiDi is a <code>UBiDi</code> object.
502
* @param isInverse specifies "forward" or "inverse" BiDi operation
505
* @see ubidi_writeReordered
508
U_CAPI void U_EXPORT2
509
ubidi_setInverse(UBiDi *pBiDi, UBool isInverse);
512
* Is this BiDi object set to perform the inverse BiDi algorithm?
514
* @param pBiDi is a <code>UBiDi</code> object.
516
* @see ubidi_setInverse
519
U_CAPI UBool U_EXPORT2
520
ubidi_isInverse(UBiDi *pBiDi);
523
* Perform the Unicode BiDi algorithm. It is defined in the
524
* <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Technical Report 9</a>,
526
* also described in The Unicode Standard, Version 3.0 .<p>
528
* This function takes a single plain text paragraph with or without
529
* externally specified embedding levels from <quote>styled</quote> text
530
* and computes the left-right-directionality of each character.<p>
532
* If the entire paragraph consists of text of only one direction, then
533
* the function may not perform all the steps described by the algorithm,
534
* i.e., some levels may not be the same as if all steps were performed.
535
* This is not relevant for unidirectional text.<br>
536
* For example, in pure LTR text with numbers the numbers would get
537
* a resolved level of 2 higher than the surrounding text according to
538
* the algorithm. This implementation may set all resolved levels to
539
* the same value in such a case.<p>
541
* The text must be externally split into separate paragraphs (rule P1).
542
* Paragraph separators (B) should appear at most at the very end.
544
* @param pBiDi A <code>UBiDi</code> object allocated with <code>ubidi_open()</code>
545
* which will be set to contain the reordering information,
546
* especially the resolved levels for all the characters in <code>text</code>.
548
* @param text is a pointer to the single-paragraph text that the
549
* BiDi algorithm will be performed on
550
* (step (P1) of the algorithm is performed externally).
551
* <strong>The text must be (at least) <code>length</code> long.</strong>
552
* This pointer is stored in the UBiDi object and can be retrieved
553
* with <code>ubidi_getText()</code>.
555
* @param length is the length of the text; if <code>length==-1</code> then
556
* the text must be zero-terminated.
558
* @param paraLevel specifies the default level for the paragraph;
559
* it is typically 0 (LTR) or 1 (RTL).
560
* If the function shall determine the paragraph level from the text,
561
* then <code>paraLevel</code> can be set to
562
* either <code>UBIDI_DEFAULT_LTR</code>
563
* or <code>UBIDI_DEFAULT_RTL</code>;
564
* if there is no strongly typed character, then
565
* the desired default is used (0 for LTR or 1 for RTL).
566
* Any other value between 0 and <code>UBIDI_MAX_EXPLICIT_LEVEL</code> is also valid,
567
* with odd levels indicating RTL.
569
* @param embeddingLevels (in) may be used to preset the embedding and override levels,
570
* ignoring characters like LRE and PDF in the text.
571
* A level overrides the directional property of its corresponding
572
* (same index) character if the level has the
573
* <code>UBIDI_LEVEL_OVERRIDE</code> bit set.<p>
574
* Except for that bit, it must be
575
* <code>paraLevel<=embeddingLevels[]<=UBIDI_MAX_EXPLICIT_LEVEL</code>.<p>
576
* <strong>Caution: </strong>A copy of this pointer, not of the levels,
577
* will be stored in the <code>UBiDi</code> object;
578
* the <code>embeddingLevels</code> array must not be
579
* deallocated before the <code>UBiDi</code> structure is destroyed or reused,
580
* and the <code>embeddingLevels</code>
581
* should not be modified to avoid unexpected results on subsequent BiDi operations.
582
* However, the <code>ubidi_setPara()</code> and
583
* <code>ubidi_setLine()</code> functions may modify some or all of the levels.<p>
584
* After the <code>UBiDi</code> object is reused or destroyed, the caller
585
* must take care of the deallocation of the <code>embeddingLevels</code> array.<p>
586
* <strong>The <code>embeddingLevels</code> array must be
587
* at least <code>length</code> long.</strong>
589
* @param pErrorCode must be a valid pointer to an error code value,
590
* which must not indicate a failure before the function call.
593
U_CAPI void U_EXPORT2
594
ubidi_setPara(UBiDi *pBiDi, const UChar *text, UTextOffset length,
595
UBiDiLevel paraLevel, UBiDiLevel *embeddingLevels,
596
UErrorCode *pErrorCode);
599
* <code>ubidi_setLine()</code> sets a <code>UBiDi</code> to
600
* contain the reordering information, especially the resolved levels,
601
* for all the characters in a line of text. This line of text is
602
* specified by referring to a <code>UBiDi</code> object representing
603
* this information for a paragraph of text, and by specifying
604
* a range of indexes in this paragraph.<p>
605
* In the new line object, the indexes will range from 0 to <code>limit-start</code>.<p>
607
* This is used after calling <code>ubidi_setPara()</code>
608
* for a paragraph, and after line-breaking on that paragraph.
609
* It is not necessary if the paragraph is treated as a single line.<p>
611
* After line-breaking, rules (L1) and (L2) for the treatment of
612
* trailing WS and for reordering are performed on
613
* a <code>UBiDi</code> object that represents a line.<p>
615
* <strong>Important: </strong><code>pLineBiDi</code> shares data with
616
* <code>pParaBiDi</code>.
617
* You must destroy or reuse <code>pLineBiDi</code> before <code>pParaBiDi</code>.
618
* In other words, you must destroy or reuse the <code>UBiDi</code> object for a line
619
* before the object for its parent paragraph.<p>
621
* The text pointer that was stored in <code>pParaBiDi</code> is also copied,
622
* and <code>start</code> is added to it so that it points to the beginning of the
623
* line for this object.
625
* @param pParaBiDi is the parent paragraph object.
627
* @param start is the line's first index into the paragraph text.
629
* @param limit is just behind the line's last index into the paragraph text
630
* (its last index +1).<br>
631
* It must be <code>0<=start<=limit<=</code>paragraph length.
633
* @param pLineBiDi is the object that will now represent a line of the paragraph.
635
* @param pErrorCode must be a valid pointer to an error code value,
636
* which must not indicate a failure before the function call.
641
U_CAPI void U_EXPORT2
642
ubidi_setLine(const UBiDi *pParaBiDi,
643
UTextOffset start, UTextOffset limit,
645
UErrorCode *pErrorCode);
648
* Get the directionality of the text.
650
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
652
* @return A <code>UBIDI_XXX</code> value that indicates if the entire text
653
* represented by this object is unidirectional,
654
* and which direction, or if it is mixed-directional.
656
* @see UBiDiDirection
659
U_CAPI UBiDiDirection U_EXPORT2
660
ubidi_getDirection(const UBiDi *pBiDi);
663
* Get the pointer to the text.
665
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
667
* @return The pointer to the text that the UBiDi object was created for.
673
U_CAPI const UChar * U_EXPORT2
674
ubidi_getText(const UBiDi *pBiDi);
677
* Get the length of the text.
679
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
681
* @return The length of the text that the UBiDi object was created for.
684
U_CAPI UTextOffset U_EXPORT2
685
ubidi_getLength(const UBiDi *pBiDi);
688
* Get the paragraph level of the text.
690
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
692
* @return The paragraph level.
697
U_CAPI UBiDiLevel U_EXPORT2
698
ubidi_getParaLevel(const UBiDi *pBiDi);
701
* Get the level for one character.
703
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
705
* @param charIndex the index of a character.
707
* @return The level for the character at charIndex.
712
U_CAPI UBiDiLevel U_EXPORT2
713
ubidi_getLevelAt(const UBiDi *pBiDi, UTextOffset charIndex);
716
* Get an array of levels for each character.<p>
718
* Note that this function may allocate memory under some
719
* circumstances, unlike <code>ubidi_getLevelAt()</code>.
721
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
723
* @param pErrorCode must be a valid pointer to an error code value,
724
* which must not indicate a failure before the function call.
726
* @return The levels array for the text,
727
* or <code>NULL</code> if an error occurs.
732
U_CAPI const UBiDiLevel * U_EXPORT2
733
ubidi_getLevels(UBiDi *pBiDi, UErrorCode *pErrorCode);
737
* This function returns information about a run and is used
738
* to retrieve runs in logical order.<p>
739
* This is especially useful for line-breaking on a paragraph.
741
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
743
* @param logicalStart is the first character of the run.
745
* @param pLogicalLimit will receive the limit of the run.
746
* The l-value that you point to here may be the
747
* same expression (variable) as the one for
748
* <code>logicalStart</code>.
749
* This pointer can be <code>NULL</code> if this
750
* value is not necessary.
752
* @param pLevel will receive the level of the run.
753
* This pointer can be <code>NULL</code> if this
754
* value is not necessary.
757
U_CAPI void U_EXPORT2
758
ubidi_getLogicalRun(const UBiDi *pBiDi, UTextOffset logicalStart,
759
UTextOffset *pLogicalLimit, UBiDiLevel *pLevel);
762
* Get the number of runs.
763
* This function may invoke the actual reordering on the
764
* <code>UBiDi</code> object, after <code>ubidi_setPara()</code>
765
* may have resolved only the levels of the text. Therefore,
766
* <code>ubidi_countRuns()</code> may have to allocate memory,
767
* and may fail doing so.
769
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
771
* @param pErrorCode must be a valid pointer to an error code value,
772
* which must not indicate a failure before the function call.
774
* @return The number of runs.
777
U_CAPI UTextOffset U_EXPORT2
778
ubidi_countRuns(UBiDi *pBiDi, UErrorCode *pErrorCode);
781
* Get one run's logical start, length, and directionality,
782
* which can be 0 for LTR or 1 for RTL.
783
* In an RTL run, the character at the logical start is
784
* visually on the right of the displayed run.
785
* The length is the number of characters in the run.<p>
786
* <code>ubidi_countRuns()</code> should be called
787
* before the runs are retrieved.
789
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
791
* @param runIndex is the number of the run in visual order, in the
792
* range <code>[0..ubidi_countRuns(pBiDi)-1]</code>.
794
* @param pLogicalStart is the first logical character index in the text.
795
* The pointer may be <code>NULL</code> if this index is not needed.
797
* @param pLength is the number of characters (at least one) in the run.
798
* The pointer may be <code>NULL</code> if this is not needed.
800
* @return the directionality of the run,
801
* <code>UBIDI_LTR==0</code> or <code>UBIDI_RTL==1</code>,
802
* never <code>UBIDI_MIXED</code>.
804
* @see ubidi_countRuns
809
* UTextOffset i, count=ubidi_countRuns(pBiDi),
810
* logicalStart, visualIndex=0, length;
811
* for(i=0; i<count; ++i) {
812
* if(UBIDI_LTR==ubidi_getVisualRun(pBiDi, i, &logicalStart, &length)) {
814
* show_char(text[logicalStart++], visualIndex++);
815
* } while(--length>0);
817
* logicalStart+=length; // logicalLimit
819
* show_char(text[--logicalStart], visualIndex++);
820
* } while(--length>0);
826
* Note that in right-to-left runs, code like this places
827
* modifier letters before base characters and second surrogates
831
U_CAPI UBiDiDirection U_EXPORT2
832
ubidi_getVisualRun(UBiDi *pBiDi, UTextOffset runIndex,
833
UTextOffset *pLogicalStart, UTextOffset *pLength);
836
* Get the visual position from a logical text position.
837
* If such a mapping is used many times on the same
838
* <code>UBiDi</code> object, then calling
839
* <code>ubidi_getLogicalMap()</code> is more efficient.<p>
841
* Note that in right-to-left runs, this mapping places
842
* modifier letters before base characters and second surrogates
845
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
847
* @param logicalIndex is the index of a character in the text.
849
* @param pErrorCode must be a valid pointer to an error code value,
850
* which must not indicate a failure before the function call.
852
* @return The visual position of this character.
854
* @see ubidi_getLogicalMap
855
* @see ubidi_getLogicalIndex
858
U_CAPI UTextOffset U_EXPORT2
859
ubidi_getVisualIndex(UBiDi *pBiDi, UTextOffset logicalIndex, UErrorCode *pErrorCode);
862
* Get the logical text position from a visual position.
863
* If such a mapping is used many times on the same
864
* <code>UBiDi</code> object, then calling
865
* <code>ubidi_getVisualMap()</code> is more efficient.<p>
867
* This is the inverse function to <code>ubidi_getVisualIndex()</code>.
869
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
871
* @param visualIndex is the visual position of a character.
873
* @param pErrorCode must be a valid pointer to an error code value,
874
* which must not indicate a failure before the function call.
876
* @return The index of this character in the text.
878
* @see ubidi_getVisualMap
879
* @see ubidi_getVisualIndex
882
U_CAPI UTextOffset U_EXPORT2
883
ubidi_getLogicalIndex(UBiDi *pBiDi, UTextOffset visualIndex, UErrorCode *pErrorCode);
886
* Get a logical-to-visual index map (array) for the characters in the UBiDi
887
* (paragraph or line) object.
889
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
891
* @param indexMap is a pointer to an array of <code>ubidi_getLength()</code>
892
* indexes which will reflect the reordering of the characters.
893
* The array does not need to be initialized.<p>
894
* The index map will result in <code>indexMap[logicalIndex]==visualIndex</code>.<p>
896
* @param pErrorCode must be a valid pointer to an error code value,
897
* which must not indicate a failure before the function call.
899
* @see ubidi_getVisualMap
900
* @see ubidi_getVisualIndex
903
U_CAPI void U_EXPORT2
904
ubidi_getLogicalMap(UBiDi *pBiDi, UTextOffset *indexMap, UErrorCode *pErrorCode);
907
* Get a visual-to-logical index map (array) for the characters in the UBiDi
908
* (paragraph or line) object.
910
* @param pBiDi is the paragraph or line <code>UBiDi</code> object.
912
* @param indexMap is a pointer to an array of <code>ubidi_getLength()</code>
913
* indexes which will reflect the reordering of the characters.
914
* The array does not need to be initialized.<p>
915
* The index map will result in <code>indexMap[visualIndex]==logicalIndex</code>.<p>
917
* @param pErrorCode must be a valid pointer to an error code value,
918
* which must not indicate a failure before the function call.
920
* @see ubidi_getLogicalMap
921
* @see ubidi_getLogicalIndex
924
U_CAPI void U_EXPORT2
925
ubidi_getVisualMap(UBiDi *pBiDi, UTextOffset *indexMap, UErrorCode *pErrorCode);
928
* This is a convenience function that does not use a UBiDi object.
929
* It is intended to be used for when an application has determined the levels
930
* of objects (character sequences) and just needs to have them reordered (L2).
931
* This is equivalent to using <code>ubidi_getLogicalMap</code> on a
932
* <code>UBiDi</code> object.
934
* @param levels is an array with <code>length</code> levels that have been determined by
937
* @param length is the number of levels in the array, or, semantically,
938
* the number of objects to be reordered.
939
* It must be <code>length>0</code>.
941
* @param indexMap is a pointer to an array of <code>length</code>
942
* indexes which will reflect the reordering of the characters.
943
* The array does not need to be initialized.<p>
944
* The index map will result in <code>indexMap[logicalIndex]==visualIndex</code>.
947
U_CAPI void U_EXPORT2
948
ubidi_reorderLogical(const UBiDiLevel *levels, UTextOffset length, UTextOffset *indexMap);
951
* This is a convenience function that does not use a UBiDi object.
952
* It is intended to be used for when an application has determined the levels
953
* of objects (character sequences) and just needs to have them reordered (L2).
954
* This is equivalent to using <code>ubidi_getVisualMap</code> on a
955
* <code>UBiDi</code> object.
957
* @param levels is an array with <code>length</code> levels that have been determined by
960
* @param length is the number of levels in the array, or, semantically,
961
* the number of objects to be reordered.
962
* It must be <code>length>0</code>.
964
* @param indexMap is a pointer to an array of <code>length</code>
965
* indexes which will reflect the reordering of the characters.
966
* The array does not need to be initialized.<p>
967
* The index map will result in <code>indexMap[visualIndex]==logicalIndex</code>.
970
U_CAPI void U_EXPORT2
971
ubidi_reorderVisual(const UBiDiLevel *levels, UTextOffset length, UTextOffset *indexMap);
974
* Invert an index map.
975
* The one-to-one index mapping of the first map is inverted and written to
978
* @param srcMap is an array with <code>length</code> indexes
979
* which define the original mapping.
981
* @param destMap is an array with <code>length</code> indexes
982
* which will be filled with the inverse mapping.
984
* @param length is the length of each array.
987
U_CAPI void U_EXPORT2
988
ubidi_invertMap(const UTextOffset *srcMap, UTextOffset *destMap, UTextOffset length);
990
/** option flags for ubidi_writeReordered() */
993
* option bit for ubidi_writeReordered():
994
* keep combining characters after their base characters in RTL runs
996
* @see ubidi_writeReordered
999
#define UBIDI_KEEP_BASE_COMBINING 1
1002
* option bit for ubidi_writeReordered():
1003
* replace characters with the "mirrored" property in RTL runs
1004
* by their mirror-image mappings
1006
* @see ubidi_writeReordered
1009
#define UBIDI_DO_MIRRORING 2
1012
* option bit for ubidi_writeReordered():
1013
* surround the run with LRMs if necessary;
1014
* this is part of the approximate "inverse BiDi" algorithm
1016
* @see ubidi_setInverse
1017
* @see ubidi_writeReordered
1020
#define UBIDI_INSERT_LRM_FOR_NUMERIC 4
1023
* option bit for ubidi_writeReordered():
1024
* remove BiDi control characters
1025
* (this does not affect UBIDI_INSERT_LRM_FOR_NUMERIC)
1027
* @see ubidi_writeReordered
1030
#define UBIDI_REMOVE_BIDI_CONTROLS 8
1033
* option bit for ubidi_writeReordered():
1034
* write the output in reverse order
1036
* <p>This has the same effect as calling <code>ubidi_writeReordered()</code>
1037
* first without this option, and then calling
1038
* <code>ubidi_writeReverse()</code> without mirroring.
1039
* Doing this in the same step is faster and avoids a temporary buffer.
1040
* An example for using this option is output to a character terminal that
1041
* is designed for RTL scripts and stores text in reverse order.</p>
1043
* @see ubidi_writeReordered
1046
#define UBIDI_OUTPUT_REVERSE 16
1049
* Take a <code>UBiDi</code> object containing the reordering
1050
* information for one paragraph or line of text as set by
1051
* <code>ubidi_setPara()</code> or <code>ubidi_setLine()</code> and
1052
* write a reordered string to the destination buffer.
1054
* This function preserves the integrity of characters with multiple
1055
* code units and (optionally) modifier letters.
1056
* Characters in RTL runs can be replaced by mirror-image characters
1057
* in the destination buffer. Note that "real" mirroring has
1058
* to be done in a rendering engine by glyph selection
1059
* and that for many "mirrored" characters there are no
1060
* Unicode characters as mirror-image equivalents.
1061
* There are also options to insert or remove BiDi control
1062
* characters; see the description of the <code>destSize</code>
1063
* and <code>options</code> parameters and of the option bit flags.
1065
* @param pBiDi A pointer to a <code>UBiDi</code> object that
1066
* is set by <code>ubidi_setPara()</code> or
1067
* <code>ubidi_setLine()</code> and contains the reordering
1068
* information for the text that it was defined for,
1069
* as well as a pointer to that text.
1070
* <p>The text was aliased (only the pointer was stored
1071
* without copying the contents) and must not have been modified
1072
* since the <code>ubidi_setPara()</code> call.</p>
1074
* @param dest A pointer to where the reordered text is to be copied.
1075
* The source text and <code>dest[destSize]</code>
1078
* @param destSize The size of the <code>dest</code> buffer,
1079
* in number of UChars.
1080
* If the <code>UBIDI_INSERT_LRM_FOR_NUMERIC</code>
1081
* option is set, then the destination length could be
1083
* <code>ubidi_getLength(pBiDi)+2*ubidi_countRuns(pBiDi)</code>.
1084
* If the <code>UBIDI_REMOVE_BIDI_CONTROLS</code> option
1085
* is set, then the destination length may be less than
1086
* <code>ubidi_getLength(pBiDi)</code>.
1087
* If none of these options is set, then the destination length
1088
* will be exactly <code>ubidi_getLength(pBiDi)</code>.
1090
* @param options A bit set of options for the reordering that control
1091
* how the reordered text is written.
1092
* The options include mirroring the characters on a code
1093
* point basis and inserting LRM characters, which is used
1094
* especially for transforming visually stored text
1095
* to logically stored text (although this is still an
1096
* imperfect implementation of an "inverse BiDi" algorithm
1097
* because it uses the "forward BiDi" algorithm at its core).
1099
* @param pErrorCode must be a valid pointer to an error code value,
1100
* which must not indicate a failure before the function call.
1102
* @return The length of the output string.
1105
U_CAPI UTextOffset U_EXPORT2
1106
ubidi_writeReordered(UBiDi *pBiDi,
1107
UChar *dest, int32_t destSize,
1109
UErrorCode *pErrorCode);
1112
* Reverse a Right-To-Left run of Unicode text.
1114
* This function preserves the integrity of characters with multiple
1115
* code units and (optionally) modifier letters.
1116
* Characters can be replaced by mirror-image characters
1117
* in the destination buffer. Note that "real" mirroring has
1118
* to be done in a rendering engine by glyph selection
1119
* and that for many "mirrored" characters there are no
1120
* Unicode characters as mirror-image equivalents.
1121
* There are also options to insert or remove BiDi control
1124
* This function is the implementation for reversing RTL runs as part
1125
* of <code>ubidi_writeReordered()</code>. For detailed descriptions
1126
* of the parameters, see there.
1127
* Since no BiDi controls are inserted here, the output string length
1128
* will never exceed <code>srcLength</code>.
1130
* @see ubidi_writeReordered
1132
* @param src A pointer to the RTL run text.
1134
* @param srcLength The length of the RTL run.
1136
* @param dest A pointer to where the reordered text is to be copied.
1137
* <code>src[srcLength]</code> and <code>dest[destSize]</code>
1140
* @param destSize The size of the <code>dest</code> buffer,
1141
* in number of UChars.
1142
* If the <code>UBIDI_REMOVE_BIDI_CONTROLS</code> option
1143
* is set, then the destination length may be less than
1144
* <code>srcLength</code>.
1145
* If this option is not set, then the destination length
1146
* will be exactly <code>srcLength</code>.
1148
* @param options A bit set of options for the reordering that control
1149
* how the reordered text is written.
1150
* See <code>ubidi_writeReordered()</code>.
1152
* @param pErrorCode must be a valid pointer to an error code value,
1153
* which must not indicate a failure before the function call.
1155
* @return The length of the output string.
1158
U_CAPI UTextOffset U_EXPORT2
1159
ubidi_writeReverse(const UChar *src, int32_t srcLength,
1160
UChar *dest, int32_t destSize,
1162
UErrorCode *pErrorCode);
1163
/*#define BIDI_SAMPLE_CODE*/