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> <IncludeSymbols>False</IncludeSymbols>
<RepositoryUrl>https://git.jacobtech.com/JacobTech.com/GraphicsManager</RepositoryUrl> <RepositoryUrl>https://git.jacobtech.com/JacobTech.com/GraphicsManager</RepositoryUrl>
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
<Version>1.0.0-alpha7</Version> <Version>1.0.0-alpha8</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -9,10 +9,59 @@ public class Font
private static List<Face>? System = null; private static List<Face>? System = null;
private List<Face> _Faces = new(); private List<Face> _Faces = new();
private Library lib; 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 IReadOnlyList<Face> Faces => _Faces.AsReadOnly();
public void SetEmbeddedFont(string Font, Assembly? Assembly = null) public void SetEmbeddedFont(string Font, Assembly? Assembly = null)
{ {
_ = Font ?? throw new ArgumentNullException(nameof(Font)); _ = 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.Assembly = Assembly;
this.Embeded = true; this.Embeded = true;
this.Name = Font; this.Name = Font;
@ -26,89 +75,14 @@ public class Font
Assembly = Assembly, Assembly = Assembly,
Embeded = true, Embeded = true,
Name = Font, Name = Font,
HasTopFont = true
}; };
Library lib = new();
string Base = "GraphicsManager.Resources.Fonts."; string Base = "GraphicsManager.Resources.Fonts.";
if (Assembly is not null) Base = string.Empty; if (Assembly is not null) Base = string.Empty;
byte[] f = (Assembly is null byte[] f = (Assembly is null
? Tools.GetResourceBytes(Base + Font) ? Tools.GetResourceBytes(Base + Font)
: Tools.GetResourceBytes(Assembly!, $"{Base}{Font}")); : Tools.GetResourceBytes(Assembly!, $"{Base}{Font}"));
fontclass._Faces.Add(new Face(lib, f, 0)); fontclass._Faces.Insert(0, new Face(fontclass.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]);
return fontclass; return fontclass;
} }
@ -120,55 +94,25 @@ public class Font
Assembly = null, Assembly = null,
Embeded = false, Embeded = false,
Name = Font, Name = Font,
HasTopFont = true
}; };
fontclass.lib = new(); fontclass._Faces.Insert(0, new Face(fontclass.lib, Font, 0));
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]);
return fontclass; return fontclass;
} }
public void SetFontFile(string Font) public void SetFontFile(string Font)
{ {
_ = Font ?? throw new ArgumentNullException(nameof(Font)); _ = Font ?? throw new ArgumentNullException(nameof(Font));
_Faces.RemoveAt(0); if (HasTopFont) _Faces[0] = new Face(lib, Font, 0);
_Faces.Reverse(); else _Faces.Insert(0, new Face(lib, Font, 0));
_Faces.Add(new Face(lib, File.ReadAllBytes(Font), 0)); HasTopFont = true;
_Faces.Reverse();
this.Assembly = null; this.Assembly = null;
this.Embeded = false; this.Embeded = false;
this.Name = Font; this.Name = Font;
} }
private bool HasTopFont = false;
public uint PixelHeight { get; set; } = 20;
public string Name { get; private set; } = default!; public string Name { get; private set; } = default!;
public bool Embeded { get; private set; } public bool Embeded { get; private set; }
public Assembly? Assembly { 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(); Texture t = new();
for (int i = 0; i < faces.Length; i++) 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.ContainsKey(l)) Label._characters.Add(l, new Dictionary<uint, Character>());
if (Label._characters[l].ContainsKey(charter)) return Label._characters[l][(ushort)charter].Texture; 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); GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);

View File

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