BitmapFontFileReaderTests.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using MonoGame.Extended.Content.BitmapFonts;
  8. namespace MonoGame.Extended.BitmapFonts.Tests;
  9. public class BitmapFontFileReaderTests
  10. {
  11. private readonly BitmapFontFileContent _expected;
  12. public BitmapFontFileReaderTests()
  13. {
  14. _expected = CreateExpected();
  15. }
  16. private static BitmapFontFileContent CreateExpected()
  17. {
  18. BitmapFontFileContent bmfFile = new BitmapFontFileContent()
  19. {
  20. Header = new()
  21. {
  22. B = (byte)'B',
  23. M = (byte)'M',
  24. F = (byte)'F',
  25. Version = 3
  26. },
  27. Info = new()
  28. {
  29. FontSize = 32,
  30. BitField = 0b1100_0000,
  31. CharSet = 0,
  32. StretchH = 50,
  33. AA = 1,
  34. PaddingUp = 1,
  35. PaddingRight = 2,
  36. PaddingDown = 3,
  37. PaddingLeft = 4,
  38. SpacingHoriz = 6,
  39. SpacingVert = 5,
  40. Outline = 2
  41. },
  42. FontName = "Cute Dino",
  43. Common = new()
  44. {
  45. LineHeight = 16,
  46. Base = 12,
  47. ScaleW = 256,
  48. ScaleH = 256,
  49. Pages = 1,
  50. BitField = 0b0000_0000,
  51. AlphaChnl = 1,
  52. RedChnl = 0,
  53. GreenChnl = 0,
  54. BlueChnl = 0,
  55. }
  56. };
  57. bmfFile.Pages.Add("test-font_0.png");
  58. bmfFile.Characters.Add(new()
  59. {
  60. ID = 70,
  61. X = 34,
  62. Y = 0,
  63. Width = 27,
  64. Height = 20,
  65. XOffset = -5,
  66. YOffset = -3,
  67. XAdvance = 17,
  68. Page = 0,
  69. Chnl = 15
  70. });
  71. bmfFile.Characters.Add(new()
  72. {
  73. ID = 74,
  74. X = 0,
  75. Y = 0,
  76. Width = 28,
  77. Height = 20,
  78. XOffset = -6,
  79. YOffset = -3,
  80. XAdvance = 18,
  81. Page = 0,
  82. Chnl = 15,
  83. });
  84. bmfFile.Kernings.Add(new()
  85. {
  86. First = 70,
  87. Second = 74,
  88. Amount = -1
  89. });
  90. return bmfFile;
  91. }
  92. [Fact]
  93. public void Read_BinaryFile_Test()
  94. {
  95. string path = "BitmapFonts/files/bmfont/test-font-binary.fnt";
  96. using FileStream stream = File.OpenRead(path);
  97. var actual = BitmapFontFileReader.Read(stream, path);
  98. Assert.Equal(_expected.Header, actual.Header);
  99. Assert.Equal(_expected.Info, actual.Info);
  100. Assert.Equal(_expected.Common, actual.Common);
  101. Assert.Equal(_expected.FontName, actual.FontName);
  102. Assert.True(_expected.Pages.SequenceEqual(actual.Pages));
  103. Assert.True(_expected.Characters.SequenceEqual(actual.Characters));
  104. Assert.True(_expected.Kernings.SequenceEqual(actual.Kernings));
  105. }
  106. [Fact]
  107. public void Read_XmlFile_Test()
  108. {
  109. string path = "BitmapFonts/files/bmfont/test-font-xml.fnt";
  110. using FileStream stream = File.OpenRead(path);
  111. var actual = BitmapFontFileReader.Read(stream, path);
  112. Assert.Equal(_expected.Header, actual.Header);
  113. Assert.Equal(_expected.Info, actual.Info);
  114. Assert.Equal(_expected.Common, actual.Common);
  115. Assert.Equal(_expected.FontName, actual.FontName);
  116. Assert.True(_expected.Pages.SequenceEqual(actual.Pages));
  117. Assert.True(_expected.Characters.SequenceEqual(actual.Characters));
  118. Assert.True(_expected.Kernings.SequenceEqual(actual.Kernings));
  119. }
  120. // Issue: MonoGame.Extended won't load XML format .fnt files if they begin with the byte order mark.
  121. // https://github.com/MonoGame-Extended/Monogame-Extended/issues/1073
  122. // It's possible that a consumer might edit the BMFont file using a different library such as SharpFNT.BitmapFont
  123. // which could save it with UTF-8 Byte Order Mark (BOM) preamble at the start of the file.
  124. [Fact]
  125. public void Read_XmlFile_With_UTF8_BOM_Test()
  126. {
  127. string path = "BitmapFonts/files/bmfont/test-font-xml-utf8-bom.fnt";
  128. using FileStream stream = File.OpenRead(path);
  129. var actual = BitmapFontFileReader.Read(stream, path);
  130. Assert.Equal(_expected.Header, actual.Header);
  131. Assert.Equal(_expected.Info, actual.Info);
  132. Assert.Equal(_expected.Common, actual.Common);
  133. Assert.Equal(_expected.FontName, actual.FontName);
  134. Assert.True(_expected.Pages.SequenceEqual(actual.Pages));
  135. Assert.True(_expected.Characters.SequenceEqual(actual.Characters));
  136. Assert.True(_expected.Kernings.SequenceEqual(actual.Kernings));
  137. }
  138. [Fact]
  139. public void Read_Text_Test()
  140. {
  141. string path = "BitmapFonts/files/bmfont/test-font-text.fnt";
  142. using FileStream stream = File.OpenRead(path);
  143. var actual = BitmapFontFileReader.Read(stream, path);
  144. Assert.Equal(_expected.Header, actual.Header);
  145. Assert.Equal(_expected.Info, actual.Info);
  146. Assert.Equal(_expected.Common, actual.Common);
  147. Assert.Equal(_expected.FontName, actual.FontName);
  148. Assert.True(_expected.Pages.SequenceEqual(actual.Pages));
  149. Assert.True(_expected.Characters.SequenceEqual(actual.Characters));
  150. Assert.True(_expected.Kernings.SequenceEqual(actual.Kernings));
  151. }
  152. }