Fixed multibyte handling.

This commit is contained in:
jaseg 2014-01-02 16:40:26 +01:00
parent 2ce31fb133
commit 3e1895b71c
2 changed files with 13 additions and 1 deletions

View file

@ -1,4 +1,7 @@
#ifndef __FONT_H__
#define __FONT_H__
#include <stdint.h>
#include <stdio.h>
@ -19,3 +22,4 @@ int read_bdf(FILE *f, glyph_t **glyph_table, unsigned int glyph_table_size);
// Requires buf to point to a buffer at least of size glyph->width*glyph->height.
void render_glyph(glyph_t *glyph, uint8_t *buf, unsigned int bufwidth, unsigned int offx, unsigned int offy);
#endif//__FONT_H__

View file

@ -6,6 +6,7 @@
#include <errno.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
/* CAUTION: REQUIRES INPUT TO BE \0-TERMINATED */
int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size){
@ -23,8 +24,14 @@ int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size
printf(" (%s)\n", s);
wchar_t c;
mbstate_t ps = {0};
memset(&ps, 0, sizeof(mbstate_t));
if(!setlocale(LC_CTYPE, "en_US.utf8")){
fprintf(stderr, "Cannot set locale\n");
goto error;
}
for(;;){
size_t inc = mbrtowc(&c, p, (s+len+1)-p, NULL);
size_t inc = mbrtowc(&c, p, MB_CUR_MAX, &ps); // MB_CUR_MAX is safe since p is \0-terminated
printf("Converted %lx (%x) remaining length %d to %lc rv %d\n", p, (unsigned char)*p, (s+len+1)-p, c, inc);
if(inc == -1 || inc == -2){
fprintf(stderr, "Error rendering string: No valid UTF-8 input.\n");
@ -63,6 +70,7 @@ int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size
unsigned int x = 0;
p = s;
memset(&ps, 0, sizeof(mbstate_t));
for(;;){
size_t inc = mbrtowc(&c, p, (s+len+1)-p, NULL);
// If p contained