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.
 
 
 
 
 
 

145 regels
3.6 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. void _cucul_get_ps(cucul_t *qq, struct cucul_buffer *ex)
  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. /* 200 is arbitrary but should be ok */
  82. ex->size = strlen(ps_header) + (qq->width * qq->height * 200);
  83. ex->buffer = malloc(ex->size);
  84. cur = ex->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. ex->size = strlen(ex->buffer) + 1;
  116. ex->buffer = realloc(ex->buffer, ex->size);
  117. }