Skip to content

Commit

Permalink
Merge pull request #19181 from hrydgard/text-wrap-bugfix
Browse files Browse the repository at this point in the history
Text wrapping bugfixes
  • Loading branch information
hrydgard authored May 25, 2024
2 parents e552ba0 + 750aad2 commit f2b36f0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Common/Data/Encoding/Utf8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "Common/Data/Encoding/Utf8.h"
#include "Common/Data/Encoding/Utf16.h"
#include "Common/Log.h"

// is start of UTF sequence
inline bool isutf(char c) {
Expand Down Expand Up @@ -209,6 +210,7 @@ int u8_charnum(const char *s, int offset)
/* reads the next utf-8 sequence out of a string, updating an index */
uint32_t u8_nextchar(const char *s, int *index, size_t size) {
uint32_t ch = 0;
_dbg_assert_(*index >= 0 && *index < 100000000);
int sz = 0;
int i = *index;
do {
Expand Down Expand Up @@ -372,8 +374,6 @@ int u8_is_locale_utf8(const char *locale)
return 0;
}

UTF8::UTF8(const char *c) : c_(c), size_((int)strlen(c)), index_(0) {}

bool AnyEmojiInString(std::string_view str, size_t byteCount) {
int i = 0;
while (i < byteCount) {
Expand Down
3 changes: 2 additions & 1 deletion Common/Data/Encoding/Utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma once

#include <cstdint>
#include <cstring>
#include <string>
#include <string_view>

Expand All @@ -39,7 +40,7 @@ class UTF8 {
public:
static const uint32_t INVALID = (uint32_t)-1;
// TODO: Try to get rid of this constructor.
explicit UTF8(const char *c);
explicit UTF8(const char *c) : c_(c), size_((int)strlen(c)), index_(0) {}
explicit UTF8(std::string_view view) : c_(view.data()), size_((int)view.size()), index_(0) {}
explicit UTF8(std::string_view view, int index) : c_(view.data()), size_((int)view.size()), index_(index) {}
bool end() const { return index_ == size_; }
Expand Down
5 changes: 4 additions & 1 deletion Common/Data/Text/WrapText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ void WordWrapper::Wrap() {
}

// Measure the entire word for kerning purposes. May not be 100% perfect.
float newWordWidth = MeasureWidth(str_.substr(lastIndex_, afterIndex - lastIndex_));
float newWordWidth = 0.0f;
if (afterIndex <= str_.length()) {
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)) {
Expand Down
7 changes: 5 additions & 2 deletions Common/Render/DrawBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,15 @@ float AtlasWordWrapper::MeasureWidth(std::string_view str) {
uint32_t c = utf.next();
if (c == '&') {
// Skip ampersand prefixes ("&&" is an ampersand.)
if (utf.end()) {
break;
}
c = utf.next();
}
const AtlasChar *ch = atlasfont_.getChar(c);
if (!ch)
if (!ch) {
ch = atlasfont_.getChar('?');

}
w += ch->wx * scale_;
}
return w;
Expand Down

0 comments on commit f2b36f0

Please sign in to comment.