Phaser.BitmapFont = function (game, key, characterWidth, characterHeight, chars, charsPerRow, xSpacing, ySpacing, xOffset, yOffset){ this.characterWidth = characterWidth; this.characterHeight = characterHeight; this.characterSpacingX = xSpacing || 0; this.characterSpacingY = ySpacing || 0; this.characterPerRow = charsPerRow; this.offsetX = xOffset || 0; this.offsetY = yOffset || 0; this.align = "left"; this.multiLine = false ; this.autoUpperCase = true ; this.customSpacingX = 0; this.customSpacingY = 0; this.fixedWidth = 0; this.fontSet = game.cache.getImage(key); this._text = ''; this.grabData = [] ; var currentX = this.offsetX; var currentY = this.offsetY; var r = 0; var data = new Phaser.FrameData(); for (var c = 0; c < _AN_Read_length('length', chars); c++ ){ var uuid = game.rnd.uuid(); var frame = data.addFrame(new Phaser.Frame(c, currentX, currentY, this.characterWidth, this.characterHeight, '', uuid)); this.grabData[chars.charCodeAt(c)] = frame.index; PIXI.TextureCache[uuid] = new PIXI.Texture(PIXI.BaseTextureCache[key], { x: currentX, y: currentY, width: this.characterWidth, height: this.characterHeight} ); r++ ; if (r == this.characterPerRow) { r = 0; currentX = this.offsetX; currentY += this.characterHeight + this.characterSpacingY; } else { currentX += this.characterWidth + this.characterSpacingX; } } game.cache.updateFrameData(key, data); this.stamp = new Phaser.Image(game, 0, 0, key, 0); Phaser.RenderTexture.call(this, game); this.type = Phaser.BITMAPFONT; } ; Phaser.BitmapFont.prototype = Object.create(Phaser.RenderTexture.prototype); Phaser.BitmapFont.prototype.constructor = Phaser.BitmapFont; Phaser.BitmapFont.ALIGN_LEFT = "left"; Phaser.BitmapFont.ALIGN_RIGHT = "right"; Phaser.BitmapFont.ALIGN_CENTER = "center"; Phaser.BitmapFont.TEXT_SET1 = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; Phaser.BitmapFont.TEXT_SET2 = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Phaser.BitmapFont.TEXT_SET3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "; Phaser.BitmapFont.TEXT_SET4 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789"; Phaser.BitmapFont.TEXT_SET5 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,/() '!?-*:0123456789"; Phaser.BitmapFont.TEXT_SET6 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!?:;0123456789\"(),-.' "; Phaser.BitmapFont.TEXT_SET7 = "AGMSY+:4BHNTZ!;5CIOU.?06DJPV,(17EKQW\")28FLRX-'39"; Phaser.BitmapFont.TEXT_SET8 = "0123456789 .ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Phaser.BitmapFont.TEXT_SET9 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ()-0123456789.:,'\"?!"; Phaser.BitmapFont.TEXT_SET10 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Phaser.BitmapFont.TEXT_SET11 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,\"-+!?()':;0123456789"; Phaser.BitmapFont.prototype.setFixedWidth = function (width, lineAlignment){ if (typeof lineAlignment === 'undefined') { lineAlignment = 'left'; } this.fixedWidth = width; this.align = lineAlignment; } ; Phaser.BitmapFont.prototype.setText = function (content, multiLine, characterSpacing, lineSpacing, lineAlignment, allowLowerCase){ this.multiLine = multiLine || false ; this.customSpacingX = characterSpacing || 0; this.customSpacingY = lineSpacing || 0; this.align = lineAlignment || 'left'; if (allowLowerCase) { this.autoUpperCase = false ; } else { this.autoUpperCase = true ; } if (_AN_Read_length('length', content) > 0) { _AN_Write_text('text', this, false , content); } } ; Phaser.BitmapFont.prototype.resize = function (width, height){ this.width = width; this.height = height; this.frame.width = this.width; this.frame.height = this.height; this.baseTexture.width = this.width; this.baseTexture.height = this.height; if (this.renderer.type === PIXI.WEBGL_RENDERER) { this.projection.x = this.width / 2; this.projection.y = - this.height / 2; var gl = this.renderer.gl; gl.bindTexture(gl.TEXTURE_2D, this.baseTexture._glTextures[gl.id]); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null ); } else { this.textureBuffer.resize(this.width, this.height); } PIXI.Texture.frameUpdates.push(this); } ; Phaser.BitmapFont.prototype.buildBitmapFontText = function (){ var cx = 0; var cy = 0; if (this.multiLine) { var lines = this._text.split("\n"); if (this.fixedWidth > 0) { this.resize(fixedWidth, (_AN_Read_length("length", lines) * (this.characterHeight + this.customSpacingY)) - this.customSpacingY); } else { this.resize(this.getLongestLine() * (this.characterWidth + this.customSpacingX), (_AN_Read_length("length", lines) * (this.characterHeight + this.customSpacingY)) - this.customSpacingY); } _AN_Call_clear("clear", this.textureBuffer); for (var i = 0; i < _AN_Read_length("length", lines); i++ ){ switch (this.align){ case Phaser.BitmapFont.ALIGN_LEFT: cx = 0; break ; case Phaser.BitmapFont.ALIGN_RIGHT: cx = this.width - (_AN_Read_length("length", lines[i]) * (this.characterWidth + this.customSpacingX)); break ; case Phaser.BitmapFont.ALIGN_CENTER: cx = (this.width / 2) - ((_AN_Read_length("length", lines[i]) * (this.characterWidth + this.customSpacingX)) / 2); cx += this.customSpacingX / 2; break ; } if (cx < 0) { cx = 0; } this.pasteLine(lines[i], cx, cy, this.customSpacingX); cy += this.characterHeight + this.customSpacingY; } } else { if (this.fixedWidth > 0) { this.resize(fixedWidth, this.characterHeight); } else { this.resize(_AN_Read_length("length", this._text) * (this.characterWidth + this.customSpacingX), this.characterHeight); } _AN_Call_clear("clear", this.textureBuffer); switch (this.align){ case Phaser.BitmapFont.ALIGN_LEFT: cx = 0; break ; case Phaser.BitmapFont.ALIGN_RIGHT: cx = this.width - (_AN_Read_length("length", this._text) * (this.characterWidth + this.customSpacingX)); break ; case Phaser.BitmapFont.ALIGN_CENTER: cx = (this.width / 2) - ((_AN_Read_length("length", this._text) * (this.characterWidth + this.customSpacingX)) / 2); cx += this.customSpacingX / 2; break ; } this.pasteLine(this._text, cx, 0, this.customSpacingX); } } ; Phaser.BitmapFont.prototype.pasteLine = function (line, x, y, customSpacingX){ var p = new Phaser.Point(); for (var c = 0; c < _AN_Read_length("length", line); c++ ){ if (line.charAt(c) == " ") { x += this.characterWidth + this.customSpacingX; } else { if (this.grabData[line.charCodeAt(c)] >= 0) { this.stamp.frame = this.grabData[line.charCodeAt(c)]; p.set(x, y); this.render(this.stamp, p, false ); x += this.characterWidth + this.customSpacingX; if (x > this.width) { break ; } } } } } ; Phaser.BitmapFont.prototype.getLongestLine = function (){ var longestLine = 0; if (_AN_Read_length("length", this._text) > 0) { var lines = this._text.split("\n"); for (var i = 0; i < _AN_Read_length("length", lines); i++ ){ if (_AN_Read_length("length", lines[i]) > longestLine) { longestLine = _AN_Read_length("length", lines[i]); } } } return longestLine; } ; Phaser.BitmapFont.prototype.removeUnsupportedCharacters = function (stripCR){ var newString = ""; for (var c = 0; c < _AN_Read_length("length", this._text); c++ ){ var char = this._text[c]; var code = char.charCodeAt(0); if (this.grabData[code] >= 0 || (!stripCR && char === "\n")) { newString = newString.concat(char); } } return newString; } ; Object.defineProperty(Phaser.BitmapFont.prototype, "text", { get: function (){ return this._text; } , set: function (value){ var newText; if (this.autoUpperCase) { newText = value.toUpperCase(); } else { newText = value; } if (newText !== this._text) { this._text = newText; this.removeUnsupportedCharacters(this.multiLine); this.buildBitmapFontText(); } } } );