No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

195 líneas
5.9 KiB

  1. /*
  2. * libcucul Unicode canvas library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file export.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \author Jean-Yves Lamoureux <jylam@lnxscene.org>
  15. * \brief Export function
  16. *
  17. * This file contains export functions for HTML and HTML3
  18. */
  19. #include "config.h"
  20. #if !defined(__KERNEL__)
  21. # include <stdlib.h>
  22. # include <stdio.h>
  23. # include <string.h>
  24. #endif
  25. #include "cucul.h"
  26. #include "cucul_internals.h"
  27. /* HTML */
  28. /** \brief Generate HTML representation of current image.
  29. *
  30. * This function generates and returns the HTML representation of
  31. * the current image.
  32. */
  33. void _cucul_get_html(cucul_t *qq, struct cucul_buffer *ex)
  34. {
  35. static int const palette[] =
  36. {
  37. 0x000, 0x008, 0x080, 0x088, 0x800, 0x808, 0x880, 0x888,
  38. 0x444, 0x44f, 0x4f4, 0x4ff, 0xf44, 0xf4f, 0xff4, 0xfff,
  39. };
  40. char *cur;
  41. unsigned int x, y, len;
  42. /* The CSS palette: roughly 13000 bytes
  43. * A line: 7 chars for "<br />\n"
  44. * A glyph: 18 chars for "<span class='bxx'>"
  45. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  46. * 7 chars for "</span>" */
  47. ex->size = 13000 + qq->height * (7 + qq->width * (18 + 9 + 7));
  48. ex->buffer = malloc(ex->size);
  49. cur = ex->buffer;
  50. /* HTML header */
  51. cur += sprintf(cur, "<html>\n<head>\n<title>Generated by libcaca %s</title>\n", VERSION);
  52. /* CSS */
  53. cur += sprintf(cur, "<style>\n");
  54. cur += sprintf(cur, ".caca { font-family: monospace, fixed; font-weight: bold; }");
  55. for(x = 0; x < 0x100; x++)
  56. {
  57. cur += sprintf(cur, ".b%02x { color:#%03x; background-color:#%03x; }\n",
  58. x, palette[x & 0xf ], palette[x >> 4]);
  59. }
  60. cur += sprintf(cur, "</style>\n</head>\n<body>\n");
  61. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  62. "font-family: monospace, fixed; font-weight: bold;");
  63. for(y = 0; y < qq->height; y++)
  64. {
  65. uint8_t *lineattr = qq->attr + y * qq->width;
  66. uint32_t *linechar = qq->chars + y * qq->width;
  67. for(x = 0; x < qq->width; x += len)
  68. {
  69. cur += sprintf(cur, "<span class='b%02x'>", lineattr[x]);
  70. for(len = 0;
  71. x + len < qq->width && lineattr[x + len] == lineattr[x];
  72. len++)
  73. {
  74. if(linechar[x + len] <= 0x00000020)
  75. cur += sprintf(cur, "&nbsp;");
  76. else if(linechar[x + len] < 0x00000080)
  77. cur += sprintf(cur, "%c", linechar[x + len]);
  78. else
  79. cur += sprintf(cur, "&#%i;", linechar[x + len]);
  80. }
  81. cur += sprintf(cur, "</span>");
  82. }
  83. /* New line */
  84. cur += sprintf(cur, "<br />\n");
  85. }
  86. cur += sprintf(cur, "</div></body></html>\n");
  87. /* Crop to really used size */
  88. ex->size = strlen(ex->buffer) + 1;
  89. ex->buffer = realloc(ex->buffer, ex->size);
  90. }
  91. /** \brief Generate HTML3 representation of current image.
  92. *
  93. * This function generates and returns the HTML3 representation of
  94. * the current image. It is way bigger than cucul_get_html(), but
  95. * permits viewing in old browsers (or limited ones such as links)
  96. * Won't work under gecko (mozilla rendering engine) unless you set
  97. * a correct header.
  98. */
  99. void _cucul_get_html3(cucul_t *qq, struct cucul_buffer *ex)
  100. {
  101. static int const palette[] =
  102. {
  103. 0x000000, 0x000088, 0x008800, 0x008888,
  104. 0x880000, 0x880088, 0x888800, 0x888888,
  105. 0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
  106. 0xff4444, 0xff44ff, 0xffff44, 0xffffff,
  107. };
  108. char *cur;
  109. unsigned int x, y, len;
  110. /* The CSS palette: roughly 13000 bytes
  111. * A line: 10 chars for "<tr></tr>\n"
  112. * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
  113. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  114. * 12 chars for "</font></td>" */
  115. ex->size = 13000 + qq->height * (10 + qq->width * (40 + 9 + 12));
  116. ex->buffer = malloc(ex->size);
  117. cur = ex->buffer;
  118. /* Table */
  119. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  120. qq->height);
  121. for(y = 0; y < qq->height; y++)
  122. {
  123. uint8_t *lineattr = qq->attr + y * qq->width;
  124. uint32_t *linechar = qq->chars + y * qq->width;
  125. cur += sprintf(cur, "<tr>");
  126. for(x = 0; x < qq->width; x += len)
  127. {
  128. unsigned int i;
  129. /* Use colspan option to factorize cells with same attributes
  130. * (see below) */
  131. len = 1;
  132. while(x + len < qq->width && lineattr[x + len] == lineattr[x])
  133. len++;
  134. cur += sprintf(cur, "<td bgcolor=#%06x", palette[lineattr[x] >> 4]);
  135. if(len > 1)
  136. cur += sprintf(cur, " colspan=%d", len);
  137. cur += sprintf(cur, "><font color=#%06x>",
  138. palette[lineattr[x] & 0x0f]);
  139. for(i = 0; i < len; i++)
  140. {
  141. if(linechar[x + i] <= 0x00000020)
  142. cur += sprintf(cur, "&nbsp;");
  143. else if(linechar[x + i] < 0x00000080)
  144. cur += sprintf(cur, "%c", linechar[x + i]);
  145. else
  146. cur += sprintf(cur, "&#%i;", linechar[x + i]);
  147. }
  148. cur += sprintf(cur, "</font></td>");
  149. }
  150. cur += sprintf(cur, "</tr>\n");
  151. }
  152. /* Footer */
  153. cur += sprintf(cur, "</table>\n");
  154. /* Crop to really used size */
  155. ex->size = strlen(ex->buffer) + 1;
  156. ex->buffer = realloc(ex->buffer, ex->size);
  157. }