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

Last change on this file since 1241 was 1241, checked in by Sam Hocevar, 13 years ago
  • Make stdin input line-aware so that we don't have to wait until EOF to display stuff.
  • Reorganise stuff and fix memory leaks.
  • 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 1241 2006-10-26 13:07:58Z 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 <string.h>
25#include <stdio.h>
26#include <cucul.h>
27
28#include "toilet.h"
29#include "render.h"
30#include "filter.h"
31
32int render_init(context_t *cx)
33{
34    if(!strcasecmp(cx->font, "mono9"))
35        return init_big(cx);
36
37    if(!strcasecmp(cx->font, "term"))
38        return init_tiny(cx);
39
40    return init_figlet(cx);
41}
42
43int render_stdin(context_t *cx)
44{
45    char buf[10];
46    unsigned int i = 0, len;
47    uint32_t ch;
48
49    /* Read from stdin */
50    while(!feof(stdin))
51    {
52        buf[i++] = getchar();
53        buf[i] = '\0';
54
55        ch = cucul_utf8_to_utf32(buf, &len);
56
57        if(!len)
58            continue;
59
60        cx->feed(cx, ch);
61        i = 0;
62
63        if(ch == '\n')
64            render_line(cx);
65    }
66
67    return 0;
68}
69
70int render_list(context_t *cx, unsigned int argc, char *argv[])
71{
72    unsigned int i, j;
73
74    for(i = 0; i < argc; i++)
75    {
76        /* Read from commandline */
77        unsigned int len;
78
79        if(i)
80            cx->feed(cx, ' ');
81
82        for(j = 0; argv[i][j];)
83        {
84            cx->feed(cx, cucul_utf8_to_utf32(argv[i] + j, &len));
85            j += len;
86        }
87    }
88
89    render_line(cx);
90
91    return 0;
92}
93
94int render_line(context_t *cx)
95{
96    cucul_buffer_t *buffer;
97
98    /* Flush current line */
99    cx->flush(cx);
100
101    /* Apply optional effects to our string */
102    filter_do(cx);
103
104    /* Output line */
105    buffer = cucul_export_canvas(cx->torender, cx->export);
106    fwrite(cucul_get_buffer_data(buffer),
107           cucul_get_buffer_size(buffer), 1, stdout);
108    cucul_free_buffer(buffer);
109    cucul_free_canvas(cx->torender);
110
111    return 0;
112}
113
114int render_end(context_t *cx)
115{
116    cx->end(cx);
117
118    return 0;
119}
120
Note: See TracBrowser for help on using the repository browser.