2024-05-12 22:51:23 +02:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using System.Drawing;
|
2024-05-12 22:35:52 +02:00
|
|
|
|
|
|
|
|
|
namespace MSDF_Test;
|
|
|
|
|
|
2024-05-12 22:51:23 +02:00
|
|
|
|
// ReSharper disable once InconsistentNaming
|
|
|
|
|
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
|
2024-05-12 22:35:52 +02:00
|
|
|
|
public class MSDF
|
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<char, Glyph> _glyphs = new();
|
|
|
|
|
|
|
|
|
|
public MSDF(string texturePath, int glyphSize, int padding)
|
|
|
|
|
{
|
2024-05-12 22:51:23 +02:00
|
|
|
|
Texture texture = new (texturePath, glyphSize, padding);
|
|
|
|
|
List<Bitmap> glyphBitmaps = texture.GetGlyphBitmaps();
|
2024-05-12 22:35:52 +02:00
|
|
|
|
for(int i = 32; i < 128; i++)
|
|
|
|
|
_glyphs.Add((char)(i+1), new Glyph(glyphBitmaps[i]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Bitmap Render(string str, int size)
|
|
|
|
|
{
|
|
|
|
|
Bitmap ret = new(size * str.Length, size);
|
2024-05-12 22:51:23 +02:00
|
|
|
|
using Graphics grD = Graphics.FromImage(ret);
|
|
|
|
|
for(int i = 0; i < str.Length; i++)
|
2024-05-12 22:35:52 +02:00
|
|
|
|
{
|
2024-05-12 22:51:23 +02:00
|
|
|
|
if (_glyphs.TryGetValue(str[i], out Glyph? glyph))
|
2024-05-12 22:35:52 +02:00
|
|
|
|
{
|
2024-05-12 22:51:23 +02:00
|
|
|
|
int destOffsetX = i * size;
|
|
|
|
|
grD.DrawImage(glyph.GetBitmap(size, Color.White, Color.Transparent), new PointF(destOffsetX, 0));
|
2024-05-12 22:35:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-12 22:51:23 +02:00
|
|
|
|
|
2024-05-12 22:35:52 +02:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|