Chromium Code Reviews| 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).truncate(); |
|
Lasse Reichstein Nielsen
2013/01/04 10:29:42
~/
floitsch
2013/03/11 13:39:15
Done in a different CL.
| |
| 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 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 /** | 61 /** |
| 62 * Add line broken text as html separated by <br> elements. | 62 * Add line broken text as html separated by <br> elements. |
| 63 * Returns the number of lines in the output. | 63 * Returns the number of lines in the output. |
| 64 * This function is safe to call with [:sb == null:] in which case just the | 64 * This function is safe to call with [:sb == null:] in which case just the |
| 65 * line count is returned. | 65 * line count is returned. |
| 66 */ | 66 */ |
| 67 int addLineBrokenText(StringBuffer sb, String text, num lineWidth, | 67 int addLineBrokenText(StringBuffer sb, String text, num lineWidth, |
| 68 int maxLines) { | 68 int maxLines) { |
| 69 // Strip surrounding whitespace. This ensures we create zero lines if there | 69 // Strip surrounding whitespace. This ensures we create zero lines if there |
| 70 // is no visible text. | 70 // is no visible text. |
| 71 text = text.trim(); | 71 text = text.trim(); |
| 72 | 72 |
| 73 // We can often avoid performing a full line break calculation when only | 73 // We can often avoid performing a full line break calculation when only |
| 74 // the number of lines and not the actual linebreaks is required. | 74 // the number of lines and not the actual linebreaks is required. |
| 75 if (sb == null) { | 75 if (sb == null) { |
| 76 _context.font = font; | 76 _context.font = font; |
| 77 int textWidth = _context.measureText(text).width.toInt(); | 77 int textWidth = _context.measureText(text).width.truncate(); |
| 78 // By the pigeon hole principle, the resulting text will require at least | 78 // By the pigeon hole principle, the resulting text will require at least |
| 79 // maxLines if the raw text is longer than the amount of text that will | 79 // maxLines if the raw text is longer than the amount of text that will |
| 80 // fit on maxLines - 1. We add the length of a whitespace | 80 // fit on maxLines - 1. We add the length of a whitespace |
| 81 // character to the lineWidth as each line is separated by a whitespace | 81 // character to the lineWidth as each line is separated by a whitespace |
| 82 // character. We assume all whitespace characters have the same length. | 82 // character. We assume all whitespace characters have the same length. |
| 83 if (textWidth >= (lineWidth + _spaceLength) * (maxLines - 1)) { | 83 if (textWidth >= (lineWidth + _spaceLength) * (maxLines - 1)) { |
| 84 return maxLines; | 84 return maxLines; |
| 85 } else if (textWidth == 0) { | 85 } else if (textWidth == 0) { |
| 86 return 0; | 86 return 0; |
| 87 } else if (textWidth < lineWidth) { | 87 } else if (textWidth < lineWidth) { |
| (...skipping 66 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 |