X Tutup
Skip to content

Commit beaece4

Browse files
committed
convert BitnapTextData to TS and to the new internal pool system
1 parent bcfd638 commit beaece4

File tree

6 files changed

+81
-74
lines changed

6 files changed

+81
-74
lines changed

packages/melonjs/src/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Trigger from "./renderable/trigger.js";
2727
import Light2d from "./renderable/light2d.js";
2828
import Text from "./renderable/text/text.js";
2929
import BitmapText from "./renderable/text/bitmaptext.js";
30-
import BitmapTextData from "./renderable/text/bitmaptextdata.js";
30+
import BitmapTextData from "./renderable/text/bitmaptextdata.ts";
3131
import { Draggable } from "./renderable/draggable.js";
3232
import { DropTarget } from "./renderable/dragndrop.js";
3333
import TMXRenderer from "./level/tiled/renderer/TMXRenderer.js";
@@ -198,7 +198,7 @@ export function boot() {
198198
console.log("melonJS 2 (v" + version + ") | http://melonjs.org");
199199
}
200200

201-
// register all built-ins objects into the object pool
201+
// register all built-ins objects into the object legacy pool
202202
pool.register("me.Entity", Entity);
203203
pool.register("me.Collectable", Collectable);
204204
pool.register("me.Trigger", Trigger);
@@ -209,10 +209,7 @@ export function boot() {
209209
pool.register("me.Renderable", Renderable);
210210
pool.register("me.Text", Text, true);
211211
pool.register("me.BitmapText", BitmapText);
212-
pool.register("me.BitmapTextData", BitmapTextData, true);
213-
pool.register("me.ImageLayer", ImageLayer);
214212
pool.register("me.ColorLayer", ColorLayer, true);
215-
216213
// duplicate all entries if use with no namespace (e.g. es6)
217214
pool.register("Entity", Entity);
218215
pool.register("Collectable", Collectable);
@@ -225,7 +222,6 @@ export function boot() {
225222
pool.register("Renderable", Renderable);
226223
pool.register("Text", Text, true);
227224
pool.register("BitmapText", BitmapText);
228-
pool.register("BitmapTextData", BitmapTextData, true);
229225
pool.register("ImageLayer", ImageLayer);
230226
pool.register("ColorLayer", ColorLayer, true);
231227

packages/melonjs/src/pool.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { vector3dPool } from "./math/vector3d";
1212
import { particlePool } from "./particles/particle";
1313
import { boundsPool } from "./physics/bounds";
1414
import { tweenPool } from "./tweens/tween";
15+
import { bitmapTextDataPool } from "./renderable/text/bitmaptextdata";
1516

1617
const pools = {
1718
vector2d: vector2dPool,
@@ -28,6 +29,7 @@ const pools = {
2829
ellipse: ellipsePool,
2930
tween: tweenPool,
3031
particle: particlePool,
32+
bitmapTextData: bitmapTextDataPool,
3133
} as const;
3234

3335
type PoolKey = keyof typeof pools;

packages/melonjs/src/renderable/text/bitmaptext.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Color } from "../../math/color.ts";
2-
import pool from "../../system/legacy_pool.js";
32
import { getImage, getBinary } from "../../loader/loader.js";
43
import Renderable from "../renderable.js";
54
import TextMetrics from "./textmetrics.js";
65
import { vector2dPool } from "../../math/vector2d.ts";
6+
import { bitmapTextDataPool } from "./bitmaptextdata.ts";
77

88
/**
99
* a bitmap font object
@@ -105,10 +105,9 @@ export default class BitmapText extends Renderable {
105105
* @private
106106
*/
107107
// use settings.font to retreive the data from the loader
108-
this.fontData = pool.pull("BitmapTextData", getBinary(settings.font));
108+
this.fontData = bitmapTextDataPool.get(getBinary(settings.font));
109109
} else {
110-
this.fontData = pool.pull(
111-
"BitmapTextData",
110+
this.fontData = bitmapTextDataPool.get(
112111
// if starting/includes "info face" the whole data string was passed as parameter
113112
settings.fontData.includes("info face")
114113
? settings.fontData
@@ -425,7 +424,7 @@ export default class BitmapText extends Renderable {
425424
destroy() {
426425
vector2dPool.release(this.fontScale);
427426
this.fontScale = undefined;
428-
pool.push(this.fontData);
427+
bitmapTextDataPool.release(this.fontData);
429428
this.fontData = undefined;
430429
this._text.length = 0;
431430
this.metrics = undefined;

packages/melonjs/src/renderable/text/bitmaptextdata.js renamed to packages/melonjs/src/renderable/text/bitmaptextdata.ts

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import Glyph from "./glyph.js";
1+
import Glyph from "./glyph.ts";
2+
import { createPool } from "../../pool.ts";
23

34
// bitmap constants
4-
const capChars = [
5+
const capChars: string[] = [
56
"M",
67
"N",
78
"B",
@@ -34,11 +35,11 @@ const capChars = [
3435
* Gets the value from a string of pairs.
3536
* @ignore
3637
*/
37-
function getValueFromPair(string, pattern) {
38+
function getValueFromPair(string: string, pattern: RegExp): string {
3839
const value = string.match(pattern);
3940
if (!value) {
4041
throw new Error(
41-
"Could not find pattern " + pattern + " in string: " + string,
42+
`${`Could not find pattern ${pattern}` as string} in string: ${string}`,
4243
);
4344
}
4445

@@ -48,13 +49,13 @@ function getValueFromPair(string, pattern) {
4849
/**
4950
* Gets the first glyph in the map that is not a space character
5051
* @ignore
51-
* @param {object} glyphs - the map of glyphs, each key is a char code
52-
* @returns {Glyph}
52+
* @param glyphs - the map of glyphs, each key is a char code
53+
* @returns the first glyph that is not a space character
5354
*/
54-
function getFirstGlyph(glyphs) {
55+
function getFirstGlyph(glyphs: any): Glyph | null {
5556
const keys = Object.keys(glyphs);
5657
for (let i = 0; i < keys.length; i++) {
57-
if (keys[i] > 32) {
58+
if (parseInt(keys[i]) > 32) {
5859
return glyphs[keys[i]];
5960
}
6061
}
@@ -64,84 +65,62 @@ function getFirstGlyph(glyphs) {
6465
/**
6566
* Creates a glyph to use for the space character
6667
* @ignore
67-
* @param {object} glyphs - the map of glyphs, each key is a char code
68+
* @param glyphs - the map of glyphs, each key is a char code
6869
*/
69-
function createSpaceGlyph(glyphs) {
70+
function createSpaceGlyph(glyphs: any) {
7071
const spaceCharCode = " ".charCodeAt(0);
7172
let glyph = glyphs[spaceCharCode];
7273
if (!glyph) {
7374
glyph = new Glyph();
7475
glyph.id = spaceCharCode;
75-
glyph.xadvance = getFirstGlyph(glyphs).xadvance;
76+
const firstGlyph = getFirstGlyph(glyphs);
77+
glyph.xadvance = firstGlyph !== null ? firstGlyph.xadvance : 0;
7678
glyphs[spaceCharCode] = glyph;
7779
}
7880
}
7981

80-
/**
81-
* Class for storing relevant data from the font file.
82-
* @ignore
83-
*/
8482
export default class BitmapTextData {
85-
/**
86-
* @param {string} data - The bitmap font data pulled from the resource loader using me.loader.getBinary()
87-
*/
88-
constructor(data) {
89-
this.onResetEvent(data);
90-
}
91-
92-
/**
93-
* @ignore
94-
*/
95-
onResetEvent(data) {
96-
this.padTop = 0;
97-
this.padRight = 0;
98-
this.padBottom = 0;
99-
this.padLeft = 0;
100-
this.lineHeight = 0;
101-
// The distance from the top of most uppercase characters to the baseline. Since the drawing position is the cap height of
102-
// the first line, the cap height can be used to get the location of the baseline.
103-
this.capHeight = 1;
104-
// The distance from the bottom of the glyph that extends the lowest to the baseline. This number is negative.
105-
this.descent = 0;
106-
107-
/**
108-
* The map of glyphs, each key is a char code.
109-
* @type {object}
110-
*/
111-
this.glyphs = {};
112-
113-
// parse the data
83+
padTop: number = 0;
84+
padRight: number = 0;
85+
padBottom: number = 0;
86+
padLeft: number = 0;
87+
lineHeight: number = 0;
88+
capHeight: number = 1;
89+
descent: number = 0;
90+
glyphs: { [key: number]: Glyph } = {};
91+
92+
constructor(data: string) {
11493
this.parse(data);
11594
}
11695

117-
/**
118-
* This parses the font data text and builds a map of glyphs containing the data for each character
119-
* @param {string} fontData
120-
*/
121-
parse(fontData) {
96+
parse(fontData: string) {
12297
if (!fontData) {
12398
throw new Error(
12499
"File containing font data was empty, cannot load the bitmap font.",
125100
);
126101
}
102+
127103
const lines = fontData.split(/\r\n|\n/);
128-
const padding = fontData.match(/padding\=\d+,\d+,\d+,\d+/g);
104+
const padding = fontData.match(/padding=\d+,\d+,\d+,\d+/g);
129105
if (!padding) {
130106
throw new Error("Padding not found in first line");
131107
}
132108
const paddingValues = padding[0].split("=")[1].split(",");
109+
133110
this.padTop = parseFloat(paddingValues[0]);
134111
this.padLeft = parseFloat(paddingValues[1]);
135112
this.padBottom = parseFloat(paddingValues[2]);
136113
this.padRight = parseFloat(paddingValues[3]);
114+
this.lineHeight = parseFloat(getValueFromPair(lines[1], /lineHeight=\d+/g));
137115

138-
this.lineHeight = parseFloat(
139-
getValueFromPair(lines[1], /lineHeight\=\d+/g),
140-
);
116+
this.capHeight = 1;
117+
this.descent = 0;
118+
this.glyphs = {};
141119

142-
const baseLine = parseFloat(getValueFromPair(lines[1], /base\=\d+/g));
120+
const baseLine = parseFloat(getValueFromPair(lines[1], /base=\d+/g));
143121
const padY = this.padTop + this.padBottom;
144-
let glyph = null;
122+
123+
let glyph: Glyph | null = null;
145124

146125
for (let i = 4; i < lines.length; i++) {
147126
const line = lines[i];
@@ -183,7 +162,7 @@ export default class BitmapTextData {
183162

184163
createSpaceGlyph(this.glyphs);
185164

186-
let capGlyph = null;
165+
let capGlyph: Glyph | null = null;
187166
for (let i = 0; i < capChars.length; i++) {
188167
const capChar = capChars[i];
189168
capGlyph = this.glyphs[capChar.charCodeAt(0)];
@@ -193,7 +172,7 @@ export default class BitmapTextData {
193172
}
194173
if (!capGlyph) {
195174
for (const charCode in this.glyphs) {
196-
if (this.glyphs.hasOwnProperty(charCode)) {
175+
if (Object.prototype.hasOwnProperty.call(this.glyphs, charCode)) {
197176
glyph = this.glyphs[charCode];
198177
if (glyph.height === 0 || glyph.width === 0) {
199178
continue;
@@ -207,3 +186,17 @@ export default class BitmapTextData {
207186
this.capHeight -= padY;
208187
}
209188
}
189+
190+
export const bitmapTextDataPool = createPool<
191+
BitmapTextData,
192+
[fontData: string]
193+
>((fontData: string) => {
194+
const instance = new BitmapTextData(fontData);
195+
196+
return {
197+
instance,
198+
reset(fontData) {
199+
instance.parse(fontData);
200+
},
201+
};
202+
});

packages/melonjs/src/renderable/text/glyph.js renamed to packages/melonjs/src/renderable/text/glyph.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// bitmap constants
21
const LOG2_PAGE_SIZE = 9;
32
const PAGE_SIZE = 1 << LOG2_PAGE_SIZE;
43

@@ -7,6 +6,24 @@ const PAGE_SIZE = 1 << LOG2_PAGE_SIZE;
76
* @ignore
87
*/
98
export default class Glyph {
9+
/**
10+
* @ignore
11+
*/
12+
id: number;
13+
x: number;
14+
y: number;
15+
width: number;
16+
height: number;
17+
u: number;
18+
v: number;
19+
u2: number;
20+
v2: number;
21+
xoffset: number;
22+
yoffset: number;
23+
xadvance: number;
24+
fixedWidth: boolean;
25+
kerning: { [key: number]: { [key: number]: number } };
26+
1027
/**
1128
* @ignore
1229
*/
@@ -29,7 +46,7 @@ export default class Glyph {
2946
/**
3047
* @ignore
3148
*/
32-
getKerning(ch) {
49+
getKerning(ch: number): number {
3350
if (this.kerning) {
3451
const page = this.kerning[ch >>> LOG2_PAGE_SIZE];
3552
if (page) {
@@ -42,7 +59,7 @@ export default class Glyph {
4259
/**
4360
* @ignore
4461
*/
45-
setKerning(ch, value) {
62+
setKerning(ch: number, value: number): void {
4663
if (!this.kerning) {
4764
this.kerning = {};
4865
}

packages/melonjs/tests/bitmapfontdata.spec.js renamed to packages/melonjs/tests/bitmapfontdata.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { beforeEach, describe, expect, it } from "vitest";
2-
import { BitmapTextData } from "../src/index.js";
2+
import { getPool } from "../src/pool.ts";
33

44
const bitmapTextDataFixture =
55
'info face="Arial" size=18 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=0,0\n' +
@@ -143,9 +143,9 @@ const bitmapTextDataFixture =
143143

144144
describe("BitmapTextData", () => {
145145
describe("parse", () => {
146-
let bitmapTextData = null;
146+
let bitmapTextData: any = null;
147147
beforeEach(() => {
148-
bitmapTextData = new BitmapTextData(bitmapTextDataFixture);
148+
bitmapTextData = getPool("bitmapTextData").get(bitmapTextDataFixture);
149149
});
150150

151151
it("should have 95 glyphs", () => {

0 commit comments

Comments
 (0)
X Tutup