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 == TextBreakDone ? length : result; | 61 return result == TextBreakDone ? 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 keepCount, | 66 unsigned keepCount, |
67 UChar* buffer) { | 67 UChar* buffer) { |
68 ASSERT(keepCount < length); | 68 DCHECK_LT(keepCount, length); |
69 ASSERT(keepCount < STRING_BUFFER_SIZE); | 69 DCHECK(keepCount < STRING_BUFFER_SIZE); |
70 | 70 |
71 unsigned omitStart = (keepCount + 1) / 2; | 71 unsigned omitStart = (keepCount + 1) / 2; |
72 NonSharedCharacterBreakIterator it(string); | 72 NonSharedCharacterBreakIterator it(string); |
73 unsigned omitEnd = boundedTextBreakFollowing( | 73 unsigned omitEnd = boundedTextBreakFollowing( |
74 it, omitStart + (length - keepCount) - 1, length); | 74 it, omitStart + (length - keepCount) - 1, length); |
75 omitStart = textBreakAtOrPreceding(it, omitStart); | 75 omitStart = textBreakAtOrPreceding(it, omitStart); |
76 | 76 |
77 unsigned truncatedLength = omitStart + 1 + (length - omitEnd); | 77 unsigned truncatedLength = omitStart + 1 + (length - omitEnd); |
78 ASSERT(truncatedLength <= length); | 78 DCHECK_LE(truncatedLength, length); |
79 | 79 |
80 string.copyTo(buffer, 0, omitStart); | 80 string.copyTo(buffer, 0, omitStart); |
81 buffer[omitStart] = horizontalEllipsisCharacter; | 81 buffer[omitStart] = horizontalEllipsisCharacter; |
82 string.copyTo(&buffer[omitStart + 1], omitEnd, length - omitEnd); | 82 string.copyTo(&buffer[omitStart + 1], omitEnd, length - omitEnd); |
83 | 83 |
84 return truncatedLength; | 84 return truncatedLength; |
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 keepCount, | 89 unsigned keepCount, |
90 UChar* buffer) { | 90 UChar* buffer) { |
91 ASSERT(keepCount < length); | 91 DCHECK_LT(keepCount, length); |
92 ASSERT(keepCount < STRING_BUFFER_SIZE); | 92 DCHECK(keepCount < STRING_BUFFER_SIZE); |
93 | 93 |
94 NonSharedCharacterBreakIterator it(string); | 94 NonSharedCharacterBreakIterator it(string); |
95 unsigned keepLength = textBreakAtOrPreceding(it, keepCount); | 95 unsigned keepLength = textBreakAtOrPreceding(it, keepCount); |
96 unsigned truncatedLength = keepLength + 1; | 96 unsigned truncatedLength = keepLength + 1; |
97 | 97 |
98 string.copyTo(buffer, 0, keepLength); | 98 string.copyTo(buffer, 0, keepLength); |
99 buffer[keepLength] = horizontalEllipsisCharacter; | 99 buffer[keepLength] = horizontalEllipsisCharacter; |
100 | 100 |
101 return truncatedLength; | 101 return truncatedLength; |
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 maxWidth, | 117 float maxWidth, |
118 const Font& font, | 118 const Font& font, |
119 TruncationFunction truncateToBuffer) { | 119 TruncationFunction truncateToBuffer) { |
120 if (string.isEmpty()) | 120 if (string.isEmpty()) |
121 return string; | 121 return string; |
122 | 122 |
123 ASSERT(maxWidth >= 0); | 123 DCHECK_GE(maxWidth, 0); |
124 | 124 |
125 float currentEllipsisWidth = | 125 float currentEllipsisWidth = |
126 stringWidth(font, &horizontalEllipsisCharacter, 1); | 126 stringWidth(font, &horizontalEllipsisCharacter, 1); |
127 | 127 |
128 UChar stringBuffer[STRING_BUFFER_SIZE]; | 128 UChar stringBuffer[STRING_BUFFER_SIZE]; |
129 unsigned truncatedLength; | 129 unsigned truncatedLength; |
130 unsigned keepCount; | 130 unsigned keepCount; |
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 keepCountForSmallestKnownToNotFit = keepCount; | 150 unsigned keepCountForSmallestKnownToNotFit = keepCount; |
151 float widthForSmallestKnownToNotFit = width; | 151 float widthForSmallestKnownToNotFit = width; |
152 | 152 |
153 if (currentEllipsisWidth >= maxWidth) { | 153 if (currentEllipsisWidth >= maxWidth) { |
154 keepCountForLargestKnownToFit = 1; | 154 keepCountForLargestKnownToFit = 1; |
155 keepCountForSmallestKnownToNotFit = 2; | 155 keepCountForSmallestKnownToNotFit = 2; |
156 } | 156 } |
157 | 157 |
158 while (keepCountForLargestKnownToFit + 1 < | 158 while (keepCountForLargestKnownToFit + 1 < |
159 keepCountForSmallestKnownToNotFit) { | 159 keepCountForSmallestKnownToNotFit) { |
160 ASSERT(widthForLargestKnownToFit <= maxWidth); | 160 DCHECK_LE(widthForLargestKnownToFit, maxWidth); |
161 ASSERT(widthForSmallestKnownToNotFit > maxWidth); | 161 DCHECK_GT(widthForSmallestKnownToNotFit, maxWidth); |
162 | 162 |
163 float ratio = | 163 float ratio = |
164 (keepCountForSmallestKnownToNotFit - keepCountForLargestKnownToFit) / | 164 (keepCountForSmallestKnownToNotFit - keepCountForLargestKnownToFit) / |
165 (widthForSmallestKnownToNotFit - widthForLargestKnownToFit); | 165 (widthForSmallestKnownToNotFit - widthForLargestKnownToFit); |
166 keepCount = static_cast<unsigned>(maxWidth * ratio); | 166 keepCount = static_cast<unsigned>(maxWidth * ratio); |
167 | 167 |
168 if (keepCount <= keepCountForLargestKnownToFit) { | 168 if (keepCount <= keepCountForLargestKnownToFit) { |
169 keepCount = keepCountForLargestKnownToFit + 1; | 169 keepCount = keepCountForLargestKnownToFit + 1; |
170 } else if (keepCount >= keepCountForSmallestKnownToNotFit) { | 170 } else if (keepCount >= keepCountForSmallestKnownToNotFit) { |
171 keepCount = keepCountForSmallestKnownToNotFit - 1; | 171 keepCount = keepCountForSmallestKnownToNotFit - 1; |
172 } | 172 } |
173 | 173 |
174 ASSERT(keepCount < length); | 174 DCHECK_LT(keepCount, length); |
175 ASSERT(keepCount > 0); | 175 DCHECK_GT(keepCount, 0u); |
176 ASSERT(keepCount < keepCountForSmallestKnownToNotFit); | 176 DCHECK_LT(keepCount, keepCountForSmallestKnownToNotFit); |
177 ASSERT(keepCount > keepCountForLargestKnownToFit); | 177 DCHECK_GT(keepCount, keepCountForLargestKnownToFit); |
178 | 178 |
179 truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer); | 179 truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer); |
180 | 180 |
181 width = stringWidth(font, stringBuffer, truncatedLength); | 181 width = stringWidth(font, stringBuffer, truncatedLength); |
182 if (width <= maxWidth) { | 182 if (width <= maxWidth) { |
183 keepCountForLargestKnownToFit = keepCount; | 183 keepCountForLargestKnownToFit = keepCount; |
184 widthForLargestKnownToFit = width; | 184 widthForLargestKnownToFit = width; |
185 } else { | 185 } else { |
186 keepCountForSmallestKnownToNotFit = keepCount; | 186 keepCountForSmallestKnownToNotFit = keepCount; |
187 widthForSmallestKnownToNotFit = width; | 187 widthForSmallestKnownToNotFit = width; |
(...skipping 21 matching lines...) Expand all Loading... |
209 float maxWidth, | 209 float maxWidth, |
210 const Font& font) { | 210 const Font& font) { |
211 return truncateString(string, maxWidth, font, rightTruncateToBuffer); | 211 return truncateString(string, maxWidth, font, rightTruncateToBuffer); |
212 } | 212 } |
213 | 213 |
214 float StringTruncator::width(const String& string, const Font& font) { | 214 float StringTruncator::width(const String& string, const Font& font) { |
215 return stringWidth(font, string); | 215 return stringWidth(font, string); |
216 } | 216 } |
217 | 217 |
218 } // namespace blink | 218 } // namespace blink |
OLD | NEW |