Skip to content
Merged
44 changes: 44 additions & 0 deletions QRCoder/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,48 @@ internal static byte[] HexColorToByteArray(this string colorString)
internal static string ToString(this char c, CultureInfo _)
=> c.ToString();
#endif

/// <summary>
/// Appends an integer value to the StringBuilder using invariant culture formatting.
/// </summary>
/// <param name="sb">The StringBuilder to append to.</param>
/// <param name="num">The integer value to append.</param>
internal static void AppendInvariant(this StringBuilder sb, int num)
{
#if NET6_0_OR_GREATER
sb.Append(CultureInfo.InvariantCulture, $"{num}");
#else
#if HAS_SPAN
Span<char> buffer = stackalloc char[16];
if (num.TryFormat(buffer, out int charsWritten, default, CultureInfo.InvariantCulture))
{
sb.Append(buffer.Slice(0, charsWritten));
return;
}
#endif
sb.Append(num.ToString(CultureInfo.InvariantCulture));
#endif
}

/// <summary>
/// Appends a float value to the StringBuilder using invariant culture formatting with G7 precision.
/// </summary>
/// <param name="sb">The StringBuilder to append to.</param>
/// <param name="num">The float value to append.</param>
internal static void AppendInvariant(this StringBuilder sb, float num)
{
#if NET6_0_OR_GREATER
sb.Append(CultureInfo.InvariantCulture, $"{num:G7}");
#else
#if HAS_SPAN
Span<char> buffer = stackalloc char[16];
if (num.TryFormat(buffer, out int charsWritten, "G7", CultureInfo.InvariantCulture))
{
sb.Append(buffer.Slice(0, charsWritten));
return;
}
#endif
sb.Append(num.ToString("G7", CultureInfo.InvariantCulture));
#endif
}
}
14 changes: 7 additions & 7 deletions QRCoder/PdfByteQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ private string CreatePathFromModules()

// Create a single rectangle for the entire run of dark modules
// Format: x y width height re
pathCommands.Append(ToStr(startX));
pathCommands.AppendInvariant(startX);
pathCommands.Append(' ');
pathCommands.Append(ToStr(y));
pathCommands.AppendInvariant(y);
pathCommands.Append(' ');
pathCommands.Append(ToStr(x - startX));
pathCommands.AppendInvariant(x - startX);
pathCommands.Append(" 1 re\r\n");
Comment on lines 205 to 212
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully for v2 this can all be cleaned up into an interpolated string, fully optimized for .NET Standard 2.1+, but compatible with .NET Standard 2.0 as well.

}
}
Expand Down Expand Up @@ -242,11 +242,11 @@ private static string ColorToPdfRgb(byte[] color)
private static string ToStr(int value) => value.ToString(CultureInfo.InvariantCulture);

/// <summary>
/// Converts an integer to a string using invariant culture for consistent PDF formatting.
/// Converts a float to a string using invariant culture for consistent PDF formatting.
/// </summary>
/// <param name="value">The integer value to convert.</param>
/// <returns>String representation of the integer.</returns>
private static string ToStr(float value) => value.ToString("0.######", CultureInfo.InvariantCulture);
/// <param name="value">The float value to convert.</param>
/// <returns>String representation of the float.</returns>
private static string ToStr(float value) => value.ToString("G7", CultureInfo.InvariantCulture);
}

/// <summary>
Expand Down
381 changes: 270 additions & 111 deletions QRCoder/SvgQRCode.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ namespace QRCoder
{
public SvgQRCode() { }
public SvgQRCode(QRCoder.QRCodeData data) { }
public string GetGraphic() { }
public string GetGraphic(int pixelsPerModule) { }
public string GetGraphic(System.Drawing.Size viewBox, bool drawQuietZones = true, QRCoder.SvgQRCode.SizingMode sizingMode = 0, QRCoder.SvgQRCode.SvgLogo? logo = null) { }
public string GetGraphic(System.Drawing.Size viewBox, System.Drawing.Color darkColor, System.Drawing.Color lightColor, bool drawQuietZones = true, QRCoder.SvgQRCode.SizingMode sizingMode = 0, QRCoder.SvgQRCode.SvgLogo? logo = null) { }
Expand Down
1 change: 1 addition & 0 deletions QRCoderApiTests/net60/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ namespace QRCoder
{
public SvgQRCode() { }
public SvgQRCode(QRCoder.QRCodeData data) { }
public string GetGraphic() { }
public string GetGraphic(int pixelsPerModule) { }
public string GetGraphic(System.Drawing.Size viewBox, bool drawQuietZones = true, QRCoder.SvgQRCode.SizingMode sizingMode = 0, QRCoder.SvgQRCode.SvgLogo? logo = null) { }
public string GetGraphic(System.Drawing.Size viewBox, System.Drawing.Color darkColor, System.Drawing.Color lightColor, bool drawQuietZones = true, QRCoder.SvgQRCode.SizingMode sizingMode = 0, QRCoder.SvgQRCode.SvgLogo? logo = null) { }
Expand Down
229 changes: 3 additions & 226 deletions QRCoderTests/SvgQRCodeRendererTests.can_render_svg_qrcode.approved.svg
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this savings are impressive 👍🏻

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading