* rewrote truncate_middle partly, it failed while trying to add an optional character

this was most noticable in Deskbar when opening DataTranslations in Expand App Mode



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27291 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Karsten Heimrich 2008-09-03 00:26:25 +00:00
parent ed64c85f06
commit 9b56aad8a0
1 changed files with 39 additions and 77 deletions

View File

@ -1322,93 +1322,55 @@ bool
truncate_middle(const char* source, char* dest, uint32 numChars, truncate_middle(const char* source, char* dest, uint32 numChars,
const float* escapementArray, float width, float ellipsisWidth, float size) const float* escapementArray, float width, float ellipsisWidth, float size)
{ {
// find visual center float mid = (width - ellipsisWidth) / 2.0;
ellipsisWidth /= size; uint32 left = 0;
// test if this is as accurate as escapementArray * size float leftWidth = 0.0;
width /= size; while (left < numChars && (leftWidth + (escapementArray[left] * size)) < mid)
leftWidth += (escapementArray[left++] * size);
float halfWidth = (width - ellipsisWidth) / 2.0; if (left == numChars)
float leftWidth = 0.0, rightWidth = 0.0;
uint32 left, right;
// coming from left...
for (left = 0; left < numChars; left++) {
if (leftWidth + escapementArray[left] > halfWidth)
break;
leftWidth += escapementArray[left];
}
if (left == numChars) {
// string is smaller than half of the maximum width
return false;
}
// check if the whole string fits in
float stringWidth = leftWidth;
uint32 i = left;
for (; i < numChars && stringWidth < width; i++) {
stringWidth += escapementArray[i];
}
if (stringWidth < width)
return false; return false;
// coming from right... float rightWidth = 0.0;
uint32 right = numChars;
while (right > left && (rightWidth + (escapementArray[right - 1] * size)) < mid)
rightWidth += (escapementArray[--right] * size);
for (right = numChars; right-- > left; ) { if (left >= right)
if (rightWidth + escapementArray[right] > halfWidth)
break;
rightWidth += escapementArray[right];
}
if (left >= right) {
// string is smaller than the maximum width
return false; return false;
}
if (left == 0 || right >= numChars - 1) { float stringWidth = leftWidth + rightWidth;
// there is no space for the ellipsis for (uint32 i = left; i < right; ++i)
stringWidth += (escapementArray[i] * size);
if (stringWidth <= width)
return false;
// if there is no space for the ellipsis
if (width < ellipsisWidth) {
strcpy(dest, ""); strcpy(dest, "");
return true; return true;
} }
// see if the gap between left/right is smaller than the ellipsis
float totalWidth = rightWidth + leftWidth;
for (uint32 i = left; i < right; i++) {
totalWidth += escapementArray[i];
if (totalWidth > width)
break;
}
if (totalWidth <= width) {
// the whole string fits!
return false;
}
// The ellipsis now definitely fits, but let's // The ellipsis now definitely fits, but let's
// see if we can add another character // see if we can add another character
float gap = width - (leftWidth + ellipsisWidth + rightWidth);
totalWidth = ellipsisWidth + rightWidth + leftWidth;
if (left > numChars - right) { if (left > numChars - right) {
// try right letter first // try right letter first
if (escapementArray[right] + totalWidth <= width) if (escapementArray[right - 1] * size <= gap) {
right--; right--;
else if (escapementArray[left] + totalWidth <= width) } else if (escapementArray[left + 1] * size <= gap) {
left++; left++;
}
} else { } else {
// try left letter first // try left letter first
if (escapementArray[left] + totalWidth <= width) if (escapementArray[left + 1] * size <= gap) {
left++; left++;
else if (escapementArray[right] + totalWidth <= width) } else if (escapementArray[right - 1] * size <= gap) {
right--; right--;
} }
}
// copy characters // copy characters