-
Notifications
You must be signed in to change notification settings - Fork 3
/
text-to-tinybraille.js
136 lines (135 loc) · 4.11 KB
/
text-to-tinybraille.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
var textToTinyBraille=(function (lines) {
// http://www.alanwood.net/unicode/braille_patterns.html
//dots:
//,___,
//|1 4|
//|2 5|
//|3 6|
//|7 8|
//`````
var braille_char_start = 0x2800
var pixel_map = [[0x01, 0x08],
[0x02, 0x10],
[0x04, 0x20],
[0x40, 0x80]]
var charactermap={
"A":["123","02","123"],
"B":["0123","013","12"],
"C":["12","03","03"],
"D":["0123","03","12"],
"E":["0123","013","03"],
"F":["0123","02","0"],
"G":["12","03","023"],
"H":["0123","2","0123"],
"I":["03","0123","03"],
"J":["2","3","012"],
"K":["0123","2","12","03"],
"L":["0123","3","3"],
"M":["0123","1","2","1","0123"],
"N":["0123","1","2","0123"],
"O":["12","03","03","12"],
"P":["0123","02","1"],
"Q":["12","03","023","123"],
"R":["0123","02","13"],
"S":["13","03","02"],
"T":["0","0123","0"],
"U":["012","3","3","012"],
"V":["01","2","3","2","01"],
"W":["012","3","12","3","012"],
"X":["023","1","023"],
"Y":["0","1","23","1","0"],
"Z":["03","023","013","03"],
".":["3"],
",":["3","2"],
";":["3","02"],
":":["13"],
"!":["013"],
"?":["0","03","1"],
"(":["12","03"],
")":["03","12"],
" ":["",""],
"+":["2","123","2"],
"-":["2","2","2"],
"\"":["01","","01"],
"'":["01"],
">":["13","2"],
"<":["2","13"],
"%":["03","2","1","03"],
"[":["0123","03"],
"]":["03","0123"],
"{":["2","123","13"],
"}":["13","123","2"],
"_":["3","3","3"],
"0":["12","03","12"],
"1":["13","0123","3"],
"2":["023","03","13"],
"3":["03","013","12"],
"4":["12","2","0123"],
"5":["013","03","02"],
"6":["123","023","023"],
"7":["03","02","01"],
"8":["12","013","12"],
"9":["12","02","0123"],
"/":["3","2","1","0"],
"\\":["0","1","2","3"],
"^":["1","0","1"],
"~":["1","0","1","0"],
"|":["0123"],
"@":["12","03","013"],
};
if(!input){
input="";
}
lines=lines.replace("\r\n","\n");
lines=lines.split("\n");
var outputlines=[];
for(var line in lines){
var input=lines[line];
input=input.toUpperCase()
var ids=[];
var si=0; //slicecount
var charid=0; //id of the current braille character
for (var i = 0, len = input.length; i < len; i++) {
var c=input[i];
if(charactermap[c]){
var cm=charactermap[c].slice()
//emtpy slice at the end of each characters
cm.push("");
for (var l in cm){
var slice=cm[l];
for(var k in slice){
var pixel=slice[k];
var add=pixel_map[pixel][si%2];
charid|=add;
}
si+=1;
ids.push(charid)
charid=0;
}
}
}
if(si % 2){
ids.push(charid)
}
var braillecharacters=[];
for (var i = 0, len = ids.length; i < len; i+=2) {
var id=ids[i]|ids[i+1]
var nextid=ids[i+2]|ids[i+3]
if(!id && nextid){
//For better line breaks replace empty Braille Pattern Blank with a space
braillecharacters[i]=" ";
}else{
braillecharacters[i]=String.fromCharCode(braille_char_start+id);
}
}
//if last character is a space, remove it. The second space is a Braille pattern dots-0
if([" ","⠀"].indexOf(braillecharacters[braillecharacters.length -1 ])!=-1){
braillecharacters.splice(-1);
}
outputlines.push(braillecharacters.join(""));
}
var output=outputlines.join("\n");
return output;
});
if (typeof module !== 'undefined') { module.exports = textToTinyBraille; }