PIXI.BitmapText = function (text, style){ PIXI.DisplayObjectContainer.call(this); this._pool = [] ; this.setText(text); this.setStyle(style); this.updateText(); this.dirty = false ; } ; PIXI.BitmapText.prototype = Object.create(PIXI.DisplayObjectContainer.prototype); PIXI.BitmapText.prototype.constructor = PIXI.BitmapText; PIXI.BitmapText.prototype.setText = function (text){ _AN_Write_text('text', this, false , text || ' '); this.dirty = true ; } ; PIXI.BitmapText.prototype.setStyle = function (style){ style = style || { } ; style.align = style.align || 'left'; this.style = style; var font = style.font.split(' '); this.fontName = font[_AN_Read_length('length', font) - 1]; this.fontSize = _AN_Read_length('length', font) >= 2? parseInt(font[_AN_Read_length('length', font) - 2], 10): PIXI.BitmapText.fonts[this.fontName].size; this.dirty = true ; this.tint = style.tint; } ; PIXI.BitmapText.prototype.updateText = function (){ var data = PIXI.BitmapText.fonts[this.fontName]; var pos = new PIXI.Point(); var prevCharCode = null ; var chars = [] ; var maxLineWidth = 0; var lineWidths = [] ; var line = 0; var scale = this.fontSize / data.size; for (var i = 0; i < _AN_Read_length('length', this.text); i++ ){ var charCode = this.text.charCodeAt(i); if (/(?:\r\n|\r|\n)/.test(this.text.charAt(i))) { lineWidths.push(pos.x); maxLineWidth = Math.max(maxLineWidth, pos.x); line++ ; pos.x = 0; pos.y += data.lineHeight; prevCharCode = null ; continue ; } var charData = data.chars[charCode]; if (!charData) continue ; if (prevCharCode && charData[prevCharCode]) { pos.x += charData.kerning[prevCharCode]; } chars.push({ texture: charData.texture, line: line, charCode: charCode, position: new PIXI.Point(pos.x + charData.xOffset, pos.y + charData.yOffset)} ); pos.x += charData.xAdvance; prevCharCode = charCode; } lineWidths.push(pos.x); maxLineWidth = Math.max(maxLineWidth, pos.x); var lineAlignOffsets = [] ; for (i = 0; i <= line; i++ ){ var alignOffset = 0; if (this.style.align === 'right') { alignOffset = maxLineWidth - lineWidths[i]; } else if (this.style.align === 'center') { alignOffset = (maxLineWidth - lineWidths[i]) / 2; } lineAlignOffsets.push(alignOffset); } var lenChildren = _AN_Read_length('length', this.children); var lenChars = _AN_Read_length('length', chars); var tint = this.tint || 16777215; for (i = 0; i < lenChars; i++ ){ var c = i < lenChildren? this.children[i]: this._pool.pop(); if (c) c.setTexture(chars[i].texture); else c = new PIXI.Sprite(chars[i].texture); c.position.x = (chars[i].position.x + lineAlignOffsets[chars[i].line]) * scale; c.position.y = chars[i].position.y * scale; c.scale.x = c.scale.y = scale; c.tint = tint; if (!c.parent) this.addChild(c); } while (_AN_Read_length('length', this.children) > lenChars){ var child = this.getChildAt(_AN_Read_length('length', this.children) - 1); this._pool.push(child); this.removeChild(child); } this.textWidth = maxLineWidth * scale; this.textHeight = (pos.y + data.lineHeight) * scale; } ; PIXI.BitmapText.prototype.updateTransform = function (){ if (this.dirty) { this.updateText(); this.dirty = false ; } PIXI.DisplayObjectContainer.prototype.updateTransform.call(this); } ; PIXI.BitmapText.fonts = { } ;