Commit e89200ca authored by deuce's avatar deuce
Browse files

Give each font a codepage.

getcliptext() and copytext() (ie: copy/paste) now work on UTF-8 strings.
Not tested on Win32 yet.
parent 0b044181
This diff is collapsed.
......@@ -36,6 +36,7 @@
#include <string.h> /* size_t */
#include "gen_defs.h"
#include "utf8_codepages.h"
#ifdef CIOLIBEXPORT
#undef CIOLIBEXPORT
......@@ -250,6 +251,7 @@ struct conio_font_data_struct {
char *eight_by_eight;
char *put_xlat;
char *desc;
enum ciolib_codepage cp;
};
CIOLIBEXPORTVAR struct conio_font_data_struct conio_fontdata[257];
......
......@@ -275,10 +275,7 @@ void exit_sdl_con(void)
void sdl_copytext(const char *text, size_t buflen)
{
size_t outlen;
uint8_t *u8 = cp_to_utf8(CIOLIB_CP437, text, buflen, &outlen);
sdl.SetClipboardText((char *)u8);
free(u8);
sdl.SetClipboardText(text);
}
char *sdl_getcliptext(void)
......
......@@ -90,6 +90,68 @@ static uint32_t cp437_unicode_table[128] = {
0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
};
static struct ciolib_cpmap cp1251_table[159] = {
{0x00a0, 160}, {0x00a4, 164}, {0x00a6, 166}, {0x00a7, 21},
{0x00a7, 167}, {0x00a9, 169}, {0x00ab, 171}, {0x00ac, 172},
{0x00ad, 173}, {0x00ae, 174}, {0x00b0, 176}, {0x00b1, 177},
{0x00b5, 181}, {0x00b6, 20}, {0x00b6, 182}, {0x00b7, 183},
{0x00bb, 187}, {0x0401, 168}, {0x0402, 128}, {0x0403, 129},
{0x0404, 170}, {0x0405, 189}, {0x0406, 178}, {0x0407, 175},
{0x0408, 163}, {0x0409, 138}, {0x040a, 140}, {0x040b, 142},
{0x040c, 141}, {0x040e, 161}, {0x040f, 143}, {0x0410, 192},
{0x0411, 193}, {0x0412, 194}, {0x0413, 195}, {0x0414, 196},
{0x0415, 197}, {0x0416, 198}, {0x0417, 199}, {0x0418, 200},
{0x0419, 201}, {0x041a, 202}, {0x041b, 203}, {0x041c, 204},
{0x041d, 205}, {0x041e, 206}, {0x041f, 207}, {0x0420, 208},
{0x0421, 209}, {0x0422, 210}, {0x0423, 211}, {0x0424, 212},
{0x0425, 213}, {0x0426, 214}, {0x0427, 215}, {0x0428, 216},
{0x0429, 217}, {0x042a, 218}, {0x042b, 219}, {0x042c, 220},
{0x042d, 221}, {0x042e, 222}, {0x042f, 223}, {0x0430, 224},
{0x0431, 225}, {0x0432, 226}, {0x0433, 227}, {0x0434, 228},
{0x0435, 229}, {0x0436, 230}, {0x0437, 231}, {0x0438, 232},
{0x0439, 233}, {0x043a, 234}, {0x043b, 235}, {0x043c, 236},
{0x043d, 237}, {0x043e, 238}, {0x043f, 239}, {0x0440, 240},
{0x0441, 241}, {0x0442, 242}, {0x0443, 243}, {0x0444, 244},
{0x0445, 245}, {0x0446, 246}, {0x0447, 247}, {0x0448, 248},
{0x0449, 249}, {0x044a, 250}, {0x044b, 251}, {0x044c, 252},
{0x044d, 253}, {0x044e, 254}, {0x044f, 255}, {0x0451, 184},
{0x0452, 144}, {0x0453, 131}, {0x0454, 186}, {0x0455, 190},
{0x0456, 179}, {0x0457, 191}, {0x0458, 188}, {0x0459, 154},
{0x045a, 156}, {0x045b, 158}, {0x045c, 157}, {0x045e, 162},
{0x045f, 159}, {0x0490, 165}, {0x0491, 180}, {0x2013, 150},
{0x2014, 151}, {0x2018, 145}, {0x2019, 146}, {0x201a, 130},
{0x201c, 147}, {0x201d, 148}, {0x201e, 132}, {0x2020, 134},
{0x2021, 135}, {0x2022, 149}, {0x2022, 7}, {0x2026, 133},
{0x2030, 137}, {0x2039, 139}, {0x203a, 155}, {0x203c, 19},
{0x20ac, 136}, {0x2116, 185}, {0x2122, 153}, {0x2190, 27},
{0x2191, 24}, {0x2192, 26}, {0x2193, 25}, {0x2194, 29},
{0x2195, 18}, {0x21a8, 23}, {0x221f, 28}, {0x25ac, 22},
{0x25b2, 30}, {0x25ba, 16}, {0x25bc, 31}, {0x25c4, 17},
{0x25cb, 9}, {0x25d8, 8}, {0x25d9, 10}, {0x263a, 1},
{0x263b, 2}, {0x263c, 15}, {0x2640, 12}, {0x2642, 11},
{0x2660, 6}, {0x2663, 5}, {0x2665, 3}, {0x2666, 4},
{0x266a, 13}, {0x266b, 14}, {0xfffd, 152}
};
static uint32_t cp1251_unicode_table[128] = {
0x0402, 0x0403, 0x201a, 0x0453, 0x201e, 0x2026, 0x2020, 0x2021,
0x20ac, 0x2030, 0x0409, 0x2039, 0x040a, 0x040c, 0x040b, 0x040f,
0x0452, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
0xfffd, 0x2122, 0x0459, 0x203a, 0x045a, 0x045c, 0x045b, 0x045f,
0x00a0, 0x040e, 0x045e, 0x0408, 0x00a4, 0x0490, 0x00a6, 0x00a7,
0x0401, 0x00a9, 0x0404, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x0407,
0x00b0, 0x00b1, 0x0406, 0x0456, 0x0491, 0x00b5, 0x00b6, 0x00b7,
0x0451, 0x2116, 0x0454, 0x00bb, 0x0458, 0x0405, 0x0455, 0x0457,
0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f,
0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f,
0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f,
0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
0x0448, 0x0449, 0x044a, 0x044b, 0x044c, 0x044d, 0x044e, 0x044f
};
static int
cmptab(const void *key, const void *entry)
{
......@@ -354,6 +416,9 @@ struct codepage_def ciolib_cp[CIOLIB_CP_COUNT] = {
{"CP437", CIOLIB_CP437, cpstr_to_utf8, utf8_to_cpstr, cptable_from_unicode_cpoint, cptable_from_unicode_cpoint_ext,
cp437_table, sizeof(cp437_table) / sizeof(cp437_table[0]),
cp437_unicode_table, cp437_ext_table},
{"CP1251", CIOLIB_CP1251, cpstr_to_utf8, utf8_to_cpstr, cptable_from_unicode_cpoint, cptable_from_unicode_cpoint_ext,
cp1251_table, sizeof(cp1251_table) / sizeof(cp1251_table[0]),
cp1251_unicode_table, cp437_ext_table},
};
uint8_t *cp_to_utf8(enum ciolib_codepage cp, const char *cpstr, size_t buflen, size_t *outlen)
......
......@@ -3,6 +3,7 @@
enum ciolib_codepage {
CIOLIB_CP437,
CIOLIB_CP1251,
CIOLIB_CP_COUNT
};
......
......@@ -806,20 +806,23 @@ void win32_copytext(const char *text, size_t buflen)
{
HGLOBAL clipbuf;
LPTSTR clip;
int new_buflen = MultiByteToWideChar(CP_UTF8, 0, text, buflen, NULL, 0);
if(!OpenClipboard(NULL))
new_buflen = MultiByteToWideChar(CP_UTF8, 0, text, buflen, NULL, 0);
clipbuf=GlobalAlloc(GMEM_MOVEABLE, new_buflen * sizeof(WCHAR));
if (clipbuf == NULL)
return;
EmptyClipboard();
clipbuf=GlobalAlloc(GMEM_MOVEABLE, buflen+1);
if(clipbuf==NULL) {
CloseClipboard();
if (MultiByteToWideChar(CP_UTF8, 0, text, buflen, clipbuf, new_buflen) != new_buflen) {
GlobalFree(clipbuf);
return;
}
if(!OpenClipboard(NULL))
return;
EmptyClipboard();
clip=GlobalLock(clipbuf);
memcpy(clip, text, buflen);
clip[buflen]=0;
memcpy(clip, clipbuf, (new_buflen)*sizeof(WCHAR));
GlobalUnlock(clipbuf);
SetClipboardData(CF_OEMTEXT, clipbuf);
SetClipboardData(CF_UNICODETEXT, clipbuf);
CloseClipboard();
}
......
......@@ -702,16 +702,17 @@ static int x11_event(XEvent *ev)
respond.xselection.property=None;
if(copybuf!=NULL) {
if(req->target==XA_STRING) {
x11.XChangeProperty(dpy, req->requestor, req->property, XA_STRING, 8, PropModeReplace, (unsigned char *)copybuf, strlen(copybuf));
respond.xselection.property=req->property;
}
else if(req->target == x11.utf8) {
uint8_t *utf8_str = cp_to_utf8(CIOLIB_CP437, copybuf, strlen(copybuf), NULL);
if (utf8_str != NULL) {
x11.XChangeProperty(dpy, req->requestor, req->property, x11.utf8, 8, PropModeReplace, utf8_str, strlen((char *)utf8_str));
char *cpstr = utf8_to_cp(CIOLIB_CP437, (uint8_t *)copybuf, '?', strlen(copybuf), NULL);
if (cpstr != NULL) {
x11.XChangeProperty(dpy, req->requestor, req->property, XA_STRING, 8, PropModeReplace, (uint8_t *)cpstr, strlen((char *)cpstr));
respond.xselection.property=req->property;
free(cpstr);
}
}
else if(req->target == x11.utf8) {
x11.XChangeProperty(dpy, req->requestor, req->property, x11.utf8, 8, PropModeReplace, (uint8_t *)copybuf, strlen((char *)copybuf));
respond.xselection.property=req->property;
}
else if(req->target == x11.targets) {
if (x11.utf8 == 0)
x11.utf8 = x11.XInternAtom(dpy, "UTF8_STRING", False);
......
......@@ -181,7 +181,7 @@ void mousedrag(struct vmem_cell *scrollback)
break;
default:
lines=abs(mevent.endy-mevent.starty)+1;
newcopybuf=realloc(copybuf, endpos-startpos+4+lines*2);
newcopybuf=realloc(copybuf, (endpos-startpos+4+lines*2)*4);
if (newcopybuf)
copybuf = newcopybuf;
else
......@@ -189,8 +189,15 @@ void mousedrag(struct vmem_cell *scrollback)
outpos=0;
lastchar=0;
for(pos=startpos;pos<=endpos;pos++) {
copybuf[outpos++]=tscreen[pos*2];
if(tscreen[pos*2]!=' ' && tscreen[pos*2])
size_t outlen;
uint8_t *utf8str;
utf8str = cp_to_utf8(conio_fontdata[screen[pos].font].cp, (char *)&screen[pos].ch, 1, &outlen);
if (utf8str == NULL)
continue;
memcpy(copybuf + outpos, utf8str, outlen);
outpos += outlen;
if(screen[pos].ch != ' ' && screen[pos].ch)
lastchar=outpos;
if((pos+1)%term.width==0) {
outpos=lastchar;
......
......@@ -438,11 +438,20 @@ void docopy(void)
break;
case CIOLIB_BUTTON_1_DRAG_END:
lines=abs(mevent.endy-mevent.starty)+1;
copybuf=malloc((endy-starty+1)*(endx-startx+1)+1+lines*2);
copybuf=malloc(((endy-starty+1)*(endx-startx+1)+1+lines*2)*4);
outpos=0;
for(y=starty-1;y<endy;y++) {
for(x=startx-1;x<endx;x++) {
copybuf[outpos++]=screen->vmem[(y*api->scrn_width+x)].ch ? screen->vmem[(y*api->scrn_width+x)].ch : ' ';
size_t outlen;
uint8_t *utf8str;
char ch;
ch = screen->vmem[(y*api->scrn_width+x)].ch ? screen->vmem[(y*api->scrn_width+x)].ch : ' ';
utf8str = cp_to_utf8(conio_fontdata[screen->vmem[(y*api->scrn_width+x)].font].cp, &ch, 1, &outlen);
if (utf8str == NULL)
continue;
memcpy(copybuf + outpos, utf8str, outlen);
outpos += outlen;
}
#ifdef _WIN32
copybuf[outpos++]='\r';
......@@ -2132,7 +2141,9 @@ int ugetstr(int left, int top, int width, char *outstr, int max, long mode, int
i=mevnt.startx-left+soffset;
if(i>j)
i=j;
pastebuf=getcliptext();
pb=(uint8_t *)getcliptext();
pastebuf = utf8_to_cp(CIOLIB_CP437, pb, '?', strlen((char *)pb), NULL);
free(pb);
pb=(unsigned char *)pastebuf;
f=0;
}
......@@ -2211,7 +2222,9 @@ int ugetstr(int left, int top, int width, char *outstr, int max, long mode, int
i=mevnt.startx-left+soffset;
if(i>j)
i=j;
pastebuf=getcliptext();
pb=(uint8_t *)getcliptext();
pastebuf = utf8_to_cp(CIOLIB_CP437, pb, '?', strlen((char *)pb), NULL);
free(pb);
pb=(unsigned char *)pastebuf;
ch=0;
}
......@@ -2255,8 +2268,10 @@ int ugetstr(int left, int top, int width, char *outstr, int max, long mode, int
continue;
case CTRL_V:
case CIO_KEY_SHIFT_IC: /* Shift-Insert: Paste */
if((pastebuf=getcliptext()) != NULL)
pb=(unsigned char *)pastebuf;
pb=(uint8_t *)getcliptext();
pastebuf = utf8_to_cp(CIOLIB_CP437, pb, '?', strlen((char *)pb), NULL);
free(pb);
pb=(unsigned char *)pastebuf;
continue;
case CIO_KEY_IC: /* insert */
api->insert_mode = !api->insert_mode;
......@@ -2325,13 +2340,21 @@ int ugetstr(int left, int top, int width, char *outstr, int max, long mode, int
continue;
case CTRL_C:
case CIO_KEY_CTRL_IC: /* Ctrl-Insert */
copytext(str, j);
{
size_t sz;
uint8_t *utf8 = cp_to_utf8(CIOLIB_CP437, str, j, &sz);
copytext((char *)utf8, sz);
free(utf8);
continue;
}
case CTRL_X:
case CIO_KEY_SHIFT_DC:
if(j)
{
copytext(str, j);
size_t sz;
uint8_t *utf8 = cp_to_utf8(CIOLIB_CP437, str, j, &sz);
copytext((char *)utf8, sz);
free(utf8);
i=j=0;
}
continue;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment