diff --git a/Common/File/VFS/VFS.cpp b/Common/File/VFS/VFS.cpp index 0b6636d83e..0923a750f3 100644 --- a/Common/File/VFS/VFS.cpp +++ b/Common/File/VFS/VFS.cpp @@ -25,7 +25,7 @@ void VFS::Clear() { } // TODO: Use Path more. -static bool IsLocalAbsolutePath(const char *path) { +static bool IsLocalAbsolutePath(std::string_view path) { bool isUnixLocal = path[0] == '/'; #ifdef _WIN32 bool isWindowsLocal = (isalpha(path[0]) && path[1] == ':') || startsWith(path, "\\\\") || startsWith(path, "//"); diff --git a/Common/Render/Text/draw_text.cpp b/Common/Render/Text/draw_text.cpp index ee618ba969..6b59ab0181 100644 --- a/Common/Render/Text/draw_text.cpp +++ b/Common/Render/Text/draw_text.cpp @@ -22,7 +22,7 @@ TextDrawer::~TextDrawer() { float TextDrawerWordWrapper::MeasureWidth(std::string_view str) { float w, h; - drawer_->MeasureString(str.data(), str.length(), &w, &h); + drawer_->MeasureString(str, &w, &h); return w; } @@ -46,7 +46,7 @@ float TextDrawer::CalculateDPIScale() { return scale; } -void TextDrawer::DrawStringRect(DrawBuffer &target, const char *str, const Bounds &bounds, uint32_t color, int align) { +void TextDrawer::DrawStringRect(DrawBuffer &target, std::string_view str, const Bounds &bounds, uint32_t color, int align) { float x = bounds.x; float y = bounds.y; if (align & ALIGN_HCENTER) { @@ -60,7 +60,7 @@ void TextDrawer::DrawStringRect(DrawBuffer &target, const char *str, const Bound y = bounds.y2(); } - std::string toDraw = str; + std::string toDraw(str); int wrap = align & (FLAG_WRAP_TEXT | FLAG_ELLIPSIZE_TEXT); if (wrap) { bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0; @@ -70,8 +70,8 @@ void TextDrawer::DrawStringRect(DrawBuffer &target, const char *str, const Bound DrawString(target, toDraw.c_str(), x, y, color, align); } -void TextDrawer::DrawStringBitmapRect(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, const Bounds &bounds, int align) { - std::string toDraw = str; +void TextDrawer::DrawStringBitmapRect(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, const Bounds &bounds, int align) { + std::string toDraw(str); int wrap = align & (FLAG_WRAP_TEXT | FLAG_ELLIPSIZE_TEXT); if (wrap) { bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0; diff --git a/Common/Render/Text/draw_text.h b/Common/Render/Text/draw_text.h index 3558733848..a6734b7e12 100644 --- a/Common/Render/Text/draw_text.h +++ b/Common/Render/Text/draw_text.h @@ -44,12 +44,12 @@ public: virtual uint32_t SetFont(const char *fontName, int size, int flags) = 0; virtual void SetFont(uint32_t fontHandle) = 0; // Shortcut once you've set the font once. void SetFontScale(float xscale, float yscale); - virtual void MeasureString(const char *str, size_t len, float *w, float *h) = 0; - virtual void MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) = 0; - virtual void DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) = 0; - void DrawStringRect(DrawBuffer &target, const char *str, const Bounds &bounds, uint32_t color, int align); - virtual void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align = ALIGN_TOPLEFT) = 0; - void DrawStringBitmapRect(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, const Bounds &bounds, int align); + virtual void MeasureString(std::string_view str, float *w, float *h) = 0; + virtual void MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) = 0; + virtual void DrawString(DrawBuffer &target, std::string_view str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) = 0; + void DrawStringRect(DrawBuffer &target, std::string_view str, const Bounds &bounds, uint32_t color, int align); + virtual void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align = ALIGN_TOPLEFT) = 0; + void DrawStringBitmapRect(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, const Bounds &bounds, int align); // Use for housekeeping like throwing out old strings. virtual void OncePerFrame() = 0; diff --git a/Common/Render/Text/draw_text_android.cpp b/Common/Render/Text/draw_text_android.cpp index d98987ccea..5abb8471a3 100644 --- a/Common/Render/Text/draw_text_android.cpp +++ b/Common/Render/Text/draw_text_android.cpp @@ -82,14 +82,14 @@ std::string TextDrawerAndroid::NormalizeString(std::string str) { return ReplaceAll(str, "&&", "&"); } -void TextDrawerAndroid::MeasureString(const char *str, size_t len, float *w, float *h) { - if (!str) { +void TextDrawerAndroid::MeasureString(std::string_view str, float *w, float *h) { + if (str.empty()) { *w = 0.0; *h = 0.0; return; } - CacheKey key{ std::string(str, len), fontHash_ }; + CacheKey key{ std::string(str), fontHash_ }; TextMeasureEntry *entry; auto iter = sizeCache_.find(key); if (iter != sizeCache_.end()) { @@ -102,7 +102,7 @@ void TextDrawerAndroid::MeasureString(const char *str, size_t len, float *w, flo } else { ERROR_LOG(G3D, "Missing font"); } - std::string text(NormalizeString(std::string(str, len))); + std::string text(NormalizeString(std::string(str))); auto env = getEnv(); jstring jstr = env->NewStringUTF(text.c_str()); uint32_t size = env->CallStaticIntMethod(cls_textRenderer, method_measureText, jstr, scaledSize); @@ -118,7 +118,12 @@ void TextDrawerAndroid::MeasureString(const char *str, size_t len, float *w, flo *h = entry->height * fontScaleY_ * dpiScale_; } -void TextDrawerAndroid::MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align) { +void TextDrawerAndroid::MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align) { + if (str.empty()) { + *w = 0.0; + *h = 0.0; + return; + } double scaledSize = 14; auto iter = fontMap_.find(fontHash_); if (iter != fontMap_.end()) { @@ -127,7 +132,7 @@ void TextDrawerAndroid::MeasureStringRect(const char *str, size_t len, const Bou ERROR_LOG(G3D, "Missing font"); } - std::string toMeasure = std::string(str, len); + std::string toMeasure = std::string(str); int wrap = align & (FLAG_WRAP_TEXT | FLAG_ELLIPSIZE_TEXT); if (wrap) { bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0; @@ -169,8 +174,8 @@ void TextDrawerAndroid::MeasureStringRect(const char *str, size_t len, const Bou *h = total_h * fontScaleY_ * dpiScale_; } -void TextDrawerAndroid::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align) { - if (!strlen(str)) { +void TextDrawerAndroid::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align) { + if (str.empty()) { bitmapData.clear(); return; } @@ -184,7 +189,7 @@ void TextDrawerAndroid::DrawStringBitmap(std::vector &bitmapData, TextS } auto env = getEnv(); - jstring jstr = env->NewStringUTF(str); + jstring jstr = env->NewStringUTF(std::string(str).c_str()); uint32_t textSize = env->CallStaticIntMethod(cls_textRenderer, method_measureText, jstr, size); int imageWidth = (short)(textSize >> 16); int imageHeight = (short)(textSize & 0xFFFF); @@ -241,9 +246,9 @@ void TextDrawerAndroid::DrawStringBitmap(std::vector &bitmapData, TextS env->DeleteLocalRef(imageData); } -void TextDrawerAndroid::DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align) { +void TextDrawerAndroid::DrawString(DrawBuffer &target, std::string_view str, float x, float y, uint32_t color, int align) { using namespace Draw; - if (!str) + if (str.empty()) return; std::string text(NormalizeString(std::string(str))); @@ -270,7 +275,7 @@ void TextDrawerAndroid::DrawString(DrawBuffer &target, const char *str, float x, TextureDesc desc{}; std::vector bitmapData; - DrawStringBitmap(bitmapData, *entry, texFormat, text.c_str(), align); + DrawStringBitmap(bitmapData, *entry, texFormat, text, align); desc.initData.push_back(&bitmapData[0]); desc.type = TextureType::LINEAR2D; diff --git a/Common/Render/Text/draw_text_android.h b/Common/Render/Text/draw_text_android.h index f4b7e431e6..430db90d2b 100644 --- a/Common/Render/Text/draw_text_android.h +++ b/Common/Render/Text/draw_text_android.h @@ -21,10 +21,10 @@ public: bool IsReady() const override; uint32_t SetFont(const char *fontName, int size, int flags) override; void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. - void MeasureString(const char *str, size_t len, float *w, float *h) override; - void MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override; - void DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override; - void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align = ALIGN_TOPLEFT) override; + void MeasureString(std::string_view str, float *w, float *h) override; + void MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override; + void DrawString(DrawBuffer &target, std::string_view str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override; + void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align = ALIGN_TOPLEFT) override; // Use for housekeeping like throwing out old strings. void OncePerFrame() override; diff --git a/Common/Render/Text/draw_text_qt.cpp b/Common/Render/Text/draw_text_qt.cpp index ce0600cb81..ebcddcc1b7 100644 --- a/Common/Render/Text/draw_text_qt.cpp +++ b/Common/Render/Text/draw_text_qt.cpp @@ -51,8 +51,8 @@ void TextDrawerQt::SetFont(uint32_t fontHandle) { } } -void TextDrawerQt::MeasureString(const char *str, size_t len, float *w, float *h) { - CacheKey key{ std::string(str, len), fontHash_ }; +void TextDrawerQt::MeasureString(std::string_view str, float *w, float *h) { + CacheKey key{ std::string(str), fontHash_ }; TextMeasureEntry *entry; auto iter = sizeCache_.find(key); @@ -61,7 +61,7 @@ void TextDrawerQt::MeasureString(const char *str, size_t len, float *w, float *h } else { QFont* font = fontMap_.find(fontHash_)->second; QFontMetrics fm(*font); - QSize size = fm.size(0, QString::fromUtf8(str, (int)len)); + QSize size = fm.size(0, QString::fromUtf8(str.data(), str.length())); entry = new TextMeasureEntry(); entry->width = size.width(); @@ -74,8 +74,8 @@ void TextDrawerQt::MeasureString(const char *str, size_t len, float *w, float *h *h = entry->height * fontScaleY_ * dpiScale_; } -void TextDrawerQt::MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align) { - std::string toMeasure = std::string(str, len); +void TextDrawerQt::MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align) { + std::string toMeasure = std::string(str); int wrap = align & (FLAG_WRAP_TEXT | FLAG_ELLIPSIZE_TEXT); if (wrap) { bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0; @@ -89,15 +89,15 @@ void TextDrawerQt::MeasureStringRect(const char *str, size_t len, const Bounds & *h = (float)size.height() * fontScaleY_ * dpiScale_; } -void TextDrawerQt::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align) { - if (!strlen(str)) { +void TextDrawerQt::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align) { + if (str.empty()) { bitmapData.clear(); return; } QFont *font = fontMap_.find(fontHash_)->second; QFontMetrics fm(*font); - QSize size = fm.size(0, QString::fromUtf8(str)); + QSize size = fm.size(0, QString::fromUtf8(str.data(), str.length())); QImage image((size.width() + 3) & ~3, (size.height() + 3) & ~3, QImage::Format_ARGB32_Premultiplied); if (image.isNull()) { bitmapData.clear(); @@ -110,7 +110,7 @@ void TextDrawerQt::DrawStringBitmap(std::vector &bitmapData, TextString painter.setFont(*font); painter.setPen(0xFFFFFFFF); // TODO: Involve ALIGN_HCENTER (bounds etc.) - painter.drawText(image.rect(), Qt::AlignTop | Qt::AlignLeft, QString::fromUtf8(str).replace("&&", "&")); + painter.drawText(image.rect(), Qt::AlignTop | Qt::AlignLeft, QString::fromUtf8(str.data(), str.length()).replace("&&", "&")); painter.end(); entry.texture = nullptr; @@ -138,10 +138,11 @@ void TextDrawerQt::DrawStringBitmap(std::vector &bitmapData, TextString } } -void TextDrawerQt::DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align) { +void TextDrawerQt::DrawString(DrawBuffer &target, std::string_view str, float x, float y, uint32_t color, int align) { using namespace Draw; - if (!strlen(str)) + if (str.empty()) { return; + } CacheKey key{ std::string(str), fontHash_ }; target.Flush(true); diff --git a/Common/Render/Text/draw_text_qt.h b/Common/Render/Text/draw_text_qt.h index 47fb8ab48f..453c00f1e8 100644 --- a/Common/Render/Text/draw_text_qt.h +++ b/Common/Render/Text/draw_text_qt.h @@ -16,10 +16,10 @@ public: uint32_t SetFont(const char *fontName, int size, int flags) override; void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. - void MeasureString(const char *str, size_t len, float *w, float *h) override; - void MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override; - void DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override; - void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align = ALIGN_TOPLEFT) override; + void MeasureString(std::string_view str, float *w, float *h) override; + void MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override; + void DrawString(DrawBuffer &target, std::string_view str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override; + void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align = ALIGN_TOPLEFT) override; // Use for housekeeping like throwing out old strings. void OncePerFrame() override; diff --git a/Common/Render/Text/draw_text_sdl.cpp b/Common/Render/Text/draw_text_sdl.cpp index 0fd0ff1817..b8db152c47 100644 --- a/Common/Render/Text/draw_text_sdl.cpp +++ b/Common/Render/Text/draw_text_sdl.cpp @@ -277,8 +277,8 @@ void TextDrawerSDL::SetFont(uint32_t fontHandle) { } } -void TextDrawerSDL::MeasureString(const char *str, size_t len, float *w, float *h) { - CacheKey key{ std::string(str, len), fontHash_ }; +void TextDrawerSDL::MeasureString(std::string_view str, float *w, float *h) { + CacheKey key{ std::string(str), fontHash_ }; TextMeasureEntry *entry; auto iter = sizeCache_.find(key); @@ -312,8 +312,8 @@ void TextDrawerSDL::MeasureString(const char *str, size_t len, float *w, float * *h = entry->height * fontScaleY_ * dpiScale_; } -void TextDrawerSDL::MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align) { - std::string toMeasure = std::string(str, len); +void TextDrawerSDL::MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align) { + std::string toMeasure = std::string(str); int wrap = align & (FLAG_WRAP_TEXT | FLAG_ELLIPSIZE_TEXT); if (wrap) { bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0; @@ -364,9 +364,9 @@ void TextDrawerSDL::MeasureStringRect(const char *str, size_t len, const Bounds *h = total_h * fontScaleY_ * dpiScale_; } -void TextDrawerSDL::DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align) { +void TextDrawerSDL::DrawString(DrawBuffer &target, std::string_view str, float x, float y, uint32_t color, int align) { using namespace Draw; - if (!strlen(str)) + if (str.empty()) return; CacheKey key{ std::string(str), fontHash_ }; @@ -416,8 +416,8 @@ void TextDrawerSDL::DrawString(DrawBuffer &target, const char *str, float x, flo } } -void TextDrawerSDL::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align) { - if (!strlen(str)) { +void TextDrawerSDL::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align) { + if (str.empty()) { bitmapData.clear(); return; } diff --git a/Common/Render/Text/draw_text_sdl.h b/Common/Render/Text/draw_text_sdl.h index af690e1acc..7b960be64e 100644 --- a/Common/Render/Text/draw_text_sdl.h +++ b/Common/Render/Text/draw_text_sdl.h @@ -19,10 +19,10 @@ public: uint32_t SetFont(const char *fontName, int size, int flags) override; void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. - void MeasureString(const char *str, size_t len, float *w, float *h) override; - void MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override; - void DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override; - void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align = ALIGN_TOPLEFT) override; + void MeasureString(std::string_view str, float *w, float *h) override; + void MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override; + void DrawString(DrawBuffer &target, std::string_view str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override; + void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align = ALIGN_TOPLEFT) override; // Use for housekeeping like throwing out old strings. void OncePerFrame() override; diff --git a/Common/Render/Text/draw_text_uwp.cpp b/Common/Render/Text/draw_text_uwp.cpp index 832a0caa03..64cc154b85 100644 --- a/Common/Render/Text/draw_text_uwp.cpp +++ b/Common/Render/Text/draw_text_uwp.cpp @@ -198,8 +198,8 @@ void TextDrawerUWP::SetFont(uint32_t fontHandle) { } } -void TextDrawerUWP::MeasureString(const char *str, size_t len, float *w, float *h) { - CacheKey key{ std::string(str, len), fontHash_ }; +void TextDrawerUWP::MeasureString(std::string_view str, float *w, float *h) { + CacheKey key{ std::string(str), fontHash_ }; TextMeasureEntry *entry; auto iter = sizeCache_.find(key); @@ -213,7 +213,7 @@ void TextDrawerUWP::MeasureString(const char *str, size_t len, float *w, float * } if (!format) return; - std::wstring wstr = ConvertUTF8ToWString(ReplaceAll(ReplaceAll(std::string(str, len), "\n", "\r\n"), "&&", "&")); + std::wstring wstr = ConvertUTF8ToWString(ReplaceAll(ReplaceAll(std::string(str), "\n", "\r\n"), "&&", "&")); format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING); @@ -242,7 +242,7 @@ void TextDrawerUWP::MeasureString(const char *str, size_t len, float *w, float * *h = entry->height * fontScaleY_ * dpiScale_; } -void TextDrawerUWP::MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align) { +void TextDrawerUWP::MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align) { IDWriteTextFormat *format = nullptr; auto iter = fontMap_.find(fontHash_); if (iter != fontMap_.end()) { @@ -254,7 +254,7 @@ void TextDrawerUWP::MeasureStringRect(const char *str, size_t len, const Bounds return; } - std::string toMeasure = std::string(str, len); + std::string toMeasure = std::string(str); int wrap = align & (FLAG_WRAP_TEXT | FLAG_ELLIPSIZE_TEXT); if (wrap) { bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0; @@ -312,8 +312,8 @@ void TextDrawerUWP::MeasureStringRect(const char *str, size_t len, const Bounds *h = total_h * fontScaleY_ * dpiScale_; } -void TextDrawerUWP::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align) { - if (!strlen(str)) { +void TextDrawerUWP::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align) { + if (!str.empty()) { bitmapData.clear(); return; } @@ -439,10 +439,11 @@ void TextDrawerUWP::DrawStringBitmap(std::vector &bitmapData, TextStrin ctx_->mirror_bmp->Unmap(); } -void TextDrawerUWP::DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align) { +void TextDrawerUWP::DrawString(DrawBuffer &target, std::string_view str, float x, float y, uint32_t color, int align) { using namespace Draw; - if (!strlen(str)) + if (str.empty()) { return; + } CacheKey key{ std::string(str), fontHash_ }; target.Flush(true); diff --git a/Common/Render/Text/draw_text_uwp.h b/Common/Render/Text/draw_text_uwp.h index 816ae7ec06..704fea832e 100644 --- a/Common/Render/Text/draw_text_uwp.h +++ b/Common/Render/Text/draw_text_uwp.h @@ -21,10 +21,10 @@ public: uint32_t SetFont(const char *fontName, int size, int flags) override; void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. - void MeasureString(const char *str, size_t len, float *w, float *h) override; - void MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override; - void DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override; - void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align = ALIGN_TOPLEFT) override; + void MeasureString(std::string_view str, float *w, float *h) override; + void MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override; + void DrawString(DrawBuffer &target, std::string_view str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override; + void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align = ALIGN_TOPLEFT) override; // Use for housekeeping like throwing out old strings. void OncePerFrame() override; diff --git a/Common/Render/Text/draw_text_win.cpp b/Common/Render/Text/draw_text_win.cpp index 36ffe8006d..59e5339cad 100644 --- a/Common/Render/Text/draw_text_win.cpp +++ b/Common/Render/Text/draw_text_win.cpp @@ -121,8 +121,8 @@ void TextDrawerWin32::SetFont(uint32_t fontHandle) { } } -void TextDrawerWin32::MeasureString(const char *str, size_t len, float *w, float *h) { - CacheKey key{ std::string(str, len), fontHash_ }; +void TextDrawerWin32::MeasureString(std::string_view str, float *w, float *h) { + CacheKey key{ std::string(str), fontHash_ }; TextMeasureEntry *entry; auto iter = sizeCache_.find(key); @@ -134,7 +134,7 @@ void TextDrawerWin32::MeasureString(const char *str, size_t len, float *w, float SelectObject(ctx_->hDC, iter->second->hFont); } - std::string toMeasure = ReplaceAll(std::string(str, len), "&&", "&"); + std::string toMeasure = ReplaceAll(std::string(str), "&&", "&"); std::vector lines; SplitString(toMeasure, '\n', lines); @@ -161,13 +161,13 @@ void TextDrawerWin32::MeasureString(const char *str, size_t len, float *w, float *h = entry->height * fontScaleY_ * dpiScale_; } -void TextDrawerWin32::MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align) { +void TextDrawerWin32::MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align) { auto iter = fontMap_.find(fontHash_); if (iter != fontMap_.end()) { SelectObject(ctx_->hDC, iter->second->hFont); } - std::string toMeasure = std::string(str, len); + std::string toMeasure = std::string(str); int wrap = align & (FLAG_WRAP_TEXT | FLAG_ELLIPSIZE_TEXT); if (wrap) { bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0; @@ -211,8 +211,8 @@ void TextDrawerWin32::MeasureStringRect(const char *str, size_t len, const Bound *h = total_h * fontScaleY_ * dpiScale_; } -void TextDrawerWin32::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align) { - if (!strlen(str)) { +void TextDrawerWin32::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align) { + if (str.empty()) { bitmapData.clear(); return; } @@ -304,10 +304,11 @@ void TextDrawerWin32::DrawStringBitmap(std::vector &bitmapData, TextStr } } -void TextDrawerWin32::DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align) { +void TextDrawerWin32::DrawString(DrawBuffer &target, std::string_view str, float x, float y, uint32_t color, int align) { using namespace Draw; - if (!strlen(str)) + if (str.empty()) { return; + } CacheKey key{ std::string(str), fontHash_ }; target.Flush(true); diff --git a/Common/Render/Text/draw_text_win.h b/Common/Render/Text/draw_text_win.h index c59fa91db4..468eb25328 100644 --- a/Common/Render/Text/draw_text_win.h +++ b/Common/Render/Text/draw_text_win.h @@ -21,10 +21,10 @@ public: uint32_t SetFont(const char *fontName, int size, int flags) override; void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. - void MeasureString(const char *str, size_t len, float *w, float *h) override; - void MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override; - void DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override; - void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align = ALIGN_TOPLEFT) override; + void MeasureString(std::string_view str, float *w, float *h) override; + void MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override; + void DrawString(DrawBuffer &target, std::string_view str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override; + void DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align = ALIGN_TOPLEFT) override; // Use for housekeeping like throwing out old strings. void OncePerFrame() override; diff --git a/Common/StringUtils.h b/Common/StringUtils.h index 2930d419e5..fb80fc3558 100644 --- a/Common/StringUtils.h +++ b/Common/StringUtils.h @@ -102,6 +102,10 @@ inline size_t truncate_cpy(char(&out)[Count], const char *src) { return truncate_cpy(out, Count, src); } size_t truncate_cpy(char *dest, size_t destSize, std::string_view src); +template +inline size_t truncate_cpy(char(&out)[Count], std::string_view src) { + return truncate_cpy(out, Count, src); +} const char* safe_string(const char* s); diff --git a/Common/UI/AsyncImageFileView.cpp b/Common/UI/AsyncImageFileView.cpp index 3fa627b900..540bf9142b 100644 --- a/Common/UI/AsyncImageFileView.cpp +++ b/Common/UI/AsyncImageFileView.cpp @@ -96,8 +96,8 @@ void AsyncImageFileView::Draw(UIContext &dc) { dc.Flush(); dc.RebindTexture(); if (!text_.empty()) { - dc.DrawText(text_.c_str(), bounds_.centerX() + 1, bounds_.centerY() + 1, 0x80000000, ALIGN_CENTER | FLAG_DYNAMIC_ASCII); - dc.DrawText(text_.c_str(), bounds_.centerX(), bounds_.centerY(), 0xFFFFFFFF, ALIGN_CENTER | FLAG_DYNAMIC_ASCII); + dc.DrawText(text_, bounds_.centerX() + 1, bounds_.centerY() + 1, 0x80000000, ALIGN_CENTER | FLAG_DYNAMIC_ASCII); + dc.DrawText(text_, bounds_.centerX(), bounds_.centerY(), 0xFFFFFFFF, ALIGN_CENTER | FLAG_DYNAMIC_ASCII); } } else { if (!texture_ || texture_->Failed()) { @@ -110,7 +110,7 @@ void AsyncImageFileView::Draw(UIContext &dc) { } } if (!text_.empty()) { - dc.DrawText(text_.c_str(), bounds_.centerX(), bounds_.centerY(), 0xFFFFFFFF, ALIGN_CENTER | FLAG_DYNAMIC_ASCII); + dc.DrawText(text_, bounds_.centerX(), bounds_.centerY(), 0xFFFFFFFF, ALIGN_CENTER | FLAG_DYNAMIC_ASCII); } } } diff --git a/Common/UI/Context.cpp b/Common/UI/Context.cpp index 33c4ef2692..775c01fa18 100644 --- a/Common/UI/Context.cpp +++ b/Common/UI/Context.cpp @@ -215,7 +215,7 @@ void UIContext::MeasureText(const UI::FontStyle &style, float scaleX, float scal } else { textDrawer_->SetFont(style.fontName.c_str(), style.sizePts, style.flags); textDrawer_->SetFontScale(scaleX, scaleY); - textDrawer_->MeasureString(str.data(), str.length(), x, y); + textDrawer_->MeasureString(str, x, y); textDrawer_->SetFont(fontStyle_->fontName.c_str(), fontStyle_->sizePts, fontStyle_->flags); } } @@ -229,13 +229,13 @@ void UIContext::MeasureTextRect(const UI::FontStyle &style, float scaleX, float } else { textDrawer_->SetFont(style.fontName.c_str(), style.sizePts, style.flags); textDrawer_->SetFontScale(scaleX, scaleY); - textDrawer_->MeasureStringRect(str.data(), str.length(), bounds, x, y, align); + textDrawer_->MeasureStringRect(str, bounds, x, y, align); textDrawer_->SetFont(fontStyle_->fontName.c_str(), fontStyle_->sizePts, fontStyle_->flags); } } -void UIContext::DrawText(const char *str, float x, float y, uint32_t color, int align) { - _dbg_assert_(str != nullptr); +void UIContext::DrawText(std::string_view str, float x, float y, uint32_t color, int align) { + _dbg_assert_(str.data() != nullptr); if (!textDrawer_ || (align & FLAG_DYNAMIC_ASCII)) { // Use the font texture if this font is in that texture instead. bool useFontTexture = Draw()->GetFontAtlas()->getFont(fontStyle_->atlasFont) != nullptr; @@ -255,13 +255,13 @@ void UIContext::DrawText(const char *str, float x, float y, uint32_t color, int RebindTexture(); } -void UIContext::DrawTextShadow(const char *str, float x, float y, uint32_t color, int align) { +void UIContext::DrawTextShadow(std::string_view str, float x, float y, uint32_t color, int align) { uint32_t alpha = (color >> 1) & 0xFF000000; DrawText(str, x + 2, y + 2, alpha, align); DrawText(str, x, y, color, align); } -void UIContext::DrawTextRect(const char *str, const Bounds &bounds, uint32_t color, int align) { +void UIContext::DrawTextRect(std::string_view str, const Bounds &bounds, uint32_t color, int align) { if (!textDrawer_ || (align & FLAG_DYNAMIC_ASCII)) { // Use the font texture if this font is in that texture instead. bool useFontTexture = Draw()->GetFontAtlas()->getFont(fontStyle_->atlasFont) != nullptr; @@ -296,7 +296,7 @@ float UIContext::CalculateTextScale(std::string_view str, float availWidth, floa return 1.0f; } -void UIContext::DrawTextRectSqueeze(const char *str, const Bounds &bounds, uint32_t color, int align) { +void UIContext::DrawTextRectSqueeze(std::string_view str, const Bounds &bounds, uint32_t color, int align) { float origScaleX = fontScaleX_; float origScaleY = fontScaleY_; float scale = CalculateTextScale(str, bounds.w / origScaleX, bounds.h / origScaleY); @@ -306,7 +306,7 @@ void UIContext::DrawTextRectSqueeze(const char *str, const Bounds &bounds, uint3 SetFontScale(origScaleX, origScaleY); } -void UIContext::DrawTextShadowRect(const char *str, const Bounds &bounds, uint32_t color, int align) { +void UIContext::DrawTextShadowRect(std::string_view str, const Bounds &bounds, uint32_t color, int align) { uint32_t alpha = (color >> 1) & 0xFF000000; Bounds shadowBounds(bounds.x+2, bounds.y+2, bounds.w, bounds.h); DrawTextRect(str, shadowBounds, alpha, align); diff --git a/Common/UI/Context.h b/Common/UI/Context.h index e1f7e61aa7..849de045fb 100644 --- a/Common/UI/Context.h +++ b/Common/UI/Context.h @@ -84,12 +84,12 @@ public: void SetFontScale(float scaleX, float scaleY); void MeasureText(const UI::FontStyle &style, float scaleX, float scaleY, std::string_view str, float *x, float *y, int align = 0) const; void MeasureTextRect(const UI::FontStyle &style, float scaleX, float scaleY, std::string_view str, const Bounds &bounds, float *x, float *y, int align = 0) const; - void DrawText(const char *str, float x, float y, uint32_t color, int align = 0); - void DrawTextShadow(const char *str, float x, float y, uint32_t color, int align = 0); - void DrawTextRect(const char *str, const Bounds &bounds, uint32_t color, int align = 0); - void DrawTextShadowRect(const char *str, const Bounds &bounds, uint32_t color, int align = 0); + void DrawText(std::string_view str, float x, float y, uint32_t color, int align = 0); + void DrawTextShadow(std::string_view str, float x, float y, uint32_t color, int align = 0); + void DrawTextRect(std::string_view str, const Bounds &bounds, uint32_t color, int align = 0); + void DrawTextShadowRect(std::string_view str, const Bounds &bounds, uint32_t color, int align = 0); // Will squeeze the text into the bounds if needed. - void DrawTextRectSqueeze(const char *str, const Bounds &bounds, uint32_t color, int align = 0); + void DrawTextRectSqueeze(std::string_view str, const Bounds &bounds, uint32_t color, int align = 0); float CalculateTextScale(std::string_view str, float availWidth, float availHeight) const; diff --git a/Common/UI/PopupScreens.cpp b/Common/UI/PopupScreens.cpp index 956b82de26..d29fbf044a 100644 --- a/Common/UI/PopupScreens.cpp +++ b/Common/UI/PopupScreens.cpp @@ -248,9 +248,9 @@ std::string PopupSliderChoice::ValueText() const { char temp[256]; temp[0] = '\0'; if (zeroLabel_.size() && *value_ == 0) { - truncate_cpy(temp, zeroLabel_.c_str()); + truncate_cpy(temp, zeroLabel_); } else if (negativeLabel_.size() && *value_ < 0) { - truncate_cpy(temp, negativeLabel_.c_str()); + truncate_cpy(temp, negativeLabel_); } else { // Would normally be dangerous to have user-controlled format strings! // However, let's check that there's only one % sign, and that it's not followed by an S. @@ -290,7 +290,7 @@ std::string PopupSliderChoiceFloat::ValueText() const { char temp[256]; temp[0] = '\0'; if (zeroLabel_.size() && *value_ == 0.0f) { - truncate_cpy(temp, zeroLabel_.c_str()); + truncate_cpy(temp, zeroLabel_); } else if (IsValidNumberFormatString(fmt_)) { snprintf(temp, sizeof(temp), fmt_.c_str(), *value_); } else { @@ -651,13 +651,13 @@ void AbstractChoiceWithValueDisplay::Draw(UIContext &dc) { } dc.SetFontScale(scale, scale); Bounds valueBounds(bounds_.x2() - textPadding_.right - imagePadding, bounds_.y, w, bounds_.h); - dc.DrawTextRect(valueText.c_str(), valueBounds, style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER | FLAG_WRAP_TEXT); + dc.DrawTextRect(valueText, valueBounds, style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER | FLAG_WRAP_TEXT); dc.SetFontScale(1.0f, 1.0f); } else { Choice::Draw(dc); float scale = CalculateValueScale(dc, valueText, bounds_.w); dc.SetFontScale(scale, scale); - dc.DrawTextRect(valueText.c_str(), bounds_.Expand(-paddingX, 0.0f), style.fgColor, ALIGN_LEFT | ALIGN_VCENTER | FLAG_WRAP_TEXT); + dc.DrawTextRect(valueText, bounds_.Expand(-paddingX, 0.0f), style.fgColor, ALIGN_LEFT | ALIGN_VCENTER | FLAG_WRAP_TEXT); dc.SetFontScale(1.0f, 1.0f); } } diff --git a/Common/UI/View.cpp b/Common/UI/View.cpp index a824776bee..8333c514bf 100644 --- a/Common/UI/View.cpp +++ b/Common/UI/View.cpp @@ -524,14 +524,14 @@ void Choice::Draw(UIContext &dc) { } if (centered_) { - dc.DrawTextRectSqueeze(text_.c_str(), bounds_, style.fgColor, ALIGN_CENTER | FLAG_WRAP_TEXT | drawTextFlags_); + dc.DrawTextRectSqueeze(text_, bounds_, style.fgColor, ALIGN_CENTER | FLAG_WRAP_TEXT | drawTextFlags_); } else { if (rightIconImage_.isValid()) { uint32_t col = rightIconKeepColor_ ? 0xffffffff : style.fgColor; // Don't apply theme to gold icon dc.Draw()->DrawImageRotated(rightIconImage_, bounds_.x2() - 32 - paddingX, bounds_.centerY(), rightIconScale_, rightIconRot_, col, rightIconFlipH_); } Bounds textBounds(bounds_.x + paddingX + textPadding_.left, bounds_.y, availWidth, bounds_.h); - dc.DrawTextRectSqueeze(text_.c_str(), textBounds, style.fgColor, ALIGN_VCENTER | FLAG_WRAP_TEXT | drawTextFlags_); + dc.DrawTextRectSqueeze(text_, textBounds, style.fgColor, ALIGN_VCENTER | FLAG_WRAP_TEXT | drawTextFlags_); } dc.SetFontScale(1.0f, 1.0f); } @@ -583,10 +583,10 @@ void InfoItem::Draw(UIContext &dc) { dc.MeasureTextRect(dc.theme->uiFont, 1.0f, 1.0f, text_, padBounds, &leftWidth, &leftHeight, ALIGN_VCENTER); dc.SetFontStyle(dc.theme->uiFont); - dc.DrawTextRect(text_.c_str(), padBounds, style.fgColor, ALIGN_VCENTER); + dc.DrawTextRect(text_, padBounds, style.fgColor, ALIGN_VCENTER); Bounds rightBounds(padBounds.x + leftWidth, padBounds.y, padBounds.w - leftWidth, padBounds.h); - dc.DrawTextRect(rightText_.c_str(), rightBounds, style.fgColor, ALIGN_VCENTER | ALIGN_RIGHT | FLAG_WRAP_TEXT); + dc.DrawTextRect(rightText_, rightBounds, style.fgColor, ALIGN_VCENTER | ALIGN_RIGHT | FLAG_WRAP_TEXT); } std::string InfoItem::DescribeText() const { @@ -602,7 +602,7 @@ ItemHeader::ItemHeader(std::string_view text, LayoutParams *layoutParams) void ItemHeader::Draw(UIContext &dc) { dc.SetFontStyle(large_ ? dc.theme->uiFont : dc.theme->uiFontSmall); - dc.DrawText(text_.c_str(), bounds_.x + 4, bounds_.centerY(), dc.theme->headerStyle.fgColor, ALIGN_LEFT | ALIGN_VCENTER); + dc.DrawText(text_, bounds_.x + 4, bounds_.centerY(), dc.theme->headerStyle.fgColor, ALIGN_LEFT | ALIGN_VCENTER); dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x, bounds_.y2()-2, bounds_.x2(), bounds_.y2(), dc.theme->headerStyle.fgColor); } @@ -641,7 +641,7 @@ void CollapsibleHeader::Draw(UIContext &dc) { float xoff = 37.0f; dc.SetFontStyle(dc.theme->uiFontSmall); - dc.DrawText(text_.c_str(), bounds_.x + 4 + xoff, bounds_.centerY(), dc.theme->headerStyle.fgColor, ALIGN_LEFT | ALIGN_VCENTER); + dc.DrawText(text_, bounds_.x + 4 + xoff, bounds_.centerY(), dc.theme->headerStyle.fgColor, ALIGN_LEFT | ALIGN_VCENTER); dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x, bounds_.y2() - 2, bounds_.x2(), bounds_.y2(), dc.theme->headerStyle.fgColor); if (hasSubItems_) { dc.Draw()->DrawImageRotated(ImageID("I_ARROW"), bounds_.x + 20.0f, bounds_.y + 20.0f, 1.0f, *toggle_ ? -M_PI / 2 : M_PI); @@ -703,7 +703,7 @@ void PopupHeader::Draw(UIContext &dc) { float tw, th; dc.SetFontStyle(dc.theme->uiFont); - dc.MeasureText(dc.GetFontStyle(), 1.0f, 1.0f, text_.c_str(), &tw, &th, 0); + dc.MeasureText(dc.GetFontStyle(), 1.0f, 1.0f, text_, &tw, &th, 0); float sineWidth = std::max(0.0f, (tw - availableWidth)) / 2.0f; @@ -717,7 +717,7 @@ void PopupHeader::Draw(UIContext &dc) { dc.PushScissor(tb); } - dc.DrawText(text_.c_str(), bounds_.x + tx, bounds_.centerY(), dc.theme->itemStyle.fgColor, ALIGN_LEFT | ALIGN_VCENTER); + dc.DrawText(text_, bounds_.x + tx, bounds_.centerY(), dc.theme->itemStyle.fgColor, ALIGN_LEFT | ALIGN_VCENTER); dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x, bounds_.y2()-2, bounds_.x2(), bounds_.y2(), dc.theme->itemStyle.fgColor); if (availableWidth < tw) { @@ -798,7 +798,7 @@ void CheckBox::Draw(UIContext &dc) { if (!text_.empty()) { Bounds textBounds(bounds_.x + paddingX, bounds_.y, availWidth, bounds_.h); - dc.DrawTextRectSqueeze(text_.c_str(), textBounds, style.fgColor, ALIGN_VCENTER | FLAG_WRAP_TEXT); + dc.DrawTextRectSqueeze(text_, textBounds, style.fgColor, ALIGN_VCENTER | FLAG_WRAP_TEXT); } dc.Draw()->DrawImage(image, bounds_.x2() - paddingX, bounds_.centerY(), 1.0f, style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER); dc.SetFontScale(1.0f, 1.0f); @@ -885,7 +885,7 @@ void Button::GetContentDimensions(const UIContext &dc, float &w, float &h) const if (!text_.empty() && !ignoreText_) { float width = 0.0f; float height = 0.0f; - dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &width, &height); + dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_, &width, &height); w += width; if (imageID_.isValid()) { @@ -922,7 +922,7 @@ void Button::Draw(UIContext &dc) { // dc.Draw()->DrawImage4Grid(style.image, bounds_.x, bounds_.y, bounds_.x2(), bounds_.y2(), style.bgColor); DrawBG(dc, style); float tw, th; - dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &tw, &th); + dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_, &tw, &th); tw *= scale_; th *= scale_; @@ -942,7 +942,7 @@ void Button::Draw(UIContext &dc) { textX += img->w / 2.0f; } } - dc.DrawText(text_.c_str(), textX, bounds_.centerY(), style.fgColor, ALIGN_CENTER); + dc.DrawText(text_, textX, bounds_.centerY(), style.fgColor, ALIGN_CENTER); } dc.SetFontScale(1.0f, 1.0f); @@ -956,7 +956,7 @@ void RadioButton::GetContentDimensions(const UIContext &dc, float &w, float &h) h = 0.0f; if (!text_.empty()) { - dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &w, &h); + dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_, &w, &h); } // Add some internal padding to not look totally ugly @@ -996,7 +996,7 @@ void RadioButton::Draw(UIContext &dc) { dc.Begin(); float tw, th; - dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &tw, &th); + dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_, &tw, &th); if (tw > bounds_.w) { dc.PushScissor(bounds_); @@ -1006,7 +1006,7 @@ void RadioButton::Draw(UIContext &dc) { if (!text_.empty()) { float textX = bounds_.x + paddingW_ * 2.0f + radioRadius_ * 2.0f; - dc.DrawText(text_.c_str(), textX, bounds_.centerY(), style.fgColor, ALIGN_LEFT | ALIGN_VCENTER); + dc.DrawText(text_, textX, bounds_.centerY(), style.fgColor, ALIGN_LEFT | ALIGN_VCENTER); } if (tw > bounds_.w) { @@ -1091,9 +1091,9 @@ void TextView::Draw(UIContext &dc) { if (shadow_) { uint32_t shadowColor = 0x80000000; - dc.DrawTextRect(text_.c_str(), textBounds.Offset(1.0f + pad_, 1.0f + pad_), shadowColor, textAlign_); + dc.DrawTextRect(text_, textBounds.Offset(1.0f + pad_, 1.0f + pad_), shadowColor, textAlign_); } - dc.DrawTextRect(text_.c_str(), textBounds.Offset(pad_, pad_), textColor, textAlign_); + dc.DrawTextRect(text_, textBounds.Offset(pad_, pad_), textColor, textAlign_); if (small_) { // If we changed font style, reset it. dc.SetFontStyle(dc.theme->uiFont); @@ -1133,10 +1133,10 @@ void TextEdit::Draw(UIContext &dc) { if (text_.empty()) { if (placeholderText_.size()) { uint32_t c = textColor & 0x50FFFFFF; - dc.DrawTextRect(placeholderText_.c_str(), bounds_, c, ALIGN_CENTER); + dc.DrawTextRect(placeholderText_, bounds_, c, ALIGN_CENTER); } } else { - dc.DrawTextRect(text_.c_str(), textBounds, textColor, ALIGN_VCENTER | ALIGN_LEFT | align_); + dc.DrawTextRect(text_, textBounds, textColor, ALIGN_VCENTER | ALIGN_LEFT | align_); } if (HasFocus()) { @@ -1156,7 +1156,7 @@ void TextEdit::Draw(UIContext &dc) { } void TextEdit::GetContentDimensions(const UIContext &dc, float &w, float &h) const { - dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, !text_.empty() ? text_.c_str() : "Wj", &w, &h, align_); + dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, !text_.empty() ? text_ : "Wj", &w, &h, align_); w += 2; h += 2; } diff --git a/Core/Util/PPGeDraw.cpp b/Core/Util/PPGeDraw.cpp index bfc2ee64e7..1d733ff4b0 100644 --- a/Core/Util/PPGeDraw.cpp +++ b/Core/Util/PPGeDraw.cpp @@ -785,7 +785,7 @@ void PPGeMeasureText(float *w, float *h, std::string_view text, float scale, int if (WrapType & PPGE_LINE_USE_ELLIPSIS) dtalign |= FLAG_ELLIPSIZE_TEXT; Bounds b(0, 0, wrapWidth <= 0 ? 480.0f : wrapWidth, 272.0f); - textDrawer->MeasureStringRect(s2.c_str(), s2.size(), b, &mw, &mh, dtalign); + textDrawer->MeasureStringRect(s2, b, &mw, &mh, dtalign); if (w) *w = mw; @@ -1119,15 +1119,15 @@ void PPGeDrawTextWrapped(std::string_view text, float x, float y, float wrapWidt Bounds b(0, 0, wrapWidth <= 0 ? 480.0f - x : wrapWidth, wrapHeight); int tdalign = 0; textDrawer->SetFontScale(style.scale, style.scale); - textDrawer->MeasureStringRect(s2.c_str(), s2.size(), b, &actualWidth, &actualHeight, tdalign | FLAG_WRAP_TEXT); + textDrawer->MeasureStringRect(s2, b, &actualWidth, &actualHeight, tdalign | FLAG_WRAP_TEXT); // Check if we need to scale the text down to fit better. PPGeStyle adjustedStyle = style; if (wrapHeight != 0.0f && actualHeight > wrapHeight) { // Cheap way to get the line height. float oneLine, twoLines; - textDrawer->MeasureString("|", 1, &actualWidth, &oneLine); - textDrawer->MeasureStringRect("|\n|", 3, Bounds(0, 0, 480, 272), &actualWidth, &twoLines); + textDrawer->MeasureString("|", &actualWidth, &oneLine); + textDrawer->MeasureStringRect("|\n|", Bounds(0, 0, 480, 272), &actualWidth, &twoLines); float lineHeight = twoLines - oneLine; if (actualHeight > wrapHeight * maxScaleDown) { diff --git a/UI/GPUDriverTestScreen.cpp b/UI/GPUDriverTestScreen.cpp index 326383dd1d..240ee8c97c 100644 --- a/UI/GPUDriverTestScreen.cpp +++ b/UI/GPUDriverTestScreen.cpp @@ -473,9 +473,9 @@ void GPUDriverTestScreen::DiscardTest(UIContext &dc) { std::string apiName = screenManager()->getDrawContext()->GetInfoString(InfoField::APINAME); std::string vendor = screenManager()->getDrawContext()->GetInfoString(InfoField::VENDORSTRING); std::string driver = screenManager()->getDrawContext()->GetInfoString(InfoField::DRIVER); - dc.DrawText(apiName.c_str(), layoutBounds.centerX(), 20, 0xFFFFFFFF, ALIGN_CENTER); - dc.DrawText(vendor.c_str(), layoutBounds.centerX(), 60, 0xFFFFFFFF, ALIGN_CENTER); - dc.DrawText(driver.c_str(), layoutBounds.centerX(), 100, 0xFFFFFFFF, ALIGN_CENTER); + dc.DrawText(apiName, layoutBounds.centerX(), 20, 0xFFFFFFFF, ALIGN_CENTER); + dc.DrawText(vendor, layoutBounds.centerX(), 60, 0xFFFFFFFF, ALIGN_CENTER); + dc.DrawText(driver, layoutBounds.centerX(), 100, 0xFFFFFFFF, ALIGN_CENTER); dc.Flush(); float testW = 170.f; @@ -579,9 +579,9 @@ void GPUDriverTestScreen::ShaderTest(UIContext &dc) { std::string apiName = screenManager()->getDrawContext()->GetInfoString(InfoField::APINAME); std::string vendor = screenManager()->getDrawContext()->GetInfoString(InfoField::VENDORSTRING); std::string driver = screenManager()->getDrawContext()->GetInfoString(InfoField::DRIVER); - dc.DrawText(apiName.c_str(), layoutBounds.centerX(), 20, 0xFFFFFFFF, ALIGN_CENTER); - dc.DrawText(vendor.c_str(), layoutBounds.centerX(), 60, 0xFFFFFFFF, ALIGN_CENTER); - dc.DrawText(driver.c_str(), layoutBounds.centerX(), 100, 0xFFFFFFFF, ALIGN_CENTER); + dc.DrawText(apiName, layoutBounds.centerX(), 20, 0xFFFFFFFF, ALIGN_CENTER); + dc.DrawText(vendor, layoutBounds.centerX(), 60, 0xFFFFFFFF, ALIGN_CENTER); + dc.DrawText(driver, layoutBounds.centerX(), 100, 0xFFFFFFFF, ALIGN_CENTER); dc.Flush(); float y = layoutBounds.y + 150; diff --git a/UI/JoystickHistoryView.cpp b/UI/JoystickHistoryView.cpp index 3aa1828f9c..f82bae8e07 100644 --- a/UI/JoystickHistoryView.cpp +++ b/UI/JoystickHistoryView.cpp @@ -15,7 +15,7 @@ void JoystickHistoryView::Draw(UIContext &dc) { float minRadius = std::min(bounds_.w, bounds_.h) * 0.5f - image->w; dc.Begin(); Bounds textBounds(bounds_.x, bounds_.centerY() + minRadius + 5.0, bounds_.w, bounds_.h / 2 - minRadius - 5.0); - dc.DrawTextShadowRect(title_.c_str(), textBounds, 0xFFFFFFFF, ALIGN_TOP | ALIGN_HCENTER | FLAG_WRAP_TEXT); + dc.DrawTextShadowRect(title_, textBounds, 0xFFFFFFFF, ALIGN_TOP | ALIGN_HCENTER | FLAG_WRAP_TEXT); dc.Flush(); dc.BeginNoTex(); dc.Draw()->RectOutline(bounds_.centerX() - minRadius, bounds_.centerY() - minRadius, minRadius * 2.0f, minRadius * 2.0f, 0x80FFFFFF); diff --git a/UI/MainScreen.cpp b/UI/MainScreen.cpp index 5ae17fc84b..a766dd72f4 100644 --- a/UI/MainScreen.cpp +++ b/UI/MainScreen.cpp @@ -333,7 +333,7 @@ void GameButton::Draw(UIContext &dc) { dc.PushScissor(bounds_); const std::string currentTitle = ginfo->GetTitle(); dc.SetFontScale(0.6f, 0.6f); - dc.DrawText(title_.c_str(), bounds_.x + 4.0f, bounds_.centerY(), style.fgColor, ALIGN_VCENTER | ALIGN_LEFT); + dc.DrawText(title_, bounds_.x + 4.0f, bounds_.centerY(), style.fgColor, ALIGN_VCENTER | ALIGN_LEFT); dc.SetFontScale(1.0f, 1.0f); title_ = currentTitle; dc.Draw()->Flush(); @@ -348,15 +348,15 @@ void GameButton::Draw(UIContext &dc) { title_ = ReplaceAll(title_, "\n", " "); } - dc.MeasureText(dc.GetFontStyle(), 1.0f, 1.0f, title_.c_str(), &tw, &th, 0); + dc.MeasureText(dc.GetFontStyle(), 1.0f, 1.0f, title_, &tw, &th, 0); int availableWidth = bounds_.w - 150; if (g_Config.bShowIDOnGameIcon) { float vw, vh; - dc.MeasureText(dc.GetFontStyle(), 0.7f, 0.7f, ginfo->id_version.c_str(), &vw, &vh, 0); + dc.MeasureText(dc.GetFontStyle(), 0.7f, 0.7f, ginfo->id_version, &vw, &vh, 0); availableWidth -= vw + 20; dc.SetFontScale(0.7f, 0.7f); - dc.DrawText(ginfo->id_version.c_str(), bounds_.x + availableWidth + 160, bounds_.centerY(), style.fgColor, ALIGN_VCENTER); + dc.DrawText(ginfo->id_version, bounds_.x + availableWidth + 160, bounds_.centerY(), style.fgColor, ALIGN_VCENTER); dc.SetFontScale(1.0f, 1.0f); } float sineWidth = std::max(0.0f, (tw - availableWidth)) / 2.0f; @@ -369,7 +369,7 @@ void GameButton::Draw(UIContext &dc) { tb.w = availableWidth; dc.PushScissor(tb); } - dc.DrawText(title_.c_str(), bounds_.x + tx, bounds_.centerY(), style.fgColor, ALIGN_VCENTER); + dc.DrawText(title_, bounds_.x + tx, bounds_.centerY(), style.fgColor, ALIGN_VCENTER); if (availableWidth < tw) { dc.PopScissor(); } @@ -378,7 +378,7 @@ void GameButton::Draw(UIContext &dc) { } else if (!texture) { dc.Draw()->Flush(); dc.PushScissor(bounds_); - dc.DrawText(title_.c_str(), bounds_.x + 4, bounds_.centerY(), style.fgColor, ALIGN_VCENTER); + dc.DrawText(title_, bounds_.x + 4, bounds_.centerY(), style.fgColor, ALIGN_VCENTER); dc.Draw()->Flush(); dc.PopScissor(); } else { @@ -416,8 +416,8 @@ void GameButton::Draw(UIContext &dc) { } if (gridStyle_ && g_Config.bShowIDOnGameIcon) { dc.SetFontScale(0.5f*g_Config.fGameGridScale, 0.5f*g_Config.fGameGridScale); - dc.DrawText(ginfo->id_version.c_str(), x+5, y+1, 0xFF000000, ALIGN_TOPLEFT); - dc.DrawText(ginfo->id_version.c_str(), x+4, y, dc.theme->infoStyle.fgColor, ALIGN_TOPLEFT); + dc.DrawText(ginfo->id_version, x+5, y+1, 0xFF000000, ALIGN_TOPLEFT); + dc.DrawText(ginfo->id_version, x+4, y, dc.theme->infoStyle.fgColor, ALIGN_TOPLEFT); dc.SetFontScale(1.0f, 1.0f); } if (overlayColor) { @@ -467,7 +467,7 @@ void DirButton::Draw(UIContext &dc) { dc.FillRect(style.background, bounds_); - std::string text(GetText()); + std::string_view text(GetText()); ImageID image = ImageID("I_FOLDER"); if (text == "..") { @@ -475,7 +475,7 @@ void DirButton::Draw(UIContext &dc) { } float tw, th; - dc.MeasureText(dc.GetFontStyle(), gridStyle_ ? g_Config.fGameGridScale : 1.0, gridStyle_ ? g_Config.fGameGridScale : 1.0, text.c_str(), &tw, &th, 0); + dc.MeasureText(dc.GetFontStyle(), gridStyle_ ? g_Config.fGameGridScale : 1.0, gridStyle_ ? g_Config.fGameGridScale : 1.0, text, &tw, &th, 0); bool compact = bounds_.w < 180 * (gridStyle_ ? g_Config.fGameGridScale : 1.0); @@ -486,7 +486,7 @@ void DirButton::Draw(UIContext &dc) { // No icon, except "up" dc.PushScissor(bounds_); if (image == ImageID("I_FOLDER")) { - dc.DrawText(text.c_str(), bounds_.x + 5, bounds_.centerY(), style.fgColor, ALIGN_VCENTER); + dc.DrawText(text, bounds_.x + 5, bounds_.centerY(), style.fgColor, ALIGN_VCENTER); } else { dc.Draw()->DrawImage(image, bounds_.centerX(), bounds_.centerY(), gridStyle_ ? g_Config.fGameGridScale : 1.0, style.fgColor, ALIGN_CENTER); } @@ -498,7 +498,7 @@ void DirButton::Draw(UIContext &dc) { scissor = true; } dc.Draw()->DrawImage(image, bounds_.x + 72, bounds_.centerY(), 0.88f*(gridStyle_ ? g_Config.fGameGridScale : 1.0), style.fgColor, ALIGN_CENTER); - dc.DrawText(text.c_str(), bounds_.x + 150, bounds_.centerY(), style.fgColor, ALIGN_VCENTER); + dc.DrawText(text, bounds_.x + 150, bounds_.centerY(), style.fgColor, ALIGN_VCENTER); if (scissor) { dc.PopScissor(); diff --git a/UI/OnScreenDisplay.cpp b/UI/OnScreenDisplay.cpp index 2a968456c6..c02e5bcbe0 100644 --- a/UI/OnScreenDisplay.cpp +++ b/UI/OnScreenDisplay.cpp @@ -164,7 +164,7 @@ static void RenderNotice(UIContext &dc, Bounds bounds, float height1, NoticeLeve UI::Drawable backgroundDark = UI::Drawable(colorAlpha(darkenColor(GetNoticeBackgroundColor(level)), alpha)); dc.FillRect(backgroundDark, bottomTextBounds); dc.SetFontScale(extraTextScale, extraTextScale); - dc.DrawTextRect(details.c_str(), bottomTextBounds, foreGround, (align & FLAG_DYNAMIC_ASCII) | ALIGN_LEFT); + dc.DrawTextRect(details, bottomTextBounds, foreGround, (align & FLAG_DYNAMIC_ASCII) | ALIGN_LEFT); } dc.SetFontScale(1.0f, 1.0f); } diff --git a/UI/RetroAchievementScreens.cpp b/UI/RetroAchievementScreens.cpp index 8fc80fc8d8..ac89c999a0 100644 --- a/UI/RetroAchievementScreens.cpp +++ b/UI/RetroAchievementScreens.cpp @@ -13,8 +13,8 @@ #include "UI/BackgroundAudio.h" #include "UI/OnScreenDisplay.h" -static inline const char *DeNull(const char *ptr) { - return ptr ? ptr : ""; +static inline std::string_view DeNull(const char *ptr) { + return ptr ? std::string_view(ptr) : ""; } // Compound view, creating a FileChooserChoice inside. @@ -560,7 +560,7 @@ static void RenderGameAchievementSummary(UIContext &dc, const Bounds &bounds, fl std::string description = Achievements::GetGameAchievementSummary(); dc.SetFontScale(0.66f, 0.66f); - dc.DrawTextRect(description.c_str(), bounds.Inset(iconSpace + 5.0f, 38.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); + dc.DrawTextRect(description, bounds.Inset(iconSpace + 5.0f, 38.0f, 5.0f, 5.0f), fgColor, ALIGN_TOPLEFT); dc.SetFontScale(1.0f, 1.0f); dc.Flush();