Skip to content

Commit 3a994a6

Browse files
committed
Updated doc strings, added newline parameter and test cases
1 parent a946c3c commit 3a994a6

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
lines changed

QRCoder/ASCIIQRCode.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ public AsciiQRCode(QRCodeData data) : base(data) { }
1616

1717

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

@@ -64,15 +67,17 @@ public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString
6467
}
6568

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

75-
var platte = new
80+
var palette = new
7681
{
7782
WHITE_ALL = "\u2588",
7883
WHITE_BLACK = "\u2580",
@@ -83,39 +88,39 @@ public string GetGraphicSmall(bool drawQuietZones = true)
8388
var moduleData = QrCodeData.ModuleMatrix;
8489
var lineBuilder = new StringBuilder();
8590

86-
var quietZonesModifier = (drawQuietZones ? 4 : 8);
91+
var quietZonesModifier = (drawQuietZones ? 0 : 8);
8792
var quietZonesOffset = (int)(quietZonesModifier * 0.5);
8893
var sideLength = (QrCodeData.ModuleMatrix.Count - quietZonesModifier);
8994

90-
for (var row = 0; row < sideLength; row = row + 2)
95+
for (var row = 0; row < sideLength; row += 2)
9196
{
92-
for (var col = 0; col < moduleData.Count - quietZonesModifier; col++)
97+
for (var col = 0; col < sideLength; col++)
9398
{
9499
try
95100
{
96-
var current = moduleData[col + quietZonesOffset][row + quietZonesOffset];
97-
var next = moduleData[col + quietZonesOffset][(row + 1) + quietZonesOffset];
101+
var current = moduleData[col + quietZonesOffset][row + quietZonesOffset] ^ invert;
102+
var next = moduleData[col + quietZonesOffset][(row + 1) + quietZonesOffset] ^ invert;
98103
if (current == WHITE && next == WHITE)
99-
lineBuilder.Append(platte.WHITE_ALL);
104+
lineBuilder.Append(palette.WHITE_ALL);
100105
else if (current == WHITE && next == BLACK)
101-
lineBuilder.Append(platte.WHITE_BLACK);
106+
lineBuilder.Append(palette.WHITE_BLACK);
102107
else if (current == BLACK && next == WHITE)
103-
lineBuilder.Append(platte.BLACK_WHITE);
108+
lineBuilder.Append(palette.BLACK_WHITE);
104109
else
105-
lineBuilder.Append(platte.BLACK_ALL);
110+
lineBuilder.Append(palette.BLACK_ALL);
106111
}
107112
catch (Exception)
108113
{
109114
if (drawQuietZones)
110-
lineBuilder.Append(platte.WHITE_BLACK);
115+
lineBuilder.Append(palette.WHITE_BLACK);
111116
else
112-
lineBuilder.Append(platte.BLACK_ALL);
117+
lineBuilder.Append(palette.BLACK_ALL);
113118
}
114119

115120
}
116121
lineBuilder.Append(endOfLine);
117122
}
118-
return lineBuilder.ToString();
123+
return lineBuilder.ToString().Trim(endOfLine.ToCharArray());
119124
}
120125
}
121126

QRCoderTests/AsciiQRCodeRendererTests.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,54 @@ public void can_render_ascii_qrcode()
2828
[Category("QRRenderer/AsciiQRCode")]
2929
public void can_render_small_ascii_qrcode()
3030
{
31-
var targetCode = "█████████████████████████\n██ ▄▄▄▄▄ █▀▄█ ▀█ ▄▄▄▄▄ ██\n██ █ █ █▄█ █▄█ █ █ ██\n██ █▄▄▄█ █▄▀▀▀▀█ █▄▄▄█ ██\n██▄▄▄▄▄▄▄█ █ ▀▄█▄▄▄▄▄▄▄██\n██ ▄▄ █▄ ██▀ ▄▄▄▀ ▀ ▄▀██\n██▀█▄█ █▄ ▄ ▀▄▀ █▄█▄▄███\n███▄▄▄▄█▄▄▄████▀▀ █▄█▄██\n██ ▄▄▄▄▄ █▄▄█▄▄▀ ▀ ▄█▄▄██\n██ █ █ █ ▀ █▄▀█ ██▄█▄██\n██ █▄▄▄█ █ ▀▄▀ █▄█▄ █ ▄██\n██▄▄▄▄▄▄▄█▄▄▄█████▄█▄▄▄██\n█████████████████████████\n";
31+
var targetCode = "█████████████████████████████\n█████████████████████████████\n████ ▄▄▄▄▄ █▀▄█ ▀█ ▄▄▄▄▄ ████\n████ █ █ █▄█ █▄█ █ █ ████\n████ █▄▄▄█ █▄▀▀▀▀█ █▄▄▄█ ████\n████▄▄▄▄▄▄▄█ █ ▀▄█▄▄▄▄▄▄▄████\n████ ▄▄ █▄ ██▀ ▄▄▄▀ ▀ ▄▀████\n████▀█▄█ █▄ ▄ ▀▄▀ █▄█▄▄█████\n█████▄▄▄▄█▄▄▄████▀▀ █▄█▄████\n████ ▄▄▄▄▄ █▄▄█▄▄▀ ▀ ▄█▄▄████\n████ █ █ █ ▀ █▄▀█ ██▄█▄████\n████ █▄▄▄█ █ ▀▄▀ █▄█▄ █ ▄████\n████▄▄▄▄▄▄▄█▄▄▄█████▄█▄▄▄████\n█████████████████████████████\n▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀";
3232

3333
//Create QR code
3434
var gen = new QRCodeGenerator();
3535
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
36-
var asciiCode = new AsciiQRCode(data).GetGraphicSmall();
36+
var asciiCode = new AsciiQRCode(data).GetGraphic();
37+
38+
asciiCode.ShouldBe(targetCode);
39+
}
40+
41+
[Fact]
42+
[Category("QRRenderer/AsciiQRCode")]
43+
public void can_render_small_ascii_qrcode_without_quietzones()
44+
{
45+
var targetCode = " ▄▄▄▄▄ █▀▄█ ▀█ ▄▄▄▄▄ \n █ █ █▄█ █▄█ █ █ \n █▄▄▄█ █▄▀▀▀▀█ █▄▄▄█ \n▄▄▄▄▄▄▄█ █ ▀▄█▄▄▄▄▄▄▄\n ▄▄ █▄ ██▀ ▄▄▄▀ ▀ ▄▀\n▀█▄█ █▄ ▄ ▀▄▀ █▄█▄▄█\n█▄▄▄▄█▄▄▄████▀▀ █▄█▄\n ▄▄▄▄▄ █▄▄█▄▄▀ ▀ ▄█▄▄\n █ █ █ ▀ █▄▀█ ██▄█▄\n █▄▄▄█ █ ▀▄▀ █▄█▄ █ ▄\n▄▄▄▄▄▄▄█▄▄▄█████▄█▄▄▄";
46+
47+
//Create QR code
48+
var gen = new QRCodeGenerator();
49+
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
50+
var asciiCode = new AsciiQRCode(data).GetGraphic(drawQuietZones: false);
51+
52+
asciiCode.ShouldBe(targetCode);
53+
}
54+
55+
[Fact]
56+
[Category("QRRenderer/AsciiQRCode")]
57+
public void can_render_small_ascii_qrcode_inverted()
58+
{
59+
var targetCode = " \n \n █▀▀▀▀▀█ ▄▀ █▄ █▀▀▀▀▀█ \n █ ███ █ ▀ █ ▀ █ ███ █ \n █ ▀▀▀ █ ▀▄▄▄▄ █ ▀▀▀ █ \n ▀▀▀▀▀▀▀ █ █▄▀ ▀▀▀▀▀▀▀ \n ██▀▀█ ▀█ ▄█▀▀▀▄█▄█▀▄ \n ▄ ▀ █ ▀██▀█▄▀▄█ ▀ ▀▀ \n ▀▀▀▀ ▀▀▀ ▄▄██ ▀ ▀ \n █▀▀▀▀▀█ ▀▀ ▀▀▄█▄█▀ ▀▀ \n █ ███ █ █▄█ ▀▄ █ ▀ ▀ \n █ ▀▀▀ █ █▄▀▄█ ▀ ▀█ █▀ \n ▀▀▀▀▀▀▀ ▀▀▀ ▀ ▀▀▀ \n \n ";
60+
61+
//Create QR code
62+
var gen = new QRCodeGenerator();
63+
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
64+
var asciiCode = new AsciiQRCode(data).GetGraphic(invert: true);
65+
66+
asciiCode.ShouldBe(targetCode);
67+
}
68+
69+
[Fact]
70+
[Category("QRRenderer/AsciiQRCode")]
71+
public void can_render_small_ascii_qrcode_with_custom_eol()
72+
{
73+
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▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀";
74+
75+
//Create QR code
76+
var gen = new QRCodeGenerator();
77+
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
78+
var asciiCode = new AsciiQRCode(data).GetGraphic(endOfLine: "\r\n");
3779

3880
asciiCode.ShouldBe(targetCode);
3981
}

0 commit comments

Comments
 (0)