54 lines
1.3 KiB
C#
54 lines
1.3 KiB
C#
|
|
using System.Numerics;
|
|
using FreeTypeSharp;
|
|
using Voile.VFS;
|
|
|
|
using static FreeTypeSharp.FT;
|
|
using static FreeTypeSharp.FT_LOAD;
|
|
|
|
namespace Voile.Resources;
|
|
|
|
public class FontLoader : ResourceLoader<Font>
|
|
{
|
|
public override IEnumerable<string> SupportedExtensions => new string[]
|
|
{
|
|
".ttf"
|
|
};
|
|
|
|
protected override Font LoadResource(string path)
|
|
{
|
|
using Stream stream = VirtualFileSystem.Read(path);
|
|
|
|
byte[] fileBuffer = new byte[stream.Length];
|
|
int bytesRead = stream.Read(fileBuffer, 0, fileBuffer.Length);
|
|
var result = new Font(path, fileBuffer);
|
|
|
|
result.BufferSize = bytesRead;
|
|
|
|
LoadFaceData(result);
|
|
result.LoadAsciiData();
|
|
|
|
return result;
|
|
}
|
|
|
|
private unsafe void LoadFaceData(Font font)
|
|
{
|
|
FT_LibraryRec_* lib;
|
|
FT_Error error = FT_Init_FreeType(&lib);
|
|
if (error != 0)
|
|
throw new Exception("Failed to init FreeType");
|
|
|
|
font.LibraryPtr = (nint)lib;
|
|
|
|
fixed (byte* data = font.Buffer)
|
|
{
|
|
FT_FaceRec_* face;
|
|
error = FT_New_Memory_Face(lib, data, (nint)font.BufferSize, 0, &face);
|
|
if (error != 0)
|
|
throw new Exception("Failed to load font face");
|
|
|
|
font.UnitsPerEm = face->units_per_EM;
|
|
font.FacePtr = (nint)face;
|
|
}
|
|
}
|
|
} |