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

Last change on this file since 1376 was 1376, checked in by Sam Hocevar, 14 years ago
  • Now that libcaca 0.99.beta10 is out, we can switch to the new API.
  • Property svn:keywords set to Id
File size: 2.4 KB
RevLine 
[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 1376 2006-11-12 20:37:58Z sam $
[1084]7 *
[1087]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.
[1084]12 */
13
[1102]14/*
15 * This file contains text to canvas rendering functions.
16 */
17
[1084]18#include "config.h"
19
[1087]20#if defined(HAVE_INTTYPES_H)
21#   include <inttypes.h>
22#endif
23#include <stdlib.h>
[1241]24#include <string.h>
25#include <stdio.h>
[1100]26#include <cucul.h>
[1084]27
[1193]28#include "toilet.h"
[1100]29#include "render.h"
[1241]30#include "filter.h"
[1087]31
[1243]32static int render_flush(context_t *);
33
[1241]34int render_init(context_t *cx)
[1193]35{
[1242]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
[1241]41    if(!strcasecmp(cx->font, "term"))
42        return init_tiny(cx);
[1193]43
[1241]44    return init_figlet(cx);
[1193]45}
46
[1241]47int render_stdin(context_t *cx)
[1193]48{
[1241]49    char buf[10];
50    unsigned int i = 0, len;
51    uint32_t ch;
[1195]52
[1241]53    /* Read from stdin */
54    while(!feof(stdin))
[1194]55    {
[1241]56        buf[i++] = getchar();
57        buf[i] = '\0';
[1193]58
[1241]59        ch = cucul_utf8_to_utf32(buf, &len);
[1194]60
[1241]61        if(!len)
62            continue;
[1193]63
[1241]64        cx->feed(cx, ch);
65        i = 0;
[1194]66
[1241]67        if(ch == '\n')
[1243]68            render_flush(cx);
[1241]69    }
[1193]70
71    return 0;
72}
73
[1241]74int render_list(context_t *cx, unsigned int argc, char *argv[])
[1193]75{
[1241]76    unsigned int i, j;
[1193]77
[1241]78    for(i = 0; i < argc; i++)
79    {
80        /* Read from commandline */
81        unsigned int len;
[1193]82
[1241]83        if(i)
84            cx->feed(cx, ' ');
[1087]85
[1241]86        for(j = 0; argv[i][j];)
87        {
88            cx->feed(cx, cucul_utf8_to_utf32(argv[i] + j, &len));
89            j += len;
90        }
91    }
[1087]92
[1243]93    render_flush(cx);
[1087]94
[1194]95    return 0;
96}
[1087]97
[1243]98int render_end(context_t *cx)
[1194]99{
[1243]100    cx->end(cx);
101    cucul_free_canvas(cx->cv);
102
103    return 0;
104}
105
106/* XXX: Following functions are local */
107
108static int render_flush(context_t *cx)
109{
[1376]110    unsigned long int len;
111    void *buffer;
[1087]112
[1241]113    /* Flush current line */
114    cx->flush(cx);
[1195]115
[1241]116    /* Apply optional effects to our string */
117    filter_do(cx);
[1087]118
[1243]119    cx->lines += cucul_get_canvas_height(cx->torender);
120
[1241]121    /* Output line */
[1376]122    buffer = cucul_export_memory(cx->torender, cx->export, &len);
[1318]123    if(!buffer)
124        return -1;
[1376]125    fwrite(buffer, len, 1, stdout);
126    free(buffer);
[1241]127    cucul_free_canvas(cx->torender);
[1194]128
129    return 0;
[1087]130}
131
Note: See TracBrowser for help on using the repository browser.