diff --git a/src/openrct2/drawing/Drawing.String.cpp b/src/openrct2/drawing/Drawing.String.cpp index 971dec3907..45ad5dab29 100644 --- a/src/openrct2/drawing/Drawing.String.cpp +++ b/src/openrct2/drawing/Drawing.String.cpp @@ -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(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; } diff --git a/src/openrct2/drawing/Drawing.h b/src/openrct2/drawing/Drawing.h index 35da0cfd23..de2196b1d2 100644 --- a/src/openrct2/drawing/Drawing.h +++ b/src/openrct2/drawing/Drawing.h @@ -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);