Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Ascii art text in C

Tags:

c

ascii-art

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 ?

like image 292
utsabiem Avatar asked Dec 21 '11 07:12

utsabiem


2 Answers

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.)

like image 98
Jaco Van Niekerk Avatar answered Sep 28 '22 23:09

Jaco Van Niekerk


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!

like image 29
Sangeeth Saravanaraj Avatar answered Sep 28 '22 21:09

Sangeeth Saravanaraj