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

Last change on this file since 1385 was 1385, checked in by Sam Hocevar, 14 years ago
  • Support for ANSI escape codes in the input:

http://zoy.org/~sam/toilet-ansi.png
http://zoy.org/~sam/toilet-ansi2.png

  • Empty lines are currently broken.
  • Property svn:keywords set to Id
File size: 3.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 1385 2006-11-13 01:02:05Z 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    cucul_canvas_t *cv;
50    char *line;
51    unsigned int i, len;
52
53    /* FIXME: we can't read longer lines */
54    len = 1024;
55    line = malloc(len);
56    cv = cucul_create_canvas(0, 0);
57
58    /* Read from stdin */
59    while(!feof(stdin))
60    {
61        if(!fgets(line, len, stdin))
62            break;
63
64        cucul_set_canvas_size(cv, 0, 0);
65        cucul_import_memory(cv, line, strlen(line), "utf8");
66        for(i = 0; i < cucul_get_canvas_width(cv); i++)
67        {
68            uint32_t ch = cucul_get_char(cv, i, 0);
69            uint32_t at = cucul_get_attr(cv, i, 0);
70            cx->feed(cx, ch, at);
71            if(cucul_utf32_is_fullwidth(ch)) i++;
72        }
73
74        render_flush(cx);
75    }
76
77    free(line);
78
79    return 0;
80}
81
82int render_list(context_t *cx, unsigned int argc, char *argv[])
83{
84    cucul_canvas_t *cv;
85    unsigned int i, j, len;
86    char *parser = NULL;
87
88    cv = cucul_create_canvas(0, 0);
89
90    for(j = 0; j < argc; )
91    {
92        char *cr;
93
94        if(!parser)
95        {
96            if(j)
97                cx->feed(cx, ' ', 0);
98            parser = argv[j];
99        }
100
101        cr = strchr(parser, '\n');
102        if(cr)
103            len = (cr - parser) + 1;
104        else
105            len = strlen(parser);
106
107        cucul_set_canvas_size(cv, 0, 0);
108        cucul_import_memory(cv, parser, len, "utf8");
109        for(i = 0; i < cucul_get_canvas_width(cv); i++)
110        {
111            uint32_t ch = cucul_get_char(cv, i, 0);
112            uint32_t at = cucul_get_attr(cv, i, 0);
113            cx->feed(cx, ch, at);
114            if(cucul_utf32_is_fullwidth(ch)) i++;
115        }
116
117        if(cr)
118        {
119            parser += len;
120            render_flush(cx);
121        }
122        else
123        {
124            parser = NULL;
125            j++;
126        }
127    }
128
129    render_flush(cx);
130
131    return 0;
132}
133
134int render_end(context_t *cx)
135{
136    cx->end(cx);
137    cucul_free_canvas(cx->cv);
138
139    return 0;
140}
141
142/* XXX: Following functions are local */
143
144static int render_flush(context_t *cx)
145{
146    unsigned long int len;
147    void *buffer;
148
149    /* Flush current line */
150    cx->flush(cx);
151
152    /* Apply optional effects to our string */
153    filter_do(cx);
154
155    cx->lines += cucul_get_canvas_height(cx->torender);
156
157    /* Output line */
158    buffer = cucul_export_memory(cx->torender, cx->export, &len);
159    if(!buffer)
160        return -1;
161    fwrite(buffer, len, 1, stdout);
162    free(buffer);
163    cucul_free_canvas(cx->torender);
164
165    return 0;
166}
167
Note: See TracBrowser for help on using the repository browser.