Skip to content

Commit

Permalink
Add NRT metadata (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebude authored Jun 3, 2024
2 parents ba16519 + b15d28b commit e25c521
Show file tree
Hide file tree
Showing 33 changed files with 585 additions and 551 deletions.
3 changes: 2 additions & 1 deletion QRCoder/AbstractQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public abstract class AbstractQRCode
protected QRCodeData QrCodeData { get; set; }

protected AbstractQRCode() {
this.QrCodeData = null!;
}

protected AbstractQRCode(QRCodeData data) {
Expand All @@ -22,7 +23,7 @@ virtual public void SetQRCodeData(QRCodeData data) {
public void Dispose()
{
this.QrCodeData?.Dispose();
this.QrCodeData = null;
this.QrCodeData = null!;
}
}
}
12 changes: 5 additions & 7 deletions QRCoder/ArtQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Bitmap GetGraphic(int pixelsPerModule)
/// </summary>
/// <param name="backgroundImage">A bitmap object that will be used as background picture</param>
/// <returns>QRCode graphic as bitmap</returns>
public Bitmap GetGraphic(Bitmap backgroundImage = null)
public Bitmap GetGraphic(Bitmap? backgroundImage = null)
{
return this.GetGraphic(10, Color.Black, Color.White, Color.Transparent, backgroundImage: backgroundImage);
}
Expand All @@ -59,9 +59,9 @@ public Bitmap GetGraphic(Bitmap backgroundImage = null)
/// <param name="backgroundImageStyle">Style of the background image (if set). Fill=spanning complete graphic; DataAreaOnly=Don't paint background into quietzone</param>
/// <param name="finderPatternImage">Optional image that should be used instead of the default finder patterns</param>
/// <returns>QRCode graphic as bitmap</returns>
public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Color backgroundColor, Bitmap backgroundImage = null, double pixelSizeFactor = 1,
public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Color backgroundColor, Bitmap? backgroundImage = null, double pixelSizeFactor = 1,
bool drawQuietZones = true, QuietZoneStyle quietZoneRenderingStyle = QuietZoneStyle.Dotted,
BackgroundImageStyle backgroundImageStyle = BackgroundImageStyle.DataAreaOnly, Bitmap finderPatternImage = null)
BackgroundImageStyle backgroundImageStyle = BackgroundImageStyle.DataAreaOnly, Bitmap? finderPatternImage = null)
{
if (pixelSizeFactor > 1)
throw new Exception("The parameter pixelSize must be between 0 and 1. (0-100%)");
Expand Down Expand Up @@ -211,8 +211,6 @@ private bool IsPartOfFinderPattern(int x, int y, int numModules, int offset)
/// <returns>Resized image as bitmap</returns>
private Bitmap Resize(Bitmap image, int newSize)
{
if (image == null) return null;

float scale = Math.Min((float)newSize / image.Width, (float)newSize / image.Height);
var scaledWidth = (int)(image.Width * scale);
var scaledHeight = (int)(image.Height * scale);
Expand Down Expand Up @@ -284,9 +282,9 @@ public static class ArtQRCodeHelper
/// <param name="finderPatternImage">Optional image that should be used instead of the default finder patterns</param>
/// <returns>QRCode graphic as bitmap</returns>
public static Bitmap GetQRCode(string plainText, int pixelsPerModule, Color darkColor, Color lightColor, Color backgroundColor, ECCLevel eccLevel, bool forceUtf8 = false,
bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, Bitmap backgroundImage = null, double pixelSizeFactor = 1.0,
bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, Bitmap? backgroundImage = null, double pixelSizeFactor = 1.0,
bool drawQuietZones = true, QuietZoneStyle quietZoneRenderingStyle = QuietZoneStyle.Flat,
BackgroundImageStyle backgroundImageStyle = BackgroundImageStyle.DataAreaOnly, Bitmap finderPatternImage = null)
BackgroundImageStyle backgroundImageStyle = BackgroundImageStyle.DataAreaOnly, Bitmap? finderPatternImage = null)
{
using (var qrGenerator = new QRCodeGenerator())
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
Expand Down
27 changes: 27 additions & 0 deletions QRCoder/Attributes/NotNullWhenAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if !NETCOREAPP
namespace System.Diagnostics.CodeAnalysis
{
/// <summary>
/// Specifies that when a method returns System.Diagnostics.CodeAnalysis.NotNullWhenAttribute.ReturnValue,
/// the parameter will not be null even if the corresponding type allows it.
/// </summary>
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
internal sealed class NotNullWhenAttribute : Attribute
{
/// <summary>
/// Initializes the attribute with the specified return value condition.
/// </summary>
/// <param name="returnValue">If the method returns this value, the associated parameter will not be null.</param>
public NotNullWhenAttribute(bool returnValue)
{
ReturnValue = returnValue;
}

/// <summary>
/// Gets the return value condition. If the method returns this value, the associated
/// parameter will not be null.
/// </summary>
public bool ReturnValue { get; }
}
}
#endif
6 changes: 5 additions & 1 deletion QRCoder/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Diagnostics.CodeAnalysis;

namespace QRCoder
{
internal static class StringExtensions
Expand All @@ -8,7 +10,9 @@ internal static class StringExtensions
/// <returns>
/// <see langword="true"/> if the <paramref name="value"/> is null, empty, or white space; otherwise, <see langword="false"/>.
/// </returns>
public static bool IsNullOrWhiteSpace(this string value)
public static bool IsNullOrWhiteSpace(
[NotNullWhen(false)]
this string? value)
{
#if NET35
if (value == null) return true;
Expand Down
6 changes: 3 additions & 3 deletions QRCoder/Extensions/StringValueAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public static class CustomExtensions
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("This method uses reflection to examine the provided enum value.")]
#endif
public static string GetStringValue(this Enum value)
public static string? GetStringValue(this Enum value)
{
#if NETSTANDARD1_3
var fieldInfo = value.GetType().GetRuntimeField(value.ToString());
#else
var fieldInfo = value.GetType().GetField(value.ToString());
var fieldInfo = value.GetType().GetField(value.ToString())!;
#endif
var attr = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
return attr.Length > 0 ? attr[0].StringValue : null;
return attr!.Length > 0 ? attr[0].StringValue : null;
}
}
}
3 changes: 2 additions & 1 deletion QRCoder/PayloadGenerator/BezahlCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class BezahlCode : Payload
{
//BezahlCode specification: http://www.bezahlcode.de/wp-content/uploads/BezahlCode_TechDok.pdf

private readonly string name, iban, bic, account, bnc, sepaReference, reason, creditorId, mandateId, periodicTimeunit;
private readonly string? iban, bic, account, bnc, sepaReference, creditorId, mandateId, periodicTimeunit;
private readonly string name, reason;
private readonly decimal amount;
private readonly int postingKey, periodicTimeunitRotation;
private readonly Currency currency;
Expand Down
2 changes: 1 addition & 1 deletion QRCoder/PayloadGenerator/BitcoinAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static partial class PayloadGenerator
{
public class BitcoinAddress : BitcoinLikeCryptoCurrencyAddress
{
public BitcoinAddress(string address, double? amount, string label = null, string message = null)
public BitcoinAddress(string address, double? amount, string? label = null, string? message = null)
: base(BitcoinLikeCryptoCurrencyType.Bitcoin, address, amount, label, message) { }
}
}
Expand Down
2 changes: 1 addition & 1 deletion QRCoder/PayloadGenerator/BitcoinCashAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static partial class PayloadGenerator
{
public class BitcoinCashAddress : BitcoinLikeCryptoCurrencyAddress
{
public BitcoinCashAddress(string address, double? amount, string label = null, string message = null)
public BitcoinCashAddress(string address, double? amount, string? label = null, string? message = null)
: base(BitcoinLikeCryptoCurrencyType.BitcoinCash, address, amount, label, message) { }
}
}
Expand Down
17 changes: 9 additions & 8 deletions QRCoder/PayloadGenerator/BitcoinLikeCryptoCurrencyAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public static partial class PayloadGenerator
public class BitcoinLikeCryptoCurrencyAddress : Payload
{
private readonly BitcoinLikeCryptoCurrencyType currencyType;
private readonly string address, label, message;
private readonly string address;
private readonly string? label, message;
private readonly double? amount;

/// <summary>
Expand All @@ -21,7 +22,7 @@ public class BitcoinLikeCryptoCurrencyAddress : Payload
/// <param name="amount">Amount of coins to transfer</param>
/// <param name="label">Reference label</param>
/// <param name="message">Referece text aka message</param>
public BitcoinLikeCryptoCurrencyAddress(BitcoinLikeCryptoCurrencyType currencyType, string address, double? amount, string label = null, string message = null)
public BitcoinLikeCryptoCurrencyAddress(BitcoinLikeCryptoCurrencyType currencyType, string address, double? amount, string? label = null, string? message = null)
{
this.currencyType = currencyType;
this.address = address;
Expand All @@ -41,12 +42,12 @@ public BitcoinLikeCryptoCurrencyAddress(BitcoinLikeCryptoCurrencyType currencyTy

public override string ToString()
{
string query = null;
string? query = null;

var queryValues = new KeyValuePair<string,string>[]{
new KeyValuePair<string, string>(nameof(label), label),
new KeyValuePair<string, string>(nameof(message), message),
new KeyValuePair<string, string>(nameof(amount), amount.HasValue ? amount.Value.ToString("#.########", CultureInfo.InvariantCulture) : null)
var queryValues = new KeyValuePair<string,string?>[]{
new KeyValuePair<string, string?>(nameof(label), label),
new KeyValuePair<string, string?>(nameof(message), message),
new KeyValuePair<string, string?>(nameof(amount), amount.HasValue ? amount.Value.ToString("#.########", CultureInfo.InvariantCulture) : null)
};

if (queryValues.Any(keyPair => !string.IsNullOrEmpty(keyPair.Value)))
Expand All @@ -57,7 +58,7 @@ public override string ToString()
.ToArray());
}

return $"{Enum.GetName(typeof(BitcoinLikeCryptoCurrencyType), currencyType).ToLower()}:{address}{query}";
return $"{Enum.GetName(typeof(BitcoinLikeCryptoCurrencyType), currencyType)!.ToLower()}:{address}{query}";
}

public enum BitcoinLikeCryptoCurrencyType
Expand Down
7 changes: 4 additions & 3 deletions QRCoder/PayloadGenerator/CalendarEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public static partial class PayloadGenerator
{
public class CalendarEvent : Payload
{
private readonly string subject, description, location, start, end;
private readonly string subject, start, end;
private readonly string? description, location;
private readonly EventEncoding encoding;

/// <summary>
Expand All @@ -19,7 +20,7 @@ public class CalendarEvent : Payload
/// <param name="end">End time (incl. UTC offset) of the event</param>
/// <param name="allDayEvent">Is it a full day event?</param>
/// <param name="encoding">Type of encoding (universal or iCal)</param>
public CalendarEvent(string subject, string description, string location, DateTimeOffset start, DateTimeOffset end, bool allDayEvent, EventEncoding encoding = EventEncoding.Universal) : this(subject, description, location, start.UtcDateTime, end.UtcDateTime, allDayEvent, encoding)
public CalendarEvent(string subject, string? description, string? location, DateTimeOffset start, DateTimeOffset end, bool allDayEvent, EventEncoding encoding = EventEncoding.Universal) : this(subject, description, location, start.UtcDateTime, end.UtcDateTime, allDayEvent, encoding)
{
}

Expand All @@ -33,7 +34,7 @@ public CalendarEvent(string subject, string description, string location, DateTi
/// <param name="end">End time of the event</param>
/// <param name="allDayEvent">Is it a full day event?</param>
/// <param name="encoding">Type of encoding (universal or iCal)</param>
public CalendarEvent(string subject, string description, string location, DateTime start, DateTime end, bool allDayEvent, EventEncoding encoding = EventEncoding.Universal)
public CalendarEvent(string subject, string? description, string? location, DateTime start, DateTime end, bool allDayEvent, EventEncoding encoding = EventEncoding.Universal)
{
this.subject = subject;
this.description = description;
Expand Down
32 changes: 16 additions & 16 deletions QRCoder/PayloadGenerator/ContactData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ public class ContactData : Payload
{
private readonly string firstname;
private readonly string lastname;
private readonly string nickname;
private readonly string org;
private readonly string orgTitle;
private readonly string phone;
private readonly string mobilePhone;
private readonly string workPhone;
private readonly string email;
private readonly string? nickname;
private readonly string? org;
private readonly string? orgTitle;
private readonly string? phone;
private readonly string? mobilePhone;
private readonly string? workPhone;
private readonly string? email;
private readonly DateTime? birthday;
private readonly string website;
private readonly string street;
private readonly string houseNumber;
private readonly string city;
private readonly string zipCode;
private readonly string stateRegion;
private readonly string country;
private readonly string note;
private readonly string? website;
private readonly string? street;
private readonly string? houseNumber;
private readonly string? city;
private readonly string? zipCode;
private readonly string? stateRegion;
private readonly string? country;
private readonly string? note;
private readonly ContactOutputType outputType;
private readonly AddressOrder addressOrder;

Expand Down Expand Up @@ -51,7 +51,7 @@ public class ContactData : Payload
/// <param name="note">Memo text / notes</param>
/// <param name="org">Organisation/Company</param>
/// <param name="orgTitle">Organisation/Company Title</param>
public ContactData(ContactOutputType outputType, string firstname, string lastname, string nickname = null, string phone = null, string mobilePhone = null, string workPhone = null, string email = null, DateTime? birthday = null, string website = null, string street = null, string houseNumber = null, string city = null, string zipCode = null, string country = null, string note = null, string stateRegion = null, AddressOrder addressOrder = AddressOrder.Default, string org = null, string orgTitle = null)
public ContactData(ContactOutputType outputType, string firstname, string lastname, string? nickname = null, string? phone = null, string? mobilePhone = null, string? workPhone = null, string? email = null, DateTime? birthday = null, string? website = null, string? street = null, string? houseNumber = null, string? city = null, string? zipCode = null, string? country = null, string? note = null, string? stateRegion = null, AddressOrder addressOrder = AddressOrder.Default, string? org = null, string? orgTitle = null)
{
this.firstname = firstname;
this.lastname = lastname;
Expand Down
2 changes: 1 addition & 1 deletion QRCoder/PayloadGenerator/LitecoinAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static partial class PayloadGenerator
{
public class LitecoinAddress : BitcoinLikeCryptoCurrencyAddress
{
public LitecoinAddress(string address, double? amount, string label = null, string message = null)
public LitecoinAddress(string address, double? amount, string? label = null, string? message = null)
: base(BitcoinLikeCryptoCurrencyType.Litecoin, address, amount, label, message) { }
}
}
Expand Down
8 changes: 4 additions & 4 deletions QRCoder/PayloadGenerator/Mail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static partial class PayloadGenerator
{
public class Mail : Payload
{
private readonly string mailReceiver, subject, message;
private readonly string? mailReceiver, subject, message;
private readonly MailEncoding encoding;


Expand All @@ -19,7 +19,7 @@ public class Mail : Payload
/// <param name="subject">Subject line of the email</param>
/// <param name="message">Message content of the email</param>
/// <param name="encoding">Payload encoding type. Choose dependent on your QR Code scanner app.</param>
public Mail(string mailReceiver = null, string subject = null, string message = null, MailEncoding encoding = MailEncoding.MAILTO)
public Mail(string? mailReceiver = null, string? subject = null, string? message = null, MailEncoding encoding = MailEncoding.MAILTO)
{
this.mailReceiver = mailReceiver;
this.subject = subject;
Expand All @@ -42,10 +42,10 @@ public override string ToString()
returnVal = $"mailto:{this.mailReceiver}{queryString}";
break;
case MailEncoding.MATMSG:
returnVal = $"MATMSG:TO:{this.mailReceiver};SUB:{EscapeInput(this.subject)};BODY:{EscapeInput(this.message)};;";
returnVal = $"MATMSG:TO:{this.mailReceiver};SUB:{EscapeInput(this.subject ?? "")};BODY:{EscapeInput(this.message ?? "")};;";
break;
case MailEncoding.SMTP:
returnVal = $"SMTP:{this.mailReceiver}:{EscapeInput(this.subject, true)}:{EscapeInput(this.message, true)}";
returnVal = $"SMTP:{this.mailReceiver}:{EscapeInput(this.subject ?? "", true)}:{EscapeInput(this.message ?? "", true)}";
break;
}
return returnVal;
Expand Down
Loading

0 comments on commit e25c521

Please sign in to comment.