-
Notifications
You must be signed in to change notification settings - Fork 34
/
asciimo.js
113 lines (100 loc) · 2.64 KB
/
asciimo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* asciimo.js
*
* Copyright (c) 2010 Marak Squires
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
* http://github.com/marak/figlet-js
* Figlet JS
*
* Copyright (c) 2010 Scott González
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* http://github.com/scottgonzalez/figlet-js
*/
(function() {
// Remark : not a very good env check
var fontPath;
if(typeof __dirname == 'undefined'){
fontPath = './fonts/';
}
else{
fontPath = __dirname + '/../fonts/';
}
var Figlet = (typeof exports !== "undefined" ? exports : window).Figlet = {
fonts: {},
parseFont: function(name, fn) { // debug.log('parseFont', name);
if (name in Figlet.fonts) {
fn();
}
Figlet.loadFont(name, function(defn) {
Figlet._parseFont(name, defn, fn);
});
},
_parseFont: function(name, defn, fn) {
var lines = defn.split("\n"),
header = lines[0].split(" "),
hardblank = header[0].charAt(header[0].length - 1),
height = +header[1],
comments = +header[5];
Figlet.fonts[name] = {
defn: lines.slice(comments + 1),
hardblank: hardblank,
height: height,
char: {}
};
fn();
},
parseChar: function(char, font) { // debug.log('parseChar');
var fontDefn = Figlet.fonts[font];
if (char in fontDefn.char) {
return fontDefn.char[char];
}
var height = fontDefn.height,
start = (char - 32) * height,
charDefn = [],
i;
for (i = 0; i < height; i++) {
charDefn[i] = fontDefn.defn[start + i]
.replace(/@/g, "")
.replace(RegExp("\\" + fontDefn.hardblank, "g"), " ");
}
return fontDefn.char[char] = charDefn;
},
write: function(str, font, fn) {
Figlet.parseFont(font, function() { // debug.log('parseFont');
var chars = [],
result = "";
for (var i = 0, len = str.length; i < len; i++) {
chars[i] = Figlet.parseChar(str.charCodeAt(i), font);
}
for (i = 0, height = chars[0].length; i < height; i++) {
for (var j = 0; j < len; j++) {
result += chars[j][i];
}
result += "\n";
}
fn(result, font);
});
},
loadFont: function(name, fn) {
if(typeof exports != 'undefined'){
var util = require('util');
require("fs").readFile(fontPath + name + ".flf", "utf-8", function(err, contents) {
if (err) {
util.puts(err);
}
else {
fn(contents);
}
});
}
else{
$.get(fontPath + name+ '.flf',function(contents){
fn(contents);
});
}
}
};
})();