| 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 [Wikipedia](http://en.wikipedia.org/wiki/Bi-directional_text): | 7 * According to [Wikipedia](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, |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 * do not allow markup, e.g. an 'option' tag. | 148 * do not allow markup, e.g. an 'option' tag. |
| 149 * This function does *not* do HTML-escaping regardless of the value of | 149 * This function does *not* do HTML-escaping regardless of the value of |
| 150 * [isHtml]. [isHtml] is used to designate if the text contains HTML (escaped | 150 * [isHtml]. [isHtml] is used to designate if the text contains HTML (escaped |
| 151 * or unescaped). | 151 * or unescaped). |
| 152 */ | 152 */ |
| 153 String wrapWithUnicode(String text, [bool isHtml=false, bool resetDir=true, | 153 String wrapWithUnicode(String text, [bool isHtml=false, bool resetDir=true, |
| 154 TextDirection direction]) { | 154 TextDirection direction]) { |
| 155 if (direction == null) direction = estimateDirection(text, isHtml); | 155 if (direction == null) direction = estimateDirection(text, isHtml); |
| 156 var result = text; | 156 var result = text; |
| 157 if (contextDirection.isDirectionChange(direction)) { | 157 if (contextDirection.isDirectionChange(direction)) { |
| 158 result = '''${direction == TextDirection.RTL ? RLE : LRE}$text$PDF'''; | 158 var marker = direction == TextDirection.RTL ? Bidi.RLE : Bidi.LRE; |
| 159 result = "${marker}$text${Bidi.PDF}"; |
| 160 |
| 159 } | 161 } |
| 160 return result.concat(resetDir? _resetDir(text, direction, isHtml) : ''); | 162 return result.concat(resetDir? _resetDir(text, direction, isHtml) : ''); |
| 161 } | 163 } |
| 162 | 164 |
| 163 /** | 165 /** |
| 164 * Estimates the directionality of [text] using the best known | 166 * Estimates the directionality of [text] using the best known |
| 165 * general-purpose method (using relative word counts). A | 167 * general-purpose method (using relative word counts). A |
| 166 * TextDirection.UNKNOWN return value indicates completely neutral input. | 168 * TextDirection.UNKNOWN return value indicates completely neutral input. |
| 167 * [isHtml] is true if [text] HTML or HTML-escaped. | 169 * [isHtml] is true if [text] HTML or HTML-escaped. |
| 168 */ | 170 */ |
| 169 TextDirection estimateDirection(String text, [bool isHtml=false]) { | 171 TextDirection estimateDirection(String text, [bool isHtml=false]) { |
| 170 return estimateDirectionOfText(text, isHtml); //TODO~!!! | 172 return Bidi.estimateDirectionOfText(text, isHtml); //TODO~!!! |
| 171 } | 173 } |
| 172 | 174 |
| 173 /** | 175 /** |
| 174 * Returns a unicode BiDi mark matching the surrounding context's [direction] | 176 * Returns a unicode BiDi mark matching the surrounding context's [direction] |
| 175 * (not necessarily the direction of [text]). The function returns an LRM or | 177 * (not necessarily the direction of [text]). The function returns an LRM or |
| 176 * RLM if the overall directionality or the exit directionality of [text] is | 178 * RLM if the overall directionality or the exit directionality of [text] is |
| 177 * opposite the context directionality. Otherwise | 179 * opposite the context directionality. Otherwise |
| 178 * return the empty string. [isHtml] is true if [text] is HTML or | 180 * return the empty string. [isHtml] is true if [text] is HTML or |
| 179 * HTML-escaped. | 181 * HTML-escaped. |
| 180 */ | 182 */ |
| 181 String _resetDir(String text, TextDirection direction, bool isHtml) { | 183 String _resetDir(String text, TextDirection direction, bool isHtml) { |
| 182 // endsWithRtl and endsWithLtr are called only if needed (short-circuit). | 184 // endsWithRtl and endsWithLtr are called only if needed (short-circuit). |
| 183 if ((contextDirection == TextDirection.LTR && | 185 if ((contextDirection == TextDirection.LTR && |
| 184 (direction == TextDirection.RTL || | 186 (direction == TextDirection.RTL || |
| 185 endsWithRtl(text, isHtml))) || | 187 Bidi.endsWithRtl(text, isHtml))) || |
| 186 (contextDirection == TextDirection.RTL && | 188 (contextDirection == TextDirection.RTL && |
| 187 (direction == TextDirection.LTR || | 189 (direction == TextDirection.LTR || |
| 188 endsWithLtr(text, isHtml)))) { | 190 Bidi.endsWithLtr(text, isHtml)))) { |
| 189 if (contextDirection == TextDirection.LTR) { | 191 if (contextDirection == TextDirection.LTR) { |
| 190 return LRM; | 192 return Bidi.LRM; |
| 191 } else { | 193 } else { |
| 192 return RLM; | 194 return Bidi.RLM; |
| 193 } | 195 } |
| 194 } else { | 196 } else { |
| 195 return ''; | 197 return ''; |
| 196 } | 198 } |
| 197 } | 199 } |
| 198 } | 200 } |
| OLD | NEW |