OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 part of view; | 5 part of view; |
6 | 6 |
7 // TODO(jacobr): handle splitting lines on symbols such as '-' that aren't | 7 // TODO(jacobr): handle splitting lines on symbols such as '-' that aren't |
8 // whitespace but are valid word breaking points. | 8 // whitespace but are valid word breaking points. |
9 /** | 9 /** |
10 * Utility class to efficiently word break and measure text without requiring | 10 * Utility class to efficiently word break and measure text without requiring |
(...skipping 27 matching lines...) Expand all Loading... |
38 // full calculation. | 38 // full calculation. |
39 static bool isWhitespace(String character) { | 39 static bool isWhitespace(String character) { |
40 return character == ' ' || character == '\t' || character == '\n'; | 40 return character == ' ' || character == '\t' || character == '\n'; |
41 } | 41 } |
42 | 42 |
43 num get typicalCharLength { | 43 num get typicalCharLength { |
44 return _typicalCharLength; | 44 return _typicalCharLength; |
45 } | 45 } |
46 | 46 |
47 String quickTruncate(String text, num lineWidth, int maxLines) { | 47 String quickTruncate(String text, num lineWidth, int maxLines) { |
48 int targetLength = (lineWidth * maxLines / _typicalCharLength).toInt(); | 48 int targetLength = lineWidth * maxLines ~/ _typicalCharLength; |
49 // Advance to next word break point. | 49 // Advance to next word break point. |
50 while(targetLength < text.length && !isWhitespace(text[targetLength])) { | 50 while(targetLength < text.length && !isWhitespace(text[targetLength])) { |
51 targetLength++; | 51 targetLength++; |
52 } | 52 } |
53 | 53 |
54 if (targetLength < text.length) { | 54 if (targetLength < text.length) { |
55 return '${text.substring(0, targetLength)}$ELLIPSIS'; | 55 return '${text.substring(0, targetLength)}$ELLIPSIS'; |
56 } else { | 56 } else { |
57 return text; | 57 return text; |
58 } | 58 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 } else if (wordStartIndex == null && !whitespace) { | 154 } else if (wordStartIndex == null && !whitespace) { |
155 wordStartIndex = i; | 155 wordStartIndex = i; |
156 } | 156 } |
157 lastWhitespace = whitespace; | 157 lastWhitespace = whitespace; |
158 } | 158 } |
159 if (currentLength > 0) { | 159 if (currentLength > 0) { |
160 callback(startIndex, text.length, currentLength); | 160 callback(startIndex, text.length, currentLength); |
161 } | 161 } |
162 } | 162 } |
163 } | 163 } |
OLD | NEW |