[1084] | 1 | /* |
---|
[1087] | 2 | * TOIlet The Other Implementation’s letters |
---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
| 4 | * All Rights Reserved |
---|
[1084] | 5 | * |
---|
[1087] | 6 | * $Id: render.c 2988 2008-10-18 21:36:17Z sam $ |
---|
[1084] | 7 | * |
---|
[1461] | 8 | * This program is free software. It comes without any warranty, to |
---|
[1451] | 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 |
---|
[1087] | 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
[1084] | 13 | */ |
---|
| 14 | |
---|
[1102] | 15 | /* |
---|
| 16 | * This file contains text to canvas rendering functions. |
---|
| 17 | */ |
---|
| 18 | |
---|
[1084] | 19 | #include "config.h" |
---|
| 20 | |
---|
[1087] | 21 | #if defined(HAVE_INTTYPES_H) |
---|
| 22 | # include <inttypes.h> |
---|
| 23 | #endif |
---|
| 24 | #include <stdlib.h> |
---|
[1241] | 25 | #include <string.h> |
---|
| 26 | #include <stdio.h> |
---|
[2988] | 27 | #include <caca.h> |
---|
[1084] | 28 | |
---|
[1193] | 29 | #include "toilet.h" |
---|
[1100] | 30 | #include "render.h" |
---|
[1241] | 31 | #include "filter.h" |
---|
[1087] | 32 | |
---|
[1243] | 33 | static int render_flush(context_t *); |
---|
| 34 | |
---|
[1241] | 35 | int render_init(context_t *cx) |
---|
[1193] | 36 | { |
---|
[1242] | 37 | cx->x = cx->y = 0; |
---|
| 38 | cx->w = cx->h = 0; |
---|
| 39 | cx->lines = 0; |
---|
[2988] | 40 | cx->cv = caca_create_canvas(0, 0); |
---|
[1242] | 41 | |
---|
[1241] | 42 | if(!strcasecmp(cx->font, "term")) |
---|
| 43 | return init_tiny(cx); |
---|
[1193] | 44 | |
---|
[1241] | 45 | return init_figlet(cx); |
---|
[1193] | 46 | } |
---|
| 47 | |
---|
[1241] | 48 | int render_stdin(context_t *cx) |
---|
[1193] | 49 | { |
---|
[2988] | 50 | caca_canvas_t *cv; |
---|
[1385] | 51 | char *line; |
---|
[2414] | 52 | int i, len; |
---|
[1195] | 53 | |
---|
[1385] | 54 | /* FIXME: we can't read longer lines */ |
---|
| 55 | len = 1024; |
---|
| 56 | line = malloc(len); |
---|
[2988] | 57 | cv = caca_create_canvas(0, 0); |
---|
[1385] | 58 | |
---|
[1241] | 59 | /* Read from stdin */ |
---|
| 60 | while(!feof(stdin)) |
---|
[1194] | 61 | { |
---|
[1385] | 62 | if(!fgets(line, len, stdin)) |
---|
| 63 | break; |
---|
[1193] | 64 | |
---|
[2988] | 65 | caca_set_canvas_size(cv, 0, 0); |
---|
| 66 | caca_import_memory(cv, line, strlen(line), "utf8"); |
---|
| 67 | for(i = 0; i < caca_get_canvas_width(cv); i++) |
---|
[1385] | 68 | { |
---|
[2988] | 69 | uint32_t ch = caca_get_char(cv, i, 0); |
---|
| 70 | uint32_t at = caca_get_attr(cv, i, 0); |
---|
[1385] | 71 | cx->feed(cx, ch, at); |
---|
[2988] | 72 | if(caca_utf32_is_fullwidth(ch)) i++; |
---|
[1385] | 73 | } |
---|
[1194] | 74 | |
---|
[1385] | 75 | render_flush(cx); |
---|
| 76 | } |
---|
[1193] | 77 | |
---|
[1385] | 78 | free(line); |
---|
[1194] | 79 | |
---|
[1193] | 80 | return 0; |
---|
| 81 | } |
---|
| 82 | |
---|
[2414] | 83 | int render_list(context_t *cx, int argc, char *argv[]) |
---|
[1193] | 84 | { |
---|
[2988] | 85 | caca_canvas_t *cv; |
---|
[2414] | 86 | int i, j, len; |
---|
[1385] | 87 | char *parser = NULL; |
---|
[1193] | 88 | |
---|
[2988] | 89 | cv = caca_create_canvas(0, 0); |
---|
[1385] | 90 | |
---|
| 91 | for(j = 0; j < argc; ) |
---|
[1241] | 92 | { |
---|
[1385] | 93 | char *cr; |
---|
[1193] | 94 | |
---|
[1385] | 95 | if(!parser) |
---|
| 96 | { |
---|
| 97 | if(j) |
---|
| 98 | cx->feed(cx, ' ', 0); |
---|
| 99 | parser = argv[j]; |
---|
| 100 | } |
---|
[1087] | 101 | |
---|
[1385] | 102 | cr = strchr(parser, '\n'); |
---|
| 103 | if(cr) |
---|
| 104 | len = (cr - parser) + 1; |
---|
| 105 | else |
---|
| 106 | len = strlen(parser); |
---|
| 107 | |
---|
[2988] | 108 | caca_set_canvas_size(cv, 0, 0); |
---|
| 109 | caca_import_memory(cv, parser, len, "utf8"); |
---|
| 110 | for(i = 0; i < caca_get_canvas_width(cv); i++) |
---|
[1241] | 111 | { |
---|
[2988] | 112 | uint32_t ch = caca_get_char(cv, i, 0); |
---|
| 113 | uint32_t at = caca_get_attr(cv, i, 0); |
---|
[1385] | 114 | cx->feed(cx, ch, at); |
---|
[2988] | 115 | if(caca_utf32_is_fullwidth(ch)) i++; |
---|
[1241] | 116 | } |
---|
[1385] | 117 | |
---|
| 118 | if(cr) |
---|
| 119 | { |
---|
| 120 | parser += len; |
---|
| 121 | render_flush(cx); |
---|
| 122 | } |
---|
| 123 | else |
---|
| 124 | { |
---|
| 125 | parser = NULL; |
---|
| 126 | j++; |
---|
| 127 | } |
---|
[1241] | 128 | } |
---|
[1087] | 129 | |
---|
[1243] | 130 | render_flush(cx); |
---|
[1087] | 131 | |
---|
[2988] | 132 | caca_free_canvas(cv); |
---|
[2414] | 133 | |
---|
[1194] | 134 | return 0; |
---|
| 135 | } |
---|
[1087] | 136 | |
---|
[1243] | 137 | int render_end(context_t *cx) |
---|
[1194] | 138 | { |
---|
[1243] | 139 | cx->end(cx); |
---|
[2988] | 140 | caca_free_canvas(cx->cv); |
---|
[1243] | 141 | |
---|
| 142 | return 0; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /* XXX: Following functions are local */ |
---|
| 146 | |
---|
| 147 | static int render_flush(context_t *cx) |
---|
| 148 | { |
---|
[1376] | 149 | unsigned long int len; |
---|
| 150 | void *buffer; |
---|
[1087] | 151 | |
---|
[1241] | 152 | /* Flush current line */ |
---|
| 153 | cx->flush(cx); |
---|
[1195] | 154 | |
---|
[1241] | 155 | /* Apply optional effects to our string */ |
---|
| 156 | filter_do(cx); |
---|
[1087] | 157 | |
---|
[1241] | 158 | /* Output line */ |
---|
[2988] | 159 | buffer = caca_export_memory(cx->torender, cx->export, &len); |
---|
[1318] | 160 | if(!buffer) |
---|
| 161 | return -1; |
---|
[1376] | 162 | fwrite(buffer, len, 1, stdout); |
---|
| 163 | free(buffer); |
---|
[2988] | 164 | caca_free_canvas(cx->torender); |
---|
[1194] | 165 | |
---|
| 166 | return 0; |
---|
[1087] | 167 | } |
---|
| 168 | |
---|