Browse Source

* 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).
pull/1/head
Sam Hocevar sam 18 years ago
parent
commit
67e7bd971f
4 changed files with 59 additions and 25 deletions
  1. +20
    -11
      src/figlet.c
  2. +1
    -2
      src/figlet.h
  3. +17
    -12
      src/main.c
  4. +21
    -0
      src/toilet.h

+ 20
- 11
src/figlet.c View File

@@ -25,6 +25,7 @@
#include <string.h>
#include <cucul.h>

#include "toilet.h"
#include "figlet.h"

struct figfont
@@ -40,17 +41,19 @@ struct figfont
unsigned int *lookup;
};

static struct figfont *open_font(char const *);
static struct figfont *open_font(void);
static void free_font(struct figfont *);

cucul_canvas_t *render_figlet(uint32_t const *string, unsigned int length,
char const *fontname)
cucul_canvas_t *render_figlet(uint32_t const *string, unsigned int length)
{
cucul_canvas_t *cv;
struct figfont *font;
unsigned int x, i, c;

font = open_font(fontname);
font = open_font();

if(!font)
return NULL;

cv = cucul_create_canvas(length * font->max_length, font->height);

@@ -75,7 +78,7 @@ cucul_canvas_t *render_figlet(uint32_t const *string, unsigned int length,
return cv;
}

static struct figfont *open_font(char const *fontname)
static struct figfont *open_font(void)
{
char *data = NULL;
char path[2048];
@@ -84,14 +87,20 @@ static struct figfont *open_font(char const *fontname)
FILE *f;
unsigned int i, j, size, comment_lines;

/* Open font */
snprintf(path, 2047, "/usr/share/figlet/%s.flf", fontname);
/* Open font: try .tlf, then .flf */
snprintf(path, 2047, "%s/%s.tlf", toilet_dir, toilet_font);
path[2047] = '\0';
f = fopen(path, "r");
if(!f)
{
fprintf(stderr, "font `%s' not found\n", path);
return NULL;
snprintf(path, 2047, "%s/%s.flf", toilet_dir, toilet_font);
path[2047] = '\0';
f = fopen(path, "r");
if(!f)
{
fprintf(stderr, "font `%s' not found\n", path);
return NULL;
}
}

font = malloc(sizeof(struct figfont));
@@ -100,7 +109,7 @@ static struct figfont *open_font(char const *fontname)
font->print_direction = 0;
font->full_layout = 0;
font->codetag_count = 0;
if(fscanf(f, "flf2a%c %u %u %u %i %u %u %u %u\n", &font->hardblank,
if(fscanf(f, "%*[ft]lf2a%c %u %u %u %i %u %u %u %u\n", &font->hardblank,
&font->height, &font->baseline, &font->max_length,
&font->old_layout, &comment_lines, &font->print_direction,
&font->full_layout, &font->codetag_count) < 6)
@@ -168,7 +177,7 @@ static struct figfont *open_font(char const *fontname)

/* Import buffer into canvas */
b = cucul_load_memory(data, i);
font->image = cucul_import_canvas(b, "ansi");
font->image = cucul_import_canvas(b, "utf8");
cucul_free_buffer(b);
free(data);



+ 1
- 2
src/figlet.h View File

@@ -14,6 +14,5 @@
/*
* This header defines functions for handling FIGlet fonts.
*/
extern cucul_canvas_t *render_figlet(uint32_t const *, unsigned int,
char const *);
extern cucul_canvas_t *render_figlet(uint32_t const *, unsigned int);


+ 17
- 12
src/main.c View File

@@ -31,10 +31,15 @@
#include <stdlib.h>
#include <cucul.h>

#include "toilet.h"
#include "render.h"
#include "figlet.h"
#include "filters.h"

char const *toilet_export = "utf8";
char const *toilet_font = "mono9";
char const *toilet_dir = "/usr/share/figlet/";

static void version(void);
#if defined(HAVE_GETOPT_H)
static void usage(void);
@@ -50,9 +55,6 @@ int main(int argc, char *argv[])

int i;

char const *export = "utf8";
char const *font = "mono9";
char const *dir = "/usr/share/figlet/";
unsigned int flag_gay = 0;
unsigned int flag_metal = 0;
unsigned int term_width = 80;
@@ -101,10 +103,10 @@ int main(int argc, char *argv[])
version();
return 0;
case 'f': /* --font */
font = optarg;
toilet_font = optarg;
break;
case 'd': /* --directory */
dir = optarg;
toilet_dir = optarg;
break;
case 'g': /* --gay */
flag_gay = 1;
@@ -128,7 +130,7 @@ int main(int argc, char *argv[])
break;
}
case 'i': /* --irc */
export = "irc";
toilet_export = "irc";
break;
case '?':
printf(MOREINFO, argv[0]);
@@ -155,10 +157,10 @@ int main(int argc, char *argv[])
printf("20201\n");
return 0;
case 2:
printf("%s\n", dir);
printf("%s\n", toilet_dir);
return 0;
case 3:
printf("%s\n", font);
printf("%s\n", toilet_font);
return 0;
case 4:
printf("%u\n", term_width);
@@ -200,15 +202,18 @@ int main(int argc, char *argv[])
}

/* Render string to canvas */
if(!strcasecmp(font, "mono9"))
if(!strcasecmp(toilet_font, "mono9"))
{
cv = render_big(string, length);
filter_autocrop(cv);
}
else if(!strcasecmp(font, "term"))
else if(!strcasecmp(toilet_font, "term"))
cv = render_tiny(string, length);
else
cv = render_figlet(string, length, font);
cv = render_figlet(string, length);

if(!cv)
return -1;

/* Do gay stuff with our string (léopard) */
if(flag_metal)
@@ -217,7 +222,7 @@ int main(int argc, char *argv[])
filter_gay(cv);

/* Output char */
buffer = cucul_export_canvas(cv, export);
buffer = cucul_export_canvas(cv, toilet_export);
fwrite(cucul_get_buffer_data(buffer),
cucul_get_buffer_size(buffer), 1, stdout);
cucul_free_buffer(buffer);


+ 21
- 0
src/toilet.h View File

@@ -0,0 +1,21 @@
/*
* TOIlet The Other Implementation’s letters
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Do What The Fuck You Want To
* Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

/*
* This header defines global variables.
*/

extern char const *toilet_export;
extern char const *toilet_font;
extern char const *toilet_dir;


Loading…
Cancel
Save