|
- /*
- * libcucul Unicode canvas library
- * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
- * 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: export.c 361 2006-03-09 13:24:06Z jylam $
- * \author Sam Hocevar <sam@zoy.org>
- * \author Jean-Yves Lamoureux <jylam@lnxscene.org>
- * \brief Export function
- *
- * This file contains export functions for HTML and HTML3
- */
-
- #include "config.h"
-
- #if !defined(__KERNEL__)
- # include <stdlib.h>
- # include <stdio.h>
- # include <string.h>
- #endif
-
- #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 *cur;
- unsigned int x, y, len;
-
- /* 13000 -> css palette
- * 40 -> max size used for a pixel (plus 10, never know)*/
- /* FIXME: Check this value */
- if(qq->html_buffer)
- free(qq->html_buffer);
-
- qq->html_buffer = malloc((13000 + ((qq->width*qq->height) * 40)) * sizeof(char));
- if(qq->html_buffer == NULL)
- return NULL;
-
-
- cur = qq->html_buffer;
-
- /* HTML header */
- cur += sprintf(cur, "<html>\n<head>\n<title>Generated by libcaca %s</title>\n", VERSION);
-
- /* CSS */
- cur += sprintf(cur, "<style>\n");
- cur += sprintf(cur, ".caca { font-family: monospace, fixed; font-weight: bold; }");
- for(x = 0; x < 0x100; x++)
- {
- cur += sprintf(cur, ".b%02x { color:#%03x; background-color:#%03x; }\n",
- x, palette[x & 0xf ], palette[x >> 4]);
- }
- cur += sprintf(cur, "</style>\n</head>\n<body>\n");
-
- cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\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, "<span class='b%02x'>", 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, "</span>");
- }
- /* New line */
- cur += sprintf(cur, "<br />\n");
- }
-
- cur += sprintf(cur, "</div></body></html>\n");
-
- /* Crop to really used size */
- *size = (strlen(qq->html_buffer) + 1) * sizeof(char);
- qq->html_buffer = realloc(qq->html_buffer, *size);
-
- return qq->html_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 *cur;
- unsigned int x, y, len;
-
- /* 13000 -> css palette
- * 40 -> max size used for a pixel (plus 10, never know) */
-
- if(qq->html3_buffer)
- free(qq->html3_buffer);
-
- qq->html3_buffer = malloc((13000 + ((qq->width*qq->height)*40))*sizeof(char));
- if(qq->html3_buffer == NULL)
- return NULL;
-
- cur = qq->html3_buffer;
-
- /* Table */
- cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\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, "<tr>");
-
- 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, "<td bgcolor=#%06x", palette[lineattr[x] >> 4]);
-
- if(len > 1)
- cur += sprintf(cur, " colspan=%d", len);
-
- cur += sprintf(cur, "><font color=#%06x>",
- 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, "</font></td>");
- }
- cur += sprintf(cur, "</tr>\n");
- }
-
- /* Footer */
- cur += sprintf(cur, "</table>\n");
-
- /* Crop to really used size */
- *size = (strlen(qq->html3_buffer) + 1) * sizeof(char);
- qq->html3_buffer = realloc(qq->html3_buffer, *size);
-
- return qq->html3_buffer;
- }
-
|