Bug 54488 - "[Mac] Non-draggable widgets in background windows should look disabled...
[mozilla-central.git] / layout / base / nsBidi.h
blobfe6b943006c6425d93dd386450a5ee53e3e516e5
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * IBM Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2000
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Simon Montagu
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef nsBidi_h__
41 #define nsBidi_h__
43 #include "nsCOMPtr.h"
44 #include "nsString.h"
45 #include "nsBidiUtils.h"
47 // Bidi reordering engine from ICU
49 * javadoc-style comments are intended to be transformed into HTML
50 * using DOC++ - see
51 * http://www.zib.de/Visual/software/doc++/index.html .
53 * The HTML documentation is created with
54 * doc++ -H nsIBidi.h
57 /**
58 * @mainpage BIDI algorithm for Mozilla (from ICU)
60 * <h2>BIDI algorithm for Mozilla</h2>
62 * This is an implementation of the Unicode Bidirectional algorithm.
63 * The algorithm is defined in the
64 * <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Technical Report 9</a>,
65 * version 5, also described in The Unicode Standard, Version 3.0 .<p>
67 * <h3>General remarks about the API:</h3>
69 * The <quote>limit</quote> of a sequence of characters is the position just after their
70 * last character, i.e., one more than that position.<p>
72 * Some of the API functions provide access to <quote>runs</quote>.
73 * Such a <quote>run</quote> is defined as a sequence of characters
74 * that are at the same embedding level
75 * after performing the BIDI algorithm.<p>
77 * @author Markus W. Scherer. Ported to Mozilla by Simon Montagu
78 * @version 1.0
81 /**
82 * nsBidiLevel is the type of the level values in this
83 * Bidi implementation.
84 * It holds an embedding level and indicates the visual direction
85 * by its bit 0 (even/odd value).<p>
87 * It can also hold non-level values for the
88 * <code>aParaLevel</code> and <code>aEmbeddingLevels</code>
89 * arguments of <code>SetPara</code>; there:
90 * <ul>
91 * <li>bit 7 of an <code>aEmbeddingLevels[]</code>
92 * value indicates whether the using application is
93 * specifying the level of a character to <i>override</i> whatever the
94 * Bidi implementation would resolve it to.</li>
95 * <li><code>aParaLevel</code> can be set to the
96 * pseudo-level values <code>NSBIDI_DEFAULT_LTR</code>
97 * and <code>NSBIDI_DEFAULT_RTL</code>.</li></ul>
99 * @see nsIBidi::SetPara
101 * <p>The related constants are not real, valid level values.
102 * <code>NSBIDI_DEFAULT_XXX</code> can be used to specify
103 * a default for the paragraph level for
104 * when the <code>SetPara</code> function
105 * shall determine it but there is no
106 * strongly typed character in the input.<p>
108 * Note that the value for <code>NSBIDI_DEFAULT_LTR</code> is even
109 * and the one for <code>NSBIDI_DEFAULT_RTL</code> is odd,
110 * just like with normal LTR and RTL level values -
111 * these special values are designed that way. Also, the implementation
112 * assumes that NSBIDI_MAX_EXPLICIT_LEVEL is odd.
114 * @see NSBIDI_DEFAULT_LTR
115 * @see NSBIDI_DEFAULT_RTL
116 * @see NSBIDI_LEVEL_OVERRIDE
117 * @see NSBIDI_MAX_EXPLICIT_LEVEL
119 typedef PRUint8 nsBidiLevel;
121 /** Paragraph level setting.
122 * If there is no strong character, then set the paragraph level to 0 (left-to-right).
124 #define NSBIDI_DEFAULT_LTR 0xfe
126 /** Paragraph level setting.
127 * If there is no strong character, then set the paragraph level to 1 (right-to-left).
129 #define NSBIDI_DEFAULT_RTL 0xff
132 * Maximum explicit embedding level.
133 * (The maximum resolved level can be up to <code>NSBIDI_MAX_EXPLICIT_LEVEL+1</code>).
136 #define NSBIDI_MAX_EXPLICIT_LEVEL 61
138 /** Bit flag for level input.
139 * Overrides directional properties.
141 #define NSBIDI_LEVEL_OVERRIDE 0x80
144 * <code>nsBidiDirection</code> values indicate the text direction.
146 enum nsBidiDirection {
147 /** All left-to-right text This is a 0 value. */
148 NSBIDI_LTR,
149 /** All right-to-left text This is a 1 value. */
150 NSBIDI_RTL,
151 /** Mixed-directional text. */
152 NSBIDI_MIXED
155 typedef enum nsBidiDirection nsBidiDirection;
157 /* miscellaneous definitions ------------------------------------------------ */
158 /** option flags for WriteReverse() */
160 * option bit for WriteReverse():
161 * keep combining characters after their base characters in RTL runs
163 * @see WriteReverse
165 #define NSBIDI_KEEP_BASE_COMBINING 1
168 * option bit for WriteReverse():
169 * replace characters with the "mirrored" property in RTL runs
170 * by their mirror-image mappings
172 * @see WriteReverse
174 #define NSBIDI_DO_MIRRORING 2
177 * option bit for WriteReverse():
178 * remove Bidi control characters
180 * @see WriteReverse
182 #define NSBIDI_REMOVE_BIDI_CONTROLS 8
184 /* helper macros for each allocated array member */
185 #define GETDIRPROPSMEMORY(length) \
186 GetMemory((void **)&mDirPropsMemory, &mDirPropsSize, \
187 mMayAllocateText, (length))
189 #define GETLEVELSMEMORY(length) \
190 GetMemory((void **)&mLevelsMemory, &mLevelsSize, \
191 mMayAllocateText, (length))
193 #define GETRUNSMEMORY(length) \
194 GetMemory((void **)&mRunsMemory, &mRunsSize, \
195 mMayAllocateRuns, (length)*sizeof(Run))
197 /* additional macros used by constructor - always allow allocation */
198 #define GETINITIALDIRPROPSMEMORY(length) \
199 GetMemory((void **)&mDirPropsMemory, &mDirPropsSize, \
200 PR_TRUE, (length))
202 #define GETINITIALLEVELSMEMORY(length) \
203 GetMemory((void **)&mLevelsMemory, &mLevelsSize, \
204 PR_TRUE, (length))
206 #define GETINITIALRUNSMEMORY(length) \
207 GetMemory((void **)&mRunsMemory, &mRunsSize, \
208 PR_TRUE, (length)*sizeof(Run))
211 * Sometimes, bit values are more appropriate
212 * to deal with directionality properties.
213 * Abbreviations in these macro names refer to names
214 * used in the Bidi algorithm.
216 typedef PRUint8 DirProp;
218 #define DIRPROP_FLAG(dir) (1UL<<(dir))
220 /* special flag for multiple runs from explicit embedding codes */
221 #define DIRPROP_FLAG_MULTI_RUNS (1UL<<31)
223 /* are there any characters that are LTR or RTL? */
224 #define MASK_LTR (DIRPROP_FLAG(L)|DIRPROP_FLAG(EN)|DIRPROP_FLAG(AN)|DIRPROP_FLAG(LRE)|DIRPROP_FLAG(LRO))
225 #define MASK_RTL (DIRPROP_FLAG(R)|DIRPROP_FLAG(AL)|DIRPROP_FLAG(RLE)|DIRPROP_FLAG(RLO))
227 /* explicit embedding codes */
228 #define MASK_LRX (DIRPROP_FLAG(LRE)|DIRPROP_FLAG(LRO))
229 #define MASK_RLX (DIRPROP_FLAG(RLE)|DIRPROP_FLAG(RLO))
230 #define MASK_OVERRIDE (DIRPROP_FLAG(LRO)|DIRPROP_FLAG(RLO))
232 #define MASK_EXPLICIT (MASK_LRX|MASK_RLX|DIRPROP_FLAG(PDF))
233 #define MASK_BN_EXPLICIT (DIRPROP_FLAG(BN)|MASK_EXPLICIT)
235 /* paragraph and segment separators */
236 #define MASK_B_S (DIRPROP_FLAG(B)|DIRPROP_FLAG(S))
238 /* all types that are counted as White Space or Neutral in some steps */
239 #define MASK_WS (MASK_B_S|DIRPROP_FLAG(WS)|MASK_BN_EXPLICIT)
240 #define MASK_N (DIRPROP_FLAG(O_N)|MASK_WS)
242 /* all types that are included in a sequence of European Terminators for (W5) */
243 #define MASK_ET_NSM_BN (DIRPROP_FLAG(ET)|DIRPROP_FLAG(NSM)|MASK_BN_EXPLICIT)
245 /* types that are neutrals or could becomes neutrals in (Wn) */
246 #define MASK_POSSIBLE_N (DIRPROP_FLAG(CS)|DIRPROP_FLAG(ES)|DIRPROP_FLAG(ET)|MASK_N)
249 * These types may be changed to "e",
250 * the embedding type (L or R) of the run,
251 * in the Bidi algorithm (N2)
253 #define MASK_EMBEDDING (DIRPROP_FLAG(NSM)|MASK_POSSIBLE_N)
255 /* the dirProp's L and R are defined to 0 and 1 values in nsCharType */
256 #define GET_LR_FROM_LEVEL(level) ((DirProp)((level)&1))
258 #define IS_DEFAULT_LEVEL(level) (((level)&0xfe)==0xfe)
260 /* handle surrogate pairs --------------------------------------------------- */
262 #define IS_FIRST_SURROGATE(uchar) (((uchar)&0xfc00)==0xd800)
263 #define IS_SECOND_SURROGATE(uchar) (((uchar)&0xfc00)==0xdc00)
265 /* get the UTF-32 value directly from the surrogate pseudo-characters */
266 #define SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
267 #define GET_UTF_32(first, second) (((first)<<10UL)+(second)-SURROGATE_OFFSET)
270 #define UTF_ERROR_VALUE 0xffff
271 /* definitions with forward iteration --------------------------------------- */
274 * all the macros that go forward assume that
275 * the initial offset is 0<=i<length;
276 * they update the offset
279 /* fast versions, no error-checking */
281 #define UTF16_APPEND_CHAR_UNSAFE(s, i, c){ \
282 if((PRUint32)(c)<=0xffff) { \
283 (s)[(i)++]=(PRUnichar)(c); \
284 } else { \
285 (s)[(i)++]=(PRUnichar)((c)>>10)+0xd7c0; \
286 (s)[(i)++]=(PRUnichar)(c)&0x3ff|0xdc00; \
290 /* safe versions with error-checking and optional regularity-checking */
292 #define UTF16_APPEND_CHAR_SAFE(s, i, length, c) { \
293 if((PRUInt32)(c)<=0xffff) { \
294 (s)[(i)++]=(PRUnichar)(c); \
295 } else if((PRUInt32)(c)<=0x10ffff) { \
296 if((i)+1<(length)) { \
297 (s)[(i)++]=(PRUnichar)((c)>>10)+0xd7c0; \
298 (s)[(i)++]=(PRUnichar)(c)&0x3ff|0xdc00; \
299 } else /* not enough space */ { \
300 (s)[(i)++]=UTF_ERROR_VALUE; \
302 } else /* c>0x10ffff, write error value */ { \
303 (s)[(i)++]=UTF_ERROR_VALUE; \
307 /* definitions with backward iteration -------------------------------------- */
310 * all the macros that go backward assume that
311 * the valid buffer range starts at offset 0
312 * and that the initial offset is 0<i<=length;
313 * they update the offset
316 /* fast versions, no error-checking */
319 * Get a single code point from an offset that points behind the last
320 * of the code units that belong to that code point.
321 * Assume 0<=i<length.
323 #define UTF16_PREV_CHAR_UNSAFE(s, i, c) { \
324 (c)=(s)[--(i)]; \
325 if(IS_SECOND_SURROGATE(c)) { \
326 (c)=GET_UTF_32((s)[--(i)], (c)); \
330 #define UTF16_BACK_1_UNSAFE(s, i) { \
331 if(IS_SECOND_SURROGATE((s)[--(i)])) { \
332 --(i); \
336 #define UTF16_BACK_N_UNSAFE(s, i, n) { \
337 PRInt32 __N=(n); \
338 while(__N>0) { \
339 UTF16_BACK_1_UNSAFE(s, i); \
340 --__N; \
344 /* safe versions with error-checking and optional regularity-checking */
346 #define UTF16_PREV_CHAR_SAFE(s, start, i, c, strict) { \
347 (c)=(s)[--(i)]; \
348 if(IS_SECOND_SURROGATE(c)) { \
349 PRUnichar __c2; \
350 if((i)>(start) && IS_FIRST_SURROGATE(__c2=(s)[(i)-1])) { \
351 --(i); \
352 (c)=GET_UTF_32(__c2, (c)); \
353 /* strict: ((c)&0xfffe)==0xfffe is caught by UTF_IS_ERROR() */ \
354 } else if(strict) {\
355 /* unmatched second surrogate */ \
356 (c)=UTF_ERROR_VALUE; \
358 } else if(strict && IS_FIRST_SURROGATE(c)) { \
359 /* unmatched first surrogate */ \
360 (c)=UTF_ERROR_VALUE; \
361 /* else strict: (c)==0xfffe is caught by UTF_IS_ERROR() */ \
365 #define UTF16_BACK_1_SAFE(s, start, i) { \
366 if(IS_SECOND_SURROGATE((s)[--(i)]) && (i)>(start) && IS_FIRST_SURROGATE((s)[(i)-1])) { \
367 --(i); \
371 #define UTF16_BACK_N_SAFE(s, start, i, n) { \
372 PRInt32 __N=(n); \
373 while(__N>0 && (i)>(start)) { \
374 UTF16_BACK_1_SAFE(s, start, i); \
375 --__N; \
379 #define UTF_PREV_CHAR_UNSAFE(s, i, c) UTF16_PREV_CHAR_UNSAFE(s, i, c)
380 #define UTF_PREV_CHAR_SAFE(s, start, i, c, strict) UTF16_PREV_CHAR_SAFE(s, start, i, c, strict)
381 #define UTF_BACK_1_UNSAFE(s, i) UTF16_BACK_1_UNSAFE(s, i)
382 #define UTF_BACK_1_SAFE(s, start, i) UTF16_BACK_1_SAFE(s, start, i)
383 #define UTF_BACK_N_UNSAFE(s, i, n) UTF16_BACK_N_UNSAFE(s, i, n)
384 #define UTF_BACK_N_SAFE(s, start, i, n) UTF16_BACK_N_SAFE(s, start, i, n)
385 #define UTF_APPEND_CHAR_UNSAFE(s, i, c) UTF16_APPEND_CHAR_UNSAFE(s, i, c)
386 #define UTF_APPEND_CHAR_SAFE(s, i, length, c) UTF16_APPEND_CHAR_SAFE(s, i, length, c)
388 #define UTF_PREV_CHAR(s, start, i, c) UTF_PREV_CHAR_SAFE(s, start, i, c, PR_FALSE)
389 #define UTF_BACK_1(s, start, i) UTF_BACK_1_SAFE(s, start, i)
390 #define UTF_BACK_N(s, start, i, n) UTF_BACK_N_SAFE(s, start, i, n)
391 #define UTF_APPEND_CHAR(s, i, length, c) UTF_APPEND_CHAR_SAFE(s, i, length, c)
393 /* Run structure for reordering --------------------------------------------- */
395 typedef struct Run {
396 PRInt32 logicalStart, /* first character of the run; b31 indicates even/odd level */
397 visualLimit; /* last visual position of the run +1 */
398 } Run;
400 /* in a Run, logicalStart will get this bit set if the run level is odd */
401 #define INDEX_ODD_BIT (1UL<<31)
403 #define MAKE_INDEX_ODD_PAIR(index, level) (index|((PRUint32)level<<31))
404 #define ADD_ODD_BIT_FROM_LEVEL(x, level) ((x)|=((PRUint32)level<<31))
405 #define REMOVE_ODD_BIT(x) ((x)&=~INDEX_ODD_BIT)
407 #define GET_INDEX(x) (x&~INDEX_ODD_BIT)
408 #define GET_ODD_BIT(x) ((PRUint32)x>>31)
409 #define IS_ODD_RUN(x) ((x&INDEX_ODD_BIT)!=0)
410 #define IS_EVEN_RUN(x) ((x&INDEX_ODD_BIT)==0)
412 typedef PRUint32 Flags;
415 * This class holds information about a paragraph of text
416 * with Bidi-algorithm-related details, or about one line of
417 * such a paragraph.<p>
418 * Reordering can be done on a line, or on a paragraph which is
419 * then interpreted as one single line.<p>
421 * On construction, the class is initially empty. It is assigned
422 * the Bidi properties of a paragraph by <code>SetPara</code>
423 * or the Bidi properties of a line of a paragraph by
424 * <code>SetLine</code>.<p>
425 * A Bidi class can be reused for as long as it is not deallocated
426 * by calling its destructor.<p>
427 * <code>SetPara</code> will allocate additional memory for
428 * internal structures as necessary.
430 class nsBidi
432 public:
433 /** @brief Default constructor.
435 * The nsBidi object is initially empty. It is assigned
436 * the Bidi properties of a paragraph by <code>SetPara()</code>
437 * or the Bidi properties of a line of a paragraph by
438 * <code>GetLine()</code>.<p>
439 * This object can be reused for as long as it is not destroyed.<p>
440 * <code>SetPara()</code> will allocate additional memory for
441 * internal structures as necessary.
444 nsBidi();
446 /** @brief Preallocating constructor
447 * Allocate an <code>nsBidi</code>
448 * object with preallocated memory for internal structures. This
449 * constructor provides an <code>nsBidi</code> object like
450 * the default constructor, but it also
451 * preallocates memory for internal structures according to the sizings
452 * supplied by the caller.<p> Subsequent functions will not allocate
453 * any more memory, and are thus guaranteed not to fail because of lack
454 * of memory.<p> The preallocation can be limited to some of the
455 * internal memory by setting some values to 0 here. That means that
456 * if, e.g., <code>aMaxRunCount</code> cannot be reasonably
457 * predetermined and should not be set to <code>aMaxLength</code> (the
458 * only failproof value) to avoid wasting memory, then
459 * <code>aMaxRunCount</code> could be set to 0 here and the internal
460 * structures that are associated with it will be allocated on demand,
461 * just like with the default constructor.
463 * If sufficient memory could not be allocated, no exception is thrown.
464 * Test whether mDirPropsSize == aMaxLength and/or mRunsSize == aMaxRunCount.
466 * @param aMaxLength is the maximum paragraph or line length that internal memory
467 * will be preallocated for. An attempt to associate this object with a
468 * longer text will fail, unless this value is 0, which leaves the allocation
469 * up to the implementation.
471 * @param aMaxRunCount is the maximum anticipated number of same-level runs
472 * that internal memory will be preallocated for. An attempt to access
473 * visual runs on an object that was not preallocated for as many runs
474 * as the text was actually resolved to will fail,
475 * unless this value is 0, which leaves the allocation up to the implementation.<p>
476 * The number of runs depends on the actual text and maybe anywhere between
477 * 1 and <code>aMaxLength</code>. It is typically small.<p>
479 nsBidi(PRUint32 aMaxLength, PRUint32 aMaxRunCount);
481 /** @brief Destructor. */
482 virtual ~nsBidi();
486 * Perform the Unicode Bidi algorithm. It is defined in the
487 * <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Technical Report 9</a>,
488 * version 5,
489 * also described in The Unicode Standard, Version 3.0 .<p>
491 * This function takes a single plain text paragraph with or without
492 * externally specified embedding levels from <quote>styled</quote> text
493 * and computes the left-right-directionality of each character.<p>
495 * If the entire paragraph consists of text of only one direction, then
496 * the function may not perform all the steps described by the algorithm,
497 * i.e., some levels may not be the same as if all steps were performed.
498 * This is not relevant for unidirectional text.<br>
499 * For example, in pure LTR text with numbers the numbers would get
500 * a resolved level of 2 higher than the surrounding text according to
501 * the algorithm. This implementation may set all resolved levels to
502 * the same value in such a case.<p>
504 * The text must be externally split into separate paragraphs (rule P1).
505 * Paragraph separators (B) should appear at most at the very end.
507 * @param aText is a pointer to the single-paragraph text that the
508 * Bidi algorithm will be performed on
509 * (step (P1) of the algorithm is performed externally).
510 * <strong>The text must be (at least) <code>aLength</code> long.</strong>
512 * @param aLength is the length of the text; if <code>aLength==-1</code> then
513 * the text must be zero-terminated.
515 * @param aParaLevel specifies the default level for the paragraph;
516 * it is typically 0 (LTR) or 1 (RTL).
517 * If the function shall determine the paragraph level from the text,
518 * then <code>aParaLevel</code> can be set to
519 * either <code>NSBIDI_DEFAULT_LTR</code>
520 * or <code>NSBIDI_DEFAULT_RTL</code>;
521 * if there is no strongly typed character, then
522 * the desired default is used (0 for LTR or 1 for RTL).
523 * Any other value between 0 and <code>NSBIDI_MAX_EXPLICIT_LEVEL</code> is also valid,
524 * with odd levels indicating RTL.
526 * @param aEmbeddingLevels (in) may be used to preset the embedding and override levels,
527 * ignoring characters like LRE and PDF in the text.
528 * A level overrides the directional property of its corresponding
529 * (same index) character if the level has the
530 * <code>NSBIDI_LEVEL_OVERRIDE</code> bit set.<p>
531 * Except for that bit, it must be
532 * <code>aParaLevel<=aEmbeddingLevels[]<=NSBIDI_MAX_EXPLICIT_LEVEL</code>.<p>
533 * <strong>Caution: </strong>A copy of this pointer, not of the levels,
534 * will be stored in the <code>nsBidi</code> object;
535 * the <code>aEmbeddingLevels</code> array must not be
536 * deallocated before the <code>nsBidi</code> object is destroyed or reused,
537 * and the <code>aEmbeddingLevels</code>
538 * should not be modified to avoid unexpected results on subsequent Bidi operations.
539 * However, the <code>SetPara</code> and
540 * <code>SetLine</code> functions may modify some or all of the levels.<p>
541 * After the <code>nsBidi</code> object is reused or destroyed, the caller
542 * must take care of the deallocation of the <code>aEmbeddingLevels</code> array.<p>
543 * <strong>The <code>aEmbeddingLevels</code> array must be
544 * at least <code>aLength</code> long.</strong>
546 nsresult SetPara(const PRUnichar *aText, PRInt32 aLength, nsBidiLevel aParaLevel, nsBidiLevel *aEmbeddingLevels);
548 #ifdef FULL_BIDI_ENGINE
550 * <code>SetLine</code> sets an <code>nsBidi</code> to
551 * contain the reordering information, especially the resolved levels,
552 * for all the characters in a line of text. This line of text is
553 * specified by referring to an <code>nsBidi</code> object representing
554 * this information for a paragraph of text, and by specifying
555 * a range of indexes in this paragraph.<p>
556 * In the new line object, the indexes will range from 0 to <code>aLimit-aStart</code>.<p>
558 * This is used after calling <code>SetPara</code>
559 * for a paragraph, and after line-breaking on that paragraph.
560 * It is not necessary if the paragraph is treated as a single line.<p>
562 * After line-breaking, rules (L1) and (L2) for the treatment of
563 * trailing WS and for reordering are performed on
564 * an <code>nsBidi</code> object that represents a line.<p>
566 * <strong>Important:</strong> the line <code>nsBidi</code> object shares data with
567 * <code>aParaBidi</code>.
568 * You must destroy or reuse this object before <code>aParaBidi</code>.
569 * In other words, you must destroy or reuse the <code>nsBidi</code> object for a line
570 * before the object for its parent paragraph.
572 * @param aParaBidi is the parent paragraph object.
574 * @param aStart is the line's first index into the paragraph text.
576 * @param aLimit is just behind the line's last index into the paragraph text
577 * (its last index +1).<br>
578 * It must be <code>0<=aStart<=aLimit<=</code>paragraph length.
580 * @see SetPara
582 nsresult SetLine(nsIBidi* aParaBidi, PRInt32 aStart, PRInt32 aLimit);
585 * Get the directionality of the text.
587 * @param aDirection receives a <code>NSBIDI_XXX</code> value that indicates if the entire text
588 * represented by this object is unidirectional,
589 * and which direction, or if it is mixed-directional.
591 * @see nsBidiDirection
593 nsresult GetDirection(nsBidiDirection* aDirection);
596 * Get the length of the text.
598 * @param aLength receives the length of the text that the nsBidi object was created for.
600 nsresult GetLength(PRInt32* aLength);
603 * Get the paragraph level of the text.
605 * @param aParaLevel receives a <code>NSBIDI_XXX</code> value indicating the paragraph level
607 * @see nsBidiLevel
609 nsresult GetParaLevel(nsBidiLevel* aParaLevel);
612 * Get the level for one character.
614 * @param aCharIndex the index of a character.
616 * @param aLevel receives the level for the character at aCharIndex.
618 * @see nsBidiLevel
620 nsresult GetLevelAt(PRInt32 aCharIndex, nsBidiLevel* aLevel);
623 * Get an array of levels for each character.<p>
625 * Note that this function may allocate memory under some
626 * circumstances, unlike <code>GetLevelAt</code>.
628 * @param aLevels receives a pointer to the levels array for the text,
629 * or <code>NULL</code> if an error occurs.
631 * @see nsBidiLevel
633 nsresult GetLevels(nsBidiLevel** aLevels);
634 #endif // FULL_BIDI_ENGINE
636 * Get the bidirectional type for one character.
638 * @param aCharIndex the index of a character.
640 * @param aType receives the bidirectional type of the character at aCharIndex.
642 nsresult GetCharTypeAt(PRInt32 aCharIndex, nsCharType* aType);
645 * Get a logical run.
646 * This function returns information about a run and is used
647 * to retrieve runs in logical order.<p>
648 * This is especially useful for line-breaking on a paragraph.
650 * @param aLogicalStart is the first character of the run.
652 * @param aLogicalLimit will receive the limit of the run.
653 * The l-value that you point to here may be the
654 * same expression (variable) as the one for
655 * <code>aLogicalStart</code>.
656 * This pointer can be <code>NULL</code> if this
657 * value is not necessary.
659 * @param aLevel will receive the level of the run.
660 * This pointer can be <code>NULL</code> if this
661 * value is not necessary.
663 nsresult GetLogicalRun(PRInt32 aLogicalStart, PRInt32* aLogicalLimit, nsBidiLevel* aLevel);
666 * Get the number of runs.
667 * This function may invoke the actual reordering on the
668 * <code>nsBidi</code> object, after <code>SetPara</code>
669 * may have resolved only the levels of the text. Therefore,
670 * <code>CountRuns</code> may have to allocate memory,
671 * and may fail doing so.
673 * @param aRunCount will receive the number of runs.
675 nsresult CountRuns(PRInt32* aRunCount);
678 * Get one run's logical start, length, and directionality,
679 * which can be 0 for LTR or 1 for RTL.
680 * In an RTL run, the character at the logical start is
681 * visually on the right of the displayed run.
682 * The length is the number of characters in the run.<p>
683 * <code>CountRuns</code> should be called
684 * before the runs are retrieved.
686 * @param aRunIndex is the number of the run in visual order, in the
687 * range <code>[0..CountRuns-1]</code>.
689 * @param aLogicalStart is the first logical character index in the text.
690 * The pointer may be <code>NULL</code> if this index is not needed.
692 * @param aLength is the number of characters (at least one) in the run.
693 * The pointer may be <code>NULL</code> if this is not needed.
695 * @param aDirection will receive the directionality of the run,
696 * <code>NSBIDI_LTR==0</code> or <code>NSBIDI_RTL==1</code>,
697 * never <code>NSBIDI_MIXED</code>.
699 * @see CountRuns<p>
701 * Example:
702 * @code
703 * PRInt32 i, count, logicalStart, visualIndex=0, length;
704 * nsBidiDirection dir;
705 * pBidi->CountRuns(&count);
706 * for(i=0; i<count; ++i) {
707 * pBidi->GetVisualRun(i, &logicalStart, &length, &dir);
708 * if(NSBIDI_LTR==dir) {
709 * do { // LTR
710 * show_char(text[logicalStart++], visualIndex++);
711 * } while(--length>0);
712 * } else {
713 * logicalStart+=length; // logicalLimit
714 * do { // RTL
715 * show_char(text[--logicalStart], visualIndex++);
716 * } while(--length>0);
719 * @endcode
721 * Note that in right-to-left runs, code like this places
722 * modifier letters before base characters and second surrogates
723 * before first ones.
725 nsresult GetVisualRun(PRInt32 aRunIndex, PRInt32* aLogicalStart, PRInt32* aLength, nsBidiDirection* aDirection);
727 #ifdef FULL_BIDI_ENGINE
729 * Get the visual position from a logical text position.
730 * If such a mapping is used many times on the same
731 * <code>nsBidi</code> object, then calling
732 * <code>GetLogicalMap</code> is more efficient.<p>
734 * Note that in right-to-left runs, this mapping places
735 * modifier letters before base characters and second surrogates
736 * before first ones.
738 * @param aLogicalIndex is the index of a character in the text.
740 * @param aVisualIndex will receive the visual position of this character.
742 * @see GetLogicalMap
743 * @see GetLogicalIndex
745 nsresult GetVisualIndex(PRInt32 aLogicalIndex, PRInt32* aVisualIndex);
748 * Get the logical text position from a visual position.
749 * If such a mapping is used many times on the same
750 * <code>nsBidi</code> object, then calling
751 * <code>GetVisualMap</code> is more efficient.<p>
753 * This is the inverse function to <code>GetVisualIndex</code>.
755 * @param aVisualIndex is the visual position of a character.
757 * @param aLogicalIndex will receive the index of this character in the text.
759 * @see GetVisualMap
760 * @see GetVisualIndex
762 nsresult GetLogicalIndex(PRInt32 aVisualIndex, PRInt32* aLogicalIndex);
765 * Get a logical-to-visual index map (array) for the characters in the nsBidi
766 * (paragraph or line) object.
768 * @param aIndexMap is a pointer to an array of <code>GetLength</code>
769 * indexes which will reflect the reordering of the characters.
770 * The array does not need to be initialized.<p>
771 * The index map will result in <code>aIndexMap[aLogicalIndex]==aVisualIndex</code>.<p>
773 * @see GetVisualMap
774 * @see GetVisualIndex
776 nsresult GetLogicalMap(PRInt32 *aIndexMap);
779 * Get a visual-to-logical index map (array) for the characters in the nsBidi
780 * (paragraph or line) object.
782 * @param aIndexMap is a pointer to an array of <code>GetLength</code>
783 * indexes which will reflect the reordering of the characters.
784 * The array does not need to be initialized.<p>
785 * The index map will result in <code>aIndexMap[aVisualIndex]==aLogicalIndex</code>.<p>
787 * @see GetLogicalMap
788 * @see GetLogicalIndex
790 nsresult GetVisualMap(PRInt32 *aIndexMap);
793 * This is a convenience function that does not use a nsBidi object.
794 * It is intended to be used for when an application has determined the levels
795 * of objects (character sequences) and just needs to have them reordered (L2).
796 * This is equivalent to using <code>GetLogicalMap</code> on a
797 * <code>nsBidi</code> object.
799 * @param aLevels is an array with <code>aLength</code> levels that have been determined by
800 * the application.
802 * @param aLength is the number of levels in the array, or, semantically,
803 * the number of objects to be reordered.
804 * It must be <code>aLength>0</code>.
806 * @param aIndexMap is a pointer to an array of <code>aLength</code>
807 * indexes which will reflect the reordering of the characters.
808 * The array does not need to be initialized.<p>
809 * The index map will result in <code>aIndexMap[aLogicalIndex]==aVisualIndex</code>.
811 nsresult ReorderLogical(const nsBidiLevel *aLevels, PRInt32 aLength, PRInt32 *aIndexMap);
812 #endif // FULL_BIDI_ENGINE
814 * This is a convenience function that does not use a nsBidi object.
815 * It is intended to be used for when an application has determined the levels
816 * of objects (character sequences) and just needs to have them reordered (L2).
817 * This is equivalent to using <code>GetVisualMap</code> on a
818 * <code>nsBidi</code> object.
820 * @param aLevels is an array with <code>aLength</code> levels that have been determined by
821 * the application.
823 * @param aLength is the number of levels in the array, or, semantically,
824 * the number of objects to be reordered.
825 * It must be <code>aLength>0</code>.
827 * @param aIndexMap is a pointer to an array of <code>aLength</code>
828 * indexes which will reflect the reordering of the characters.
829 * The array does not need to be initialized.<p>
830 * The index map will result in <code>aIndexMap[aVisualIndex]==aLogicalIndex</code>.
832 nsresult ReorderVisual(const nsBidiLevel *aLevels, PRInt32 aLength, PRInt32 *aIndexMap);
834 #ifdef FULL_BIDI_ENGINE
836 * Invert an index map.
837 * The one-to-one index mapping of the first map is inverted and written to
838 * the second one.
840 * @param aSrcMap is an array with <code>aLength</code> indexes
841 * which define the original mapping.
843 * @param aDestMap is an array with <code>aLength</code> indexes
844 * which will be filled with the inverse mapping.
846 * @param aLength is the length of each array.
848 nsresult InvertMap(const PRInt32 *aSrcMap, PRInt32 *aDestMap, PRInt32 aLength);
849 #endif // FULL_BIDI_ENGINE
851 * Reverse a Right-To-Left run of Unicode text.
853 * This function preserves the integrity of characters with multiple
854 * code units and (optionally) modifier letters.
855 * Characters can be replaced by mirror-image characters
856 * in the destination buffer. Note that "real" mirroring has
857 * to be done in a rendering engine by glyph selection
858 * and that for many "mirrored" characters there are no
859 * Unicode characters as mirror-image equivalents.
860 * There are also options to insert or remove Bidi control
861 * characters; see the description of the <code>aDestSize</code>
862 * and <code>aOptions</code> parameters and of the option bit flags.
864 * Since no Bidi controls are inserted here, this function will never
865 * write more than <code>aSrcLength</code> characters to <code>aDest</code>.
867 * @param aSrc A pointer to the RTL run text.
869 * @param aSrcLength The length of the RTL run.
870 * If the <code>NSBIDI_REMOVE_BIDI_CONTROLS</code> option
871 * is set, then the destination length may be less than
872 * <code>aSrcLength</code>.
873 * If this option is not set, then the destination length
874 * will be exactly <code>aSrcLength</code>.
876 * @param aDest A pointer to where the reordered text is to be copied.
877 * <code>aSrc[aSrcLength]</code> and <code>aDest[aSrcLength]</code>
878 * must not overlap.
880 * @param aOptions A bit set of options for the reordering that control
881 * how the reordered text is written.
883 * @param aDestSize will receive the number of characters that were written to <code>aDest</code>.
885 nsresult WriteReverse(const PRUnichar *aSrc, PRInt32 aSrcLength, PRUnichar *aDest, PRUint16 aOptions, PRInt32 *aDestSize);
887 protected:
888 /** length of the current text */
889 PRInt32 mLength;
891 /** memory sizes in bytes */
892 PRSize mDirPropsSize, mLevelsSize, mRunsSize;
894 /** allocated memory */
895 DirProp* mDirPropsMemory;
896 nsBidiLevel* mLevelsMemory;
897 Run* mRunsMemory;
899 /** indicators for whether memory may be allocated after construction */
900 PRBool mMayAllocateText, mMayAllocateRuns;
902 const DirProp* mDirProps;
903 nsBidiLevel* mLevels;
905 /** the paragraph level */
906 nsBidiLevel mParaLevel;
908 /** flags is a bit set for which directional properties are in the text */
909 Flags mFlags;
911 /** the overall paragraph or line directionality - see nsBidiDirection */
912 nsBidiDirection mDirection;
914 /** characters after trailingWSStart are WS and are */
915 /* implicitly at the paraLevel (rule (L1)) - levels may not reflect that */
916 PRInt32 mTrailingWSStart;
918 /** fields for line reordering */
919 PRInt32 mRunCount; /* ==-1: runs not set up yet */
920 Run* mRuns;
922 /** for non-mixed text, we only need a tiny array of runs (no malloc()) */
923 Run mSimpleRuns[1];
925 private:
927 void Init();
929 PRBool GetMemory(void **aMemory, PRSize* aSize, PRBool aMayAllocate, PRSize aSizeNeeded);
931 void Free();
933 void GetDirProps(const PRUnichar *aText);
935 nsBidiDirection ResolveExplicitLevels();
937 nsresult CheckExplicitLevels(nsBidiDirection *aDirection);
939 nsBidiDirection DirectionFromFlags(Flags aFlags);
941 void ResolveImplicitLevels(PRInt32 aStart, PRInt32 aLimit, DirProp aSOR, DirProp aEOR);
943 void AdjustWSLevels();
945 void SetTrailingWSStart();
947 PRBool GetRuns();
949 void GetSingleRun(nsBidiLevel aLevel);
951 void ReorderLine(nsBidiLevel aMinLevel, nsBidiLevel aMaxLevel);
953 PRBool PrepareReorder(const nsBidiLevel *aLevels, PRInt32 aLength, PRInt32 *aIndexMap, nsBidiLevel *aMinLevel, nsBidiLevel *aMaxLevel);
955 PRInt32 doWriteReverse(const PRUnichar *src, PRInt32 srcLength,
956 PRUnichar *dest, PRUint16 options);
960 #endif // _nsBidi_h_