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 1461 2006-12-12 01:50:48Z sam $ |
---|
7 | * |
---|
8 | * This program is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * This file contains text to canvas rendering functions. |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | |
---|
21 | #if defined(HAVE_INTTYPES_H) |
---|
22 | # include <inttypes.h> |
---|
23 | #endif |
---|
24 | #include <stdlib.h> |
---|
25 | #include <string.h> |
---|
26 | #include <stdio.h> |
---|
27 | #include <cucul.h> |
---|
28 | |
---|
29 | #include "toilet.h" |
---|
30 | #include "render.h" |
---|
31 | #include "filter.h" |
---|
32 | |
---|
33 | static int render_flush(context_t *); |
---|
34 | |
---|
35 | int render_init(context_t *cx) |
---|
36 | { |
---|
37 | cx->x = cx->y = 0; |
---|
38 | cx->w = cx->h = 0; |
---|
39 | cx->lines = 0; |
---|
40 | cx->cv = cucul_create_canvas(0, 0); |
---|
41 | |
---|
42 | if(!strcasecmp(cx->font, "term")) |
---|
43 | return init_tiny(cx); |
---|
44 | |
---|
45 | return init_figlet(cx); |
---|
46 | } |
---|
47 | |
---|
48 | int render_stdin(context_t *cx) |
---|
49 | { |
---|
50 | cucul_canvas_t *cv; |
---|
51 | char *line; |
---|
52 | unsigned int i, len; |
---|
53 | |
---|
54 | /* FIXME: we can't read longer lines */ |
---|
55 | len = 1024; |
---|
56 | line = malloc(len); |
---|
57 | cv = cucul_create_canvas(0, 0); |
---|
58 | |
---|
59 | /* Read from stdin */ |
---|
60 | while(!feof(stdin)) |
---|
61 | { |
---|
62 | if(!fgets(line, len, stdin)) |
---|
63 | break; |
---|
64 | |
---|
65 | cucul_set_canvas_size(cv, 0, 0); |
---|
66 | cucul_import_memory(cv, line, strlen(line), "utf8"); |
---|
67 | for(i = 0; i < cucul_get_canvas_width(cv); i++) |
---|
68 | { |
---|
69 | uint32_t ch = cucul_get_char(cv, i, 0); |
---|
70 | uint32_t at = cucul_get_attr(cv, i, 0); |
---|
71 | cx->feed(cx, ch, at); |
---|
72 | if(cucul_utf32_is_fullwidth(ch)) i++; |
---|
73 | } |
---|
74 | |
---|
75 | render_flush(cx); |
---|
76 | } |
---|
77 | |
---|
78 | free(line); |
---|
79 | |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | int render_list(context_t *cx, unsigned int argc, char *argv[]) |
---|
84 | { |
---|
85 | cucul_canvas_t *cv; |
---|
86 | unsigned int i, j, len; |
---|
87 | char *parser = NULL; |
---|
88 | |
---|
89 | cv = cucul_create_canvas(0, 0); |
---|
90 | |
---|
91 | for(j = 0; j < argc; ) |
---|
92 | { |
---|
93 | char *cr; |
---|
94 | |
---|
95 | if(!parser) |
---|
96 | { |
---|
97 | if(j) |
---|
98 | cx->feed(cx, ' ', 0); |
---|
99 | parser = argv[j]; |
---|
100 | } |
---|
101 | |
---|
102 | cr = strchr(parser, '\n'); |
---|
103 | if(cr) |
---|
104 | len = (cr - parser) + 1; |
---|
105 | else |
---|
106 | len = strlen(parser); |
---|
107 | |
---|
108 | cucul_set_canvas_size(cv, 0, 0); |
---|
109 | cucul_import_memory(cv, parser, len, "utf8"); |
---|
110 | for(i = 0; i < cucul_get_canvas_width(cv); i++) |
---|
111 | { |
---|
112 | uint32_t ch = cucul_get_char(cv, i, 0); |
---|
113 | uint32_t at = cucul_get_attr(cv, i, 0); |
---|
114 | cx->feed(cx, ch, at); |
---|
115 | if(cucul_utf32_is_fullwidth(ch)) i++; |
---|
116 | } |
---|
117 | |
---|
118 | if(cr) |
---|
119 | { |
---|
120 | parser += len; |
---|
121 | render_flush(cx); |
---|
122 | } |
---|
123 | else |
---|
124 | { |
---|
125 | parser = NULL; |
---|
126 | j++; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | render_flush(cx); |
---|
131 | |
---|
132 | return 0; |
---|
133 | } |
---|
134 | |
---|
135 | int render_end(context_t *cx) |
---|
136 | { |
---|
137 | cx->end(cx); |
---|
138 | cucul_free_canvas(cx->cv); |
---|
139 | |
---|
140 | return 0; |
---|
141 | } |
---|
142 | |
---|
143 | /* XXX: Following functions are local */ |
---|
144 | |
---|
145 | static int render_flush(context_t *cx) |
---|
146 | { |
---|
147 | unsigned long int len; |
---|
148 | void *buffer; |
---|
149 | |
---|
150 | /* Flush current line */ |
---|
151 | cx->flush(cx); |
---|
152 | |
---|
153 | /* Apply optional effects to our string */ |
---|
154 | filter_do(cx); |
---|
155 | |
---|
156 | cx->lines += cucul_get_canvas_height(cx->torender); |
---|
157 | |
---|
158 | /* Output line */ |
---|
159 | buffer = cucul_export_memory(cx->torender, cx->export, &len); |
---|
160 | if(!buffer) |
---|
161 | return -1; |
---|
162 | fwrite(buffer, len, 1, stdout); |
---|
163 | free(buffer); |
---|
164 | cucul_free_canvas(cx->torender); |
---|
165 | |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|