From f6ca8101e05e5eb591c988743a18afea6ceaa1a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 24 May 2024 12:39:20 +0200 Subject: [PATCH] Start getting string_view into WordWrapper --- Common/Data/Text/WrapText.cpp | 8 ++++---- Common/Data/Text/WrapText.h | 3 ++- Common/Render/DrawBuffer.cpp | 6 +++--- Common/Render/Text/draw_text.cpp | 4 ++-- Common/Render/Text/draw_text.h | 2 +- unittest/UnitTest.cpp | 4 ++-- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Common/Data/Text/WrapText.cpp b/Common/Data/Text/WrapText.cpp index 3f9474e8ca..4da742f5be 100644 --- a/Common/Data/Text/WrapText.cpp +++ b/Common/Data/Text/WrapText.cpp @@ -167,7 +167,7 @@ void WordWrapper::AppendWord(int endIndex, int lastChar, bool addNewline) { if (lastLineStart_ != out_.size()) { // To account for kerning around spaces, we recalculate the entire line width. - x_ = MeasureWidth(out_.c_str() + lastLineStart_, out_.size() - lastLineStart_); + x_ = MeasureWidth(std::string_view(out_.c_str() + lastLineStart_, out_.size() - lastLineStart_)); } else { x_ = 0.0f; } @@ -179,7 +179,7 @@ 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(str_, len) <= maxW_) { + if (MeasureWidth(std::string_view(str_, len)) <= maxW_) { // If it fits, we don't need to go through each character. out_ = str_; return; @@ -190,7 +190,7 @@ void WordWrapper::Wrap() { out_.reserve(len + len / 16); if (flags_ & FLAG_ELLIPSIZE_TEXT) { - ellipsisWidth_ = MeasureWidth("...", 3); + ellipsisWidth_ = MeasureWidth("..."); } for (UTF8 utf(str_); !utf.end(); ) { @@ -219,7 +219,7 @@ void WordWrapper::Wrap() { } // Measure the entire word for kerning purposes. May not be 100% perfect. - float newWordWidth = MeasureWidth(str_ + lastIndex_, afterIndex - lastIndex_); + float newWordWidth = MeasureWidth(std::string_view(str_ + 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 d51f860362..98ec8d3159 100644 --- a/Common/Data/Text/WrapText.h +++ b/Common/Data/Text/WrapText.h @@ -1,6 +1,7 @@ #pragma once #include +#include class WordWrapper { public: @@ -12,7 +13,7 @@ public: std::string Wrapped(); protected: - virtual float MeasureWidth(const char *str, size_t bytes) = 0; + virtual float MeasureWidth(std::string_view str) = 0; void Wrap(); bool WrapBeforeWord(); void AppendWord(int endIndex, int lastChar, bool addNewline); diff --git a/Common/Render/DrawBuffer.cpp b/Common/Render/DrawBuffer.cpp index 31f5232624..0c3ec5fc40 100644 --- a/Common/Render/DrawBuffer.cpp +++ b/Common/Render/DrawBuffer.cpp @@ -450,15 +450,15 @@ public: } protected: - float MeasureWidth(const char *str, size_t bytes) override; + float MeasureWidth(std::string_view str) override; const AtlasFont &atlasfont_; const float scale_; }; -float AtlasWordWrapper::MeasureWidth(const char *str, size_t bytes) { +float AtlasWordWrapper::MeasureWidth(std::string_view str) { float w = 0.0f; - for (UTF8 utf(str); utf.byteIndex() < (int)bytes; ) { + for (UTF8 utf(str); !utf.end(); ) { uint32_t c = utf.next(); if (c == '&') { // Skip ampersand prefixes ("&&" is an ampersand.) diff --git a/Common/Render/Text/draw_text.cpp b/Common/Render/Text/draw_text.cpp index cb09dbda98..44fb0f2493 100644 --- a/Common/Render/Text/draw_text.cpp +++ b/Common/Render/Text/draw_text.cpp @@ -20,9 +20,9 @@ TextDrawer::TextDrawer(Draw::DrawContext *draw) : draw_(draw) { TextDrawer::~TextDrawer() { } -float TextDrawerWordWrapper::MeasureWidth(const char *str, size_t bytes) { +float TextDrawerWordWrapper::MeasureWidth(std::string_view str) { float w, h; - drawer_->MeasureString(str, bytes, &w, &h); + drawer_->MeasureString(str.data(), str.length(), &w, &h); return w; } diff --git a/Common/Render/Text/draw_text.h b/Common/Render/Text/draw_text.h index 252029b8a3..949fc2a3e8 100644 --- a/Common/Render/Text/draw_text.h +++ b/Common/Render/Text/draw_text.h @@ -94,7 +94,7 @@ public: : WordWrapper(str, maxW, flags), drawer_(drawer) {} protected: - float MeasureWidth(const char *str, size_t bytes) override; + float MeasureWidth(std::string_view str) override; TextDrawer *drawer_; }; diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index 0fa08b8ed8..c3cc9eb276 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -793,10 +793,10 @@ public: } protected: - float MeasureWidth(const char *str, size_t bytes) override { + float MeasureWidth(std::string_view str) override { // Simple case for unit testing. int w = 0; - for (UTF8 utf(str); !utf.end() && (size_t)utf.byteIndex() < bytes; ) { + for (UTF8 utf(str); !utf.end(); ) { uint32_t c = utf.next(); switch (c) { case ' ':