| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 63 |
| 64 unsigned omitStart = (keepCount + 1) / 2; | 64 unsigned omitStart = (keepCount + 1) / 2; |
| 65 NonSharedCharacterBreakIterator it(string); | 65 NonSharedCharacterBreakIterator it(string); |
| 66 unsigned omitEnd = boundedTextBreakFollowing(it, omitStart + (length - keepC
ount) - 1, length); | 66 unsigned omitEnd = boundedTextBreakFollowing(it, omitStart + (length - keepC
ount) - 1, length); |
| 67 omitStart = textBreakAtOrPreceding(it, omitStart); | 67 omitStart = textBreakAtOrPreceding(it, omitStart); |
| 68 | 68 |
| 69 unsigned truncatedLength = omitStart + 1 + (length - omitEnd); | 69 unsigned truncatedLength = omitStart + 1 + (length - omitEnd); |
| 70 ASSERT(truncatedLength <= length); | 70 ASSERT(truncatedLength <= length); |
| 71 | 71 |
| 72 string.copyTo(buffer, 0, omitStart); | 72 string.copyTo(buffer, 0, omitStart); |
| 73 buffer[omitStart] = horizontalEllipsis; | 73 buffer[omitStart] = horizontalEllipsisCharacter; |
| 74 string.copyTo(&buffer[omitStart + 1], omitEnd, length - omitEnd); | 74 string.copyTo(&buffer[omitStart + 1], omitEnd, length - omitEnd); |
| 75 | 75 |
| 76 return truncatedLength; | 76 return truncatedLength; |
| 77 } | 77 } |
| 78 | 78 |
| 79 static unsigned rightTruncateToBuffer(const String& string, unsigned length, uns
igned keepCount, UChar* buffer) | 79 static unsigned rightTruncateToBuffer(const String& string, unsigned length, uns
igned keepCount, UChar* buffer) |
| 80 { | 80 { |
| 81 ASSERT(keepCount < length); | 81 ASSERT(keepCount < length); |
| 82 ASSERT(keepCount < STRING_BUFFER_SIZE); | 82 ASSERT(keepCount < STRING_BUFFER_SIZE); |
| 83 | 83 |
| 84 NonSharedCharacterBreakIterator it(string); | 84 NonSharedCharacterBreakIterator it(string); |
| 85 unsigned keepLength = textBreakAtOrPreceding(it, keepCount); | 85 unsigned keepLength = textBreakAtOrPreceding(it, keepCount); |
| 86 unsigned truncatedLength = keepLength + 1; | 86 unsigned truncatedLength = keepLength + 1; |
| 87 | 87 |
| 88 string.copyTo(buffer, 0, keepLength); | 88 string.copyTo(buffer, 0, keepLength); |
| 89 buffer[keepLength] = horizontalEllipsis; | 89 buffer[keepLength] = horizontalEllipsisCharacter; |
| 90 | 90 |
| 91 return truncatedLength; | 91 return truncatedLength; |
| 92 } | 92 } |
| 93 | 93 |
| 94 static float stringWidth(const Font& renderer, const String& string) | 94 static float stringWidth(const Font& renderer, const String& string) |
| 95 { | 95 { |
| 96 TextRun run(string); | 96 TextRun run(string); |
| 97 return renderer.width(run); | 97 return renderer.width(run); |
| 98 } | 98 } |
| 99 | 99 |
| 100 static float stringWidth(const Font& renderer, const UChar* characters, unsigned
length) | 100 static float stringWidth(const Font& renderer, const UChar* characters, unsigned
length) |
| 101 { | 101 { |
| 102 TextRun run(characters, length); | 102 TextRun run(characters, length); |
| 103 return renderer.width(run); | 103 return renderer.width(run); |
| 104 } | 104 } |
| 105 | 105 |
| 106 static String truncateString(const String& string, float maxWidth, const Font& f
ont, TruncationFunction truncateToBuffer) | 106 static String truncateString(const String& string, float maxWidth, const Font& f
ont, TruncationFunction truncateToBuffer) |
| 107 { | 107 { |
| 108 if (string.isEmpty()) | 108 if (string.isEmpty()) |
| 109 return string; | 109 return string; |
| 110 | 110 |
| 111 ASSERT(maxWidth >= 0); | 111 ASSERT(maxWidth >= 0); |
| 112 | 112 |
| 113 float currentEllipsisWidth = stringWidth(font, &horizontalEllipsis, 1); | 113 float currentEllipsisWidth = stringWidth(font, &horizontalEllipsisCharacter,
1); |
| 114 | 114 |
| 115 UChar stringBuffer[STRING_BUFFER_SIZE]; | 115 UChar stringBuffer[STRING_BUFFER_SIZE]; |
| 116 unsigned truncatedLength; | 116 unsigned truncatedLength; |
| 117 unsigned keepCount; | 117 unsigned keepCount; |
| 118 unsigned length = string.length(); | 118 unsigned length = string.length(); |
| 119 | 119 |
| 120 if (length > STRING_BUFFER_SIZE) { | 120 if (length > STRING_BUFFER_SIZE) { |
| 121 keepCount = STRING_BUFFER_SIZE - 1; // need 1 character for the ellipsis | 121 keepCount = STRING_BUFFER_SIZE - 1; // need 1 character for the ellipsis |
| 122 truncatedLength = centerTruncateToBuffer(string, length, keepCount, stri
ngBuffer); | 122 truncatedLength = centerTruncateToBuffer(string, length, keepCount, stri
ngBuffer); |
| 123 } else { | 123 } else { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 { | 192 { |
| 193 return truncateString(string, maxWidth, font, rightTruncateToBuffer); | 193 return truncateString(string, maxWidth, font, rightTruncateToBuffer); |
| 194 } | 194 } |
| 195 | 195 |
| 196 float StringTruncator::width(const String& string, const Font& font) | 196 float StringTruncator::width(const String& string, const Font& font) |
| 197 { | 197 { |
| 198 return stringWidth(font, string); | 198 return stringWidth(font, string); |
| 199 } | 199 } |
| 200 | 200 |
| 201 } // namespace blink | 201 } // namespace blink |
| OLD | NEW |