Skip to content

Commit

Permalink
Initial release.
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraBertaOldham committed Apr 21, 2018
1 parent 1d3a207 commit 902ced2
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SharpFNT

## Introduction
SharpFNT is a C# library for reading and writing [Angel Code bitmap fonts](http://www.angelcode.com/products/bmfont/). The binary, XML, and text formats are all supported. This is still a work in progress and is not ready for use. This will be available on Nuget later.
SharpFNT is a C# library for reading and writing [Angel Code bitmap fonts](http://www.angelcode.com/products/bmfont/) in binary, XML and text. It can be downloaded on [Nuget](https://www.nuget.org/packages/SharpFNT/).

## Possible Future Additions
1. A class for creating the character rectangles for rendering and measuring.
Expand Down
44 changes: 30 additions & 14 deletions SharpFNT/BitmapFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ public void Save(string path, FormatHint formatHint)

case FormatHint.XML:
{
using (XmlWriter xmlWriter = XmlWriter.Create(fileStream))
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = " "
};

using (XmlWriter xmlWriter = XmlWriter.Create(fileStream, settings))
{
this.WriteXML(xmlWriter);
}
Expand Down Expand Up @@ -135,13 +141,16 @@ public void WriteXML(XmlWriter xmlWriter)
{
XDocument document = new XDocument();

XElement fontElement = new XElement("font");
document.Add(fontElement);

// Info

if (this.Info != null)
{
XElement infoElement = new XElement("info");
this.Info.WriteXML(infoElement);
document.Add(infoElement);
fontElement.Add(infoElement);
}

// Common
Expand All @@ -150,7 +159,7 @@ public void WriteXML(XmlWriter xmlWriter)
{
XElement commonElement = new XElement("common");
this.Common.WriteXML(commonElement, this.Pages.Count);
document.Add(commonElement);
fontElement.Add(commonElement);
}

// Pages
Expand All @@ -167,7 +176,7 @@ public void WriteXML(XmlWriter xmlWriter)
pagesElement.Add(pageElement);
}

document.Add(pagesElement);
fontElement.Add(pagesElement);
}

// Characters
Expand All @@ -184,7 +193,7 @@ public void WriteXML(XmlWriter xmlWriter)
charactersElement.Add(characterElement);
}

document.Add(charactersElement);
fontElement.Add(charactersElement);
}

// Kernings
Expand All @@ -201,7 +210,7 @@ public void WriteXML(XmlWriter xmlWriter)
kerningsElement.Add(kerningElement);
}

document.Add(kerningsElement);
fontElement.Add(kerningsElement);
}

document.WriteTo(xmlWriter);
Expand All @@ -216,14 +225,16 @@ public void WriteText(TextWriter textWriter)
{
stringBuilder.Append("info");
this.Info.WriteText(stringBuilder);
stringBuilder.AppendLine();
}

// Common

if (this.Common != null)
{
stringBuilder.AppendLine("common");
stringBuilder.Append("common");
this.Common.WriteText(stringBuilder, this.Pages.Count);
stringBuilder.AppendLine();
}

// Pages
Expand All @@ -232,23 +243,26 @@ public void WriteText(TextWriter textWriter)
{
foreach (KeyValuePair<int, string> page in this.Pages)
{
stringBuilder.AppendLine("page");
stringBuilder.Append("page");
TextFormatUtility.WriteInt("id", page.Key, stringBuilder);
TextFormatUtility.WriteString("id", page.Value, stringBuilder);
TextFormatUtility.WriteString("file", page.Value, stringBuilder);
stringBuilder.AppendLine();
}
}

// Characters

if (this.Characters != null)
{
stringBuilder.AppendLine("chars");
stringBuilder.Append("chars");
TextFormatUtility.WriteInt("count", this.Characters.Count, stringBuilder);
stringBuilder.AppendLine();

foreach (Character character in this.Characters.Values)
{
stringBuilder.AppendLine("char");
stringBuilder.Append("char");
character.WriteText(stringBuilder);
stringBuilder.AppendLine();
}

}
Expand All @@ -257,13 +271,15 @@ public void WriteText(TextWriter textWriter)

if (this.KerningPairs != null && this.KerningPairs.Count > 0)
{
stringBuilder.AppendLine("kernings");
stringBuilder.Append("kernings");
TextFormatUtility.WriteInt("count", this.KerningPairs.Count, stringBuilder);
stringBuilder.AppendLine();

foreach (KerningPair kerningPair in this.KerningPairs.Keys)
{
stringBuilder.AppendLine("kerning");
stringBuilder.Append("kerning");
kerningPair.WriteText(stringBuilder);
stringBuilder.AppendLine();
}

textWriter.Write(stringBuilder);
Expand Down Expand Up @@ -321,7 +337,7 @@ public static BitmapFont ReadBinary(BinaryReader binaryReader)

int pageCount = 32;

while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
while (binaryReader.PeekChar() != -1)
{
BlockID blockId = (BlockID)binaryReader.ReadByte();

Expand Down
7 changes: 4 additions & 3 deletions SharpFNT/BitmapFontCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// This code is licensed under MIT.
// ****************************************************************************

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -35,7 +36,7 @@ public void WriteBinary(BinaryWriter binaryWriter, int pages)
binaryWriter.Write((ushort)pages);

byte packed = 0;
packed = packed.SetBit(7, this.Packed);
packed = packed.SetBit(0, this.Packed);
binaryWriter.Write(packed);

binaryWriter.Write((byte)this.AlphaChannel);
Expand All @@ -52,7 +53,7 @@ public void WriteXML(XElement element, int pages)

element.SetAttributeValue("pages", pages);

element.SetAttributeValue("packed", this.Packed);
element.SetAttributeValue("packed", Convert.ToInt32(this.Packed));

element.SetAttributeValue("alphaChnl", (int)this.AlphaChannel);
element.SetAttributeValue("redChnl", (int)this.RedChannel);
Expand Down Expand Up @@ -92,7 +93,7 @@ public static BitmapFontCommon ReadBinary(BinaryReader binaryReader, out int pag

pageCount = binaryReader.ReadUInt16();

binary.Packed = binaryReader.ReadByte().IsBitSet(7);
binary.Packed = binaryReader.ReadByte().IsBitSet(0);
binary.AlphaChannel = (ChannelData)binaryReader.ReadByte();
binary.RedChannel = (ChannelData)binaryReader.ReadByte();
binary.GreenChannel = (ChannelData)binaryReader.ReadByte();
Expand Down
33 changes: 17 additions & 16 deletions SharpFNT/BitmapFontInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public void WriteBinary(BinaryWriter binaryWriter)

byte bitField = 0;

bitField = bitField.SetBit(0, this.Smooth);
bitField = bitField.SetBit(1, this.Unicode);
bitField = bitField.SetBit(2, this.Italic);
bitField = bitField.SetBit(3, this.Bold);
bitField = bitField.SetBit(7, this.Smooth);
bitField = bitField.SetBit(6, this.Unicode);
bitField = bitField.SetBit(5, this.Italic);
bitField = bitField.SetBit(4, this.Bold);

binaryWriter.Write(bitField);

Expand All @@ -73,20 +73,20 @@ public void WriteXML(XElement element)
{
element.SetAttributeValue("face", this.Face);
element.SetAttributeValue("size", this.Size);
element.SetAttributeValue("bold", this.Bold);
element.SetAttributeValue("italic", this.Italic);
element.SetAttributeValue("bold", Convert.ToInt32(this.Bold));
element.SetAttributeValue("italic", Convert.ToInt32(this.Italic));

element.SetAttributeValue("charset", this.Charset);

element.SetAttributeValue("unicode", this.Unicode);
element.SetAttributeValue("unicode", Convert.ToInt32(this.Unicode));
element.SetAttributeValue("stretchH", this.StretchHeight);
element.SetAttributeValue("smooth", this.Smooth);
element.SetAttributeValue("smooth", Convert.ToInt32(this.Smooth));
element.SetAttributeValue("aa", this.SuperSamplingLevel);

string padding = $"{this.PaddingUp}, {this.PaddingRight}, {this.PaddingDown}, {this.PaddingLeft}";
string padding = $"{this.PaddingUp},{this.PaddingRight},{this.PaddingDown},{this.PaddingLeft}";
element.SetAttributeValue("padding", padding);

string spacing = $"{this.SpacingHorizontal}, {this.SpacingVertical}";
string spacing = $"{this.SpacingHorizontal},{this.SpacingVertical}";
element.SetAttributeValue("spacing", spacing);

element.SetAttributeValue("outline", this.Outline);
Expand All @@ -102,12 +102,13 @@ public void WriteText(StringBuilder stringBuilder)

TextFormatUtility.WriteBool("unicode", this.Unicode, stringBuilder);
TextFormatUtility.WriteInt("stretchH", this.StretchHeight, stringBuilder);
TextFormatUtility.WriteBool("smooth", this.Smooth, stringBuilder);
TextFormatUtility.WriteInt("aa", this.SuperSamplingLevel, stringBuilder);

string padding = $"{this.PaddingUp}, {this.PaddingRight}, {this.PaddingDown}, {this.PaddingLeft}";
string padding = $"{this.PaddingUp},{this.PaddingRight},{this.PaddingDown},{this.PaddingLeft}";
TextFormatUtility.WriteValue("padding", padding, stringBuilder);

string spacing = $"{this.SpacingHorizontal}, {this.SpacingVertical}";
string spacing = $"{this.SpacingHorizontal},{this.SpacingVertical}";
TextFormatUtility.WriteValue("spacing", spacing, stringBuilder);

TextFormatUtility.WriteInt("outline", this.Outline, stringBuilder);
Expand All @@ -126,10 +127,10 @@ public static BitmapFontInfo ReadBinary(BinaryReader binaryReader)

byte bitField = binaryReader.ReadByte();

bitmapFontInfo.Smooth = bitField.IsBitSet(0);
bitmapFontInfo.Unicode = bitField.IsBitSet(1);
bitmapFontInfo.Italic = bitField.IsBitSet(2);
bitmapFontInfo.Bold = bitField.IsBitSet(3);
bitmapFontInfo.Smooth = bitField.IsBitSet(7);
bitmapFontInfo.Unicode = bitField.IsBitSet(6);
bitmapFontInfo.Italic = bitField.IsBitSet(5);
bitmapFontInfo.Bold = bitField.IsBitSet(4);

CharacterSet characterSet = (CharacterSet)binaryReader.ReadByte();
bitmapFontInfo.Charset = characterSet.ToString().ToUpper();
Expand Down
10 changes: 7 additions & 3 deletions SharpFNT/SharpFNT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Authors>Todd Berta-Oldham</Authors>
<Company>Todd Berta-Oldham</Company>
<Company></Company>
<NeutralLanguage>en</NeutralLanguage>
<Description>SharpFNT is a C# library for reading Angel Code bitmap fonts.</Description>
<Version>0.0.1</Version>
<Description>SharpFNT is a C# library for reading and writing Angel Code bitmap fonts in binary, XML and text.</Description>
<Version>1.0.0</Version>
<PackageLicenseUrl>https://github.com/ToddBertaOldham/SharpFNT/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/ToddBertaOldham/SharpFNT</PackageProjectUrl>
<RepositoryUrl>https://github.com/ToddBertaOldham/SharpFNT</RepositoryUrl>
<Copyright>Copyright © Todd Berta-Oldham 2018</Copyright>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Initial release.</PackageReleaseNotes>
<PackageTags>fnt;bitmap;font;bmfont</PackageTags>
</PropertyGroup>

</Project>
Binary file modified TestApp/Binary.fnt
Binary file not shown.
4 changes: 2 additions & 2 deletions TestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ private static void Main()
PrintKerningPairs(bitmapFont);
break;
case ConsoleKey.S:
PrintPropertyValues(bitmapFont.Info);
PrintInfo(bitmapFont);
break;
case ConsoleKey.D:
PrintPropertyValues(bitmapFont.Common);
PrintCommon(bitmapFont);
break;
case ConsoleKey.F:
PrintPages(bitmapFont);
Expand Down
5 changes: 4 additions & 1 deletion TestApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Resources;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -34,3 +35,5 @@
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: NeutralResourcesLanguage("en")]

0 comments on commit 902ced2

Please sign in to comment.