Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 79 additions & 34 deletions Source/Atlas/Charset.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,89 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpMSDF.Atlas
using System.Collections;

namespace SharpMSDF.Atlas;
/// <summary>
/// Represents a set of Unicode codepoints (characters)
/// </summary>
public struct Charset
{
/// Represents a set of Unicode codepoints (characters)
public partial class Charset : IEnumerable<uint>
{
/// The set of the 95 printable ASCII characters
public readonly static Charset ASCII = CreateAsciiCharset();
private SortedSet<uint> _codepoints;

/// <summary>
/// The set of the 95 printable ASCII characters
/// </summary>
public static readonly SortedSet<uint> ASCII = CreateAsciiCharset();

private static SortedSet<uint> CreateAsciiCharset()
{
var ascii = new SortedSet<uint>();
for (uint cp = 0x20; cp < 0x7f; ++cp)
{
ascii.Add(cp);
}
return ascii;
}

public Charset()
{
_codepoints = new SortedSet<uint>();
}

public Charset(SortedSet<uint> codepoints)
{
_codepoints = codepoints ?? new SortedSet<uint>();
}

static Charset CreateAsciiCharset()
{
Charset ascii = new();
for (uint cp = 0x20; cp < 0x7f; ++cp)
ascii.Add(cp);
return ascii;
}
/// <summary>
/// Adds a codepoint
/// </summary>
public void Add(uint cp)
{
_codepoints.Add(cp);
}

/// <summary>
/// Adds a codepoint
/// </summary>
public void Add(uint cp) => _Codepoints.Add(cp);
/// <summary>
/// Removes a codepoint
/// </summary>
public void Remove(uint cp) => _Codepoints.Remove(cp);
/// <summary>
/// Removes a codepoint
/// </summary>
public void Remove(uint cp)
{
_codepoints.Remove(cp);
}

public int Size() => _Codepoints.Count;
public bool Empty() => _Codepoints.Count == 0;
public int Size()
{
return _codepoints.Count;
}

IEnumerator<uint> IEnumerable<uint>.GetEnumerator() => _Codepoints.GetEnumerator();
public bool Empty()
{
return _codepoints.Count == 0;
}

IEnumerator IEnumerable.GetEnumerator() => _Codepoints.GetEnumerator();
public SortedSet<uint>.Enumerator GetEnumerator()
{
return _codepoints.GetEnumerator();
}

private SortedSet<uint> _Codepoints = [];
public SortedSet<uint> GetCodepoints()
{
return _codepoints;
}

};
// Implicit conversion from ReadOnlySpan<char> to Charset
public static implicit operator Charset(ReadOnlySpan<char> chars)
{
var charset = new Charset();
foreach (char ch in chars)
charset.Add(ch);
return charset;
}

// Implicit conversion from ReadOnlySpan<uint> to Charset
public static implicit operator Charset(ReadOnlySpan<uint> codepoints)
{
var charset = new Charset();
foreach (uint cp in codepoints)
charset.Add(cp);
return charset;
}
}
Loading