Support for the FreeMonoTengwar font and ConScript encoding
[tengwarjs.git] / notation.js
1
2 exports.encode = encode;
3 function encode(sections) {
4 return sections.map(function (section) {
5 return section.map(function (paragraph) {
6 return paragraph.map(function (line) {
7 return line.map(function (word) {
8 return word.map(function (column) {
9 var parts = [];
10 if (column.above)
11 parts.push(column.above);
12 if (column.below)
13 parts.push(column.below);
14 if (column.following)
15 parts.push(column.following);
16 if (column.tildeAbove)
17 parts.push("tilde-above");
18 if (column.tildeBelow)
19 parts.push("tilde-below");
20 if (parts.length) {
21 return column.tengwa + ":" + parts.join(",");
22 } else {
23 return column.tengwa;
24 }
25 }).join(";");
26 }).join(" ");;
27 }).join("\n");
28 }).join("\n\n");
29 }).join("\n\n\n");
30 }
31
32 exports.decode = decode;
33 function decode(encoding, makeColumn) {
34 return encoding.split("\n\n\n").map(function (section) {
35 return section.split("\n\n").map(function (paragraph) {
36 return paragraph.split("\n").map(function (line) {
37 return line.split(" ").map(function (word) {
38 return decodeWord(word, makeColumn);
39 });
40 });
41 });
42 });
43 }
44
45 exports.decodeWord = decodeWord;
46 function decodeWord(word, makeColumn) {
47 return word.split(";").map(function (column) {
48 var parts = column.split(":");
49 var tengwa = parts.shift();
50 var tehtar = parts.length ? parts.shift().split(",") : [];
51 var result = makeColumn(tengwa);
52 tehtar.forEach(function (tehta) {
53 if (tehta === "tilde-above") {
54 result.addTildeAbove();
55 } else if (tehta === "tilde-below") {
56 result.addTildeBelow();
57 } else if (tehta === "y") {
58 result.addBelow("y");
59 } else if (
60 tehta === "s" ||
61 tehta === "s-inverse" ||
62 tehta === "s-extended" ||
63 tehta === "s-flourish"
64 ) {
65 if (
66 tehta === "s" &&
67 (tengwa === "calma" || tengwa === "quesse")
68 ) {
69 result.addBelow(tehta, "s");
70 } else {
71 result.addFollowing(tehta, "s");
72 }
73 } else {
74 result.addAbove(tehta, "s");
75 }
76 });
77 return result;
78 });
79 }
80