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

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