Changeset 1143


Ignore:
Timestamp:
Sep 30, 2006, 1:06:20 PM (14 years ago)
Author:
Sam Hocevar
Message:
  • Better error checking in the figlet renderer.
  • Allow to open fonts that have Unicode data (requires libcucul 0.99.beta6 which is not yet released).
Location:
toilet/trunk/src
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • toilet/trunk/src/figlet.c

    r1113 r1143  
    2626#include <cucul.h>
    2727
     28#include "toilet.h"
    2829#include "figlet.h"
    2930
     
    4142};
    4243
    43 static struct figfont *open_font(char const *);
     44static struct figfont *open_font(void);
    4445static void free_font(struct figfont *);
    4546
    46 cucul_canvas_t *render_figlet(uint32_t const *string, unsigned int length,
    47                               char const *fontname)
     47cucul_canvas_t *render_figlet(uint32_t const *string, unsigned int length)
    4848{
    4949    cucul_canvas_t *cv;
     
    5151    unsigned int x, i, c;
    5252
    53     font = open_font(fontname);
     53    font = open_font();
     54
     55    if(!font)
     56        return NULL;
    5457
    5558    cv = cucul_create_canvas(length * font->max_length, font->height);
     
    7679}
    7780
    78 static struct figfont *open_font(char const *fontname)
     81static struct figfont *open_font(void)
    7982{
    8083    char *data = NULL;
     
    8588    unsigned int i, j, size, comment_lines;
    8689
    87     /* Open font */
    88     snprintf(path, 2047, "/usr/share/figlet/%s.flf", fontname);
     90    /* Open font: try .tlf, then .flf */
     91    snprintf(path, 2047, "%s/%s.tlf", toilet_dir, toilet_font);
    8992    path[2047] = '\0';
    9093    f = fopen(path, "r");
    9194    if(!f)
    9295    {
    93         fprintf(stderr, "font `%s' not found\n", path);
    94         return NULL;
     96        snprintf(path, 2047, "%s/%s.flf", toilet_dir, toilet_font);
     97        path[2047] = '\0';
     98        f = fopen(path, "r");
     99        if(!f)
     100        {
     101            fprintf(stderr, "font `%s' not found\n", path);
     102            return NULL;
     103        }
    95104    }
    96105
     
    101110    font->full_layout = 0;
    102111    font->codetag_count = 0;
    103     if(fscanf(f, "flf2a%c %u %u %u %i %u %u %u %u\n", &font->hardblank,
     112    if(fscanf(f, "%*[ft]lf2a%c %u %u %u %i %u %u %u %u\n", &font->hardblank,
    104113              &font->height, &font->baseline, &font->max_length,
    105114              &font->old_layout, &comment_lines, &font->print_direction,
     
    169178    /* Import buffer into canvas */
    170179    b = cucul_load_memory(data, i);
    171     font->image = cucul_import_canvas(b, "ansi");
     180    font->image = cucul_import_canvas(b, "utf8");
    172181    cucul_free_buffer(b);
    173182    free(data);
  • toilet/trunk/src/figlet.h

    r1102 r1143  
    1515 * This header defines functions for handling FIGlet fonts.
    1616 */
    17 extern cucul_canvas_t *render_figlet(uint32_t const *, unsigned int,
    18                                      char const *);
     17extern cucul_canvas_t *render_figlet(uint32_t const *, unsigned int);
    1918
  • toilet/trunk/src/main.c

    r1116 r1143  
    3232#include <cucul.h>
    3333
     34#include "toilet.h"
    3435#include "render.h"
    3536#include "figlet.h"
    3637#include "filters.h"
     38
     39char const *toilet_export = "utf8";
     40char const *toilet_font = "mono9";
     41char const *toilet_dir = "/usr/share/figlet/";
    3742
    3843static void version(void);
     
    5156    int i;
    5257
    53     char const *export = "utf8";
    54     char const *font = "mono9";
    55     char const *dir = "/usr/share/figlet/";
    5658    unsigned int flag_gay = 0;
    5759    unsigned int flag_metal = 0;
     
    102104            return 0;
    103105        case 'f': /* --font */
    104             font = optarg;
     106            toilet_font = optarg;
    105107            break;
    106108        case 'd': /* --directory */
    107             dir = optarg;
     109            toilet_dir = optarg;
    108110            break;
    109111        case 'g': /* --gay */
     
    129131        }
    130132        case 'i': /* --irc */
    131             export = "irc";
     133            toilet_export = "irc";
    132134            break;
    133135        case '?':
     
    156158            return 0;
    157159        case 2:
    158             printf("%s\n", dir);
     160            printf("%s\n", toilet_dir);
    159161            return 0;
    160162        case 3:
    161             printf("%s\n", font);
     163            printf("%s\n", toilet_font);
    162164            return 0;
    163165        case 4:
     
    201203
    202204    /* Render string to canvas */
    203     if(!strcasecmp(font, "mono9"))
     205    if(!strcasecmp(toilet_font, "mono9"))
    204206    {
    205207        cv = render_big(string, length);
    206208        filter_autocrop(cv);
    207209    }
    208     else if(!strcasecmp(font, "term"))
     210    else if(!strcasecmp(toilet_font, "term"))
    209211        cv = render_tiny(string, length);
    210212    else
    211         cv = render_figlet(string, length, font);
     213        cv = render_figlet(string, length);
     214
     215    if(!cv)
     216        return -1;
    212217
    213218    /* Do gay stuff with our string (léopard) */
     
    218223
    219224    /* Output char */
    220     buffer = cucul_export_canvas(cv, export);
     225    buffer = cucul_export_canvas(cv, toilet_export);
    221226    fwrite(cucul_get_buffer_data(buffer),
    222227           cucul_get_buffer_size(buffer), 1, stdout);
Note: See TracChangeset for help on using the changeset viewer.