Purge all old files.

This commit is contained in:
H. Utku Maden 2023-06-29 14:17:32 +03:00
parent 8d2afd0955
commit 72d0f02440
19 changed files with 221 additions and 316 deletions

@ -156,7 +156,7 @@ namespace Quik.OpenTK
foreach (DrawCall call in queue)
{
GL.BindTexture(GL_TEXTURE_2D, ((OpenGLTexture)call.Texture)?.TextureId ?? 0);
GL.BindTexture(GL_TEXTURE_2D, 0);
m = Matrix4.CreateOrthographicOffCenter(0, call.Bounds.Right, 0, call.Bounds.Top, 1.0f, -1.0f);
GL.UniformMatrix4(_locM4View, false, ref m.Row0.X);
GL.DrawElements(GL_TRIANGLES, call.Count, GL_UNSIGNED_INT, call.Start);

@ -1,132 +0,0 @@
using System;
using Quik.OpenGL;
using static Quik.OpenGL.GLEnum;
namespace Quik.OpenTK
{
public class OpenGLTexture : QuikTexture
{
public int TextureId { get; private set; }
private OpenGLTextureManager Manager { get; }
private int _width;
private int _height;
private bool _mipmaps;
internal OpenGLTexture(OpenGLTextureManager manager, QuikImageFormat format, QVec2 size, bool mipmaps)
{
Manager = manager;
_mipmaps = mipmaps;
_width = (int)size.X;
_height = (int)size.Y;
TextureId = GL.GenTexture();
GL.BindTexture(GL_TEXTURE_2D, TextureId);
GL.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR));
GL.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GL.TexImage2D(
GL_TEXTURE_2D,
0,
GetGlImageFormat(format),
Width, Height, 0,
GL_RGBA, GL_UNSIGNED_BYTE,
IntPtr.Zero);
}
~OpenGLTexture()
{
Dispose(false);
}
/// <inheritdoc />
public override bool Equals(QuikTexture other) =>
other is OpenGLTexture && ((OpenGLTexture)other).TextureId == TextureId;
private bool _isDisposed = false;
protected override void Dispose(bool disposing)
{
if (_isDisposed) return;
Manager?.Reclaim(this);
_isDisposed = true;
}
/// <inheritdoc />
public override int Width => _width;
/// <inheritdoc />
public override int Height => _height;
/// <inheritdoc />
public override bool Mipmaps => _mipmaps;
/// <inheritdoc />
public override void Image(IntPtr data, QuikImageFormat format, QVec2 size, int level, int alignment = 4)
{
GL.BindTexture(GL_TEXTURE_2D, TextureId);
GL.PixelStore(GL_UNPACK_ALIGNMENT, alignment);
GL.TexSubImage2D(GL_TEXTURE_2D, level, 0, 0, Width, Height, GetGlImageFormat(format), GetGlDataFormat(format), data);
}
/// <inheritdoc />
public override void SubImage(IntPtr data, QuikImageFormat format, QRectangle location, int level, int alignment = 4)
{
GL.BindTexture(GL_TEXTURE_2D, TextureId);
GL.PixelStore(GL_UNPACK_ALIGNMENT, alignment);
GL.TexSubImage2D(
GL_TEXTURE_2D,
level,
(int)location.Left,
(int)location.Bottom,
(int)location.Size.X,
(int)location.Size.Y,
GetGlImageFormat(format),
GetGlDataFormat(format),
data);
}
/// <inheritdoc />
public override void GenerateMipMaps()
{
GL.BindTexture(GL_TEXTURE_2D, TextureId);
GL.GenerateMipmap(GL_TEXTURE_2D);
}
private static GLEnum GetGlImageFormat(QuikImageFormat format)
{
switch (format)
{
case QuikImageFormat.RedF: case QuikImageFormat.RedU8:
return GL_RED;
case QuikImageFormat.RgbF: case QuikImageFormat.RgbU8:
return GL_RGB;
case QuikImageFormat.RgbaF: case QuikImageFormat.RgbaU8:
return GL_RGBA;
case QuikImageFormat.AlphaF: case QuikImageFormat.AlphaU8:
return GL_ALPHA;
default:
throw new ArgumentOutOfRangeException();
}
}
private static GLEnum GetGlDataFormat(QuikImageFormat format)
{
switch (format)
{
case QuikImageFormat.RedF:
case QuikImageFormat.RgbaF:
case QuikImageFormat.RgbF:
case QuikImageFormat.AlphaF:
return GL_FLOAT;
case QuikImageFormat.RedU8:
case QuikImageFormat.RgbaU8:
case QuikImageFormat.RgbU8:
case QuikImageFormat.AlphaU8:
return GL_UNSIGNED_BYTE;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}

@ -1,28 +0,0 @@
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4;
namespace Quik.OpenTK
{
public class OpenGLTextureManager : IQuikTextureManager
{
public QuikContext Context { get; set; }
private List<int> _reclaimList = new List<int>();
public QuikTexture CreateTexture(QVec2 size, bool mipmaps, QuikImageFormat format)
{
return new OpenGLTexture(this, format, size, mipmaps);
}
internal void Reclaim(OpenGLTexture texture)
{
_reclaimList.Add(texture.TextureId);
}
public void Clear()
{
GL.DeleteTextures(_reclaimList.Count, _reclaimList.ToArray());
_reclaimList.Clear();
}
}
}

@ -8,7 +8,7 @@ namespace Quik.Controls
public string Text { get; set; } = "Button";
public float Padding { get; set; } = 4.0f;
public QuikFont Font { get; set; }
// public QuikFont Font { get; set; }
public QuikStrokeStyle NormalStroke { get; set; }
public QuikFillStyle NormalFill { get; set; }

@ -10,7 +10,7 @@ namespace Quik.Controls
public float Padding { get; set; } = 4.0f;
public QuikFont Font { get; set; }
// public QuikFont Font { get; set; }
// protected override void OnPaint(CommandQueue draw)
// {

@ -1,4 +1,5 @@
using System;
using Quik.Media;
namespace Quik
{
@ -40,7 +41,7 @@ namespace Quik
/// <param name="size">Size of the texture data.</param>
/// <param name="level">Mip level.</param>
/// <param name="alignment">Pixel alignment. Expected to be 1, 2, 4, or 8.</param>
public abstract void Image(IntPtr data, QuikImageFormat format, QVec2 size, int level, int alignment = 4);
public abstract void Image(IntPtr data, QImageFormat format, QVec2 size, int level, int alignment = 4);
/// <summary>
/// Upload texture data.
@ -50,7 +51,7 @@ namespace Quik
/// <param name="location">Location of the data in the texture.</param>
/// <param name="level">Mip level.</param>
/// <param name="alignment">Pixel alignment. Expected to be 1, 2, 4, or 8.</param>
public abstract void SubImage(IntPtr data, QuikImageFormat format, QRectangle location, int level, int alignment = 4);
public abstract void SubImage(IntPtr data, QImageFormat format, QRectangle location, int level, int alignment = 4);
/// <summary>
/// Generate the mip maps for the texture.

@ -1,3 +1,4 @@
using Quik.Media;
namespace Quik
{
/// <summary>
@ -21,7 +22,7 @@ namespace Quik
/// All parameters are hints. If there is a situation where the texture does not
/// have mip levels, or format cannot be specified, ignore these parameters.
/// </remarks>
QuikTexture CreateTexture(QVec2 size, bool mipmaps, QuikImageFormat format);
QuikTexture CreateTexture(QVec2 size, bool mipmaps, QImageFormat format);
/// <summary>
/// A function called on context clear. (useful for discarding old textures)

48
Quik/Media/FontInfo.cs Normal file

@ -0,0 +1,48 @@
using System;
namespace Quik.Media
{
public struct FontInfo : IEquatable<FontInfo>
{
public string Family { get; }
public FontStyle Style { get; }
public float Size { get; }
public override string ToString()
{
return $"{Family} {Style} {Size}";
}
public override int GetHashCode()
{
return Family.GetHashCode() ^
(Style.GetHashCode() * 3976061) ^
(Size.GetHashCode() * 9428791);
}
public static bool operator==(FontInfo a, FontInfo b)
{
return (a.Style == b.Style) &&
(a.Size == b.Size) &&
(a.Family == a.Family);
}
public static bool operator!=(FontInfo a, FontInfo b)
{
return (a.Style != b.Style) ||
(a.Size != b.Size) ||
(a.Family != b.Family);
}
public bool Equals(FontInfo other)
{
return this == other;
}
public override bool Equals(object obj)
{
return (obj.GetType() == typeof(FontInfo)) &&
this == (FontInfo)obj;
}
}
}

14
Quik/Media/FontStyle.cs Normal file

@ -0,0 +1,14 @@
using System;
namespace Quik.Media
{
[Flags]
public enum FontStyle
{
Italic = 1 << 0,
Bold = 1 << 1,
Normal = 0,
BoldItalic = Italic | Bold,
}
}

@ -1,6 +1,8 @@
namespace Quik
using System;
namespace Quik.Media
{
public enum QuikImageFormat
public enum QImageFormat
{
RedU8,
RgbU8,

22
Quik/Media/MediaLoader.cs Normal file

@ -0,0 +1,22 @@
using System;
using System.IO;
namespace Quik.Media
{
public enum MediaHint
{
None,
Image,
Font
}
public interface MediaLoader
{
IDisposable GetMedia(object key, MediaHint hint);
}
public interface MediaLoader<T> : MediaLoader
{
IDisposable GetMedia(T uri, MediaHint hint);
}
}

33
Quik/Media/QFont.cs Normal file

@ -0,0 +1,33 @@
using System;
namespace Quik.Media
{
/// <summary>
/// Abstract class that represents a font.
/// </summary>
public abstract class QFont : IDisposable
{
public abstract FontInfo Info { get; }
public string Family => Info.Family;
public FontStyle Style => Info.Style;
public float Size => Info.Size;
public abstract bool HasRune(int rune);
public abstract QImage RenderPage(int codepage, float resolution, bool sdf);
public abstract QGlyphMetrics GetMetricsForRune(int rune);
public abstract QGlyphMetrics[] GetMetricsForPage(int codepage);
// IDisposable
private bool isDisposed = false;
private void DisposePrivate(bool disposing)
{
if (isDisposed) return;
Dispose(disposing);
isDisposed = true;
}
protected virtual void Dispose(bool disposing) { }
public void Dispose() => DisposePrivate(true);
}
}

@ -1,41 +1,41 @@
namespace Quik.Typography
namespace Quik.Media
{
/// <summary>
/// Glyph properties with metrics based on FreeType glyph metrics.
/// </summary>
public struct QuikGlyph
public struct QGlyphMetrics
{
/// <summary>
/// The code point for the character.
/// </summary>
public int Character { get; }
public int Rune { get; }
/// <summary>
/// Location of the glyph on the atlas.
/// </summary>
public QRectangle Location { get; }
/// <summary>
/// Size of the glyph in units.
/// </summary>
public QVec2 Size { get; }
/// <summary>
/// Bearing vector for horizontal layout.
/// </summary>
public QVec2 HorizontalBearing { get; }
/// <summary>
/// Bearing vector for vertical layout.
/// </summary>
public QVec2 VerticalBearing { get; }
/// <summary>
/// Advance vector for vertical and horizontal layouts.
/// </summary>
public QVec2 Advance { get; }
public QuikGlyph(
public QGlyphMetrics(
int character,
QRectangle location,
QVec2 size,
@ -43,7 +43,7 @@ namespace Quik.Typography
QVec2 verticalBearing,
QVec2 advance)
{
Character = character;
Rune = character;
Location = location;
Size = size;
HorizontalBearing = horizontalBearing;

31
Quik/Media/QImage.cs Normal file

@ -0,0 +1,31 @@
using System;
namespace Quik.Media
{
public abstract class QImage : IDisposable
{
public abstract int Width { get; }
public abstract int Height { get; }
public abstract int Depth { get; }
public virtual int MipMapLevels => 0;
public virtual bool Premultiplied => false;
public abstract IntPtr LockBits2d(QImageFormat format, int level = 0);
public abstract IntPtr LockBits3d(QImageFormat format, int level = 0);
public abstract IntPtr LockBits3d(QImageFormat format, int depth, int level = 0);
public abstract void UnlockBits();
// IDisposable
private bool isDisposed = false;
private void DisposePrivate(bool disposing)
{
if (isDisposed) return;
Dispose(disposing);
isDisposed = true;
}
protected virtual void Dispose(bool disposing) { }
public void Dispose() => DisposePrivate(true);
}
}

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Quik.Media;
using Quik.Typography;
namespace Quik
@ -126,9 +127,9 @@ namespace Quik
set => this["stroke-color"] = value;
}
public QuikFont Font
public FontInfo Font
{
get => (QuikFont)this["font"];
get => (FontInfo)this["font"];
set => this["font"] = value;
}

@ -1,22 +0,0 @@
namespace Quik.Typography
{
public interface IQuikFontManager
{
/// <summary>
/// The context owning the font manager.
/// </summary>
QuikContext Context { get; set; }
/// <summary>
/// Function called on clear.
/// </summary>
void Clear();
/// <summary>
/// Get a font object for the given font.
/// </summary>
/// <param name="fontStyle">The font style to fetch.</param>
/// <returns>The font.</returns>
QuikFont GetFont(QuikFontStyle fontStyle);
}
}

@ -1,17 +0,0 @@
namespace Quik.Typography
{
public abstract class QuikFont
{
public abstract QuikFontStyle Style { get; }
public string FontFamily => Style.Family;
public float FontSize => Style.Size;
public QuikFontStyle FontStyle => Style;
public abstract float Ascender { get; }
public abstract float Descender { get; }
public abstract bool HasCharacter(int character);
public abstract void GetCharacter(int character, out QuikTexture texture, out QuikGlyph glyph);
}
}

@ -1,50 +0,0 @@
namespace Quik.Typography
{
public class QuikFontStyle
{
public string Family { get; }
public QuikFontType Type { get; }
public float Size { get; }
public QuikFontStyle(string family, float size, QuikFontType type = QuikFontType.Normal)
{
Family = family;
Size = size;
Type = type;
}
public override int GetHashCode()
{
return
Family.GetHashCode() ^
(Type.GetHashCode() * 1303) ^
(Size.GetHashCode() * 2447);
}
public override bool Equals(object obj)
{
return
obj is QuikFontStyle other &&
other.Family == Family &&
other.Size == Size &&
other.Type == Type;
}
public static bool operator==(QuikFontStyle a, QuikFontStyle b)
{
return a.Size == b.Size && a.Type == b.Type && a.Family == b.Family;
}
public static bool operator !=(QuikFontStyle a, QuikFontStyle b)
{
return a.Size != b.Size || a.Type != b.Type || a.Family != b.Family;
}
}
public enum QuikFontType
{
Normal,
Italic,
Bold
}
}

@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Quik.Media;
namespace Quik.Typography
{
@ -15,7 +16,7 @@ namespace Quik.Typography
/// The font associated with the text block.
/// </summary>
/// <value></value>
public QuikFont Font { get; }
// public QuikFont Font { get; }
/// <summary>
/// Textual contents of the text block.
/// </summary>
@ -35,9 +36,9 @@ namespace Quik.Typography
public float Descend { get; }
public float Height => Ascend - Descend;
public HorizontalTextBlock(QuikFont font, string text, bool rtl = false)
public HorizontalTextBlock(object font, string text, bool rtl = false)
{
Font = font;
// Font = font;
Text = text;
IsRTL = rtl;
@ -47,10 +48,10 @@ namespace Quik.Typography
foreach (char chr in text)
{
font.GetCharacter(chr, out _, out QuikGlyph glyph);
width += glyph.Advance.X;
ascend = Math.Max(ascend, glyph.HorizontalBearing.Y);
descend = Math.Min(descend, glyph.HorizontalBearing.Y - glyph.Size.Y);
// font.GetCharacter(chr, out _, out QGlyphMetrics glyph);
// width += glyph.Advance.X;
// ascend = Math.Max(ascend, glyph.HorizontalBearing.Y);
// descend = Math.Min(descend, glyph.HorizontalBearing.Y - glyph.Size.Y);
}
Width = width;
@ -60,7 +61,7 @@ namespace Quik.Typography
public HorizontalTextBlock(float width)
{
Font = null;
// Font = null;
Text = string.Empty;
IsRTL = false;
Width = width;
@ -73,15 +74,15 @@ namespace Quik.Typography
/// </summary>
public struct VerticalTextBlock
{
public QuikFont Font { get; }
// public QuikFont Font { get; }
public string Text { get; }
public bool IsWhitespace => string.IsNullOrWhiteSpace(Text);
public float Width { get; }
public float Height { get; }
public VerticalTextBlock(QuikFont font, string text)
public VerticalTextBlock(object font, string text)
{
Font = font;
// Font = font;
Text = text;
float width = 0.0f;
@ -89,9 +90,9 @@ namespace Quik.Typography
foreach(char chr in text)
{
font.GetCharacter(chr, out _, out QuikGlyph glyph);
width = Math.Max(width, - glyph.VerticalBearing.X * 2);
height += glyph.Advance.Y;
// font.GetCharacter(chr, out _, out QGlyphMetrics glyph);
// width = Math.Max(width, - glyph.VerticalBearing.X * 2);
// height += glyph.Advance.Y;
}
Width = width;
@ -100,7 +101,7 @@ namespace Quik.Typography
public VerticalTextBlock(float height)
{
Font = null;
// Font = null;
Text = string.Empty;
Width = 0.0f;
Height = height;
@ -120,9 +121,9 @@ namespace Quik.Typography
public abstract void Typeset(TypesetGroup group, float width);
protected abstract void AppendBlock(QuikFont font, string text, bool rtl = false);
protected abstract void AppendBlock(object font, string text, bool rtl = false);
public void ConsumeText(QuikFont font, string text)
public void ConsumeText(object font, string text)
{
StringBuilder segment = new StringBuilder();
bool rtl = false;
@ -280,20 +281,20 @@ namespace Quik.Typography
for (int i = block.Text.Length - 1; i >= 0; i--)
{
char chr = block.Text[i];
block.Font.GetCharacter(chr, out QuikTexture texture, out QuikGlyph metrics);
group.Add(
new TypesetCharacter(
chr,
texture,
new QRectangle(
penpal.X + metrics.Advance.X,
penpal.Y + metrics.HorizontalBearing.Y,
penpal.X + metrics.HorizontalBearing.X,
penpal.Y - metrics.Size.Y + metrics.HorizontalBearing.Y),
metrics.Location
)
);
penpal.X += metrics.Advance.X;
// block.Font.GetCharacter(chr, out QuikTexture texture, out QGlyphMetrics metrics);
// group.Add(
// new TypesetCharacter(
// chr,
// texture,
// new QRectangle(
// penpal.X + metrics.Advance.X,
// penpal.Y + metrics.HorizontalBearing.Y,
// penpal.X + metrics.HorizontalBearing.X,
// penpal.Y - metrics.Size.Y + metrics.HorizontalBearing.Y),
// metrics.Location
// )
// );
// penpal.X += metrics.Advance.X;
}
}
else
@ -301,21 +302,21 @@ namespace Quik.Typography
for (int i = 0; i < block.Text.Length; i++)
{
char chr = block.Text[i];
block.Font.GetCharacter(chr, out QuikTexture texture, out QuikGlyph metrics);
group.Add(
new TypesetCharacter(
chr,
texture,
new QRectangle(
penpal.X + metrics.Advance.X,
penpal.Y + metrics.HorizontalBearing.Y,
penpal.X + metrics.HorizontalBearing.X,
penpal.Y - metrics.Size.Y + metrics.HorizontalBearing.Y),
metrics.Location
)
);
// block.Font.GetCharacter(chr, out QuikTexture texture, out QGlyphMetrics metrics);
// group.Add(
// new TypesetCharacter(
// chr,
// texture,
// new QRectangle(
// penpal.X + metrics.Advance.X,
// penpal.Y + metrics.HorizontalBearing.Y,
// penpal.X + metrics.HorizontalBearing.X,
// penpal.Y - metrics.Size.Y + metrics.HorizontalBearing.Y),
// metrics.Location
// )
// );
penpal.X += metrics.Advance.X;
// penpal.X += metrics.Advance.X;
}
}
@ -327,7 +328,7 @@ namespace Quik.Typography
pen = penpal;
}
protected override void AppendBlock(QuikFont font, string text, bool rtl = false)
protected override void AppendBlock(object font, string text, bool rtl = false)
{
Blocks.Add(new HorizontalTextBlock(font, text, rtl));
}
@ -343,7 +344,7 @@ namespace Quik.Typography
throw new NotImplementedException();
}
protected override void AppendBlock(QuikFont font, string text, bool rtl = false)
protected override void AppendBlock(object font, string text, bool rtl = false)
{
Blocks.Add(new VerticalTextBlock(font, text));
}