Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

152 rindas
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. static char const *ps_header =
  28. "%%! \n"
  29. "%%%% libcaca PDF export \n"
  30. "%%%%LanguageLevel: 2 \n"
  31. "%%%%Pages: 1 \n"
  32. "%%%%DocumentData: Clean7Bit \n"
  33. "/csquare { \n"
  34. " newpath \n"
  35. " 0 0 moveto \n"
  36. " 0 1 rlineto \n"
  37. " 1 0 rlineto \n"
  38. " 0 -1 rlineto \n"
  39. " closepath \n"
  40. " setrgbcolor \n"
  41. " fill \n"
  42. "} def \n"
  43. "/S { \n"
  44. " Show \n"
  45. "} bind def \n"
  46. "/Courier-Bold findfont \n"
  47. "8 scalefont \n"
  48. "setfont\n"
  49. "gsave \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. char *cur;
  59. int x, y;
  60. static float const paletteR[] =
  61. {
  62. 0, 0, 0, 0,
  63. 0.5, 0.5, 0.5, 0.5,
  64. 0.5, 0.5, 0.5, 0.5,
  65. 1.0, 1.0, 1.0, 1.0,
  66. };
  67. static float const paletteG[] =
  68. {
  69. 0, 0, 0.5, 0.5,
  70. 0, 0, 0.5, 0.5,
  71. 0, 0, 1.0, 1.0,
  72. 0, 0, 1.0, 1.0,
  73. };
  74. static float const paletteB[] =
  75. {
  76. 0, 0.5, 0, 0.5,
  77. 0, 0.5, 0, 0.5,
  78. 0, 1.0, 0.5, 1.0,
  79. 0, 1.0, 0, 1.0,
  80. };
  81. if(qq->ps_buffer)
  82. free(qq->ps_buffer);
  83. /* 200 is arbitrary but should be ok */
  84. qq->ps_buffer = malloc((strlen(ps_header) + (qq->height*qq->width)*200) * 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=(int)(qq->height-1);y>=0; y--) {
  90. uint8_t *lineattr = qq->attr + y * qq->width;
  91. for(x = 0; x < (int)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. cur += sprintf(cur, "grestore\n"); // Restore normal transformation matrix
  101. for(y=(int)(qq->height-1);y>=0; y--) {
  102. uint8_t *lineattr = qq->attr + y * qq->width;
  103. uint32_t *linechar = qq->chars + y * qq->width;
  104. for(x = 0; x < (int)qq->width; x++) {
  105. uint32_t cR = linechar[x];
  106. float fgR = paletteR[lineattr[x] & 0x0f];
  107. float fgG = paletteG[lineattr[x] & 0x0f];
  108. float fgB = paletteB[lineattr[x] & 0x0f];
  109. cur += sprintf(cur, "newpath\n%d %d moveto\n", (x+1)*6, (y)*10);
  110. cur += sprintf(cur, "%f %f %f setrgbcolor\n", fgR, fgG, fgB);
  111. cur += sprintf(cur, "(%c) show\n", cR);
  112. }
  113. }
  114. cur += sprintf(cur, "showpage");
  115. /* Crop to really used size */
  116. *size = (strlen(qq->ps_buffer) + 1) * sizeof(char);
  117. qq->ps_buffer = realloc(qq->ps_buffer, *size);
  118. return qq->ps_buffer;
  119. }