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

Last change on this file since 1318 was 1318, checked in by Sam Hocevar, 13 years ago
  • Don't crash if an export error occurs.
  • Property svn:keywords set to Id
File size: 2.4 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 1318 2006-11-10 07:38:52Z 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
32static int render_flush(context_t *);
33
34int render_init(context_t *cx)
35{
36    cx->x = cx->y = 0;
37    cx->w = cx->h = 0;
38    cx->lines = 0;
39    cx->cv = cucul_create_canvas(0, 0);
40
41    if(!strcasecmp(cx->font, "term"))
42        return init_tiny(cx);
43
44    return init_figlet(cx);
45}
46
47int render_stdin(context_t *cx)
48{
49    char buf[10];
50    unsigned int i = 0, len;
51    uint32_t ch;
52
53    /* Read from stdin */
54    while(!feof(stdin))
55    {
56        buf[i++] = getchar();
57        buf[i] = '\0';
58
59        ch = cucul_utf8_to_utf32(buf, &len);
60
61        if(!len)
62            continue;
63
64        cx->feed(cx, ch);
65        i = 0;
66
67        if(ch == '\n')
68            render_flush(cx);
69    }
70
71    return 0;
72}
73
74int render_list(context_t *cx, unsigned int argc, char *argv[])
75{
76    unsigned int i, j;
77
78    for(i = 0; i < argc; i++)
79    {
80        /* Read from commandline */
81        unsigned int len;
82
83        if(i)
84            cx->feed(cx, ' ');
85
86        for(j = 0; argv[i][j];)
87        {
88            cx->feed(cx, cucul_utf8_to_utf32(argv[i] + j, &len));
89            j += len;
90        }
91    }
92
93    render_flush(cx);
94
95    return 0;
96}
97
98int render_end(context_t *cx)
99{
100    cx->end(cx);
101    cucul_free_canvas(cx->cv);
102
103    return 0;
104}
105
106/* XXX: Following functions are local */
107
108static int render_flush(context_t *cx)
109{
110    cucul_buffer_t *buffer;
111
112    /* Flush current line */
113    cx->flush(cx);
114
115    /* Apply optional effects to our string */
116    filter_do(cx);
117
118    cx->lines += cucul_get_canvas_height(cx->torender);
119
120    /* Output line */
121    buffer = cucul_export_canvas(cx->torender, cx->export);
122    if(!buffer)
123        return -1;
124    fwrite(cucul_get_buffer_data(buffer),
125           cucul_get_buffer_size(buffer), 1, stdout);
126    cucul_free_buffer(buffer);
127    cucul_free_canvas(cx->torender);
128
129    return 0;
130}
131
Note: See TracBrowser for help on using the repository browser.