1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-15 19:13:07 +01:00

Refactor ShortenPath

This commit is contained in:
ζeh Matt
2023-08-10 14:39:38 +03:00
parent 67d51c3584
commit 39458a3df3
2 changed files with 16 additions and 33 deletions

View File

@@ -1075,45 +1075,28 @@ void GfxDrawStringWithYOffsets(
dpi.lastStringPos = { info.x, info.y };
}
void ShortenPath(utf8* buffer, size_t bufferSize, const utf8* path, int32_t availableWidth, FontStyle fontStyle)
u8string ShortenPath(const u8string& path, int32_t availableWidth, FontStyle fontStyle)
{
size_t length = strlen(path);
// Return full string if it fits
if (GfxGetStringWidth(const_cast<char*>(path), fontStyle) <= availableWidth)
if (GfxGetStringWidth(path, fontStyle) <= availableWidth)
{
SafeStrCpy(buffer, path, bufferSize);
return;
return path;
}
// Count path separators
int32_t path_separators = 0;
for (size_t x = 0; x < length; x++)
u8string shortenedPath = u8"...";
size_t begin = 0;
while (begin < path.size())
{
if (path[x] == *PATH_SEPARATOR || path[x] == '/')
begin = path.find_first_of(*PATH_SEPARATOR, begin + 1);
if (begin == path.npos)
break;
shortenedPath = u8"..." + path.substr(begin);
if (GfxGetStringWidth(shortenedPath, fontStyle) <= availableWidth)
{
path_separators++;
return shortenedPath;
}
}
// TODO: Replace with unicode ellipsis when supported
SafeStrCpy(buffer, "...", bufferSize);
// Abbreviate beginning with xth separator
int32_t begin = -1;
for (int32_t x = 0; x < path_separators; x++)
{
do
{
begin++;
} while (path[begin] != *PATH_SEPARATOR && path[begin] != '/');
SafeStrCpy(buffer + 3, path + begin, bufferSize - 3);
if (GfxGetStringWidth(buffer, fontStyle) <= availableWidth)
{
return;
}
}
SafeStrCpy(buffer, path, bufferSize);
return shortenedPath;
}

View File

@@ -572,7 +572,7 @@ int32_t GfxGetStringWidthNewLined(std::string_view text, FontStyle fontStyle);
int32_t GfxGetStringWidthNoFormatting(std::string_view text, FontStyle fontStyle);
int32_t StringGetHeightRaw(std::string_view text, FontStyle fontStyle);
int32_t GfxClipString(char* buffer, int32_t width, FontStyle fontStyle);
void ShortenPath(utf8* buffer, size_t bufferSize, const utf8* path, int32_t availableWidth, FontStyle fontStyle);
u8string ShortenPath(const u8string& path, int32_t availableWidth, FontStyle fontStyle);
void TTFDrawString(
DrawPixelInfo& dpi, const_utf8string text, int32_t colour, const ScreenCoordsXY& coords, bool noFormatting,
FontStyle fontStyle, TextDarkness darkness);