Skip to content

Commit

Permalink
Misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed Jun 5, 2024
1 parent 0e25239 commit d66816d
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 273 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ csharp_style_var_elsewhere = true:suggestion

# C# code style settings - Expression-bodied members
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#expression-bodied-members
csharp_style_expression_bodied_methods = when_on_single_line:suggestion
csharp_style_expression_bodied_methods = when_on_single_line:warning
csharp_style_expression_bodied_constructors = false:warning
csharp_style_expression_bodied_operators = when_on_single_line:warning
csharp_style_expression_bodied_properties = when_on_single_line:warning
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wf-verify-formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ jobs:
- name: Restore NuGet Packages
run: dotnet restore

- name: Format QRCoder
- name: Format solution
run: dotnet format --verify-no-changes --severity error
10 changes: 3 additions & 7 deletions QRCoder/Base64QRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,16 @@ public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
#endif
private string BitmapToBase64(Bitmap bmp, ImageType imgType)
{
var base64 = string.Empty;
var iFormat = imgType switch
{
ImageType.Png => ImageFormat.Png,
ImageType.Jpeg => ImageFormat.Jpeg,
ImageType.Gif => ImageFormat.Gif,
_ => ImageFormat.Png,
};
using (var memoryStream = new MemoryStream())
{
bmp.Save(memoryStream, iFormat);
base64 = Convert.ToBase64String(memoryStream.ToArray(), Base64FormattingOptions.None);
}
return base64;
using var memoryStream = new MemoryStream();
bmp.Save(memoryStream, iFormat);
return Convert.ToBase64String(memoryStream.ToArray(), Base64FormattingOptions.None);
}
#endif

Expand Down
13 changes: 5 additions & 8 deletions QRCoder/PayloadGenerator/Geolocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@ public Geolocation(string latitude, string longitude, GeolocationEncoding encodi
/// Returns a string representation of the geolocation payload.
/// </summary>
/// <returns>A string representation of the geolocation payload in the specified encoding format.</returns>
public override string ToString()
public override string ToString() => _encoding switch
{
return _encoding switch
{
GeolocationEncoding.GEO => $"geo:{_latitude},{_longitude}",
GeolocationEncoding.GoogleMaps => $"http://maps.google.com/maps?q={_latitude},{_longitude}",
_ => "geo:",
};
}
GeolocationEncoding.GEO => $"geo:{_latitude},{_longitude}",
GeolocationEncoding.GoogleMaps => $"http://maps.google.com/maps?q={_latitude},{_longitude}",
_ => "geo:",
};

/// <summary>
/// Defines the encoding types for geolocation payloads.
Expand Down
24 changes: 5 additions & 19 deletions QRCoder/PayloadGenerator/MMS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,12 @@ public MMS(string number, string subject, MMSEncoding encoding = MMSEncoding.MMS
/// Returns the MMS payload as a string.
/// </summary>
/// <returns>The MMS payload as a string.</returns>
public override string ToString()
public override string ToString() => _encoding switch
{
var returnVal = string.Empty;
switch (_encoding)
{
case MMSEncoding.MMSTO:
var queryStringMmsTo = string.Empty;
if (!string.IsNullOrEmpty(_subject))
queryStringMmsTo = $"?subject={Uri.EscapeDataString(_subject)}";
returnVal = $"mmsto:{_number}{queryStringMmsTo}";
break;
case MMSEncoding.MMS:
var queryStringMms = string.Empty;
if (!string.IsNullOrEmpty(_subject))
queryStringMms = $"?body={Uri.EscapeDataString(_subject)}";
returnVal = $"mms:{_number}{queryStringMms}";
break;
}
return returnVal;
}
MMSEncoding.MMSTO => $"mmsto:{_number}{(string.IsNullOrEmpty(_subject) ? string.Empty : $"?subject={Uri.EscapeDataString(_subject)}")}",
MMSEncoding.MMS => $"mms:{_number}{(string.IsNullOrEmpty(_subject) ? string.Empty : $"?body={Uri.EscapeDataString(_subject)}")}",
_ => string.Empty,
};

/// <summary>
/// Defines the encoding types for the MMS payload.
Expand Down
13 changes: 5 additions & 8 deletions QRCoder/PayloadGenerator/Mail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public Mail(string? mailReceiver = null, string? subject = null, string? message
/// <returns>The email payload as a string.</returns>
public override string ToString()
{
var returnVal = string.Empty;
switch (_encoding)
{
case MailEncoding.MAILTO:
Expand All @@ -46,16 +45,14 @@ public override string ToString()
if (!string.IsNullOrEmpty(_message))
parts.Add("body=" + Uri.EscapeDataString(_message));
var queryString = parts.Any() ? $"?{string.Join("&", parts.ToArray())}" : "";
returnVal = $"mailto:{_mailReceiver}{queryString}";
break;
return $"mailto:{_mailReceiver}{queryString}";
case MailEncoding.MATMSG:
returnVal = $"MATMSG:TO:{_mailReceiver};SUB:{EscapeInput(_subject ?? "")};BODY:{EscapeInput(_message ?? "")};;";
break;
return $"MATMSG:TO:{_mailReceiver};SUB:{EscapeInput(_subject ?? "")};BODY:{EscapeInput(_message ?? "")};;";
case MailEncoding.SMTP:
returnVal = $"SMTP:{_mailReceiver}:{EscapeInput(_subject ?? "", true)}:{EscapeInput(_message ?? "", true)}";
break;
return $"SMTP:{_mailReceiver}:{EscapeInput(_subject ?? "", true)}:{EscapeInput(_message ?? "", true)}";
default:
return string.Empty;
}
return returnVal;
}

/// <summary>
Expand Down
13 changes: 5 additions & 8 deletions QRCoder/PayloadGenerator/OneTimePassword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,12 @@ public enum OoneTimePasswordAuthAlgorithm
/// Returns the OTP payload as a string.
/// </summary>
/// <returns>The OTP payload as a string.</returns>
public override string ToString()
public override string ToString() => Type switch
{
return Type switch
{
OneTimePasswordAuthType.TOTP => TimeToString(),
OneTimePasswordAuthType.HOTP => HMACToString(),
_ => throw new ArgumentOutOfRangeException(),
};
}
OneTimePasswordAuthType.TOTP => TimeToString(),
OneTimePasswordAuthType.HOTP => HMACToString(),
_ => throw new ArgumentOutOfRangeException(),
};

// Note: Issuer:Label must only contain 1 : if either of the Issuer or the Label has a : then it is invalid.
// Defaults are 6 digits and 30 for Period
Expand Down
28 changes: 6 additions & 22 deletions QRCoder/PayloadGenerator/SMS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,13 @@ public SMS(string number, string subject, SMSEncoding encoding = SMSEncoding.SMS
/// Returns the SMS payload as a string.
/// </summary>
/// <returns>The SMS payload as a string.</returns>
public override string ToString()
public override string ToString() => _encoding switch
{
var returnVal = string.Empty;
switch (_encoding)
{
case SMSEncoding.SMS:
var queryString = string.Empty;
if (!string.IsNullOrEmpty(_subject))
queryString = $"?body={Uri.EscapeDataString(_subject)}";
returnVal = $"sms:{_number}{queryString}";
break;
case SMSEncoding.SMS_iOS:
var queryStringiOS = string.Empty;
if (!string.IsNullOrEmpty(_subject))
queryStringiOS = $";body={Uri.EscapeDataString(_subject)}";
returnVal = $"sms:{_number}{queryStringiOS}";
break;
case SMSEncoding.SMSTO:
returnVal = $"SMSTO:{_number}:{_subject}";
break;
}
return returnVal;
}
SMSEncoding.SMS => $"sms:{_number}{(string.IsNullOrEmpty(_subject) ? string.Empty : $"?body={Uri.EscapeDataString(_subject)}")}",
SMSEncoding.SMS_iOS => $"sms:{_number}{(string.IsNullOrEmpty(_subject) ? string.Empty : $";body={Uri.EscapeDataString(_subject)}")}",
SMSEncoding.SMSTO => $"SMSTO:{_number}:{_subject}",
_ => string.Empty,
};

/// <summary>
/// Specifies the encoding type for the SMS payload.
Expand Down
5 changes: 1 addition & 4 deletions QRCoder/PayloadGenerator/SkypeCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public SkypeCall(string skypeUsername)
/// Converts the Skype call payload to a string.
/// </summary>
/// <returns>A string representation of the Skype call payload.</returns>
public override string ToString()
{
return $"skype:{_skypeUsername}?call";
}
public override string ToString() => $"skype:{_skypeUsername}?call";
}
}
5 changes: 1 addition & 4 deletions QRCoder/PayloadGenerator/Url.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public Url(string url)
/// Returns the URL payload as a string.
/// </summary>
/// <returns>The URL payload as a string, ensuring it starts with "http://" if no protocol is specified.</returns>
public override string ToString()
{
return (!_url.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? "http://" + _url : _url);
}
public override string ToString() => !_url.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? "http://" + _url : _url;
}
}
67 changes: 18 additions & 49 deletions QRCoder/SvgQRCode.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#if !NETSTANDARD1_3
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using QRCoder.Extensions;
using static QRCoder.QRCodeGenerator;
using static QRCoder.SvgQRCode;
Expand Down Expand Up @@ -84,9 +81,7 @@ public string GetGraphic(int pixelsPerModule, string darkColorHex, string lightC
/// <param name="logo">An optional logo to be rendered on the code (either Bitmap or SVG).</param>
/// <returns>Returns the QR code graphic as an SVG string.</returns>
public string GetGraphic(Size viewBox, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo? logo = null)
{
return GetGraphic(viewBox, Color.Black, Color.White, drawQuietZones, sizingMode, logo);
}
=> GetGraphic(viewBox, Color.Black, Color.White, drawQuietZones, sizingMode, logo);

/// <summary>
/// Returns a QR code as an SVG string with custom colors and optional quiet zones and an optional logo.
Expand All @@ -99,9 +94,7 @@ public string GetGraphic(Size viewBox, bool drawQuietZones = true, SizingMode si
/// <param name="logo">An optional logo to be rendered on the code (either Bitmap or SVG).</param>
/// <returns>Returns the QR code graphic as an SVG string.</returns>
public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo? logo = null)
{
return GetGraphic(viewBox, ColorTranslator.ToHtml(Color.FromArgb(darkColor.ToArgb())), ColorTranslator.ToHtml(Color.FromArgb(lightColor.ToArgb())), drawQuietZones, sizingMode, logo);
}
=> GetGraphic(viewBox, ColorTranslator.ToHtml(Color.FromArgb(darkColor.ToArgb())), ColorTranslator.ToHtml(Color.FromArgb(lightColor.ToArgb())), drawQuietZones, sizingMode, logo);

/// <summary>
/// Returns a QR code as an SVG string with custom colors (in HEX syntax), optional quiet zones, and an optional logo.
Expand Down Expand Up @@ -220,9 +213,7 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex
}

private bool IsBlockedByLogo(double x, double y, ImageAttributes attr, double pixelPerModule)
{
return x + pixelPerModule >= attr.X && x <= attr.X + attr.Width && y + pixelPerModule >= attr.Y && y <= attr.Y + attr.Height;
}
=> x + pixelPerModule >= attr.X && x <= attr.X + attr.Width && y + pixelPerModule >= attr.Y && y <= attr.Y + attr.Height;

private ImageAttributes GetLogoAttributes(SvgLogo logo, Size viewBox)
{
Expand All @@ -247,13 +238,11 @@ private struct ImageAttributes
public double Y;
}

//Clean double values for international use/formats
//We use explicitly "G15" to avoid differences between .NET full and Core platforms
//https://stackoverflow.com/questions/64898117/tostring-has-a-different-behavior-between-net-462-and-net-core-3-1
private string CleanSvgVal(double input)
{
//Clean double values for international use/formats
//We use explicitly "G15" to avoid differences between .NET full and Core platforms
//https://stackoverflow.com/questions/64898117/tostring-has-a-different-behavior-between-net-462-and-net-core-3-1
return input.ToString("G15", System.Globalization.CultureInfo.InvariantCulture);
}
=> input.ToString("G15", System.Globalization.CultureInfo.InvariantCulture);

/// <summary>
/// Mode of sizing attribution on svg root node
Expand Down Expand Up @@ -338,52 +327,35 @@ public SvgLogo(byte[] iconRasterized, int iconSizePercent = 15, bool fillLogoBac
/// <summary>
/// Returns the raw logo's data
/// </summary>
public object GetRawLogo()
{
return _logoRaw;
}
public object GetRawLogo() => _logoRaw;

/// <summary>
/// Defines, if the logo shall be natively embedded.
/// true=native svg embedding, false=embedding via image-tag
/// </summary>
public bool IsEmbedded()
{
return _isEmbedded;
}
public bool IsEmbedded() => _isEmbedded;

/// <summary>
/// Returns the media type of the logo
/// </summary>
/// <returns></returns>
public MediaType GetMediaType()
{
return _mediaType;
}
public MediaType GetMediaType() => _mediaType;

/// <summary>
/// Returns the logo as data-uri
/// </summary>
public string GetDataUri()
{
return $"data:{GetMimeType(_mediaType)};base64,{_logoData}";
}
=> $"data:{GetMimeType(_mediaType)};base64,{_logoData}";

/// <summary>
/// Returns how much of the QR code should be covered by the logo (in percent)
/// </summary>
public int GetIconSizePercent()
{
return _iconSizePercent;
}
public int GetIconSizePercent() => _iconSizePercent;

/// <summary>
/// Returns if the background of the logo should be cleaned (no QR modules will be rendered behind the logo)
/// </summary>
public bool FillLogoBackground()
{
return _fillLogoBackground;
}
public bool FillLogoBackground() => _fillLogoBackground;

/// <summary>
/// Media types for SvgLogos
Expand All @@ -400,15 +372,12 @@ public enum MediaType : int
SVG = 1
}

private string GetMimeType(MediaType type)
private string GetMimeType(MediaType type) => type switch
{
return type switch
{
MediaType.PNG => "image/png",
MediaType.SVG => "image/svg+xml",
_ => throw new ArgumentOutOfRangeException(nameof(type)),
};
}
MediaType.PNG => "image/png",
MediaType.SVG => "image/svg+xml",
_ => throw new ArgumentOutOfRangeException(nameof(type)),
};

}
}
Expand Down
19 changes: 8 additions & 11 deletions QRCoderConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,14 @@ public QRCodeGenerator.ECCLevel GetECCLevel(string value)
#if NET6_0_WINDOWS
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public ImageFormat GetImageFormat(string value)
public ImageFormat GetImageFormat(string value) => value.ToLower() switch
{
return value.ToLower() switch
{
"jpg" => ImageFormat.Jpeg,
"jpeg" => ImageFormat.Jpeg,
"gif" => ImageFormat.Gif,
"bmp" => ImageFormat.Bmp,
"tiff" => ImageFormat.Tiff,
_ => ImageFormat.Png,
};
}
"jpg" => ImageFormat.Jpeg,
"jpeg" => ImageFormat.Jpeg,
"gif" => ImageFormat.Gif,
"bmp" => ImageFormat.Bmp,
"tiff" => ImageFormat.Tiff,
_ => ImageFormat.Png,
};
}

Loading

0 comments on commit d66816d

Please sign in to comment.