Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

191 righe
5.8 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. /** \brief Generate HTML representation of current image.
  28. *
  29. * This function generates and returns the HTML representation of
  30. * the current image.
  31. */
  32. void _cucul_get_html(cucul_t *qq, struct cucul_export *ex)
  33. {
  34. static int const palette[] =
  35. {
  36. 0x000, 0x008, 0x080, 0x088, 0x800, 0x808, 0x880, 0x888,
  37. 0x444, 0x44f, 0x4f4, 0x4ff, 0xf44, 0xf4f, 0xff4, 0xfff,
  38. };
  39. char *cur;
  40. unsigned int x, y, len;
  41. /* The CSS palette: roughly 13000 bytes
  42. * A line: 7 chars for "<br />\n"
  43. * A glyph: 18 chars for "<span class='bxx'>"
  44. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  45. * 7 chars for "</span>" */
  46. ex->size = 13000 + qq->height * (7 + qq->width * (18 + 9 + 7));
  47. ex->buffer = malloc(ex->size);
  48. cur = ex->buffer;
  49. /* HTML header */
  50. cur += sprintf(cur, "<html>\n<head>\n<title>Generated by libcaca %s</title>\n", VERSION);
  51. /* CSS */
  52. cur += sprintf(cur, "<style>\n");
  53. cur += sprintf(cur, ".caca { font-family: monospace, fixed; font-weight: bold; }");
  54. for(x = 0; x < 0x100; x++)
  55. {
  56. cur += sprintf(cur, ".b%02x { color:#%03x; background-color:#%03x; }\n",
  57. x, palette[x & 0xf ], palette[x >> 4]);
  58. }
  59. cur += sprintf(cur, "</style>\n</head>\n<body>\n");
  60. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  61. "font-family: monospace, fixed; font-weight: bold;");
  62. for(y = 0; y < qq->height; y++)
  63. {
  64. uint8_t *lineattr = qq->attr + y * qq->width;
  65. uint32_t *linechar = qq->chars + y * qq->width;
  66. for(x = 0; x < qq->width; x += len)
  67. {
  68. cur += sprintf(cur, "<span class='b%02x'>", lineattr[x]);
  69. for(len = 0;
  70. x + len < qq->width && lineattr[x + len] == lineattr[x];
  71. len++)
  72. {
  73. if(linechar[x + len] <= 0x00000020)
  74. cur += sprintf(cur, "&nbsp;");
  75. else if(linechar[x + len] < 0x00000080)
  76. cur += sprintf(cur, "%c", linechar[x + len]);
  77. else
  78. cur += sprintf(cur, "&#%i;", linechar[x + len]);
  79. }
  80. cur += sprintf(cur, "</span>");
  81. }
  82. /* New line */
  83. cur += sprintf(cur, "<br />\n");
  84. }
  85. cur += sprintf(cur, "</div></body></html>\n");
  86. /* Crop to really used size */
  87. ex->size = strlen(ex->buffer) + 1;
  88. ex->buffer = realloc(ex->buffer, ex->size);
  89. }
  90. /** \brief Generate HTML3 representation of current image.
  91. *
  92. * This function generates and returns the HTML3 representation of
  93. * the current image. It is way bigger than cucul_get_html(), but
  94. * permits viewing in old browsers (or limited ones such as links)
  95. * Won't work under gecko (mozilla rendering engine) unless you set
  96. * a correct header.
  97. */
  98. void _cucul_get_html3(cucul_t *qq, struct cucul_export *ex)
  99. {
  100. static int const palette[] =
  101. {
  102. 0x000000, 0x000088, 0x008800, 0x008888,
  103. 0x880000, 0x880088, 0x888800, 0x888888,
  104. 0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
  105. 0xff4444, 0xff44ff, 0xffff44, 0xffffff,
  106. };
  107. char *cur;
  108. unsigned int x, y, len;
  109. /* The CSS palette: roughly 13000 bytes
  110. * A line: 10 chars for "<tr></tr>\n"
  111. * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
  112. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  113. * 12 chars for "</font></td>" */
  114. ex->size = 13000 + qq->height * (10 + qq->width * (40 + 9 + 12));
  115. ex->buffer = malloc(ex->size);
  116. cur = ex->buffer;
  117. /* Table */
  118. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  119. qq->height);
  120. for(y = 0; y < qq->height; y++)
  121. {
  122. uint8_t *lineattr = qq->attr + y * qq->width;
  123. uint32_t *linechar = qq->chars + y * qq->width;
  124. cur += sprintf(cur, "<tr>");
  125. for(x = 0; x < qq->width; x += len)
  126. {
  127. unsigned int i;
  128. /* Use colspan option to factorize cells with same attributes
  129. * (see below) */
  130. len = 1;
  131. while(x + len < qq->width && lineattr[x + len] == lineattr[x])
  132. len++;
  133. cur += sprintf(cur, "<td bgcolor=#%06x", palette[lineattr[x] >> 4]);
  134. if(len > 1)
  135. cur += sprintf(cur, " colspan=%d", len);
  136. cur += sprintf(cur, "><font color=#%06x>",
  137. palette[lineattr[x] & 0x0f]);
  138. for(i = 0; i < len; i++)
  139. {
  140. if(linechar[x + i] <= 0x00000020)
  141. cur += sprintf(cur, "&nbsp;");
  142. else if(linechar[x + i] < 0x00000080)
  143. cur += sprintf(cur, "%c", linechar[x + i]);
  144. else
  145. cur += sprintf(cur, "&#%i;", linechar[x + i]);
  146. }
  147. cur += sprintf(cur, "</font></td>");
  148. }
  149. cur += sprintf(cur, "</tr>\n");
  150. }
  151. /* Footer */
  152. cur += sprintf(cur, "</table>\n");
  153. /* Crop to really used size */
  154. ex->size = strlen(ex->buffer) + 1;
  155. ex->buffer = realloc(ex->buffer, ex->size);
  156. }