/// <summary> /// This method for loading/saving a font file generated from AngelCode BMFont. /// </summary> /// <param name="serializer">The binary serializer to use.</param> private void SerializeBMFFont(BinarySerializer serializer) { // ---------------------------------------------------------- // Read block Info (1) // ---------------------------------------------------------- byte blockType = 1; serializer.Serialize(ref blockType); if (blockType != 1) { return; } // Skip Info block int expectedBlockSize = 0; serializer.Serialize(ref expectedBlockSize); serializer.Stream.Seek(expectedBlockSize, SeekOrigin.Current); // ---------------------------------------------------------- // Read block Common (2) // ---------------------------------------------------------- serializer.Serialize(ref blockType); if (blockType != 2) { return; } var common = new BMFCommon(); common.Serialize(serializer); // Copy the base offset. BaseOffset = common.Base; LineSpacing = common.LineHeight; // ---------------------------------------------------------- // Read block page names (3) // ---------------------------------------------------------- serializer.Serialize(ref blockType); if (blockType != 3) { return; } serializer.Serialize(ref expectedBlockSize); // Create bitmap array. Bitmaps = new Bitmap[common.PageCount]; for (int i = 0; i < Bitmaps.Length; i++) { string name = null; serializer.Serialize(ref name, true); // Store the name in data Bitmaps[i] = new Bitmap { Data = name }; } // ---------------------------------------------------------- // Read block glyphs (4) // ---------------------------------------------------------- serializer.Serialize(ref blockType); if (blockType != 4) { return; } serializer.Serialize(ref expectedBlockSize); int countChars = expectedBlockSize / 20; var bmfGlyph = new BMFGlyph(); Glyphs = new Glyph[countChars]; for (int i = 0; i < Glyphs.Length; i++) { bmfGlyph.Serialize(serializer); Glyphs[i] = new Glyph { Character = bmfGlyph.Id, Subrect = new Rectangle(bmfGlyph.X, bmfGlyph.Y, bmfGlyph.Width, bmfGlyph.Height), Offset = { X = bmfGlyph.OffsetX, Y = bmfGlyph.OffsetY }, XAdvance = bmfGlyph.AdvanceX, BitmapIndex = bmfGlyph.PageIndex }; } // ---------------------------------------------------------- // Read block kernings (5) optional // ---------------------------------------------------------- if (serializer.Stream.Position < serializer.Stream.Length) { // If there is still some data to read, there is probably some kernings serializer.Serialize(ref blockType); if (blockType != 5) { return; } serializer.Serialize(ref expectedBlockSize); int kernelCount = expectedBlockSize / 10; Kernings = new Kerning[kernelCount]; for (int i = 0; i < Kernings.Length; i++) { serializer.Serialize(ref Kernings[i].First); serializer.Serialize(ref Kernings[i].Second); short offset = 0; serializer.Serialize(ref offset); Kernings[i].Offset = offset; } } }
/// <summary> /// This method for loading/saving a font file generated from AngelCode BMFont. /// </summary> /// <param name="serializer">The binaryserializer to use.</param> private void SerializeBMFFont(BinarySerializer serializer) { // ---------------------------------------------------------- // Read block Info (1) // ---------------------------------------------------------- byte blockType = 1; serializer.Serialize(ref blockType); if (blockType != 1) return; // Skip Info block int expectedBlockSize = 0; serializer.Serialize(ref expectedBlockSize); serializer.Stream.Seek(expectedBlockSize, SeekOrigin.Current); // ---------------------------------------------------------- // Read block Common (2) // ---------------------------------------------------------- serializer.Serialize(ref blockType); if (blockType != 2) return; var common = new BMFCommon(); common.Serialize(serializer); // Copy the base offset. BaseOffset = common.Base; LineSpacing = common.LineHeight; // ---------------------------------------------------------- // Read block page names (3) // ---------------------------------------------------------- serializer.Serialize(ref blockType); if (blockType != 3) return; serializer.Serialize(ref expectedBlockSize); // Create bitmap array. Bitmaps = new Bitmap[common.PageCount]; for (int i = 0; i < Bitmaps.Length; i++) { string name = null; serializer.Serialize(ref name, true); // Store the name in data Bitmaps[i] = new Bitmap { Data = name }; } // ---------------------------------------------------------- // Read block glyphs (4) // ---------------------------------------------------------- serializer.Serialize(ref blockType); if (blockType != 4) return; serializer.Serialize(ref expectedBlockSize); int countChars = expectedBlockSize/20; var bmfGlyph = new BMFGlyph(); Glyphs = new Glyph[countChars]; for (int i = 0; i < Glyphs.Length; i++) { bmfGlyph.Serialize(serializer); Glyphs[i] = new Glyph { Character = bmfGlyph.Id, Subrect = new Rectangle(bmfGlyph.X, bmfGlyph.Y, bmfGlyph.Width, bmfGlyph.Height), Offset = {X = bmfGlyph.OffsetX, Y = bmfGlyph.OffsetY}, XAdvance = bmfGlyph.AdvanceX, BitmapIndex = bmfGlyph.PageIndex }; } // ---------------------------------------------------------- // Read block kernings (5) optional // ---------------------------------------------------------- if (serializer.Stream.Position < serializer.Stream.Length) { // If there is still some data to read, there is probably some kernings serializer.Serialize(ref blockType); if (blockType != 5) return; serializer.Serialize(ref expectedBlockSize); int kernelCount = expectedBlockSize/10; Kernings = new Kerning[kernelCount]; for (int i = 0; i < Kernings.Length; i++) { serializer.Serialize(ref Kernings[i].First); serializer.Serialize(ref Kernings[i].Second); short offset = 0; serializer.Serialize(ref offset); Kernings[i].Offset = offset; } } }