From fb875426157cce5aa3576a51c646db313a0f7d7b Mon Sep 17 00:00:00 2001 From: Logan Date: Thu, 14 Dec 2023 14:49:43 -0700 Subject: [PATCH 1/4] new regex for bmfont attribute matching. start of test cases --- flixel/graphics/frames/bmfont/BMFontUtil.hx | 10 +- .../graphics/frames/bmfont/BMFontTest.hx | 162 ++++++++++++++++++ 2 files changed, 167 insertions(+), 5 deletions(-) create mode 100644 tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx diff --git a/flixel/graphics/frames/bmfont/BMFontUtil.hx b/flixel/graphics/frames/bmfont/BMFontUtil.hx index 8d2390175e..51dbc6287c 100644 --- a/flixel/graphics/frames/bmfont/BMFontUtil.hx +++ b/flixel/graphics/frames/bmfont/BMFontUtil.hx @@ -8,16 +8,16 @@ using StringTools; @:noCompletion class BMFontUtil { - public static var SPACE_REG = ~/ +/g; + public static var ATTRIBUTE_REG = ~/(\w+?)=((".*?")|.*?)(\s|$)/g; public static var QUOTES_REG = ~/^"(.+)"$/; public static function forEachAttribute(text:UnicodeString, callback:(key:String, value:UnicodeString)->Void) { - for (s in SPACE_REG.split(text)) + while (ATTRIBUTE_REG.match(text)) { - final split = s.split('='); - final key = parseKey(split[0]); - final value = parseValue(split[1]); + final key = ATTRIBUTE_REG.matched(1); + final value = ATTRIBUTE_REG.matched(2); callback(key, value); + text = ATTRIBUTE_REG.matchedRight(); } } diff --git a/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx b/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx new file mode 100644 index 0000000000..8c382f9c19 --- /dev/null +++ b/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx @@ -0,0 +1,162 @@ +package flixel.graphics.frames.bmfont; + +import massive.munit.Assert; +// import flixel.graphics.frames.bmfont.; + +class BMFontTest extends FlxTest +{ + @Test + function testXMLFormat() + { + var xml = ' + + + + + + + + + + + + + + + + + + '; + + var font = BMFont.parse(cast xml); + + // INFO + Assert.areEqual(font.info.face, "Test Font"); + Assert.areEqual(font.info.size, 32); + Assert.areEqual(font.info.bold, false); + Assert.areEqual(font.info.italic, false); + Assert.areEqual(font.info.charset, ""); + Assert.areEqual(font.info.unicode, true); + Assert.areEqual(font.info.stretchH, 100); + Assert.areEqual(font.info.smooth, true); + Assert.areEqual(font.info.aa, 1); + Assert.areEqual(font.info.paddingUp, 1); + Assert.areEqual(font.info.paddingRight, 2); + Assert.areEqual(font.info.paddingDown, 3); + Assert.areEqual(font.info.paddingLeft, 4); + Assert.areEqual(font.info.spacingHoriz, 1); + Assert.areEqual(font.info.spacingVert, 2); + Assert.areEqual(font.info.outline, 0); + + // COMMON + Assert.areEqual(font.common.lineHeight, 32); + Assert.areEqual(font.common.base, 26); + Assert.areEqual(font.common.scaleW, 256); + Assert.areEqual(font.common.scaleH, 256); + Assert.areEqual(font.common.pages, 1); + Assert.areEqual(font.common.packed, false); + Assert.areEqual(font.common.alphaChnl, 1); + Assert.areEqual(font.common.redChnl, 0); + Assert.areEqual(font.common.greenChnl, 0); + Assert.areEqual(font.common.blueChnl, 0); + + // PAGES + Assert.areEqual(font.pages.length, 1); + Assert.areEqual(font.pages[0].id, 0); + Assert.areEqual(font.pages[0].file, "tester_xml_0.png"); + + // Chars + Assert.areEqual(font.chars.length, 3); + var expectedChars:Array = [ + { + id: 64, + x: 0, + y: 0, + width: 26, + height: 26, + xoffset: 1, + yoffset: 6, + xadvance: 27, + page: 0, + chnl: 15, + letter: "@" + }, + { + id: 65, + x: 40, + y: 26, + width: 19, + height: 20, + xoffset: -1, + yoffset: 6, + xadvance: 18, + page: 0, + chnl: 15, + letter: "A" + }, + { + id: 66, + x: 35, + y: 48, + width: 16, + height: 20, + xoffset: 1, + yoffset: 6, + xadvance: 18, + page: 0, + chnl: 15, + letter: "B" + }, + ]; + + for (i in 0...expectedChars.length) { + assertCharMatches(expectedChars[i], font.chars[i]); + } + + // Kerning + Assert.areEqual(font.chars.length, 3); + var expectedKerns:Array = [ + { + first: 65, + second: 32, + amount: -1 + }, + { + first: 65, + second: 84, + amount: -2 + }, + { + first: 66, + second: 86, + amount: -3 + }, + ]; + + for (i in 0...expectedKerns.length) { + assertKerningMatches(expectedKerns[i], font.kerning[i]); + } + } + + private function assertCharMatches(expected:BMFontChar, actual:BMFontChar) { + Assert.areEqual(expected.id, actual.id); + Assert.areEqual(expected.x, actual.x); + Assert.areEqual(expected.y, actual.y); + Assert.areEqual(expected.width, actual.width); + Assert.areEqual(expected.height, actual.height); + Assert.areEqual(expected.xoffset, actual.xoffset); + Assert.areEqual(expected.yoffset, actual.yoffset); + Assert.areEqual(expected.xadvance, actual.xadvance); + Assert.areEqual(expected.page, actual.page); + Assert.areEqual(expected.chnl, actual.chnl); + // if (expected.letter != null) { + // Assert.areEqual(expected.letter, actual.letter); + // } + } + + private function assertKerningMatches(expected:BMFontKerning, actual:BMFontKerning) { + Assert.areEqual(expected.first, actual.first); + Assert.areEqual(expected.second, actual.second); + Assert.areEqual(expected.amount, actual.amount); + } +} \ No newline at end of file From 90e69663f1a84972672a64d48db40a9df445888f Mon Sep 17 00:00:00 2001 From: Logan Date: Thu, 14 Dec 2023 15:36:24 -0700 Subject: [PATCH 2/4] add BMFont text format test --- .../graphics/frames/bmfont/BMFontTest.hx | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx b/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx index ef9d01e0ae..a9f284f217 100644 --- a/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx +++ b/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx @@ -5,6 +5,27 @@ import massive.munit.Assert; class BMFontTest extends FlxTest { + @Test + function testTextFormat() + { + var text = 'info face="Test Font" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=1,2,3,4 spacing=1,2 outline=0 +common lineHeight=32 base=26 scaleW=256 scaleH=256 pages=1 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0 +page id=0 file="tester_0.png" +chars count=3 +char id=64 x=0 y=0 width=26 height=26 xoffset=1 yoffset=6 xadvance=27 page=0 chnl=15 +char id=65 x=40 y=26 width=19 height=20 xoffset=-1 yoffset=6 xadvance=18 page=0 chnl=15 +char id=66 x=35 y=48 width=16 height=20 xoffset=1 yoffset=6 xadvance=18 page=0 chnl=15 +kernings count=3 +kerning first=65 second=32 amount=-1 +kerning first=65 second=84 amount=-2 +kerning first=66 second=86 amount=-3 +'; + + var font = BMFont.parse(cast text); + trace(font); + assertFont(font); + } + @Test function testXMLFormat() { @@ -13,7 +34,7 @@ class BMFontTest extends FlxTest - + @@ -29,7 +50,11 @@ class BMFontTest extends FlxTest '; var font = BMFont.parse(cast xml); + assertFont(font); + } + // This assumes the incoming font has a specific configuration we are checking for + private function assertFont(font:BMFont) { // INFO Assert.areEqual(font.info.face, "Test Font"); Assert.areEqual(font.info.size, 32); @@ -63,7 +88,7 @@ class BMFontTest extends FlxTest // PAGES Assert.areEqual(font.pages.length, 1); Assert.areEqual(font.pages[0].id, 0); - Assert.areEqual(font.pages[0].file, "tester_xml_0.png"); + Assert.areEqual(font.pages[0].file, "tester_0.png"); // Chars Assert.areEqual(font.chars.length, 3); From 62db5d3e9abd84f04389bccae1309c7e20404ee4 Mon Sep 17 00:00:00 2001 From: Logan Date: Thu, 14 Dec 2023 17:36:54 -0700 Subject: [PATCH 3/4] update BMFontTest to include binary file test --- flixel/graphics/frames/bmfont/BMFontInfo.hx | 8 +- flixel/graphics/frames/bmfont/BMFontPage.hx | 2 +- .../graphics/frames/bmfont/BMFontTest.hx | 162 +++++++++--------- 3 files changed, 88 insertions(+), 84 deletions(-) diff --git a/flixel/graphics/frames/bmfont/BMFontInfo.hx b/flixel/graphics/frames/bmfont/BMFontInfo.hx index 8665bb3c51..a8495f0b60 100644 --- a/flixel/graphics/frames/bmfont/BMFontInfo.hx +++ b/flixel/graphics/frames/bmfont/BMFontInfo.hx @@ -107,17 +107,19 @@ class BMFontInfo static function fromBytes(bytes:BytesInput) { final blockSize = bytes.readInt32(); + final size = bytes.readInt16(); final bitField = bytes.readByte(); + final charsetByte = bytes.readByte(); final fontInfo:BMFontInfo = { - size: bytes.readInt16(), + size: size, smooth: (bitField & 0x80) != 0, unicode: (bitField & (0x80 >> 1)) != 0, italic: (bitField & (0x80 >> 2)) != 0, bold: (bitField & (0x80 >> 3)) != 0, fixedHeight: (bitField & (0x80 >> 4)) != 0, - charset: String.fromCharCode(bytes.readByte()), - stretchH: bytes.readInt16(), + charset: charsetByte > 0 ? String.fromCharCode(charsetByte) : "", + stretchH: bytes.readUInt16(), aa: bytes.readByte(), padding: BMFontPadding.fromBytes(new BytesInput(bytes.read(4))), spacing: BMFontSpacing.fromBytes(new BytesInput(bytes.read(2))), diff --git a/flixel/graphics/frames/bmfont/BMFontPage.hx b/flixel/graphics/frames/bmfont/BMFontPage.hx index 68206e1ef9..304d9f9979 100644 --- a/flixel/graphics/frames/bmfont/BMFontPage.hx +++ b/flixel/graphics/frames/bmfont/BMFontPage.hx @@ -63,7 +63,7 @@ class BMFontPage final pages = new Array(); var i = 0; - while (blockSize < 0) + while (blockSize > 0) { final bytesBuf = new BytesBuffer(); var curByte = bytes.readByte(); diff --git a/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx b/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx index a9f284f217..1f32c3a0d5 100644 --- a/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx +++ b/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx @@ -1,28 +1,26 @@ package flixel.graphics.frames.bmfont; +import haxe.io.Bytes; import massive.munit.Assert; -// import flixel.graphics.frames.bmfont.; class BMFontTest extends FlxTest { @Test function testTextFormat() { - var text = 'info face="Test Font" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=1,2,3,4 spacing=1,2 outline=0 -common lineHeight=32 base=26 scaleW=256 scaleH=256 pages=1 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0 -page id=0 file="tester_0.png" + var text = 'info face="Arial Black" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=1,2,3,4 spacing=2,1 outline=0 +common lineHeight=32 base=25 scaleW=256 scaleH=256 pages=1 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0 +page id=0 file="arial_black_0.png" chars count=3 -char id=64 x=0 y=0 width=26 height=26 xoffset=1 yoffset=6 xadvance=27 page=0 chnl=15 -char id=65 x=40 y=26 width=19 height=20 xoffset=-1 yoffset=6 xadvance=18 page=0 chnl=15 -char id=66 x=35 y=48 width=16 height=20 xoffset=1 yoffset=6 xadvance=18 page=0 chnl=15 -kernings count=3 -kerning first=65 second=32 amount=-1 +char id=64 x=0 y=0 width=25 height=24 xoffset=-5 yoffset=7 xadvance=17 page=0 chnl=15 +char id=65 x=27 y=0 width=26 height=21 xoffset=-5 yoffset=7 xadvance=18 page=0 chnl=15 +char id=84 x=55 y=0 width=23 height=21 xoffset=-4 yoffset=7 xadvance=16 page=0 chnl=15 +kernings count=2 +kerning first=84 second=65 amount=-2 kerning first=65 second=84 amount=-2 -kerning first=66 second=86 amount=-3 '; var font = BMFont.parse(cast text); - trace(font); assertFont(font); } @@ -31,106 +29,115 @@ kerning first=66 second=86 amount=-3 { var xml = ' - - + + - + - - - + + + - - + + - - - '; + '; var font = BMFont.parse(cast xml); assertFont(font); } + @Test + function testBinaryFormat() + { + var binary = Bytes.ofHex("424D4603011A0000002000C00064000101020304020100417269616C20426C61636B00020F0000002000190000010001010000010000000312000000617269616C5F626C61636B5F302E706E6700043C000000400000000000000019001800FBFF07001100000F410000001B0000001A001500FBFF07001200000F540000003700000017001500FCFF07001000000F05140000005400000041000000FEFF4100000054000000FEFF"); + + var font = BMFont.parse(cast binary); + assertFont(font); + } + // This assumes the incoming font has a specific configuration we are checking for private function assertFont(font:BMFont) { + trace(font); + // INFO - Assert.areEqual(font.info.face, "Test Font"); - Assert.areEqual(font.info.size, 32); - Assert.areEqual(font.info.bold, false); - Assert.areEqual(font.info.italic, false); - Assert.areEqual(font.info.charset, ""); - Assert.areEqual(font.info.unicode, true); - Assert.areEqual(font.info.stretchH, 100); - Assert.areEqual(font.info.smooth, true); - Assert.areEqual(font.info.aa, 1); - Assert.areEqual(font.info.padding.up, 1); - Assert.areEqual(font.info.padding.right, 2); - Assert.areEqual(font.info.padding.down, 3); - Assert.areEqual(font.info.padding.left, 4); - Assert.areEqual(font.info.spacing.x, 1); - Assert.areEqual(font.info.spacing.y, 2); - Assert.areEqual(font.info.outline, 0); + Assert.areEqual("Arial Black", font.info.face); + Assert.areEqual(32, font.info.size); + Assert.areEqual(false, font.info.bold); + Assert.areEqual(false, font.info.italic); + Assert.isEmpty(font.info.charset); + Assert.areEqual(true, font.info.unicode); + Assert.areEqual(100, font.info.stretchH); + Assert.areEqual(true, font.info.smooth); + Assert.areEqual(1, font.info.aa); + Assert.areEqual(1, font.info.padding.up); + Assert.areEqual(2, font.info.padding.right); + Assert.areEqual(3, font.info.padding.down); + Assert.areEqual(4, font.info.padding.left); + Assert.areEqual(2, font.info.spacing.x); + Assert.areEqual(1, font.info.spacing.y); + Assert.areEqual(0, font.info.outline); // COMMON - Assert.areEqual(font.common.lineHeight, 32); - Assert.areEqual(font.common.base, 26); - Assert.areEqual(font.common.scaleW, 256); - Assert.areEqual(font.common.scaleH, 256); - Assert.areEqual(font.common.pages, 1); - Assert.areEqual(font.common.packed, false); - Assert.areEqual(font.common.alphaChnl, 1); - Assert.areEqual(font.common.redChnl, 0); - Assert.areEqual(font.common.greenChnl, 0); - Assert.areEqual(font.common.blueChnl, 0); + Assert.areEqual(32, font.common.lineHeight); + Assert.areEqual(25, font.common.base); + Assert.areEqual(256, font.common.scaleW); + Assert.areEqual(256, font.common.scaleH); + Assert.areEqual(1, font.common.pages); + Assert.areEqual(false, font.common.packed); + Assert.areEqual(1, font.common.alphaChnl); + Assert.areEqual(0, font.common.redChnl); + Assert.areEqual(0, font.common.greenChnl); + Assert.areEqual(0, font.common.blueChnl); // PAGES - Assert.areEqual(font.pages.length, 1); - Assert.areEqual(font.pages[0].id, 0); - Assert.areEqual(font.pages[0].file, "tester_0.png"); + Assert.areEqual(1, font.pages.length); + Assert.areEqual(0, font.pages[0].id); + Assert.areEqual("arial_black_0.png", font.pages[0].file); // Chars - Assert.areEqual(font.chars.length, 3); + Assert.areEqual(3, font.chars.length); var expectedChars:Array = [ { id: 64, x: 0, y: 0, - width: 26, - height: 26, - xoffset: 1, - yoffset: 6, - xadvance: 27, + width: 25, + height: 24, + xoffset: -5, + yoffset: 7, + xadvance: 17, page: 0, chnl: 15, letter: "@" }, { id: 65, - x: 40, - y: 26, - width: 19, - height: 20, - xoffset: -1, - yoffset: 6, + x: 27, + y: 0, + width: 26, + height: 21, + xoffset: -5, + yoffset: 7, xadvance: 18, page: 0, chnl: 15, letter: "A" }, { - id: 66, - x: 35, - y: 48, - width: 16, - height: 20, - xoffset: 1, - yoffset: 6, - xadvance: 18, + id: 84, + x: 55, + y: 0, + width: 23, + height: 21, + xoffset: -4, + yoffset: 7, + xadvance: 16, page: 0, chnl: 15, - letter: "B" + letter: "T" }, ]; @@ -142,20 +149,15 @@ kerning first=66 second=86 amount=-3 Assert.areEqual(font.chars.length, 3); var expectedKerns:Array = [ { - first: 65, - second: 32, - amount: -1 + first: 84, + second: 65, + amount: -2 }, { first: 65, second: 84, amount: -2 }, - { - first: 66, - second: 86, - amount: -3 - }, ]; for (i in 0...expectedKerns.length) { From 396131f1f0c4cd9a2a0082bbf60eec88bbec14b4 Mon Sep 17 00:00:00 2001 From: Logan Date: Thu, 14 Dec 2023 17:44:16 -0700 Subject: [PATCH 4/4] formatting --- tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx b/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx index 1f32c3a0d5..68e4558ef8 100644 --- a/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx +++ b/tests/unit/src/flixel/graphics/frames/bmfont/BMFontTest.hx @@ -52,7 +52,12 @@ kerning first=65 second=84 amount=-2 @Test function testBinaryFormat() { - var binary = Bytes.ofHex("424D4603011A0000002000C00064000101020304020100417269616C20426C61636B00020F0000002000190000010001010000010000000312000000617269616C5F626C61636B5F302E706E6700043C000000400000000000000019001800FBFF07001100000F410000001B0000001A001500FBFF07001200000F540000003700000017001500FCFF07001000000F05140000005400000041000000FEFF4100000054000000FEFF"); + var binary = Bytes.ofHex("424D4603011A0000002000C00064000101020304020" + + "100417269616C20426C61636B00020F0000002000190000010001010000010000000" + + "312000000617269616C5F626C61636B5F302E706E6700043C0000004000000000000" + + "00019001800FBFF07001100000F410000001B0000001A001500FBFF07001200000F5" + + "40000003700000017001500FCFF07001000000F05140000005400000041000000FEF" + + "F4100000054000000FEFF"); var font = BMFont.parse(cast binary); assertFont(font);