From 2ce31fb1333ce6a8ffe62c49cb9f42aac5547ae2 Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 2 Jan 2014 15:55:36 +0100 Subject: mbrtowc behaves strange. or i'm just dumb. --- host/matelight/font.c | 37 +++-------------------------------- host/matelight/font.h | 2 +- host/matelight/main.c | 53 ++++++++++++++++++++++++++++++++------------------- 3 files changed, 37 insertions(+), 55 deletions(-) diff --git a/host/matelight/font.c b/host/matelight/font.c index 753c442..53d27b9 100644 --- a/host/matelight/font.c +++ b/host/matelight/font.c @@ -5,15 +5,9 @@ #include #include -void render_glyph(glyph_t *g, char *buf, unsigned int bufwidth, unsigned int offx, unsigned int offy){ +void render_glyph(glyph_t *g, uint8_t *buf, unsigned int bufwidth, unsigned int offx, unsigned int offy){ unsigned int bitmap_row_width = g->width/8; uint8_t *bitmap = ((uint8_t *)g) + sizeof(glyph_t); - printf("READING GLYPH FROM %016lx (BITMAP %016lx) SIZE %d ROW WIDTH %d\n", g, bitmap, sizeof(glyph_t), bitmap_row_width); - char *p = bitmap; - for(int i=0; iheight; i++){ - printf("%02x ", *p++); - } - printf("\n"); for(unsigned int y=0; y < g->height; y++){ long int data = 0; for(unsigned int i=0; iwidth; x++){ - if(data&1) - printf("█"); - else - printf(" "); - *p++ = (data&1) ? 1 : 0; - data >>= 1; + *p++ = (data&(1<<(g->width-1))) ? 1 : 0; + data <<= 1; } - printf("\n"); } } @@ -175,31 +163,12 @@ int read_bdf(FILE *f, glyph_t **glyph_table, unsigned int glyph_table_size){ // Right-align data data >>= ((read-1)*4 - dwidth); // Copy rightmost bytes of data to destination buffer - if(encoding == 'A') - printf("%02d %04lx ", i, data); for(unsigned int j=0; j>= 8; } - if(encoding == 'A') - printf("\n"); i++; } - if(encoding == 'A'){ - printf("WRITING GLYPH %d TO %016lx (BITMAP %016lx) SIZE %d ROW WIDTH %d\n", encoding, glyph_data, bitmap, sizeof(glyph_t), row_bytes); - char *p = bitmap; - for(int i=0; iwidth*glyph->height. -void render_glyph(glyph_t *glyph, char *buf, unsigned int bufwidth, unsigned int offx, unsigned int offy); +void render_glyph(glyph_t *glyph, uint8_t *buf, unsigned int bufwidth, unsigned int offx, unsigned int offy); diff --git a/host/matelight/main.c b/host/matelight/main.c index d40ef29..a32706c 100644 --- a/host/matelight/main.c +++ b/host/matelight/main.c @@ -10,24 +10,36 @@ /* CAUTION: REQUIRES INPUT TO BE \0-TERMINATED */ int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size){ unsigned int len = strlen(s); - char *t = s; - wchar_t *buf = calloc(len, sizeof(wchar_t)); - if(buf == 0){ - fprintf(stderr, "Cannot calloc() %ld bytes.\n", len*sizeof(wchar_t)); - goto error; - } - mbsrtowcs(buf, &t, len, NULL); - char *gbuf = NULL; + uint8_t *gbuf = NULL; unsigned int gbufwidth = 0; unsigned int gbufheight = 0; - for(wchar_t *c=buf; *c; c++){ - if(*c > glyph_table_size){ + char *p = s; + + printf("Input: "); + for(int i=0; i<=len; i++){ + printf("%02x ", (unsigned char)s[i]); + } + printf(" (%s)\n", s); + + wchar_t c; + for(;;){ + size_t inc = mbrtowc(&c, p, (s+len+1)-p, NULL); + 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"); + goto error; + } + if(inc == 0) // Reached end of string + break; + p += inc; + + if(c > glyph_table_size){ fprintf(stderr, "Error rendering string: Codepoint 0x%lx out of valid range (0-%d).\n", (long int)c, glyph_table_size); goto error; } - glyph_t *g = glyph_table[*c]; + glyph_t *g = glyph_table[c]; if(!g){ fprintf(stderr, "Error rendering string: Codepoint 0x%lx not in font.\n", (long int)c); goto error; @@ -47,10 +59,18 @@ int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size fprintf(stderr, "Cannot malloc() %d bytes.\n", gbufsize); goto error; } + memset(gbuf, 0, gbufsize); unsigned int x = 0; - for(wchar_t *c=buf; *c; c++){ - glyph_t *g = glyph_table[*c]; + p = s; + for(;;){ + size_t inc = mbrtowc(&c, p, (s+len+1)-p, NULL); + // If p contained + if(inc == 0) // Reached end of string + break; + p += inc; + + glyph_t *g = glyph_table[c]; render_glyph(g, gbuf, gbufwidth, x, 0); x += g->width; } @@ -58,11 +78,6 @@ int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size for(unsigned int y=0; y < gbufheight; y++){ for(unsigned int x=0; x < gbufwidth; x++){ //Da magicks: ▀█▄ - if(gbuf[y*gbufwidth + x]) - printf("█"); - else - printf(" "); - /* char c1 = gbuf[y*gbufwidth + x]; char c2 = gbuf[(y+1)*gbufwidth + x]; if(c1 && c2) @@ -73,14 +88,12 @@ int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size printf("▄"); else printf(" "); - */ } printf("\n"); } return 0; error: free(gbuf); - free(buf); return 1; } -- cgit