59 lines
1.3 KiB
C#
59 lines
1.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace MoonWorks.Graphics.Font.MSDF;
|
|
|
|
public class Font : IDisposable
|
|
{
|
|
public Texture Texture { get; }
|
|
internal AtlasData AtlasData;
|
|
|
|
static JsonSerializerOptions SerializerOptions = new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
IncludeFields = true
|
|
};
|
|
static AtlasDataContext Context = new AtlasDataContext(SerializerOptions);
|
|
|
|
private bool IsDisposed;
|
|
|
|
public static Font Load(
|
|
GraphicsDevice graphicsDevice,
|
|
CommandBuffer commandBuffer,
|
|
string jsonPath
|
|
) {
|
|
var atlasData = (AtlasData) JsonSerializer.Deserialize(File.ReadAllText(jsonPath), typeof(AtlasData), Context);
|
|
var imagePath = Path.ChangeExtension(jsonPath, ".png");
|
|
var texture = Texture.FromImageFile(graphicsDevice, commandBuffer, imagePath);
|
|
|
|
return new Font(texture, atlasData);
|
|
}
|
|
|
|
private Font(Texture texture, AtlasData atlasData)
|
|
{
|
|
Texture = texture;
|
|
AtlasData = atlasData;
|
|
}
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (!IsDisposed)
|
|
{
|
|
if (disposing)
|
|
{
|
|
Texture.Dispose();
|
|
}
|
|
|
|
IsDisposed = true;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
Dispose(disposing: true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|