2016-07-04 10:52:43 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
class WordWrapper {
|
|
|
|
|
public:
|
2020-03-10 19:06:30 -07:00
|
|
|
WordWrapper(const char *str, float maxW, int flags)
|
|
|
|
|
: str_(str), maxW_(maxW), flags_(flags) {
|
2016-07-04 10:52:43 -07:00
|
|
|
}
|
2022-12-10 20:32:12 -08:00
|
|
|
virtual ~WordWrapper() {}
|
2016-07-04 10:52:43 -07:00
|
|
|
|
|
|
|
|
std::string Wrapped();
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual float MeasureWidth(const char *str, size_t bytes) = 0;
|
|
|
|
|
void Wrap();
|
2019-05-25 18:37:36 +02:00
|
|
|
bool WrapBeforeWord();
|
2021-09-25 11:37:10 -07:00
|
|
|
void AppendWord(int endIndex, int lastChar, bool addNewline);
|
2021-09-25 10:58:45 -07:00
|
|
|
void AddEllipsis();
|
2016-07-04 10:52:43 -07:00
|
|
|
|
|
|
|
|
static bool IsCJK(uint32_t c);
|
|
|
|
|
static bool IsPunctuation(uint32_t c);
|
|
|
|
|
static bool IsSpace(uint32_t c);
|
|
|
|
|
static bool IsShy(uint32_t c);
|
2021-09-25 11:37:10 -07:00
|
|
|
static bool IsSpaceOrShy(uint32_t c) {
|
|
|
|
|
return IsSpace(c) || IsShy(c);
|
|
|
|
|
}
|
2016-07-04 10:52:43 -07:00
|
|
|
|
|
|
|
|
const char *const str_;
|
|
|
|
|
const float maxW_;
|
2020-03-10 19:06:30 -07:00
|
|
|
const int flags_;
|
2016-07-04 10:52:43 -07:00
|
|
|
std::string out_;
|
2020-03-10 19:06:30 -07:00
|
|
|
|
2016-07-04 10:52:43 -07:00
|
|
|
// Index of last output / start of current word.
|
2017-06-03 09:39:18 -07:00
|
|
|
int lastIndex_ = 0;
|
2021-09-25 10:58:45 -07:00
|
|
|
// Ideal place to put an ellipsis if we run out of space.
|
|
|
|
|
int lastEllipsisIndex_ = -1;
|
2017-06-03 09:39:18 -07:00
|
|
|
// Index of last line start.
|
2019-05-25 18:37:36 +02:00
|
|
|
size_t lastLineStart_ = 0;
|
2021-09-25 11:37:10 -07:00
|
|
|
// Last character written to out_.
|
|
|
|
|
int lastChar_ = 0;
|
2016-07-04 10:52:43 -07:00
|
|
|
// Position the current word starts at.
|
2017-06-03 09:39:18 -07:00
|
|
|
float x_ = 0.0f;
|
2016-07-04 10:52:43 -07:00
|
|
|
// Most recent width of word since last index.
|
2019-06-18 01:08:25 +02:00
|
|
|
float wordWidth_ = 0.0f;
|
2020-03-10 19:06:30 -07:00
|
|
|
// Width of "..." when flag is set, zero otherwise.
|
|
|
|
|
float ellipsisWidth_ = 0.0f;
|
2016-07-04 10:52:43 -07:00
|
|
|
// Force the next word to cut partially and wrap.
|
2017-06-03 09:39:18 -07:00
|
|
|
bool forceEarlyWrap_ = false;
|
2020-03-10 19:06:30 -07:00
|
|
|
// Skip all characters until the next newline.
|
|
|
|
|
bool scanForNewline_ = false;
|
2021-09-25 09:41:11 -07:00
|
|
|
// Skip the next word, replaced with ellipsis.
|
|
|
|
|
bool skipNextWord_ = false;
|
2016-07-04 10:52:43 -07:00
|
|
|
};
|