Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

130 рядки
3.9 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. /*
  32. * This function generates and returns an SVG representation of
  33. * the current image.
  34. */
  35. void _cucul_get_svg(cucul_t *qq, cucul_buffer_t *ex)
  36. {
  37. char *cur;
  38. unsigned int x, y;
  39. /* 200 is arbitrary but should be ok */
  40. ex->size = strlen(svg_header) + (qq->width * qq->height * 200);
  41. ex->data = malloc(ex->size);
  42. cur = ex->data;
  43. /* Header */
  44. cur += sprintf(cur, svg_header, qq->width * 6, qq->height * 10,
  45. qq->width * 6, qq->height * 10);
  46. cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"12\">\n");
  47. /* Background */
  48. for(y = 0; y < qq->height; y++)
  49. {
  50. uint32_t *lineattr = qq->attr + y * qq->width;
  51. for(x = 0; x < qq->width; x++)
  52. {
  53. cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
  54. " width=\"6\" height=\"10\"/>\n",
  55. _cucul_argb32_to_rgb12bg(*lineattr++),
  56. x * 6, y * 10);
  57. }
  58. }
  59. /* Text */
  60. for(y = 0; y < qq->height; y++)
  61. {
  62. uint32_t *lineattr = qq->attr + y * qq->width;
  63. uint32_t *linechar = qq->chars + y * qq->width;
  64. for(x = 0; x < qq->width; x++)
  65. {
  66. uint32_t c = *linechar++;
  67. cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
  68. "x=\"%d\" y=\"%d\">",
  69. _cucul_argb32_to_rgb12fg(*lineattr++),
  70. x * 6, (y * 10) + 10);
  71. if(c < 0x00000020)
  72. cur += sprintf(cur, "?");
  73. else if(c > 0x0000007f)
  74. {
  75. static const uint8_t mark[7] =
  76. {
  77. 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
  78. };
  79. char buf[10], *parser;
  80. int bytes = (c < 0x800) ? 2 : (c < 0x10000) ? 3 : 4;
  81. buf[bytes] = '\0';
  82. parser = buf + bytes;
  83. switch(bytes)
  84. {
  85. case 4: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  86. case 3: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  87. case 2: *--parser = (c | 0x80) & 0xbf; c >>= 6;
  88. }
  89. *--parser = c | mark[bytes];
  90. cur += sprintf(cur, "%s", buf);
  91. }
  92. else switch((uint8_t)c)
  93. {
  94. case '>': cur += sprintf(cur, "&gt;"); break;
  95. case '<': cur += sprintf(cur, "&lt;"); break;
  96. case '&': cur += sprintf(cur, "&amp;"); break;
  97. default: cur += sprintf(cur, "%c", c); break;
  98. }
  99. cur += sprintf(cur, "</text>\n");
  100. }
  101. }
  102. cur += sprintf(cur, " </g>\n");
  103. cur += sprintf(cur, "</svg>\n");
  104. /* Crop to really used size */
  105. ex->size = (uintptr_t)(cur - ex->data);
  106. ex->data = realloc(ex->data, ex->size);
  107. }