Skip to content

Commit b38b00c

Browse files
committed
Loader.bitmapFont now has 2 extra parameters: xSpacing and ySpacing. These allow you to add extra spacing to each letter or line of the font.
1 parent 24f2e2a commit b38b00c

6 files changed

Lines changed: 75 additions & 62 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Significant API changes:
8585
* Stage.canvas has been moved to Game.canvas (which used to be a reference to Stage.canvas, but is now the actual object).
8686
* BitmapData.addTo removed and enhanced BitmapData.add so it can accept either a single Sprite/Image or an Array of them.
8787
* BitmapData has had all of the EaselJS functions removed. It was just taking up space and you can do it all via BitmapData.context directly.
88+
* BitmapText has had a bit of an overhaul - the signature for adding a BitmapText has changed to: x, y, font, text, size. See the docs and examples for details.
8889

8990

9091
New features:
@@ -105,9 +106,9 @@ New features:
105106
* TileSprite can now use a frame from a texture atlas or a sprite sheet.
106107
* TileSprites can now be animated. See new example :)
107108
* TileSprites have a new method: autoScroll(x, y) which sets an automatic scroll running (until stopped with TileSprite.stopScroll).
108-
109-
110-
New Examples:
109+
* BitmapText now uses the new XML parser which should work under CocoonJS without clashes.
110+
* BitmapText signature changed so you can support fonts with spaces in their names.
111+
* Loader.bitmapFont now has 2 extra parameters: xSpacing and ySpacing. These allow you to add extra spacing to each letter or line of the font.
111112

112113

113114
Updates:

examples/wip/bitmaptext1.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: p
33

44
function preload() {
55

6-
game.load.bitmapFont('desyrel', 'assets/fonts/desyrel.png', 'assets/fonts/desyrel.xml');
7-
game.load.bitmapFont('carrier', 'assets/fonts/carrier_command.png', 'assets/fonts/carrier_command.xml');
6+
game.load.bitmapFont('desyrel', 'assets/fonts/desyrel.png', 'assets/fonts/desyrel.xml', null, 0, -32);
7+
game.load.bitmapFont('carrier', 'assets/fonts/carrier_command.png', 'assets/fonts/carrier_command.xml', null, 0, 24);
88

99
}
1010

@@ -13,17 +13,19 @@ var text2;
1313

1414
function create() {
1515

16-
text = game.add.bitmapText(200, 100, 'carrier', 'Phaser & Pixi\nrocking!', 32);
17-
text2 = game.add.bitmapText(200, 300, 'desyrel', 'Phaser & Pixi\nrocking!', 32);
16+
text = game.add.bitmapText(100, 100, 'carrier', 'Phaser and Pixi\nrocking!', 32);
17+
18+
text2 = game.add.bitmapText(100, 300, 'desyrel', 'Phaser & Pixi\nrocking!', 64);
1819

19-
// text.tint = Math.random() * 0xFFFFFF;
2020
game.input.onDown.add(change, this);
2121

2222
}
2323

2424
function change() {
2525

26-
text.fontSize++;
26+
text.align = 'center';
27+
28+
text2.tint = Math.random() * 0xFFFFFF;
2729

2830
}
2931

src/gameobjects/BitmapText.js

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
* @param {number} x - X position of the new bitmapText object.
2121
* @param {number} y - Y position of the new bitmapText object.
2222
* @param {string} font - The key of the BitmapFont as stored in Game.Cache.
23-
* @param {string} [text] - The actual text that will be rendered. Can be set later via BitmapText.text.
24-
* @param {number} [size] - The size the font will be rendered in, in pixels.
23+
* @param {string} [text=''] - The actual text that will be rendered. Can be set later via BitmapText.text.
24+
* @param {number} [size=32] - The size the font will be rendered in, in pixels.
2525
*/
2626
Phaser.BitmapText = function (game, x, y, font, text, size) {
2727

@@ -85,10 +85,16 @@ Phaser.BitmapText = function (game, x, y, font, text, size) {
8585
this._fontSize = size;
8686

8787
/**
88-
* @property {number} lineSpacing - Additional spacing (in pixels) between each line of text if multi-line.
88+
* @property {string} _align - Internal cache var.
8989
* @private
9090
*/
91-
this._lineSpacing = 0;
91+
this._align = 'left';
92+
93+
/**
94+
* @property {number} _tint - Internal cache var.
95+
* @private
96+
*/
97+
this._tint = 0xFFFFFF;
9298

9399
/**
94100
* @property {Phaser.Events} events - The Events you can subscribe to that are dispatched when certain things happen on this Sprite or its components.
@@ -119,19 +125,10 @@ Phaser.BitmapText.prototype.constructor = Phaser.BitmapText;
119125
*/
120126
Phaser.BitmapText.prototype.setStyle = function() {
121127

122-
// style = style || {};
123-
// style.align = style.align || 'left';
124-
this.style = { align: 'left' };
125-
128+
this.style = { align: this._align };
126129
this.fontName = this._font;
127130
this.fontSize = this._fontSize;
128-
129-
// var font = style.font.split(' ');
130-
// this.fontName = font[font.length - 1];
131-
// this.fontSize = font.length >= 2 ? parseInt(font[font.length - 2], 10) : PIXI.BitmapText.fonts[this.fontName].size;
132-
133131
this.dirty = true;
134-
// this.tint = style.tint;
135132

136133
};
137134

@@ -182,16 +179,6 @@ Phaser.BitmapText.prototype.postUpdate = function () {
182179
*/
183180
Phaser.BitmapText.prototype.destroy = function() {
184181

185-
if (this.canvas && this.canvas.parentNode)
186-
{
187-
this.canvas.parentNode.removeChild(this.canvas);
188-
}
189-
else
190-
{
191-
this.canvas = null;
192-
this.context = null;
193-
}
194-
195182
if (this.filters)
196183
{
197184
this.filters = null;
@@ -202,23 +189,20 @@ Phaser.BitmapText.prototype.destroy = function() {
202189
this.parent.remove(this);
203190
}
204191

205-
this.texture.destroy();
206-
207-
if (this.canvas.parentNode)
208-
{
209-
this.canvas.parentNode.removeChild(this.canvas);
210-
}
211-
else
212-
{
213-
this.canvas = null;
214-
this.context = null;
215-
}
216-
217192
this.exists = false;
218193
this.visible = false;
219194

220195
this.game = null;
221196

197+
if (this.children.length > 0)
198+
{
199+
do
200+
{
201+
this.removeChild(this.children[0]);
202+
}
203+
while (this.children.length > 0);
204+
}
205+
222206
}
223207

224208
/**
@@ -228,21 +212,42 @@ Phaser.BitmapText.prototype.destroy = function() {
228212
Object.defineProperty(Phaser.BitmapText.prototype, 'align', {
229213

230214
get: function() {
231-
return this.style.align;
215+
return this._align;
232216
},
233217

234218
set: function(value) {
235219

236-
if (value !== this.style.align)
220+
if (value !== this._align)
237221
{
238-
this.style.align = value;
222+
this._align = value;
239223
this.dirty = true;
240224
}
241225

242226
}
243227

244228
});
245229

230+
/**
231+
* @name Phaser.BitmapText#tint
232+
* @property {number} tint - The tint applied to the BitmapText. This is a hex value. Set to white to disable (0xFFFFFF)
233+
*/
234+
Object.defineProperty(Phaser.BitmapText.prototype, 'tint', {
235+
236+
get: function() {
237+
return this._tint;
238+
},
239+
240+
set: function(value) {
241+
242+
if (value !== this._tint)
243+
{
244+
this._tint = value;
245+
this.dirty = true;
246+
}
247+
248+
}
249+
250+
});
246251

247252
/**
248253
* Indicates the rotation of the Text, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.

src/loader/Cache.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,18 @@ Phaser.Cache.prototype = {
212212
* @param {string} key - The unique key by which you will reference this object.
213213
* @param {string} url - URL of this font xml file.
214214
* @param {object} data - Extra font data.
215-
* @param xmlData {object} Texture atlas frames data.
215+
* @param {object} xmlData - Texture atlas frames data.
216+
* @param {number} [xSpacing=0] - If you'd like to add additional horizontal spacing between the characters then set the pixel value here.
217+
* @param {number} [ySpacing=0] - If you'd like to add additional vertical spacing between the lines then set the pixel value here.
216218
*/
217-
addBitmapFont: function (key, url, data, xmlData) {
219+
addBitmapFont: function (key, url, data, xmlData, xSpacing, ySpacing) {
218220

219221
this._images[key] = { url: url, data: data, spriteSheet: true };
220222

221223
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
222224
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
223225

224-
Phaser.LoaderParser.bitmapFont(this.game, xmlData, key);
225-
// this._images[key].frameData = Phaser.AnimationParser.XMLData(this.game, xmlData, key);
226+
Phaser.LoaderParser.bitmapFont(this.game, xmlData, key, xSpacing, ySpacing);
226227

227228
},
228229

src/loader/Loader.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,17 +497,21 @@ Phaser.Loader.prototype = {
497497
* @param {string} textureURL - The url of the font image file.
498498
* @param {string} [xmlURL] - The url of the font data file (xml/fnt)
499499
* @param {object} [xmlData] - An optional XML data object.
500+
* @param {number} [xSpacing=0] - If you'd like to add additional horizontal spacing between the characters then set the pixel value here.
501+
* @param {number} [ySpacing=0] - If you'd like to add additional vertical spacing between the lines then set the pixel value here.
500502
* @return {Phaser.Loader} This Loader instance.
501503
*/
502-
bitmapFont: function (key, textureURL, xmlURL, xmlData) {
504+
bitmapFont: function (key, textureURL, xmlURL, xmlData, xSpacing, ySpacing) {
503505

504506
if (typeof xmlURL === "undefined") { xmlURL = null; }
505507
if (typeof xmlData === "undefined") { xmlData = null; }
508+
if (typeof xSpacing === "undefined") { xSpacing = 0; }
509+
if (typeof ySpacing === "undefined") { ySpacing = 0; }
506510

507511
// A URL to a json/xml file has been given
508512
if (xmlURL)
509513
{
510-
this.addToFileList('bitmapfont', key, textureURL, { xmlURL: xmlURL });
514+
this.addToFileList('bitmapfont', key, textureURL, { xmlURL: xmlURL, xSpacing: xSpacing, ySpacing: ySpacing });
511515
}
512516
else
513517
{
@@ -540,7 +544,7 @@ Phaser.Loader.prototype = {
540544
}
541545
else
542546
{
543-
this.addToFileList('bitmapfont', key, textureURL, { xmlURL: null, xmlData: xml });
547+
this.addToFileList('bitmapfont', key, textureURL, { xmlURL: null, xmlData: xml, xSpacing: xSpacing, ySpacing: ySpacing });
544548
}
545549
}
546550
}
@@ -1005,7 +1009,7 @@ Phaser.Loader.prototype = {
10051009

10061010
if (file.xmlURL == null)
10071011
{
1008-
this.game.cache.addBitmapFont(file.key, file.url, file.data, file.xmlData);
1012+
this.game.cache.addBitmapFont(file.key, file.url, file.data, file.xmlData, file.xSpacing, file.ySpacing);
10091013
}
10101014
else
10111015
{
@@ -1208,7 +1212,7 @@ Phaser.Loader.prototype = {
12081212

12091213
if (file.type == 'bitmapfont')
12101214
{
1211-
this.game.cache.addBitmapFont(file.key, file.url, file.data, xml);
1215+
this.game.cache.addBitmapFont(file.key, file.url, file.data, xml, file.xSpacing, file.ySpacing);
12121216
}
12131217
else if (file.type == 'textureatlas')
12141218
{

src/loader/LoaderParser.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
* @class Phaser.LoaderParser
1111
*/
1212
Phaser.LoaderParser = {
13-
13+
1414
/**
15-
* Parse frame data from an XML file.
15+
* Parse a Bitmap Font from an XML file.
1616
* @method Phaser.LoaderParser.bitmapFont
1717
* @param {Phaser.Game} game - A reference to the current game.
1818
* @param {object} xml - XML data you want to parse.
1919
* @param {string} cacheKey - The key of the texture this font uses in the cache.
2020
*/
21-
bitmapFont: function (game, xml, cacheKey) {
21+
bitmapFont: function (game, xml, cacheKey, xSpacing, ySpacing) {
2222

2323
if (!xml || /MSIE 9/i.test(navigator.userAgent) || navigator.isCocoonJS)
2424
{
@@ -41,7 +41,7 @@ Phaser.LoaderParser = {
4141

4242
data.font = info.getAttribute('face');
4343
data.size = parseInt(info.getAttribute('size'), 10);
44-
data.lineHeight = parseInt(common.getAttribute('lineHeight'), 10);
44+
data.lineHeight = parseInt(common.getAttribute('lineHeight'), 10) + ySpacing;
4545
data.chars = {};
4646

4747
var letters = xml.getElementsByTagName('char');
@@ -61,7 +61,7 @@ Phaser.LoaderParser = {
6161
data.chars[charCode] = {
6262
xOffset: parseInt(letters[i].getAttribute('xoffset'), 10),
6363
yOffset: parseInt(letters[i].getAttribute('yoffset'), 10),
64-
xAdvance: parseInt(letters[i].getAttribute('xadvance'), 10),
64+
xAdvance: parseInt(letters[i].getAttribute('xadvance'), 10) + xSpacing,
6565
kerning: {},
6666
texture: PIXI.TextureCache[cacheKey] = new PIXI.Texture(texture, textureRect)
6767
};

0 commit comments

Comments
 (0)