| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 int offset, | 58 int offset, |
| 59 int length) { | 59 int length) { |
| 60 int result = it.Following(offset); | 60 int result = it.Following(offset); |
| 61 return result == kTextBreakDone ? length : result; | 61 return result == kTextBreakDone ? length : result; |
| 62 } | 62 } |
| 63 | 63 |
| 64 static unsigned CenterTruncateToBuffer(const String& string, | 64 static unsigned CenterTruncateToBuffer(const String& string, |
| 65 unsigned length, | 65 unsigned length, |
| 66 unsigned keep_count, | 66 unsigned keep_count, |
| 67 UChar* buffer) { | 67 UChar* buffer) { |
| 68 ASSERT(keep_count < length); | 68 DCHECK_LT(keep_count, length); |
| 69 ASSERT(keep_count < STRING_BUFFER_SIZE); | 69 DCHECK(keep_count < STRING_BUFFER_SIZE); |
| 70 | 70 |
| 71 unsigned omit_start = (keep_count + 1) / 2; | 71 unsigned omit_start = (keep_count + 1) / 2; |
| 72 NonSharedCharacterBreakIterator it(string); | 72 NonSharedCharacterBreakIterator it(string); |
| 73 unsigned omit_end = BoundedTextBreakFollowing( | 73 unsigned omit_end = BoundedTextBreakFollowing( |
| 74 it, omit_start + (length - keep_count) - 1, length); | 74 it, omit_start + (length - keep_count) - 1, length); |
| 75 omit_start = TextBreakAtOrPreceding(it, omit_start); | 75 omit_start = TextBreakAtOrPreceding(it, omit_start); |
| 76 | 76 |
| 77 unsigned truncated_length = omit_start + 1 + (length - omit_end); | 77 unsigned truncated_length = omit_start + 1 + (length - omit_end); |
| 78 ASSERT(truncated_length <= length); | 78 DCHECK_LE(truncated_length, length); |
| 79 | 79 |
| 80 string.CopyTo(buffer, 0, omit_start); | 80 string.CopyTo(buffer, 0, omit_start); |
| 81 buffer[omit_start] = kHorizontalEllipsisCharacter; | 81 buffer[omit_start] = kHorizontalEllipsisCharacter; |
| 82 string.CopyTo(&buffer[omit_start + 1], omit_end, length - omit_end); | 82 string.CopyTo(&buffer[omit_start + 1], omit_end, length - omit_end); |
| 83 | 83 |
| 84 return truncated_length; | 84 return truncated_length; |
| 85 } | 85 } |
| 86 | 86 |
| 87 static unsigned RightTruncateToBuffer(const String& string, | 87 static unsigned RightTruncateToBuffer(const String& string, |
| 88 unsigned length, | 88 unsigned length, |
| 89 unsigned keep_count, | 89 unsigned keep_count, |
| 90 UChar* buffer) { | 90 UChar* buffer) { |
| 91 ASSERT(keep_count < length); | 91 DCHECK_LT(keep_count, length); |
| 92 ASSERT(keep_count < STRING_BUFFER_SIZE); | 92 DCHECK(keep_count < STRING_BUFFER_SIZE); |
| 93 | 93 |
| 94 NonSharedCharacterBreakIterator it(string); | 94 NonSharedCharacterBreakIterator it(string); |
| 95 unsigned keep_length = TextBreakAtOrPreceding(it, keep_count); | 95 unsigned keep_length = TextBreakAtOrPreceding(it, keep_count); |
| 96 unsigned truncated_length = keep_length + 1; | 96 unsigned truncated_length = keep_length + 1; |
| 97 | 97 |
| 98 string.CopyTo(buffer, 0, keep_length); | 98 string.CopyTo(buffer, 0, keep_length); |
| 99 buffer[keep_length] = kHorizontalEllipsisCharacter; | 99 buffer[keep_length] = kHorizontalEllipsisCharacter; |
| 100 | 100 |
| 101 return truncated_length; | 101 return truncated_length; |
| 102 } | 102 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 113 return renderer.Width(run); | 113 return renderer.Width(run); |
| 114 } | 114 } |
| 115 | 115 |
| 116 static String TruncateString(const String& string, | 116 static String TruncateString(const String& string, |
| 117 float max_width, | 117 float max_width, |
| 118 const Font& font, | 118 const Font& font, |
| 119 TruncationFunction truncate_to_buffer) { | 119 TruncationFunction truncate_to_buffer) { |
| 120 if (string.IsEmpty()) | 120 if (string.IsEmpty()) |
| 121 return string; | 121 return string; |
| 122 | 122 |
| 123 ASSERT(max_width >= 0); | 123 DCHECK_GE(max_width, 0); |
| 124 | 124 |
| 125 float current_ellipsis_width = | 125 float current_ellipsis_width = |
| 126 StringWidth(font, &kHorizontalEllipsisCharacter, 1); | 126 StringWidth(font, &kHorizontalEllipsisCharacter, 1); |
| 127 | 127 |
| 128 UChar string_buffer[STRING_BUFFER_SIZE]; | 128 UChar string_buffer[STRING_BUFFER_SIZE]; |
| 129 unsigned truncated_length; | 129 unsigned truncated_length; |
| 130 unsigned keep_count; | 130 unsigned keep_count; |
| 131 unsigned length = string.length(); | 131 unsigned length = string.length(); |
| 132 | 132 |
| 133 if (length > STRING_BUFFER_SIZE) { | 133 if (length > STRING_BUFFER_SIZE) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 150 unsigned keep_count_for_smallest_known_to_not_fit = keep_count; | 150 unsigned keep_count_for_smallest_known_to_not_fit = keep_count; |
| 151 float width_for_smallest_known_to_not_fit = width; | 151 float width_for_smallest_known_to_not_fit = width; |
| 152 | 152 |
| 153 if (current_ellipsis_width >= max_width) { | 153 if (current_ellipsis_width >= max_width) { |
| 154 keep_count_for_largest_known_to_fit = 1; | 154 keep_count_for_largest_known_to_fit = 1; |
| 155 keep_count_for_smallest_known_to_not_fit = 2; | 155 keep_count_for_smallest_known_to_not_fit = 2; |
| 156 } | 156 } |
| 157 | 157 |
| 158 while (keep_count_for_largest_known_to_fit + 1 < | 158 while (keep_count_for_largest_known_to_fit + 1 < |
| 159 keep_count_for_smallest_known_to_not_fit) { | 159 keep_count_for_smallest_known_to_not_fit) { |
| 160 ASSERT(width_for_largest_known_to_fit <= max_width); | 160 DCHECK_LE(width_for_largest_known_to_fit, max_width); |
| 161 ASSERT(width_for_smallest_known_to_not_fit > max_width); | 161 DCHECK_GT(width_for_smallest_known_to_not_fit, max_width); |
| 162 | 162 |
| 163 float ratio = | 163 float ratio = |
| 164 (keep_count_for_smallest_known_to_not_fit - | 164 (keep_count_for_smallest_known_to_not_fit - |
| 165 keep_count_for_largest_known_to_fit) / | 165 keep_count_for_largest_known_to_fit) / |
| 166 (width_for_smallest_known_to_not_fit - width_for_largest_known_to_fit); | 166 (width_for_smallest_known_to_not_fit - width_for_largest_known_to_fit); |
| 167 keep_count = static_cast<unsigned>(max_width * ratio); | 167 keep_count = static_cast<unsigned>(max_width * ratio); |
| 168 | 168 |
| 169 if (keep_count <= keep_count_for_largest_known_to_fit) { | 169 if (keep_count <= keep_count_for_largest_known_to_fit) { |
| 170 keep_count = keep_count_for_largest_known_to_fit + 1; | 170 keep_count = keep_count_for_largest_known_to_fit + 1; |
| 171 } else if (keep_count >= keep_count_for_smallest_known_to_not_fit) { | 171 } else if (keep_count >= keep_count_for_smallest_known_to_not_fit) { |
| 172 keep_count = keep_count_for_smallest_known_to_not_fit - 1; | 172 keep_count = keep_count_for_smallest_known_to_not_fit - 1; |
| 173 } | 173 } |
| 174 | 174 |
| 175 ASSERT(keep_count < length); | 175 DCHECK_LT(keep_count, length); |
| 176 ASSERT(keep_count > 0); | 176 DCHECK_GT(keep_count, 0u); |
| 177 ASSERT(keep_count < keep_count_for_smallest_known_to_not_fit); | 177 DCHECK_LT(keep_count, keep_count_for_smallest_known_to_not_fit); |
| 178 ASSERT(keep_count > keep_count_for_largest_known_to_fit); | 178 DCHECK_GT(keep_count, keep_count_for_largest_known_to_fit); |
| 179 | 179 |
| 180 truncated_length = | 180 truncated_length = |
| 181 truncate_to_buffer(string, length, keep_count, string_buffer); | 181 truncate_to_buffer(string, length, keep_count, string_buffer); |
| 182 | 182 |
| 183 width = StringWidth(font, string_buffer, truncated_length); | 183 width = StringWidth(font, string_buffer, truncated_length); |
| 184 if (width <= max_width) { | 184 if (width <= max_width) { |
| 185 keep_count_for_largest_known_to_fit = keep_count; | 185 keep_count_for_largest_known_to_fit = keep_count; |
| 186 width_for_largest_known_to_fit = width; | 186 width_for_largest_known_to_fit = width; |
| 187 } else { | 187 } else { |
| 188 keep_count_for_smallest_known_to_not_fit = keep_count; | 188 keep_count_for_smallest_known_to_not_fit = keep_count; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 212 float max_width, | 212 float max_width, |
| 213 const Font& font) { | 213 const Font& font) { |
| 214 return TruncateString(string, max_width, font, RightTruncateToBuffer); | 214 return TruncateString(string, max_width, font, RightTruncateToBuffer); |
| 215 } | 215 } |
| 216 | 216 |
| 217 float StringTruncator::Width(const String& string, const Font& font) { | 217 float StringTruncator::Width(const String& string, const Font& font) { |
| 218 return StringWidth(font, string); | 218 return StringWidth(font, string); |
| 219 } | 219 } |
| 220 | 220 |
| 221 } // namespace blink | 221 } // namespace blink |
| OLD | NEW |