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

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