From 916aced1bef07001ef4eee2a1cde6cd6e33b4bc3 Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 3 Jan 2014 00:10:20 +0100 Subject: Now with even more abstract art. --- host/matelight/main.c | 256 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 244 insertions(+), 12 deletions(-) (limited to 'host/matelight/main.c') diff --git a/host/matelight/main.c b/host/matelight/main.c index 7a23a0a..48c50d9 100644 --- a/host/matelight/main.c +++ b/host/matelight/main.c @@ -1,5 +1,6 @@ #include "font.h" +#include "color.h" #include #include #include @@ -7,6 +8,7 @@ #include #include #include +#include /* CAUTION: REQUIRES INPUT TO BE \0-TERMINATED */ int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size){ @@ -53,7 +55,8 @@ int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size // For easier rendering on the terminal, round up to multiples of two gbufheight += gbufheight&1; - unsigned int gbufsize = gbufwidth*gbufheight; + // gbuf uses 3 bytes per color for r, g and b + unsigned int gbufsize = gbufwidth*3*gbufheight; gbuf = malloc(gbufsize); if(gbuf == 0){ fprintf(stderr, "Cannot malloc() %d bytes.\n", gbufsize); @@ -64,31 +67,260 @@ 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)); + struct { + color_t fg; + color_t bg; + int blink:4; + int bold:1; // TODO + int underline:1; + int strikethrough:1; + int fraktur:1; // TODO See: Flat10 Fraktur font + int invert:1; + } style = { + colortable[DEFAULT_FG_COLOR], colortable[DEFAULT_BG_COLOR], 0, 0, 0, 0, 0, 0 + }; for(;;){ + // NOTE: This nested escape sequence parsing does not contain any unicode-awareness whatsoever + if(*p == '\033'){ // Escape sequence YAY + char *sequence_start = p++; + if(*p == '['){ // This was a CSI! + long elems[MAX_CSI_ELEMENTS]; + int nelems; + for(nelems = 0; nelemsy usually is a negative index of the glyph's baseline measured from the glyph's bottom + int uly = gbufheight + g->y; + for(int i=0; iwidth; i++){ + if(style.strikethrough) + *((color_t *)(gbuf + (sty*gbufwidth + i)*3)) = fg; + if(style.underline) + *((color_t *)(gbuf + (uly*gbufwidth + i)*3)) = fg; + } + } x += g->width; } + for(unsigned int y=0; y < gbufheight; y++){ + for(unsigned int x=0; x < gbufwidth; x++){ + color_t c = *((color_t *)(gbuf + (y*gbufwidth + x)*3)); + //printf("\033[0m%02x,%02x,%02x-%02x\033[38;5;%dm█", c.r, c.g, c.b, xterm_color_index(c), xterm_color_index(c)); + printf("\033[38;5;%dm█", xterm_color_index(c)); + } + printf("\n"); + } + return 0; + color_t lastfg = {0, 0, 0}, lastbg = {0, 0, 0}; + printf("\e[38;5;0;48;5;0m"); for(unsigned int y=0; y < gbufheight; y+=2){ for(unsigned int x=0; x < gbufwidth; x++){ //Da magicks: ▀█▄ - char c1 = gbuf[y*gbufwidth + x]; - char c2 = gbuf[(y+1)*gbufwidth + x]; - if(c1 && c2) - printf("█"); - else if(c1 && !c2) - printf("▀"); - else if(!c1 && c2) - printf("▄"); - else - printf(" "); + color_t ct = *((color_t *)(gbuf + (y*gbufwidth + x)*3)); // Top pixel + color_t cb = *((color_t *)(gbuf + ((y+1)*gbufwidth + x)*3)); // Bottom pixel + if(!memcmp(&ct, &lastfg, sizeof(color_t))){ + if(!memcmp(&cb, &lastbg, sizeof(color_t))){ + printf("▀"); + }else if(!memcmp(&cb, &lastfg, sizeof(color_t))){ + printf("█"); + }else{ + printf("\033[48;5;%dm▀", xterm_color_index(cb)); + lastbg = cb; + } + }else if(!memcmp(&ct, &lastbg, sizeof(color_t))){ + if(!memcmp(&cb, &lastfg, sizeof(color_t))){ + printf("▄"); + }else if(!memcmp(&cb, &lastbg, sizeof(color_t))){ + printf(" "); + }else{ + printf("\033[38;5;%dm▄", xterm_color_index(cb)); + lastfg = cb; + } + }else{ // No matches for the upper pixel + if(!memcmp(&cb, &lastfg, sizeof(color_t))){ + printf("\033[48;5;%dm▄", xterm_color_index(ct)); + lastbg = ct; + }else if(!memcmp(&cb, &lastbg, sizeof(color_t))){ + printf("\033[38;5;%dm▀", xterm_color_index(ct)); + lastfg = ct; + }else{ + printf("\033[38;5;%d;48;5;%dm▀", xterm_color_index(ct), xterm_color_index(cb)); + lastfg = ct; + lastbg = cb; + } + } } printf("\n"); } -- cgit