source: toilet/trunk/src/render.c @ 1100

Last change on this file since 1100 was 1100, checked in by Sam Hocevar, 15 years ago
  • Split main.c into main.c/render.c/filters.c.
  • Property svn:keywords set to Id
File size: 2.2 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: render.c 1100 2006-09-23 19:54:20Z 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
16#if defined(HAVE_INTTYPES_H)
17#   include <inttypes.h>
18#endif
19#include <stdlib.h>
20#include <cucul.h>
21
22#include "render.h"
23
24cucul_canvas_t *render_big(uint32_t const *string, unsigned int length)
25{
26    cucul_canvas_t *cv;
27    cucul_font_t *f;
28    char const * const * fonts;
29    unsigned char *buf;
30    unsigned int w, h, x, y, miny, maxy;
31
32    cv = cucul_create_canvas(length, 1);
33    for(x = 0; x < length; x++)
34        cucul_putchar(cv, x, 0, string[x]);
35
36    fonts = cucul_get_font_list();
37    f = cucul_load_font(fonts[0], 0);
38
39    /* Create our bitmap buffer (32-bit ARGB) */
40    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
41    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
42    buf = malloc(4 * w * h);
43
44    /* Render the canvas onto our image buffer */
45    cucul_render_canvas(cv, f, buf, w, h, 4 * w);
46
47    /* Free our canvas, and allocate a bigger one */
48    cucul_free_font(f);
49    cucul_free_canvas(cv);
50    cv = cucul_create_canvas(w, h);
51
52    /* Render the image buffer on the canvas */
53    cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_TRANSPARENT);
54    cucul_clear_canvas(cv);
55
56    miny = h; maxy = 0;
57
58    for(y = 0; y < h; y++)
59       for(x = 0; x < w; x++)
60    {
61        unsigned char c = buf[4 * (x + y * w) + 2];
62
63        if(c >= 0xa0)
64            cucul_putstr(cv, x, y, "█");
65        else if(c >= 0x80)
66            cucul_putstr(cv, x, y, "▓");
67        else if(c >= 0x40)
68            cucul_putstr(cv, x, y, "▒");
69        else if(c >= 0x20)
70            cucul_putstr(cv, x, y, "░");
71    }
72
73    free(buf);
74
75    return cv;
76}
77
78cucul_canvas_t *render_tiny(uint32_t const *string, unsigned int length)
79{
80    unsigned int x;
81    cucul_canvas_t *cv = cucul_create_canvas(length, 1);
82
83    for(x = 0; x < length; x++)
84        cucul_putchar(cv, x, 0, string[x]);
85
86    return cv;
87}
88
Note: See TracBrowser for help on using the repository browser.