Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: lib/i18n/bidi_formatter.dart

Issue 10703030: Get rid of BidiUtils as a class holding only static methods. I was reminded by (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | lib/i18n/bidi_utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 * neutral or matches the context, so that the DOM structure of the output 69 * neutral or matches the context, so that the DOM structure of the output
70 * does not depend on the combination of directionalities. 70 * does not depend on the combination of directionalities.
71 */ 71 */
72 BidiFormatter.LTR([alwaysSpan=false]) : contextDirection = TextDirection.LTR, 72 BidiFormatter.LTR([alwaysSpan=false]) : contextDirection = TextDirection.LTR,
73 _alwaysSpan = alwaysSpan; 73 _alwaysSpan = alwaysSpan;
74 BidiFormatter.RTL([alwaysSpan=false]) : contextDirection = TextDirection.RTL, 74 BidiFormatter.RTL([alwaysSpan=false]) : contextDirection = TextDirection.RTL,
75 _alwaysSpan = alwaysSpan; 75 _alwaysSpan = alwaysSpan;
76 BidiFormatter.UNKNOWN([alwaysSpan=false]) : 76 BidiFormatter.UNKNOWN([alwaysSpan=false]) :
77 contextDirection = TextDirection.UNKNOWN, _alwaysSpan = alwaysSpan; 77 contextDirection = TextDirection.UNKNOWN, _alwaysSpan = alwaysSpan;
78 78
79 /** Is true if the context direction for this formatter is RTL. */ 79 /** Is true if the known context direction for this formatter is RTL. */
80 bool get isRTL() => contextDirection == TextDirection.RTL; 80 bool get isRTL() => contextDirection == TextDirection.RTL;
81 81
82 /** 82 /**
83 * Formats a string of a given (or estimated, if not provided) 83 * Formats a string of a given (or estimated, if not provided)
84 * [direction] for use in HTML output of the context directionality, so 84 * [direction] for use in HTML output of the context directionality, so
85 * an opposite-directionality string is neither garbled nor garbles what 85 * an opposite-directionality string is neither garbled nor garbles what
86 * follows it. 86 * follows it.
87 * If the input string's directionality doesn't match the context 87 * If the input string's directionality doesn't match the context
88 * directionality, we wrap it with a `span` tag and add a `dir` attribute 88 * directionality, we wrap it with a `span` tag and add a `dir` attribute
89 * (either "dir=rtl" or "dir=ltr"). 89 * (either "dir=rtl" or "dir=ltr").
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 * do not allow markup, e.g. an 'option' tag. 130 * do not allow markup, e.g. an 'option' tag.
131 * This function does *not* do HTML-escaping regardless of the value of 131 * This function does *not* do HTML-escaping regardless of the value of
132 * [isHtml]. [isHtml] is used to designate if the text contains HTML (escaped 132 * [isHtml]. [isHtml] is used to designate if the text contains HTML (escaped
133 * or unescaped). 133 * or unescaped).
134 */ 134 */
135 String wrapWithUnicode(String text, [bool isHtml=false, bool resetDir=true, 135 String wrapWithUnicode(String text, [bool isHtml=false, bool resetDir=true,
136 TextDirection direction]) { 136 TextDirection direction]) {
137 if (direction == null) direction = estimateDirection(text, isHtml); 137 if (direction == null) direction = estimateDirection(text, isHtml);
138 var result = text; 138 var result = text;
139 if (contextDirection.isDirectionChange(direction)) { 139 if (contextDirection.isDirectionChange(direction)) {
140 result = '''${direction == TextDirection.RTL ? 140 result = '''${direction == TextDirection.RTL ? RLE : LRE}$text$PDF''';
141 BidiUtils.RLE :
142 BidiUtils.LRE}$text${BidiUtils.PDF}''';
143 } 141 }
144 return result.concat(resetDir? _resetDir(text, direction, isHtml) : ''); 142 return result.concat(resetDir? _resetDir(text, direction, isHtml) : '');
145 } 143 }
146 144
147 /** 145 /**
148 * Estimates the directionality of [text] using the best known 146 * Estimates the directionality of [text] using the best known
149 * general-purpose method (using relative word counts). A 147 * general-purpose method (using relative word counts). A
150 * TextDirection.UNKNOWN return value indicates completely neutral input. 148 * TextDirection.UNKNOWN return value indicates completely neutral input.
151 * [isHtml] is true if [text] HTML or HTML-escaped. 149 * [isHtml] is true if [text] HTML or HTML-escaped.
152 */ 150 */
153 TextDirection estimateDirection(String text, [bool isHtml=false]) { 151 TextDirection estimateDirection(String text, [bool isHtml=false]) {
154 return BidiUtils.estimateDirection(text, isHtml); 152 return estimateDirectionOfText(text, isHtml); //TODO~!!!
155 } 153 }
156 154
157 /** 155 /**
158 * Returns a unicode BiDi mark matching the surrounding context's [direction] 156 * Returns a unicode BiDi mark matching the surrounding context's [direction]
159 * (not necessarily the direction of [text]). The function returns an LRM or 157 * (not necessarily the direction of [text]). The function returns an LRM or
160 * RLM if the overall directionality or the exit directionality of [text] is 158 * RLM if the overall directionality or the exit directionality of [text] is
161 * opposite the context directionality. Otherwise 159 * opposite the context directionality. Otherwise
162 * return the empty string. [isHtml] is true if [text] is HTML or 160 * return the empty string. [isHtml] is true if [text] is HTML or
163 * HTML-escaped. 161 * HTML-escaped.
164 */ 162 */
165 String _resetDir(String text, TextDirection direction, bool isHtml) { 163 String _resetDir(String text, TextDirection direction, bool isHtml) {
166 // endsWithRtl and endsWithLtr are called only if needed (short-circuit). 164 // endsWithRtl and endsWithLtr are called only if needed (short-circuit).
167 if ((contextDirection == TextDirection.LTR && 165 if ((contextDirection == TextDirection.LTR &&
168 (direction == TextDirection.RTL || 166 (direction == TextDirection.RTL ||
169 BidiUtils.endsWithRtl(text, isHtml))) || 167 endsWithRtl(text, isHtml))) ||
170 (contextDirection == TextDirection.RTL && 168 (contextDirection == TextDirection.RTL &&
171 (direction == TextDirection.LTR || 169 (direction == TextDirection.LTR ||
172 BidiUtils.endsWithLtr(text, isHtml)))) { 170 endsWithLtr(text, isHtml)))) {
173 if (contextDirection == TextDirection.LTR) { 171 if (contextDirection == TextDirection.LTR) {
174 return BidiUtils.LRM; 172 return LRM;
175 } else { 173 } else {
176 return BidiUtils.RLM; 174 return RLM;
177 } 175 }
178 } else { 176 } else {
179 return ''; 177 return '';
180 } 178 }
181 } 179 }
182 } 180 }
OLDNEW
« no previous file with comments | « no previous file | lib/i18n/bidi_utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698