diff --git a/src/graphics.c b/src/graphics.c index a3f663f..c97edb0 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -1863,57 +1863,83 @@ char* caca_get_html(void) * This function generates and returns the HTML3 representation of * the current image. It is way bigger than caca_get_html(), but * permits viewing in old browsers (or limited ones such as links) - * + * Won't work under gecko (mozilla rendering engine) unless you set + * a correct header. */ char* caca_get_html3(void) { - char *buffer, *cur; - unsigned int x, y, len; + char *buffer, *cur; + unsigned int x,y; - /* 13000 -> css palette - * 40 -> max size used for a pixel (plus 10, never know)*/ - /* FIXME: Check this value */ - buffer = malloc((13000 + ((_caca_width*_caca_height) * 40)) * sizeof(char)); - cur = buffer; + /* 13000 -> css palette + 40 -> max size used for a pixel (plus 10, never know)*/ - cur += sprintf(cur, "
\n", - "font-family: monospace, fixed; font-weight: bold;"); + buffer = malloc((13000 + ((_caca_width*_caca_height)*40))*sizeof(char)); + cur = buffer; - for(y = 0; y < _caca_height; y++) - { - for(x = 0; x < _caca_width; x += len) - { - uint8_t *cur_attr = cache_attr + y * _caca_width; - uint8_t *cur_char = cache_char + y * _caca_width; + /* Table */ + cur += sprintf(cur, "\n", caca_get_height()); - cur += sprintf(cur, - "", - html_palette[cur_attr[x] & 0x0f], - html_palette[cur_attr[x] >> 4]); - - for(len = 0; - x + len < _caca_width && cur_attr[x + len] == cur_attr[x]; - len++) - { - if(cur_char[x + len] == ' ') - cur += sprintf(cur, " "); - else - cur += sprintf(cur, "%c", cur_char[x + len]); - } - cur += sprintf(cur, ""); - } - /* New line */ - cur += sprintf(cur, "
\n"); + for(y=0;y<_caca_height;y++) + { + cur += sprintf(cur, ""); + + for(x=0;x<_caca_width;x++) + { + int len; + int i; + uint8_t *attr = cache_attr + x + y * _caca_width; + + /* Use colspan option to factorize cells with same attributes + (see below) */ + len=1; + while(x + len < _caca_width + && (attr[len]>>4) == (attr[0]>>4) && + (attr[len]&0x0f) == (attr[0]&0x0f)) + len++; + + if(len==1) + { + cur += sprintf(cur, + "", + html_palette[cache_attr[x+y*caca_get_width()]>>4], + html_palette[cache_attr[x+y*caca_get_width()]&0x0f], + cache_char[x+y*caca_get_width()]); + } + else + { + cur += sprintf(cur, + ""); + + } + + } + cur += sprintf(cur, "\n"); } - cur += sprintf(cur, "\n"); + /* Footer */ + cur += sprintf(cur, "
%c", + html_palette[cache_attr[x+y*caca_get_width()]>> 4], + len+1, + html_palette[cache_attr[x+y*caca_get_width()]&0x0f]); + + for(i=0;i
\n"); - /* Crop to really used size */ - buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char)); + /* Crop to really used size */ + buffer = realloc(buffer, (strlen(buffer)+1) * sizeof(char)); - return buffer; + return buffer; } + static int const irc_palette[] = { /* Dark */ @@ -1937,6 +1963,7 @@ char* caca_get_irc(void) unsigned int x, y; /* 15 bytes assumed for max length per pixel */ buffer = malloc(((_caca_width*_caca_height*15)+1)*sizeof(char)); + sprintf(buffer, "%%O"); for(y = 0; y < _caca_height; y++) @@ -1945,18 +1972,18 @@ char* caca_get_irc(void) { if(cache_char[x+y*caca_get_width()] == ' ') { - sprintf(buffer, - "%s%%C%d,%d%c", buffer, - irc_palette[cache_attr[x+y*caca_get_width()]>>4], - irc_palette[cache_attr[x+y*caca_get_width()]>>4], - '#'); + sprintf(buffer, + "%s%%C%d,%d%c", buffer, + irc_palette[cache_attr[x+y*caca_get_width()]>>4], + irc_palette[cache_attr[x+y*caca_get_width()]>>4], + '#'); } else if(cache_char[x+y*caca_get_width()] == '%') { - sprintf(buffer, - "%s%%C%d,%d%%%%", buffer, - irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], - irc_palette[cache_attr[x+y*caca_get_width()]>> 4]); + sprintf(buffer, + "%s%%C%d,%d%%%%", buffer, + irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], + irc_palette[cache_attr[x+y*caca_get_width()]>> 4]); } else if(cache_char[x+y*caca_get_width()]>='0' && cache_char[x+y*caca_get_width()]<='9') { @@ -1966,21 +1993,21 @@ char* caca_get_irc(void) irc_palette[cache_attr[x+y*caca_get_width()]>> 4], cache_char[x+y*caca_get_width()]); } - else - { - sprintf(buffer, - "%s%%C%d,%d%c", buffer, - irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], - irc_palette[cache_attr[x+y*caca_get_width()]>> 4], - cache_char[x+y*caca_get_width()]); + else + { + sprintf(buffer, + "%s%%C%d,%d%c", buffer, + irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], + irc_palette[cache_attr[x+y*caca_get_width()]>> 4], + cache_char[x+y*caca_get_width()]); } - + } sprintf(buffer, "%s\n", buffer); - } - + } + /* Crop to really used size */ buffer = realloc(buffer, (strlen(buffer)+1) * sizeof(char)); - + return buffer; }