CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Data/Text/WrapText.h
Views: 1401
1
#pragma once
2
3
#include <string>
4
#include <string_view>
5
6
class WordWrapper {
7
public:
8
WordWrapper(std::string_view str, float maxW, int flags)
9
: str_(str), maxW_(maxW), flags_(flags) {
10
}
11
virtual ~WordWrapper() {}
12
13
// TODO: This should return a vector of std::string_view for the lines, instead of building up a new string.
14
std::string Wrapped();
15
16
protected:
17
virtual float MeasureWidth(std::string_view str) = 0;
18
19
void Wrap();
20
bool WrapBeforeWord();
21
void AppendWord(int endIndex, int lastChar, bool addNewline);
22
void AddEllipsis();
23
24
static bool IsCJK(uint32_t c);
25
static bool IsPunctuation(uint32_t c);
26
static bool IsSpace(uint32_t c);
27
static bool IsShy(uint32_t c);
28
static bool IsSpaceOrShy(uint32_t c) {
29
return IsSpace(c) || IsShy(c);
30
}
31
32
const std::string_view str_;
33
const float maxW_;
34
const int flags_;
35
std::string out_;
36
37
// Index of last output / start of current word.
38
int lastIndex_ = 0;
39
// Ideal place to put an ellipsis if we run out of space.
40
int lastEllipsisIndex_ = -1;
41
// Index of last line start.
42
size_t lastLineStart_ = 0;
43
// Last character written to out_.
44
int lastChar_ = 0;
45
// Position the current word starts at.
46
float x_ = 0.0f;
47
// Most recent width of word since last index.
48
float wordWidth_ = 0.0f;
49
// Width of "..." when flag is set, zero otherwise.
50
float ellipsisWidth_ = 0.0f;
51
// Force the next word to cut partially and wrap.
52
bool forceEarlyWrap_ = false;
53
// Skip all characters until the next newline.
54
bool scanForNewline_ = false;
55
// Skip the next word, replaced with ellipsis.
56
bool skipNextWord_ = false;
57
};
58
59