Made font rendering easier.

This commit is contained in:
H. Utku Maden 2024-05-27 21:49:25 +03:00
parent 19cc765955
commit 29829fd299
6 changed files with 116 additions and 14 deletions

@ -0,0 +1,24 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Quik.PAL;
namespace Quik.Media.Defaults
{
public class FreeTypeFontFactory : IFontFactory
{
public bool TryOpen(Stream stream, [NotNullWhen(true)] out QFont font)
{
try
{
font = new QFontFreeType(stream);
return true;
}
catch
{
font = null;
return false;
}
}
}
}

@ -112,6 +112,8 @@ namespace Quik.OpenGL
GL.Enable(GL_BLEND);
GL.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GL.Enable(GL_SCISSOR_TEST);
GL.Enable(GL_DEPTH_TEST);
GL.DepthFunc(GL_LESS);
foreach (DrawCall call in queue)
{
@ -151,6 +153,10 @@ namespace Quik.OpenGL
GL.DrawElements(GL_TRIANGLES, call.Count, GL_UNSIGNED_INT, sizeof(int)*call.Start);
}
GL.Disable(GL_SCISSOR_TEST);
GL.Disable(GL_DEPTH_TEST);
GL.Disable(GL_BLEND);
}
public void ClearDrawQueue(DrawQueue queue)

@ -81,9 +81,13 @@ namespace Quik.OpenGL
GL_TRIANGLES = 0x0004,
GL_SCISSOR_TEST = 0x0C11,
GL_DEPTH_TEST = 0x0B71,
GL_TEXTURE_SWIZZLE_R = 0x8E42,
GL_TEXTURE_SWIZZLE_G = 0x8E43,
GL_TEXTURE_SWIZZLE_B = 0x8E44,
GL_TEXTURE_SWIZZLE_A = 0x8E45
GL_TEXTURE_SWIZZLE_A = 0x8E45,
GL_LESS = 0x0201,
}
}

12
Quik/PAL/IFontFactory.cs Normal file

@ -0,0 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Quik.Media;
namespace Quik.PAL
{
public interface IFontFactory
{
bool TryOpen(Stream stream, [NotNullWhen(true)] out QFont font);
}
}

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Quik.CommandMachine;
using Quik.Controls;
using Quik.Media;
@ -85,22 +86,41 @@ namespace Quik
return disposable;
}
public void Run(View mainView)
private CommandList cmd = new CommandList();
public void Run(View mainView, bool yield = true)
{
Init(mainView);
while (RunSync())
{
if (yield)
{
Thread.Yield();
}
}
}
public void Init(View mainView)
{
MainPort = new QuikPort(Platform) { UIElement = mainView };
CommandList cmd = new CommandList();
MainPort.EventRaised += (sender, ea) => mainView.NotifyEvent(sender, ea);
}
while (MainPort.IsValid)
public bool RunSync()
{
if (!MainPort.IsValid)
return false;
Platform.ProcessEvents(false);
if (MainPort.IsValid)
{
cmd.Clear();
MainPort.Paint(cmd);
}
}
return true;
}
public static QuikApplication Current { get; private set; }

@ -1,8 +1,10 @@
using Quik.Media;
using Quik.Media.Font;
using Quik.PAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Reflection;
namespace Quik.Typography
{
@ -14,6 +16,8 @@ namespace Quik.Typography
private Dictionary<FontFace, QFont> Fonts { get; } = new Dictionary<FontFace, QFont>();
private HashSet<QFont> UsedFonts { get; } = new HashSet<QFont>();
public readonly FontRasterizerOptions RasterizerOptions;
public IFontDataBase Database { get; set; }
public IFontFactory FontFactory { get; set; }
private readonly QuikApplication App;
public QFont this[FontFace info]
@ -22,8 +26,15 @@ namespace Quik.Typography
{
if (!Fonts.TryGetValue(info, out QFont font))
{
font = (QFont)App.GetMedia(info, MediaHint.Font);
Fonts[info] = font;
using Stream str = Database?.Open(info);
if (FontFactory.TryOpen(str, out font))
{
Fonts.Add(info, font);
}
else
{
throw new Exception("Font not found.");
}
}
UsedFonts.Add(font);
@ -31,10 +42,35 @@ namespace Quik.Typography
}
}
public QFont this[SystemFontFamily family]
{
get
{
return this[Database.GetSystemFontFace(family)];
}
}
public FontProvider(QuikApplication app, in FontRasterizerOptions options)
{
RasterizerOptions = options;
App = app;
Type fdb = Type.GetType("Quik.Media.Defaults.FontDataBaseProvider, Quik.Media.Defaults");
if (fdb != null)
{
PropertyInfo instanceProperty = fdb.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty);
if (instanceProperty != null)
{
Database = (IFontDataBase)instanceProperty.GetValue(null);
}
}
Type ffact = Type.GetType("Quik.Media.Defaults.FreeTypeFontFactory, Quik.Media.Defaults");
if (ffact != null)
{
ConstructorInfo ctor = ffact.GetConstructor(Array.Empty<Type>());
FontFactory = (IFontFactory)ctor.Invoke(null);
}
}
public FontProvider(QuikApplication app)
: this(app, FontRasterizerOptions.Default)