shithub: drawterm-fdroid

Download patch

ref: a130d441722ac3f759d2d83b98eb6aef7e84f97e
parent: dbe5ea124123547cea539c078130d5b2168a2fd2
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Mon Aug 2 14:33:28 EDT 2021

gui-win32: delete utf-16 routines, just use WideCharToMultiByte()/MultiByteToWideChar() kernel32 functions

--- a/gui-win32/Makefile
+++ b/gui-win32/Makefile
@@ -4,7 +4,6 @@
 
 OFILES=\
 	screen.$O\
-	r16.$O
 
 default: $(LIB)
 $(LIB): $(OFILES)
--- a/gui-win32/r16.c
+++ /dev/null
@@ -1,186 +1,0 @@
-#define _WIN32_WINNT 0x0500
-#include <windows.h>
-
-#include "u.h"
-#include "lib.h"
-#include "r16.h"
-
-#define Bit(i) (7-(i))
-/* N 0's preceded by i 1's, T(Bit(2)) is 1100 0000 */
-#define T(i) (((1 << (Bit(i)+1))-1) ^ 0xFF)
-/* 0000 0000 0000 0111 1111 1111 */
-#define	RuneX(i) ((1 << (Bit(i) + ((i)-1)*Bitx))-1)
-
-enum
-{
-	Bitx	= Bit(1),
-
-	Tx	= T(1),			/* 1000 0000 */
-	Rune1 = (1<<(Bit(0)+0*Bitx))-1,	/* 0000 0000 0000 0000 0111 1111 */
-
-	Maskx	= (1<<Bitx)-1,		/* 0011 1111 */
-	Testx	= Maskx ^ 0xFF,		/* 1100 0000 */
-
-	SurrogateMax	= 0xDFFF,
-	HiSurrogate		= 0xD800,
-	LoSurrogate		= 0xDC00,
-
-	Bad	= Runeerror,
-};
-
-Rune16*
-runes16dup(Rune16 *r)
-{
-	int n;
-	Rune16 *s;
-
-	n = runes16len(r) + 1;
-	s = calloc(n, sizeof(Rune16));
-	memcpy(s, r, n * sizeof(Rune16));
-	return s;
-}
-
-int
-runes16len(Rune16 *r)
-{
-	int n;
-
-	n = 0;
-	while(*r++ != 0)
-		n++;
-	return n;
-}
-
-char*
-runes16toutf(char *p, Rune16 *r, int nc)
-{
-	char *op, *ep;
-	int n, c;
-	Rune rc;
-
-	op = p;
-	ep = p + nc;
-	while(c = *r++){
-		n = 1;
-		if(c >= Runeself)
-			n = runelen(c);
-		if(p + n >= ep)
-			break;
-		if(c < Runeself){
-			*p++ = c;
-			continue;
-		}
-		rc = c;
-		if(c >= LoSurrogate && c <= SurrogateMax)
-			rc = Bad;
-		else if(c >= HiSurrogate && c <= 0xDBFF){ /* decode a surrogate pair properly */
-			if(p + n+1 >= ep)
-				rc = Bad;
-			else if((c = *r) >= LoSurrogate && c <= SurrogateMax){
-				rc = 0x10000 | (*(r-1) - HiSurrogate) << 10 | (c - LoSurrogate);
-				r++;
-			}else
-				rc = Bad;
-		}
-		p += runetochar(p, &rc);
-	}
-	*p = '\0';
-	return op;
-}
-
-int
-rune16nlen(Rune16 *r, int nrune)
-{
-	int nb, i;
-	Rune c;
-
-	nb = 0;
-	while(nrune--) {
-		c = *r++;
-		if(c <= Rune1){
-			nb++;
-		} else {
-			for(i = 2; i < UTFmax + 1; i++)
-				if(c <= RuneX(i) || i == UTFmax){
-					nb += i;
-					break;
-				}
-		}
-	}
-	return nb;
-}
-
-Rune16*
-utftorunes16(Rune16 *r, char *p, int nc)
-{
-	Rune16 *or, *er;
-	Rune rc;
-
-	or = r;
-	er = r + nc;
-	while(*p != '\0' && r + 1 < er){
-		p += chartorune(&rc, p);
-		if(rc >= 0x10000){ /* got to encode it in a surrogate pair */
-			rc -= 0x10000;
-			*r++ = (rc >> 10)+HiSurrogate;
-			*r++ = (rc & 0x3FF)+LoSurrogate;
-		}else
-			*r++ = rc;
-	}
-	*r = '\0';
-	return or;
-}
-
-int
-runes16cmp(Rune16 *s1, Rune16 *s2)
-{
-	Rune16 r1, r2;
-
-	for(;;) {
-		r1 = *s1++;
-		r2 = *s2++;
-		if(r1 != r2) {
-			if(r1 > r2)
-				return 1;
-			return -1;
-		}
-		if(r1 == 0)
-			return 0;
-	}
-}
-
-wchar_t *
-widen(char *s)
-{
-	int n;
-	wchar_t *ws;
-
-	n = utflen(s) + 1;
-	ws = calloc(n, sizeof(wchar_t));
-	utftorunes16(ws, s, n);
-	return ws;
-}
-
-
-char *
-narrowen(wchar_t *ws)
-{
-	char *s;
-	int n;
-
-	n = widebytes(ws);
-	s = malloc(n);
-	runes16toutf(s, ws, n);
-	return s;
-}
-
-
-int
-widebytes(wchar_t *ws)
-{
-	int n = 0;
-
-	while(*ws)
-		n += runelen(*ws++);
-	return n+1;
-}
--- a/gui-win32/r16.h
+++ /dev/null
@@ -1,11 +1,0 @@
-typedef unsigned short Rune16;
-
-wchar_t	*widen(char *s);
-char		*narrowen(wchar_t *ws);
-int		widebytes(wchar_t *ws);
-int		runes16len(Rune16*);
-int		rune16nlen(Rune16*, int);
-Rune16*	runes16dup(Rune16*);
-Rune16*	utftorunes16(Rune16*, char*, int);
-char*	runes16toutf(char*, Rune16*, int);
-int		runes16cmp(Rune16*, Rune16*);
--- a/gui-win32/screen.c
+++ b/gui-win32/screen.c
@@ -14,7 +14,6 @@
 #include <memdraw.h>
 #include "screen.h"
 #include "keyboard.h"
-#include "r16.h"
 
 Memimage	*gscreen;
 Screeninfo	screen;
@@ -557,14 +556,14 @@
 char*
 clipreadunicode(HANDLE h)
 {
-	Rune16 *p;
+	wchar_t *p;
 	int n;
 	char *q;
 
 	p = GlobalLock(h);
-	n = rune16nlen(p, runes16len(p)+1);
-	q = malloc(n);
-	runes16toutf(q, p, n);
+	n = WideCharToMultiByte(CP_UTF8, 0, p, -1, 0, 0, 0, 0);
+	q = malloc(n+1);
+	WideCharToMultiByte(CP_UTF8, 0, p, -1, q, n, 0, 0);
 	GlobalUnlock(h);
 
 	return q;
@@ -642,7 +641,7 @@
 {
 	HANDLE h;
 	char *p;
-	Rune16 *rp;
+	wchar_t *rp;
 	char *crbuf;
 	int n;
 
@@ -659,11 +658,11 @@
 
 	crbuf = addcr(buf, &n);
 
-	h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, (n+1)*sizeof(Rune));
+	h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, (n+1)*sizeof(rp[0]));
 	if(h == NULL)
 		panic("out of memory");
 	rp = GlobalLock(h);
-	utftorunes16(rp, crbuf, n+1);
+	MultiByteToWideChar(CP_UTF8, 0, crbuf, -1, rp, n+1);
 	GlobalUnlock(h);
 
 	SetClipboardData(CF_UNICODETEXT, h);