Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PostscriptQRCode so it runs on Linux #493

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions QRCoder/PostscriptQRCode.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
#if !NETSTANDARD1_3
using System;
using System.Drawing;
using static QRCoder.QRCodeGenerator;

namespace QRCoder
{

#if NET6_0_WINDOWS
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public class PostscriptQRCode : AbstractQRCode, IDisposable
{
/// <summary>
Expand Down Expand Up @@ -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)
Expand All @@ -157,5 +151,4 @@ public static string GetQRCode(string plainText, int pointsPerModule, string dar
}
}
}

#endif
91 changes: 91 additions & 0 deletions QRCoderTests/PostscriptQRCodeRendererTests.cs
Original file line number Diff line number Diff line change
@@ -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
Loading