Skip to content

Commit 7b15d45

Browse files
committed
TStaticText: add Unicode support
1 parent b8cedd4 commit 7b15d45

3 files changed

Lines changed: 30 additions & 19 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ void TDrawBuffer::moveBuf(ushort indent, const TScreenCell *source, ushort count
318318
```
319319
In case you happen to have an array of `TScreenCell`, this method allows you to copy them into a `TDrawBuffer`.
320320
321-
There are other useful unicode-aware functions:
321+
There are other useful Unicode-aware functions:
322322
323323
```c++
324324
int cstrlen(const char *s);
@@ -339,7 +339,7 @@ The functions above depend on the following lower-level functions. You will need
339339
```c++
340340
size_t TText::next(TStringView text);
341341
size_t TText::prev(TStringView text, size_t index);
342-
size_t TText::wseek(TStringView text, int count);
342+
size_t TText::wseek(TStringView text, int count, Boolean incRemainder=True);
343343
#ifndef __BORLANDC__
344344
void TText::eat(TScreenCell *cell, size_t n, size_t &width, TStringView text, size_t &bytes);
345345
void TText::next(TStringView text, size_t &bytes, size_t &width);
@@ -429,6 +429,7 @@ The overload of `moveStr` used here is `TDrawBuffer::moveStr(ushort indent, TStr
429429
Support for creating Unicode-aware views is in place, but some views that are part of the original Turbo Vision library have not been adapted to handle Unicode.
430430

431431
* At least `TFrame`, `THistoryViewer`, `TListViewer` and `TMenuBox` are able to display Unicode text properly.
432-
* Automatic shortcuts in `TMenuBox` won't work with Unicode text, as shortcuts are still compared against `event.keyDown.charScan.charCode`.
433432
* `TInputLine` can display and process Unicode text.
433+
* Word wrapping in `TStaticText` is Unicode-aware.
434+
* Automatic shortcuts in `TMenuBox` won't work with Unicode text, as shortcuts are still compared against `event.keyDown.charScan.charCode`.
434435
* `TEditor` assumes a single-byte encoding both when handling input events and when displaying text. So it won't display UTF-8 but at least it has a consistent behaviour.

‎include/tvision/ttext.h‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class TText {
122122

123123
static size_t next(TStringView text);
124124
static size_t prev(TStringView text, size_t index);
125-
static size_t wseek(TStringView text, int count);
125+
static size_t wseek(TStringView text, int count, Boolean incRemainder=True);
126126

127127
#ifndef __BORLANDC__
128128
static void eat(TScreenCell *cell, size_t n, size_t &width, TStringView text, size_t &bytes);
@@ -139,14 +139,14 @@ inline size_t TText::next(TStringView text)
139139
return text.size() ? 1 : 0;
140140
}
141141

142-
inline size_t TText::prev(TStringView text, size_t index)
142+
inline size_t TText::prev(TStringView, size_t index)
143143
{
144144
return index ? 1 : 0;
145145
}
146146

147-
inline size_t TText::wseek(TStringView text, int count)
147+
inline size_t TText::wseek(TStringView text, int count, Boolean)
148148
{
149-
return count > 0 ? count : 0;
149+
return count > 0 ? min(count, text.size()) : 0;
150150
}
151151

152152
#else
@@ -182,13 +182,16 @@ inline size_t TText::prev(TStringView text, size_t index)
182182
return 0;
183183
}
184184

185-
inline size_t TText::wseek(TStringView text, int count)
185+
inline size_t TText::wseek(TStringView text, int count, Boolean incRemainder)
186186
// Seeks a string by an amount of display columns ('count'). If that amount
187-
// partially overlaps a multi-column character, the whole character is included.
187+
// partially overlaps a multi-column character, the whole character is included,
188+
// unless 'incRemainder' is False.
188189
// Returns the number of bytes seeked.
189190
{
190191
size_t index = 0, remainder = 0;
191192
wseek(text, index, remainder, count);
193+
if (!incRemainder && remainder)
194+
index -= TText::prev(text, index);
192195
return index;
193196
}
194197

@@ -277,14 +280,19 @@ inline void TText::wseek(TStringView text, size_t &index, size_t &remainder, int
277280
// * count: number of columns to seek.
278281
{
279282
if (count > 0) {
280-
while (count > 0 && index < text.size()) {
283+
while (index < text.size()) {
281284
size_t width = 0;
282285
TText::next({&text[index], text.size() - index}, index, width);
283286
count -= width;
287+
if (count <= 0) {
288+
// Immediately return when the requested width is exceeded.
289+
remainder = -count;
290+
return;
291+
}
284292
}
285-
remainder = -count;
286-
} else
287-
remainder = 0;
293+
}
294+
// No remainder when the end of string was reached.
295+
remainder = 0;
288296
}
289297

290298
#endif // __BORLANDC__

‎source/tvision/tstatict.cpp‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,27 @@ void TStaticText::draw()
6565
++p;
6666
}
6767
i = p;
68+
int last = i + TText::wseek(TStringView(&s[i], l-i), size.x, False);
6869
do {
6970
j = p;
7071
while ((p < l) && (s[p] == ' '))
7172
++p;
7273
while ((p < l) && (s[p] != ' ') && (s[p] != '\n'))
73-
++p;
74-
} while ((p < l) && (p < i + size.x) && (s[p] != '\n'));
75-
if (p > i + size.x)
74+
p += TText::next(TStringView(&s[p], l-p));
75+
} while ((p < l) && (p < last) && (s[p] != '\n'));
76+
if (p > last)
7677
{
7778
if (j > i)
7879
p = j;
7980
else
80-
p = i + size.x;
81+
p = last;
8182
}
83+
int width = strwidth(TStringView(&s[i], p-i));
8284
if (center == True)
83-
j = (size.x - p + i) / 2 ;
85+
j = (size.x - width) / 2 ;
8486
else
8587
j = 0;
86-
b.moveBuf(j, &s[i], color, (p - i));
88+
b.moveStr(j, TStringView(&s[i], l-i), color, (ushort) width);
8789
while ((p < l) && (s[p] == ' '))
8890
p++;
8991
if ((p < l) && (s[p] == '\n'))

0 commit comments

Comments
 (0)