/* * libcucul Unicode canvas library * Copyright (c) 2002-2006 Sam Hocevar * All Rights Reserved * * This library is free software; you can redistribute it and/or * modify it under the terms of the Do What The Fuck You Want To * Public License, Version 2, as published by Sam Hocevar. See * http://sam.zoy.org/wtfpl/COPYING for more details. */ /** \file export.c * \version \$Id$ * \author Sam Hocevar * \author Jean-Yves Lamoureux * \brief Export function * * This file contains export functions for various file formats such * as HTML or IRC. */ #include "config.h" #include #include #include #include "cucul.h" #include "cucul_internals.h" /* HTML */ /** \brief Generate HTML representation of current image. * * This function generates and returns the HTML representation of * the current image. */ char* cucul_get_html(cucul_t *qq, int *size) { static int const palette[] = { 0x000, 0x008, 0x080, 0x088, 0x800, 0x808, 0x880, 0x888, 0x444, 0x44f, 0x4f4, 0x4ff, 0xf44, 0xf4f, 0xff4, 0xfff, }; char *buffer, *cur; unsigned int x, y, len; /* 13000 -> css palette * 40 -> max size used for a pixel (plus 10, never know)*/ /* FIXME: Check this value */ buffer = malloc((13000 + ((qq->width*qq->height) * 40)) * sizeof(char)); cur = buffer; /* HTML header */ cur += sprintf(cur, "\n\nGenerated by libcaca %s\n", VERSION); /* CSS */ cur += sprintf(cur, "\n\n\n"); cur += sprintf(cur, "
\n", "font-family: monospace, fixed; font-weight: bold;"); for(y = 0; y < qq->height; y++) { uint8_t *lineattr = qq->attr + y * qq->width; uint32_t *linechar = qq->chars + y * qq->width; for(x = 0; x < qq->width; x += len) { cur += sprintf(cur, "", lineattr[x]); for(len = 0; x + len < qq->width && lineattr[x + len] == lineattr[x]; len++) { if(linechar[x + len] == (uint32_t)' ') cur += sprintf(cur, " "); else cur += sprintf(cur, "%c", linechar[x + len] & 0x7f); } cur += sprintf(cur, ""); } /* New line */ cur += sprintf(cur, "
\n"); } cur += sprintf(cur, "
\n"); /* Crop to really used size */ *size = (strlen(buffer) + 1) * sizeof(char); buffer = realloc(buffer, *size); return buffer; } /** \brief Generate HTML3 representation of current image. * * This function generates and returns the HTML3 representation of * the current image. It is way bigger than cucul_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* cucul_get_html3(cucul_t *qq, int *size) { static int const palette[] = { 0x000000, 0x000088, 0x008800, 0x008888, 0x880000, 0x880088, 0x888800, 0x888888, 0x444444, 0x4444ff, 0x44ff44, 0x44ffff, 0xff4444, 0xff44ff, 0xffff44, 0xffffff, }; char *buffer, *cur; unsigned int x, y, len; /* 13000 -> css palette * 40 -> max size used for a pixel (plus 10, never know) */ buffer = malloc((13000 + ((qq->width*qq->height)*40))*sizeof(char)); cur = buffer; /* Table */ cur += sprintf(cur, "\n", qq->height); for(y = 0; y < qq->height; y++) { uint8_t *lineattr = qq->attr + y * qq->width; uint32_t *linechar = qq->chars + y * qq->width; cur += sprintf(cur, ""); for(x = 0; x < qq->width; x += len) { unsigned int i; /* Use colspan option to factorize cells with same attributes * (see below) */ len = 1; while(x + len < qq->width && lineattr[x + len] == lineattr[x]) len++; cur += sprintf(cur, ""); } cur += sprintf(cur, "\n"); } /* Footer */ cur += sprintf(cur, "
> 4]); if(len > 1) cur += sprintf(cur, " colspan=%d", len); cur += sprintf(cur, ">", palette[lineattr[x] & 0x0f]); for(i = 0; i < len; i++) { if(linechar[x + i] == (uint32_t)' ') cur += sprintf(cur, " "); else cur += sprintf(cur, "%c", linechar[x + i] & 0x7f); } cur += sprintf(cur, "
\n"); /* Crop to really used size */ *size = (strlen(buffer) + 1) * sizeof(char); buffer = realloc(buffer, *size); return buffer; } /** \brief Generate IRC representation of current image. * * This function generates and returns an IRC representation of * the current image. */ char* cucul_get_irc(cucul_t *qq, int *size) { static int const palette[] = { 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */ 14, 12, 9, 11, 4, 13, 8, 0, /* Light */ }; char *buffer, *cur; unsigned int x, y; /* 11 bytes assumed for max length per pixel. Worst case scenario: * ^Cxx,yy 6 bytes * ^B^B 2 bytes * c 1 byte * \r\n 2 bytes * In real life, the average bytes per pixel value will be around 5. */ buffer = malloc((2 + (qq->width * qq->height * 11)) * sizeof(char)); cur = buffer; *cur++ = '\x0f'; for(y = 0; y < qq->height; y++) { uint8_t *lineattr = qq->attr + y * qq->width; uint32_t *linechar = qq->chars + y * qq->width; uint8_t prevfg = -1; uint8_t prevbg = -1; for(x = 0; x < qq->width; x++) { uint8_t fg = palette[lineattr[x] & 0x0f]; uint8_t bg = palette[lineattr[x] >> 4]; uint32_t c = linechar[x]; if(bg == prevbg) { if(fg == prevfg) ; /* Same fg/bg, do nothing */ else if(c == (uint32_t)' ') fg = prevfg; /* Hackety hack */ else { cur += sprintf(cur, "\x03%d", fg); if(c >= (uint32_t)'0' && c <= (uint32_t)'9') cur += sprintf(cur, "\x02\x02"); } } else { if(fg == prevfg) cur += sprintf(cur, "\x03,%d", bg); else cur += sprintf(cur, "\x03%d,%d", fg, bg); if(c >= (uint32_t)'0' && c <= (uint32_t)'9') cur += sprintf(cur, "\x02\x02"); } *cur++ = c & 0x7f; prevfg = fg; prevbg = bg; } *cur++ = '\r'; *cur++ = '\n'; } *cur++ = '\x0f'; /* Crop to really used size */ *size = (strlen(buffer) + 1) * sizeof(char); buffer = realloc(buffer, *size); return buffer; } /** \brief Generate ANSI representation of current image. * * This function generates and returns an ANSI representation of * the current image. * \param trailing if 0, raw ANSI will be generated. Otherwise, you'll be * able to cut/paste the result to a function like printf * \return buffer containing generated ANSI codes as a big string */ char * cucul_get_ansi(cucul_t *qq, int trailing, int *size) { static int const palette[] = { 30, 34, 32, 36, 31, 35, 33, 37, /* Both lines (light and dark) are the same, */ 30, 34, 32, 36, 31, 35, 33, 37, /* light colors handling is done later */ }; char *cur; unsigned int x, y; /* 20 bytes assumed for max length per pixel. * Add height*9 to that (zeroes color at the end and jump to next line) */ if(qq->ansi_buffer) free(qq->ansi_buffer); qq->ansi_buffer = malloc(((qq->height*9) + (qq->width * qq->height * 20)) * sizeof(char)); cur = qq->ansi_buffer; // *cur++ = ''; for(y = 0; y < qq->height; y++) { uint8_t *lineattr = qq->attr + y * qq->width; uint32_t *linechar = qq->chars + y * qq->width; uint8_t prevfg = -1; uint8_t prevbg = -1; for(x = 0; x < qq->width; x++) { uint8_t fg = palette[lineattr[x] & 0x0f]; uint8_t bg = (palette[lineattr[x] >> 4])+10; uint32_t c = linechar[x]; if(!trailing) cur += sprintf(cur, "\033["); else cur += sprintf(cur, "\\033["); if(fg > 7) cur += sprintf(cur, "1;%d;%dm",fg,bg); else cur += sprintf(cur, "0;%d;%dm",fg,bg); *cur++ = c & 0x7f; if((c == '%') && trailing) *cur++ = c & 0x7f; prevfg = fg; prevbg = bg; } if(!trailing) cur += sprintf(cur, "\033[0m\n\r"); else cur += sprintf(cur, "\\033[0m\\n\n"); } /* Crop to really used size */ *size = (strlen(qq->ansi_buffer) + 1)* sizeof(char); qq->ansi_buffer = realloc(qq->ansi_buffer, *size); return qq->ansi_buffer; }