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

Add word-wrap support

parent 7e00f2c8
Branches
Tags
No related merge requests found
...@@ -353,18 +353,30 @@ function output(str, font) { ...@@ -353,18 +353,30 @@ function output(str, font) {
font = filename.replace(/\.tdf$/i, ""); font = filename.replace(/\.tdf$/i, "");
} }
} }
if (typeof font == "string") if (typeof font == "string")
font = loadfont(font); font = loadfont(font);
var width = getwidth(str, font);
if (width > screen_width() || (this.opt && opt.wrap)) { // Word-wrap
var array = str.split(/\s+/);
if (array.length > 1) {
var out = "";
while (str = array.shift()) {
out += output_line(str, font);
if (array.length && (!this.opt || opt.blankline !== false))
out += "\r\n";
}
return out;
}
}
return output_line(str, font);
}
var maxheight = font.height; // Use the pre-calculated max height from loadfont // Calculate the total width of the string using the font
var linewidth = 0; function getwidth(str, font) {
var len = str.length; var linewidth = 0;
var n = 0;
var out = "";
// Calculate the total width of the string using the font // Calculate the total width of the string using the font
for (var i = 0; i < len; i++) { for (var i = 0; i < str.length; i++) {
var char_index = lookupchar(str[i], font); var char_index = lookupchar(str[i], font);
if (char_index === -1) { if (char_index === -1) {
...@@ -374,11 +386,14 @@ function output(str, font) { ...@@ -374,11 +386,14 @@ function output(str, font) {
var g = font.glyphs[char_index]; var g = font.glyphs[char_index];
linewidth += g.width; linewidth += g.width;
if (i < len - 1) { // Add spacing between characters, but not after the last one if (i < str.length - 1) { // Add spacing between characters, but not after the last one
linewidth += font.spacing; linewidth += font.spacing;
} }
} }
return linewidth;
}
function screen_width() {
var width = this.opt && opt.width; var width = this.opt && opt.width;
if (!width) { if (!width) {
if (js.global.console) // Auto-detect screen width, when possible if (js.global.console) // Auto-detect screen width, when possible
...@@ -386,6 +401,17 @@ function output(str, font) { ...@@ -386,6 +401,17 @@ function output(str, font) {
else else
width = DEFAULT_WIDTH; width = DEFAULT_WIDTH;
} }
return width;
}
function output_line(str, font) {
var maxheight = font.height; // Use the pre-calculated max height from loadfont
var linewidth = getwidth(str, font);
var len = str.length;
var n = 0;
var out = "";
var width = screen_width();
var margin = this.opt && opt.margin ? opt.margin : 0; var margin = this.opt && opt.margin ? opt.margin : 0;
var padding = margin; var padding = margin;
......
...@@ -19,6 +19,8 @@ function usage() { ...@@ -19,6 +19,8 @@ function usage() {
writeln(" -a Enable ANSI sequences (default: Synchronet Ctrl-A)"); writeln(" -a Enable ANSI sequences (default: Synchronet Ctrl-A)");
writeln(" -u Encode characters as UTF-8 (default: CP437)"); writeln(" -u Encode characters as UTF-8 (default: CP437)");
writeln(" -x n Index to font within file (default: 0)"); writeln(" -x n Index to font within file (default: 0)");
writeln(" -n No blank line between wrapped output lines");
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(" -h Print usage"); writeln(" -h Print usage");
...@@ -68,6 +70,10 @@ for(i = 0; i < argv.length; ++i) { ...@@ -68,6 +70,10 @@ 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 === "-n") {
tdf.opt.blankline = false;
} else if (arg === "-W") {
tdf.opt.wrap = true;
} else if (arg === "-h") { } else if (arg === "-h") {
usage(); usage();
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment