Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

151 rader
4.6 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the Do What The Fuck You Want To
  11. * Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains export functions for SVG (Scalable Vector Graphics files
  16. */
  17. #include "config.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdlib.h>
  20. # include <stdio.h>
  21. # include <string.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "cucul_internals.h"
  25. static char const svg_header[] =
  26. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  27. "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
  28. " xmlns=\"http://www.w3.org/2000/svg\""
  29. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  30. " xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n"
  31. " <defs>\n"
  32. " <style type=\"text/css\">\n"
  33. " <![CDATA[\n";
  34. /** \brief Generate SVG representation of current image.
  35. *
  36. * This function generates and returns a SVG representation of
  37. * the current image.
  38. */
  39. void _cucul_get_svg(cucul_t *qq, cucul_buffer_t *ex)
  40. {
  41. static int const palette[] =
  42. {
  43. 0x000000, 0x000088, 0x008800, 0x008888,
  44. 0x880000, 0x880088, 0x888800, 0x888888,
  45. 0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
  46. 0xff4444, 0xff44ff, 0xffff44, 0xffffff,
  47. };
  48. char *cur;
  49. unsigned int x, y;
  50. /* 200 is arbitrary but should be ok */
  51. ex->size = strlen(svg_header) + (qq->width * qq->height * 200);
  52. ex->data = malloc(ex->size);
  53. cur = ex->data;
  54. /* Header */
  55. cur += sprintf(cur, svg_header, qq->width * 6, qq->height * 10,
  56. 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", x, palette[x >> 4]);
  61. cur += sprintf(cur, ".f%02x {fill:#%06X}\n", x, palette[x & 0xf]);
  62. }
  63. cur += sprintf(cur, "]]>\n");
  64. cur += sprintf(cur, " </style>\n");
  65. cur += sprintf(cur, " </defs>\n");
  66. cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"12\">\n");
  67. /* Background */
  68. for(y = 0; y < qq->height; y++)
  69. {
  70. uint32_t *lineattr = qq->attr + y * qq->width;
  71. for(x = 0; x < qq->width; x++)
  72. {
  73. cur += sprintf(cur, "<rect class=\"b%02x\" x=\"%d\" y=\"%d\""
  74. " width=\"6\" height=\"10\"/>\n",
  75. _cucul_argb32_to_ansi8(*lineattr++),
  76. x * 6, y * 10);
  77. }
  78. }
  79. /* Text */
  80. for(y = 0; y < qq->height; y++)
  81. {
  82. uint32_t *lineattr = qq->attr + y * qq->width;
  83. uint32_t *linechar = qq->chars + y * qq->width;
  84. for(x = 0; x < qq->width; x++)
  85. {
  86. uint32_t c = *linechar++;
  87. cur += sprintf(cur, "<text class=\"f%02x\" x=\"%d\" y=\"%d\">",
  88. _cucul_argb32_to_ansi8(*lineattr++),
  89. 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 = (uintptr_t)(cur - ex->data);
  125. ex->data = realloc(ex->data, ex->size);
  126. }