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.
 
 
 
 
 
 

153 lines
4.7 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_svg.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 SVG (Scalable Vector Graphics files
  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. static char const svg_header[] =
  28. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  29. "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
  30. " xmlns=\"http://www.w3.org/2000/svg\""
  31. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  32. " xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n"
  33. " <defs>\n"
  34. " <style type=\"text/css\">\n"
  35. " <![CDATA[\n";
  36. /** \brief Generate SVG representation of current image.
  37. *
  38. * This function generates and returns a SVG representation of
  39. * the current image.
  40. */
  41. void _cucul_get_svg(cucul_t *qq, struct cucul_export *ex)
  42. {
  43. static int const palette[] =
  44. {
  45. 0x000000, 0x000088, 0x008800, 0x008888,
  46. 0x880000, 0x880088, 0x888800, 0x888888,
  47. 0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
  48. 0xff4444, 0xff44ff, 0xffff44, 0xffffff,
  49. };
  50. char *cur;
  51. unsigned int x, y;
  52. /* 200 is arbitrary but should be ok */
  53. ex->size = strlen(svg_header) + (qq->width * qq->height * 200);
  54. ex->buffer = malloc(ex->size);
  55. cur = ex->buffer;
  56. /* Header */
  57. cur += sprintf(cur, svg_header, qq->width * 6, qq->height * 10,
  58. qq->width * 6, qq->height * 10);
  59. /* Precalc of colors in CSS style */
  60. for(x = 0; x < 0x100; x++)
  61. {
  62. cur += sprintf(cur, ".b%02x {fill:#%06X}\n", x, palette[x >> 4]);
  63. cur += sprintf(cur, ".f%02x {fill:#%06X}\n", x, palette[x & 0xf]);
  64. }
  65. cur += sprintf(cur, "]]>\n");
  66. cur += sprintf(cur, " </style>\n");
  67. cur += sprintf(cur, " </defs>\n");
  68. cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"12\">\n");
  69. /* Background */
  70. for(y = 0; y < qq->height; y++)
  71. {
  72. uint32_t *lineattr = qq->attr + y * qq->width;
  73. for(x = 0; x < qq->width; x++)
  74. {
  75. cur += sprintf(cur, "<rect class=\"b%02x\" x=\"%d\" y=\"%d\""
  76. " width=\"6\" height=\"10\"/>\n",
  77. _cucul_argb32_to_ansi8(*lineattr++),
  78. x * 6, y * 10);
  79. }
  80. }
  81. /* Text */
  82. for(y = 0; y < qq->height; y++)
  83. {
  84. uint32_t *lineattr = qq->attr + y * qq->width;
  85. uint32_t *linechar = qq->chars + y * qq->width;
  86. for(x = 0; x < qq->width; x++)
  87. {
  88. uint32_t c = *linechar++;
  89. cur += sprintf(cur, "<text class=\"f%02x\" x=\"%d\" y=\"%d\">",
  90. _cucul_argb32_to_ansi8(*lineattr++),
  91. x * 6, (y * 10) + 10);
  92. if(c < 0x00000020)
  93. cur += sprintf(cur, "?");
  94. else if(c > 0x0000007f)
  95. {
  96. static const uint8_t mark[7] =
  97. {
  98. 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
  99. };
  100. char buf[10], *parser;
  101. int bytes = (c < 0x800) ? 2 : (c < 0x10000) ? 3 : 4;
  102. buf[bytes] = '\0';
  103. parser = buf + bytes;
  104. switch(bytes)
  105. {
  106. case 4: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  107. case 3: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  108. case 2: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  109. }
  110. *--parser = c | mark[bytes];
  111. cur += sprintf(cur, "%s", buf);
  112. }
  113. else switch((uint8_t)c)
  114. {
  115. case '>': cur += sprintf(cur, "&gt;"); break;
  116. case '<': cur += sprintf(cur, "&lt;"); break;
  117. case '&': cur += sprintf(cur, "&amp;"); break;
  118. default: cur += sprintf(cur, "%c", c); break;
  119. }
  120. cur += sprintf(cur, "</text>\n");
  121. }
  122. }
  123. cur += sprintf(cur, " </g>\n");
  124. cur += sprintf(cur, "</svg>\n");
  125. /* Crop to really used size */
  126. ex->size = (uintptr_t)(cur - ex->buffer);
  127. ex->buffer = realloc(ex->buffer, ex->size);
  128. }