/
ParseRetroFont.js
126 lines (104 loc) · 2.92 KB
/
ParseRetroFont.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var GetValue = require('../../utils/object/GetValue');
/**
* Parses a Retro Font configuration object so you can pass it to the BitmapText constructor
* and create a BitmapText object using a fixed-width retro font.
*
* @function Phaser.GameObjects.RetroFont.Parse
* @since 3.0.0
*
* @param {Phaser.Scene} scene - A reference to the Phaser Scene.
* @param {Phaser.Types.GameObjects.BitmapText.RetroFontConfig} config - The font configuration object.
*
* @return {object} A parsed Bitmap Font data entry for the Bitmap Font cache.
*/
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 = GetValue(config, 'chars', '');
if (letters === '')
{
return;
}
var key = GetValue(config, 'image', '');
var frame = scene.sys.textures.getFrame(key);
var textureX = frame.cutX;
var textureY = frame.cutY;
var textureWidth = frame.source.width;
var textureHeight = frame.source.height;
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 lineSpacing = GetValue(config, 'lineSpacing', 0);
var charsPerRow = GetValue(config, 'charsPerRow', null);
if (charsPerRow === null)
{
charsPerRow = textureWidth / w;
if (charsPerRow > letters.length)
{
charsPerRow = letters.length;
}
}
var x = offsetX;
var y = offsetY;
var data = {
retroFont: true,
font: key,
size: w,
lineHeight: h + lineSpacing,
chars: {}
};
var r = 0;
for (var i = 0; i < letters.length; i++)
{
var charCode = letters.charCodeAt(i);
var u0 = (textureX + x) / textureWidth;
var v0 = (textureY + y) / textureHeight;
var u1 = (textureX + x + w) / textureWidth;
var v1 = (textureY + y + h) / textureHeight;
data.chars[charCode] =
{
x: x,
y: y,
width: w,
height: h,
centerX: cx,
centerY: cy,
xOffset: 0,
yOffset: 0,
xAdvance: w,
data: {},
kerning: {},
u0: u0,
v0: v0,
u1: u1,
v1: v1
};
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;
};
module.exports = ParseRetroFont;