1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 | bool LabelTextFormatter::multilineText(Label *theLabel)
{
auto limit = theLabel->_limitShowCount;
auto strWhole = theLabel->_currentUTF16String;
std::vector<char16_t> multiline_string;
multiline_string.reserve( limit );
std::vector<char16_t> last_word;
last_word.reserve( 25 );
bool isStartOfLine = false, isStartOfWord = false;
float startOfLine = -1, startOfWord = -1;
int skip = 0;
int tIndex = 0;
float scalsX = theLabel->getScaleX();
float lineWidth = theLabel->_maxLineWidth;
bool breakLineWithoutSpace = theLabel->_lineBreakWithoutSpaces;
Label::LetterInfo* info = nullptr;
for (int j = 0; j+skip < limit; j++)
{
info = & theLabel->_lettersInfo.at(j+skip);
unsigned int justSkipped = 0;
while (info->def.validDefinition == false)
{
justSkipped++;
tIndex = j+skip+justSkipped;
if (strWhole[tIndex-1] == '\n')
{
StringUtils::trimUTF16Vector(last_word);
last_word.push_back('\n');
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
last_word.clear();
isStartOfWord = false;
isStartOfLine = false;
startOfWord = -1;
startOfLine = -1;
}
if(tIndex < limit)
{
info = & theLabel->_lettersInfo.at( tIndex );
}
else
break;
}
skip += justSkipped;
tIndex = j + skip;
if (tIndex >= limit)
break;
char16_t character = strWhole[tIndex];
if (!isStartOfWord)
{
startOfWord = info->position.x * scalsX;
isStartOfWord = true;
}
if (!isStartOfLine)
{
startOfLine = startOfWord;
isStartOfLine = true;
}
// 1) Whitespace.
// 2) This character is non-CJK, but the last character is CJK
bool isspace = StringUtils::isUnicodeSpace(character);
bool isCJK = false;
if(!isspace)
{
isCJK = StringUtils::isCJKUnicode(character);
}
if (isspace ||
(!last_word.empty() && StringUtils::isCJKUnicode(last_word.back()) && !isCJK))
{
// if current character is white space, put it into the current word
if (isspace) last_word.push_back(character);
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
last_word.clear();
isStartOfWord = false;
startOfWord = -1;
// put the CJK character in the last word
// and put the non-CJK(ASCII) character in the current word
if (!isspace) last_word.push_back(character);
continue;
}
float posRight = (info->position.x + info->def.xAdvance) * scalsX;
// Out of bounds.
if (posRight - startOfLine > lineWidth)
{
if (!breakLineWithoutSpace && !isCJK)
{
last_word.push_back(character);
int found = StringUtils::getIndexOfLastNotChar16(multiline_string, ' ');
if (found != -1)
StringUtils::trimUTF16Vector(multiline_string);
else
multiline_string.clear();
if (multiline_string.size() > 0)
multiline_string.push_back('\n');
isStartOfLine = false;
startOfLine = -1;
}
else
{
StringUtils::trimUTF16Vector(last_word);
//issue #8492:endless loop if not using system font, and constrained length is less than one character width
if (isStartOfLine && startOfWord == startOfLine && last_word.size() == 0)
last_word.push_back(character);
else
--j;
last_word.push_back('\n');
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
last_word.clear();
isStartOfWord = false;
isStartOfLine = false;
startOfWord = -1;
startOfLine = -1;
}
}
else
{
// Character is normal.
last_word.push_back(character);
}
}
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
std::u16string strNew(multiline_string.begin(), multiline_string.end());
theLabel->_currentUTF16String = strNew;
theLabel->computeStringNumLines();
theLabel->computeHorizontalKernings(theLabel->_currentUTF16String);
return true;
}
|