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.
 
 
 
 
 
 

121 lines
3.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: export_irc.c 384 2006-03-13 18:07:35Z sam $
  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\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" "
  31. "xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n"
  32. " <defs>\n"
  33. " <style type=\"text/css\">\n"
  34. " <![CDATA[\n";
  35. /** \brief Generate SVG representation of current image.
  36. *
  37. * This function generates and returns a SVG representation of
  38. * the current image.
  39. */
  40. void _cucul_get_svg(cucul_t *qq, struct cucul_buffer *ex)
  41. {
  42. char *cur;
  43. int x, y;
  44. static int const palette[] =
  45. {
  46. 0x000000, 0x000088, 0x008800, 0x008888,
  47. 0x880000, 0x880088, 0x888800, 0x888888,
  48. 0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
  49. 0xff4444, 0xff44ff, 0xffff44, 0xffffff,
  50. };
  51. /* 200 is arbitrary but should be ok */
  52. ex->size = strlen(svg_header) + (qq->width * qq->height * 200);
  53. ex->buffer = malloc(ex->size);
  54. cur = ex->buffer;
  55. /* Header */
  56. cur += sprintf(cur, svg_header, qq->width*6, qq->height*10, qq->width*6, qq->height*10);
  57. /* Precalc of colors in CSS style */
  58. for(x = 0; x < 0x100; x++)
  59. {
  60. cur += sprintf(cur, ".b%02x {fill:#%06X}\n",
  61. x,
  62. palette[x >> 4 ]);
  63. cur += sprintf(cur, ".f%02x {fill:#%06X}\n",
  64. x,
  65. palette[x & 0xf ]);
  66. }
  67. cur += sprintf(cur, "]]>\n");
  68. cur += sprintf(cur, "</style>\n");
  69. cur += sprintf(cur, "</defs>\n");
  70. cur += sprintf(cur, "<g id=\"mainlayer\" font-size=\"12\">\n");
  71. /* Background */
  72. for(y=0; y<(int)(qq->height);y++) {
  73. uint8_t *lineattr = qq->attr + y * qq->width;
  74. for(x = 0; x < (int)qq->width; x++) {
  75. cur += sprintf(cur, "<rect class=\"b%02x\" x=\"%d\" y=\"%d\" width=\"6\" height=\"10\"/>\n",
  76. lineattr[x],
  77. x*6,
  78. y*10);
  79. }
  80. }
  81. /* Text */
  82. for(y=0; y<(int)(qq->height);y++) {
  83. uint8_t *lineattr = qq->attr + y * qq->width;
  84. uint32_t *linechar = qq->chars + y * qq->width;
  85. for(x = 0; x < (int)qq->width; x++) {
  86. cur += sprintf(cur, "<text class=\"f%02x\" x=\"%d\" y=\"%d\">%c</text>\n",
  87. lineattr[x],
  88. x*6,
  89. (y*10)+10,
  90. linechar[x]);
  91. }
  92. }
  93. cur += sprintf(cur, "</g>\n");
  94. cur += sprintf(cur, "</svg>\n");
  95. /* Crop to really used size */
  96. ex->size = strlen(ex->buffer) + 1;
  97. ex->buffer = realloc(ex->buffer, ex->size);
  98. }