From 3a994a6a12fc77410ced2016c7b7493c20f679d0 Mon Sep 17 00:00:00 2001 From: Raffael Herrmann Date: Mon, 22 Apr 2024 23:29:44 +0200 Subject: [PATCH] Updated doc strings, added newline parameter and test cases --- QRCoder/ASCIIQRCode.cs | 39 +++++++++++--------- QRCoderTests/AsciiQRCodeRendererTests.cs | 46 ++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 19 deletions(-) diff --git a/QRCoder/ASCIIQRCode.cs b/QRCoder/ASCIIQRCode.cs index 8649da7b..9ac4b468 100644 --- a/QRCoder/ASCIIQRCode.cs +++ b/QRCoder/ASCIIQRCode.cs @@ -16,15 +16,18 @@ public AsciiQRCode(QRCodeData data) : base(data) { } /// - /// Returns a strings that contains the resulting QR code as ASCII chars. + /// Returns a strings that contains the resulting QR code as textual representation. /// /// Number of repeated darkColorString/whiteSpaceString per module. /// String for use as dark color modules. In case of string make sure whiteSpaceString has the same length. /// String for use as white modules (whitespace). In case of string make sure darkColorString has the same length. + /// Bool that defines if quiet zones around the QR code shall be drawn /// End of line separator. (Default: \n) /// 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)); } @@ -64,15 +67,17 @@ public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString } /// - /// 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. /// + /// Bool that defines if quiet zones around the QR code shall be drawn + /// If set to true, dark and light colors will be inverted + /// End of line separator. (Default: \n) /// - 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", @@ -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()); } } diff --git a/QRCoderTests/AsciiQRCodeRendererTests.cs b/QRCoderTests/AsciiQRCodeRendererTests.cs index 04c91182..da3daa49 100644 --- a/QRCoderTests/AsciiQRCodeRendererTests.cs +++ b/QRCoderTests/AsciiQRCodeRendererTests.cs @@ -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); }