Made copy_from_start() more clear and probably faster.
Commented out a check that would prevent characters smaller than B_UTF8_ELLIPSIS to be rendered (ie. the maximum width could be less than the ellipsisWidth but greater than the complete string width for small strings). Minor cleanup. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13013 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -735,44 +735,46 @@ do_minimize_team(BRect zoomRect, team_id team, bool zoom)
|
|||||||
// ToDo: implement me, needed for Deskbar!
|
// ToDo: implement me, needed for Deskbar!
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy_from_start
|
|
||||||
|
// #pragma mark - truncate string
|
||||||
|
|
||||||
|
|
||||||
static char*
|
static char*
|
||||||
copy_from_start(const char* src, char* dst, uint32 numChars,
|
copy_from_start(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)
|
||||||
{
|
{
|
||||||
//printf("copy_from_start: %.1f\n", width);
|
|
||||||
float currentWidth = 0.0;
|
float currentWidth = 0.0;
|
||||||
for (uint32 c = 0; c < numChars; c++) {
|
ellipsisWidth /= size; // test if this is as accurate as escapementArray * size
|
||||||
//printf("forward: %c (%ld) (%.1f + %.1f = %.1f)\n", *src, c, currentWidth, escapementArray[c] * size, currentWidth + escapementArray[c] * size);
|
width /= size;
|
||||||
currentWidth += escapementArray[c] * size;
|
uint32 lastFit = 0, c;
|
||||||
if (currentWidth > width) {
|
|
||||||
//*dst = *src;
|
for (c = 0; c < numChars; c++) {
|
||||||
// ups, we definitely don't fit. go back until the ellipsis fits
|
currentWidth += escapementArray[c];
|
||||||
currentWidth += ellipsisWidth;
|
if (currentWidth + ellipsisWidth <= width)
|
||||||
for (int32 c2 = c; c2 >= 0; c2--) {
|
lastFit = c;
|
||||||
//printf(" backward: %c (%ld) (%.1f - %.1f = %.1f)\n", *dst, c2, currentWidth, escapementArray[c2] * size, currentWidth - escapementArray[c2] * size);
|
|
||||||
currentWidth -= escapementArray[c2] * size;
|
if (currentWidth > width)
|
||||||
do {
|
|
||||||
dst--;
|
|
||||||
} while (IsInsideGlyph(*dst));
|
|
||||||
// see if we went back enough
|
|
||||||
if (currentWidth <= width)
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// dst needs to point behind the last glyph we want to use
|
|
||||||
dst++;
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
// copy one glyph
|
|
||||||
do {
|
|
||||||
*dst++ = *src++;
|
|
||||||
} while (IsInsideGlyph(*src));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dst;
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy_from_end
|
if (c < numChars) {
|
||||||
|
// string does not fit into width
|
||||||
|
c = lastFit + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy string to destination
|
||||||
|
|
||||||
|
for (uint32 i = 0; i < c; i++) {
|
||||||
|
// copy one glyph
|
||||||
|
do {
|
||||||
|
*dest++ = *source++;
|
||||||
|
} while (IsInsideGlyph(*source));
|
||||||
|
}
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static char*
|
static char*
|
||||||
copy_from_end(const char* src, char* dst, uint32 numChars, uint32 length,
|
copy_from_end(const char* src, char* dst, uint32 numChars, uint32 length,
|
||||||
const float* escapementArray, float width, float ellipsisWidth, float size)
|
const float* escapementArray, float width, float ellipsisWidth, float size)
|
||||||
@@ -811,24 +813,26 @@ copy_from_end(const char* src, char* dst, uint32 numChars, uint32 length,
|
|||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static char*
|
static char*
|
||||||
write_ellipsis(char* dst)
|
write_ellipsis(char* dst)
|
||||||
{
|
{
|
||||||
strcpy(dst, B_UTF8_ELLIPSIS);
|
strcpy(dst, B_UTF8_ELLIPSIS);
|
||||||
//strcpy(dst, "...");
|
|
||||||
return dst + 3;
|
return dst + 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
// truncated_string
|
|
||||||
void
|
void
|
||||||
truncate_string(const char* string,
|
truncate_string(const char* string, uint32 mode, float width,
|
||||||
uint32 mode, float width, char* result,
|
char* result, const float* escapementArray, float fontSize,
|
||||||
const float* escapementArray, float fontSize,
|
|
||||||
float ellipsisWidth, int32 length, int32 numChars)
|
float ellipsisWidth, int32 length, int32 numChars)
|
||||||
{
|
{
|
||||||
if (string) {
|
// ToDo: that's actually not correct: the string could be smaller than ellipsisWidth
|
||||||
// skip calculation if we don't even have enough room for that
|
if (string == NULL /*|| width < ellipsisWidth*/) {
|
||||||
if (width >= ellipsisWidth) {
|
// we don't have room for a single glyph
|
||||||
|
strcpy(result, "");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// iterate over glyphs and copy source into result string
|
// iterate over glyphs and copy source into result string
|
||||||
// one glyph at a time as long as we have room for the "…" yet
|
// one glyph at a time as long as we have room for the "…" yet
|
||||||
@@ -837,7 +841,6 @@ truncate_string(const char* string,
|
|||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case B_TRUNCATE_BEGINNING: {
|
case B_TRUNCATE_BEGINNING: {
|
||||||
|
|
||||||
dst = copy_from_end(src, dst, numChars, length,
|
dst = copy_from_end(src, dst, numChars, length,
|
||||||
escapementArray, width, ellipsisWidth, fontSize);
|
escapementArray, width, ellipsisWidth, fontSize);
|
||||||
// "dst" points to the position behind the last glyph that
|
// "dst" points to the position behind the last glyph that
|
||||||
@@ -864,9 +867,9 @@ truncate_string(const char* string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
case B_TRUNCATE_END:
|
case B_TRUNCATE_END:
|
||||||
|
dst = copy_from_start(src, dst, numChars, escapementArray,
|
||||||
|
width, ellipsisWidth, fontSize);
|
||||||
|
|
||||||
dst = copy_from_start(src, dst, numChars,
|
|
||||||
escapementArray, width, ellipsisWidth, fontSize);
|
|
||||||
// "dst" points to the position behind the last glyph that
|
// "dst" points to the position behind the last glyph that
|
||||||
// was copied.
|
// was copied.
|
||||||
if (dst - result < length) {
|
if (dst - result < length) {
|
||||||
@@ -896,11 +899,6 @@ truncate_string(const char* string,
|
|||||||
*dst = 0;
|
*dst = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// we don't have room for a single glyph
|
|
||||||
strcpy(result, "");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user