From ece10b253f7c4fe789966726d69a80147f41ee1a Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Wed, 12 Nov 2014 22:40:20 -0800 Subject: [PATCH] Clean up font.js --- font.js | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/font.js b/font.js index 462a01b8..08e2f79c 100644 --- a/font.js +++ b/font.js @@ -1,27 +1,21 @@ // Manage rendering text from this skin's text.bmp file Font = function() { + // Fill a node with a
containing character
s this.setNodeToString = function(node, string) { - stringElement = this.stringNode(string); + stringElement = this._stringNode(string); node.innerHTML = ''; node.appendChild(stringElement); } - this.stringNode = function(string) { - parentDiv = document.createElement('div'); - for (var i = 0, len = string.length; i < len; i++) { - char = string[i].toLowerCase(); - parentDiv.appendChild(this.characterNode(char)); - } - return parentDiv; - } - + // Get a
containing char this.characterNode = function(char) { return this.displayCharacterInNode(char, document.createElement('div')); } + // Style/populate a
to display a character this.displayCharacterInNode = function(character, node) { - position = this.charPosition(character); + position = this._getCharPosition(character); row = position[0]; column = position[1]; verticalOffset = row * 6; @@ -32,24 +26,32 @@ Font = function() { node.style.backgroundPosition = x + ' ' + y; node.classList.add('character'); - // Spaces cause a strange issue with inline-block elements - if(character == ' ') character = ' '; - node.innerHTML = character; return node; } - this.charPosition = function(char) { - position = this.fontLookup[char]; - if(!position) { - return this.fontLookup[' ']; + // Get a
containing character
s + this._stringNode = function(string) { + parentDiv = document.createElement('div'); + for (var i = 0, len = string.length; i < len; i++) { + char = string[i].toLowerCase(); + parentDiv.appendChild(this.characterNode(char)); } + return parentDiv; + } + + // Find the background offsets for a given character + this._getCharPosition = function(char) { + position = this._fontLookup[char]; + if(!position) { + return this._fontLookup[' ']; + } return position; } - /* XXX There are too many " " and "_" characters */ - this.fontLookup = { + /* TODO: There are too many " " and "_" characters */ + this._fontLookup = { "a": [0,0], "b": [0,1], "c": [0,2], "d": [0,3], "e": [0,4], "f": [0,5], "g": [0,6], "h": [0,7], "i": [0,8], "j": [0,9], "k": [0,10], "l": [0,11], "m": [0,12], "n": [0,13], "o": [0,14], "p": [0,15], @@ -64,5 +66,5 @@ Font = function() { "=": [1,28], "$": [1,29], "#": [1,30], "Å": [2,0], "Ö": [2,1], "Ä": [2,2], "?": [2,3], "*": [2,4], " ": [2,5], "<": [1,22], ">": [1,23], "{": [1,22], "}": [1,23] - }; + } }