BitmapFontParser/BitmapFont.cs
Download
BitmapFontParser.zip
(500.33 KB | 02 January 2012 )
Source code for a parser for AngelCode BMFont text and XML font formats.
Donate
If this site or its services have saved you time, please consider a donation to help with running costs and timely updates.
Contents of BitmapFontParser.zip/BitmapFontParser/BitmapFont.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
namespace Cyotek.Drawing.BitmapFont
{
public class BitmapFont : IEnumerable<Character>
{
#region Public Member Declarations
public const int NoMaxWidth = -1;
#endregion Public Member Declarations
#region Public Constructors
public BitmapFont()
{ }
#endregion Public Constructors
#region Public Methods
public IEnumerator<Character> GetEnumerator()
{
foreach (KeyValuePair<char, Character> pair in this.Characters)
yield return pair.Value;
}
public int GetKerning(char previous, char current)
{
Kerning key;
int result;
key = new Kerning(previous, current, 0);
if (!this.Kernings.TryGetValue(key, out result))
result = 0;
return result;
}
public Size MeasureFont(string text)
{
return this.MeasureFont(text, BitmapFont.NoMaxWidth);
}
public Size MeasureFont(string text, double maxWidth)
{
char previousCharacter;
string normalizedText;
int currentLineWidth;
int currentLineHeight;
int blockWidth;
int blockHeight;
List<int> lineHeights;
previousCharacter = ' ';
normalizedText = this.NormalizeLineBreaks(text);
currentLineWidth = 0;
currentLineHeight = this.LineHeight;
blockWidth = 0;
blockHeight = 0;
lineHeights = new List<int>();
foreach (char character in normalizedText)
{
switch (character)
{
case '\n':
lineHeights.Add(currentLineHeight);
blockWidth = Math.Max(blockWidth, currentLineWidth);
currentLineWidth = 0;
currentLineHeight = this.LineHeight;
break;
default:
Character data;
int width;
data = this[character];
width = data.XAdvance + this.GetKerning(previousCharacter, character);
if (maxWidth != BitmapFont.NoMaxWidth && currentLineWidth + width >= maxWidth)
{
lineHeights.Add(currentLineHeight);
blockWidth = Math.Max(blockWidth, currentLineWidth);
currentLineWidth = 0;
currentLineHeight = this.LineHeight;
}
currentLineWidth += width;
currentLineHeight = Math.Max(currentLineHeight, data.Bounds.Height + data.Offset.Y);
previousCharacter = character;
break;
}
}
// finish off the current line if required
if (currentLineHeight != 0)
lineHeights.Add(currentLineHeight);
// reduce any lines other than the last back to the base
for (int i = 0; i < lineHeights.Count - 1; i++)
lineHeights[i] = this.LineHeight;
// calculate the final block height
foreach (int lineHeight in lineHeights)
blockHeight += lineHeight;
return new Size(Math.Max(currentLineWidth, blockWidth), blockHeight);
}
public string NormalizeLineBreaks(string s)
{
return s.Replace("\r\n", "\n").Replace("\r", "\n");
}
#endregion Public Methods
#region Public Properties
public int AlphaChannel { get; set; }
public int BaseHeight { get; set; }
public int BlueChannel { get; set; }
public bool Bold { get; set; }
public IDictionary<char, Character> Characters { get; set; }
public string Charset { get; set; }
public string FamilyName { get; set; }
public int FontSize { get; set; }
public int GreenChannel { get; set; }
public bool Italic { get; set; }
public IDictionary<Kerning, int> Kernings { get; set; }
public int LineHeight { get; set; }
public int OutlineSize { get; set; }
public bool Packed { get; set; }
public Padding Padding { get; set; }
public Page[] Pages { get; set; }
public int RedChannel { get; set; }
public bool Smoothed { get; set; }
public Point Spacing { get; set; }
public int StretchedHeight { get; set; }
public int SuperSampling { get; set; }
public Size TextureSize { get; set; }
public Character this[char character]
{ get { return this.Characters[character]; } }
public bool Unicode { get; set; }
#endregion Public Properties
#region Private Methods
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion Private Methods
}
}
Files
- BitmapFontParser
-
BitmapFontViewer
- Properties
- Resources
-
samples
- arial-32bi.bmfc
- arial-32bi.fnt
- arial-32bi.xml.fnt
- arial-32bi.xml_0.png
- arial-32bi.xml_1.png
- arial-32bi.xml_2.png
- arial-32bi.xml_3.png
- arial-32bi_0.png
- arial-32bi_1.png
- arial-32bi_2.png
- arial-32bi_3.png
- pixelmix-32.bmfc
- pixelmix-32.fnt
- pixelmix-32_0.png
- resource-32.bmfc
- resource-32.fnt
- resource-32_0.png
- treasuremap-32.bmfc
- treasuremap-32.fnt
- treasuremap-32_0.png
- treasuremap-32b.fnt
- treasuremap-32b_0.png
- treasuremap-48.bmfc
- treasuremap-48.fnt
- treasuremap-48_0.png
- treasuremap-48b.fnt
- treasuremap-48b.xml.fnt
- treasuremap-48b.xml_0.png
- treasuremap-48b_0.png
- BitmapFontViewer.csproj
- BitmapFontViewer.sln
- Icon.ico
- ImageBox.cs
- ImageBox.designer.cs
- ImageBoxBorderStyle.cs
- ImageBoxGridDisplayMode.cs
- ImageBoxGridScale.cs
- MainForm.cs
- MainForm.Designer.cs
- MainForm.resx
- Program.cs
- PropertyGrid.cs
