Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

151 righe
4.5 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.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. uint8_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. *lineattr++, x * 6, y * 10);
  78. }
  79. }
  80. /* Text */
  81. for(y = 0; y < qq->height; y++)
  82. {
  83. uint8_t *lineattr = qq->attr + y * qq->width;
  84. uint32_t *linechar = qq->chars + y * qq->width;
  85. for(x = 0; x < qq->width; x++)
  86. {
  87. uint32_t c = *linechar++;
  88. cur += sprintf(cur, "<text class=\"f%02x\" x=\"%d\" y=\"%d\">",
  89. *lineattr++, x * 6, (y * 10) + 10);
  90. if(c < 0x00000020)
  91. cur += sprintf(cur, "?");
  92. else if(c > 0x0000007f)
  93. {
  94. static const uint8_t mark[7] =
  95. {
  96. 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
  97. };
  98. char buf[10], *parser;
  99. int bytes = (c < 0x800) ? 2 : (c < 0x10000) ? 3 : 4;
  100. buf[bytes] = '\0';
  101. parser = buf + bytes;
  102. switch(bytes)
  103. {
  104. case 4: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  105. case 3: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  106. case 2: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  107. }
  108. *--parser = c | mark[bytes];
  109. cur += sprintf(cur, "%s", buf);
  110. }
  111. else switch((uint8_t)c)
  112. {
  113. case '>': cur += sprintf(cur, "&gt;"); break;
  114. case '<': cur += sprintf(cur, "&lt;"); break;
  115. case '&': cur += sprintf(cur, "&amp;"); break;
  116. default: cur += sprintf(cur, "%c", c); break;
  117. }
  118. cur += sprintf(cur, "</text>\n");
  119. }
  120. }
  121. cur += sprintf(cur, " </g>\n");
  122. cur += sprintf(cur, "</svg>\n");
  123. /* Crop to really used size */
  124. ex->size = strlen(ex->buffer) + 1;
  125. ex->buffer = realloc(ex->buffer, ex->size);
  126. }