From 08b2ef9d71f438ff7de2923673340d1f8cc3c0ea Mon Sep 17 00:00:00 2001 From: Shane32 Date: Tue, 2 Apr 2024 18:30:17 -0400 Subject: [PATCH] Update PostscriptQRCode so it runs on Linux --- QRCoder/PostscriptQRCode.cs | 9 +- QRCoderTests/PostscriptQRCodeRendererTests.cs | 91 +++++++++++++++++++ 2 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 QRCoderTests/PostscriptQRCodeRendererTests.cs diff --git a/QRCoder/PostscriptQRCode.cs b/QRCoder/PostscriptQRCode.cs index 710551d1..d0484a9b 100644 --- a/QRCoder/PostscriptQRCode.cs +++ b/QRCoder/PostscriptQRCode.cs @@ -1,4 +1,4 @@ -#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS +#if !NETSTANDARD1_3 using System; using System.Drawing; using static QRCoder.QRCodeGenerator; @@ -6,9 +6,6 @@ namespace QRCoder { -#if NET6_0_WINDOWS - [System.Runtime.Versioning.SupportedOSPlatform("windows")] -#endif public class PostscriptQRCode : AbstractQRCode, IDisposable { /// @@ -143,9 +140,6 @@ sc sc scale "; } -#if NET6_0_WINDOWS - [System.Runtime.Versioning.SupportedOSPlatform("windows")] -#endif public static class PostscriptQRCodeHelper { public static string GetQRCode(string plainText, int pointsPerModule, string darkColorHex, string lightColorHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, bool drawQuietZones = true, bool epsFormat = false) @@ -157,5 +151,4 @@ public static string GetQRCode(string plainText, int pointsPerModule, string dar } } } - #endif \ No newline at end of file diff --git a/QRCoderTests/PostscriptQRCodeRendererTests.cs b/QRCoderTests/PostscriptQRCodeRendererTests.cs new file mode 100644 index 00000000..a71db527 --- /dev/null +++ b/QRCoderTests/PostscriptQRCodeRendererTests.cs @@ -0,0 +1,91 @@ +#if !NETCOREAPP1_1 +using QRCoder; +using QRCoderTests.Helpers; +using QRCoderTests.Helpers.XUnitExtenstions; +using Shouldly; +using System; +using System.Drawing; +using System.IO; +using System.Text.RegularExpressions; +using Xunit; + +namespace QRCoderTests +{ + public class PostscriptQRCodeRendererTests + { + [Fact] + [Category("QRRenderer/PostscriptQRCode")] + public void can_render_postscript_qrcode_simple() + { + //Create QR code + var gen = new QRCodeGenerator(); + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L); + var ps = new PostscriptQRCode(data).GetGraphic(5); + + var result = HelperFunctions.StringToHash(RemoveCreationDate(ps)); + result.ShouldBe("06b90d1e64bf022a248453e5f91101a0"); + } + + [Fact] + [Category("QRRenderer/PostscriptQRCode")] + public void can_render_postscript_qrcode_eps() + { + //Create QR code + var gen = new QRCodeGenerator(); + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L); + var ps = new PostscriptQRCode(data).GetGraphic(5, true); + + var result = HelperFunctions.StringToHash(RemoveCreationDate(ps)); + result.ShouldBe("50f6152cdb0b685595d80e7888712d3b"); + } + + [Fact] + [Category("QRRenderer/PostscriptQRCode")] + public void can_render_postscript_qrcode_size() + { + //Create QR code + var gen = new QRCodeGenerator(); + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L); + var ps = new PostscriptQRCode(data).GetGraphic(new Size(33, 33)); + + var result = HelperFunctions.StringToHash(RemoveCreationDate(ps)); + result.ShouldBe("49c7faaafef312eb4b6ea1fec195e63d"); + } + + [Fact] + [Category("QRRenderer/PostscriptQRCode")] + public void can_render_postscript_qrcode_size_no_quiet_zones() + { + //Create QR code + var gen = new QRCodeGenerator(); + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L); + var ps = new PostscriptQRCode(data).GetGraphic(new Size(50, 50), false); + + var result = HelperFunctions.StringToHash(RemoveCreationDate(ps)); + result.ShouldBe("9bfa0468e125d9815a39902133a10762"); + } + + [Fact] + [Category("QRRenderer/PostscriptQRCode")] + public void can_render_postscript_qrcode_colors() + { + //Create QR code + var gen = new QRCodeGenerator(); + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L); + var ps = new PostscriptQRCode(data).GetGraphic(5, Color.Red, Color.Blue); + + var result = HelperFunctions.StringToHash(RemoveCreationDate(ps)); + result.ShouldBe("2e001d7f67a446eb1b5df32ff5321808"); + } + + private static string RemoveCreationDate(string text) + { + // Regex pattern to match lines that start with %%CreationDate: followed by any characters until the end of the line + string pattern = @"%%CreationDate:.*\r?\n?"; + + // Use Regex.Replace to remove matching lines + return Regex.Replace(text, pattern, string.Empty, RegexOptions.Multiline); + } + } +} +#endif \ No newline at end of file