OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Bidi stands for Bi-directional text. | 6 * Bidi stands for Bi-directional text. |
7 * According to http://en.wikipedia.org/wiki/Bi-directional_text: | 7 * According to http://en.wikipedia.org/wiki/Bi-directional_text: |
8 * Bi-directional text is text containing text in both text directionalities, | 8 * Bi-directional text is text containing text in both text directionalities, |
9 * both right-to-left (RTL) and left-to-right (LTR). It generally involves text | 9 * both right-to-left (RTL) and left-to-right (LTR). It generally involves text |
10 * containing different types of alphabets, but may also refer to boustrophedon, | 10 * containing different types of alphabets, but may also refer to boustrophedon, |
11 * which is changing text directionality in each row. | 11 * which is changing text directionality in each row. |
12 * | 12 * |
13 * This file provides some utility classes for determining directionality of | 13 * This file provides some utility classes for determining directionality of |
14 * text, switching CSS layout from LTR to RTL, and other normalizing utilities | 14 * text, switching CSS layout from LTR to RTL, and other normalizing utilities |
15 * needed when switching between RTL and LTR formatting. | 15 * needed when switching between RTL and LTR formatting. |
16 * | 16 * |
17 * It defines the TextDirection class which is used to represent directionality | 17 * It defines the TextDirection class which is used to represent directionality |
18 * of text, | 18 * of text, |
19 * In most cases, it is preferable to use bidi_formatter.dart, which provides | 19 * In most cases, it is preferable to use bidi_formatter.dart, which provides |
20 * bidi functionality in the given directional context, instead of using | 20 * bidi functionality in the given directional context, instead of using |
21 * bidi_utils.dart directly. | 21 * bidi_utils.dart directly. |
22 */ | 22 */ |
23 class TextDirection { | 23 class TextDirection { |
24 static final LTR = const TextDirection._('LTR', 'ltr'); | 24 static const LTR = const TextDirection._('LTR', 'ltr'); |
25 static final RTL = const TextDirection._('RTL', 'rtl'); | 25 static const RTL = const TextDirection._('RTL', 'rtl'); |
26 // If the directionality of the text cannot be determined and we are not using | 26 // If the directionality of the text cannot be determined and we are not using |
27 // the context direction (or if the context direction is unknown), then the | 27 // the context direction (or if the context direction is unknown), then the |
28 // text falls back on the more common ltr direction. | 28 // text falls back on the more common ltr direction. |
29 static final UNKNOWN = const TextDirection._('UNKNOWN', 'ltr'); | 29 static const UNKNOWN = const TextDirection._('UNKNOWN', 'ltr'); |
30 | 30 |
31 /** | 31 /** |
32 * Textual representation of the directionality constant. One of | 32 * Textual representation of the directionality constant. One of |
33 * 'LTR', 'RTL', or 'UNKNOWN'. | 33 * 'LTR', 'RTL', or 'UNKNOWN'. |
34 */ | 34 */ |
35 final String value; | 35 final String value; |
36 | 36 |
37 /** Textual representation of the directionality when used in span tag. */ | 37 /** Textual representation of the directionality when used in span tag. */ |
38 final String spanText; | 38 final String spanText; |
39 | 39 |
40 const TextDirection._(this.value, this.spanText); | 40 const TextDirection._(this.value, this.spanText); |
41 | 41 |
42 /** | 42 /** |
43 * Returns true if [otherDirection] is known to be different from this | 43 * Returns true if [otherDirection] is known to be different from this |
44 * direction. | 44 * direction. |
45 */ | 45 */ |
46 bool isDirectionChange(TextDirection otherDirection) { | 46 bool isDirectionChange(TextDirection otherDirection) { |
47 return otherDirection != TextDirection.UNKNOWN && this != otherDirection; | 47 return otherDirection != TextDirection.UNKNOWN && this != otherDirection; |
48 } | 48 } |
49 } | 49 } |
50 | 50 |
51 /** Unicode "Left-To-Right Embedding" (LRE) character. */ | 51 /** Unicode "Left-To-Right Embedding" (LRE) character. */ |
52 final LRE = '\u202A'; | 52 const LRE = '\u202A'; |
53 | 53 |
54 /** Unicode "Right-To-Left Embedding" (RLE) character. */ | 54 /** Unicode "Right-To-Left Embedding" (RLE) character. */ |
55 final RLE = '\u202B'; | 55 const RLE = '\u202B'; |
56 | 56 |
57 /** Unicode "Pop Directional Formatting" (PDF) character. */ | 57 /** Unicode "Pop Directional Formatting" (PDF) character. */ |
58 final PDF = '\u202C'; | 58 const PDF = '\u202C'; |
59 | 59 |
60 /** Unicode "Left-To-Right Mark" (LRM) character. */ | 60 /** Unicode "Left-To-Right Mark" (LRM) character. */ |
61 final LRM = '\u200E'; | 61 const LRM = '\u200E'; |
62 | 62 |
63 /** Unicode "Right-To-Left Mark" (RLM) character. */ | 63 /** Unicode "Right-To-Left Mark" (RLM) character. */ |
64 final RLM = '\u200F'; | 64 const RLM = '\u200F'; |
65 | 65 |
66 /** Constant to define the threshold of RTL directionality. */ | 66 /** Constant to define the threshold of RTL directionality. */ |
67 num _RTL_DETECTION_THRESHOLD = 0.40; | 67 num _RTL_DETECTION_THRESHOLD = 0.40; |
68 | 68 |
69 /** | 69 /** |
70 * Practical patterns to identify strong LTR and RTL characters, respectively. | 70 * Practical patterns to identify strong LTR and RTL characters, respectively. |
71 * These patterns are not completely correct according to the Unicode | 71 * These patterns are not completely correct according to the Unicode |
72 * standard. They are simplified for performance and small code size. | 72 * standard. They are simplified for performance and small code size. |
73 */ | 73 */ |
74 final String _LTR_CHARS = | 74 const String _LTR_CHARS = |
75 @'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590' | 75 @'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590' |
76 @'\u0800-\u1FFF\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF'; | 76 @'\u0800-\u1FFF\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF'; |
77 final String _RTL_CHARS = @'\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC'; | 77 const String _RTL_CHARS = @'\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC'; |
78 | 78 |
79 /** | 79 /** |
80 * Returns the input [text] with spaces instead of HTML tags or HTML escapes, | 80 * Returns the input [text] with spaces instead of HTML tags or HTML escapes, |
81 * which is helpful for text directionality estimation. | 81 * which is helpful for text directionality estimation. |
82 * Note: This function should not be used in other contexts. | 82 * Note: This function should not be used in other contexts. |
83 * It does not deal well with many things: comments, script, | 83 * It does not deal well with many things: comments, script, |
84 * elements, style elements, dir attribute,`>` in quoted attribute values, | 84 * elements, style elements, dir attribute,`>` in quoted attribute values, |
85 * etc. But it does handle well enough the most common use cases. | 85 * etc. But it does handle well enough the most common use cases. |
86 * Since the worst that can happen as a result of these shortcomings is that | 86 * Since the worst that can happen as a result of these shortcomings is that |
87 * the wrong directionality will be estimated, we have not invested in | 87 * the wrong directionality will be estimated, we have not invested in |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 } | 382 } |
383 | 383 |
384 /** | 384 /** |
385 * Check the estimated directionality of [str], return true if the piece of | 385 * Check the estimated directionality of [str], return true if the piece of |
386 * text should be laid out in RTL direction. If [isHtml] is true, the string | 386 * text should be laid out in RTL direction. If [isHtml] is true, the string |
387 * is HTML or HTML-escaped. | 387 * is HTML or HTML-escaped. |
388 */ | 388 */ |
389 bool detectRtlDirectionality(String str, [bool isHtml]) { | 389 bool detectRtlDirectionality(String str, [bool isHtml]) { |
390 return estimateDirectionOfText(str, isHtml) == TextDirection.RTL; | 390 return estimateDirectionOfText(str, isHtml) == TextDirection.RTL; |
391 } | 391 } |
OLD | NEW |