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

Last change on this file since 1193 was 1193, checked in by Sam Hocevar, 16 years ago
  • Break everything. Temporarily. Only the term output works.
  • Allow to read from stdin.
  • Wrap at terminal width (currently letter-wrap, not word-wrap).
  • Property svn:keywords set to Id
File size: 3.3 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 1193 2006-10-08 12:14:13Z 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/*
15 * This file contains text to canvas rendering functions.
16 */
17
18#include "config.h"
19
20#if defined(HAVE_INTTYPES_H)
21#   include <inttypes.h>
22#endif
23#include <stdlib.h>
24#include <cucul.h>
25
26#include "toilet.h"
27#include "render.h"
28
29static int feed_tiny(context_t *, uint32_t);
30static int end_tiny(context_t *);
31
32int init_tiny(context_t *cx)
33{
34    cx->ew = 16;
35    cx->eh = 2;
36    cx->x = cx->y = 0;
37    cx->w = cx->h = 0;
38    cx->cv = cucul_create_canvas(cx->ew, cx->eh);
39
40    cx->feed = feed_tiny;
41    cx->end = end_tiny;
42
43    return 0;
44}
45
46static int feed_tiny(context_t *cx, uint32_t ch)
47{
48    if(cx->x >= cx->w)
49        cx->w = cx->x + 1;
50
51    if(cx->y >= cx->h)
52        cx->h = cx->y + 1;
53
54    switch(ch)
55    {
56        case (uint32_t)'\r':
57            return 0;
58        case (uint32_t)'\n':
59            cx->x = 0;
60            cx->y++;
61            break;
62        case (uint32_t)'\t':
63            cx->x = (cx->x & ~7) + 8;
64            break;
65        default:
66            cucul_putchar(cx->cv, cx->x, cx->y, ch);
67            cx->x++;
68            break;
69    }
70
71    if(cx->x >= cx->term_width)
72    {
73        cx->x = 0;
74        cx->y++;
75    }
76
77    if(cx->x >= cx->ew)
78        cx->ew = cx->ew + cx->ew / 2;
79
80    if(cx->y >= cx->eh)
81        cx->eh = cx->eh + cx->eh / 2;
82
83    cucul_set_canvas_size(cx->cv, cx->ew, cx->eh);
84
85    return 0;
86}
87
88static int end_tiny(context_t *cx)
89{
90    cucul_set_canvas_size(cx->cv, cx->w, cx->h);
91
92    return 0;
93}
94
95#if 0
96cucul_canvas_t *render_big(uint32_t const *string, unsigned int length)
97{
98    cucul_canvas_t *cv;
99    cucul_font_t *f;
100    char const * const * fonts;
101    unsigned char *buf;
102    unsigned int w, h, x, y, miny, maxy;
103
104    cv = cucul_create_canvas(length, 1);
105    cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
106    for(x = 0; x < length; x++)
107        cucul_putchar(cv, x, 0, string[x]);
108
109    fonts = cucul_get_font_list();
110    f = cucul_load_font(fonts[0], 0);
111
112    /* Create our bitmap buffer (32-bit ARGB) */
113    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
114    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
115    buf = malloc(4 * w * h);
116
117    /* Render the canvas onto our image buffer */
118    cucul_render_canvas(cv, f, buf, w, h, 4 * w);
119
120    /* Free our canvas, and allocate a bigger one */
121    cucul_free_font(f);
122    cucul_free_canvas(cv);
123    cv = cucul_create_canvas(w, h);
124
125    /* Render the image buffer on the canvas */
126    cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_TRANSPARENT);
127    cucul_clear_canvas(cv);
128
129    miny = h; maxy = 0;
130
131    for(y = 0; y < h; y++)
132       for(x = 0; x < w; x++)
133    {
134        unsigned char c = buf[4 * (x + y * w) + 2];
135
136        if(c >= 0xa0)
137            cucul_putstr(cv, x, y, "█");
138        else if(c >= 0x80)
139            cucul_putstr(cv, x, y, "▓");
140        else if(c >= 0x40)
141            cucul_putstr(cv, x, y, "▒");
142        else if(c >= 0x20)
143            cucul_putstr(cv, x, y, "░");
144    }
145
146    free(buf);
147
148    return cv;
149}
150#endif
151
Note: See TracBrowser for help on using the repository browser.