1 /*
2 * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30 #include "StringTruncator.h"
31
32 #include "CharacterNames.h"
33 #include "Font.h"
34 #include "TextBreakIterator.h"
35 #include <wtf/Assertions.h>
36 #include <wtf/Vector.h>
37
38 namespace WebCore {
39
40 #define STRING_BUFFER_SIZE 2048
41
42 typedef unsigned TruncationFunction(const String&, unsigned length, unsigned keepCount, UChar* buffer);
43
textBreakAtOrPreceding(TextBreakIterator * it,int offset)44 static inline int textBreakAtOrPreceding(TextBreakIterator* it, int offset)
45 {
46 if (isTextBreak(it, offset))
47 return offset;
48
49 int result = textBreakPreceding(it, offset);
50 return result == TextBreakDone ? 0 : result;
51 }
52
boundedTextBreakFollowing(TextBreakIterator * it,int offset,int length)53 static inline int boundedTextBreakFollowing(TextBreakIterator* it, int offset, int length)
54 {
55 int result = textBreakFollowing(it, offset);
56 return result == TextBreakDone ? length : result;
57 }
58
centerTruncateToBuffer(const String & string,unsigned length,unsigned keepCount,UChar * buffer)59 static unsigned centerTruncateToBuffer(const String& string, unsigned length, unsigned keepCount, UChar* buffer)
60 {
61 ASSERT(keepCount < length);
62 ASSERT(keepCount < STRING_BUFFER_SIZE);
63
64 unsigned omitStart = (keepCount + 1) / 2;
65 TextBreakIterator* it = characterBreakIterator(string.characters(), length);
66 unsigned omitEnd = boundedTextBreakFollowing(it, omitStart + (length - keepCount) - 1, length);
67 omitStart = textBreakAtOrPreceding(it, omitStart);
68
69 unsigned truncatedLength = omitStart + 1 + (length - omitEnd);
70 ASSERT(truncatedLength <= length);
71
72 memcpy(buffer, string.characters(), sizeof(UChar) * omitStart);
73 buffer[omitStart] = horizontalEllipsis;
74 memcpy(&buffer[omitStart + 1], &string.characters()[omitEnd], sizeof(UChar) * (length - omitEnd));
75
76 return truncatedLength;
77 }
78
rightTruncateToBuffer(const String & string,unsigned length,unsigned keepCount,UChar * buffer)79 static unsigned rightTruncateToBuffer(const String& string, unsigned length, unsigned keepCount, UChar* buffer)
80 {
81 ASSERT(keepCount < length);
82 ASSERT(keepCount < STRING_BUFFER_SIZE);
83
84 TextBreakIterator* it = characterBreakIterator(string.characters(), length);
85 unsigned keepLength = textBreakAtOrPreceding(it, keepCount);
86 unsigned truncatedLength = keepLength + 1;
87
88 memcpy(buffer, string.characters(), sizeof(UChar) * keepLength);
89 buffer[keepLength] = horizontalEllipsis;
90
91 return truncatedLength;
92 }
93
stringWidth(const Font & renderer,const UChar * characters,unsigned length,bool disableRoundingHacks)94 static float stringWidth(const Font& renderer, const UChar* characters, unsigned length, bool disableRoundingHacks)
95 {
96 TextRun run(characters, length);
97 if (disableRoundingHacks)
98 run.disableRoundingHacks();
99 return renderer.floatWidth(run);
100 }
101
truncateString(const String & string,float maxWidth,const Font & font,TruncationFunction truncateToBuffer,bool disableRoundingHacks)102 static String truncateString(const String& string, float maxWidth, const Font& font, TruncationFunction truncateToBuffer, bool disableRoundingHacks)
103 {
104 if (string.isEmpty())
105 return string;
106
107 ASSERT(maxWidth >= 0);
108
109 float currentEllipsisWidth = stringWidth(font, &horizontalEllipsis, 1, disableRoundingHacks);
110
111 UChar stringBuffer[STRING_BUFFER_SIZE];
112 unsigned truncatedLength;
113 unsigned keepCount;
114 unsigned length = string.length();
115
116 if (length > STRING_BUFFER_SIZE) {
117 keepCount = STRING_BUFFER_SIZE - 1; // need 1 character for the ellipsis
118 truncatedLength = centerTruncateToBuffer(string, length, keepCount, stringBuffer);
119 } else {
120 keepCount = length;
121 memcpy(stringBuffer, string.characters(), sizeof(UChar) * length);
122 truncatedLength = length;
123 }
124
125 float width = stringWidth(font, stringBuffer, truncatedLength, disableRoundingHacks);
126 if (width <= maxWidth)
127 return string;
128
129 unsigned keepCountForLargestKnownToFit = 0;
130 float widthForLargestKnownToFit = currentEllipsisWidth;
131
132 unsigned keepCountForSmallestKnownToNotFit = keepCount;
133 float widthForSmallestKnownToNotFit = width;
134
135 if (currentEllipsisWidth >= maxWidth) {
136 keepCountForLargestKnownToFit = 1;
137 keepCountForSmallestKnownToNotFit = 2;
138 }
139
140 while (keepCountForLargestKnownToFit + 1 < keepCountForSmallestKnownToNotFit) {
141 ASSERT(widthForLargestKnownToFit <= maxWidth);
142 ASSERT(widthForSmallestKnownToNotFit > maxWidth);
143
144 float ratio = (keepCountForSmallestKnownToNotFit - keepCountForLargestKnownToFit)
145 / (widthForSmallestKnownToNotFit - widthForLargestKnownToFit);
146 keepCount = static_cast<unsigned>(maxWidth * ratio);
147
148 if (keepCount <= keepCountForLargestKnownToFit) {
149 keepCount = keepCountForLargestKnownToFit + 1;
150 } else if (keepCount >= keepCountForSmallestKnownToNotFit) {
151 keepCount = keepCountForSmallestKnownToNotFit - 1;
152 }
153
154 ASSERT(keepCount < length);
155 ASSERT(keepCount > 0);
156 ASSERT(keepCount < keepCountForSmallestKnownToNotFit);
157 ASSERT(keepCount > keepCountForLargestKnownToFit);
158
159 truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer);
160
161 width = stringWidth(font, stringBuffer, truncatedLength, disableRoundingHacks);
162 if (width <= maxWidth) {
163 keepCountForLargestKnownToFit = keepCount;
164 widthForLargestKnownToFit = width;
165 } else {
166 keepCountForSmallestKnownToNotFit = keepCount;
167 widthForSmallestKnownToNotFit = width;
168 }
169 }
170
171 if (keepCountForLargestKnownToFit == 0) {
172 keepCountForLargestKnownToFit = 1;
173 }
174
175 if (keepCount != keepCountForLargestKnownToFit) {
176 keepCount = keepCountForLargestKnownToFit;
177 truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer);
178 }
179
180 return String(stringBuffer, truncatedLength);
181 }
182
centerTruncate(const String & string,float maxWidth,const Font & font,bool disableRoundingHacks)183 String StringTruncator::centerTruncate(const String& string, float maxWidth, const Font& font, bool disableRoundingHacks)
184 {
185 return truncateString(string, maxWidth, font, centerTruncateToBuffer, disableRoundingHacks);
186 }
187
rightTruncate(const String & string,float maxWidth,const Font & font,bool disableRoundingHacks)188 String StringTruncator::rightTruncate(const String& string, float maxWidth, const Font& font, bool disableRoundingHacks)
189 {
190 return truncateString(string, maxWidth, font, rightTruncateToBuffer, disableRoundingHacks);
191 }
192
width(const String & string,const Font & font,bool disableRoundingHacks)193 float StringTruncator::width(const String& string, const Font& font, bool disableRoundingHacks)
194 {
195 return stringWidth(font, string.characters(), string.length(), disableRoundingHacks);
196 }
197
198 } // namespace WebCore
199