Toolkit.UI.Controls.Avalonia

This commit is contained in:
TheXamlGuy
2024-04-13 11:41:33 +01:00
parent 62a7e94e19
commit 862e7b2e34
97 changed files with 8558 additions and 0 deletions
@@ -0,0 +1,14 @@
using Gma.QrCodeNet.Encoding.Positioning.Stencils;
namespace Gma.QrCodeNet.Encoding.Positioning;
internal static class PositioningPatternBuilder
{
internal static void EmbedBasicPatterns(int version, TriStateMatrix matrix)
{
new PositionDetectionPattern(version).ApplyTo(matrix);
new DarkDotAtLeftBottom(version).ApplyTo(matrix);
new AlignmentPattern(version).ApplyTo(matrix);
new TimingPattern(version).ApplyTo(matrix);
}
}
@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Gma.QrCodeNet.Encoding.Positioning.Stencils;
internal class AlignmentPattern : PatternStencilBase
{
public AlignmentPattern(int version)
: base(version)
{
}
private static bool[,] AlignmentPatternArray { get; } =
new[,]
{
{ X, X, X, X, X },
{ X, O, O, O, X },
{ X, O, X, O, X },
{ X, O, O, O, X },
{ X, X, X, X, X }
};
public override bool[,] Stencil => AlignmentPatternArray;
// Table E.1 — Row/column coordinates of center module of Alignment Patterns
private static byte[][] AlignmentPatternCoordinatesByVersion { get; } =
new[]
{
Array.Empty<byte>(),
Array.Empty<byte>(),
new byte[] { 6, 18 },
new byte[] { 6, 22 },
new byte[] { 6, 26 },
new byte[] { 6, 30 },
new byte[] { 6, 34 },
new byte[] { 6, 22, 38 },
new byte[] { 6, 24, 42 },
new byte[] { 6, 26, 46 },
new byte[] { 6, 28, 50 },
new byte[] { 6, 30, 54 },
new byte[] { 6, 32, 58 },
new byte[] { 6, 34, 62 },
new byte[] { 6, 26, 46, 66 },
new byte[] { 6, 26, 48, 70 },
new byte[] { 6, 26, 50, 74 },
new byte[] { 6, 30, 54, 78 },
new byte[] { 6, 30, 56, 82 },
new byte[] { 6, 30, 58, 86 },
new byte[] { 6, 34, 62, 90 },
new byte[] { 6, 28, 50, 72, 94 },
new byte[] { 6, 26, 50, 74, 98 },
new byte[] { 6, 30, 54, 78, 102 },
new byte[] { 6, 28, 54, 80, 106 },
new byte[] { 6, 32, 58, 84, 110 },
new byte[] { 6, 30, 58, 86, 114 },
new byte[] { 6, 34, 62, 90, 118 },
new byte[] { 6, 26, 50, 74, 98, 122 },
new byte[] { 6, 30, 54, 78, 102, 126 },
new byte[] { 6, 26, 52, 78, 104, 130 },
new byte[] { 6, 30, 56, 82, 108, 134 },
new byte[] { 6, 34, 60, 86, 112, 138 },
new byte[] { 6, 30, 58, 86, 114, 142 },
new byte[] { 6, 34, 62, 90, 118, 146 },
new byte[] { 6, 30, 54, 78, 102, 126, 150 },
new byte[] { 6, 24, 50, 76, 102, 128, 154 },
new byte[] { 6, 28, 54, 80, 106, 132, 158 },
new byte[] { 6, 32, 58, 84, 110, 136, 162 },
new byte[] { 6, 26, 54, 82, 110, 138, 166 },
new byte[] { 6, 30, 58, 86, 114, 142, 170 }
};
public override void ApplyTo(TriStateMatrix matrix)
{
foreach (MatrixPoint coordinatePair in GetNonColidingCoordinatePairs(matrix))
{
CopyTo(matrix, coordinatePair, MatrixStatus.NoMask);
}
}
public IEnumerable<MatrixPoint> GetNonColidingCoordinatePairs(TriStateMatrix matrix)
{
return
GetAllCoordinatePairs()
.Where(point => matrix.MStatus(point.Offset(2, 2)) == MatrixStatus.None);
}
private IEnumerable<MatrixPoint> GetAllCoordinatePairs()
{
IEnumerable<byte> coordinates = GetPatternCoordinatesByVersion(Version);
foreach (byte centerX in coordinates)
{
foreach (byte centerY in coordinates)
{
MatrixPoint location = new(centerX - 2, centerY - 2);
yield return location;
}
}
}
private static IEnumerable<byte> GetPatternCoordinatesByVersion(int version)
{
return AlignmentPatternCoordinatesByVersion[version];
}
}
@@ -0,0 +1,17 @@
using System;
namespace Gma.QrCodeNet.Encoding.Positioning.Stencils;
internal class DarkDotAtLeftBottom : PatternStencilBase
{
public DarkDotAtLeftBottom(int version) : base(version)
{
}
public override bool[,] Stencil => throw new NotImplementedException();
public override void ApplyTo(TriStateMatrix matrix)
{
matrix[8, matrix.Width - 8, MatrixStatus.NoMask] = true;
}
}
@@ -0,0 +1,32 @@
using System;
namespace Gma.QrCodeNet.Encoding.Positioning.Stencils;
internal abstract class PatternStencilBase : BitMatrix
{
protected const bool O = false;
protected const bool X = true;
internal PatternStencilBase(int version)
{
Version = version;
}
public int Version { get; private set; }
public abstract bool[,] Stencil { get; }
public override int Width => Stencil.GetLength(0);
public override int Height => Stencil.GetLength(1);
public override bool[,] InternalArray => throw new NotImplementedException();
public override bool this[int i, int j]
{
get => Stencil[i, j];
set => throw new NotSupportedException();
}
public abstract void ApplyTo(TriStateMatrix matrix);
}
@@ -0,0 +1,41 @@
namespace Gma.QrCodeNet.Encoding.Positioning.Stencils;
internal class PositionDetectionPattern : PatternStencilBase
{
public PositionDetectionPattern(int version)
: base(version)
{
}
private static bool[,] PositionDetection { get; } =
new[,]
{
{ O, O, O, O, O, O, O, O, O },
{ O, X, X, X, X, X, X, X, O },
{ O, X, O, O, O, O, O, X, O },
{ O, X, O, X, X, X, O, X, O },
{ O, X, O, X, X, X, O, X, O },
{ O, X, O, X, X, X, O, X, O },
{ O, X, O, O, O, O, O, X, O },
{ O, X, X, X, X, X, X, X, O },
{ O, O, O, O, O, O, O, O, O }
};
public override bool[,] Stencil => PositionDetection;
public override void ApplyTo(TriStateMatrix matrix)
{
MatrixSize size = GetSizeOfSquareWithSeparators();
MatrixPoint leftTopCorner = new(0, 0);
CopyTo(matrix, new MatrixRectangle(new MatrixPoint(1, 1), size), leftTopCorner, MatrixStatus.NoMask);
MatrixPoint rightTopCorner = new(matrix.Width - Width + 1, 0);
CopyTo(matrix, new MatrixRectangle(new MatrixPoint(0, 1), size), rightTopCorner, MatrixStatus.NoMask);
MatrixPoint leftBottomCorner = new(0, matrix.Width - Width + 1);
CopyTo(matrix, new MatrixRectangle(new MatrixPoint(1, 0), size), leftBottomCorner, MatrixStatus.NoMask);
}
private MatrixSize GetSizeOfSquareWithSeparators() => new(Width - 1, Height - 1);
}
@@ -0,0 +1,35 @@
using System;
namespace Gma.QrCodeNet.Encoding.Positioning.Stencils;
internal class TimingPattern : PatternStencilBase
{
public TimingPattern(int version)
: base(version)
{
}
public override bool[,] Stencil => throw new NotImplementedException();
public override void ApplyTo(TriStateMatrix matrix)
{
// -8 is for skipping position detection patterns (size 7), and two horizontal/vertical
// separation patterns (size 1). Thus, 8 = 7 + 1.
for (int i = 8; i < matrix.Width - 8; ++i)
{
bool value = (sbyte)((i + 1) % 2) == 1;
// Horizontal line.
if (matrix.MStatus(6, i) == MatrixStatus.None)
{
matrix[6, i, MatrixStatus.NoMask] = value;
}
// Vertical line.
if (matrix.MStatus(i, 6) == MatrixStatus.None)
{
matrix[i, 6, MatrixStatus.NoMask] = value;
}
}
}
}