Created
August 15, 2017 03:22
-
-
Save kddlb/b257c6dc8659b5ff25f9f9bb1033684c to your computer and use it in GitHub Desktop.
JSON "markup" to ESC/POS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.parseToControlCommands = function (ob) { | |
let ns = ""; | |
ob.input.forEach(function (t, number, ts) { | |
if (typeof(t) === "string") { | |
ns = ns.concat(t) | |
} else { | |
switch (t.command) { | |
case "tab": | |
ns = ns.concat("\x09"); | |
break; | |
case "lineFeed": | |
if (!t.hasOwnProperty("value")) { | |
ns = ns.concat("\x0a"); | |
break; | |
} | |
if (!(t.value >= 0 && t.value <= 255)) { | |
throw new Error("The tag's value should be between 0 and 255."); | |
} | |
ns = ns.concat("\x1b\x64").concat(String.fromCharCode(t.value)); | |
break; | |
case "setRightMargin": | |
if (!t.hasOwnProperty("value")) { | |
throw new Error("A setRightMargin tag has no value."); | |
} | |
if (!(t.value >= 0 && t.value <= 255)) { | |
throw new Error("The tag's value should be between 0 and 255."); | |
} | |
ns = ns.concat("\x1b\x20").concat(String.fromCharCode(t.value)); | |
break; | |
case "underline": | |
let ul; | |
if (!t.hasOwnProperty("value")) { | |
throw new Error("An underline tag has no value."); | |
} | |
if (typeof(t.value) === "string") { | |
if (t.value !== "double") throw new Error("The tag's value should be either true, false or \"double\"."); | |
ul = 2; | |
} | |
if (typeof(t.value) === "boolean") ul = t.value ? 1 : 0; | |
ns = ns.concat("\x1b\x2d").concat(String.fromCharCode(ul)); | |
break; | |
case "lineSpacing": | |
let ls; | |
if (!t.hasOwnProperty("value")) { | |
throw new Error("A lineSpacing tag has no value."); | |
} | |
if (typeof(t.value) === "string") { | |
if (t.value !== "default") throw new Error("The tag's value should be either a number between 0 and 255, or \"default\"."); | |
ls = 2; | |
} | |
if (typeof(t.value) === "number") { | |
if (!(t.value >= 0 && t.value <= 255)) { | |
throw new Error("The tag's value should be between 0 and 255."); | |
} | |
ls = 1 | |
} | |
ns = ls === 2 ? ns.concat("\x1b\x32") : ns.concat("\x1b\x33").concat(String.fromCharCode(t.value)); | |
break; | |
case "reset": | |
ns = ns.concat("\x1b\x40"); | |
break; | |
case "bold": | |
if (!t.hasOwnProperty("value")) { | |
throw new Error("A bold tag has no value."); | |
} | |
if (typeof(t.value) !== "boolean") { | |
throw new Error("The tag's value should be true or false."); | |
} | |
ns = ns.concat("\x1b\x45").concat(String.fromCharCode(t.value ? 1 : 0)); | |
break; | |
case "justify": | |
let j; | |
if (!t.hasOwnProperty("value")) { | |
throw new Error("A justify tag has no value."); | |
} | |
if (typeof(t.value) !== "string") { | |
throw new Error("The tag's value should be \"left\", \"center\", or \"right\"."); | |
} | |
switch (t.value) { | |
case "left": | |
j=0; | |
break; | |
case "center": | |
j=1; | |
break; | |
case "right": | |
j=2; | |
break; | |
default: | |
throw new Error("The tag's value should be \"left\", \"center\", or \"right\"."); | |
} | |
ns = ns.concat("\x1b\x61").concat(String.fromCharCode(j)); | |
break; | |
case "setFontSize": | |
if (!t.hasOwnProperty("value")) { | |
throw new Error("A setFontSize tag has no value."); | |
} | |
if (typeof(t.value) !== "object") { | |
throw new Error("The tag's value should be an object containing the \"h\" and \"v\" properties."); | |
} | |
ns = ns.concat("\x1d\x21").concat(String.fromCharCode(parseInt(`0x${t.value.h}${t.value.v}`))); | |
break; | |
case "reverseVideo": | |
if (!t.hasOwnProperty("value")) { | |
throw new Error("A bold reverseVideo has no value."); | |
} | |
if (typeof(t.value) !== "boolean") { | |
throw new Error("The tag's value should be true or false."); | |
} | |
ns = ns.concat("\x1d\x42").concat(String.fromCharCode(t.value ? 1 : 0)); | |
break; | |
case "setLeftMargin": | |
if (!t.hasOwnProperty("value")) { | |
throw new Error("A setLeftMargin tag has no value."); | |
} | |
if (!(t.value >= 0 && t.value <= 255)) { | |
throw new Error("The tag's value should be between 0 and 255."); | |
} | |
ns = ns.concat("\x1d\x4c").concat(String.fromCharCode(t.value)); | |
break; | |
case "cutPaper": | |
ns = ns.concat("\x1d\x56\x01"); | |
break; | |
} | |
} | |
}); | |
return Buffer.from(ns, 'latin1'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment