From a95ddb46ad3966f4b1ebe55429b8dc332125a3fc Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Sun, 28 Jul 2024 11:16:07 +0300 Subject: [PATCH] Add some blurg test code. --- Dashboard.BlurgText/BlurgCommand.cs | 26 ++++++++ .../Dashboard.BlurgText.csproj | 1 + Dashboard.BlurgText/DashboardBlurg.cs | 63 +++++++++++++++++++ Dashboard/Media/QImage.cs | 4 +- tests/Dashboard.Demo/Dashboard.Demo.csproj | 3 +- tests/Dashboard.Demo/Program.cs | 24 ++++--- 6 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 Dashboard.BlurgText/BlurgCommand.cs create mode 100644 Dashboard.BlurgText/DashboardBlurg.cs diff --git a/Dashboard.BlurgText/BlurgCommand.cs b/Dashboard.BlurgText/BlurgCommand.cs new file mode 100644 index 0000000..1797074 --- /dev/null +++ b/Dashboard.BlurgText/BlurgCommand.cs @@ -0,0 +1,26 @@ +using BlurgText; +using Dashboard.CommandMachine; + +namespace Dashboard.BlurgText +{ + public static class BlurgCommand + { + public static void PutBlurgText(this CommandList list, DashboardBlurg blurg, BlurgResult result, QVec2 origin) + { + for (int i = 0; i < result.Count; i++) + { + BlurgRect rect = result[i]; + + QRectangle pos = new QRectangle() + { + Min = origin + new QVec2(rect.X, rect.Y), + Size = new QVec2(rect.Width, rect.Height) + }; + + QRectangle uv = new QRectangle(rect.U1, rect.V1, rect.U0, rect.V0); + + list.Image(blurg.Images[(int)rect.UserData], pos, uv); + } + } + } +} \ No newline at end of file diff --git a/Dashboard.BlurgText/Dashboard.BlurgText.csproj b/Dashboard.BlurgText/Dashboard.BlurgText.csproj index a5c231b..06c6883 100644 --- a/Dashboard.BlurgText/Dashboard.BlurgText.csproj +++ b/Dashboard.BlurgText/Dashboard.BlurgText.csproj @@ -7,6 +7,7 @@ + diff --git a/Dashboard.BlurgText/DashboardBlurg.cs b/Dashboard.BlurgText/DashboardBlurg.cs new file mode 100644 index 0000000..314b3b0 --- /dev/null +++ b/Dashboard.BlurgText/DashboardBlurg.cs @@ -0,0 +1,63 @@ + +using BlurgText; +using Dashboard.Media; +using Dashboard.Media.Color; + +namespace Dashboard.BlurgText +{ + public class DashboardBlurg : IDisposable + { + private readonly List images = new List(); + + public Blurg Blurg { get; } + + public IReadOnlyList Images => images.AsReadOnly(); + + public DashboardBlurg() + { + Blurg = new Blurg(TextureAllocationCallback, TextureUpdateCallback); + } + + private nint TextureAllocationCallback(int width, int height) + { + QImageBuffer image = new QImageBuffer(QImageFormat.RgbaU8, width, height); + images.Add(image); + return images.Count - 1; + } + + private void TextureUpdateCallback(nint userData, nint buffer, int x, int y, int width, int height) + { + if (width == 0 || height == 0) + return; + + QImageLock src = new QImageLock(QImageFormat.RgbaU8, width, height, 1, buffer); + QImageBuffer image = images[(int)userData]; + image.LockBits2d(out QImageLock dest, QImageLockOptions.Default); + + src.CopyTo(dest, x, y); + image.UnlockBits(); + } + + private bool _isDisposed = false; + private void Dispose(bool disposing) + { + if (_isDisposed) return; + + if (disposing) + { + Blurg.Dispose(); + + foreach (QImageBuffer image in images) + { + image.Dispose(); + } + + images.Clear(); + } + + _isDisposed = true; + } + + public void Dispose() => Dispose(true); + } +} \ No newline at end of file diff --git a/Dashboard/Media/QImage.cs b/Dashboard/Media/QImage.cs index f30fcaf..870fc51 100644 --- a/Dashboard/Media/QImage.cs +++ b/Dashboard/Media/QImage.cs @@ -65,8 +65,8 @@ namespace Dashboard.Media public unsafe void CopyTo(QImageLock destination, int x, int y) { if ( - Width + x > destination.Width || - Height + y > destination.Height) + Width + x >= destination.Width || + Height + y >= destination.Height) { throw new Exception("Image falls outside the bounds of the destination."); } diff --git a/tests/Dashboard.Demo/Dashboard.Demo.csproj b/tests/Dashboard.Demo/Dashboard.Demo.csproj index f2c17d1..0a06d7b 100644 --- a/tests/Dashboard.Demo/Dashboard.Demo.csproj +++ b/tests/Dashboard.Demo/Dashboard.Demo.csproj @@ -4,11 +4,12 @@ + Exe - net6.0 + net8.0 disable enable diff --git a/tests/Dashboard.Demo/Program.cs b/tests/Dashboard.Demo/Program.cs index 4d41cc8..e242111 100644 --- a/tests/Dashboard.Demo/Program.cs +++ b/tests/Dashboard.Demo/Program.cs @@ -5,6 +5,8 @@ using Dashboard.OpenTK; using Dashboard.Media.Defaults; using Dashboard.Media; using Dashboard.PAL; +using Dashboard.BlurgText; +using BlurgText; namespace Dashboard.Demo { @@ -21,24 +23,30 @@ namespace Dashboard.Demo public class EmptyView : View { private QFont? font; - private readonly Label Label = new Label() { Text = "Hello world!", Position = new QVec2(300, 300) }; + DashboardBlurg dblurg = new DashboardBlurg(); + BlurgResult? result; + // private readonly Label Label = new Label() { Text = "Hello world!", Position = new QVec2(300, 300) }; protected override void PaintBegin(CommandList cmd) { base.PaintBegin(cmd); - if (font == null) + if (result == null) { - IFontDataBase db = FontDataBaseProvider.Instance; - font = new QFontFreeType(db.FontFileInfo(db.Sans).OpenRead()); + // IFontDataBase db = FontDataBaseProvider.Instance; + // font = new QFontFreeType(db.FontFileInfo(db.Sans).OpenRead()); - Label.Font = font; - Label.TextSize = 12; - Label.InvalidateVisual(); + // Label.Font = font; + // Label.TextSize = 12; + // Label.InvalidateVisual(); + + var font = dblurg.Blurg.AddFontFile("/usr/share/fonts/google-noto/NotoSans-Regular.ttf"); + result = dblurg.Blurg.BuildString(font!, 12, BlurgColor.White, "Hello World"); } + cmd.PutBlurgText(dblurg, result!, new QVec2(300, 300)); cmd.Rectangle(new QRectangle(16, 16, 0, 0)); - Label.Paint(cmd); + // Label.Paint(cmd); } } } \ No newline at end of file