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.

export_html.c 5.9 KiB

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