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.
 
 
 
 
 
 

159 lines
3.7 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 Postscript 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. char ps_header[] = {"%%! \n \
  28. %%%% libcaca PDF export \n \
  29. %%%%LanguageLevel: 2 \n \
  30. %%%%Pages: 1 \n \
  31. %%%%DocumentData: Clean7Bit \n \
  32. /csquare { \n \
  33. newpath \n \
  34. 0 0 moveto \n \
  35. 0 1 rlineto \n \
  36. 1 0 rlineto \n \
  37. 0 -1 rlineto \n \
  38. closepath \n \
  39. setrgbcolor \n \
  40. fill \n \
  41. } def \n \
  42. /S { \n \
  43. Show \n \
  44. } bind def \n \
  45. \n \
  46. \n \
  47. /Times-Roman findfont \n \
  48. 8 scalefont \n \
  49. setfont\n \
  50. 6 10 scale \n"};
  51. /** \brief Generate Postscript representation of current image.
  52. *
  53. * This function generates and returns a Postscript representation of
  54. * the current image.
  55. */
  56. char* cucul_get_ps(cucul_t *qq, int *size)
  57. {
  58. static float const paletteR[] =
  59. {
  60. 0, 0, 0, 0,
  61. 0.5, 0.5, 0.5, 0.5,
  62. 0.5, 0.5, 0.5, 0.5,
  63. 1.0, 1.0, 1.0, 1.0,
  64. };
  65. static float const paletteG[] =
  66. {
  67. 0, 0, 0.5, 0.5,
  68. 0, 0, 0.5, 0.5,
  69. 0, 0, 1.0, 1.0,
  70. 0, 0, 1.0, 1.0,
  71. };
  72. static float const paletteB[] =
  73. {
  74. 0, 0.5, 0, 0.5,
  75. 0, 0.5, 0, 0.5,
  76. 0, 1.0, 0.5, 1.0,
  77. 0, 1.0, 0, 1.0,
  78. };
  79. char *cur;
  80. unsigned int x, y;
  81. if(qq->ps_buffer)
  82. free(qq->ps_buffer);
  83. /* 400 is arbitrary and needs to be precised */
  84. qq->ps_buffer = malloc((strlen(ps_header) + (qq->height*qq->width)*400) * sizeof(char));
  85. cur = qq->ps_buffer;
  86. /* Header */
  87. cur += sprintf(cur, "%s", ps_header);
  88. /* Background, drawn using csquare macro defined in header */
  89. for(y=0;y<qq->height; y++) {
  90. uint8_t *lineattr = qq->attr + y * qq->width;
  91. for(x = 0; x < qq->width; x++) {
  92. float bgR = paletteR[lineattr[x] >> 4];
  93. float bgG = paletteG[lineattr[x] >> 4];
  94. float bgB = paletteB[lineattr[x] >> 4];
  95. cur += sprintf(cur, "1 0 translate \n %f %f %f csquare\n", bgR, bgG, bgB);
  96. }
  97. /* Return to beginning of the line, and jump to the next one */
  98. cur += sprintf(cur, "%d 1 translate \n", -qq->width);
  99. }
  100. /* Text. FIXME, doesn't work yet */
  101. cur += sprintf(cur, "1 1 scale\n");
  102. cur += sprintf(cur, "newpath\n0 0 moveto\n");
  103. for(y=0;y<qq->height; y++) {
  104. uint8_t *lineattr = qq->attr + y * qq->width;
  105. uint32_t *linechar = qq->chars + y * qq->width;
  106. for(x = 0; x < qq->width; x++) {
  107. uint32_t cR = linechar[x];
  108. float fgR = paletteR[lineattr[x] & 0x0f];
  109. float fgG = paletteG[lineattr[x] & 0x0f];
  110. float fgB = paletteB[lineattr[x] & 0x0f];
  111. cur += sprintf(cur, "%f %f %f setrgbcolor\n", fgR, fgG, fgB);
  112. cur += sprintf(cur, "1 0 translate\n");
  113. cur += sprintf(cur, "{%c} \n", cR);
  114. }
  115. cur += sprintf(cur, "%d 1 translate \n", -qq->width);
  116. }
  117. cur += sprintf(cur, "showpage");
  118. /* Crop to really used size */
  119. *size = (strlen(qq->ps_buffer) + 1) * sizeof(char);
  120. qq->ps_buffer = realloc(qq->ps_buffer, *size);
  121. return qq->ps_buffer;
  122. }