summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/unicode.cc
diff options
context:
space:
mode:
authorAdam Borowski <kilobyte@angband.pl>2011-03-13 21:29:41 +0100
committerAdam Borowski <kilobyte@angband.pl>2011-03-31 22:17:43 +0200
commit9c0a425e56f3c7b337c0a0276d26de2d8f38cfbf (patch)
tree8dafb69896b03aff777b87c47d650b8fc1d961d6 /crawl-ref/source/unicode.cc
parent1beff1240c1563c548b2ffd9ebbed6b32481c6be (diff)
downloadcrawl-ref-9c0a425e56f3c7b337c0a0276d26de2d8f38cfbf.tar.gz
crawl-ref-9c0a425e56f3c7b337c0a0276d26de2d8f38cfbf.zip
Unicode support in the line_reader, including CJK and combining.
There are sadly some redraw errors when there's line-wrapping involved, especially if you're editing something not at the end of the buffer, but these appear to be not regressions so I left them for now. I am tempted to just brute-force it by redrawing the whole thing and let ncurses optimize it...
Diffstat (limited to 'crawl-ref/source/unicode.cc')
-rw-r--r--crawl-ref/source/unicode.cc37
1 files changed, 37 insertions, 0 deletions
diff --git a/crawl-ref/source/unicode.cc b/crawl-ref/source/unicode.cc
index 53f22aa534..1d27647393 100644
--- a/crawl-ref/source/unicode.cc
+++ b/crawl-ref/source/unicode.cc
@@ -475,3 +475,40 @@ int strwidth(const std::string &s)
{
return strwidth(s.c_str());
}
+
+int wclen(ucs_t c)
+{
+ char dummy[4];
+ return wctoutf8(dummy, c);
+}
+
+char *prev_glyph(char *s, char *start)
+{
+ ucs_t c;
+ do
+ {
+ // Find the start of the previous code point.
+ do
+ if (--s < start)
+ return 0;
+ while ((*s & 0xc0) == 0x80);
+ // If a combining one, continue.
+ utf8towc(&c, s);
+ } while (!wcwidth(c));
+ return s;
+}
+
+char *next_glyph(char *s)
+{
+ char *s_cur;
+ ucs_t c;
+ // Skip at least one character.
+ s += utf8towc(&c, s);
+ if (!c)
+ return 0;
+ do
+ s += utf8towc(&c, s_cur = s);
+ // And any combining ones after it.
+ while (c && !wcwidth(c));
+ return s_cur;
+}