- 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
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
import { BitmapFontData } from '../BitmapFontData';
/**
* Internal data format used to convert to BitmapFontData.
* @private
*/
export interface IBitmapFontRawData
{
info: {
face: string;
size: string;
}[];
common: { lineHeight: string }[];
page: {
id: string;
file: string;
}[];
chars: {
count: number;
}[];
char: {
id: string;
page: string;
x: string;
y: string;
width: string;
height: string;
xoffset: string;
yoffset: string;
xadvance: string;
}[];
kernings?: {
count: number;
}[];
kerning?: {
first: string;
second: string;
amount: string;
}[];
distanceField?: {
fieldType: string;
distanceRange: string;
}[]
}
/**
* BitmapFont format that's Text-based.
* @private
*/
export class TextFormat
{
/**
* Check if resource refers to txt font data.
* @param data
* @returns - True if resource could be treated as font data, false otherwise.
*/
static test(data: unknown): boolean
{
return typeof data === 'string' && data.indexOf('info face=') === 0;
}
/**
* Convert text font data to a javascript object.
* @param txt - Raw string data to be converted
* @returns - Parsed font data
*/
static parse(txt: string): BitmapFontData
{
// Retrieve data item
const items = txt.match(/^[a-z]+\s+.+$/gm);
const rawData: IBitmapFontRawData = {
info: [],
common: [],
page: [],
char: [],
chars: [],
kerning: [],
kernings: [],
distanceField: [],
};
for (const i in items)
{
// Extract item name
const name = items[i].match(/^[a-z]+/gm)[0] as keyof BitmapFontData;
// Extract item attribute list as string ex.: "width=10"
const attributeList = items[i].match(/[a-zA-Z]+=([^\s"']+|"([^"]*)")/gm);
// Convert attribute list into an object
const itemData: any = {};
for (const i in attributeList)
{
// Split key-value pairs
const split = attributeList[i].split('=');
const key = split[0];
// Remove eventual quotes from value
const strValue = split[1].replace(/"/gm, '');
// Try to convert value into float
const floatValue = parseFloat(strValue);
// Use string value case float value is NaN
const value = isNaN(floatValue) ? strValue : floatValue;
itemData[key] = value;
}
// Push current item to the resulting data
rawData[name].push(itemData);
}
const font = new BitmapFontData();
rawData.info.forEach((info) => font.info.push({
face: info.face,
size: parseInt(info.size, 10),
}));
rawData.common.forEach((common) => font.common.push({
lineHeight: parseInt(common.lineHeight, 10),
}));
rawData.page.forEach((page) => font.page.push({
id: parseInt(page.id, 10),
file: page.file,
}));
rawData.char.forEach((char) => font.char.push({
id: parseInt(char.id, 10),
page: parseInt(char.page, 10),
x: parseInt(char.x, 10),
y: parseInt(char.y, 10),
width: parseInt(char.width, 10),
height: parseInt(char.height, 10),
xoffset: parseInt(char.xoffset, 10),
yoffset: parseInt(char.yoffset, 10),
xadvance: parseInt(char.xadvance, 10),
}));
rawData.kerning.forEach((kerning) => font.kerning.push({
first: parseInt(kerning.first, 10),
second: parseInt(kerning.second, 10),
amount: parseInt(kerning.amount, 10),
}));
rawData.distanceField.forEach((df) => font.distanceField.push({
distanceRange: parseInt(df.distanceRange, 10),
fieldType: df.fieldType,
}));
return font;
}
}