Use NuGet package instead of copy-pasted code in BizwareGL/Borrowed
Our copy was equivalent to version 1.0.0 + a patch which allowed Stream instead of requiring a filename, so I've used 1.0.2 as it has the author's implementation of the same feature. There are more recent versions available (note, 1.0.2 isn't for .NET Core). NuGet: https://www.nuget.org/packages/Cyotek.Drawing.BitmapFont Project repo: https://github.com/cyotek/Cyotek.Drawing.BitmapFont
This commit is contained in:
parent
a5fd5f5d78
commit
1e37350e99
|
@ -7,6 +7,7 @@
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Cyotek.Drawing.BitmapFont" Version="1.0.2" PrivateAssets="all" NoWarn="NU1701" />
|
||||||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /><!-- for (extra?) dynamic type support -->
|
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /><!-- for (extra?) dynamic type support -->
|
||||||
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
|
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
|
||||||
<PackageReference Include="OpenTK" Version="3.3.0" NoWarn="NU1701" />
|
<PackageReference Include="OpenTK" Version="3.3.0" NoWarn="NU1701" />
|
||||||
|
|
|
@ -1,71 +0,0 @@
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Drawing;
|
|
||||||
|
|
||||||
// public domain assumed from cyotek.com
|
|
||||||
namespace Cyotek.Drawing.BitmapFont
|
|
||||||
{
|
|
||||||
public class BitmapFont : IEnumerable<Character>
|
|
||||||
{
|
|
||||||
public IEnumerator<Character> GetEnumerator()
|
|
||||||
{
|
|
||||||
foreach (KeyValuePair<char, Character> pair in Characters)
|
|
||||||
{
|
|
||||||
yield return pair.Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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] => Characters[character];
|
|
||||||
|
|
||||||
public bool Unicode { get; set; }
|
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator()
|
|
||||||
{
|
|
||||||
return GetEnumerator();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,162 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Drawing;
|
|
||||||
using System.IO;
|
|
||||||
using System.Xml;
|
|
||||||
|
|
||||||
// public domain assumed from cyotek.com
|
|
||||||
namespace Cyotek.Drawing.BitmapFont
|
|
||||||
{
|
|
||||||
// Parsing class for bitmap fonts generated by AngelCode BMFont
|
|
||||||
// http://www.angelcode.com/products/bmfont/
|
|
||||||
|
|
||||||
public static class BitmapFontLoader
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Loads a bitmap font from an XML file.
|
|
||||||
/// </summary>
|
|
||||||
public static BitmapFont LoadFontFromXmlFile(Stream stream)
|
|
||||||
{
|
|
||||||
var document = new XmlDocument();
|
|
||||||
IDictionary<int, Page> pageData = new SortedDictionary<int, Page>();
|
|
||||||
IDictionary<Kerning, int> kerningDictionary = new Dictionary<Kerning, int>();
|
|
||||||
IDictionary<char, Character> charDictionary = new Dictionary<char, Character>();
|
|
||||||
var font = new BitmapFont();
|
|
||||||
|
|
||||||
document.Load(stream);
|
|
||||||
XmlNode root = document.DocumentElement;
|
|
||||||
|
|
||||||
// load the basic attributes
|
|
||||||
var properties = root.SelectSingleNode("info");
|
|
||||||
font.FamilyName = properties.Attributes["face"].Value;
|
|
||||||
font.FontSize = Convert.ToInt32(properties.Attributes["size"].Value);
|
|
||||||
font.Bold = Convert.ToInt32(properties.Attributes["bold"].Value) != 0;
|
|
||||||
font.Italic = Convert.ToInt32(properties.Attributes["italic"].Value) != 0;
|
|
||||||
font.Unicode = Convert.ToInt32(properties.Attributes["unicode"].Value) != 0;
|
|
||||||
font.StretchedHeight = Convert.ToInt32(properties.Attributes["stretchH"].Value);
|
|
||||||
font.Charset = properties.Attributes["charset"].Value;
|
|
||||||
font.Smoothed = Convert.ToInt32(properties.Attributes["smooth"].Value) != 0;
|
|
||||||
font.SuperSampling = Convert.ToInt32(properties.Attributes["aa"].Value);
|
|
||||||
font.Padding = ParsePadding(properties.Attributes["padding"].Value);
|
|
||||||
font.Spacing = ParsePoint(properties.Attributes["spacing"].Value);
|
|
||||||
font.OutlineSize = Convert.ToInt32(properties.Attributes["outline"].Value);
|
|
||||||
|
|
||||||
// common attributes
|
|
||||||
properties = root.SelectSingleNode("common");
|
|
||||||
font.BaseHeight = Convert.ToInt32(properties.Attributes["lineHeight"].Value);
|
|
||||||
font.LineHeight = Convert.ToInt32(properties.Attributes["base"].Value);
|
|
||||||
font.TextureSize = new Size
|
|
||||||
(
|
|
||||||
Convert.ToInt32(properties.Attributes["scaleW"].Value),
|
|
||||||
Convert.ToInt32(properties.Attributes["scaleH"].Value)
|
|
||||||
);
|
|
||||||
font.Packed = Convert.ToInt32(properties.Attributes["packed"].Value) != 0;
|
|
||||||
font.AlphaChannel = Convert.ToInt32(properties.Attributes["alphaChnl"].Value);
|
|
||||||
font.RedChannel = Convert.ToInt32(properties.Attributes["redChnl"].Value);
|
|
||||||
font.GreenChannel = Convert.ToInt32(properties.Attributes["greenChnl"].Value);
|
|
||||||
font.BlueChannel = Convert.ToInt32(properties.Attributes["blueChnl"].Value);
|
|
||||||
|
|
||||||
// load texture information
|
|
||||||
foreach (XmlNode node in root.SelectNodes("pages/page"))
|
|
||||||
{
|
|
||||||
var page = new Page
|
|
||||||
{
|
|
||||||
Id = Convert.ToInt32(node.Attributes["id"].Value),
|
|
||||||
FileName = node.Attributes["file"].Value
|
|
||||||
};
|
|
||||||
|
|
||||||
pageData.Add(page.Id, page);
|
|
||||||
}
|
|
||||||
font.Pages = ToArray(pageData.Values);
|
|
||||||
|
|
||||||
// load character information
|
|
||||||
foreach (XmlNode node in root.SelectNodes("chars/char"))
|
|
||||||
{
|
|
||||||
var character = new Character
|
|
||||||
{
|
|
||||||
Char = (char) Convert.ToInt32(node.Attributes["id"].Value),
|
|
||||||
Bounds = new Rectangle
|
|
||||||
(
|
|
||||||
Convert.ToInt32(node.Attributes["x"].Value),
|
|
||||||
Convert.ToInt32(node.Attributes["y"].Value),
|
|
||||||
Convert.ToInt32(node.Attributes["width"].Value),
|
|
||||||
Convert.ToInt32(node.Attributes["height"].Value)
|
|
||||||
),
|
|
||||||
Offset = new Point
|
|
||||||
(
|
|
||||||
Convert.ToInt32(node.Attributes["xoffset"].Value),
|
|
||||||
Convert.ToInt32(node.Attributes["yoffset"].Value)
|
|
||||||
),
|
|
||||||
XAdvance = Convert.ToInt32(node.Attributes["xadvance"].Value),
|
|
||||||
TexturePage = Convert.ToInt32(node.Attributes["page"].Value),
|
|
||||||
Channel = Convert.ToInt32(node.Attributes["chnl"].Value)
|
|
||||||
};
|
|
||||||
|
|
||||||
charDictionary.Add(character.Char, character);
|
|
||||||
}
|
|
||||||
|
|
||||||
font.Characters = charDictionary;
|
|
||||||
|
|
||||||
// loading kerning information
|
|
||||||
foreach (XmlNode node in root.SelectNodes("kernings/kerning"))
|
|
||||||
{
|
|
||||||
var key = new Kerning((char)Convert.ToInt32(node.Attributes["first"].Value), (char)Convert.ToInt32(node.Attributes["second"].Value), Convert.ToInt32(node.Attributes["amount"].Value));
|
|
||||||
|
|
||||||
if (!kerningDictionary.ContainsKey(key))
|
|
||||||
{
|
|
||||||
kerningDictionary.Add(key, key.Amount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
font.Kernings = kerningDictionary;
|
|
||||||
|
|
||||||
return font;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a Padding object from a string representation
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="s">The string.</param>
|
|
||||||
private static Padding ParsePadding(string s)
|
|
||||||
{
|
|
||||||
var parts = s.Split(',');
|
|
||||||
|
|
||||||
return new Padding
|
|
||||||
{
|
|
||||||
Left = Convert.ToInt32(parts[3].Trim()),
|
|
||||||
Top = Convert.ToInt32(parts[0].Trim()),
|
|
||||||
Right = Convert.ToInt32(parts[1].Trim()),
|
|
||||||
Bottom = Convert.ToInt32(parts[2].Trim())
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a Point object from a string representation
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="s">The string.</param>
|
|
||||||
private static Point ParsePoint(string s)
|
|
||||||
{
|
|
||||||
var parts = s.Split(',');
|
|
||||||
|
|
||||||
return new Point
|
|
||||||
{
|
|
||||||
X = Convert.ToInt32(parts[0].Trim()),
|
|
||||||
Y = Convert.ToInt32(parts[1].Trim())
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts the given collection into an array
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">Type of the items in the array</typeparam>
|
|
||||||
/// <param name="values">The values.</param>
|
|
||||||
private static T[] ToArray<T>(ICollection<T> values)
|
|
||||||
{
|
|
||||||
// avoid a forced .NET 3 dependency just for one call to Linq
|
|
||||||
var result = new T[values.Count];
|
|
||||||
values.CopyTo(result, 0);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,26 +0,0 @@
|
||||||
//public domain assumed from cyotek.com
|
|
||||||
|
|
||||||
using System.Drawing;
|
|
||||||
|
|
||||||
namespace Cyotek.Drawing.BitmapFont
|
|
||||||
{
|
|
||||||
public struct Character
|
|
||||||
{
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return this.Char.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Channel { get; set; }
|
|
||||||
|
|
||||||
public Rectangle Bounds { get; set; }
|
|
||||||
|
|
||||||
public Point Offset { get; set; }
|
|
||||||
|
|
||||||
public char Char { get; set; }
|
|
||||||
|
|
||||||
public int TexturePage { get; set; }
|
|
||||||
|
|
||||||
public int XAdvance { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,26 +0,0 @@
|
||||||
//public domain assumed from cyotek.com
|
|
||||||
|
|
||||||
namespace Cyotek.Drawing.BitmapFont
|
|
||||||
{
|
|
||||||
public struct Kerning
|
|
||||||
{
|
|
||||||
public Kerning(char firstCharacter, char secondCharacter, int amount)
|
|
||||||
: this()
|
|
||||||
{
|
|
||||||
this.FirstCharacter = firstCharacter;
|
|
||||||
this.SecondCharacter = secondCharacter;
|
|
||||||
this.Amount = amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return string.Format("{0} to {1} = {2}", this.FirstCharacter, this.SecondCharacter, this.Amount);
|
|
||||||
}
|
|
||||||
|
|
||||||
public char FirstCharacter { get; set; }
|
|
||||||
|
|
||||||
public char SecondCharacter { get; set; }
|
|
||||||
|
|
||||||
public int Amount { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,29 +0,0 @@
|
||||||
//public domain assumed from cyotek.com
|
|
||||||
|
|
||||||
namespace Cyotek.Drawing.BitmapFont
|
|
||||||
{
|
|
||||||
public struct Padding
|
|
||||||
{
|
|
||||||
public Padding(int left, int top, int right, int bottom)
|
|
||||||
: this()
|
|
||||||
{
|
|
||||||
this.Top = top;
|
|
||||||
this.Left = left;
|
|
||||||
this.Right = right;
|
|
||||||
this.Bottom = bottom;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return string.Format("{0}, {1}, {2}, {3}", this.Left, this.Top, this.Right, this.Bottom);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Top { get; set; }
|
|
||||||
|
|
||||||
public int Left { get; set; }
|
|
||||||
|
|
||||||
public int Right { get; set; }
|
|
||||||
|
|
||||||
public int Bottom { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
// public domain assumed from cyotek.com
|
|
||||||
namespace Cyotek.Drawing.BitmapFont
|
|
||||||
{
|
|
||||||
public struct Page
|
|
||||||
{
|
|
||||||
public Page(int id, string fileName)
|
|
||||||
: this()
|
|
||||||
{
|
|
||||||
FileName = fileName;
|
|
||||||
Id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return $"{Id} ({Path.GetFileName(FileName)})";
|
|
||||||
}
|
|
||||||
|
|
||||||
public string FileName { get; set; }
|
|
||||||
|
|
||||||
public int Id { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +0,0 @@
|
||||||
BitmapFontParser
|
|
||||||
http://cyotek.com/blog/angelcode-bitmap-font-parsing-using-csharp
|
|
||||||
License not stated. I'm content with calling it public domain. As future work, the code could be replaced easily, so I don't consider this a big deal. It's most trivial code.
|
|
||||||
It loads fonts created by http://www.angelcode.com/products/bmfont/
|
|
|
@ -14,7 +14,8 @@ namespace BizHawk.Bizware.BizwareGL
|
||||||
public StringRenderer(IGL owner, Stream xml, params Stream[] textures)
|
public StringRenderer(IGL owner, Stream xml, params Stream[] textures)
|
||||||
{
|
{
|
||||||
Owner = owner;
|
Owner = owner;
|
||||||
FontInfo = BitmapFontLoader.LoadFontFromXmlFile(xml);
|
FontInfo = new();
|
||||||
|
FontInfo.LoadXml(xml);
|
||||||
|
|
||||||
// load textures
|
// load textures
|
||||||
for(int i=0;i<FontInfo.Pages.Length;i++)
|
for(int i=0;i<FontInfo.Pages.Length;i++)
|
||||||
|
|
Loading…
Reference in New Issue