Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

186 lignes
5.3 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. /* 13000 -> css palette
  43. * 40 -> max size used for a pixel (plus 10, never know)*/
  44. /* FIXME: Check this value */
  45. ex->size = 13000 + (qq->width * qq->height * 40);
  46. ex->buffer = malloc(ex->size);
  47. cur = ex->buffer;
  48. /* HTML header */
  49. cur += sprintf(cur, "<html>\n<head>\n<title>Generated by libcaca %s</title>\n", VERSION);
  50. /* CSS */
  51. cur += sprintf(cur, "<style>\n");
  52. cur += sprintf(cur, ".caca { font-family: monospace, fixed; font-weight: bold; }");
  53. for(x = 0; x < 0x100; x++)
  54. {
  55. cur += sprintf(cur, ".b%02x { color:#%03x; background-color:#%03x; }\n",
  56. x, palette[x & 0xf ], palette[x >> 4]);
  57. }
  58. cur += sprintf(cur, "</style>\n</head>\n<body>\n");
  59. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  60. "font-family: monospace, fixed; font-weight: bold;");
  61. for(y = 0; y < qq->height; y++)
  62. {
  63. uint8_t *lineattr = qq->attr + y * qq->width;
  64. uint32_t *linechar = qq->chars + y * qq->width;
  65. for(x = 0; x < qq->width; x += len)
  66. {
  67. cur += sprintf(cur, "<span class='b%02x'>", lineattr[x]);
  68. for(len = 0;
  69. x + len < qq->width && lineattr[x + len] == lineattr[x];
  70. len++)
  71. {
  72. if(linechar[x + len] == (uint32_t)' ')
  73. cur += sprintf(cur, "&nbsp;");
  74. else
  75. cur += sprintf(cur, "%c", linechar[x + len] & 0x7f);
  76. }
  77. cur += sprintf(cur, "</span>");
  78. }
  79. /* New line */
  80. cur += sprintf(cur, "<br />\n");
  81. }
  82. cur += sprintf(cur, "</div></body></html>\n");
  83. /* Crop to really used size */
  84. ex->size = strlen(ex->buffer) + 1;
  85. ex->buffer = realloc(ex->buffer, ex->size);
  86. }
  87. /** \brief Generate HTML3 representation of current image.
  88. *
  89. * This function generates and returns the HTML3 representation of
  90. * the current image. It is way bigger than cucul_get_html(), but
  91. * permits viewing in old browsers (or limited ones such as links)
  92. * Won't work under gecko (mozilla rendering engine) unless you set
  93. * a correct header.
  94. */
  95. void _cucul_get_html3(cucul_t *qq, struct cucul_buffer *ex)
  96. {
  97. static int const palette[] =
  98. {
  99. 0x000000, 0x000088, 0x008800, 0x008888,
  100. 0x880000, 0x880088, 0x888800, 0x888888,
  101. 0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
  102. 0xff4444, 0xff44ff, 0xffff44, 0xffffff,
  103. };
  104. char *cur;
  105. unsigned int x, y, len;
  106. /* 13000 -> css palette
  107. * 40 -> max size used for a pixel (plus 10, never know) */
  108. ex->size = 13000 + (qq->width * qq->height * 40);
  109. ex->buffer = malloc(ex->size);
  110. cur = ex->buffer;
  111. /* Table */
  112. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  113. qq->height);
  114. for(y = 0; y < qq->height; y++)
  115. {
  116. uint8_t *lineattr = qq->attr + y * qq->width;
  117. uint32_t *linechar = qq->chars + y * qq->width;
  118. cur += sprintf(cur, "<tr>");
  119. for(x = 0; x < qq->width; x += len)
  120. {
  121. unsigned int i;
  122. /* Use colspan option to factorize cells with same attributes
  123. * (see below) */
  124. len = 1;
  125. while(x + len < qq->width && lineattr[x + len] == lineattr[x])
  126. len++;
  127. cur += sprintf(cur, "<td bgcolor=#%06x", palette[lineattr[x] >> 4]);
  128. if(len > 1)
  129. cur += sprintf(cur, " colspan=%d", len);
  130. cur += sprintf(cur, "><font color=#%06x>",
  131. palette[lineattr[x] & 0x0f]);
  132. for(i = 0; i < len; i++)
  133. {
  134. if(linechar[x + i] == (uint32_t)' ')
  135. cur += sprintf(cur, "&nbsp;");
  136. else
  137. cur += sprintf(cur, "%c", linechar[x + i] & 0x7f);
  138. }
  139. cur += sprintf(cur, "</font></td>");
  140. }
  141. cur += sprintf(cur, "</tr>\n");
  142. }
  143. /* Footer */
  144. cur += sprintf(cur, "</table>\n");
  145. /* Crop to really used size */
  146. ex->size = strlen(ex->buffer) + 1;
  147. ex->buffer = realloc(ex->buffer, ex->size);
  148. }