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

Add output() method that just outputs the font/string, doesn't print

for Nelgin
parent 97a64bcc
No related branches found
No related tags found
No related merge requests found
...@@ -311,6 +311,7 @@ function lookupchar(c, font) { ...@@ -311,6 +311,7 @@ function lookupchar(c, font) {
function printcolor(color) { function printcolor(color) {
var fg = color & 0x0f; var fg = color & 0x0f;
var bg = (color & 0xf0) >> 4; var bg = (color & 0xf0) >> 4;
var out = "";
// TheDraw colors mapped to ANSI and MIRC // TheDraw colors mapped to ANSI and MIRC
var fgacolors = [30, 34, 32, 36, 31, 35, 33, 37, 90, 94, 92, 96, 91, 95, 93, 97]; // Normal/Bright var fgacolors = [30, 34, 32, 36, 31, 35, 33, 37, 90, 94, 92, 96, 91, 95, 93, 97]; // Normal/Bright
...@@ -319,21 +320,22 @@ function printcolor(color) { ...@@ -319,21 +320,22 @@ function printcolor(color) {
var bgmcolors = [1, 2, 3, 10, 5, 6, 7, 15, 14, 12, 9, 11, 4, 13, 8, 0]; // MIRC backgrounds var bgmcolors = [1, 2, 3, 10, 5, 6, 7, 15, 14, 12, 9, 11, 4, 13, 8, 0]; // MIRC backgrounds
if (opt.color === COLOR_ANSI) { if (opt.color === COLOR_ANSI) {
// Use write for printing parts of the ANSI escape code out += "\x1b[";
write("\x1b["); out += (fgacolors[fg] + ";");
write(fgacolors[fg] + ";"); out += (bgacolors[bg] + "m");
write(bgacolors[bg] + "m");
} else { // MIRC color } else { // MIRC color
write("\x03"); out += "\x03";
write(fgmcolors[fg] + ","); out += (fgmcolors[fg] + ",");
write(bgmcolors[bg]); out += (bgmcolors[bg]);
} }
return out;
} }
function printrow(glyph, row) { function printrow(glyph, row) {
var utfchar; var utfchar;
var color; var color;
var lastcolor = -1; // Use -1 or similar to ensure color is printed for the first cell var lastcolor = -1; // Use -1 or similar to ensure color is printed for the first cell
var out = "";
for (var i = 0; i < glyph.width; i++) { for (var i = 0; i < glyph.width; i++) {
var cell_idx = glyph.width * row + i; var cell_idx = glyph.width * row + i;
...@@ -342,31 +344,33 @@ function printrow(glyph, row) { ...@@ -342,31 +344,33 @@ function printrow(glyph, row) {
color = glyph.cell[cell_idx].color; color = glyph.cell[cell_idx].color;
if (i === 0 || color !== lastcolor) { if (i === 0 || color !== lastcolor) {
printcolor(color); out += printcolor(color);
lastcolor = color; lastcolor = color;
} }
write(utfchar); // Use write to print character without newline out += utfchar;
} else { } else {
// Should not happen if glyph.cell is initialized correctly, but for safety // Should not happen if glyph.cell is initialized correctly, but for safety
write(" "); out += " ";
} }
} }
// Reset color at the end of the row // Reset color at the end of the row
if (opt.color === COLOR_ANSI) { if (opt.color === COLOR_ANSI) {
write("\x1b[0m"); out += "\x1b[0m";
} else { } else {
write("\x03"); out += "\x03";
} }
return out;
} }
function printstr(str, font) { function output(str, font) {
var maxheight = font.height; // Use the pre-calculated max height from loadfont var maxheight = font.height; // Use the pre-calculated max height from loadfont
var linewidth = 0; var linewidth = 0;
var len = str.length; var len = str.length;
var padding = 0; var padding = 0;
var n = 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 < len; i++) {
...@@ -400,7 +404,7 @@ function printstr(str, font) { ...@@ -400,7 +404,7 @@ function printstr(str, font) {
for (var i = 0; i < maxheight; i++) { for (var i = 0; i < maxheight; i++) {
// Print padding spaces // Print padding spaces
for (var p = 0; p < padding; p++) { for (var p = 0; p < padding; p++) {
write(" "); out += " ";
} }
// Print glyphs for each character in the string // Print glyphs for each character in the string
...@@ -411,7 +415,7 @@ function printstr(str, font) { ...@@ -411,7 +415,7 @@ function printstr(str, font) {
// If character not found, print spaces equivalent to default glyph width or 1? // If character not found, print spaces equivalent to default glyph width or 1?
// Let's print spaces equal to the font's spacing + a minimal width (e.g., 1) // Let's print spaces equal to the font's spacing + a minimal width (e.g., 1)
for(var s = 0; s < font.spacing + 1; s++) { for(var s = 0; s < font.spacing + 1; s++) {
write(" "); out += " ";
} }
continue; continue;
} }
...@@ -419,23 +423,29 @@ function printstr(str, font) { ...@@ -419,23 +423,29 @@ function printstr(str, font) {
var g = font.glyphs[char_index]; var g = font.glyphs[char_index];
// printrow handles printing the characters and colors for a single row of the glyph // printrow handles printing the characters and colors for a single row of the glyph
printrow(g, i); out += printrow(g, i);
// Print spacing between glyphs (except after the last glyph) // Print spacing between glyphs (except after the last glyph)
if (c < len - 1) { if (c < len - 1) {
for (var s = 0; s < font.spacing; s++) { for (var s = 0; s < font.spacing; s++) {
write(" "); out += " ";
} }
} }
} }
// End the line and reset color // End the line and reset color
if (opt.color === COLOR_ANSI) { if (opt.color === COLOR_ANSI) {
writeln("\x1b[0m"); // Reset color and print newline out += "\x1b[0m"; // Reset color and print newline
} else { } else {
writeln("\x03"); // Reset MIRC color and print newline out += "\x03"; // Reset MIRC color and print newline
}
out += "\n";
} }
return out;
} }
function printstr(str, font) {
write(output(str, font));
} }
this; this;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment