diff --git a/Common/Data/Text/WrapText.cpp b/Common/Data/Text/WrapText.cpp index 4da742f5be..68335db1a9 100644 --- a/Common/Data/Text/WrapText.cpp +++ b/Common/Data/Text/WrapText.cpp @@ -141,7 +141,7 @@ void WordWrapper::AppendWord(int endIndex, int lastChar, bool addNewline) { // This will include the newline. if (x_ <= maxW_) { - out_.append(str_ + lastWordStartIndex, str_ + endIndex); + out_.append(str_.data() + lastWordStartIndex, str_.data() + endIndex); } else { scanForNewline_ = true; } @@ -178,10 +178,10 @@ void WordWrapper::AppendWord(int endIndex, int lastChar, bool addNewline) { void WordWrapper::Wrap() { // First, let's check if it fits as-is. - size_t len = strlen(str_); - if (MeasureWidth(std::string_view(str_, len)) <= maxW_) { + size_t len = str_.length(); + if (MeasureWidth(str_) <= maxW_) { // If it fits, we don't need to go through each character. - out_ = str_; + out_ = std::string(str_); return; } @@ -219,7 +219,7 @@ void WordWrapper::Wrap() { } // Measure the entire word for kerning purposes. May not be 100% perfect. - float newWordWidth = MeasureWidth(std::string_view(str_ + lastIndex_, afterIndex - lastIndex_)); + float newWordWidth = MeasureWidth(str_.substr(lastIndex_, afterIndex - lastIndex_)); // Is this the end of a word (space)? We'll also output up to a soft hyphen. if (wordWidth_ > 0.0f && IsSpaceOrShy(c)) { diff --git a/Common/Data/Text/WrapText.h b/Common/Data/Text/WrapText.h index 98ec8d3159..e08e064665 100644 --- a/Common/Data/Text/WrapText.h +++ b/Common/Data/Text/WrapText.h @@ -5,7 +5,7 @@ class WordWrapper { public: - WordWrapper(const char *str, float maxW, int flags) + WordWrapper(std::string_view str, float maxW, int flags) : str_(str), maxW_(maxW), flags_(flags) { } virtual ~WordWrapper() {} @@ -27,7 +27,7 @@ protected: return IsSpace(c) || IsShy(c); } - const char *const str_; + const std::string_view str_; const float maxW_; const int flags_; std::string out_; diff --git a/Common/Render/DrawBuffer.cpp b/Common/Render/DrawBuffer.cpp index 0c3ec5fc40..d2920a7035 100644 --- a/Common/Render/DrawBuffer.cpp +++ b/Common/Render/DrawBuffer.cpp @@ -446,7 +446,8 @@ void DrawBuffer::DrawImage2GridH(ImageID atlas_image, float x1, float y1, float class AtlasWordWrapper : public WordWrapper { public: // Note: maxW may be height if rotated. - AtlasWordWrapper(const AtlasFont &atlasfont, float scale, const char *str, float maxW, int flags) : WordWrapper(str, maxW, flags), atlasfont_(atlasfont), scale_(scale) { + AtlasWordWrapper(const AtlasFont &atlasfont, float scale, std::string_view str, float maxW, int flags) + : WordWrapper(str, maxW, flags), atlasfont_(atlasfont), scale_(scale) { } protected: @@ -535,7 +536,7 @@ void DrawBuffer::MeasureTextRect(FontID font_id, std::string_view text, const Bo *h = 0.0f; return; } - AtlasWordWrapper wrapper(*font, fontscalex, toMeasure.c_str(), bounds.w, wrap); + AtlasWordWrapper wrapper(*font, fontscalex, toMeasure, bounds.w, wrap); toMeasure = wrapper.Wrapped(); } MeasureText(font_id, toMeasure, w, h); @@ -578,7 +579,7 @@ void DrawBuffer::DrawTextRect(FontID font, const char *text, float x, float y, f if (!atlasfont) atlasfont = atlas->getFont(font); if (wrap && atlasfont) { - AtlasWordWrapper wrapper(*atlasfont, fontscalex, toDraw.c_str(), w, wrap); + AtlasWordWrapper wrapper(*atlasfont, fontscalex, toDraw, w, wrap); toDraw = wrapper.Wrapped(); } @@ -610,7 +611,7 @@ void DrawBuffer::DrawTextRect(FontID font, const char *text, float x, float y, f // ROTATE_* doesn't yet work right. void DrawBuffer::DrawText(FontID font, std::string_view text, float x, float y, Color color, int align) { // rough estimate - int textLen = text.length(); + int textLen = (int)text.length(); if (count_ + textLen * 6 > MAX_VERTS) { Flush(true); if (textLen * 6 >= MAX_VERTS) { diff --git a/Common/Render/Text/draw_text.cpp b/Common/Render/Text/draw_text.cpp index 44fb0f2493..ee618ba969 100644 --- a/Common/Render/Text/draw_text.cpp +++ b/Common/Render/Text/draw_text.cpp @@ -26,7 +26,7 @@ float TextDrawerWordWrapper::MeasureWidth(std::string_view str) { return w; } -void TextDrawer::WrapString(std::string &out, const char *str, float maxW, int flags) { +void TextDrawer::WrapString(std::string &out, std::string_view str, float maxW, int flags) { TextDrawerWordWrapper wrapper(this, str, maxW, flags); out = wrapper.Wrapped(); } diff --git a/Common/Render/Text/draw_text.h b/Common/Render/Text/draw_text.h index 949fc2a3e8..3558733848 100644 --- a/Common/Render/Text/draw_text.h +++ b/Common/Render/Text/draw_text.h @@ -67,7 +67,7 @@ protected: Draw::DrawContext *draw_; virtual void ClearCache() = 0; - void WrapString(std::string &out, const char *str, float maxWidth, int flags); + void WrapString(std::string &out, std::string_view str, float maxWidth, int flags); struct CacheKey { bool operator < (const CacheKey &other) const { @@ -90,7 +90,7 @@ protected: class TextDrawerWordWrapper : public WordWrapper { public: - TextDrawerWordWrapper(TextDrawer *drawer, const char *str, float maxW, int flags) + TextDrawerWordWrapper(TextDrawer *drawer, std::string_view str, float maxW, int flags) : WordWrapper(str, maxW, flags), drawer_(drawer) {} protected: diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index c3cc9eb276..e482b6f0e2 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -788,7 +788,7 @@ static bool TestAndroidContentURI() { class UnitTestWordWrapper : public WordWrapper { public: - UnitTestWordWrapper(const char *str, float maxW, int flags) + UnitTestWordWrapper(std::string_view str, float maxW, int flags) : WordWrapper(str, maxW, flags) { }