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

Last change on this file since 757 was 757, checked in by Sam Hocevar, 14 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
RevLine 
[602]1/*
[672]2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
[602]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
[731]12/** \file export_ps.c
[634]13 *  \version \$Id: export_ps.c 757 2006-04-13 12:50:41Z sam $
[602]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
[606]32static char const *ps_header =
[634]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"
[606]53    "setfont\n"
[634]54    "gsave\n"
55    "6 10 scale\n";
[602]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 */
[757]62void _cucul_get_ps(cucul_t *qq, struct cucul_buffer *ex)
[602]63{
[634]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",
[602]68
[634]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    };
[602]72
[634]73    char *cur;
74    unsigned int x, y;
[602]75
[603]76    /* 200 is arbitrary but should be ok */
[609]77    ex->size = strlen(ps_header) + (qq->width * qq->height * 200);
[757]78    ex->data = malloc(ex->size);
[602]79
[757]80    cur = ex->data;
[609]81
[602]82    /* Header */
83    cur += sprintf(cur, "%s", ps_header);
84
85    /* Background, drawn using csquare macro defined in header */
[634]86    for(y = qq->height; y--; )
87    {
[724]88        uint32_t *lineattr = qq->attr + y * qq->width;
[602]89
[634]90        for(x = 0; x < qq->width; x++)
91        {
92            cur += sprintf(cur, "1 0 translate\n %s csquare\n",
[728]93                           palette[_cucul_argb32_to_ansi4bg(*lineattr++)]);
[634]94        }
[602]95
[634]96        /* Return to beginning of the line, and jump to the next one */
97        cur += sprintf(cur, "-%d 1 translate\n", qq->width);
98    }
[602]99
[634]100    cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
[602]101
[634]102    for(y = qq->height; y--; )
103    {
[724]104        uint32_t *lineattr = qq->attr + (qq->height - y - 1) * qq->width;
[639]105        uint32_t *linechar = qq->chars + (qq->height - y - 1) * qq->width;
[602]106
[634]107        for(x = 0; x < qq->width; x++)
108        {
[635]109            uint32_t c = *linechar++;
110
[634]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",
[728]114                           palette[_cucul_argb32_to_ansi4fg(*lineattr++)]);
[635]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            }
[634]131        }
[602]132    }
133
[634]134    cur += sprintf(cur, "showpage\n");
[602]135
136    /* Crop to really used size */
[757]137    ex->size = (uintptr_t)(cur - ex->data);
138    ex->data = realloc(ex->data, ex->size);
[602]139}
140
Note: See TracBrowser for help on using the repository browser.