using Quik.Media;
using Quik.Media.Font;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Quik.Typography
{
///
/// The font provider is a caching object that provides fonts for typesetting classes.
///
public class FontProvider : IDisposable
{
private Dictionary Fonts { get; } = new Dictionary();
private HashSet UsedFonts { get; } = new HashSet();
public readonly FontRasterizerOptions RasterizerOptions;
private readonly QuikApplication App;
public QFont this[FontFace info]
{
get
{
if (!Fonts.TryGetValue(info, out QFont font))
{
font = (QFont)App.GetMedia(info, MediaHint.Font);
Fonts[info] = font;
}
UsedFonts.Add(font);
return font;
}
}
public FontProvider(QuikApplication app, in FontRasterizerOptions options)
{
RasterizerOptions = options;
App = app;
}
public FontProvider(QuikApplication app)
: this(app, FontRasterizerOptions.Default)
{
}
///
/// Tracks the use of fonts used by this typesetter and removes any that haven't been referenced since the last cycle.
///
public void Collect()
{
// foreach (FontJar jar in Fonts.Values.ToArray())
// {
// if (!UsedFonts.Contains(jar))
// {
// Fonts.Remove(jar.Info);
// }
// }
// UsedFonts.Clear();
}
private bool isDisposed = false;
public void Dispose()
{
if (isDisposed) return;
isDisposed = true;
foreach (QFont font in Fonts.Values)
{
font.Dispose();
}
}
}
}