Skip to content

Commit 702114d

Browse files
committed
Add Unicode support in TEditor
It was not that difficult.
1 parent b5ee046 commit 702114d

7 files changed

Lines changed: 188 additions & 53 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ I'm sorry, the root makefile assumes it is executed from the `project` directory
9292
* Ncurses-based terminal support.
9393
* Mouse and key modifiers support on the linux console.
9494
* Overall better display performance than SET's or Sergio Sigala's ports.
95-
* Reads UTF-8 input from the terminal and displays UTF-8 text, but still works with 8-bit ASCII characters internally.
95+
* UTF-8 support both in terminal I/O and the API.
9696
* Implementation of some Borland C++ RTL functions: `findfirst`, `findnext`, `fnsplit`, `_dos_findfirst`, `_dos_findnext`, `getdisk`, `setdisk`, `getcurdir`, `filelenght`.
9797
* Accepts both Unix and Windows-style file paths in 'Open File' dialogs.
9898
* Simple segmentation fault handler that gives you the chance to 'continue running' the application if something goes wrong.
@@ -341,11 +341,9 @@ The functions above depend on the following lower-level functions. You will need
341341
size_t TText::next(TStringView text);
342342
size_t TText::prev(TStringView text, size_t index);
343343
size_t TText::wseek(TStringView text, int count, Boolean incRemainder=True);
344-
#ifndef __BORLANDC__
345344
void TText::eat(TScreenCell *cell, size_t n, size_t &width, TStringView text, size_t &bytes);
346345
void TText::next(TStringView text, size_t &bytes, size_t &width);
347346
void TText::wseek(TStringView text, size_t &index, size_t &remainder, int count);
348-
#endif
349347
```
350348

351349
For drawing `TScreenCell` buffers directly, the following methods are available:
@@ -433,4 +431,4 @@ Support for creating Unicode-aware views is in place, but some views that are pa
433431
* `TInputLine` can display and process Unicode text.
434432
* Word wrapping in `TStaticText` and `THelpViewer` is Unicode-aware.
435433
* Automatic shortcuts in `TMenuBox` won't work with Unicode text, as shortcuts are still compared against `event.keyDown.charScan.charCode`.
436-
* `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.
434+
* `TEditor` instances (as in the `tvedit` application) are in UTF-8 mode by default. Press `Ctrl+P` to switch back to single-byte mode. This only changes how the document is displayed and the encoding of user input; it does not alter the document.

‎include/tvision/editors.h‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ const int
7777
cmIndentMode = 522,
7878
cmUpdateTitle = 523,
7979
cmSelectAll = 524,
80-
cmDelWordLeft = 525;
80+
cmDelWordLeft = 525,
81+
cmEncoding = 526;
8182

8283
const int
8384
edOutOfMemory = 0,
@@ -216,7 +217,7 @@ class TEditor : public TView
216217
void doUpdate();
217218
void doSearchReplace();
218219
void drawLines( int, int, uint );
219-
void formatLine(ushort *, uint, int, ushort );
220+
void formatLine(TScreenCell *, uint, int, ushort );
220221
void find();
221222
uint getMousePtr( TPoint );
222223
Boolean hasSelection();
@@ -238,6 +239,7 @@ class TEditor : public TView
238239
void setBufLen( uint );
239240
void setCurPtr( uint, uchar );
240241
void startSelect();
242+
void toggleEncoding();
241243
void toggleInsMode();
242244
void unlock();
243245
void update( uchar );
@@ -272,6 +274,13 @@ class TEditor : public TView
272274
const char* eolBytes;
273275
uint eolSize;
274276

277+
Boolean encSingleByte;
278+
char charsBuf[4];
279+
void nextChar( TStringView, uint &P, uint &width );
280+
void formatCell( TScreenCell*, uint, uint&, TStringView, uint& , TCellAttribs );
281+
TStringView bufChars( uint );
282+
TStringView bufPrevChars( uint );
283+
275284
static TEditorDialog _NEAR editorDialog;
276285
static ushort _NEAR editorFlags;
277286
static char _NEAR findStr[maxFindStrLen];

‎include/tvision/scrncell.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ inline void setChar(TScreenCell &cell, TCellChar ch)
3636
((uchar *) &cell)[0] = ch;
3737
}
3838

39+
inline void setCell(TScreenCell &cell, TCellChar ch, TCellAttribs attr)
40+
{
41+
cell = (attr << 8) | ch;
42+
}
43+
3944
#else
4045

4146
#include <cstring>
@@ -238,6 +243,14 @@ inline void setChar(TScreenCell &cell, uchar ch)
238243
cell.extraWidth = 0;
239244
}
240245

246+
inline void setCell(TScreenCell &cell, TCellChar ch, TCellAttribs attr, uchar extraWidth=0)
247+
{
248+
TScreenCell c = 0;
249+
::setChar(c, ch, extraWidth);
250+
::setAttr(c, attr);
251+
cell = c;
252+
}
253+
241254
#endif // __BORLANDC__
242255

243256
#endif

‎include/tvision/ttext.h‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,9 @@ class TText {
136136
static size_t prev(TStringView text, size_t index);
137137
static size_t wseek(TStringView text, int count, Boolean incRemainder=True);
138138

139-
#ifndef __BORLANDC__
140139
static void eat(TScreenCell *cell, size_t n, size_t &width, TStringView text, size_t &bytes);
141140
static void next(TStringView text, size_t &bytes, size_t &width);
142141
static void wseek(TStringView text, size_t &index, size_t &remainder, int count);
143-
#endif
144142

145143
};
146144

@@ -161,6 +159,31 @@ inline size_t TText::wseek(TStringView text, int count, Boolean)
161159
return count > 0 ? min(count, text.size()) : 0;
162160
}
163161

162+
inline void TText::eat( TScreenCell *cell, size_t n, size_t &width,
163+
TStringView text, size_t &bytes )
164+
{
165+
if (n) {
166+
::setChar(*cell, text[0];
167+
++width;
168+
++bytes;
169+
}
170+
}
171+
172+
inline void TText::next(TStringView text, size_t &bytes, size_t &width)
173+
{
174+
if (text.size()) {
175+
++bytes;
176+
++width;
177+
}
178+
}
179+
180+
inline void TText::wseek(TStringView text, size_t &index, size_t &remainder, int count)
181+
{
182+
if (count > 0)
183+
index += count;
184+
remainder = 0;
185+
}
186+
164187
#else
165188

166189
inline size_t TText::next(TStringView text)

‎source/tvision/edits.cpp‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ uint TEditor::bufPtr( uint P )
2727
return P < curPtr ? P : P + gapLen;
2828
}
2929

30-
void TEditor::formatLine( ushort *DrawBuf,
30+
void TEditor::formatLine( TScreenCell *DrawBuf,
3131
uint P,
3232
int Width,
3333
ushort Colors
@@ -42,29 +42,28 @@ void TEditor::formatLine( ushort *DrawBuf,
4242
{ uchar(Colors), bufLen }
4343
};
4444

45-
uchar Color = uchar(Colors);
4645
int X = 0;
47-
4846
for (int r = 0; r < 3; ++r)
4947
{
50-
Color = ranges[r].color;
48+
uchar Color = ranges[r].color;
5149
while (P < ranges[r].end && X < Width)
5250
{
53-
uchar Char = bufChar(P++);
51+
TStringView chars = bufChars(P);
52+
uchar Char = chars[0];
5453
if (Char == '\r' || Char == '\n')
5554
goto fill;
5655
if (Char == '\t') {
5756
do {
58-
DrawBuf[X++] = (Color << 8) | ' ';
57+
::setCell(DrawBuf[X++], ' ', Color);
5958
} while (X%8 != 0 && X < Width);
60-
} else {
61-
DrawBuf[X++] = (Color << 8) | Char;
62-
}
59+
++P;
60+
} else
61+
formatCell(&DrawBuf[X], Width - X, (uint&) X, chars, P, Color);
6362
}
6463
}
6564
fill:
6665
while (X < Width)
67-
DrawBuf[X++] = (Color << 8) | ' ';
66+
::setCell(DrawBuf[X++], ' ', uchar(Colors));
6867
}
6968

7069
uint TEditor::lineEnd( uint P )
@@ -103,7 +102,10 @@ uint TEditor::nextChar( uint P )
103102
{
104103
if (bufChar(P) == '\r' && bufChar(P + 1) == '\n')
105104
return P + 2;
106-
return P + 1;
105+
if (encSingleByte)
106+
return P + 1;
107+
else
108+
return P + TText::next(bufChars(P));
107109
}
108110
return bufLen;
109111
}
@@ -114,7 +116,13 @@ uint TEditor::prevChar( uint P )
114116
{
115117
if (bufChar(P - 2) == '\r' && bufChar(P - 1) == '\n')
116118
return P - 2;
117-
return P - 1;
119+
if (encSingleByte)
120+
return P - 1;
121+
else
122+
{
123+
TStringView t = bufPrevChars(P);
124+
return P - TText::prev(t, t.size());
125+
}
118126
}
119127
return 0;
120128
}

0 commit comments

Comments
 (0)