Skip to content

Commit

Permalink
Refactor BitmapFont (#887)
Browse files Browse the repository at this point in the history
* Update file header

* Created initial project to move BitmapFont too

* XMl, Text, and Binar reading now supported for BMFont

* Refactor BittmapFont

* Update content pipeline from BitmapFont refactor

* Update changelog
  • Loading branch information
AristurtleDev committed Jun 11, 2024
1 parent 1f0235c commit 6627756
Show file tree
Hide file tree
Showing 39 changed files with 1,693 additions and 559 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
file_header_template = Copyright (c) Christopher Whitley. All rights reserved.\nLicensed under the MIT license.\nSee LICENSE file in the project root for full license information.
file_header_template = Copyright (c) Craftwork Games. All rights reserved.\nLicensed under the MIT license.\nSee LICENSE file in the project root for full license information.

################################################################################
### Xml files
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated solution (.sln) to Visual Studio 2022 [@nkast](https://github.com/nkast) [#880](https://github.com/craftworkgames/MonoGame.Extended/pull/880)
- Updated project types to .Net.Sdk [@nkast](https://github.com/nkast) [#880](https://github.com/craftworkgames/MonoGame.Extended/pull/880)
- Use `System.IO.Compression.ZLibStream` [@nkast](https://github.com/nkast) [#822](https://github.com/craftworkgames/MonoGame.Extended/pull/882)
- `BitmapFont` refactored. Now supports all three BMFont export types (XML, Text, and Binary). [@AristurtleDev](https://github.com/AristurtleDev) [#877](https://github.com/craftworkgames/MonoGame.Extended/pull/887)

### Removed
- All projects now output build artifacts to a common `.artifacts` directory at the root of the repository. [@AristurtleDev](https://github.com/AristurtleDev) [#865](https://github.com/craftworkgames/MonoGame.Extended/pull/865)
- Dependency on NewtonSoft.Json was completely removed in favor of using dotnet's `System.Text.Json` [@AristurtleDev](https://github.com/AristurtleDev) [#869](https://github.com/craftworkgames/MonoGame.Extended/pull/869)
- Removed `Size3`. It was redundant since `Microsoft.Xna.Framework.Vector3` exists. [@AristurtleDev](https://github.com/AristurtleDev) [#872](https://github.com/craftworkgames/MonoGame.Extended/pull/872)
- Removed `Point3`. It was redundant since `Microsoft.Xna.Framework.Vector3` exists. [@AristurtleDev](https://github.com/AristurtleDev) [#874](https://github.com/craftworkgames/MonoGame.Extended/pull/874)
- Removed `Point2`. It was redundant since `Microsoft.Xna.Framework.Vector2` exists. [@AristurtleDev](https://github.com/AristurtleDev) [#875](https://github.com/craftworkgames/MonoGame.Extended/pull/875)
- Removed unneccessary dependency introduced by previous pull requests [@nkast](https://github.com/nkast) [#881](https://github.com/craftworkgames/MonoGame.Extended/pull/881)
- Removed unnecessary dependency introduced by previous pull requests [@nkast](https://github.com/nkast) [#881](https://github.com/craftworkgames/MonoGame.Extended/pull/881)


### Fixed
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using System.IO;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Content.Pipeline;
using MonoGame.Extended.BitmapFonts;
using MonoGame.Extended.BitmapFonts.BmfTypes;

namespace MonoGame.Extended.Content.Pipeline.BitmapFonts
{
[ContentImporter(".fnt", DefaultProcessor = "BitmapFontProcessor",
DisplayName = "BMFont Importer - MonoGame.Extended")]
public class BitmapFontImporter : ContentImporter<BitmapFontFile>
public class BitmapFontImporter : ContentImporter<ContentImporterResult<BmfFile>>
{
public override BitmapFontFile Import(string filename, ContentImporterContext context)
public override ContentImporterResult<BmfFile> Import(string filename, ContentImporterContext context)
{
context.Logger.LogMessage("Importing XML file: {0}", filename);
context.Logger.LogMessage("Importing FNT file: {0}", filename);

using (var streamReader = new StreamReader(filename))
{
var deserializer = new XmlSerializer(typeof(BitmapFontFile));
return (BitmapFontFile)deserializer.Deserialize(streamReader);
}
using FileStream stream = File.OpenRead(filename);
BmfFile file = BmfFile.FromStream(stream);
return new ContentImporterResult<BmfFile>(filename, file);
}
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
using System;
using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline;
using MonoGame.Extended.BitmapFonts.BmfTypes;

namespace MonoGame.Extended.Content.Pipeline.BitmapFonts
{
[ContentProcessor(DisplayName = "BMFont Processor - MonoGame.Extended")]
public class BitmapFontProcessor : ContentProcessor<BitmapFontFile, BitmapFontProcessorResult>
public class BitmapFontProcessor : ContentProcessor<ContentImporterResult<BmfFile>, BitmapFontProcessorResult>
{
public override BitmapFontProcessorResult Process(BitmapFontFile bitmapFontFile, ContentProcessorContext context)
public override BitmapFontProcessorResult Process(ContentImporterResult<BmfFile> importerResult, ContentProcessorContext context)
{
try
{
BmfFile bmfFile = importerResult.Data;
context.Logger.LogMessage("Processing BMFont");
var result = new BitmapFontProcessorResult(bitmapFontFile);
var result = new BitmapFontProcessorResult(bmfFile);

foreach (var fontPage in bitmapFontFile.Pages)
foreach (var fontPage in bmfFile.Pages)
{
var assetName = Path.GetFileNameWithoutExtension(fontPage.File);
var assetName = Path.GetFileNameWithoutExtension(fontPage);
context.Logger.LogMessage("Expected texture asset: {0}", assetName);
result.TextureAssets.Add(assetName);
}
Expand All @@ -30,4 +32,4 @@ public override BitmapFontProcessorResult Process(BitmapFontFile bitmapFontFile,
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System.Collections.Generic;
using MonoGame.Extended.BitmapFonts.BmfTypes;

namespace MonoGame.Extended.Content.Pipeline.BitmapFonts
{
public class BitmapFontProcessorResult
{
public List<string> TextureAssets { get; private set; }
public BitmapFontFile FontFile { get; private set; }
public BmfFile FontFile { get; private set; }

public BitmapFontProcessorResult(BitmapFontFile fontFile)
public BitmapFontProcessorResult(BmfFile fontFile)
{
FontFile = fontFile;
TextureAssets = new List<string>();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ protected override void Write(ContentWriter writer, BitmapFontProcessorResult re
writer.Write(textureAsset);

var fontFile = result.FontFile;
writer.Write(fontFile.FontName);
writer.Write(fontFile.Info.FontSize);
writer.Write(fontFile.Common.LineHeight);
writer.Write(fontFile.Chars.Count);
writer.Write(fontFile.Characters.Count);

foreach (var c in fontFile.Chars)
foreach (var c in fontFile.Characters)
{
writer.Write(c.Id);
writer.Write(c.ID);
writer.Write(c.Page);
writer.Write(c.X);
writer.Write(c.Y);
Expand Down Expand Up @@ -49,4 +51,4 @@ public override string GetRuntimeReader(TargetPlatform targetPlatform)
return "MonoGame.Extended.BitmapFonts.BitmapFontReader, MonoGame.Extended";
}
}
}
}
10 changes: 5 additions & 5 deletions source/MonoGame.Extended.Graphics/Batcher2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private void EnqueueBuiltGeometry(Texture2D texture, float depth)
var lineSpacing = bitmapFont.LineHeight;
var offset = new Vector2(0, 0);

BitmapFontRegion lastGlyph = null;
BitmapFontCharacter lastGlyph = null;
for (var i = 0; i < text.Length;)
{
int character;
Expand Down Expand Up @@ -253,7 +253,7 @@ private void EnqueueBuiltGeometry(Texture2D texture, float depth)
continue;
}

var fontRegion = bitmapFont.GetCharacterRegion(character);
var fontRegion = bitmapFont.GetCharacter(character);
if (fontRegion == null)
continue;

Expand All @@ -266,7 +266,7 @@ private void EnqueueBuiltGeometry(Texture2D texture, float depth)
DrawSprite(textureRegion.Texture, ref transform1Matrix, ref bounds, color, flags, depth);

var advance = fontRegion.XAdvance + bitmapFont.LetterSpacing;
if (BitmapFont.UseKernings && lastGlyph != null)
if (bitmapFont.UseKernings && lastGlyph != null)
{
int amount;
if (lastGlyph.Kernings.TryGetValue(character, out amount))
Expand All @@ -277,7 +277,7 @@ private void EnqueueBuiltGeometry(Texture2D texture, float depth)

offset.X += i != text.Length - 1
? advance
: fontRegion.XOffset + fontRegion.Width;
: fontRegion.XOffset + fontRegion.TextureRegion.Width;

lastGlyph = fontRegion;
}
Expand Down Expand Up @@ -355,7 +355,7 @@ private void EnqueueBuiltGeometry(Texture2D texture, float depth)
transform1Matrix.M31 += glyph.Position.X;
transform1Matrix.M32 += glyph.Position.Y;

var texture = glyph.FontRegion.TextureRegion.Texture;
var texture = glyph.Character.TextureRegion.Texture;
var bounds = texture.Bounds;
DrawSprite(texture, ref transform1Matrix, ref bounds, color, flags, depth);
}
Expand Down
Loading

0 comments on commit 6627756

Please sign in to comment.