Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

193 рядки
6.0 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  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_html.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. uint32_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'>",
  69. _cucul_argb32_to_ansi8(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_export *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. uint32_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",
  135. palette[_cucul_argb32_to_ansi4bg(lineattr[x])]);
  136. if(len > 1)
  137. cur += sprintf(cur, " colspan=%d", len);
  138. cur += sprintf(cur, "><font color=#%06x>",
  139. palette[_cucul_argb32_to_ansi4fg(lineattr[x])]);
  140. for(i = 0; i < len; i++)
  141. {
  142. if(linechar[x + i] <= 0x00000020)
  143. cur += sprintf(cur, "&nbsp;");
  144. else if(linechar[x + i] < 0x00000080)
  145. cur += sprintf(cur, "%c", linechar[x + i]);
  146. else
  147. cur += sprintf(cur, "&#%i;", linechar[x + i]);
  148. }
  149. cur += sprintf(cur, "</font></td>");
  150. }
  151. cur += sprintf(cur, "</tr>\n");
  152. }
  153. /* Footer */
  154. cur += sprintf(cur, "</table>\n");
  155. /* Crop to really used size */
  156. ex->size = (uintptr_t)(cur - ex->buffer);
  157. ex->buffer = realloc(ex->buffer, ex->size);
  158. }