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

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