Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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