phaser/src/gameobjects/bitmaptext/ParseRetroFont.js

204 lines
5.9 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var GetValue = require('../../utils/object/GetValue');
/**
* @typedef {object} Phaser.GameObjects.BitmapText.ParseRetroFont.RetroFontConfig
*
* @property {string} image - [description]
* @property {number} offset.x - If the font set doesn't start at the top left of the given image, specify the X coordinate offset here.
* @property {number} offset.y - If the font set doesn't start at the top left of the given image, specify the Y coordinate offset here.
* @property {number} width - The width of each character in the font set.
* @property {number} height - The height of each character in the font set.
* @property {string} chars - The characters used in the font set, in display order. You can use the TEXT_SET consts for common font set arrangements.
* @property {number} charsPerRow - The number of characters per row in the font set. If not given charsPerRow will be the image width / characterWidth.
* @property {number} spacing.x - If the characters in the font set have horizontal spacing between them set the required amount here.
* @property {number} spacing.y - If the characters in the font set have vertical spacing between them set the required amount here.
*/
2018-03-16 17:29:39 +00:00
/**
* [description]
*
* @function Phaser.GameObjects.BitmapText.ParseRetroFont
2018-03-16 17:29:39 +00:00
* @since 3.0.0
*
* @param {Phaser.Scene} scene - A reference to the Phaser Scene.
* @param {Phaser.GameObjects.BitmapText.ParseRetroFont.RetroFontConfig} config - The font configuration object.
2018-03-16 17:29:39 +00:00
*/
var ParseRetroFont = function (scene, config)
{
var w = config.width;
var h = config.height;
var cx = Math.floor(w / 2);
var cy = Math.floor(h / 2);
var letters = config.chars;
var key = GetValue(config, 'image', '');
var offsetX = GetValue(config, 'offset.x', 0);
var offsetY = GetValue(config, 'offset.y', 0);
var spacingX = GetValue(config, 'spacing.x', 0);
var spacingY = GetValue(config, 'spacing.y', 0);
var charsPerRow = GetValue(config, 'charsPerRow', null);
if (charsPerRow === null)
{
charsPerRow = scene.sys.textures.getFrame(key).width / w;
2017-06-28 00:50:34 +00:00
if (charsPerRow > letters.length)
{
charsPerRow = letters.length;
}
}
var x = offsetX;
var y = offsetY;
var data = {
retroFont: true,
font: key,
size: w,
lineHeight: h,
chars: {}
};
var r = 0;
for (var i = 0; i < letters.length; i++)
{
// var node = letters[i];
var charCode = letters.charCodeAt(i);
data.chars[charCode] =
{
x: x,
y: y,
width: w,
height: h,
centerX: cx,
centerY: cy,
xOffset: 0,
yOffset: 0,
xAdvance: w,
data: {},
kerning: {}
};
r++;
if (r === charsPerRow)
{
r = 0;
x = offsetX;
y += h + spacingY;
}
else
{
x += w + spacingX;
}
}
var entry = {
data: data,
frame: null,
texture: key
};
return entry;
};
2017-06-28 00:50:34 +00:00
/**
* Text Set 1 = !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~
*
* @name Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET1
* @type {string}
*/
2018-02-16 18:17:51 +00:00
ParseRetroFont.TEXT_SET1 = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
/**
* Text Set 2 = !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ
*
* @name Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET2
* @type {string}
*/
2018-02-16 18:17:51 +00:00
ParseRetroFont.TEXT_SET2 = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ';
/**
* Text Set 3 = ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
* @name Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET3
*
* @type {string}
*/
2018-02-16 18:17:51 +00:00
ParseRetroFont.TEXT_SET3 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ';
/**
* Text Set 4 = ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789
*
* @name Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET4
* @type {string}
*/
2018-02-16 18:17:51 +00:00
ParseRetroFont.TEXT_SET4 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789';
/**
* Text Set 5 = ABCDEFGHIJKLMNOPQRSTUVWXYZ.,/() '!?-*:0123456789
*
* @name Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET5
* @type {string}
*/
2018-02-16 18:17:51 +00:00
ParseRetroFont.TEXT_SET5 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.,/() \'!?-*:0123456789';
/**
* Text Set 6 = ABCDEFGHIJKLMNOPQRSTUVWXYZ!?:;0123456789"(),-.'
*
* @name Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET6
* @type {string}
* /
2018-02-16 18:17:51 +00:00
ParseRetroFont.TEXT_SET6 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ!?:;0123456789"(),-.\' ';
/**
* Text Set 7 = AGMSY+:4BHNTZ!;5CIOU.?06DJPV,(17EKQW")28FLRX-'39
*
* @name Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET7
* @type {string}
*/
2018-02-16 18:17:51 +00:00
ParseRetroFont.TEXT_SET7 = 'AGMSY+:4BHNTZ!;5CIOU.?06DJPV,(17EKQW")28FLRX-\'39';
/**
* Text Set 8 = 0123456789 .ABCDEFGHIJKLMNOPQRSTUVWXYZ
*
* @name Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET8
* @type {string}
*/
2018-02-16 18:17:51 +00:00
ParseRetroFont.TEXT_SET8 = '0123456789 .ABCDEFGHIJKLMNOPQRSTUVWXYZ';
/**
* Text Set 9 = ABCDEFGHIJKLMNOPQRSTUVWXYZ()-0123456789.:,'"?!
*
* @name Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET9
* @type {string}
*/
2018-02-16 18:17:51 +00:00
ParseRetroFont.TEXT_SET9 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ()-0123456789.:,\'"?!';
/**
* Text Set 10 = ABCDEFGHIJKLMNOPQRSTUVWXYZ
*
* @name Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET10
* @type {string}
*/
2018-02-16 18:17:51 +00:00
ParseRetroFont.TEXT_SET10 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
/**
* Text Set 11 = ABCDEFGHIJKLMNOPQRSTUVWXYZ.,"-+!?()':;0123456789
*
* @name Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET11
* @type {string}
*/
2018-02-16 18:17:51 +00:00
ParseRetroFont.TEXT_SET11 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.,"-+!?()\':;0123456789';
module.exports = ParseRetroFont;