source: libcaca/trunk/test/toilet.c @ 1073

Last change on this file since 1073 was 1073, checked in by Sam Hocevar, 14 years ago
  • TOIlet, The Other Implementation's letters. Will be a FIGlet clone. Later.
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1/*
2 *  TOIlet        The Other Implementation’s letters
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: toilet.c 1073 2006-09-20 16:31:12Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15#include "common.h"
16
17#if !defined(__KERNEL__)
18#   if defined(HAVE_INTTYPES_H)
19#      include <inttypes.h>
20#   endif
21#   include <stdio.h>
22#   include <string.h>
23#   include <stdlib.h>
24#endif
25
26#include "cucul.h"
27#include "caca.h"
28
29/* String to canvas transformations */
30static cucul_canvas_t *cuculize_big(char const *);
31static cucul_canvas_t *cuculize_tiny(char const *);
32
33/* Canvas special effects */
34static void make_gay(cucul_canvas_t *);
35
36int main(int argc, char *argv[])
37{
38    cucul_canvas_t *cv;
39    cucul_buffer_t *buffer;
40
41    char *string = NULL;
42    unsigned int i, n;
43
44    if(argc < 2)
45    {
46        fprintf(stderr, "%s: too few arguments\n", argv[0]);
47        fprintf(stderr, "usage: %s [OPTIONS] <string>\n", argv[0]);
48        return -1;
49    }
50
51    for(i = 1, n = 0; i < (unsigned int)argc; i++)
52    {
53        unsigned int l = strlen(argv[i]);
54        if(i > 1)
55            string[n++] = ' ';
56        string = realloc(string, n + l + 1);
57        strcpy(string + n, argv[i]);
58        n += l;
59    }
60
61    /* Do gay stuff with our string (léopard) */
62    cv = cuculize_big(string);
63    make_gay(cv);
64
65    /* Output char */
66    buffer = cucul_export_canvas(cv, "utf8");
67    fwrite(cucul_get_buffer_data(buffer),
68           cucul_get_buffer_size(buffer), 1, stdout);
69    cucul_free_buffer(buffer);
70
71    cucul_free_canvas(cv);
72
73    return 0;
74}
75
76static cucul_canvas_t *cuculize_big(char const *string)
77{
78    cucul_canvas_t *cv;
79    cucul_font_t *f;
80    char const * const * fonts;
81    unsigned char *buf;
82    unsigned int n, w, h, x, y, miny, maxy;
83
84    n = strlen(string);
85    cv = cucul_create_canvas(n, 1);
86    cucul_putstr(cv, 0, 0, string);
87
88    fonts = cucul_get_font_list();
89    f = cucul_load_font(fonts[0], 0);
90
91    /* Create our bitmap buffer (32-bit ARGB) */
92    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
93    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
94    buf = malloc(4 * w * h);
95
96    /* Render the canvas onto our image buffer */
97    cucul_render_canvas(cv, f, buf, w, h, 4 * w);
98
99    /* Free our canvas, and allocate a bigger one */
100    cucul_free_font(f);
101    cucul_free_canvas(cv);
102    cv = cucul_create_canvas(w, h);
103
104    /* Render the image buffer on the canvas */
105    cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_TRANSPARENT);
106    cucul_clear_canvas(cv);
107
108    miny = h; maxy = 0;
109
110    for(y = 0; y < h; y++)
111       for(x = 0; x < w; x++)
112    {
113        unsigned char c = buf[4 * (x + y * w) + 2];
114
115        if(c >= 0xa0)
116            cucul_putstr(cv, x, y, "█");
117        else if(c >= 0x80)
118            cucul_putstr(cv, x, y, "▓");
119        else if(c >= 0x40)
120            cucul_putstr(cv, x, y, "▒");
121        else if(c >= 0x20)
122            cucul_putstr(cv, x, y, "░");
123    }
124
125    free(buf);
126
127    return cv;
128}
129
130static cucul_canvas_t *cuculize_tiny(char const *string)
131{
132    unsigned int n = strlen(string);
133    cucul_canvas_t *cv = cucul_create_canvas(n, 1);
134
135    cucul_putstr(cv, 0, 0, string);
136
137    return cv;
138}
139
140static void make_gay(cucul_canvas_t *cv)
141{
142    static unsigned char const rainbow[] =
143    {
144        CUCUL_COLOR_LIGHTMAGENTA,
145        CUCUL_COLOR_LIGHTRED,
146        CUCUL_COLOR_YELLOW,
147        CUCUL_COLOR_LIGHTGREEN,
148        CUCUL_COLOR_LIGHTCYAN,
149        CUCUL_COLOR_LIGHTBLUE,
150    };
151    unsigned int x, y, w, h;
152
153    w = cucul_get_canvas_width(cv);
154    h = cucul_get_canvas_height(cv);
155
156    for(y = 0; y < h; y++)
157        for(x = 0; x < w; x++)
158    {
159        unsigned long int ch = cucul_getchar(cv, x, y);
160        if(ch != (unsigned char)' ')
161        {
162            cucul_set_color(cv, rainbow[(x + y) % 6], CUCUL_COLOR_TRANSPARENT);
163            cucul_putchar(cv, x, y, ch);
164        }
165    }
166}
167
Note: See TracBrowser for help on using the repository browser.