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

If no font object or filename is passed to lib, pick a font file at random

This moves the random file selection into tdfonts_lib.js

We should also pick a font index at random (when opt.random is true), but
that's not happening yet.

Also fixed up some indentation issues (when using 4 space tabstops). The
original commit of this file used spaces for indentation, so it's kind of
a mess now and should be uncrustified.
parent a1d0028c
No related branches found
No related tags found
No related merge requests found
...@@ -82,23 +82,23 @@ function loadfont(fn_arg) { ...@@ -82,23 +82,23 @@ function loadfont(fn_arg) {
try { try {
const sequence = "\x55\xaa\x00\xff"; const sequence = "\x55\xaa\x00\xff";
if(this.opt && opt.random) if(this.opt && opt.random)
opt.index = (map.match(new RegExp(sequence, 'g'))-1); opt.index = (map.match(new RegExp(sequence, 'g'))-1);
var index = -1; var index = -1;
var n = 0; var n = 0;
if(this.opt && opt.index>0) { if(this.opt && opt.index>0) {
while (n < opt.index) { while (n < opt.index) {
index = map.indexOf(sequence, index + 1); index = map.indexOf(sequence, index + 1);
if (index === -1) if (index === -1)
break; break;
n++; n++;
} }
if (index !== -1) if (index !== -1)
map = map.slice(0, 20) + map.slice(index); map = map.slice(0, 20) + map.slice(index);
} }
font.namelen = map.charCodeAt(24); font.namelen = map.charCodeAt(24);
font.name = map.substring(25, 25 + font.namelen); font.name = map.substring(25, 25 + font.namelen);
font.fonttype = map.charCodeAt(41); font.fonttype = map.charCodeAt(41);
...@@ -342,6 +342,16 @@ function reset_color() ...@@ -342,6 +342,16 @@ function reset_color()
} }
function output(str, font) { function output(str, font) {
if (!font) { // Random font file selection
var fontDir = FONT_DIR;
var files = directory(fontDir + "/*.tdf"); // Get all .tdf files
if (files.length > 0) {
var randomIndex = random((files.length)+1);
var filename = file_getname(files[randomIndex]);
font = filename.replace(/\.tdf$/i, "");
}
}
if (typeof font == "string") if (typeof font == "string")
font = loadfont(font); font = loadfont(font);
...@@ -369,7 +379,7 @@ function output(str, font) { ...@@ -369,7 +379,7 @@ function output(str, font) {
} }
// Calculate padding for justification // Calculate padding for justification
if (this.top && opt.justify === CENTER_JUSTIFY) { if (this.opt && opt.justify === CENTER_JUSTIFY) {
padding = Math.floor((opt.width - linewidth) / 2); padding = Math.floor((opt.width - linewidth) / 2);
} else if (this.opt && opt.justify === RIGHT_JUSTIFY) { } else if (this.opt && opt.justify === RIGHT_JUSTIFY) {
padding = opt.width - linewidth; padding = opt.width - linewidth;
......
...@@ -48,65 +48,55 @@ for (var i = 0; i < argc; i++) ...@@ -48,65 +48,55 @@ for (var i = 0; i < argc; i++)
var i = 0; var i = 0;
var input_string = ""; var input_string = "";
while (i < args.length) { while (i < args.length) {
var arg = args[i]; var arg = args[i];
if (arg === "-f" && i + 1 < args.length) { if (arg === "-f" && i + 1 < args.length) {
fontfile = args[i + 1]; fontfile = args[i + 1];
i += 2; i += 2;
} else if (arg === "-j" && i + 1 < args.length) { } else if (arg === "-j" && i + 1 < args.length) {
switch (args[i + 1]) { switch (args[i + 1]) {
case "l": case "l":
opt.justify = LEFT_JUSTIFY; opt.justify = LEFT_JUSTIFY;
break; break;
case "r": case "r":
opt.justify = RIGHT_JUSTIFY; opt.justify = RIGHT_JUSTIFY;
break; break;
case "c": case "c":
opt.justify = CENTER_JUSTIFY; opt.justify = CENTER_JUSTIFY;
break; break;
default: default:
log("Invalid justification option. Use l, r, or c."); log("Invalid justification option. Use l, r, or c.");
exit(1); exit(1);
} }
i += 2; i += 2;
} else if (arg === "-w" && i + 1 < args.length) { } else if (arg === "-w" && i + 1 < args.length) {
opt.width = parseInt(args[i + 1], 10); opt.width = parseInt(args[i + 1], 10);
i += 2; i += 2;
} else if (arg === "-x" && i + 1 < args.length) { } else if (arg === "-x" && i + 1 < args.length) {
opt.index = parseInt(args[i + 1], 10); opt.index = parseInt(args[i + 1], 10);
i += 2; i += 2;
} else if (arg === "-a") { } else if (arg === "-a") {
opt.ansi = true; opt.ansi = true;
++i; ++i;
} else if (arg === "-u") { } else if (arg === "-u") {
opt.utf8 = true; opt.utf8 = true;
++i; ++i;
} else if (arg === "-i") { } else if (arg === "-i") {
opt.info = true; opt.info = true;
i += 1; i += 1;
} else if (arg === "-r") { } else if (arg === "-r") {
opt.random = true; opt.random = true;
i += 1; i += 1;
} else if (arg === "-h") { } else if (arg === "-h") {
usage(); usage();
} else { } else {
input_string += (input_string ? " " : "") + arg; input_string += (input_string ? " " : "") + arg;
i += 1; i += 1;
}
}
// Handle random font selection
if (!fontfile && opt.random) {
var fontDir = FONT_DIR;
var files = directory(fontDir + "/*.tdf"); // Get all .tdf files
if (files.length > 0) {
var randomIndex = random((files.length)+1);
var filename = file_getname(files[randomIndex]);
fontfile = filename.replace(/\.tdf$/i, "");
} }
} }
if (!fontfile)
if (!fontfile && !opt.random)
usage(); usage();
writeln(""); writeln("");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment