source: libcaca/trunk/cucul/export_ps.c @ 769

Last change on this file since 769 was 769, checked in by Sam Hocevar, 15 years ago
  • Removed \file directives from all files except caca.h and cucul.h, to remove redundencies in the Doxygen documentation.
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
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: export_ps.c 769 2006-04-14 07:30:53Z sam $
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/*
16 *  This file contains export functions for Postscript files
17 */
18
19#include "config.h"
20
21#if !defined(__KERNEL__)
22#   include <stdlib.h>
23#   include <stdio.h>
24#   include <string.h>
25#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30static char const *ps_header =
31    "%!\n"
32    "%% libcaca PDF export\n"
33    "%%LanguageLevel: 2\n"
34    "%%Pages: 1\n"
35    "%%DocumentData: Clean7Bit\n"
36    "/csquare {\n"
37    "  newpath\n"
38    "  0 0 moveto\n"
39    "  0 1 rlineto\n"
40    "  1 0 rlineto\n"
41    "  0 -1 rlineto\n"
42    "  closepath\n"
43    "  setrgbcolor\n"
44    "  fill\n"
45    "} def\n"
46    "/S {\n"
47    "  Show\n"
48    "} bind def\n"
49    "/Courier-Bold findfont\n"
50    "8 scalefont\n"
51    "setfont\n"
52    "gsave\n"
53    "6 10 scale\n";
54
55/** \brief Generate Postscript representation of current image.
56 *
57 *  This function generates and returns a Postscript representation of
58 *  the current image.
59 */
60void _cucul_get_ps(cucul_t *qq, struct cucul_buffer *ex)
61{
62    static char const * const palette[] =
63    {
64        "0.0 0.0 0.0", "0.0 0.0 0.5", "0.0 0.5 0.0", "0.0 0.5 0.5",
65        "0.5 0.0 0.0", "0.5 0.0 0.5", "0.5 0.5 0.0", "0.5 0.5 0.5",
66
67        "0.2 0.2 0.2", "0.2 0.2 1.0", "0.2 1.0 0.2", "0.2 1.0 1.0",
68        "1.0 0.2 0.2", "1.0 0.2 1.0", "1.0 1.0 0.2", "1.0 1.0 1.0",
69    };
70
71    char *cur;
72    unsigned int x, y;
73
74    /* 200 is arbitrary but should be ok */
75    ex->size = strlen(ps_header) + (qq->width * qq->height * 200);
76    ex->data = malloc(ex->size);
77
78    cur = ex->data;
79
80    /* Header */
81    cur += sprintf(cur, "%s", ps_header);
82
83    /* Background, drawn using csquare macro defined in header */
84    for(y = qq->height; y--; )
85    {
86        uint32_t *lineattr = qq->attr + y * qq->width;
87
88        for(x = 0; x < qq->width; x++)
89        {
90            cur += sprintf(cur, "1 0 translate\n %s csquare\n",
91                           palette[_cucul_argb32_to_ansi4bg(*lineattr++)]);
92        }
93
94        /* Return to beginning of the line, and jump to the next one */
95        cur += sprintf(cur, "-%d 1 translate\n", qq->width);
96    }
97
98    cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
99
100    for(y = qq->height; y--; )
101    {
102        uint32_t *lineattr = qq->attr + (qq->height - y - 1) * qq->width;
103        uint32_t *linechar = qq->chars + (qq->height - y - 1) * qq->width;
104
105        for(x = 0; x < qq->width; x++)
106        {
107            uint32_t c = *linechar++;
108
109            cur += sprintf(cur, "newpath\n");
110            cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10);
111            cur += sprintf(cur, "%s setrgbcolor\n",
112                           palette[_cucul_argb32_to_ansi4fg(*lineattr++)]);
113
114            if(c < 0x00000020)
115                cur += sprintf(cur, "(?) show\n");
116            else if(c >= 0x00000080)
117                cur += sprintf(cur, "(?) show\n");
118            else switch((uint8_t)(c & 0x7f))
119            {
120                case '\\': cur += sprintf(cur, "(\\\\) show\n"); break;
121                case '(': cur += sprintf(cur, "(\\() show\n"); break;
122                case ')':
123                    cur += sprintf(cur, "(\\%c) show\n", c);
124                    break;
125                default:
126                    cur += sprintf(cur, "(%c) show\n", c);
127                    break;
128            }
129        }
130    }
131
132    cur += sprintf(cur, "showpage\n");
133
134    /* Crop to really used size */
135    ex->size = (uintptr_t)(cur - ex->data);
136    ex->data = realloc(ex->data, ex->size);
137}
138
Note: See TracBrowser for help on using the repository browser.