Rahul Rajput Rahul Rajput - 1 month ago 10
Node.js Question

How can I write ASCII art for text(using any language)?

I want to write a program to print following pattern for text "NUOSPIN":

* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * *


I am working on Javascript(NodeJS). I have to output it on console. Implementation in any language will work for me including java, C, javascript. I know I can do this by using prebuilt npm packages(like asciify), but I want to do this raw. What should be my approach for this? Do I have to write code for each character's pattern separately? And then print them by running logic of each character's pattern one by one?

Answer

You could use a bitmap and assemble the dots to the ASCII style, you want.

function getWord(s) {
    var ascii = [],
        font = {
            N: [7, 5, 5, 5],
            U: [5, 5, 5, 7],
            O: [7, 5, 5, 7],
            S: [7, 4, 7, 7],
            P: [7, 7, 4, 4],
            I: [1, 1, 1, 1]
        };

    s.split('').forEach(function (c) {
        var size =( font[c].reduce(function (r, a) {
            return r | a;
        }, 0)).toString(2).length;

        font[c].forEach(function (a, i) {
            var temp = a.toString(2).split('').map(function (c) {
                return +c ? '*' : ' ';
            });
            while (temp.length < size) {
                temp.unshift(' ');
            }
            ascii[i] = ascii[i] || [];
            ascii[i].push(temp.join(''));
        });
    });
    return ascii.map(function (a) {
        return a.join(' ');
    }).join('\n')
}

document.getElementById('tt').innerHTML = getWord('NUOSPIN');
<pre id="tt"></pre>