Skip to content
Snippets Groups Projects
Commit dc6caaf2 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Add tdfiglet -R option for random font with retry (e.g. if too wide for screen)

... use with caution as you could get into an infinite exception/retry loop
using this feature.

tdfonts_lib opt.retry added (default: false/disabled)

Removed some unnecessary comments from lib source
parent c0a9c942
Branches
No related tags found
No related merge requests found
...@@ -20,17 +20,13 @@ var CENTER_JUSTIFY = 2; ...@@ -20,17 +20,13 @@ var CENTER_JUSTIFY = 2;
var DEFAULT_WIDTH = 80; var DEFAULT_WIDTH = 80;
// Default font directory and extension - adjust as needed for your Synchronet setup var FONT_DIR = system.ctrl_dir + 'tdfonts/';
var FONT_DIR = system.ctrl_dir + 'tdfonts/'; // Assuming fonts directory is relative to the script
var FONT_EXT = "tdf"; var FONT_EXT = "tdf";
var DEFAULT_FONT = "brndamgx"; var DEFAULT_FONT = "brndamgx";
// Character list // Character list
var charlist = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; var charlist = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
// Synchronet JS does not have direct equivalents for gettimeofday and srand for seeding random.
// We will use Math.random() directly for random font selection if needed.
function loadfont(fn_arg) { function loadfont(fn_arg) {
var font = {}; // Use object for font_t var font = {}; // Use object for font_t
var map = null; // Represents the font file data var map = null; // Represents the font file data
...@@ -344,6 +340,9 @@ function reset_color() ...@@ -344,6 +340,9 @@ function reset_color()
} }
function output(str, font) { function output(str, font) {
var orgfont = font;
while (true) {
try {
if (!font) { // Random font file selection if (!font) { // Random font file selection
var fontDir = FONT_DIR; var fontDir = FONT_DIR;
var files = directory(fontDir + "/*.tdf"); // Get all .tdf files var files = directory(fontDir + "/*.tdf"); // Get all .tdf files
...@@ -360,8 +359,9 @@ function output(str, font) { ...@@ -360,8 +359,9 @@ function output(str, font) {
var array = str.split(/\s+/); var array = str.split(/\s+/);
if (array.length > 1) { if (array.length > 1) {
var out = ""; var out = "";
while (str = array.shift()) { var word;
out += output_line(str, font); while (word = array.shift()) {
out += output_line(word, font);
if (array.length && (!this.opt || opt.blankline !== false)) if (array.length && (!this.opt || opt.blankline !== false))
out += "\r\n"; out += "\r\n";
} }
...@@ -369,6 +369,16 @@ function output(str, font) { ...@@ -369,6 +369,16 @@ function output(str, font) {
} }
} }
return output_line(str, font); return output_line(str, font);
} catch(e) {
if (!orgfont && this.opt && opt.retry === true) {
if (opt.info)
alert("exception: " + e);
font = undefined;
continue;
}
throw e;
}
}
} }
// Calculate the total width of the string using the font // Calculate the total width of the string using the font
...@@ -420,9 +430,10 @@ function output_line(str, font) { ...@@ -420,9 +430,10 @@ function output_line(str, font) {
if (justify === CENTER_JUSTIFY) { if (justify === CENTER_JUSTIFY) {
padding = Math.floor((width - linewidth) / 2); padding = Math.floor((width - linewidth) / 2);
} else if (justify === RIGHT_JUSTIFY) { } else if (justify === RIGHT_JUSTIFY) {
padding = Math.floor(width - (linewidth + padding)); padding = width - (linewidth + padding);
} }
linewidth += Math.max(0, padding); padding = Math.max(0, padding);
linewidth += padding;
if(linewidth > width) if(linewidth > width)
throw new Error(format("Rendered line width (%u) > screen width (%u)", linewidth, width)); throw new Error(format("Rendered line width (%u) > screen width (%u)", linewidth, width));
......
...@@ -23,6 +23,7 @@ function usage() { ...@@ -23,6 +23,7 @@ function usage() {
writeln(" -W Always word-wrap the output"); writeln(" -W Always word-wrap the output");
writeln(" -i Print font details"); writeln(" -i Print font details");
writeln(" -r Use random font and/or index (if not specified with -x)"); writeln(" -r Use random font and/or index (if not specified with -x)");
writeln(" -R Use random font and auto-retry upon exception");
writeln(" -h Print usage"); writeln(" -h Print usage");
writeln(""); writeln("");
exit(1); exit(1);
...@@ -70,6 +71,9 @@ for(i = 0; i < argv.length; ++i) { ...@@ -70,6 +71,9 @@ for(i = 0; i < argv.length; ++i) {
tdf.opt.info = true; tdf.opt.info = true;
} else if (arg === "-r") { } else if (arg === "-r") {
tdf.opt.random = true; tdf.opt.random = true;
} else if (arg === "-R") {
tdf.opt.random = true;
tdf.opt.retry = true;
} else if (arg === "-n") { } else if (arg === "-n") {
tdf.opt.blankline = false; tdf.opt.blankline = false;
} else if (arg === "-W") { } else if (arg === "-W") {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment