| 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 // TODO(jacobr): handle splitting lines on symbols such as '-' that aren't | 5 // TODO(jacobr): handle splitting lines on symbols such as '-' that aren't |
| 6 // whitespace but are valid word breaking points. | 6 // whitespace but are valid word breaking points. |
| 7 /** | 7 /** |
| 8 * Utility class to efficiently word break and measure text without requiring | 8 * Utility class to efficiently word break and measure text without requiring |
| 9 * access to the DOM. | 9 * access to the DOM. |
| 10 */ | 10 */ |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 // TODO(jacobr): we are DOA for i18N... | 32 // TODO(jacobr): we are DOA for i18N... |
| 33 // the right solution is for the server to send us text perparsed into words | 33 // the right solution is for the server to send us text perparsed into words |
| 34 // perhaps even with hints on the guess for the correct breaks so on the | 34 // perhaps even with hints on the guess for the correct breaks so on the |
| 35 // client all we have to do is verify and fix errors rather than perform the | 35 // client all we have to do is verify and fix errors rather than perform the |
| 36 // full calculation. | 36 // full calculation. |
| 37 static bool isWhitespace(String character) { | 37 static bool isWhitespace(String character) { |
| 38 return character == ' ' || character == '\t' || character == '\n'; | 38 return character == ' ' || character == '\t' || character == '\n'; |
| 39 } | 39 } |
| 40 | 40 |
| 41 num get typicalCharLength() { | 41 num get typicalCharLength { |
| 42 return _typicalCharLength; | 42 return _typicalCharLength; |
| 43 } | 43 } |
| 44 | 44 |
| 45 String quickTruncate(String text, num lineWidth, int maxLines) { | 45 String quickTruncate(String text, num lineWidth, int maxLines) { |
| 46 int targetLength = (lineWidth * maxLines / _typicalCharLength).toInt(); | 46 int targetLength = (lineWidth * maxLines / _typicalCharLength).toInt(); |
| 47 // Advance to next word break point. | 47 // Advance to next word break point. |
| 48 while(targetLength < text.length && !isWhitespace(text[targetLength])) { | 48 while(targetLength < text.length && !isWhitespace(text[targetLength])) { |
| 49 targetLength++; | 49 targetLength++; |
| 50 } | 50 } |
| 51 | 51 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 } else if (wordStartIndex === null && !whitespace) { | 152 } else if (wordStartIndex === null && !whitespace) { |
| 153 wordStartIndex = i; | 153 wordStartIndex = i; |
| 154 } | 154 } |
| 155 lastWhitespace = whitespace; | 155 lastWhitespace = whitespace; |
| 156 } | 156 } |
| 157 if (currentLength > 0) { | 157 if (currentLength > 0) { |
| 158 callback(startIndex, text.length, currentLength); | 158 callback(startIndex, text.length, currentLength); |
| 159 } | 159 } |
| 160 } | 160 } |
| 161 } | 161 } |
| OLD | NEW |