Question

I am trying to generate ascii art text for a fun application. From FIGLET, I got the ASCII pattern. I am using that pattern in a printf statement to print letters. Here is a screenshot of the pattern I got from FIGLET:

FIGLET

Here is the code snippet I use to print A:

printf("      _/_/\n   _/    _/\n  _/_/_/_/\n _/    _/\n_/    _/\n");

Now, I take an input text from user, and show it in ASCII art. As I use printf, I can only generate it vertically:

Vertical image

But I need to do horizontal ASCII art. How to do that ?

Was it helpful?

Solution

Yes, this is a well known problem. The last time I solved this is to use an array for each line and rendering each letter separately.

Firstly, I would represent each letter in an array. For example your A would be something like this:

char* letter[8]; 
letter[0] = "      _/_/ ";
letter[1] = "   _/    _/";
etc.

(Actually a 2D array would be used where each letter is at a different index.)

The actual render would be in an array as well, something along the lines of:

char* render[8];

and then use concatenation to build each line. A simply nested for loop should do the trick:

for each line, i to render (i.e the height of the letters)
    for each letter, j
       concatenate line i of letter j to the to char* i in the array

Finally loop though the array and print each line. Actually, you can skip the render array and simply print each line without a carriage return. Something like the following comes to mind:

for each line, i to render : // (i.e the height of the letters) 
    for each letter, j {
       output line i of letter j
    }
    output a carriage return
}

(My C is a bit rusty, from there the "pseudo" code. However, I think my logic is sound.)

OTHER TIPS

You can try something like the following:

NOTE: Obviously, the following lacks in a lot of things like memory de-allocation, error checking, incomplete code, etc. The idea is to give you a hint!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define ROW 4
#define COL 8
#define CHAR_INDEX_MAX 26

enum alph_enum {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z};

typedef struct _alphabets {
    char *line[ROW];
}alphabet;

alphabet chars[26];

init_a(enum alph_enum letter)
{
    int i;
    for (i=0 ; i<ROW ; i++) {
        chars[letter].line[i] = (char *) malloc(COL);
        if (chars[letter].line[i] == NULL) {
            printf("memory allocation failed \n");
            return;
        }
    }

    switch (letter) {
                                     /*0123 4567*/
    case H:
        strcpy(chars[letter].line[0], "|       |");
        strcpy(chars[letter].line[1], "|_______|");
        strcpy(chars[letter].line[2], "|       |");
        strcpy(chars[letter].line[3], "|       |");
        break;
    case E:
        strcpy(chars[letter].line[0], "|-------");
        strcpy(chars[letter].line[1], "|_______");
        strcpy(chars[letter].line[2], "|       ");
        strcpy(chars[letter].line[3], "|_______");
        break;
    case L:
        strcpy(chars[letter].line[0], "|       ");
        strcpy(chars[letter].line[1], "|       ");
        strcpy(chars[letter].line[2], "|       ");
        strcpy(chars[letter].line[3], "|_______");
        break;
    /*  for all the other alphabets */
    }

    return;

}

print_str(char word[])
{
    int i, j;

    printf("\n");
    for (i=0; i<ROW; i++) {
        for (j=0 ; j<strlen(word) ; j++) {
            printf("%s", chars[word[j] % 'A'].line[i]);
        }
        printf("\n");
    }
    printf("\n");

    return;
}


int main(void)
{
    init_a(H);
    init_a(E);
    init_a(L);
    /* print_str("HELLO"); */
    print_str("HELL");
    return 0;

    /* free the memory for HEL here */
}

The output will be as follows:

> gcc test.c -o print
> print

|       ||-------|       |
|_______||_______|       |
|       ||       |       |
|       ||_______|_______|_______

>

TODO:

  • You can include numbers, special characters (like !, @, #, $, etc), blank space, may be all ASCII chars makes sense.
  • You can support bold, italics, underline and highlight for the characters
  • You can support foreground and background colors
  • You can support animation

Hope this helps!

Instead of printing one whole character at a time as one string, divide the characters into multiple strings -- one for each line of text that makes up the character. Then print the first line of each character, newline, then the second line of each character, etc. You'll need to pad the lines with spaces to make sure they're all the right width.

Great to see that you like Figlet. The Figlet widget you are showing does actually contain a command line tool that generates the ASCII output. http://www.figlet.org/ It can also easily be installed with brew http://mxcl.github.com/homebrew/.

Think you could easily capture its output from within your own program by popen(..) or similar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top