| 1 | /* |
|---|
| 2 | * TOIlet The Other Implementation’s letters |
|---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 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 <caca.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 = caca_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 | caca_canvas_t *cv; |
|---|
| 51 | char *line; |
|---|
| 52 | int i, len; |
|---|
| 53 | |
|---|
| 54 | /* FIXME: we can't read longer lines */ |
|---|
| 55 | len = 1024; |
|---|
| 56 | line = malloc(len); |
|---|
| 57 | cv = caca_create_canvas(0, 0); |
|---|
| 58 | |
|---|
| 59 | /* Read from stdin */ |
|---|
| 60 | while(!feof(stdin)) |
|---|
| 61 | { |
|---|
| 62 | if(!fgets(line, len, stdin)) |
|---|
| 63 | break; |
|---|
| 64 | |
|---|
| 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++) |
|---|
| 68 | { |
|---|
| 69 | uint32_t ch = caca_get_char(cv, i, 0); |
|---|
| 70 | uint32_t at = caca_get_attr(cv, i, 0); |
|---|
| 71 | cx->feed(cx, ch, at); |
|---|
| 72 | if(caca_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, int argc, char *argv[]) |
|---|
| 84 | { |
|---|
| 85 | caca_canvas_t *cv; |
|---|
| 86 | int i, j, len; |
|---|
| 87 | char *parser = NULL; |
|---|
| 88 | |
|---|
| 89 | cv = caca_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 | 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++) |
|---|
| 111 | { |
|---|
| 112 | uint32_t ch = caca_get_char(cv, i, 0); |
|---|
| 113 | uint32_t at = caca_get_attr(cv, i, 0); |
|---|
| 114 | cx->feed(cx, ch, at); |
|---|
| 115 | if(caca_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 | caca_free_canvas(cv); |
|---|
| 133 | |
|---|
| 134 | return 0; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | int render_end(context_t *cx) |
|---|
| 138 | { |
|---|
| 139 | cx->end(cx); |
|---|
| 140 | caca_free_canvas(cx->cv); |
|---|
| 141 | |
|---|
| 142 | return 0; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /* XXX: Following functions are local */ |
|---|
| 146 | |
|---|
| 147 | static int render_flush(context_t *cx) |
|---|
| 148 | { |
|---|
| 149 | unsigned long int len; |
|---|
| 150 | void *buffer; |
|---|
| 151 | |
|---|
| 152 | /* Flush current line */ |
|---|
| 153 | cx->flush(cx); |
|---|
| 154 | |
|---|
| 155 | /* Apply optional effects to our string */ |
|---|
| 156 | filter_do(cx); |
|---|
| 157 | |
|---|
| 158 | /* Output line */ |
|---|
| 159 | buffer = caca_export_memory(cx->torender, cx->export, &len); |
|---|
| 160 | if(!buffer) |
|---|
| 161 | return -1; |
|---|
| 162 | fwrite(buffer, len, 1, stdout); |
|---|
| 163 | free(buffer); |
|---|
| 164 | caca_free_canvas(cx->torender); |
|---|
| 165 | |
|---|
| 166 | return 0; |
|---|
| 167 | } |
|---|
| 168 | |
|---|