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.
 
 
 
 
 
 

141 lines
3.9 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$
  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_export *ex)
  57. {
  58. static char const * const palette[] =
  59. {
  60. "0.0 0.0 0.0", "0.0 0.0 0.5", "0.0 0.5 0.0", "0.0 0.5 0.5",
  61. "0.5 0.0 0.0", "0.5 0.0 0.5", "0.5 0.5 0.0", "0.5 0.5 0.5",
  62. "0.2 0.2 0.2", "0.2 0.2 1.0", "0.2 1.0 0.2", "0.2 1.0 1.0",
  63. "1.0 0.2 0.2", "1.0 0.2 1.0", "1.0 1.0 0.2", "1.0 1.0 1.0",
  64. };
  65. char *cur;
  66. unsigned int x, y;
  67. /* 200 is arbitrary but should be ok */
  68. ex->size = strlen(ps_header) + (qq->width * qq->height * 200);
  69. ex->buffer = malloc(ex->size);
  70. cur = ex->buffer;
  71. /* Header */
  72. cur += sprintf(cur, "%s", ps_header);
  73. /* Background, drawn using csquare macro defined in header */
  74. for(y = qq->height; y--; )
  75. {
  76. uint8_t *lineattr = qq->attr + y * qq->width;
  77. for(x = 0; x < qq->width; x++)
  78. {
  79. cur += sprintf(cur, "1 0 translate\n %s csquare\n",
  80. palette[*lineattr++ >> 4]);
  81. }
  82. /* Return to beginning of the line, and jump to the next one */
  83. cur += sprintf(cur, "-%d 1 translate\n", qq->width);
  84. }
  85. cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
  86. for(y = qq->height; y--; )
  87. {
  88. uint8_t *lineattr = qq->attr + (qq->height - y - 1) * qq->width;
  89. uint32_t *linechar = qq->chars + (qq->height - y - 1) * qq->width;
  90. for(x = 0; x < qq->width; x++)
  91. {
  92. uint32_t c = *linechar++;
  93. cur += sprintf(cur, "newpath\n");
  94. cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10);
  95. cur += sprintf(cur, "%s setrgbcolor\n",
  96. palette[*lineattr++ & 0x0f]);
  97. if(c < 0x00000020)
  98. cur += sprintf(cur, "(?) show\n");
  99. else if(c >= 0x00000080)
  100. cur += sprintf(cur, "(?) show\n");
  101. else switch((uint8_t)(c & 0x7f))
  102. {
  103. case '\\': cur += sprintf(cur, "(\\\\) show\n"); break;
  104. case '(': cur += sprintf(cur, "(\\() show\n"); break;
  105. case ')':
  106. cur += sprintf(cur, "(\\%c) show\n", c);
  107. break;
  108. default:
  109. cur += sprintf(cur, "(%c) show\n", c);
  110. break;
  111. }
  112. }
  113. }
  114. cur += sprintf(cur, "showpage\n");
  115. /* Crop to really used size */
  116. ex->size = strlen(ex->buffer) + 1;
  117. ex->buffer = realloc(ex->buffer, ex->size);
  118. }