aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaseg <jaseg@jaseg.net>2014-01-02 15:55:36 +0100
committerjaseg <jaseg@jaseg.net>2014-01-02 15:55:36 +0100
commit2ce31fb1333ce6a8ffe62c49cb9f42aac5547ae2 (patch)
treec73df7fd19fb1ae43a6db189e5f567238293adf7
parent6d84637d7e34dbbda8a4192b17a274810a55adc3 (diff)
downloadmatelight-2ce31fb1333ce6a8ffe62c49cb9f42aac5547ae2.tar.gz
matelight-2ce31fb1333ce6a8ffe62c49cb9f42aac5547ae2.tar.bz2
matelight-2ce31fb1333ce6a8ffe62c49cb9f42aac5547ae2.zip
mbrtowc behaves strange. or i'm just dumb.
-rw-r--r--host/matelight/font.c37
-rw-r--r--host/matelight/font.h2
-rw-r--r--host/matelight/main.c53
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 <stdlib.h>
#include <string.h>
-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; i<bitmap_row_width*g->height; i++){
- printf("%02x ", *p++);
- }
- printf("\n");
for(unsigned int y=0; y < g->height; y++){
long int data = 0;
for(unsigned int i=0; i<bitmap_row_width; i++){
@@ -21,16 +15,10 @@ void render_glyph(glyph_t *g, char *buf, unsigned int bufwidth, unsigned int off
data |= bitmap[y*bitmap_row_width+i];
}
uint8_t *p = buf + (offy+y)*bufwidth + offx;
- printf("R %02d %04lx ", y, data);
for(unsigned int x=0; x < g->width; 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<row_bytes; j++){
- if(encoding == 'A')
- for(unsigned int bit=0; bit<8; bit++){
- if(data&(1<<bit))
- printf("█");
- else
- printf(" ");
- }
bitmap[(i+1)*row_bytes-j-1] = data&0xFF;
data >>= 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; i<row_bytes*current_glyph.height; i++){
- printf("%02x ", *p++);
- }
- printf("\n");
- }
memcpy(glyph_data, &current_glyph, sizeof(glyph_t));
glyph_table[encoding] = glyph_data;
diff --git a/host/matelight/font.h b/host/matelight/font.h
index 6213eb4..658609a 100644
--- a/host/matelight/font.h
+++ b/host/matelight/font.h
@@ -17,5 +17,5 @@ typedef struct {
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, 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;
}