Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

153 řádky
3.8 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 const *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. "/Courier-Bold findfont \n"
  46. "8 scalefont \n"
  47. "setfont\n"
  48. "gsave \n"
  49. "6 10 scale \n";
  50. /** \brief Generate Postscript representation of current image.
  51. *
  52. * This function generates and returns a Postscript representation of
  53. * the current image.
  54. */
  55. char* cucul_get_ps(cucul_t *qq, int *size)
  56. {
  57. char *cur;
  58. int x, y;
  59. static float const paletteR[] =
  60. {
  61. 0, 0, 0, 0,
  62. 0.5, 0.5, 0.5, 0.5,
  63. 0.5, 0.5, 0.5, 0.5,
  64. 1.0, 1.0, 1.0, 1.0,
  65. };
  66. static float const paletteG[] =
  67. {
  68. 0, 0, 0.5, 0.5,
  69. 0, 0, 0.5, 0.5,
  70. 0, 0, 1.0, 1.0,
  71. 0, 0, 1.0, 1.0,
  72. };
  73. static float const paletteB[] =
  74. {
  75. 0, 0.5, 0, 0.5,
  76. 0, 0.5, 0, 0.5,
  77. 0, 1.0, 0.5, 1.0,
  78. 0, 1.0, 0, 1.0,
  79. };
  80. if(qq->ps_buffer)
  81. free(qq->ps_buffer);
  82. /* 200 is arbitrary but should be ok */
  83. qq->ps_buffer = malloc((strlen(ps_header) + (qq->height*qq->width)*200) * sizeof(char));
  84. cur = qq->ps_buffer;
  85. /* Header */
  86. cur += sprintf(cur, "%s", ps_header);
  87. /* Background, drawn using csquare macro defined in header */
  88. for(y=(int)(qq->height-1);y>=0; y--) {
  89. uint8_t *lineattr = qq->attr + y * qq->width;
  90. for(x = 0; x < (int)qq->width; x++) {
  91. float bgR = paletteR[lineattr[x] >> 4];
  92. float bgG = paletteG[lineattr[x] >> 4];
  93. float bgB = paletteB[lineattr[x] >> 4];
  94. cur += sprintf(cur, "1 0 translate \n %f %f %f csquare\n", bgR, bgG, bgB);
  95. }
  96. /* Return to beginning of the line, and jump to the next one */
  97. cur += sprintf(cur, "%d 1 translate \n", -qq->width);
  98. }
  99. cur += sprintf(cur, "grestore\n"); // Restore normal transformation matrix
  100. for(y=(int)(qq->height-1);y>=0; y--) {
  101. uint8_t *lineattr = qq->attr + y * qq->width;
  102. uint32_t *linechar = qq->chars + y * qq->width;
  103. for(x = 0; x < (int)qq->width; x++) {
  104. uint32_t cR = linechar[x];
  105. float fgR = paletteR[lineattr[x] & 0x0f];
  106. float fgG = paletteG[lineattr[x] & 0x0f];
  107. float fgB = paletteB[lineattr[x] & 0x0f];
  108. cur += sprintf(cur, "newpath\n%d %d moveto\n", (x+1)*6, (y)*10);
  109. cur += sprintf(cur, "%f %f %f setrgbcolor\n", fgR, fgG, fgB);
  110. cur += sprintf(cur, "(%c) show\n", cR);
  111. }
  112. }
  113. cur += sprintf(cur, "showpage");
  114. /* Crop to really used size */
  115. *size = (strlen(qq->ps_buffer) + 1) * sizeof(char);
  116. qq->ps_buffer = realloc(qq->ps_buffer, *size);
  117. return qq->ps_buffer;
  118. }