Skip to content

Commit

Permalink
Updated doc strings, added newline parameter and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
codebude committed Apr 22, 2024
1 parent a946c3c commit 3a994a6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
39 changes: 22 additions & 17 deletions QRCoder/ASCIIQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ public AsciiQRCode(QRCodeData data) : base(data) { }


/// <summary>
/// Returns a strings that contains the resulting QR code as ASCII chars.
/// Returns a strings that contains the resulting QR code as textual representation.
/// </summary>
/// <param name="repeatPerModule">Number of repeated darkColorString/whiteSpaceString per module.</param>
/// <param name="darkColorString">String for use as dark color modules. In case of string make sure whiteSpaceString has the same length.</param>
/// <param name="whiteSpaceString">String for use as white modules (whitespace). In case of string make sure darkColorString has the same length.</param>
/// <param name="drawQuietZones">Bool that defines if quiet zones around the QR code shall be drawn</param>
/// <param name="endOfLine">End of line separator. (Default: \n)</param>
/// <returns></returns>
public string GetGraphic(int repeatPerModule, string darkColorString = "██", string whiteSpaceString = " ", bool drawQuietZones = true, string endOfLine = "\n")
{
if (repeatPerModule < 1)
throw new Exception("The repeatPerModule-parameter must be 1 or greater.");
return string.Join(endOfLine, GetLineByLineGraphic(repeatPerModule, darkColorString, whiteSpaceString, drawQuietZones));
}

Expand Down Expand Up @@ -64,15 +67,17 @@ public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString
}

/// <summary>
/// Returns a strings that contains the resulting QR code as ASCII chars.
/// Returns a strings that contains the resulting QR code as minified textual representation.
/// </summary>
/// <param name="drawQuietZones">Bool that defines if quiet zones around the QR code shall be drawn</param>
/// <param name="invert">If set to true, dark and light colors will be inverted</param>
/// <param name="endOfLine">End of line separator. (Default: \n)</param>
/// <returns></returns>
public string GetGraphicSmall(bool drawQuietZones = true)
public string GetGraphic(bool drawQuietZones = true, bool invert = false, string endOfLine = "\n")
{
string endOfLine = "\n";
bool BLACK = true, WHITE = false;

var platte = new
var palette = new
{
WHITE_ALL = "\u2588",
WHITE_BLACK = "\u2580",
Expand All @@ -83,39 +88,39 @@ public string GetGraphicSmall(bool drawQuietZones = true)
var moduleData = QrCodeData.ModuleMatrix;
var lineBuilder = new StringBuilder();

var quietZonesModifier = (drawQuietZones ? 4 : 8);
var quietZonesModifier = (drawQuietZones ? 0 : 8);
var quietZonesOffset = (int)(quietZonesModifier * 0.5);
var sideLength = (QrCodeData.ModuleMatrix.Count - quietZonesModifier);

for (var row = 0; row < sideLength; row = row + 2)
for (var row = 0; row < sideLength; row += 2)
{
for (var col = 0; col < moduleData.Count - quietZonesModifier; col++)
for (var col = 0; col < sideLength; col++)
{
try
{
var current = moduleData[col + quietZonesOffset][row + quietZonesOffset];
var next = moduleData[col + quietZonesOffset][(row + 1) + quietZonesOffset];
var current = moduleData[col + quietZonesOffset][row + quietZonesOffset] ^ invert;
var next = moduleData[col + quietZonesOffset][(row + 1) + quietZonesOffset] ^ invert;
if (current == WHITE && next == WHITE)
lineBuilder.Append(platte.WHITE_ALL);
lineBuilder.Append(palette.WHITE_ALL);
else if (current == WHITE && next == BLACK)
lineBuilder.Append(platte.WHITE_BLACK);
lineBuilder.Append(palette.WHITE_BLACK);
else if (current == BLACK && next == WHITE)
lineBuilder.Append(platte.BLACK_WHITE);
lineBuilder.Append(palette.BLACK_WHITE);
else
lineBuilder.Append(platte.BLACK_ALL);
lineBuilder.Append(palette.BLACK_ALL);
}
catch (Exception)
{
if (drawQuietZones)
lineBuilder.Append(platte.WHITE_BLACK);
lineBuilder.Append(palette.WHITE_BLACK);
else
lineBuilder.Append(platte.BLACK_ALL);
lineBuilder.Append(palette.BLACK_ALL);
}

}
lineBuilder.Append(endOfLine);
}
return lineBuilder.ToString();
return lineBuilder.ToString().Trim(endOfLine.ToCharArray());
}
}

Expand Down
46 changes: 44 additions & 2 deletions QRCoderTests/AsciiQRCodeRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,54 @@ public void can_render_ascii_qrcode()
[Category("QRRenderer/AsciiQRCode")]
public void can_render_small_ascii_qrcode()
{
var targetCode = "█████████████████████████\n██ ▄▄▄▄▄ █▀▄█ ▀█ ▄▄▄▄▄ ██\n██ █ █ █▄█ █▄█ █ █ ██\n██ █▄▄▄█ █▄▀▀▀▀█ █▄▄▄█ ██\n██▄▄▄▄▄▄▄█ █ ▀▄█▄▄▄▄▄▄▄██\n██ ▄▄ █▄ ██▀ ▄▄▄▀ ▀ ▄▀██\n██▀█▄█ █▄ ▄ ▀▄▀ █▄█▄▄███\n███▄▄▄▄█▄▄▄████▀▀ █▄█▄██\n██ ▄▄▄▄▄ █▄▄█▄▄▀ ▀ ▄█▄▄██\n██ █ █ █ ▀ █▄▀█ ██▄█▄██\n██ █▄▄▄█ █ ▀▄▀ █▄█▄ █ ▄██\n██▄▄▄▄▄▄▄█▄▄▄█████▄█▄▄▄██\n█████████████████████████\n";
var targetCode = "█████████████████████████████\n█████████████████████████████\n████ ▄▄▄▄▄ █▀▄█ ▀█ ▄▄▄▄▄ ████\n████ █ █ █▄█ █▄█ █ █ ████\n████ █▄▄▄█ █▄▀▀▀▀█ █▄▄▄█ ████\n████▄▄▄▄▄▄▄█ █ ▀▄█▄▄▄▄▄▄▄████\n████ ▄▄ █▄ ██▀ ▄▄▄▀ ▀ ▄▀████\n████▀█▄█ █▄ ▄ ▀▄▀ █▄█▄▄█████\n█████▄▄▄▄█▄▄▄████▀▀ █▄█▄████\n████ ▄▄▄▄▄ █▄▄█▄▄▀ ▀ ▄█▄▄████\n████ █ █ █ ▀ █▄▀█ ██▄█▄████\n████ █▄▄▄█ █ ▀▄▀ █▄█▄ █ ▄████\n████▄▄▄▄▄▄▄█▄▄▄█████▄█▄▄▄████\n█████████████████████████████\n▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀";

//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
var asciiCode = new AsciiQRCode(data).GetGraphicSmall();
var asciiCode = new AsciiQRCode(data).GetGraphic();

asciiCode.ShouldBe(targetCode);
}

[Fact]
[Category("QRRenderer/AsciiQRCode")]
public void can_render_small_ascii_qrcode_without_quietzones()
{
var targetCode = " ▄▄▄▄▄ █▀▄█ ▀█ ▄▄▄▄▄ \n █ █ █▄█ █▄█ █ █ \n █▄▄▄█ █▄▀▀▀▀█ █▄▄▄█ \n▄▄▄▄▄▄▄█ █ ▀▄█▄▄▄▄▄▄▄\n ▄▄ █▄ ██▀ ▄▄▄▀ ▀ ▄▀\n▀█▄█ █▄ ▄ ▀▄▀ █▄█▄▄█\n█▄▄▄▄█▄▄▄████▀▀ █▄█▄\n ▄▄▄▄▄ █▄▄█▄▄▀ ▀ ▄█▄▄\n █ █ █ ▀ █▄▀█ ██▄█▄\n █▄▄▄█ █ ▀▄▀ █▄█▄ █ ▄\n▄▄▄▄▄▄▄█▄▄▄█████▄█▄▄▄";

//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
var asciiCode = new AsciiQRCode(data).GetGraphic(drawQuietZones: false);

asciiCode.ShouldBe(targetCode);
}

[Fact]
[Category("QRRenderer/AsciiQRCode")]
public void can_render_small_ascii_qrcode_inverted()
{
var targetCode = " \n \n █▀▀▀▀▀█ ▄▀ █▄ █▀▀▀▀▀█ \n █ ███ █ ▀ █ ▀ █ ███ █ \n █ ▀▀▀ █ ▀▄▄▄▄ █ ▀▀▀ █ \n ▀▀▀▀▀▀▀ █ █▄▀ ▀▀▀▀▀▀▀ \n ██▀▀█ ▀█ ▄█▀▀▀▄█▄█▀▄ \n ▄ ▀ █ ▀██▀█▄▀▄█ ▀ ▀▀ \n ▀▀▀▀ ▀▀▀ ▄▄██ ▀ ▀ \n █▀▀▀▀▀█ ▀▀ ▀▀▄█▄█▀ ▀▀ \n █ ███ █ █▄█ ▀▄ █ ▀ ▀ \n █ ▀▀▀ █ █▄▀▄█ ▀ ▀█ █▀ \n ▀▀▀▀▀▀▀ ▀▀▀ ▀ ▀▀▀ \n \n ";

//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
var asciiCode = new AsciiQRCode(data).GetGraphic(invert: true);

asciiCode.ShouldBe(targetCode);
}

[Fact]
[Category("QRRenderer/AsciiQRCode")]
public void can_render_small_ascii_qrcode_with_custom_eol()
{
var targetCode = "█████████████████████████████\r\n█████████████████████████████\r\n████ ▄▄▄▄▄ █▀▄█ ▀█ ▄▄▄▄▄ ████\r\n████ █ █ █▄█ █▄█ █ █ ████\r\n████ █▄▄▄█ █▄▀▀▀▀█ █▄▄▄█ ████\r\n████▄▄▄▄▄▄▄█ █ ▀▄█▄▄▄▄▄▄▄████\r\n████ ▄▄ █▄ ██▀ ▄▄▄▀ ▀ ▄▀████\r\n████▀█▄█ █▄ ▄ ▀▄▀ █▄█▄▄█████\r\n█████▄▄▄▄█▄▄▄████▀▀ █▄█▄████\r\n████ ▄▄▄▄▄ █▄▄█▄▄▀ ▀ ▄█▄▄████\r\n████ █ █ █ ▀ █▄▀█ ██▄█▄████\r\n████ █▄▄▄█ █ ▀▄▀ █▄█▄ █ ▄████\r\n████▄▄▄▄▄▄▄█▄▄▄█████▄█▄▄▄████\r\n█████████████████████████████\r\n▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀";

//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
var asciiCode = new AsciiQRCode(data).GetGraphic(endOfLine: "\r\n");

asciiCode.ShouldBe(targetCode);
}
Expand Down

0 comments on commit 3a994a6

Please sign in to comment.