UNPKG

starling-framework

Version:

A fast, productive library for 2D cross-platform development.

490 lines (474 loc) 18.9 kB
// Class: starling.text.BitmapFont var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this $global.Object.defineProperty(exports, "__esModule", {value: true}); var __map_reserved = {}; // Imports var $hxClasses = require("./../../hxClasses_stub").default; var $hxEnums = require("./../../hxEnums_stub").default; var $import = require("./../../import_stub").default; function starling_text_ITextCompositor() {return require("./../../starling/text/ITextCompositor");} function Xml() {return require("./../../Xml");} function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");} function openfl_errors_ArgumentError() {return $import(require("openfl/errors/ArgumentError"));} function Std() {return require("./../../Std");} function haxe_Log() {return require("./../../haxe/Log");} function openfl_geom_Rectangle() {return $import(require("openfl/geom/Rectangle"));} function starling_textures_Texture() {return require("./../../starling/textures/Texture");} function starling_text_BitmapChar() {return require("./../../starling/text/BitmapChar");} function openfl__$Vector_Vector_$Impl_$() {return require("./../../openfl/_Vector/Vector_Impl_");} function HxOverrides() {return require("./../../HxOverrides");} function starling_display_Sprite() {return require("./../../starling/display/Sprite");} function starling_text_BitmapCharLocation() {return require("./../../starling/text/BitmapCharLocation");} function starling_styles_DistanceFieldStyle() {return require("./../../starling/styles/DistanceFieldStyle");} function starling_utils_StringUtil() {return require("./../../starling/utils/StringUtil");} function starling_text_TextOptions() {return require("./../../starling/text/TextOptions");} function starling_text_MiniBitmapFont() {return require("./../../starling/text/MiniBitmapFont");} function haxe_ds_IntMap() {return require("./../../haxe/ds/IntMap");} function starling_display_Image() {return require("./../../starling/display/Image");} // Constructor var BitmapFont = function(texture,fontData) { if(texture == null && fontData == null) { texture = (starling_text_MiniBitmapFont().default).get_texture(); fontData = (starling_text_MiniBitmapFont().default).get_xml(); } else if(texture != null && fontData == null) { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Set both of the 'texture' and 'fontData' arguments to valid objects or leave both of them null.")); } this.__name = "unknown"; this.__lineHeight = this.__size = this.__baseline = 14; this.__offsetX = this.__offsetY = this.__padding = 0.0; this.__texture = texture; this.__chars = new (haxe_ds_IntMap().default)(); this.__helperImage = new (starling_display_Image().default)(texture); this.__type = "standard"; this.__distanceFieldSpread = 0.0; this.addChar(0,new (starling_text_BitmapChar().default)(0,null,0,0,0)); this.parseFontData(fontData); } // Meta BitmapFont.__name__ = "starling.text.BitmapFont"; BitmapFont.__isInterface__ = false; BitmapFont.__interfaces__ = [(starling_text_ITextCompositor().default)]; BitmapFont.prototype = { dispose: function() { if(this.__texture != null) { this.__texture.dispose(); } }, parseFontData: function(data) { try { var fontXml = null; if(typeof(data) == "string") { fontXml = (Xml().default).parse(data).firstElement(); } else if(((data) instanceof (Xml().default))) { fontXml = data; } this.parseFontXml(fontXml); } catch( error ) { var error1 = ((error) instanceof (js__$Boot_HaxeError().default)) ? error.val : error; throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("BitmapFont only supports XML data")); } }, parseFontXml: function(fontXml) { var scale = this.__texture.get_scale(); var frame = this.__texture.get_frame(); var frameX = frame != null ? frame.x : 0; var frameY = frame != null ? frame.y : 0; var info = null; var infoIterator = fontXml.elementsNamed("info"); if(infoIterator.hasNext()) { info = infoIterator.next(); } if(info == null) { fontXml = fontXml.firstElement(); infoIterator = fontXml.elementsNamed("info"); if(infoIterator.hasNext()) { info = infoIterator.next(); } } var common = null; var commonIterator = fontXml.elementsNamed("common"); if(commonIterator.hasNext()) { common = commonIterator.next(); } this.__name = info != null ? info.get("face") : ""; this.__size = info != null ? (Std().default).parseFloat(info.get("size")) / scale : NaN; this.__lineHeight = common != null ? (Std().default).parseFloat(common.get("lineHeight")) / scale : NaN; this.__baseline = common != null ? (Std().default).parseFloat(common.get("base")) / scale : NaN; if(info != null && info.get("smooth") == "0") { this.set_smoothing("none"); } if(this.__size <= 0) { (haxe_Log().default).trace("[Starling] Warning: invalid font size in '" + this.__name + "' font.",{ fileName : "../src/starling/text/BitmapFont.hx", lineNumber : 211, className : "starling.text.BitmapFont", methodName : "parseFontXml"}); this.__size = this.__size == 0.0 ? 16.0 : this.__size * -1.0; } var distanceField = null; var distanceFieldIterator = fontXml.elementsNamed("distanceField"); if(distanceFieldIterator.hasNext()) { distanceField = distanceFieldIterator.next(); } if(distanceField != null && distanceField.exists("distanceRange") && distanceField.exists("fieldType")) { this.__distanceFieldSpread = (Std().default).parseFloat(distanceField.get("distanceRange")); this.__type = distanceField.get("fieldType") == "msdf" ? "multiChannelDistanceField" : "distanceField"; } else { this.__distanceFieldSpread = 0.0; this.__type = "standard"; } var chars = null; var charsIterator = fontXml.elementsNamed("chars"); if(charsIterator.hasNext()) { chars = charsIterator.next(); } if(chars != null) { var charElement = chars.elementsNamed("char"); while(charElement.hasNext()) { var charElement1 = charElement.next(); var id = (Std().default).parseInt(charElement1.get("id")); var xOffset = (Std().default).parseFloat(charElement1.get("xoffset")) / scale; var yOffset = (Std().default).parseFloat(charElement1.get("yoffset")) / scale; var xAdvance = (Std().default).parseFloat(charElement1.get("xadvance")) / scale; var region = new (openfl_geom_Rectangle().default)(); region.x = (Std().default).parseFloat(charElement1.get("x")) / scale + frameX; region.y = (Std().default).parseFloat(charElement1.get("y")) / scale + frameY; region.width = (Std().default).parseFloat(charElement1.get("width")) / scale; region.height = (Std().default).parseFloat(charElement1.get("height")) / scale; var texture = (starling_textures_Texture().default).fromTexture(this.__texture,region); var bitmapChar = new (starling_text_BitmapChar().default)(id,texture,xOffset,yOffset,xAdvance); this.addChar(id,bitmapChar); } } var kernings = null; var kerningsIterator = fontXml.elementsNamed("kernings"); if(kerningsIterator.hasNext()) { kernings = kerningsIterator.next(); } if(kernings != null) { var kerningElement = kernings.elementsNamed("kerning"); while(kerningElement.hasNext()) { var kerningElement1 = kerningElement.next(); var first = (Std().default).parseInt(kerningElement1.get("first")); var second = (Std().default).parseInt(kerningElement1.get("second")); var amount = (Std().default).parseFloat(kerningElement1.get("amount")) / scale; if(this.__chars.exists(second)) { this.getChar(second).addKerning(first,amount); } } } }, getChar: function(charID) { return this.__chars.get(charID); }, addChar: function(charID,bitmapChar) { this.__chars.set(charID,bitmapChar); }, getCharIDs: function(result) { if(result == null) { result = (openfl__$Vector_Vector_$Impl_$().default)._new(); } var key = this.__chars.keys(); while(key.hasNext()) { var key1 = key.next(); (openfl__$Vector_Vector_$Impl_$().default).set(result,result.length,key1); } return result; }, hasChars: function(text) { if(text == null) { return true; } var charID; var numChars = text.length; var _g = 0; var _g1 = numChars; while(_g < _g1) { var i = _g++; charID = (HxOverrides().default).cca(text,i); if(charID != 32 && charID != 9 && charID != 10 && charID != 13 && this.getChar(charID) == null) { return false; } } return true; }, createSprite: function(width,height,text,format,options) { var charLocations = this.arrangeChars(width,height,text,format,options); var numChars = charLocations.length; var sprite = new (starling_display_Sprite().default)(); var _g = 0; var _g1 = numChars; while(_g < _g1) { var i = _g++; var charLocation = charLocations[i]; var char = charLocation.char.createImage(); char.set_x(charLocation.x); char.set_y(charLocation.y); char.set_scale(charLocation.scale); char.set_color(format.get_color()); char.set_textureSmoothing(this.get_smoothing()); sprite.addChild(char); } (starling_text_BitmapCharLocation().default).rechargePool(); return sprite; }, fillMeshBatch: function(meshBatch,width,height,text,format,options) { var charLocations = this.arrangeChars(width,height,text,format,options); var numChars = charLocations.length; this.__helperImage.set_color(format.get_color()); var _g = 0; var _g1 = numChars; while(_g < _g1) { var i = _g++; var charLocation = charLocations[i]; this.__helperImage.set_texture(charLocation.char.get_texture()); this.__helperImage.readjustSize(); this.__helperImage.set_x(charLocation.x); this.__helperImage.set_y(charLocation.y); this.__helperImage.set_scale(charLocation.scale); meshBatch.addMeshAt(this.__helperImage); } (starling_text_BitmapCharLocation().default).rechargePool(); }, clearMeshBatch: function(meshBatch) { meshBatch.clear(); }, getDefaultMeshStyle: function(previousStyle,format,options) { if(this.__type == "standard") { return null; } else { var fontSize = format.get_size() < 0 ? format.get_size() * -this.__size : format.get_size(); var dfStyle = ((previousStyle) instanceof (starling_styles_DistanceFieldStyle().default)) ? previousStyle : new (starling_styles_DistanceFieldStyle().default)(); dfStyle.set_multiChannel(this.__type == "multiChannelDistanceField"); dfStyle.set_softness(this.__size / (fontSize * this.__distanceFieldSpread)); return dfStyle; } }, arrangeChars: function(width,height,text,format,options) { if(text == null || text.length == 0) { return (starling_text_BitmapCharLocation().default).vectorFromPool(); } if(options == null) { options = BitmapFont.sDefaultOptions; } var kerning = format.get_kerning(); var leading = format.get_leading(); var spacing = format.get_letterSpacing(); var hAlign = format.get_horizontalAlign(); var vAlign = format.get_verticalAlign(); var fontSize = format.get_size(); var autoScale = options.get_autoScale(); var wordWrap = options.get_wordWrap(); var finished = false; var charLocation; var numChars; var containerWidth = 0; var containerHeight = 0; var scale = 0; var i; var j; if(fontSize < 0) { fontSize *= -this.__size; } var currentY = 0; while(!finished) { BitmapFont.sLines.splice(0,BitmapFont.sLines.length); scale = fontSize / this.__size; containerWidth = (width - 2 * this.__padding) / scale; containerHeight = (height - 2 * this.__padding) / scale; if(this.__size <= containerHeight) { var lastWhiteSpace = -1; var lastCharID = -1; var currentLine = (starling_text_BitmapCharLocation().default).vectorFromPool(); var currentX = 0; currentY = 0; numChars = text.length; var i1 = 0; while(i1 < numChars) { var lineFull = false; var charID = (HxOverrides().default).cca(text,i1); var char = this.getChar(charID); if(charID == 10 || charID == 13) { lineFull = true; } else { if(char == null) { (haxe_Log().default).trace((starling_utils_StringUtil().default).format("[Starling] Character '{0}' (id: {1}) not found in '{2}'",[text.charAt(i1),charID,this.get_name()]),{ fileName : "../src/starling/text/BitmapFont.hx", lineNumber : 460, className : "starling.text.BitmapFont", methodName : "arrangeChars"}); charID = 0; char = this.getChar(0); } if(charID == 32 || charID == 9) { lastWhiteSpace = i1; } if(kerning) { currentX += char.getKerning(lastCharID); } charLocation = (starling_text_BitmapCharLocation().default).instanceFromPool(char); charLocation.index = i1; charLocation.x = currentX + char.get_xOffset(); charLocation.y = currentY + char.get_yOffset(); (openfl__$Vector_Vector_$Impl_$().default).set(currentLine,currentLine.length,charLocation); currentX += char.get_xAdvance() + spacing; lastCharID = charID; if(charLocation.x + char.get_width() > containerWidth) { if(wordWrap) { if(autoScale && lastWhiteSpace == -1) { break; } var numCharsToRemove = lastWhiteSpace == -1 ? 1 : i1 - lastWhiteSpace; var _g = 0; var _g1 = numCharsToRemove; while(_g < _g1) { var j1 = _g++; (openfl__$Vector_Vector_$Impl_$().default).pop(currentLine); } if(currentLine.length == 0) { break; } i1 -= numCharsToRemove; } else { if(autoScale) { break; } (openfl__$Vector_Vector_$Impl_$().default).pop(currentLine); while(i1 < numChars - 1 && (HxOverrides().default).cca(text,i1) != 10) ++i1; } lineFull = true; } } if(i1 == numChars - 1) { BitmapFont.sLines[BitmapFont.sLines.length] = currentLine; finished = true; } else if(lineFull) { BitmapFont.sLines[BitmapFont.sLines.length] = currentLine; if(lastWhiteSpace == i1) { (openfl__$Vector_Vector_$Impl_$().default).pop(currentLine); } if(currentY + this.__lineHeight + leading + this.__size <= containerHeight) { currentLine = (starling_text_BitmapCharLocation().default).vectorFromPool(); currentX = 0; currentY += this.__lineHeight + leading; lastWhiteSpace = -1; lastCharID = -1; } else { break; } } ++i1; } } if(autoScale && !finished && fontSize > 3) { --fontSize; } else { finished = true; } } var finalLocations = (starling_text_BitmapCharLocation().default).vectorFromPool(); var numLines = BitmapFont.sLines.length; var bottom = currentY + this.__lineHeight; var yOffset = 0; if(vAlign == "bottom") { yOffset = (Std().default).int(containerHeight - bottom); } else if(vAlign == "center") { yOffset = (Std().default).int((containerHeight - bottom) / 2); } var _g2 = 0; var _g11 = numLines; while(_g2 < _g11) { var lineID = _g2++; var line = BitmapFont.sLines[lineID]; numChars = line.length; if(numChars == 0) { continue; } var xOffset = 0; var lastLocation = line[line.length - 1]; var right = lastLocation.x - lastLocation.char.get_xOffset() + lastLocation.char.get_xAdvance(); if(hAlign == "right") { xOffset = (Std().default).int(containerWidth - right); } else if(hAlign == "center") { xOffset = (Std().default).int((containerWidth - right) / 2); } var _g3 = 0; var _g12 = numChars; while(_g3 < _g12) { var c = _g3++; charLocation = line[c]; charLocation.x = scale * (charLocation.x + xOffset + this.__padding); charLocation.y = scale * (charLocation.y + yOffset + this.__padding); charLocation.scale = scale; if(charLocation.char.get_width() > 0 && charLocation.char.get_height() > 0) { (openfl__$Vector_Vector_$Impl_$().default).set(finalLocations,finalLocations.length,charLocation); } } } return finalLocations; }, get_name: function() { return this.__name; }, get_size: function() { return this.__size; }, get_type: function() { return this.__type; }, set_type: function(value) { return this.__type = value; }, get_distanceFieldSpread: function() { return this.__distanceFieldSpread; }, set_distanceFieldSpread: function(value) { return this.__distanceFieldSpread = value; }, get_lineHeight: function() { return this.__lineHeight; }, set_lineHeight: function(value) { this.__lineHeight = value; }, get_smoothing: function() { return this.__helperImage.get_textureSmoothing(); }, set_smoothing: function(value) { return this.__helperImage.set_textureSmoothing(value); }, get_baseline: function() { return this.__baseline; }, set_baseline: function(value) { return this.__baseline = value; }, get_offsetX: function() { return this.__offsetX; }, set_offsetX: function(value) { return this.__offsetX = value; }, get_offsetY: function() { return this.__offsetY; }, set_offsetY: function(value) { return this.__offsetY = value; }, get_padding: function() { return this.__padding; }, set_padding: function(value) { return this.__padding = value; }, get_texture: function() { return this.__texture; } }; BitmapFont.prototype.__class__ = BitmapFont.prototype.constructor = $hxClasses["starling.text.BitmapFont"] = BitmapFont; // Init Object.defineProperties(BitmapFont.prototype,{ name : { get : function () { return this.get_name (); }}, size : { get : function () { return this.get_size (); }}, lineHeight : { get : function () { return this.get_lineHeight (); }}, smoothing : { get : function () { return this.get_smoothing (); }, set : function (v) { return this.set_smoothing (v); }}, baseline : { get : function () { return this.get_baseline (); }, set : function (v) { return this.set_baseline (v); }}, offsetX : { get : function () { return this.get_offsetX (); }, set : function (v) { return this.set_offsetX (v); }}, offsetY : { get : function () { return this.get_offsetY (); }, set : function (v) { return this.set_offsetY (v); }}, padding : { get : function () { return this.get_padding (); }, set : function (v) { return this.set_padding (v); }}, type : { get : function () { return this.get_type (); }, set : function (v) { return this.set_type (v); }}, distanceFieldSpread : { get : function () { return this.get_distanceFieldSpread (); }, set : function (v) { return this.set_distanceFieldSpread (v); }}}); // Statics BitmapFont.NATIVE_SIZE = -1 BitmapFont.MINI = "mini" BitmapFont.CHAR_MISSING = 0 BitmapFont.CHAR_TAB = 9 BitmapFont.CHAR_NEWLINE = 10 BitmapFont.CHAR_CARRIAGE_RETURN = 13 BitmapFont.CHAR_SPACE = 32 BitmapFont.sLines = [] BitmapFont.sDefaultOptions = new (starling_text_TextOptions().default)() // Export exports.default = BitmapFont;