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- */
8482export 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 ( / p a d d i n g \ =\d + , \d + , \d + , \d + / g) ;
104+ const padding = fontData . match ( / p a d d i n g = \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 ] , / l i n e H e i g h t = \d + / g) ) ;
137115
138- this . lineHeight = parseFloat (
139- getValueFromPair ( lines [ 1 ] , / l i n e H e i g h t \= \d + / g ) ,
140- ) ;
116+ this . capHeight = 1 ;
117+ this . descent = 0 ;
118+ this . glyphs = { } ;
141119
142- const baseLine = parseFloat ( getValueFromPair ( lines [ 1 ] , / b a s e \ =\d + / g) ) ;
120+ const baseLine = parseFloat ( getValueFromPair ( lines [ 1 ] , / b a s e = \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+ } ) ;
0 commit comments