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.
 
 
 
 
 
 

139 line
3.9 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the Do What The Fuck You Want To
  11. * Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains export functions for Postscript files
  16. */
  17. #include "config.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdlib.h>
  20. # include <stdio.h>
  21. # include <string.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "cucul_internals.h"
  25. static char const *ps_header =
  26. "%!\n"
  27. "%% libcaca PDF export\n"
  28. "%%LanguageLevel: 2\n"
  29. "%%Pages: 1\n"
  30. "%%DocumentData: Clean7Bit\n"
  31. "/csquare {\n"
  32. " newpath\n"
  33. " 0 0 moveto\n"
  34. " 0 1 rlineto\n"
  35. " 1 0 rlineto\n"
  36. " 0 -1 rlineto\n"
  37. " closepath\n"
  38. " setrgbcolor\n"
  39. " fill\n"
  40. "} def\n"
  41. "/S {\n"
  42. " Show\n"
  43. "} bind def\n"
  44. "/Courier-Bold findfont\n"
  45. "8 scalefont\n"
  46. "setfont\n"
  47. "gsave\n"
  48. "6 10 scale\n";
  49. /** \brief Generate Postscript representation of current image.
  50. *
  51. * This function generates and returns a Postscript representation of
  52. * the current image.
  53. */
  54. void _cucul_get_ps(cucul_t *qq, struct cucul_buffer *ex)
  55. {
  56. static char const * const palette[] =
  57. {
  58. "0.0 0.0 0.0", "0.0 0.0 0.5", "0.0 0.5 0.0", "0.0 0.5 0.5",
  59. "0.5 0.0 0.0", "0.5 0.0 0.5", "0.5 0.5 0.0", "0.5 0.5 0.5",
  60. "0.2 0.2 0.2", "0.2 0.2 1.0", "0.2 1.0 0.2", "0.2 1.0 1.0",
  61. "1.0 0.2 0.2", "1.0 0.2 1.0", "1.0 1.0 0.2", "1.0 1.0 1.0",
  62. };
  63. char *cur;
  64. unsigned int x, y;
  65. /* 200 is arbitrary but should be ok */
  66. ex->size = strlen(ps_header) + (qq->width * qq->height * 200);
  67. ex->data = malloc(ex->size);
  68. cur = ex->data;
  69. /* Header */
  70. cur += sprintf(cur, "%s", ps_header);
  71. /* Background, drawn using csquare macro defined in header */
  72. for(y = qq->height; y--; )
  73. {
  74. uint32_t *lineattr = qq->attr + y * qq->width;
  75. for(x = 0; x < qq->width; x++)
  76. {
  77. cur += sprintf(cur, "1 0 translate\n %s csquare\n",
  78. palette[_cucul_argb32_to_ansi4bg(*lineattr++)]);
  79. }
  80. /* Return to beginning of the line, and jump to the next one */
  81. cur += sprintf(cur, "-%d 1 translate\n", qq->width);
  82. }
  83. cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
  84. for(y = qq->height; y--; )
  85. {
  86. uint32_t *lineattr = qq->attr + (qq->height - y - 1) * qq->width;
  87. uint32_t *linechar = qq->chars + (qq->height - y - 1) * qq->width;
  88. for(x = 0; x < qq->width; x++)
  89. {
  90. uint32_t c = *linechar++;
  91. cur += sprintf(cur, "newpath\n");
  92. cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10);
  93. cur += sprintf(cur, "%s setrgbcolor\n",
  94. palette[_cucul_argb32_to_ansi4fg(*lineattr++)]);
  95. if(c < 0x00000020)
  96. cur += sprintf(cur, "(?) show\n");
  97. else if(c >= 0x00000080)
  98. cur += sprintf(cur, "(?) show\n");
  99. else switch((uint8_t)(c & 0x7f))
  100. {
  101. case '\\': cur += sprintf(cur, "(\\\\) show\n"); break;
  102. case '(': cur += sprintf(cur, "(\\() show\n"); break;
  103. case ')':
  104. cur += sprintf(cur, "(\\%c) show\n", c);
  105. break;
  106. default:
  107. cur += sprintf(cur, "(%c) show\n", c);
  108. break;
  109. }
  110. }
  111. }
  112. cur += sprintf(cur, "showpage\n");
  113. /* Crop to really used size */
  114. ex->size = (uintptr_t)(cur - ex->data);
  115. ex->data = realloc(ex->data, ex->size);
  116. }