diff console/libgnt/gntutils.c @ 14311:fda9dc44807d

[gaim-migrate @ 17001] Wide-characters should no longer eat characters at the end of the line in textview. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Wed, 23 Aug 2006 13:46:51 +0000
parents 81648c6aa918
children 71666c137a07
line wrap: on
line diff
--- a/console/libgnt/gntutils.c	Wed Aug 23 12:29:08 2006 +0000
+++ b/console/libgnt/gntutils.c	Wed Aug 23 13:46:51 2006 +0000
@@ -1,5 +1,9 @@
 #include "gntutils.h"
 
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+
 void gnt_util_get_text_bound(const char *text, int *width, int *height)
 {
 	const char *s = text, *last;
@@ -34,3 +38,49 @@
 		*width = max + (count > 1);
 }
 
+int gnt_util_onscreen_width(const char *start, const char *end)
+{
+	wchar_t wch;
+	int size;
+	int width = 0;
+
+	while (start < end) {
+		if ((size = mbtowc(&wch, start, end - start)) > 0) {
+			start += size;
+			width += wcwidth(wch);
+		} else {
+			++width;
+			++start;
+		}
+	}
+	return width;
+}
+
+char *gnt_util_onscreen_width_to_pointer(const char *string, int len, int *w)
+{
+	wchar_t wch;
+	int size;
+	int width = 0;
+	char *str = (char*)string;
+	int slen = strlen(string);  /* Yeah, no. of bytes */
+
+	while (width < len && *str) {
+		if ((size = mbtowc(&wch, str, slen)) > 0) {
+			if (width + wcwidth(wch) > len)
+				break;
+			str += size;
+			width += wcwidth(wch);
+			slen -= size;
+		} else {
+			++str;
+			++width;
+			--slen;
+		}
+	}
+
+	if (w)
+		*w = width;
+
+	return str;
+}
+