Start getting string_view into WordWrapper

This commit is contained in:
Henrik Rydgård 2024-05-24 12:39:20 +02:00
parent d39d4270e1
commit f6ca8101e0
6 changed files with 14 additions and 13 deletions

View File

@ -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)) {

View File

@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <string_view>
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);

View File

@ -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.)

View File

@ -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;
}

View File

@ -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_;
};

View File

@ -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 ' ':