Optimization

Anything to do with labes drawing fonts has more optimization
This commit is contained in:
JacobTech 2023-01-02 09:59:45 -05:00
parent a88fef1f80
commit 522088227b
4 changed files with 76 additions and 149 deletions

View File

@ -10,7 +10,7 @@
<IncludeSymbols>False</IncludeSymbols>
<RepositoryUrl>https://git.jacobtech.com/JacobTech.com/GraphicsManager</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>1.0.0-alpha7</Version>
<Version>1.0.0-alpha8</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -9,10 +9,59 @@ public class Font
private static List<Face>? System = null;
private List<Face> _Faces = new();
private Library lib;
internal Font()
{
Name = null!;
Assembly = null!;
Embeded = false;
if (lib is null) lib = new();
//TODO add more systemfont dection methods
if (System is null)
{
System = new();
if (OperatingSystem.IsLinux())
{
try
{
Process proc = new()
{
StartInfo = new()
{
FileName = "/bin/bash",
Arguments = "-c \"fc-list ':' file\"",
RedirectStandardOutput = true,
}
};
proc.Start();
proc.WaitForExit();
string[] files = proc.StandardOutput.ReadToEnd().Split($": {Environment.NewLine}");
for (int i = 0; i < files.Length; i++)
{
System!.Add(new Face(lib, files[i], 0));
}
}
catch
{
}
}
}
for (int i = 0; i < System!.Count; i++) _Faces.Add(System![i]);
//TODO add a reserved backup font
}
public IReadOnlyList<Face> Faces => _Faces.AsReadOnly();
public void SetEmbeddedFont(string Font, Assembly? Assembly = null)
{
_ = Font ?? throw new ArgumentNullException(nameof(Font));
string Base = "GraphicsManager.Resources.Fonts.";
if (Assembly is not null) Base = string.Empty;
byte[] f = (Assembly is null
? Tools.GetResourceBytes(Base + Font)
: Tools.GetResourceBytes(Assembly!, $"{Base}{Font}"));
if (HasTopFont) _Faces[0] = new Face(lib, f, 0);
else _Faces.Insert(0, new Face(lib, f, 0));
HasTopFont = true;
this.Assembly = Assembly;
this.Embeded = true;
this.Name = Font;
@ -26,89 +75,14 @@ public class Font
Assembly = Assembly,
Embeded = true,
Name = Font,
HasTopFont = true
};
Library lib = new();
string Base = "GraphicsManager.Resources.Fonts.";
if (Assembly is not null) Base = string.Empty;
byte[] f = (Assembly is null
? Tools.GetResourceBytes(Base + Font)
: Tools.GetResourceBytes(Assembly!, $"{Base}{Font}"));
fontclass._Faces.Add(new Face(lib, f, 0));
if (System is null)
{
System = new();
if (OperatingSystem.IsLinux())
{
try
{
Process proc = new()
{
StartInfo = new()
{
FileName = "/bin/bash",
Arguments = "-c \"fc-list ':' file\"",
RedirectStandardOutput = true,
}
};
proc.Start();
proc.WaitForExit();
string[] files = proc.StandardOutput.ReadToEnd().Split($": {Environment.NewLine}");
for (int i = 0; i < files.Length; i++)
{
System!.Add(new Face(fontclass.lib, files[i], 0));
}
}
catch
{
}
}
}
for (int i = 0; i < System!.Count; i++) fontclass._Faces.Add(System![i]);
return fontclass;
}
public static Font MakeFontFromSystem()
{
Font fontclass = new()
{
Assembly = null,
Embeded = false,
Name = default!,
lib = new()
};
if (Font.System is null)
{
Font.System = new();
if (OperatingSystem.IsLinux())
{
try
{
Process proc = new()
{
StartInfo = new()
{
FileName = "/bin/bash",
Arguments = "-c \"fc-list ':' file\"",
RedirectStandardOutput = true,
}
};
proc.Start();
proc.WaitForExit();
string[] files = proc.StandardOutput.ReadToEnd().Split($": {Environment.NewLine}");
for (int i = 0; i < files.Length; i++)
{
Font.System!.Add(new Face(fontclass.lib, files[i], 0));
}
}
catch
{
}
}
}
for (int i = 0; i < Font.System!.Count; i++) fontclass._Faces.Add(Font.System![i]);
fontclass._Faces.Insert(0, new Face(fontclass.lib, f, 0));
return fontclass;
}
@ -120,55 +94,25 @@ public class Font
Assembly = null,
Embeded = false,
Name = Font,
HasTopFont = true
};
fontclass.lib = new();
fontclass._Faces.Add(new Face(fontclass.lib, File.ReadAllBytes(Font), 0));
if (System is null)
{
System = new();
if (OperatingSystem.IsLinux())
{
try
{
Process proc = new()
{
StartInfo = new()
{
FileName = "/bin/bash",
Arguments = "-c \"fc-list ':' file\"",
RedirectStandardOutput = true,
}
};
proc.Start();
proc.WaitForExit();
string[] files = proc.StandardOutput.ReadToEnd().Split($": {Environment.NewLine}");
for (int i = 0; i < files.Length; i++)
{
System!.Add(new Face(fontclass.lib, files[i], 0));
}
}
catch
{
}
}
}
for (int i = 0; i < System!.Count; i++) fontclass._Faces.Add(System![i]);
fontclass._Faces.Insert(0, new Face(fontclass.lib, Font, 0));
return fontclass;
}
public void SetFontFile(string Font)
{
_ = Font ?? throw new ArgumentNullException(nameof(Font));
_Faces.RemoveAt(0);
_Faces.Reverse();
_Faces.Add(new Face(lib, File.ReadAllBytes(Font), 0));
_Faces.Reverse();
if (HasTopFont) _Faces[0] = new Face(lib, Font, 0);
else _Faces.Insert(0, new Face(lib, Font, 0));
HasTopFont = true;
this.Assembly = null;
this.Embeded = false;
this.Name = Font;
}
private bool HasTopFont = false;
public uint PixelHeight { get; set; } = 20;
public string Name { get; private set; } = default!;
public bool Embeded { get; private set; }
public Assembly? Assembly { get; private set; }

View File

@ -45,7 +45,7 @@ public class Texture
{
}
internal static Texture TextureForChar(Label l, char charter, uint PixelHeight, Face[] faces)
internal static Texture TextureForChar(Font l, char charter, Face[] faces)
{
Texture t = new();
for (int i = 0; i < faces.Length; i++)
@ -54,7 +54,7 @@ public class Texture
{
if (!Label._characters.ContainsKey(l)) Label._characters.Add(l, new Dictionary<uint, Character>());
if (Label._characters[l].ContainsKey(charter)) return Label._characters[l][(ushort)charter].Texture;
faces[i].SetPixelSizes(0, PixelHeight);
faces[i].SetPixelSizes(0, l.PixelHeight);
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);

View File

@ -12,6 +12,7 @@ namespace GraphicsManager.Objects;
public class Label : IRenderObject
{
public static readonly Shader DefaultTextShader = new("Label", true);
public static readonly Font DefaultFont = new();
public IParent? Parent { get; private set; }
public ObjectAnchor Anchor { get; set; } = ObjectAnchor.Left | ObjectAnchor.Top;
private Vector2 laf = new(), saf = new();
@ -20,10 +21,10 @@ public class Label : IRenderObject
public Vector2 SizeAsFloat { get { return saf; } }
public bool Visible { get; set; } = true;
public static readonly Dictionary<Label, Dictionary<uint, Character>> _characters = new();
public static readonly Dictionary<Font, Dictionary<uint, Character>> _characters = new();
private string text = string.Empty;
public int VAO { get; set; }
public int VBO { get; set; }
public int VAO { get; private set; }
public int VBO { get; private set; }
public Vector2 DIR { get; set; } = new Vector2(1f, 0f);
public string Text
{
@ -34,22 +35,20 @@ public class Label : IRenderObject
if (Loaded)
{
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
if (!_characters.ContainsKey(this)) _characters.Add(this, new Dictionary<uint, Character>());
if (!_characters.ContainsKey(Font)) _characters.Add(Font, new Dictionary<uint, Character>());
foreach (char character in value)
{
if (_characters[this].ContainsKey(character)) continue;
var f = Texture.TextureForChar(this, character, PixelHeight, Font.Faces.ToArray());
if (_characters[Font].ContainsKey(character)) continue;
var f = Texture.TextureForChar(Font, character, Font.Faces.ToArray());
f.LoadText();
}
if (Window is not null && Window.CanControleUpdate && Loaded) Window.DrawFrame();
}
}
}
public uint PixelHeight { get; set; } = 20;
public float Scale { get; set; } = 1.0f;
public Shader Shader { get; set; } = DefaultTextShader;
public Font Font { get; set; } = Font.MakeFontFromSystem();
public Font Font { get; set; } = DefaultFont;
public float Scale { get; set; } = 1.0f;
public Vector4 Color { get; set; } = new Vector4(1, 1, 1, 1);
public Vector2i Distance { get; private set; }
private Vector2i loc_ = new();
@ -101,12 +100,12 @@ public class Label : IRenderObject
float hhh = 0f;
foreach (char c in Text)
{
if (!_characters[this].ContainsKey(c)) break;
Character ch = _characters[this][c];
if (!_characters[Font].ContainsKey(c)) break;
Character ch = _characters[Font][c];
if (c == '\n')
{
hhh += PixelHeight;
hhh += Font.PixelHeight;
char_x = 0f;
}
else
@ -138,18 +137,11 @@ public class Label : IRenderObject
public void LoadToParent(IParent window, Window win)
{
if (Loaded) return;
if (!_characters.ContainsKey(this)) _characters.Add(this, new Dictionary<uint, Character>());
if (!_characters.ContainsKey(Font)) _characters.Add(Font, new Dictionary<uint, Character>());
Parent = window;
Window = win;
Library lib = new();
//Face face = new(lib, Font.Faces.First(), 0);
//face.SetPixelSizes(0, PixelHeight);
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
//face.SelectCharmap(Encoding.Unicode);
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
float[] vquad =
{
0.0f, -1.0f, 0.0f, 0.0f,
@ -164,16 +156,7 @@ public class Label : IRenderObject
GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
GL.BufferData(BufferTarget.ArrayBuffer, 4 * 6 * 4, vquad, BufferUsageHint.StaticDraw);
VAO = GL.GenVertexArray();/*
if (!_characters.ContainsKey(this)) _characters.Add(this, new Dictionary<uint, Character>());
foreach (char character in Text)
{
if (_characters[this].ContainsKey(character) == false)
{
var f = new Texture(this, character, PixelHeight, face);
f.LoadText();
}
}*/
VAO = GL.GenVertexArray();
GL.BindVertexArray(VAO);
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 4 * 4, 0);