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.

export_ps.c 3.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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$
  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. //"%!PS-Adobe-2.0\n"
  29. "%!\n"
  30. "%% libcaca PDF export\n"
  31. "%%LanguageLevel: 2\n"
  32. "%%Pages: 1\n"
  33. "%%DocumentData: Clean7Bit\n"
  34. "/csquare {\n"
  35. " newpath\n"
  36. " 0 0 moveto\n"
  37. " 0 1 rlineto\n"
  38. " 1 0 rlineto\n"
  39. " 0 -1 rlineto\n"
  40. " closepath\n"
  41. " setrgbcolor\n"
  42. " fill\n"
  43. "} def\n"
  44. "/S {\n"
  45. " Show\n"
  46. "} bind def\n"
  47. "/Courier-Bold findfont\n"
  48. "8 scalefont\n"
  49. "setfont\n"
  50. "gsave\n"
  51. "6 10 scale\n";
  52. /** \brief Generate Postscript representation of current image.
  53. *
  54. * This function generates and returns a Postscript representation of
  55. * the current image.
  56. */
  57. void _cucul_get_ps(cucul_t *qq, struct cucul_buffer *ex)
  58. {
  59. static char const * const palette[] =
  60. {
  61. "0.0 0.0 0.0", "0.0 0.0 0.5", "0.0 0.5 0.0", "0.0 0.5 0.5",
  62. "0.5 0.0 0.0", "0.5 0.0 0.5", "0.5 0.5 0.0", "0.5 0.5 0.5",
  63. "0.2 0.2 0.2", "0.2 0.2 1.0", "0.2 1.0 0.2", "0.2 1.0 1.0",
  64. "1.0 0.2 0.2", "1.0 0.2 1.0", "1.0 1.0 0.2", "1.0 1.0 1.0",
  65. };
  66. char *cur;
  67. unsigned int x, y;
  68. /* 200 is arbitrary but should be ok */
  69. ex->size = strlen(ps_header) + (qq->width * qq->height * 200);
  70. ex->buffer = malloc(ex->size);
  71. cur = ex->buffer;
  72. /* Header */
  73. cur += sprintf(cur, "%s", ps_header);
  74. /* Background, drawn using csquare macro defined in header */
  75. for(y = qq->height; y--; )
  76. {
  77. uint8_t *lineattr = qq->attr + y * qq->width;
  78. for(x = 0; x < qq->width; x++)
  79. {
  80. cur += sprintf(cur, "1 0 translate\n %s csquare\n",
  81. palette[*lineattr++ >> 4]);
  82. }
  83. /* Return to beginning of the line, and jump to the next one */
  84. cur += sprintf(cur, "-%d 1 translate\n", qq->width);
  85. }
  86. cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
  87. for(y = qq->height; y--; )
  88. {
  89. uint8_t *lineattr = qq->attr + y * qq->width;
  90. uint32_t *linechar = qq->chars + y * qq->width;
  91. for(x = 0; x < qq->width; x++)
  92. {
  93. uint32_t c = *linechar++;
  94. cur += sprintf(cur, "newpath\n");
  95. cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10);
  96. cur += sprintf(cur, "%s setrgbcolor\n",
  97. palette[*lineattr++ & 0x0f]);
  98. if(c < 0x00000020)
  99. cur += sprintf(cur, "(?) show\n");
  100. else if(c >= 0x00000080)
  101. cur += sprintf(cur, "(?) show\n");
  102. else switch((uint8_t)(c & 0x7f))
  103. {
  104. case '\\': cur += sprintf(cur, "(\\\\) show\n"); break;
  105. case '(': cur += sprintf(cur, "(\\() show\n"); break;
  106. case ')':
  107. cur += sprintf(cur, "(\\%c) show\n", c);
  108. break;
  109. default:
  110. cur += sprintf(cur, "(%c) show\n", c);
  111. break;
  112. }
  113. }
  114. }
  115. cur += sprintf(cur, "showpage\n");
  116. /* Crop to really used size */
  117. ex->size = strlen(ex->buffer) + 1;
  118. ex->buffer = realloc(ex->buffer, ex->size);
  119. }