source: toilet/trunk/src/figlet.c @ 4345

Last change on this file since 4345 was 4345, checked in by Sam Hocevar, 12 years ago

Search for missing fonts in the current directory (Fixes #40) and display
an error message in case of failure.

  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1/*
2 *  TOIlet        The Other Implementation’s letters
3 *  Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: figlet.c 4345 2010-02-08 23:34:05Z 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 functions for handling FIGlet fonts.
17 */
18
19#include "config.h"
20
21#if defined(HAVE_INTTYPES_H)
22#   include <inttypes.h>
23#endif
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <caca.h>
28
29#include "toilet.h"
30#include "render.h"
31
32#define STD_GLYPHS (127 - 32)
33#define EXT_GLYPHS (STD_GLYPHS + 7)
34
35static int feed_figlet(context_t *, uint32_t, uint32_t);
36static int flush_figlet(context_t *);
37static int end_figlet(context_t *);
38
39int init_figlet(context_t *cx)
40{
41    char path[2048];
42
43    snprintf(path, 2047, "%s/%s", cx->dir, cx->font);
44    if(caca_canvas_set_figfont(cx->cv, path))
45    {
46        snprintf(path, 2047, "./%s", cx->font);
47        if(caca_canvas_set_figfont(cx->cv, path))
48        {
49            fprintf(stderr, "error: could not load font %s\n", cx->font);
50            return -1;
51        }
52    }
53
54    cx->feed = feed_figlet;
55    cx->flush = flush_figlet;
56    cx->end = end_figlet;
57
58    return 0;
59}
60
61static int feed_figlet(context_t *cx, uint32_t ch, uint32_t attr)
62{
63    return caca_put_figchar(cx->cv, ch);
64}
65
66static int flush_figlet(context_t *cx)
67{
68    /* We copy cx->cv into cx->torender instead of swapping pointers
69     * because that would lose the figfont information. */
70    /* FIXME: use caca_copy_canvas() or whatever when it's implemented. */
71    int ret = caca_flush_figlet(cx->cv);
72    cx->torender = caca_create_canvas(caca_get_canvas_width(cx->cv),
73                                      caca_get_canvas_height(cx->cv));
74    caca_blit(cx->torender, 0, 0, cx->cv, NULL);
75    caca_set_canvas_size(cx->cv, 0, 0);
76    return ret;
77}
78
79static int end_figlet(context_t *cx)
80{
81    return caca_canvas_set_figfont(cx->cv, NULL);
82}
83
Note: See TracBrowser for help on using the repository browser.