You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

199 lines
5.7 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: export.c 361 2006-03-09 13:24:06Z jylam $
  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. char* cucul_get_html(cucul_t *qq, int *size)
  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. if(qq->html_buffer)
  46. free(qq->html_buffer);
  47. qq->html_buffer = malloc((13000 + ((qq->width*qq->height) * 40)) * sizeof(char));
  48. if(qq->html_buffer == NULL)
  49. return NULL;
  50. cur = qq->html_buffer;
  51. /* HTML header */
  52. cur += sprintf(cur, "<html>\n<head>\n<title>Generated by libcaca %s</title>\n", VERSION);
  53. /* CSS */
  54. cur += sprintf(cur, "<style>\n");
  55. cur += sprintf(cur, ".caca { font-family: monospace, fixed; font-weight: bold; }");
  56. for(x = 0; x < 0x100; x++)
  57. {
  58. cur += sprintf(cur, ".b%02x { color:#%03x; background-color:#%03x; }\n",
  59. x, palette[x & 0xf ], palette[x >> 4]);
  60. }
  61. cur += sprintf(cur, "</style>\n</head>\n<body>\n");
  62. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  63. "font-family: monospace, fixed; font-weight: bold;");
  64. for(y = 0; y < qq->height; y++)
  65. {
  66. uint8_t *lineattr = qq->attr + y * qq->width;
  67. uint32_t *linechar = qq->chars + y * qq->width;
  68. for(x = 0; x < qq->width; x += len)
  69. {
  70. cur += sprintf(cur, "<span class='b%02x'>", lineattr[x]);
  71. for(len = 0;
  72. x + len < qq->width && lineattr[x + len] == lineattr[x];
  73. len++)
  74. {
  75. if(linechar[x + len] == (uint32_t)' ')
  76. cur += sprintf(cur, "&nbsp;");
  77. else
  78. cur += sprintf(cur, "%c", linechar[x + len] & 0x7f);
  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. *size = (strlen(qq->html_buffer) + 1) * sizeof(char);
  88. qq->html_buffer = realloc(qq->html_buffer, *size);
  89. return qq->html_buffer;
  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. char* cucul_get_html3(cucul_t *qq, int *size)
  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. /* 13000 -> css palette
  111. * 40 -> max size used for a pixel (plus 10, never know) */
  112. if(qq->html3_buffer)
  113. free(qq->html3_buffer);
  114. qq->html3_buffer = malloc((13000 + ((qq->width*qq->height)*40))*sizeof(char));
  115. if(qq->html3_buffer == NULL)
  116. return NULL;
  117. cur = qq->html3_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] == (uint32_t)' ')
  142. cur += sprintf(cur, "&nbsp;");
  143. else
  144. cur += sprintf(cur, "%c", linechar[x + i] & 0x7f);
  145. }
  146. cur += sprintf(cur, "</font></td>");
  147. }
  148. cur += sprintf(cur, "</tr>\n");
  149. }
  150. /* Footer */
  151. cur += sprintf(cur, "</table>\n");
  152. /* Crop to really used size */
  153. *size = (strlen(qq->html3_buffer) + 1) * sizeof(char);
  154. qq->html3_buffer = realloc(qq->html3_buffer, *size);
  155. return qq->html3_buffer;
  156. }