/* * libcucul Canvas for ultrafast compositing of Unicode letters * Copyright (c) 2002-2006 Sam Hocevar * 2006 Jean-Yves Lamoureux * All Rights Reserved * * $Id$ * * 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. */ /* * This file contains export functions for HTML and HTML3 */ #include "config.h" #if !defined(__KERNEL__) # include # include # include #endif #include "cucul.h" #include "cucul_internals.h" /** \brief Generate HTML representation of current image. * * This function generates and returns the HTML representation of * the current image. */ void _cucul_get_html(cucul_t *qq, cucul_buffer_t *ex) { static int const palette[] = { 0x000, 0x008, 0x080, 0x088, 0x800, 0x808, 0x880, 0x888, 0x444, 0x44f, 0x4f4, 0x4ff, 0xf44, 0xf4f, 0xff4, 0xfff, }; char *cur; unsigned int x, y, len; /* The CSS palette: roughly 13000 bytes * A line: 7 chars for "
\n" * A glyph: 18 chars for "" * up to 9 chars for "&#xxxxxx;", far less for pure ASCII * 7 chars for "" */ ex->size = 13000 + qq->height * (7 + qq->width * (18 + 9 + 7)); ex->data = malloc(ex->size); cur = ex->data; /* 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++) { uint32_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, "", _cucul_argb32_to_ansi8(lineattr[x])); for(len = 0; x + len < qq->width && lineattr[x + len] == lineattr[x]; len++) { if(linechar[x + len] <= 0x00000020) cur += sprintf(cur, " "); else if(linechar[x + len] < 0x00000080) cur += sprintf(cur, "%c", linechar[x + len]); else cur += sprintf(cur, "&#%i;", linechar[x + len]); } cur += sprintf(cur, ""); } /* New line */ cur += sprintf(cur, "
\n"); } cur += sprintf(cur, "
\n"); /* Crop to really used size */ ex->size = strlen(ex->data) + 1; ex->data = realloc(ex->data, ex->size); } /** \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. */ void _cucul_get_html3(cucul_t *qq, cucul_buffer_t *ex) { static int const palette[] = { 0x000000, 0x000088, 0x008800, 0x008888, 0x880000, 0x880088, 0x888800, 0x888888, 0x444444, 0x4444ff, 0x44ff44, 0x44ffff, 0xff4444, 0xff44ff, 0xffff44, 0xffffff, }; char *cur; unsigned int x, y, len; /* The CSS palette: roughly 13000 bytes * A line: 10 chars for "\n" * A glyph: 40 chars for "" * up to 9 chars for "&#xxxxxx;", far less for pure ASCII * 12 chars for "" */ ex->size = 13000 + qq->height * (10 + qq->width * (40 + 9 + 12)); ex->data = malloc(ex->size); cur = ex->data; /* Table */ cur += sprintf(cur, "\n", qq->height); for(y = 0; y < qq->height; y++) { uint32_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, "
1) cur += sprintf(cur, " colspan=%d", len); cur += sprintf(cur, ">", palette[_cucul_argb32_to_ansi4fg(lineattr[x])]); for(i = 0; i < len; i++) { if(linechar[x + i] <= 0x00000020) cur += sprintf(cur, " "); else if(linechar[x + i] < 0x00000080) cur += sprintf(cur, "%c", linechar[x + i]); else cur += sprintf(cur, "&#%i;", linechar[x + i]); } cur += sprintf(cur, "
\n"); /* Crop to really used size */ ex->size = (uintptr_t)(cur - ex->data); ex->data = realloc(ex->data, ex->size); }