From 17196c94373c5626a399423799a099c049b450d1 Mon Sep 17 00:00:00 2001 From: dnesov Date: Wed, 25 Jun 2025 23:20:46 +0200 Subject: [PATCH] WIP: load fonts with FreeType. --- Voile/Source/Resources/Font.cs | 33 +++++++++++ Voile/Source/Resources/Loaders/FontLoader.cs | 59 +++++++++++++++++++- Voile/Voile.csproj | 3 +- 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/Voile/Source/Resources/Font.cs b/Voile/Source/Resources/Font.cs index 3b0e454..9e6a998 100644 --- a/Voile/Source/Resources/Font.cs +++ b/Voile/Source/Resources/Font.cs @@ -1,5 +1,18 @@ +using System.Numerics; + namespace Voile; +public struct Glyph +{ + public int TextureId { get; set; } = -1; + public float Width { get; set; } + public float Height { get; set; } + public Vector2 Bearing { get; set; } + public int Advance { get; set; } + + public Glyph() { } +} + /// /// Represents font data. /// @@ -18,4 +31,24 @@ public class Font : Resource { Buffer = buffer; } + + public void Measure(string text) + { + foreach (char c in text) + { + + } + } + + internal void AddGlyph(char c, Glyph glyph) + { + _glyphs.Add(c, glyph); + } + + internal void GetGlyphBoundingBox(Glyph glyph) + { + + } + + private Dictionary _glyphs = new(); } \ No newline at end of file diff --git a/Voile/Source/Resources/Loaders/FontLoader.cs b/Voile/Source/Resources/Loaders/FontLoader.cs index 5ea6854..5bf60c2 100644 --- a/Voile/Source/Resources/Loaders/FontLoader.cs +++ b/Voile/Source/Resources/Loaders/FontLoader.cs @@ -1,6 +1,13 @@ +using System.Numerics; +using System.Runtime.InteropServices; +using FreeTypeSharp; using Voile.VFS; +using static FreeTypeSharp.FT; +using static FreeTypeSharp.FT_LOAD; +using static FreeTypeSharp.FT_Render_Mode_; + namespace Voile.Resources; public class FontLoader : ResourceLoader @@ -10,7 +17,6 @@ public class FontLoader : ResourceLoader ".ttf" }; - protected override Font LoadResource(string path) { using Stream stream = VirtualFileSystem.Read(path); @@ -21,6 +27,57 @@ public class FontLoader : ResourceLoader result.BufferSize = bytesRead; + LoadFaceData(result); + return result; } + + private unsafe void LoadFaceData(Font font) + { + LoadFreeType(); + + fixed (FT_LibraryRec_** lib = &_lib) + { + fixed (FT_FaceRec_* face = &_face) + { + FT_Error error; + + var buffer = new Memory(font.Buffer); + var handle = buffer.Pin(); + + error = FT_New_Memory_Face(*lib, (byte*)handle.Pointer, (nint)font.BufferSize, 0, &face); + error = FT_Set_Pixel_Sizes(face, 0, (uint)font.Size); + + error = FT_Load_Char(face, 'F', FT_LOAD_NO_BITMAP); + + var metrics = face->glyph->metrics; + + font.AddGlyph('F', new Glyph() + { + Width = metrics.width >> 6, + Height = metrics.height >> 6, + Bearing = new Vector2(metrics.horiBearingX >> 6, metrics.horiBearingY >> 6), + Advance = (int)metrics.horiAdvance >> 6, + }); + + FT_Done_Face(face); + FT_Done_FreeType(*lib); + } + } + } + + private unsafe void LoadFreeType() + { + fixed (FT_LibraryRec_** lib = &_lib) + { + FT_Error error; + if (_lib == null) + { + error = FT_Init_FreeType(lib); + } + } + } + + private unsafe FT_LibraryRec_* _lib; + private unsafe FT_FaceRec_ _face; } \ No newline at end of file diff --git a/Voile/Voile.csproj b/Voile/Voile.csproj index 7f18252..908cd34 100644 --- a/Voile/Voile.csproj +++ b/Voile/Voile.csproj @@ -9,6 +9,7 @@ + @@ -16,8 +17,6 @@ - -